Skip to content

fix: invalidate retained context usage after compaction - #168

Merged
hetaoBackend merged 1 commit into
mainfrom
codex/fix-compaction-usage
Sep 18, 2026
Merged

hetaoBackend merged 1 commit into
mainfrom
codex/fix-compaction-usage

Conversation

@hetaoBackend

@hetaoBackend hetaoBackend commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

Change

Fixes #152. After shared ContextManager compaction, a small summary plus retained reply could still estimate 180,100 tokens because the reply's usage measured the old context. The synthetic regression now estimates 15 tokens and the next checkpoint stays below the trigger.

Persist the retained-message count on the summary and exclude that prefix from usage anchoring. Responses appended after this boundary can supply fresh usage, including when timestamps tie or the clock moves backwards. Preserve all historical assistant usage and cost data. Legacy summaries without the boundary use the existing local runtime's timestamp freshness rule; ambiguous timestamps fall back to local estimation. Remote token counting continues to control trigger decisions.

Scope: this fixes the shared module. No direct ContextManager instantiation was found in the public runtime; its separate local estimator already filters pre-compaction usage by timestamp. The issue's claim that the condition is always permanent is too broad: a new valid provider response can also replace the stale anchor. No CLI flags or documentation changes are needed.

Validation

  • Reproduced before the fix: 8 regression failures, including 180,100 instead of 15 tokens.
  • PASS: 15 focused compaction-usage regressions covering replacement metrics, next checkpoint, JSON restore, historical metrics, new usage, repeated compaction, legacy timestamps, invalid boundaries, failed responses, and remote counter payload/decision preservation.
  • PASS: pnpm typecheck, pnpm check:source, pnpm check:tsconfig, git diff --check.
  • PASS: pnpm verify full profile, all 14 gates, on clean commit c9e82774ba8f110c91d2244b4327de2eb4866c80 (macOS arm64, Node 26.4.0). Includes source export, release tools, types, build, standalone/artifact checks, capabilities, status contract, smoke, offline BYOK, policy, and sandbox.
  • NOT RUN: live providers and interactive UI acceptance. Local validation is macOS arm64; Linux/Windows acceptance depends on CI. Legacy restoration without explicit provenance assumes comparable timestamps, as the existing local runtime does.

Publication and contribution checks

  • I have permission to contribute these changes under the existing licenses applicable to the changed files/packages; no imported material or license changes.
  • No credentials, account data, real user content, internal source history or private review material is included.
  • The added synthetic regression file was reviewed before regenerating release/public-source.json and declared in test/vitest-suites.json.
  • No shared documentation or capability contract change; offline fixtures are not represented as live-service acceptance.

Maintainer handoff

Publication scope: one synthetic regression test added to the reviewed source inventory. License changes: none.

Shared-source port: pending.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

@hetaoBackend
hetaoBackend merged commit 3123293 into main Sep 18, 2026
8 checks passed
@hetaoBackend
hetaoBackend deleted the codex/fix-compaction-usage branch September 18, 2026 23:34
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.

[Bug]: Context compaction trigger stays tripped after compaction — stale provider usage is trusted as the prefix sum

1 participant