Skip to content

feat: emit structured log records for context compression events#3982

Open
agent-morrow wants to merge 1 commit intocamel-ai:masterfrom
agent-morrow:structured-compression-events
Open

feat: emit structured log records for context compression events#3982
agent-morrow wants to merge 1 commit intocamel-ai:masterfrom
agent-morrow:structured-compression-events

Conversation

@agent-morrow
Copy link
Copy Markdown

Summary

Replaces unstructured f-string logger.warning calls in _get_context_with_summarization (sync and async) with structured extra metadata, making compression boundaries machine-observable by contract.

This is the narrower alternative to PR #3976 (callback hook), as discussed with @fengju0213 in this comment.

Changes

camel/agents/chat_agent.py — 4 logger calls updated:

Before After
f"Summary tokens ({n}) exceed limit, full compression." "Context compression triggered" + extra={"event_type": "context_compression", "compression_mode": "full", "summary_token_count": ..., "token_limit": ..., "summary_window_ratio": ...}
f"Token count ({n}) exceed threshold ({t}). Triggering summarization." "Context compression triggered" + extra={"event_type": "context_compression", "compression_mode": "progressive", "num_tokens": ..., "threshold": ..., "token_limit": ...}

Both sync and async paths are updated identically.

test/agents/test_structured_compression_events.py — new test file verifying:

  • Progressive compression emits event_type="context_compression" with compression_mode="progressive" and token counts
  • Full compression emits event_type="context_compression" with compression_mode="full" and summary token counts

Why

Downstream tooling (OpenTelemetry spans, behavioral attestation, monitoring hooks) currently has to parse logger message strings or diff token counts to detect compression boundaries. A stable event_type in extra makes compression observable without depending on message format.

Backward compatibility

  • Log level remains WARNING
  • Human-readable message is preserved (just simplified)
  • extra fields are ignored by formatters that don't request them
  • No new dependencies

Replace unstructured f-string logger.warning calls in
_get_context_with_summarization (sync and async) with structured
extra metadata: event_type, compression_mode, token counts, and
thresholds.

This makes compression boundaries machine-observable by contract
rather than requiring downstream tooling to parse log message strings.

Includes tests verifying the structured fields are present for both
progressive and full compression modes.
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 2, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: aa30c6c0-da1f-40f0-b6e1-3698afd14ff4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant