From d781cb8385f4f635ac32c13fea4b039b945a2d87 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 09:50:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20snapshot-lane=20=E2=80=94=20pin=20proxy?= =?UTF-8?q?-lane=20historical=20ids=20server-side=20(#3756)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Proxy-mode clients resend their full history every turn, so message ids were re-derived from client bytes each turn. Any kernel identity-function change re-minted every historical id and mass-unfolded persisted sessions (#3752; the #3658 seed swap was the incident). This adds an opt-in snapshot lane that pins historical ids server-side: - src/snapshot-lane.ts: pinSnapshotIds() aligns the client resend against the unified-store snapshot (order-centric walk, dialect-neutral content fingerprints per #3642) and stamps stored msg_ids onto aligned prefix positions before the pipeline runs. Appends ride the existing rawTail ingest (#3007); registry state persists via session_ext (snap:reg / snap:head). Every unaligned shape bails to byte-identical passthrough — the lane can only change ids, never content or order. - src/server/prepare-{anthropic,openai,responses}.ts: hook after XToCore; plugin mode returns the same reference untouched. - src/rollout.ts: proxySnapshotLane feature entry (percent 0 = default off). - tests/snapshot-lane.test.ts: 9 tests incl. the #3752 identity-swap replay (pinned resend keeps coverage and byte-stable outbound; unpinned control drifts), dsh-style checkpoint absorption (observed, never forwarded), rewind/truncate clamping, and gate/bail directions. - reference/architecture.md: module map line. Closes #3756 Fixes part of #3752 (R2 root cause; R1 capacity cliff stays out of scope — non-D lanes still rely on the #3753 lite-anchor stopgap) --- reference/architecture.md | 1 + src/rollout.ts | 9 + src/server/prepare-anthropic.ts | 6 +- src/server/prepare-openai.ts | 6 +- src/server/prepare-responses.ts | 5 +- src/snapshot-lane.ts | 438 ++++++++++++++++++++++++++++++++ tests/snapshot-lane.test.ts | 347 +++++++++++++++++++++++++ 7 files changed, 809 insertions(+), 3 deletions(-) create mode 100644 src/snapshot-lane.ts create mode 100644 tests/snapshot-lane.test.ts diff --git a/reference/architecture.md b/reference/architecture.md index f929892475..bb1f6d8ee3 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -100,6 +100,7 @@ billion-context/ │ ├── registry-snapshot.json # Bundled full models.dev snapshot (offline floor) │ ├── session.ts # Session model + in-memory store (bounded, MAX_SESSIONS) │ ├── session-id.ts # Session identity: client value verbatim + persona fork fingerprint +│ ├── snapshot-lane.ts # Proxy-lane snapshot id-pinning: stored ids stamped onto resends, identity-upgrade immunity (#3756) │ ├── session-gc.ts # Session-file GC, opt-in (sessions.gc.* / BILI_SESSION_GC*) (#1082) │ ├── persist.ts # On-disk session persistence (kernel StateStore) │ ├── persist-eperm.ts # EPERM/EACCES write-failure alert layer (#362) diff --git a/src/rollout.ts b/src/rollout.ts index fdc67a0d02..9f78e6796d 100644 --- a/src/rollout.ts +++ b/src/rollout.ts @@ -61,6 +61,15 @@ export const ROLLOUT_FEATURES: Record = { // `rollout.features.anthropicThreadsReforge: true` or // `BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1`. anthropicThreadsReforge: { percent: 0 }, + // #3756 snapshot lane: proxy-lane outbound history is pinned onto the + // server-side unified-store snapshot so historical message ids are never + // re-minted by a kernel identity-function change (#3752 R2 root fix). + // Default 0% — every session keeps today's derive-on-resend behavior; + // unsupported shapes always pass through byte-identical even when on. + // Also requires the unified store to be live (shadow or full). Force on + // with `rollout.features.proxySnapshotLane: true` or + // `BILI_ROLLOUT_PROXY_SNAPSHOT_LANE=1`. + proxySnapshotLane: { percent: 0 }, }; export const BP_MAX = 10_000; diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 67d84035b9..4de890944f 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -33,6 +33,7 @@ import { countSystemAndToolsTokens, emergencyNudge, emergencyNudgeArmed, project import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectSystem, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions } from "./lane-decisions.js"; +import { pinSnapshotIds } from "../snapshot-lane.js"; export async function prepareAnthropic( parsed: AnthropicRequestBody, @@ -194,7 +195,10 @@ export async function prepareAnthropic( logRawStripCounts(markerCounts, log, sessionId); try { - const { msgs, cacheControls } = anthropicToCore(parsed); + const { msgs: parsedMsgs, cacheControls } = anthropicToCore(parsed); + // #3756: pin historical ids onto the server-side snapshot before any + // downstream metering/mutation (alignment runs on pristine parse bytes). + const msgs = pinSnapshotIds({ sessionId, protocol: "anthropic", pluginMode, msgs: parsedMsgs, state: session.state, log }); originalMessages = msgs; // #1320: signature-only thinking blocks bill restored thinking tokens upstream // but project as empty text locally — attribute the provider-vs-local residual diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 2fb81e6499..ebf3b93e6e 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -31,6 +31,7 @@ import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, e import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions } from "./lane-decisions.js"; +import { pinSnapshotIds } from "../snapshot-lane.js"; export async function prepareOpenai( parsed: OpenAIRequestBody, @@ -101,7 +102,10 @@ export async function prepareOpenai( // OUT of the fold space: system content is host runtime state and // must not feed ids/fingerprints. Capture it and re-inject below — // otherwise the proxy would forward payloads without any system. - const { msgs, systemText } = openaiToCore(parsed); + const { msgs: parsedMsgs, systemText } = openaiToCore(parsed); + // #3756: pin historical ids onto the server-side snapshot (proxy lane + // only; passthrough byte-identical whenever the lane does not engage). + const msgs = pinSnapshotIds({ sessionId, protocol: "openai", pluginMode, msgs: parsedMsgs, state: session.state, log }); openaiSystemText = systemText; warnDroppedOpenaiParts(parsed, sessionId, log); // Title-gen side-requests carry their own tiny system — reconciling diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index b8ec042f3e..ac25e2a0c2 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -35,6 +35,7 @@ import { applyImageRescaleItems } from "../image-rescale.js"; import { clampOutgoingOutput, countLoadedToolTokens, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge, emergencyNudgeArmed, modelVisibleTools } from "./budget.js"; import { FORCE_TEXT_PROTOCOL, injectResponsesTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions } from "./lane-decisions.js"; +import { pinSnapshotIds } from "../snapshot-lane.js"; export async function prepareResponses( parsed: ResponsesRequestBody, @@ -206,7 +207,9 @@ export async function prepareResponses( sysNotes = outcome.notes; if (outcome.outbound !== fresh) projection.systemParts = outcome.outbound ? [outcome.outbound] : []; } - const { msgs } = projection; + // #3756: pin historical ids onto the server-side snapshot (proxy lane + // only; passthrough byte-identical whenever the lane does not engage). + const msgs = pinSnapshotIds({ sessionId, protocol: "responses", pluginMode, msgs: projection.msgs, state: session.state, log }); originalMessages = msgs; if (opts.debug) { log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); diff --git a/src/snapshot-lane.ts b/src/snapshot-lane.ts new file mode 100644 index 0000000000..4410861eec --- /dev/null +++ b/src/snapshot-lane.ts @@ -0,0 +1,438 @@ +/** + * Snapshot lane (#3756): the server-side snapshot is the authoritative rendering + * source for proxy-lane outbound history — root fix of #3752 (R2). + * + * Why this exists: in proxy mode every message id is derived from the bytes the + * client resends each turn (deriveCanonicalMessageId at parse time). When the + * kernel's identity function changes (#3658), every id in a persisted session is + * re-minted → block effectiveMessageIds coverage lists lose track of everything → + * heuristic reconciliation cannot save past-limit sessions → mass unfold + * (126K→998K tokens, 2% cache hit). This lane pins historical ids to what the + * unified store already recorded, so re-minting old messages structurally + * disappears: a kernel upgrade or identity-function swap can never touch them. + * + * Mechanism (per proxy-mode turn, after XToCore): + * - A per-session registry mirrors the unified `messages` table rows + * ((session_id, seq), protocol-neutral core BiliMessage JSON — schema.ts:127), + * cold-seeded from unifiedRawMessages and kept exact across restarts by a + * small persisted row-id list (session_ext "snap:reg"). + * - The client's freshly parsed core view is aligned against the registry + * ORDER-CENTRICALLY (KDD #11 doctrine at lane level): content keys are + * dialect-neutral (role|contentType|toolName|text — toolCallId excluded by + * design, #3642) and HOST-computed (node:crypto sha256), so they survive + * kernel identity-function changes; stored-id equality is the fast path. + * - Aligned prefix messages are STAMPED with their stored ids before the + * pipeline continues. Downstream nothing changes shape: reconcileFoldCoverage + * finds full coverage (dormant), the kernel's own fold rendering matches + * zero-drift, and the outbound prefix bytes stay stable turn-over-turn. + * - New tail messages keep their derived ids and join the registry on append — + * they become pinned from the next turn on. + * + * Divergence semantics (pinned down in #3756): + * - UNCOVERED tail region: the client is authoritative. Rewind/edit-tail + * truncates the registry there (originals stay retrievable via CCR); rows the + * client stopped resending are dropped. + * - COVERED region: the server is authoritative. Rows covered by an active + * block survive any client delete/rewind/checkpoint replacement — they stay + * in the view so the kernel keeps rendering their summaries (outbound + * unchanged). A foreign head the client swaps in (dsh-style wholesale + * checkpoint replacement) is ABSORBED as observation: recorded (head marker + + * one info log) but never forwarded into the outbound. + * + * Shapes handled in v1 — everything else falls through to PASSTHROUGH (the + * client's parsed view is returned untouched = today's exact behavior, zero + * regression by construction): + * APPEND registry is a prefix of the resend (steady state; also appends that + * land after a truncation whose retained covered tail sits at the end + * of the registry — all trailing registry rows must be covered). + * TRUNCATE resend is a strict prefix of the registry (rewind / edit-tail). + * REPLACE m==0, matched suffix L≥MIN_TAIL_MATCH, foreign middle segment with + * no key overlap anywhere in the registry (checkpoint landing). + * Bail reasons: mid-conversation insertion, reorder/reinsert, ambiguous short + * tails, gaps beyond MAX_GAP_SKIP, duplicate-append collision. + * + * Gate: rollout feature `proxySnapshotLane` (default OFF — env/config gray + * release per #3062) AND a live unified store (shadow or full). Plugin mode is + * out of scope (the agent owns compression there). Legacy storage: one info log + * per session, existing path unchanged. Block bookkeeping stays id-keyed — no + * seq migration because stored ids are never re-minted (id-keyed == seq-keyed + * stability). Non-goals (v1): google lane, middle-edit pinning, CCR storage of + * absorbed heads, #3771 image fingerprint seeds. + */ + +import { createHash } from "node:crypto"; +import type { BiliMessage } from "acp-kernel/wire"; +import type { CompressionState } from "acp-kernel"; +import { getStore } from "./persist.js"; +import { featureEnabled } from "./rollout.js"; +import { coveredRealHistoryIds } from "./session.js"; + +const FEATURE = "proxySnapshotLane"; +const EXT_REGISTRY = "snap:reg"; +const EXT_HEAD = "snap:head"; + +/** In-memory registries, LRU-ordered (insertion order = recency). Mirrors the + * incremental lane's canonicalCache bound — full histories are MB-scale. */ +const REGISTRY_LIMIT = 16; +/** Max registry rows the forward walk may skip over (server-retained rows the + * client stopped resending) before declaring divergence. */ +const MAX_GAP_SKIP = 64; +/** Consecutive agreements required to accept a gap jump (resync run). */ +const MIN_RESYNC_RUN = 3; +/** Minimum matched suffix for foreign-head absorption (REPLACE). */ +const MIN_TAIL_MATCH = 3; +/** Bound for the info-once legacy-fallback set. */ +const LEGACY_INFO_LIMIT = 1024; + +interface Row { + seq: number; + id: string; + key: string; + core: BiliMessage; +} + +interface Registry { + rows: Row[]; + /** High-water seq for cold-seed filtering (appended rows beyond it are + * picked up from the store without an ext rewrite). */ + maxSeq: number; + /** Absorbed foreign-head key list (REPLACE); null = none. */ + headKeys: string[] | null; +} + +const registries = new Map(); +const legacyInfoLogged = new Set(); +const bailLogged = new Map>(); + +/** Host-side alignment key: dialect-neutral and independent of the kernel's + * (upgradable) identity function. toolCallId is deliberately EXCLUDED — it is + * re-minted across dialects/projections (#3642 doctrine); an image sidecar + * digest is included so image-only differences don't collide. */ +export function alignKeyOf(m: BiliMessage): string { + let s = `${m.role}|${m.contentType}|${m.toolName ?? ""}|${m.text ?? ""}`; + if (typeof m.imageBase64 === "string" && m.imageBase64.length > 0) { + s += "|img:" + createHash("sha256").update(m.imageBase64).digest("hex"); + } + return createHash("sha256").update(s).digest("hex"); +} + +function pairEq(r: Row, c: BiliMessage, rKey: string, cKey: string): boolean { + return r.id === c.id || rKey === cKey; +} + +function bailReasons(sessionId: string): Set { + let set = bailLogged.get(sessionId); + if (!set) { + set = new Set(); + bailLogged.set(sessionId, set); + } + return set; +} + +function infoOnceLegacy(sessionId: string, log: (level: string, msg: string) => void): void { + if (legacyInfoLogged.has(sessionId)) return; + legacyInfoLogged.add(sessionId); + if (legacyInfoLogged.size > LEGACY_INFO_LIMIT) legacyInfoLogged.clear(); + log("info", `[snaplane:${sessionId}] unified store unavailable — snapshot lane inactive, existing path unchanged (info once)`); +} + +function bailOnce(sessionId: string, reason: string, detail: string, log: (level: string, msg: string) => void): void { + const set = bailReasons(sessionId); + if (set.has(reason)) return; + set.add(reason); + log("debug", `[snaplane:${sessionId}] passthrough: ${reason} (${detail})`); +} + +interface PersistedRegistry { + v: number; + ids: string[]; + maxSeq: number; +} + +function readPersistedRegistry(sessionId: string): PersistedRegistry | null { + try { + const v = getStore().getSessionExt(sessionId, EXT_REGISTRY); + if (typeof v !== "object" || v === null) return null; + const o = v as Partial; + if (o.v !== 1 || !Array.isArray(o.ids) || !Number.isFinite(o.maxSeq)) return null; + return { v: 1, ids: o.ids.filter((x): x is string => typeof x === "string"), maxSeq: o.maxSeq as number }; + } catch { + return null; + } +} + +function persistRegistry(sessionId: string, reg: Registry, log: (level: string, msg: string) => void): void { + try { + getStore().setSessionExt(sessionId, EXT_REGISTRY, { + v: 1, + ids: reg.rows.map((r) => r.id), + maxSeq: reg.maxSeq, + } satisfies PersistedRegistry); + } catch (err) { + bailOnce(sessionId, "ext-write-failed", String(err), log); + } +} + +function parseRows(raw: { seq: number; content: string }[]): Row[] { + const out: Row[] = []; + for (const row of raw) { + let p: unknown; + try { + p = JSON.parse(row.content); + } catch { + continue; // malformed row: skip (store-level corruption is logged elsewhere) + } + if (typeof p !== "object" || p === null) continue; + const m = p as BiliMessage; + if (typeof m.id !== "string" || typeof m.role !== "string" || typeof m.contentType !== "string") continue; + out.push({ seq: row.seq, id: m.id, key: alignKeyOf(m), core: m }); + } + return out; +} + +function ensureRegistry(sessionId: string): Registry | null { + let reg = registries.get(sessionId); + if (reg) { + // LRU touch: move to the back (most recent). + registries.delete(sessionId); + registries.set(sessionId, reg); + return reg; + } + const store = getStore(); + if (!store.replicasAvailable()) return null; + const raw = store.unifiedRawMessages(sessionId); + if (raw === null) return null; + const all = parseRows(raw); + let rows = all; + const persisted = readPersistedRegistry(sessionId); + if (persisted && persisted.ids.length > 0) { + const byId = new Map(); + for (const r of all) if (!byId.has(r.id)) byId.set(r.id, r); + const out: Row[] = []; + for (const id of persisted.ids) { + const r = byId.get(id); + if (r) out.push(r); + } + rows = out; + } + // Appended rows ingested after the last ext rewrite ride the seq filter. + const maxSeqSoFar = persisted?.maxSeq ?? Number.MIN_SAFE_INTEGER; + if (persisted) { + for (const r of all) { + if (r.seq > maxSeqSoFar && !rows.some((x) => x.id === r.id)) rows.push(r); + } + rows.sort((a, b) => a.seq - b.seq); + } + reg = { + rows, + maxSeq: rows.reduce((mx, r) => Math.max(mx, r.seq), 0), + headKeys: null, + }; + try { + const h = store.getSessionExt(sessionId, EXT_HEAD); + if (Array.isArray(h) && h.every((x) => typeof x === "string")) reg.headKeys = h as string[]; + } catch { + /* head marker unavailable: treat as none */ + } + registries.set(sessionId, reg); + while (registries.size > REGISTRY_LIMIT) { + const oldest = registries.keys().next().value as string | undefined; + if (oldest === undefined) break; + registries.delete(oldest); + bailLogged.delete(oldest); + } + return reg; +} + +function appendRows(reg: Registry, fresh: BiliMessage[], seenIds: Set, sessionId: string, log: (level: string, msg: string) => void): void { + for (const m of fresh) { + if (seenIds.has(m.id)) { + bailOnce(sessionId, "dup-append", `id ${m.id} already in registry`, log); + continue; + } + reg.maxSeq += 1; + reg.rows.push({ seq: reg.maxSeq, id: m.id, key: alignKeyOf(m), core: m }); + seenIds.add(m.id); + } +} + +export interface PinInput { + sessionId: string; + protocol: string; + pluginMode: boolean; + msgs: BiliMessage[]; + state: CompressionState; + log: (level: string, msg: string) => void; +} + +/** Pin historical message ids onto the server-side snapshot. Returns the same + * array reference untouched whenever the lane does not engage (gate off, + * plugin mode, legacy storage, empty view, or any unsupported shape) — the + * caller then proceeds exactly as before. */ +export function pinSnapshotIds(input: PinInput): BiliMessage[] { + const { sessionId, pluginMode, msgs, state, log } = input; + if (pluginMode || msgs.length === 0) return msgs; + if (!featureEnabled(FEATURE)) return msgs; + const reg = ensureRegistry(sessionId); + if (reg === null) { + infoOnceLegacy(sessionId, log); + return msgs; + } + const covered = coveredRealHistoryIds(state.blocks); + const cKeys = msgs.map(alignKeyOf); + + // Absorbed foreign head (previous REPLACE): verify, then exclude from the + // aligned view — it was observed, never adopted into the outbound. + let shift = 0; + if (reg.headKeys && reg.headKeys.length > 0) { + if (msgs.length >= reg.headKeys.length) { + let ok = true; + for (let i = 0; i < reg.headKeys.length; i++) { + if (cKeys[i] !== reg.headKeys[i]) { ok = false; break; } + } + shift = ok ? reg.headKeys.length : 0; + if (!ok) reg.headKeys = null; // client dropped the absorbed head + } else { + reg.headKeys = null; + } + } + const C = msgs.slice(shift); + const cK = cKeys.slice(shift); + const R = reg.rows; + const rK = R.map((r) => r.key); + + // Order-centric forward walk: positional agreement advances both; a break + // tries a bounded gap-jump over server-retained rows and accepts it only + // on a MIN_RESYNC_RUN consecutive-agreement resync (KDD #11 doctrine). + // mapC/t records the EXACT C↔R pairing (a jump makes mC ≠ mR, so parallel + // indexing would mis-stamp ids); skipped records jumped-over registry rows. + const mapC: number[] = []; + const skipped: number[] = []; + let i = 0; + let j = 0; + outer: while (i < C.length && j < R.length) { + if (pairEq(R[j], C[i], rK[j], cK[i])) { mapC.push(j); i++; j++; continue; } + for (let k = j + 1; k < R.length && k <= j + MAX_GAP_SKIP; k++) { + if (!pairEq(R[k], C[i], rK[k], cK[i])) continue; + let ii = i; + let kk = k; + let run = 0; + while (ii < C.length && kk < R.length && pairEq(R[kk], C[ii], rK[kk], cK[ii])) { ii++; kk++; run++; } + if (run >= MIN_RESYNC_RUN) { + for (let s = j; s < k; s++) skipped.push(s); + for (let s = i; s < ii; s++) mapC.push(k + (s - i)); + i = ii; + j = kk; + continue outer; + } + break; + } + break; + } + const mC = i; + const mR = j; + const restR = R.length - mR; + const restC = C.length - mC; + + // Aligned prefix in registry order: aligned rows take the client's bytes + // (cache-stable) stamped with the stored id; gap-skipped rows emit their + // stored core so their ids stay visible to the kernel (absence of a + // covered id reads as drift and would unfold the block). + const emitAlignedPrefix = (out: BiliMessage[]): void => { + let t = 0; + for (let r = 0; r < mR; r++) { + if (t < mC && mapC[t] === r) { out.push({ ...C[t], id: R[r].id }); t++; } + else out.push(R[r].core); + } + }; + + // APPEND: everything left in the registry is covered-retained (or empty) — + // the client's remainder is all-new tail. + if (restR === 0 || R.slice(mR).every((r) => covered.has(r.id))) { + const gapBad = skipped.find((s) => !covered.has(R[s].id)); + if (gapBad !== undefined) { + bailOnce(sessionId, "gap-uncovered", `registry row ${gapBad} skipped by the client is not covered (mid-history edit — v1 passthrough)`, log); + return msgs; + } + // Ambiguity guard: the UNALIGNED registry is what the client no longer + // presents in place — tail content re-presenting ITS keys is a + // checkpoint-shaped rewrite, not an append. (Aligned rows may legally + // recur in the tail: repeated user content.) + const rKeySet = new Set(R.slice(mR).map((r) => r.key)); + for (let t = mC; t < C.length; t++) { + if (rKeySet.has(cK[t])) { + bailOnce(sessionId, "dup-append", `tail key collides with unaligned registry at position ${t}`, log); + return msgs; + } + } + const view: BiliMessage[] = []; + emitAlignedPrefix(view); + for (let t = mR; t < R.length; t++) view.push(R[t].core); + for (let t = mC; t < C.length; t++) view.push(C[t]); + const seen = new Set(reg.rows.map((r) => r.id)); + appendRows(reg, C.slice(mC), seen, sessionId, log); + return view; + } + + // TRUNCATE: the client stopped resending at mC — uncovered dead-zone rows + // drop (client authoritative, CCR keeps originals), covered rows retain + // (server authoritative — their summaries keep rendering). + if (restC === 0) { + const dead = R.slice(mR); + const retained = dead.filter((r) => covered.has(r.id)); + const view: BiliMessage[] = []; + emitAlignedPrefix(view); + for (const r of retained) view.push(r.core); + reg.rows = [...R.slice(0, mR), ...retained]; + reg.maxSeq = Math.max(...reg.rows.map((r) => r.seq), 0); + persistRegistry(sessionId, reg, log); + return view; + } + + // REPLACE: matched suffix + foreign middle segment (checkpoint landing). + let L = 0; + while (L < restC && L < restR && (rK[R.length - 1 - L] === cK[C.length - 1 - L] || R[R.length - 1 - L].id === C[C.length - 1 - L].id)) { + L++; + } + if (L < MIN_TAIL_MATCH) { + bailOnce(sessionId, "short-tail", `matched suffix ${L} < ${MIN_TAIL_MATCH} (mC=${mC}, mR=${mR}, restC=${restC}, restR=${restR})`, log); + return msgs; + } + if (mC !== 0) { + bailOnce(sessionId, "mid-insert", `agreed prefix ${mC} with non-empty remainders (v1 supports head-only foreign segments)`, log); + return msgs; + } + const middleC = C.slice(0, C.length - L); + const dead = R.slice(mR, R.length - L); + const rKeySet = new Set(rK); + for (const k of middleC) { + if (rKeySet.has(alignKeyOf(k))) { + bailOnce(sessionId, "foreign-overlap", "middle segment key present in registry (reorder/reinsert — v1 passthrough)", log); + return msgs; + } + } + const retained = dead.filter((r) => covered.has(r.id)); + const view: BiliMessage[] = []; + for (const r of retained) view.push(r.core); + for (let t = 0; t < L; t++) { + view.push({ ...C[C.length - L + t], id: R[R.length - L + t].id }); + } + reg.rows = [...retained, ...R.slice(R.length - L)]; + reg.maxSeq = Math.max(...reg.rows.map((r) => r.seq), 0); + reg.headKeys = middleC.map((m) => alignKeyOf(m)); + persistRegistry(sessionId, reg, log); + try { + getStore().setSessionExt(sessionId, EXT_HEAD, reg.headKeys); + } catch (err) { + bailOnce(sessionId, "ext-write-failed", String(err), log); + } + log("info", `[snaplane:${sessionId}] absorbed ${middleC.length} foreign head message(s) (client checkpoint/rewrite) — recorded as observation, not forwarded; outbound keeps server summary + snapshot prefix (#3756)`); + return view; +} + +/** Test-only: drop all in-memory registries and once-log latches. */ +export function __resetSnapshotLaneForTests(): void { + registries.clear(); + legacyInfoLogged.clear(); + bailLogged.clear(); +} diff --git a/tests/snapshot-lane.test.ts b/tests/snapshot-lane.test.ts new file mode 100644 index 0000000000..ef4c13637e --- /dev/null +++ b/tests/snapshot-lane.test.ts @@ -0,0 +1,347 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { createCore, createInitialState, defaultConfig, type CompressionBlock, type CoreMessage } from "acp-kernel"; +import type { BiliMessage } from "acp-kernel/wire"; +import type { Session } from "../src/session.ts"; +import { foldCoverage } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { alignKeyOf, pinSnapshotIds, __resetSnapshotLaneForTests } from "../src/snapshot-lane.ts"; +import { rmrf } from "./tmp-rm.ts"; + +// #2671-style hermeticity: pin config + state dir before any store or rollout +// touch; feature forced ON per-test-file (the one gate-off test unsets it). +process.env.BILI_CONFIG_FILE = path.join( + mkdtempSync(path.join(tmpdir(), "bili-snaplane-cfg-")), + "no-config.json", +); +process.env.BILI_STATE_DIR = mkdtempSync(path.join(tmpdir(), "bili-snaplane-state-")); +process.env.BILI_STORAGE_UNIFIED = "legacy"; +process.env.BILI_ROLLOUT_PROXY_SNAPSHOT_LANE = "1"; + +function makeSession(id: string): Session { + return { + id, + meta: { protocol: "openai", upstreamOrigin: "http://up:1", title: "snapshot lane test" }, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 1000, compressCreditTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + createdAt: Date.now() - 1000, + lastSeen: Date.now(), + state: createInitialState(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +function mkText(id: string, n: number): BiliMessage { + return { id, role: n % 2 === 0 ? "user" : "assistant", contentType: "text", text: `snap message ${n} ${"z".repeat(48)}` }; +} + +function mkBlock(ids: string[]): CompressionBlock { + return { + blockId: `blk-${ids[0]}`, + runId: "run-1", + tier: 1, + summary: "s", + directMessageIds: [], + effectiveMessageIds: ids, + directBlockIds: [], + compressedTokens: 100, + createdAt: Date.now(), + survivedCount: 0, + generation: "young", + active: true, + }; +} + +type Log = [string, string]; + +/** Hermetic shadow-mode store with `cores` ingested as the session's raw tail + * (writeNow mirrors the production #3007 ingest), pointed at by getStore(). */ +async function seedStore(dir: string, sessionId: string, cores: BiliMessage[]): Promise { + const prev = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "shadow"; + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + _setStoreForTest(store); + const session = makeSession(sessionId); + session.lastMessages = cores as CoreMessage[]; + await store.writeNow(session); + if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prev; + return store; +} + +function storedIds(store: SessionStore, sessionId: string): string[] { + const rows = store.unifiedRawMessages(sessionId); + assert.ok(rows, "unified raw rows must exist after seed"); + return [...rows!].sort((a, b) => a.seq - b.seq).map((r) => (JSON.parse(r.content) as BiliMessage).id); +} + +function pin(sessionId: string, state: ReturnType, msgs: BiliMessage[], extra?: Partial<{ pluginMode: boolean; protocol: string }>): { out: BiliMessage[]; logs: Log[] } { + const logs: Log[] = []; + const out = pinSnapshotIds({ + sessionId, + protocol: extra?.protocol ?? "anthropic", + pluginMode: extra?.pluginMode ?? false, + msgs, + state, + log: (level, msg) => { logs.push([level, msg]); }, + }); + return { out, logs }; +} + +test("alignKeyOf: dialect-neutral (toolCallId excluded), content-sensitive (#3642 doctrine)", () => { + const base = mkText("h_a", 1); + const reminted: BiliMessage = { ...base, id: "h_other", toolCallId: "call_abc" }; + assert.equal(alignKeyOf(base), alignKeyOf(reminted), "id/toolCallId churn must not change the alignment key"); + assert.notEqual(alignKeyOf(base), alignKeyOf(mkText("h_b", 2)), "different text → different key"); + assert.notEqual(alignKeyOf(base), alignKeyOf({ ...base, role: base.role === "user" ? "assistant" : "user" }), "different role → different key"); + const imgA: BiliMessage = { ...base, imageBase64: "aWFtYWdlQ" }; + const imgB: BiliMessage = { ...base, imageBase64: "aW1hZ2VC" }; + assert.notEqual(alignKeyOf(imgA), alignKeyOf(imgB), "image sidecar differences must not collide"); +}); + +test("gates: feature off / plugin mode / empty view pass through the SAME reference", () => { + const state = createInitialState(); + const msgs = [mkText("h_a", 1)]; + const saved = process.env.BILI_ROLLOUT_PROXY_SNAPSHOT_LANE; + delete process.env.BILI_ROLLOUT_PROXY_SNAPSHOT_LANE; + try { + assert.equal(pin("snap-gate-off", state, msgs).out, msgs, "feature off → untouched ref"); + } finally { + if (saved === undefined) delete process.env.BILI_ROLLOUT_PROXY_SNAPSHOT_LANE; + else process.env.BILI_ROLLOUT_PROXY_SNAPSHOT_LANE = saved; + } + assert.equal(pin("snap-plugin", state, msgs, { pluginMode: true }).out, msgs, "plugin mode → untouched ref"); + const empty: BiliMessage[] = []; + assert.equal(pin("snap-empty", state, empty).out, empty, "empty view → untouched ref"); +}); + +test("legacy storage: passthrough + info-once fallback note", async () => { + __resetSnapshotLaneForTests(); + const dir = mkdtempSync(path.join(tmpdir(), "bili-snaplane-legacy-")); + const prev = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "legacy"; + try { + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + _setStoreForTest(store); + const state = createInitialState(); + const msgs = [mkText("h_a", 1), mkText("h_b", 2)]; + const first = pin("snap-legacy-1", state, msgs); + assert.equal(first.out, msgs, "legacy-only instance → untouched ref"); + assert.ok(first.logs.some(([lvl, m]) => lvl === "info" && /snapshot/i.test(m)), "first call must log the fallback once"); + assert.equal(first.logs.filter(([lvl]) => lvl === "info").length, 1); + const second = pin("snap-legacy-1", state, msgs); + assert.equal(second.out, msgs); + assert.equal(second.logs.length, 0, "second call of the same session must stay quiet"); + store.closeQuietly(); + } finally { + if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = prev; + rmrf(dir); + } +}); + +test("APPEND: steady-state tail growth stamps the prefix and keeps client bytes verbatim", async () => { + __resetSnapshotLaneForTests(); + const dir = mkdtempSync(path.join(tmpdir(), "bili-snaplane-append-")); + try { + const a = mkText("h_a", 1), b = mkText("h_b", 2), c = mkText("h_c", 3); + const store = await seedStore(dir, "snap-append", [a, b, c]); + assert.deepEqual(storedIds(store, "snap-append"), ["h_a", "h_b", "h_c"], "ingest must preserve explicit ids verbatim"); + const resent = [mkText("p_a", 1), mkText("p_b", 2), mkText("p_c", 3), mkText("p_d", 4)]; + const { out } = pin("snap-append", createInitialState(), resent); + assert.deepEqual(out.map((m) => m.id), ["h_a", "h_b", "h_c", "p_d"], "prefix pinned to stored ids, tail keeps its own"); + assert.deepEqual(out.map((m) => m.text), resent.map((m) => m.text), "client bytes verbatim (cache-stable)"); + // Durability: appended rows ride the production save path (#3007 + // rawTail ingest on writeNow) — simulate the post-turn save. + const after = makeSession("snap-append"); + after.lastMessages = [a, b, c, resent[3]] as CoreMessage[]; + await store.writeNow(after); + assert.deepEqual(storedIds(store, "snap-append"), ["h_a", "h_b", "h_c", "p_d"], "appended tail persisted via the normal save path"); + store.closeQuietly(); + } finally { + rmrf(dir); + } +}); + +test("TRUNCATE (uncovered rewind): registry clamps to the client's prefix", async () => { + __resetSnapshotLaneForTests(); + const dir = mkdtempSync(path.join(tmpdir(), "bili-snaplane-trunc-")); + try { + const store = await seedStore(dir, "snap-trunc", [mkText("h_a", 1), mkText("h_b", 2), mkText("h_c", 3)]); + const resent = [mkText("p_a", 1), mkText("p_b", 2)]; + const { out } = pin("snap-trunc", createInitialState(), resent); + assert.deepEqual(out.map((m) => m.id), ["h_a", "h_b"]); + const reg = store.getSessionExt("snap-trunc", "snap:reg") as { ids: string[] }; + assert.deepEqual(reg.ids, ["h_a", "h_b"], "dead uncovered tail dropped from the registry"); + store.closeQuietly(); + } finally { + rmrf(dir); + } +}); + +test("TRUNCATE into covered region: covered rows stay server-authoritative in view AND registry", async () => { + __resetSnapshotLaneForTests(); + const dir = mkdtempSync(path.join(tmpdir(), "bili-snaplane-trunc-cover-")); + try { + const store = await seedStore(dir, "snap-trunc-cov", [mkText("h_a", 1), mkText("h_b", 2), mkText("h_c", 3)]); + const state = createInitialState(); + state.blocks.push(mkBlock(["h_c"])); + const resent = [mkText("p_a", 1), mkText("p_b", 2)]; + const { out } = pin("snap-trunc-cov", state, resent); + // Only the covered row was rewound past → APPEND shape (dead tail fully + // covered): the covered row must stay in the outbound view (its id must + // remain visible to the kernel or the block unfolds). + assert.deepEqual(out.map((m) => m.id), ["h_a", "h_b", "h_c"], "covered row survives the client rewind (server authority over the covered region)"); + const rowsCov = store.unifiedRawMessages("snap-trunc-cov"); + assert.ok(rowsCov, "rows exist"); + assert.deepEqual(out[2], JSON.parse(rowsCov[2].content), "covered row rendered from the stored snapshot core"); + store.closeQuietly(); + } finally { + rmrf(dir); + } +}); + +test("REPLACE: foreign checkpoint head absorbed (observed, never forwarded); next turn re-verifies it", async () => { + __resetSnapshotLaneForTests(); + const dir = mkdtempSync(path.join(tmpdir(), "bili-snaplane-replace-")); + try { + const cores = [mkText("h_a", 1), mkText("h_b", 2), mkText("h_c", 3), mkText("h_d", 4), mkText("h_e", 5), mkText("h_f", 6)]; + const store = await seedStore(dir, "snap-replace", cores); + const state = createInitialState(); + state.blocks.push(mkBlock(["h_c", "h_d", "h_e", "h_f"])); + const ckpt = (): BiliMessage => ({ id: "ckpt-1", role: "user", contentType: "text", text: "full checkpoint replacement blob" }); + // Turn 1: client replaces its whole history with checkpoint + fresh + // copies of the last three messages (new wire ids, same content). + const t1 = [ckpt(), mkText("q_d", 4), mkText("q_e", 5), mkText("q_f", 6)]; + const r1 = pin("snap-replace", state, t1); + assert.deepEqual(r1.out.map((m) => m.id), ["h_c", "h_d", "h_e", "h_f"], "outbound = server snapshot only; checkpoint NOT forwarded"); + assert.ok(!r1.out.some((m) => m.text === "full checkpoint replacement blob"), "checkpoint content must never reach the outbound"); + assert.ok(r1.logs.some(([lvl, m]) => lvl === "info" && /absorbed/i.test(m)), "absorption is reported once at info level"); + // Turn 2: same checkpoint head again + one brand-new tail message. + const t2 = [ckpt(), mkText("r_d", 4), mkText("r_e", 5), mkText("r_f", 6), mkText("r_g", 7)]; + const r2 = pin("snap-replace", state, t2); + assert.deepEqual(r2.out.map((m) => m.id), ["h_c", "h_d", "h_e", "h_f", "r_g"], "head marker verified → checkpoint excluded, tail appended"); + assert.ok(!r2.out.some((m) => m.id === "ckpt-1")); + // APPEND deliberately does NOT rewrite the registry ext — appended rows + // ride the production save path + seq filter. Simulate the post-turn + // save, then prove restart determinism via a cold re-seed. + const reg = store.getSessionExt("snap-replace", "snap:reg") as { ids: string[] }; + assert.deepEqual(reg.ids, ["h_c", "h_d", "h_e", "h_f"], "ext still holds the REPLACE write; append rides rawTail"); + const saved = makeSession("snap-replace"); + saved.lastMessages = r2.out as CoreMessage[]; + await store.writeNow(saved); + __resetSnapshotLaneForTests(); + // g re-mints its wire id (s_g) after the upgrade — the lane must pin it + // back to the id frozen at ingest (r_g), exactly acceptance ①. + const r3 = pin("snap-replace", state, [ckpt(), mkText("s_d", 4), mkText("s_e", 5), mkText("s_f", 6), mkText("s_g", 7)]); + assert.deepEqual(r3.out.map((m) => m.id), ["h_c", "h_d", "h_e", "h_f", "r_g"], "cold re-seed recovers the appended tail and pins its frozen id across the re-mint"); + store.closeQuietly(); + } finally { + rmrf(dir); + } +}); + +test("bails: mid-insert / short-tail / dup-append all return the input untouched", async () => { + __resetSnapshotLaneForTests(); + const dirs: string[] = []; + let store: SessionStore | null = null; + try { + const d1 = mkdtempSync(path.join(tmpdir(), "bili-snaplane-bail1-")); + dirs.push(d1); + store = await seedStore(d1, "snap-bail-mid", [mkText("h_a", 1), mkText("h_b", 2), mkText("h_c", 3)]); + const midInsert: BiliMessage[] = [mkText("p_a", 1), { id: "x-mid", role: "user", contentType: "text", text: "inserted middle message" }, mkText("p_b", 2), mkText("p_c", 3)]; + assert.equal(pin("snap-bail-mid", createInitialState(), midInsert).out, midInsert, "mid-insert (uncovered remainder both sides) → passthrough"); + + const d2 = mkdtempSync(path.join(tmpdir(), "bili-snaplane-bail2-")); + dirs.push(d2); + store = await seedStore(d2, "snap-bail-short", [mkText("h_a", 1), mkText("h_b", 2), mkText("h_c", 3)]); + const shortTail: BiliMessage[] = [{ id: "x-head", role: "user", contentType: "text", text: "some foreign head" }, mkText("p_c", 3)]; + assert.equal(pin("snap-bail-short", createInitialState(), shortTail).out, shortTail, "matched suffix < 3 → passthrough"); + + // dup-append: the whole registry is covered (so APPEND is reached), + // but the resent tail re-presents unaligned registry content under new + // ids — a checkpoint-shaped rewrite, ambiguous as an append; bail. + const d3 = mkdtempSync(path.join(tmpdir(), "bili-snaplane-bail3-")); + dirs.push(d3); + store = await seedStore(d3, "snap-bail-dup", [mkText("h_a", 1), mkText("h_b", 2)]); + const stateDup = createInitialState(); + stateDup.blocks.push(mkBlock(["h_a", "h_b"])); + const dupAppend: BiliMessage[] = [{ id: "x-h", role: "user", contentType: "text", text: "foreign head" }, mkText("p_a", 1), mkText("p_b", 2)]; + assert.equal(pin("snap-bail-dup", stateDup, dupAppend).out, dupAppend, "dup-append collision → passthrough"); + } finally { + store?.closeQuietly(); + for (const d of dirs) rmrf(d); + } +}); + +// #3752 replay: an identity-function swap (kernel upgrade re-seeding every +// content hash) mass-unfolded persisted sessions because each turn re-derived +// historical ids from client-resent bytes. With the snapshot lane engaged the +// historical ids come from the store instead — coverage survives and the +// outbound stays byte-stable. The unpinned control leg proves the drift is +// real without the lane. +test("identity swap (acceptance 1+4): pinned resend keeps coverage, outbound byte-stable; unpinned control drifts", async () => { + __resetSnapshotLaneForTests(); + const dir = mkdtempSync(path.join(tmpdir(), "bili-snaplane-swap-")); + try { + const session = makeSession("snap-swap"); + const core = createCore(); + const config = defaultConfig(200000); + const mkOld = (i: number): CoreMessage => ({ id: `h_old${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `message ${i} ${"x".repeat(2000)}` }); + const oldMsgs: CoreMessage[] = []; + for (let i = 0; i < 24; i++) oldMsgs.push(mkOld(i)); + const turn0 = core.processTurn({ messages: oldMsgs, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn0.state; + const res0 = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00007", summary: "compressed early history that is long enough to pass the min summary length check" }], + messages: turn0.messages, + state: turn0.state, + config, + }); + session.state = res0.state; + const block = session.state.blocks[session.state.blocks.length - 1]; + assert.ok(block.active, "fixture block must be active"); + const coveredIds = [...block.effectiveMessageIds]; + assert.ok(coveredIds.length > 0, "fixture block must cover message ids"); + + const store = await seedStore(dir, "snap-swap", oldMsgs as BiliMessage[]); + assert.deepEqual(storedIds(store, "snap-swap"), oldMsgs.map((m) => m.id)); + const coveredBefore = new Set(coveredIds); + + // Leg A — steady state today (same ids, lane engaged): baseline render. + const stateA = structuredClone(session.state); + const legA = pin("snap-swap", stateA, oldMsgs.map((m) => ({ ...m })) as BiliMessage[]); + assert.deepEqual(legA.out.map((m) => m.id), oldMsgs.map((m) => m.id)); + const turnA = core.processTurn({ messages: legA.out, state: stateA, config, tokenCount: 9999, renderTags: "text-only" }); + + // Leg B — identity swap (every historical id re-minted by the "new" + // kernel): pinning must restore the stored ids before processTurn. + const swapped = oldMsgs.map((m, i) => ({ ...m, id: `h_new${i}` })) as BiliMessage[]; + const stateB = structuredClone(session.state); + const legB = pin("snap-swap", stateB, swapped); + assert.deepEqual(legB.out.map((m) => m.id), oldMsgs.map((m) => m.id), "historical ids pinned to the snapshot despite the identity swap"); + assert.deepEqual(legB.out.map((m) => m.text), swapped.map((m) => m.text), "client bytes stay verbatim"); + assert.equal(foldCoverage(coveredBefore, legB.out.map((m) => m.id)), null, "coverage intact after pinning"); + const turnB = core.processTurn({ messages: legB.out, state: stateB, config, tokenCount: 9999, renderTags: "text-only" }); + const blockAfter = turnB.state.blocks[turnB.state.blocks.length - 1]; + assert.equal(blockAfter.active, true, "block must survive the swapped resend"); + + // Leg C — control: the same swapped resend WITHOUT the lane. + const stateC = structuredClone(session.state); + core.processTurn({ messages: swapped, state: stateC, config, tokenCount: 9999, renderTags: "text-only" }); + const gapC = foldCoverage(coveredBefore, swapped.map((m) => m.id)); + assert.ok(gapC !== null, "unpinned swapped resend must show a coverage gap"); + assert.equal(gapC.matched, 0, "no covered id survives the identity swap without pinning"); + + assert.deepEqual(JSON.stringify(turnA.messages), JSON.stringify(turnB.messages), "outbound byte-stable across the identity swap (acceptance 1+4)"); + store.closeQuietly(); + } finally { + rmrf(dir); + } +});