diff --git a/src/storage/backend.ts b/src/storage/backend.ts index 4af7c27859..4f8b260865 100644 --- a/src/storage/backend.ts +++ b/src/storage/backend.ts @@ -75,8 +75,8 @@ export interface StorageBackend { seqByRaw(sessionId: string): Promise | null>; /** #4316 PR3/3: fork adoption row clone (idempotent). */ cloneSessionRows(parentId: string, childId: string, throughSeq: number, initial?: IngestSessionInput): Promise; - getMessages(sessionId: string): Promise<(MessageRow & { contentHash: string })[]>; - getBlocks(sessionId: string): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]>; + getMessages(sessionId: string, opts?: { includeRetired?: boolean; seqs?: readonly number[] }): Promise<(MessageRow & { contentHash: string })[]>; + getBlocks(sessionId: string, opts?: { limit: number; offset?: number }): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]>; getDeadRefs(sessionId: string): Promise<{ ord: number; ref: string; refNum: number; recordedAt: number }[]>; getRefs(sessionId: string): Promise; getCcr(sessionId: string): Promise; @@ -190,11 +190,11 @@ export class SqliteBackend implements StorageBackend { cloneSessionRows(parentId: string, childId: string, throughSeq: number, initial?: IngestSessionInput): Promise { return Promise.resolve(this.store.cloneSessionRows(parentId, childId, throughSeq, initial)); } - getMessages(sessionId: string): Promise<(MessageRow & { contentHash: string })[]> { - return Promise.resolve(this.store.getMessages(sessionId)); + getMessages(sessionId: string, opts?: { includeRetired?: boolean; seqs?: readonly number[] }): Promise<(MessageRow & { contentHash: string })[]> { + return Promise.resolve(this.store.getMessages(sessionId, opts)); } - getBlocks(sessionId: string): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]> { - return Promise.resolve(this.store.getBlocks(sessionId)); + getBlocks(sessionId: string, opts?: { limit: number; offset?: number }): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]> { + return Promise.resolve(this.store.getBlocks(sessionId, opts)); } getDeadRefs(sessionId: string): Promise<{ ord: number; ref: string; refNum: number; recordedAt: number }[]> { return Promise.resolve(this.store.getDeadRefs(sessionId)); diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index f3ecdf8578..77b2b1ef75 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -1336,13 +1336,14 @@ export class MysqlBackend implements StorageBackend { }; } - async getMessages(sessionId: string, opts?: { includeRetired?: boolean }): Promise<(MessageRow & { contentHash: string; retiredAt: number | null })[]> { + async getMessages(sessionId: string, opts?: { includeRetired?: boolean; seqs?: readonly number[] }): Promise<(MessageRow & { contentHash: string; retiredAt: number | null })[]> { // #4390: retired rows stay out of the replay by default. + if (opts?.seqs?.length === 0) return []; + const retiredFilter = opts?.includeRetired === true ? "" : " AND retired_at IS NULL"; + const seqFilter = opts?.seqs ? ` AND seq IN (${opts.seqs.map(() => "?").join(", ")})` : ""; const [rows] = (await this.pool.query( - opts?.includeRetired === true - ? "SELECT seq, msg_id, ref, ref_num, role, kind, content_hash, retired_at FROM messages WHERE session_id = ? ORDER BY seq" - : "SELECT seq, msg_id, ref, ref_num, role, kind, content_hash, retired_at FROM messages WHERE session_id = ? AND retired_at IS NULL ORDER BY seq", - [sessionId], + `SELECT seq, msg_id, ref, ref_num, role, kind, content_hash, retired_at FROM messages WHERE session_id = ?${retiredFilter}${seqFilter} ORDER BY seq`, + [sessionId, ...(opts?.seqs ?? [])], )) as unknown as Row[][]; const out: (MessageRow & { contentHash: string; retiredAt: number | null })[] = []; for (const r of rows) { @@ -1399,12 +1400,12 @@ export class MysqlBackend implements StorageBackend { return out; } - async getBlocks(sessionId: string): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]> { + async getBlocks(sessionId: string, opts?: { limit: number; offset?: number }): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]> { const [rows] = (await this.pool.query( `SELECT block_id, ord, start_seq, end_seq, start_ref, end_ref, tier, summary_hash, one_hash, supersedes_block_id, active, created_at, covered_hash, payload_json - FROM blocks WHERE session_id = ? ORDER BY ord`, - [sessionId], + FROM blocks WHERE session_id = ? ORDER BY ord, block_id${opts ? " LIMIT ? OFFSET ?" : ""}`, + [sessionId, ...(opts ? [opts.limit, opts.offset ?? 0] : [])], )) as unknown as Row[][]; const out: (BlockRow & { summaryHash: string; oneHash: string | null })[] = []; for (const r of rows) { diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index f7d66cc8d5..b5db7a08bc 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -32,10 +32,11 @@ import { resolveBackendName, backendSelectionNotice, selectedBackendRaw, openBac import { canonicalize, sha256 } from "./canonical.ts"; import { recordSidecarInstance, recordSidecarOpenFailure } from "./runtime-status.ts"; import type { SqliteEngineName, SqliteEngineSource } from "./driver.ts"; -import type { MessageRow, MessageRefContentRow } from "./store.js"; +import type { MessageRow, BlockRow, SessionRow, RefRow, MessageRefContentRow } from "./store.js"; import type { CoreMessage, MessageContentStore } from "acp-kernel"; export const MAX_CONSECUTIVE_FAILURES = 3; +const VERIFY_BATCH_SIZE = 32; /** #3133: read-back verification cadence — the verify leg (full read-back + * double shadowDigest) is O(log) per save, while the drift it guards is a @@ -377,20 +378,18 @@ export class UnifiedShadow { this.stats.verifySamples++; const sessionRow = this.sync.getSessionRow(legacy.id); if (!sessionRow) throw new Error(`session row missing after ingest: ${legacy.id}`); - const rows = { - session: sessionRow, - messages: this.sync.getMessages(legacy.id), - blocks: this.sync.getBlocks(legacy.id), - deadRefs: this.sync.getDeadRefs(legacy.id), - refs: this.sync.getRefs(legacy.id), - }; - const replayed = loadLegacyView(rows); - warnTailGaps(legacy.id, replayed); - const expectView = { ...legacy, blockContents: orderedBlockContents(completeBlockContents(legacy.blockContents, legacy.state.blocks)) }; - const gotView = { ...replayed, blockContents: orderedBlockContents(replayed.blockContents) }; - const expect = shadowDigest(expectView); - const got = shadowDigest(gotView); - if (got !== expect) { + const verifier = new BatchedViewVerification(legacy, sessionRow, this.sync.getRefs(legacy.id), this.sync.getDeadRefs(legacy.id)); + for (let offset = 0; ; offset += VERIFY_BATCH_SIZE) { + const rows = this.sync.getBlocks(legacy.id, { limit: VERIFY_BATCH_SIZE, offset }); + verifier.addBlocks(rows); + if (rows.length < VERIFY_BATCH_SIZE) break; + } + for (let i = 0; i < verifier.messageSeqs.length; i += VERIFY_BATCH_SIZE) { + verifier.addMessages(this.sync.getMessages(legacy.id, { seqs: verifier.messageSeqs.slice(i, i + VERIFY_BATCH_SIZE) })); + } + const verified = verifier.finish(); + warnTailGaps(legacy.id, verified); + if (!verified.matches) { this.stats.verifyDrift++; // #3919: membership from BEFORE this add is the repeat // signal — two consecutive sampled drifts mean the views @@ -416,8 +415,8 @@ export class UnifiedShadow { this.frozenEnvelopeIds.add(legacy.id); let sides = ""; try { - const envBlocks = expectView.state.blocks.filter((b) => b.active).length; - const idxBlocks = (gotView.state.blocks as { active?: boolean }[] | null | undefined)?.filter((b) => b.active === true).length ?? 0; + const envBlocks = legacy.state.blocks.filter((b) => b.active).length; + const idxBlocks = verified.activeBlocks; sides = ` [envelope ${envBlocks} active block(s) vs index ${idxBlocks}]`; } catch { // counts must never break the heal path @@ -443,7 +442,7 @@ export class UnifiedShadow { // The line prefix stays byte-identical for existing greps. let diagnosis = ""; try { - diagnosis = formatDriftDiagnostics(describeViewDivergence(expectView, gotView)); + diagnosis = formatDriftDiagnostics(verified.diagnostics); } catch { // diagnostics must never break the heal path } @@ -959,17 +958,18 @@ export class UnifiedShadow { this.stats.verifySamples++; const sessionRow = await backend.getSessionRow(legacy.id); if (!sessionRow) throw new Error(`session row missing after ingest: ${legacy.id}`); - const rows = { - session: sessionRow, - messages: await backend.getMessages(legacy.id), - blocks: await backend.getBlocks(legacy.id), - deadRefs: await backend.getDeadRefs(legacy.id), - refs: await backend.getRefs(legacy.id), - }; - const loaded = loadLegacyView(rows); - const expect = shadowDigest({ ...legacy, blockContents: orderedBlockContents(completeBlockContents(legacy.blockContents, legacy.state.blocks)) }); - const got = shadowDigest({ ...loaded, blockContents: orderedBlockContents(loaded.blockContents) }); - if (got !== expect) { + const verifier = new BatchedViewVerification(legacy, sessionRow, await backend.getRefs(legacy.id), await backend.getDeadRefs(legacy.id)); + for (let offset = 0; ; offset += VERIFY_BATCH_SIZE) { + const rows = await backend.getBlocks(legacy.id, { limit: VERIFY_BATCH_SIZE, offset }); + verifier.addBlocks(rows); + if (rows.length < VERIFY_BATCH_SIZE) break; + } + for (let i = 0; i < verifier.messageSeqs.length; i += VERIFY_BATCH_SIZE) { + verifier.addMessages(await backend.getMessages(legacy.id, { seqs: verifier.messageSeqs.slice(i, i + VERIFY_BATCH_SIZE) })); + } + const verified = verifier.finish(); + warnTailGaps(legacy.id, verified); + if (!verified.matches) { this.stats.verifyDrift++; this.driftedSessions.add(legacy.id); this.stats.lastError = `digest drift for ${legacy.id}: unified replay != legacy load view`; @@ -1246,13 +1246,6 @@ function shadowDigest(view: unknown): string { return sha256(JSON.stringify(canonicalize(view))); } -// #3407: the parity digest must compare blockContents CONTENTS, not insertion order — -// the expect leg keeps the live Map's order (completeBlockContents appends synthesized -// entries at the end) while the got leg lays blocks out in state.blocks array order. -function orderedBlockContents(m: Map): Map { - return new Map([...m.entries()].sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))); -} - interface DriftSlotFingerprint { id: string; role: string; @@ -1267,6 +1260,126 @@ interface DriftDiagnostics { tailSlot?: { index: number; expect: DriftSlotFingerprint; got: DriftSlotFingerprint }; } +/** Compare the replay by component, retaining only message fingerprints across batches. */ +class BatchedViewVerification { + readonly messageSeqs: number[]; + private readonly tail: { tailSeqs: number[] | null; pluginSnapshotSeqs: number[] | null }; + private readonly remainingContents: LegacySessionLike["blockContents"]; + private readonly orphans: LegacySessionLike["blockContents"]; + private readonly messages = new Map>(); + private readonly keys = new Set(); + private drift = false; + private blockCount = 0; + private activeBlocks = 0; + private tailGaps = 0; + private tailSlot: DriftDiagnostics["tailSlot"]; + + constructor(private readonly legacy: LegacySessionLike, session: SessionRow, refs: RefRow[], deadRefs: { ord: number; ref: string }[]) { + const header = loadLegacyView({ session, refs, deadRefs, messages: [], blocks: [] }); + this.orphans = header.blockContents; + this.remainingContents = new Map(completeBlockContents(legacy.blockContents, legacy.state.blocks)); + this.tail = JSON.parse(session.tailJson); + this.messageSeqs = [...new Set([...(this.tail.tailSeqs ?? []), ...(this.tail.pluginSnapshotSeqs ?? [])])]; + const withoutBulk = (view: LegacySessionLike | typeof header) => ({ + ...view, + state: { ...view.state, blocks: [] }, + blockContents: new Map(), + lastMessages: view.lastMessages === undefined ? undefined : [], + pluginSnapshot: view.pluginSnapshot === undefined ? undefined : [], + }); + const expect = withoutBulk(legacy); + const got = withoutBulk(header); + if (shadowDigest(expect) !== shadowDigest(got)) { + this.drift = true; + try { + for (const key of describeViewDivergence(expect, got).keys) this.keys.add(key); + } catch { + // Diagnostic detail must not suppress a proven drift. + } + } + } + + addBlocks(rows: BlockRow[]): void { + for (const row of rows) { + const payload = JSON.parse(row.payloadJson); + if (payload?.active === true) this.activeBlocks++; + if (this.blockCount >= this.legacy.state.blocks.length || shadowDigest(payload) !== shadowDigest(this.legacy.state.blocks[this.blockCount])) this.changed("state"); + this.blockCount++; + this.compareContents(row.blockId, { one: row.one === null ? null : JSON.parse(row.one), full: JSON.parse(row.summary) }); + // A current block wins over an identically named journal entry. + this.orphans.delete(row.blockId); + } + } + + addMessages(rows: MessageRow[]): void { + for (const row of rows) this.messages.set(row.seq, messageSignature(JSON.parse(row.content))); + } + + private changed(key: string): void { + this.drift = true; + this.keys.add(key); + } + + private compareContents(id: string, view: unknown): void { + if (!this.remainingContents.has(id) || shadowDigest(this.remainingContents.get(id)) !== shadowDigest(view)) this.changed("blockContents"); + this.remainingContents.delete(id); + } + + private compareMessages(key: "lastMessages" | "pluginSnapshot", seqs: number[] | null): void { + const expected = this.legacy[key]; + let index = 0; + for (const seq of seqs ?? []) { + const got = this.messages.get(seq); + if (!got) { + this.tailGaps++; + continue; + } + const want = expected && index < expected.length ? messageSignature(expected[index]) : undefined; + if (!want || want.digest !== got.digest) { + this.changed(key); + if (key === "lastMessages" && expected && !this.tailSlot) { + this.tailSlot = { index, expect: want?.slot ?? missingSlot(), got: got.slot }; + } + } + index++; + } + if (seqs && expected && index < expected.length) { + this.changed(key); + if (key === "lastMessages" && !this.tailSlot) this.tailSlot = { index, expect: messageSignature(expected[index]).slot, got: missingSlot() }; + } + } + + finish(): { matches: boolean; activeBlocks: number; tailGaps: number; diagnostics: DriftDiagnostics } { + if (this.blockCount !== this.legacy.state.blocks.length) this.changed("state"); + for (const [id, view] of this.orphans) this.compareContents(id, view); + if (this.remainingContents.size > 0) this.changed("blockContents"); + this.compareMessages("lastMessages", this.tail.tailSeqs); + this.compareMessages("pluginSnapshot", this.tail.pluginSnapshotSeqs); + const diagnostics: DriftDiagnostics = { keys: [...this.keys].sort() }; + if (this.tailSlot) diagnostics.tailSlot = this.tailSlot; + return { matches: !this.drift, activeBlocks: this.activeBlocks, tailGaps: this.tailGaps, diagnostics }; + } +} + +function missingSlot(): DriftSlotFingerprint { + return { id: "-", role: "-", length: 0, sha8: "-" }; +} + +function messageSignature(value: unknown): { digest: string; slot: DriftSlotFingerprint } { + const json = JSON.stringify(canonicalize(value)) ?? "null"; + const digest = sha256(json); + const message = value as { id?: unknown; role?: unknown } | null; + return { + digest, + slot: !message || typeof message !== "object" ? missingSlot() : { + id: typeof message.id === "string" ? message.id : "-", + role: typeof message.role === "string" ? message.role : "-", + length: json.length, + sha8: digest.slice(0, 8), + }, + }; +} + // #3919: which dimensions disagree when the sampled verify detects drift — // the digest is a single hash and leaves classification to guesswork. Pure; // runs only on detected drift (rare). Fingerprints carry id/role/length/ diff --git a/src/storage/store.ts b/src/storage/store.ts index b52f54fcc6..6344b3c1c9 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2508,17 +2508,18 @@ export class UnifiedStore { })); } - getMessages(sessionId: string, opts?: { includeRetired?: boolean }): (MessageRow & { contentHash: string; retiredAt: number | null })[] { + getMessages(sessionId: string, opts?: { includeRetired?: boolean; seqs?: readonly number[] }): (MessageRow & { contentHash: string; retiredAt: number | null })[] { // #4390: retired rows (rewound/undo history) stay out of the unified // replay by default — they are history, not view. Callers that need // the full historian record (export/diagnostics) opt in. + if (opts?.seqs?.length === 0) return []; + const retiredFilter = opts?.includeRetired === true ? "" : " AND retired_at IS NULL"; + const seqFilter = opts?.seqs ? ` AND seq IN (${opts.seqs.map(() => "?").join(", ")})` : ""; const rows = this.db .prepare( - opts?.includeRetired === true - ? "SELECT seq, msg_id, ref, ref_num, role, kind, content_hash, retired_at FROM messages WHERE session_id = ? ORDER BY seq" - : "SELECT seq, msg_id, ref, ref_num, role, kind, content_hash, retired_at FROM messages WHERE session_id = ? AND retired_at IS NULL ORDER BY seq", + `SELECT seq, msg_id, ref, ref_num, role, kind, content_hash, retired_at FROM messages WHERE session_id = ?${retiredFilter}${seqFilter} ORDER BY seq`, ) - .all(sessionId) as Record[]; + .all(sessionId, ...(opts?.seqs ?? [])) as Record[]; return rows.map((r) => ({ seq: r.seq as number, msgId: r.msg_id as string, @@ -2574,14 +2575,14 @@ export class UnifiedStore { return row ? row.refcount : 0; } - getBlocks(sessionId: string): (BlockRow & { summaryHash: string; oneHash: string | null })[] { + getBlocks(sessionId: string, opts?: { limit: number; offset?: number }): (BlockRow & { summaryHash: string; oneHash: string | null })[] { const rows = this.db .prepare( `SELECT block_id, ord, start_seq, end_seq, start_ref, end_ref, tier, summary_hash, one_hash, supersedes_block_id, active, created_at, covered_hash, payload_json - FROM blocks WHERE session_id = ? ORDER BY ord`, + FROM blocks WHERE session_id = ? ORDER BY ord, block_id${opts ? " LIMIT ? OFFSET ?" : ""}`, ) - .all(sessionId) as Record[]; + .all(sessionId, ...(opts ? [opts.limit, opts.offset ?? 0] : [])) as Record[]; return rows.map((r) => ({ blockId: r.block_id as string, ord: r.ord as number, diff --git a/src/web/blogfeed.ts b/src/web/blogfeed.ts index 494e7627dc..13bec271ce 100644 --- a/src/web/blogfeed.ts +++ b/src/web/blogfeed.ts @@ -105,6 +105,8 @@ export function _setBlogRootForTest(root: string | null): void { /** Mirror of the website tools' frontmatter parser (key: value between --- fences). */ function parseFrontmatter(text: string): { fm: Record; body: string } { + // Windows checkouts use CRLF; parse and render the same Markdown on every platform. + text = text.replace(/\r\n/g, "\n"); const fm: Record = {}; let body = text; // Git-for-Windows autocrlf checks .md posts out with CRLF; normalize so diff --git a/tests/launcher-opencode-inline.test.ts b/tests/launcher-opencode-inline.test.ts index 09f433c205..db1c0a3e78 100644 --- a/tests/launcher-opencode-inline.test.ts +++ b/tests/launcher-opencode-inline.test.ts @@ -38,16 +38,12 @@ interface SpawnRecord { // clone (plugin injection only; 2.x directory wrapper when the host is 2.x). test("runLaunch opencode-inline: no proxy spawned, BILI_OPENCODE_INLINE=1, attach/proxy env stripped, plugin rides OPENCODE_CONFIG", async () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ocinline-")); - const prevHome = process.env.HOME; - const prevUserProfile = process.env.USERPROFILE; const prevXdg = process.env.XDG_CONFIG_HOME; const prevOcConfig = process.env.OPENCODE_CONFIG; const prevClientBin = process.env.BILI_CLIENT_BIN; const prevBcp = process.env.BILLION_CONTEXT_PROXY; const prevHttpsProxy = process.env.HTTPS_PROXY; const prevMitmHosts = process.env.BILI_MITM_HOSTS; - process.env.HOME = home; - if (prevUserProfile !== undefined) process.env.USERPROFILE = home; // Isolated config root: no user opencode.json, no ambient OPENCODE_CONFIG. process.env.XDG_CONFIG_HOME = path.join(home, ".config"); delete process.env.OPENCODE_CONFIG; @@ -122,14 +118,15 @@ test("runLaunch opencode-inline: no proxy spawned, BILI_OPENCODE_INLINE=1, attac const childComspec = ce.COMSPEC ?? ce.ComSpec ?? ce.comspec; const expectedComspec = typeof childComspec === "string" && childComspec.trim().length > 0 ? childComspec : "cmd.exe"; assert.equal(cap.command, expectedComspec, "#679 comspec wrap uses the child env's COMSPEC"); - assert.deepEqual(cap.args.slice(0, 3), ["/d", "/s", "/c"]); + const quotedOc = /\s/.test(fakeOc) ? `"${fakeOc}"` : fakeOc; + assert.deepEqual(cap.args, ["/d", "/s", "/c", `"${quotedOc}"`]); assert.equal(cap.verbatim, true, "#679 verbatim argv keeps cmd.exe from re-splitting"); assert.match(cap.args[3] ?? "", /^".*\.cmd"$/, "comspec line wraps the quoted .cmd shim"); } else { assert.equal(cap.command, fakeOc); assert.deepEqual(cap.args, []); } - const env = spawns[0].env!; + const env = cap.env!; assert.equal(env.BILI_OPENCODE_INLINE, "1"); assert.equal(env.BILLION_CONTEXT_PROXY, undefined, "inherited BILLION_CONTEXT_PROXY must be stripped (attach would win)"); assert.equal(env.HTTPS_PROXY, undefined, "inherited HTTPS_PROXY must be stripped (no MITM lane)"); @@ -153,9 +150,6 @@ test("runLaunch opencode-inline: no proxy spawned, BILI_OPENCODE_INLINE=1, attac assert.equal(clone.compaction?.auto, false, "host auto-compaction stays off (ACP owns compression)"); } finally { process.exit = prevExit; - process.env.HOME = prevHome; - if (prevUserProfile === undefined) delete process.env.USERPROFILE; - else process.env.USERPROFILE = prevUserProfile; if (prevXdg === undefined) delete process.env.XDG_CONFIG_HOME; else process.env.XDG_CONFIG_HOME = prevXdg; if (prevOcConfig === undefined) delete process.env.OPENCODE_CONFIG; diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 45bb18a302..853ace5ed1 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -115,7 +115,7 @@ async function harness(persist = false, seedParent = true, compressExtra: Record assert.equal(r.status, 200); await r.text(); } - return { request, messages, forwarded, dir, store, upstreamUrl, origin, close: async () => { store.cancelAll(); await closeServers([proxy, upstream], "plugin-fork harness"); } }; + return { request, messages, forwarded, dir, store, upstreamUrl, origin, close: async () => { store.closeQuietly(); await closeServers([proxy, upstream], "plugin-fork harness"); } }; } test("plain proxy panel snapshots do not change raw-history persistence", { timeout: 60_000 }, async () => { @@ -885,82 +885,81 @@ for (const corruption of ["missing-ref", "missing-payload", "orphan-payload", "w }); } -test("HTTP fork revision and originals survive a new proxy process", { timeout: 60_000 }, async () => { - const h = await harness(true); - let child: ReturnType | undefined; - try { - await compress(h); - // CI-only race (#4599): the compress fold lands in the live session - // state asynchronously after the tool response (#3573 replay - // semantics). On slow runners flushSync below would otherwise persist - // the pre-fold state and the cold load reports "0 block(s) - // (compressions=0)". Bounded-wait for the fold first. - for (let i = 0; i < 100 && !(resolveConversation("parent").session?.state.blocks?.length ?? 0); i++) { - await new Promise((r) => setTimeout(r, 50)); - } - const parent = resolveConversation("parent").session!; - parent.metadata.effectiveCcr = { ...DEFAULT_CCR_CONFIG, enabled: true }; - parent.contentStore = storeOriginal(createContentStore(), { ref: "m00001", rawId: parent.pluginSnapshot![0].id, text: "CCR original retained independently after restart", kind: "original", tokens: 10, head: "CCR original" }); - parent.contentStoreDirty = true; - assert(h.store.flushSync(parent)); - // #4316 PR3/3: cold load runs the one-time ledger alignment, but only - // when the unified store is available — config-clean machines (CI) run - // legacy mode where the alignment is an honest skip and the loaded - // state never carries refLedgerAligned. Mirror the mode in the - // expectation instead of asserting the flag unconditionally. - const persistedState = JSON.parse(JSON.stringify(parent.state)); - if (h.store.replicasAvailable()) persistedState.refLedgerAligned = true; - assert.deepEqual(JSON.parse(JSON.stringify(h.store.loadSync("parent")!.state)), persistedState); - const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; - const req = forkRequest(snapshot); - const fork = await h.request("/__bili/plugin/fork", req); - assert.equal(fork.status, 201); - const childSnapshot = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; - const code = `import {startServer} from './src/server.ts'; import {SessionStore,_setStoreForTest} from './src/persist.ts'; import {defaultConfig} from 'acp-kernel'; import {_setForTest} from './src/registry.ts'; _setForTest({}); _setStoreForTest(new SessionStore({dir:${JSON.stringify(h.dir + "/sessions")},enabled:true})); const s=await startServer({port:0,host:'127.0.0.1',upstream:${JSON.stringify(h.upstreamUrl)},modelContextLimit:400000,routes:{},kernelConfig:defaultConfig(400000),compress:{injectTool:true,injectNudge:true},promptCache:{routing:'auto'},sessionHeader:'x-acp-session',log:false,debug:false,passthrough:false,autoUpdate:false,mitm:{enabled:false,domains:[]}}); const ready=()=>process.stdout.write(JSON.stringify({port:s.address().port})+'\\n'); if(s.listening)ready();else s.once('listening',ready);`; - child = spawn(process.execPath, ["--import", "tsx", "--input-type=module", "--eval", code], { cwd: fileURLToPath(new URL("../", import.meta.url)), env: { ...process.env, NODE_ENV: "test" }, stdio: ["ignore", "pipe", "pipe"] }); - let stderr = ""; - child.stderr!.on("data", (b) => { stderr += b.toString(); }); - const port = await new Promise((resolve, reject) => { - let stdout = ""; - const timer = setTimeout(() => reject(new Error("restart timeout: " + stderr + stdout)), 15000); - child!.stdout!.on("data", (b) => { - stdout += b.toString(); - const port = stdout.match(/\{"port":(\d+)\}/); - if (port) { clearTimeout(timer); resolve(Number(port[1])); } +for (const storageMode of ["legacy", "full"] as const) { + test(`HTTP fork revision and originals survive a new proxy process (${storageMode})`, { timeout: 60_000 }, async () => { + const previousMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = storageMode; + const h = await harness(true); + let child: ReturnType | undefined; + try { + await compress(h); + // The fold settles asynchronously after the tool response (#4599). + for (let i = 0; i < 100 && !(resolveConversation("parent").session?.state.blocks?.length ?? 0); i++) { + await new Promise((r) => setTimeout(r, 50)); + } + const parent = resolveConversation("parent").session!; + parent.metadata.effectiveCcr = { ...DEFAULT_CCR_CONFIG, enabled: true }; + parent.contentStore = storeOriginal(createContentStore(), { ref: "m00001", rawId: parent.pluginSnapshot![0].id, text: "CCR original retained independently after restart", kind: "original", tokens: 10, head: "CCR original" }); + parent.contentStoreDirty = true; + assert(h.store.flushSync(parent)); + // #4316 PR3/3: only a unified cold load runs the one-time ledger + // alignment; the CI legacy baseline retains the original state. + const persistedState = JSON.parse(JSON.stringify(parent.state)); + if (h.store.replicasAvailable()) persistedState.refLedgerAligned = true; + assert.deepEqual(JSON.parse(JSON.stringify(h.store.loadSync("parent")!.state)), persistedState); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const req = forkRequest(snapshot); + const fork = await h.request("/__bili/plugin/fork", req); + assert.equal(fork.status, 201); + const childSnapshot = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; + const code = `import {startServer} from './src/server.ts'; import {SessionStore,_setStoreForTest} from './src/persist.ts'; import {defaultConfig} from 'acp-kernel'; import {_setForTest} from './src/registry.ts'; _setForTest({}); _setStoreForTest(new SessionStore({dir:${JSON.stringify(h.dir + "/sessions")},enabled:true})); const s=await startServer({port:0,host:'127.0.0.1',upstream:${JSON.stringify(h.upstreamUrl)},modelContextLimit:400000,routes:{},kernelConfig:defaultConfig(400000),compress:{injectTool:true,injectNudge:true},promptCache:{routing:'auto'},sessionHeader:'x-acp-session',log:false,debug:false,passthrough:false,autoUpdate:false,mitm:{enabled:false,domains:[]}}); const ready=()=>process.stdout.write(JSON.stringify({port:s.address().port})+'\\n'); if(s.listening)ready();else s.once('listening',ready);`; + child = spawn(process.execPath, ["--import", "tsx", "--input-type=module", "--eval", code], { cwd: fileURLToPath(new URL("../", import.meta.url)), env: { ...process.env, NODE_ENV: "test" }, stdio: ["ignore", "pipe", "pipe"] }); + let stderr = ""; + child.stderr!.on("data", (b) => { stderr += b.toString(); }); + const port = await new Promise((resolve, reject) => { + let stdout = ""; + const timer = setTimeout(() => reject(new Error("restart timeout: " + stderr + stdout)), 15000); + child!.stdout!.on("data", (b) => { + stdout += b.toString(); + const port = stdout.match(/\{"port":(\d+)\}/); + if (port) { clearTimeout(timer); resolve(Number(port[1])); } + }); + child!.once("exit", (code) => { clearTimeout(timer); reject(new Error(`restart exited ${code}: ${stderr}`)); }); + child!.once("error", (error) => { clearTimeout(timer); reject(error); }); }); - child!.once("exit", (code) => { clearTimeout(timer); reject(new Error(`restart exited ${code}: ${stderr}`)); }); - child!.once("error", (error) => { clearTimeout(timer); reject(error); }); - }); - const call = async (path: Path, body?: unknown) => { - const r = await fetch(`http://127.0.0.1:${port}` + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); - return { status: r.status, body: await responseBody(r, path) }; - }; - const restored = await call("/__bili/plugin/snapshot?conversationId=child"); - const restoredStatus = await call("/__bili/plugin/status?conversationId=parent&fallback=latest"); - assert.equal(restoredStatus.status, 200); - assert.equal(restoredStatus.body.sessionId, "parent"); - assert.equal(restoredStatus.body.fallback, undefined); - assert.equal(restoredStatus.body.sessionRevision, snapshot.parentRevision); - assert.equal(restoredStatus.body.contextTokensSource, "estimate"); - assert.equal(restored.status, 200); - assert.equal(restored.body.parentRevision, childSnapshot.parentRevision); - assert.deepEqual(restored.body.orderedMessages, childSnapshot.orderedMessages); - assert.equal((await call("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); - const replay = await call("/__bili/plugin/fork", req); - assert.equal(replay.status, 200); - assert.equal(replay.body.replayed, true); - const retrieved = await call("/__bili/plugin/tool", { conversationId: "child", tool: "acp_retrieve", args: { ref: "m00001" } }); - assert.equal(retrieved.status, 200); - assert.match(retrieved.body.result, /CCR original retained independently after restart/); - const original = await call("/__bili/plugin/tool", { conversationId: "child", tool: "decompress", args: { blockId: "b1", full: true } }); - assert.match(original.body.result, /first original/); - assert.match(original.body.result, /second original/); - assert(!original.body.result.includes("tail original")); - } finally { - if (child?.pid && child.exitCode === null && child.signalCode === null) { child.kill("SIGTERM"); await once(child, "exit"); } - await h.close(); - } -}); + const call = async (path: Path, body?: unknown) => { + const r = await fetch(`http://127.0.0.1:${port}` + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + return { status: r.status, body: await responseBody(r, path) }; + }; + const restored = await call("/__bili/plugin/snapshot?conversationId=child"); + const restoredStatus = await call("/__bili/plugin/status?conversationId=parent&fallback=latest"); + assert.equal(restoredStatus.status, 200); + assert.equal(restoredStatus.body.sessionId, "parent"); + assert.equal(restoredStatus.body.fallback, undefined); + assert.equal(restoredStatus.body.sessionRevision, snapshot.parentRevision); + assert.equal(restoredStatus.body.contextTokensSource, "estimate"); + assert.equal(restored.status, 200); + assert.equal(restored.body.parentRevision, childSnapshot.parentRevision); + assert.deepEqual(restored.body.orderedMessages, childSnapshot.orderedMessages); + assert.equal((await call("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + const replay = await call("/__bili/plugin/fork", req); + assert.equal(replay.status, 200); + assert.equal(replay.body.replayed, true); + const retrieved = await call("/__bili/plugin/tool", { conversationId: "child", tool: "acp_retrieve", args: { ref: "m00001" } }); + assert.equal(retrieved.status, 200); + assert.match(retrieved.body.result, /CCR original retained independently after restart/); + const original = await call("/__bili/plugin/tool", { conversationId: "child", tool: "decompress", args: { blockId: "b1", full: true } }); + assert.match(original.body.result, /first original/); + assert.match(original.body.result, /second original/); + assert(!original.body.result.includes("tail original")); + } finally { + if (child?.pid && child.exitCode === null && child.signalCode === null) { child.kill("SIGTERM"); await once(child, "exit"); } + await h.close(); + if (previousMode === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = previousMode; + } + }); +} test("HTTP status separates real usage from a manual-compression estimate and changing generation", { timeout: 60_000 }, async () => { const h = await harness(); diff --git a/tests/storage-batched-verification.test.ts b/tests/storage-batched-verification.test.ts new file mode 100644 index 0000000000..4e2d77fbeb --- /dev/null +++ b/tests/storage-batched-verification.test.ts @@ -0,0 +1,161 @@ +import { after, test, type TestContext } from "node:test"; +import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import type { CompressionBlock, CoreMessage } from "acp-kernel"; +import { SqliteBackend } from "../src/storage/backend.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; +import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; +import { UnifiedShadow } from "../src/storage/shadow.ts"; +import { UnifiedStore } from "../src/storage/store.ts"; + +const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-batched-verify-")); +process.env.BILI_CONFIG_FILE = path.join(root, "no-config.json"); +process.env.BILI_PERSIST_ZSTD = "0"; +after(() => fs.rmSync(root, { recursive: true, force: true })); + +function message(i: number): CoreMessage { + return { id: `raw-${i}`, role: "user", contentType: "text", text: `message ${i}` }; +} + +function session(): LegacySessionLike { + const blocks: CompressionBlock[] = Array.from({ length: 70 }, (_, i) => ({ + blockId: `b${i + 1}`, runId: `run-${i}`, tier: 1, summary: `summary ${i}`, + directMessageIds: [], effectiveMessageIds: [], directBlockIds: [], + compressedTokens: 10, createdAt: 1_700_000_000_000, survivedCount: 1, + generation: "young", active: i % 3 !== 0, + })); + const messages = Array.from({ length: 80 }, (_, i) => message(i)); + messages[69] = { + ...messages[69], + rawOpenaiContent: { type: "image_url", image_url: { url: `data:image/png;base64,${"QUJD".repeat(700)}` } }, + } as CoreMessage; + const blockContents: LegacySessionLike["blockContents"] = new Map( + [...blocks].reverse().map((b) => [b.blockId, { one: null, full: { text: `original ${b.blockId}`, count: 2 } }]), + ); + blockContents.set("b-orphan", { one: null, full: { text: "retained orphan", count: 1 } }); + return { + id: "batched-session", createdAt: 1_700_000_000_000, lastSeen: 1_700_000_000_500, + meta: { protocol: "responses" }, stats: { requests: 1 }, metadata: {}, + state: { + blocks, + messageRefs: { byRaw: { ghost: "m00001", protected: "BLOCKED" }, byRef: { m00001: "ghost" } }, + nextBlockId: 71, nextRunId: 71, + }, + blockContents, lastMessages: messages, lastMessagesFolded: false, + pluginSnapshot: [messages[75], messages[1], messages[75], messages[40]], + }; +} + +function fixture(t: TestContext, asyncLane = false) { + const dir = fs.mkdtempSync(path.join(root, "case-")); + const dbPath = path.join(dir, "index.db"); + const store = UnifiedStore.open(dbPath); + const load = (record: unknown) => record as LegacySessionLike; + // Exercise the network facade's queue against the real SQLite contract adapter. + const shadow = Reflect.construct(UnifiedShadow, ["shadow", asyncLane ? null : store, load, asyncLane ? new SqliteBackend(store) : undefined]) as UnifiedShadow; + let closed = false; + const close = async () => { + if (!closed) { + closed = true; + await shadow.closeAsync(); + } + }; + t.after(async () => { + await close(); + fs.rmSync(dir, { recursive: true, force: true }); + }); + return { store, shadow, dbPath, close, view: session() }; +} + +function changeBeforeRead(t: TestContext, store: UnifiedStore, dbPath: string, mutate: (db: ReturnType["db"]) => void): void { + const getSessionRow = store.getSessionRow.bind(store); + let changed = false; + t.mock.method(store, "getSessionRow", (id: string) => { + if (!changed) { + changed = true; + const { db } = openSqliteDatabase(dbPath); + try { mutate(db); } finally { db.close(); } + } + return getSessionRow(id); + }); +} + +for (const asyncLane of [false, true]) { + test(`sampled verification bounds reads and preserves live contents (${asyncLane ? "async" : "sync"})`, async (t) => { + const { store, shadow, view, close } = fixture(t, asyncLane); + store.ingestSession(planIngest({ ...view, pluginSnapshot: undefined, lastMessages: Array.from({ length: 200 }, (_, i) => message(i + 1000)) })); + const originalContents = structuredClone(view.blockContents); + const messageReads: { requested: number[] | undefined; returned: number }[] = []; + const blockReads: { limit: number | undefined; returned: number }[] = []; + const getMessages = store.getMessages.bind(store); + const getBlocks = store.getBlocks.bind(store); + t.mock.method(store, "getMessages", (id: string, opts?: Parameters[1]) => { + const rows = getMessages(id, opts); + messageReads.push({ requested: opts?.seqs ? [...opts.seqs] : undefined, returned: rows.length }); + return rows; + }); + t.mock.method(store, "getBlocks", (id: string, opts?: Parameters[1]) => { + const rows = getBlocks(id, opts); + blockReads.push({ limit: opts?.limit, returned: rows.length }); + return rows; + }); + shadow.record(view); + if (asyncLane) await close(); + assert.equal(shadow.stats.verifyOk, 1, shadow.stats.lastError ?? "sample must pass"); + assert.equal(shadow.stats.verifyDrift, 0); + assert.deepEqual(view.blockContents, originalContents); + assert.ok(messageReads.length >= 3 && blockReads.length >= 3); + assert.ok(messageReads.every((r) => r.requested && r.requested.length <= 32 && r.returned <= 32)); + assert.equal(messageReads.reduce((n, r) => n + r.returned, 0), 80, "only referenced messages are hydrated"); + assert.ok(blockReads.every((r) => r.limit === 32 && r.returned <= 32)); + }); +} + +test("a changed message beyond the first batch trips drift and clears the watermark", (t) => { + const { store, shadow, dbPath, view } = fixture(t); + changeBeforeRead(t, store, dbPath, (db) => { + const row = db.prepare("SELECT content_hash FROM messages WHERE session_id = ? AND msg_id = ?").get(view.id, "raw-70") as { content_hash: string }; + db.prepare("UPDATE content SET bytes = ? WHERE hash = ?").run(Buffer.from(JSON.stringify({ ...message(70), text: "changed" })), row.content_hash); + }); + shadow.record(view); + assert.equal(shadow.stats.verifyDrift, 1, shadow.stats.lastError ?? "changed content must drift"); + assert.equal(store.ingestWatermark(view.id), null); + assert.ok(shadow.driftedSessions.has(view.id)); +}); + +test("later block batches detect changed, missing and extra block contents", (t) => { + for (const kind of ["changed", "missing", "extra"] as const) { + const { store, shadow, dbPath, view } = fixture(t); + changeBeforeRead(t, store, dbPath, (db) => { + if (kind === "changed") { + const row = db.prepare("SELECT summary_hash FROM blocks WHERE session_id = ? AND block_id = ?").get(view.id, "b69") as { summary_hash: string }; + db.prepare("UPDATE content SET bytes = ? WHERE hash = ?").run(Buffer.from(JSON.stringify({ text: "changed original", count: 2 })), row.summary_hash); + } else if (kind === "missing") { + db.prepare("DELETE FROM blocks WHERE session_id = ? AND block_id = ?").run(view.id, "b69"); + } else { + const orphans = { "b-orphan": view.blockContents.get("b-orphan"), unexpected: { one: null, full: { text: "extra original", count: 1 } } }; + db.prepare("UPDATE sessions SET orphans_json = ? WHERE session_id = ?").run(JSON.stringify(orphans), view.id); + } + }); + shadow.record(view); + assert.equal(shadow.stats.verifyDrift, 1, `${kind}: ${shadow.stats.lastError}`); + assert.equal(store.ingestWatermark(view.id), null, kind); + } +}); + +test("missing and retired tail rows remain visible as drift", (t) => { + for (const retired of [false, true]) { + const { store, shadow, dbPath, view } = fixture(t); + changeBeforeRead(t, store, dbPath, (db) => { + const sql = retired + ? "UPDATE messages SET retired_at = 1700000000000 WHERE session_id = ? AND msg_id = ?" + : "DELETE FROM messages WHERE session_id = ? AND msg_id = ?"; + db.prepare(sql).run(view.id, "raw-70"); + }); + shadow.record(view); + assert.equal(shadow.stats.verifyDrift, 1, shadow.stats.lastError ?? "missing referenced message must drift"); + assert.equal(store.ingestWatermark(view.id), null); + } +}); diff --git a/tests/storage-engine-proofs.test.ts b/tests/storage-engine-proofs.test.ts index d9eb3334b2..9bbe15e75e 100644 --- a/tests/storage-engine-proofs.test.ts +++ b/tests/storage-engine-proofs.test.ts @@ -22,8 +22,8 @@ * IMPORT GUARD — a hand-aged legacy file (older savedAt than the * index watermark) must not re-import over newer index * state on load. - * ROLLBACK — unset the env after running full → the legacy file - * loads with the exact digest. + * ROLLBACK — explicitly select legacy after persisting the full + * load's migration → the file loads the exact digest. * SELF-DISABLE — 3 consecutive sidecar failures disable it; reads flip * back to legacy; the legacy file stays readable. * CORRUPT INDEX — a corrupt index.db degrades the store to legacy-only @@ -167,6 +167,7 @@ for (const wire of WIRES) { const record = await driveWire(wire as Wire, `golden-${wire}`, { storageUnified: "full", reloadAfterEdit: leg }); assert.equal(record.turns.length, legacy.turns.length, `${wire}/${leg}: body count drifted vs legacy restart`); for (let i = 0; i < legacy.turns.length; i++) { + if (i <= record.editedAt) assert.equal(record.turns[i]!.sha, golden.turns[i]!.sha, `${wire}/${leg}: pre-migration body #${i} digest drift vs committed goldens`); // #4316 PR3/3 (380ef4dac): a full-mode cold load re-homes citation // numbers once (one-time ledger alignment) — the ONLY permitted // wire delta. Citation-normalized digests must match turn-for-turn. @@ -281,10 +282,10 @@ test("storage engine: load-side import guard skips a hand-aged legacy file (inde }); // --------------------------------------------------------------------------- -// ROLLBACK: full → unset env → legacy file authoritative, digest exact. +// ROLLBACK: full → explicit legacy → legacy file authoritative, digest exact. // --------------------------------------------------------------------------- -test("storage engine: rollback contract — unset the env and the legacy read path serves the identical state", async () => { +test("storage engine: rollback contract — explicit legacy mode serves the identical persisted state", async () => { const dir = tmpdir("bili-engproof-rollback-"); const prev = process.env.BILI_STORAGE_UNIFIED; try { @@ -305,8 +306,10 @@ test("storage engine: rollback contract — unset the env and the legacy read pa // merely deleting the env would read unified and vacate this leg. const storeFull = new SessionStore({ dir, debounceMs: 1, enabled: true }); const viaFull = storeFull.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); - storeFull.closeQuietly(); assert.ok(viaFull, "full-mode reload returned null"); + // Persist the one-time cold-load alignment before exercising rollback. + await storeFull.writeNow(viaFull); + storeFull.closeQuietly(); const digestFull = viewDigest(toLegacyLike(viaFull)); process.env.BILI_STORAGE_UNIFIED = "legacy"; @@ -378,8 +381,10 @@ test("storage engine: corrupt index.db degrades to legacy-only instead of bricki // fallback must reproduce exactly from the legacy file. const clean = new SessionStore({ dir, debounceMs: 1, enabled: true }); const viaFull = clean.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); - clean.closeQuietly(); assert.ok(viaFull, "full-mode read returned null"); + // Both replicas must contain the migration before corrupting the index. + await clean.writeNow(viaFull); + clean.closeQuietly(); // #4338: everything past this point (corrupt the header → degrade // construction → fallback read → rename guards → write → close) runs diff --git a/tests/storage-orphan-recovery.test.ts b/tests/storage-orphan-recovery.test.ts index 9fd6f13b0a..aa5c5fbfde 100644 --- a/tests/storage-orphan-recovery.test.ts +++ b/tests/storage-orphan-recovery.test.ts @@ -75,6 +75,8 @@ test("cold load recovers the pre-fix orphan freeze before processing a request", const alignedRef = recovered.state.messageRefs.byRaw["raw-new"]; assert.ok(alignedRef, "raw-new must keep a citation number after recovery"); assert.equal(recovered.state.messageRefs.byRef[alignedRef], "raw-new"); + assert.equal(alignedRef, "m00001", "the first stored message aligns to seq 0 + 1"); + assert.equal(recovered.restampSuperseded?.get("m00003"), "raw-new"); assert.equal(recovered.lastMessages?.at(-1)?.text, "new turn"); assert.equal(recovered.metadata.recoveryMarker, "fresh"); assert.equal(recovered.blockContents.get("b1")?.full.text, "retained original"); diff --git a/tests/web-blogfeed.test.ts b/tests/web-blogfeed.test.ts index fe9a00387c..8f8a61c840 100644 --- a/tests/web-blogfeed.test.ts +++ b/tests/web-blogfeed.test.ts @@ -10,7 +10,7 @@ */ import { test, beforeEach, afterEach } from "node:test"; import assert from "node:assert/strict"; -import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "node:fs"; +import { mkdtempSync, rmSync, readFileSync, writeFileSync, mkdirSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { ServerResponse } from "node:http"; @@ -94,6 +94,21 @@ test("rendering is escape-first: script tags survive only escaped; tables become assert.ok(alpha.html.includes("two")); }); +test("CRLF markdown preserves bilingual frontmatter and rendered content", () => { + const expected = getBlogFeed(); + for (const relative of [ + "website/blog/posts/2026-10-08_demo.md", + "website/blog/posts/2026-10-08_demo.zh.md", + "paper/a.md", + "paper/b.md", + ]) { + const file = join(pkgRoot, relative); + writeFileSync(file, readFileSync(file, "utf8").replace(/\n/g, "\r\n")); + } + _setBlogRootForTest(pkgRoot); + assert.deepEqual(getBlogFeed(), expected); +}); + test("image refs: shipped files reroute to the panel asset prefix; traversal/unshipped refs drop silently", () => { const data = getBlogFeed(); const demo = data.items.find((i) => i.id === "demo")!; diff --git a/tests/web-storage-diagnostics-redaction.test.ts b/tests/web-storage-diagnostics-redaction.test.ts index eb8ef03b1d..d49295c6da 100644 --- a/tests/web-storage-diagnostics-redaction.test.ts +++ b/tests/web-storage-diagnostics-redaction.test.ts @@ -33,12 +33,16 @@ function restoreEnv(snap: EnvSnap): void { } /** Pin every path-relevant env var (unset ones deleted so homedir fallbacks can - * never reach the real machine's dirs) AND $HOME (so os.homedir() is + * never reach the real machine's dirs) AND HOME/USERPROFILE (so os.homedir() is * deterministic); all restored in finally. */ function withEnv(home: string | undefined, overrides: Record, fn: () => T): T { const saved = saveEnv(); const savedHome = process.env.HOME; - if (home !== undefined) process.env.HOME = home; + const savedProfile = process.env.USERPROFILE; + if (home !== undefined) { + process.env.HOME = home; + process.env.USERPROFILE = home; + } for (const k of ENV_KEYS) { if (overrides[k] !== undefined) process.env[k] = overrides[k]; else delete process.env[k]; @@ -49,13 +53,13 @@ function withEnv(home: string | undefined, overrides: Record { - const savedHome = process.env.HOME; - process.env.HOME = "/home/alice"; - try { + withEnv("/home/alice", {}, () => { assert.equal(maskPathsForPublish("/home/alice/.local/share/billion-context/x.db"), "~/.local/share/billion-context/x.db"); assert.equal(maskPathsForPublish("open failed at /home/alice/db"), "open failed at ~/db"); assert.equal(maskPathsForPublish('ENOENT: no such file or directory, open \'/home/alice/index.db\''), "ENOENT: no such file or directory, open '~/index.db'"); @@ -65,16 +69,11 @@ test("#4427 maskPathsForPublish rewrites this machine's $HOME to ~, anchored on // (host masking is a separate net; the path masker must not corrupt URLs) assert.equal(maskPathsForPublish("https://relay.internal/home/alice/doc.pdf"), "https://relay.internal/home/alice/doc.pdf", "URL pathname not rewritten"); assert.equal(maskPathsForPublish("~/.already-tilded"), "~/.already-tilded", "idempotent on already-tilded text"); - } finally { - if (savedHome === undefined) delete process.env.HOME; - else process.env.HOME = savedHome; - } + }); }); test("#4427 redactForPublish composes secret scrub + host mask + home-path strip in one pass", () => { - const savedHome = process.env.HOME; - process.env.HOME = "/home/bob"; - try { + withEnv("/home/bob", {}, () => { const out = redactForPublish("probe https://user:SecretPass@db.internal:3306/app?token=FAKE_TOKEN at /home/bob/.cache/x authorization: bearer sk-live-abcdef1234567890 failed"); assert.ok(!out.includes("db.internal"), "private URL host masked"); assert.ok(!out.includes("SecretPass"), "URL userinfo password dropped"); @@ -84,10 +83,7 @@ test("#4427 redactForPublish composes secret scrub + host mask + home-path strip assert.ok(out.includes("~/.cache/x"), "path stays actionable via ~"); assert.ok(!out.includes("sk-live-abcdef1234567890"), "bearer token scrubbed"); assert.ok(out.includes(" {