From 2ee74c194b6a132e8089c79bfe2d3c4c09fb8d1d Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 00:52:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?test:=20#4316=20layer=203=20red=20stakes=20?= =?UTF-8?q?=E2=80=94=20block=20seq=20bounds=20filled=20from=20transcript?= =?UTF-8?q?=20coverage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pins the storage-side contract for "编号直连正史 seq": a block whose covered messages exist as append-only rows carries their real min/max seq; unresolvable coverage (imported pre-fold) keeps the 0 placeholder. Red against the current hard-wired 0 at the single write site (src/storage/ingest.ts). --- tests/issue4316-seq-bounds.test.ts | 105 +++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/issue4316-seq-bounds.test.ts diff --git a/tests/issue4316-seq-bounds.test.ts b/tests/issue4316-seq-bounds.test.ts new file mode 100644 index 0000000000..e391b35d20 --- /dev/null +++ b/tests/issue4316-seq-bounds.test.ts @@ -0,0 +1,105 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; +import type { CoreMessage, CompressionBlock } from "acp-kernel"; + +// #4316 layer 3 (owner ruling 2026-10-22): block coverage becomes a canonical +// [start_seq, end_seq] interval so a client byte-rewrite can't churn held +// m-numbers. RED today — start_seq/end_seq are hard-wired to 0 at the single +// write site (src/storage/ingest.ts); GREEN once the store-layer fill resolves +// covered ids -> seq from the append-only transcript. + +function msg(id: string, role: "user" | "assistant"): CoreMessage { + return { id, role, contentType: "text", text: `body ${id}` }; +} + +function block(id: string, covered: string[]): CompressionBlock { + return { + blockId: id, + runId: `run-${id}`, + tier: 1, + summary: `summary ${id}`, + directMessageIds: [], + effectiveMessageIds: covered, + directBlockIds: [], + compressedTokens: 10, + createdAt: 1_000, + survivedCount: 0, + generation: "young", + active: true, + }; +} + +function session(id: string, tail: CoreMessage[], blocks: CompressionBlock[]): 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: blocks.length + 1, nextRunId: 1, messageRefs: { byRaw, byRef } }, + blockContents: new Map(blocks.map((b) => [b.blockId, { one: null, full: { text: b.summary, count: 1 } }])), + lastMessages: tail, + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + }; +} + +function seqOf(store: UnifiedStore, sessionId: string, msgId: string): number | undefined { + return store.getMessages(sessionId).find((m) => m.msgId === msgId)?.seq; +} + +test("#4316: a block whose covered messages are present carries their real seq bounds (RED: placeholder 0)", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-4316-bounds-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + const sid = "sess-4316"; + const tail = [msg("h_a", "user"), msg("h_b", "assistant"), msg("h_c", "user"), msg("h_d", "assistant"), msg("h_e", "user")]; + // One fold over the middle three — all still resolvable in the transcript. + store.ingestSession(planIngest(session(sid, tail, [block("b1", ["h_b", "h_c", "h_d"])]))); + + const expStart = seqOf(store, sid, "h_b"); + const expEnd = seqOf(store, sid, "h_d"); + assert.notEqual(expStart, undefined, "fixture sanity: covered message h_b must be a transcript row"); + assert.notEqual(expEnd, undefined, "fixture sanity: covered message h_d must be a transcript row"); + + const b = store.getBlocks(sid).find((x) => x.blockId === "b1")!; + assert.ok(b.startSeq > 0, `start_seq must be filled from the transcript, got placeholder ${b.startSeq}`); + assert.equal(b.startSeq, expStart, "start_seq = seq of first covered message"); + assert.equal(b.endSeq, expEnd, "end_seq = seq of last covered message"); + assert.ok(b.endSeq >= b.startSeq, "interval must be ordered"); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handle */ } + } +}); + +test("#4316: a block whose covered ids are absent from the transcript keeps placeholder 0 (no crash, honest gap)", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-4316-gap-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + const sid = "sess-4316-gap"; + // Visible tail holds only h_x/h_y; the block covers h_old, which was + // folded away / imported pre-fold and thus has no seq row to join. + store.ingestSession(planIngest(session(sid, [msg("h_x", "user"), msg("h_y", "assistant")], [block("b1", ["h_old"])]))); + + assert.equal(seqOf(store, sid, "h_old"), undefined, "fixture sanity: h_old must NOT be a transcript row"); + const b = store.getBlocks(sid).find((x) => x.blockId === "b1")!; + assert.equal(b.startSeq, 0, "unresolvable coverage keeps the 0 placeholder, never a fabricated bound"); + assert.equal(b.endSeq, 0, "unresolvable coverage keeps the 0 placeholder, never a fabricated bound"); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handle */ } + } +}); From 05dc6831964234e02165e0906451efbc56755405 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 00:52:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20#4316=20layer=203=20=E2=80=94=20fill?= =?UTF-8?q?=20blocks.start=5Fseq/end=5Fseq=20from=20append-only=20transcri?= =?UTF-8?q?pt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolve each block's covered ids to canonical [start_seq, end_seq] bounds at ingest time (store.ts sqlite + mysql.ts), after final absolute seqs are known (delta placeholders renumber in-transaction, so the pure planner cannot compute them). Unresolvable coverage keeps the 0 placeholder — honest gap, never a fabricated bound. Zero compression-behavior change: nothing reads these columns yet; only export surfaces them (diagnostic 0 -> real). --- src/storage/mysql.ts | 38 ++++++++++++++++++++++++++++++++++++-- src/storage/store.ts | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 79de335a6c..f8a1242ec7 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -452,7 +452,41 @@ export class MysqlBackend implements StorageBackend { if (stale.one_hash !== null && stale.one_hash !== undefined) await this.releaseContent(c, str(stale.one_hash)); await c.query("DELETE FROM blocks WHERE session_id = ? AND block_id = ?", [s.sessionId, stale.block_id]); } + // #4316 layer 3 (mirror of store.ts): resolve coverage to canonical + // seq bounds from the append-only transcript; unresolvable keeps 0. + const seqSpanById = new Map(); + if (input.blocks.length > 0) { + const spanRows = (await c.query( + "SELECT msg_id, seq FROM messages WHERE session_id = ?", + [s.sessionId], + )) as unknown as Row[][]; + for (const r of spanRows[0]) { + const msgId = str(r.msg_id); + const seq = Number(r.seq); + const cur = seqSpanById.get(msgId); + if (!cur) seqSpanById.set(msgId, [seq, seq]); + else { + if (seq < cur[0]) cur[0] = seq; + if (seq > cur[1]) cur[1] = seq; + } + } + } + const boundsForCovered = (payloadJson: string): [number, number] => { + let lo = Number.POSITIVE_INFINITY; + let hi = -1; + try { + const cov = JSON.parse(payloadJson) as { effectiveMessageIds?: string[]; directMessageIds?: string[] }; + for (const id of [...(cov.effectiveMessageIds ?? []), ...(cov.directMessageIds ?? [])]) { + const sp = seqSpanById.get(id); + if (!sp) continue; + if (sp[0] < lo) lo = sp[0]; + if (sp[1] > hi) hi = sp[1]; + } + } catch { /* malformed payload: keep the 0 placeholder */ } + return hi < 0 ? [0, 0] : [lo, hi]; + }; for (const b of input.blocks) { + const [bStartSeq, bEndSeq] = boundsForCovered(b.payloadJson); const summaryHash = await this.putContent(c, b.summary); const oneHash = b.one === null ? null : await this.putContent(c, b.one); const [existingRows] = (await c.query( @@ -484,8 +518,8 @@ export class MysqlBackend implements StorageBackend { b.blockId, s.sessionId, b.ord, - b.startSeq, - b.endSeq, + bStartSeq, + bEndSeq, b.startRef, b.endRef, b.tier, diff --git a/src/storage/store.ts b/src/storage/store.ts index 140e042a46..e8c04d4564 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -624,7 +624,41 @@ export class UnifiedStore { if (stale.one_hash !== null) this.releaseContent(stale.one_hash); this.db.prepare("DELETE FROM blocks WHERE session_id = ? AND block_id = ?").run(s.sessionId, stale.block_id); } + // #4316 layer 3: resolve each block's coverage to canonical seq + // bounds from the append-only transcript. Final absolute seqs exist + // only here (delta placeholders are renumbered in-transaction), so + // the pure planner cannot know them. Coverage whose ids have no + // transcript row (imported pre-fold) keeps the 0 placeholder. + const seqSpanById = new Map(); + if (input.blocks.length > 0) { + const rows = this.db + .prepare("SELECT msg_id, seq FROM messages WHERE session_id = ?") + .all(s.sessionId) as Array<{ msg_id: string; seq: number }>; + for (const r of rows) { + const cur = seqSpanById.get(r.msg_id); + if (!cur) seqSpanById.set(r.msg_id, [r.seq, r.seq]); + else { + if (r.seq < cur[0]) cur[0] = r.seq; + if (r.seq > cur[1]) cur[1] = r.seq; + } + } + } + const boundsForCovered = (payloadJson: string): [number, number] => { + let lo = Number.POSITIVE_INFINITY; + let hi = -1; + try { + const cov = JSON.parse(payloadJson) as { effectiveMessageIds?: string[]; directMessageIds?: string[] }; + for (const id of [...(cov.effectiveMessageIds ?? []), ...(cov.directMessageIds ?? [])]) { + const sp = seqSpanById.get(id); + if (!sp) continue; + if (sp[0] < lo) lo = sp[0]; + if (sp[1] > hi) hi = sp[1]; + } + } catch { /* malformed payload: keep the 0 placeholder */ } + return hi < 0 ? [0, 0] : [lo, hi]; + }; for (const b of input.blocks) { + const [bStartSeq, bEndSeq] = boundsForCovered(b.payloadJson); const summaryHash = this.putContent(b.summary); const oneHash = b.one === null ? null : this.putContent(b.one); // Re-ingest of the same block id replaces the row (dry-run @@ -660,8 +694,8 @@ export class UnifiedStore { b.blockId, s.sessionId, b.ord, - b.startSeq, - b.endSeq, + bStartSeq, + bEndSeq, b.startRef, b.endRef, b.tier,