Skip to content
11 changes: 10 additions & 1 deletion src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,19 @@ export function noteForwardedBody(session: Session, body: string, msgs?: number
// sharing a 512 KiB prefix apart. Re-noting a byte-identical payload (the
// non-streaming settle path) reuses the prior slot's count/full instead of
// re-deriving them.
// #7149 batch3-1 (delta anchoring): the byte-identical re-note inherits the
// prior digest via a pure byte compare — ZERO hashing on the common path.
// Changed/new bodies keep the full-body digest: `bd` is the ground-truth
// equality proof (#2131) and the ONLY equality proof once the payload
// exceeds BODY_FULL_CAP (nothing is retained), so a delta descriptor cannot
// replace it without breaking settle's be decision. View-ID anchoring is
// not available here — the send chokepoints hold the wire string, not the
// stored canonical views (those live in the storage layer).
const buf = Buffer.from(body, "utf8");
const digest = createHash("sha256").update(buf).digest("hex");
const transient = pendingCapture(session);
const prior = transient.sentBody;
const sameBytes = prior !== undefined && prior.bytes === buf.byteLength && prior.full !== null && prior.full.equals(buf);
const digest = sameBytes ? prior.digest : createHash("sha256").update(buf).digest("hex");
const samePayload = prior !== undefined && prior.digest === digest;
transient.sentBody = {
order: ++captureOrder,
Expand Down
26 changes: 21 additions & 5 deletions src/fold-reconcile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2597,14 +2597,24 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt
export function noteSystemPromptFingerprint(session: Session, system: unknown, opts: ReconcileOptions = {}): void {
if (!session.metadata) return;
const text = system === undefined || system === null ? "" : JSON.stringify(system) ?? "";
const fp = system === undefined || system === null ? "-" : createHash("sha256").update(text).digest("hex").slice(0, 16);
const prev = session.metadata[METADATA_SYSTEM_FP] as { fp: string; size: number } | undefined;
// Change probe (#7149 batch2): the probe (size + first 256 chars) is a
// detection SIGNAL, not a security boundary — a rewrite that preserves
// length AND the first 256 chars stays unobserved, which is acceptable for
// a ledger (#1921), and the stored fp is still the true full-hash value
// every time it is actually computed. Probe miss / first turn / legacy
// stored shape (no head) ⇒ full hash.
const prev = session.metadata[METADATA_SYSTEM_FP] as { fp: string; size: number; head?: string } | undefined;
const fp = system === undefined || system === null
? "-"
: (prev !== undefined && prev.fp !== "-" && prev.size === text.length && prev.head !== undefined && prev.head === text.slice(0, 256)
? prev.fp
: createHash("sha256").update(text).digest("hex").slice(0, 16));
if (prev !== undefined && prev.fp !== fp) {
if (opts.log !== undefined) {
opts.log("info", `${opts.sessionId === undefined ? "" : `[${opts.sessionId}] `}[fold-reconcile] system prompt changed (${prev.size} → ${text.length} chars); fold state unaffected (#1921)`);
}
}
session.metadata[METADATA_SYSTEM_FP] = { fp, size: text.length };
session.metadata[METADATA_SYSTEM_FP] = { fp, size: text.length, head: text.slice(0, 256) };
}

/** #2867: canonical INCOMING system/developer fingerprint — the mode-independent
Expand All @@ -2616,6 +2626,12 @@ export function noteSystemPromptFingerprint(session: Session, system: unknown, o
export function noteCanonicalSystemFingerprint(session: Session, canonical: unknown): void {
if (!session.metadata) return;
const text = canonical === undefined || canonical === null ? "" : JSON.stringify(canonical) ?? "";
const fp = canonical === undefined || canonical === null ? "-" : createHash("sha256").update(text).digest("hex").slice(0, 16);
session.metadata[METADATA_CANONICAL_SYS_FP] = { fp, size: text.length };
// Same change-probe tradeoff as noteSystemPromptFingerprint (#7149 batch2).
const prev = session.metadata[METADATA_CANONICAL_SYS_FP] as { fp: string; size: number; head?: string } | undefined;
const fp = canonical === undefined || canonical === null
? "-"
: (prev !== undefined && prev.fp !== "-" && prev.size === text.length && prev.head !== undefined && prev.head === text.slice(0, 256)
? prev.fp
: createHash("sha256").update(text).digest("hex").slice(0, 16));
session.metadata[METADATA_CANONICAL_SYS_FP] = { fp, size: text.length, head: text.slice(0, 256) };
}
9 changes: 9 additions & 0 deletions src/persona-anchor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// RETIRED — no production importers (identity resolution lives in
// src/session-resolve.ts, the neutral-skeleton successor). Retained ONLY as a
// characterization pin: tests/issue2944-neutral-skeleton.test.ts,
// tests/issue2601-model-drift-absorption.test.ts,
// tests/issue2601-persona-fork-lineage.test.ts and
// tests/dsh-persona-continuity.test.ts import it to freeze the old ladder's
// behavior as a comparison artifact. Removing this file requires dropping all
// four pins deliberately, not incidentally.
//
// #2241/#2250: continuity-aware persona anchor for id-sharing traffic
// (dsh persona lanes, codex root threads / older builds, claude-over-Responses).
//
Expand Down
72 changes: 72 additions & 0 deletions tests/fold-reconcile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
reconcileFoldCoverage,
resolveFoldReconcileMode,
noteSystemPromptFingerprint,
noteCanonicalSystemFingerprint,
METADATA_FOLD_COVERAGE,
resetNormalizedIdentityWork,
turnContexts,
Expand Down Expand Up @@ -291,6 +292,77 @@ describe("reconcileFoldCoverage (#1921)", () => {
assert.equal(logs.length, 1);
assert.match(logs[0], /system prompt changed/);
assert.match(logs[0], /fold state unaffected/);
// #7149 batch2: dropping the prompt to null is an observable projection
// change too (prev "…" → "-"), so it logs — "absence does not" only
// covers the first-ever null, not a withdrawal.
noteSystemPromptFingerprint(session, null, logOpts);
assert.equal(logs.length, 2);
assert.match(logs[1], /system prompt changed/);
});

test("system-fingerprint change probe (#7149 batch2): unchanged reuses, changed recomputes, legacy shape full-hashes", () => {
const logs: string[] = [];
const logOpts = { sessionId: "s1", log: (_l: string, m: string) => logs.push(m) };

// (a) unchanged input → stable fp, probe head stored, no log. NOTE: the
// probe sees the JSON.stringify'd text (a string input gains quotes), so
// head/size compare against the stringified form.
const session = fakeSession([]);
const longPrompt = "P".repeat(300); // > 256 so the head is a true prefix slice
const stringifiedLong = JSON.stringify(longPrompt);
noteSystemPromptFingerprint(session, longPrompt, logOpts);
const first = session.metadata.systemFp as { fp: string; size: number; head: string };
assert.ok(first.fp !== "-");
assert.equal(first.head, stringifiedLong.slice(0, 256));
assert.equal(first.size, stringifiedLong.length);
noteSystemPromptFingerprint(session, longPrompt, logOpts);
const second = session.metadata.systemFp as { fp: string };
assert.equal(second.fp, first.fp, "probe hit keeps the stored fp");
assert.equal(logs.length, 0, "unchanged prompt logs nothing");

// (b) change beyond byte 256 with SAME length+head: documented tradeoff —
// the probe is a detection signal, not a boundary; fp stays the last true hash.
const tailTweaked = longPrompt.slice(0, 256) + longPrompt.slice(257) + "X";
noteSystemPromptFingerprint(session, tailTweaked, logOpts);
assert.equal((session.metadata.systemFp as { fp: string }).fp, first.fp, "tail-only same-size edit stays unobserved (documented probe contract)");
assert.equal(logs.length, 0);

// (c) observable change (size differs) → recompute + log
noteSystemPromptFingerprint(session, "shorter now", logOpts);
const third = session.metadata.systemFp as { fp: string };
assert.notEqual(third.fp, first.fp);
assert.equal(logs.length, 1);
assert.match(logs[0], /system prompt changed/);

// (d) legacy stored shape { fp, size } without head → full-hash path (compat)
const legacy = fakeSession([]);
legacy.metadata.systemFp = { fp: first.fp, size: longPrompt.length };
noteSystemPromptFingerprint(legacy, longPrompt, logOpts);
assert.equal((legacy.metadata.systemFp as { fp: string }).fp, first.fp, "full hash reproduces the stored fp");
assert.equal((legacy.metadata.systemFp as { head?: string }).head !== undefined, true, "head backfilled for the next probe");

// (e) null/undefined → "-" sentinel, probe inert. (c) already logged one
// change into the shared array, so compare against a captured baseline.
const logsBeforeE = logs.length;
noteSystemPromptFingerprint(legacy, null, logOpts);
assert.equal((legacy.metadata.systemFp as { fp: string }).fp, "-");
assert.equal(logs.length, logsBeforeE + 1, "prompt→null withdrawal logs once");
noteSystemPromptFingerprint(legacy, null, logOpts);
assert.equal(logs.length, logsBeforeE + 1, "null→null logs nothing");
});

test("canonical system fingerprint probe (#7149 batch2): silent twin, same contract", () => {
const session = fakeSession([]);
const longPrompt = "C".repeat(400);
noteCanonicalSystemFingerprint(session, longPrompt);
const first = session.metadata.canonicalSysFp as { fp: string; head: string };
assert.ok(first.fp !== "-");
noteCanonicalSystemFingerprint(session, longPrompt);
assert.equal((session.metadata.canonicalSysFp as { fp: string }).fp, first.fp);
noteCanonicalSystemFingerprint(session, "rewritten");
assert.notEqual((session.metadata.canonicalSysFp as { fp: string }).fp, first.fp, "changed canonical recomputes");
noteCanonicalSystemFingerprint(session, undefined);
assert.equal((session.metadata.canonicalSysFp as { fp: string }).fp, "-");
});
});

Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/anthropic.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "201eb6df8c4cf505ffaa628e67f738fe31daf482eae08baeb3b704b68932571a",
"stateDigestAligned": "7b249e7388c7875c8540e7e950f6fda8aed08a57e1412149919e0a70e2eeec82",
"stateDigest": "ba0f75f6ddc882d00d5c456facd79166b62e3f2cacadd60744aa370d6b247105",
"stateDigestAligned": "66991d5cb8864f3ca029914aa9afc20cc8b9687cfd4391ffe200017c68057946",
"sectionDigests": {
"state": "37b26b4f6898afff",
"blockContents": "dc7d13d250c94bb4",
"stats": "7caf981f99159be9",
"metadata": "d5a483de67911378"
"metadata": "ba51746c6749d0cf"
},
"stateKeyDigests": {
"blocks": "3a454b9a16e4f0b9",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "1377fd8b0e9aee644b0252ed66762fce72ce357778734c1e22a4b99d05c76fe9",
"stateDigestAligned": "402c083e9fcab5ba6b6d91fe2d4b4cf65f3b1fcf3fa88e2c040e81fc017d5765",
"stateDigest": "2c0b36206756617ae1e32cf0d221ddd8bf7f3e06994b49d11578bd1a35c90f26",
"stateDigestAligned": "3011800706759958cfeab6520289f1ffe257cf4f7c3275d846c03a991f9ed6b8",
"sectionDigests": {
"state": "90857fbf82001a2b",
"blockContents": "94a58f96d7c5cfc3",
"stats": "7b6162aef3873d31",
"metadata": "57404903cbbf85a1"
"metadata": "7218c1e3430c3d21"
},
"stateKeyDigests": {
"blocks": "d9770feee91dafc6",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/google.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "11bc49add04e1408678a44de2cc4142a623c6fa723d69277fd150c99be33ff11",
"stateDigestAligned": "08d9988e139152731ecefae4c320df288ee9a132ec5203ef8edff604c23681b1",
"stateDigest": "c5eefef6525f180ab54a7ea57c4ddf004a568db0fc2d6d227ede31dc960706db",
"stateDigestAligned": "5217aa02331256a419837cd91105d3e3172f2ff38f0f3914c0e01128e221f012",
"sectionDigests": {
"state": "c5243ba17364d3ac",
"blockContents": "473264d9df8ba338",
"stats": "c90671e484dc5d37",
"metadata": "518c072aca3f9f76"
"metadata": "2980a8959aa0a39c"
},
"stateKeyDigests": {
"blocks": "0cfd0b9c0fc53073",
Expand Down
6 changes: 3 additions & 3 deletions tests/golden/storage-render/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "642951dac35e9b2eb4fb0b700f7d23e98f5477eb9c19e718b9deab62ce8485f5",
"stateDigestAligned": "c36820a7b7e8bda7541714cee83ffd4870e46602a6191956deaab15c0c91d12e",
"stateDigest": "ff092bc66e11c35b15ad98acf6206629115825b62168da2e5542e06297f6d47a",
"stateDigestAligned": "db5f08a7d57c14952c31571e45b4dbc6c2ed35fe8a97c58cda6fa854d411eb25",
"sectionDigests": {
"state": "bee6e1ba0c6fba62",
"blockContents": "b6701268a0bdcd8a",
"stats": "1b56c993f03cd122",
"metadata": "71746aec32a84063"
"metadata": "77714417e484ba89"
},
"stateKeyDigests": {
"blocks": "34b0dc17f8c24595",
Expand Down
27 changes: 27 additions & 0 deletions tests/issue2868-byte-lcp-utf8.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,30 @@ test("seam forensics reports byte-exact lcpBytes for CJK bodies", () => {
assert.equal(ev!.lcpBytes, utf8Lcp(b1, b2), "SeamEvent.lcpBytes must be real UTF-8 bytes");
assert.notEqual(ev!.lcpBytes, codeUnitLcp(b1, b2), "must NOT be the old code-unit value");
});

// #7149 batch3-1: byte-identical re-notes inherit the digest via a pure byte
// compare (zero hashing on the common non-streaming settle path). The
// inherited value MUST equal the full-body sha256 — `bd` stays the
// ground-truth equality proof (#2131), only its derivation is memoized.
test("byte-identical re-note inherits the digest (delta anchoring, #7149 batch3)", () => {
const b = JSON.stringify({ model: "m", messages: [{ role: "user", content: "重复的载荷".repeat(300) }] });
const s = makeSession();
noteForwardedBody(s, b);
settle(s, T0, 100_000, 99_000);
// Non-streaming settle re-notes the identical wire body: no new hash, the
// inherited bd equals what an independent full digest would produce.
noteForwardedBody(s, b);
settle(s, T0 + 1000, 100_000, 99_000);
const line = lastLine(s);
assert.equal(line.bd, createHash("sha256").update(b, "utf8").digest("hex"), "inherited bd equals the independent full digest");
assert.equal(line.be, 1, "identical re-note still settles as body-stable");

// A changed body recomputes a fresh digest and settles as changed.
const b2 = JSON.stringify({ model: "m", messages: [{ role: "user", content: "重复的载荷".repeat(299) }] });
noteForwardedBody(s, b2);
settle(s, T0 + 2000, 100_000, 90_000);
const line2 = lastLine(s);
assert.notEqual(line2.bd, line.bd);
assert.equal(line2.bd, createHash("sha256").update(b2, "utf8").digest("hex"), "changed body gets a fresh full digest");
assert.equal(line2.be, 0);
});
Loading