From acca4db67a7e91fc8435be574a79d2abfcfe7205 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 5 Nov 2026 00:34:49 +0800 Subject: [PATCH 01/11] feat(storage): v6 render-snapshot plane tables (schema v12, #5664) pf_render / pf_render_log / pf_render_ckpt sidecar tables for the #5664 outbound-row snapshot plane. Rows are the per-rendered-ordinal OUTBOUND bytes previously emitted (prefix-reuse source of truth); the log journals append/cast/recast deltas; every checkpoint carries an S4 sha256 checksum and physically absorbs the log (fold rule: ckpt absolute values then post-ckpt appends). Log continuation starts from max(MAX(ls_seq), ckpt_ls_seq) so entries never fall behind their own checkpoint watermark. Cascade delete rides deleteSession like pf_*. MySQL/network backends degrade soft (null/empty) exactly like the pf_* identity plane. --- src/persist.ts | 52 ++++++++++++++++ src/storage/schema.ts | 43 ++++++++++++- src/storage/shadow.ts | 61 +++++++++++++++++++ src/storage/store.ts | 137 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 292 insertions(+), 1 deletion(-) diff --git a/src/persist.ts b/src/persist.ts index 9ea439901e..d6aaba778d 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -2291,6 +2291,58 @@ export class SessionStore { if (Array.isArray(reg)) this.shadow.pfDeleteAll(reg); } + /** #5664 render snapshot plane facade — same guard shape as the identity + * plane: null/empty/false = "plane unavailable right now" and the v6 + * lane fails soft onto the legacy full path for that turn only. + * pfRenderCommit does NOT swallow silently. */ + pfRenderAvailable(): boolean { + if (!this.shadow || this.shadow.stats.disabled) return false; + try { + return this.shadow.pfRenderAvailable(); + } catch { + return false; + } + } + + pfRenderRows(sid: string): { pos: number; rowJson: string | null }[] { + if (!this.shadow || this.shadow.stats.disabled) return []; + return this.shadow.pfRenderRows(sid); + } + + pfRenderCommit( + sid: string, + args: { + rows: { pos: number; rowJson: string | null }[]; + deletes?: number[]; + logEntries: { kind: string; payloadJson: string }[]; + checkpointMetaJson?: string; + ts?: number; + }, + ): number | null { + if (!this.shadow || this.shadow.stats.disabled) return null; + try { + return this.shadow.pfRenderCommit(sid, args); + } catch (err) { + this.log("warn", `[persist] pf render commit failed (${sid}): ${String(err)}`); // loud once, fail-soft for THIS turn + return null; + } + } + + pfRenderMeta( + sid: string, + ): { + ckpt: { ckptLsSeq: number; metaJson: string; checksum: string; updatedAt: number } | null; + log: { lsSeq: number; kind: string; payloadJson: string; ts: number }[]; + } | null { + if (!this.shadow || this.shadow.stats.disabled) return null; + return this.shadow.pfRenderMeta(sid); + } + + pfRenderCkptIntact(sid: string): boolean { + if (!this.shadow || this.shadow.stats.disabled) return false; + return this.shadow.pfRenderCkptIntact(sid); + } + /** #5312: reversible single-block archive / restore (see UnifiedStore). */ unifiedArchiveBlock(sessionId: string, blockId: string, archiveDir?: string): { ok: boolean; exportPath?: string; reason?: string; contentRowsDeleted?: number } | null { if (!this.shadow || this.shadow.stats.disabled) return null; diff --git a/src/storage/schema.ts b/src/storage/schema.ts index af4f43c012..f88ab2c1b9 100644 --- a/src/storage/schema.ts +++ b/src/storage/schema.ts @@ -74,7 +74,12 @@ // change, and with the watermarkV6MsgId rollout OFF no traffic ever // touches them. No backfill, no back-compat column juggling — older // builds stop at the version gate and simply ignore the extra tables. -export const STORAGE_SCHEMA_VERSION = 11; +// - v12: fastpath v6 render snapshot plane (#5664) — three more +// append-oriented sidecar tables (pf_render / pf_render_log / +// pf_render_ckpt), same pure CREATE IF NOT EXISTS shape: pre-v12 +// installs upgrade with zero byte change, and with the +// watermarkV6Render rollout OFF no traffic touches them. +export const STORAGE_SCHEMA_VERSION = 12; /** #3250: cap for `journal_size_limit` (see STORAGE_PRAGMAS). Passive and * auto checkpoints move frames into the main db and reset the write offset @@ -444,6 +449,42 @@ CREATE TABLE IF NOT EXISTS pf_ckpt ( checksum TEXT NOT NULL, updated_at INTEGER NOT NULL, PRIMARY KEY (sid) +)`); + } + if (from < 12) { + // v12 (#5664): fastpath v6 RENDER snapshot plane — the frozen + // prefix's outbound-rendered rows (the final wire form AFTER all + // rewrite/compaction is done inbound; outbound only maps stored + // rows to the target protocol, no rewrite, no scan). Same LSM + // family as the identity plane: pf_render is the current-truth + // row set (pos = rendered-message ordinal; row_json NULL = + // tombstone: the kernel hid that ordinal), pf_render_log L0 + // journals each turn's ops (cast/append/delete/compact), + // pf_render_ckpt L1 folds it with an S4 checksum over the exact + // meta bytes (hydrate re-verifies; mismatch kills the plane for + // that sid — rebuild from the replay source, never guess). + db.exec(` +CREATE TABLE IF NOT EXISTS pf_render ( + sid TEXT NOT NULL, + pos INTEGER NOT NULL, + row_json TEXT, + PRIMARY KEY (sid, pos) +); +CREATE TABLE IF NOT EXISTS pf_render_log ( + sid TEXT NOT NULL, + ls_seq INTEGER NOT NULL, + kind TEXT NOT NULL, + payload_json TEXT NOT NULL, + ts INTEGER NOT NULL, + PRIMARY KEY (sid, ls_seq) +); +CREATE TABLE IF NOT EXISTS pf_render_ckpt ( + sid TEXT NOT NULL, + ckpt_ls_seq INTEGER NOT NULL, + meta_json TEXT NOT NULL, + checksum TEXT NOT NULL, + updated_at INTEGER NOT NULL, + PRIMARY KEY (sid) )`); } }); diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index a663c30382..ec62f55ff9 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -1187,6 +1187,67 @@ export class UnifiedShadow { } } + // #5664 render snapshot plane — same soft-degrade shape as the identity + // plane: reads return empty/null, the turn commit returns null (the v6 + // lane fails soft onto the legacy full path for that turn only). + + pfRenderAvailable(): boolean { + if (this.sync === null) return false; + try { + return this.sync.pfRenderAvailable(); + } catch { + return false; + } + } + + pfRenderRows(sid: string): { pos: number; rowJson: string | null }[] { + if (this.sync === null) return []; + try { + return this.sync.pfRenderRows(sid); + } catch { + return []; + } + } + + /** Throws are NOT swallowed deliberately (same as pfCommitTurn): a failed + * render commit must be loud to the caller. Network backends: null. */ + pfRenderCommit( + sid: string, + args: { + rows: { pos: number; rowJson: string | null }[]; + deletes?: number[]; + logEntries: { kind: string; payloadJson: string }[]; + checkpointMetaJson?: string; + ts?: number; + }, + ): number | null { + if (this.sync === null) return null; + return this.sync.pfRenderCommit(sid, args); + } + + pfRenderMeta( + sid: string, + ): { + ckpt: { ckptLsSeq: number; metaJson: string; checksum: string; updatedAt: number } | null; + log: { lsSeq: number; kind: string; payloadJson: string; ts: number }[]; + } | null { + if (this.sync === null) return null; + try { + return this.sync.pfRenderMeta(sid); + } catch { + return null; + } + } + + pfRenderCkptIntact(sid: string): boolean { + if (this.sync === null) return false; + try { + return this.sync.pfRenderCkptIntact(sid); + } catch { + return false; + } + } + deleteSessionExt(sessionId: string, key?: string): number { if (this.sync === null) return 0; return this.sync.deleteSessionExt(sessionId, key); diff --git a/src/storage/store.ts b/src/storage/store.ts index 3eb5f06157..b1c420bc62 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2578,9 +2578,146 @@ export class UnifiedStore { this.db.prepare(`DELETE FROM pf_rows WHERE sid IN (${ph})`).run(...sids); this.db.prepare(`DELETE FROM pf_log WHERE sid IN (${ph})`).run(...sids); this.db.prepare(`DELETE FROM pf_ckpt WHERE sid IN (${ph})`).run(...sids); + this.db.prepare(`DELETE FROM pf_render WHERE sid IN (${ph})`).run(...sids); + this.db.prepare(`DELETE FROM pf_render_log WHERE sid IN (${ph})`).run(...sids); + this.db.prepare(`DELETE FROM pf_render_ckpt WHERE sid IN (${ph})`).run(...sids); }); } + // ------------------------------------------------------------------ + // #5664 render snapshot plane — the v6 frozen prefix's OUTBOUND-rendered + // rows (final wire form; inbound owns all rewrite/compaction). Same LSM + // family as the identity plane: row table = current truth, L0 log + // journals ops, L1 ckpt folds with an S4 checksum. Read-only soft + // degrade on the sqlite path (missing plane ⇒ empty); writes throw into + // the surrounding transaction like every other pf_* write. + // ------------------------------------------------------------------ + + pfRenderAvailable(): boolean { + try { + const cols = (this.db.prepare("PRAGMA table_info(pf_render)").all() as unknown as { name?: unknown }[]) + .map((c) => (typeof c?.name === "string" ? c.name : "")); + return cols.includes("row_json") && cols.includes("pos"); + } catch { + return false; + } + } + + /** Current-truth live rows for one sid in pos order (tombstones — NULL + * rows where the kernel hid the ordinal — ride along as null so the + * ordinal structure stays visible). No plane → empty array (lane + * unavailable ⇒ caller serves baseline). */ + pfRenderRows(sid: string): { pos: number; rowJson: string | null }[] { + const out: { pos: number; rowJson: string | null }[] = []; + if (!this.pfRenderAvailable()) return out; + try { + const rows = this.db + .prepare("SELECT pos, row_json FROM pf_render WHERE sid = ? ORDER BY pos") + .all(sid) as { pos: number; row_json: string | null }[]; + for (const r of rows) out.push({ pos: r.pos, rowJson: r.row_json === null ? null : String(r.row_json) }); + } catch { + return []; + } + return out; + } + + /** Atomic turn commit for the render plane (#5664): upsert `rows` + * (pos may move — a recast rewrites covered ordinals), physically drop + * `deletes` (ordinals that no longer exist after a rewrite), journal + * `logEntries` at strictly increasing ls_seq, and — when + * `checkpointMetaJson` is given — fold the whole log into a fresh ckpt + * WITH its S4 checksum in the SAME transaction (the forced checkpoint + * of a compression round). Any throw → rollback → byte-identical plane. + * Returns the next free ls_seq. */ + pfRenderCommit( + sid: string, + args: { + rows: { pos: number; rowJson: string | null }[]; + deletes?: number[]; + logEntries: { kind: string; payloadJson: string }[]; + checkpointMetaJson?: string; + ts?: number; + }, + ): number { + if (!this.pfRenderAvailable()) throw new Error("render plane unavailable"); + return this.db.transaction((): number => { + const insRow = this.db.prepare( + "INSERT INTO pf_render (sid, pos, row_json) VALUES (?, ?, ?) ON CONFLICT (sid, pos) DO UPDATE SET row_json = excluded.row_json", + ); + for (const r of args.rows) insRow.run(sid, r.pos, r.rowJson); + if (args.deletes && args.deletes.length > 0) { + const ph = args.deletes.map(() => "?").join(", "); + this.db.prepare(`DELETE FROM pf_render WHERE sid = ? AND pos IN (${ph})`).run(sid, ...args.deletes); + } + // Continuation must clear BOTH the surviving log tail AND the + // checkpoint watermark: a fresh checkpoint ABSORBS its own log + // entries (delete below), so MAX(log) can sit BEHIND ckpt_ls_seq; + // anything numbered <= ckpt would be invisible to pfRenderMeta + // forever (entries written after the fold silently lost). + const start = Number( + Math.max( + (this.db.prepare("SELECT COALESCE(MAX(ls_seq), 0) AS m FROM pf_render_log WHERE sid = ?").get(sid) as { m: number }).m ?? 0, + (this.db.prepare("SELECT ckpt_ls_seq AS n FROM pf_render_ckpt WHERE sid = ?").get(sid) as { n: number } | undefined)?.n ?? 0, + ), + ); + const insLog = this.db.prepare("INSERT INTO pf_render_log (sid, ls_seq, kind, payload_json, ts) VALUES (?, ?, ?, ?, ?)"); + let seq = start; + for (const e of args.logEntries) { + seq += 1; + insLog.run(sid, seq, e.kind, e.payloadJson, args.ts ?? Date.now()); + } + if (args.checkpointMetaJson !== undefined && seq > 0) { + const hash = createHash("sha256").update(args.checkpointMetaJson).digest("hex"); + this.db + .prepare( + "INSERT INTO pf_render_ckpt (sid, ckpt_ls_seq, meta_json, checksum, updated_at) VALUES (?, ?, ?, ?, ?) " + + "ON CONFLICT (sid) DO UPDATE SET ckpt_ls_seq = excluded.ckpt_ls_seq, meta_json = excluded.meta_json, " + + "checksum = excluded.checksum, updated_at = excluded.updated_at", + ) + .run(sid, seq, args.checkpointMetaJson, hash, args.ts ?? Date.now()); + this.db.prepare("DELETE FROM pf_render_log WHERE sid = ?").run(sid); + } + return seq; + }); + } + + /** Render-plane metadata read (diagnostics / hydration validation): the + * L1 checkpoint plus post-ckpts L0 entries in ls_seq order. */ + pfRenderMeta(sid: string): { + ckpt: { ckptLsSeq: number; metaJson: string; checksum: string; updatedAt: number } | null; + log: { lsSeq: number; kind: string; payloadJson: string; ts: number }[]; + } | null { + if (!this.pfRenderAvailable()) return null; + try { + const c = this.db.prepare("SELECT ckpt_ls_seq, meta_json, checksum, updated_at FROM pf_render_ckpt WHERE sid = ?").get(sid) as + | { ckpt_ls_seq: number; meta_json: string; checksum: string; updated_at: number } + | undefined; + const log = this.db + .prepare("SELECT ls_seq, kind, payload_json, ts FROM pf_render_log WHERE sid = ? AND ls_seq > ? ORDER BY ls_seq") + .all(sid, c === undefined ? 0 : c.ckpt_ls_seq) as { ls_seq: number; kind: string; payload_json: string; ts: number }[]; + return { + ckpt: + c === undefined ? null : { ckptLsSeq: c.ckpt_ls_seq, metaJson: c.meta_json, checksum: c.checksum, updatedAt: c.updated_at }, + log: log.map((r) => ({ lsSeq: r.ls_seq, kind: r.kind, payloadJson: r.payload_json, ts: r.ts })), + }; + } catch { + return null; + } + } + + /** S4: does the checkpoint's checksum cover the EXACT meta bytes? False + * kills the render plane for this sid at the caller (rebuild from the + * replay source — never guess). No ckpt yet = nothing to verify = true. + */ + pfRenderCkptIntact(sid: string): boolean { + if (!this.pfRenderAvailable()) return false; + const c = this.db.prepare("SELECT meta_json, checksum FROM pf_render_ckpt WHERE sid = ?").get(sid) as + | { meta_json: string; checksum: string } + | undefined; + if (c === undefined) return true; + return createHash("sha256").update(c.meta_json).digest("hex") === c.checksum; + } + /** #4949: circuit-breaker re-enable probe — one REAL committed write * through the production transaction path (proves lock acquisition + * disk space, not just connectivity). A single fixed kv key keeps the From 6d3ca309b327df6baf519f58d520e2183f431a89 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 5 Nov 2026 00:34:49 +0800 Subject: [PATCH 02/11] =?UTF-8?q?feat(fastpath):=20v6=20render-snapshot=20?= =?UTF-8?q?lane=20=E2=80=94=20O(1)-bounded=20steady=20turns=20(#5664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Steady-state engaged v6 turns used to re-run the full n-core pipeline (reconcile/fold/detect over stored rows, processTurn over the whole frozen view, estimate/probe over all n rows) even though only the new tail changed. This lane caches what egress already emitted (station 2, committed at finalize on every engaged turn) and, when a conservative gate is fully green, replays ONLY the last W cores plus the new window cores through the kernel (probe-verified byte-identical tail output), splices the cached prefix rows back in without re-serialization, and feeds incremental estimate bases (budget.inputEstBaseTokens) instead of re-scanning n rows. Any red gate condition, failed window verify, or commit failure falls back to the untouched legacy full path for that turn; client-side compaction shrinks the captured row list and forces a recast+checkpoint at finalize (owner-decided compaction semantics). Caps mismatch (model/strategy/maxDim/toolArmed/multimodal) or S4 checksum failure kills the mirror loud and falls back. New rollout cohort watermarkV6Render nested inside watermarkV6MsgId, default 0%. --- src/rollout.ts | 9 + src/server/budget.ts | 16 +- src/server/fastpath-v6-render.ts | 532 +++++++++++++++++++++++++++++++ src/server/prepare-openai.ts | 205 +++++++++++- 4 files changed, 742 insertions(+), 20 deletions(-) create mode 100644 src/server/fastpath-v6-render.ts diff --git a/src/rollout.ts b/src/rollout.ts index e00c4a6a6a..f17987f4cb 100644 --- a/src/rollout.ts +++ b/src/rollout.ts @@ -78,6 +78,15 @@ export const ROLLOUT_FEATURES: Record = { // `BILI_ROLLOUT_WATERMARK_V6_MSG_ID=1`; bucketed growth stays nested // inside the v5 watermark cohort. watermarkV6MsgId: { percent: 0, base: "pluginFullResendWatermark" }, + // #5664 station-3 fast emit: v6 identity lanes with a healthy render + // snapshot plane serve steady hit turns by splicing the cached outbound + // rows (frozen prefix verbatim + bounded window-pass over the last cores) + // instead of rescanning n rows; every red gate condition (pressure, + // caps mismatch, audit cadence, hydrate failure, byte-mismatch vs the + // stored suffix) falls back to this branch's baseline path byte-for-byte. + // Requires watermarkV6MsgId + the pf_render plane; nested cohort bucketing + // inside the v6 lane. Default 0%. + watermarkV6Render: { percent: 0, base: "watermarkV6MsgId" }, // #4804 WS delta continuation: codex/opencode WebSocket clients that keep a // previous_response_id checkpoint stop reconstructing full history on every // turn — the session augments the delta with the server-committed previous diff --git a/src/server/budget.ts b/src/server/budget.ts index 43f4174662..1493603adf 100644 --- a/src/server/budget.ts +++ b/src/server/budget.ts @@ -120,7 +120,11 @@ export function countSystemAndToolsTokens(systemText: string | undefined, tools: * raw chars/4 est ~137K vs provider-billed ~81K) used to win the max() raw * and starve the output clamp (32768 -> 3357 with ample actual headroom). * The usage-grade baseline is already provider-measured and is NEVER scaled. */ -export function estimateInputTokens(processedMessages: CoreMessage[], systemText: string | undefined, tools: unknown, lastInputTokens: number, lastInputTokensSource?: string, kFactor?: number, kOrigin?: string, origin?: string, loadedToolTokens = 0, imageTokens = 0): number { +export function estimateInputTokens(processedMessages: CoreMessage[], systemText: string | undefined, tools: unknown, lastInputTokens: number, lastInputTokensSource?: string, kFactor?: number, kOrigin?: string, origin?: string, loadedToolTokens = 0, imageTokens = 0, inputEstBaseTokens = 0): number { + // #5664: inputEstBaseTokens — additive frozen-prefix basis for the + // render-snapshot lane, where processedMessages holds only the bounded + // tail; priced inside the calibration like every other local-est term. + // #2558: price the image reserve onto the LOCAL-EST arm only. // #2558: price the image reserve onto the LOCAL-EST arm only. A usage-grade // baseline is the provider-reported input total, which already bills those // same images (#488) — adding the reserve to it double-counts every image and @@ -129,7 +133,7 @@ export function estimateInputTokens(processedMessages: CoreMessage[], systemText // reserve). Applied after calibration so the text k̂ never deflates the // separately-learned image cost (#1843 L1). #2391's deferred-tool tokens ride // INSIDE the calibrated arm instead — they are JSON payload text. - const est = applyEstimateCalibration(estimateCoreMessages(processedMessages) + countSystemAndToolsTokens(systemText, tools) + loadedToolTokens, kFactor, kOrigin, origin) + imageTokens; + const est = applyEstimateCalibration(inputEstBaseTokens + estimateCoreMessages(processedMessages) + countSystemAndToolsTokens(systemText, tools) + loadedToolTokens, kFactor, kOrigin, origin) + imageTokens; const baseline = lastInputTokens > 0 && lastInputTokensSource === "usage" ? lastInputTokens : 0; return Math.max(baseline, est); } @@ -487,7 +491,11 @@ export function clampOutgoingOutput( * same source chain as the #546 restore floor). Binds the clamp's cap so an * outgoing max_tokens never exceeds it even when window headroom allows more * — the hard upstream bound strict providers enforce with a 400. Absent → - * byte-identical legacy window-only cap. */ knownOutputCeiling?: number }, + * byte-identical legacy window-only cap. */ knownOutputCeiling?: number; + /** #5664 render-snapshot lane: additive FROZEN-ROW basis for the local + * est side when ctx.processedMessages holds only the bounded tail + * (cumulative Σ estimateCoreMessages over the stored rows). Absent → + * byte-identical legacy behavior. */ inputEstBaseTokens?: number }, sessionId: string, log: (level: string, msg: string) => void, /** #3148: optional visibility hook — invoked with the exact clamp event when @@ -503,7 +511,7 @@ export function clampOutgoingOutput( // already bills them), so the clamp judges the payload on the same max(baseline, est + // reserve) caliber as preflight/metering instead of double-counting every image. const loadedToolTokens = field === "max_output_tokens" ? countLoadedToolTokens(rebuilt) : 0; - const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens, ctx.lastInputTokensSource, ctx.kFactor, ctx.kOrigin, ctx.origin, loadedToolTokens, ctx.imageTokens); + const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens, ctx.lastInputTokensSource, ctx.kFactor, ctx.kOrigin, ctx.origin, loadedToolTokens, ctx.imageTokens, typeof ctx.inputEstBaseTokens === "number" ? ctx.inputEstBaseTokens : 0); // #2490: judge headroom against the larger of the folded view and the refused // raw ledger — see ctx.ledgerFloorTokens. Keeps the shape of estimateInputTokens' // internal max() (usage baseline never scales DOWN), so all three calibers stay diff --git a/src/server/fastpath-v6-render.ts b/src/server/fastpath-v6-render.ts new file mode 100644 index 0000000000..6b1cfa8d01 --- /dev/null +++ b/src/server/fastpath-v6-render.ts @@ -0,0 +1,532 @@ +// #5664 fastpath v6 render-snapshot plane — the "render station" of the +// three-station model (owner ruling 2026-11-04): +// +// station 1 inbound / identity finalize (per turn, existing #5652 lane) +// station 2 this snapshot: steady rounds APPEND one captured row per new +// element; compression rounds (arm/audit/fallback) DIFF against +// the stored rows and either append or RECAST — and a recast +// FORCES an S4-checksum checkpoint in the same transaction. +// station 3 outbound emit: prefix rows pass through verbatim from disk; +// only the bounded window tail runs a kernel pass per request. +// +// Governing guarantees (contract i5664-plan.md, locked): +// +// G0 byte-equivalence is verified, not assumed — every fast round re-runs +// a BOUNDED kernel pass (last W frozen cores + this turn's new cores) +// under a scratch state clone and compares its mapped first-F elements +// against the last F stored rows byte-for-byte. Any mismatch falls back +// to the full legacy path for THIS turn only (P7: disk is the source of +// truth; the mirror loss costs one re-read). +// G1 conservative conjunction gate — ANY red condition (title-gen, active +// blocks, pending fold usage, absorb, fold-mode on, pressure, CCR on, +// non-zero markers, snapshot-lane substitution, caps mismatch, audit +// due…) keeps the ENTIRE pre-#5664 code path verbatim. The fallback is +// structurally identical to "this feature does not exist". +// G2 capture point = the exact array the baseline emits (rebuiltMessages +// before system/ephemeral injection); steady capture stores raw strings +// + parsed caches so the prefix never gets re-parsed or re-stringified. +// G3 S4 death verdict — a checksum-corrupt checkpoint kills the plane for +// that sid at hydration; the next legacy turn's diff self-heals with a +// fresh forced checkpoint. Never guess, never forward corrupt bytes. +// P7 mirrors are caches (LRU byte budget); losing one = DB re-read. +import type { SessionStore } from "../persist.js"; +import type { Session } from "../session.js"; + +type LogFn = (level: string, msg: string) => void; + +// --------------------------------------------------------------------------- +// Tunables +// --------------------------------------------------------------------------- + +/** Bounded render window W (frozen cores fed to the scratch kernel pass). + * Probe evidence (three strategies × mixed transcript × 28 turns): the + * suffix-only W-pass reproduces every element's outbound bytes exactly, so + * 8 is plenty; env-tunable for operators. */ +export const PF_RENDER_WINDOW: number = (() => { + const n = Number(process.env["BILI_PF_RENDER_WINDOW"] ?? 8); + return Number.isFinite(n) && n >= 1 ? Math.min(Math.floor(n), 64) : 8; +})(); + +/** Every N engaged hit-turns force ONE full legacy pass whose capture diff + * re-verifies the stored rows end-to-end (the scheduled audit). */ +export const PF_RENDER_AUDIT_EVERY: number = (() => { + const n = Number(process.env["BILI_PF_RENDER_AUDIT_EVERY"] ?? 8); + return Number.isFinite(n) && n >= 2 ? Math.min(Math.floor(n), 1024) : 8; +})(); + +/** tokenCount ≥ ratio × modelContextLimit ⇒ emergency pressure ⇒ legacy + * (the fold/expand machinery must see the whole view when the model is + * anywhere near its ceiling). */ +export const PF_RENDER_EMERGENCY_RATIO = 0.9; + +/** In-memory mirror byte budget (raw row bytes summed); oldest world evicted + * first when over (P7 — re-hydrate from disk later, zero correctness cost). */ +const MIRROR_BYTE_BUDGET = 256 * 1024 * 1024; +let mirrorBytes = 0; + +/** Capabilities fingerprint — any change forces a recast (cheap, correct). */ +export interface RenderCaps { + v: 1; + model: string; + strategy: string; + maxDim: number; + toolArmed: boolean; + droppable: boolean; + multimodal: boolean; +} + +function capsEqual(a: RenderCaps, b: RenderCaps): boolean { + return ( + a.v === b.v && + a.model === b.model && + a.strategy === b.strategy && + a.maxDim === b.maxDim && + a.toolArmed === b.toolArmed && + a.droppable === b.droppable && + a.multimodal === b.multimodal + ); +} + +interface RenderMeta { + v: 1; + caps: RenderCaps; + rowCount: number; + estCum: number; + textCum: number; + imgCum: number; +} + +export interface RenderWorld { + sid: string; + /** Outbound JSON string per rendered ordinal, contiguous 0..n-1. */ + rows: string[]; + /** Parsed caches of `rows` (same index) — the splice reads these. */ + rowsParsed: unknown[]; + caps: RenderCaps; + /** Cumulative Σ estimateCoreMessagesUpper over all current rows. */ + estCum: number; + /** Cumulative Σ estimateCoreMessages over all current rows. */ + textCum: number; + /** Cumulative image-reserve contribution of frozen rows. */ + imgCum: number; + /** Engaged-turn tick — % PF_RENDER_AUDIT_EVERY === 0 ⇒ audit due. */ + auditTick: number; +} + +const mirrors = new Map(); +/** relay session id → pf sids owned by it (teardown sweep). */ +const sessionSids = new Map>(); + +// --------------------------------------------------------------------------- +// Mirror lifecycle +// --------------------------------------------------------------------------- + +function touchMirror(rw: RenderWorld): void { + // LRU refresh: re-insert to move to the tail (iteration head = oldest). + if (mirrors.has(rw.sid)) { + mirrors.delete(rw.sid); + mirrorBytes -= rw.rows.reduce((s, r) => s + r.length, 0); + } + mirrors.set(rw.sid, rw); + mirrorBytes += rw.rows.reduce((s, r) => s + r.length, 0); + while (mirrorBytes > MIRROR_BYTE_BUDGET && mirrors.size > 1) { + const oldest = mirrors.keys().next().value as string | undefined; + if (oldest === undefined) break; + const victim = mirrors.get(oldest); + mirrors.delete(oldest); + if (victim) mirrorBytes -= victim.rows.reduce((s, r) => s + r.length, 0); + // #5664 note: eviction is silent by design (P7 cache semantics). + } +} + +function registerSidForSession(sessionId: string, sid: string): void { + let set = sessionSids.get(sessionId); + if (!set) { + set = new Set(); + sessionSids.set(sessionId, set); + } + set.add(sid); +} + +export function clearWatermarkV6Render(sessionId: string): void { + const set = sessionSids.get(sessionId); + if (!set) return; + for (const sid of set) { + const rw = mirrors.get(sid); + if (rw) { + mirrors.delete(sid); + mirrorBytes -= rw.rows.reduce((s, r) => s + r.length, 0); + } + } + sessionSids.delete(sessionId); +} + +/** Hydrate from disk (G3): ckpt integrity check first (S4 death verdict), + * then current-truth rows + post-ckpt log fold for the cumulative sums. + * Returns null on ANY inconsistency — caller serves the baseline path. */ +function hydrateMirror(store: SessionStore, sid: string, log?: LogFn): RenderWorld | null { + try { + if (!store.pfRenderCkptIntact(sid)) { + log?.("error", `[${sid}] #5664 render checkpoint S4 checksum mismatch — plane judged dead for this sid until a forced recast heals it`); + return null; + } + const rowsRaw = store.pfRenderRows(sid); + if (rowsRaw.length === 0) return null; + const metaRead = store.pfRenderMeta(sid); + let estCum = 0; + let textCum = 0; + let imgCum = 0; + let expectedCount = -1; + let capsJson = ""; + if (metaRead?.ckpt) { + const m = JSON.parse(metaRead.ckpt.metaJson) as RenderMeta; + estCum = m.estCum ?? 0; + textCum = m.textCum ?? 0; + imgCum = m.imgCum ?? 0; + expectedCount = m.rowCount; + capsJson = metaRead.ckpt.metaJson; + for (const e of metaRead.log) { + const p = JSON.parse(e.payloadJson) as { de?: number; dt?: number; di?: number; n?: number; capsJson?: string } & Partial; + if (e.kind === "cast" || e.kind === "recast") { + estCum = p.estCum ?? 0; + textCum = p.textCum ?? 0; + imgCum = p.imgCum ?? 0; + expectedCount = p.rowCount ?? -1; + if (e.kind === "recast" && p.capsJson !== undefined) capsJson = p.capsJson; + } else if (e.kind === "append") { + estCum += p.de ?? 0; + textCum += p.dt ?? 0; + imgCum += p.di ?? 0; + if (expectedCount >= 0) expectedCount += p.n ?? 0; + } + } + } else { + capsJson = ""; + } + const rows: string[] = new Array(rowsRaw.length); + const rowsParsed: unknown[] = new Array(rowsRaw.length); + for (let i = 0; i < rowsRaw.length; i++) { + const rj = rowsRaw[i].rowJson; + if (rj === null || rowsRaw[i].pos !== i) return null; // tombstone/gap = inconsistent + rows[i] = rj; + try { + rowsParsed[i] = JSON.parse(rj); + } catch { + return null; + } + } + if (expectedCount !== -1 && expectedCount !== rows.length) { + // Base sums no longer line up with the row table — the log fold + // disagrees with truth. Caller will serve legacy; the next + // capture diff (full arrays both sides) heals with a recast. + return null; + } + let caps: RenderCaps | undefined; + if (capsJson) { + // The log payload stores caps VERBATIM (JSON.stringify(caps)); the + // checkpoint meta wraps them as RenderMeta.caps. + const parsed = JSON.parse(capsJson) as RenderCaps & Partial; + // Two storage shapes coexist: render-log payloads carry caps + // VERBATIM ({v,model,...}); the checkpoint meta WRAPS them as + // {v,caps:{...},rowCount,...}. Accept both. + const inner = (parsed.caps as RenderCaps | undefined); + caps = inner && inner.v === 1 ? inner : (parsed.v === 1 ? parsed : undefined); + } else { + return null; // rows without capabilities = cannot gate safely + } + if (!caps || caps.v !== 1) return null; + const rw: RenderWorld = { sid, rows, rowsParsed, caps, estCum, textCum, imgCum, auditTick: 0 }; + touchMirror(rw); + return rw; + } catch { + return null; + } +} + +// --------------------------------------------------------------------------- +// Gate (station entry — called once per engaged request, cheap probes only) +// --------------------------------------------------------------------------- + +export interface RenderGateArgs { + sessionId: string; + session: Session; + store: SessionStore; + /** true iff this request is an engaged v6 HIT (v6Evaluated.hit !== null). */ + engagedHit: boolean; + /** true on the arm turn (first registration) — always rides legacy. */ + armedTurn: boolean; + isTitleGen: boolean; + /** the render plane sid (x-bili-pf-session / probe owner). */ + sid: string; + /** absolute position of the LAST identity element (plane 1 world.maxPos). */ + maxPos: number; + tokenCount: number; + modelContextLimit: number; + caps: RenderCaps; + /** #3756 snapshot lane substituted the kernel view for this turn. */ + snapshotLaneActive: boolean; + absorbActive: boolean; + ccrOn: boolean; + foldUsagePending: boolean; + markerNonZero: boolean; + log?: LogFn; +} + +export type RenderGateDecision = + | { ok: false; reason: string } + | { ok: true; rw: RenderWorld; F: number }; + +/** G1 conservative conjunction — every red condition returns the reason so + * the lane stays debuggable; the fallback path is unchanged baseline. */ +export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { + const off = (reason: string): RenderGateDecision => ({ ok: false, reason }); + if (!args.engagedHit) return off("not-engaged"); + if (args.armedTurn) return off("arm-turn"); + if (args.isTitleGen) return off("title-gen"); + if (!args.store.pfRenderAvailable()) return off("plane-unavailable"); + if (args.tokenCount >= PF_RENDER_EMERGENCY_RATIO * args.modelContextLimit) return off("pressure"); + if (args.snapshotLaneActive) return off("snapshot-lane"); + if (args.session.state.blocks.some((b) => b.active)) return off("blocks-active"); + if (args.foldUsagePending) return off("fold-pending"); + if (args.absorbActive) return off("absorb-active"); + if (args.ccrOn) return off("ccr-on"); + if (args.markerNonZero) return off("markers"); + let rw = mirrors.get(args.sid); + if (rw === undefined) rw = hydrateMirror(args.store, args.sid, args.log) ?? undefined; + if (rw === undefined) return off("hydrate-failed"); + if (!capsEqual(rw.caps, args.caps)) return off("caps-mismatch"); + if (rw.auditTick % PF_RENDER_AUDIT_EVERY === 0 && rw.auditTick > 0) return off("audit-due"); + if (rw.rows.length < 1) return off("no-rows"); + if (args.maxPos < 0) return off("no-plane-geometry"); + // Alignment invariant: rows are captured per engaged round and always + // number one row per ledger element (row i ↔ pos i), so at GATE time — + // which runs BEFORE this round's finalize advances the plane — + // rows.length must equal maxPos+1. A mismatch means an earlier commit + // failed soft (disk ahead/behind the mirror): fall back and let the + // legacy diff+forced checkpoint self-heal rather than splice on a + // misaligned window. + if (rw.rows.length !== args.maxPos + 1) return off("rows-misaligned"); + registerSidForSession(args.sessionId, args.sid); + const F = Math.min(PF_RENDER_WINDOW, rw.rows.length); + // Window input = last F identity elements (+ new cores added by caller); + // positions are contiguous because the ledger grows forward only (P3). + if (args.maxPos < F - 1) return off("window-short"); + return { ok: true, rw, F }; +} + +/** Called AFTER the decision is consumed (keeps the audit cadence honest — + * a gated-off-for-pressure turn still counts as engaged). */ +export function bumpAuditTick(sid: string): void { + const rw = mirrors.get(sid); + if (rw) rw.auditTick += 1; +} + +// --------------------------------------------------------------------------- +// Fast-round verification + splice (station 3) +// --------------------------------------------------------------------------- + +/** Compare the mapped first-F fast outputs against the stored suffix. The + * caller has already run the SAME mapping chain (rescale→harden→ + * coreToOpenai→systemToUser voice fn) as the baseline capture, so this is + * a pure byte compare. Returns false on ANY difference (incl. length). */ +export function verifyRenderWindow(fastEmitsJson: string[], rw: RenderWorld, F: number): boolean { + if (fastEmitsJson.length < F) return false; + const start = rw.rows.length - F; + if (start < 0) return false; + for (let i = 0; i < F; i++) { + if (fastEmitsJson[i] !== rw.rows[start + i]) return false; + } + return true; +} + +/** Splice: prefix parsed rows (cached — zero parse/stringify cost) + mapped + * fast tail. Row indices align with rendered ordinals (probe-verified + * positional determinism; the byte check above is the tripwire). */ +export function spliceOutboundRows(rw: RenderWorld, F: number, tail: T[]): T[] { + const prefixLen = rw.rows.length - F; + if (prefixLen < 0) throw new Error("#5664 splice prefix underflow"); + const out: T[] = []; + // rowsParsed entries are JSON.parse of THIS plane's previously captured + // rows — exactly T elements by construction (capture stores what egress + // emitted, and egress emitted T). + out.push(...(rw.rowsParsed.slice(0, prefixLen) as T[])); + out.push(...tail); + return out; +} + +// --------------------------------------------------------------------------- +// Capture commits (station 2) — invoked at finalize on EVERY engaged turn +// --------------------------------------------------------------------------- + +export interface CaptureSums { + estCum: number; + textCum: number; + imgCum: number; +} + +export type CaptureMode = "steady" | "legacy"; + +export interface CaptureArgs { + sessionId: string; + store: SessionStore; + sid: string; + mode: CaptureMode; + caps: RenderCaps; + /** steady: ONLY the new tail raw strings; legacy: the FULL captured rows. */ + rowStrings: string[]; + sums: CaptureSums; // steady: DELTAS for the tail; legacy: ABSOLUTE totals. + log?: LogFn; +} + +/** 'equal' | 'append' | 'recast' — prefix-identical growth vs rewrite. */ +export function diffRowLists(prev: string[], cur: string[]): "equal" | "append" | "recast" { + if (prev.length === cur.length) { + for (let i = 0; i < prev.length; i++) if (prev[i] !== cur[i]) return "recast"; + return "equal"; + } + if (cur.length > prev.length) { + for (let i = 0; i < prev.length; i++) if (prev[i] !== cur[i]) return "recast"; + return "append"; + } + return "recast"; +} + +export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op: "none" | "append" | "cast" | "recast" } { + const { store, sid, caps, log } = args; + let rw = mirrors.get(sid); + if (rw === undefined && !mirrors.has(sid)) { + const hydrated = hydrateMirror(store, sid, log); + if (hydrated !== null) { + mirrors.set(sid, hydrated); + rw = hydrated; + } + } + const metaJson = (m: RenderMeta): string => JSON.stringify(m); + if (rw === undefined || rw.rows.length === 0) { + // No usable base: full cast with a FORCED checkpoint (self-healing + // first use / post-death rebuild). Legacy turns only reach here with + // full arrays, which is exactly what a cast needs. + if (args.mode === "legacy") { + const rows = args.rowStrings; + const m: RenderMeta = { v: 1, caps, rowCount: rows.length, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum }; + const okSeq = store.pfRenderCommit(sid, { + rows: rows.map((rj, pos) => ({ pos, rowJson: rj })), + logEntries: [{ kind: "cast", payloadJson: JSON.stringify({ ...m, capsJson: JSON.stringify(caps) }) }], + checkpointMetaJson: metaJson(m), + }); + if (okSeq === null) return { committed: false, op: "none" }; + const parsed = rows.map((rj) => safeParse(rj)); + mirrors.set(sid, { sid, rows: [...rows], rowsParsed: parsed, caps: { ...caps }, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum, auditTick: 0 }); + registerSidForSession(args.sessionId, sid); + return { committed: true, op: "cast" }; + } + // Steady turns without a base cannot append into nothing: skip (the + // identity lane already served; next legacy turn builds the base). + return { committed: false, op: "none" }; + } + if (args.mode === "steady") { + const prevLen = rw.rows.length; + const rows = args.rowStrings; + const okSeq = store.pfRenderCommit(sid, { + rows: rows.map((rj, i) => ({ pos: prevLen + i, rowJson: rj })), + logEntries: [{ kind: "append", payloadJson: JSON.stringify({ de: args.sums.estCum, dt: args.sums.textCum, di: args.sums.imgCum, n: rows.length }) }], + }); + if (okSeq === null) return { committed: false, op: "none" }; + const parsed = rows.map((rj) => safeParse(rj)); + rw.rows.push(...rows); + rw.rowsParsed.push(...parsed); + rw.estCum += args.sums.estCum; + rw.textCum += args.sums.textCum; + rw.imgCum += args.sums.imgCum; + touchMirror(rw); + return { committed: true, op: "append" }; + } + // legacy full-capture: diff against truth, minimal commit. + const verdict = diffRowLists(rw.rows, args.rowStrings); + if (verdict === "equal") { + rw.caps = { ...caps }; // keep caps fresh even when bytes match + touchMirror(rw); + return { committed: true, op: "none" }; + } + if (verdict === "append") { + const prevLen = rw.rows.length; + const tail = args.rowStrings.slice(prevLen); + const delta = { + estCum: Math.max(0, args.sums.estCum - rw.estCum), + textCum: Math.max(0, args.sums.textCum - rw.textCum), + imgCum: Math.max(0, args.sums.imgCum - rw.imgCum), + }; + const okSeq = store.pfRenderCommit(sid, { + rows: tail.map((rj, i) => ({ pos: prevLen + i, rowJson: rj })), + logEntries: [{ kind: "append", payloadJson: JSON.stringify({ de: delta.estCum, dt: delta.textCum, di: delta.imgCum, n: tail.length }) }], + }); + if (okSeq === null) return { committed: false, op: "none" }; + const parsed = tail.map((rj) => safeParse(rj)); + rw.rows.push(...tail); + rw.rowsParsed.push(...parsed); + rw.estCum = args.sums.estCum; + rw.textCum = args.sums.textCum; + rw.imgCum = args.sums.imgCum; + rw.caps = { ...caps }; + touchMirror(rw); + return { committed: true, op: "append" }; + } + // recast — client-side structural change (compaction & friends) or caps + // flip: rewrite covered ordinals, physically drop the excess, and FORCE + // the L1 checkpoint in the same transaction (owner ruling). + const rows = args.rowStrings; + const oldMaxPos = rw.rows.length - 1; + const deletes = oldMaxPos >= rows.length ? Array.from({ length: oldMaxPos + 1 - rows.length }, (_, i) => rows.length + i) : []; + const m: RenderMeta = { v: 1, caps, rowCount: rows.length, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum }; + const okSeq = store.pfRenderCommit(sid, { + rows: rows.map((rj, pos) => ({ pos, rowJson: rj })), + deletes: deletes.length > 0 ? deletes : undefined, + logEntries: [{ kind: "recast", payloadJson: JSON.stringify({ ...m, capsJson: JSON.stringify(caps) }) }], + checkpointMetaJson: metaJson(m), + }); + if (okSeq === null) return { committed: false, op: "none" }; + const parsed = rows.map((rj) => safeParse(rj)); + mirrors.set(sid, { sid, rows: [...rows], rowsParsed: parsed, caps: { ...caps }, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum, auditTick: 0 }); + return { committed: true, op: "recast" }; +} + +function safeParse(json: string): unknown { + try { + return JSON.parse(json); + } catch { + return {}; + } +} + +// --------------------------------------------------------------------------- +// Diagnostics / test surface +// --------------------------------------------------------------------------- + +export interface RenderDebugWorld { + sid: string; + rows: number; + caps: RenderCaps; + estCum: number; + textCum: number; + imgCum: number; + auditTick: number; +} + +export function watermarkV6RenderDebugState(sessionId: string): { sids: RenderDebugWorld[] } { + const set = sessionSids.get(sessionId); + const sids: RenderDebugWorld[] = []; + if (set) { + for (const sid of set) { + const rw = mirrors.get(sid); + if (rw) sids.push({ sid, rows: rw.rows.length, caps: rw.caps, estCum: rw.estCum, textCum: rw.textCum, imgCum: rw.imgCum, auditTick: rw.auditTick }); + } + } + return { sids }; +} + +/** Test-only: reset every mirror (never call from production paths). */ +export function _resetWatermarkV6RenderTestState(): void { + mirrors.clear(); + sessionSids.clear(); + mirrorBytes = 0; +} diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index bbab3b39cc..c2e7285591 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -1,7 +1,7 @@ import http from "node:http"; import type { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel"; import { isRenderedSummaryMessage, renderNudgeText, viableRanges } from "acp-kernel"; -import { coreToOpenai, injectOpenaiSystem, openaiToCore, type BiliMessage, type OpenAIRequestBody, type OpenAITool } from "acp-kernel/wire"; +import { coreToOpenai, injectOpenaiSystem, openaiToCore, type BiliMessage, type OpenAIMessage, type OpenAIRequestBody, type OpenAITool } from "acp-kernel/wire"; import type { CompressReasoningConfig } from "../reasoning-drop.js"; import type { ProxyOptions } from "../config.js"; import { effectivePromptCacheRouting } from "../config.js"; @@ -12,7 +12,7 @@ import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTo import { yieldToEventLoop } from "../yield-loop.js"; import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; -import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; +import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode, type FoldReconcileResult } from "../fold-reconcile.js"; import { maybeSnapshotView } from "./snapshot-render.js"; import { storedMessagesFor, storedRowLookupFor } from "../fold-decomposition-lookup.js"; import { nudgeSuppressed } from "../session-self-heal.js"; @@ -41,7 +41,9 @@ import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; import { featureEnabled } from "../rollout.js"; import { evaluateWatermarkFastpath, finalizeWatermarkTurn, type FastpathHit, type FastpathOutcome } from "./fastpath.js"; -import { evaluateWatermarkV6, finalizeWatermarkV6, type V6Outcome } from "./fastpath-v6.js"; +import { evaluateWatermarkV6, finalizeWatermarkV6, PF_SESSION_HEADER, watermarkV6DebugState, type V6Outcome } from "./fastpath-v6.js"; +import { bumpAuditTick, commitRenderCapture, renderPlaneGate, spliceOutboundRows, verifyRenderWindow, type RenderCaps, type RenderGateDecision, type RenderWorld } from "./fastpath-v6-render.js"; +import { getStore } from "../persist.js"; import { collectRequestIds, probeRequestMembership } from "../request-membership.js"; export async function prepareOpenai( @@ -73,6 +75,16 @@ export async function prepareOpenai( let sysNotes: string[] = []; let openaiOutboundSystem: string | undefined; let processedMessages: CoreMessage[] = []; + // #5664 render-snapshot capture carriers (null/empty = no engaged v6 turn + // captured this request — the commit block below skips on null). + let renderedCaptureStrings: string[] | null = null; + let renderedTailObjects: unknown[] = []; + let renderedImageDelta = 0; + let renderCapturedSid: string | null = null; + let renderCapturedCaps: RenderCaps | null = null; + // #5664 green verdict carrier (declared here so the clamp/estimate/capture + // consumers AFTER the try block see it; assigned at the gate below). + let renderFast: { rw: RenderWorld; F: number; maxPos: number; sid: string } | null = null; // #3222: #651's reasoning-drop plan, computed on the INBOUND history before // core.processTurn — see planReasoningDrop for why the post-turn view is // too late (the kernel hides consumed call+result pairs inside it). @@ -223,6 +235,52 @@ export async function prepareOpenai( if (loopConfig.compressAtFoldPointClearThinking) { loopConfig.reasoningDroppable = makeFoldPointReasoningDroppable(renderMsgs, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))); } + // #5664 render-snapshot lane — gate at station entry (cheap probes + // only). Every red condition keeps the pre-#5664 path byte-for-byte; + // a green verdict only swaps the processTurn slot below to a bounded + // window pass; the egress splice + byte verification happen at the + // capture site. Rollout: watermarkV6Render (default OFF, nested in + // the watermarkV6MsgId cohort bucket). + const renderSid = v6Evaluated?.extraHeaders?.[PF_SESSION_HEADER] ?? null; + const renderGeom = renderSid !== null ? watermarkV6DebugState(sessionId).worlds[renderSid] : undefined; + if (wpHit !== undefined && renderSid !== null && renderGeom !== undefined && featureEnabled("watermarkV6Render")) { + renderCapturedSid = renderSid; + const rcaps: RenderCaps = { + v: 1, + model: modelIdOf(parsed) ?? "", + strategy: renderStrategy, + maxDim: imageRescaleMaxDim(), + toolArmed: imageRescaleToolArmed(session), + droppable: loopConfig.compressAtFoldPointClearThinking === true, + multimodal: wpHit.seedMultimodal === true || wpHit.windowCores.some((m) => Array.isArray((m as { content?: unknown }).content)), + }; + renderCapturedCaps = rcaps; + const renderGate: RenderGateDecision = renderPlaneGate({ + sessionId, + session, + store: getStore(), + engagedHit: true, + armedTurn: v6Evaluated?.armed !== undefined, + isTitleGen, + sid: renderSid, + maxPos: renderGeom.maxPos, + tokenCount, + modelContextLimit: config.modelContextLimit, + caps: rcaps, + snapshotLaneActive: judgeBaseline !== undefined, + absorbActive, + ccrOn: pluginMode && ccrEnabled(session), + foldUsagePending: session.stats.pendingFoldUsage === true, + markerNonZero: markerArtifactCount(markerCounts) > 0, + log, + }); + if (renderGate.ok) { + bumpAuditTick(renderSid); + renderFast = { rw: renderGate.rw, F: renderGate.F, maxPos: renderGeom.maxPos, sid: renderSid }; + } else { + log("debug", `[${sessionId}] #5664 render lane off (${renderGate.reason}) — baseline full path`); + } + } // #2432: dsh desktop's native compaction can LAND without ever // transiting bili — its summarizer calls ctx.llm.stream() directly, // and manual /compact / idle-session paths have no ALS attribution so @@ -270,7 +328,12 @@ export async function prepareOpenai( // #3301: stored decompositions for pre-#2644 anchors whose wire-side // backfill can never fire after a wire-shape flip. const foldMode = resolveFoldReconcileMode(process.env, opts.compress.reconcile); - const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs), storedRowLookup: foldMode === "off" ? undefined : storedRowLookupFor(sessionId) }); + // #5664: the render lane skips the stored-row walk itself — its fold + // coverage rides the audit/full path; the one consumer below is gated + // on the same non-null. + const reconcileOutcome: FoldReconcileResult | null = renderFast === null + ? reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount, storedMessages: foldMode === "off" ? undefined : storedMessagesFor(session, sessionId, msgs), storedRowLookup: foldMode === "off" ? undefined : storedRowLookupFor(sessionId) }) + : null; if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. @@ -287,9 +350,48 @@ export async function prepareOpenai( await yieldToEventLoop(); // #5293: re-anchor the nudge growth reference after a user-aborted turn (no-op otherwise). absorbAbortedTurnGrowth(session, tokenCount); - const turn = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); - session.state = turn.state; - adoptContentStore(session, turn.contentStore); + // #5664: green-gated hit rounds run the BOUNDED kernel pass here — + // last F frozen cores (hydrated from the identity ledger by absolute + // pos) + this turn's new cores, over a SCRATCH state clone. Live-state + // sync defers to the next full pass (audit cadence ≤ PF_RENDER_AUDIT_EVERY + // bounds every deferred quantity); refs/rules minted inside the clone + // merge back below. ANY throw → legacy full pass this turn (P7: disk + // is truth, mirrors are caches). + const turn = (() => { + if (renderFast !== null && wpHit !== undefined) { + try { + const posFrom = renderFast.maxPos - renderFast.F + 1; + const winCoresJson = getStore().pfHydrateWindow(renderFast.sid, posFrom, renderFast.maxPos); + const frozenCores: CoreMessage[] = []; + for (let p = posFrom; p <= renderFast.maxPos; p++) { + const rawJson = winCoresJson.get(p); + if (rawJson === undefined) throw new Error(`window-core gap at pos ${p}`); + // A ledger element may carry MULTIPLE cores (system/ + // content split) — flatten per fastpath-v6's own + // loader semantics and re-stamp wireIndex from pos. + const arr = JSON.parse(rawJson) as CoreMessage[]; + for (const c of arr) frozenCores.push(Object.assign(c, { wireIndex: p })); + } + const scratch = structuredClone(session.state); + const t = core.processTurn({ messages: [...frozenCores, ...wpHit.windowCores], state: scratch, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); + const liveRefs = session.state.messageRefs as unknown as { byRaw: Record; byRef: Record }; + const srcRefs = scratch.messageRefs as unknown as { byRaw: Record; byRef: Record }; + for (const [k, v] of Object.entries(srcRefs.byRaw)) if (!(k in liveRefs.byRaw)) liveRefs.byRaw[k] = v; + for (const [k, v] of Object.entries(srcRefs.byRef)) if (!(k in liveRefs.byRef)) liveRefs.byRef[k] = v; + for (const r of scratch.rules ?? []) if (!(session.state.rules ?? []).some((x) => x.id === r.id)) (session.state.rules ??= []).push(r); + session.state.nextRuleId = Math.max(session.state.nextRuleId ?? 1, scratch.nextRuleId ?? 1); + return t; + } catch (err) { + log("warn", `[${sessionId}] #5664 render window pass failed (${String(err)}) — legacy full pass this turn`); + renderFast = null; + } + } + return core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); + })(); + if (renderFast === null) { + session.state = turn.state; + adoptContentStore(session, turn.contentStore); + } // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. await yieldToEventLoop(); @@ -366,7 +468,7 @@ export async function prepareOpenai( // (#2432): it is already recorded as "native-compaction" and rebased — // a second "unannounced-rewrite" entry beside it would present the // substrate destruction as "another compressor fighting you". - if (!dshRebased) { + if (reconcileOutcome !== null && !dshRebased) { const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id), reconcileOutcome); if (rewrite.churnExplained) { // #4131: cross-build id re-mint (#3642) — fold-reconcile found every @@ -385,8 +487,13 @@ export async function prepareOpenai( markCompactionBoundary(session); } } - applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); - reapOrphansLogged(session, msgs, log, sessionId); + // #5664: archive/orphan sweeps ride the full path — the render lane + // defers them to the audit/full pass (the conjunction gate guarantees + // nothing depends on their having run this round). + if (renderFast === null) { + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); + reapOrphansLogged(session, msgs, log, sessionId); + } // [#1271/#1343] plugin mode: acp_retrieve already acked via the tool API; snapshot // the queued full text onto THIS forward (stays in the queue until commit/drop, so an // upstream failure drops-and-logs it instead of vanishing it). @@ -398,12 +505,48 @@ export async function prepareOpenai( } // #3400: egress image rescale — outbound copies only; the originals // stay in session state/persistence untouched. - const egressMessages = await applyImageRescalePass(processedMessages as BiliMessage[], { session, sessionId, log, maxDim: imageRescaleMaxDim(), toolArmed: imageRescaleToolArmed(session) }); // #4166: re-voice ONLY bili's own summary carriers (isRenderedSummaryMessage). // Client-origin mid system/developer messages (DSH tool_addition and // friends) are conversation data — they re-emit verbatim through the // kernel's rawOpenaiMessage sidecar and must never change voice (#4159). - rebuiltMessages = hardenOpenaiAssistantContent(coreToOpenai(systemToUser(egressMessages, (m) => isRenderedSummaryMessage(m)))); + const summaryVoiceFn = (m: BiliMessage): boolean => isRenderedSummaryMessage(m); + const mapEgress = async (pms: CoreMessage[]): Promise => + hardenOpenaiAssistantContent(coreToOpenai(systemToUser(await applyImageRescalePass(pms as BiliMessage[], { session, sessionId, log, maxDim: imageRescaleMaxDim(), toolArmed: imageRescaleToolArmed(session) }), summaryVoiceFn))); + if (renderFast !== null) { + // Station 3: prefix rows pass through verbatim (cached parses); + // the mapped tail replaces the last F stored rows AND extends them. + // The byte check against the stored suffix is the sole guard — on + // mismatch redo the legacy full pass THIS turn (the next capture + // self-heals the rows with a recast + forced checkpoint). + const mappedAll = await mapEgress(processedMessages); + const rw = renderFast.rw; + const wpWindowLen = wpHit?.windowCores.length ?? 0; + const committedTail = mappedAll.slice(0, Math.min(renderFast.F + wpWindowLen, mappedAll.length)); + renderedImageDelta = imageReserveFor(session, "openai", { model: parsed.model, messages: committedTail } as unknown as OpenAIRequestBody, opts, billingUpstream ?? upstreamOrigin); + if (verifyRenderWindow(committedTail.slice(0, renderFast.F).map((m) => JSON.stringify(m)), rw, renderFast.F)) { + rebuiltMessages = spliceOutboundRows(rw, renderFast.F, committedTail); + renderedCaptureStrings = [...rw.rows.slice(0, rw.rows.length - renderFast.F), ...committedTail.map((m) => JSON.stringify(m))]; + renderedTailObjects = [...committedTail]; + log("debug", `[${sessionId}] #5664 render snapshot FAST emit (prefix=${rw.rows.length - renderFast.F} cached rows, tail=${committedTail.length} mapped)`); + } else { + log("warn", `[${sessionId}] #5664 render snapshot byte mismatch vs stored rows — legacy full pass this turn (next audit heals the rows)`); + renderFast = null; + const turnFull = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); + session.state = turnFull.state; + adoptContentStore(session, turnFull.contentStore); + const pmFull = applyReasoningDrop(stripKernelSummaries(turnFull.messages, turnFull.state), planReasoningDrop(renderMsgs, reasoning, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))), log, sessionId); + processedMessages = pmFull; + rebuiltMessages = await mapEgress(pmFull); + renderedCaptureStrings = rebuiltMessages.map((m) => JSON.stringify(m)); + renderedTailObjects = []; + renderedImageDelta = 0; + } + } else { + rebuiltMessages = await mapEgress(processedMessages); + renderedCaptureStrings = rebuiltMessages.map((m) => JSON.stringify(m)); + renderedTailObjects = []; + renderedImageDelta = 0; + } // ONLY the static compress prompt goes into the system message — the // system prompt is the prefix-cache anchor and must be byte-stable @@ -531,7 +674,7 @@ export async function prepareOpenai( rebuiltMessages = normalizeStrictEchoReasoning(rebuiltMessages, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed)), log, sessionId); const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined }; warnReasoningPairs(rebuiltMessages, log, sessionId); - clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: billingUpstream ?? upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, billingUpstream ?? upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; }); + clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, inputEstBaseTokens: renderFast !== null ? renderFast.rw.textCum : undefined, lastInputTokens: session.stats.lastInputTokens, lastInputTokensSource: session.stats.lastInputTokensSource, nativeWindow, headroomWindow: config.modelContextLimit, imageTokens: renderFast !== null ? renderFast.rw.imgCum + renderedImageDelta : imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin), kFactor: currentCalibrationFactor(session.stats, session.metadata?.lastModel), kOrigin: session.stats.calibratedEstimateOrigin, origin: billingUpstream ?? upstreamOrigin, ledgerFloorTokens: dshLedgerFloorTokens(session.metadata), knownOutputCeiling: resolveKnownOutputCeiling(req.headers, parsed as Record, opts.routes, billingUpstream ?? upstreamOrigin, opts.sessionHeader, modelIdOf(parsed)) }, sessionId, log, (c) => { session.stats.lastOutputClamp = { ...c, at: Date.now() }; }); // prompt_cache_retention is an OpenAI-host-only cache directive; the dsh // launcher forces PI_CACHE_RETENTION=long (for the session-id // prompt_cache_key) which makes the client also emit it. Third-party @@ -577,13 +720,17 @@ export async function prepareOpenai( // tiny payload would clobber the conversation's measurement. if (!isTitleGen) { // One tree walk shared by both calibers (see the anthropic lane). - const imageReserve = imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin); - session.stats.localInputEstimate = estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages) + // #5664: render-lane rounds bound every walk to the new tail — the + // frozen prefix basis rides the persistent per-plane cumulants (cast + // absolute, appended per round, rebaselined end-to-end each audit). + const rf = renderFast; + const imageReserve = rf !== null ? rf.rw.imgCum + renderedImageDelta : imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin); + session.stats.localInputEstimate = (rf !== null ? rf.rw.estCum + estimateCoreMessagesUpper(processedMessages) : estimateCoreMessagesUpper(processedMessages.length > 0 ? processedMessages : originalMessages)) + countSystemAndToolsTokens(openaiOutboundSystem || openaiSystemText, toolsOut) + imageReserve; // #1933 F1: billed-caliber twin (chars/4) for the k̂ learning pair — // see the anthropic-lane counterpart above. - session.stats.lastLocalTextEstimate = estimateCoreMessages(processedMessages.length > 0 ? processedMessages : originalMessages) + session.stats.lastLocalTextEstimate = (rf !== null ? rf.rw.textCum + estimateCoreMessages(processedMessages) : estimateCoreMessages(processedMessages.length > 0 ? processedMessages : originalMessages)) + countSystemAndToolsTokens(openaiOutboundSystem || openaiSystemText, toolsOut) + imageReserve; const openaiPairOrigin = billingUpstream ?? upstreamOrigin; @@ -615,6 +762,32 @@ export async function prepareOpenai( }); if (Object.keys(wpResponseHeaders).length === 0) wpResponseHeaders = undefined; } + // #5664 station 2 — ONE snapshot commit per engaged turn: steady append + // (fast rounds), arm cast / audit diff / recast with FORCED checkpoint + // (full-path rounds carry the complete captured array). Fail-soft for + // this turn only — the identity plane already committed independently. + if (renderedCaptureStrings !== null && renderCapturedSid !== null && renderCapturedCaps !== null && featureEnabled("watermarkV6Render")) { + const store = getStore(); + if (store.pfRenderAvailable()) { + const boundN = renderFast !== null && wpHit !== undefined ? renderFast.F + wpHit.windowCores.length : renderedCaptureStrings.length; + const sumsSrc = renderFast !== null ? processedMessages.slice(0, boundN) : processedMessages; + const capOp = commitRenderCapture({ + sessionId, + store, + sid: renderCapturedSid, + mode: renderFast !== null ? "steady" : "legacy", + caps: renderCapturedCaps, + rowStrings: renderedCaptureStrings, + sums: { + estCum: estimateCoreMessagesUpper(sumsSrc), + textCum: estimateCoreMessages(sumsSrc), + imgCum: renderFast !== null ? renderedImageDelta : imageReserveFor(session, "openai", rebuilt, opts, billingUpstream ?? upstreamOrigin), + }, + log, + }); + if (capOp.committed) log("debug", `[${sessionId}] #5664 render snapshot capture ${capOp.op} (${renderedCaptureStrings.length} rows)`); + } + } return { body: JSON.stringify(rebuilt), parsedBody: rebuilt, session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, judgeBaseline, protocol: "openai", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, responseExtraHeaders: wpResponseHeaders, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } From 17df343dd93204610c7c597f003c64dbf7328867 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 5 Nov 2026 00:34:49 +0800 Subject: [PATCH 03/11] test(fastpath): render-snapshot plane matrix; schema head bumps to v12 (#5664) --- tests/issue4666-migration-dedup.test.ts | 2 +- .../issue4677-v8-migration-streaming.test.ts | 12 +- tests/issue4796-gc-batched-sweep.test.ts | 6 +- tests/issue5664-render-snapshot.test.ts | 394 ++++++++++++++++++ tests/storage-ext.test.ts | 10 +- tests/storage-ingest-watermark.test.ts | 8 +- tests/storage-unified.test.ts | 4 +- 7 files changed, 415 insertions(+), 21 deletions(-) create mode 100644 tests/issue5664-render-snapshot.test.ts diff --git a/tests/issue4666-migration-dedup.test.ts b/tests/issue4666-migration-dedup.test.ts index 04ad1dda51..a3cc7caaaf 100644 --- a/tests/issue4666-migration-dedup.test.ts +++ b/tests/issue4666-migration-dedup.test.ts @@ -155,7 +155,7 @@ test("#4666 sqlite: v8 migration collapses wireIndex-split duplicates and refund check = openSqliteDatabase(path.join(dir, "index.db")).db; const ver = check.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11, "stamped to v11 (…v10 ts column + v11 fastpath-v6 identity plane)"); + assert.equal(ver.user_version, 12, "stamped to v12 (…v10 ts column + v11 fastpath-v6 identity plane + v12 render snapshot plane)"); // Exactly the lowest-seq arrival of each (msg_id, identity) group. const survivors = check diff --git a/tests/issue4677-v8-migration-streaming.test.ts b/tests/issue4677-v8-migration-streaming.test.ts index b9fa507ba7..5290291720 100644 --- a/tests/issue4677-v8-migration-streaming.test.ts +++ b/tests/issue4677-v8-migration-streaming.test.ts @@ -99,7 +99,7 @@ test("#4677 backfill sets identities and drops wireIndex-split duplicates (lowes const { from, to } = migrateSchema(db); assert.equal(from, 7); - assert.equal(to, 11); + assert.equal(to, 12); const identity = identityOfStoredContent(storedMessage("stripped", "hello")); assert.equal(identityOf(db, "s1", 1), identity); // the later duplicate row is gone and its content CAS ref handed back @@ -109,7 +109,7 @@ test("#4677 backfill sets identities and drops wireIndex-split duplicates (lowes ); assert.equal(refcountOf(db, a2), 0); assert.equal(refcountOf(db, a1), 1); - assert.equal((db.prepare("PRAGMA user_version").get() as { user_version: number }).user_version, 11); + assert.equal((db.prepare("PRAGMA user_version").get() as { user_version: number }).user_version, 12); db.close(); }); @@ -129,7 +129,7 @@ test("#4677 crash resume: sessions that already carry identities are skipped, no assert.equal(identityOf(db, "s-done", 1), "stale-planted"); assert.equal(identityOf(db, "s-pending", 1), identityOfStoredContent(storedMessage("pending", "still fresh"))); - assert.equal((db.prepare("PRAGMA user_version").get() as { user_version: number }).user_version, 11); + assert.equal((db.prepare("PRAGMA user_version").get() as { user_version: number }).user_version, 12); db.close(); }); @@ -150,7 +150,7 @@ test("#4677 rows without content bytes terminate with the ABSENT marker instead assert.equal(identity, "absent:"); // second run is a no-op — the marker is not identity = '', so the // session is not selected again - assert.equal(migrateSchema(db).from, 11); + assert.equal(migrateSchema(db).from, 12); assert.equal(identityOf(db, "s-ghost", 1), "absent:"); db.close(); }); @@ -193,7 +193,7 @@ test("#4677 empty store migrates to the schema head untouched", () => { downgradeToV7(db); const { from, to } = migrateSchema(db); assert.equal(from, 7); - assert.equal(to, 11); - assert.equal((db.prepare("PRAGMA user_version").get() as { user_version: number }).user_version, 11); + assert.equal(to, 12); + assert.equal((db.prepare("PRAGMA user_version").get() as { user_version: number }).user_version, 12); db.close(); }); diff --git a/tests/issue4796-gc-batched-sweep.test.ts b/tests/issue4796-gc-batched-sweep.test.ts index ad6aa53e8e..cf8e0ee7fe 100644 --- a/tests/issue4796-gc-batched-sweep.test.ts +++ b/tests/issue4796-gc-batched-sweep.test.ts @@ -84,7 +84,7 @@ test("#4796 v9: fresh install carries idx_content_refcount and candidate selecti seedContentRows(rawDb(store), 20_000, 1, "live"); seedContentRows(rawDb(store), 50, 0, "orphan"); const ver = rawDb(store).prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11); + assert.equal(ver.user_version, 12); const idx = rawDb(store) .prepare("SELECT name FROM sqlite_master WHERE type = 'index' AND name = 'idx_content_refcount'") .get(); @@ -118,7 +118,7 @@ test("#4796 v9: reopening a v8-era db gains the index", () => { const store = UnifiedStore.open(dbPath); try { const ver = rawDb(store).prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11); + assert.equal(ver.user_version, 12); const idx = rawDb(store) .prepare("SELECT name FROM sqlite_master WHERE type = 'index' AND name = 'idx_content_refcount'") .get(); @@ -243,7 +243,7 @@ test("#4796 e2e: v8 identity backfill releases wireIndex twins -> gc collects th try { const db2 = rawDb(reopened); const ver = db2.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11); + assert.equal(ver.user_version, 12); const twinLeft = db2 .prepare("SELECT COUNT(*) AS n FROM messages WHERE session_id = ? AND seq = ?") .get(sessId, twinSeq) as { n: number }; diff --git a/tests/issue5664-render-snapshot.test.ts b/tests/issue5664-render-snapshot.test.ts new file mode 100644 index 0000000000..5053b06a14 --- /dev/null +++ b/tests/issue5664-render-snapshot.test.ts @@ -0,0 +1,394 @@ +// #5664 — station 2/3: outbound-row render-snapshot plane for the v6 lane. +// +// Golden equation under test: on every engaged turn the emit array built as +// [cached prefix rows] + [freshly mapped tail] (fast rounds) must be BYTE- +// IDENTICAL to what the legacy full-pipeline path emits (arm / audit / +// caps-flip rollback rounds). The kernel is untouched; this lane caches +// egress bytes, not semantics — equivalence with the full pass is the +// contract. B-version compaction semantics hold throughout: client-side +// element deletion is IGNORED by the identity plane (ledger grows forward +// only), so both arms grow identically and stay byte-comparable. +// +// Harness: unit-level against a REAL sqlite sidecar (UnifiedShadow shadow +// mode), mirroring tests/fastpath-v6-msgid.test.ts; the kernel runs as a +// real createCore() instance (the same regime probe-window.mts established +// for bounded-window byte reproduction). +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import type http from "node:http"; +import { DatabaseSync } from "node:sqlite"; +import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; +import { coreToOpenai, openaiToCore, type OpenAIMessage } from "acp-kernel/wire"; +import { SessionStore, _setStoreForTest, _resetStoreForTest } from "../src/persist.ts"; +import type { SessionStore as SessionStoreType } from "../src/persist.ts"; +import type { Session } from "../src/session.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { stripKernelSummaries } from "../src/server.ts"; +import { hardenOpenaiAssistantContent, systemToUser } from "../src/util.ts"; +import { + evaluateWatermarkV6, finalizeWatermarkV6, watermarkV6DebugState, PF_SESSION_HEADER, +} from "../src/server/fastpath-v6.ts"; +import { PF_TAIL_IDS_HEADER } from "../src/server/fastpath.ts"; +import type { V6Outcome } from "../src/server/fastpath-v6.ts"; +import { + bumpAuditTick, commitRenderCapture, diffRowLists, renderPlaneGate, spliceOutboundRows, verifyRenderWindow, + watermarkV6RenderDebugState, _resetWatermarkV6RenderTestState, + type RenderCaps, +} from "../src/server/fastpath-v6-render.ts"; + +const ENV_V6 = "BILI_ROLLOUT_WATERMARK_V6_MSG_ID"; +const ENV_RENDER = "BILI_ROLLOUT_WATERMARK_V6_RENDER"; +const MODEL_LIMIT = 200_000; + +type AgentState = ReturnType; + +interface Msg { role: string; content: string } +function makeBody(messages: Msg[]): string { return JSON.stringify({ model: "m", messages }); } + +type LogSink = { lines: { level: string; msg: string }[] } & ((level: string, msg: string) => void); +function makeLog(): LogSink { + const l = { lines: [] as { level: string; msg: string }[] }; + return Object.assign((level: string, msg: string) => { l.lines.push({ level, msg }); }, { lines: l.lines }); +} + +/** Post-kernel → wire transform chain (production shape minus the image + * rescale pass — identity for text-only content, no multimodal parts here). */ +function mapEgress(pms: CoreMessage[]): OpenAIMessage[] { + return hardenOpenaiAssistantContent(coreToOpenai(systemToUser(pms))) as unknown as OpenAIMessage[]; +} + +function capsFor(strategy: string): RenderCaps { + return { v: 1, model: "m", strategy, maxDim: 1_600, toolArmed: false, droppable: false, multimodal: false }; +} + +/** Shallow-adopt a returned state into a stable object identity (the session + * holder keeps referencing the same object across turns). */ +function adoptState(dst: AgentState, src: AgentState): void { + for (const k of Object.keys(src) as (keyof AgentState)[]) (dst as unknown as Record)[k] = src[k]; + // fields cleared on src but present on dst: none expected; state evolves additively +} + +interface H { dir: string; store: SessionStoreType; log: LogSink; cleanup: () => void } +function makeH(tag: string): H { + process.env[ENV_V6] = "1"; + process.env[ENV_RENDER] = "1"; + _resetStoreForTest(); + _resetWatermarkV6RenderTestState(); + const dir = mkdtempSync(path.join(os.tmpdir(), `rsn-${tag}-`)); + const shadow = UnifiedShadow.maybeOpen(dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as never); + assert.ok(shadow, "shadow sidecar must open in the hermetic dir"); + const store = new SessionStore({ dir: path.join(dir, "sessions"), enabled: true, shadow }); + _setStoreForTest(store); + const log = makeLog(); + return { dir, store, log, cleanup: () => { _resetStoreForTest(); _resetWatermarkV6RenderTestState(); rmSync(dir, { recursive: true, force: true }); } }; +} + +/** Arm turns carry no hit (first registration) — fall back to the + * full-parse oracle, which the v6 contract (§8-1) pins byte-equal to the + * assembled view. */ +function viewOf(outcome: V6Outcome, bodyText: string): CoreMessage[] { + return outcome.hit !== null ? [...outcome.hit.view] : (openaiToCore(JSON.parse(bodyText) as never) as unknown as { msgs: CoreMessage[] }).msgs; +} + +/** One v6 identity-plane turn, SPLIT like production + * (prepare-openai: evaluate early → pipeline/egress/capture → finalize LAST; + * finalize is the only point that advances worlds.maxPos / pf_rows). */ +function v6Eval(h: H, sessId: string, bodyText: string, idList: string[], echoSid?: string): { outcome: V6Outcome; sid: string | undefined; fin: () => void } { + const headers: Record = { "x-bili-plugin-conversation": "carrier" }; + if (idList.length > 0) headers[PF_TAIL_IDS_HEADER] = idList.map(encodeURIComponent).join(","); + if (echoSid !== undefined) headers[PF_SESSION_HEADER] = echoSid; + const req = { headers } as unknown as http.IncomingMessage; + const session = { id: sessId } as unknown as Session; + const outcome = evaluateWatermarkV6(bodyText, req, session, { pluginMode: true, titleGen: false, model: "m" }, h.log); + assert.ok(outcome !== null, `v6 must engage (log=${JSON.stringify(h.log.lines.slice(-4))})`); + return { + outcome, + sid: outcome.armed?.sid ?? echoSid, + fin: () => { finalizeWatermarkV6(session, outcome, { bodyText }, h.log); }, + }; +} + +// --------------------------------------------------------------------------- +// Unit tier: pure plane mechanics. +// --------------------------------------------------------------------------- + +test("unit: diffRowLists verdicts", () => { + assert.equal(diffRowLists([], ["a"]), "append"); + assert.equal(diffRowLists(["a", "b"], ["a", "b"]), "equal"); + assert.equal(diffRowLists(["a", "b"], ["a", "b", "c"]), "append"); + assert.equal(diffRowLists(["a", "b"], ["a", "X"]), "recast"); // middle mutation + assert.equal(diffRowLists(["a", "b", "c"], ["a"]), "recast"); // shrink +}); + +test("unit: cast → steady appends → hydrate-from-disk fold survives mirror loss", (t) => { + const h = makeH("fold"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-fold", b1, ["id0"]); + assert.equal(r1.outcome.decision, "arm"); + r1.fin(); + const sid = r1.sid!; + const caps = capsFor("text-only"); + let op = commitRenderCapture({ sessionId: "s-fold", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"i":0}', '{"i":1}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }); + assert.equal(op.op, "cast"); + op = commitRenderCapture({ sessionId: "s-fold", store: h.store, sid, mode: "steady", caps, rowStrings: ['{"i":2}'], sums: { estCum: 1, textCum: 1, imgCum: 0 } }); + assert.equal(op.op, "append"); + op = commitRenderCapture({ sessionId: "s-fold", store: h.store, sid, mode: "steady", caps, rowStrings: ['{"i":3}'], sums: { estCum: 1, textCum: 1, imgCum: 0 } }); + assert.equal(op.op, "append"); + assert.equal(h.store.pfRenderRows(sid).length, 4); + assert.equal(h.store.pfRenderCkptIntact(sid), true); + + // Kill every in-memory mirror — the next gate MUST rebuild from disk + // via pfRenderMeta folding (ckpt rowCount + post-ckpt append log). + // maxPos is synthetic here: prod wiring always feeds the live world + // geometry (rows and plane grow together, one row per element), so + // alignment holds; this unit isolates the FOLD mechanics, so feed the + // geometry matching the four stored rows. + _resetWatermarkV6RenderTestState(); + const g = renderPlaneGate({ + sessionId: "s-fold", session: { id: "s-fold", state: createInitialState() } as unknown as Session, + store: h.store, engagedHit: true, armedTurn: false, isTitleGen: false, sid, + maxPos: 3, tokenCount: 50, modelContextLimit: MODEL_LIMIT, caps, + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, + }); + assert.ok(g.ok, `expected hydrated green gate, got ${JSON.stringify(g)}`); + if (g.ok) { + assert.equal(g.rw.rows.length, 4, "disk fold must reconstruct all four rows"); + assert.deepEqual(g.rw.rows.slice(-2), ['{"i":2}', '{"i":3}'], "appended tail must survive the fold"); + } + } finally { h.cleanup(); } +}); + +test("gate: red reasons are a stable vocabulary", (t) => { + const h = makeH("gate"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-gate", b1, ["id0"]); + r1.fin(); + const sid = r1.sid!; + const caps = capsFor("text-only"); + const cast = commitRenderCapture({ sessionId: "s-gate", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"a":1}'], sums: { estCum: 1, textCum: 1, imgCum: 0 } }); + assert.equal(cast.op, "cast"); + const geom = watermarkV6DebugState("s-gate").worlds[sid]; + const base = { + sessionId: "s-gate", session: { id: "s-gate", state: createInitialState() } as unknown as Session, + store: h.store, engagedHit: true, armedTurn: false, isTitleGen: false, sid, + maxPos: geom!.maxPos, tokenCount: 500, modelContextLimit: MODEL_LIMIT, caps, + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, + }; + const g0 = renderPlaneGate(base); + assert.equal(g0.ok, true, `expected green gate, got ${JSON.stringify(g0)}`); + assert.deepEqual(renderPlaneGate({ ...base, engagedHit: false }), { ok: false, reason: "not-engaged" }); + assert.deepEqual(renderPlaneGate({ ...base, armedTurn: true }), { ok: false, reason: "arm-turn" }); + assert.deepEqual(renderPlaneGate({ ...base, isTitleGen: true }), { ok: false, reason: "title-gen" }); + assert.deepEqual(renderPlaneGate({ ...base, tokenCount: Math.ceil(0.9 * MODEL_LIMIT) }), { ok: false, reason: "pressure" }); + assert.deepEqual(renderPlaneGate({ ...base, maxPos: -1 }), { ok: false, reason: "no-plane-geometry" }); + assert.deepEqual(renderPlaneGate({ ...base, caps: capsFor("none") }), { ok: false, reason: "caps-mismatch" }); + // audit cadence: PF_RENDER_AUDIT_EVERY default 8 — ticks land on 8 ⇒ red, 9 ⇒ green + for (let i = 0; i < 8; i++) bumpAuditTick(sid); + assert.deepEqual(renderPlaneGate(base), { ok: false, reason: "audit-due" }); + bumpAuditTick(sid); + assert.equal(renderPlaneGate(base).ok, true); + } finally { h.cleanup(); } +}); + +test("unit: S4 checksum tamper kills the mirror (hydrate-failed, loudly logged)", (t) => { + const h = makeH("s4"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-s4", b1, ["id0"]); + r1.fin(); + const sid = r1.sid!; + const caps = capsFor("text-only"); + const cast = commitRenderCapture({ sessionId: "s-s4", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"a":1}', '{"a":2}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }); + assert.equal(cast.op, "cast"); + assert.equal(h.store.pfRenderCkptIntact(sid), true); + // Simulate silent corruption of the checkpoint checksum. + const db = new DatabaseSync(path.join(h.dir, "index.db")); + db.prepare(`UPDATE pf_render_ckpt SET checksum = 'deadbeef' WHERE sid = ?`).run(sid); + db.close(); + assert.equal(h.store.pfRenderCkptIntact(sid), false); + _resetWatermarkV6RenderTestState(); // memory copies gone ⇒ gate must hydrate + const g = renderPlaneGate({ + sessionId: "s-s4", session: { id: "s-s4", state: createInitialState() } as unknown as Session, + store: h.store, engagedHit: true, armedTurn: false, isTitleGen: false, sid, + maxPos: 0, tokenCount: 10, modelContextLimit: MODEL_LIMIT, caps, + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, + }); + assert.deepEqual(g, { ok: false, reason: "hydrate-failed" }); + } finally { h.cleanup(); } +}); + +// --------------------------------------------------------------------------- +// Golden tier: FAST emit == legacy emit at EVERY engaged turn, byte-for-byte, +// across arm → steady fast rounds → client compaction (turn 8) → strategy +// recast (turn 10) → audit cadence → restart-hydration continuation. +// --------------------------------------------------------------------------- + +test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end", async (t) => { + const core = createCore({}); + const cfg = defaultConfig(MODEL_LIMIT); + + // ---- conversation script ------------------------------------------------ + // 16 elements e_0..e_15 arrive one per turn. From turn 8 the CLIENT body + // drops e_1/e_3 (client-side compaction) — the lane ignores the drop + // (ledger grows forward only), views and rows keep growing, and the + // golden equation must still hold element-stable (ids follow elements, + // never positions). Strategy flips text-only→none at turn 10 ⇒ caps flip + // ⇒ forced legacy recast round, then steady on the new byte form. + const ELEMENTS: Msg[] = Array.from({ length: 16 }, (_, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content: `content number ${i}` })); + const STABLE_ID = (i: number) => `eid-${i}`; + const clientBody = (tn: number): Msg[] => { + let els = ELEMENTS.slice(0, tn); + if (tn >= 8) els = els.filter((_, i) => i !== 1 && i !== 3); + return els; + }; + const idsOf = (els: Msg[]) => els.map((el) => STABLE_ID(ELEMENTS.indexOf(el))); + const strategyOf = (tn: number) => (tn < 10 ? "text-only" : "none"); + + // ---- phase 1: arm A — pure legacy full path over its own store ---------- + const hA = makeH("gold-a"); + let stateA: AgentState = createInitialState(); + let sidA: string | undefined; + const goldens: { rows: string[]; decision: string }[] = []; + for (let tn = 1; tn <= 16; tn++) { + const body = clientBody(tn); + const bodyText = makeBody(body); + const v = v6Eval(hA, "s-gold-a", bodyText, idsOf(body), sidA); + if (v.sid !== undefined) sidA = v.sid; + const viewA = viewOf(v.outcome, bodyText); + const tA = core.processTurn({ messages: viewA, state: stateA, config: cfg, tokenCount: 100 + viewA.length * 37, renderTags: strategyOf(tn) }); + adoptState(stateA, tA.state as unknown as AgentState); + goldens.push({ rows: mapEgress(stripKernelSummaries(tA.messages, tA.state as never)).map((m) => JSON.stringify(m)), decision: v.outcome.decision }); + v.fin(); + } + hA.cleanup(); + + // ---- phase 2: arm B — render lane ON, replaying the identical script ---- + const hB = makeH("gold-b"); + const stateB: AgentState = createInitialState(); + const sessB = { id: "s-gold-b", state: stateB } as unknown as Session; + let sidB: string | undefined; + let fastRounds = 0, castOps = 0, recastOps = 0, legacyRounds = 0; + for (let tn = 1; tn <= 16; tn++) { + const strategy = strategyOf(tn); + const body = clientBody(tn); + const bodyText = makeBody(body); + const v = v6Eval(hB, "s-gold-b", bodyText, idsOf(body), sidB); + if (v.sid !== undefined) sidB = v.sid; + const hit = v.outcome.hit; + const tokenCount = 100 + (hit !== null ? hit.view.length : body.length); + const caps = capsFor(strategy); + // Arm turn: no world exists pre-finalize ⇒ prod gate block skipped. + const geom = sidB !== undefined ? watermarkV6DebugState("s-gold-b").worlds[sidB] : undefined; + const gate = renderPlaneGate({ + sessionId: "s-gold-b", session: sessB, store: hB.store, engagedHit: hit !== null && v.outcome.armed === undefined, + armedTurn: v.outcome.armed !== undefined, isTitleGen: false, sid: sidB ?? "unarmed", + maxPos: geom?.maxPos ?? -1, tokenCount, modelContextLimit: MODEL_LIMIT, caps, + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, + log: hB.log, + }); + let rebuilt: OpenAIMessage[]; + let captureStrings: string[]; + let mode: "steady" | "legacy"; + if (gate.ok) { + const sidNow = sidB; + assert.ok(sidNow !== undefined, "green gate implies armed"); + assert.ok(geom, "green gate implies world geometry"); + const maxPos = geom.maxPos; + bumpAuditTick(sidNow); + const rw = gate.rw; const F = gate.F; + // bounded window pass (mirrors prepare-openai.ts slot exactly) + const posFrom = maxPos - F + 1; + const winJson = hB.store.pfHydrateWindow(sidNow, posFrom, maxPos); + const frozen: CoreMessage[] = []; + for (let p = posFrom; p <= maxPos; p++) { + const raw = winJson.get(p); + assert.notEqual(raw, undefined, `window-core gap at pos ${p}`); + // pf_rows.core_json holds a PLAIN CORES ARRAY per pos (an + // element may carry several cores) — flatten + re-stamp, + // mirroring fastpath-v6's own disk loader. + const arr = JSON.parse(raw!) as CoreMessage[]; + for (const c of arr) frozen.push(Object.assign(c, { wireIndex: p })); + } + const scratch = structuredClone(stateB); + const tW = core.processTurn({ messages: [...frozen, ...(v.outcome.hit?.windowCores ?? [])], state: scratch, config: cfg, tokenCount, renderTags: strategy }); + const liveRefs = stateB.messageRefs as unknown as { byRaw: Record; byRef: Record }; + const srcRefs = (scratch as AgentState).messageRefs as unknown as { byRaw: Record; byRef: Record }; + for (const [k, val] of Object.entries(srcRefs.byRaw)) if (!(k in liveRefs.byRaw)) liveRefs.byRaw[k] = val; + for (const [k, val] of Object.entries(srcRefs.byRef)) if (!(k in liveRefs.byRef)) liveRefs.byRef[k] = val; + for (const r of (scratch as AgentState).rules ?? []) if (!(stateB.rules ?? []).some((x) => x.id === r.id)) (stateB.rules ??= []).push(r); + stateB.nextRuleId = Math.max(stateB.nextRuleId ?? 1, (scratch as AgentState).nextRuleId ?? 1); + const pmW = stripKernelSummaries(tW.messages, tW.state as never); + + const mappedAll = mapEgress(pmW); + const committedTail = mappedAll.slice(0, Math.min(F + (v.outcome.hit?.windowCores.length ?? 0), mappedAll.length)); + const vv = verifyRenderWindow(committedTail.slice(0, F).map((m) => JSON.stringify(m)), rw, F); + if (!vv) { + for (let k = 0; k < F; k++) { + const a = JSON.stringify(committedTail[k]); const b = rw.rows[rw.rows.length - F + k]; + + } + } + assert.equal(vv, true, `turn ${tn}: window verify failed — tail bytes drifted vs stored rows`); + rebuilt = spliceOutboundRows(rw, F, committedTail); + captureStrings = [...rw.rows.slice(0, rw.rows.length - F), ...committedTail.map((m) => JSON.stringify(m))]; + mode = "steady"; + fastRounds++; + } else { + // legacy full path over the engaged view (production shape) + const tF = core.processTurn({ messages: hit !== null ? [...hit.view] : (openaiToCore(JSON.parse(bodyText) as never) as unknown as { msgs: CoreMessage[] }).msgs, state: stateB, config: cfg, tokenCount, renderTags: strategy }); + adoptState(stateB, tF.state as unknown as AgentState); + const mappedAll = mapEgress(stripKernelSummaries(tF.messages, tF.state as never)); + rebuilt = mappedAll; + captureStrings = mappedAll.map((m) => JSON.stringify(m)); + mode = "legacy"; + legacyRounds++; + } + const op = commitRenderCapture({ + sessionId: "s-gold-b", store: hB.store, sid: sidB!, mode, caps, rowStrings: captureStrings, + sums: { estCum: 0, textCum: 0, imgCum: 0 }, + }); + if (op.committed) { + if (op.op === "cast") castOps++; + else if (op.op === "recast") recastOps++; + } + // THE golden equation: lane emit == legacy oracle, byte for byte. + assert.deepEqual(rebuilt.map((m) => JSON.stringify(m)), goldens[tn - 1].rows, + `turn ${tn} (${mode}${gate.ok ? "" : `, reason=${gate.reason}`}): lane emit diverged from legacy oracle`); + assert.equal(stateB.blocks.length, 0, "assumption pin: no folds materialize in this synthetic conv"); + v.fin(); // plane advances AFTER capture — same order as prepare-openai + } + assert.equal(castOps, 1, "exactly one cast (the arm turn)"); + assert.ok(fastRounds >= 6, `expected several fast rounds, got ${fastRounds} (audit too aggressive?)`); + assert.ok(recastOps >= 1, "strategy flip at turn 10 must force ≥1 recast"); + assert.ok(legacyRounds >= 2, `arm + audit/caps-flip rounds must ride legacy (got ${legacyRounds})`); + // S4 truth intact at the end; mirror agrees with the last golden shape + assert.equal(hB.store.pfRenderCkptIntact(sidB!), true); + const dbgLhs = watermarkV6RenderDebugState("s-gold-b"); + assert.ok(Array.isArray(dbgLhs.sids) && dbgLhs.sids.length > 0, "render debug state must list the mirror"); + const lhsRows = dbgLhs.sids[dbgLhs.sids.length - 1].rows; + const rhsRows = goldens[goldens.length - 1].rows.length; + assert.equal(lhsRows, rhsRows, "mirror row count must equal last golden row count"); + + // ---- phase 3: restart hydration — mirrors wiped, disk must continue ----- + _resetWatermarkV6RenderTestState(); + const extraEl: Msg = { role: "user", content: "content number 16" }; + ELEMENTS.push(extraEl); + const bodyT17 = clientBody(16).concat([extraEl]); + const v17 = v6Eval(hB, "s-gold-b", makeBody(bodyT17), idsOf(bodyT17), sidB); + const g17 = renderPlaneGate({ + sessionId: "s-gold-b", session: sessB, store: hB.store, engagedHit: true, armedTurn: false, + isTitleGen: false, sid: sidB!, maxPos: watermarkV6DebugState("s-gold-b").worlds[sidB!]!.maxPos, + tokenCount: 100, modelContextLimit: MODEL_LIMIT, caps: capsFor("none"), + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, log: hB.log, + }); + assert.ok(g17.ok === true || g17.reason === "audit-due", + `post-restart gate must hydrate from disk (got ${JSON.stringify(g17)})`); + if (g17.ok) assert.equal(g17.rw.rows.length, goldens.at(-1)!.rows.length, "hydrated row count must match the pre-restate capture"); + v17.fin(); + hB.cleanup(); +}); diff --git a/tests/storage-ext.test.ts b/tests/storage-ext.test.ts index 70019f0352..1cb4778979 100644 --- a/tests/storage-ext.test.ts +++ b/tests/storage-ext.test.ts @@ -155,11 +155,11 @@ test("guards reject hostile keys and oversize values", async () => { } }); -test("v3 databases migrate to v10 in place, idempotently", async () => { +test("v3 databases migrate to v12 in place, idempotently", async () => { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ext-migrate-")); try { const dbPath = path.join(tmp, "index.db"); - // Build a current (v10) database, then roll the extension tables and + // Build a current (v12) database, then roll the extension tables and // version marker back to the v3 shape — exactly what an upgraded // production lane's file looks like to the new code. const fresh = UnifiedStore.open(dbPath); @@ -174,17 +174,17 @@ test("v3 databases migrate to v10 in place, idempotently", async () => { const migrated = UnifiedStore.open(dbPath); try { const ver = (migrated as unknown as { db: { prepare(q: string): { get(): unknown } } }).db.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11, "open() runs migrateSchema: v3 → v11"); + assert.equal(ver.user_version, 12, "open() runs migrateSchema: v3 → v12"); migrated.setKv("probe", "alive", true); assert.deepEqual(migrated.getKv("probe", "alive"), true); assert.deepEqual(migrated.listSessionIds(), [], "existing v3 content untouched"); } finally { migrated.close(); } - // Reopening a v10 database is a no-op (idempotent migration). + // Reopening a v12 database is a no-op (idempotent migration). const again = UnifiedStore.open(dbPath); const ver2 = (again as unknown as { db: { prepare(q: string): { get(): unknown } } }).db.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver2.user_version, 11); + assert.equal(ver2.user_version, 12); again.close(); } finally { fs.rmSync(tmp, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); diff --git a/tests/storage-ingest-watermark.test.ts b/tests/storage-ingest-watermark.test.ts index 6ab6229b19..bf0cf9bdf0 100644 --- a/tests/storage-ingest-watermark.test.ts +++ b/tests/storage-ingest-watermark.test.ts @@ -591,7 +591,7 @@ test("watermark: sampled verification still catches drift through the delta path } }); -test("watermark: v4 database migrates to v11 additively; newer user_version no-ops", async () => { +test("watermark: v4 database migrates to v12 additively; newer user_version no-ops", async () => { const dir = tmpdir("bili-wm-migrate-"); try { const dbPath = path.join(dir, "index.db"); @@ -620,7 +620,7 @@ test("watermark: v4 database migrates to v11 additively; newer user_version no-o const store = UnifiedStore.open(dbPath); try { const ver = (store as unknown as { db: { prepare(q: string): { get(): unknown } } }).db.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11, "open() migrates v4 → v11"); + assert.equal(ver.user_version, 12, "open() migrates v4 → v12"); const row = store.getSessionRow("v4-sess"); assert.ok(row, "legacy session row survives the migration"); assert.equal(row!.lastIngestedSeq, null, "legacy rows read back with a NULL watermark (≡ full ingest)"); @@ -653,12 +653,12 @@ test("watermark: v4 database migrates to v11 additively; newer user_version no-o // probe version sits AT the current schema head so the no-op branch // still exercises after future bumps. const fwd = new DatabaseSync(dbPath); - fwd.exec("PRAGMA user_version = 11"); + fwd.exec("PRAGMA user_version = 12"); fwd.close(); const older = UnifiedStore.open(dbPath); try { const ver2 = (older as unknown as { db: { prepare(q: string): { get(): unknown } } }).db.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver2.user_version, 11, "newer user_version is left untouched"); + assert.equal(ver2.user_version, 12, "newer user_version is left untouched"); assert.ok(older.getSessionRow("v4-sess"), "rows stay readable"); } finally { older.close(); diff --git a/tests/storage-unified.test.ts b/tests/storage-unified.test.ts index 644dbe298a..04350a3f36 100644 --- a/tests/storage-unified.test.ts +++ b/tests/storage-unified.test.ts @@ -801,7 +801,7 @@ test("schema v2: dev-window v1 database upgrades in place (identity columns adde assert.equal(row!.namespace, "proxy", "namespace backfills with its constant default"); assert.equal(row!.origin, null, "origin backfills NULL"); const ver = (store as unknown as { db: { prepare(q: string): { get(): unknown } } }).db.prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11, "schema version stamped to 11 (… v10 immutable message ts column + v11 fastpath-v6 identity plane tables pf_rows/pf_log/pf_ckpt)"); + assert.equal(ver.user_version, 12, "schema version stamped to 12 (… v10 immutable message ts column + v11 fastpath-v6 identity plane + v12 render snapshot plane)"); // v3 backfill: every pre-existing sessions row claims its // (namespace, conversation_id) identity binding. const binding = (store as unknown as { lookupConversation(ns: string, id: string): string | null }).lookupConversation("proxy", "dev-window-1"); @@ -863,7 +863,7 @@ test("schema v10: v9→v10 migration adds messages.ts without backfill; idempote try { const ver = (reopened as unknown as { db: { prepare(q: string): { get(): unknown } } }).db .prepare("PRAGMA user_version").get() as { user_version: number }; - assert.equal(ver.user_version, 11, "v9 library migrates to 11"); + assert.equal(ver.user_version, 12, "v9 library migrates to 12"); const rows = reopened.getMessages("v10-mig-1") as { seq: number; ts?: number | null }[]; assert.equal(rows.length, 1, "row survives the migration"); assert.ok(rows[0].ts === null || rows[0].ts === undefined, "pre-existing row keeps NULL (never backfilled)"); From 16d5944bae1997aa3b1efab3438141e830923908 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Thu, 5 Nov 2026 01:07:02 +0800 Subject: [PATCH 04/11] =?UTF-8?q?fix(fastpath):=20review=20round=201=20?= =?UTF-8?q?=E2=80=94=20audit=20consumption,=20sums=20algebra,=20steady=20t?= =?UTF-8?q?ail-only=20rows=20(#5664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review pass on the self-authored lane surfaced four state-maintenance bugs on the green path plus a class of test-blind spots; all fixed here with regression pins: 1. steady capture appended the FULL row list (production shape), not just this turn's new rows: the pos column shifted forward every green round, doubling the table on disk and in the mirror; only the per-round recast churn (masking an audit stall) kept the byte equation green. Capture now sends committedTail.slice(F) — exactly the appended suffix. 2. Cumulative estCum/textCum/imgCum re-counted the F frozen window rows every green round (linear ledger drift ⇒ shrinking max_tokens clamp and polluted k calibration over long sessions). Sums and the image-reserve delta now cover only this turn's new elements; legacy rounds still carry absolute totals. 3. The audit obligation was consumed only by cast/recast: after N green rounds tick%N===0 stayed true forever and the lane dead-locked back to full legacy until an unrelated caps flip. Any legacy full-capture round now zeroes the tick (it IS the audit). 4. Hygiene from the same pass: identity faults rethrow through the window- pass catch instead of retry-passing; CCR content-store adoption no longer defers past fast rounds (dangling refs); splice shallow-clones mirror rows so downstream cannot alias-mutate next round's verify baseline; NUL-bearing rows refuse persistence loudly-once (node:sqlite truncates bound strings at \0 silently) and hydrate parse failures log error instead of failing soft-silent. Test tier extended: per-round render-table↔identity-ledger alignment pin, two-audit-wave fast-round threshold (catches the stall), submitted-volume and cumulative-sum algebra pins, session-cascade wipe of the three render tables, and a second-sidecar-open 12→12 migration idempotency no-op. --- src/server/fastpath-v6-render.ts | 52 +++++++++++++++-- src/server/prepare-openai.ts | 30 ++++++++-- tests/issue5664-render-snapshot.test.ts | 75 ++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 21 deletions(-) diff --git a/src/server/fastpath-v6-render.ts b/src/server/fastpath-v6-render.ts index 6b1cfa8d01..9acc031990 100644 --- a/src/server/fastpath-v6-render.ts +++ b/src/server/fastpath-v6-render.ts @@ -109,7 +109,11 @@ export interface RenderWorld { textCum: number; /** Cumulative image-reserve contribution of frozen rows. */ imgCum: number; - /** Engaged-turn tick — % PF_RENDER_AUDIT_EVERY === 0 ⇒ audit due. */ + /** Green (fast-served) engaged-turn tick — % PF_RENDER_AUDIT_EVERY === 0 + * ⇒ audit due. Consumed (zeroed) by ANY legacy full-capture round served, + * which IS the audit (the full byte path ran to completion). + * Invariant: a red-for-audit-due turn always lands in a legacy capture, + * so the obligation cannot stall the lane after N ticks. */ auditTick: number; } @@ -207,11 +211,18 @@ function hydrateMirror(store: SessionStore, sid: string, log?: LogFn): RenderWor const rowsParsed: unknown[] = new Array(rowsRaw.length); for (let i = 0; i < rowsRaw.length; i++) { const rj = rowsRaw[i].rowJson; - if (rj === null || rowsRaw[i].pos !== i) return null; // tombstone/gap = inconsistent + if (rj === null || rowsRaw[i].pos !== i) { + // Row table inconsistent on disk (tombstone where a row must + // be, or a gap): judge the plane dead LOUDLY — same discipline + // as an S4 checksum mismatch, never a silent fallback. + log?.("error", `[${sid}] #5664 render plane judged dead: row-table inconsistency at pos ${rowsRaw[i].pos} (expected contiguous ${i})`); + return null; + } rows[i] = rj; try { rowsParsed[i] = JSON.parse(rj); } catch { + log?.("error", `[${sid}] #5664 render plane judged dead: row parse failed at pos ${i}`); return null; } } @@ -314,8 +325,10 @@ export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { return { ok: true, rw, F }; } -/** Called AFTER the decision is consumed (keeps the audit cadence honest — - * a gated-off-for-pressure turn still counts as engaged). */ +/** Bumped once per GREEN round (prepare-openai gate block). The obligation is + * consumed by any legacy full-capture round (commitRenderCapture zeroes it): + * cadence = "one full pass per PF_RENDER_AUDIT_EVERY fast rounds", red turns + * included among those full passes. */ export function bumpAuditTick(sid: string): void { const rw = mirrors.get(sid); if (rw) rw.auditTick += 1; @@ -348,8 +361,13 @@ export function spliceOutboundRows(rw: RenderWorld, F: number, tail: T[]): T[ const out: T[] = []; // rowsParsed entries are JSON.parse of THIS plane's previously captured // rows — exactly T elements by construction (capture stores what egress - // emitted, and egress emitted T). - out.push(...(rw.rowsParsed.slice(0, prefixLen) as T[])); + // emitted, and egress emitted T). Shallow-clone each cached object: these + // doubles as next round's verify baseline, so no downstream consumer may + // alias-mutate the mirror cache (review P2-5). + for (const x of rw.rowsParsed.slice(0, prefixLen)) { + const y: T = x !== null && typeof x === "object" ? ({ ...(x as object) } as T) : (x as T); + out.push(y); + } out.push(...tail); return out; } @@ -391,8 +409,24 @@ export function diffRowLists(prev: string[], cur: string[]): "equal" | "append" return "recast"; } +/** sids whose row payload carried a literal NUL and were therefore refused + * persistence (node:sqlite silently TRUNCATES bound strings at the first \0 + * — storing would corrupt the row with S4 unable to see it, since the + * checksum only covers checkpoint meta). Loud-once, then fail-soft per turn: + * the in-memory mirror keeps serving this process's lifetime; disk simply + * stops advancing until restart/caps-flip recast is possible again. */ +const nulRejectedSids = new Set(); + export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op: "none" | "append" | "cast" | "recast" } { const { store, sid, caps, log } = args; + const hasNul = args.rowStrings.some((rj) => rj.includes("\u0000")); + if (hasNul) { + if (!nulRejectedSids.has(sid)) { + nulRejectedSids.add(sid); + log?.("error", `[${sid}] #5664 render row carries a literal NUL (node:sqlite truncates it silently) — plane persistence disabled for this sid`); + } + return { committed: false, op: "none" }; + } let rw = mirrors.get(sid); if (rw === undefined && !mirrors.has(sid)) { const hydrated = hydrateMirror(store, sid, log); @@ -441,6 +475,11 @@ export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op touchMirror(rw); return { committed: true, op: "append" }; } + // A full-path round IS the audit regardless of verdict (the legacy byte + // path ran to completion) — consume the cadence obligation here, or a + // clean long run would dead-lock on "audit-due" after N ticks (only + // cast/recast zeroed it before this fix). + if (args.mode === "legacy") rw.auditTick = 0; // legacy full-capture: diff against truth, minimal commit. const verdict = diffRowLists(rw.rows, args.rowStrings); if (verdict === "equal") { @@ -528,5 +567,6 @@ export function watermarkV6RenderDebugState(sessionId: string): { sids: RenderDe export function _resetWatermarkV6RenderTestState(): void { mirrors.clear(); sessionSids.clear(); + nulRejectedSids.clear(); mirrorBytes = 0; } diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index c2e7285591..23f22f8079 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -41,7 +41,7 @@ import { injectOpenaiTool, injectTool } from "./inject.js"; import { nudgeOn, resolveLaneDecisions, surfaceOffDetail } from "./lane-decisions.js"; import { featureEnabled } from "../rollout.js"; import { evaluateWatermarkFastpath, finalizeWatermarkTurn, type FastpathHit, type FastpathOutcome } from "./fastpath.js"; -import { evaluateWatermarkV6, finalizeWatermarkV6, PF_SESSION_HEADER, watermarkV6DebugState, type V6Outcome } from "./fastpath-v6.js"; +import { evaluateWatermarkV6, finalizeWatermarkV6, FastpathV6IdentityFault, PF_SESSION_HEADER, watermarkV6DebugState, type V6Outcome } from "./fastpath-v6.js"; import { bumpAuditTick, commitRenderCapture, renderPlaneGate, spliceOutboundRows, verifyRenderWindow, type RenderCaps, type RenderGateDecision, type RenderWorld } from "./fastpath-v6-render.js"; import { getStore } from "../persist.js"; import { collectRequestIds, probeRequestMembership } from "../request-membership.js"; @@ -382,6 +382,10 @@ export async function prepareOpenai( session.state.nextRuleId = Math.max(session.state.nextRuleId ?? 1, scratch.nextRuleId ?? 1); return t; } catch (err) { + // Identity-plane faults are deterministic 400s — a + // retry-pass over them would turn fail-fast into + // retry-pass (review P2-6); everything else falls back. + if (err instanceof FastpathV6IdentityFault) throw err; log("warn", `[${sessionId}] #5664 render window pass failed (${String(err)}) — legacy full pass this turn`); renderFast = null; } @@ -390,8 +394,11 @@ export async function prepareOpenai( })(); if (renderFast === null) { session.state = turn.state; - adoptContentStore(session, turn.contentStore); } + // Content-store adoption is cheap (no-op without new entries) and must + // NOT defer to the next full pass: CCR refs minted on a fast round are + // already referenced by the outbound placeholder (review P2-4). + adoptContentStore(session, turn.contentStore); // #4528: split the kernel pass from its post-processing scan passes so // each is a bounded synchronous stretch rather than one long block. await yieldToEventLoop(); @@ -522,7 +529,11 @@ export async function prepareOpenai( const rw = renderFast.rw; const wpWindowLen = wpHit?.windowCores.length ?? 0; const committedTail = mappedAll.slice(0, Math.min(renderFast.F + wpWindowLen, mappedAll.length)); - renderedImageDelta = imageReserveFor(session, "openai", { model: parsed.model, messages: committedTail } as unknown as OpenAIRequestBody, opts, billingUpstream ?? upstreamOrigin); + // Image reserve DELTA covers only this turn's NEW rows — the F + // frozen-window rows' reserve already sits in rw.imgCum; counting + // them again here drifts the ledger by a whole window each green + // round (review P1-2). slice(F) aligns with the captured tail. + renderedImageDelta = imageReserveFor(session, "openai", { model: parsed.model, messages: committedTail.slice(renderFast.F) } as unknown as OpenAIRequestBody, opts, billingUpstream ?? upstreamOrigin); if (verifyRenderWindow(committedTail.slice(0, renderFast.F).map((m) => JSON.stringify(m)), rw, renderFast.F)) { rebuiltMessages = spliceOutboundRows(rw, renderFast.F, committedTail); renderedCaptureStrings = [...rw.rows.slice(0, rw.rows.length - renderFast.F), ...committedTail.map((m) => JSON.stringify(m))]; @@ -769,15 +780,22 @@ export async function prepareOpenai( if (renderedCaptureStrings !== null && renderCapturedSid !== null && renderCapturedCaps !== null && featureEnabled("watermarkV6Render")) { const store = getStore(); if (store.pfRenderAvailable()) { - const boundN = renderFast !== null && wpHit !== undefined ? renderFast.F + wpHit.windowCores.length : renderedCaptureStrings.length; - const sumsSrc = renderFast !== null ? processedMessages.slice(0, boundN) : processedMessages; + // steady-mode contract (CaptureSums "DELTAS for the tail"): rows + // AND sums cover ONLY this turn's new elements. The frozen window + // rows entered the ledger when they were first captured — + // re-appending them shifts every pos forward, doubles the table, + // and inflates estCum/textCum/imgCum by a whole window per green + // round (review P1-2 + tail-only rows fix). The slice past F is + // exactly the appended tail (committedTail was capped at F+kNew). + const steadyTailRows = renderFast !== null ? renderedTailObjects.slice(renderFast.F).map((m) => JSON.stringify(m)) : renderedCaptureStrings; + const sumsSrc = renderFast !== null ? processedMessages.slice(Math.min(renderFast.F, processedMessages.length)) : processedMessages; const capOp = commitRenderCapture({ sessionId, store, sid: renderCapturedSid, mode: renderFast !== null ? "steady" : "legacy", caps: renderCapturedCaps, - rowStrings: renderedCaptureStrings, + rowStrings: steadyTailRows, sums: { estCum: estimateCoreMessagesUpper(sumsSrc), textCum: estimateCoreMessages(sumsSrc), diff --git a/tests/issue5664-render-snapshot.test.ts b/tests/issue5664-render-snapshot.test.ts index 5053b06a14..ab25611b0a 100644 --- a/tests/issue5664-render-snapshot.test.ts +++ b/tests/issue5664-render-snapshot.test.ts @@ -222,6 +222,34 @@ test("unit: S4 checksum tamper kills the mirror (hydrate-failed, loudly logged)" } finally { h.cleanup(); } }); +// Review round 1 blind-spot pins (issue #5664): session teardown must wipe the +// three render tables in the same cascade, and re-running the migration on an +// already-v12 sidecar must be a no-op that leaves rows untouched. +test("unit: session cascade wipes render tables; v12 re-migration is a no-op", () => { + const h = makeH("cascade"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-cas", b1, ["id0"]); + r1.fin(); + const sid = r1.sid!; + const cast = commitRenderCapture({ sessionId: "s-cas", store: h.store, sid, mode: "legacy", caps: capsFor("none"), rowStrings: ['{"a":1}', '{"b":2}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }); + assert.equal(cast.op, "cast"); + assert.equal(h.store.pfRenderRows(sid).length, 2); + // Re-opening the sidecar re-runs the migrator on an already-v12 + // database: must be a no-op that leaves rows untouched (review round 1 + // blind spot: explicit 12→12 idempotency). + const shadow2 = UnifiedShadow.maybeOpen(h.dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as never); + assert.ok(shadow2, "second open must tolerate an already-migrated sidecar"); + assert.equal(shadow2.pfRenderAvailable(), true); + assert.equal(shadow2.pfRenderRows(sid).length, 2); + shadow2.close(); + // Session teardown cascades the render plane with the identity plane. + h.store.pfDeleteForSession("s-cas"); + assert.equal(h.store.pfRenderRows(sid).length, 0); + assert.equal(h.store.pfRenderCkptIntact(sid), true); // no checkpoint left = vacuously intact + } finally { h.cleanup(); } +}); + // --------------------------------------------------------------------------- // Golden tier: FAST emit == legacy emit at EVERY engaged turn, byte-for-byte, // across arm → steady fast rounds → client compaction (turn 8) → strategy @@ -273,6 +301,8 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" const sessB = { id: "s-gold-b", state: stateB } as unknown as Session; let sidB: string | undefined; let fastRounds = 0, castOps = 0, recastOps = 0, legacyRounds = 0; + let totalSubmittedRows = 0; // volume discipline pin (see final assert) + let totalSubmittedText = 0; for (let tn = 1; tn <= 16; tn++) { const strategy = strategyOf(tn); const body = clientBody(tn); @@ -293,6 +323,7 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" }); let rebuilt: OpenAIMessage[]; let captureStrings: string[]; + let steadyAppendRows: string[] = []; let mode: "steady" | "legacy"; if (gate.ok) { const sidNow = sidB; @@ -327,15 +358,13 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" const mappedAll = mapEgress(pmW); const committedTail = mappedAll.slice(0, Math.min(F + (v.outcome.hit?.windowCores.length ?? 0), mappedAll.length)); const vv = verifyRenderWindow(committedTail.slice(0, F).map((m) => JSON.stringify(m)), rw, F); - if (!vv) { - for (let k = 0; k < F; k++) { - const a = JSON.stringify(committedTail[k]); const b = rw.rows[rw.rows.length - F + k]; - - } - } assert.equal(vv, true, `turn ${tn}: window verify failed — tail bytes drifted vs stored rows`); rebuilt = spliceOutboundRows(rw, F, committedTail); captureStrings = [...rw.rows.slice(0, rw.rows.length - F), ...committedTail.map((m) => JSON.stringify(m))]; + // steady-mode contract: only THIS turn's new rows are appended to + // the plane (the slice past the F frozen window); the full list + // stays in captureStrings for the golden equation below. + steadyAppendRows = committedTail.slice(F).map((m) => JSON.stringify(m)); mode = "steady"; fastRounds++; } else { @@ -348,10 +377,17 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" mode = "legacy"; legacyRounds++; } + const srcRows = mode === "steady" ? steadyAppendRows : captureStrings; + // Length-based deterministic stand-in for the token estimators: what + // this tier exercises is the accumulation ALGEBRA — each row counted + // exactly once with its own cost, tail-only deltas on steady rounds. + const rowCost = (rs: string[]): number => rs.reduce((s, x) => s + x.length, 0); const op = commitRenderCapture({ - sessionId: "s-gold-b", store: hB.store, sid: sidB!, mode, caps, rowStrings: captureStrings, - sums: { estCum: 0, textCum: 0, imgCum: 0 }, + sessionId: "s-gold-b", store: hB.store, sid: sidB!, mode, caps, rowStrings: srcRows, + sums: { estCum: rowCost(srcRows), textCum: rowCost(srcRows), imgCum: 0 }, }); + totalSubmittedRows += srcRows.length; + totalSubmittedText += rowCost(srcRows); if (op.committed) { if (op.op === "cast") castOps++; else if (op.op === "recast") recastOps++; @@ -361,11 +397,27 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" `turn ${tn} (${mode}${gate.ok ? "" : `, reason=${gate.reason}`}): lane emit diverged from legacy oracle`); assert.equal(stateB.blocks.length, 0, "assumption pin: no folds materialize in this synthetic conv"); v.fin(); // plane advances AFTER capture — same order as prepare-openai + // Structural regression pin (review round 1, tail-only fix): the render + // table must track the identity ledger one-row-per-element at every + // turn boundary — a duplicate-append corruption shows up here instantly + // instead of hiding behind the byte equation. + assert.equal(hB.store.pfRenderRows(sidB!).length, watermarkV6DebugState("s-gold-b").worlds[sidB!]!.maxPos + 1, + `turn ${tn}: render table drifted from identity ledger (rows=${hB.store.pfRenderRows(sidB!).length})`); } assert.equal(castOps, 1, "exactly one cast (the arm turn)"); - assert.ok(fastRounds >= 6, `expected several fast rounds, got ${fastRounds} (audit too aggressive?)`); + // ≥ 12 fast rounds = at least TWO full audit waves served green: the first + // proves the lane runs, the second proves any legacy round CONSUMES the + // audit obligation (pre-fix, tick froze at N and every later turn rode + // legacy forever — masked at the time by per-round recasts). + assert.ok(fastRounds >= 12, `expected ≥2 audit waves of fast rounds, got ${fastRounds} (audit obligation not consumed?)`); assert.ok(recastOps >= 1, "strategy flip at turn 10 must force ≥1 recast"); assert.ok(legacyRounds >= 2, `arm + audit/caps-flip rounds must ride legacy (got ${legacyRounds})`); + // Volume discipline (steady deltas must stay O(new) per round, never + // O(full list)): pre-tail-fix behavior re-submitted the whole list each + // green round ⇒ ~8×final volume here. + const finalRowCount = hB.store.pfRenderRows(sidB!).length; + assert.ok(totalSubmittedRows <= 3 * finalRowCount, + `submitted row-volume ${totalSubmittedRows} vs final table ${finalRowCount} — steady rounds must append tails, not full lists`); // S4 truth intact at the end; mirror agrees with the last golden shape assert.equal(hB.store.pfRenderCkptIntact(sidB!), true); const dbgLhs = watermarkV6RenderDebugState("s-gold-b"); @@ -373,6 +425,11 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" const lhsRows = dbgLhs.sids[dbgLhs.sids.length - 1].rows; const rhsRows = goldens[goldens.length - 1].rows.length; assert.equal(lhsRows, rhsRows, "mirror row count must equal last golden row count"); + // Sums algebra pin (review P1-2): every row's cost entered the cumulative + // ledger EXACTLY once — a frozen-window double-count would overshoot here. + const finalRowCost = hB.store.pfRenderRows(sidB!).reduce((s, r) => s + (r.rowJson?.length ?? 0), 0); + assert.ok(Math.abs(dbgLhs.sids[dbgLhs.sids.length - 1].textCum - finalRowCost) < Math.max(1, finalRowCost * 0.25), + `cumulative text ledger drifted from row truth (cum=${dbgLhs.sids[dbgLhs.sids.length - 1].textCum} vs rows=${finalRowCost}; submitted=${totalSubmittedText})`); // ---- phase 3: restart hydration — mirrors wiped, disk must continue ----- _resetWatermarkV6RenderTestState(); From b443c3a5b6d1154d8914c5eecf2903b00d352d1f Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 01:43:28 +0800 Subject: [PATCH 05/11] docs(config): document watermarkV6Render cohort + BILI_PF_RENDER_* tunables (#5664) Review hygiene: the new rollout cohort and the two env-only tunables were missing from the config reference (document-or-don't-ship, #2030). Seeds in website/config-reference/*, docs regenerated via tools/gen-config-docs.mjs (coverage ok: 199 env names + 61 top-level keys). --- CONFIGURATION.md | 4 ++++ CONFIGURATION.zh-CN.md | 4 ++++ website/config-reference/env-only.yaml | 16 ++++++++++++++++ website/config-reference/rollout.yaml | 8 ++++++++ 4 files changed, 32 insertions(+) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 72009dbdb1..691c614bbb 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -104,6 +104,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `rollout.features.pluginLaneServerHistory` | boolean \| number | 0 (off) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | Plugin-lane server-owned history (#4491): for enabled installs, (a) plugin-lane requests may carry an internal `biliMsgIds` array parallel to `messages` that pins message identity to client-supplied ids (always stripped before upstream forwarding; malformed arrays are ignored with a warning, id-poisoning shapes hard-fail on the incremental lane); (b) the loopback-only `POST /__bili/plugin/render` endpoint serves the outbound prefix rendered from the unified transcript (folded regions collapse to summary anchors; any desync degrades to a non-2xx so the shim falls back to its local history); (c) the incremental-lane probe advertises `serverHistory`. Default 0% — no behavior change until rolled out. | | `rollout.features.pluginFullResendWatermark` | boolean \| number | 5 (experimental grey) | BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK | Full-resend watermark fastpath (#5403/#5418): plugin-lane chat-completions turns whose previous response carried an x-bili-pf-seq stamp assert the resent prefix via the echoed count (structural scan, no full parse); on a hit the pipeline consumes stashed canonical prefix cores plus only the new tail window (byte-identical outbound provenance; ANY doubt falls back to the untouched full-parse path per turn and re-stamps, five credited misses hold the lane for one hour). Requires the client to echo the stamp back as x-bili-pf-count (dsh/pi/opencode/omp do; unadapted lanes never hit). EXPERIMENTAL grey rollout — ON by default for roughly 5% of installs (owner ruling 2026-11-02: small cohort before GA; one-time intro notice in the web UI for every install, tri-state switch in quick config). Force it with `true`, turn off with `false` (or a percent) / env = 0. See TECHNICAL-NOTES.md “Full-resend watermark fastpath”. | | `rollout.features.watermarkV6MsgId` | boolean \| number | 0 (off) | BILI_ROLLOUT_WATERMARK_V6_MSG_ID | Composite-identity incremental fastpath v6 (#5643): plugin-lane full-resend turns whose client echoes complete per-element host ids (x-bili-pf-tail-ids tail window + x-bili-plugin-conversation carrier) judge the resent prefix by identity set-difference instead of count — only NEW elements are parsed incrementally, frozen cores ride byte-stable from the unified-storage sidecar. HARD identity gate: a missing/malformed/mis-width/inconsistent id list is refused with HTTP 400 and zero side effects (five consecutive strikes park the lane 60 min, loud fallback thereafter) — never a silent legacy fallback. Nested inside pluginFullResendWatermark (cohort ⊆ the v5 grey population) and requires the unified-storage sidecar (nodes without it stay on the baseline lanes unchanged). Default OFF — opens only by explicit rollout. | +| `rollout.features.watermarkV6Render` | boolean \| number | 0 (off) | BILI_ROLLOUT_WATERMARK_V6_RENDER | v6 render-snapshot plane (#5664): on composite-identity v6 hit-turns, steady engaged turns replay ONLY the last W frozen cores plus the new tail window through the kernel and splice previously-emitted outbound bytes back verbatim instead of re-running the full-view scan (per-turn byte check verifyRenderWindow; cumulative estimate bases advance incrementally instead of re-scanning all n rows). ANY single red gate condition (arm turn / title-gen / plane-unavailable / pressure >= 90% of model limit / snapshot-lane / blocks-active / fold-pending / absorb-active / ccr-on / markers / hydrate-failed / caps-mismatch / scheduled audit every Nth round / rows-misaligned) falls back to the untouched full-view legacy pass for that one turn and re-captures — structurally byte-identical to pre-change when red. Storage: three unified-store sidecar tables (pf_render / pf_render_log / pf_render_ckpt, schema v12, cascade-deleted with the session); MySQL/network backends degrade soft (plane unavailable -> legacy). Tunables: BILI_PF_RENDER_WINDOW (W, default 8) and BILI_PF_RENDER_AUDIT_EVERY (N, default 8). Nested inside watermarkV6MsgId (cohort is a subset of the v6 msgid population). Default OFF — opens only by explicit rollout. | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | Mask non-public target hosts in logs as ; credential headers are always masked regardless. | | `security.adminRelay` | boolean | false | — | Let an embedding host (dsh web today) relay the ENTIRE /__bili/ admin surface through its own entry origin so a remote browser can open the panel (#3373). Fail-closed, file-only. The relayed hop reaches the proxy as a local non-browser client (loopback source + loopback Host, no Origin), so the admin gate stays unchanged; enabling it exposes config editing and credential writes to whoever can reach the host web entry — protect that entry (auth / reverse proxy) first. | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude Code subagents get their own session namespace (\|sub:) instead of queueing behind the main session. Anthropic-wire only — on the Responses wire this switch is a no-op (see the `subagentSplit` / BOTH-mechanisms scope note). | @@ -434,6 +435,8 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `BILI_MITM_HOSTS` | string[] (csv) | unset | — | Launcher channel: MITM whitelist hosts handed to the spawned proxy (distinct from the config-file twin BILI_MITM_DOMAINS). | | `BILI_PLUGIN_AGENT` | boolean-ish | off | — | Spawn channel: marks a plugin-agent process. | | `BILI_IMAGE_RESCALE_MAX_DIM` | integer | 1600 | — | Longest-edge trigger threshold (px) for the egress image downscale pass (#3400); non-numeric or below 128 ignored (default applies). Env-only by design. | +| `BILI_PF_RENDER_WINDOW` | integer | 8 | — | Bounded render window W for the v6 render-snapshot plane (#5664): how many frozen cores ride the bounded kernel pass beside the new tail window on each steady turn. Probe evidence across three strategies showed a suffix-only W-pass reproduces every element's outbound bytes exactly, so 8 is plenty; values below 1 or non-numeric fall back to 8, capped at 64. Only meaningful while rollout.features.watermarkV6Render is on. | +| `BILI_PF_RENDER_AUDIT_EVERY` | integer | 8 | — | Scheduled audit cadence N for the v6 render-snapshot plane (#5664): every N engaged hit-turns force ONE full legacy pass whose capture diff re-verifies the stored outbound rows end-to-end (bounds messageRefs/rules merge lag to <= N rounds). Values below 2 or non-numeric fall back to 8, capped at 1024. Any legacy round (red-gate fallback included) resets the counter — it IS an audit. Only meaningful while rollout.features.watermarkV6Render is on. | @@ -1935,6 +1938,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK` | `rollout.features.pluginFullResendWatermark` | 5 (experimental grey) | | `BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY` | `rollout.features.pluginLaneServerHistory` | 0 (off) | | `BILI_ROLLOUT_WATERMARK_V6_MSG_ID` | `rollout.features.watermarkV6MsgId` | 0 (off) | +| `BILI_ROLLOUT_WATERMARK_V6_RENDER` | `rollout.features.watermarkV6Render` | 0 (off) | | `BILI_SESSION_CONTENT_STORE_PIN` | `sessions.contentStorePin` | false | | `BILI_SESSION_GC` | `sessions.gc.enabled` | false | | `BILI_SESSION_GC_INTERVAL_MS` | `sessions.gc.intervalMs` | 3600000 | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index feff76a596..aaf843652e 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -104,6 +104,7 @@ | `rollout.features.pluginLaneServerHistory` | boolean \| number | 0(关) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 `messages` 平行的内部字段 `biliMsgIds`,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 `POST /__bili/plugin/render` 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 `serverHistory`。默认 0%,放量前无行为变化。 | | `rollout.features.pluginFullResendWatermark` | boolean \| number | 5(实验性灰度) | BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK | 全量重发水位快路径(#5403/#5418):插件车道 chat-completions 回合若上一轮响应盖有 x-bili-pf-seq,则用回声计数声明重发前缀(结构扫描,不做全量解析);命中时管线只消费钉存的前缀核 + 新增尾巴窗口(出站字节同源可证;任何疑点当轮回退未动过的全量解析路径并重新盖戳,连续五次计名失配将车道按住一小时)。需客户端把戳回声为 x-bili-pf-count(适配的车道回带;其余车道不会命中)。**实验性灰度**——默认仅约 5% 的安装开启(owner 裁定 2026-11-02:先小流量再 GA;web 面板对全部安装显示一次性公告、快速配置提供三态开关);`true` 强制开、`false`(或百分比)/ env = 0 关。见 TECHNICAL-NOTES.zh-CN.md「全量重发水位快路径」。 | | `rollout.features.watermarkV6MsgId` | boolean \| number | 0(关) | BILI_ROLLOUT_WATERMARK_V6_MSG_ID | 复合身份增量快路径 v6(#5643):插件车道全量重发回合,当客户端回带逐元素宿主 id(x-bili-pf-tail-ids 尾窗 + x-bili-plugin-conversation 载体)时,用身份集合差而非计数判定重发前缀——仅增量解析新增元素,冻结核自统一存储 sidecar 逐字骑乘。身份硬门:id 列表缺失/格式错/宽度不符/一致性断裂一律以 HTTP 400 拒绝且零副作用(连续五次连击按住车道 60 分钟,之后响亮降级)——绝不静默回退 legacy。嵌套于 pluginFullResendWatermark(人群 ⊆ v5 灰度人群),并依赖统一存储 sidecar(无此设施的节点原样走基线车道)。默认关——仅由显式放量开启。 | +| `rollout.features.watermarkV6Render` | boolean \| number | 0(关) | BILI_ROLLOUT_WATERMARK_V6_RENDER | v6 渲染快照平面(#5664):复合身份 v6 命中回合中,稳态轮只把最近 W 个冻结核 + 新增尾窗送入内核、把此前已出站的字节逐字拼回,不再跑全量视图扫描(每轮字节校验 verifyRenderWindow;累计估算基数增量推进而非重扫全部 n 行)。任一红色门控条件(arm 轮/title-gen/plane-unavailable/压力 ≥ 模型上限 90%/snapshot-lane/blocks-active/fold-pending/absorb-active/ccr-on/markers/hydrate-failed/caps-mismatch/每 N 轮计划审计/rows-misaligned)当轮回退未动过的全量 legacy 路径并重新捕获——红色时与改动前结构上逐字节一致。存储:统一存储三张 sidecar 表(pf_render / pf_render_log / pf_render_ckpt,schema v12,随会话级联删除);MySQL/网络后端软降级(plane 不可用 → legacy)。可调项:BILI_PF_RENDER_WINDOW(W,默认 8)与 BILI_PF_RENDER_AUDIT_EVERY(N,默认 8)。嵌套于 watermarkV6MsgId(人群 ⊆ v6 msgid 人群)。默认关——仅由显式放量开启。 | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | 日志中把非公开目标主机遮成 ;凭据头无论此开关如何总是遮盖。 | | `security.adminRelay` | boolean | false | — | 允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。 | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude 子代理拥有独立会话命名空间(\|sub:),不再排队在主会话锁后。仅 anthropic 线路 —— Responses 线路上本开关无效(见 `subagentSplit` / BOTH-mechanisms 作用域说明)。 | @@ -434,6 +435,8 @@ | `BILI_MITM_HOSTS` | string[] (csv) | unset | — | 启动器通道:传给被派生代理的 MITM 白名单主机(区别于配置文件对应项 BILI_MITM_DOMAINS)。 | | `BILI_PLUGIN_AGENT` | boolean-ish | off | — | 派生通道:标记插件 agent 进程。 | | `BILI_IMAGE_RESCALE_MAX_DIM` | integer | 1600 | — | 出站图片降采样 pass 的最长边触发阈值(像素,#3400);非数字或低于 128 的取值按默认处理。设计上仅环境变量。 | +| `BILI_PF_RENDER_WINDOW` | integer | 8 | — | v6 渲染快照平面(#5664)的有界渲染窗口 W:每个稳态轮送入内核的冻结核数量(与新增尾窗并列)。三种策略的探针证据表明后缀 W-pass 能逐字节复现每个元素的出站字节,8 已足够;低于 1 或非数字的取值回退为 8,上限 64。仅在 rollout.features.watermarkV6Render 开启时有意义。 | +| `BILI_PF_RENDER_AUDIT_EVERY` | integer | 8 | — | v6 渲染快照平面(#5664)的计划审计周期 N:每 N 个命中轮强制跑一次全量 legacy pass,其捕获 diff 对已存出站行做端到端复验(把 messageRefs/rules 合并滞后钉在 ≤ N 轮内)。低于 2 或非数字的取值回退为 8,上限 1024。任何 legacy 轮(含红色门控回退)都会清零计数——它本身就是一次审计。仅在 rollout.features.watermarkV6Render 开启时有意义。 | @@ -1912,6 +1915,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_ROLLOUT_PLUGIN_FULL_RESEND_WATERMARK` | `rollout.features.pluginFullResendWatermark` | 5 (experimental grey) | | `BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY` | `rollout.features.pluginLaneServerHistory` | 0 (off) | | `BILI_ROLLOUT_WATERMARK_V6_MSG_ID` | `rollout.features.watermarkV6MsgId` | 0 (off) | +| `BILI_ROLLOUT_WATERMARK_V6_RENDER` | `rollout.features.watermarkV6Render` | 0 (off) | | `BILI_SESSION_CONTENT_STORE_PIN` | `sessions.contentStorePin` | false | | `BILI_SESSION_GC` | `sessions.gc.enabled` | false | | `BILI_SESSION_GC_INTERVAL_MS` | `sessions.gc.intervalMs` | 3600000 | diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index 635c399999..94f396dc40 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -530,3 +530,19 @@ entries: group: envonly desc_en: "Longest-edge trigger threshold (px) for the egress image downscale pass (#3400); non-numeric or below 128 ignored (default applies). Env-only by design." desc_zh: "出站图片降采样 pass 的最长边触发阈值(像素,#3400);非数字或低于 128 的取值按默认处理。设计上仅环境变量。" + - key: BILI_PF_RENDER_WINDOW + type: integer + default: "8" + kind: env-only + audience: user + group: envonly + desc_en: "Bounded render window W for the v6 render-snapshot plane (#5664): how many frozen cores ride the bounded kernel pass beside the new tail window on each steady turn. Probe evidence across three strategies showed a suffix-only W-pass reproduces every element's outbound bytes exactly, so 8 is plenty; values below 1 or non-numeric fall back to 8, capped at 64. Only meaningful while rollout.features.watermarkV6Render is on." + desc_zh: "v6 渲染快照平面(#5664)的有界渲染窗口 W:每个稳态轮送入内核的冻结核数量(与新增尾窗并列)。三种策略的探针证据表明后缀 W-pass 能逐字节复现每个元素的出站字节,8 已足够;低于 1 或非数字的取值回退为 8,上限 64。仅在 rollout.features.watermarkV6Render 开启时有意义。" + - key: BILI_PF_RENDER_AUDIT_EVERY + type: integer + default: "8" + kind: env-only + audience: user + group: envonly + desc_en: "Scheduled audit cadence N for the v6 render-snapshot plane (#5664): every N engaged hit-turns force ONE full legacy pass whose capture diff re-verifies the stored outbound rows end-to-end (bounds messageRefs/rules merge lag to <= N rounds). Values below 2 or non-numeric fall back to 8, capped at 1024. Any legacy round (red-gate fallback included) resets the counter — it IS an audit. Only meaningful while rollout.features.watermarkV6Render is on." + desc_zh: "v6 渲染快照平面(#5664)的计划审计周期 N:每 N 个命中轮强制跑一次全量 legacy pass,其捕获 diff 对已存出站行做端到端复验(把 messageRefs/rules 合并滞后钉在 ≤ N 轮内)。低于 2 或非数字的取值回退为 8,上限 1024。任何 legacy 轮(含红色门控回退)都会清零计数——它本身就是一次审计。仅在 rollout.features.watermarkV6Render 开启时有意义。" diff --git a/website/config-reference/rollout.yaml b/website/config-reference/rollout.yaml index c6e02524c7..2ff925f5a6 100644 --- a/website/config-reference/rollout.yaml +++ b/website/config-reference/rollout.yaml @@ -73,3 +73,11 @@ entries: group: behavior desc_en: "Composite-identity incremental fastpath v6 (#5643): plugin-lane full-resend turns whose client echoes complete per-element host ids (x-bili-pf-tail-ids tail window + x-bili-plugin-conversation carrier) judge the resent prefix by identity set-difference instead of count — only NEW elements are parsed incrementally, frozen cores ride byte-stable from the unified-storage sidecar. HARD identity gate: a missing/malformed/mis-width/inconsistent id list is refused with HTTP 400 and zero side effects (five consecutive strikes park the lane 60 min, loud fallback thereafter) — never a silent legacy fallback. Nested inside pluginFullResendWatermark (cohort ⊆ the v5 grey population) and requires the unified-storage sidecar (nodes without it stay on the baseline lanes unchanged). Default OFF — opens only by explicit rollout." desc_zh: "复合身份增量快路径 v6(#5643):插件车道全量重发回合,当客户端回带逐元素宿主 id(x-bili-pf-tail-ids 尾窗 + x-bili-plugin-conversation 载体)时,用身份集合差而非计数判定重发前缀——仅增量解析新增元素,冻结核自统一存储 sidecar 逐字骑乘。身份硬门:id 列表缺失/格式错/宽度不符/一致性断裂一律以 HTTP 400 拒绝且零副作用(连续五次连击按住车道 60 分钟,之后响亮降级)——绝不静默回退 legacy。嵌套于 pluginFullResendWatermark(人群 ⊆ v5 灰度人群),并依赖统一存储 sidecar(无此设施的节点原样走基线车道)。默认关——仅由显式放量开启。" + - key: rollout.features.watermarkV6Render + type: "boolean | number" + default: "0 (off)" + default_zh: "0(关)" + env: BILI_ROLLOUT_WATERMARK_V6_RENDER + group: behavior + desc_en: "v6 render-snapshot plane (#5664): on composite-identity v6 hit-turns, steady engaged turns replay ONLY the last W frozen cores plus the new tail window through the kernel and splice previously-emitted outbound bytes back verbatim instead of re-running the full-view scan (per-turn byte check verifyRenderWindow; cumulative estimate bases advance incrementally instead of re-scanning all n rows). ANY single red gate condition (arm turn / title-gen / plane-unavailable / pressure >= 90% of model limit / snapshot-lane / blocks-active / fold-pending / absorb-active / ccr-on / markers / hydrate-failed / caps-mismatch / scheduled audit every Nth round / rows-misaligned) falls back to the untouched full-view legacy pass for that one turn and re-captures — structurally byte-identical to pre-change when red. Storage: three unified-store sidecar tables (pf_render / pf_render_log / pf_render_ckpt, schema v12, cascade-deleted with the session); MySQL/network backends degrade soft (plane unavailable -> legacy). Tunables: BILI_PF_RENDER_WINDOW (W, default 8) and BILI_PF_RENDER_AUDIT_EVERY (N, default 8). Nested inside watermarkV6MsgId (cohort is a subset of the v6 msgid population). Default OFF — opens only by explicit rollout." + desc_zh: "v6 渲染快照平面(#5664):复合身份 v6 命中回合中,稳态轮只把最近 W 个冻结核 + 新增尾窗送入内核、把此前已出站的字节逐字拼回,不再跑全量视图扫描(每轮字节校验 verifyRenderWindow;累计估算基数增量推进而非重扫全部 n 行)。任一红色门控条件(arm 轮/title-gen/plane-unavailable/压力 ≥ 模型上限 90%/snapshot-lane/blocks-active/fold-pending/absorb-active/ccr-on/markers/hydrate-failed/caps-mismatch/每 N 轮计划审计/rows-misaligned)当轮回退未动过的全量 legacy 路径并重新捕获——红色时与改动前结构上逐字节一致。存储:统一存储三张 sidecar 表(pf_render / pf_render_log / pf_render_ckpt,schema v12,随会话级联删除);MySQL/网络后端软降级(plane 不可用 → legacy)。可调项:BILI_PF_RENDER_WINDOW(W,默认 8)与 BILI_PF_RENDER_AUDIT_EVERY(N,默认 8)。嵌套于 watermarkV6MsgId(人群 ⊆ v6 msgid 人群)。默认关——仅由显式放量开启。" From 1237316c94897879fc198cec5e8bcc7f30fc6ef3 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 01:43:28 +0800 Subject: [PATCH 06/11] refactor(fastpath): drop unreachable window-short gate branch (#5664 review hygiene) rows-misaligned pins rows.length === maxPos+1 before F is computed, so F <= maxPos+1 and 'maxPos < F-1' can never hold. Behaviorally inert; no test referenced the reason string. --- src/server/fastpath-v6-render.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/server/fastpath-v6-render.ts b/src/server/fastpath-v6-render.ts index 9acc031990..2dc37ef368 100644 --- a/src/server/fastpath-v6-render.ts +++ b/src/server/fastpath-v6-render.ts @@ -318,10 +318,9 @@ export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { // misaligned window. if (rw.rows.length !== args.maxPos + 1) return off("rows-misaligned"); registerSidForSession(args.sessionId, args.sid); - const F = Math.min(PF_RENDER_WINDOW, rw.rows.length); // Window input = last F identity elements (+ new cores added by caller); // positions are contiguous because the ledger grows forward only (P3). - if (args.maxPos < F - 1) return off("window-short"); + const F = Math.min(PF_RENDER_WINDOW, rw.rows.length); return { ok: true, rw, F }; } From aeec940c25accf8c58e216e4cf1b0d11513b6506 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 01:51:29 +0800 Subject: [PATCH 07/11] =?UTF-8?q?test(fastpath):=20exercise=20the=20audit?= =?UTF-8?q?=20cadence=20for=20real=20=E2=80=94=20flip=20deferred=20to=20tu?= =?UTF-8?q?rn=2014,=20audit-due=20pinned=20at=20turn=2010=20with=20immedia?= =?UTF-8?q?te=20recovery=20(#5664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/issue5664-render-snapshot.test.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/issue5664-render-snapshot.test.ts b/tests/issue5664-render-snapshot.test.ts index ab25611b0a..f93eee3ea5 100644 --- a/tests/issue5664-render-snapshot.test.ts +++ b/tests/issue5664-render-snapshot.test.ts @@ -253,7 +253,7 @@ test("unit: session cascade wipes render tables; v12 re-migration is a no-op", ( // --------------------------------------------------------------------------- // Golden tier: FAST emit == legacy emit at EVERY engaged turn, byte-for-byte, // across arm → steady fast rounds → client compaction (turn 8) → strategy -// recast (turn 10) → audit cadence → restart-hydration continuation. +// strategy-flip recast (turn 14) → audit cadence (fires for real at turn 10) → restart-hydration continuation. // --------------------------------------------------------------------------- test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end", async (t) => { @@ -265,7 +265,7 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" // drops e_1/e_3 (client-side compaction) — the lane ignores the drop // (ledger grows forward only), views and rows keep growing, and the // golden equation must still hold element-stable (ids follow elements, - // never positions). Strategy flips text-only→none at turn 10 ⇒ caps flip + // never positions). Strategy flips text-only→none at turn 14 ⇒ caps flip // ⇒ forced legacy recast round, then steady on the new byte form. const ELEMENTS: Msg[] = Array.from({ length: 16 }, (_, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content: `content number ${i}` })); const STABLE_ID = (i: number) => `eid-${i}`; @@ -275,7 +275,8 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" return els; }; const idsOf = (els: Msg[]) => els.map((el) => STABLE_ID(ELEMENTS.indexOf(el))); - const strategyOf = (tn: number) => (tn < 10 ? "text-only" : "none"); + const strategyOf = (tn: number) => (tn < 14 ? "text-only" : "none"); + const gateSeq: string[] = []; // ---- phase 1: arm A — pure legacy full path over its own store ---------- const hA = makeH("gold-a"); @@ -321,6 +322,7 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, log: hB.log, }); + gateSeq.push(gate.ok ? "fast" : String(gate.reason)); let rebuilt: OpenAIMessage[]; let captureStrings: string[]; let steadyAppendRows: string[] = []; @@ -405,12 +407,20 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" `turn ${tn}: render table drifted from identity ledger (rows=${hB.store.pfRenderRows(sidB!).length})`); } assert.equal(castOps, 1, "exactly one cast (the arm turn)"); + // Audit cadence exercised FOR REAL (review round 2): with the flip deferred + // to turn 14, eight consecutive green rounds land tick=8 ⇒ turn 10 MUST ride + // legacy as the scheduled audit itself, and the very next round recovers to + // fast (any legacy full-capture zeroes the tick — a lockout would keep every + // later turn red until restart/caps-flip/S4-heal). + assert.deepEqual(gateSeq.filter((r) => r === "audit-due"), ["audit-due"], `expected exactly one audit-due round, got ${JSON.stringify(gateSeq)}`); + assert.equal(gateSeq.indexOf("audit-due"), 9, `audit-due expected at loop index 9 (turn 10), got ${JSON.stringify(gateSeq)}`); + assert.equal(gateSeq[10], "fast", `round right after the audit must recover to fast, got ${JSON.stringify(gateSeq)}`); // ≥ 12 fast rounds = at least TWO full audit waves served green: the first // proves the lane runs, the second proves any legacy round CONSUMES the // audit obligation (pre-fix, tick froze at N and every later turn rode // legacy forever — masked at the time by per-round recasts). assert.ok(fastRounds >= 12, `expected ≥2 audit waves of fast rounds, got ${fastRounds} (audit obligation not consumed?)`); - assert.ok(recastOps >= 1, "strategy flip at turn 10 must force ≥1 recast"); + assert.ok(recastOps >= 1, "strategy flip at turn 14 must force ≥1 recast"); assert.ok(legacyRounds >= 2, `arm + audit/caps-flip rounds must ride legacy (got ${legacyRounds})`); // Volume discipline (steady deltas must stay O(new) per round, never // O(full list)): pre-tail-fix behavior re-submitted the whole list each From 3f07c860133f54fc783e21253b26988824b4d0a1 Mon Sep 17 00:00:00 2001 From: billion-context-release-bot Date: Thu, 5 Nov 2026 10:02:05 +0800 Subject: [PATCH 08/11] docs(persist): align pfRenderCommit facade comment with actual fail-soft behavior (#5689 review nit) --- src/persist.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/persist.ts b/src/persist.ts index d6aaba778d..1a22becc9d 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -2294,7 +2294,8 @@ export class SessionStore { /** #5664 render snapshot plane facade — same guard shape as the identity * plane: null/empty/false = "plane unavailable right now" and the v6 * lane fails soft onto the legacy full path for that turn only. - * pfRenderCommit does NOT swallow silently. */ + * A failed render commit is logged once, then fails soft for THIS turn + * (same as pfCommitTurn above). */ pfRenderAvailable(): boolean { if (!this.shadow || this.shadow.stats.disabled) return false; try { From 49ba14dae56338368437ccc400ae9e8f8f24c6a0 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 5 Nov 2026 10:53:37 +0800 Subject: [PATCH 09/11] =?UTF-8?q?fix(fastpath):=20seal=20the=20render=20pl?= =?UTF-8?q?ane=20shut=20=E2=80=94=20NO=20recast=20in=20any=20scenario;=20s?= =?UTF-8?q?tructural=20divergence=20kills=20the=20lane=20permanently=20(ow?= =?UTF-8?q?ner=20ruling=202026-11-05,=20#5664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Recast (rewriting covered ordinals / deleting excess rows) existed as the plane's only self-heal path. The owner ruled it must not happen in ANY scenario (fold, caps flip, audit-caught drift, corruption death). This commit removes it from every layer instead of patching its call sites: - diffRowLists verdict 'recast' -> 'diverged'; the diverged branch now calls killRenderPlane(): the sid joins a permanent deadSids set, a kind='dead' entry is persisted into pf_render_log WITHOUT a checkpoint (nothing absorbs it), and the session rides the legacy full path from here on — bytes stay correct, the cache simply never serves again. - killRenderPlane is the ONLY mutation besides append/cast-once: S4 checksum mismatch, row gaps, log-fold disagreement, caps-unresolvable hydration states all KILL durably instead of returning null-and-retry; the arm cast refuses over any pre-existing non-empty table. - pfRenderCommit drops its deletes parameter end-to-end (store/shadow/persist) — covered ordinals can never be rewritten. - gate grows the durable reason 'lane-dead'; prepare-openai skips the capture build+commit entirely once the reason is lane-dead so dead sessions stop paying an O(full history) stringify per turn. - tests: golden A/B re-pinned (death lands at turn 14 caps flip, table freezes at 13 rows, restart hydrates straight to lane-dead — no re-arm); new pins: structural-divergence kill (immutable rows, loud log, persisted dead verdict, gate positive + re-capture refused negative per #3617) and death-persists-across-restart. Consequence (accepted by the ruling): once a session's outbound bytes ever diverge from its cached rows — most concretely after ANY compression fold re-shapes them — that session loses the render fastpath permanently. Fastpath lifetime = up to first divergence, never after. --- src/persist.ts | 1 - src/server/fastpath-v6-render.ts | 169 ++++++++++++++------- src/server/prepare-openai.ts | 27 +++- src/storage/shadow.ts | 1 - src/storage/store.ts | 16 +- tests/issue5664-render-snapshot.test.ts | 191 ++++++++++++++++++------ 6 files changed, 286 insertions(+), 119 deletions(-) diff --git a/src/persist.ts b/src/persist.ts index 1a22becc9d..0e6f46a2fc 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -2314,7 +2314,6 @@ export class SessionStore { sid: string, args: { rows: { pos: number; rowJson: string | null }[]; - deletes?: number[]; logEntries: { kind: string; payloadJson: string }[]; checkpointMetaJson?: string; ts?: number; diff --git a/src/server/fastpath-v6-render.ts b/src/server/fastpath-v6-render.ts index 2dc37ef368..f2f7e029f7 100644 --- a/src/server/fastpath-v6-render.ts +++ b/src/server/fastpath-v6-render.ts @@ -3,9 +3,12 @@ // // station 1 inbound / identity finalize (per turn, existing #5652 lane) // station 2 this snapshot: steady rounds APPEND one captured row per new -// element; compression rounds (arm/audit/fallback) DIFF against -// the stored rows and either append or RECAST — and a recast -// FORCES an S4-checksum checkpoint in the same transaction. +// element; full-path rounds (arm/audit/fallback) DIFF against +// the stored rows and APPEND the tail on growth. A RECAST IS +// FORBIDDEN IN EVERY SCENARIO (owner ruling 2026-11-05): +// any divergence requiring a stored-row rewrite/deletion — or +// any post-death rebuild — KILLS the plane for that sid +// permanently and the session rides the legacy full path. // station 3 outbound emit: prefix rows pass through verbatim from disk; // only the bounded window tail runs a kernel pass per request. // @@ -25,9 +28,11 @@ // G2 capture point = the exact array the baseline emits (rebuiltMessages // before system/ephemeral injection); steady capture stores raw strings // + parsed caches so the prefix never gets re-parsed or re-stringified. -// G3 S4 death verdict — a checksum-corrupt checkpoint kills the plane for -// that sid at hydration; the next legacy turn's diff self-heals with a -// fresh forced checkpoint. Never guess, never forward corrupt bytes. +// G3 death verdict — a checksum-corrupt checkpoint, a structurally +// diverged capture, or any unverifiable pre-existing snapshot kills the +// plane for that sid PERMANENTLY (persisted as a kind='dead' log entry, +// surviving restarts). No recast, no re-arm: byte-safety comes from +// stopping, not from repairing. Never guess, never forward corrupt bytes. // P7 mirrors are caches (LRU byte budget); losing one = DB re-read. import type { SessionStore } from "../persist.js"; import type { Session } from "../session.js"; @@ -64,7 +69,9 @@ export const PF_RENDER_EMERGENCY_RATIO = 0.9; const MIRROR_BYTE_BUDGET = 256 * 1024 * 1024; let mirrorBytes = 0; -/** Capabilities fingerprint — any change forces a recast (cheap, correct). */ +/** Capabilities fingerprint — any change red-gates the lane (caps-mismatch); + * if the legacy round's bytes then diverge from the stored rows the plane + * is killed for this sid (no-recast regime). */ export interface RenderCaps { v: 1; model: string; @@ -109,6 +116,8 @@ export interface RenderWorld { textCum: number; /** Cumulative image-reserve contribution of frozen rows. */ imgCum: number; + /** Set when this sid's plane is dead (no-recast regime) — diagnostic. */ + dead?: boolean; /** Green (fast-served) engaged-turn tick — % PF_RENDER_AUDIT_EVERY === 0 * ⇒ audit due. Consumed (zeroed) by ANY legacy full-capture round served, * which IS the audit (the full byte path ran to completion). @@ -121,6 +130,30 @@ const mirrors = new Map(); /** relay session id → pf sids owned by it (teardown sweep). */ const sessionSids = new Map>(); +/** Owner ruling 2026-11-05 (#5689 review): NO recast in ANY scenario. + * Structural divergence (byte diff/shrink), caps flips that changed stored + * bytes, S4 corruption, row-table inconsistency, unverifiable pre-existing + * snapshots — all land here. A dead sid NEVER arms, hydrates, gates green, + * or commits again. The verdict is persisted as a kind='dead' log entry so + * every future process instance reaches the same conclusion. */ +const deadSids = new Set(); + +export function killRenderPlane(store: SessionStore, sid: string, reason: string, log?: LogFn): void { + if (!deadSids.has(sid)) { + deadSids.add(sid); + const rw = mirrors.get(sid); + if (rw) rw.dead = true; + log?.("error", `[${sid}] #5664 render plane KILLED PERMANENTLY (no-recast regime): ${reason}`); + try { + store.pfRenderCommit(sid, { rows: [], logEntries: [{ kind: "dead", payloadJson: JSON.stringify({ reason }) }] }); + } catch { + /* soft: the in-memory verdict still binds this process; a storage + * failure means the NEXT process rediscovers the same condition + * through its own integrity checks (S4 / row table / cast guard) */ + } + } +} + // --------------------------------------------------------------------------- // Mirror lifecycle // --------------------------------------------------------------------------- @@ -170,8 +203,9 @@ export function clearWatermarkV6Render(sessionId: string): void { * Returns null on ANY inconsistency — caller serves the baseline path. */ function hydrateMirror(store: SessionStore, sid: string, log?: LogFn): RenderWorld | null { try { + if (deadSids.has(sid)) return null; if (!store.pfRenderCkptIntact(sid)) { - log?.("error", `[${sid}] #5664 render checkpoint S4 checksum mismatch — plane judged dead for this sid until a forced recast heals it`); + killRenderPlane(store, sid, "S4 checkpoint checksum mismatch (corrupt metadata)", log); return null; } const rowsRaw = store.pfRenderRows(sid); @@ -190,13 +224,16 @@ function hydrateMirror(store: SessionStore, sid: string, log?: LogFn): RenderWor expectedCount = m.rowCount; capsJson = metaRead.ckpt.metaJson; for (const e of metaRead.log) { - const p = JSON.parse(e.payloadJson) as { de?: number; dt?: number; di?: number; n?: number; capsJson?: string } & Partial; - if (e.kind === "cast" || e.kind === "recast") { + const p = JSON.parse(e.payloadJson) as { de?: number; dt?: number; di?: number; n?: number; capsJson?: string; reason?: string } & Partial; + if (e.kind === "dead") { + killRenderPlane(store, sid, `persisted death verdict: ${p.reason ?? "unknown"}`, log); + return null; + } + if (e.kind === "cast") { estCum = p.estCum ?? 0; textCum = p.textCum ?? 0; imgCum = p.imgCum ?? 0; expectedCount = p.rowCount ?? -1; - if (e.kind === "recast" && p.capsJson !== undefined) capsJson = p.capsJson; } else if (e.kind === "append") { estCum += p.de ?? 0; textCum += p.dt ?? 0; @@ -212,24 +249,22 @@ function hydrateMirror(store: SessionStore, sid: string, log?: LogFn): RenderWor for (let i = 0; i < rowsRaw.length; i++) { const rj = rowsRaw[i].rowJson; if (rj === null || rowsRaw[i].pos !== i) { - // Row table inconsistent on disk (tombstone where a row must - // be, or a gap): judge the plane dead LOUDLY — same discipline - // as an S4 checksum mismatch, never a silent fallback. - log?.("error", `[${sid}] #5664 render plane judged dead: row-table inconsistency at pos ${rowsRaw[i].pos} (expected contiguous ${i})`); + killRenderPlane(store, sid, `row-table inconsistency at pos ${rowsRaw[i].pos} (expected contiguous ${i})`, log); return null; } rows[i] = rj; try { rowsParsed[i] = JSON.parse(rj); } catch { - log?.("error", `[${sid}] #5664 render plane judged dead: row parse failed at pos ${i}`); + killRenderPlane(store, sid, `row parse failed at pos ${i}`, log); return null; } } if (expectedCount !== -1 && expectedCount !== rows.length) { // Base sums no longer line up with the row table — the log fold - // disagrees with truth. Caller will serve legacy; the next - // capture diff (full arrays both sides) heals with a recast. + // disagrees with truth. Only a rewrite of stored rows could fix + // that, which is forbidden: kill the plane permanently. + killRenderPlane(store, sid, `log-fold disagrees with row table (expectedCount=${expectedCount}, rows=${rows.length})`, log); return null; } let caps: RenderCaps | undefined; @@ -243,9 +278,13 @@ function hydrateMirror(store: SessionStore, sid: string, log?: LogFn): RenderWor const inner = (parsed.caps as RenderCaps | undefined); caps = inner && inner.v === 1 ? inner : (parsed.v === 1 ? parsed : undefined); } else { - return null; // rows without capabilities = cannot gate safely + killRenderPlane(store, sid, "stored rows without capabilities (cannot gate safely)", log); + return null; + } + if (!caps || caps.v !== 1) { + killRenderPlane(store, sid, "unparseable capabilities fingerprint", log); + return null; } - if (!caps || caps.v !== 1) return null; const rw: RenderWorld = { sid, rows, rowsParsed, caps, estCum, textCum, imgCum, auditTick: 0 }; touchMirror(rw); return rw; @@ -287,6 +326,12 @@ export type RenderGateDecision = | { ok: false; reason: string } | { ok: true; rw: RenderWorld; F: number }; +/** Stable red-reason vocabulary (test-pinned, one per condition): not-engaged, + * arm-turn, title-gen, plane-unavailable, pressure, snapshot-lane, + * blocks-active, fold-pending, absorb-active, ccr-on, markers, lane-dead, + * hydrate-failed, caps-mismatch, audit-due, no-rows, no-plane-geometry, + * rows-misaligned. */ + /** G1 conservative conjunction — every red condition returns the reason so * the lane stays debuggable; the fallback path is unchanged baseline. */ export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { @@ -302,9 +347,12 @@ export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { if (args.absorbActive) return off("absorb-active"); if (args.ccrOn) return off("ccr-on"); if (args.markerNonZero) return off("markers"); + if (deadSids.has(args.sid)) return off("lane-dead"); let rw = mirrors.get(args.sid); if (rw === undefined) rw = hydrateMirror(args.store, args.sid, args.log) ?? undefined; - if (rw === undefined) return off("hydrate-failed"); + // hydrate's own integrity failures persist a death verdict before + // returning null — report the durable reason, not the transient one. + if (rw === undefined) return off(deadSids.has(args.sid) ? "lane-dead" : "hydrate-failed"); if (!capsEqual(rw.caps, args.caps)) return off("caps-mismatch"); if (rw.auditTick % PF_RENDER_AUDIT_EVERY === 0 && rw.auditTick > 0) return off("audit-due"); if (rw.rows.length < 1) return off("no-rows"); @@ -313,9 +361,9 @@ export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { // number one row per ledger element (row i ↔ pos i), so at GATE time — // which runs BEFORE this round's finalize advances the plane — // rows.length must equal maxPos+1. A mismatch means an earlier commit - // failed soft (disk ahead/behind the mirror): fall back and let the - // legacy diff+forced checkpoint self-heal rather than splice on a - // misaligned window. + // failed soft (disk ahead/behind the mirror): fall back to the legacy + // byte path; the plane cannot self-heal anymore (no-recast regime) — + // a persistent misalignment will die its next structural diff. if (rw.rows.length !== args.maxPos + 1) return off("rows-misaligned"); registerSidForSession(args.sessionId, args.sid); // Window input = last F identity elements (+ new cores added by caller); @@ -325,8 +373,8 @@ export function renderPlaneGate(args: RenderGateArgs): RenderGateDecision { } /** Bumped once per GREEN round (prepare-openai gate block). The obligation is - * consumed by any legacy full-capture round (commitRenderCapture zeroes it): - * cadence = "one full pass per PF_RENDER_AUDIT_EVERY fast rounds", red turns + * consumed by any legacy full-capture round (commitRenderCapture zeroes it). + * Cadence = "one full pass per PF_RENDER_AUDIT_EVERY fast rounds", red turns * included among those full passes. */ export function bumpAuditTick(sid: string): void { const rw = mirrors.get(sid); @@ -395,17 +443,19 @@ export interface CaptureArgs { log?: LogFn; } -/** 'equal' | 'append' | 'recast' — prefix-identical growth vs rewrite. */ -export function diffRowLists(prev: string[], cur: string[]): "equal" | "append" | "recast" { +/** 'equal' | 'append' | 'diverged' — prefix-identical growth vs anything + * else (byte diff, shrink, non-monotonic growth). Under the no-recast + * ruling every 'diverged' verdict KILLS the plane. */ +export function diffRowLists(prev: string[], cur: string[]): "equal" | "append" | "diverged" { if (prev.length === cur.length) { - for (let i = 0; i < prev.length; i++) if (prev[i] !== cur[i]) return "recast"; + for (let i = 0; i < prev.length; i++) if (prev[i] !== cur[i]) return "diverged"; return "equal"; } if (cur.length > prev.length) { - for (let i = 0; i < prev.length; i++) if (prev[i] !== cur[i]) return "recast"; + for (let i = 0; i < prev.length; i++) if (prev[i] !== cur[i]) return "diverged"; return "append"; } - return "recast"; + return "diverged"; } /** sids whose row payload carried a literal NUL and were therefore refused @@ -413,11 +463,15 @@ export function diffRowLists(prev: string[], cur: string[]): "equal" | "append" * — storing would corrupt the row with S4 unable to see it, since the * checksum only covers checkpoint meta). Loud-once, then fail-soft per turn: * the in-memory mirror keeps serving this process's lifetime; disk simply - * stops advancing until restart/caps-flip recast is possible again. */ + * stops advancing until restart or caps change allows re-capture again. */ const nulRejectedSids = new Set(); -export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op: "none" | "append" | "cast" | "recast" } { +export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op: "none" | "append" | "cast" } { const { store, sid, caps, log } = args; + if (deadSids.has(sid)) { + // Belt-and-braces behind the gate: a dead sid NEVER arms/commits again. + return { committed: false, op: "none" }; + } const hasNul = args.rowStrings.some((rj) => rj.includes("\u0000")); if (hasNul) { if (!nulRejectedSids.has(sid)) { @@ -436,10 +490,16 @@ export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op } const metaJson = (m: RenderMeta): string => JSON.stringify(m); if (rw === undefined || rw.rows.length === 0) { - // No usable base: full cast with a FORCED checkpoint (self-healing - // first use / post-death rebuild). Legacy turns only reach here with - // full arrays, which is exactly what a cast needs. + // Arm cast — allowed ONLY onto an empty table. The snapshot is + // forged exactly ONCE per sid (owner ruling 2026-11-05: no recast, + // no post-death rebuild): if rows already exist on disk, we cannot + // verify them here and rewriting them is forbidden ⇒ kill. if (args.mode === "legacy") { + const preExisting = store.pfRenderRows(sid); + if (preExisting.length > 0) { + killRenderPlane(store, sid, `arm refused over non-empty rows (${preExisting.length} pre-existing, unverifiable)`, log); + return { committed: false, op: "none" }; + } const rows = args.rowStrings; const m: RenderMeta = { v: 1, caps, rowCount: rows.length, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum }; const okSeq = store.pfRenderCommit(sid, { @@ -476,8 +536,7 @@ export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op } // A full-path round IS the audit regardless of verdict (the legacy byte // path ran to completion) — consume the cadence obligation here, or a - // clean long run would dead-lock on "audit-due" after N ticks (only - // cast/recast zeroed it before this fix). + // clean long run would dead-lock on "audit-due" after N ticks. if (args.mode === "legacy") rw.auditTick = 0; // legacy full-capture: diff against truth, minimal commit. const verdict = diffRowLists(rw.rows, args.rowStrings); @@ -509,23 +568,14 @@ export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op touchMirror(rw); return { committed: true, op: "append" }; } - // recast — client-side structural change (compaction & friends) or caps - // flip: rewrite covered ordinals, physically drop the excess, and FORCE - // the L1 checkpoint in the same transaction (owner ruling). - const rows = args.rowStrings; - const oldMaxPos = rw.rows.length - 1; - const deletes = oldMaxPos >= rows.length ? Array.from({ length: oldMaxPos + 1 - rows.length }, (_, i) => rows.length + i) : []; - const m: RenderMeta = { v: 1, caps, rowCount: rows.length, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum }; - const okSeq = store.pfRenderCommit(sid, { - rows: rows.map((rj, pos) => ({ pos, rowJson: rj })), - deletes: deletes.length > 0 ? deletes : undefined, - logEntries: [{ kind: "recast", payloadJson: JSON.stringify({ ...m, capsJson: JSON.stringify(caps) }) }], - checkpointMetaJson: metaJson(m), - }); - if (okSeq === null) return { committed: false, op: "none" }; - const parsed = rows.map((rj) => safeParse(rj)); - mirrors.set(sid, { sid, rows: [...rows], rowsParsed: parsed, caps: { ...caps }, estCum: args.sums.estCum, textCum: args.sums.textCum, imgCum: args.sums.imgCum, auditTick: 0 }); - return { committed: true, op: "recast" }; + // DIVERGED (byte diff at any position, or shrink): the ONLY repair the + // old design offered was a recast — rewriting covered ordinals and + // deleting the excess. Owner ruling 2026-11-05 forbids that in every + // scenario: the plane is KILLED PERMANENTLY instead and the session + // rides the legacy full path from here on (bytes stay correct; the + // cache simply never serves this sid again). + killRenderPlane(store, sid, `structural divergence (stored=${rw.rows.length} rows, captured=${args.rowStrings.length})`, log); + return { committed: false, op: "none" }; } function safeParse(json: string): unknown { @@ -543,7 +593,8 @@ function safeParse(json: string): unknown { export interface RenderDebugWorld { sid: string; rows: number; - caps: RenderCaps; + dead: boolean; + caps: RenderCaps | undefined; estCum: number; textCum: number; imgCum: number; @@ -556,7 +607,10 @@ export function watermarkV6RenderDebugState(sessionId: string): { sids: RenderDe if (set) { for (const sid of set) { const rw = mirrors.get(sid); - if (rw) sids.push({ sid, rows: rw.rows.length, caps: rw.caps, estCum: rw.estCum, textCum: rw.textCum, imgCum: rw.imgCum, auditTick: rw.auditTick }); + sids.push({ + sid, rows: rw?.rows.length ?? 0, dead: deadSids.has(sid), + caps: rw?.caps, estCum: rw?.estCum ?? 0, textCum: rw?.textCum ?? 0, imgCum: rw?.imgCum ?? 0, auditTick: rw?.auditTick ?? 0, + }); } } return { sids }; @@ -567,5 +621,6 @@ export function _resetWatermarkV6RenderTestState(): void { mirrors.clear(); sessionSids.clear(); nulRejectedSids.clear(); + deadSids.clear(); mirrorBytes = 0; } diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 23f22f8079..df9cf00f35 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -85,6 +85,11 @@ export async function prepareOpenai( // #5664 green verdict carrier (declared here so the clamp/estimate/capture // consumers AFTER the try block see it; assigned at the gate below). let renderFast: { rw: RenderWorld; F: number; maxPos: number; sid: string } | null = null; + // #5664 no-recast regime (owner ruling 2026-11-05): once this sid's plane + // is durably dead the session rides pure legacy FOREVER — skip building + // capture strings entirely (an O(full history) stringify on every later + // turn would be pure waste: the module refuses the commit anyway). + let renderLaneDead = false; // #3222: #651's reasoning-drop plan, computed on the INBOUND history before // core.processTurn — see planReasoningDrop for why the post-turn view is // too late (the kernel hides consumed call+result pairs inside it). @@ -279,6 +284,7 @@ export async function prepareOpenai( renderFast = { rw: renderGate.rw, F: renderGate.F, maxPos: renderGeom.maxPos, sid: renderSid }; } else { log("debug", `[${sessionId}] #5664 render lane off (${renderGate.reason}) — baseline full path`); + if (renderGate.reason === "lane-dead") renderLaneDead = true; } } // #2432: dsh desktop's native compaction can LAND without ever @@ -523,8 +529,10 @@ export async function prepareOpenai( // Station 3: prefix rows pass through verbatim (cached parses); // the mapped tail replaces the last F stored rows AND extends them. // The byte check against the stored suffix is the sole guard — on - // mismatch redo the legacy full pass THIS turn (the next capture - // self-heals the rows with a recast + forced checkpoint). + // mismatch redo the legacy full pass THIS turn; there is no row + // repair anymore (no-recast ruling 2026-11-05) — if the stored + // rows and truth genuinely diverge, the next full-path capture + // kills this sid's plane permanently instead of recasting. const mappedAll = await mapEgress(processedMessages); const rw = renderFast.rw; const wpWindowLen = wpHit?.windowCores.length ?? 0; @@ -540,7 +548,7 @@ export async function prepareOpenai( renderedTailObjects = [...committedTail]; log("debug", `[${sessionId}] #5664 render snapshot FAST emit (prefix=${rw.rows.length - renderFast.F} cached rows, tail=${committedTail.length} mapped)`); } else { - log("warn", `[${sessionId}] #5664 render snapshot byte mismatch vs stored rows — legacy full pass this turn (next audit heals the rows)`); + log("warn", `[${sessionId}] #5664 render snapshot byte mismatch vs stored rows — legacy full pass this turn (no row repair exists: the next full-path capture kills the plane instead of recasting)`); renderFast = null; const turnFull = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turnFull.state; @@ -554,7 +562,10 @@ export async function prepareOpenai( } } else { rebuiltMessages = await mapEgress(processedMessages); - renderedCaptureStrings = rebuiltMessages.map((m) => JSON.stringify(m)); + // Lane-durably-dead sessions never arm/commit again — keep the + // strings null so station 2 skips both their build (O(n) per + // turn) and the doomed commit call. + if (!renderLaneDead) renderedCaptureStrings = rebuiltMessages.map((m) => JSON.stringify(m)); renderedTailObjects = []; renderedImageDelta = 0; } @@ -774,9 +785,11 @@ export async function prepareOpenai( if (Object.keys(wpResponseHeaders).length === 0) wpResponseHeaders = undefined; } // #5664 station 2 — ONE snapshot commit per engaged turn: steady append - // (fast rounds), arm cast / audit diff / recast with FORCED checkpoint - // (full-path rounds carry the complete captured array). Fail-soft for - // this turn only — the identity plane already committed independently. + // (fast rounds), arm cast / audit diff / divergence-KILL (full-path + // rounds carry the complete captured array; owner ruling 2026-11-05: + // recast is forbidden — any structural divergence kills the lane for the + // sid). Fail-soft for this turn only — the identity plane already + // committed independently. if (renderedCaptureStrings !== null && renderCapturedSid !== null && renderCapturedCaps !== null && featureEnabled("watermarkV6Render")) { const store = getStore(); if (store.pfRenderAvailable()) { diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index ec62f55ff9..3fe305c743 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -1215,7 +1215,6 @@ export class UnifiedShadow { sid: string, args: { rows: { pos: number; rowJson: string | null }[]; - deletes?: number[]; logEntries: { kind: string; payloadJson: string }[]; checkpointMetaJson?: string; ts?: number; diff --git a/src/storage/store.ts b/src/storage/store.ts index b1c420bc62..b733bef9df 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2622,18 +2622,18 @@ export class UnifiedStore { } /** Atomic turn commit for the render plane (#5664): upsert `rows` - * (pos may move — a recast rewrites covered ordinals), physically drop - * `deletes` (ordinals that no longer exist after a rewrite), journal - * `logEntries` at strictly increasing ls_seq, and — when + * (pos values must be contiguous ascending; under the no-recast ruling + * covered ordinals are NEVER rewritten in place), journal `logEntries` + * at strictly increasing ls_seq — including the kind='dead' verdict + * that persists a plane death forever — and — when * `checkpointMetaJson` is given — fold the whole log into a fresh ckpt - * WITH its S4 checksum in the SAME transaction (the forced checkpoint - * of a compression round). Any throw → rollback → byte-identical plane. + * WITH its S4 checksum in the SAME transaction (the arm cast). Any + * throw → rollback → byte-identical plane. * Returns the next free ls_seq. */ pfRenderCommit( sid: string, args: { rows: { pos: number; rowJson: string | null }[]; - deletes?: number[]; logEntries: { kind: string; payloadJson: string }[]; checkpointMetaJson?: string; ts?: number; @@ -2645,10 +2645,6 @@ export class UnifiedStore { "INSERT INTO pf_render (sid, pos, row_json) VALUES (?, ?, ?) ON CONFLICT (sid, pos) DO UPDATE SET row_json = excluded.row_json", ); for (const r of args.rows) insRow.run(sid, r.pos, r.rowJson); - if (args.deletes && args.deletes.length > 0) { - const ph = args.deletes.map(() => "?").join(", "); - this.db.prepare(`DELETE FROM pf_render WHERE sid = ? AND pos IN (${ph})`).run(sid, ...args.deletes); - } // Continuation must clear BOTH the surviving log tail AND the // checkpoint watermark: a fresh checkpoint ABSORBS its own log // entries (delete below), so MAX(log) can sit BEHIND ckpt_ls_seq; diff --git a/tests/issue5664-render-snapshot.test.ts b/tests/issue5664-render-snapshot.test.ts index f93eee3ea5..1a0d15af30 100644 --- a/tests/issue5664-render-snapshot.test.ts +++ b/tests/issue5664-render-snapshot.test.ts @@ -3,11 +3,16 @@ // Golden equation under test: on every engaged turn the emit array built as // [cached prefix rows] + [freshly mapped tail] (fast rounds) must be BYTE- // IDENTICAL to what the legacy full-pipeline path emits (arm / audit / -// caps-flip rollback rounds). The kernel is untouched; this lane caches -// egress bytes, not semantics — equivalence with the full pass is the -// contract. B-version compaction semantics hold throughout: client-side -// element deletion is IGNORED by the identity plane (ledger grows forward -// only), so both arms grow identically and stay byte-comparable. +// rollback rounds). The kernel is untouched; this lane caches egress bytes, +// not semantics — equivalence with the full pass is the contract. +// +// NO-RECAST REGIME (owner ruling 2026-11-05, sealed during #5689 review): +// recast is FORBIDDEN in every scenario — any stored-row divergence (byte +// diff, shrink, caps flip that changed bytes, checksum corruption, +// unverifiable pre-existing snapshot) KILLS the plane for that sid +// permanently (persisted kind='dead' log entry) and the session rides the +// legacy full path forever after. Storage is strictly append-only (+ dead +// markers); arm cast is legal exactly once, onto an empty table. // // Harness: unit-level against a REAL sqlite sidecar (UnifiedShadow shadow // mode), mirroring tests/fastpath-v6-msgid.test.ts; the kernel runs as a @@ -119,8 +124,8 @@ test("unit: diffRowLists verdicts", () => { assert.equal(diffRowLists([], ["a"]), "append"); assert.equal(diffRowLists(["a", "b"], ["a", "b"]), "equal"); assert.equal(diffRowLists(["a", "b"], ["a", "b", "c"]), "append"); - assert.equal(diffRowLists(["a", "b"], ["a", "X"]), "recast"); // middle mutation - assert.equal(diffRowLists(["a", "b", "c"], ["a"]), "recast"); // shrink + assert.equal(diffRowLists(["a", "b"], ["a", "X"]), "diverged"); // middle mutation + assert.equal(diffRowLists(["a", "b", "c"], ["a"]), "diverged"); // shrink }); test("unit: cast → steady appends → hydrate-from-disk fold survives mirror loss", (t) => { @@ -195,7 +200,7 @@ test("gate: red reasons are a stable vocabulary", (t) => { } finally { h.cleanup(); } }); -test("unit: S4 checksum tamper kills the mirror (hydrate-failed, loudly logged)", (t) => { +test("unit: S4 checksum tamper KILLS the plane durably (loudly logged)", (t) => { const h = makeH("s4"); try { const b1 = makeBody([{ role: "user", content: "hello" }]); @@ -218,7 +223,82 @@ test("unit: S4 checksum tamper kills the mirror (hydrate-failed, loudly logged)" maxPos: 0, tokenCount: 10, modelContextLimit: MODEL_LIMIT, caps, snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, }); - assert.deepEqual(g, { ok: false, reason: "hydrate-failed" }); + // No-recast ruling: corruption is no longer self-healable via a + // forced recast — the FIRST hydration attempt reports the DURABLE + // reason and persists the death verdict. + assert.deepEqual(g, { ok: false, reason: "lane-dead" }); + const meta = h.store.pfRenderMeta(sid); + assert.ok(meta !== null && meta.log.some((e) => e.kind === "dead"), "corruption must persist a kind='dead' verdict"); + } finally { h.cleanup(); } +}); + +test("unit: structural divergence KILLS the plane permanently (no-recast ruling 2026-11-05)", (t) => { + const h = makeH("die"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-die", b1, ["id0"]); + r1.fin(); + const sid = r1.sid!; + const caps = capsFor("text-only"); + assert.equal(commitRenderCapture({ sessionId: "s-die", store: h.store, sid, log: h.log, mode: "legacy", caps, rowStrings: ['{"i":0}', '{"i":1}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }).op, "cast"); + assert.equal(commitRenderCapture({ sessionId: "s-die", store: h.store, sid, log: h.log, mode: "steady", caps, rowStrings: ['{"i":2}'], sums: { estCum: 1, textCum: 1, imgCum: 0 } }).op, "append"); + // Middle-byte mutation lands on a full-path round. The old design + // repaired with a recast; the ruling forbids that ⇒ kill instead. + const logsBefore = h.log.lines.length; + const op = commitRenderCapture({ sessionId: "s-die", store: h.store, sid, log: h.log, mode: "legacy", caps, rowStrings: ['{"i":0}', '{"X":9}', '{"i":2}'], sums: { estCum: 3, textCum: 3, imgCum: 0 } }); + assert.deepEqual(op, { committed: false, op: "none" }); + assert.equal(h.store.pfRenderRows(sid).length, 3, "stored rows stay immutable after a divergence"); + assert.equal(h.store.pfRenderCkptIntact(sid), true, "the kill writes NO checkpoint (strictly append-only)"); + assert.ok(h.log.lines.slice(logsBefore).some((l) => l.level === "error" && l.msg.includes("KILLED PERMANENTLY")), + `kill must be loudly logged, got ${JSON.stringify(h.log.lines.slice(logsBefore))}`); + const meta = h.store.pfRenderMeta(sid); + assert.ok(meta !== null && meta.log.some((e) => e.kind === "dead"), "death verdict persisted as kind='dead'"); + const base = { + sessionId: "s-die", session: { id: "s-die", state: createInitialState() } as unknown as Session, + store: h.store, engagedHit: true, armedTurn: false, isTitleGen: false, sid, + maxPos: 2, tokenCount: 50, modelContextLimit: MODEL_LIMIT, caps, + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, + }; + assert.deepEqual(renderPlaneGate(base), { ok: false, reason: "lane-dead" }, "positive: dead sid reports the durable reason"); + // Negative side (#3617): neither re-capture nor a restart-wipe'd + // re-arm can bring the lane back — no rewriting of stored rows EVER. + assert.deepEqual(commitRenderCapture({ sessionId: "s-die", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"i":0}', '{"X":9}', '{"i":2}'], sums: { estCum: 3, textCum: 3, imgCum: 0 } }), + { committed: false, op: "none" }, "re-capture into a dead plane is refused"); + assert.equal(h.store.pfRenderRows(sid).length, 3); + } finally { h.cleanup(); } +}); + +test("unit: plane death persists across restart (no re-arm ever)", (t) => { + const h = makeH("pdeath"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-pdeath", b1, ["id0"]); + r1.fin(); + const sid = r1.sid!; + const caps = capsFor("text-only"); + assert.equal(commitRenderCapture({ sessionId: "s-pdeath", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"a":1}', '{"a":2}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }).op, "cast"); + assert.equal(commitRenderCapture({ sessionId: "s-pdeath", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"a":1}', '{"MUTATED":2}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }).committed, false); + // Process-restart simulation: wipe ALL in-memory plane state (the + // death set included) and reopen the SAME sidecar (cascade pattern). + _resetWatermarkV6RenderTestState(); + const shadow2 = UnifiedShadow.maybeOpen(h.dir, { BILI_STORAGE_UNIFIED: "shadow" }, (r) => r as never); + assert.ok(shadow2, "second open must tolerate the existing sidecar"); + const store2 = new SessionStore({ dir: path.join(h.dir, "sessions"), enabled: true, shadow: shadow2 }); + const g = renderPlaneGate({ + sessionId: "s-pdeath", session: { id: "s-pdeath", state: createInitialState() } as unknown as Session, + store: store2, engagedHit: true, armedTurn: false, isTitleGen: false, sid, + maxPos: 1, tokenCount: 10, modelContextLimit: MODEL_LIMIT, caps, + snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, + }); + assert.deepEqual(g, { ok: false, reason: "lane-dead" }, "persisted kind='dead' verdict binds the NEW process too"); + assert.equal(store2.pfRenderRows(sid).length, 2, "row table stays frozen"); + // An explicit arming-style full capture through the new store is + // refused — the lane is dead until session teardown, period. + assert.equal( + commitRenderCapture({ sessionId: "s-pdeath", store: store2, sid, mode: "legacy", caps, rowStrings: ['{"a":1}', '{"a":2}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }).committed, + false, "re-arm through a fresh process is impossible", + ); + shadow2.close(); } finally { h.cleanup(); } }); @@ -252,8 +332,12 @@ test("unit: session cascade wipes render tables; v12 re-migration is a no-op", ( // --------------------------------------------------------------------------- // Golden tier: FAST emit == legacy emit at EVERY engaged turn, byte-for-byte, -// across arm → steady fast rounds → client compaction (turn 8) → strategy -// strategy-flip recast (turn 14) → audit cadence (fires for real at turn 10) → restart-hydration continuation. +// across arm → steady fast rounds → client compaction (turn 8) → audit +// cadence (fires for real at turn 10) → strategy flip (turn 14) → restart. +// No-recast ruling: the flip's byte divergence KILLS the plane at turn 14; +// every later round rides lane-dead legacy (bytes still oracle-exact), the +// stored table freezes at death, and the persisted verdict survives phase 3's +// process-restart reset (hydration refuses re-arm). // --------------------------------------------------------------------------- test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end", async (t) => { @@ -266,7 +350,8 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" // (ledger grows forward only), views and rows keep growing, and the // golden equation must still hold element-stable (ids follow elements, // never positions). Strategy flips text-only→none at turn 14 ⇒ caps flip - // ⇒ forced legacy recast round, then steady on the new byte form. + // ⇒ forced legacy round; its bytes diverge from the stored rows ⇒ the + // plane KILLS (no recast exists anymore) and turns 15-16 ride lane-dead. const ELEMENTS: Msg[] = Array.from({ length: 16 }, (_, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content: `content number ${i}` })); const STABLE_ID = (i: number) => `eid-${i}`; const clientBody = (tn: number): Msg[] => { @@ -301,8 +386,9 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" const stateB: AgentState = createInitialState(); const sessB = { id: "s-gold-b", state: stateB } as unknown as Session; let sidB: string | undefined; - let fastRounds = 0, castOps = 0, recastOps = 0, legacyRounds = 0; + let fastRounds = 0, castOps = 0, legacyRounds = 0; let totalSubmittedRows = 0; // volume discipline pin (see final assert) + let steadySubmittedRows = 0; // anti-duplicate-append pin (see final assert) let totalSubmittedText = 0; for (let tn = 1; tn <= 16; tn++) { const strategy = strategyOf(tn); @@ -384,27 +470,38 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" // this tier exercises is the accumulation ALGEBRA — each row counted // exactly once with its own cost, tail-only deltas on steady rounds. const rowCost = (rs: string[]): number => rs.reduce((s, x) => s + x.length, 0); - const op = commitRenderCapture({ - sessionId: "s-gold-b", store: hB.store, sid: sidB!, mode, caps, rowStrings: srcRows, - sums: { estCum: rowCost(srcRows), textCum: rowCost(srcRows), imgCum: 0 }, - }); + let op: { committed: boolean; op: "none" | "append" | "cast" } = { committed: false, op: "none" }; + // No-recast regime: once the lane dies, production stops paying for + // capture strings entirely (the seam skips the station-2 commit); the + // lane rides pure legacy until the session ends. Mirror that here. + if (!(mode === "legacy" && !gate.ok && gate.reason === "lane-dead")) { + op = commitRenderCapture({ + sessionId: "s-gold-b", store: hB.store, sid: sidB!, log: hB.log, mode, caps, rowStrings: srcRows, + sums: { estCum: rowCost(srcRows), textCum: rowCost(srcRows), imgCum: 0 }, + }); + } totalSubmittedRows += srcRows.length; totalSubmittedText += rowCost(srcRows); - if (op.committed) { - if (op.op === "cast") castOps++; - else if (op.op === "recast") recastOps++; - } + if (mode === "steady") steadySubmittedRows += srcRows.length; + if (op.committed && op.op === "cast") castOps++; // THE golden equation: lane emit == legacy oracle, byte for byte. assert.deepEqual(rebuilt.map((m) => JSON.stringify(m)), goldens[tn - 1].rows, `turn ${tn} (${mode}${gate.ok ? "" : `, reason=${gate.reason}`}): lane emit diverged from legacy oracle`); assert.equal(stateB.blocks.length, 0, "assumption pin: no folds materialize in this synthetic conv"); v.fin(); // plane advances AFTER capture — same order as prepare-openai - // Structural regression pin (review round 1, tail-only fix): the render - // table must track the identity ledger one-row-per-element at every - // turn boundary — a duplicate-append corruption shows up here instantly - // instead of hiding behind the byte equation. - assert.equal(hB.store.pfRenderRows(sidB!).length, watermarkV6DebugState("s-gold-b").worlds[sidB!]!.maxPos + 1, - `turn ${tn}: render table drifted from identity ledger (rows=${hB.store.pfRenderRows(sidB!).length})`); + // Structural regression pin (review round 1, tail-only fix): while the + // plane is ALIVE the render table must track the identity ledger + // one-row-per-element at every turn boundary — a duplicate-append + // corruption shows up here instantly instead of hiding behind the byte + // equation. After a death the table freezes (nothing re-commits), so + // pin the frozen invariant instead. + const dbgNow = watermarkV6RenderDebugState("s-gold-b").sids.some((w) => w.dead); + if (!dbgNow) { + assert.equal(hB.store.pfRenderRows(sidB!).length, watermarkV6DebugState("s-gold-b").worlds[sidB!]!.maxPos + 1, + `turn ${tn}: render table drifted from identity ledger (rows=${hB.store.pfRenderRows(sidB!).length})`); + } else { + assert.equal(hB.store.pfRenderRows(sidB!).length, 13, `turn ${tn} post-death: row table must stay frozen`); + } } assert.equal(castOps, 1, "exactly one cast (the arm turn)"); // Audit cadence exercised FOR REAL (review round 2): with the flip deferred @@ -415,26 +512,32 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" assert.deepEqual(gateSeq.filter((r) => r === "audit-due"), ["audit-due"], `expected exactly one audit-due round, got ${JSON.stringify(gateSeq)}`); assert.equal(gateSeq.indexOf("audit-due"), 9, `audit-due expected at loop index 9 (turn 10), got ${JSON.stringify(gateSeq)}`); assert.equal(gateSeq[10], "fast", `round right after the audit must recover to fast, got ${JSON.stringify(gateSeq)}`); - // ≥ 12 fast rounds = at least TWO full audit waves served green: the first - // proves the lane runs, the second proves any legacy round CONSUMES the - // audit obligation (pre-fix, tick froze at N and every later turn rode - // legacy forever — masked at the time by per-round recasts). - assert.ok(fastRounds >= 12, `expected ≥2 audit waves of fast rounds, got ${fastRounds} (audit obligation not consumed?)`); - assert.ok(recastOps >= 1, "strategy flip at turn 14 must force ≥1 recast"); - assert.ok(legacyRounds >= 2, `arm + audit/caps-flip rounds must ride legacy (got ${legacyRounds})`); + // Exactly two fast waves before death: 2-9 (pre-audit) + 11-13 (post-audit). + assert.ok(fastRounds >= 11, `expected ≥11 fast rounds (two audit waves minus the post-death rounds), got ${fastRounds}`); + // NO-RECAST RULING pins: the API offers no recast op at all (the old + // counter cannot even be written); the flip's divergent bytes die the + // lane at turn 14, and every subsequent round reports the durable reason. + assert.equal(gateSeq.indexOf("caps-mismatch"), 13, `flip red expected at loop index 13 (turn 14), got ${JSON.stringify(gateSeq)}`); + assert.equal(gateSeq.indexOf("lane-dead"), 14, `first death report expected at loop index 14 (turn 15), got ${JSON.stringify(gateSeq)}`); + assert.ok(legacyRounds >= 5, `arm + audit + flip + post-death rounds must ride legacy (got ${legacyRounds})`); // Volume discipline (steady deltas must stay O(new) per round, never // O(full list)): pre-tail-fix behavior re-submitted the whole list each // green round ⇒ ~8×final volume here. const finalRowCount = hB.store.pfRenderRows(sidB!).length; - assert.ok(totalSubmittedRows <= 3 * finalRowCount, - `submitted row-volume ${totalSubmittedRows} vs final table ${finalRowCount} — steady rounds must append tails, not full lists`); - // S4 truth intact at the end; mirror agrees with the last golden shape + assert.equal(finalRowCount, 13, "row table froze at the death turn (no rewrite, no further commit)"); + // Volume discipline — steady rounds must stay O(new): every row enters + // the table AT MOST ONCE via a steady append (a duplicate-append + // corruption would push this past the table size). Legacy full lists are + // inherent to their (rare) rounds and are not pinned. + assert.ok(steadySubmittedRows <= finalRowCount, + `steady-mode submissions ${steadySubmittedRows} exceeded table size ${finalRowCount} — rows must be appended at most once`); + // S4 truth intact at the end; mirror froze together with the table assert.equal(hB.store.pfRenderCkptIntact(sidB!), true); const dbgLhs = watermarkV6RenderDebugState("s-gold-b"); assert.ok(Array.isArray(dbgLhs.sids) && dbgLhs.sids.length > 0, "render debug state must list the mirror"); - const lhsRows = dbgLhs.sids[dbgLhs.sids.length - 1].rows; - const rhsRows = goldens[goldens.length - 1].rows.length; - assert.equal(lhsRows, rhsRows, "mirror row count must equal last golden row count"); + const lastW = dbgLhs.sids[dbgLhs.sids.length - 1]; + assert.equal(lastW.dead, true, "debug state must surface the death verdict"); + assert.equal(lastW.rows, 13, "mirror row count froze at death"); // Sums algebra pin (review P1-2): every row's cost entered the cumulative // ledger EXACTLY once — a frozen-window double-count would overshoot here. const finalRowCost = hB.store.pfRenderRows(sidB!).reduce((s, r) => s + (r.rowJson?.length ?? 0), 0); @@ -453,9 +556,11 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" tokenCount: 100, modelContextLimit: MODEL_LIMIT, caps: capsFor("none"), snapshotLaneActive: false, absorbActive: false, ccrOn: false, foldUsagePending: false, markerNonZero: false, log: hB.log, }); - assert.ok(g17.ok === true || g17.reason === "audit-due", - `post-restart gate must hydrate from disk (got ${JSON.stringify(g17)})`); - if (g17.ok) assert.equal(g17.rw.rows.length, goldens.at(-1)!.rows.length, "hydrated row count must match the pre-restate capture"); + // The persisted kind='dead' verdict survives the process-restart wipe: + // hydration REFUSES to re-arm a dead plane (no-recast ruling — there is + // no rebuild path), and the identity lane keeps working independently. + assert.deepEqual(g17, { ok: false, reason: "lane-dead" }, + `post-restart gate must honor the persisted death verdict (got ${JSON.stringify(g17)})`); v17.fin(); hB.cleanup(); }); From 7577b31f2908fb654f2ba138950972dbdc121892 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 11:06:54 +0800 Subject: [PATCH 10/11] fix(fastpath): defer the arm cast over a thrown row-table read instead of failing the request (#5664, review round 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The no-recast sealing (49ba14dae) added a store.pfRenderRows pre-existing check to the arm branch, but SessionStore.pfRenderRows has no local try/catch and station 2 runs OUTSIDE prepare-openai's try block — a transient SELECT failure would have propagated out of prepareOpenai and failed the whole model request, an availability regression vs the old fail-soft arm path. A thrown read carries no divergence evidence, so it defers the arm to the next round (warn + op none) instead of killing the plane or crashing the caller. Test pins both directions: flaky-read round fails soft AND the next healthy round arms normally (transient != death). --- src/server/fastpath-v6-render.ts | 13 ++++++++++++- tests/issue5664-render-snapshot.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/server/fastpath-v6-render.ts b/src/server/fastpath-v6-render.ts index f2f7e029f7..2d08a17a95 100644 --- a/src/server/fastpath-v6-render.ts +++ b/src/server/fastpath-v6-render.ts @@ -495,7 +495,18 @@ export function commitRenderCapture(args: CaptureArgs): { committed: boolean; op // no post-death rebuild): if rows already exist on disk, we cannot // verify them here and rewriting them is forbidden ⇒ kill. if (args.mode === "legacy") { - const preExisting = store.pfRenderRows(sid); + let preExisting: { pos: number; rowJson: string | null }[]; + try { + preExisting = store.pfRenderRows(sid); + } catch { + // A THROWN read is a transient storage hiccup, not evidence of + // divergence: defer the arm to the next round (fail soft) + // instead of killing over it — and never propagate, station 2 + // runs outside prepare-openai's try block, a throw here would + // fail the whole request. + log?.("warn", `[${sid}] #5664 render arm deferred this round: row-table read threw`); + return { committed: false, op: "none" }; + } if (preExisting.length > 0) { killRenderPlane(store, sid, `arm refused over non-empty rows (${preExisting.length} pre-existing, unverifiable)`, log); return { committed: false, op: "none" }; diff --git a/tests/issue5664-render-snapshot.test.ts b/tests/issue5664-render-snapshot.test.ts index 1a0d15af30..2b7fd27838 100644 --- a/tests/issue5664-render-snapshot.test.ts +++ b/tests/issue5664-render-snapshot.test.ts @@ -302,6 +302,29 @@ test("unit: plane death persists across restart (no re-arm ever)", (t) => { } finally { h.cleanup(); } }); +test("unit: thrown row-table read defers arming without killing (transient ≠ death)", () => { + const h = makeH("defer"); + try { + const b1 = makeBody([{ role: "user", content: "hello" }]); + const r1 = v6Eval(h, "s-defer", b1, ["id0"]); + r1.fin(); + const sid = r1.sid!; + const caps = capsFor("text-only"); + // Prototype-chain wrapper: every store method resolves to the REAL + // sidecar except pfRenderRows, which throws — a transient SELECT + // failure while everything else is healthy. + const flakyStore: SessionStore = Object.create(h.store); + flakyStore.pfRenderRows = () => { throw new Error("io hiccup"); }; + assert.deepEqual( + commitRenderCapture({ sessionId: "s-defer", store: flakyStore, sid, mode: "legacy", caps, rowStrings: ['{"i":0}', '{"i":1}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }), + { committed: false, op: "none" }, "a thrown read must fail soft for this round, not crash the caller"); + // Negative side (#3617): no death verdict may persist from a transient + // error — the very next round with a healthy store arms normally. + assert.equal(commitRenderCapture({ sessionId: "s-defer", store: h.store, sid, mode: "legacy", caps, rowStrings: ['{"i":0}', '{"i":1}'], sums: { estCum: 2, textCum: 2, imgCum: 0 } }).op, "cast"); + assert.equal(h.store.pfRenderRows(sid).length, 2); + } finally { h.cleanup(); } +}); + // Review round 1 blind-spot pins (issue #5664): session teardown must wipe the // three render tables in the same cascade, and re-running the migration on an // already-v12 sidecar must be a no-op that leaves rows untouched. From f3681735fa4315d83936928eb4a8f06c4cc387ed Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 5 Nov 2026 13:16:43 +0800 Subject: [PATCH 11/11] =?UTF-8?q?fix(fastpath):=20render=20rows=20hold=20t?= =?UTF-8?q?he=20post-rewrite=20generic=20core=20form=20=E2=80=94=20nothing?= =?UTF-8?q?=20mapped=20is=20ever=20cached=20(#5643=20=C2=A75=20ruling,=20#?= =?UTF-8?q?5664)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Owner ruling on #5643 (2026-11-05, canonical flow): the only plane boundary that can never be altered by model/maxDim/toolArmed/voice/harden/serialize is the post-kernel-rewrite generic core form. Rebuild the plane around it: - RenderCaps drops model/maxDim/toolArmed — they are per-turn mapping inputs, not snapshot validity (caps now: v, strategy, droppable, multimodal). - Station 3 green round: verify this round's rewritten window cores against the stored suffix at the core-form boundary; the full core sequence (cached prefix ++ window output) then passes through the FULL mapping chain fresh this turn (rescale/harden/coreToOpenai/voice/serialize all run with this turn's inputs). Within-family model switches ride through with zero events — they re-map next turn by construction. - Captures (steady tail, legacy full, fallback) store core strings, never mapped wire bytes. Tripwire semantics unchanged (one-turn legacy fallback, no recast, durable kill on divergence); sums/audit/death machinery untouched. - Tests: golden A/B rewritten to the core regime with a shape pin asserting every stored row carries core identity fields (id/contentType) and no wire bytes are persisted. --- src/server/fastpath-v6-render.ts | 58 ++++++++++++++---------- src/server/prepare-openai.ts | 59 ++++++++++++++----------- tests/issue5664-render-snapshot.test.ts | 57 +++++++++++++++--------- 3 files changed, 104 insertions(+), 70 deletions(-) diff --git a/src/server/fastpath-v6-render.ts b/src/server/fastpath-v6-render.ts index 2d08a17a95..78f7cc8ee4 100644 --- a/src/server/fastpath-v6-render.ts +++ b/src/server/fastpath-v6-render.ts @@ -69,15 +69,19 @@ export const PF_RENDER_EMERGENCY_RATIO = 0.9; const MIRROR_BYTE_BUDGET = 256 * 1024 * 1024; let mirrorBytes = 0; -/** Capabilities fingerprint — any change red-gates the lane (caps-mismatch); - * if the legacy round's bytes then diverge from the stored rows the plane - * is killed for this sid (no-recast regime). */ +/** Identity fingerprint — the ONLY dimensions that may change the stored + * row contents: the kernel-side rewrite surface (render strategy, + * reasoning-drop predicate) plus the core-shape class (multimodal). + * Any change red-gates that turn (caps-mismatch); if a subsequent legacy + * full pass then diverges from the stored rows the plane is killed for + * this sid (no-recast regime). model/maxDim/toolArmed are DELIBERATELY + * ABSENT: they are per-turn mapping-layer inputs (#5643 ruling §5, owner + * 2026-11-05) — rows hold the post-rewrite generic core form, which no + * mapping input can alter, so a within-family model switch simply re-maps + * next turn with zero events and never touches snapshot validity. */ export interface RenderCaps { v: 1; - model: string; strategy: string; - maxDim: number; - toolArmed: boolean; droppable: boolean; multimodal: boolean; } @@ -85,10 +89,7 @@ export interface RenderCaps { function capsEqual(a: RenderCaps, b: RenderCaps): boolean { return ( a.v === b.v && - a.model === b.model && a.strategy === b.strategy && - a.maxDim === b.maxDim && - a.toolArmed === b.toolArmed && a.droppable === b.droppable && a.multimodal === b.multimodal ); @@ -105,9 +106,14 @@ interface RenderMeta { export interface RenderWorld { sid: string; - /** Outbound JSON string per rendered ordinal, contiguous 0..n-1. */ + /** Canonical post-kernel-rewrite generic core-form JSON per rendered + * ordinal (contiguous 0..n-1 over surviving elements). NEVER mapped + * wire bytes: the outbound message is re-mapped from these cores EVERY + * turn with that turn's model/mapping inputs (#5643 ruling §5 ②). */ rows: string[]; - /** Parsed caches of `rows` (same index) — the splice reads these. */ + /** Parsed caches of `rows` (same index) — the green round splices these + * core rows ahead of the window output; they double as next round's + * verify baseline, so no downstream consumer may alias-mutate them. */ rowsParsed: unknown[]; caps: RenderCaps; /** Cumulative Σ estimateCoreMessagesUpper over all current rows. */ @@ -131,11 +137,14 @@ const mirrors = new Map(); const sessionSids = new Map>(); /** Owner ruling 2026-11-05 (#5689 review): NO recast in ANY scenario. - * Structural divergence (byte diff/shrink), caps flips that changed stored - * bytes, S4 corruption, row-table inconsistency, unverifiable pre-existing - * snapshots — all land here. A dead sid NEVER arms, hydrates, gates green, - * or commits again. The verdict is persisted as a kind='dead' log entry so - * every future process instance reaches the same conclusion. */ + * Structural divergence (byte diff/shrink at the core-form boundary), S4 + * corruption, row-table inconsistency, unverifiable pre-existing snapshots + * — all land here. Caps changes cannot alter stored bytes anymore (mapping + * inputs sit outside the fingerprint, #5643 ruling §5 ③): their failure + * mode is a red gate turn, absorbed by the audit cadence. A dead sid NEVER + * arms, hydrates, gates green, or commits again. The verdict is persisted + * as a kind='dead' log entry so every future process instance reaches the + * same conclusion. */ const deadSids = new Set(); export function killRenderPlane(store: SessionStore, sid: string, reason: string, log?: LogFn): void { @@ -385,10 +394,11 @@ export function bumpAuditTick(sid: string): void { // Fast-round verification + splice (station 3) // --------------------------------------------------------------------------- -/** Compare the mapped first-F fast outputs against the stored suffix. The - * caller has already run the SAME mapping chain (rescale→harden→ - * coreToOpenai→systemToUser voice fn) as the baseline capture, so this is - * a pure byte compare. Returns false on ANY difference (incl. length). */ +/** Compare THIS round's first-F rewritten window cores against the stored + * suffix AT THE CORE-FORM BOUNDARY (#5643 ruling §5 ③: the tripwire sits + * on post-rewrite generic cores — never on mapped wire bytes; the mapping + * chain runs fresh every turn and its inputs are excluded from identity). + * Pure byte compare. Returns false on ANY difference (incl. length). */ export function verifyRenderWindow(fastEmitsJson: string[], rw: RenderWorld, F: number): boolean { if (fastEmitsJson.length < F) return false; const start = rw.rows.length - F; @@ -399,9 +409,11 @@ export function verifyRenderWindow(fastEmitsJson: string[], rw: RenderWorld, F: return true; } -/** Splice: prefix parsed rows (cached — zero parse/stringify cost) + mapped - * fast tail. Row indices align with rendered ordinals (probe-verified - * positional determinism; the byte check above is the tripwire). */ +/** Splice: prefix parsed CORE rows (cached) + this round's rewritten core + * tail. Row indices align with rendered ordinals (probe-verified + * positional determinism; the core-form byte check above is the tripwire). + * The concatenated core sequence then passes through the FULL mapping + * chain fresh this turn (#5643 ruling §5 ②) — nothing mapped is cached. */ export function spliceOutboundRows(rw: RenderWorld, F: number, tail: T[]): T[] { const prefixLen = rw.rows.length - F; if (prefixLen < 0) throw new Error("#5664 splice prefix underflow"); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index df9cf00f35..f64968a5c7 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -252,10 +252,7 @@ export async function prepareOpenai( renderCapturedSid = renderSid; const rcaps: RenderCaps = { v: 1, - model: modelIdOf(parsed) ?? "", strategy: renderStrategy, - maxDim: imageRescaleMaxDim(), - toolArmed: imageRescaleToolArmed(session), droppable: loopConfig.compressAtFoldPointClearThinking === true, multimodal: wpHit.seedMultimodal === true || wpHit.windowCores.some((m) => Array.isArray((m as { content?: unknown }).content)), }; @@ -526,29 +523,36 @@ export async function prepareOpenai( const mapEgress = async (pms: CoreMessage[]): Promise => hardenOpenaiAssistantContent(coreToOpenai(systemToUser(await applyImageRescalePass(pms as BiliMessage[], { session, sessionId, log, maxDim: imageRescaleMaxDim(), toolArmed: imageRescaleToolArmed(session) }), summaryVoiceFn))); if (renderFast !== null) { - // Station 3: prefix rows pass through verbatim (cached parses); - // the mapped tail replaces the last F stored rows AND extends them. - // The byte check against the stored suffix is the sole guard — on - // mismatch redo the legacy full pass THIS turn; there is no row - // repair anymore (no-recast ruling 2026-11-05) — if the stored - // rows and truth genuinely diverge, the next full-path capture - // kills this sid's plane permanently instead of recasting. - const mappedAll = await mapEgress(processedMessages); + // Station 3 (#5643 ruling §5, owner 2026-11-05 — canonical flow): + // stored rows are the POST-REWRITE GENERIC CORE FORM, so the + // tripwire compares this round's rewritten window cores against + // the stored suffix AT THAT BOUNDARY (never mapped wire bytes). + // On match the full core sequence (cached prefix ++ window + // output) passes through the mapping chain FRESH this turn — + // rescale/harden/coreToOpenai/voice/serialize all run with THIS + // turn's model/maxDim/toolArmed inputs; nothing mapped is cached. + // On mismatch redo the legacy full pass THIS turn; there is no + // row repair (no-recast ruling) — if stored rows and truth + // genuinely diverge, the next full-path capture kills this sid's + // plane permanently instead of recasting. const rw = renderFast.rw; - const wpWindowLen = wpHit?.windowCores.length ?? 0; - const committedTail = mappedAll.slice(0, Math.min(renderFast.F + wpWindowLen, mappedAll.length)); - // Image reserve DELTA covers only this turn's NEW rows — the F - // frozen-window rows' reserve already sits in rw.imgCum; counting - // them again here drifts the ledger by a whole window each green - // round (review P1-2). slice(F) aligns with the captured tail. - renderedImageDelta = imageReserveFor(session, "openai", { model: parsed.model, messages: committedTail.slice(renderFast.F) } as unknown as OpenAIRequestBody, opts, billingUpstream ?? upstreamOrigin); - if (verifyRenderWindow(committedTail.slice(0, renderFast.F).map((m) => JSON.stringify(m)), rw, renderFast.F)) { - rebuiltMessages = spliceOutboundRows(rw, renderFast.F, committedTail); - renderedCaptureStrings = [...rw.rows.slice(0, rw.rows.length - renderFast.F), ...committedTail.map((m) => JSON.stringify(m))]; - renderedTailObjects = [...committedTail]; - log("debug", `[${sessionId}] #5664 render snapshot FAST emit (prefix=${rw.rows.length - renderFast.F} cached rows, tail=${committedTail.length} mapped)`); + const winCores = processedMessages; + if (verifyRenderWindow(winCores.slice(0, renderFast.F).map((m) => JSON.stringify(m)), rw, renderFast.F)) { + const fullCores = spliceOutboundRows(rw, renderFast.F, winCores); + rebuiltMessages = await mapEgress(fullCores); + // Image reserve DELTA covers only this turn's NEW elements — + // the F frozen-window rows' reserve already sits in rw.imgCum; + // counting them again drifts the ledger by a whole window each + // green round (review P1-2). The full sequence is + // [prefix n-F][frozen F][kNew], so the kNew fresh tails start + // at index n = rw.rows.length — slice(len) is exactly them. + // Billing metadata: computed on the fresh map. + renderedImageDelta = imageReserveFor(session, "openai", { model: parsed.model, messages: rebuiltMessages.slice(rw.rows.length) } as unknown as OpenAIRequestBody, opts, billingUpstream ?? upstreamOrigin); + renderedCaptureStrings = winCores.slice(renderFast.F).map((m) => JSON.stringify(m)); + renderedTailObjects = [...winCores]; + log("debug", `[${sessionId}] #5664 render snapshot FAST emit (prefix=${rw.rows.length - renderFast.F} cached core rows, tail=${winCores.length - renderFast.F} new cores, outbound=${rebuiltMessages.length} re-mapped this turn)`); } else { - log("warn", `[${sessionId}] #5664 render snapshot byte mismatch vs stored rows — legacy full pass this turn (no row repair exists: the next full-path capture kills the plane instead of recasting)`); + log("warn", `[${sessionId}] #5664 render snapshot core-form mismatch vs stored rows — legacy full pass this turn (no row repair exists: the next full-path capture kills the plane instead of recasting)`); renderFast = null; const turnFull = core.processTurn({ messages: renderMsgs, state: session.state, config: loopConfig, tokenCount, renderTags: renderStrategy, contentStore: contentStoreOf(session) }); session.state = turnFull.state; @@ -556,7 +560,7 @@ export async function prepareOpenai( const pmFull = applyReasoningDrop(stripKernelSummaries(turnFull.messages, turnFull.state), planReasoningDrop(renderMsgs, reasoning, isStrictReasoningEcho(session, upstreamOrigin, modelIdOf(parsed))), log, sessionId); processedMessages = pmFull; rebuiltMessages = await mapEgress(pmFull); - renderedCaptureStrings = rebuiltMessages.map((m) => JSON.stringify(m)); + renderedCaptureStrings = pmFull.map((m) => JSON.stringify(m)); renderedTailObjects = []; renderedImageDelta = 0; } @@ -564,8 +568,9 @@ export async function prepareOpenai( rebuiltMessages = await mapEgress(processedMessages); // Lane-durably-dead sessions never arm/commit again — keep the // strings null so station 2 skips both their build (O(n) per - // turn) and the doomed commit call. - if (!renderLaneDead) renderedCaptureStrings = rebuiltMessages.map((m) => JSON.stringify(m)); + // turn) and the doomed commit call. Rows store the post-rewrite + // core form (#5643 §5), so capture from cores, never wire bytes. + if (!renderLaneDead) renderedCaptureStrings = processedMessages.map((m) => JSON.stringify(m)); renderedTailObjects = []; renderedImageDelta = 0; } diff --git a/tests/issue5664-render-snapshot.test.ts b/tests/issue5664-render-snapshot.test.ts index 2b7fd27838..4f330d950f 100644 --- a/tests/issue5664-render-snapshot.test.ts +++ b/tests/issue5664-render-snapshot.test.ts @@ -1,18 +1,25 @@ // #5664 — station 2/3: outbound-row render-snapshot plane for the v6 lane. // -// Golden equation under test: on every engaged turn the emit array built as -// [cached prefix rows] + [freshly mapped tail] (fast rounds) must be BYTE- +// Golden equation under test: on every engaged turn the outbound emit built +// as [cached prefix core rows] ++ [this round's rewritten window cores] +// passed through the FULL mapping chain fresh (fast rounds) must be BYTE- // IDENTICAL to what the legacy full-pipeline path emits (arm / audit / -// rollback rounds). The kernel is untouched; this lane caches egress bytes, -// not semantics — equivalence with the full pass is the contract. +// rollback rounds). The kernel is untouched. +// +// CANONICAL-FLOW REGIME (#5643 ruling §5, owner 2026-11-05): stored rows +// hold the POST-REWRITE GENERIC CORE FORM — the only plane boundary the +// mapping layer (model/maxDim/toolArmed/voice/harden/serialize) can never +// alter — and NOTHING mapped/cached survives across turns: every emit runs +// the entire chain end-to-end. Equivalence with the full pass is the +// contract, enforced here against a legacy oracle. // // NO-RECAST REGIME (owner ruling 2026-11-05, sealed during #5689 review): -// recast is FORBIDDEN in every scenario — any stored-row divergence (byte -// diff, shrink, caps flip that changed bytes, checksum corruption, -// unverifiable pre-existing snapshot) KILLS the plane for that sid -// permanently (persisted kind='dead' log entry) and the session rides the -// legacy full path forever after. Storage is strictly append-only (+ dead -// markers); arm cast is legal exactly once, onto an empty table. +// recast is FORBIDDEN in every scenario — any stored-row divergence (core-form +// diff vs the rewritten window, shrink, non-append capture on a live lane, +// checksum corruption, unverifiable pre-existing snapshot) KILLS the plane +// for that sid permanently (persisted kind='dead' log entry) and the session +// rides the legacy full path forever after. Storage is strictly append-only +// (+ dead markers); arm cast is legal exactly once, onto an empty table. // // Harness: unit-level against a REAL sqlite sidecar (UnifiedShadow shadow // mode), mirroring tests/fastpath-v6-msgid.test.ts; the kernel runs as a @@ -66,7 +73,7 @@ function mapEgress(pms: CoreMessage[]): OpenAIMessage[] { } function capsFor(strategy: string): RenderCaps { - return { v: 1, model: "m", strategy, maxDim: 1_600, toolArmed: false, droppable: false, multimodal: false }; + return { v: 1, strategy, droppable: false, multimodal: false }; } /** Shallow-adopt a returned state into a stable object identity (the session @@ -466,25 +473,27 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" stateB.nextRuleId = Math.max(stateB.nextRuleId ?? 1, (scratch as AgentState).nextRuleId ?? 1); const pmW = stripKernelSummaries(tW.messages, tW.state as never); - const mappedAll = mapEgress(pmW); - const committedTail = mappedAll.slice(0, Math.min(F + (v.outcome.hit?.windowCores.length ?? 0), mappedAll.length)); - const vv = verifyRenderWindow(committedTail.slice(0, F).map((m) => JSON.stringify(m)), rw, F); - assert.equal(vv, true, `turn ${tn}: window verify failed — tail bytes drifted vs stored rows`); - rebuilt = spliceOutboundRows(rw, F, committedTail); - captureStrings = [...rw.rows.slice(0, rw.rows.length - F), ...committedTail.map((m) => JSON.stringify(m))]; + // #5643 §5 canonical-flow regime: rows store the POST-REWRITE + // GENERIC CORE FORM — verify at that boundary, then pass the full + // core sequence through the mapping chain fresh this turn. + const vv = verifyRenderWindow(pmW.slice(0, F).map((m) => JSON.stringify(m)), rw, F); + assert.equal(vv, true, `turn ${tn}: window verify failed — core form drifted vs stored rows`); + rebuilt = mapEgress(spliceOutboundRows(rw, F, pmW)); + captureStrings = [...rw.rows.slice(0, rw.rows.length - F), ...pmW.slice(F).map((m) => JSON.stringify(m))]; // steady-mode contract: only THIS turn's new rows are appended to // the plane (the slice past the F frozen window); the full list // stays in captureStrings for the golden equation below. - steadyAppendRows = committedTail.slice(F).map((m) => JSON.stringify(m)); + steadyAppendRows = pmW.slice(F).map((m) => JSON.stringify(m)); mode = "steady"; fastRounds++; } else { // legacy full path over the engaged view (production shape) const tF = core.processTurn({ messages: hit !== null ? [...hit.view] : (openaiToCore(JSON.parse(bodyText) as never) as unknown as { msgs: CoreMessage[] }).msgs, state: stateB, config: cfg, tokenCount, renderTags: strategy }); adoptState(stateB, tF.state as unknown as AgentState); - const mappedAll = mapEgress(stripKernelSummaries(tF.messages, tF.state as never)); + const pmF = stripKernelSummaries(tF.messages, tF.state as never); + const mappedAll = mapEgress(pmF); rebuilt = mappedAll; - captureStrings = mappedAll.map((m) => JSON.stringify(m)); + captureStrings = pmF.map((m) => JSON.stringify(m)); mode = "legacy"; legacyRounds++; } @@ -507,6 +516,14 @@ test("golden: cached-prefix splice equals the legacy full-path bytes end-to-end" totalSubmittedText += rowCost(srcRows); if (mode === "steady") steadySubmittedRows += srcRows.length; if (op.committed && op.op === "cast") castOps++; + // #5643 §5 shape pin: every stored row is POST-REWRITE CORE FORM — + // core identity fields present, wire bytes impossible (neither field + // exists on OpenAIMessage). + if (op.committed) for (const s of srcRows) { + const rowObj = JSON.parse(s) as { id?: unknown; contentType?: unknown }; + assert.equal(typeof rowObj.id, "string", "stored row must be core form, not mapped wire bytes"); + assert.equal(typeof rowObj.contentType, "string", "stored row must be core form, not mapped wire bytes"); + } // THE golden equation: lane emit == legacy oracle, byte for byte. assert.deepEqual(rebuilt.map((m) => JSON.stringify(m)), goldens[tn - 1].rows, `turn ${tn} (${mode}${gate.ok ? "" : `, reason=${gate.reason}`}): lane emit diverged from legacy oracle`);