feat(ext): budget routed compaction off the served model's context window - #970
feat(ext): budget routed compaction off the served model's context window#970Symbiomancer wants to merge 3 commits into
Conversation
…ndow Pi budgets auto-compaction off the model the client requested, but the router routinely serves a different (often larger-context) model. Without the served window, a session requesting claude-sonnet-4-6 compacted at ~200K even while a 1M-context model served it. Follow the router's x-router-context-window header (emitted by #968): capture it per successful response and use it as the compaction budget at agent_end, falling back to the requested model's static window when absent. The served window is authoritative in both directions - a larger served model avoids premature compaction; a smaller one still compacts early enough. - config: add ROUTED_CONTEXT_WINDOW_HEADER - compaction: track servedContextWindow per run (reset on start/compact) and apply it in the agent_end budget; ignore non-2xx responses and malformed values - tests: 3 new cases (larger served window no pre-compaction; smaller served window mid-loop compaction; absent header keeps requested budget); the harness now parameterizes the requested model window - e2e.sh: unit-suite pass count 22 -> 25
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 162c734. Configure here.
What T-Rex did
|
…requested budget Pi's built-in compaction only fires when the requested model's static window is exceeded. When the router serves a smaller window, a final turn above the served threshold but below the requested budget previously fell through both sides: agent_end returned early for Pi to own it, Pi never fired, and the served model could overflow. Only defer the over-threshold final turn to Pi when Pi's own budget is also exceeded (or the served window is at least the requested window). Add a regression test for the served-window-smaller-than-requested final-turn case. Addresses Cursor Bugbot review on #970.

Summary
Pi 0.74 budgets automatic compaction off the requested model's static context window (installer default
200000), but the router routinely serves a different model — often one with a much larger context. The server side of this fix landed in #968 (x-router-context-windowheader, emitted with the served model's effective window at every routed response site, re-stamped fresh on semantic-cache hits). This PR is the client half: the pi extension now consumes that header.Before: a session requesting
claude-sonnet-4-6compacted at ~200K even while a 1M-context model served it.After: the extension tracks the served window per response and uses it as the compaction budget at
agent_end, falling back to the requested model's static window when the header is absent.Changes
src/config.tsROUTED_CONTEXT_WINDOW_HEADERconstantsrc/compaction.tsservedContextWindowfrom successful responses (ignores non-2xx, malformed values); reset per run; use it in theagent_endbudgettest/compaction.test.tstest/e2e.shBehavior
Validated with
tsx --test(same class of TS loader pi uses): 25/25 unit tests pass.