Skip to content

feat(compaction): Request a background summary after each model call - #2743

Merged
thomasjiangcy merged 1 commit into
mainfrom
feat/compaction-request-every-model-call
Nov 5, 2026
Merged

thomasjiangcy merged 1 commit into
mainfrom
feat/compaction-request-every-model-call

Conversation

@sentry-junior

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

Copy link
Copy Markdown
Contributor

A background summary was requested only after the agent loop ended, so a single long Turn that crossed the compaction threshold mid-run never had one ready. Since #2648 shipped, prod traces show no over_target discards. The inline compactions that remain are active ones (~20s inside the Turn), and mid-Turn swaps almost never apply.

The request now runs in prepareNextTurnWithContext, after every model call. That covers the final reply too, so the Turn-end call is removed. Pi waits for turn_end listeners before this hook, so the tool results are already committed when the mark is taken, and the mark lands on a safe cut after a tool result. The request is skipped when the hook just replaced history, because the last usage then counts the old history.

The hook runs before every model call, so the check above the mark now reads only the newest compaction_requested/compaction_prepared fact first. It loads the full history only when no request is pending and no summary became ready in this slice. loadLatestBackgroundCompaction moves from the swap module into background-compaction.ts, so both use one copy.

The mid-Turn swap stays at the threshold. Swapping as soon as a summary is ready would break the warm prompt cache in the middle of a Turn.

Test coverage gap: I verified the hook wiring with a real-agent integration test (Slack harness, scripted model stream). It fails on main and passes here. I did not commit it, because test-architecture:check does not allow tests in packages/junior/tests to run the agent. An integration eval would have to tune the context window to real token counts, and I couldn't run evals in this environment. Only the request rule is covered: a pending request blocks another one. Decide whether you want an eval before merge.

via @thomasjiangcy.

--

View Session [Sentry]

Co-Authored-By: Thomas Jiang <20501235+thomasjiangcy@users.noreply.github.com>
Co-Authored-By: Thomas Jiang <thomas.jiang@sentry.io>
@vercel

vercel Bot commented Nov 5, 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 5, 2026 1:11pm UTC

Request Review

@github-actions

github-actions Bot commented Nov 5, 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

@thomasjiangcy
thomasjiangcy enabled auto-merge (squash) November 5, 2026 13:15
@thomasjiangcy
thomasjiangcy merged commit a4e8212 into main Nov 5, 2026
45 checks passed
@thomasjiangcy
thomasjiangcy deleted the feat/compaction-request-every-model-call branch November 5, 2026 13:18

This branch was successfully deployed

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

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant