Skip to content

feat(compaction): Swap in prepared background summaries - #2605

Merged
thomasjiangcy merged 3 commits into
mainfrom
compaction-swap-prepared-summary
Nov 3, 2026
Merged

thomasjiangcy merged 3 commits into
mainfrom
compaction-swap-prepared-summary

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Stacked on #2602 (base compaction-summary-main-model). Retarget to main once #2602 merges. Depends on the event schemas from #2600, which are already released.

Reads compaction_prepared events and swaps a ready summary into history without a model call. A separate PR adds the job that writes these events. Until it merges, nothing in prod writes them, so this PR changes no behavior there.

When a swap runs

  • Turn start (the agent's compaction step before its first model request): swaps whenever a ready summary exists.
  • Between model calls: swaps only above the 90% threshold, and only in place of the blocking summary call.
  • A summary is used only if all of these hold:
    • it is the newest background compaction event in the current History version;
    • its outcome is ready;
    • its historyVersion and modelProfile match the current ones;
    • the replacement fits under the compaction target.
  • Otherwise the Turn carries on as today, including the blocking fallback.

Replacement history

Tool-result trimming (tool-result-trimming.ts)

  • Applies to carried-over results before the last user message.
  • A result over 2k chars becomes a header, its first and last 500 chars, and readConversationEvent({ seq }).
  • These stay whole: the latest Turn's results, error results up to 8k chars, results with no source event, and short results.
  • Images become [image cleared: <tool>, seq N].
  • Tool call ids don't change, so every call keeps its result.
  • The blocking path's replacement carries no tool results today, so trimming isn't hooked in there.

Telemetry: a swap gets its own agent.context_compaction span. Its attributes are:

  • app.compaction.outcome: applied / not_ready / discarded;
  • app.compaction.discard_reason;
  • the prepared summary's age;
  • the count and estimate of carried messages;
  • the trimmed result count and chars removed.

The mode (blocking / active) gives the swap point. The details are in TELEMETRY.md.

Decisions for review

  • split_tool_call is an extra discard reason. It applies when the first message after the mark is a tool result, because the mark falls between a tool call and its result.
  • Turn start now runs two small indexed queries per Turn to look for a background event.
  • A stale prepared event (its historyVersion doesn't match, but it was appended in the current version) is reported as discarded on every Turn until the next replacement.
  • The read-back eval in continuity.eval.ts can't pass yet. Its compaction is the blocking path, which keeps no tool results. It needs the background job to write compaction_prepared first.

via @thomasjiangcy.

--

View Session [Sentry]

@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 3, 2026 2:01pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Nov 3, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

A conflicting replacement means the active messages are stale. The
blocking summary wrote them without a version check, which could drop
the other replacement.
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

1 screenshot change — 1 changed · 0 added · 0 removed

Review screenshots in Frameshift

Settings Mcp · Desktop
Settings Mcp · Desktop
Changed

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit e1775f3. Configure here.

Trimming took the last user message as the Turn boundary. Runtime
context and summaries are user messages too, so context added after a
Turn's tool results got those results cleared. Use the last Turn
instruction as the boundary.
@thomasjiangcy
thomasjiangcy merged commit fa2df0f into main Nov 3, 2026
46 of 47 checks passed
@thomasjiangcy
thomasjiangcy deleted the compaction-swap-prepared-summary branch November 3, 2026 14:08

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 5f2d2d53 Deployed Nov 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant