Skip to content

fix(compaction): cap summary reasoning, degrade on truncation, key the failure breaker by attempt - #376

Open
ZouR-Ma wants to merge 1 commit into
mainfrom
fix/compaction-truncation-p0
Open

ZouR-Ma wants to merge 1 commit into
mainfrom
fix/compaction-truncation-p0

Conversation

@ZouR-Ma

@ZouR-Ma ZouR-Ma commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Problem

Automatic compaction of a long session (1M context window, 64k output cap, session thinking level max) failed twice in a row on the same input, each attempt taking about 8.7 minutes, and succeeded on the third attempt by chance. The agent loop was blocked for about 28 minutes.

Root causes:

  1. The summary request inherited the session thinking level. Adaptive thinking has no budget, so reasoning used 40k-47k tokens of the 64k output cap and the summary body was truncated (stopReason=length).
  2. The prompts asked for a complete handoff and told the model never to drop finished items, so the summary grew on every compaction (59,319 to 78,797 characters).
  3. A truncated summary failed the whole compaction with no recovery path.
  4. Switching models reset the failure count, so the three-failure breaker never tripped.

Changes

  • Summary thinking level: new compaction.thinkingLevel (default low, inherit keeps the old behavior). The summary request uses min(session level, configured level), clamped to what the model supports.
  • Summary length budget: new compaction.summaryTargetTokens (default 16000, capped at half the model output limit). The prompts state a length budget, and the update prompt consolidates the previous summary instead of copying it forward.
  • Truncation recovery: on length, retry once with reasoning removed, then once with a tightened target, before failing. The steps are recorded as degradations in the diagnostics. A truncated summary is still never persisted.
  • Failure breaker: threshold-compaction failures are counted per attempt fingerprint (provider/model | maxTokens | window | summary thinking level) instead of being cleared on every model switch. A pause applies only to the fingerprint that failed. A summary that is still truncated after the whole recovery ladder pauses automatic compaction on the first occurrence.

New files: src/core/compaction/summary-policy.ts, test/compaction-thinking-level.test.ts. Docs (docs/compaction.md, docs/settings.md) and schema/config.schema.json are updated.

Verification

Run on a clean checkout of main:

  • pnpm install --frozen-lockfile, tsgo --noEmit, biome check --error-on-warnings packages/coding-agent, generate-config-schema.ts --check: all exit 0
  • check-public-boundary.mjs and check-no-observability.mjs: pass
  • 10 compaction-related test files, 196 tests: all pass
  • The pre-commit hook ran in full and passed

Notes for reviewers

  • The default compaction.thinkingLevel = low is provisional. It has not yet been validated by replaying a real long session at each level. inherit restores the previous behavior if a more conservative rollout is preferred.
  • test/suite/regressions/compaction-adaptive-wire.test.ts pins the SHA256 of the three summary prompt templates. The hashes are updated for the new prompts, and any later prompt change must update them again.
  • Not verified: whether adaptive thinking on the target gateway can be bounded with budget_tokens, and whether 64k is the gateway's real output limit.

…e failure breaker by attempt

Automatic compaction of a long session could fail repeatedly: the summary request
inherited the session thinking level (for example max), adaptive thinking has no
budget, reasoning consumed most of the output cap and truncated the summary body,
and the whole compaction was reported as failed. The prompts also asked for a
complete handoff that never dropped finished items, so summaries grew on every
compaction, and switching models reset the failure count so the breaker never tripped.

- Add compaction.thinkingLevel (default low, inherit keeps the old behavior). The
  summary request uses min(session level, configured level), clamped to what the
  model supports.
- Add compaction.summaryTargetTokens (default 16000, capped at half the output
  limit). The prompts state a length budget and the update prompt consolidates the
  previous summary instead of copying it forward.
- When a summary stops on length, retry once with reasoning removed and once with a
  tightened target before failing. Attempts are recorded as degradations in the
  diagnostics and a truncated summary is still never persisted.
- Count threshold-compaction failures per attempt fingerprint (provider/model,
  maxTokens, context window, summary thinking level) instead of resetting on every
  model switch. A pause only applies to the fingerprint that failed, and a summary
  that is still truncated after the whole ladder pauses automatic compaction on the
  first occurrence.
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