Repository navigation
fix(agent-runtime): calibrate the context estimate against what requests cost - #779
Merged
vastsa merged 2 commits intoSep 21, 2026
Merged
Conversation
…sts cost Reopens vastsa#683 with the four points from the review addressed. 1. Conservative handling of anomalous usage. The correction is asymmetric now: upward applies once three observations exist, while downward needs `CONTEXT_CALIBRATION_DOWNWARD_CONSECUTIVE` agreeing samples, is capped at `CONTEXT_CALIBRATION_DOWNWARD_STEP_MAX` per step, and can never take the value below `CONTEXT_CALIBRATION_FACTOR_MIN` (0.85) of the raw estimate. A projection at `CONTEXT_CALIBRATION_BOUNDARY_GUARD_RATIO` (1.18x) of the hard limit therefore still compacts. A report outside 0.5x-3x of what the calibration predicted is a misreport: it is discarded and moves nothing, and two consecutive ones (`CONTEXT_CALIBRATION_ANOMALY_FREEZE`) freeze the downward direction until a usable report arrives. 2. Ratio and fixed offset are separated. The per-character bias stays a scale-free ratio over the guessed tail. An unanchored residual is applied as a ratio only for observations at a comparable scale (`CONTEXT_CALIBRATION_SCALE_BAND_*`, 0.5x-2x of the estimate); otherwise only the observed overhead is added, capped by `CONTEXT_CALIBRATION_OVERHEAD_CAP`. A 100k-scale sample can no longer be applied as a ratio to a 1M projection, and once the large scale is observed the correction becomes proportional instead of staying at the small-scale offset. 3. Regression coverage for the four areas the review named: consecutive under-reporting (the floor holds; a downward move needs agreeing samples), different context scales (ratio in band, capped overhead out of band, proportional once observed), real CJK text (the tail is corrected to the measured cost, ratio inside the documented 2-4 band), and the compaction boundary (the guard ratio still compacts; the deferral is bounded). 4. Rebased onto current main (79cd7ae) with `contextBudgetFor` from the budget formula extraction (vastsa#733), and the full agent-runtime suite re-run. C:\Users\10470\.pi-desktop\scratch\cb9e2d41-8a55-4a18-899d-92ca2791ab51\commit-683-r2.txt
Resolve documentation conflicts while preserving the calibration and unique tool-call scenarios from both branches.
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.
This reopens #683 with the four points from your review addressed. Thank you for the specific diagnosis — the asymmetry of the lower clamp and the fixed offset were both real defects in the first version.
1. Anomalous usage is handled conservatively
The correction is asymmetric now, because this number gates compaction:
CONTEXT_CALIBRATION_FACTOR_MAX(6×)CONTEXT_CALIBRATION_DOWNWARD_CONSECUTIVE(3) agreeing samplesCONTEXT_CALIBRATION_DOWNWARD_STEP_MAX(15 %) per step, and never belowCONTEXT_CALIBRATION_FACTOR_MIN(0.85) of the raw estimateConsequences that are now asserted in tests rather than argued: a projection at
CONTEXT_CALIBRATION_BOUNDARY_GUARD_RATIO(1 / 0.85≈ 1.18×) of the hard limit still compacts, and how far a correction may defer the boundary is bounded by1 − FACTOR_MIN. A report outside 0.5×–3× of what the calibration predicted is treated as a misreport: it moves the value in neither direction and is counted, andCONTEXT_CALIBRATION_ANOMALY_FREEZE(2) consecutive ones freeze the downward direction until a usable report arrives.2. Ratio and fixed offset are separate quantities
CONTEXT_CALIBRATION_SCALE_BAND_*, 0.5×–2× of the estimate being corrected). Out of band, only the fixed part is added — the observed overhead, capped atCONTEXT_CALIBRATION_OVERHEAD_CAP(32,000 tokens) so it stays an overhead.3. Regression coverage
packages/agent-runtime/src/context-calibration.test.ts(13 cases) covers the four areas you named:1 − FACTOR_MIN; a wild report moves nothing.4. Rebase and validation
Rebased onto
main(79cd7ae8). The only conflict was the budget-formula extraction (#733):contextBudgetFornow owns the body, and the calibration is applied on top of that budget.Local, on this branch:
pnpm --filter @pi-desktop/agent-runtime build0;pnpm --filter @pi-desktop/agent-runtime test47/48 files — the single failure is the pre-existingnative-pi-sessioncase that fails identically on pristinemain;node scripts/check-architecture.mjspassed;pnpm lintclean;pnpm docs:check(78 EN/zh pairs, 495 pages) exit 0.E2E gate: NOT RUN. The scenario is written (
E2E-CONTEXT-estimate-calibration-stays-safe) but no desktop E2E suite was executed: it needs a scripted-usage provider fixture that does not exist yet. Alternative validation: the module is pure arithmetic and fully unit-covered, including the CJK and boundary cases. Remaining risk: the calibrated number as it appears in the live occupancy ring has not been observed against a real provider's usage reports.Specification and decisions
docs/spec/03-runtime/02-agent-runtime.md§5.1 — the calibration paragraph, next to the boundary it protects; mirrored todocs/zh-CN/….08-meta/decisions-log.md— D606 (numbered after the D604/D605 proposed in feat(agent-runtime): recall, a reversible compaction boundary, and per-model context settings #721; I will renumber if that lands later).06-delivery/04-e2e-test-plan.md— the scenario above plus its traceability row; mirrored.