Skip to content

fix(agent-runtime): include cached usage in context budget - #1994

Merged
vastsa merged 1 commit into
vastsa:mainfrom
FuYukiH:fix/context-usage-compaction
Oct 30, 2026
Merged

vastsa merged 1 commit into
vastsa:mainfrom
FuYukiH:fix/context-usage-compaction

Conversation

@FuYukiH

@FuYukiH FuYukiH commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

A Responses gateway can report cached input in its usage buckets while omitting it from total_tokens. With 20,000 uncached input tokens, 60,000 cached input tokens and 100 output tokens, the inspector shows 80,100 tokens, but the runtime previously anchored its compaction budget to the smaller reported total of 20,100. In a 100,000-token window, a subsequent turn could therefore be sent without the expected checkpoint.

Share the inspector's request-occupancy calculation with the runtime. The budget anchor takes the larger of the bucket sum and the reported total, preserving the existing guard when a gateway supplies only partial bucket details. Reasoning remains part of output, and billing totals are unchanged. This reproduces one possible cause related to #1760; that report does not include the gateway's wire usage, so this does not claim to resolve its particular installation.

Validation on Windows / Node 24.14.0:

  • Before the fix, two real Agent-loop regressions on main 7260d0a00 missed the checkpoint for both a live and a restored session. The production-sidecar fixture also fails against the earlier pre-fix bundle.
  • Agent-runtime suite: 1,592 passed, 1 skipped across 116 files. New regressions cover live and restored sessions, continuation after a tool result, under-budget requests, partial bucket reports, and the delegate's compaction boundary.
  • Shared suite: 1,340 passed. Focused Desktop tests: 83 passed, including context usage and Electron interaction tests.
  • Production-sidecar E2E: 3/3 passed with a localhost Responses gateway and fixture Host. It exercises the default summary path: cached input omitted from total triggers a summary and durable checkpoint; a larger total with incomplete buckets still triggers; normal usage does not. Run node scripts/e2e-context-usage-compaction.mjs after building shared and the sidecar.
  • Workspace TypeScript builds, Desktop typecheck and production build, sidecar bundle, lint, architecture/style checks, policy sync, docs checks and diff checks passed.

The full Desktop suite on Windows had 4,506 passed, 74 failed, 8 skipped. Fourteen failures were caused by a missing Electron binary; after restoring the same version from the local cache, the focused Desktop run above passed and eight more affected tests passed. The remaining Electron case, an unrelated S3 settings fixture, timed out. Sampled POSIX path, macOS release and SSH tests also fail on the pre-fix worktree in this Windows environment. The remaining full-suite failures were not individually classified; remote CI provides the Linux gate.

Tested candidate: d2f18b29d3126cd380304a5bf547e1894d07a67e. Incorporated main: 7260d0a00a75c198b01642313c8b7654e8c63940.

GitHub merge candidate b2bbb9a52afdfd10277f3d90f3ef1af8c9f73eb7 has the identical tree (836e93af49f1b0acfafa2deb14b8ed17f3e99d13) as the tested candidate.

Some gateways report request totals that omit cached input even though
their usage buckets include it. The inspector shows the full occupancy,
but the compaction guard trusted the smaller total and could send an
oversized request.

Use the inspector occupancy as a floor for the runtime usage anchor,
while retaining a larger reported total when the bucket breakdown is
incomplete. Keep billing totals unchanged.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Independently verified the root cause by substituting main's pi-runtime-estimates.ts: live-session, restored-session and tool-continuation regressions all miss the checkpoint (3 fail, 2 controls pass). Restored candidate passes the full runtime suite (116 files / 1,592 tests), shared suite (104 / 1,340), and 32 focused Desktop context-usage/compaction tests. Shared/runtime TypeScript builds and production sidecar bundle pass. Production sidecar + localhost Responses gateway E2E passes all three cases: omitted cached total, partial buckets with larger total, and normal under-budget usage. No paid provider or physical Desktop E2E was run. Initial reused dependencies were stale/incompatible; validation used isolated lockfile dependencies without changing primary checkout dependencies.

The change is a coherent root-cause fix: includes cache occupancy, retains the existing total floor, leaves billing totals untouched and avoids double-counting reasoning. Six remote CI checks are green. Latest main 7260d0a is an ancestor; fetched PR merge candidate tree matches tested head exactly (836e93af49f1b0acfafa2deb14b8ed17f3e99d13). Diff check passes, no contributor code changes needed. #1760 remains an unproven installation-specific report and should remain open as described.

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.

2 participants