Skip to content

Add MicrocompactMiddleware: clear older tool-result bodies#9

Merged
senamakel merged 2 commits into
mainfrom
feat/microcompact-middleware
Jul 4, 2026
Merged

Add MicrocompactMiddleware: clear older tool-result bodies#9
senamakel merged 2 commits into
mainfrom
feat/microcompact-middleware

Conversation

@senamakel

Copy link
Copy Markdown
Member

Summary

Adds a built-in MicrocompactMiddleware alongside ContextCompressionMiddleware and MessageTrimMiddleware.

In before_model it clears the bodies of older tool-result messages while keeping the keep_recent most recent verbatim (preserving each tool_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 with ContextCompressionMiddleware (which summarizes older chat history near the context window) — this one only ever blanks stale tool payloads.

  • Idempotent: a body already equal to the placeholder is left as-is, so repeated before_model passes converge.
  • No-op when there are at most keep_recent tool results.
  • The placeholder text is caller-supplied (MicrocompactMiddleware::new(keep_recent, placeholder)) so host applications keep their own model-facing wording stable.
  • Event emission is opt-in (with_events(true), default off): when enabled and at least one body is cleared, an AgentEvent::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 NoProgressTracker precedent). The ported tests form the byte-for-byte behavior contract.

API Or Behavior Changes

New public API (additive, no breaking changes):

  • tinyagents::harness::middleware::MicrocompactMiddleware
    • MicrocompactMiddleware::new(keep_recent: usize, placeholder: impl Into<String>) -> Self
    • .with_events(emit_events: bool) -> Self
    • .keep_recent() -> usize, .placeholder() -> &str
    • impl<State, Ctx> Middleware<State, Ctx> (before_model hook; 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 --check
  • cargo 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 matrix
  • cargo build --all-targets — covered by clippy --all-targets (all targets type-check)
  • cargo build --all-targets --all-features — not run locally
  • cargo test --lib764 passed; 0 failed
  • cargo test --all-features — not run locally

New 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_recent
  • microcompact_is_a_noop_when_within_keep_recent
  • microcompact_is_idempotent
  • microcompact_emits_no_event_by_default
  • microcompact_emits_compressed_event_when_enabled

Documentation

Public API documented with rustdoc on the struct, constructor, with_events, and the Middleware impl (behavior, idempotence, event semantics, relationship to ContextCompressionMiddleware). No separate module doc needed — it sits in the existing built-in-middleware module next to its siblings.

senamakel added 2 commits July 3, 2026 23:16
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
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 8 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 592b7d9a-e0c5-41f9-9fab-558f88a08964

📥 Commits

Reviewing files that changed from the base of the PR and between 0e38de7 and 5af6a62.

📒 Files selected for processing (3)
  • src/harness/middleware/mod.rs
  • src/harness/middleware/test.rs
  • src/harness/middleware/types.rs

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

@senamakel
senamakel merged commit 74de1a6 into main Jul 4, 2026
3 checks passed
@senamakel
senamakel deleted the feat/microcompact-middleware branch July 5, 2026 18:23
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