From b217d7905062cf0b387c13b85a84fe67f4de18ad Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:00:51 -0700 Subject: [PATCH 1/7] perf: verify stored sessions in bounded batches (#4538) --- src/storage/backend.ts | 12 +- src/storage/mysql.ts | 17 +- src/storage/shadow.ts | 185 ++++++++++++++---- src/storage/store.ts | 17 +- tests/storage-batched-verification.test.ts | 161 +++++++++++++++ .../web-storage-diagnostics-redaction.test.ts | 28 ++- 6 files changed, 346 insertions(+), 74 deletions(-) create mode 100644 tests/storage-batched-verification.test.ts diff --git a/src/storage/backend.ts b/src/storage/backend.ts index dd8473893a..299b912f4f 100644 --- a/src/storage/backend.ts +++ b/src/storage/backend.ts @@ -73,8 +73,8 @@ export interface StorageBackend { seqByRaw(sessionId: string): Promise | null>; /** #4316 PR3/3: fork adoption row clone (idempotent). */ cloneSessionRows(parentId: string, childId: string, throughSeq: number): 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; @@ -186,11 +186,11 @@ export class SqliteBackend implements StorageBackend { this.store.cloneSessionRows(parentId, childId, throughSeq); return Promise.resolve(true); } - 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 f3c46ea373..ea9e392bbd 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -1007,13 +1007,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) { @@ -1070,12 +1071,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 d527be96c4..7c0ba0e0f0 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 } from "./store.js"; +import type { MessageRow, BlockRow, SessionRow, RefRow } 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 @@ -367,20 +368,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 @@ -404,8 +403,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 @@ -431,7 +430,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 } @@ -929,17 +928,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`; @@ -1167,13 +1167,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; @@ -1188,6 +1181,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 a13acf4edb..86ad9f4cae 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -2308,17 +2308,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, @@ -2374,14 +2375,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/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/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(" { From c3619a0511186e83b6a57adf63773e9c42faf1dc Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:09:26 -0700 Subject: [PATCH 2/7] fix: preserve blog metadata and launcher checks on Windows --- src/web/blogfeed.ts | 2 ++ tests/launcher-opencode-inline.test.ts | 20 +++++++++++--------- tests/web-blogfeed.test.ts | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/web/blogfeed.ts b/src/web/blogfeed.ts index 6efe1560a7..0d67c61d8f 100644 --- a/src/web/blogfeed.ts +++ b/src/web/blogfeed.ts @@ -76,6 +76,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; if (text.startsWith("---\n")) { diff --git a/tests/launcher-opencode-inline.test.ts b/tests/launcher-opencode-inline.test.ts index 7ff04b2ace..fd5452a332 100644 --- a/tests/launcher-opencode-inline.test.ts +++ b/tests/launcher-opencode-inline.test.ts @@ -28,6 +28,7 @@ interface SpawnRecord { command: string; args: string[]; env: NodeJS.ProcessEnv | undefined; + windowsVerbatimArguments?: boolean; } // `bili opencode-inline` (#4669): the launcher must start NO proxy, hand the @@ -37,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; @@ -75,7 +72,7 @@ test("runLaunch opencode-inline: no proxy spawned, BILI_OPENCODE_INLINE=1, attac const spawns: SpawnRecord[] = []; const spawnImpl: SpawnFn = (cmd, args, options) => { - spawns.push({ command: cmd, args: [...args], env: options?.env }); + spawns.push({ command: cmd, args: [...args], env: options?.env, windowsVerbatimArguments: options?.windowsVerbatimArguments }); const child = makeFakeChild(0); const orig = child.on!.bind(child); (child as { on: SpawnChild["on"] }).on = (event, listener) => { @@ -109,8 +106,16 @@ test("runLaunch opencode-inline: no proxy spawned, BILI_OPENCODE_INLINE=1, attac { fetchImpl, fetchHealthInfo, spawnImpl, sleep: () => Promise.resolve() }, ); assert.equal(spawns.length, 1, "exactly one child (opencode itself) — no proxy spawn"); - assert.equal(spawns[0].command, fakeOc); const env = spawns[0].env!; + if (process.platform === "win32") { + assert.equal(spawns[0].command, env.COMSPEC?.trim() ? env.COMSPEC : "cmd.exe"); + const quotedOc = /\s/.test(fakeOc) ? `"${fakeOc}"` : fakeOc; + assert.deepEqual(spawns[0].args, ["/d", "/s", "/c", `"${quotedOc}"`]); + assert.equal(spawns[0].windowsVerbatimArguments, true); + } else { + assert.equal(spawns[0].command, fakeOc); + assert.deepEqual(spawns[0].args, []); + } 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)"); @@ -134,9 +139,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/web-blogfeed.test.ts b/tests/web-blogfeed.test.ts index 0d1d820907..5280ba9dd3 100644 --- a/tests/web-blogfeed.test.ts +++ b/tests/web-blogfeed.test.ts @@ -9,7 +9,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"; @@ -93,6 +93,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")!; From c12bee29299552daac51e6d4643093d85fc3d410 Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:35:44 -0700 Subject: [PATCH 3/7] test: verify fork restart under legacy and full storage --- tests/plugin-fork.test.ts | 136 ++++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index d15bc89fe9..caf2c00688 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -114,7 +114,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 () => { @@ -742,71 +742,77 @@ 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); - 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 — the loaded - // state now carries refLedgerAligned. Mirror it in the expectation. - const persistedState = JSON.parse(JSON.stringify(parent.state)); - 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); + 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 (storageMode === "full") 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(); From 8576277d2cfd8e7dc82cbeee0a85177b1eec93a3 Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:36:40 -0700 Subject: [PATCH 4/7] test(storage): account for persisted engine lane in render goldens The engineLane=server metadata added by f590da94 explains the entire drift: removing that one field reproduces the old metadata digest. Regeneration changes only metadata and the enclosing digest; all wire bodies, state, originals and statistics remain identical. --- tests/golden/storage-render/anthropic.json | 4 ++-- tests/golden/storage-render/chat.json | 4 ++-- tests/golden/storage-render/google.json | 4 ++-- tests/golden/storage-render/responses.json | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 5afa14b2c4..13811dcab0 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "6727f42bd91879a7aa370e1ce0c78963bcba0df92e50390d4a8011f8f0da334d", + "stateDigest": "0a241435d456ba994c92b0072d2173bf2a6d00ee73223458fb4302e170efb1e3", "sectionDigests": { "state": "f93dc1c876c672d4", "blockContents": "dc7d13d250c94bb4", "stats": "c6f1d434975d047d", - "metadata": "f1ec4aab3aff777a" + "metadata": "16cad648ccf41950" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 46764f97db..01f69b1036 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "e149c301f382438ee063a38f70dd794207b88e94bdf3c14c5191da0eeaa99bc9", + "stateDigest": "9f1b8258a916fcf63af14625def9ed0236eb8ec9c542f0027206985598d2a5b8", "sectionDigests": { "state": "8390f21336885863", "blockContents": "94a58f96d7c5cfc3", "stats": "24d705b0e39bd13c", - "metadata": "55733fb81ccf7d69" + "metadata": "2198f47054c8acae" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index ba999cadec..fe9f9c95dd 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "80e83935e5fa7b052a3cb0f1d641bd81cb6ee59f91f3c1f2f31c204de29368a5", + "stateDigest": "c4979d4f58cfa49bc8be8c43b8e1f1b0861b3b9d44cdc415ee09e4ca21d9aa5a", "sectionDigests": { "state": "2e1def3a3234afdc", "blockContents": "473264d9df8ba338", "stats": "900481685c848462", - "metadata": "88cd02a467b4457e" + "metadata": "939dc0c09b6cbcbf" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 7004d90435..0402e7d43c 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "08526d3552fca7ac7552cd16a221893a652fbe740c9b8dccac6db57b3d6302ee", + "stateDigest": "b3bddfa75ce3f8b94424be9a0164c1b9a183faa9daf705e6175cabdd671c95ff", "sectionDigests": { "state": "db313e8eba2d65fc", "blockContents": "b6701268a0bdcd8a", "stats": "327d3967b8371aa8", - "metadata": "b5c39d84db67f424" + "metadata": "a9eecd3816738158" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", From 9397bc8370874275081f38a84cadbead30b1cca6 Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:44:01 -0700 Subject: [PATCH 5/7] test(storage): account for one-time ledger ref migration on restart Compare unified reads with an independent envelope reader plus the documented migration. Persist alignment before rollback/corrupt-index checks, select legacy explicitly, and assert the recovered raw ID under its ledger ref. Keep checkpoint writes deterministic so sampled ingest cadence cannot change the seed between reader legs. All 28 targeted storage tests pass; no product migration or digest assertion is disabled. --- tests/golden-storage-drive.ts | 25 ++++++++++++--- tests/storage-engine-proofs.test.ts | 45 ++++++++++++++++----------- tests/storage-orphan-recovery.test.ts | 6 +++- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/tests/golden-storage-drive.ts b/tests/golden-storage-drive.ts index a6b14205f8..41a8b2166d 100644 --- a/tests/golden-storage-drive.ts +++ b/tests/golden-storage-drive.ts @@ -17,10 +17,10 @@ import os from "node:os"; import path from "node:path"; import { once } from "node:events"; import { canonicalize, sha256 } from "./golden-canonical.ts"; -import { defaultConfig } from "acp-kernel"; +import { defaultConfig, restampRefsToLedger } from "acp-kernel"; import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import { flushAllSessions, _resetSessionsForTest } from "../src/session.ts"; +import { flushAllSessions, getSession, _resetSessionsForTest } from "../src/session.ts"; import type { ProxyOptions } from "../src/config.ts"; /** @@ -462,6 +462,8 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: dumpStateTo?: string; storageUnified?: "shadow" | "full"; reloadAfterEdit?: "keepFile" | "dropFile"; + // Independent legacy-read oracle for #4316's intentional cold-load migration. + alignLegacyRefsOnReload?: boolean; } = {}): Promise { __goldenGuard(); fs.mkdirSync(SCRATCH_ROOT, { recursive: true }); @@ -503,7 +505,12 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: upstream.listen(0, "127.0.0.1"); await listen(upstream); const upstreamPort = (upstream.address() as { port: number }).port; - store = new SessionStore({ dir: sessionsDir, debounceMs: 1, enabled: true }); + // Save at the explicit restart/final checkpoints below. A 1ms timer + // makes ingest/verification counts depend on machine speed, so the + // three reader legs can migrate refs from different ledger snapshots. + // The debounce exceeds the suite's four-minute deadline; it is never + // awaited. Delta/sampled-write behavior has storage-watermark tests. + store = new SessionStore({ dir: sessionsDir, debounceMs: 300_000, enabled: true }); _setStoreForTest(store); setRegistryForTest({}); proxy = await startServer(goldenProxyOptions(upstreamPort, 200_000)); @@ -558,8 +565,16 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: if (fs.statSync(fp).isFile() && (f as string).endsWith(".json") && !fs.existsSync(fp + ".tmp")) fs.rmSync(fp, { force: true }); } } - store = new SessionStore({ dir: sessionsDir, debounceMs: 1, enabled: true }); + store = new SessionStore({ dir: sessionsDir, debounceMs: 300_000, enabled: true }); _setStoreForTest(store); + if (opts.alignLegacyRefsOnReload) { + const loaded = getSession(sessionId); + const seqByRaw = store.unifiedSeqByRaw(sessionId); + if (!seqByRaw?.size) throw new Error("legacy alignment oracle requires a populated shadow ledger"); + const outcome = restampRefsToLedger(loaded.state, seqByRaw); + if (outcome.superseded.size > 0) loaded.restampSuperseded = outcome.superseded; + store.scheduleSave(loaded); + } } // Post-edit growth: 2 trailing turns. Also load-bearing for the // STATE digest: the persist layer does not reliably write the final @@ -660,7 +675,7 @@ export async function driveWire(wire: Wire, sessionId = `golden-${wire}`, opts: // orphaned inode and REOPEN the deleted path, resurrecting the tree // mid-rmrf (the #1646/#1910 ENOTEMPTY/EBUSY race class). The detach // + target-clear below are synchronous, so no interleaving window. - // The 1ms-debounced store writer races the same way (recreating + // A pending store writer races the same way (recreating // sessions// after the final flush). try { store?.cancelAll(); } catch { /* no store on early-exit paths */ } void closeLogger(); diff --git a/tests/storage-engine-proofs.test.ts b/tests/storage-engine-proofs.test.ts index 118ca0bde7..782ab1973a 100644 --- a/tests/storage-engine-proofs.test.ts +++ b/tests/storage-engine-proofs.test.ts @@ -5,21 +5,19 @@ * STATE-level equality between the legacy files and the unified index. This * suite pins the seams that audit found unproven: * - * WIRE PARITY — BILI_STORAGE_UNIFIED=full must be invisible on the - * wire: per-turn outbound digests, fold landmarks and - * the final state digest equal the COMMITTED storage - * render goldens, both with a mid-conversation restart - * and with the legacy file DELETED at the restart (the - * index alone carries the session — the migration's - * end-state promise). + * WIRE PARITY — full reads match envelope reads plus the documented + * #4316 ledger-ref migration. All post-restart bodies, + * landmarks and final digests agree, even when the + * legacy file is DELETED at restart. Pre-migration + * bodies also match the committed render goldens. * STALE REJECT — a multi-instance stale write that the guarded build * rejects must never reach index.db; the converged disk * record is what gets ingested. * 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 @@ -141,11 +139,11 @@ function unifiedRows(store: UnifiedStore, id: string): ReturnType { + test(`storage engine: full-mode ${wire} restart matches legacy reads plus ledger alignment`, { timeout: 240_000 }, async () => { const golden = JSON.parse(fs.readFileSync(path.join(RENDER_ROOT, `${wire}.json`), "utf8")) as { turns: Array<{ i: number; sha: string }>; foldAt: number; editedAt: number; summaryFrom: number; @@ -157,13 +155,20 @@ for (const wire of WIRES) { // slots (cacheLedger line bs/bl/be pairing, #2131) live in memory, so // exactly the first post-restart ledger line loses its bs fields — // identically in both legs. Everything else must be byte-equal. - const legacy = await driveWire(wire as Wire, `golden-${wire}`, { reloadAfterEdit: "keepFile" }); + // #4316 deliberately renumbers legacy refs at the first unified cold + // load. Keep the independent envelope reader as the oracle, applying + // only that documented migration against its shadow ledger. Every + // post-restart byte still has to match; the old pre-migration golden + // remains the oracle for all turns before the restart. + const legacy = await driveWire(wire as Wire, `golden-${wire}`, { + storageUnified: "shadow", reloadAfterEdit: "keepFile", alignLegacyRefsOnReload: true, + }); for (const leg of ["keepFile", "dropFile"] as const) { 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++) { assert.equal(record.turns[i]!.sha, legacy.turns[i]!.sha, `${wire}/${leg}: outbound body #${i} digest drift vs legacy restart`); - assert.equal(record.turns[i]!.sha, golden.turns[i]!.sha, `${wire}/${leg}: outbound body #${i} digest drift vs committed goldens (restart must not change the wire)`); + 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`); } assert.equal(record.foldAt, legacy.foldAt, `${wire}/${leg}: fold landmark drifted`); assert.equal(record.editedAt, legacy.editedAt, `${wire}/${leg}: edit landmark drifted`); @@ -274,10 +279,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 { @@ -295,11 +300,13 @@ test("storage engine: rollback contract — unset the env and the legacy read pa // tail normalization, identically under both paths.) 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)); - delete process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "legacy"; const store2 = new SessionStore({ dir, debounceMs: 1, enabled: true }); const legacy = store2.loadSync("corpus-folded", { protocol: "anthropic", upstreamOrigin: "http://127.0.0.1:8199" }); store2.closeQuietly(); @@ -366,8 +373,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 c63f3f63fb..cb39fcd6a1 100644 --- a/tests/storage-orphan-recovery.test.ts +++ b/tests/storage-orphan-recovery.test.ts @@ -69,7 +69,11 @@ test("cold load recovers the pre-fix orphan freeze before processing a request", assert.equal(recovered.stats.requests, 876); assert.equal(recovered.state.nextBlockId, 4); assert.deepEqual(recovered.state.blocks.map(b => b.blockId), ["b3"]); - assert.equal(recovered.state.messageRefs.byRef.m00003, "raw-new"); + // Recovery is followed by #4316's one-time ledger alignment: raw-new is + // the first stored message, so its durable ref is seq 0 + 1. + assert.equal(recovered.state.messageRefs.byRaw["raw-new"], "m00001"); + assert.equal(recovered.state.messageRefs.byRef.m00001, "raw-new"); + 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"); From 858cb701a5720186622702abe5e9d4e6eeff3b92 Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Wed, 28 Oct 2026 03:47:58 -0700 Subject: [PATCH 6/7] test: fix Windows graceful shutdown and fixture teardown Windows SIGTERM terminates the child instead of running its graceful shutdown handler. Trigger the existing loopback shutdown route on Windows so the draining-marker regression exercises the same lifecycle as POSIX. Wait for child close before deleting its database, destroy sockets owned by the silent TCP fixture, and stop health polling when the child exits. Keep both regression scenarios and their behavioral assertions intact. Validation: the isolated CI-preloaded serial test file passes 6/6 and exits naturally in 11.58 seconds; npm run typecheck and npm run build pass. --- tests/issue4982-draining-twin-ladder.test.ts | 50 +++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/tests/issue4982-draining-twin-ladder.test.ts b/tests/issue4982-draining-twin-ladder.test.ts index 71ea29a279..d0eb94783e 100644 --- a/tests/issue4982-draining-twin-ladder.test.ts +++ b/tests/issue4982-draining-twin-ladder.test.ts @@ -228,21 +228,26 @@ async function waitForStatus(port: number, timeoutMs: number): Promise async function killChild(child: ChildProcess): Promise { if (child.exitCode !== null || child.signalCode !== null) return; - child.kill("SIGTERM"); - await Promise.race([ - new Promise((resolve) => child.once("exit", () => resolve())), - new Promise((resolve) => setTimeout(resolve, 10_000)), - ]); - if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL"); + await new Promise((resolve) => { + const timer = setTimeout(() => child.kill("SIGKILL"), 10_000); + child.once("close", () => { + clearTimeout(timer); + resolve(); + }); + child.kill("SIGTERM"); + }); } /** A TCP listener that accepts connections and then says nothing — invisible * to /__bili/health probes (they time out as "no evidence") but still owns * the socket, so binders get EADDRINUSE. Deterministically forces the LATE * EADDRINUSE decision path instead of the pre-bind foreign-owner ladder. */ -function silentTcpListener(port: number): Promise { +function silentTcpListener(port: number): Promise<{ close(): Promise }> { return new Promise((resolve, reject) => { + const sockets = new Set(); const server = net.createServer((sock) => { + sockets.add(sock); + sock.once("close", () => sockets.delete(sock)); // accept and hang — never answer HTTP; drain the probe's bytes so peer // EOF is actually delivered (a paused socket buffers them and never sees // EOF) and release the connection, or a dead prober pins server.close() @@ -251,7 +256,12 @@ function silentTcpListener(port: number): Promise { sock.on("end", () => sock.destroy()); }); server.once("error", reject); - server.listen(port, "127.0.0.1", () => resolve(server)); + server.listen(port, "127.0.0.1", () => resolve({ + close: () => new Promise((done) => { + server.close(() => done()); + for (const socket of sockets) socket.destroy(); + }), + })); }); } @@ -319,6 +329,9 @@ function spawnBili(sandbox: string, port: number): { child: ChildProcess; out: { async function waitUntilLadderedHealthy(stateHome: string, child: ChildProcess, label: string): Promise { let port = 0; await waitUntil(async () => { + if (child.exitCode !== null || child.signalCode !== null) { + throw new Error(`${label} exited before becoming healthy`); + } const m = readMarkers(stateHome).find((e) => e.pid === child.pid); if (!m || typeof m.port !== "number") return false; port = m.port as number; @@ -335,7 +348,7 @@ test("#4982 e2e: draining same-lane predecessor + squatter → newcomer ladders let p1Ref: ReturnType | null = null; let p2Ref: ReturnType | null = null; let heldRef: net.Socket | null = null; - let squatterRef: net.Server | null = null; + let squatterRef: Awaited> | null = null; try { // P1: the serving proxy that will enter graceful shutdown. const p1 = spawnBili(sandbox, port); @@ -353,7 +366,18 @@ test("#4982 e2e: draining same-lane predecessor + squatter → newcomer ladders }); heldRef = held; - p1.child.kill("SIGTERM"); + if (process.platform === "win32") { + // Windows SIGTERM terminates immediately instead of invoking the + // handler. The loopback admin route enters the same graceful path. + const response = await fetch(`http://127.0.0.1:${port}/__bili/shutdown`, { + method: "POST", + signal: AbortSignal.timeout(2_000), + }); + assert.equal(response.status, 202, "P1 must accept the graceful shutdown request"); + await response.arrayBuffer(); + } else { + p1.child.kill("SIGTERM"); + } const p1MarkerFile = path.join(stateHome, "billion-context", "instances", `${p1Marker.instanceId}.json`); await waitUntil(async () => { let stamped = false; @@ -395,7 +419,7 @@ test("#4982 e2e: draining same-lane predecessor + squatter → newcomer ladders heldRef?.destroy(); if (p1Ref) await killChild(p1Ref.child); const squatter = squatterRef; - if (squatter) await new Promise((resolve) => squatter.close(() => resolve())); + if (squatter) await squatter.close(); fs.rmSync(sandbox, { recursive: true, force: true }); } }); @@ -406,7 +430,7 @@ test("#4982 e2e: unverified registry twin claim (unstamped marker) → ladder, n const port = await freePort(); await assertPortDead(port, { label: "4982 unverified-twin port" }); let p2Ref: ReturnType | null = null; - let squatterRef: net.Server | null = null; + let squatterRef: Awaited> | null = null; try { // A registry marker claiming `port` with a LIVE pid and full identity // match — but NO portReleasedAt stamp (pre-fix build, or a failed @@ -456,7 +480,7 @@ test("#4982 e2e: unverified registry twin claim (unstamped marker) → ladder, n } finally { if (p2Ref) await killChild(p2Ref.child); const squatter = squatterRef; - if (squatter) await new Promise((resolve) => squatter.close(() => resolve())); + if (squatter) await squatter.close(); fs.rmSync(sandbox, { recursive: true, force: true }); } }); From 47b54b36aaf82fcd6b73e6cb3c55e1901bc89d3f Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Wed, 28 Oct 2026 06:11:26 -0700 Subject: [PATCH 7/7] test: handle reset health probes in silent TCP fixture Windows CI can reset an aborted probe connection instead of ending it. The silent TCP fixture did not handle errors on accepted sockets, so ECONNRESET became an uncaught test exception after the probe finished. Destroy reset sockets and add a real resetAndDestroy regression against the same fixture. Keep the existing port-owner and ladder assertions. Validation: the new regression reproduces ECONNRESET before the fix; the isolated CI-preloaded serial test file then passes 7/7 and exits naturally. Pure tsc and git diff --check pass. --- tests/issue4982-draining-twin-ladder.test.ts | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/issue4982-draining-twin-ladder.test.ts b/tests/issue4982-draining-twin-ladder.test.ts index d0eb94783e..5c8e178cce 100644 --- a/tests/issue4982-draining-twin-ladder.test.ts +++ b/tests/issue4982-draining-twin-ladder.test.ts @@ -254,6 +254,9 @@ function silentTcpListener(port: number): Promise<{ close(): Promise }> { // with CLOSE-WAIT sockets sock.resume(); sock.on("end", () => sock.destroy()); + // An aborted health probe may reset instead of ending the connection. + // Socket errors belong to this silent fixture, not the enclosing test. + sock.on("error", () => sock.destroy()); }); server.once("error", reject); server.listen(port, "127.0.0.1", () => resolve({ @@ -265,6 +268,25 @@ function silentTcpListener(port: number): Promise<{ close(): Promise }> { }); } +test("silent TCP fixture survives an aborted health probe", { timeout: 5_000 }, async () => { + const port = await freePort(); + const fixture = await silentTcpListener(port); + const client = net.connect({ host: "127.0.0.1", port }); + try { + await new Promise((resolve, reject) => { + client.once("connect", resolve); + client.once("error", reject); + }); + client.write("GET /__bili/health HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n"); + await new Promise((resolve) => setTimeout(resolve, 25)); + client.resetAndDestroy(); + await new Promise((resolve) => setTimeout(resolve, 25)); + } finally { + client.destroy(); + await fixture.close(); + } +}); + function tcpRefused(port: number): Promise { return new Promise((resolve) => { const s = net.connect({ port, host: "127.0.0.1" });