Repository navigation
fix(agent-runtime): calibrate the context estimate against what requests cost - #748
zhangqingkun976 wants to merge 1 commit into
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-r.txt
42f8dba to
fc6a5d7
Compare
|
I verified that the calibration problem is real, but this follow-up still violates its safety invariant. recordResidual counts an anomalous sample and can freeze calibration, while recordUnanchored and recordAnchored still append that same sample to the calibration window. A direct repro in this PR worktree records two 4x anomalous samples, then one valid sample. The calibration unfreezes, but the stored anomaly makes correct(...) return 4,000,000 for a 1,000,000-token estimate (ratio 4). Thus a sample described as discarded still affects correction after recovery, and the tests do not cover this path. This is not a root-safe fix. I am closing this PR; the already-closed #683 is not being reopened. |
|
Rebased onto The only conflicts were append-vs-append — the E2E scenario catalog and the decisions log, where main and this branch each added an entry — and both sides are kept. The identifier bands are still free: upstream's newest ADR is 0299 and its newest decisions-log entry is D607 (the plugin crash report, which is this repository's own fix for issue #747 built on my #756). |
|
Closed by accident and GitHub will not reopen it: an API-side ref update to rebase the branch reads as a force-push, and reopening is blocked for such a branch. The same content continues in #779. |
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.