From e944a8ddac92798dfe18ff4df4b63b49683cd8a5 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 12:42:27 +0800 Subject: [PATCH 01/13] chore: empty PR (no changes) From 36b26d62eb61f2f26fb7458793cafa50b7aad024 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 14:36:02 +0800 Subject: [PATCH 02/13] fix(lane): multi-block wire messages pin-degrade instead of a sticky 400 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dsh/omp send msgIds parallel to the WIRE (one id per message), but a multi-block assistant turn ([text, tool_use]) converts to TWO core rows, so the ids can never map 1:1 after conversion. The lane hard-400'd that shape — and since clients retry the identical shape and only understand 200/409/503, the conversation failed FOREVER once it happened. Now a post-conversion count mismatch drops that delta's client ids (derived ids pin the rows) and the turn applies; clean 1:1 deltas keep pinning. Found in the #4801 branch review (P0). --- src/incremental-lane.ts | 24 ++++++++++++++++----- tests/msgid-lane-flow.test.ts | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 56a97cb5e1..fb2a0a14be 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -457,12 +457,26 @@ function applyTurn( let deltaMsgs = flat.msgs; if (deltaIds !== undefined) { const v = validateMsgIds(deltaIds, flat.msgs.length); - if (!v.ok) return { ok: false, status: 400, error: `msgIds invalid (${v.reason})`, serverSeq }; - const claimed = new Set(entry.msgs.map((m) => m.id)); - for (const id of v.ids) { - if (!resync && claimed.has(id)) return { ok: false, status: 400, error: "msgIds reuse an existing canonical id", serverSeq }; + if (!v.ok && v.reason !== "length-mismatch") { + return { ok: false, status: 400, error: `msgIds invalid (${v.reason})`, serverSeq }; } - deltaMsgs = applyMsgIdPin(flat.msgs, v.ids); + if (v.ok) { + const claimed = new Set(entry.msgs.map((m) => m.id)); + for (const id of v.ids) { + if (!resync && claimed.has(id)) return { ok: false, status: 400, error: "msgIds reuse an existing canonical id", serverSeq }; + } + deltaMsgs = applyMsgIdPin(flat.msgs, v.ids); + } + // !v.ok && reason === "length-mismatch": the envelope gate already + // proved the ids are parallel to the WIRE messages, so a mismatch + // HERE means the wire→core conversion expanded multi-block messages + // (1 wire → N core rows — assistant [text+tool_use], user + // [text+tool_result]) — exactly the "core conversion can legally map + // wire messages non-1:1" case the lane contract (#4491, envelope + // gate in applyIncrementalTurn) says must DEGRADE to derived ids, + // never hard-fail. A 400 here made every dsh/omp turn carrying a + // multi-block message fail forever — the client retries the same + // shape and has no 400 recovery (only 200/409/503 are handled). } const base = resync ? [] : entry.msgs; entry.msgs = [...base, ...deltaMsgs]; diff --git a/tests/msgid-lane-flow.test.ts b/tests/msgid-lane-flow.test.ts index 196bb73d13..897c92cfa7 100644 --- a/tests/msgid-lane-flow.test.ts +++ b/tests/msgid-lane-flow.test.ts @@ -215,3 +215,42 @@ test("msgid lane flow: turns without msgIds stay on content-derived identities ( await h.close(); } }); + +test("msgid lane flow: multi-block wire content degrades to derived ids, never a sticky 400", async () => { + const h = await harness(true); + try { + // #4801 review (P0): dsh/omp wire shapes carry multi-block messages — + // an assistant turn of [text, tool_use] converts to TWO core rows + // while the client's msgIds stay parallel to the WIRE (one id per + // message). A hard 400 on the count mismatch made the turn fail + // FOREVER: the client retries the identical shape and only handles + // 200/409/503. The lane contract is degrade-not-refuse — the ids for + // that delta drop (derived ids pin the rows instead), the turn still + // applies, and a subsequent delta with clean 1:1 ids pins again. + const seed = await h.turn({ sinceSeq: 0, resync: true, msgIds: ["u1", "a1", "u2"], messages: [umsg("alpha"), amsg("beta"), umsg("gamma")] }); + assert.equal(seed.status, 200); + const next = await h.turn({ + sinceSeq: seed.appliedSeq ?? 0, + msgIds: ["u3", "a2"], + messages: [ + umsg("delta question"), + { role: "assistant", content: [{ type: "text", text: "acting" }, { type: "tool_use", id: "tu_1", name: "read", input: { path: "/x" } }] }, + ], + }); + assert.equal(next.status, 200, "count mismatch after wire→core expansion degrades, not a 400"); + assert.ok((next.appliedSeq ?? 0) > (seed.appliedSeq ?? 0), "the delta applied and advanced the tip"); + + // The dropped ids must NOT be stamped into rows (they were ambiguous); + // the derived ids own the turn's rows. + const stamped = h.rows().map((r) => r.msgId); + assert.ok(!stamped.includes("a2"), "the ambiguous client id was dropped, not misattributed"); + assert.ok(!stamped.includes("u3"), "the whole delta's ids drop together (per-message granularity is a lie mid-expansion)"); + + // Recovery: a clean 1:1 delta pins again — the lane is not poisoned. + const after = await h.turn({ sinceSeq: next.appliedSeq ?? 0, msgIds: ["u4"], messages: [umsg("clean follow-up")] }); + assert.equal(after.status, 200); + assert.ok(h.rows().some((r) => r.msgId === "u4"), "subsequent clean deltas pin client ids again"); + } finally { + await h.close(); + } +}); From 533828320b07853fce3cdeb12801ad7a44dc24b1 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 14:36:13 +0800 Subject: [PATCH 03/13] fix(storage): fork clone takes one CAS ref per cloned row, not per hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cloneSessionRows bumped a blob's refcount once per DISTINCT hash while every other path (ingest, delete, sweep) accounts per referencing row. A fork whose parent rows share one blob (one image on several messages, #3762 media mappings) walked out with a single ref for N rows; any later session delete then over-released and drove the refcount negative, and delete-on-zero GC swept bytes sessions still referenced — a fork could eat blobs it never owned. The MySQL twin had the same bug. Found in the #4801 branch review (P1). Tests: fork-clone-media-refcount. --- src/storage/mysql.ts | 18 ++- src/storage/store.ts | 30 +++-- tests/fork-clone-media-refcount.test.ts | 158 ++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 tests/fork-clone-media-refcount.test.ts diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index dd7fc1195f..4fb0ff7273 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -723,8 +723,18 @@ export class MysqlBackend implements StorageBackend { [s.sessionId, r.msgId, r.to, r.seq], )) as unknown as Row[][]; for (const l of leftovers) { + const [lMedia] = (await c.query( + "SELECT media_hash FROM message_media WHERE session_id = ? AND seq = ?", + [s.sessionId, num(l.seq)], + )) as unknown as Row[][]; await c.query("DELETE FROM message_media WHERE session_id = ? AND seq = ?", [s.sessionId, num(l.seq)]); await c.query("DELETE FROM messages WHERE session_id = ? AND seq = ?", [s.sessionId, num(l.seq)]); + // Per-row refcount release, mirroring the SQLite twin: the + // deleted row owned one ref on r.to and each mapping row one + // on its media hash — skip this and the blobs are permanently + // over-counted (zero-ref GC can never reclaim them). + await this.releaseContent(c, r.to); + for (const lm of lMedia) await this.releaseContent(c, String(lm.media_hash)); } await this.putContent(c, r.content, r.to); await c.query("UPDATE messages SET content_hash = ? WHERE session_id = ? AND seq = ?", [r.to, s.sessionId, r.seq]); @@ -996,13 +1006,17 @@ export class MysqlBackend implements StorageBackend { WHERE session_id = ? AND seq <= ?`, [childId, parentId, throughSeq], ); + // Per COPIED ROW, not DISTINCT — same under-count fix as the + // SQLite twin: two mapping rows sharing one media_hash must bump + // twice or the zero-ref sweep deletes bytes a later delete path + // still releases twice. const [hashes] = (await conn.query( - "SELECT DISTINCT content_hash AS h FROM messages WHERE session_id = ?", + "SELECT content_hash AS h FROM messages WHERE session_id = ?", [childId], )) as unknown as Row[][]; for (const row of hashes) await conn.query("UPDATE content SET refcount = refcount + 1 WHERE hash = ?", [row.h]); const [media] = (await conn.query( - "SELECT DISTINCT media_hash AS h FROM message_media WHERE session_id = ?", + "SELECT media_hash AS h FROM message_media WHERE session_id = ?", [childId], )) as unknown as Row[][]; for (const row of media) await conn.query("UPDATE content SET refcount = refcount + 1 WHERE hash = ?", [row.h]); diff --git a/src/storage/store.ts b/src/storage/store.ts index 234958e002..c4f42400cb 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -1173,11 +1173,21 @@ export class UnifiedStore { .prepare("SELECT seq FROM messages WHERE session_id = ? AND msg_id = ? AND content_hash = ? AND seq <> ? AND retired_at IS NOT NULL") .all(s.sessionId, r.msgId, r.to, r.seq) as { seq: number }[]; if (leftovers.length > 0) { + const selMedia = this.db.prepare("SELECT media_hash FROM message_media WHERE session_id = ? AND seq = ?"); const delMedia = this.db.prepare("DELETE FROM message_media WHERE session_id = ? AND seq = ?"); const delRow = this.db.prepare("DELETE FROM messages WHERE session_id = ? AND seq = ?"); for (const l of leftovers) { + const leftoverMedia = selMedia.all(s.sessionId, l.seq) as { media_hash: string }[]; delMedia.run(s.sessionId, l.seq); delRow.run(s.sessionId, l.seq); + // Every deleted row owned one refcount on r.to (bumped at + // its insert) and each of its mapping rows owned one on + // its media hash. Deleting without releasing is a + // permanent over-count — #4800's delete-on-zero GC can + // then never reclaim the blob. Same per-row discipline as + // deleteSession() and the retire path above. + this.releaseContent(r.to); + for (const lm of leftoverMedia) this.releaseContent(lm.media_hash); } } this.putContent(r.content, r.to); @@ -2322,18 +2332,22 @@ export class UnifiedStore { WHERE session_id = ? AND seq <= ?`, ) .run(childId, parentId, throughSeq); - // One refcount bump per distinct CAS body now referenced by the - // child (rows sharing content must bump once each, not per row). + // One refcount bump per COPIED ROW — the same discipline as the + // insert path (putContent per landed row) and every release path + // (deleteSession/gc/retire release per row). DISTINCT here + // under-counted when one session attached the same media bytes to + // two messages (two mapping rows, one hash → +1 instead of +2): + // deleting the child then the parent dropped the count to zero + // while a third session still referenced the bytes, and the + // zero-ref sweep deleted live media. (For message content the + // serialized JSON embeds the message id, so distinct == per-row + // there — but per-row is the invariant, not the optimization.) const hashes = this.db - .prepare( - "SELECT DISTINCT content_hash AS h FROM messages WHERE session_id = ?", - ) + .prepare("SELECT content_hash AS h FROM messages WHERE session_id = ?") .all(childId) as Record[]; for (const row of hashes) this.putContentRef(row.h as string); const media = this.db - .prepare( - "SELECT DISTINCT media_hash AS h FROM message_media WHERE session_id = ?", - ) + .prepare("SELECT media_hash AS h FROM message_media WHERE session_id = ?") .all(childId) as Record[]; for (const row of media) this.putContentRef(row.h as string); this.db diff --git a/tests/fork-clone-media-refcount.test.ts b/tests/fork-clone-media-refcount.test.ts new file mode 100644 index 0000000000..8949d8323d --- /dev/null +++ b/tests/fork-clone-media-refcount.test.ts @@ -0,0 +1,158 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { UnifiedStore } from "../src/storage/store.ts"; +import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; +import type { CoreMessage } from "acp-kernel"; + +process.env.BILI_PERSIST_ZSTD = "0"; +process.env.BILI_STORAGE_UNIFIED = "legacy"; // hermetic tier baseline; tests drive UnifiedStore directly + +/** + * #4801 review (P1 storage): fork cloning must take ONE content ref PER + * CLONED ROW. The pre-fix clone bumped the blob refcount exactly once per + * (session, hash) pair — a fork whose parent rows share one blob (the very + * common case: one image attached to several messages, or #3762 media + * mappings under multiple rows) walked out with a SINGLE ref while acquiring + * N referencing rows. Every later session delete then released N and drove + * the refcount NEGATIVE, and delete-on-zero GC (#4800) swept the blob out + * from under sessions that still referenced it — a fork could eat bytes it + * never owned. + * + * Mirrors the #4730 fork-live-seq fixture shape (seed → fork → delete). + */ + +const sha256Hex = (s: string): string => createHash("sha256").update(s).digest("hex"); + +function imgMsg(id: string, role: "user" | "assistant", bytes: string): CoreMessage { + // Top-level imageBase64 rides the stored JSON — #3762 splitMediaContent + // pulls it into the media CAS (content row + message_media mapping), + // which is exactly the shared-blob shape the fork clone must count. + return { id, role, contentType: "text", text: `body ${id}`, imageBase64: bytes } as unknown as CoreMessage; +} + +function session(id: string, tail: CoreMessage[]): LegacySessionLike { + const byRaw: Record = {}; + const byRef: Record = {}; + tail.forEach((m, i) => { + const ref = `m${String(i + 1).padStart(5, "0")}`; + byRaw[m.id] = ref; + byRef[ref] = m.id; + }); + return { + id, + createdAt: 1_000, + lastSeen: 2_000, + meta: { protocol: "openai" }, + stats: {}, + metadata: {}, + state: { blocks: [], nextBlockId: 1, nextRunId: 1, messageRefs: { byRaw, byRef } }, + blockContents: new Map(), + lastMessages: tail, + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + }; +} + +interface Handle { + dir: string; + store: UnifiedStore; + refcount(hash: string): number; + rawAddRow(sessionId: string, seq: number, msgId: string, hash: string): void; + close(): void; +} + +function open(label: string): Handle { + const dir = mkdtempSync(path.join(tmpdir(), `bili-clone-ref-${label}-`)); + const dbPath = path.join(dir, "index.db"); + const store = UnifiedStore.open(dbPath); + const raw = openSqliteDatabase(dbPath, "node:sqlite"); + const q = raw.db as unknown as { prepare(sql: string): { get(...a: unknown[]): unknown; run(...a: unknown[]): unknown }; close?(): void }; + return { + dir, + store, + refcount(hash) { + const row = q.prepare("SELECT refcount FROM content WHERE hash = ?").get(hash) as { refcount: number } | undefined; + return row?.refcount ?? 0; + }, + rawAddRow(sessionId, seq, msgId, hash) { + q.prepare("INSERT INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash, identity, retired_at) VALUES (?, ?, ?, NULL, 2, 'user', 'text', ?, '', NULL)").run(sessionId, seq, msgId, hash); + q.prepare("UPDATE content SET refcount = refcount + 1 WHERE hash = ?").run(hash); + }, + close() { + try { store.close(); } catch { /* already closed */ } + try { q.close?.(); } catch { /* no-op */ } + rmSync(dir, { recursive: true, force: true }); + }, + }; +} + +test("fork clone: N rows referencing one blob acquire N refs — shared bytes survive every delete", () => { + const h = open("media"); + try { + // Parent: two messages carrying the SAME image bytes → one media + // blob, TWO message_media mappings (content refcount 2 pre-fork). + const IMG = "aG9sZGVyLWltYWdlLWJ5dGVz"; + const parent = ["p_a", "p_b"].map((id, i) => imgMsg(id, i % 2 ? "assistant" : "user", IMG)); + h.store.ingestSession(planIngest(session("sess-clone-parent", parent))); + + // A THIRD session holds one more reference to the same blob: the + // bystander whose bytes the buggy clone could eat. + h.store.ingestSession(planIngest(session("sess-clone-holder", [imgMsg("h_x", "user", IMG)]))); + + const mediaHash = sha256Hex(IMG); + assert.equal(h.refcount(mediaHash), 3, "fixture sanity: 2 parent mappings + 1 holder = 3 refs"); + + // Fork the parent (through its last live seq, the #4730 contract). + h.store.cloneSessionRows("sess-clone-parent", "sess-clone-child", h.store.lastLiveSeq("sess-clone-parent")!); + assert.equal(h.refcount(mediaHash), 5, "clone acquires one ref PER cloned mapping (2), not one per (session, hash)"); + + // Delete the fork and then the parent: every clone/parent ref is + // released, the holder's single ref keeps the blob alive. + h.store.deleteSession("sess-clone-child"); + assert.equal(h.refcount(mediaHash), 3, "deleting the fork releases exactly what the clone took"); + h.store.deleteSession("sess-clone-parent"); + assert.equal(h.refcount(mediaHash), 1, "deleting the parent leaves the holder's ref intact"); + + const holderRows = h.store.getMessages("sess-clone-holder"); + assert.equal(holderRows.length, 1, "holder row survives"); + const hydrated = JSON.parse(holderRows[0].content) as { imageBase64?: unknown }; + assert.equal(hydrated.imageBase64, IMG, "blob still resolvable — getMessages hydrates the original bytes, not swept by GC"); + } finally { + h.close(); + } +}); + +test("fork clone: identical TEXT bytes under two rows count too (content CAS, not just media)", () => { + // Ids are baked into serialized message bytes, so two DIFFERENT messages + // never share the text CAS. The realistic shape for shared text bytes is + // #3762's sibling: splitMediaContent rewrites TWO payloads (top-level + a + // base64 data-URL sidecar) of one message onto hashes — plus the raw + // row-level shape a repair or clone leaves behind. Drive that directly: + // seed one session, raw-insert a second row pointing at the SAME content + // hash, then fork and count. + const h = open("text"); + try { + h.store.ingestSession(planIngest(session("sess-text-parent", [{ id: "t_a", role: "user", contentType: "text", text: "same bytes twice" } as unknown as CoreMessage]))); + const seeded = h.store.getMessages("sess-text-parent"); + assert.equal(seeded.length, 1, "fixture sanity: one seeded row"); + const dupHash = seeded[0].contentHash; + assert.equal(h.refcount(dupHash), 1, "fixture sanity: seeded row refs the blob once"); + h.rawAddRow("sess-text-parent", 1, "t_b", dupHash); + assert.equal(h.refcount(dupHash), 2, "fixture sanity: both parent rows ref the shared text blob"); + + h.store.cloneSessionRows("sess-text-parent", "sess-text-child", h.store.lastLiveSeq("sess-text-parent")!); + assert.equal(h.refcount(dupHash), 4, "clone bumps per cloned row (2), not per (session, hash)"); + + h.store.deleteSession("sess-text-child"); + h.store.deleteSession("sess-text-parent"); + assert.equal(h.refcount(dupHash), 0, "no refs left behind once both owners are gone"); + } finally { + h.close(); + } +}); From 4135b8261e049e5cdb69b73ecf80b2957931904d Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 14:36:13 +0800 Subject: [PATCH 04/13] fix(storage): #4793 blocker cleanup releases the CAS refs it deletes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The UNIQUE-collision repair deleted byte-identical blocker rows and their media mappings WITHOUT releasing their content refcounts — a permanent over-count, so those blobs could never be GC'd (#4800 delete-on-zero only reclaims exact accounting). Same for the MySQL twin. Found in the #4801 branch review (P1). Tests: unified-remap-collision. --- tests/unified-remap-collision.test.ts | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/unified-remap-collision.test.ts b/tests/unified-remap-collision.test.ts index 46f8bef7f1..accd5d36b4 100644 --- a/tests/unified-remap-collision.test.ts +++ b/tests/unified-remap-collision.test.ts @@ -83,6 +83,9 @@ interface Fixture { sessionId: string; openStore(): UnifiedStore; rawInsert(opts: { hash: string; bytes: string; retiredAt: number | null }): void; + rawInsertMedia(opts: { hash: string; bytes: string; retiredAt: number | null; mediaHash: string; mediaBytes: string }): void; + contentRefcount(hash: string): number; + mediaRows(seq: number): number; closeAll(): void; } @@ -111,6 +114,20 @@ function openFixture(sessionId: string): Fixture { raw.prepare("INSERT INTO content (hash, byte_len, algo, bytes, refcount) VALUES (?, ?, 'sha256', ?, 1)").run(opts.hash, Buffer.byteLength(opts.bytes), Buffer.from(opts.bytes)); raw.prepare("INSERT INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash, identity, retired_at) VALUES (?, 1, ?, NULL, 2, 'user', 'text', ?, ?, ?)").run(sessionId, X, opts.hash, "", opts.retiredAt); }, + rawInsertMedia(opts) { + raw.prepare("INSERT INTO content (hash, byte_len, algo, bytes, refcount) VALUES (?, ?, 'sha256', ?, 1)").run(opts.hash, Buffer.byteLength(opts.bytes), Buffer.from(opts.bytes)); + raw.prepare("INSERT INTO content (hash, byte_len, algo, bytes, refcount) VALUES (?, ?, 'sha256', ?, 1)").run(opts.mediaHash, Buffer.byteLength(opts.mediaBytes), Buffer.from(opts.mediaBytes)); + raw.prepare("INSERT INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash, identity, retired_at) VALUES (?, 1, ?, NULL, 2, 'user', 'text', ?, ?, ?)").run(sessionId, X, opts.hash, "", opts.retiredAt); + raw.prepare("INSERT INTO message_media (session_id, seq, media_hash) VALUES (?, 1, ?)").run(sessionId, opts.mediaHash); + }, + contentRefcount(hash) { + const row = raw.prepare("SELECT refcount FROM content WHERE hash = ?").get(hash) as { refcount: number } | undefined; + return row?.refcount ?? 0; + }, + mediaRows(seq) { + const row = raw.prepare("SELECT COUNT(*) AS n FROM message_media WHERE session_id = ? AND seq = ?").get(sessionId, seq) as { n: number }; + return row.n; + }, closeAll() { for (const h of [...handles].reverse()) { try { h.close?.(); } catch { /* no-op */ } } rmSync(dir, { recursive: true, force: true }); @@ -150,7 +167,11 @@ test("#4793: LIVE sibling holds the target bytes → retired before the adopting test("#4793: RETIRED duplicate holds the target bytes → constraint still spans it → dropped outright", () => { const fx = openFixture("u4793-dead"); try { - fx.rawInsert({ hash: H2, bytes: G2, retiredAt: Date.now() }); + // A media blob attached to the doomed sibling row: its mapping and + // its content refcount must go with it (#4800 delete-on-zero GC can + // only reclaim blobs whose accounting is exact). + const MEDIA = sha256Hex("sibling-media-bytes"); + fx.rawInsertMedia({ hash: H2, bytes: G2, retiredAt: Date.now(), mediaHash: MEDIA, mediaBytes: "sibling-media-bytes" }); const store = fx.openStore(); const ok = store.ingestSession(fullInput(fx.sessionId, G2)); @@ -160,6 +181,15 @@ test("#4793: RETIRED duplicate holds the target bytes → constraint still spans assert.equal(rows.length, 1, "the retired byte-identical duplicate is dropped"); assert.equal(rows[0].seq, 0); assert.equal(rows[0].contentHash, H2); + + // Refcount accounting: the adopter row owns EXACTLY ONE ref on H2 + // (its own); the deleted sibling's ref and its media mapping's ref + // were both released. Pre-fix both leaked permanently — the blob + // could never be GC'd (#4800). + assert.equal(fx.contentRefcount(H2), 1, "adopted blob: one ref for one live row, sibling's ref released"); + assert.equal(fx.contentRefcount(sha256Hex(G1)), 0, "superseded G1 blob fully released"); + assert.equal(fx.contentRefcount(MEDIA), 0, "sibling media mapping's blob released to zero and swept"); + assert.equal(fx.mediaRows(1), 0, "the sibling's media mapping row is gone"); } finally { fx.closeAll(); } From 8d10abfc69cf242ea5725618411c5f1eb710c9de Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 14:36:13 +0800 Subject: [PATCH 05/13] fix(cache): SDK-HMAC-SHA256 credentials fingerprint the Access key, not the per-request Signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #1884 resign lane re-signs every request (Signature is an HMAC over the per-request date + body), so credentialFingerprint returned a UNIQUE identity per request: the #4204 429 governor's holds never matched (the APIG lane never throttled), the #3376 keepalive snapshot deleted itself after every forward, and #2131 cache attribution fragmented into one bucket per request. The stable credential on that line is the Access key id — fingerprint only that. Found in the #4801 branch review (P1). --- src/cache-ledger.ts | 16 +++++++- .../cache-ledger-sdk-hmac-fingerprint.test.ts | 40 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/cache-ledger-sdk-hmac-fingerprint.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 19a8450bb2..3f0f8b9afc 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -653,7 +653,21 @@ export function credentialFingerprint(headers: Record | undefine // Strip the auth-scheme envelope so "Bearer X" (authorization) and // a bare "X" (x-api-key) hash to the SAME credential identity. const v = value.trim().replace(/^Bearer\s+/i, "").trim(); - if (v !== "") { cred = v; break; } + if (v !== "") { + // #1884 resign lane: SDK-HMAC-SHA256 authorization lines are + // re-signed EVERY request (Signature is an HMAC over the + // per-request date + body hash), so hashing the raw line + // yields a per-request-unique identity — downstream that + // meant the 429 governor's holds never matched (#4204 never + // throttled on the APIG lane it was built for), the #3376 + // keepalive snapshot deleted itself after every forward, and + // #2131 cache-sample attribution fragmented into one bucket + // per request. The STABLE credential on that line is the + // Access key id — fingerprint only that. + const m = /^SDK-HMAC-SHA256\s+Access=([^\s,]+)/i.exec(v); + cred = m !== null ? `sdk-hmac:${m[1]!}` : v; + break; + } } } if (cred === undefined) return undefined; diff --git a/tests/cache-ledger-sdk-hmac-fingerprint.test.ts b/tests/cache-ledger-sdk-hmac-fingerprint.test.ts new file mode 100644 index 0000000000..3eb39aa39f --- /dev/null +++ b/tests/cache-ledger-sdk-hmac-fingerprint.test.ts @@ -0,0 +1,40 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { credentialFingerprint } from "../src/cache-ledger.ts"; + +/** + * #1884 resign lane / #4204 / #3376 / #2131: SDK-HMAC-SHA256 authorization + * headers are RE-SIGNED on every request — the Signature component is an + * HMAC over the per-request date and body, so two requests from the same + * upstream account carry different header bytes. credentialFingerprint + * hashed the raw line, giving every request a UNIQUE identity: the #4204 + * governor's 429 holds never matched (the resign lane never throttled), the + * #3376 keepalive snapshot deleted itself after every forward, and #2131 + * cache-sample attribution fragmented into one bucket per request. The + * stable credential on that line is the Access key id — the fingerprint must + * capture only that. + * + * Header shape as produced by src/apig-resign.ts:292: + * SDK-HMAC-SHA256 Access=AKxxxx,SignedHeaders=a;b,Signature=hex + */ + +const line = (ak: string, sig: string, date: string): string => + `SDK-HMAC-SHA256 Access=${ak},SignedHeaders=content-type;host;x-sdk-date,Signature=${sig} x-sdk-date=${date}`; + +test("credentialFingerprint: SDK-HMAC re-signed lines fold onto the Access key id", () => { + const a = credentialFingerprint({ authorization: line("AKLA_4444", "deadbeef".repeat(8), "20261023T120000Z") }); + const b = credentialFingerprint({ authorization: line("AKLA_4444", "cafebabe".repeat(8), "20261023T130000Z") }); + assert.equal(a, b, "same Access key, different per-request Signature/date → SAME fingerprint"); +}); + +test("credentialFingerprint: different Access keys stay distinct (and distinct from bearer)", () => { + const a = credentialFingerprint({ authorization: line("AKLA_4444", "aa", "d1") }); + const b = credentialFingerprint({ authorization: line("AKLA_OTHER", "aa", "d1") }); + assert.notEqual(a, b, "a different Access key is a different credential"); + assert.notEqual(a, credentialFingerprint({ authorization: "Bearer AKLA_4444" }), "resign lane never aliases onto the bearer envelope"); +}); + +test("credentialFingerprint: bearer path unchanged (regression)", () => { + assert.equal(credentialFingerprint({ authorization: "Bearer sk-live-one" }), credentialFingerprint({ "x-api-key": "sk-live-one" }), "bearer/x-api-key unification still holds"); + assert.notEqual(credentialFingerprint({ authorization: "Bearer sk-live-one" }), credentialFingerprint({ authorization: "Bearer sk-live-two" })); +}); From c90389b9556fefe13f19d7247f511192adbee353 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 14:36:13 +0800 Subject: [PATCH 06/13] test(gate): baseline-demote check accepts the 'usage-baseline=none' spelling server.ts prints usage-baseline=none when no floor is set; the gate's demote assertion matched only the numeric spelling and every no-baseline run failed line B. Found in the #4801 branch review. --- scripts/release-gate-local.mjs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/release-gate-local.mjs b/scripts/release-gate-local.mjs index 8bcdf93a4b..252c8b2377 100644 --- a/scripts/release-gate-local.mjs +++ b/scripts/release-gate-local.mjs @@ -472,8 +472,11 @@ async function main() { record("B", "same lineage across switch + proxy restart", (!!pfaPre && pfa3 === pfaPre) || (forked && adopted), forked ? `${pfaPre} -> ${pfa3} (forked + adopted)` : `${pfaPre} -> ${pfa3}`); // If a usage-baseline measured on the OLD upstream gets cited while // routing to the NEW one, it must be demoted to untrusted (#2283/#2867). - // (A forked lineage may have no baseline at all — that's fine.) - const noBaseline = !/usage-baseline/.test(s3); + // (A forked lineage may have no baseline at all — that's fine, and a + // fork legitimately prints "usage-baseline=none"; the old + // !/usage-baseline/ pattern matched THAT line and red-flagged a healthy + // fork. Only a cited NUMERIC baseline needs the demotion check.) + const noBaseline = !/usage-baseline=\d/.test(s3); record("B", "cross-upstream baseline demoted when cited", noBaseline || /demoting to untrusted/.test(s3), noBaseline ? "no baseline cited this run" : "demoting to untrusted"); record("B", "both markers recalled after switch", has(t3.final, CODEWORDS[6]) && has(t3.final, CODEWORDS[39]), `final="${t3.final.trim().slice(0, 90)}"`); record("B", `request routed to ${G.relayModel}`, s3.includes(G.relayUrl) || s3.includes(G.relayModel), ""); From dfadc21b339bfa45c6ec03b2f13e723ce6bf0507 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:22:50 +0800 Subject: [PATCH 07/13] fix(ci): sync lockfile recorded versions with acp-kernel 0.0.165 (#4599) master window commits 293ae6542/de5e03659 bumped kernel/package.json to 0.0.165 without regenerating the lockfiles; lock-sync re-resolution then rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and fails. Pure recorded-version sync via npm install --package-lock-only; no dependency or semantic change. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": { From e59e34a73a805400bb70c6fad18a96f1c58ab20e Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:24:53 +0800 Subject: [PATCH 08/13] fix(storage): resolve block seq bounds over LIVE rows only (#4801 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit seqSpanById joined retired generations into a covered id's span, so an in-place edit (old generation tombstoned by the complete-log sweep, new one past max(seq)) widened stored block bounds across generations. Bounds describe the CURRENT replay — getMessages filters retired rows — so the point query now takes live rows only; a covered id with no live rows yields the -1 sentinel and the upsert keeps stored bounds (#4608). Second facet, same symptom: the sqlite re-save branch bound the PLAN-LOCAL b.startSeq/b.endSeq (positions in this plan's log) instead of the transcript-resolved bounds the fresh-insert branch uses; after generation turnover those no longer coincide and every re-save clobbered the durable interval with plan-relative numbers. Both branches now bind the same source. The mysql mirror already bound the resolved values. Regression pin: tests/block-bounds-live-only.test.ts (fails pre-fix). --- src/storage/mysql.ts | 7 +- src/storage/store.ts | 41 +++++++--- tests/block-bounds-live-only.test.ts | 115 +++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 tests/block-bounds-live-only.test.ts diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 4fb0ff7273..5eca6d93e8 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -540,8 +540,13 @@ export class MysqlBackend implements StorageBackend { const wantedIds = Array.from(wanted); for (let i = 0; i < wantedIds.length; i += SEQ_SPAN_QUERY_BATCH) { const ids = wantedIds.slice(i, i + SEQ_SPAN_QUERY_BATCH); + // LIVE rows only (mirror of store.ts): block bounds describe + // the CURRENT replay — getMessages filters retired rows, so + // a tombstoned generation must never widen the stored span. + // No live rows for a covered id → -1 sentinel → the upsert's + // IF(VALUES(start_seq)>=0,...) preserves stored bounds (#4608). const spanRows = (await c.query( - `SELECT msg_id, seq FROM messages WHERE session_id = ? AND msg_id IN (${ids.map(() => "?").join(", ")})`, + `SELECT msg_id, seq FROM messages WHERE session_id = ? AND msg_id IN (${ids.map(() => "?").join(", ")}) AND retired_at IS NULL`, [s.sessionId, ...ids], )) as unknown as Row[][]; for (const r of spanRows[0]) { diff --git a/src/storage/store.ts b/src/storage/store.ts index c4f42400cb..cae5726e24 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -886,11 +886,19 @@ export class UnifiedStore { for (const id of [...(cov.effectiveMessageIds ?? []), ...(cov.directMessageIds ?? [])]) wanted.add(id); } catch { /* malformed payload: no covered ids to resolve */ } } + // LIVE rows only (#4801 review): block bounds describe what is + // CURRENTLY covered in the unified replay — getMessages filters + // retired rows, so a bound spanning a retired generation would + // claim coverage over rows the replay never serves (a pinned-id + // edit/rewind leaves the old generation tombstoned while its id + // is re-covered at a new seq). An id whose rows are all retired + // yields no span → boundsForCovered demotes it honestly; if that + // empties the whole claim the -1 sentinel preserves stored bounds (#4608). const wantedIds = Array.from(wanted); for (let i = 0; i < wantedIds.length; i += SEQ_SPAN_QUERY_BATCH) { const ids = wantedIds.slice(i, i + SEQ_SPAN_QUERY_BATCH); const rows = this.db - .prepare(`SELECT msg_id, seq FROM messages WHERE session_id = ? AND msg_id IN (${ids.map(() => "?").join(", ")})`) + .prepare(`SELECT msg_id, seq FROM messages WHERE session_id = ? AND msg_id IN (${ids.map(() => "?").join(", ")}) AND retired_at IS NULL`) .all(s.sessionId, ...ids) as Array<{ msg_id: string; seq: number }>; for (const r of rows) { const cur = seqSpanById.get(r.msg_id); @@ -940,27 +948,34 @@ export class UnifiedStore { // identical-re-ingest undo (same hash) is a pure -1 // against the +1 bump and cannot reach zero while the row // itself holds the reference. + // Re-saves bind the TRANSCRIPT-resolved bounds (same source + // as the fresh-insert branch below), never the plan-local + // b.startSeq/endSeq: those are positions in THIS plan's log, + // and once a covered id's old generation is tombstoned (an + // in-place edit lands the new one past max(seq)) they no + // longer coincide with absolute seqs — writing them would + // clobber the durable interval with plan-relative numbers. this.db .prepare( `INSERT INTO blocks (block_id, session_id, ord, start_seq, end_seq, start_ref, end_ref, tier, summary_hash, one_hash, supersedes_block_id, active, created_at, covered_hash, payload_json) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ON CONFLICT(session_id, block_id) DO UPDATE SET - ord = excluded.ord, - start_seq = CASE WHEN excluded.start_seq >= 0 THEN excluded.start_seq ELSE blocks.start_seq END, - end_seq = CASE WHEN excluded.end_seq >= 0 THEN excluded.end_seq ELSE blocks.end_seq END, - start_ref = excluded.start_ref, end_ref = excluded.end_ref, tier = excluded.tier, - summary_hash = excluded.summary_hash, one_hash = excluded.one_hash, - supersedes_block_id = excluded.supersedes_block_id, active = excluded.active, - created_at = excluded.created_at, covered_hash = excluded.covered_hash, - payload_json = excluded.payload_json`, + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(session_id, block_id) DO UPDATE SET + ord = excluded.ord, + start_seq = CASE WHEN excluded.start_seq >= 0 THEN excluded.start_seq ELSE blocks.start_seq END, + end_seq = CASE WHEN excluded.end_seq >= 0 THEN excluded.end_seq ELSE blocks.end_seq END, + start_ref = excluded.start_ref, end_ref = excluded.end_ref, tier = excluded.tier, + summary_hash = excluded.summary_hash, one_hash = excluded.one_hash, + supersedes_block_id = excluded.supersedes_block_id, active = excluded.active, + created_at = excluded.created_at, covered_hash = excluded.covered_hash, + payload_json = excluded.payload_json`, ) .run( b.blockId, s.sessionId, b.ord, - b.startSeq, - b.endSeq, + bStartSeq, + bEndSeq, b.startRef, b.endRef, b.tier, diff --git a/tests/block-bounds-live-only.test.ts b/tests/block-bounds-live-only.test.ts new file mode 100644 index 0000000000..8537119998 --- /dev/null +++ b/tests/block-bounds-live-only.test.ts @@ -0,0 +1,115 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { UnifiedStore } from "../src/storage/store.ts"; +import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; +import type { CoreMessage } from "acp-kernel"; + +process.env.BILI_PERSIST_ZSTD = "0"; +process.env.BILI_STORAGE_UNIFIED = "legacy"; // hermetic tier baseline; tests drive UnifiedStore directly + +/** + * #4801 review (P2 storage): a block's start_seq/end_seq describe what the + * unified replay CURRENTLY serves — getMessages filters retired rows, so a + * bound spanning a tombstoned generation claims coverage over rows the + * replay never returns. Realistic trigger: a message is edited in place + * (same client id, new bytes → new identity); a complete-log save sweeps the + * old generation to a tombstone and lands the new one past max(seq), so the + * covered id spans two generations. Pre-fix resolution took min/max over ALL + * rows including retired, widening the bound onto the dead row. Post-fix an + * all-retired id yields no span and boundsForCovered demotes it honestly (if + * that empties the whole claim the -1 sentinel preserves stored bounds, #4608). + */ + +function msg(id: string): CoreMessage { + return { id, role: "user", contentType: "text", text: `body ${id}` } as unknown as CoreMessage; +} + +function session(id: string, tail: CoreMessage[]): LegacySessionLike { + const byRaw: Record = {}; + const byRef: Record = {}; + tail.forEach((m, i) => { + const ref = `m${String(i + 1).padStart(5, "0")}`; + byRaw[m.id] = ref; + byRef[ref] = m.id; + }); + const blk = { + blockId: "blk1", + runId: 1, + createdAt: 1_000, + tier: 1, + active: true, + summary: "summary body", + kind: "fold", + reason: "test", + effectiveMessageIds: tail.map((m) => m.id), + directMessageIds: [] as string[], + }; + return { + id, + createdAt: 1_000, + lastSeen: 2_000, + meta: { protocol: "openai" }, + stats: {}, + metadata: {}, + state: { blocks: [blk as never], nextBlockId: 2, nextRunId: 2, messageRefs: { byRaw, byRef } }, + blockContents: new Map(), + lastMessages: tail, + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + }; +} + +interface Handle { + dir: string; + store: UnifiedStore; + bounds(): [number, number]; + close(): void; +} + +function open(label: string): Handle { + const dir = mkdtempSync(path.join(tmpdir(), `bili-block-bounds-${label}-`)); + const dbPath = path.join(dir, "index.db"); + const store = UnifiedStore.open(dbPath); + const raw = openSqliteDatabase(dbPath, "node:sqlite"); + const q = raw.db as unknown as { prepare(sql: string): { get(...a: unknown[]): unknown; run(...a: unknown[]): unknown }; close?(): void }; + return { + dir, + store, + bounds() { + const row = q.prepare("SELECT start_seq, end_seq FROM blocks WHERE session_id = ? AND block_id = ?").get("sess-bounds", "blk1") as { start_seq: number; end_seq: number } | undefined; + assert.ok(row, "block row present"); + return [row.start_seq, row.end_seq]; + }, + close() { + try { store.close(); } catch { /* already closed */ } + try { q.close?.(); } catch { /* no-op */ } + rmSync(dir, { recursive: true, force: true }); + }, + }; +} + +test("block seq bounds follow LIVE rows only: a retired generation never widens the span", () => { + const h = open("retired"); + try { + const S = session("sess-bounds", ["A", "B", "C"].map(msg)); + h.store.ingestSession(planIngest(S)); + assert.deepEqual(h.bounds(), [0, 2], "fixture sanity: full live coverage resolves to [0,2]"); + + // Pinned-id edit shape: A is rewritten in place (same id, new bytes → + // new identity). A complete-log save tombstones the old generation + // (its identity is absent from the incoming log) and lands the new one + // past max(seq) — covered id A now spans two generations. + const edited = ["A", "B", "C"].map((id) => (id === "A" ? { ...msg(id), text: "body A v2" } : msg(id))); + h.store.ingestSession(planIngest(session("sess-bounds", edited)), undefined, { completeLog: true }); + + assert.equal(h.bounds()[0], 1, "start bound follows the FIRST LIVE covered row (B@1) — pre-fix the retired A@0 leaked in and widened it to 0"); + assert.equal(h.bounds()[1], 3, "end bound follows the newest live generation (A'@3)"); + } finally { + h.close(); + } +}); From 253010be330c8166427a6c5a86d15eb4cd0166a4 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:24:53 +0800 Subject: [PATCH 09/13] fix(storage): fork clone fails loudly when a content row is missing (#4801 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit putContentRef (sqlite) and the two cloneSessionRows refcount bump loops (mysql) were silent no-ops when the UPDATE matched nothing: the clone walked out with rows referencing a blob nobody counted, and every later delete released a ref nobody took — negative refcount lets delete-on-zero GC sweep bytes other sessions still reference. The mysql DDL declares no foreign keys, so dangling refs are reachable there; the sqlite DDL FK fires earlier at INSERT..SELECT, and the guard keeps both backends equivalent-loud for FK-less/corrupted-file shapes. Contract pin extends tests/fork-clone-media-refcount.test.ts: the clone refuses and leaves no partial child rows. --- src/storage/mysql.ts | 10 +++++-- src/storage/store.ts | 8 ++++- tests/fork-clone-media-refcount.test.ts | 39 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 5eca6d93e8..78d7da86cc 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -1019,12 +1019,18 @@ export class MysqlBackend implements StorageBackend { "SELECT content_hash AS h FROM messages WHERE session_id = ?", [childId], )) as unknown as Row[][]; - for (const row of hashes) await conn.query("UPDATE content SET refcount = refcount + 1 WHERE hash = ?", [row.h]); + for (const row of hashes) { + const [res] = (await conn.query("UPDATE content SET refcount = refcount + 1 WHERE hash = ?", [row.h])) as unknown as [{ affectedRows: number }, unknown]; + if (res.affectedRows === 0) throw new Error(`cloneSessionRows: content row missing for hash ${String(row.h).slice(0, 16)}… — refusing to fork with an uncounted ref`); + } const [media] = (await conn.query( "SELECT media_hash AS h FROM message_media WHERE session_id = ?", [childId], )) as unknown as Row[][]; - for (const row of media) await conn.query("UPDATE content SET refcount = refcount + 1 WHERE hash = ?", [row.h]); + for (const row of media) { + const [res] = (await conn.query("UPDATE content SET refcount = refcount + 1 WHERE hash = ?", [row.h])) as unknown as [{ affectedRows: number }, unknown]; + if (res.affectedRows === 0) throw new Error(`cloneSessionRows: media content row missing for hash ${String(row.h).slice(0, 16)}… — refusing to fork with an uncounted ref`); + } await conn.query( `INSERT INTO sessions (session_id, created_at, updated_at, parent_session_id, fork_point_seq) VALUES (?, ?, ?, ?, ?) diff --git a/src/storage/store.ts b/src/storage/store.ts index cae5726e24..f902d0c34f 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2384,11 +2384,17 @@ export class UnifiedStore { } private putContentRef(hash: string): void { - this.db + const res = this.db .prepare( "UPDATE content SET refcount = refcount + 1 WHERE hash = ?", ) .run(hash); + if (res.changes === 0) { + // A dangling reference would otherwise be an uncounted clone row: + // every later delete releases a ref nobody took (negative count → + // delete-on-zero GC sweeps bytes other sessions still reference). + throw new Error(`putContentRef: content row missing for hash ${hash.slice(0, 16)}… — refusing to fork with an uncounted ref`); + } } /** #D5-5 render windowing: identity-only rows (seq + msg_id) — no diff --git a/tests/fork-clone-media-refcount.test.ts b/tests/fork-clone-media-refcount.test.ts index 8949d8323d..5f6702cc61 100644 --- a/tests/fork-clone-media-refcount.test.ts +++ b/tests/fork-clone-media-refcount.test.ts @@ -64,6 +64,7 @@ interface Handle { store: UnifiedStore; refcount(hash: string): number; rawAddRow(sessionId: string, seq: number, msgId: string, hash: string): void; + rawExec(sql: string, ...args: unknown[]): void; close(): void; } @@ -84,6 +85,9 @@ function open(label: string): Handle { q.prepare("INSERT INTO messages (session_id, seq, msg_id, ref, ref_num, role, kind, content_hash, identity, retired_at) VALUES (?, ?, ?, NULL, 2, 'user', 'text', ?, '', NULL)").run(sessionId, seq, msgId, hash); q.prepare("UPDATE content SET refcount = refcount + 1 WHERE hash = ?").run(hash); }, + rawExec(sql, ...args) { + q.prepare(sql).run(...args); + }, close() { try { store.close(); } catch { /* already closed */ } try { q.close?.(); } catch { /* no-op */ } @@ -156,3 +160,38 @@ test("fork clone: identical TEXT bytes under two rows count too (content CAS, no h.close(); } }); + +test("fork clone: a cloned row whose content row is gone fails LOUDLY, not a silent under-count", () => { + // putContentRef is an UPDATE ... WHERE hash = ? — pre-fix, a missing + // content row was a silent no-op: the clone walked out with rows + // referencing a blob it never counted, and every later delete released a + // ref nobody took (negative refcount → delete-on-zero GC swept bytes + // other sessions still referenced). Accounting bugs (this file's fix + // family) or a tampered DB leave dangling refs; the clone must refuse so + // the fork FAILS instead of corrupting the ledger. + const h = open("missing"); + try { + h.store.ingestSession(planIngest(session("sess-missing-parent", [{ id: "x_a", role: "user", contentType: "text", text: "orphaned blob" } as unknown as CoreMessage]))); + const seeded = h.store.getMessages("sess-missing-parent"); + assert.equal(seeded.length, 1, "fixture sanity: one seeded row"); + const hash = seeded[0].contentHash; + assert.equal(h.refcount(hash), 1, "fixture sanity: seeded row refs the blob once"); + // The FK would refuse the delete — a corrupted/tampered DB bypassed it. + h.rawExec("PRAGMA foreign_keys = OFF"); + h.rawExec("DELETE FROM content WHERE hash = ?", hash); + assert.equal(h.refcount(hash), 0, "fixture sanity: content row removed — the dangling-ref shape"); + + // On sqlite the DDL foreign key fires first (messages.content_hash + // REFERENCES content(hash)); the mysql DDL declares NO foreign keys, + // where the putContentRef guard below is the only loud failure. + // Contract pinned either way: the clone refuses, nothing partial lands. + assert.throws( + () => h.store.cloneSessionRows("sess-missing-parent", "sess-missing-child", h.store.lastLiveSeq("sess-missing-parent")!), + /FOREIGN KEY|content/i, + "clone must throw on the missing content row instead of silently skipping the ref bump", + ); + assert.equal(h.store.getMessages("sess-missing-child").length, 0, "rolled-back clone left no child rows behind"); + } finally { + h.close(); + } +}); From 1b8a0ab126db69cf7cd4f29a24c5cf1b9c895a99 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:24:53 +0800 Subject: [PATCH 10/13] fix(model-catalog): log the 304 replay to unconditional GETs once per credential (#4801 review) The wrapper never sends If-None-Match, so a 304 answers a request the upstream treated as revalidation anyway. The replay itself stays (availability-first: codex clients expect a complete 200 catalog), but savedAt resets on every replay, which would pin a stale catalog forever with zero trace if the gateway loops. One warn per credential key makes the loop visible. --- src/server/codex-catalog-cache.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/server/codex-catalog-cache.ts b/src/server/codex-catalog-cache.ts index 1b69b362a0..f343fb2f95 100644 --- a/src/server/codex-catalog-cache.ts +++ b/src/server/codex-catalog-cache.ts @@ -90,6 +90,7 @@ export function createCodexCatalogFetch( ) { const memory = new Map(); const invalidated = new Set(); + const replayNoted = new Set(); const clock = options.now ?? Date.now; const maxAge = options.maxAgeMs ?? 6 * 60 * 60 * 1000; const fresh = (entry: unknown): entry is Entry => @@ -213,6 +214,15 @@ export function createCodexCatalogFetch( if ([500, 502, 503, 504].includes(status)) return fresh(entry) ? cached(`temporary HTTP ${status}`, result) : result; if (status === 304) { if (!entry) return result; + // A 304 answers a CONDITIONAL request; this wrapper never + // sends If-None-Match, so a persistent one means the gateway + // revalidates unconditional GETs — and savedAt keeps resetting + // below, which would pin this cached catalog forever without + // any trace. Log once per credential so the loop is visible. + if (!replayNoted.has(key)) { + replayNoted.add(key); + log("warn", `[model-catalog] upstream answered 304 to an unconditional GET; replaying cached catalog as a full 200; age=${Math.floor((clock() - entry.savedAt) / 1000)}s`); + } await remember({ ...entry, savedAt: clock() }); discard(result); return replay(entry, false); From c504eb3f2201ee56d62819538964825e479191e4 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:40:43 +0800 Subject: [PATCH 11/13] fix(tests): declare get() on the raw sqlite handle type in unified-remap-collision (#4801) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fixture's prepare() cast only declared run(), while contentRefcount/ mediaRows call .get() — a tsc error the image-lane typecheck gate caught on this head (the previous head never ran the job during the queue stall). Type-only; no runtime change. --- tests/unified-remap-collision.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unified-remap-collision.test.ts b/tests/unified-remap-collision.test.ts index accd5d36b4..702c44e8a3 100644 --- a/tests/unified-remap-collision.test.ts +++ b/tests/unified-remap-collision.test.ts @@ -100,7 +100,7 @@ function openFixture(sessionId: string): Fixture { const handles: { close?: () => void }[] = []; const openedRaw = openSqliteDatabase(dbPath, "node:sqlite"); - const raw = openedRaw.db as unknown as { prepare(sql: string): { run(...args: unknown[]): unknown }; close?(): void }; + const raw = openedRaw.db as unknown as { prepare(sql: string): { run(...args: unknown[]): unknown; get(...args: unknown[]): unknown }; close?(): void }; handles.push(raw); return { dbPath, From 36f74ea95bb4447bd5d3ebe776a0d206913e7afb Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 26 Oct 2026 15:47:28 +0800 Subject: [PATCH 12/13] fix(gate): force chunked corpus reads at t1 + accept fresh-start fork adoption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two gate-side causes behind the 15:22 rerun reds (neither is a product bug): 1. dsh's read tool elides the middle of results over ~8KB (head+tail kept). The single 441-line/90KB corpus read at t1 never delivered S07 (title at ~line 71), so both S07 recall checks measured a hole that predates the fold — and the elision also kept the t2 payload at ~13.6k, under the 16.4k text target, so preflight rightly skipped folding (#4288) and 'fold fired mid-session' went red on a turn that legitimately fit. t1 now enumerates 13 explicit 35-line read chunks. 2. The lineage assertion only matched the old adoption wording. A healthy fork with nothing fully-present takes the fresh-start path ('no fully-present blocks to adopt … starting fresh; lineage linked', #629/#3925) — that is same-lineage by design and now counts. --- scripts/release-gate-local.mjs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripts/release-gate-local.mjs b/scripts/release-gate-local.mjs index 252c8b2377..0282fa0561 100644 --- a/scripts/release-gate-local.mjs +++ b/scripts/release-gate-local.mjs @@ -406,8 +406,13 @@ async function main() { if (!G.skip.includes("a")) { log(`\n-- Lane A: fold + post-fold continuity (${G.sglangModel}; window shrunk ${G.bigWindow} → ${G.smallWindow} mid-session)`); // T1 at the real window: read + answer, no fold expected (all fresh). + // dsh's read elides the MIDDLE of any result over ~8KB (head+tail kept): + // a single 90KB/441-line corpus read loses lines ~41–400 — S07 (line ~71) + // was never delivered and the t2 payload stayed under the fold threshold + // (#4288 then rightly skips folding). Force chunked reads so the whole + // corpus actually enters history (35 lines ≈ 7KB < the elision budget). const m1 = mark(); - const t1 = await turn("Read corpus.md in your cwd fully, then reply with ONLY the TITLE line of section S40.", "t1"); + const t1 = await turn(`Read corpus.md in your cwd fully. The read tool elides the middle of any large result — the file is 441 lines / ~90KB, so read it in EXACTLY these 13 chunks, one read call each, in order: offsets 1, 36, 71, 106, 141, 176, 211, 246, 281, 316, 351, 386, 421 with limit 35 each. After all 13 reads are done, reply with ONLY the TITLE line of section S40.`, "t1"); ctx.sessionId = ctx.sessionId || t1.sessionId; record("A", "turn 1 (read @ real window) completes", t1.ok, t1.ok ? "" : t1.why.slice(0, 160)); const s1 = slice(m1); @@ -463,7 +468,11 @@ async function main() { const s3 = slice(m3); const pfa3 = /\[sess=(pfa-[0-9a-f]+)\]/.exec(s3)?.[1]; const forked = pfa3 && pfa3 !== pfaPre && s3.includes(`lineage=forked of ${pfaPre}`); - const adopted = /\[fork-adoption\] fork of .*: parent has \d+ adoptable block/.test(s3); + // Either the fork adopts the parent's blocks, or it takes the designed + // fresh-start path: "no fully-present blocks to adopt … starting fresh; + // lineage linked …" (#629/#3925) — the lineage link alone preserves the + // folded context via the startup sweep. + const adopted = /\[fork-adoption\] fork of [^:]+: (?:parent has \d+ adoptable block|no fully-present blocks to adopt .*starting fresh; lineage linked)/.test(s3); record("B", "switch turn completes", t3.ok, t3.ok ? "" : t3.why.slice(0, 160)); // Same lineage across the switch: either the session continues on the // same pfa, or dsh's resume-replay forks it and the fork machinery From 0d7a6e063c2e63b87d390a05e89f1e80fa3fa81e Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:53:44 +0800 Subject: [PATCH 13/13] chore: retrigger ci:full after kernel-change declaration (#4801)