From 8151240e5402200517fbf35aaba59d41899d7d68 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 26 Oct 2026 20:45:07 +0800 Subject: [PATCH 01/10] fix(#4805): two independent guards against side-lane usage pollution of the session anchor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #4805/#4720 family: a 2-message auxiliary request under the SAME session id (system prompt shrunk 6777->1028 chars) settles a 5100-token usage sample over the main conversation's 300K anchor. The anchor write froze the nudge (growth 3900 < floor 22500) while the real context grew to 1.2M; once requests failed (no usage on failure) nothing could repair the anchor — compression dead for hours. #4383's time-based staleness cannot catch it: the polluted sample is seconds old. - settleUsageReport (#4805 source gate): a usage sample >=8x SMALLER than the current usage-grade anchor, with no compress credit, is side-lane traffic — counters still run, the anchor fields are kept. One-directional (far-larger samples always write); post-fold settles (credit > 0) are exempt — our own shrinkage is not a lane change. - effectiveTokenCount (#4805 size guard): even with a polluted anchor, a request whose own upper bound is >=8x the anchor with >25K absolute gap cannot be sized by it — fall through to the local estimate. Early-session growth (small absolute gaps) never trips. issue4805 T1/T1b/T2 green; preflight family green. --- src/cache-ledger.ts | 19 ++++++ src/server.ts | 15 ++++- tests/issue4805-anchor-pollution.test.ts | 86 ++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 tests/issue4805-anchor-pollution.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 19a8450bb2..e16ab821ce 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1789,6 +1789,25 @@ export function settleUsageReport( // #793: a zero-total sample carries no information (gateway placeholder or // relay echo) — it must not clobber the last trusted lastInputTokens. if (s.total > 0) { + // #4805 source gate: a usage sample an order of magnitude SMALLER than + // the session's current usage-grade anchor is side-lane pollution (a + // 2-message auxiliary request under the same session id), not a main- + // lane measurement — writing it froze compression until the context + // blew the window. Skip the ANCHOR fields (counters and calibration + // still run; a later main-lane report restores them). One-directional: + // only a far-SMALLER sample is gated. Exempt settles carrying a + // compress credit — those are our own post-fold re-requests, whose + // shrinkage is the fold's doing, not a lane change. + const prevAnchor = session.stats.lastInputTokens; + const prevUsageGrade = session.stats.lastInputTokensSource === "usage"; + const sideLaneSample = prevUsageGrade && prevAnchor > 0 + && (session.stats.compressCreditTokens ?? 0) <= 0 + && s.total * 8 < prevAnchor; + if (sideLaneSample) { + session.stats.inputTokens += s.total; + loggerLog("info", `[${session.id}] usage sample ${s.total} tok is ~${Math.round(prevAnchor / Math.max(1, s.total))}× smaller than the ${prevAnchor}-tok anchor — treated as side-lane traffic, anchor kept (#4805)`); + return; + } session.stats.inputTokens += s.total; // Net out this turn's compress credit: the post-compress re-request // re-sends the unfolded history, so its usage report over-reports the diff --git a/src/server.ts b/src/server.ts index 3030b92888..98faaf5fc6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2505,7 +2505,18 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // #4383 (both guards): stale-small — a sidecar/side-request sample under // the same session id, superseded by later main sends — must not freeze the // meter either; fall through to the fresher measurement. - if (!anchorStale && 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" }; + // #4805 (size guard): a FRESH tiny anchor can still be lane pollution — a + // 2-message side request's usage (5100 tok) overwrote a 1700-message + // conversation's anchor and froze compression until the context blew the + // window. Time-based staleness cannot catch it (it is seconds old), but the + // size dimension can: a usage sample cannot describe a resent history whose + // own upper bound is ~an order of magnitude larger. Fall through to the + // local estimate then — the nudge sizes the payload it actually sees. + const payloadUpperNow = estimateCoreMessagesUpper(msgs) + inboundImageTokens; + const anchorImpossible = session.stats.lastInputTokens > 0 + && payloadUpperNow >= session.stats.lastInputTokens * 8 + && payloadUpperNow - session.stats.lastInputTokens > 25_000; + 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" }; // #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 @@ -2517,7 +2528,7 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // reporting upstreams fall back to legacy sizing rather than a frozen meter. const anchored = postRebuildAnchorTokens(session); if (anchored > 0) return { tokens: anchored, source: "estimate" }; - const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens; + const raw = payloadUpperNow; // #1569/#1839: while the latest baseline is not usage-grade (the transient // window right after a failed turn), sizing on ANY re-derived view is how // ghosts enter: #1569 first tried min(est, raw) — the char-count upper diff --git a/tests/issue4805-anchor-pollution.test.ts b/tests/issue4805-anchor-pollution.test.ts new file mode 100644 index 0000000000..4ccfa4923d --- /dev/null +++ b/tests/issue4805-anchor-pollution.test.ts @@ -0,0 +1,86 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { settleUsageReport } from "../src/cache-ledger.ts"; +import { effectiveTokenCount } from "../src/server.ts"; +import type { Session } from "../src/session.ts"; +import type { CoreMessage } from "acp-kernel"; + +// #4805 / #4720 family: a tiny side-lane request (2 messages, its own short +// system prompt) riding the SAME session id settles a 5100-token usage sample +// over the main conversation's 300K-token anchor. The anchor write froze the +// nudge (growth 3900 < floor 22500) while the real context kept growing to +// 1.2M — compression permanently stopped, and once requests started failing +// (no usage on failure) nothing could repair the anchor. Two guards, each +// independently breaking the loop: +// - settleUsageReport: a usage sample >=8x smaller than the current +// usage-grade anchor (with no compress credit) is side-lane pollution — +// counters only, anchor kept; +// - effectiveTokenCount: a fresh anchor whose size is impossible for the +// payload at hand (upper bound >=8x the anchor, >25K absolute gap) cannot +// size THIS request — fall through to the local estimate. + +function sessionWith(anchor: number): Session { + return { + id: "s4805", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: anchor, lastInputTokensSource: "usage", contextTokens: anchor } as never, + metadata: {}, + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + } 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 settle gate: a usage sample 8x smaller than the anchor is side-lane pollution — anchor kept, counters still accrue", () => { + const session = sessionWith(300_000); + settleUsageReport(session, { total: 5_100, reportedCached: 128, output: 30, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 300_000, "the 5100-tok side sample must NOT overwrite a 300K anchor (#4805)"); + assert.equal(session.stats.lastInputTokensSource, "usage"); + assert.equal(session.stats.inputTokens, 5_100, "the sample still counts toward cumulative usage"); + // A same-lane report (300K -> 290K, ratio fine) writes normally. + settleUsageReport(session, { total: 290_000, reportedCached: null, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 290_000, "main-lane reports keep updating the anchor"); +}); + +test("T1b settle gate exemptions: no anchor yet, non-usage anchor, and post-fold credit settles all pass", () => { + const fresh = sessionWith(0); + settleUsageReport(fresh, { total: 5_100, reportedCached: null }); + assert.equal(fresh.stats.lastInputTokens, 5_100, "first settle on an empty anchor always writes"); + + const estimate = sessionWith(300_000); + (estimate.stats as { lastInputTokensSource?: string }).lastInputTokensSource = "estimate"; + settleUsageReport(estimate, { total: 5_100, reportedCached: null }); + assert.equal(estimate.stats.lastInputTokens, 5_100, "an estimate-grade anchor is always replaceable by usage"); + + const folded = sessionWith(300_000); + (folded.stats as { compressCreditTokens?: number }).compressCreditTokens = 40_000; + settleUsageReport(folded, { total: 30_000, reportedCached: null }); + assert.equal(folded.stats.lastInputTokens, 0, "a credit-carrying (post-fold) settle nets against the credit and writes — our own shrinkage is not side-lane"); +}); + +test("T2 meter guard: even with a polluted 5100 anchor, a 1.2M-token payload is sized by its own estimate", () => { + const session = sessionWith(5_100); + const msgs = bigHistory(800); // ~4.8M chars -> ~1.2M tokens upper bound >> 5100*8 + const verdict = effectiveTokenCount(session, msgs, 0); + assert.notEqual(verdict.tokens, 5_100, `the impossible anchor must not size a ${verdict.tokens}-token payload request`); + assert.equal(verdict.source, "estimate", "falls through to the local measurement (#4805 size guard)"); + // Small-session growth stays on the anchor: no absolute-gap trip below 25K. + const small = sessionWith(800); + const earlyMsgs = bigHistory(3); + const early = effectiveTokenCount(small, earlyMsgs, 0); + assert.equal(early.tokens, 800, "early-session anchors keep sizing small payloads (no false trips)"); +}); From 83e5cb406171a101292f0fc35ac040c7462ef01e Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 20:54:41 +0800 Subject: [PATCH 02/10] test(#4805): pin the two anchor-pollution guards in the side-lane invariants suite lane-guard (#2170) requires a dedicated side-lane suite update alongside lane-logic changes. The #4805 settle gate and meter size guard ARE side-lane invariants (side-lane usage samples must not pollute the main conversation's usage anchor), so they are pinned as tests (K) and (K2) in tests/side-lane-invariants.test.ts, at their decision points (settleUsageReport / effectiveTokenCount), with both directions asserted (gate fires / exemptions pass). --- tests/side-lane-invariants.test.ts | 71 +++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 1755e36f7a..aaeecbd6f3 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -6,8 +6,9 @@ import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath } from "node:url"; -import { defaultConfig, ABSORB_TOOL_NAME, RULE_TOOL_NAME, RETRIEVE_TOOL_NAME } from "acp-kernel"; -import { startServer, BILI_TOOL_NAMES } from "../src/server.ts"; +import { defaultConfig, ABSORB_TOOL_NAME, RULE_TOOL_NAME, RETRIEVE_TOOL_NAME, type CoreMessage } from "acp-kernel"; +import { settleUsageReport } from "../src/cache-ledger.ts"; +import { startServer, BILI_TOOL_NAMES, effectiveTokenCount } from "../src/server.ts"; import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../src/compress-tool.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -366,3 +367,69 @@ test("(I) revival (#3402): the absorb tool name is bili-owned again", () => { test("(J) post-#3400: acp_read_original_image is bili-owned for #1897 demotion", () => { assert.ok(BILI_TOOL_NAMES.has(READ_ORIGINAL_IMAGE_TOOL_NAME), "acp_read_original_image is in the bili-owned set"); }); + +// (#4805/#4720): side-lane usage samples must never pollute the main +// conversation's usage anchor. Reported incident: a 2-message auxiliary +// request under the SAME session id settled a 5100-token sample over a +// 300K-token anchor — nudge growth could then never reach its floor, +// compression froze while real context grew past the window. Time-based +// staleness cannot catch a seconds-old contaminant, so the defense is +// directional SIZE plus credit exemption, at two independent decision +// points (each guard alone breaks the freeze loop; incident regression +// detail in tests/issue4805-anchor-pollution.test.ts). +function anchorSession(anchor: number): Session { + return { + id: "s4805", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: anchor, lastInputTokensSource: "usage", contextTokens: anchor } as never, + metadata: {}, + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + } as never as Session; +} + +function longHistory(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("(K) side-lane settle gate: a far-smaller usage sample never overwrites the usage-grade anchor", () => { + const session = anchorSession(300_000); + settleUsageReport(session, { total: 5_100, reportedCached: 128, output: 30, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 300_000, "a 5100-tok side sample must NOT clobber a 300K-tok anchor (#4805)"); + assert.equal(session.stats.lastInputTokensSource, "usage"); + assert.equal(session.stats.inputTokens, 5_100, "counters still accrue — cumulative usage stays honest"); + // same-lane reports keep writing (the gate must not over-suppress) + settleUsageReport(session, { total: 290_000, reportedCached: null, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 290_000, "main-lane reports keep updating the anchor"); + // exemptions: empty anchor, estimate-grade anchor, post-fold credit + const fresh = anchorSession(0); + settleUsageReport(fresh, { total: 5_100, reportedCached: null }); + assert.equal(fresh.stats.lastInputTokens, 5_100, "first settle on an empty anchor always writes"); + const est = anchorSession(300_000); + (est.stats as { lastInputTokensSource?: string }).lastInputTokensSource = "estimate"; + settleUsageReport(est, { total: 5_100, reportedCached: null }); + assert.equal(est.stats.lastInputTokens, 5_100, "estimate-grade anchors are always replaceable by usage"); + const folded = anchorSession(300_000); + (folded.stats as { compressCreditTokens?: number }).compressCreditTokens = 40_000; + settleUsageReport(folded, { total: 30_000, reportedCached: null }); + assert.equal(folded.stats.lastInputTokens, 0, "credit-carrying (post-fold) settles pass — our own shrinkage is not side-lane"); +}); + +test("(K2) side-lane meter guard: an impossible anchor sizes nothing — the meter falls back to the payload estimate", () => { + const session = anchorSession(5_100); + const verdict = effectiveTokenCount(session, longHistory(800), 0); + assert.notEqual(verdict.tokens, 5_100, "a 5100-tok anchor cannot describe a multi-million-char payload request"); + assert.equal(verdict.source, "estimate", "falls through to the local measurement (#4805 size guard)"); + // early-session growth stays anchored (no false trips below the absolute floor) + const small = anchorSession(800); + const early = effectiveTokenCount(small, longHistory(3), 0); + assert.equal(early.tokens, 800, "small payloads keep sizing off the anchor"); +}); From 1fd84195ec5e92a2ae1e771a46a6e9e2c937b40f Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 21:09:07 +0800 Subject: [PATCH 03/10] refactor(#4805): name the side-lane magnitude constants (review small-fix) The 8x shrink ratio and the 25K absolute-delta floor were inline magic numbers in two files. SIDE_LANE_SHRINK_RATIO now lives in cache-ledger.ts (shared by both guards so they keep agreeing on what 'the anchor cannot describe this request' means); the meter-side absolute floor is named at its single use site in server.ts. --- src/cache-ledger.ts | 9 ++++++++- src/server.ts | 12 +++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index e16ab821ce..a4f88d93a9 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1781,6 +1781,13 @@ export function noteOutboundContextEstimates( session.stats.contextTokensSource = "estimate"; } +// #4805: an order-of-magnitude gap between a usage sample and the session's current anchor is +// the side-lane signature — a main-lane resend cannot be 8x smaller than the last measurement +// (post-fold shrinkage carries a credit and is exempt below). Shared with the meter-side size +// guard in src/server.ts (effectiveTokenCount) so both guards agree on what "the anchor cannot +// describe this request" means. +export const SIDE_LANE_SHRINK_RATIO = 8; + export function settleUsageReport( session: Session, s: { total: number; reportedCached: number | null; output?: number; protocol?: string; upstream?: string }, @@ -1802,7 +1809,7 @@ export function settleUsageReport( const prevUsageGrade = session.stats.lastInputTokensSource === "usage"; const sideLaneSample = prevUsageGrade && prevAnchor > 0 && (session.stats.compressCreditTokens ?? 0) <= 0 - && s.total * 8 < prevAnchor; + && s.total * SIDE_LANE_SHRINK_RATIO < prevAnchor; if (sideLaneSample) { session.stats.inputTokens += s.total; loggerLog("info", `[${session.id}] usage sample ${s.total} tok is ~${Math.round(prevAnchor / Math.max(1, s.total))}× smaller than the ${prevAnchor}-tok anchor — treated as side-lane traffic, anchor kept (#4805)`); diff --git a/src/server.ts b/src/server.ts index 98faaf5fc6..746c545ce7 100644 --- a/src/server.ts +++ b/src/server.ts @@ -84,7 +84,7 @@ import { AUTO_FOLD_TARGET_MIN, AUTO_FOLD_MIN_BENEFIT_TOKENS } from "./external-s import { auditStartupSummaryCredentials } from "./external-summary-credentials.js"; import { applyRanges } from "./stream.js"; import { attachSubagentSessions } from "./subagent-sessions.js"; -import { buildSessionCacheReport, handleAcpCache, learnedImageReserve, noteClientAbort, readKeySwitchStats, readModelSwitchStats, settleUsageReport } from "./cache-ledger.js"; +import { buildSessionCacheReport, handleAcpCache, learnedImageReserve, noteClientAbort, readKeySwitchStats, readModelSwitchStats, settleUsageReport, SIDE_LANE_SHRINK_RATIO } from "./cache-ledger.js"; import { extractBillingAttributionBlock, extractSummaryFromSse, preflightCompress, estimateCoreMessages, estimateCoreMessagesUpper, estimateCoreMessagesUpperBytes, describeCoreTextDuplication, estimateRawBodyTokens, type PreflightResult } from "./preflight.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, DEFAULT_DECIDE_MAX_TOKENS, DECIDE_TIMEOUT_MS, extractDecisionText, ladderMode, parseDecision, recordDecision, resolveDecisionRange, type DecideConfig, type DecisionOutcome } from "./nudge-decide.js"; import { gcConfigFromEnv, gcSessionFiles } from "./session-gc.js"; @@ -2475,6 +2475,12 @@ export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: strin // all require usage >= their pct lines). #728 fix: such sessions fall back to // the PREVIOUS turn's locally-measured outbound payload upper bound // (session.stats.localInputEstimate, recorded in prepare* each turn). The +// #4805: the meter-side size guard (anchorImpossible below) also requires an ABSOLUTE gap, +// not just the ratio: on early/small sessions the char-count upper bound is already an order +// of magnitude above the real token count, so a pure-ratio test would demote healthy small +// anchors to estimates and lose usage-grade sizing exactly where it is cheapest to keep. +const SIDE_LANE_ANCHOR_MIN_ABS_DELTA = 25_000; + // estimator only errs EARLY (char-count upper bound → compress earlier, never // later), is active only while lastInputTokens == 0 (a real usage report takes // precedence immediately — same invariant as #604's armFailureShrink @@ -2514,8 +2520,8 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // local estimate then — the nudge sizes the payload it actually sees. const payloadUpperNow = estimateCoreMessagesUpper(msgs) + inboundImageTokens; const anchorImpossible = session.stats.lastInputTokens > 0 - && payloadUpperNow >= session.stats.lastInputTokens * 8 - && payloadUpperNow - session.stats.lastInputTokens > 25_000; + && payloadUpperNow >= session.stats.lastInputTokens * SIDE_LANE_SHRINK_RATIO + && payloadUpperNow - session.stats.lastInputTokens > SIDE_LANE_ANCHOR_MIN_ABS_DELTA; 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" }; // #1820: right after a preflight rebuild the usage-grade baseline above is // momentarily absent (the rebuild request's own report hasn't landed yet, From f371501315c2931a5b4fb6be7371361d57f1f09b Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 21:09:07 +0800 Subject: [PATCH 04/10] test(#4805): pin settle-gate one-directionality (review small-fix) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PR body claims growth direction is never gated ('增长方向的 usage 永远照写') but no test asserted it — T1 only covered a same-scale (300K->290K) follow-up. T1c pins a far-LARGER sample (5M over a 300K anchor) writing through. --- tests/issue4805-anchor-pollution.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/issue4805-anchor-pollution.test.ts b/tests/issue4805-anchor-pollution.test.ts index 4ccfa4923d..bb512d27c2 100644 --- a/tests/issue4805-anchor-pollution.test.ts +++ b/tests/issue4805-anchor-pollution.test.ts @@ -56,6 +56,12 @@ test("T1 settle gate: a usage sample 8x smaller than the anchor is side-lane pol assert.equal(session.stats.lastInputTokens, 290_000, "main-lane reports keep updating the anchor"); }); +test("T1c settle gate is one-directional: a far-LARGER sample always writes through", () => { + const session = sessionWith(300_000); + settleUsageReport(session, { total: 5_000_000, reportedCached: null, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 5_000_000, "growth direction is never gated — only far-smaller samples are"); +}); + test("T1b settle gate exemptions: no anchor yet, non-usage anchor, and post-fold credit settles all pass", () => { const fresh = sessionWith(0); settleUsageReport(fresh, { total: 5_100, reportedCached: null }); From 1715f9cfed121c14c1635066b13d1ab2eef2e862 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 26 Oct 2026 22:19:17 +0800 Subject: [PATCH 05/10] fix(#4805): meter-side anchor guard keyed to the producing request's payload snapshot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settle-side magnitude gate rejected during review: a far-smaller usage sample cannot be distinguished from the #1492 B self-heal (the next real report superseding a poisoned above-window anchor) at the settle chokepoint — magnitude alone is ambiguous there, and gating it froze the self-heal path (#1492 B, #1820 B both red on the previous push). Settle now stays unconditional (usage-grade truth always supersedes) and records WHICH request produced the anchor: stats.lastUsagePayloadUpper snapshots the producer's own outbound upper (localInputEstimate) at settle time. effectiveTokenCount refuses an anchor only when the payload at hand towers over BOTH the anchor and its producer's snapshot (8x each, >25K absolute gap) — #4805's side-lane sample (producer ~2.6K upper, main payload ~1.2M) is refused, while #1820 B's honest under-reporting anchor (producer at this scale) keeps ground-truth authority. --- src/cache-ledger.ts | 27 +---------- src/persist.ts | 1 + src/server.ts | 14 +++++- src/session.ts | 5 ++ tests/issue4805-anchor-pollution.test.ts | 59 ++++++++++++++---------- tests/side-lane-invariants.test.ts | 54 +++++++++++++++------- 6 files changed, 92 insertions(+), 68 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index a4f88d93a9..300fd07c54 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1781,13 +1781,6 @@ export function noteOutboundContextEstimates( session.stats.contextTokensSource = "estimate"; } -// #4805: an order-of-magnitude gap between a usage sample and the session's current anchor is -// the side-lane signature — a main-lane resend cannot be 8x smaller than the last measurement -// (post-fold shrinkage carries a credit and is exempt below). Shared with the meter-side size -// guard in src/server.ts (effectiveTokenCount) so both guards agree on what "the anchor cannot -// describe this request" means. -export const SIDE_LANE_SHRINK_RATIO = 8; - export function settleUsageReport( session: Session, s: { total: number; reportedCached: number | null; output?: number; protocol?: string; upstream?: string }, @@ -1796,25 +1789,6 @@ export function settleUsageReport( // #793: a zero-total sample carries no information (gateway placeholder or // relay echo) — it must not clobber the last trusted lastInputTokens. if (s.total > 0) { - // #4805 source gate: a usage sample an order of magnitude SMALLER than - // the session's current usage-grade anchor is side-lane pollution (a - // 2-message auxiliary request under the same session id), not a main- - // lane measurement — writing it froze compression until the context - // blew the window. Skip the ANCHOR fields (counters and calibration - // still run; a later main-lane report restores them). One-directional: - // only a far-SMALLER sample is gated. Exempt settles carrying a - // compress credit — those are our own post-fold re-requests, whose - // shrinkage is the fold's doing, not a lane change. - const prevAnchor = session.stats.lastInputTokens; - const prevUsageGrade = session.stats.lastInputTokensSource === "usage"; - const sideLaneSample = prevUsageGrade && prevAnchor > 0 - && (session.stats.compressCreditTokens ?? 0) <= 0 - && s.total * SIDE_LANE_SHRINK_RATIO < prevAnchor; - if (sideLaneSample) { - session.stats.inputTokens += s.total; - loggerLog("info", `[${session.id}] usage sample ${s.total} tok is ~${Math.round(prevAnchor / Math.max(1, s.total))}× smaller than the ${prevAnchor}-tok anchor — treated as side-lane traffic, anchor kept (#4805)`); - return; - } session.stats.inputTokens += s.total; // Net out this turn's compress credit: the post-compress re-request // re-sends the unfolded history, so its usage report over-reports the @@ -1825,6 +1799,7 @@ export function settleUsageReport( // never by estimate-grade samples or arming paths; dropped at native- // compaction boundaries via resetSessionCompression (session.ts). session.stats.lastUsageGradeTokens = session.stats.lastInputTokens; + session.stats.lastUsagePayloadUpper = session.stats.localInputEstimate ?? 0; session.stats.lastInputTokensSource = "usage"; // #3148: a real usage report supersedes the failed-round arm — drop the // visibility marker so acp_status stops advertising a stale emergency state. diff --git a/src/persist.ts b/src/persist.ts index a6aa944109..3a3fbf5794 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -167,6 +167,7 @@ interface PersistedSession { // pendingFoldUsage / lastLocalTextEstimate(+Origin), which ride along // but are deliberately not restored — see buildSession). localInputEstimate?: number; + lastUsagePayloadUpper?: number; lastUsageGradeTokens?: number; calibratedEstimate?: number; calibratedEstimateOrigin?: string; diff --git a/src/server.ts b/src/server.ts index 746c545ce7..a25b07cf3f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -84,7 +84,7 @@ import { AUTO_FOLD_TARGET_MIN, AUTO_FOLD_MIN_BENEFIT_TOKENS } from "./external-s import { auditStartupSummaryCredentials } from "./external-summary-credentials.js"; import { applyRanges } from "./stream.js"; import { attachSubagentSessions } from "./subagent-sessions.js"; -import { buildSessionCacheReport, handleAcpCache, learnedImageReserve, noteClientAbort, readKeySwitchStats, readModelSwitchStats, settleUsageReport, SIDE_LANE_SHRINK_RATIO } from "./cache-ledger.js"; +import { buildSessionCacheReport, handleAcpCache, learnedImageReserve, noteClientAbort, readKeySwitchStats, readModelSwitchStats, settleUsageReport } from "./cache-ledger.js"; import { extractBillingAttributionBlock, extractSummaryFromSse, preflightCompress, estimateCoreMessages, estimateCoreMessagesUpper, estimateCoreMessagesUpperBytes, describeCoreTextDuplication, estimateRawBodyTokens, type PreflightResult } from "./preflight.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, DEFAULT_DECIDE_MAX_TOKENS, DECIDE_TIMEOUT_MS, extractDecisionText, ladderMode, parseDecision, recordDecision, resolveDecisionRange, type DecideConfig, type DecisionOutcome } from "./nudge-decide.js"; import { gcConfigFromEnv, gcSessionFiles } from "./session-gc.js"; @@ -2480,6 +2480,14 @@ export function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: strin // of magnitude above the real token count, so a pure-ratio test would demote healthy small // anchors to estimates and lose usage-grade sizing exactly where it is cheapest to keep. const SIDE_LANE_ANCHOR_MIN_ABS_DELTA = 25_000; +// The anchor's OWN request snapshot (lastUsagePayloadUpper, taken at settle) +// carries the honesty margin: a usage report can legitimately under-report a +// text-heavy payload by ~8x (#1820 B — char-bound 220k over a 5k report), so the +// anchor size alone cannot prove pollution; requiring the payload to ALSO tower +// 8x over the payload of the request that produced the anchor keeps #1820 B's +// 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; // estimator only errs EARLY (char-count upper bound → compress earlier, never // later), is active only while lastInputTokens == 0 (a real usage report takes @@ -2520,7 +2528,9 @@ export function effectiveTokenCount(session: Session, msgs: CoreMessage[], inbou // local estimate then — the nudge sizes the payload it actually sees. const payloadUpperNow = estimateCoreMessagesUpper(msgs) + inboundImageTokens; const anchorImpossible = session.stats.lastInputTokens > 0 - && payloadUpperNow >= session.stats.lastInputTokens * SIDE_LANE_SHRINK_RATIO + && (session.stats.lastUsagePayloadUpper ?? 0) > 0 + && 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; 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" }; // #1820: right after a preflight rebuild the usage-grade baseline above is diff --git a/src/session.ts b/src/session.ts index 278a75effe..6daa094da4 100644 --- a/src/session.ts +++ b/src/session.ts @@ -396,6 +396,11 @@ export type Session = { * Cleared by resetSessionCompression (native-compaction boundary). * Persisted (survives restart like the rest of stats). */ localInputEstimate?: number; + /** #4805: the payload upper estimate of the request whose usage report + * wrote the current usage-grade anchor — the meter-side size guard + * compares it against the payload at hand to refuse anchors that + * cannot describe this conversation (side-lane pollution). */ + lastUsagePayloadUpper?: number; /** #2117: billing-caliber estimate of the CURRENT outbound request — * estimateCoreMessages + wire overhead, k̂-scaled only where the * factor's route+model provenance matches (#1933 F1 / #2117 B), plus diff --git a/tests/issue4805-anchor-pollution.test.ts b/tests/issue4805-anchor-pollution.test.ts index bb512d27c2..e1cbeb6281 100644 --- a/tests/issue4805-anchor-pollution.test.ts +++ b/tests/issue4805-anchor-pollution.test.ts @@ -15,12 +15,18 @@ import type { CoreMessage } from "acp-kernel"; // 1.2M — compression permanently stopped, and once requests started failing // (no usage on failure) nothing could repair the anchor. Two guards, each // independently breaking the loop: -// - settleUsageReport: a usage sample >=8x smaller than the current -// usage-grade anchor (with no compress credit) is side-lane pollution — -// counters only, anchor kept; -// - effectiveTokenCount: a fresh anchor whose size is impossible for the -// payload at hand (upper bound >=8x the anchor, >25K absolute gap) cannot -// size THIS request — fall through to the local estimate. +// Final design (settle-side magnitude gate REJECTED during PR review): +// a far-smaller usage sample CANNOT be distinguished from the #1492 B +// self-heal (next real report superseding a poisoned 1.2M anchor) at the +// settle chokepoint — magnitude alone is ambiguous there. Settle stays +// unconditional (usage-grade truth always supersedes) and records WHICH +// request produced the anchor (stats.lastUsagePayloadUpper = the producer's +// own payload upper estimate, snapshotted at settle from localInputEstimate): +// - effectiveTokenCount refuses an anchor only when the payload at hand +// towers over BOTH the anchor and its producer's snapshot (8x each, >25K +// absolute) — a #4805 side-lane sample (producer ~2600 upper, main payload +// ~1.2M) is refused, while #1820 B's honest under-reporting anchor +// (producer 220K upper, same-scale payload) keeps ground-truth authority. function sessionWith(anchor: number): Session { return { @@ -45,45 +51,50 @@ function bigHistory(count: number): CoreMessage[] { return msgs; } -test("T1 settle gate: a usage sample 8x smaller than the anchor is side-lane pollution — anchor kept, counters still accrue", () => { +test("T1 settle keeps usage truth: the far-smaller sample supersedes AND snapshots its producer's payload", () => { const session = sessionWith(300_000); + session.stats.localInputEstimate = 2_600; // the side-lane request's own outbound upper settleUsageReport(session, { total: 5_100, reportedCached: 128, output: 30, protocol: "openai" }); - assert.equal(session.stats.lastInputTokens, 300_000, "the 5100-tok side sample must NOT overwrite a 300K anchor (#4805)"); - assert.equal(session.stats.lastInputTokensSource, "usage"); + assert.equal(session.stats.lastInputTokens, 5_100, "usage-grade samples supersede unconditionally (#1492 B self-heal)"); + assert.equal(session.stats.lastUsagePayloadUpper, 2_600, "the anchor now carries its producing request's payload snapshot"); assert.equal(session.stats.inputTokens, 5_100, "the sample still counts toward cumulative usage"); - // A same-lane report (300K -> 290K, ratio fine) writes normally. + // A same-lane report (producer at main scale) writes and re-snapshots. + session.stats.localInputEstimate = 295_000; settleUsageReport(session, { total: 290_000, reportedCached: null, protocol: "openai" }); assert.equal(session.stats.lastInputTokens, 290_000, "main-lane reports keep updating the anchor"); + assert.equal(session.stats.lastUsagePayloadUpper, 295_000, "the snapshot follows the producing request"); }); -test("T1c settle gate is one-directional: a far-LARGER sample always writes through", () => { - const session = sessionWith(300_000); - settleUsageReport(session, { total: 5_000_000, reportedCached: null, protocol: "openai" }); - assert.equal(session.stats.lastInputTokens, 5_000_000, "growth direction is never gated — only far-smaller samples are"); -}); - -test("T1b settle gate exemptions: no anchor yet, non-usage anchor, and post-fold credit settles all pass", () => { +test("T1b settle anchors: empty anchors always write; post-fold credit nets through", () => { const fresh = sessionWith(0); settleUsageReport(fresh, { total: 5_100, reportedCached: null }); assert.equal(fresh.stats.lastInputTokens, 5_100, "first settle on an empty anchor always writes"); - const estimate = sessionWith(300_000); - (estimate.stats as { lastInputTokensSource?: string }).lastInputTokensSource = "estimate"; - settleUsageReport(estimate, { total: 5_100, reportedCached: null }); - assert.equal(estimate.stats.lastInputTokens, 5_100, "an estimate-grade anchor is always replaceable by usage"); - const folded = sessionWith(300_000); (folded.stats as { compressCreditTokens?: number }).compressCreditTokens = 40_000; settleUsageReport(folded, { total: 30_000, reportedCached: null }); assert.equal(folded.stats.lastInputTokens, 0, "a credit-carrying (post-fold) settle nets against the credit and writes — our own shrinkage is not side-lane"); }); -test("T2 meter guard: even with a polluted 5100 anchor, a 1.2M-token payload is sized by its own estimate", () => { +test("T2 meter guard: a polluted anchor from a tiny producer cannot size a 1.2M-token payload", () => { const session = sessionWith(5_100); - const msgs = bigHistory(800); // ~4.8M chars -> ~1.2M tokens upper bound >> 5100*8 + session.stats.contextTokens = 5_100; + (session.stats as { contextTokensSource?: string }).contextTokensSource = "usage"; + session.stats.localInputEstimate = 0; // demotion below must come from the size guard alone + session.stats.lastUsagePayloadUpper = 2_600; // the side-lane producer's own upper + const msgs = bigHistory(800); // ~4.8M chars -> upper >> anchor*8 and >> snapshot*8 const verdict = effectiveTokenCount(session, msgs, 0); assert.notEqual(verdict.tokens, 5_100, `the impossible anchor must not size a ${verdict.tokens}-token payload request`); assert.equal(verdict.source, "estimate", "falls through to the local measurement (#4805 size guard)"); + // #1820 B honesty margin: an anchor produced by a request at THIS scale keeps + // authority even when its upstream under-reports 8x. + const honest = sessionWith(5_100); + honest.stats.contextTokens = 5_100; + (honest.stats as { contextTokensSource?: string }).contextTokensSource = "usage"; + 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)"); // Small-session growth stays on the anchor: no absolute-gap trip below 25K. const small = sessionWith(800); const earlyMsgs = bigHistory(3); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index aaeecbd6f3..8954a2d17c 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -400,36 +400,58 @@ function longHistory(count: number): CoreMessage[] { return msgs; } -test("(K) side-lane settle gate: a far-smaller usage sample never overwrites the usage-grade anchor", () => { +test("(K) settle keeps usage-grade truth: a far-smaller usage sample supersedes AND snapshots its payload size (#4805/#1492 B)", () => { + // #1492 B contract: the next real usage report ALWAYS self-heals the anchor — + // the settle side must not magnitude-gate (a poisoned 1.2M anchor over a + // folded 5K conversation is the SAME small-sample shape as side-lane + // pollution). #4805's protection moved entirely to the meter (K2): settle + // records WHICH request produced the anchor (lastUsagePayloadUpper) so the + // meter can refuse anchors written by tiny requests. const session = anchorSession(300_000); + session.stats.localInputEstimate = 2_600; // the side-lane request's own payload upper settleUsageReport(session, { total: 5_100, reportedCached: 128, output: 30, protocol: "openai" }); - assert.equal(session.stats.lastInputTokens, 300_000, "a 5100-tok side sample must NOT clobber a 300K-tok anchor (#4805)"); - assert.equal(session.stats.lastInputTokensSource, "usage"); - assert.equal(session.stats.inputTokens, 5_100, "counters still accrue — cumulative usage stays honest"); - // same-lane reports keep writing (the gate must not over-suppress) + assert.equal(session.stats.lastInputTokens, 5_100, "usage-grade samples supersede unconditionally (#1492 B self-heal)"); + assert.equal(session.stats.lastUsagePayloadUpper, 2_600, "the anchor carries its producing request's payload snapshot"); + assert.equal(session.stats.inputTokens, 5_100, "counters accrue"); + // same-lane reports keep writing and re-snapshot + session.stats.localInputEstimate = 295_000; settleUsageReport(session, { total: 290_000, reportedCached: null, protocol: "openai" }); assert.equal(session.stats.lastInputTokens, 290_000, "main-lane reports keep updating the anchor"); - // exemptions: empty anchor, estimate-grade anchor, post-fold credit + assert.equal(session.stats.lastUsagePayloadUpper, 295_000, "the snapshot follows the producing request"); + // first settle on an empty anchor always writes const fresh = anchorSession(0); settleUsageReport(fresh, { total: 5_100, reportedCached: null }); assert.equal(fresh.stats.lastInputTokens, 5_100, "first settle on an empty anchor always writes"); - const est = anchorSession(300_000); - (est.stats as { lastInputTokensSource?: string }).lastInputTokensSource = "estimate"; - settleUsageReport(est, { total: 5_100, reportedCached: null }); - assert.equal(est.stats.lastInputTokens, 5_100, "estimate-grade anchors are always replaceable by usage"); - const folded = anchorSession(300_000); - (folded.stats as { compressCreditTokens?: number }).compressCreditTokens = 40_000; - settleUsageReport(folded, { total: 30_000, reportedCached: null }); - assert.equal(folded.stats.lastInputTokens, 0, "credit-carrying (post-fold) settles pass — our own shrinkage is not side-lane"); }); -test("(K2) side-lane meter guard: an impossible anchor sizes nothing — the meter falls back to the payload estimate", () => { +test("(K2) side-lane meter guard: an anchor written by a request far smaller than the payload at hand sizes nothing (#4805/#1820 B)", () => { + // #4805 shape: anchor 5100 written by a ~2600-upper side request; the main + // payload towers over BOTH the anchor and its producing request's snapshot. const session = anchorSession(5_100); + session.stats.contextTokens = 5_100; + (session.stats as { contextTokensSource?: string }).contextTokensSource = "usage"; + session.stats.localInputEstimate = 0; // not cycle-stale: the demotion below must come from the size guard alone + session.stats.lastUsagePayloadUpper = 2_600; const verdict = effectiveTokenCount(session, longHistory(800), 0); - assert.notEqual(verdict.tokens, 5_100, "a 5100-tok anchor cannot describe a multi-million-char payload request"); + assert.notEqual(verdict.tokens, 5_100, "a 5100-tok anchor from a 2600-upper request cannot describe a multi-million-char payload"); assert.equal(verdict.source, "estimate", "falls through to the local measurement (#4805 size guard)"); + // #1820 B shape: an HONEST anchor whose request was this big (its upstream + // under-reports 8x) keeps the fast path — the snapshot term is the margin. + const honest = anchorSession(5_100); + honest.stats.contextTokens = 5_100; + (honest.stats as { contextTokensSource?: string }).contextTokensSource = "usage"; + honest.stats.localInputEstimate = 0; + honest.stats.lastUsagePayloadUpper = 220_000; + // payload at the anchor-producer's own scale (~240k upper over a 220k snapshot) + const trusted = effectiveTokenCount(honest, longHistory(40), 0); + assert.equal(trusted.source, "usage", "an anchor written by THIS-size request stays ground truth (#1820 B)"); + assert.equal(trusted.tokens, 5_100, "usage-grade sizing preserved"); // early-session growth stays anchored (no false trips below the absolute floor) const small = anchorSession(800); + small.stats.contextTokens = 800; + (small.stats as { contextTokensSource?: string }).contextTokensSource = "usage"; + small.stats.localInputEstimate = 0; + small.stats.lastUsagePayloadUpper = 900; const early = effectiveTokenCount(small, longHistory(3), 0); assert.equal(early.tokens, 800, "small payloads keep sizing off the anchor"); }); From 2918e45bdf6973aed59b31e28c0f0924333fcc4d Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 26 Oct 2026 23:36:28 +0800 Subject: [PATCH 06/10] =?UTF-8?q?fix(#4805):=20settle=20membership=20gate?= =?UTF-8?q?=20=E2=80=94=20foreign=20id=20families=20cannot=20rewrite=20the?= =?UTF-8?q?=20session=20anchor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Second, identity-based guard on top of the meter-side size guard: a usage report now only writes the anchor family when the request that produced it joined the session's known message-id family (envelope biliMsgIds first, resent tool-call ids otherwise). Auxiliary host personas riding the same conversation id carry zero known ids — their settles count toward cumulative usage but can no longer touch the anchor. Abstains on thin evidence (first turns, tool-less sidecars); the #2821 id re-mint family self-heals on the second resend via the main-scale recording tiebreak. --- src/cache-ledger.ts | 14 ++++ src/request-membership.ts | 81 ++++++++++++++++++++++++ src/server/prepare-anthropic.ts | 6 ++ src/server/prepare-openai.ts | 5 ++ src/session.ts | 10 +++ tests/issue4805-anchor-pollution.test.ts | 75 ++++++++++++++++++++++ 6 files changed, 191 insertions(+) create mode 100644 src/request-membership.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 300fd07c54..0c1f5f5078 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -18,6 +18,7 @@ import { import { log as loggerLog } from "./logger.js"; import { METADATA_CANONICAL_SYS_FP, METADATA_FOLD_COVERAGE, METADATA_LAST_RESPONSES_MODE, METADATA_SYSTEM_FP } from "./fold-reconcile.js"; import { clearPostRebuildAnchor, markDirty, type Session } from "./session.js"; +import { isForeignMembership } from "./request-membership.js"; import { normalizeUpstreamOrigin } from "./util.js"; import { toolFail, toolOk, type ProxyToolResult } from "./proxy-tool-result.js"; @@ -1790,6 +1791,18 @@ export function settleUsageReport( // relay echo) — it must not clobber the last trusted lastInputTokens. if (s.total > 0) { session.stats.inputTokens += s.total; + // #4805: id-membership gate — a report produced by a request whose + // message ids do not join this session's known id family is foreign + // billing data under a colliding session key (auxiliary host personas + // stamp the host conversation id on their own tiny payloads). It + // still counts toward cumulative usage, but it is not this + // conversation's truth and must not touch the anchor family. Abstain + // paths (no evidence / first turns / #2821 re-mints) settle exactly + // as before: the gate only ever refuses the clearly-foreign. + if (isForeignMembership(session.transient?.requestMembership)) { + const m = session.transient!.requestMembership!; + loggerLog("info", `[${session.id}] [membership] usage sample (total=${s.total}) refused anchor write: its request carried no known message ids (${m.overlap}/${m.total} joined, ${m.known} known) — foreign payload under a shared session id (#4805)`); + } else { // Net out this turn's compress credit: the post-compress re-request // re-sends the unfolded history, so its usage report over-reports the // context the NEXT request will actually carry (see stream.ts applyRanges). @@ -1836,6 +1849,7 @@ export function settleUsageReport( // re-arms both in one event). delete session.stats.overflowArmTokens; delete session.stats.overflowArmCompletionReserve; + } } // #1933 F1: roll the pending pair forward to the current turn's value — // it will be consumed by the NEXT report (or cleared when the lane diff --git a/src/request-membership.ts b/src/request-membership.ts new file mode 100644 index 0000000000..ae1d428b3a --- /dev/null +++ b/src/request-membership.ts @@ -0,0 +1,81 @@ +import type { CoreMessage } from "acp-kernel"; +import type { Session } from "./session.js"; + +// #4805: settle-side membership gate. A usage report is only "this +// conversation's truth" when the request that produced it actually carried +// this conversation's messages. Hosts stamp auxiliary personas (dsh +// auto-review and its relatives) with the SAME conversation id, so the +// session key alone cannot tell a main-turn report from a foreign +// 2-message one — but the message ids can: a main turn resends history the +// session already knows, while an auxiliary payload brings ids the session +// has never seen. Evidence is id-based ONLY (no content hashing — host ids +// are stable per-lane observation; content joins stay with fold-reconcile): +// - plugin lanes: the biliMsgIds envelope the host stamps alongside the +// message array (#4491), read regardless of the pin rollout state; +// - every wire: the tool-call ids resent in the history (model-minted, +// client-echoed — the only ids the raw protocol wires carry). +// The verdict abstains (never gates) when evidence is thin: first turns, +// tool-less sidecars, or the #2821 id re-mint family — a re-minted main +// turn loses at most ONE anchor update and self-heals on the next resend +// (its ids then join the known family). +export const MEMBERSHIP_HISTORY_CAP = 4000; +export const MEMBERSHIP_MIN_KNOWN_IDS = 8; +export const MEMBERSHIP_MIN_REQUEST_IDS = 4; +export const MEMBERSHIP_MIN_OVERLAP_RATIO = 0.25; +// #2821 tiebreak: a main turn whose ids were ALL re-minted reads as foreign +// too — but unlike an auxiliary persona it carries main-scale payload. Only +// at-conversation-scale requests may join the known family on a zero-overlap +// verdict, so the re-mint heals on its second resend while a tiny polluter +// (payload ~1% of the anchor) never graduates. +export const MEMBERSHIP_RECORD_SCALE_RATIO = 0.25; + +export type RequestMembership = { overlap: number; total: number; known: number }; + +export function collectRequestIds(envelopeIds: unknown, msgs: CoreMessage[]): string[] { + const out: string[] = []; + const seen = new Set(); + if (Array.isArray(envelopeIds)) { + for (const v of envelopeIds) { + if (typeof v === "string" && v.length > 0 && v.length <= 512 && !v.startsWith("acp_summary_") && !seen.has(v)) { + seen.add(v); + out.push(v); + } + } + if (out.length > 0) return out; + } + for (const m of msgs) { + const id = m.toolCallId; + if (typeof id === "string" && id.length > 0 && !seen.has(id)) { + seen.add(id); + out.push(id); + } + } + return out; +} + +export function isForeignMembership(m: RequestMembership | null | undefined): boolean { + if (!m) return false; + return m.known >= MEMBERSHIP_MIN_KNOWN_IDS && m.total >= MEMBERSHIP_MIN_REQUEST_IDS && m.overlap < m.total * MEMBERSHIP_MIN_OVERLAP_RATIO; +} + +export function classifyAndRecordMembership(session: Session, ids: string[], payloadUpper: number): RequestMembership | null { + if (ids.length === 0) return null; + const t = session.transient; + if (!t) return null; + const known = new Set(t.knownMessageIds); + let overlap = 0; + for (const id of ids) if (known.has(id)) overlap++; + const verdict: RequestMembership = { overlap, total: ids.length, known: known.size }; + t.requestMembership = verdict; + // Only admitted, evidence-poor, or main-scale requests teach the session + // its id family — a tiny foreign payload's ids must never enter the known + // set, or a persistent auxiliary persona would graduate to "member" on + // its second visit and reopen #4805 through the front door. + const mainScale = session.stats.lastInputTokens <= 0 || payloadUpper >= session.stats.lastInputTokens * MEMBERSHIP_RECORD_SCALE_RATIO; + if (!isForeignMembership(verdict) || mainScale) { + for (const id of ids) known.add(id); + const all = [...known]; + t.knownMessageIds = all.length > MEMBERSHIP_HISTORY_CAP ? all.slice(all.length - MEMBERSHIP_HISTORY_CAP) : all; + } + return verdict; +} diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index fa57f1e68e..f3c8309903 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -41,6 +41,7 @@ import { injectSystem, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; import { featureEnabled } from "../rollout.js"; import { MSG_IDS_BODY_FIELD, MSG_SEQS_BODY_FIELD, applyMsgIdPin, applyMsgSeqStamps, validateMsgIds, validateMsgSeqs } from "../msgid-pin.js"; +import { classifyAndRecordMembership, collectRequestIds } from "../request-membership.js"; import { alignIdSet, looksLikeSummaryCarrier } from "../msgid-algebra.js"; export async function prepareAnthropic( @@ -594,6 +595,11 @@ export async function prepareAnthropic( session.stats.lastLocalTextEstimate = estimateCoreMessages(processedMessages.length > 0 ? processedMessages : originalMessages) + countSystemAndToolsTokens(extractSystem(systemOut), toolsOut) + imageReserveFor(session, "anthropic", rebuilt, opts, upstreamOrigin); + // #4805: id-membership evidence for the settle gate — the envelope ids + // when the lane sends them (any rollout state), else the tool-call ids + // resent in the history. Placed after the payload row above so the + // main-scale tiebreak sees this request's own upper bound. + classifyAndRecordMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0); if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin; return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 0767039e0b..876e2936fc 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -37,6 +37,7 @@ import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, e import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; +import { classifyAndRecordMembership, collectRequestIds } from "../request-membership.js"; export async function prepareOpenai( parsed: OpenAIRequestBody, @@ -494,6 +495,10 @@ export async function prepareOpenai( + imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin); const openaiPairOrigin = billingUpstream ?? upstreamOrigin; if (openaiPairOrigin) session.stats.lastLocalTextEstimateOrigin = openaiPairOrigin; + // #4805: id-membership evidence for the settle gate (see the + // anthropic-lane counterpart) — chat wire has no msgid envelope, so + // evidence is the tool-call ids resent in the history. + classifyAndRecordMembership(session, collectRequestIds(undefined, processedMessages.length > 0 ? processedMessages : originalMessages), session.stats.localInputEstimate ?? 0); } snapshotMessages(session, originalMessages); markDirty(session); diff --git a/src/session.ts b/src/session.ts index 6daa094da4..cf790b9da2 100644 --- a/src/session.ts +++ b/src/session.ts @@ -249,6 +249,16 @@ export type Session = { * bili's rebuild. */ inBody?: InboundSlot; settledInBody?: InboundSlot; + /** #4805: message ids this session has ADMITTED across its requests + * (envelope biliMsgIds first, tool-call ids otherwise) — the known + * family the settle-side membership gate joins each request against. + * In-memory only: after a restart the first request rebuilds it and + * the gate abstains while evidence is thin. */ + knownMessageIds?: string[]; + /** #4805: the in-flight request's membership verdict, recorded at + * prepare and consumed by settleUsageReport — {overlap, total, + * known} scalars only. */ + requestMembership?: { overlap: number; total: number; known: number }; }; id: string; /** Identity / descriptive metadata. Populated on first request. */ diff --git a/tests/issue4805-anchor-pollution.test.ts b/tests/issue4805-anchor-pollution.test.ts index e1cbeb6281..67f5267c9a 100644 --- a/tests/issue4805-anchor-pollution.test.ts +++ b/tests/issue4805-anchor-pollution.test.ts @@ -5,6 +5,7 @@ process.env.NODE_ENV = "test"; import { settleUsageReport } from "../src/cache-ledger.ts"; import { effectiveTokenCount } from "../src/server.ts"; +import { classifyAndRecordMembership, collectRequestIds, isForeignMembership } from "../src/request-membership.ts"; import type { Session } from "../src/session.ts"; import type { CoreMessage } from "acp-kernel"; @@ -40,6 +41,7 @@ function sessionWith(anchor: number): Session { inFlight: 0, persisted: false, pendingRetrievals: [], + transient: {}, } as never as Session; } @@ -101,3 +103,76 @@ test("T2 meter guard: a polluted anchor from a tiny producer cannot size a 1.2M- const early = effectiveTokenCount(small, earlyMsgs, 0); assert.equal(early.tokens, 800, "early-session anchors keep sizing small payloads (no false trips)"); }); + +// T3: the id-membership settle gate (#4805, second guard). The host stamps +// auxiliary personas with the SAME conversation id, so the session key alone +// cannot attribute a usage report — but the resent message ids can: a main +// turn joins the session's known id family, a foreign payload brings ids the +// session has never seen. +test("T3 settle membership gate: a foreign id family cannot rewrite the anchor", () => { + const session = sessionWith(0); + // Main turn 1: 12 ids, none known yet — first-turn abstention settles. + classifyAndRecordMembership(session, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); + settleUsageReport(session, { total: 300_000, reportedCached: null, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 300_000, "admitted first settle writes (known-family bootstrap)"); + // Foreign payload under the same session id: 5 ids, 0 joined, 12 known. + const foreignIds = ["X1", "X2", "X3", "X4", "X5"]; + classifyAndRecordMembership(session, foreignIds, 2_600); + assert.ok(isForeignMembership(session.transient?.requestMembership), "verdict marks the payload foreign"); + settleUsageReport(session, { total: 5_100, reportedCached: 128, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 300_000, "the foreign sample cannot touch the anchor (#4805)"); + assert.equal(session.stats.inputTokens, 305_100, "it still counts toward cumulative usage"); + assert.equal(session.stats.lastUsagePayloadUpper, 0, "no producer snapshot from a refused settle"); + // Foreign ids never enter the known family: a persistent auxiliary + // persona must not graduate to member on its second visit. + classifyAndRecordMembership(session, foreignIds, 2_600); + assert.ok(isForeignMembership(session.transient?.requestMembership), "repeat foreign payloads stay foreign"); + settleUsageReport(session, { total: 4_800, reportedCached: null, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 300_000, "repeat pollution stays gated"); + // Main conversation recovers: resent ids join, the settle writes again. + classifyAndRecordMembership(session, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12", "M13"], 320_000); + assert.ok(!isForeignMembership(session.transient?.requestMembership), "main resends join their family"); + settleUsageReport(session, { total: 320_000, reportedCached: null, protocol: "openai" }); + assert.equal(session.stats.lastInputTokens, 320_000, "main-lane reports keep updating the anchor"); +}); + +test("T3b membership abstention: thin evidence settles exactly as before", () => { + // First turn (no known family) must not gate anything. + const fresh = sessionWith(0); + classifyAndRecordMembership(fresh, ["A", "B", "C", "D"], 5_000); + assert.ok(!isForeignMembership(fresh.transient?.requestMembership), "known < 8 abstains"); + settleUsageReport(fresh, { total: 5_100, reportedCached: null }); + assert.equal(fresh.stats.lastInputTokens, 5_100, "thin-evidence settles write"); + // No evidence at all (tool-less sidecar, envelope absent). + const bare = sessionWith(0); + classifyAndRecordMembership(bare, [], 9_000); + assert.equal(bare.transient?.requestMembership, undefined, "empty evidence records nothing"); + settleUsageReport(bare, { total: 9_000, reportedCached: null }); + assert.equal(bare.stats.lastInputTokens, 9_000, "no-evidence settles write"); + // #2821 re-mint: a main turn whose ids were ALL re-minted loses at most + // one settle; the resent tail re-joins on the next turn. + const churn = sessionWith(0); + classifyAndRecordMembership(churn, Array.from({ length: 20 }, (_, i) => `C${i}`), 200_000); + settleUsageReport(churn, { total: 200_000, reportedCached: null }); + classifyAndRecordMembership(churn, Array.from({ length: 20 }, (_, i) => `D${i}`), 210_000); + assert.ok(isForeignMembership(churn.transient?.requestMembership), "full re-mint reads as foreign (one skipped settle)"); + settleUsageReport(churn, { total: 210_000, reportedCached: null }); + assert.equal(churn.stats.lastInputTokens, 200_000, "the re-minted turn's settle is skipped once"); + classifyAndRecordMembership(churn, Array.from({ length: 20 }, (_, i) => `D${i}`).concat("D20"), 215_000); + assert.ok(!isForeignMembership(churn.transient?.requestMembership), "the resent re-minted family joins and heals"); + settleUsageReport(churn, { total: 220_000, reportedCached: null }); + assert.equal(churn.stats.lastInputTokens, 220_000, "self-heals on the next resend"); +}); + +test("T3c evidence extraction: envelope first, tool-call ids as the wire fallback", () => { + const msgs = [ + { id: "a", role: "user", text: "hi" }, + { id: "b", role: "assistant", text: "", toolCallId: "call_1" }, + { id: "c", role: "tool", text: "out", toolCallId: "call_1" }, + { id: "d", role: "assistant", text: "done" }, + ] as never; + assert.deepEqual(collectRequestIds(undefined, msgs), ["call_1"], "tool-call ids extracted, call/result deduped"); + assert.deepEqual(collectRequestIds(["e1", "e2"], msgs), ["e1", "e2"], "envelope ids win when present"); + assert.deepEqual(collectRequestIds(["acp_summary_x", 7, ""], msgs), ["call_1"], "invalid envelope falls back to tool-call ids"); + assert.deepEqual(collectRequestIds(undefined, [{ id: "u", role: "user", text: "hi" } as never]), [], "tool-less history carries no evidence (abstain)"); +}); From c037fe68e04200caeb58e0d87e515f9c7da4d4ff Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 26 Oct 2026 23:42:16 +0800 Subject: [PATCH 07/10] fix(#4805): fork children inherit the admitted id family from turn 1 A fork child's resent history IS the parent's, so the membership family must not cold-start: an auxiliary persona striking the child first would sail through an empty family and pollute the fresh anchor before any guard has evidence. --- src/plugin.ts | 8 ++++++++ tests/issue4805-anchor-pollution.test.ts | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/plugin.ts b/src/plugin.ts index 5f70c8a9f2..00bfbd1fcb 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1524,6 +1524,14 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr // takes precedence immediately (#728 invariant), and every prepare* // overwrites it with the measured outbound each turn. child.stats.localInputEstimate = child.stats.contextTokens; + // #4805: the child inherits the parent's admitted id family — its + // resent history IS the parent's, so membership must not + // cold-start (an auxiliary persona striking the fork child first + // would sail through an empty family and pollute the fresh + // anchor before any guard has evidence). + if (parent.transient?.knownMessageIds?.length) { + (child.transient ??= {}).knownMessageIds = [...parent.transient.knownMessageIds]; + } const response = { ok: true, status: crossing.length > 0 ? "expanded" : "exact", protocolVersion: 2, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false }; child.metadata.publicForkReceipt = { requestHash, response }; publishForkSession(child); diff --git a/tests/issue4805-anchor-pollution.test.ts b/tests/issue4805-anchor-pollution.test.ts index 67f5267c9a..99fcf7665d 100644 --- a/tests/issue4805-anchor-pollution.test.ts +++ b/tests/issue4805-anchor-pollution.test.ts @@ -176,3 +176,20 @@ test("T3c evidence extraction: envelope first, tool-call ids as the wire fallbac assert.deepEqual(collectRequestIds(["acp_summary_x", 7, ""], msgs), ["call_1"], "invalid envelope falls back to tool-call ids"); assert.deepEqual(collectRequestIds(undefined, [{ id: "u", role: "user", text: "hi" } as never]), [], "tool-less history carries no evidence (abstain)"); }); + +test("T3d fork inheritance: a fork child carries the parent's id family from turn 1", () => { + const parent = sessionWith(0); + classifyAndRecordMembership(parent, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); + settleUsageReport(parent, { total: 300_000, reportedCached: null }); + // Fork child (plugin.ts copies the family alongside the stats seed). + const child = sessionWith(0); + (child.transient ??= {}).knownMessageIds = [...(parent.transient?.knownMessageIds ?? [])]; + // An auxiliary persona striking the CHILD first stays gated. + classifyAndRecordMembership(child, ["X1", "X2", "X3", "X4", "X5"], 2_600); + assert.ok(isForeignMembership(child.transient?.requestMembership), "fork child gates first-strike pollution"); + settleUsageReport(child, { total: 5_100, reportedCached: null }); + assert.equal(child.stats.lastInputTokens, 0, "no anchor write on the gated settle"); + // The child's own first main turn joins the inherited family directly. + classifyAndRecordMembership(child, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12"], 310_000); + assert.ok(!isForeignMembership(child.transient?.requestMembership), "inherited family joins immediately"); +}); From 23a483383ce06fcbd2345bcefcb304ace4188148 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 27 Oct 2026 00:19:51 +0800 Subject: [PATCH 08/10] refactor(#4805): membership probe reduced to a single remembered id (needle) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 4000-entry admitted-id family is replaced by exactly one id — the conversation's latest admitted message id — probed with 'does the resent history contain it'. A main turn always carries its own latest id; a foreign payload never does, and since the needle only moves on member / first-turn / main-scale requests (scale reference: the anchor, or the needle's own planting payload before an anchor exists), a polluter can neither pass nor hijack the probe. O(1) state, fork-inherited alongside its payload scale. --- src/cache-ledger.ts | 2 +- src/plugin.ts | 11 ++-- src/request-membership.ts | 69 +++++++++------------ src/server/prepare-anthropic.ts | 4 +- src/server/prepare-openai.ts | 4 +- src/session.ts | 20 +++--- tests/issue4805-anchor-pollution.test.ts | 77 +++++++++++++----------- 7 files changed, 92 insertions(+), 95 deletions(-) diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 0c1f5f5078..16537f1dfb 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1801,7 +1801,7 @@ export function settleUsageReport( // as before: the gate only ever refuses the clearly-foreign. if (isForeignMembership(session.transient?.requestMembership)) { const m = session.transient!.requestMembership!; - loggerLog("info", `[${session.id}] [membership] usage sample (total=${s.total}) refused anchor write: its request carried no known message ids (${m.overlap}/${m.total} joined, ${m.known} known) — foreign payload under a shared session id (#4805)`); + loggerLog("info", `[${session.id}] [membership] usage sample (total=${s.total}) refused anchor write: its request did not carry the conversation's latest message id (needle=${m.needle ?? "-"}) — foreign payload under a shared session id (#4805)`); } else { // Net out this turn's compress credit: the post-compress re-request // re-sends the unfolded history, so its usage report over-reports the diff --git a/src/plugin.ts b/src/plugin.ts index 00bfbd1fcb..088bdaa49e 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1524,13 +1524,14 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr // takes precedence immediately (#728 invariant), and every prepare* // overwrites it with the measured outbound each turn. child.stats.localInputEstimate = child.stats.contextTokens; - // #4805: the child inherits the parent's admitted id family — its - // resent history IS the parent's, so membership must not + // #4805: the child inherits the parent's membership needle — its + // resent history IS the parent's, so the probe must not // cold-start (an auxiliary persona striking the fork child first - // would sail through an empty family and pollute the fresh + // would sail through an unplanted needle and pollute the fresh // anchor before any guard has evidence). - if (parent.transient?.knownMessageIds?.length) { - (child.transient ??= {}).knownMessageIds = [...parent.transient.knownMessageIds]; + if (parent.transient?.latestKnownMsgId) { + (child.transient ??= {}).latestKnownMsgId = parent.transient.latestKnownMsgId; + child.transient.needlePayloadUpper = parent.transient.needlePayloadUpper; } const response = { ok: true, status: crossing.length > 0 ? "expanded" : "exact", protocolVersion: 2, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false }; child.metadata.publicForkReceipt = { requestHash, response }; diff --git a/src/request-membership.ts b/src/request-membership.ts index ae1d428b3a..a3e48cc256 100644 --- a/src/request-membership.ts +++ b/src/request-membership.ts @@ -1,35 +1,28 @@ import type { CoreMessage } from "acp-kernel"; import type { Session } from "./session.js"; -// #4805: settle-side membership gate. A usage report is only "this +// #4805: settle-side membership probe. A usage report is only "this // conversation's truth" when the request that produced it actually carried // this conversation's messages. Hosts stamp auxiliary personas (dsh // auto-review and its relatives) with the SAME conversation id, so the // session key alone cannot tell a main-turn report from a foreign -// 2-message one — but the message ids can: a main turn resends history the -// session already knows, while an auxiliary payload brings ids the session -// has never seen. Evidence is id-based ONLY (no content hashing — host ids -// are stable per-lane observation; content joins stay with fold-reconcile): -// - plugin lanes: the biliMsgIds envelope the host stamps alongside the -// message array (#4491), read regardless of the pin rollout state; -// - every wire: the tool-call ids resent in the history (model-minted, -// client-echoed — the only ids the raw protocol wires carry). -// The verdict abstains (never gates) when evidence is thin: first turns, -// tool-less sidecars, or the #2821 id re-mint family — a re-minted main -// turn loses at most ONE anchor update and self-heals on the next resend -// (its ids then join the known family). -export const MEMBERSHIP_HISTORY_CAP = 4000; -export const MEMBERSHIP_MIN_KNOWN_IDS = 8; -export const MEMBERSHIP_MIN_REQUEST_IDS = 4; -export const MEMBERSHIP_MIN_OVERLAP_RATIO = 0.25; -// #2821 tiebreak: a main turn whose ids were ALL re-minted reads as foreign -// too — but unlike an auxiliary persona it carries main-scale payload. Only -// at-conversation-scale requests may join the known family on a zero-overlap -// verdict, so the re-mint heals on its second resend while a tiny polluter -// (payload ~1% of the anchor) never graduates. +// 2-message one — but one remembered id can: a main turn resents the whole +// history and therefore contains the conversation's latest admitted +// message id, while an auxiliary payload never does. The probe key updates +// ONLY from member / first-turn / main-scale requests, so a foreign +// payload can neither pass nor hijack the needle (#2821 re-minted main +// turns are main-scale: they lose one settle and move the needle, healing +// on the next resend). Evidence ids are id-based ONLY (no content +// hashing): plugin lanes send the biliMsgIds envelope (#4491, any rollout +// state); every wire carries resent tool-call ids (model-minted, +// client-echoed — the only ids the raw protocol wires carry). export const MEMBERSHIP_RECORD_SCALE_RATIO = 0.25; -export type RequestMembership = { overlap: number; total: number; known: number }; +export type RequestMembership = { member: boolean; needle: string | null }; + +// The payload upper estimate of the request that planted/moved the needle: +// before an anchor exists (fresh or fork child), the needle's own scale is +// the main-scale reference, so a tiny polluter cannot hijack it. export function collectRequestIds(envelopeIds: unknown, msgs: CoreMessage[]): string[] { const out: string[] = []; @@ -54,28 +47,26 @@ export function collectRequestIds(envelopeIds: unknown, msgs: CoreMessage[]): st } export function isForeignMembership(m: RequestMembership | null | undefined): boolean { - if (!m) return false; - return m.known >= MEMBERSHIP_MIN_KNOWN_IDS && m.total >= MEMBERSHIP_MIN_REQUEST_IDS && m.overlap < m.total * MEMBERSHIP_MIN_OVERLAP_RATIO; + return m?.member === false; } -export function classifyAndRecordMembership(session: Session, ids: string[], payloadUpper: number): RequestMembership | null { +export function probeRequestMembership(session: Session, ids: string[], payloadUpper: number): RequestMembership | null { if (ids.length === 0) return null; const t = session.transient; if (!t) return null; - const known = new Set(t.knownMessageIds); - let overlap = 0; - for (const id of ids) if (known.has(id)) overlap++; - const verdict: RequestMembership = { overlap, total: ids.length, known: known.size }; + const needle = t.latestKnownMsgId ?? null; + // First turn (no needle yet) abstains and plants one. + const member = needle === null || ids.includes(needle); + const verdict: RequestMembership = { member, needle }; t.requestMembership = verdict; - // Only admitted, evidence-poor, or main-scale requests teach the session - // its id family — a tiny foreign payload's ids must never enter the known - // set, or a persistent auxiliary persona would graduate to "member" on - // its second visit and reopen #4805 through the front door. - const mainScale = session.stats.lastInputTokens <= 0 || payloadUpper >= session.stats.lastInputTokens * MEMBERSHIP_RECORD_SCALE_RATIO; - if (!isForeignMembership(verdict) || mainScale) { - for (const id of ids) known.add(id); - const all = [...known]; - t.knownMessageIds = all.length > MEMBERSHIP_HISTORY_CAP ? all.slice(all.length - MEMBERSHIP_HISTORY_CAP) : all; + // Needle discipline: only member / first-turn / main-scale requests may + // move it — a tiny foreign payload must never own the needle, or the + // next main turn would probe against the polluter's id and be gated. + const referenceScale = session.stats.lastInputTokens > 0 ? session.stats.lastInputTokens : (t.needlePayloadUpper ?? 0); + const mainScale = payloadUpper >= referenceScale * MEMBERSHIP_RECORD_SCALE_RATIO; + if (member || mainScale) { + t.latestKnownMsgId = ids[ids.length - 1]; + t.needlePayloadUpper = payloadUpper; } return verdict; } diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index f3c8309903..f1ab54c404 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -41,7 +41,7 @@ import { injectSystem, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; import { featureEnabled } from "../rollout.js"; import { MSG_IDS_BODY_FIELD, MSG_SEQS_BODY_FIELD, applyMsgIdPin, applyMsgSeqStamps, validateMsgIds, validateMsgSeqs } from "../msgid-pin.js"; -import { classifyAndRecordMembership, collectRequestIds } from "../request-membership.js"; +import { collectRequestIds, probeRequestMembership } from "../request-membership.js"; import { alignIdSet, looksLikeSummaryCarrier } from "../msgid-algebra.js"; export async function prepareAnthropic( @@ -599,7 +599,7 @@ export async function prepareAnthropic( // when the lane sends them (any rollout state), else the tool-call ids // resent in the history. Placed after the payload row above so the // main-scale tiebreak sees this request's own upper bound. - classifyAndRecordMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0); + probeRequestMembership(session, collectRequestIds(incomingMsgIds, originalMessages), session.stats.localInputEstimate ?? 0); if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin; return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 876e2936fc..ba599b94a7 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -37,7 +37,7 @@ import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, e import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; -import { classifyAndRecordMembership, collectRequestIds } from "../request-membership.js"; +import { collectRequestIds, probeRequestMembership } from "../request-membership.js"; export async function prepareOpenai( parsed: OpenAIRequestBody, @@ -498,7 +498,7 @@ export async function prepareOpenai( // #4805: id-membership evidence for the settle gate (see the // anthropic-lane counterpart) — chat wire has no msgid envelope, so // evidence is the tool-call ids resent in the history. - classifyAndRecordMembership(session, collectRequestIds(undefined, processedMessages.length > 0 ? processedMessages : originalMessages), session.stats.localInputEstimate ?? 0); + probeRequestMembership(session, collectRequestIds(undefined, processedMessages.length > 0 ? processedMessages : originalMessages), session.stats.localInputEstimate ?? 0); } snapshotMessages(session, originalMessages); markDirty(session); diff --git a/src/session.ts b/src/session.ts index cf790b9da2..9f39fcde76 100644 --- a/src/session.ts +++ b/src/session.ts @@ -249,16 +249,16 @@ export type Session = { * bili's rebuild. */ inBody?: InboundSlot; settledInBody?: InboundSlot; - /** #4805: message ids this session has ADMITTED across its requests - * (envelope biliMsgIds first, tool-call ids otherwise) — the known - * family the settle-side membership gate joins each request against. - * In-memory only: after a restart the first request rebuilds it and - * the gate abstains while evidence is thin. */ - knownMessageIds?: string[]; - /** #4805: the in-flight request's membership verdict, recorded at - * prepare and consumed by settleUsageReport — {overlap, total, - * known} scalars only. */ - requestMembership?: { overlap: number; total: number; known: number }; + /** #4805: the conversation's latest ADMITTED message id — the single + * needle the settle-side membership probe tests each resent history + * against. Updated only by member / first-turn / main-scale + * requests, so a foreign payload can neither pass nor hijack it. + * In-memory only; a restart replants it on the first turn. */ + latestKnownMsgId?: string; + needlePayloadUpper?: number; + /** #4805: the in-flight request's probe verdict ({member, needle}), + * recorded at prepare and consumed by settleUsageReport. */ + requestMembership?: { member: boolean; needle: string | null }; }; id: string; /** Identity / descriptive metadata. Populated on first request. */ diff --git a/tests/issue4805-anchor-pollution.test.ts b/tests/issue4805-anchor-pollution.test.ts index 99fcf7665d..286e897938 100644 --- a/tests/issue4805-anchor-pollution.test.ts +++ b/tests/issue4805-anchor-pollution.test.ts @@ -5,7 +5,7 @@ process.env.NODE_ENV = "test"; import { settleUsageReport } from "../src/cache-ledger.ts"; import { effectiveTokenCount } from "../src/server.ts"; -import { classifyAndRecordMembership, collectRequestIds, isForeignMembership } from "../src/request-membership.ts"; +import { collectRequestIds, isForeignMembership, probeRequestMembership } from "../src/request-membership.ts"; import type { Session } from "../src/session.ts"; import type { CoreMessage } from "acp-kernel"; @@ -104,62 +104,66 @@ test("T2 meter guard: a polluted anchor from a tiny producer cannot size a 1.2M- assert.equal(early.tokens, 800, "early-session anchors keep sizing small payloads (no false trips)"); }); -// T3: the id-membership settle gate (#4805, second guard). The host stamps -// auxiliary personas with the SAME conversation id, so the session key alone -// cannot attribute a usage report — but the resent message ids can: a main -// turn joins the session's known id family, a foreign payload brings ids the -// session has never seen. -test("T3 settle membership gate: a foreign id family cannot rewrite the anchor", () => { +// T3: the single-needle membership probe (#4805, second guard). The host +// stamps auxiliary personas with the SAME conversation id, so the session +// key alone cannot attribute a usage report — but one remembered id can: +// a main turn resents the whole history and contains the conversation's +// latest admitted id; a foreign payload never does. +test("T3 settle membership probe: a foreign payload cannot rewrite the anchor", () => { const session = sessionWith(0); - // Main turn 1: 12 ids, none known yet — first-turn abstention settles. - classifyAndRecordMembership(session, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); + // Main turn 1: no needle yet — abstention plants one and settles. + probeRequestMembership(session, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); settleUsageReport(session, { total: 300_000, reportedCached: null, protocol: "openai" }); - assert.equal(session.stats.lastInputTokens, 300_000, "admitted first settle writes (known-family bootstrap)"); - // Foreign payload under the same session id: 5 ids, 0 joined, 12 known. + assert.equal(session.stats.lastInputTokens, 300_000, "first settle plants the needle and writes"); + assert.equal(session.transient?.latestKnownMsgId, "M11", "needle = latest admitted id"); + // Foreign payload under the same session id: does not carry the needle. const foreignIds = ["X1", "X2", "X3", "X4", "X5"]; - classifyAndRecordMembership(session, foreignIds, 2_600); + probeRequestMembership(session, foreignIds, 2_600); assert.ok(isForeignMembership(session.transient?.requestMembership), "verdict marks the payload foreign"); settleUsageReport(session, { total: 5_100, reportedCached: 128, protocol: "openai" }); assert.equal(session.stats.lastInputTokens, 300_000, "the foreign sample cannot touch the anchor (#4805)"); assert.equal(session.stats.inputTokens, 305_100, "it still counts toward cumulative usage"); assert.equal(session.stats.lastUsagePayloadUpper, 0, "no producer snapshot from a refused settle"); - // Foreign ids never enter the known family: a persistent auxiliary - // persona must not graduate to member on its second visit. - classifyAndRecordMembership(session, foreignIds, 2_600); + // The needle never moves to a tiny foreign payload: repeat strikes stay gated. + assert.equal(session.transient?.latestKnownMsgId, "M11", "the polluter cannot hijack the needle"); + probeRequestMembership(session, foreignIds, 2_600); assert.ok(isForeignMembership(session.transient?.requestMembership), "repeat foreign payloads stay foreign"); settleUsageReport(session, { total: 4_800, reportedCached: null, protocol: "openai" }); assert.equal(session.stats.lastInputTokens, 300_000, "repeat pollution stays gated"); - // Main conversation recovers: resent ids join, the settle writes again. - classifyAndRecordMembership(session, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12", "M13"], 320_000); - assert.ok(!isForeignMembership(session.transient?.requestMembership), "main resends join their family"); + // Main conversation recovers: carries the needle, the settle writes again. + probeRequestMembership(session, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12", "M13"], 320_000); + assert.ok(!isForeignMembership(session.transient?.requestMembership), "main resends carry the needle"); settleUsageReport(session, { total: 320_000, reportedCached: null, protocol: "openai" }); assert.equal(session.stats.lastInputTokens, 320_000, "main-lane reports keep updating the anchor"); + assert.equal(session.transient?.latestKnownMsgId, "M13", "the needle follows the conversation head"); }); test("T3b membership abstention: thin evidence settles exactly as before", () => { - // First turn (no known family) must not gate anything. + // First turn (no needle) must not gate anything. const fresh = sessionWith(0); - classifyAndRecordMembership(fresh, ["A", "B", "C", "D"], 5_000); - assert.ok(!isForeignMembership(fresh.transient?.requestMembership), "known < 8 abstains"); + probeRequestMembership(fresh, ["A", "B", "C", "D"], 5_000); + assert.ok(!isForeignMembership(fresh.transient?.requestMembership), "no needle = abstain"); settleUsageReport(fresh, { total: 5_100, reportedCached: null }); - assert.equal(fresh.stats.lastInputTokens, 5_100, "thin-evidence settles write"); + assert.equal(fresh.stats.lastInputTokens, 5_100, "first-turn settles write"); // No evidence at all (tool-less sidecar, envelope absent). const bare = sessionWith(0); - classifyAndRecordMembership(bare, [], 9_000); + probeRequestMembership(bare, [], 9_000); assert.equal(bare.transient?.requestMembership, undefined, "empty evidence records nothing"); settleUsageReport(bare, { total: 9_000, reportedCached: null }); assert.equal(bare.stats.lastInputTokens, 9_000, "no-evidence settles write"); - // #2821 re-mint: a main turn whose ids were ALL re-minted loses at most - // one settle; the resent tail re-joins on the next turn. + // #2821 re-mint: a main turn whose ids were ALL re-minted does not carry + // the needle (one skipped settle) but IS main-scale, so it moves the + // needle and the next resend joins. const churn = sessionWith(0); - classifyAndRecordMembership(churn, Array.from({ length: 20 }, (_, i) => `C${i}`), 200_000); + probeRequestMembership(churn, Array.from({ length: 20 }, (_, i) => `C${i}`), 200_000); settleUsageReport(churn, { total: 200_000, reportedCached: null }); - classifyAndRecordMembership(churn, Array.from({ length: 20 }, (_, i) => `D${i}`), 210_000); - assert.ok(isForeignMembership(churn.transient?.requestMembership), "full re-mint reads as foreign (one skipped settle)"); + probeRequestMembership(churn, Array.from({ length: 20 }, (_, i) => `D${i}`), 210_000); + assert.ok(isForeignMembership(churn.transient?.requestMembership), "full re-mint misses the needle (one skipped settle)"); settleUsageReport(churn, { total: 210_000, reportedCached: null }); assert.equal(churn.stats.lastInputTokens, 200_000, "the re-minted turn's settle is skipped once"); - classifyAndRecordMembership(churn, Array.from({ length: 20 }, (_, i) => `D${i}`).concat("D20"), 215_000); - assert.ok(!isForeignMembership(churn.transient?.requestMembership), "the resent re-minted family joins and heals"); + assert.equal(churn.transient?.latestKnownMsgId, "D19", "main-scale re-mint moves the needle"); + probeRequestMembership(churn, Array.from({ length: 20 }, (_, i) => `D${i}`).concat("D20"), 215_000); + assert.ok(!isForeignMembership(churn.transient?.requestMembership), "the re-minted family carries the new needle and heals"); settleUsageReport(churn, { total: 220_000, reportedCached: null }); assert.equal(churn.stats.lastInputTokens, 220_000, "self-heals on the next resend"); }); @@ -177,19 +181,20 @@ test("T3c evidence extraction: envelope first, tool-call ids as the wire fallbac assert.deepEqual(collectRequestIds(undefined, [{ id: "u", role: "user", text: "hi" } as never]), [], "tool-less history carries no evidence (abstain)"); }); -test("T3d fork inheritance: a fork child carries the parent's id family from turn 1", () => { +test("T3d fork inheritance: a fork child carries the parent's needle from turn 1", () => { const parent = sessionWith(0); - classifyAndRecordMembership(parent, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); + probeRequestMembership(parent, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); settleUsageReport(parent, { total: 300_000, reportedCached: null }); - // Fork child (plugin.ts copies the family alongside the stats seed). + // Fork child (plugin.ts copies the needle alongside the stats seed). const child = sessionWith(0); - (child.transient ??= {}).knownMessageIds = [...(parent.transient?.knownMessageIds ?? [])]; + (child.transient ??= {}).latestKnownMsgId = parent.transient?.latestKnownMsgId; + (child.transient ??= {}).needlePayloadUpper = parent.transient?.needlePayloadUpper; // An auxiliary persona striking the CHILD first stays gated. - classifyAndRecordMembership(child, ["X1", "X2", "X3", "X4", "X5"], 2_600); + probeRequestMembership(child, ["X1", "X2", "X3", "X4", "X5"], 2_600); assert.ok(isForeignMembership(child.transient?.requestMembership), "fork child gates first-strike pollution"); settleUsageReport(child, { total: 5_100, reportedCached: null }); assert.equal(child.stats.lastInputTokens, 0, "no anchor write on the gated settle"); // The child's own first main turn joins the inherited family directly. - classifyAndRecordMembership(child, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12"], 310_000); + probeRequestMembership(child, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12"], 310_000); assert.ok(!isForeignMembership(child.transient?.requestMembership), "inherited family joins immediately"); }); From 8b733bc9349713e7211732fd614c80848fd945d7 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 00:33:55 +0800 Subject: [PATCH 09/10] fix(#4805): keep membership probe tuning constant module-private (unused-exports gate) CI job unused-exports flags MEMBERSHIP_RECORD_SCALE_RATIO and the RequestMembership type in src/request-membership.ts as INTERNAL_ONLY (no reference outside their own file). Drop the leading 'export' per the gate's own fix hint - both stay at module scope, behavior unchanged. --- src/request-membership.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/request-membership.ts b/src/request-membership.ts index a3e48cc256..56906ba19e 100644 --- a/src/request-membership.ts +++ b/src/request-membership.ts @@ -16,9 +16,9 @@ import type { Session } from "./session.js"; // hashing): plugin lanes send the biliMsgIds envelope (#4491, any rollout // state); every wire carries resent tool-call ids (model-minted, // client-echoed — the only ids the raw protocol wires carry). -export const MEMBERSHIP_RECORD_SCALE_RATIO = 0.25; +const MEMBERSHIP_RECORD_SCALE_RATIO = 0.25; -export type RequestMembership = { member: boolean; needle: string | null }; +type RequestMembership = { member: boolean; needle: string | null }; // The payload upper estimate of the request that planted/moved the needle: // before an anchor exists (fresh or fork child), the needle's own scale is From 11763acf2568e8eb148d8aa64c52aac2250880a7 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 27 Oct 2026 01:12:02 +0800 Subject: [PATCH 10/10] fix(#4805): log the membership probe verdict per request (debug) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The empirical record for this work cites a per-request '[#4805] membership probe: ids=N needle= payloadUpper=X' debug line, but at head only the settle-time refusal info line existed — member requests were silent. Emit the verdict from probeRequestMembership so the probe is auditable in the log for every request that carries evidence (abstention paths stay silent). Logging only; no behavior change. typecheck + issue4805 suite 7/7 locally. --- src/request-membership.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/request-membership.ts b/src/request-membership.ts index 56906ba19e..f414626d2c 100644 --- a/src/request-membership.ts +++ b/src/request-membership.ts @@ -1,4 +1,5 @@ import type { CoreMessage } from "acp-kernel"; +import { log as loggerLog } from "./logger.js"; import type { Session } from "./session.js"; // #4805: settle-side membership probe. A usage report is only "this @@ -59,6 +60,7 @@ export function probeRequestMembership(session: Session, ids: string[], payloadU const member = needle === null || ids.includes(needle); const verdict: RequestMembership = { member, needle }; t.requestMembership = verdict; + loggerLog("debug", `[${session.id}] [#4805] membership probe: ids=${ids.length} needle=${member ? "member" : "miss"} payloadUpper=${payloadUpper}`); // Needle discipline: only member / first-turn / main-scale requests may // move it — a tiny foreign payload must never own the needle, or the // next main turn would probe against the polluter's id and be gated.