From 95f4d874bab1077eb6e8b049a1d00199896a0dfa Mon Sep 17 00:00:00 2001 From: Afloat Date: Mon, 9 Nov 2026 21:48:34 +0800 Subject: [PATCH 1/4] fix: reject corrupt positional affinity snapshot carriers --- src/prefix-affinity.ts | 12 +++--- tests/affinity-snapshot-position.test.ts | 50 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 tests/affinity-snapshot-position.test.ts diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index 52e9845bf4..6c926f761b 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -1213,18 +1213,18 @@ export class PrefixAffinityResolver { // malformed list degrades the entry to exact-hash resolution. let sketches: string[] | undefined; if (hv === 2 && Array.isArray(e.sketches)) { - const valid = (e.sketches as unknown[]).filter( - (s): s is string => typeof s === "string" && /^[0-9a-f]{16}$/.test(s), - ); - if (valid.length >= ALIGN_MIN_DEPTH) sketches = valid.slice(0, SKETCH_MAX_DEPTH); + const valid = e.sketches as unknown[]; + if (valid.every((s): s is string => typeof s === "string" && /^[0-9a-f]{16}$/.test(s)) && + valid.length >= ALIGN_MIN_DEPTH) sketches = valid.slice(0, SKETCH_MAX_DEPTH); } // #2265: user-role flags for the ownership-anchor gate — optional, // malformed lists simply degrade the entry (no user anchor → the // simhash rung skips it, exact-hash resolution still works). let userFlags: boolean[] | undefined; if (sketches !== undefined && Array.isArray(e.userFlags)) { - const validU = (e.userFlags as unknown[]).filter((u): u is boolean => typeof u === "boolean"); - if (validU.length > 0) userFlags = validU.slice(-MAX_STORED_ITEMS); + const validU = e.userFlags as unknown[]; + if (validU.every((u): u is boolean => typeof u === "boolean") && + validU.length > 0) userFlags = validU.slice(-MAX_STORED_ITEMS); } const entry: ChainEntry = { sessionId: e.sessionId, diff --git a/tests/affinity-snapshot-position.test.ts b/tests/affinity-snapshot-position.test.ts new file mode 100644 index 0000000000..5d138cfc01 --- /dev/null +++ b/tests/affinity-snapshot-position.test.ts @@ -0,0 +1,50 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { PrefixAffinityResolver } from "../src/prefix-affinity.ts"; + +function seed() { + const messages = Array.from({ length: 16 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: `message ${i}: deployment analysis includes the latency profile and retry budget of service ${i}`, + })); + const resolver = new PrefixAffinityResolver(); + const affinity = resolver.resolve(messages); + assert.ok(affinity); + resolver.note(affinity.sessionId, affinity.incomingDepth, affinity.tailHash, affinity.itemHashes, false, affinity.sketches, affinity.userFlags); + const snapshot = resolver.exportSnapshot()[0]!; + assert.equal(snapshot.sketches?.length, 16); + assert.equal(snapshot.userFlags?.length, 16); + return { messages, snapshot }; +} + +test("a malformed middle sketch degrades the carrier without shifting positions", () => { + const { messages, snapshot } = seed(); + const sketches: unknown[] = [...snapshot.sketches!]; + sketches[4] = null; + const restored = new PrefixAffinityResolver(); + assert.equal(restored.importSnapshot([{ ...snapshot, sketches }]), 1); + assert.equal(restored.exportSnapshot()[0]!.sketches, undefined); + assert.equal(restored.exportSnapshot()[0]!.userFlags, undefined); + const replay = restored.resolve(messages); + assert.ok(replay); + assert.equal(replay.sessionId, snapshot.sessionId); + assert.equal(replay.via, "prefix"); +}); + +test("a malformed middle role flag drops flags instead of moving later anchors", () => { + const { snapshot } = seed(); + const userFlags: unknown[] = [...snapshot.userFlags!]; + userFlags[3] = "invalid"; + const restored = new PrefixAffinityResolver(); + assert.equal(restored.importSnapshot([{ ...snapshot, userFlags }]), 1); + const actual = restored.exportSnapshot()[0]!; + assert.deepEqual(actual.sketches, snapshot.sketches); + assert.equal(actual.userFlags, undefined); +}); + +test("valid positional carriers round-trip unchanged", () => { + const { snapshot } = seed(); + const restored = new PrefixAffinityResolver(); + assert.equal(restored.importSnapshot([snapshot]), 1); + assert.deepEqual(restored.exportSnapshot(), [snapshot]); +}); From caf14bb702346eae823ede9b46d501952b3cca4d Mon Sep 17 00:00:00 2001 From: Afloat Date: Mon, 9 Nov 2026 21:52:55 +0800 Subject: [PATCH 2/4] test: reject holes in positional snapshot carriers --- src/prefix-affinity.ts | 4 ++-- tests/affinity-snapshot-position.test.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index 6c926f761b..6a085425c4 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -1213,7 +1213,7 @@ export class PrefixAffinityResolver { // malformed list degrades the entry to exact-hash resolution. let sketches: string[] | undefined; if (hv === 2 && Array.isArray(e.sketches)) { - const valid = e.sketches as unknown[]; + const valid: unknown[] = Array.from(e.sketches); if (valid.every((s): s is string => typeof s === "string" && /^[0-9a-f]{16}$/.test(s)) && valid.length >= ALIGN_MIN_DEPTH) sketches = valid.slice(0, SKETCH_MAX_DEPTH); } @@ -1222,7 +1222,7 @@ export class PrefixAffinityResolver { // simhash rung skips it, exact-hash resolution still works). let userFlags: boolean[] | undefined; if (sketches !== undefined && Array.isArray(e.userFlags)) { - const validU = e.userFlags as unknown[]; + const validU: unknown[] = Array.from(e.userFlags); if (validU.every((u): u is boolean => typeof u === "boolean") && validU.length > 0) userFlags = validU.slice(-MAX_STORED_ITEMS); } diff --git a/tests/affinity-snapshot-position.test.ts b/tests/affinity-snapshot-position.test.ts index 5d138cfc01..d64fb710c6 100644 --- a/tests/affinity-snapshot-position.test.ts +++ b/tests/affinity-snapshot-position.test.ts @@ -48,3 +48,18 @@ test("valid positional carriers round-trip unchanged", () => { assert.equal(restored.importSnapshot([snapshot]), 1); assert.deepEqual(restored.exportSnapshot(), [snapshot]); }); + +test("sparse positional carriers are rejected without skipping their holes", () => { + const { snapshot } = seed(); + const sketches = [...snapshot.sketches!]; + delete sketches[4]; + const restored = new PrefixAffinityResolver(); + restored.importSnapshot([{ ...snapshot, sketches }]); + assert.equal(restored.exportSnapshot()[0]!.sketches, undefined); + + const userFlags = [...snapshot.userFlags!]; + delete userFlags[3]; + restored.importSnapshot([{ ...snapshot, userFlags }]); + assert.deepEqual(restored.exportSnapshot()[0]!.sketches, snapshot.sketches); + assert.equal(restored.exportSnapshot()[0]!.userFlags, undefined); +}); From dede80555b9d9d3fdef1831f2a22d1f018c537a6 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 10 Nov 2026 00:10:18 +0800 Subject: [PATCH 3/4] fix: reject corrupt tailFrags positional carriers in importSnapshot (#6037) Same defect class as the sketches/userFilters fix in this PR: the old filter-based validation dropped a malformed middle slot and shifted every later fragment onto the wrong source message, so the append-continuation rung compared stored vs incoming fragments positionally out of sync (worst case: a true continuation misses and mints a fresh session id). Whole-carrier validation now matches the other two carriers; exact-hash resolution is untouched either way. Regression coverage: a corrupted middle tail fragment degrades the whole carrier while exact replay still reattaches via 'prefix'; a valid tailFrags list round-trips byte-identical. Both fail/pass correctly against the pre-fix code. --- src/prefix-affinity.ts | 8 +++-- tests/affinity-snapshot-position.test.ts | 39 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index 6a085425c4..e61a5eef2b 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -1241,9 +1241,13 @@ export class PrefixAffinityResolver { // #2807: optional tail-turn fragments for the append-continuation // rung — hv2 only (a v1 entry's fragments were never noted), // malformed lists degrade the entry to exact-ladder behavior. + // Whole-carrier validation like sketches/userFlags above: a + // filtered middle slot would shift later fragments onto the + // wrong source message. if (hv === 2 && Array.isArray(e.tailFrags)) { - const validF = (e.tailFrags as unknown[]).filter((f): f is string => typeof f === "string" && /^[0-9a-f]{64}$/.test(f)); - if (validF.length > 0) entry.tailFrags = validF.slice(0, MAX_TURN_FRAGS); + const frags: unknown[] = Array.from(e.tailFrags); + if (frags.every((f): f is string => typeof f === "string" && /^[0-9a-f]{64}$/.test(f)) && + frags.length > 0) entry.tailFrags = frags.slice(0, MAX_TURN_FRAGS); } this.trackedChains.delete(entry.sessionId); this.trackedChains.set(entry.sessionId, entry); diff --git a/tests/affinity-snapshot-position.test.ts b/tests/affinity-snapshot-position.test.ts index d64fb710c6..dac3d0695c 100644 --- a/tests/affinity-snapshot-position.test.ts +++ b/tests/affinity-snapshot-position.test.ts @@ -63,3 +63,42 @@ test("sparse positional carriers are rejected without skipping their holes", () assert.deepEqual(restored.exportSnapshot()[0]!.sketches, snapshot.sketches); assert.equal(restored.exportSnapshot()[0]!.userFlags, undefined); }); + +function seedWithTailFrags() { + const messages = [ + ...Array.from({ length: 16 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: `message ${i}: deployment analysis includes the latency profile and retry budget of service ${i}`, + })), + { role: "assistant", content: "follow-up note on the retry budget for service 9" }, + ]; + const resolver = new PrefixAffinityResolver(); + const affinity = resolver.resolve(messages); + assert.ok(affinity); + const fp = resolver.chainFingerprint(messages); + assert.ok(fp && fp.tailFrags && fp.tailFrags.length >= 2); + resolver.note(affinity.sessionId, affinity.incomingDepth, affinity.tailHash, affinity.itemHashes, false, affinity.sketches, affinity.userFlags, undefined, fp.tailFrags); + const snapshot = resolver.exportSnapshot()[0]!; + assert.ok(snapshot.tailFrags && snapshot.tailFrags.length >= 2); + return { messages, snapshot }; +} + +test("a malformed middle tail fragment degrades the carrier without shifting positions", () => { + const { messages, snapshot } = seedWithTailFrags(); + const tailFrags: unknown[] = [...snapshot.tailFrags!]; + tailFrags[1] = null; + const restored = new PrefixAffinityResolver(); + assert.equal(restored.importSnapshot([{ ...snapshot, tailFrags }]), 1); + assert.equal(restored.exportSnapshot()[0]!.tailFrags, undefined); + const replay = restored.resolve(messages); + assert.ok(replay); + assert.equal(replay.sessionId, snapshot.sessionId); + assert.equal(replay.via, "prefix"); +}); + +test("valid tail fragments round-trip unchanged", () => { + const { snapshot } = seedWithTailFrags(); + const restored = new PrefixAffinityResolver(); + assert.equal(restored.importSnapshot([snapshot]), 1); + assert.deepEqual(restored.exportSnapshot(), [snapshot]); +}); From 95c75ffda992942abbfe85cb61b3f0a098b54769 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 10 Nov 2026 00:16:36 +0800 Subject: [PATCH 4/4] refactor: name importSnapshot carrier lists after their content (#6037) The local lists hold the raw persisted slots BEFORE validation, so 'valid'/'validU' misnamed them. Renamed to slots/flags to match the frags naming in the tailFrags block. No behavior change. --- src/prefix-affinity.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index e61a5eef2b..4046625d46 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -1213,18 +1213,18 @@ export class PrefixAffinityResolver { // malformed list degrades the entry to exact-hash resolution. let sketches: string[] | undefined; if (hv === 2 && Array.isArray(e.sketches)) { - const valid: unknown[] = Array.from(e.sketches); - if (valid.every((s): s is string => typeof s === "string" && /^[0-9a-f]{16}$/.test(s)) && - valid.length >= ALIGN_MIN_DEPTH) sketches = valid.slice(0, SKETCH_MAX_DEPTH); + const slots: unknown[] = Array.from(e.sketches); + if (slots.every((s): s is string => typeof s === "string" && /^[0-9a-f]{16}$/.test(s)) && + slots.length >= ALIGN_MIN_DEPTH) sketches = slots.slice(0, SKETCH_MAX_DEPTH); } // #2265: user-role flags for the ownership-anchor gate — optional, // malformed lists simply degrade the entry (no user anchor → the // simhash rung skips it, exact-hash resolution still works). let userFlags: boolean[] | undefined; if (sketches !== undefined && Array.isArray(e.userFlags)) { - const validU: unknown[] = Array.from(e.userFlags); - if (validU.every((u): u is boolean => typeof u === "boolean") && - validU.length > 0) userFlags = validU.slice(-MAX_STORED_ITEMS); + const flags: unknown[] = Array.from(e.userFlags); + if (flags.every((u): u is boolean => typeof u === "boolean") && + flags.length > 0) userFlags = flags.slice(-MAX_STORED_ITEMS); } const entry: ChainEntry = { sessionId: e.sessionId,