diff --git a/CHANGELOG.md b/CHANGELOG.md index d84c5708f7..a62127be22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Added +### D5-5 — 渲染源窗口化 + 判官 tailStart 视图域修复 + +- **渲染分窗(storage-review D5-5)**:`buildView` 不再全表加载正史行(数万行 × 每行媒体水合 = 每请求读放大)。新两层查询:`messageIdRows`(seq+msg_id 列,无字节,全行,驱动块锡定+接缝链)+ `uncoveredRows`(仅覆盖区间外的行取字节+媒体;covered 字节永不读——covered 行腐败不再导致渲染降级)。块锡定从字节匹配改 `msg_id` 列匹配零语义变化;两查询间隙新行的竞态→一次全量回退兼音。`unifiedTailViewRows` 保留为回退路径。 +- **判官 tailStart 视图域(#4241 回归修复)**:`snapshotRenderTailStart` 原记客户端域下标(clientJoin),但判官用它索引 `judgeBaseline`(渲染视图)——视图前缀压缩后 clientJoin 可指向视图末尾之外→判官空转(total=0 空转 clean,T1 合车后把 #4241 牙齿测试打红)。改记视图域(视图行数 − 尾巴数)。牙齿测试同步改注尾巴行(引擎尾随 nudge 占末位,最后一条尾巴在 exitCores.length − 2):渲染前缀豁免是设计内(#4241:跨轮前缀稳定归 seamLcp/shadow),尾巴行丢失必须开火。 + ### #4316 层 3 PR3/3 — 存量编号一次性对齐正史 + fork 克隆保号 - **kernel `restampRefsToLedger`**:把既有引用空间一次性重排到正史账本(ref = store 行 seq + 1)。计数器路径按到达顺序发号但受保护消息不占号,而行 seq 照发 → 两者天然漂移;编辑与 churn 拉大缺口。重排后引用空间与存储真值一致,churn 不再改写编号。诚实边界:无映射 id 留给 assignRefs 的章路径;BLOCKED 仍不占号;死 ref 保留;被夺号的无行引用重安居新号(id 永不复用契约不破);多代(同 id 多行编辑)id 由 `seqByRaw` 排除不误绑。`CompressionState.refLedgerAligned` 标记一次性(`state-hydration` 字段单一事实源自动持久化)。 diff --git a/src/persist.ts b/src/persist.ts index 9e8279405f..787fa62b96 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1823,6 +1823,29 @@ export class SessionStore { } } + /** #D5-5 render windowing: identity-only rows (seq + msg_id). Null when + * the unified store is unavailable — the render lane degrades, never + * guesses. */ + unifiedMessageIdRows(sessionId: string): { seq: number; msgId: string }[] | null { + if (!this.shadow || this.shadow.stats.disabled) return null; + try { + return this.shadow.messageIdRows(sessionId); + } catch { + return null; + } + } + + /** #D5-5 render windowing: content rows outside the covered intervals. + * Null when the unified store is unavailable. */ + unifiedUncoveredRows(sessionId: string, intervals: readonly [number, number][]): import("./storage/store.js").MessageRow[] | null { + if (!this.shadow || this.shadow.stats.disabled) return null; + try { + return this.shadow.uncoveredRows(sessionId, intervals); + } catch { + return null; + } + } + /** #2960 R2.1: summary rows for list surfaces (CLI export, web exposure * tick). Full mode only — in shadow the JSON tree is the truth base and * rows may lag files never yet recorded, so list surfaces must walk. diff --git a/src/server/snapshot-render.ts b/src/server/snapshot-render.ts index 0d000bbe79..aca513dfc4 100644 --- a/src/server/snapshot-render.ts +++ b/src/server/snapshot-render.ts @@ -86,7 +86,11 @@ export function identitySigForTest(m: BiliMessage): string { interface ViewRow { seq: number; - msg: BiliMessage; + msgId: string; + /** Present only for UNCOVERED rows — covered rows' bytes never ride the + * outbound (suppressed behind anchors), so the windowed loader (#D5-5) + * never reads them. */ + msg?: BiliMessage; } interface SnapshotRenderDeps { @@ -152,17 +156,20 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { if (activeBlocks.length === 0) return { ok: false, reason: "no-active-blocks" }; const store = getStore(); if (!store) return { ok: false, reason: "store-unavailable" }; + // #D5-5 windowed load — two tiers: + // (a) identity rows (seq + msg_id, no bytes) for EVERY live row — + // positions and ids drive block anchoring and the byId seam chain; + // (b) content rows (bytes + media) ONLY for seqs OUTSIDE the covered + // intervals — covered rows are suppressed behind anchors, so their + // bytes never ride the outbound and are never read. A covered row + // with corrupt bytes therefore cannot degrade the render. let rows: ViewRow[] = []; try { - const rawRows = store.unifiedTailViewRows(session.id); + const light = store.unifiedMessageIdRows(session.id); const seqs = store.unifiedTailSeqs(session.id); - if (!rawRows || rawRows.length === 0 || !seqs || seqs.length === 0) return { ok: false, reason: "transcript-empty" }; + if (!light || light.length === 0 || !seqs || seqs.length === 0) return { ok: false, reason: "transcript-empty" }; const bySeq = new Map(); - for (const row of rawRows) { - const msg = parseRow(row.content, row.seq); - if (!msg) return { ok: false, reason: "row-unparsable" }; // untrustworthy transcript: degrade, never guess - bySeq.set(row.seq, { seq: row.seq, msg }); - } + for (const row of light) bySeq.set(row.seq, { seq: row.seq, msgId: row.msgId }); for (const seq of seqs) { const v = bySeq.get(seq); if (!v) return { ok: false, reason: "view-pointer-missing-row" }; // view pointer to a missing row: degrade @@ -184,10 +191,9 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { const coveredSeqUnion = new Set(); for (const block of activeBlocks) { const blockSeqs = new Set(); - for (const id of block.effectiveMessageIds) { - for (let i = 0; i < rows.length; i++) { - if (rows[i].msg.id === id) blockSeqs.add(rows[i].seq); - } + const wanted = new Set(block.effectiveMessageIds); + for (let i = 0; i < rows.length; i++) { + if (wanted.has(rows[i].msgId)) blockSeqs.add(rows[i].seq); } // #3812: no fold-time id resolves against the view — the block cannot // be positioned (and its covered region cannot be bounded). Anchoring @@ -199,6 +205,59 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { blockSeeds.push({ blockId: block.blockId, seqs: blockSeqs }); } + // #D5-5: merge the covered seqs into disjoint intervals and load bytes + // only for the uncovered window. Row-unparsable now guards UNCOVERED + // bytes only — covered corruption cannot degrade the render. + try { + const intervals: [number, number][] = []; + { + const sorted = [...coveredSeqUnion].sort((a, b) => a - b); + for (const seq of sorted) { + const last = intervals[intervals.length - 1]; + if (last && seq <= last[1] + 1) last[1] = Math.max(last[1], seq); + else intervals.push([seq, seq]); + } + } + const contentRows = store.unifiedUncoveredRows(session.id, intervals); + if (contentRows !== null) { + const bySeq = new Map(); + for (const row of contentRows) { + const msg = parseRow(row.content, row.seq); + if (!msg) return { ok: false, reason: "row-unparsable" }; // uncovered bytes are untrustworthy: degrade, never guess + bySeq.set(row.seq, { seq: row.seq, msgId: row.msgId, msg }); + } + let coveredSeen = 0; + for (const r of rows) { + const v = bySeq.get(r.seq); + if (v) r.msg = v.msg; + else if (coveredSeqUnion.has(r.seq)) coveredSeen++; + } + // Every row must be accounted for: uncovered-with-bytes or + // covered. A gap (a row neither loaded nor covered — e.g. a + // concurrent ingest raced the two queries) falls back to one full + // load rather than rendering a hole. + if (coveredSeen !== [...coveredSeqUnion].filter((seq) => rows.some((r) => r.seq === seq)).length || rows.some((r) => !r.msg && !coveredSeqUnion.has(r.seq))) { + const full = store.unifiedTailViewRows(session.id); + if (full !== null) { + const fullBySeq = new Map(); + for (const row of full) { + const msg = parseRow(row.content, row.seq); + if (msg) fullBySeq.set(row.seq, msg); + } + for (const r of rows) { + if (!r.msg) { + const m = fullBySeq.get(r.seq); + if (!m && !coveredSeqUnion.has(r.seq)) return { ok: false, reason: "row-unparsable" }; + if (m) r.msg = m; + } + } + } + } + } + } catch { + return { ok: false, reason: "transcript-error" }; + } + // Backward chain: from the client's end, match view rows (id fast path, // then identity signature). The chain must be contiguous; the first // unmatched client message bounds the client tail. @@ -206,7 +265,7 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { let clientJoin = clientMsgs.length; // first client index of the tail { const byId = new Map(); // msgId -> view index (last occurrence) - for (let i = 0; i < rows.length; i++) byId.set(rows[i].msg.id, i); + for (let i = 0; i < rows.length; i++) byId.set(rows[i].msgId, i); const bySig = new Map(); // sig -> descending view indices for (let i = rows.length - 1; i >= 0; i--) { // #4052: covered rows take no signature binds. They never render @@ -222,7 +281,9 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { // an exact id is unambiguous (kernel contract: ids never reused) // and steady-state seams need it. if (coveredSeqUnion.has(rows[i].seq)) continue; - const sig = identitySig(rows[i].msg); + const m = rows[i].msg; + if (!m) continue; + const sig = identitySig(m); const list = bySig.get(sig); if (list) list.push(i); else bySig.set(sig, [i]); @@ -319,12 +380,22 @@ function buildView(session: Session, clientMsgs: BiliMessage[]): ViewResult { for (let i = 0; i < d; i++) { while (ai < anchors.length && anchors[ai].at === i) merged.push(anchors[ai++].msg); if (coveredSeqUnion.has(rows[i].seq)) continue; - merged.push(rows[i].msg); + const m = rows[i].msg; + if (!m) return { ok: false, reason: "row-unparsable" }; // uncovered row without bytes: never render a hole + merged.push(m); } while (ai < anchors.length) merged.push(anchors[ai++].msg); for (let k = clientJoin; k < clientMsgs.length; k++) merged.push(clientMsgs[k]); - return { ok: true, view: merged, tailStart: clientJoin }; + // #4241 tailStart is recorded in VIEW domain: the judge indexes its + // baseline (judgeBaseline === this rendered view) with it, so it must + // count the view rows that precede the first tail row. The client-domain + // join (clientJoin) is NOT a valid index into the view — the view prefix + // suppressed covered rows, so clientJoin can point past the view's end and + // the judge would validate nothing (vacuous clean — the regression that + // blinded the #4241 teeth on the T1-consolidation train). + const tailCount = clientMsgs.length - clientJoin; + return { ok: true, view: merged, tailStart: merged.length - tailCount }; } export function maybeSnapshotView(session: Session, clientMsgs: BiliMessage[], deps: SnapshotRenderDeps): BiliMessage[] { diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index ad85a3c447..d5c49d815d 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -737,6 +737,20 @@ export class UnifiedShadow { * coverage matcher needs (msgId for fast path, ref for the * upgrade-tolerant interval path, contentHash for the exact-content * dedup path). */ + /** #D5-5: identity-only rows (no content bytes) for the render lane. */ + messageIdRows(sessionId: string): { seq: number; msgId: string }[] { + if (this.sync === null) return []; + this.assertLive(); + return this.sync.messageIdRows(sessionId); + } + + /** #D5-5: content rows outside the covered intervals only. */ + uncoveredRows(sessionId: string, intervals: readonly [number, number][]): (MessageRow & { contentHash: string })[] { + if (this.sync === null) return []; + this.assertLive(); + return this.sync.uncoveredRows(sessionId, intervals); + } + tailViewRows(sessionId: string): MessageRow[] { if (this.sync === null) return []; this.assertLive(); diff --git a/src/storage/store.ts b/src/storage/store.ts index 299a9b7cd7..33f4119370 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2219,6 +2219,43 @@ export class UnifiedStore { .run(hash); } + /** #D5-5 render windowing: identity-only rows (seq + msg_id) — no + * content bytes, no media hydration. The render lane needs every row's + * POSITION and id (block anchoring, seam chain byId), but covered rows' + * BYTES never ride the outbound — loading them per request is pure read + * amplification on big folded sessions. */ + messageIdRows(sessionId: string): { seq: number; msgId: string }[] { + const rows = this.db + .prepare("SELECT seq, msg_id FROM messages WHERE session_id = ? AND retired_at IS NULL ORDER BY seq") + .all(sessionId) as Record[]; + return rows.map((r) => ({ seq: r.seq as number, msgId: r.msg_id as string })); + } + + /** #D5-5 render windowing: full rows (content + media) ONLY for seqs + * outside the covered intervals. Inclusive [start, end] pairs; overlaps + * are harmless (each pair contributes an outside-of-pair conjunct). + * Empty interval list = every live row. */ + uncoveredRows(sessionId: string, intervals: readonly [number, number][]): (MessageRow & { contentHash: string })[] { + let sql = "SELECT seq, msg_id, ref, ref_num, role, kind, content_hash FROM messages WHERE session_id = ? AND retired_at IS NULL"; + const params: unknown[] = [sessionId]; + for (const [a, b] of intervals) { + sql += " AND (seq < ? OR seq > ?)"; + params.push(a, b); + } + sql += " ORDER BY seq"; + const rows = this.db.prepare(sql).all(...params) as Record[]; + return rows.map((r) => ({ + seq: r.seq as number, + msgId: r.msg_id as string, + ref: (r.ref as string | null) ?? null, + refNum: (r.ref_num as number | null) ?? null, + role: r.role as string, + kind: r.kind as string, + contentHash: r.content_hash as string, + content: this.hydrateMedia(this.getContent(r.content_hash as string) ?? ""), + })); + } + getMessages(sessionId: string, opts?: { includeRetired?: boolean }): (MessageRow & { contentHash: string; retiredAt: number | null })[] { // #4390: retired rows (rewound/undo history) stay out of the unified // replay by default — they are history, not view. Callers that need diff --git a/tests/d5-5-render-window.test.ts b/tests/d5-5-render-window.test.ts new file mode 100644 index 0000000000..abfc83b51c --- /dev/null +++ b/tests/d5-5-render-window.test.ts @@ -0,0 +1,262 @@ +// D5-5 — render-lane read amplification. The snapshot-render source loads +// EVERY transcript row with content bytes + media hydration on EVERY request +// (store.getMessages), while covered rows' bytes never ride the outbound — +// they are suppressed behind summary anchors. On big folded sessions this is +// thousands of rows × content + base64 media hydration per request. +// +// Contract staked BEFORE the fix (打桩): +// +// 1. On a render turn (mid-list fold + live tail), the buildView path must +// load ONLY the uncovered window: the full-content loader +// (unifiedTailViewRows) must NOT be called during the request; the +// windowed reads (identity rows + uncovered content rows) must be. +// 2. Covered rows' bytes are never read — corrupting a COVERED row's content +// in the store (bad JSON in the CAS) must NOT degrade the render: the +// turn still renders server-authoritative (summary anchor + original +// prefix + tail). Before the fix this degrades (row-unparsable) because +// the loader parses every row including covered ones. +// 3. The rendered outbound stays complete: original uncovered prefix rows, +// the mid-list summary anchor, suppressed covered rows, live tail. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST_ZSTD = "0"; +// The D-lane renders from the unified store's tail view — its sidecar must be +// open (see tests/snapshot-render.test.ts for the #3771 note on ambient env). +process.env.BILI_STORAGE_UNIFIED = "shadow"; +process.env.BILI_CONFIG_FILE = path.join(mkdtempSync(path.join(tmpdir(), "bili-d55-hermetic-")), "no-config.json"); + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions, _resetSessionsForTest } from "../src/session.ts"; +import { rmrf } from "./tmp-rm.ts"; + +const SUMMARY = "MID-HISTORY-SUMMARY-OF-THE-MIDDLE-REGION-LONG-ENOUGH-FOR-THE-KERNEL-MINIMUM-LENGTH-CHECK"; + +function sse(obj: unknown): string { + return `data: ${JSON.stringify(obj)}\n\n`; +} + +// Same shape as the #4241 fixture: 46 messages × ~375 tok under a 30k window, +// mid-list fold m00017..m00030 (the shape that actually exercises the prefix +// walk — a head fold free-passes everything). +function conversation(tail: { role: string; content: string }[] = []): { role: string; content: string }[] { + const out: { role: string; content: string }[] = []; + for (let i = 0; i < 46; i++) { + out.push({ role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the working session. ` + `WORK_${i}_content_`.repeat(88) }); + } + return [...out, ...tail]; +} + +type Harness = { + proxy: http.Server; + upstream: http.Server; + bodies: string[]; + openaiUrl: string; + store: SessionStore; + sessionOf: (label: string) => { meta: { label: string }; state: { blocks: { id: string; active: boolean }[] }; metadata: Record } | undefined; +}; + +async function withHarness(fn: (h: Harness) => Promise): Promise { + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + bodies.push(Buffer.concat(chunks).toString("utf8")); + const n = bodies.length; + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (n === 1) { + const args = JSON.stringify({ content: [{ startId: "m00017", endId: "m00030", topic: "middle region", summary: SUMMARY }] }); + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: null, tool_calls: [{ index: 0, id: "call_p", type: "function", function: { name: "compress", arguments: args } }] } }] })); + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })); + } else { + const text = n === 2 ? "done compressing" : `ok ${n}`; + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: text } }] })); + res.write(sse({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })); + } + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + const dir = mkdtempSync(path.join(tmpdir(), "bili-d55-store-")); + const store = new SessionStore({ enabled: true, dir, debounceMs: 0 }); + _setStoreForTest(store); + _resetSessionsForTest(); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 30_000 } } } }, + modelContextLimit: 30_000, + kernelConfig: defaultConfig(30_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: Boolean(process.env.SNAP_DEBUG), + debug: Boolean(process.env.SNAP_DEBUG), + passthrough: false, + passthroughSource: null, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: true, + releaseNotesCheck: true, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + } as unknown as ProxyOptions); + await once(proxy, "listening"); + const port = (proxy.address() as { port: number }).port; + const h: Harness = { + proxy, + upstream, + bodies, + openaiUrl: `http://127.0.0.1:${port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`, + store, + sessionOf: (label) => listSessions().find((x) => x.meta.label === label) as never, + }; + try { + await fn(h); + } finally { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + delete process.env.BILI_ROLLOUT_SNAPSHOT_RENDER; + store.closeQuietly(); + void rmrf(dir); + } +} + +type Msg = { role: string; content: string }; + +async function turn(h: Harness, messages: unknown[], label: string): Promise<{ status: number; body: string; idx: number }> { + const r = await fetch(h.openaiUrl, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": label }, + body: JSON.stringify({ model: "gpt-test", messages, stream: true }), + }); + const body = await r.text(); + await settleRows(h, label); + return { status: r.status ?? 0, body, idx: h.bodies.length - 1 }; +} + +const rowsSeen = new Map(); +async function settleRows(h: Harness, label: string): Promise { + const prev = rowsSeen.get(label) ?? 0; + for (let i = 0; i < 200; i++) { + const n = h.store.unifiedTailSeqs(label)?.length ?? 0; + if (n !== prev && n > 0) { + rowsSeen.set(label, n); + return; + } + await new Promise((res) => setTimeout(res, 25)); + } + throw new Error(`settleRows: store tail view never changed for ${label} (still ${prev})`); +} + +function sent(bodies: string[], i: number): { role: string; content: unknown }[] { + return JSON.parse(bodies[i]).messages as { role: string; content: unknown }[]; +} + +function textOf(m: { role: string; content: unknown }): string { + return typeof m.content === "string" ? m.content : JSON.stringify(m.content); +} + +test("D5-5: render loads only the uncovered window — no full-content load, covered corruption tolerated", async () => { + process.env.BILI_ROLLOUT_SNAPSHOT_RENDER = "100"; + await withHarness(async (h) => { + const label = "s-d55"; + const history = conversation(); + // turn 1: mid-list fold lands (m17..m30) → covered rows exist. + const r1 = await turn(h, history, label); + assert.equal(r1.status, 200, r1.body); + if (process.env.D55_DEBUG) { + const fs = await import("node:fs"); + fs.writeFileSync("/tmp/d55-r1-body.txt", r1.body); + fs.writeFileSync("/tmp/d55-sent1.json", JSON.stringify(JSON.parse(h.bodies[0]), null, 1)); + fs.writeFileSync("/tmp/d55-sent2.json", h.bodies[1] ?? "NO-SECOND"); + const sess = h.sessionOf(label); + fs.writeFileSync("/tmp/d55-sess.json", JSON.stringify(sess?.state.blocks ?? [], null, 1)); + } + assert.ok(h.sessionOf(label)!.state.blocks.some((b) => b.active), "block armed"); + + // Instrument the store's read surface for the render turn. + let fullLoads = 0; + let idLoads = 0; + let windowLoads = 0; + const proto = SessionStore.prototype as unknown as Record; + const origFull = proto["unifiedTailViewRows"] as (this: SessionStore, id: string) => unknown; + const origIds = proto["unifiedMessageIdRows"] as ((this: SessionStore, id: string) => unknown) | undefined; + const origWindow = proto["unifiedUncoveredRows"] as ((this: SessionStore, id: string, iv: unknown) => unknown) | undefined; + proto["unifiedTailViewRows"] = function (this: SessionStore, id: string) { + fullLoads++; + return origFull.call(this, id); + }; + if (typeof origIds === "function") { + proto["unifiedMessageIdRows"] = function (this: SessionStore, id: string) { + idLoads++; + return origIds.call(this, id); + }; + } + if (typeof origWindow === "function") { + proto["unifiedUncoveredRows"] = function (this: SessionStore, id: string, iv: unknown) { + windowLoads++; + return origWindow.call(this, id, iv); + }; + } + try { + // T2 precondition: corrupt a COVERED row's content bytes in the + // CAS (refs m00017..m00030 == store seq 16..29, 0-based). + const coveredSeq = 20; + const { DatabaseSync } = await import("node:sqlite"); + const db = new DatabaseSync(path.join((h.store as unknown as { dir?: string }).dir ?? "", "index.db"), { readOnly: false }); + try { + db.exec(`UPDATE content SET bytes = CAST('{"broken' AS BLOB) WHERE hash = (SELECT content_hash FROM messages WHERE session_id = '${label}' AND seq = ${coveredSeq})`); + } finally { + db.close(); + } + + // turn 2: the render turn. + const full2 = [...conversation(), { role: "user", content: "Message 46 tail question. " + "TAIL_A_".repeat(80) }]; + const r2 = await turn(h, full2, label); + assert.equal(r2.status, 200, r2.body); + + const out2 = sent(h.bodies, r2.idx); + const joined2 = out2.map(textOf).join("\n"); + // T3: the rendered outbound stays complete. + assert.ok(joined2.includes(SUMMARY), "mid-list anchor rendered"); + assert.ok(joined2.includes("WORK_2_content_"), "uncovered prefix rows render from the store"); + assert.ok(!joined2.includes("WORK_17_content_"), "covered middle rows suppressed"); + assert.ok(joined2.includes("TAIL_A_"), "live tail present"); + const meta = h.sessionOf(label)!.metadata; + const renderTurns = meta["snapshotRenderTurns"] as number | undefined; + assert.ok(typeof renderTurns === "number" && renderTurns >= 1, `D lane active on turn 2 (renderTurns=${String(renderTurns)}), covered-row corruption must not degrade the render`); + + // T1: the window discipline. + assert.equal(fullLoads, 0, `unifiedTailViewRows (full content load) must not run on a render turn — got ${fullLoads}`); + assert.ok(idLoads >= 1, "identity rows were read through the windowed path"); + assert.ok(windowLoads >= 1, "uncovered content rows were read through the windowed path"); + } finally { + proto["unifiedTailViewRows"] = origFull; + if (typeof origIds === "function") proto["unifiedMessageIdRows"] = origIds; + if (typeof origWindow === "function") proto["unifiedUncoveredRows"] = origWindow; + } + }); +}); diff --git a/tests/issue4241-judge-render-baseline.test.ts b/tests/issue4241-judge-render-baseline.test.ts index 87a0a687c5..b73d4a46c8 100644 --- a/tests/issue4241-judge-render-baseline.test.ts +++ b/tests/issue4241-judge-render-baseline.test.ts @@ -249,10 +249,13 @@ test("#4241: render turn with a host-side history edit — judge reconciles agai assert.ok(v.ok, `main-lane verdict must be clean on a render turn: ${JSON.stringify(v.violations[0])}`); } - // teeth: same shape, but the outbound DROPS a frozen-prefix row — - // the judge must fire against the baseline (the swap must not - // blind it to real defects). - _setInvariantJudgeFaultForTest((exitCores: CoreMessage[]) => exitCores.filter((_, i) => i !== 3)); + // teeth: same shape, but the outbound DROPS the last client tail + // row (exitCores.length - 2: the engine appends its trailing nudge + // as the final wire message, so the last tail row sits one before + // it) — the part the render lane owns byte-intact. The rendered + // PREFIX is exempt by design (#4241: cross-turn prefix stability + // is seamLcp's/shadow's job); the tail drop must fire. + _setInvariantJudgeFaultForTest((exitCores: CoreMessage[]) => exitCores.filter((_, i) => i !== exitCores.length - 2)); verdicts.length = 0; const r3 = await turn(h, [...full2, { role: "user", content: "And one more. " + "TAIL_B_".repeat(80) }], label, null); assert.equal(r3.status, 200, r3.body);