Repository navigation
fix(agent-runtime): include cached usage in context budget - #1994
Conversation
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
left a comment
There was a problem hiding this comment.
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.
fix: pick upstream compaction fixes vastsa#1984, vastsa#1988, vastsa#1994
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:
7260d0a00missed the checkpoint for both a live and a restored session. The production-sidecar fixture also fails against the earlier pre-fix bundle.node scripts/e2e-context-usage-compaction.mjsafter building shared and the sidecar.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
b2bbb9a52afdfd10277f3d90f3ef1af8c9f73eb7has the identical tree (836e93af49f1b0acfafa2deb14b8ed17f3e99d13) as the tested candidate.