From 18e5171cffd1e02b2569511524a8b5062e9fd77a Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sat, 24 Oct 2026 09:10:23 -0700 Subject: [PATCH 01/10] fix: route codex context-only prewarms to the probe relay via the upstream proxy (#4594) A generate:false request that replays no conversation turn (empty input, or only developer/system/user messages) is a prewarm probe: forward it verbatim instead of running it as a zero-message ACP turn, which deactivated every fold block and re-injected the whole forged-summary list. History prewarms keep riding the pipeline. The probe relay now egresses through the same upstream proxy the relay resolves; ws-bridge exposes the resolved proxy instead of a boolean so the tunnel guard and the probe share one decision. Co-Authored-By: Claude Opus 5.5 --- src/responses-ws.ts | 31 ++++++---- src/server.ts | 2 +- src/ws-bridge.ts | 11 ++-- tests/codex-responses-ws.test.ts | 100 +++++++++++++++++++++++++++++-- 4 files changed, 124 insertions(+), 20 deletions(-) diff --git a/src/responses-ws.ts b/src/responses-ws.ts index 30a2202486..2114be2595 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -6,6 +6,7 @@ import { markWsLaneTransport, withFetchTransport } from "./fetch-transport.js"; import { BILI_HOP_HEADER } from "./server/context-window.js"; import { connectionNamedHeaders, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; import { BILI_TUNNEL_HEADER } from "./tunnel-guard.js"; +import { proxyDispatcher } from "./upstream-proxy.js"; import { normalizeSseLineEndings, finalizeSseLineEndings } from "./sse-util.js"; import type { WsBridgeCodec, WsBridgeContext, WsBridgeSession } from "./ws-bridge.js"; @@ -70,6 +71,11 @@ function object(value: unknown): value is JsonObject { return value !== null && typeof value === "object" && !Array.isArray(value); } +function isPrewarmProbe(body: JsonObject): boolean { + return body.generate === false && Array.isArray(body.input) && body.input.every(item => + object(item) && (item.type === undefined || item.type === "message") && (item.role === "developer" || item.role === "system" || item.role === "user")); +} + function describeEventType(frame: unknown): string { if (frame === null) return "null"; if (!object(frame)) return Array.isArray(frame) ? "array" : String(typeof frame); @@ -644,20 +650,25 @@ class ResponsesWsSession implements WsBridgeSession { for (const [key, value] of Object.entries(source.headers)) { if (!UPSTREAM_HOP_HEADERS.has(key) && !connectionHeaders.has(key) && !key.startsWith("sec-websocket-") && key !== "content-encoding") handshakeHeaders[key] = Array.isArray(value) ? value.join(", ") : String(value); } - // Prewarm probes (empty input, generate:false) are transport-layer - // noise, not conversation turns. Codex sends one on every new - // connection and expects only a terminal event back. Routing them - // through the ACP pipeline would normalize the empty input into a - // synthesized message (#1862) and record a phantom turn in the - // kernel's ref sequence, shifting every later fold anchor; injecting - // tools and compression prompts into a no-generation request is pure - // byte inflation. Forward verbatim on the upstream transport. - if (Array.isArray(body.input) && body.input.length === 0 && body.generate === false) { + // Prewarm probes (generate:false replaying no conversation turn) are + // transport-layer noise, not conversation turns. Codex sends one on + // every new connection — empty input (0.147), or only its developer / + // user context items (startup and idle-thread warmup) — and expects + // only a terminal event back. Routing them through the ACP pipeline + // would normalize empty input into a synthesized message (#1862) and + // record a phantom turn in the kernel's ref sequence, shifting every + // later fold anchor; a context-only view also deactivates every fold + // block and re-injects every forged summary (#4594). Injecting tools + // and compression prompts into a no-generation request is pure byte + // inflation. Forward verbatim on the upstream transport, through the + // relay's upstream proxy. A history prewarm replays real turns and + // stays on the pipeline. + if (isPrewarmProbe(body)) { void (async () => { const sendFrame = (payload: string): void => { if (peer.readyState === WebSocket.OPEN) peer.send(payload); }; let sawTerminal = false; try { - const response = await this.transport.fetch(this.context.upstreamUrl, { method: "POST", headers: { ...handshakeHeaders, "content-type": "application/json" }, body: JSON.stringify(body) }); + const response = await this.transport.fetch(this.context.upstreamUrl, { method: "POST", headers: { ...handshakeHeaders, "content-type": "application/json" }, body: JSON.stringify(body), dispatcher: proxyDispatcher(this.context.egressProxyFor?.(this.context.upstreamUrl)) }); if (response.body === null) { const text = await response.text(); if (!response.ok) { diff --git a/src/server.ts b/src/server.ts index 2dd74daee8..4fc1f0f985 100644 --- a/src/server.ts +++ b/src/server.ts @@ -709,7 +709,7 @@ export async function startServer(opts: ProxyOptions): Promise { // Generic WebSocket bridge: protocol codecs claim upgrades here (#1467 // phase-2 shell); the Responses codec is the first (and currently only) // entry. Unclaimed upgrades still fall through to the 426 contract below. - const wsUpgrade = installWebSocketBridge(server, dispatch, log, [responsesCodec, codexResponsesCodec], (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined, instanceId); + const wsUpgrade = installWebSocketBridge(server, dispatch, log, [responsesCodec, codexResponsesCodec], (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy, instanceId); // Unclaimed upgrades retain the immediate HTTP fallback contract. // An explicit 'upgrade' listener is // required: without one Node's behavior is version-dependent (some diff --git a/src/ws-bridge.ts b/src/ws-bridge.ts index 4ecdecb929..d370d8fc56 100644 --- a/src/ws-bridge.ts +++ b/src/ws-bridge.ts @@ -50,6 +50,9 @@ export interface WsBridgeContext { * handshake — probe and turns alike — so a chained bili sees one * consistent processor marker instead of marker-less probes. */ instanceId?: string; + /** The upstream proxy the relay's egress uses for a destination (undefined = direct). + * Codec traffic that egresses outside the relay (the prewarm probe) resolves the same route. */ + egressProxyFor?: (destinationUrl: string) => string | undefined; } export interface WsBridgeCodec { @@ -86,8 +89,8 @@ export function installWebSocketBridge( dispatch: (req: http.IncomingMessage, res: http.ServerResponse) => Promise, log: (level: string, message: string) => void, codecs: readonly WsBridgeCodec[], - /** #2124: whether a WS-upstream destination's egress leaves through an upstream proxy (its hostname is then resolved remotely by that proxy, not the local resolver). */ - isEgressProxied?: (destinationUrl: string) => boolean, + /** #2124: the upstream proxy a WS-upstream destination's egress leaves through, if any (a proxied hostname is resolved remotely by that proxy, not the local resolver). */ + egressProxyFor?: (destinationUrl: string) => string | undefined, /** #300/#3073: this server's per-instance identity for the x-bili-hop chain stamp (see WsBridgeContext.instanceId). */ instanceId?: string, ): (req: http.IncomingMessage, socket: Duplex, head: Buffer) => boolean { @@ -127,7 +130,7 @@ export function installWebSocketBridge( // side requests are identified by the #1699 persona header instead). source.headers["x-bili-ws-lane"] = codec.name; void (async () => { - const verdict = await checkTunnelDestination(upstream, { selfPort: source.socket.localPort, clientLoopback: true, allowlist: tunnelAllowlistFromEnv(), egressProxied: isEgressProxied?.(upstream) }); + const verdict = await checkTunnelDestination(upstream, { selfPort: source.socket.localPort, clientLoopback: true, allowlist: tunnelAllowlistFromEnv(), egressProxied: egressProxyFor?.(upstream) !== undefined }); if (!verdict.ok) { socket.end("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); return; @@ -136,7 +139,7 @@ export function installWebSocketBridge( wss.handleUpgrade(source, socket, head, peer => { const label = `[${codec.name}] [conn=${++connectionId}] [session=${JSON.stringify(conversationId.slice(0, 128))}]`; const trace: WsBridgeLog = (level, message) => log(level, `${label} ${message}`); - const session = codec.createSession({ log: trace, peer, source, upstreamUrl: upstream, dispatch, codec, instanceId }); + const session = codec.createSession({ log: trace, peer, source, upstreamUrl: upstream, dispatch, codec, instanceId, egressProxyFor }); const entry = { session, peer }; sessions.add(entry); peer.on("error", () => {}); diff --git a/tests/codex-responses-ws.test.ts b/tests/codex-responses-ws.test.ts index cc31b88cb3..a28db271c5 100644 --- a/tests/codex-responses-ws.test.ts +++ b/tests/codex-responses-ws.test.ts @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import http from "node:http"; import fs from "node:fs"; +import net from "node:net"; import os from "node:os"; import path from "node:path"; import { on, once } from "node:events"; @@ -24,13 +25,14 @@ process.env.XDG_DATA_HOME = testHome; process.env.XDG_CACHE_HOME = testHome; const user = (text: string): Item => ({ type: "message", role: "user", content: [{ type: "input_text", text }] }); +const developer = (text: string): Item => ({ type: "message", role: "developer", content: [{ type: "input_text", text }] }); // Mirrors the captured codex 0.147.0 Responses-over-WebSocket client: the // upgrade targets the ordinary prefix-mode path with codex's own transport // headers (no plugin marker), and every request is a full `response.create` // replay — a `generate: false` probe with empty input first, then one // full-history frame per turn. Codex never sends previous_response_id. -async function fixture() { +async function fixture(overrides: Partial = {}) { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-ws-")); _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); @@ -48,8 +50,8 @@ async function fixture() { rows.push({ request, headers: req.headers }); const id = `resp_codex_${++serial}`; const send = (type: string, fields: Item): void => peer.send(JSON.stringify({ type, ...fields })); - if (Array.isArray(request.input) && request.input.length === 0) { - // Probe: codex expects a terminal event, nothing more. + if (request.generate === false || (Array.isArray(request.input) && request.input.length === 0)) { + // Probe / no-generation warmup: codex expects a terminal event, nothing more. send("response.created", { response: { id, status: "in_progress", output: [] } }); send("response.completed", { response: { id, status: "completed", output: [], usage: { input_tokens: 0, output_tokens: 1, total_tokens: 1 } } }); return; @@ -73,7 +75,7 @@ async function fixture() { await once(upstream, "listening"); const config = defaultConfig(200000); config.preserveRecentTokens = 0; - const opts: ProxyOptions = { host: "127.0.0.1", port: 0, upstream: "http://127.0.0.1", routes: {}, modelContextLimit: 200000, kernelConfig: config, compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, logFile: path.join(tmp, "bili.log"), debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } }; + const opts: ProxyOptions = { host: "127.0.0.1", port: 0, upstream: "http://127.0.0.1", routes: {}, modelContextLimit: 200000, kernelConfig: config, compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, logFile: path.join(tmp, "bili.log"), debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] }, ...overrides }; const proxy = await startServer(opts); await once(proxy, "listening"); const proxyOrigin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; @@ -108,7 +110,7 @@ async function fixture() { // terminal event before sending the real turn; the test must respect the // same ordering or the session correctly rejects it with response_in_progress. const probe = (client: WebSocket = peer): Promise => exchange(frame([], { generate: false }), client); - const turn = (input: Item[], client: WebSocket = peer): Promise => exchange(frame(input), client); + const turn = (input: Item[], client: WebSocket = peer, extra: Item = {}): Promise => exchange(frame(input, extra), client); const peer = await openPeer(); peer.on("error", e => console.error("[TEST-CLIENT-ERR]", e.message)); return { rows, upstreamConnections, sid, proxyOrigin, upstreamOrigin, proxy, peer, turn, probe, frame, openPeer, @@ -361,6 +363,94 @@ test("codex WS: folding covered history shrinks the replayed turn", { timeout: 3 } finally { await f.close(); } }); +// codex's startup / idle-thread warmup is generate:false carrying only its +// developer/user context items. Run as a pipeline turn, that zero-conversation +// view deactivated every fold and re-injected every forged summary (#4594). +test("codex WS: a context-only generate:false prewarm is relayed verbatim and leaves the fold intact (#4594)", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + await f.probe(); + const replay: Item[] = []; + const play = async (text: string): Promise => { + const reply = completed(await f.turn([...replay, user(text)])); + replay.push(user(text)); + const message = (reply.output as Item[]).find(item => item.type === "message"); + if (message) replay.push(message); + }; + await play("session purpose: codex prewarm regression"); + await play("PREWARM-BULKY " + "unique filler content ".repeat(1200)); + const bulky = JSON.stringify((f.rows.at(-1)!.request.input as Item[]).find(item => JSON.stringify(item).includes("PREWARM-BULKY"))); + const ref = bulky.match(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/)?.[1]; + assert.ok(ref, bulky.slice(-300)); + for (let i = 0; i < 4; i++) await play(`push ${i}`); + const args = { content: [{ startId: ref, endId: ref, summary: "SUMMARY-PREWARM-FOLD: bulky deterministic filler, folded before the prewarm arrives." }] }; + const result = await fetch(`${f.proxyOrigin}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ conversationId: f.sid, tool: "compress", args }) }); + assert.equal(((await result.json()) as { ok: boolean }).ok, true); + const session = peekSession(f.sid); + assert.ok(session); + assert.ok(session.state.blocks.some(b => b.active)); + session.metadata.codexForgedSummaries = ["FORGED-SUMMARY-SENTINEL"]; + const requests = session.stats.requests; + const context = [developer("codex permissions and instructions"), user("environment context: cwd=/repo shell=bash")]; + const warm = completed(await f.turn(context, f.peer, { generate: false })); + assert.deepEqual(warm.output, []); + const sent = f.rows.at(-1)!.request; + assert.equal(sent.generate, false); + assert.deepEqual(sent.input, context, "the prewarm reaches upstream verbatim"); + assert.deepEqual(sent.tools, [], "no ACP tools ride a no-generation request"); + assert.ok(!JSON.stringify(sent).includes("FORGED-SUMMARY-SENTINEL"), "forged summaries are not re-injected into the prewarm"); + assert.equal(session.stats.requests, requests, "the prewarm is not a pipeline turn"); + assert.ok(peekSession(f.sid)?.state.blocks.some(b => b.active), "the prewarm leaves the fold active"); + } finally { await f.close(); } +}); + +test("codex WS: a history-replaying generate:false prewarm stays on the ACP pipeline (#4594)", { timeout: 30000 }, async () => { + const f = await fixture(); + try { + await f.probe(); + completed(await f.turn([user("history-turn")])); + const warm = completed(await f.turn([user("history-turn"), { type: "message", role: "assistant", content: [{ type: "output_text", text: "prior answer" }] }], f.peer, { generate: false })); + assert.deepEqual(warm.output, []); + const sent = f.rows.at(-1)!.request; + assert.equal(sent.generate, false); + assert.ok((sent.tools as Item[]).some(t => t.name === "acp_status" || t.name === "compress"), "a history prewarm is still processed by the pipeline"); + assert.equal(peekSession(f.sid)?.stats.requests, 2); + } finally { await f.close(); } +}); + +test("codex WS: the prewarm probe egresses through the configured upstream proxy (#4594)", { timeout: 30000 }, async () => { + let connects = 0; + const tunnels = new Set(); + const upstreamProxy = http.createServer(); + upstreamProxy.on("connect", (req, socket, head) => { + connects++; + const destination = new URL(`http://${req.url}`); + const tunnel = net.connect(Number(destination.port), destination.hostname, () => { + socket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); + if (head.length) tunnel.write(head); + tunnel.pipe(socket); + socket.pipe(tunnel); + }); + tunnels.add(tunnel); + tunnel.on("error", () => socket.destroy()); + tunnel.on("close", () => { tunnels.delete(tunnel); socket.destroy(); }); + socket.on("error", () => tunnel.destroy()); + socket.on("close", () => tunnel.destroy()); + }); + upstreamProxy.listen(0, "127.0.0.1"); + await once(upstreamProxy, "listening"); + const f = await fixture({ proxy: `http://127.0.0.1:${(upstreamProxy.address() as { port: number }).port}` }); + try { + await f.probe(); + assert.equal(connects, 1, "the probe's upstream WebSocket tunnels through the configured proxy"); + assert.deepEqual(f.rows[0]?.request.input, []); + } finally { + await f.close(); + for (const tunnel of tunnels) tunnel.destroy(); + await new Promise(resolve => upstreamProxy.close(() => resolve())); + } +}); + test("codex WS: admission requires the conversation header and the responses path", { timeout: 30000 }, async () => { const f = await fixture(); const origin = f.proxyOrigin.replace(/^http/, "ws"); From 64cab41050ebb17f12832092a14531191a7640ca Mon Sep 17 00:00:00 2001 From: z0d0dOr <128966342+z0d0dOr@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:32:33 -0700 Subject: [PATCH 02/10] fix: carry the prewarm proxy route through the inline bridge --- src/inline-engine.ts | 2 +- src/inline-ws.ts | 7 ++--- tests/pi-inline-ws.test.ts | 57 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/inline-engine.ts b/src/inline-engine.ts index 2e53cb65b1..5d3ea81302 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -423,7 +423,7 @@ async function startInlineEngine(): Promise { const bridge = startInlineWsBridge({ dispatchHttp, log: ctx.log, - isEgressProxied: (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined, + egressProxyFor: (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy, instanceId: ctx.instanceId, }); const wsOrigin = bridge diff --git a/src/inline-ws.ts b/src/inline-ws.ts index cf893035e0..9c849d12ff 100644 --- a/src/inline-ws.ts +++ b/src/inline-ws.ts @@ -37,9 +37,8 @@ interface InlineWsBridge { export async function startInlineWsBridge(options: { dispatchHttp: (req: http.IncomingMessage, res: http.ServerResponse) => Promise; log: (level: string, message: string) => void; - /** #2124 parity with startServer(): lets checkTunnelDestination know when - * a destination resolves remotely (upstream proxy owns DNS). */ - isEgressProxied?: (destinationUrl: string) => boolean; + /** Same egress route as startServer(), including prewarm probes outside the pipeline. */ + egressProxyFor?: (destinationUrl: string) => string | undefined; /** #300/#3073 hop-chain instance identity, same as the spawned server. */ instanceId?: string; }): Promise { @@ -55,7 +54,7 @@ export async function startInlineWsBridge(options: { options.dispatchHttp, options.log, [responsesCodec, codexResponsesCodec], - options.isEgressProxied, + options.egressProxyFor, options.instanceId, ); server.on("upgrade", (source, socket, head) => { diff --git a/tests/pi-inline-ws.test.ts b/tests/pi-inline-ws.test.ts index d96fe00542..c013d69cef 100644 --- a/tests/pi-inline-ws.test.ts +++ b/tests/pi-inline-ws.test.ts @@ -1,5 +1,7 @@ import assert from "node:assert/strict"; import http from "node:http"; +import net from "node:net"; +import { once } from "node:events"; import test from "node:test"; import { WebSocket as WsClient, WebSocketServer } from "ws"; import { @@ -214,6 +216,61 @@ test("inline WS: plain HTTP against the bridge port is refused with 426", async assert.match(await response.text(), /WebSocket upgrade required/); }); +test("inline WS: context-only prewarm uses the configured upstream proxy", { timeout: TMO }, async () => { + const upstream = await startFakeUpstream(); + let connects = 0; + const tunnels = new Set(); + const upstreamProxy = http.createServer(); + upstreamProxy.on("connect", (req, socket, head) => { + connects++; + const destination = new URL(`http://${req.url}`); + const tunnel = net.connect(Number(destination.port), destination.hostname, () => { + socket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); + if (head.length) tunnel.write(head); + tunnel.pipe(socket); + socket.pipe(tunnel); + }); + tunnels.add(tunnel); + tunnel.on("error", () => socket.destroy()); + tunnel.on("close", () => { tunnels.delete(tunnel); socket.destroy(); }); + socket.on("error", () => tunnel.destroy()); + socket.on("close", () => tunnel.destroy()); + }); + upstreamProxy.listen(0, "127.0.0.1"); + await once(upstreamProxy, "listening"); + const proxyUrl = `http://127.0.0.1:${(upstreamProxy.address() as { port: number }).port}`; + const bridge = await startInlineWsBridge({ + dispatchHttp: async () => { throw new Error("context-only prewarm must bypass the pipeline"); }, + log: () => {}, + egressProxyFor: () => proxyUrl, + }); + const client = new WsClient(`${bridge.origin.replace(/^http/, "ws")}/bili/${upstream.url}`, { + headers: { "session-id": "conv-inline-prewarm-proxy" }, + }); + try { + const terminal = new Promise((resolve, reject) => { + client.on("error", reject); + client.on("message", data => { + const frame = JSON.parse(data.toString()) as Frame; + if (frame.type === "response.completed") resolve(); + }); + }); + await once(client, "open"); + const input = [{ type: "message", role: "developer", content: [{ type: "input_text", text: "startup context" }] }]; + client.send(JSON.stringify({ type: "response.create", model: "gpt-test", input, generate: false })); + await withTimeout(terminal, TMO, "prewarm response.completed"); + assert.equal(connects, 1, "the inline prewarm tunnels through the selected upstream proxy"); + assert.deepEqual(upstream.seen[0]?.input, input); + assert.equal(upstream.seen[0]?.generate, false); + } finally { + client.terminate(); + await bridge.close(); + for (const tunnel of tunnels) tunnel.destroy(); + await new Promise(resolve => upstreamProxy.close(() => resolve())); + await upstream.close(); + } +}); + test("inline WS: wsOrigin settling undefined degrades the codex lane DIRECT (warned once)", async () => { const descriptor = savedDescriptor(); _resetForTest(); From a2d6759313d3534ca0e451cefd3ba1ae6327a23c 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 03/10] 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 60d57d5b01f46dc25eb43c9c0db386928079b981 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 04/10] 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 607940f5b417fc0d4e70dc1dded5d3a0e2e74efb 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 05/10] 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 8511d34ed8e122289fd3eb7f5e1e04396fe338f1 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 06/10] 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 168c0b0f9487a2ac74f6b4f2a7badff5687cff9f 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 07/10] 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 5504e08977197c89e3202ea0d2ec254c73144fea 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 08/10] 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" }); From 13e6b519849242d9ac84de78ee2f1f3a0790b548 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 12:58:20 +0800 Subject: [PATCH 09/10] fix(tests): adapt ws-bridge-pending-close mock to the egressProxyFor param shape MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The master-added tests/ws-bridge-pending-close.test.ts was written against the old isEgressProxied?: (url) => boolean param. The PR renamed that param to egressProxyFor?: (url) => string | undefined, so the test's () => proxied mock no longer typechecks (TS2345 on static-gates). Pass a proxy URL string when proxied, undefined otherwise — preserves the exact egressProxied boolean the tunnel-guard derives (egressProxyFor?.(upstream) !== undefined). Final reviewed head (full CI pass). --- tests/ws-bridge-pending-close.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ws-bridge-pending-close.test.ts b/tests/ws-bridge-pending-close.test.ts index 05ab031718..8e78835d83 100644 --- a/tests/ws-bridge-pending-close.test.ts +++ b/tests/ws-bridge-pending-close.test.ts @@ -21,7 +21,7 @@ function fixture(proxied: boolean) { return { onMessage() {}, onClose() {}, shutdown() {} }; }, }; - const handler = installWebSocketBridge(server, async () => {}, () => {}, [codec], () => proxied); + const handler = installWebSocketBridge(server, async () => {}, () => {}, [codec], () => (proxied ? "http://127.0.0.1:3128" : undefined)); return { server, handler, created: () => created }; } From e7dd72123a38ecf164c8aa9f51c476086ca53bb5 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 13:34:07 +0800 Subject: [PATCH 10/10] fix(tests): correct stale prewarm-guard comment to match post-#3342 probe-relay behavior The comment described the pre-#3342 / pre-#4595 behavior: it claimed the session-level prewarm guard catches ONLY empty-input probes and forwards them 'without committing a checkpoint', and that a non-empty prewarm goes through the ACP pipeline. Both are now inaccurate: since #3342 the probe relay commits a checkpoint from the frame body, and since #4595 the guard (isPrewarmProbe) also relays context-only (developer/system/user) prewarms verbatim. The test itself still pins the expand() guard that strips generate:false from a committed prewarm body; only the comment needed to track current behavior. --- tests/responses-prewarm-generate.test.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/responses-prewarm-generate.test.ts b/tests/responses-prewarm-generate.test.ts index c6007ad972..722c26a911 100644 --- a/tests/responses-prewarm-generate.test.ts +++ b/tests/responses-prewarm-generate.test.ts @@ -89,12 +89,13 @@ test("continuation preserves prior input/output/tools and current metadata; chec assert.equal(JSON.stringify(priorRequest), snapshotBefore); }); -// The session-level prewarm guard (src/responses-ws.ts:512) catches only -// EMPTY-input probes (input.length === 0 && generate === false) and forwards -// them verbatim without committing a checkpoint. A NON-empty prewarm (e.g. a -// developer message + generate:false) goes through the ACP pipeline and gets -// committed, so its generate:false leaks into the next continuation via -// expand(). This test pins that specific trigger shape. +// The session-level prewarm guard (isPrewarmProbe, src/responses-ws.ts) +// forwards generate:false frames that replay no conversation turn (empty +// input, or only developer/system/user messages) verbatim on the probe +// relay, which still commits a #3342 checkpoint from the frame's body. A +// history-replaying prewarm rides the ACP pipeline instead. Either way a +// committed generate:false body can reach expand(), so this test pins the +// guard that strips it from a later continuation. test("non-empty prewarm with developer message + generate:false does not poison continuation (#2966)", () => { const history = new ResponsesWsHistory(); history.commit(