From e2a706692656c12cf4890e01359be02d9b2ae8f1 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 21:52:41 +0800 Subject: [PATCH 01/14] fix(pi): inline mode now intercepts codex Responses WebSocket traffic (#4526) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BILI_PI_INLINE left the Codex Responses WebSocket lane unpatched on the false premise that "the pi lane does not use codex WS" (pi rides the codex provider over that exact lane, #2073/#2111), so codex WS dialed the model directly — uncompressed, no ACP session record. - inline engine binds a loopback-only WS-upgrade listener (ephemeral 127.0.0.1 port, unref'd) serving the same per-request pipeline through installWebSocketBridge verbatim (loopback-source admission, tunnel destination SSRF guard, 426 fallthrough parity with server.ts); a bind failure degrades ONLY the WS channel direct (warned) — HTTP compression and the engine stay healthy - native-ws-intercept gains the inline lane: rewrites through state.resolveInlineWs/wsOrigin instead of the undialable pseudo-origin; defers connections until the engine decision settles; bootstrap failure degrades direct. Spawn/attach lanes byte-identical (branch keyed on the inline-only resolver being present) - BILI_PI_INLINE value table: any non-empty value other than 0/false/no/off now enables (on/yes/etc. used to fall through to spawn silently, reading as "inline does nothing"); off tokens expanded from 0/false to 0/false/no/off — they now disable over the config-file pi.inline value too; arm-time info log makes the switch observable - docs: CONFIGURATION.md/.zh-CN.md, LIFECYCLE.md - tests: planNativePi truth table, WS-intercept inline seams (fast-path rewrite / deferred hold / degrade-direct / bootstrap rejection), new issue4526-pi-inline-ws integration suite (real engine boot + real ws client round trip through the bridge: probe + turn, ACP tools injected, session recorded; 404 plain HTTP; 426 wrong-shape upgrades) --- .tmpfailids.txt | 1 + CONFIGURATION.md | 4 +- CONFIGURATION.zh-CN.md | 4 +- LIFECYCLE.md | 2 +- src/agent/native-intercept.ts | 15 ++ src/agent/native-ws-intercept.ts | 36 ++++ src/agent/pi-native.ts | 45 ++++- src/inline-engine.ts | 139 +++++++++++-- tests/issue4526-pi-inline-ws.test.ts | 269 +++++++++++++++++++++++++ tests/native-ws-intercept.test.ts | 94 +++++++++ tests/pi-native.test.ts | 21 ++ website/config-reference/env-only.yaml | 4 +- website/config-reference/server.yaml | 4 +- 13 files changed, 607 insertions(+), 31 deletions(-) create mode 100644 .tmpfailids.txt create mode 100644 tests/issue4526-pi-inline-ws.test.ts diff --git a/.tmpfailids.txt b/.tmpfailids.txt new file mode 100644 index 0000000000..cbf5e0f5f1 --- /dev/null +++ b/.tmpfailids.txt @@ -0,0 +1 @@ +120896365209 120896364720 \ No newline at end of file diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 471913adc4..3a6ad3a83f 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -136,7 +136,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `native.attachUrl` | string | unset (discover per lane) | BILI_MCP_PROXY | Explicit target origin for native-hook attach (any URL). When set, a hook attaches ONLY to that exact origin instead of auto-discovering, and spawns its own if it is not live; it must still be code-compatible and pass the lifecycle gate. The BILI_MCP_PROXY env var (also set by CLI --origin) takes precedence over this file value; empty/whitespace means unset. | | `claude.nativePort` | number | unset (lane sticky zone port) | BILI_CLAUDE_NATIVE_PORT | Exact port pin for the claude native lane's hook-spawned proxy (strict-port: squatters are refused loudly). | | `pi.subagents` | object \| boolean | {} (acp_delegate enabled with package defaults) | PI_ACP_DELEGATE_FORCE_ENABLE, PI_ACP_DELEGATE_MAX_DEPTH, PI_ACP_DELEGATE_SYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_IDLE_TIMEOUT_MINUTES, PI_ACP_DELEGATE_ASYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_MAX_CONCURRENT | Built-in pi-lane sub-agents (acp_delegate) surface, #2230 config-home. The pi.subagents section of billion-context.json owns the delegate config; the four acp.json keys (delegate/delegatePrompt/displayUsage/debug) are a deprecated fallback read only while the section is absent. prompt replaces delegatePrompt; debug is scoped to the sub-agent subsystem. Boolean shorthand subagents: false disables the whole surface. Full field table: the pi section in CONFIGURATION.md. | -| `pi.inline` | boolean | false | BILI_PI_INLINE | Pi inline lane (#3278): run the pi extension's compression IN-PROCESS — no spawned proxy, no ephemeral port; model traffic and the /__bili//__acp tool surface are served by an in-process engine through a pseudo-origin. Resolved env > file > default(off): BILI_PI_INLINE=1/true enables, BILI_PI_INLINE=0/false explicitly disables over this file value. | +| `pi.inline` | boolean | false | BILI_PI_INLINE | Pi inline lane (#3278): run the pi extension's compression IN-PROCESS — no spawned proxy; model traffic and the /__bili//__acp tool surface are served by an in-process engine through a pseudo-origin, and Codex Responses WebSocket traffic by a loopback-only upgrade bridge inside the engine (#4526; a bind failure degrades only that channel direct, warned). Resolved env > file > default(off): BILI_PI_INLINE accepts ANY non-empty value except 0/false/no/off as on (previously only 1/true — on/yes silently spawned); the four off-tokens explicitly disable over this file value. | **MITM lane** @@ -396,7 +396,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `BILI_NATIVE_OMP` | boolean-ish | off | — | Host posture: marks an omp-native plugin process. | | `BILI_NATIVE_OPENCODE` | boolean-ish | off | — | Host posture: marks an opencode-native plugin process. | | `BILI_NATIVE_PI` | boolean-ish | off | — | Host posture: marks a pi-native plugin process. | -| `BILI_PI_INLINE` | boolean-ish | off | — | Host posture: pi inline lane (#3278) — the pi extension compresses IN-PROCESS through the pseudo-origin instead of spawning its own proxy; outranks a preset BILLION_CONTEXT_PROXY attach target. | +| `BILI_PI_INLINE` | boolean-ish | off | — | Host posture: pi inline lane (#3278) — the pi extension compresses IN-PROCESS through the pseudo-origin instead of spawning its own proxy; outranks a preset BILLION_CONTEXT_PROXY attach target. Value table (#4526): any non-empty value except 0/false/no/off enables (previously only 1/true — on/yes silently fell back to spawn); the four off-tokens disable even over config-file pi.inline. | | `BILI_NATIVE_ZCODE` | boolean-ish | off | — | Host posture: marks a zcode-native plugin process. | | `BILI_PI_BIN` | string | pi (PATH lookup) | — | Host posture: explicit binary path for the pi client binary. | | `BILI_DSH_BIN` | string | dsh (PATH lookup) | — | Host posture: explicit binary path for the dsh client binary. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 77c116c811..fbd377d481 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -136,7 +136,7 @@ | `native.attachUrl` | string | unset (discover per lane) | BILI_MCP_PROXY | native hook 附着的显式目标源(任意 URL)。设置后,hook 只附着到该精确 origin 而不是自动发现,若其不在线则自行派生;仍需代码兼容并通过生命周期门禁。BILI_MCP_PROXY 环境变量(CLI --origin 亦会设置它)优先于此文件值;空/空白视为未设置。 | | `claude.nativePort` | number | unset (lane sticky zone port) | BILI_CLAUDE_NATIVE_PORT | claude 原生车道钩子派生代理的精确端口钉死(严格端口:被占用时响亮拒绝而非跳端口)。 | | `pi.subagents` | object \| boolean | {} (acp_delegate enabled with package defaults) | PI_ACP_DELEGATE_FORCE_ENABLE, PI_ACP_DELEGATE_MAX_DEPTH, PI_ACP_DELEGATE_SYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_IDLE_TIMEOUT_MINUTES, PI_ACP_DELEGATE_ASYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_MAX_CONCURRENT | 内置 pi lane 子代理(acp_delegate)面(#2230 配置搬家)。billion-context.json 的 pi.subagents 段拥有 delegate 配置;acp.json 四键(delegate/delegatePrompt/displayUsage/debug)为已废弃回退源,仅段缺失时读取。prompt 取代 delegatePrompt;debug 限定子代理子系统。布尔简写 subagents: false 整体关闭。完整字段表见 CONFIGURATION.md 的 pi 段。 | -| `pi.inline` | boolean | false | BILI_PI_INLINE | pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务。解析顺序 env > 文件 > 默认(off):BILI_PI_INLINE=1/true 开启,BILI_PI_INLINE=0/false 显式关闭且优先于本文件值。 | +| `pi.inline` | boolean | false | BILI_PI_INLINE | pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务,Codex Responses WebSocket 流量由引擎内的回环升级桥接服务(#4526;绑定失败仅该车道回退直连并告警)。解析顺序 env > 文件 > 默认(off):BILI_PI_INLINE 除 0/false/no/off 外任何非空值均视为开(此前仅认 1/true——on/yes 会静默回落到 spawn);四个关闭取值显式优先于本文件值。 | **MITM 通道** @@ -396,7 +396,7 @@ | `BILI_NATIVE_OMP` | boolean-ish | off | — | 宿主姿态:标记 omp 原生插件进程。 | | `BILI_NATIVE_OPENCODE` | boolean-ish | off | — | 宿主姿态:标记 opencode 原生插件进程。 | | `BILI_NATIVE_PI` | boolean-ish | off | — | 宿主姿态:标记 pi 原生插件进程。 | -| `BILI_PI_INLINE` | boolean-ish | off | — | 宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。 | +| `BILI_PI_INLINE` | boolean-ish | off | — | 宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。取值表(#4526):除 0/false/no/off 外任何非空值均开启(此前仅认 1/true——on/yes 会静默回落 spawn);四个关闭取值即使配置文件 pi.inline 为 true 也强制关闭。 | | `BILI_NATIVE_ZCODE` | boolean-ish | off | — | 宿主姿态:标记 zcode 原生插件进程。 | | `BILI_PI_BIN` | string | pi (PATH lookup) | — | 宿主姿态:pi 客户端二进制的显式路径。 | | `BILI_DSH_BIN` | string | dsh (PATH lookup) | — | 宿主姿态:dsh 客户端二进制的显式路径。 | diff --git a/LIFECYCLE.md b/LIFECYCLE.md index 3c72995136..bcd79e3863 100644 --- a/LIFECYCLE.md +++ b/LIFECYCLE.md @@ -48,7 +48,7 @@ | `BILI_ALLOW_DSH_COMPACTION` | dsh compaction | dsh compaction opt-in(env 语义同 BILI_PERSIST 家族) | 转正后移 §2 | opt-in(`=1`) | | `BILI_SNAPSHOT_SHADOW` | #3756 | snapshot-render 影子观测:每个 armed turn 计算快照视图但**不使用**(出站仍走 legacy 路径),与实际发送对比记 per-session 计数(面板 + `snapshot-shadow` 诊断话题)——render 引擎在 rollout 翻转前用真实流量证明健康(dry-run-then-flip,同存储 #2815 shadow→#3818 full 纪律) | render 车道转正(rollout 100% 且影子计数归稳)后退役 | 默认 on(render lane 本身 rollout 0%,纯观测) | | `BILI_DSH_AUTHORED_COMPACTION` | #3733 | dsh 原生 compaction summarize 调用由 #1729 guard 拒绝改为**本地应答折叠快照**(零出账;dsh 记 checkpoint、会话文件收缩) | adoption 后按 #2028 模式转 file/web 开关,env 退役(file 键 `dsh.authoredCompaction` 已镜像) | 0.1.203 dogfood 站稳后默认 on;`=0` 回 #1729 拒绝路径 | -| `BILI_PI_INLINE` | #3278 | pi inline 模式(默认 off):不起 proxy 进程,fetch 链在 intercept 之下的一个环节把伪 origin 流量路由进进程内引擎(inline-engine.ts,跑同 startServer 的每请求管线);file 家 `"pi": {"inline": true}`,`=0` 显式关 | inline 车道 GA(转 §2 常设)或实验放弃后收束 | opt-in(默认 off) | +| `BILI_PI_INLINE` | #3278/#4526 | pi inline 模式(默认 off):不起 proxy 进程,fetch 链在 intercept 之下的一个环节把伪 origin 流量路由进进程内引擎(inline-engine.ts,跑同 startServer 的每请求管线),Codex Responses WS 走引擎内的回环桥接(#4526,绑定失败仅该车道回退直连);file 家 `"pi": {"inline": true}`,`0/false/no/off` 显式关,其余非空值均开(#4526) | inline 车道 GA(转 §2 常设)或实验放弃后收束 | opt-in(默认 off) | | 测试钩子:`BILI_DSH_RECOVERY_INTERVAL_MS` / `BILI_DSH_RETRY_INTERVAL_MS` / `BILI_MODEL_INFO_RETRY_MS` | #2082 等 | 缩短测试/运维节奏(dsh-native.ts;NODE_TEST_CONTEXT 感知,避免生产误缩) | 永久测试设施,随宿主代码走 | 测试钩子 | ## 2. 常设面全量登记表(src/ 实际读取的其余 BILI_* 环境变量) diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index 3f22eaf97a..cd537792f5 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -44,6 +44,21 @@ export interface NativeInterceptState { origin: string | undefined; /** Resolves to the proxy origin once healthy, or undefined on failure. */ ready: Promise; + /** #4526: inline-lane Codex Responses WebSocket target — the dialable + * loopback origin of the in-process engine's WS bridge + * ("http://127.0.0.1:"). Written once the engine settles; stays + * undefined when the bridge bind failed (the channel degrades direct). + * Only set in inline mode — every other lane routes WS through + * state.origin exactly as before. */ + wsOrigin?: string; + /** #4526: inline-lane Codex Responses WebSocket decision resolver — the + * WS wrapper awaits it instead of readyOrigin in inline mode (state.origin + * is frozen to the pseudo-origin that never listens, so readyOrigin would + * short-circuit to an undialable URL). Resolves to wsOrigin once the + * engine settles, or undefined when the bridge bind failed; a bootstrap + * rejection propagates and the wrapper degrades direct. Invoking it may + * trigger the memoized engine start. Absent on every non-inline lane. */ + resolveInlineWs?: () => Promise; /** Owner hook: re-run the bootstrap (proxy died → respawn). */ respawn?: () => Promise; /** Owner hook: fired once when a respawn attempt fails and the session diff --git a/src/agent/native-ws-intercept.ts b/src/agent/native-ws-intercept.ts index 4e4e43aeca..64dbef871e 100644 --- a/src/agent/native-ws-intercept.ts +++ b/src/agent/native-ws-intercept.ts @@ -403,6 +403,42 @@ export function installNativeWebSocketIntercept(initialState: NativeInterceptSta state.onDispatch?.(rewritten, "rewrite"); return new RoutedWebSocket(rewritten, ...rest); }; + const resolveInlineWs = state.resolveInlineWs; + if (resolveInlineWs !== undefined) { + // #4526 inline lane: state.origin is the pseudo-origin that never + // listens — the only dialable endpoint is the engine's loopback WS + // bridge. Decision already made → route now; still booting → hold + // the shell until it settles (the engine start is memoized). + const settled = state.wsOrigin; + if (settled !== undefined) { + const rewritten = rewriteCodexResponsesWsUrl(target, settled); + // Deliberately NO noteRoutedOrigin here: the bridge is a + // same-process listener, not pinned-channel evidence — + // recording its loopback origin against the pseudo-origin + // would fire the #3923 split-brain alarm for nothing. + state.onDispatch?.(rewritten, "rewrite"); + return new RoutedWebSocket(rewritten, ...rest); + } + return new DeferredWebSocket( + urlStr, + rest, + async () => { + const bridge = await resolveInlineWs().catch(() => undefined as string | undefined); + if (bridge === undefined) { + if (!warnedDirect) { + warnedDirect = true; + logHostEvent("warn", "[native-client]", `bili-native(ws): inline WebSocket bridge unavailable — Codex Responses WebSocket goes DIRECT (uncompressed): ${urlStr}`); + } + state.onDispatch?.(target, "direct"); + return undefined; + } + const rewritten = rewriteCodexResponsesWsUrl(target, bridge); + state.onDispatch?.(rewritten, "rewrite"); + return rewritten; + }, + constructLive, + ); + } const origin = state.origin; if (origin !== undefined) return rewrite(origin); // Origin still settling → defer the real connection (bounded by diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index 277cdc7d4a..83281f81ee 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -60,14 +60,17 @@ export function shouldBootstrapNative(env: NodeJS.ProcessEnv): boolean { * already proxy-shaped. BILI_PI_INLINE (#3278) outranks ambient attach envs: * it is a deliberate choice about WHERE compression runs (in this process), * not just which proxy to use. Resolved env > file > default(off): the - * config-file home is `"pi": {"inline": true}`; an explicit BILI_PI_INLINE=0/ - * false disables over the file. */ + * config-file home is `"pi": {"inline": true}`; an explicit BILI_PI_INLINE + * off value (0/false/no/off) disables over the file, and any OTHER non-empty + * value enables inline (#4526 — on/yes/etc. used to fall through to spawn + * silently, reading as "inline does nothing"). */ export function planNativePi(env: NodeJS.ProcessEnv, fileInline = false): { mode: "off" | "attach" | "spawn" | "inline"; attachOrigin?: string } { if (env.BILLION_CONTEXT_PLUGIN === "0" || env.BILI_NATIVE_PI === "0") return { mode: "off" }; if (env.BILI_PROVIDER_REWRITES !== undefined) return { mode: "off" }; const inlineEnv = env.BILI_PI_INLINE?.trim().toLowerCase(); - const inlineEnvOff = inlineEnv === "0" || inlineEnv === "false"; - if ((inlineEnv === "1" || inlineEnv === "true") || (!inlineEnvOff && fileInline)) return { mode: "inline" }; + const inlineEnvOff = inlineEnv === "0" || inlineEnv === "false" || inlineEnv === "no" || inlineEnv === "off"; + const inlineEnvOn = inlineEnv !== undefined && inlineEnv.length > 0 && !inlineEnvOff; + if (inlineEnvOn || (!inlineEnvOff && fileInline)) return { mode: "inline" }; const attachOrigin = nativeAttachOrigin(env) ?? proxyEnvOrigin(env); if (attachOrigin !== undefined) return { mode: "attach", attachOrigin }; return { mode: "spawn" }; @@ -270,11 +273,24 @@ export function armNativePi(p: typeof plan): void { state.origin = INLINE_ORIGIN; process.env.BILLION_CONTEXT_PROXY = INLINE_ORIGIN; warnLegacyBcpCoResident(); + // #4526: make the switch observable — "inline 模式,不另起 proxy" in one + // line, so a user who set BILI_PI_INLINE sees whether it took effect. + logHostEvent("info", "[native-client]", "[bili-native(pi)] inline mode active — compression runs in-process, no proxy spawned"); const start = singleFlight(async () => { - await getInlineEngine(); + const engine = await getInlineEngine(); + // #4526: publish the dialable WS-bridge origin (undefined when the + // bind failed — the Codex WS channel then degrades direct). + state.wsOrigin = engine.wsOrigin; return INLINE_ORIGIN; }); state.respawn = start; + // Lazy on purpose: awaiting it triggers the memoized engine start, so a + // test-context arm stays inert until something actually resolves it. + // A bootstrap rejection propagates to the wrapper's catch → degrade. + state.resolveInlineWs = async () => { + await start(); + return state.wsOrigin; + }; setNativeOriginWaiter({ wait: () => readyOrigin(state) }); state.onGiveUp = () => { delete process.env.BILLION_CONTEXT_PROXY; @@ -311,9 +327,13 @@ if (process.env.NODE_TEST_CONTEXT === undefined && plan.mode !== "off") { // #2111: the Codex Responses WS leg shares the fetch intercept's enable // conditions and lifecycle — off mode patches nothing, repeated loads are // no-ops, and the wrapper installs before any model connection exists. - // Inline mode has no proxy WebSocket bridge to reach (and the pi lane - // does not use codex WS), so it stays unpatched there. - if (plan.mode !== "inline") installNativeWebSocketIntercept(state); + // #4526: inline mode gets it TOO — pi rides the codex provider over the + // Responses WebSocket lane, and its dialable endpoint is the engine's + // loopback WS bridge (state.resolveInlineWs/wsOrigin). The old note that + // "the pi lane does not use codex WS" was wrong (#2073/#2111 evidence), + // which left codex WS traffic bypassing compression entirely in inline + // mode; a bridge-bind failure degrades only that channel direct. + installNativeWebSocketIntercept(state); } /** Test hook: expose the armed runtime-recovery seam (mirrors opencode/dsh). */ @@ -321,6 +341,13 @@ export function _stateRespawnForTest(): (() => Promise) | un return state.respawn; } +/** Test hook (#4526): the module-level intercept state, so suites can drive + * installNativeWebSocketIntercept against exactly what armNativePi armed + * (the install block itself is skipped under NODE_TEST_CONTEXT). */ +export function _nativeStateForTest(): NativeInterceptState { + return state; +} + /** Test hook (#1365): record routed-channel evidence without the fetch patch. */ export function _noteRoutedForTest(url: string): void { noteRoutedOrigin(state, url); @@ -331,6 +358,8 @@ export function _noteRoutedForTest(url: string): void { export function _resetNativeStateForTest(): void { state.attach = undefined; state.origin = undefined; + state.wsOrigin = undefined; + state.resolveInlineWs = undefined; state.routedOrigin = undefined; state.onRoutedOriginObserved = undefined; state.respawn = undefined; diff --git a/src/inline-engine.ts b/src/inline-engine.ts index a332e65a5c..498b5f94bf 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -3,21 +3,24 @@ // inline lane is the first consumer: model and tool-management requests are // routed here through a pseudo-origin instead of a spawned proxy. Deliberately // skipped vs startServer(): auto-update/advisory (the engine updates with its -// host package), WebSocket bridge, port bind + announce, request watchdog -// (in-process callers are bounded by upstream timeouts). +// host package), full port bind + announce (only the loopback-only Codex +// Responses WebSocket bridge binds, #4526), request watchdog (in-process +// callers are bounded by upstream timeouts). import http from "node:http"; import { EventEmitter } from "node:events"; import { Buffer } from "node:buffer"; -import { ensureConfigTemplate, loadOptions } from "./config.js"; +import { ensureConfigTemplate, loadOptions, type ProxyOptions } from "./config.js"; import { ensureSqliteEngine } from "./storage/engine-bootstrap.js"; import { recordEngineBootstrap } from "./storage/runtime-status.js"; -import { resolveProxy } from "./upstream-proxy.js"; +import { resolveProxy, resolveProxyDecision } from "./upstream-proxy.js"; import { createEngineContext, type EngineContext } from "./server.js"; import { handle } from "./server/handle.js"; import { withFetchTransport } from "./fetch-transport.js"; import type { FetchOptions } from "./fetch-util.js"; import { log as loggerLog } from "./logger.js"; import { buildProxyFailureResponse, isExpectedFailFast } from "./upstream-fail.js"; +import { installWebSocketBridge } from "./ws-bridge.js"; +import { codexResponsesCodec } from "./responses-ws.js"; /** Pseudo-origin of the inline lane. Never DNS-resolved: any request reaching * this host is served in-process by installInlineTransport before egress. */ @@ -29,6 +32,11 @@ type BodyLike = string | URLSearchParams | Blob | ArrayBuffer | ArrayBufferView export interface InlineEngine { origin: string; + /** #4526: dialable loopback origin of this engine's Codex Responses + * WebSocket bridge ("http://127.0.0.1:"). undefined when the bind + * failed — the client-side wrapper then sends that channel direct + * (uncompressed, warned) instead of an undialable URL. */ + wsOrigin?: string; /** Run one request through the shared pipeline. `rawFetch` is the host's * pre-patch fetch: it becomes this engine's upstream transport (via * withFetchTransport) so pipeline egress never re-enters the patched @@ -219,7 +227,15 @@ function responseFrom(res: InlineRes): Response { return new Response(res.bodyStream, { status: res.statusCode, statusText: res.statusMessage || undefined, headers }); } -function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { +/** The bare per-request pipeline — exactly what startServer's dispatch calls + * (server.ts). Shared by the HTTP dispatcher AND the WebSocket bridge: the + * WS session wraps it with its own upstream transport via AsyncLocalStorage, + * so it must NOT be pre-wrapped here. */ +function pipelineFor(ctx: EngineContext): (req: http.IncomingMessage, res: http.ServerResponse) => Promise { + return (req, res) => handle(req, res, ctx.opts, ctx.core, ctx.config, ctx.log, ctx.instanceId, ctx.instanceStartedAt, ctx.proxyWatchers, ctx.initialWatcherPid, ctx.adminCtx); +} + +function makeDispatcher(pipeline: ReturnType, log: EngineContext["log"]): InlineEngine["dispatch"] { return async (input, init, rawFetch) => { const req = await toInlineReq(input, init); const res = new InlineRes(); @@ -251,24 +267,24 @@ function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { // it would rewrite the upstream URL back onto the pseudo-origin // and loop forever. const transport = (url: string, options: FetchOptions): Promise => (rawFetch ?? globalThis.fetch)(url, options as RequestInit); - const pipeline = withFetchTransport( + const run = withFetchTransport( transport, - () => handle(req as unknown as http.IncomingMessage, res as unknown as http.ServerResponse, ctx.opts, ctx.core, ctx.config, ctx.log, ctx.instanceId, ctx.instanceStartedAt, ctx.proxyWatchers, ctx.initialWatcherPid, ctx.adminCtx), + () => pipeline(req as unknown as http.IncomingMessage, res as unknown as http.ServerResponse), ); - if (abortPromise !== undefined) await Promise.race([pipeline, abortPromise]); - else await pipeline; + if (abortPromise !== undefined) await Promise.race([run, abortPromise]); + else await run; } catch (err) { // Mirror of startServer()'s dispatch catch (server.ts): same // client-abort classification, same error shape on the wire. const msg = String(err); const e = err as { name?: string; message?: string }; const clientAbort = (e?.name === "AbortError" || /abort/i.test(String(e?.message ?? ""))) && (res.destroyed || res.writableEnded); - if (clientAbort) ctx.log("info", `client aborted mid-stream: ${msg}`); + if (clientAbort) log("info", `client aborted mid-stream: ${msg}`); // #4254: a deliberate circuit-breaker fail-fast logs nothing here — the // throttled [warn] at the throw site is the canonical record. else if (!isExpectedFailFast(err)) { - if (err instanceof Error && err.stack) ctx.log("error", `${msg}\n${err.stack.split("\n").slice(1, 8).join("\n")}`); - else ctx.log("error", msg); + if (err instanceof Error && err.stack) log("error", `${msg}\n${err.stack.split("\n").slice(1, 8).join("\n")}`); + else log("error", msg); } // A destroyed res belongs to an aborted caller whose promise // carries the abort reason — the body stream is errored, so writing @@ -292,6 +308,95 @@ function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { }; } +interface InlineWsBridge { + /** Dialable loopback origin, e.g. "http://127.0.0.1:53210". */ + origin: string; + close(): Promise; +} + +/** #4526: the inline lane's own WebSocket bridge. The engine otherwise binds + * nothing, but the Codex Responses lane dials a REAL endpoint — so bind a + * loopback-only upgrade listener on an ephemeral port and serve the same + * per-request pipeline through installWebSocketBridge (the spawn-mode bridge + * verbatim: loopback-source admission, tunnel-destination SSRF guard, + * session lifecycle). unref'd so it never holds the host process open; a + * bind failure degrades ONLY the WS channel direct (warned) — HTTP + * compression keeps running and the engine never fails for this. */ +async function startInlineWsBridge( + pipeline: ReturnType, + opts: ProxyOptions, + log: EngineContext["log"], + instanceId: string, +): Promise { + const server = http.createServer((_req, res) => { + // This port exists for WebSocket upgrades only — model HTTP traffic is + // served in-process by the fetch chain, nothing else should land here. + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "HTTP is not served by the pi inline WebSocket bridge" })); + }); + const wsUpgrade = installWebSocketBridge( + server, + pipeline, + log, + [codexResponsesCodec], + (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined, + instanceId, + ); + // Unclaimed upgrades keep the 426 contract (server.ts parity): Codex's + // built-in fast-fallback needs a clean 426 to switch to HTTP POST, which + // still gets compressed through the in-process fetch chain. + server.on("upgrade", (req, socket, head) => { + if (wsUpgrade(req, socket, head)) return; + socket.on("error", () => {}); + const body = JSON.stringify({ error: "WebSocket upgrades are not supported; use HTTP POST" }); + socket.end( + "HTTP/1.1 426 Upgrade Required\r\n" + + "Connection: close\r\n" + + "Content-Type: application/json\r\n" + + `Content-Length: ${Buffer.byteLength(body)}\r\n` + + "\r\n" + body, + ); + }); + try { + await new Promise((resolve, reject) => { + const onError = (err: Error) => reject(err); + server.once("error", onError); + server.listen(0, "127.0.0.1", () => { + server.removeListener("error", onError); + resolve(); + }); + }); + } catch (err) { + log("warn", `[inline] WebSocket bridge bind failed (${String(err)}) — Codex Responses WebSocket traffic will go direct (uncompressed); HTTP compression unaffected (#4526)`); + try { server.closeAllConnections(); } catch { /* not listening yet */ } + return undefined; + } + server.unref(); + const addr = server.address(); + if (addr === null || typeof addr !== "object") { + log("warn", "[inline] WebSocket bridge bound without a readable address — treating as unavailable (#4526)"); + return undefined; + } + log("info", `[inline] Codex Responses WebSocket bridge listening on 127.0.0.1:${addr.port} (#4526)`); + return { + origin: `http://127.0.0.1:${addr.port}`, + close: () => new Promise((resolve) => { + try { server.closeAllConnections(); } catch { /* already closed */ } + server.close(() => resolve()); + }), + }; +} + +let liveWsBridge: InlineWsBridge | undefined; + +/** Test seam: close the real bridge a booted engine bound (unref'd listeners + * don't block process exit, but an open client/upstream socket does). */ +export async function _closeInlineWsBridgeForTest(): Promise { + const bridge = liveWsBridge; + liveWsBridge = undefined; + await bridge?.close(); +} + let engineFactory: () => Promise = startInlineEngine; /** Test seam: replace the bootstrap (fake engine) without touching config or @@ -339,6 +444,12 @@ async function startInlineEngine(): Promise { const ctx = await createEngineContext(opts); // No bind/announce step exists inline — ready the moment context is up. ctx.markBootReady(); - ctx.log("info", `[inline] compression engine running in-process (${INLINE_ORIGIN}) — no proxy spawned`); - return { origin: INLINE_ORIGIN, dispatch: makeDispatcher(ctx) }; + // #4526: the one real listener the inline lane carries — the Codex + // Responses WebSocket bridge. A bind failure degrades only that channel; + // the engine itself stays healthy (HTTP compression is unaffected). + const pipeline = pipelineFor(ctx); + const wsBridge = await startInlineWsBridge(pipeline, opts, ctx.log, ctx.instanceId); + liveWsBridge = wsBridge; + ctx.log("info", `[inline] compression engine running in-process (${INLINE_ORIGIN})${wsBridge !== undefined ? `, Codex Responses WebSocket bridge at ${wsBridge.origin}` : ""} — no proxy spawned`); + return { origin: INLINE_ORIGIN, wsOrigin: wsBridge?.origin, dispatch: makeDispatcher(pipeline, ctx.log) }; } diff --git a/tests/issue4526-pi-inline-ws.test.ts b/tests/issue4526-pi-inline-ws.test.ts new file mode 100644 index 0000000000..1dbaab6e82 --- /dev/null +++ b/tests/issue4526-pi-inline-ws.test.ts @@ -0,0 +1,269 @@ +// #4526 regression: pi inline mode (BILI_PI_INLINE) must ALSO intercept the +// Codex Responses WebSocket lane. Pre-fix, the inline branch skipped +// installNativeWebSocketIntercept entirely (on the false premise "the pi lane +// does not use codex WS"), so codex WS traffic dialed the model directly — +// uncompressed, with no ACP session record. Now the engine binds a +// loopback-only WS bridge and the patched global WebSocket routes into it. +import assert from "node:assert/strict"; +import http from "node:http"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; +import WebSocket, { WebSocketServer } from "ws"; + +// Hermetic XDG homes (store modules resolve paths lazily, but set these +// before any src import anyway) + never patch globals / spawn proxies. +const ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-issue4526-")); +const PREV_ENV: Record = {}; +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME", "NODE_TEST_CONTEXT", "BILI_PI_INLINE", "BILLION_CONTEXT_PROXY"]) { + PREV_ENV[key] = process.env[key]; + if (key.startsWith("XDG")) process.env[key] = ROOT; +} +process.env.NODE_TEST_CONTEXT = "1"; + +type Item = Record; +const user = (text: string): Item => ({ type: "message", role: "user", content: [{ type: "input_text", text }] }); + +let serial = 0; +const rows: Array<{ request: Item; headers: http.IncomingHttpHeaders }> = []; +let upstream: http.Server; +let wss: WebSocketServer; +let upstreamOrigin = ""; + +test.before(async () => { + // Mock Codex Responses-over-WS upstream: JSON frames, probe + turns. + upstream = http.createServer((_req, res) => { res.writeHead(404).end(); }); + wss = new WebSocketServer({ server: upstream }); + wss.on("connection", (peer, req) => { + peer.on("error", () => {}); + peer.on("message", (raw) => { + const request = JSON.parse(raw.toString()) as Item; + rows.push({ request, headers: req.headers }); + const id = `resp_inline_${++serial}`; + const send = (type: string, fields: Item): void => peer.send(JSON.stringify({ type, ...fields })); + if (Array.isArray(request.input) && request.input.length === 0) { + 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; + } + const item: Item = { type: "message", id: `msg_${id}`, role: "assistant", status: "completed", content: [{ type: "output_text", text: `ok-${serial}` }] }; + send("response.created", { response: { id, status: "in_progress", output: [] } }); + send("response.output_item.added", { output_index: 0, item: { ...item, status: "in_progress", content: [] } }); + send("response.output_text.delta", { item_id: item.id, output_index: 0, content_index: 0, delta: `ok-${serial}` }); + send("response.output_text.done", { item_id: item.id, output_index: 0, content_index: 0, text: `ok-${serial}` }); + send("response.output_item.done", { output_index: 0, item }); + send("response.completed", { response: { id, object: "response", status: "completed", output: [item], usage: { input_tokens: 500, output_tokens: 10, total_tokens: 510 } } }); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + upstreamOrigin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; +}); + +test.after(async () => { + for (const client of wss.clients) client.terminate(); + await Promise.all([ + new Promise((resolve) => upstream.close(() => resolve())), + ]); + wss.close(); + const { _closeInlineWsBridgeForTest, _resetInlineEngineForTest } = await import("../src/inline-engine.js"); + await _closeInlineWsBridgeForTest(); + _resetInlineEngineForTest(); + const { flushConversations } = await import("../src/plugin.js"); + const { flushAllSessions } = await import("../src/session.js"); + flushConversations(); + await flushAllSessions(); + for (const [key, prev] of Object.entries(PREV_ENV)) { + if (prev === undefined) delete process.env[key]; + else process.env[key] = prev; + } + fs.rmSync(ROOT, { recursive: true, force: true }); +}); + +const bridgeOriginOf = async (): Promise => { + const { getInlineEngine, INLINE_ORIGIN } = await import("../src/inline-engine.js"); + const engine = await getInlineEngine(); + assert.equal(engine.origin, INLINE_ORIGIN); + assert.match(engine.wsOrigin ?? "", /^http:\/\/127\.0\.0\.1:\d+$/, "the engine exposes a dialable loopback WS origin"); + return engine.wsOrigin!; +}; + +const frame = (sid: string, input: Item[], extra: Item = {}): string => JSON.stringify({ + type: "response.create", model: "gpt-5.2", instructions: "You are Codex", store: false, stream: true, + prompt_cache_key: sid, tools: [], input, client_metadata: { session_id: sid, thread_id: sid }, ...extra, +}); + +// EventTarget-only surface: satisfied by BOTH the raw ws-package client and +// the DeferredWebSocket shell the intercept hands back (the shell records +// window-phase listeners and replays them on attach; post-attach calls proxy +// straight through to the real socket). +// Listener param is `unknown` so BOTH the ws package's typed listener union +// (MessageEvent & co.) and the shell's EventListener satisfy the interface. +interface Exchangeable { + send(data: unknown): void; + addEventListener(type: string, listener: (event: unknown) => void): void; + removeEventListener(type: string, listener: (event: unknown) => void): void; +} + +async function exchange(payload: string, client: Exchangeable): Promise { + client.send(payload); + return new Promise((resolve, reject) => { + const events: Item[] = []; + let done = false; + const finish = (outcome: (() => void) | undefined, err?: Error): void => { + if (done) return; + done = true; + clearTimeout(timer); + client.removeEventListener("message", onMessage); + client.removeEventListener("close", onClose); + if (err !== undefined) reject(err); + else outcome?.(); + }; + const timer = setTimeout(() => finish(undefined, new Error("WS turn timed out")), 15000); + const onClose = (): void => { finish(undefined, new Error("WS client closed")); }; + const onMessage = (event: unknown): void => { + const parsed = JSON.parse(String((event as { data?: unknown }).data)) as Item; + events.push(parsed); + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(String(parsed.type))) { + finish(() => resolve(events)); + } + }; + client.addEventListener("message", onMessage); + client.addEventListener("close", onClose); + }); +} + +const completed = (events: Item[]): Item => { + const event = events.find((e) => e.type === "response.completed"); + assert.ok(event, JSON.stringify(events)); + return event.response as Item; +}; + +test("bridge: the inline engine binds a loopback-only upgrade listener with sane fallthroughs", { timeout: 30000 }, async () => { + const bridge = await bridgeOriginOf(); + const sid = randomUUID(); + // Plain HTTP gets 404 — this port exists for upgrades only. + const res = await fetch(`${bridge}/`); + assert.equal(res.status, 404); + // Unclaimed upgrade shapes get the 426 contract (server.ts parity): + // codex's fast-fallback switches to HTTP POST on a clean 426. + // ws-package 'unexpected-response' delivers (request, RESPONSE, head) — + // the status code rides on the second argument. + const expectStatus = (client: WebSocket): Promise => new Promise((resolve, reject) => { + client.on("error", () => {}); + client.on("unexpected-response", (_req: unknown, res: { statusCode?: number; socket?: { destroy: () => void } }) => { res.socket?.destroy(); resolve(res.statusCode ?? -1); }); + client.on("open", () => reject(new Error("unexpected upgrade success"))); + setTimeout(() => reject(new Error("timed out waiting for the refusal")), 5000); + }); + // Unclaimed upgrade shapes get the 426 contract (server.ts parity): + // codex's fast-fallback switches to HTTP POST on a clean 426. + assert.equal( + await expectStatus(new WebSocket(`${bridge.replace(/^http/, "ws")}/__acp/status`, { headers: { "session-id": sid } })), + 426, + ); + // Admission still requires a conversation id (#2111 gate). + assert.equal( + await expectStatus(new WebSocket(`${bridge.replace(/^http/, "ws")}/bili/${upstreamOrigin}/v1/responses`, {})), + 426, + ); +}); + +test("bridge: codex WS rides the IN-PROCESS pipeline — probe + turn, ACP session recorded", { timeout: 30000 }, async () => { + const bridge = await bridgeOriginOf(); + const sid = `pi-inline-${randomUUID()}`; + const client = new WebSocket(`${bridge.replace(/^http/, "ws")}/bili/${upstreamOrigin}/v1/responses`, { + headers: { authorization: "Bearer fake-credential", "session-id": sid, "thread-id": sid, "user-agent": "codex_exec/0.147.0" }, + }); + client.on("error", () => {}); + await once(client, "open"); + const baseRows = rows.length; + try { + const probeEvents = await exchange(frame(sid, [], { generate: false }), client); + assert.equal(completed(probeEvents).status, "completed"); + const turnEvents = await exchange(frame(sid, [user("pi-inline-codex-ws")]), client); + const done = completed(turnEvents); + assert.equal(done.status, "completed"); + assert.ok(JSON.stringify(done.output).includes("ok-")); + // Probe forwarded verbatim; the turn went THROUGH the pipeline — + // bili's ACP tools ride alongside codex's (empty) tool list. + assert.equal(rows.length, baseRows + 2); + assert.deepEqual(rows[baseRows].request.input, []); + assert.equal(rows[baseRows].request.generate, false); + assert.ok(Array.isArray(rows[baseRows + 1].request.tools) && (rows[baseRows + 1].request.tools as Item[]).some((t) => t.name === "acp_status" || t.name === "compress"), JSON.stringify((rows[baseRows + 1].request.tools as Item[] | undefined)?.map?.((t) => t.name))); + assert.ok(JSON.stringify(rows[baseRows + 1].request.input).includes("pi-inline-codex-ws")); + assert.equal(rows[baseRows + 1].headers["session-id"], sid); + // The issue's second complaint: inline mode left NO ACP session record. + const { peekSession } = await import("../src/session.js"); + const session = peekSession(sid); + assert.ok(session, "an ACP session exists for the bridged conversation"); + assert.equal(session.stats.requests, 1); + } finally { + client.terminate(); + } +}); + +test("client wiring: BILI_PI_INLINE=on arms the inline lane and the patched global WebSocket reaches the bridge", { timeout: 30000 }, async () => { + const bridge = await bridgeOriginOf(); + // Relaxed switch value ("on" was the silent-spawn trap, #4526) + fresh + // module import so the module-level plan sees it. + process.env.BILI_PI_INLINE = "on"; + const pi = await import("../src/agent/pi-native.js"); + const state = pi._nativeStateForTest(); + assert.equal(state.origin, "http://billion-context.inline", "'on' planned the inline lane (pre-fix it spawned a proxy)"); + assert.equal(typeof state.resolveInlineWs, "function"); + const { installNativeWebSocketIntercept, _resetForTest } = await import("../src/agent/native-ws-intercept.js"); + const saved = Object.getOwnPropertyDescriptor(globalThis, "WebSocket"); + Object.defineProperty(globalThis, "WebSocket", { value: WebSocket, writable: true, configurable: true }); + try { + installNativeWebSocketIntercept(state); + const sid = `pi-inline-client-${randomUUID()}`; + const top = (globalThis as Record).WebSocket as new (...args: unknown[]) => WebSocket; + // The claimable URL embeds the LIVE mock upstream: the bridge dials it + // as the tunnel destination, so it must be reachable. The mock answers + // frames on any path, so a degrade-direct regression would still make + // the exchanges below succeed — the /bili/ url assertion is what pins + // the routing decision. + const targetWs = upstreamOrigin.replace(/^http/, "ws") + "/backend-api/codex/responses"; + // Constructed while the engine decision is still pending → deferred + // shell; awaiting the respawn seam settles it onto the real bridge. + const ws = new top(targetWs, { + headers: { authorization: "Bearer fake-credential", "session-id": sid, "thread-id": sid }, + }); + // Swallow dial failures: a regression that degrades this connection + // direct must fail the assertions below, not crash on an unhandled 'error'. + (ws as unknown as { addEventListener: (type: string, listener: unknown) => void }).addEventListener("error", () => {}); + const respawn = pi._stateRespawnForTest(); + assert.ok(respawn !== undefined, "arm wired the runtime-recovery seam"); + await respawn(); + await new Promise((resolve) => { + const deadline = Date.now() + 10000; + const poll = setInterval(() => { + if ((ws as WebSocket).readyState === WebSocket.OPEN || Date.now() >= deadline) { + clearInterval(poll); + resolve(); + } + }, 10); + }); + assert.equal((ws as WebSocket).readyState, WebSocket.OPEN, "the shell attached to a live bridge connection"); + assert.ok((ws as WebSocket).url.includes("/bili/"), "the wrapper rewrote the dial to the loopback bridge, not the upstream"); + try { + const probeEvents = await exchange(frame(sid, [], { generate: false }), ws as WebSocket); + assert.equal(completed(probeEvents).status, "completed"); + const turnEvents = await exchange(frame(sid, [user("via-patched-global-websocket")]), ws as WebSocket); + assert.equal(completed(turnEvents).status, "completed"); + const last = rows[rows.length - 1]; + assert.ok(JSON.stringify(last.request.input).includes("via-patched-global-websocket")); + // Pipeline-proof: only traffic through the in-process engine gets + // the ACP tools injected — direct-degraded traffic never reaches here. + assert.ok(Array.isArray(last.request.tools) && (last.request.tools as Item[]).some((t) => t.name === "acp_status" || t.name === "compress"), JSON.stringify((last.request.tools as Item[] | undefined)?.map?.((t) => t.name))); + } finally { + ws.close(); + } + } finally { + _resetForTest(); + if (saved !== undefined) Object.defineProperty(globalThis, "WebSocket", saved); + } +}); diff --git a/tests/native-ws-intercept.test.ts b/tests/native-ws-intercept.test.ts index 617823c193..bf35a52fa7 100644 --- a/tests/native-ws-intercept.test.ts +++ b/tests/native-ws-intercept.test.ts @@ -470,6 +470,100 @@ test("adoption: non-constructor replacements are refused, previous constructor k }); }); +// ——— Inline lane (#4526) —————————————————————————————————————————————————————— +// In inline mode state.origin is the pseudo-origin that never listens. The +// wrapper must route exclusively through resolveInlineWs/wsOrigin — never to +// the frozen pseudo-origin — and degrade direct when the bridge is absent. + +const INLINE_ORIGIN = "http://billion-context.inline"; +const BRIDGE = "http://127.0.0.1:53210"; +const BRIDGE_REWRITTEN = `ws://127.0.0.1:53210/bili/https://chatgpt.com/backend-api/codex/responses`; + +function inlineState(extra?: Partial): NativeInterceptState { + return { + origin: INLINE_ORIGIN, + ready: Promise.resolve(INLINE_ORIGIN), + ...extra, + }; +} + +test("#4526 inline: settled bridge origin → immediate rewrite to the loopback bridge", async () => { + await withPatchedGlobal(async () => { + const dispatched: Array<[string, string]> = []; + const state = inlineState({ + wsOrigin: BRIDGE, + resolveInlineWs: async () => BRIDGE, + onDispatch: (url, action) => dispatched.push([url, action]), + }); + installNativeWebSocketIntercept(state); + const init = { headers: { "session-id": "sess-inline" } }; + const ws = new (WsCtor())(UPSTREAM_WS, init); + assert.ok(ws instanceof FakeWs, "decision already made — no shell"); + assert.equal(ws.url, BRIDGE_REWRITTEN); + assert.equal(ws.ctorArgs[1], init); + assert.deepEqual(dispatched, [[BRIDGE_REWRITTEN, "rewrite"]]); + // The bridge is a same-process listener, not pinned-channel evidence — + // recording it would false-alarm the #3923 split-brain check. + assert.equal(state.routedOrigin, undefined); + }); +}); + +test("#4526 inline: engine still booting → shell holds until the decision settles, then routes", async () => { + await withPatchedGlobal(async () => { + let resolveDecision!: (o: string | undefined) => void; + const decision = new Promise((resolve) => { resolveDecision = resolve; }); + const state = inlineState({ resolveInlineWs: () => decision }); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => { url: string }; + const ws = new top(UPSTREAM_WS, {}); + assert.ok(!(ws instanceof FakeWs), "shell stands in while the engine boots"); + // readyOrigin short-circuits on the frozen pseudo-origin — nothing may + // attach there: pre-#4526 this is where the undialable URL was built. + await tick(); + await tick(); + assert.equal(FakeWs.instances.length, 0); + resolveDecision(BRIDGE); + await tick(); + await tick(); + const [real] = FakeWs.instances; + assert.equal(real.url, BRIDGE_REWRITTEN); + }); +}); + +test("#4526 inline: bridge bind failed → channel degrades DIRECT, not an undialable URL", async () => { + await withPatchedGlobal(async () => { + const dispatched: Array<[string, string]> = []; + const state = inlineState({ + resolveInlineWs: async () => undefined, + onDispatch: (url, action) => dispatched.push([url, action]), + }); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => FakeWs; + const ws = new top(UPSTREAM_WS, {}); + await tick(); + await tick(); + const [real] = FakeWs.instances; + assert.equal(real.url, UPSTREAM_WS, "direct connection keeps codex working uncompressed"); + assert.equal(ws.url, UPSTREAM_WS); + assert.deepEqual(dispatched, [["https://chatgpt.com/backend-api/codex/responses", "direct"]]); + }); +}); + +test("#4526 inline: bootstrap rejection degrades direct without an unhandled rejection", async () => { + await withPatchedGlobal(async () => { + const state = inlineState({ + resolveInlineWs: () => Promise.reject(new Error("engine bootstrap failed")), + }); + installNativeWebSocketIntercept(state); + const top = (globalThis as GlobalRecord).WebSocket as unknown as new (...args: unknown[]) => FakeWs; + const ws = new top(UPSTREAM_WS, {}); + await tick(); + await tick(); + const [real] = FakeWs.instances; + assert.equal(real.url, UPSTREAM_WS); + }); +}); + // ——— Reset ————————————————————————————————————————————————————————————————————— test("_resetForTest restores the pre-install global and clears the flag", async () => { diff --git a/tests/pi-native.test.ts b/tests/pi-native.test.ts index 9ef5a2b285..a3d9a5b865 100644 --- a/tests/pi-native.test.ts +++ b/tests/pi-native.test.ts @@ -209,6 +209,27 @@ test("#3278: pi.inline (config file) enables the lane; env resolves over it", () assert.deepEqual(planNativePi({ BILI_PROVIDER_REWRITES: "{}" }, true), { mode: "off" }); }); +test("#4526: BILI_PI_INLINE accepts any non-empty non-off value; no/off disable over the file", () => { + // common on-spellings that used to silently fall through to spawn (the + // "inline does nothing, a server still starts" reports) + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "on" }), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "yes" }), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "ON" }), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: " True " }), { mode: "inline" }); + // explicit off tokens — no/off included, which were unrecognized before + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "no" }), { mode: "spawn" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "off" }), { mode: "spawn" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "OFF" }), { mode: "spawn" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "false" }), { mode: "spawn" }); + // no/off now beat the config-file opt-in (previously only 0/false did) + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "no" }, true), { mode: "spawn" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "off" }, true), { mode: "spawn" }); + // blank values stay neutral — the config file decides + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "" }, true), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: " " }, true), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "" }), { mode: "spawn" }); +}); + test("#3278: armNativePi(inline) freezes the pseudo-origin synchronously without spawning", () => { const savedProxy = process.env.BILLION_CONTEXT_PROXY; try { diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index 69e2d89682..9db5b8b169 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -360,8 +360,8 @@ entries: kind: env-only audience: internal group: envonly - desc_en: "Host posture: pi inline lane (#3278) — the pi extension compresses IN-PROCESS through the pseudo-origin instead of spawning its own proxy; outranks a preset BILLION_CONTEXT_PROXY attach target." - desc_zh: "宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。" + desc_en: "Host posture: pi inline lane (#3278) — the pi extension compresses IN-PROCESS through the pseudo-origin instead of spawning its own proxy; outranks a preset BILLION_CONTEXT_PROXY attach target. Value table (#4526): any non-empty value except 0/false/no/off enables (previously only 1/true — on/yes silently fell back to spawn); the four off-tokens disable even over config-file pi.inline." + desc_zh: "宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。取值表(#4526):除 0/false/no/off 外任何非空值均开启(此前仅认 1/true——on/yes 会静默回落 spawn);四个关闭取值即使配置文件 pi.inline 为 true 也强制关闭。" - key: BILI_NATIVE_ZCODE type: boolean-ish default: "off" diff --git a/website/config-reference/server.yaml b/website/config-reference/server.yaml index 114ff39d5b..9fab61c43f 100644 --- a/website/config-reference/server.yaml +++ b/website/config-reference/server.yaml @@ -483,8 +483,8 @@ entries: default: "false" env: BILI_PI_INLINE group: behavior - desc_en: "Pi inline lane (#3278): run the pi extension's compression IN-PROCESS — no spawned proxy, no ephemeral port; model traffic and the /__bili//__acp tool surface are served by an in-process engine through a pseudo-origin. Resolved env > file > default(off): BILI_PI_INLINE=1/true enables, BILI_PI_INLINE=0/false explicitly disables over this file value." - desc_zh: "pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务。解析顺序 env > 文件 > 默认(off):BILI_PI_INLINE=1/true 开启,BILI_PI_INLINE=0/false 显式关闭且优先于本文件值。" + desc_en: "Pi inline lane (#3278): run the pi extension's compression IN-PROCESS — no spawned proxy; model traffic and the /__bili//__acp tool surface are served by an in-process engine through a pseudo-origin, and Codex Responses WebSocket traffic by a loopback-only upgrade bridge inside the engine (#4526; a bind failure degrades only that channel direct, warned). Resolved env > file > default(off): BILI_PI_INLINE accepts ANY non-empty value except 0/false/no/off as on (previously only 1/true — on/yes silently spawned); the four off-tokens explicitly disable over this file value." + desc_zh: "pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务,Codex Responses WebSocket 流量由引擎内的回环升级桥接服务(#4526;绑定失败仅该车道回退直连并告警)。解析顺序 env > 文件 > 默认(off):BILI_PI_INLINE 除 0/false/no/off 外任何非空值均视为开(此前仅认 1/true——on/yes 会静默回落到 spawn);四个关闭取值显式优先于本文件值。" - key: mitm.enabled type: boolean default: "true" From 5aa58330738210e24548ea1178f28fa1eb1a5621 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 22:47:28 +0800 Subject: [PATCH 02/14] docs: correct stale BILI_PI_INLINE off-token comment in config.ts (#4559 small fix) #4526 widened the explicit-off tokens from {0,false} to {0,false,no/off}; the pi.inline doc comment in FileConfig still listed only 0/false. Comment-only, matches planNativePi (src/agent/pi-native.ts). --- src/config.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index 077ec0286b..a242d636e2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1900,8 +1900,8 @@ type FileConfig = { * deprecated fallback, read only while this section is absent. * `inline` (#3278): run the pi lane's compression IN-PROCESS (no spawned * proxy) — the config-file home of the BILI_PI_INLINE knob, resolved - * env > file > default(off); BILI_PI_INLINE=0/false explicitly disables - * it over the file. */ + * env > file > default(off); BILI_PI_INLINE=0/false/no/off explicitly + * disables it over the file (#4526). */ pi?: { subagents?: PiSubagentsFileConfig | boolean; inline?: boolean }; /** Feature rollout (canary) block (#3062, resolved by src/rollout.ts): * per-feature overrides of the version's baked-in rollout plan From 0cea4801c2646a342d09de512ebb4efb599915e3 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 22:53:04 +0800 Subject: [PATCH 03/14] chore(ci): drop accidentally committed scratch file .tmpfailids.txt (#4559 small fix) Check-run id list left behind while debugging the CI failures; not part of the change. --- .tmpfailids.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .tmpfailids.txt diff --git a/.tmpfailids.txt b/.tmpfailids.txt deleted file mode 100644 index cbf5e0f5f1..0000000000 --- a/.tmpfailids.txt +++ /dev/null @@ -1 +0,0 @@ -120896365209 120896364720 \ No newline at end of file From dc7d8885ebde34dc62dc393ed72bc57db39599c3 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 23:09:25 +0800 Subject: [PATCH 04/14] fix(pi): render WS bridge dispatch failures into the envelope (#4526) Review finding on the new inline WS bridge: it passed the bare pipeline to installWebSocketBridge, so if handle() rejected (last-resort internal error), the session's own catch swallowed it (warn + bare end) and Codex saw a truncated stream instead of an error frame. Spawn mode renders the failure into the same envelope via its dispatch catch (informative error, 503+Retry-After on a circuit-open fail-fast, #4254) before resolving. Wrap the bridge's dispatch with the identical egress so both lanes report the same failure shape. --- src/inline-engine.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/inline-engine.ts b/src/inline-engine.ts index 498b5f94bf..01b949b582 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -334,9 +334,35 @@ async function startInlineWsBridge( res.writeHead(404, { "content-type": "application/json" }); res.end(JSON.stringify({ error: "HTTP is not served by the pi inline WebSocket bridge" })); }); + // The WS session swallows a rejecting dispatch (warn + bare end), which + // would leave Codex staring at a truncated stream. Render the failure + // into the envelope instead — same egress as the spawn-mode dispatch + // catch (server.ts) and makeDispatcher above: informative error, 503+ + // Retry-After on a circuit-open fail-fast (#4254). + const guarded: ReturnType = async (req, res) => { + try { + await pipeline(req, res); + } catch (err) { + const msg = String(err); + const e = err as { name?: string; message?: string }; + const clientAbort = (e?.name === "AbortError" || /abort/i.test(String(e?.message ?? ""))) && (res.destroyed || res.writableEnded); + if (clientAbort) log("info", `client aborted mid-stream: ${msg}`); + else if (!isExpectedFailFast(err)) { + if (err instanceof Error && err.stack) log("error", `${msg}\n${err.stack.split("\n").slice(1, 8).join("\n")}`); + else log("error", msg); + } + if (!res.headersSent) { + const { status, headers, body } = buildProxyFailureResponse(msg, err); + res.writeHead(status, headers); + res.end(body); + } else { + res.end(); + } + } + }; const wsUpgrade = installWebSocketBridge( server, - pipeline, + guarded, log, [codexResponsesCodec], (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined, From e82ccd7d6f3ed2442064ea5ae6bce0914fcbb9a2 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sat, 24 Oct 2026 23:41:50 +0800 Subject: [PATCH 05/14] test: pass generationBoundaryActive correctly in #4305 injectSystem test (born-red positional drift) (#4559 small fix) --- tests/generation-boundary.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generation-boundary.test.ts b/tests/generation-boundary.test.ts index 48afbb44a1..e4408e18d5 100644 --- a/tests/generation-boundary.test.ts +++ b/tests/generation-boundary.test.ts @@ -126,7 +126,7 @@ test("#4305: injectSystem appends the note only when the boundary is active", () const parsed = { system: "You are a helpful assistant.", messages: [] }; const off = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, false) as string; assert.ok(!off.includes(GENERATION_BOUNDARY_NOTE), "inactive: system block unchanged by this feature"); - const on = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, true) as string; + const on = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, false, true) as string; assert.ok(on.includes(GENERATION_BOUNDARY_NOTE), "active: note rides the ACP standing block"); assert.ok(on.startsWith("You are a helpful assistant."), "client system block stays byte-exact (appended, never merged)"); // sysParts order contract: client head, standing prompt (attribution outermost), THEN the note. From 804252314c274b3650bd2ecf435cf7eb5714bdd2 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 01:39:46 +0800 Subject: [PATCH 06/14] fix: drop internal-only ClientBuildCheck export flagged by unused-exports (#4559 small fix) The #4545 train added this type used only inside client-build-note.ts; master's 4d34abfaf de-export sweep missed it, so every PR rebased onto the tip inherits the red. Behavior-neutral (type only, zero external usages). --- src/agent/client-build-note.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/client-build-note.ts b/src/agent/client-build-note.ts index 8ca7e9ddaf..6c08149309 100644 --- a/src/agent/client-build-note.ts +++ b/src/agent/client-build-note.ts @@ -29,7 +29,7 @@ import path from "node:path"; import { isVersionNewer } from "../update.js"; import { logHostEvent } from "./host-log.js"; -export type ClientBuildCheck = { +type ClientBuildCheck = { /** Short human label used in the note text ("dsh", "pi", ...). */ label: string; /** The client's npm id, as shown in the note (@deepseek-ai/dsh, ...). */ From 6f4eb16351caa6c4904a5e4601a1d0c24e53be7f Mon Sep 17 00:00:00 2001 From: Zhishulo <963336185@qq.com> Date: Sun, 25 Oct 2026 01:34:00 +0800 Subject: [PATCH 07/14] fix(types): keep ClientBuildCheck module-private to clear the unused-exports gate (#4602) --- src/agent/client-build-note.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/client-build-note.ts b/src/agent/client-build-note.ts index 8ca7e9ddaf..6c08149309 100644 --- a/src/agent/client-build-note.ts +++ b/src/agent/client-build-note.ts @@ -29,7 +29,7 @@ import path from "node:path"; import { isVersionNewer } from "../update.js"; import { logHostEvent } from "./host-log.js"; -export type ClientBuildCheck = { +type ClientBuildCheck = { /** Short human label used in the note text ("dsh", "pi", ...). */ label: string; /** The client's npm id, as shown in the note (@deepseek-ai/dsh, ...). */ From 79eedfd83936c2696864057a5f79ba2d67cbd514 Mon Sep 17 00:00:00 2001 From: Zhishulo <963336185@qq.com> Date: Sun, 25 Oct 2026 01:34:00 +0800 Subject: [PATCH 08/14] fix(ci): register the #3890 drain suite in LIVE_FILES (#4604) --- scripts/live-files.mjs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 41cb876080..c7b3df0ee3 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -61,6 +61,9 @@ export const LIVE_FILES = [ // #4434 (split-brain surface): spawns bili CLI children against a throwaway // origin; own probe server is listen(0) so only the zone band matters "tests/issue-split-brain-surface.test.ts", + // #3890: spawns the drain fixture; that fixture is listen(0) and reports + // READY , so no zone band is needed — the entry is for the tripwire + "tests/issue3890-parent-gone-confirm-drain.test.ts", ]; /** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry; From 701f149a30a0747286767135daa2f95460637ac9 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 01:58:35 +0800 Subject: [PATCH 09/14] test: pass generationBoundaryActive correctly in #4305 injectSystem test (master born-red, #4599 sentinel) The tier-5 train added generationBoundaryActive as injectSystem's 9th parameter and updated every caller except this test's 'active' case: its trailing true landed in mcpDialectNote, so the note never appeared and the assertion must fail. Identical one-line fix already committed on PR #4559's branch (e82ccd7d6); drop that commit when rebasing whichever way merges first. --- tests/generation-boundary.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/generation-boundary.test.ts b/tests/generation-boundary.test.ts index 48afbb44a1..e4408e18d5 100644 --- a/tests/generation-boundary.test.ts +++ b/tests/generation-boundary.test.ts @@ -126,7 +126,7 @@ test("#4305: injectSystem appends the note only when the boundary is active", () const parsed = { system: "You are a helpful assistant.", messages: [] }; const off = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, false) as string; assert.ok(!off.includes(GENERATION_BOUNDARY_NOTE), "inactive: system block unchanged by this feature"); - const on = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, true) as string; + const on = injectSystem({ ...parsed }, OPTS, undefined, defaultConfig(400_000), undefined, true, false, false, true) as string; assert.ok(on.includes(GENERATION_BOUNDARY_NOTE), "active: note rides the ACP standing block"); assert.ok(on.startsWith("You are a helpful assistant."), "client system block stays byte-exact (appended, never merged)"); // sysParts order contract: client head, standing prompt (attribution outermost), THEN the note. From 945f993f0fb1cfb861e855a1ede9eb291e944f75 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 01:58:35 +0800 Subject: [PATCH 10/14] fix(ci): suspend pages deploy push trigger while the repo has no Pages site (#4598, #4599 sentinel) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pages.yml has failed 75/75 runs since landing: actions/configure-pages@v5 gets 'Get Pages site failed ... Not Found' because GitHub Pages was never enabled on this repo (the earlier enablement:true attempt was reverted without a recorded reason — not re-applied here). Stop-the-bleed per #4598 option 1: drop the push trigger, keep manual workflow_dispatch so the deploy path stays intact and no website push red-lines master again. Final direction (re-enable Pages vs retire the workflow vs in-install serving) awaits owner decision. --- .github/workflows/pages.yml | 8 +++----- website/README.md | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 9376f56812..89fe4b1c79 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -1,11 +1,9 @@ name: Deploy website to GitHub Pages on: - push: - branches: [master] - paths: - - "website/**" - - ".github/workflows/pages.yml" + # Push trigger suspended (#4598): the repo has no GitHub Pages site, so every + # website push red-lined the deploy job. Manual dispatch stays until the + # publishing direction is decided. workflow_dispatch: permissions: diff --git a/website/README.md b/website/README.md index d66681d3b4..3a50cac46e 100644 --- a/website/README.md +++ b/website/README.md @@ -2,7 +2,8 @@ Static landing page + docs. No build step, no dependencies — plain HTML/CSS/JS. Deployed to [GitHub Pages](https://ranxianglei.github.io/billion-context/) by -`.github/workflows/pages.yml` (publishes this folder on every push to master). +`.github/workflows/pages.yml` — manual `workflow_dispatch` only for now: the repo +has no GitHub Pages site yet, so auto-deploy on push is suspended (#4598). ## Local preview From 78046a55de80a777e67695a69b55817e0c1b5086 Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 09:58:19 +0800 Subject: [PATCH 11/14] fix(web): restore the #/updates topbar anchor dropped by the #3536 line-rewrite (#4599 sentinel) The #3719 Updates tab stayed routed (PAGES/route/loadUpdates in client.ts) but its topbar anchor was dropped when #3536 rewrote the whole topbar line, making nav.updates a dead catalog key (web-i18n born-red on every PR head). Restored at the original position (after logs); identical to the sibling fix on the panel-blog branch (900c09c67), adapted to master (no blog anchor yet). --- src/web/page.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/page.ts b/src/web/page.ts index 6df33d1844..881b0c77ca 100644 --- a/src/web/page.ts +++ b/src/web/page.ts @@ -23,7 +23,7 @@ export function renderPage(origin: string, version: string, embed = false, theme // that request used to miss every admin route and get relayed onto the // configured upstream (a wasted request + 403 noise in the log). return `billion-context -
billion-contextv${version}
+
billion-contextv${version}
From e59e0206028f8bf47698c4f1ebcd2a3431df93af Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 09:58:19 +0800 Subject: [PATCH 12/14] fix(log-mask): mask every home-root spelling at the publish boundary (#4427 Windows red, #4599 sentinel) maskPathsForPublish keyed off os.homedir() alone. On Windows that reads %USERPROFILE% while git-bash-style tool output carries $HOME paths, so the publish-boundary identity leak masked one spelling and leaked the other (#4427 suite red on windows shards only). Now masks the deduped set of os.homedir() + $HOME + %USERPROFILE%; POSIX behavior unchanged (values dedupe to one root), anchored-flank semantics preserved. --- src/log-mask.ts | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/log-mask.ts b/src/log-mask.ts index 83e8e89c08..6e41c3a6e6 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -156,17 +156,32 @@ function escapeRegExp(s: string): string { * publish-boundary text, rewriting it to `~` (the shell universal for $HOME). * This is the identity leak #3959 proved in the wild — a public diagnostics * report naming /home//... hands a stranger the reporter's username and - * home layout. Only THIS machine's os.homedir() is rewritten, as an ANCHORED - * match on BOTH flanks: preceded by start-of-string or a non-token boundary - * (whitespace / quote / bracket / colon / equals) AND followed by a path - * separator (/ or \), a quote, whitespace or end-of-string — so sibling homes - * (/home/), URL hosts and URL pathnames are all left alone. The result - * stays actionable: ~/.cache/billion-context/... is exactly where the user - * looks to recover. Idempotent (~ has no homedir in it). */ + * home layout. Every spelling of THIS machine's home root is rewritten — + * os.homedir() plus the $HOME and %USERPROFILE% env values (on Windows they + * diverge: os.homedir() reads USERPROFILE while git-bash-style tool output + * carries $HOME paths, so masking only one leaks the other; POSIX values + * dedupe to one root) — each as an ANCHORED match on BOTH flanks: preceded by + * start-of-string or a non-token boundary (whitespace / quote / bracket / + * colon / equals) AND followed by a path separator (/ or \), a quote, + * whitespace or end-of-string — so sibling homes (/home/), URL hosts + * and URL pathnames are all left alone. The result stays actionable: + * ~/.cache/billion-context/... is exactly where the user looks to recover. + * Idempotent (~ has no homedir in it). */ +function publishHomeRoots(): string[] { + const roots = new Set(); + for (const raw of [os.homedir(), process.env.HOME, process.env.USERPROFILE]) { + if (typeof raw !== "string") continue; + const r = raw.replace(/[/\\]+$/, ""); + if (r) roots.add(r); + } + return [...roots].sort((a, b) => b.length - a.length); +} + export function maskPathsForPublish(text: string): string { - const home = os.homedir(); - if (!text || !home) return text; - const re = new RegExp("(? Date: Sun, 25 Oct 2026 09:58:19 +0800 Subject: [PATCH 13/14] test(golden): regenerate storage-render goldens for the #4316 layer-3 restamp flag (#4599 sentinel) Deliberate persisted-state change: 380ef4dac (#4316 layer 3 PR3/3) added the one-time refLedgerAligned boolean to session state. All four wire digests are UNCHANGED; only the metadata section digest moved (+refLedgerAligned state key). Regen per the suite's own protocol (justification recorded here and in the ci-fixes PR body). --- tests/golden/storage-render/anthropic.json | 5 +++-- tests/golden/storage-render/chat.json | 5 +++-- tests/golden/storage-render/google.json | 5 +++-- tests/golden/storage-render/responses.json | 5 +++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 627cad7a26..5afa14b2c4 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": "2afbea1dbbc9381f8ec7b49a4fa6b5d55b4084ff20304f20a1f51cb120b9a49f", + "stateDigest": "6727f42bd91879a7aa370e1ce0c78963bcba0df92e50390d4a8011f8f0da334d", "sectionDigests": { "state": "f93dc1c876c672d4", "blockContents": "dc7d13d250c94bb4", "stats": "c6f1d434975d047d", - "metadata": "9c05fef8fb761941" + "metadata": "f1ec4aab3aff777a" }, "stateKeyDigests": { "blocks": "3a454b9a16e4f0b9", @@ -98,6 +98,7 @@ "lastPassIds": "f13452893a1b4980", "hiddenOrphanRefs": "4f53cda18c2baa0c", "deadRefs": "74234e98afe7498f", + "refLedgerAligned": "74234e98afe7498f", "terminalStreak": "5feceb66ffc86f38", "nextRuleId": "6b86b273ff34fce1", "absorbed": "4f53cda18c2baa0c", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index c3686433ba..46764f97db 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": "a4abd4cb1cc9ade667c277fb1095641d6135cae0ac2d6ee2905920365f4f56b4", + "stateDigest": "e149c301f382438ee063a38f70dd794207b88e94bdf3c14c5191da0eeaa99bc9", "sectionDigests": { "state": "8390f21336885863", "blockContents": "94a58f96d7c5cfc3", "stats": "24d705b0e39bd13c", - "metadata": "68d1e5ca212cef7b" + "metadata": "55733fb81ccf7d69" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", @@ -98,6 +98,7 @@ "lastPassIds": "d18ace2914339891", "hiddenOrphanRefs": "4f53cda18c2baa0c", "deadRefs": "74234e98afe7498f", + "refLedgerAligned": "74234e98afe7498f", "terminalStreak": "5feceb66ffc86f38", "nextRuleId": "6b86b273ff34fce1", "absorbed": "4f53cda18c2baa0c", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index 145d5c3f6e..ba999cadec 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": "9ad5cff9d94aa21024b20aa870e94cd0a62c9dedc452ad7e76d8821a3635f30a", + "stateDigest": "80e83935e5fa7b052a3cb0f1d641bd81cb6ee59f91f3c1f2f31c204de29368a5", "sectionDigests": { "state": "2e1def3a3234afdc", "blockContents": "473264d9df8ba338", "stats": "900481685c848462", - "metadata": "2f0093fdaa6fb5a7" + "metadata": "88cd02a467b4457e" }, "stateKeyDigests": { "blocks": "0cfd0b9c0fc53073", @@ -98,6 +98,7 @@ "lastPassIds": "1e3edaf045fbb7d1", "hiddenOrphanRefs": "4f53cda18c2baa0c", "deadRefs": "74234e98afe7498f", + "refLedgerAligned": "74234e98afe7498f", "terminalStreak": "5feceb66ffc86f38", "nextRuleId": "6b86b273ff34fce1", "absorbed": "4f53cda18c2baa0c", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 2070742004..7004d90435 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": "f1c5b00ba772868d48c8addcb150a54908eb308f9bd3a4fd46d7a79084effdbc", + "stateDigest": "08526d3552fca7ac7552cd16a221893a652fbe740c9b8dccac6db57b3d6302ee", "sectionDigests": { "state": "db313e8eba2d65fc", "blockContents": "b6701268a0bdcd8a", "stats": "327d3967b8371aa8", - "metadata": "e4cac45276f335cc" + "metadata": "b5c39d84db67f424" }, "stateKeyDigests": { "blocks": "34b0dc17f8c24595", @@ -98,6 +98,7 @@ "lastPassIds": "de5c5da67428b09f", "hiddenOrphanRefs": "4f53cda18c2baa0c", "deadRefs": "74234e98afe7498f", + "refLedgerAligned": "74234e98afe7498f", "terminalStreak": "5feceb66ffc86f38", "nextRuleId": "6b86b273ff34fce1", "absorbed": "4f53cda18c2baa0c", From 177bbce322bf4af7144fe725c432dbfd6a9e129b Mon Sep 17 00:00:00 2001 From: ci-check Date: Sun, 25 Oct 2026 09:58:19 +0800 Subject: [PATCH 14/14] test(snapshot-render): bring stale test doubles up to the #D5-5 windowed-load interface (#4599 sentinel) #D5-5 changed buildView to call store.unifiedMessageIdRows() + store.unifiedUncoveredRows(id, intervals) first; the pre-existing fake stores (only unifiedTailSeqs/unifiedTailViewRows) threw TypeError, caught by the by-design degrade net as transcript-error, turning every render-expecting assertion red (chain 6/6, verif replay). Extended both real-JS fakes and the symbolic harness portImpls (__method_unifiedMessageIdRows / __method_unifiedUncoveredRows) to mirror SessionStore semantics. --- tests/snapshot-render-chain.test.ts | 5 ++++- tests/verif-snapshot-render.test.ts | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/snapshot-render-chain.test.ts b/tests/snapshot-render-chain.test.ts index 637c247a46..0d8f60b9f3 100644 --- a/tests/snapshot-render-chain.test.ts +++ b/tests/snapshot-render-chain.test.ts @@ -47,10 +47,13 @@ let nextSessionId = 0; /** Install a fabricated unified-store tail view and render `client` through it. */ function render(blocks: BlockLike[], client: BiliMessage[], rows: BiliMessage[]): { session: Session; view: BiliMessage[] } { - const rawRows = rows.map((m, i) => ({ seq: i + 1, content: JSON.stringify(m) })); + const rawRows = rows.map((m, i) => ({ seq: i + 1, msgId: m.id, content: JSON.stringify(m) })); + const inWindow = (seq: number, intervals: readonly [number, number][]) => intervals.some(([a, b]) => seq >= a && seq <= b); const fake = { unifiedTailSeqs: () => rawRows.map((r) => r.seq), unifiedTailViewRows: () => rawRows, + unifiedMessageIdRows: () => rawRows.map(({ seq, msgId }) => ({ seq, msgId })), + unifiedUncoveredRows: (_id: string, intervals: readonly [number, number][]) => rawRows.filter((r) => !inWindow(r.seq, intervals)), }; _setStoreForTest(fake as unknown as SessionStore); nextSessionId++; diff --git a/tests/verif-snapshot-render.test.ts b/tests/verif-snapshot-render.test.ts index 05a0b7a4df..78ab2a200a 100644 --- a/tests/verif-snapshot-render.test.ts +++ b/tests/verif-snapshot-render.test.ts @@ -75,6 +75,7 @@ test("bug3756 judge: buildView keeps the client tail and the summary anchor (sym const row = (seq: number, id: Term, text: Term): SymVal => objv([ ["seq", numv(seq)], + ["msgId", strv(id)], ["content", strv(S0.concat(id).concat(S1).concat(ROLE).concat(S2).concat(CT).concat(S3).concat(text).concat(S4))], ]); const rows = arrv([row(1, r1, t1), row(2, r2, t2), row(3, r3, t3)]); @@ -107,6 +108,12 @@ test("bug3756 judge: buildView keeps the client tail and the summary anchor (sym getStore: (): SymVal => objv([]), __method_unifiedTailViewRows: (): SymVal => rows, __method_unifiedTailSeqs: (): SymVal => arrv([numv(1), numv(2), numv(3)]), + __method_unifiedMessageIdRows: (): SymVal => arrv([ + objv([["seq", numv(1)], ["msgId", strv(r1)]]), + objv([["seq", numv(2)], ["msgId", strv(r2)]]), + objv([["seq", numv(3)], ["msgId", strv(r3)]]), + ]), + __method_unifiedUncoveredRows: (): SymVal => rows, }, }, }).exec(); @@ -189,8 +196,16 @@ test("bug3756 replay: maybeSnapshotView end-to-end on real JS (success / #3812 l content: JSON.stringify({ id, role: "user", contentType: "text", text: `body-${id}` }), contentHash: `h${i}`, tokens: 3, })); - const fakeStore = (rows: unknown[] | null): SessionStore => - ({ unifiedTailViewRows: () => rows, unifiedTailSeqs: () => (rows ? rows.map((_, i) => i + 1) : null) }) as unknown as SessionStore; + const fakeStore = (rows: unknown[] | null): SessionStore => { + const list = rows as { seq: number; msgId: string }[] | null; + return ({ + unifiedTailViewRows: () => rows, + unifiedTailSeqs: () => (rows ? rows.map((_, i) => i + 1) : null), + unifiedMessageIdRows: () => (list ? list.map(({ seq, msgId }) => ({ seq, msgId })) : null), + unifiedUncoveredRows: (_id: string, intervals: readonly [number, number][]) => + (list ? list.filter((r) => !intervals.some(([a, b]) => r.seq >= a && r.seq <= b)) : null), + }) as unknown as SessionStore; + }; const mkSession = (blockIds: string[]): { session: never; blocks: never } => { const session = { id: "s1",