Repository navigation
fix: stabilize system prompt token estimate (#255) - #322
Merged
ranxianglei merged 4 commits intoAug 18, 2026
Merged
ranxianglei merged 4 commits into
ranxianglei merged 4 commits into
Conversation
Contributor
Author
Dual-agent reviewIndependent review of the current PR diff is complete.
CI noteAll code-validation checks are passing:
The only failing required check is Its checkout, Leaving the artifact check for maintainer-side handling. |
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ACP already populated
state.systemPromptTokensbefore pruning, but nudge andacp_statusignored that cache — each re-estimated the system prompt tokensfrom a different message view. After compression, the first visible assistant
can belong to a much later turn whose input includes large accumulated history,
inflating the estimate and causing the two numbers to diverge.
Root Cause
state.systemPromptTokens) existed but was neverconsumed.
messages are removed and the first visible assistant is a late turn.
acp_statusre-estimated from its own fetched message view.producing inflated and divergent estimates.
Fix
Three minimal production changes:
cacheSystemPromptTokenspreserves the first stable positive estimate —later transforms no longer overwrite it with a degraded post-compression
estimate (write-if-undefined guard).
estimateContextCompositionprefersstate.systemPromptTokenswhen it is apositive value; falls back to the existing
estimateSystemPromptTokens(messages)unchanged when undefined.acp_status(collectVisibleMessages) prefers the same cached value; fallsback to the existing estimate when undefined.
Scope
/acp contextchange.Tests
inflate the system estimate (cached 10K survives first-visible-assistant
input of ~200K), and undefined cache falls back to the original estimate.
acp_statusoverview regression asserting the cached value appears in thebreakdown.
preserveRecentMessages: 20): baseline →growth → nudge → compress → new baseline → growth → nudge again, asserting the
stable system estimate survives compression.
that ≥2 nudge observations report the same system prompt estimate.
applied.
npm run typecheck,npm run build,npm run test(1010 pass),Docker E2E (12/12 scenarios),
check-pr.sh.acp_statusexecutes without ACP errors. This verifies the runtime path only — the precise
estimateSystemPromptTokens inconsistent between nudge and acp_status after compression #255 regression is covered by the automated tests; the live environment was
not used to reconstruct a 200K post-compression session.
Known limitations
from degraded history.
Both are intentionally out of scope for this fix.
Fixes #255