Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/acp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ function fmtBytes(n: number): string {
return `${(n / (1024 * 1024)).toFixed(1)}MiB`;
}

// #2366: usage fraction at which the PRESSURE NOTE becomes worth showing —
// below it the window still has headroom and "stop folding" is noise.
const PRESSURE_NOTE_USAGE_FRACTION = 0.6;

export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx): ProxyToolResult {
const scope = typeof args.scope === "string" ? (args.scope as "compressed" | "uncompressed") : undefined;
const view = typeof args.view === "string" ? (args.view as "ranges" | "messages") : undefined;
Expand All @@ -56,6 +60,37 @@ export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx
});
if (scope) return toolOk(base);
const extra: string[] = [];
// #2366: everything in the report above is LOCAL ESTIMATES (defaultCountTokens);
// on CJK-heavy routes upstream billing runs 2–4× higher, so an agent judging
// pressure from those figures misreads a filling window as healthy and folds
// late, when little compressible mass is left. Surface the usage-grade reading
// next to the estimate view — statusInputBaseline's provenance contract, the
// same number the nudge decision runs on — and flag the ratio when it diverges
// hard. Never-reporting upstreams have no anchor → no line.
const billed = statusInputBaseline(ctx.session);
if (billed > 0) {
let estTotal = 0;
for (const m of ctx.messages) estTotal += defaultCountTokens(m.text ?? "");
// Billed input covers system+tools too (and images); the est view must
// carry the same overhead — every prepare site keeps
// metadata.systemPromptTokens current — or every system-heavy session
// reads as divergence with no tokenizer gap behind it.
const sysOverhead = ctx.session.metadata?.systemPromptTokens;
if (typeof sysOverhead === "number" && sysOverhead > 0) estTotal += sysOverhead;
const srcLabel = ctx.session.stats.lastInputTokensSource === "overflow-arm" ? "overflow arm (bounded)" : "upstream usage";
let billedLine = `BILLED INPUT (${srcLabel}): ${billed} tok`;
const measuredAt = ctx.session.metadata?.contextTokensAt;
if (typeof measuredAt === "number") {
const ageMin = Math.round((Date.now() - measuredAt) / 60_000);
if (ageMin >= 2) billedLine += `, measured ${ageMin}m ago`;
}
if (estTotal > 0) billedLine += ` · est-view total ${estTotal} tok · ratio ${(billed / estTotal).toFixed(1)}×`;
extra.push("");
extra.push(billedLine);
if (estTotal > 0 && billed / estTotal >= 1.5) {
extra.push("NOTE: the token figures in the report above are local estimates and run well below what upstream actually bills (tokenizer-dependent; CJK-heavy content is the usual cause). Judge context pressure from BILLED INPUT; use the breakdown only to locate what to compress.");
}
}
try {
const turn = ctx.core.processTurn({
messages: ctx.messages,
Expand All @@ -69,6 +104,13 @@ export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx
if (nudge) {
extra.push("");
extra.push(nudge.shouldInject ? `Nudge: ACTIVE — ${nudge.reason}` : `Nudge: idle — ${nudge.reason}`);
// #2366: high billed pressure + exhausted compressible mass is the
// deadlock invisible from the estimate view alone — tell the agent
// further folding is futile (each fold rewrites the prefix and
// forfeits the cache hit for almost-nothing reclaimed).
if (!nudge.shouldInject && nudge.contextUsage >= PRESSURE_NOTE_USAGE_FRACTION && nudge.breakdown.maxPending < nudge.breakdown.nudgeGrowthTokens) {
extra.push(`PRESSURE NOTE: billed input sits at ${Math.round(nudge.contextUsage * 100)}% of the ${ctx.config.modelContextLimit}-token limit while max compressible mass is only ~${nudge.breakdown.maxPending} tokens — little left to fold. Repeated small folds rewrite the prefix (forfeiting cache hits) while reclaiming almost nothing; continue the task instead of folding again unless pressure climbs further.`);
}
// #847: only advertise ranges the submit gate accepts — the gate
// counts raw chars (minCompressRange), not tokens, so a range can
// be "viable" yet deterministically uncompressible.
Expand Down
30 changes: 18 additions & 12 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,27 +953,33 @@ export function recordCacheSample(
* no cache tokens (recordCacheSample quarantines that sample). */
// #1933 F1: estimator calibration constants. The local chars/4 estimate is a
// proxy whose ratio to real billing varies per upstream (observed 1.3–2.5× on
// one relay vs ~1.0× on another in the same session), so k̂ is learned per
// one relay vs ~1.0× on another in the same session; CJK-heavy content on
// llama-family tokenizers runs 2.4–4.0× ABOVE it, #2366), so k̂ is learned per
// route and only applied on that route. Samples below MIN are noise (tiny
// requests), clamps bound a single pathological sample from wrecking the EMA
// and pin the correction to the deflate direction only (max 1, see below).
// requests), clamps bound a single pathological sample from wrecking the mean.
const CALIBRATION_MIN_ESTIMATE = 2000;
// Plausibility band for admitting a sample: outside it, the report and the
// payload it bills demonstrably don't correspond (placeholder billing, relay
// echo, mock upstreams) — a ratio there must never teach a factor.
export const CALIBRATION_SAMPLE_MIN = 0.2;
export const CALIBRATION_SAMPLE_MAX = 5;
// Final clamp on the published factor: bounds how far calibration can move
// any decision away from the raw estimate. One-way by design: the clamp max
// is 1, so a learned factor can only DEFLATE the estimate (fire later than
// the raw proxy would), never inflate it. Routes whose billing runs ABOVE
// the local estimate (samples >1) publish k̂=1 — legacy raw behavior — and
// stay covered by the overflow arm / learn-on-failure ladder instead. This
// eliminates the class "calibration itself causes an earlier trigger": the
// observed #1933 damage was over-triggering (37% window tax, fold churn),
// while the opposite error already has a backstop. Discussion: PR #1940.
// any decision away from the raw estimate, in BOTH directions since #2366.
// It was one-way (max 1, "deflate only") by PR #1940's design: inflated
// estimates were deemed safe because they just fire early, with the overflow
// arm as backstop. That premise failed empirically on CJK routes — the
// estimator runs 2.4–4.0× BELOW real billing there (#2366 evidence: 53K
// estimated vs 152,903 billed), so under-estimating routes published k̂=1
// forever: the >1 samples were collected into the ring and discarded at
// publish, and every decision (preflight trigger, output budget, the
// agent-visible status) stayed in the wrong caliber until the window filled
// past recoverable compression. The max now bounds symmetric inflation at 4×:
// it covers the measured CJK band and stays BELOW CALIBRATION_SAMPLE_MAX (5),
// so non-corresponding reports are still rejected before the clamp sees them.
// Wild values remain bounded by the evidence gates: ≥2 recent samples agreeing
// within ×2, keyed by route AND model, MIN_ESTIMATE floor.
export const CALIBRATION_CLAMP_MIN = 0.25;
export const CALIBRATION_CLAMP_MAX = 1;
export const CALIBRATION_CLAMP_MAX = 4;
// Evidence requirements: ≥2 recent same-route samples agreeing within ×2.
// One lucky/degenerate pair must not flip every estimate on the route.
export const CALIBRATION_SAMPLE_WINDOW = 3;
Expand Down
10 changes: 5 additions & 5 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,11 +787,11 @@ function isBlockView(v: unknown): v is BlockView {
}

// #2129: restore-time validation for the #1933 F1 calibrated factor. The learner
// publishes factors clamped to [CLAMP_MIN, CLAMP_MAX], but applyEstimateCalibration
// applies whatever it is given UNCLAMPED — a corrupt/hand-edited file with k̂>1
// would INFLATE every estimate (violating the one-way deflate contract) and
// k̂<0.25 over-deflates past the published band. Reject out-of-band values instead
// of trusting them: absent falls back to the raw estimate (today's behavior).
// publishes factors clamped to [CLAMP_MIN, CLAMP_MAX] (two-way since #2366), but
// applyEstimateCalibration applies whatever it is given UNCLAMPED — a corrupt or
// hand-edited file outside the published band would move every calibrated reading
// off-scale (k̂>4 over-inflates, k̂<0.25 over-deflates). Reject out-of-band values
// instead of trusting them: absent falls back to the raw estimate (legacy behavior).
function restoreCalibratedEstimate(v: unknown): number | undefined {
return typeof v === "number" && Number.isFinite(v) && v >= CALIBRATION_CLAMP_MIN && v <= CALIBRATION_CLAMP_MAX ? v : undefined;
}
Expand Down
15 changes: 8 additions & 7 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4417,11 +4417,11 @@ async function preflightCompressIfNeeded(
}
// #1933 F1: scale the local text estimate by the per-route calibration
// factor k̂ learned from this session's own usage reports (local estimate ÷
// what upstream actually billed, EMA, clamped 0.25–1 — one-way, deflate
// only; see settleUsageReport). Unknown/mismatched origin → raw estimate,
// i.e. today's behavior. #2117 B: the model dimension gates too — a factor
// learned on another model acts as absent here rather than deciding with a
// cross-model billing scale (currentCalibrationFactor).
// what upstream actually billed; clamped 0.25–4, two-way since #2366 — see
// settleUsageReport). Unknown/mismatched origin → raw estimate, i.e. the
// uncalibrated legacy behavior. #2117 B: the model dimension gates too — a
// factor learned on another model acts as absent here rather than deciding
// with a cross-model billing scale (currentCalibrationFactor).
const kFactor = currentCalibrationFactor(session.stats, session.metadata?.lastModel);
const kOrigin = session.stats.calibratedEstimateOrigin;
const calibratedText = applyEstimateCalibration(textEstimate + overheadEstimate, kFactor, kOrigin, currentOrigin);
Expand Down Expand Up @@ -4451,8 +4451,9 @@ async function preflightCompressIfNeeded(
// upstream's billing scale diverges from the estimator's caliber
// (incident #2313: a local OpenAI-compatible shim billed ~200 B/token —
// the trigger read 7.3M-10.3M against a real 305K input, ~24-34x over),
// calibration cannot correct it (k̂ is one-way, clamped 0.25-1, and
// consistent samples below CALIBRATION_SAMPLE_MIN are discarded), so
// calibration cannot correct it (k̂ is two-way since #2366 but clamped
// 0.25-4, and a 24-34x shim sits far outside the clamp; consistent
// samples below CALIBRATION_SAMPLE_MIN are discarded), so
// estimate-driven folding demands unreachable targets, cannot finish
// inside client stream patience (~300s idle abort), and never lets a
// forward through — 0 successful forwards in 12h while every failed
Expand Down
9 changes: 5 additions & 4 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ export type Session = {
lastInputTokensOrigin?: string;
/** #1933 F1: calibrated scale factor k̂ = mean of up to 3 recent
* consistent same-route samples of (upstream-billed input ÷ local
* text estimate), clamped to [0.25, 1] — one-way by design: the
* correction can only deflate the estimate (never fire earlier
* than the raw proxy); routes billing above the local estimate
* publish k̂=1 (legacy behavior, overflow arm covers them). A
* text estimate), clamped to [0.25, 4] — two-way since #2366: the
* correction can inflate OR deflate the estimate toward the route's
* real billing scale (the one-way max-1 design failed empirically on
* CJK-heavy routes where billing runs 2.4–4.0× above the local
* estimate, so under-estimating routes learned nothing). A
* sample is only admitted
* when it falls in the plausibility band [0.2, 5] — outside it the
* report and the payload clearly don't correspond (placeholder
Expand Down
19 changes: 10 additions & 9 deletions tests/issue1933-preflight-calibration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ test("settleUsageReport: cross-route starts a fresh ring and clears the old fact

pair(s, 10000, "http://b", 21000);
// 2.0/2.1 are consistent under-estimate evidence (billing ABOVE the local
// estimate), but calibration is one-way: the mean clamps to 1 = raw
// estimate, legacy behavior — it may never fire earlier than the proxy.
assert.equal(st.calibratedEstimate, 1, "under-estimate route clamps to k̂=1 (no inflation)");
// estimate). #2366 made calibration two-way (clamp [0.25, 4]): the mean
// publishes as-is — under-estimating routes (CJK-heavy content) must learn
// their real billing scale instead of being clamped back to the raw proxy.
assert.ok(Math.abs((st.calibratedEstimate ?? 0) - 2.05) < 1e-9, `under-estimate route learns k̂≈2.05, got ${st.calibratedEstimate}`);
assert.equal(st.calibratedEstimateOrigin, "http://b");
});

Expand Down Expand Up @@ -140,21 +141,21 @@ test("settleUsageReport: implausible pairs never enter the ring", () => {
assert.equal(st.calibrationRing?.values.length, 3, "ring capped at CALIBRATION_SAMPLE_WINDOW");
});

test("settleUsageReport: published factors clamp to [0.25, 1] — one-way, deflate only", () => {
test("settleUsageReport: published factors clamp to [0.25, 4] — bounded both ways (#2366)", () => {
const hi = makeSession();
pair(hi, 10000, "http://e", 45000);
pair(hi, 10000, "http://e", 50000);
assert.equal(hi.stats.calibratedEstimate, 1, "mean 4.75 clamps down to 1: an under-estimating route keeps legacy behavior, calibration never inflates a reading");
assert.equal(hi.stats.calibratedEstimate, 4, "mean 4.75 clamps down to 4: inflation is real on under-estimating (CJK) routes but bounded — and below the sample band max 5, so non-corresponding reports are still rejected first");

const lo = makeSession();
pair(lo, 10000, "http://f", 2200);
pair(lo, 10000, "http://f", 2400);
assert.equal(lo.stats.calibratedEstimate, 0.25, "mean 0.23 clamps up to 0.25");

// The corrected reading can only drop BELOW the raw estimate, never rise:
// applying the clamped hi factor is a no-op (raw × 1), applying the lo
// one deflates — both bounded by the clamp range.
assert.equal(applyEstimateCalibration(10000, hi.stats.calibratedEstimate, "http://e", "http://e"), 10000);
// The corrected reading can now move either way, always inside the clamp
// band: applying the hi factor inflates to the bound (raw × 4), applying
// the lo one deflates (raw × 0.25).
assert.equal(applyEstimateCalibration(10000, hi.stats.calibratedEstimate, "http://e", "http://e"), 40000);
assert.equal(applyEstimateCalibration(10000, lo.stats.calibratedEstimate, "http://f", "http://f"), 2500);
});

Expand Down
7 changes: 4 additions & 3 deletions tests/issue2129-restart-measurement-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ test("corrupt persisted measurement values are rejected at restore (#2129)", asy
}
};

// Out-of-band values: a k̂ above the clamp band would INFLATE estimates
// (applyEstimateCalibration applies unclamped — one-way deflate contract),
// negative anchors/estimates are nonsense, empty origins match no route.
// Out-of-band values: a k̂ above the two-way clamp band [0.25, 4] (#2366)
// would move every calibrated reading off-scale (applyEstimateCalibration
// applies the stored factor unclamped), negative anchors/estimates are
// nonsense, empty origins match no route.
// JSON encodes NaN/Infinity as null — the restore guards reject both.
const bad = await loadMutated((st) => {
st.calibratedEstimate = 5;
Expand Down
Loading
Loading