Skip to content

fix(agent): enforce context window compaction - #1045

Merged
dcramer merged 4 commits into
mainfrom
codex/fix-context-window-compaction
Jul 25, 2026
Merged

dcramer merged 4 commits into
mainfrom
codex/fix-context-window-compaction

Conversation

@dcramer

@dcramer dcramer commented Jul 25, 2026

Copy link
Copy Markdown
Member

Summary

  • cap effective context at min(botConfig.contextWindowTokens, model.contextWindow) with a configurable 400k default
  • compact before the first provider request and after tool batches at 90% of the effective window, then block before a provider call if the exact replacement remains above the 95% input ceiling
  • preserve the current instruction and queued steering input verbatim across compaction, and use the active handoff profile for thresholds and diagnostics
  • bound large editFile diff output and expose privacy-safe compaction metadata in conversation reports, dashboard events, and Markdown exports

Root cause

Junior only compacted reusable history before starting a turn. A tool result—especially an unbounded generated-file diff—could expand the active Pi transcript past the provider limit between model calls. The next provider request was therefore allowed without another capacity check.

The first active-compaction implementation also treated fresh and queued instructions as summary input. That could truncate the current ask, and it did not re-estimate the actual summary-plus-retained replacement before continuing.

Compatibility

  • Existing compaction events remain readable; the new replacement estimate is optional on historical rows.
  • The configured cap defaults to 400k and can be overridden through botConfig / AI_MODEL_CONTEXT_WINDOW_TOKENS.
  • Model-advertised limits always win when they are smaller than the configured cap.

Validation

  • 2,160 Junior tests across 281 files
  • 133 dashboard tests across 14 files
  • focused boundary coverage for preflight compaction, between-call tool growth, oversized retained input, exact current-instruction preservation, and post-handoff model selection
  • Junior and dashboard typechecks and builds
  • Junior lint/dependency checks, ast-grep, formatting, diff checks, and release alignment

Closes #431

@vercel

vercel Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
junior-docs Ready Ready Preview Jul 25, 2026 9:39pm

Request Review

Comment thread packages/junior/src/chat/agent/index.ts Resolved
@dcramer
dcramer marked this pull request as ready for review July 25, 2026 20:56
@github-actions github-actions Bot added the risk: high PR risk score: high label Jul 25, 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.

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 39bdc30. Configure here.

Comment thread packages/junior/src/chat/agent/index.ts Resolved
@dcramer
dcramer force-pushed the codex/fix-context-window-compaction branch from 2f88a25 to 334465f Compare July 25, 2026 21:38
@dcramer
dcramer merged commit 7a299c9 into main Jul 25, 2026
32 checks passed
@dcramer
dcramer deleted the codex/fix-context-window-compaction branch July 25, 2026 21:50

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 334465f1 Deployed Jul 25, 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.

Rework compaction to use history-replacement model

1 participant