diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 3f0f8b9afc..5e2f7dbc90 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"; @@ -1804,6 +1805,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 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 // context the NEXT request will actually carry (see stream.ts applyRanges). @@ -1813,6 +1826,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. @@ -1849,6 +1863,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/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/plugin.ts b/src/plugin.ts index 5f70c8a9f2..088bdaa49e 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1524,6 +1524,15 @@ 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 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 unplanted needle and pollute the fresh + // anchor before any guard has evidence). + 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 }; publishForkSession(child); diff --git a/src/request-membership.ts b/src/request-membership.ts new file mode 100644 index 0000000000..f414626d2c --- /dev/null +++ b/src/request-membership.ts @@ -0,0 +1,74 @@ +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 +// 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 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). +const MEMBERSHIP_RECORD_SCALE_RATIO = 0.25; + +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[] = []; + 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 { + return m?.member === false; +} + +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 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; + 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. + 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.ts b/src/server.ts index 3030b92888..a25b07cf3f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2475,6 +2475,20 @@ 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; +// 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 // precedence immediately — same invariant as #604's armFailureShrink @@ -2505,7 +2519,20 @@ 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 + && (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 // 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 +2544,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/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index fa57f1e68e..f1ab54c404 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 { collectRequestIds, probeRequestMembership } 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. + 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 0767039e0b..ba599b94a7 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 { collectRequestIds, probeRequestMembership } 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. + 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 278a75effe..9f39fcde76 100644 --- a/src/session.ts +++ b/src/session.ts @@ -249,6 +249,16 @@ export type Session = { * bili's rebuild. */ inBody?: InboundSlot; settledInBody?: InboundSlot; + /** #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. */ @@ -396,6 +406,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 new file mode 100644 index 0000000000..286e897938 --- /dev/null +++ b/tests/issue4805-anchor-pollution.test.ts @@ -0,0 +1,200 @@ +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 { collectRequestIds, isForeignMembership, probeRequestMembership } from "../src/request-membership.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: +// 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 { + 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: [], + transient: {}, + } as never as Session; +} + +function bigHistory(count: number): CoreMessage[] { + const msgs: CoreMessage[] = []; + for (let i = 0; i < count; i++) { + msgs.push({ id: `m${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: "x".repeat(6000) }); + } + return msgs; +} + +test("T1 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, 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 (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("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 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: a polluted anchor from a tiny producer cannot size a 1.2M-token payload", () => { + const session = sessionWith(5_100); + 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); + const early = effectiveTokenCount(small, earlyMsgs, 0); + assert.equal(early.tokens, 800, "early-session anchors keep sizing small payloads (no false trips)"); +}); + +// 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: 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, "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"]; + 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"); + // 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: 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 needle) must not gate anything. + const fresh = sessionWith(0); + 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, "first-turn settles write"); + // No evidence at all (tool-less sidecar, envelope absent). + const bare = sessionWith(0); + 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 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); + probeRequestMembership(churn, Array.from({ length: 20 }, (_, i) => `C${i}`), 200_000); + settleUsageReport(churn, { total: 200_000, reportedCached: null }); + 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"); + 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"); +}); + +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)"); +}); + +test("T3d fork inheritance: a fork child carries the parent's needle from turn 1", () => { + const parent = sessionWith(0); + probeRequestMembership(parent, Array.from({ length: 12 }, (_, i) => `M${i}`), 300_000); + settleUsageReport(parent, { total: 300_000, reportedCached: null }); + // Fork child (plugin.ts copies the needle alongside the stats seed). + const child = sessionWith(0); + (child.transient ??= {}).latestKnownMsgId = parent.transient?.latestKnownMsgId; + (child.transient ??= {}).needlePayloadUpper = parent.transient?.needlePayloadUpper; + // An auxiliary persona striking the CHILD first stays gated. + 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. + probeRequestMembership(child, [...Array.from({ length: 12 }, (_, i) => `M${i}`), "M12"], 310_000); + assert.ok(!isForeignMembership(child.transient?.requestMembership), "inherited family joins immediately"); +}); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 1755e36f7a..8954a2d17c 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,91 @@ 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) 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, 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"); + 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"); +}); + +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 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"); +});