diff --git a/scripts/release-gate-local.mjs b/scripts/release-gate-local.mjs index 8bcdf93a4b..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 @@ -472,8 +481,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), ""); 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/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/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); diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index dd7fc1195f..78d7da86cc 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]) { @@ -723,8 +728,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,16 +1011,26 @@ 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]); + 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 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]); + 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 234958e002..f902d0c34f 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, @@ -1173,11 +1188,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 +2347,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 @@ -2355,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/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(); + } +}); 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" })); +}); diff --git a/tests/fork-clone-media-refcount.test.ts b/tests/fork-clone-media-refcount.test.ts new file mode 100644 index 0000000000..5f6702cc61 --- /dev/null +++ b/tests/fork-clone-media-refcount.test.ts @@ -0,0 +1,197 @@ +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; + rawExec(sql: string, ...args: unknown[]): 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); + }, + rawExec(sql, ...args) { + q.prepare(sql).run(...args); + }, + 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(); + } +}); + +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(); + } +}); 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(); + } +}); diff --git a/tests/unified-remap-collision.test.ts b/tests/unified-remap-collision.test.ts index 46f8bef7f1..702c44e8a3 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; } @@ -97,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, @@ -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(); }