Skip to content

fix(llmloop): reserve complete compression context budget - #1993

Merged
lizhengfeng101 merged 6 commits into
mainfrom
fix/compression-zone-budget
Nov 19, 2026
Merged

lizhengfeng101 merged 6 commits into
mainfrom
fix/compression-zone-budget

Conversation

@lizhengfeng101

Copy link
Copy Markdown
Contributor

Description

Fixes #838.

Three-zone compression could keep recent rounds against almost the entire warning threshold, then add the fixed prompt and a new summary on top. The loop could stop with StopCompression even when compressing more history would have allowed it to continue. The all-fitting and missing-template paths could also summarize or discard live context unnecessarily.

  • Charge the fixed system/user prefix, including earlier summaries, when selecting active rounds.
  • Reserve the new summary and its wrapper before retaining complete recent rounds. Bound the compression output cap by the configured completion limit, the 20% context margin, and the remaining fixed-prefix space.
  • Leave history intact when nothing needs compression or the compression template is absent. Do not summarize the entire live tail while the original conversation is below the warning limit.
  • Validate the rebuilt context: oversized summaries return an error with the original history preserved; empty or non-shrinking summaries leave it unchanged.
  • Add regression tests for partitioning, summary reservations, strict threshold boundaries, history preservation, and main-loop continuation. Update the affected Review/Scan expectations and architecture documentation.

Scope and compatibility

This is an independent fix on main, without the Analysis feature commits from #1937. It applies to the shared loop used by Review and Scan.

The 60%/80% compression thresholds and aggregate --max-tokens-budget semantics are unchanged. Compression requests may now use a smaller output cap, particularly with small configured context limits. Unrecoverable context overflow still stops explicitly rather than silently dropping uncompressed history.

Compression-request input bounding (#1876) and suppression of repeated rejected requests (#1877) remain separate work. This PR addresses the same frozen-prefix accounting problem as the unmerged #1473 and additionally reserves new-summary space and checks the rebuilt result.

Validation

  • make check
  • make test (race-enabled, full suite)
  • make coverage: 91.8%, above the 90% threshold
  • make build
  • Required ocr review --audience agent --background ...: one selected implementation file, no findings; tests and documentation were also inspected locally.
  • Analysis compatibility: the internal/behaviortask and internal/workflow suites passed on an isolated snapshot of feat(review): add optional behavior and impact analysis #1937 with this implementation overlaid, without adding those commits to this branch.

The initial regression cases fail on the unchanged implementation and pass with the fix. A local recorded-history replay with a stubbed summary response reproduced a 160,173-token rebuilt context against a 160,000-token threshold. The fixed reconstruction used 131,291 tokens, with 143,897 tokens reserved even when allowing the full summary cap. The replay checks budgeting and history retention; it is not a fresh provider run or an evaluation of summary quality. Recorded conversation contents are not included in this PR.

Checks used project-local temporary directories and Git discovery ceilings to isolate non-repository fixtures.

AI/LLM disclosure

Implementation and PR preparation used OpenAI Codex with gpt-6-astra. Automated pre-commit review used OpenCodeReview (ocr) with gpt-6-astra.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment on lines +245 to +248
originalTokens := CountMessagesTokens(msgs)
if part.activeCount == 0 && originalTokens < limit {
return msgs, nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · medium
When activeCount == 0 and originalTokens < limit, the function returns msgs unchanged (no error). But this means the caller (addNextMessage) will see that tokens are still above warnLimit (since this path is only reached when the caller determined CountMessagesTokens(*messages) > warnLimit), yet compression reports success with unchanged messages. The caller will then proceed to append more messages, making the situation worse.

Consider whether this should either:

  1. Return an error so the caller knows compression couldn't help, or
  2. Still attempt compression even when originalTokens < limit if activeCount == 0, since the goal is to reduce tokens below warnLimit (which is limit), not just below MaxContextTokens.

Note: limit here IS PromptTokenLimit(MaxContextTokens) = warnLimit, so originalTokens < limit means we're already under the warning threshold. In that case returning unchanged is actually correct — the caller's pre-compression check was stale. Please verify this reasoning matches intent.

@lizhengfeng101
lizhengfeng101 force-pushed the fix/compression-zone-budget branch from cb31c4a to 989e8cd Compare November 18, 2026 13:02
@lizhengfeng101
lizhengfeng101 force-pushed the fix/compression-zone-budget branch from 7abb23e to 36e924b Compare November 18, 2026 13:43

@css521 css521 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 445ad9d into main Nov 19, 2026
15 checks passed
@lizhengfeng101
lizhengfeng101 deleted the fix/compression-zone-budget branch November 19, 2026 02:00
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.

Three-zone compression summarizes or drops live rounds it is meant to preserve

2 participants