diff --git a/src/identity-harvest.ts b/src/identity-harvest.ts index 085248825c..62b86ee726 100644 --- a/src/identity-harvest.ts +++ b/src/identity-harvest.ts @@ -20,8 +20,18 @@ // source, #4491) and the whole-turn-degrade contract: an adapter returns // exactly `expectedLen` ids in wire order or null — a partial or shifted // array degrades the ENTIRE turn to the pre-wiring path. Ids are never -// reused by any harvested client, so misbinding is impossible; a failed -// alignment at worst misses pinning, it cannot attach a wrong identity. +// reused by any harvested client, so a harvested id can never belong to +// ANOTHER session; the residual risk is an intra-session positional +// misbinding — an EVEN transcript/wire shift keeps the user/assistant +// role pattern intact and defeats role checks — which the per-session +// id-continuity check in alignTailIds exists to reject: any store +// rewrite/compaction/late-appended row that moves an already-pinned row +// degrades the turn instead of re-stamping shifted ids. A failed +// alignment at worst misses pinning (consequence: cache-pin drift); +// identity leakage across sessions is structurally impossible. A pure +// EVEN late append that leaves every already-pinned row untouched is +// undecidable from roles alone (the store rows really are at those +// indices) and stays a bounded pin-drift residual. // // Consumption after a hit is exactly today's stamped-body path: handle.ts // re-stamps the validated ids onto `parsed` under MSG_IDS_BODY_FIELD before @@ -42,8 +52,20 @@ const ENV = "BILI_IDENTITY_HARVEST"; * tail (dropping a possibly-partial first line) is exact for its purpose. */ export const HARVEST_TAIL_BYTES = 16 * 1024 * 1024; export const MAX_PROJECT_DIRS = 2000; -/** Boundary positions compared when both sides expose roles at them. */ -const MAX_ROLE_SPOT_CHECK = 4; + +/** #5898 review: per-session id-continuity guard. The role check cannot see + * an EVEN transcript/wire shift (user/assistant alternation keeps the role + * pattern intact — e.g. the client appended a row after the request was + * built, or its store was compacted/rewritten), so the registry remembers + * the last stamped tail per session and rejects any alignment whose overlap + * with that tail disagrees on an id — the signature of a moved row. Append- + * only stores pass trivially: shared rows keep their ids. Bounded: the + * oldest session is evicted at the cap; a missing entry merely skips the + * check for that one turn and re-arms on the next successful alignment + * (cost bounded to the pre-check behavior — a role-pattern-consistent even + * shift on a FRESH session degrades to pin drift, never cross-session + * leakage, since ids are never reused by harvested clients). */ +const LAST_ALIGNED_MAX = 512; export interface IdentityHarvestCtx { /** The recognized conversation-identity value for this request (the raw @@ -91,6 +113,7 @@ function enabledSources(): Set | null { } const warned = new Set(); +const lastAligned = new Map(); function warnOnce(key: string, message: string): void { if (warned.has(key)) return; warned.add(key); @@ -107,27 +130,49 @@ export function headerFirst(ctx: IdentityHarvestCtx, name: string): string | und export function resetIdentityHarvestForTest(): void { sources = []; warned.clear(); + lastAligned.clear(); } /** Tail alignment shared by every harvest source: exact count parity on the - * last `expectedLen` rows + a role spot-check over up to MAX_ROLE_SPOT_CHECK - * boundary positions WHERE BOTH SIDES EXPOSE A ROLE (either side lacking one - * at a position skips it — schemas differ per client). Any agreement - * disagreement degrades the whole turn. ids in wire order. */ + * last `expectedLen` rows + a FULL role-sequence check over every position + * where both sides expose a role (was a 4-position spot check; a shift that + * flips the role pattern anywhere in the tail degrades the turn) + the + * per-session id-continuity check (see lastAligned). Either side lacking a + * role at a position skips that position — schemas differ per client. ids + * in wire order. */ export function alignTailIds(ctx: IdentityHarvestCtx, rows: Array<{ id: string; role?: string }>): string[] | null { if (rows.length < ctx.expectedLen) return null; const tail = rows.slice(-ctx.expectedLen); if (ctx.messageRoles && ctx.messageRoles.length === ctx.expectedLen) { - let checked = 0; - for (let i = tail.length - 1; i >= 0 && checked < MAX_ROLE_SPOT_CHECK; i--) { + for (let i = 0; i < tail.length; i++) { const want = String(ctx.messageRoles[i] ?? "").toLowerCase(); const have = tail[i].role?.toLowerCase(); if (!want || !have) continue; if (want !== have) return null; - checked++; } } - return tail.map((r) => r.id); + const ids = tail.map((r) => r.id); + const prev = lastAligned.get(ctx.sessionId); + if (prev !== undefined) { + // Overlap of the two tails on shared STORE-ROW indices: [max(starts), + // min(end)). Append-only stores agree there by construction; a + // rewrite/shift/compaction moves a row's id into a different index + // and the mismatch degrades the turn instead of re-stamping it. + const prevStart = prev.rowsLen - prev.ids.length; + const curStart = rows.length - ids.length; + const overlapStart = Math.max(prevStart, curStart); + const overlapEnd = Math.min(prev.rowsLen, rows.length); + for (let r = overlapStart; r < overlapEnd; r++) { + if (prev.ids[r - prevStart] !== ids[r - curStart]) return null; + } + } + lastAligned.set(ctx.sessionId, { ids, rowsLen: rows.length }); + while (lastAligned.size > LAST_ALIGNED_MAX) { + const oldest = lastAligned.keys().next().value; + if (oldest === undefined) break; + lastAligned.delete(oldest); + } + return ids; } type HarvestResult = { ids: readonly string[]; sourceId: string } | null; diff --git a/src/server/handle.ts b/src/server/handle.ts index 3ab47a420f..223b0d6c33 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -519,7 +519,7 @@ async function handleRequest( credHeaders: earlyHoldCredHeaders(req), protocol, bodyBytes: bodyBuffer.length, - requestModel: googleModel ?? vertexModel ?? requestModelFromBody(bodyBuffer, req), + requestModel: googleModel ?? vertexModel ?? (() => requestModelFromBody(bodyBuffer, req)), res, streamErrorShape: opts.streamErrorShape, log, diff --git a/src/server/relay.ts b/src/server/relay.ts index 8cfb7f526e..30d7dcf175 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -295,7 +295,14 @@ export function tryAnswerEarlyHold(o: { credHeaders: Record; protocol: WireProtocol | null; bodyBytes: number; - requestModel?: string; + /** The request model — or a lazy supplier for it. The supplier form exists + * because the only model source that is not a header read is a full-document + * body scan: callers that pass a supplier pay that O(body) walk ONLY when + * this function actually consults the model (i.e. the body cleared the + * threshold and the wire is known). A sub-threshold or unknown-wire request + * bails out above and never touches the payload (#5898 review: the eager + * scan taxed every proxied POST with a result it could not use). */ + requestModel?: string | (() => string | undefined); res: http.ServerResponse; streamErrorShape?: "protocol" | "completion"; log: (level: string, msg: string) => void; @@ -304,7 +311,8 @@ export function tryAnswerEarlyHold(o: { }): boolean { if (o.method === "GET" || o.method === "HEAD") return false; if (o.protocol === null || o.bodyBytes < EARLY_HOLD_GATE_MIN_BYTES) return false; - const reject = upstreamThrottlePeekReject(o.upstreamUrl, o.credHeaders, o.now ?? Date.now(), o.requestModel); + const requestModel = typeof o.requestModel === "function" ? o.requestModel() : o.requestModel; + const reject = upstreamThrottlePeekReject(o.upstreamUrl, o.credHeaders, o.now ?? Date.now(), requestModel); if (reject === undefined) return false; answerLocalThrottleReject(o.res, reject, o.protocol, o.streamErrorShape, o.log, "pre-ingest"); return true; diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index c426bbc54f..b3005bb5f4 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -820,8 +820,12 @@ export class MysqlBackend implements StorageBackend { [s.sessionId], )) as unknown as StoredBlockRow[][]; for (const row of storedBlockRows[0]) storedBlocks.set(str(row.block_id), row); + // #5898 review (P3, mirror of store.ts): O(1) membership set + // instead of a per-stored-block Array.some over the incoming + // blocks (O(stored×incoming) on the record() hot path). + const incomingBlockIds = new Set(input.blocks.map((b) => b.blockId)); for (const [blockId, stale] of storedBlocks) { - if (input.blocks.some((b) => b.blockId === blockId)) continue; + if (incomingBlockIds.has(blockId)) continue; // referencing row first, then release (#4242 delete-on-zero) await c.query("DELETE FROM blocks WHERE session_id = ? AND block_id = ?", [s.sessionId, blockId]); await this.releaseContent(c, str(stale.summary_hash)); diff --git a/src/storage/store.ts b/src/storage/store.ts index 78c3b7bd82..ba1a3186be 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -1131,8 +1131,13 @@ export class UnifiedStore { .all(s.sessionId) as StoredBlockRow[]) { storedBlocks.set(row.block_id, row); } + // #5898 review (P3): rebuild the O(1) membership set the + // pre-#5744 code had — a per-stored-block Array.some over the + // incoming blocks is O(stored×incoming) string compares on the + // sync record() path that #5744 exists to make cheaper. + const incomingBlockIds = new Set(input.blocks.map((b) => b.blockId)); for (const [blockId, stale] of storedBlocks) { - if (input.blocks.some((b) => b.blockId === blockId)) continue; + if (incomingBlockIds.has(blockId)) continue; // referencing row first, then release (#4242 delete-on-zero) this.db.prepare("DELETE FROM blocks WHERE session_id = ? AND block_id = ?").run(s.sessionId, blockId); this.releaseContent(stale.summary_hash); diff --git a/tests/identity-harvest.test.ts b/tests/identity-harvest.test.ts index c3f6346a15..3a26d7eed9 100644 --- a/tests/identity-harvest.test.ts +++ b/tests/identity-harvest.test.ts @@ -11,7 +11,7 @@ import os from "node:os"; import path from "node:path"; import test from "node:test"; -import { HARVEST_TAIL_BYTES, findProjectJsonlFiles, headerFirst, harvestIdentity, readJsonlTail, registerIdentitySource, resetIdentityHarvestForTest, type IdentityHarvestCtx } from "../src/identity-harvest.js"; +import { alignTailIds, HARVEST_TAIL_BYTES, findProjectJsonlFiles, headerFirst, harvestIdentity, readJsonlTail, registerIdentitySource, resetIdentityHarvestForTest, type IdentityHarvestCtx } from "../src/identity-harvest.js"; import { claudeVisibleRows, registerClaudeIdentitySource } from "../src/identity-claude.js"; import { codebuddyVisibleRows, registerCodebuddyIdentitySource } from "../src/identity-codebuddy.js"; import { registerGooseIdentitySource } from "../src/identity-goose.js"; @@ -348,3 +348,97 @@ test("session recognition: x-conversation-id sits below client-specific + affini assert.equal(src({ "agent-session-id": "20261107_1", "session_id": "gen" })?.name, "agent-session-id"); assert.equal(src({ "x-claude-code-session-id": "cc-1", "agent-session-id": "20261107_1" })?.name, "x-claude-code-session-id"); }); + +/* ---------------- alignTailIds: role-sequence + id-continuity (PR #5898 review) ---------------- */ + +const acCtx = (sessionId: string, expectedLen: number, messageRoles?: string[]): IdentityHarvestCtx => + ({ sessionId, expectedLen, messageRoles }); + +const rows = (n: number, base = "r"): Array<{ id: string; role: string }> => + Array.from({ length: n }, (_, i) => ({ id: `${base}${i + 1}`, role: i % 2 === 0 ? "user" : "assistant" })); + +test("alignTailIds: a store rewrite moving a pinned row into the overlap degrades the turn", () => { + resetIdentityHarvestForTest(); + // turn 1: 4 rows, wire = last 2 ([r3 user, r4 assistant]) → pins land. + const first = alignTailIds(acCtx("s1", 2, ["user", "assistant"]), rows(4)); + assert.deepEqual(first, ["r3", "r4"]); + // turn 2: the store was compacted/rewritten — same row COUNT and same role + // pattern at the tail, but the last two rows carry NEW ids (the old r3/r4 + // were replaced). A pre-fix alignment would have stamped the wire's r3/r4 + // content with the new rows' ids. Now the overlap disagrees → null. + const after = [...rows(2, "w"), { id: "r5x", role: "user" as const }, { id: "r6x", role: "assistant" as const }]; + assert.equal(alignTailIds(acCtx("s1", 2, ["user", "assistant"]), after), null); +}); + +test("alignTailIds: healthy append-only progression passes, with and without overlap", () => { + resetIdentityHarvestForTest(); + const first = alignTailIds(acCtx("s2", 3, ["assistant", "user", "assistant"]), rows(4)); + assert.deepEqual(first, ["r2", "r3", "r4"]); + // +2 rows (the normal turn exchange): overlap of the tails is non-empty + // and consistent → no false degrade. + const grown = alignTailIds(acCtx("s2", 3, ["assistant", "user", "assistant"]), rows(6)); + assert.deepEqual(grown, ["r4", "r5", "r6"]); + // a whole session's worth of new rows: the tails no longer overlap at all. + // Append-only ⇒ nothing moved ⇒ still fine. + const far = alignTailIds(acCtx("s2", 3, ["assistant", "user", "assistant"]), rows(40)); + assert.deepEqual(far, ["r38", "r39", "r40"]); +}); + +test("alignTailIds: a role flip OUTSIDE the old 4-position spot-check window degrades", () => { + resetIdentityHarvestForTest(); + // 10 rows, wire = all of them; the wire claims user at position 2 where the + // store says assistant. The old spot check only compared the last 4 + // positions and passed this; the full-sequence check must not. + const r = rows(10); + const badRoles = ["user", "assistant", "user", "user", "assistant", "user", "assistant", "user", "assistant", "user"]; + assert.equal(alignTailIds(acCtx("s3", 10, badRoles), r), null); + // and the matching sequence passes + const ok = alignTailIds(acCtx("s3b", 10, r.map((x) => x.role)), rows(10)); + assert.deepEqual(ok, rows(10).map((x) => x.id)); +}); + +test("alignTailIds: a pure even late append with no moved row still passes (bounded pin drift, not leakage)", () => { + resetIdentityHarvestForTest(); + // turn 1 pins the last 2 of 4 rows. + assert.deepEqual(alignTailIds(acCtx("s4", 2, ["user", "assistant"]), rows(4)), ["r3", "r4"]); + // turn 2: a FULL extra turn (user+assistant) was persisted after the + // request was built. Roles match at every position (alternation) and no + // store row moved — undecidable from roles alone by design; the residual + // cost is pin drift for this turn, never a cross-session wrong identity. + const stale = alignTailIds(acCtx("s4", 2, ["user", "assistant"]), rows(6)); + assert.deepEqual(stale, ["r5", "r6"]); +}); + +test("alignTailIds: state is per-session; cap eviction fails open for one turn", () => { + resetIdentityHarvestForTest(); + // two sessions interleave without contaminating each other + assert.deepEqual(alignTailIds(acCtx("a", 2, ["user", "assistant"]), rows(4, "A")), ["A3", "A4"]); + assert.deepEqual(alignTailIds(acCtx("b", 2, ["user", "assistant"]), rows(4, "B")), ["B3", "B4"]); + // session "a" store rewritten → still degrades (its state survived "b") + assert.equal( + alignTailIds(acCtx("a", 2, ["user", "assistant"]), [ + { id: "A1", role: "user" }, { id: "A2", role: "assistant" }, + { id: "Ax", role: "user" }, { id: "Ay", role: "assistant" }, + ]), + null, + ); + // fill the 512-entry cap with other sessions → "a"'s state evicted; its + // next rewritten-store alignment fails OPEN (one turn of pin drift at + // worst), then re-arms. + for (let i = 0; i < 600; i++) alignTailIds(acCtx(`fill${i}`, 2, ["user", "assistant"]), rows(4, `F${i}`)); + assert.deepEqual( + alignTailIds(acCtx("a", 2, ["user", "assistant"]), [ + { id: "A1", role: "user" }, { id: "A2", role: "assistant" }, + { id: "Ax", role: "user" }, { id: "Ay", role: "assistant" }, + ]), + ["Ax", "Ay"], + ); + // and re-armed: a further rewrite now degrades again + assert.equal( + alignTailIds(acCtx("a", 2, ["user", "assistant"]), [ + { id: "A1", role: "user" }, { id: "A2", role: "assistant" }, + { id: "Aw", role: "user" }, { id: "Av", role: "assistant" }, + ]), + null, + ); +}); diff --git a/tests/issue5795-early-hold-fastpath.test.ts b/tests/issue5795-early-hold-fastpath.test.ts index dee9135d70..e93eb960a3 100644 --- a/tests/issue5795-early-hold-fastpath.test.ts +++ b/tests/issue5795-early-hold-fastpath.test.ts @@ -461,3 +461,39 @@ test("#5795 e2e: model-scoped hold — other models egress, matching model held" } }); }); + +test("#5898 review: the early-hold requestModel supplier is lazy — sub-threshold and unknown-wire bodies are never scanned", () => { + withKnobs(KNOWN_KNOBS, () => { + _resetUpstreamThrottleForTest(); + noteUpstreamThrottle429(URL_A, H_EMPTY, null, T0); + const logs: string[] = []; + let scans = 0; + const supplier = () => { scans += 1; return "model-x"; }; + const big = EARLY_HOLD_GATE_MIN_BYTES + 1; + + // Sub-threshold body: the function bails out before the model is ever + // needed — the full-document scan must not run (hot-path tax). + const small = makeFakeRes(); + assert.equal(tryAnswerEarlyHold({ ...earlyHoldBase(logs), method: "POST", bodyBytes: big - 2, requestModel: supplier, res: small }), false); + assert.equal(scans, 0, "sub-threshold body never scans"); + + // Unknown wire: bails out before the model is needed — no scan. + const pnull = makeFakeRes(); + assert.equal(tryAnswerEarlyHold({ ...earlyHoldBase(logs), method: "POST", protocol: null, bodyBytes: big, requestModel: supplier, res: pnull }), false); + assert.equal(scans, 0, "unknown wire never scans"); + + // At threshold with a hold active: the model IS needed — the supplier + // runs exactly once and the local 429 lands. + const held = makeFakeRes(); + assert.equal(tryAnswerEarlyHold({ ...earlyHoldBase(logs), method: "POST", bodyBytes: big, requestModel: supplier, now: T0 + 100, res: held }), true); + assert.equal(scans, 1, "consulted exactly once when actually used"); + assert.equal(held.headStatus, 429); + + // The plain-string form still works unchanged. + const strRes = makeFakeRes(); + assert.equal(tryAnswerEarlyHold({ ...earlyHoldBase(logs), method: "POST", bodyBytes: big, requestModel: "model-x", now: T0 + 100, res: strRes }), true); + assert.equal(strRes.headStatus, 429); + + _resetUpstreamThrottleForTest(); + }); +});