diff --git a/src/server.ts b/src/server.ts index 3fb954b5ff..cd8de0092e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2892,6 +2892,18 @@ const SIDE_LANE_ANCHOR_MIN_ABS_DELTA = 25_000; // honest anchors on the fast path while #4805's side-lane sample (anchor request // ~3k upper, main payload ~680k upper) is refused with 2-20x margin. const SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO = 8; +// #7444: upstream clipping (agy-cli's 112 KiB replay cap) only bites on +// multi-MiB sessions — the clip is a fixed byte budget, so the report-vs-payload +// gap only becomes proof of clipping once the forwarded history is far above it. +// 500K chars (~125K tokens) is the floor: below it, a dense-code payload can +// legitimately outrank an honest report by 16x+ (#1820 B e2e: 186K chars over +// a 5k report = 37x) and the anchor must stay on the fast path. +const CLIPPED_REPORT_MIN_OUTBOUND = 500_000; +// #7444: minimum outbound-char-bound / report ratio that proves clipping. +// 16x sits above the honest dense-code under-report band (#1820 B e2e observed +// up to ~37x chars-per-token, but only below the 500K floor) and below the +// agy-cli clip's ~40x gap, so it separates the two without a per-host knob. +const CLIPPED_REPORT_MIN_RATIO = 16; // estimator only errs EARLY (char-count upper bound → compress earlier, never // later), is active only while lastInputTokens == 0 (a real usage report takes @@ -2936,6 +2948,22 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou && payloadUpperNow >= session.stats.lastInputTokens * SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO && payloadUpperNow >= session.stats.lastUsagePayloadUpper! * SIDE_LANE_ANCHOR_IMPOSSIBLE_RATIO && payloadUpperNow - session.stats.lastInputTokens > SIDE_LANE_ANCHOR_MIN_ABS_DELTA; + // #7444: upstream clipping (agy-cli clips to 112 KiB due to agy.exe's 128 KiB + // ACP prompt limit) makes the usage report describe the CLIPPED payload, not + // the full payload bili sent. anchorImpossible cannot catch this: condition B + // requires the current payload to tower 8x over the producer's snapshot, but + // both are at the same scale because bili sent the full history both times. + // The distinguishing fact: the outbound char-bound (localInputEstimate) towers + // 16x+ over the report AND is itself multi-megabyte (>= 500K chars). An + // honest report of a small-to-medium payload can legitimately under-report + // by 16x+ on dense code/JSON (chars-per-token up to ~37x observed in #1820 + // B's e2e harness), but the clipped-payload class only manifests on + // multi-MiB sessions where the upstream's byte cap (112–128 KiB) is far + // below the forwarded history. The 500K-char floor keeps the #1820 B + // honesty margin (186K-char harness) on the fast path. + const reportClipped = session.stats.lastInputTokens > 0 + && (session.stats.localInputEstimate ?? 0) >= session.stats.lastInputTokens * CLIPPED_REPORT_MIN_RATIO + && (session.stats.localInputEstimate ?? 0) >= CLIPPED_REPORT_MIN_OUTBOUND; // #5135: pending compress credit proves THIS session executed its own fold // since the last settle — applyRanges (stream.ts) already netted the // savings out of lastInputTokens while lastUsageGradeTokens still carries @@ -2961,7 +2989,7 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // it (loop/core.ts), so this cannot freeze the meter; zero is a valid // drained reading (kernel treats 0 as unknown). if (session.stats.lastInputTokensSource === "usage" && (session.stats.compressCreditTokens ?? 0) > 0 && session.stats.lastInputTokens >= 0) return { tokens: session.stats.lastInputTokens, source: "usage" }; - if (!anchorStale && !anchorImpossible && session.stats.lastInputTokens > 0 && (session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm") && staleSmallAnchorFallback(session.stats, session.stats.lastInputTokens) === null) return { tokens: session.stats.lastInputTokens, source: "usage" }; + if (!anchorStale && !anchorImpossible && !reportClipped && session.stats.lastInputTokens > 0 && (session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm") && staleSmallAnchorFallback(session.stats, session.stats.lastInputTokens) === null) return { tokens: session.stats.lastInputTokens, source: "usage" }; // #1820: right after a preflight rebuild the usage-grade baseline above is // momentarily absent (the rebuild request's own report hasn't landed yet, // or the upstream never reports), and every branch below sizes on the @@ -2994,7 +3022,7 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // upstreams (#553/#728) are unaffected: their anchor stays absent, so the // fail-closed upper bounds below still apply. const grade = session.stats.lastUsageGradeTokens; - if (!anchorStale && grade !== undefined && grade > 0) { + if (!anchorStale && !reportClipped && grade !== undefined && grade > 0) { // #4383: stale-small retained anchor — the calibrated local estimate // of the current outbound is strictly better than the char-count // branches below (#1839 inflation), so it wins immediately. diff --git a/tests/issue7444-agy-cli-clipping.test.ts b/tests/issue7444-agy-cli-clipping.test.ts new file mode 100644 index 0000000000..35f1eb4a00 --- /dev/null +++ b/tests/issue7444-agy-cli-clipping.test.ts @@ -0,0 +1,134 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { effectiveTokenCount } from "../src/server.ts"; +import type { Session } from "../src/session.ts"; +import type { CoreMessage } from "acp-kernel"; + +// #7444: agy-cli (opencode-agy-cli-auth) clips replayed history to 112 KiB +// (maxInitialPromptBytes, agy.exe's 128 KiB ACP prompt limit) on every replay +// (no_live / drop_live / model switch). bili forwards the full 7.0 MiB history +// (1287 msgs, ~2M chars), agy-cli clips it to 41 msgs / 44.5 KB, agy.exe +// reports [acp-usage] input=50032. The existing anchorImpossible guard cannot +// catch this: condition B requires the current payload to tower 8x over the +// producer's snapshot, but both are ~2M chars (bili sent the full history both +// times). The distinguishing fact: the outbound char-bound (localInputEstimate) +// towers 16x+ over the report AND is itself multi-megabyte (>= 500K chars). +// An honest report of a small-to-medium payload can legitimately under-report +// by 16x+ on dense code (#1820 B e2e: 186K chars over a 5k report = 37x), but +// the clipped-payload class only manifests on multi-MiB sessions. +// +// The fix adds a `reportClipped` condition to effectiveTokenCount that gates +// both usage-returning branches (main usage + grade), falling through to the +// local estimate so the nudge sees the true payload size and fires. + +function sessionWith(anchor: number): Session { + return { + id: "s7444", + meta: {}, + stats: { + requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, + outputTokens: 0, cacheSamples: 0, + lastInputTokens: anchor, lastInputTokensSource: "usage", + contextTokens: anchor, contextTokensSource: "usage", + } as never, + metadata: {}, + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + transient: {}, + } as never as Session; +} + +function bigHistory(count: number): CoreMessage[] { + const msgs: CoreMessage[] = []; + for (let i = 0; i < count; i++) { + msgs.push({ id: `m${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: "x".repeat(6000) }); + } + return msgs; +} + +test("T1 agy-cli clipping: a report from a clipped payload cannot size the full 2M-char history", () => { + const session = sessionWith(50_032); + // bili forwarded the full 2M-char history; agy-cli clipped it to 113 KB + // before sending to agy.exe, which reported 50032 tokens for the clip. + session.stats.localInputEstimate = 2_000_000; // outbound char-bound (full payload) + session.stats.lastUsagePayloadUpper = 2_000_000; // producer's snapshot (same scale) + // 334 messages × 6000 chars = 2,004,000 chars ≈ 2M + const msgs = bigHistory(334); + const verdict = effectiveTokenCount(session, msgs, 0); + assert.equal(verdict.source, "estimate", "clipped report must not size the meter (#7444)"); + assert.ok(verdict.tokens > 50_032, `meter must see the true payload size (${verdict.tokens} > 50032)`); + assert.ok(verdict.tokens >= 1_000_000, `meter must be in the megabyte range (${verdict.tokens})`); +}); + +test("T2 #1820 B honesty margin preserved: localInputEstimate=0 keeps the anchor", () => { + // The unit-test #1820 B case sets localInputEstimate=0, so reportClipped + // cannot fire (requires localInputEstimate > 25_000). The anchor retains + // usage-grade authority. + const honest = sessionWith(5_100); + honest.stats.localInputEstimate = 0; + honest.stats.lastUsagePayloadUpper = 220_000; + const trusted = effectiveTokenCount(honest, bigHistory(40), 0); // ~240K upper + assert.equal(trusted.source, "usage", "an anchor written by THIS-scale request stays ground truth (#1820 B)"); + assert.equal(trusted.tokens, 5_100); +}); + +test("T3 small-session guard: reportClipped does not trip below the 500K-char floor", () => { + // A small session with a 30K-char outbound and a 5K report: the ratio is + // 6x (< 16x) AND the outbound is below the 500K floor, so reportClipped is + // false. The anchor retains authority. + const small = sessionWith(5_000); + small.stats.localInputEstimate = 30_000; // 30K chars, ratio 6x < 16x + small.stats.lastUsagePayloadUpper = 30_000; + const msgs = bigHistory(5); // 30K chars + const verdict = effectiveTokenCount(small, msgs, 0); + assert.equal(verdict.tokens, 5_000, "small-session anchors keep sizing (no false trips)"); + assert.equal(verdict.source, "usage"); +}); + +test("T3b #1820 B e2e margin: dense-code 37x ratio below the 500K floor keeps the anchor", () => { + // The #1820 B e2e harness: 186K chars of dense ASCII code, honest report of + // 5K tokens. Ratio 37x > 16x, but the outbound (186K) is below the 500K + // floor, so reportClipped is false. The anchor retains authority. + const dense = sessionWith(5_000); + dense.stats.localInputEstimate = 186_000; // 186K chars, ratio 37x > 16x + dense.stats.lastUsagePayloadUpper = 186_000; + const msgs = bigHistory(31); // 186K chars + const verdict = effectiveTokenCount(dense, msgs, 0); + assert.equal(verdict.tokens, 5_000, "dense-code honest anchors keep sizing below the 500K floor (#1820 B)"); + assert.equal(verdict.source, "usage"); +}); + +test("T4 grade branch also gated: lastUsageGradeTokens cannot re-return the clipped anchor", () => { + // Even if the main usage branch is gated, the grade branch (which reads + // lastUsageGradeTokens, set to lastInputTokens at settle) would re-return + // the same clipped value. Both branches must be gated. + const session = sessionWith(50_032); + session.stats.localInputEstimate = 2_000_000; + session.stats.lastUsagePayloadUpper = 2_000_000; + (session.stats as { lastUsageGradeTokens?: number }).lastUsageGradeTokens = 50_032; + const msgs = bigHistory(334); + const verdict = effectiveTokenCount(session, msgs, 0); + assert.equal(verdict.source, "estimate", "grade branch must also be gated (#7444)"); + assert.ok(verdict.tokens > 50_032, `grade branch must not re-return the clipped anchor (${verdict.tokens})`); +}); + +test("T5 honest large-session report: ratio below 16x keeps the anchor", () => { + // A large session where the report is 4x the char-bound / 4 (the typical + // chars-per-token ratio): the report is an honest measurement of the full + // payload. reportClipped requires 16x, so it does not fire. + const session = sessionWith(500_000); + // 2M chars / 4 = 500K tokens → ratio 4x < 16x + session.stats.localInputEstimate = 2_000_000; + session.stats.lastUsagePayloadUpper = 2_000_000; + const msgs = bigHistory(334); + const verdict = effectiveTokenCount(session, msgs, 0); + assert.equal(verdict.tokens, 500_000, "honest large-session report keeps authority (ratio 4x < 16x)"); + assert.equal(verdict.source, "usage"); +});