Add MicrocompactMiddleware: clear older tool-result bodies#9
Conversation
Adds a built-in `MicrocompactMiddleware` alongside `ContextCompressionMiddleware` and `MessageTrimMiddleware`. In `before_model` it blanks the bodies of older tool-result messages (keeping the newest `keep_recent` verbatim and preserving each `tool_call_id`), bounding the cost of long, tool-heavy threads without the semantic loss of summarization or dropping any chat turn. The pass is idempotent and the placeholder text is caller-supplied so hosts keep their model-facing wording stable. Event emission (`AgentEvent::Compressed`) is opt-in via `with_events` and off by default, so it can be a silent transcript rewrite. Extracted from the OpenHuman in-house microcompact middleware; the ported tests form the byte-for-byte behavior contract. Claude-Session: https://claude.ai/code/session_01Bgv25hEkC6nP3DYdat91PV
`cargo clippy --all-targets -- -D warnings` flagged `&"x".repeat(400)` as `needless_borrows_for_generic_args` (Message::tool takes impl Into<String>). Pass the owned String directly. Claude-Session: https://claude.ai/code/session_01Bgv25hEkC6nP3DYdat91PV
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 8 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
Comment |
Summary
Adds a built-in
MicrocompactMiddlewarealongsideContextCompressionMiddlewareandMessageTrimMiddleware.In
before_modelit clears the bodies of older tool-result messages while keeping thekeep_recentmost recent verbatim (preserving eachtool_call_id), bounding the cost of a long, tool-heavy thread without the semantic loss of summarization and without dropping any chat turn. It composes cleanly withContextCompressionMiddleware(which summarizes older chat history near the context window) — this one only ever blanks stale tool payloads.before_modelpasses converge.keep_recenttool results.MicrocompactMiddleware::new(keep_recent, placeholder)) so host applications keep their own model-facing wording stable.with_events(true), default off): when enabled and at least one body is cleared, anAgentEvent::Compressed { from_tokens, to_tokens }is emitted; when disabled the middleware is a silent transcript rewrite.Extracted upstream from the in-house OpenHuman middleware of the same name (mirroring the
NoProgressTrackerprecedent). The ported tests form the byte-for-byte behavior contract.API Or Behavior Changes
New public API (additive, no breaking changes):
tinyagents::harness::middleware::MicrocompactMiddlewareMicrocompactMiddleware::new(keep_recent: usize, placeholder: impl Into<String>) -> Self.with_events(emit_events: bool) -> Self.keep_recent() -> usize,.placeholder() -> &strimpl<State, Ctx> Middleware<State, Ctx>(before_modelhook;name()="microcompact")No existing behavior changes: the type is only active when a caller pushes it onto their
MiddlewareStack.Tests
Ran locally from
vendor/tinyagents:cargo fmt --checkcargo clippy --all-targets -- -D warnings(clean; compiles all targets)cargo clippy --all-targets --all-features -- -D warnings— not run locally; CI covers the all-features matrixcargo build --all-targets— covered byclippy --all-targets(all targets type-check)cargo build --all-targets --all-features— not run locallycargo test --lib— 764 passed; 0 failedcargo test --all-features— not run locallyNew unit tests in
src/harness/middleware/test.rs(ported from the OpenHuman in-house version as the parity contract):microcompact_clears_older_tool_bodies_and_keeps_recentmicrocompact_is_a_noop_when_within_keep_recentmicrocompact_is_idempotentmicrocompact_emits_no_event_by_defaultmicrocompact_emits_compressed_event_when_enabledDocumentation
Public API documented with rustdoc on the struct, constructor,
with_events, and theMiddlewareimpl (behavior, idempotence, event semantics, relationship toContextCompressionMiddleware). No separate module doc needed — it sits in the existing built-in-middleware module next to its siblings.