From 7371b1ee97ce4969391669d8b28cd3bc2898db0b Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 19:08:25 +0800 Subject: [PATCH 01/11] fix(agent): route the codex Responses WS lane through a loopback bridge in pi inline mode (#3278 follow-up) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inline mode froze state.origin to the never-dialable INLINE_ORIGIN and skipped installNativeWebSocketIntercept entirely, so codex WS traffic went DIRECT and bypassed compression with no ACP session. The inline engine now boots the same ws-bridge the spawned server serves, bound to 127.0.0.1:0 (loopback-only admission, tunnel guard, unref'd), the WS wrapper prefers state.wsOrigin over the pseudo-origin, and a failed or wedged bridge degrades that lane DIRECT (warned, 20s bound) while HTTP compression keeps running in-process. planNativePi now treats any non-empty BILI_PI_INLINE except 0/false/no/off as on: on/yes/TRUE used to miss the 1/true set and silently spawn a proxy server — the 'inline does not work' report. --- CONFIGURATION.md | 4 +- src/agent/native-intercept.ts | 7 + src/agent/native-ws-intercept.ts | 33 +++- src/agent/pi-native.ts | 23 ++- src/inline-engine.ts | 63 +++++++- src/inline-ws.ts | 82 ++++++++++ tests/pi-inline-ws.test.ts | 258 +++++++++++++++++++++++++++++++ tests/pi-inline.test.ts | 2 + 8 files changed, 457 insertions(+), 15 deletions(-) create mode 100644 src/inline-ws.ts create mode 100644 tests/pi-inline-ws.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 471913adc4..2dc97c8008 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, no ephemeral port; model traffic and the /__bili//__acp tool surface are served by an in-process engine through a pseudo-origin. The codex Responses WebSocket lane is served by a loopback-only WS bridge booted with the engine. Resolved env > file > default(off): any non-empty value except 0/false/no/off enables (BILI_PI_INLINE=1/true/on/yes), an explicit off value disables 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. Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable; a miss used to silently fall back to spawning a proxy). Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process. | | `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/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index 3f22eaf97a..409505b639 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -42,6 +42,13 @@ export interface NativeInterceptState { /** Proxy origin ("http://127.0.0.1:PORT") once the bootstrap resolved. * Written by the owner (pi-native.ts); read synchronously on each call. */ origin: string | undefined; + /** Origin the WS wrapper should rewrite codex Responses URLs to, when it + * differs from `origin`. Inline mode (#3278 follow-up): `origin` is the + * never-dialable pseudo-origin, so the WS lane needs the loopback bridge + * (inline-ws.ts) instead. Holds the bridge origin, a Promise settling to + * it (bridge still booting; construction defers, bounded, then degrades + * DIRECT if it settles undefined), or undefined (use `origin`). */ + wsOrigin?: string | Promise; /** Resolves to the proxy origin once healthy, or undefined on failure. */ ready: Promise; /** Owner hook: re-run the bootstrap (proxy died → respawn). */ diff --git a/src/agent/native-ws-intercept.ts b/src/agent/native-ws-intercept.ts index 4e4e43aeca..53b4ae01d4 100644 --- a/src/agent/native-ws-intercept.ts +++ b/src/agent/native-ws-intercept.ts @@ -110,6 +110,25 @@ export function codexResponsesWsTarget(url: string | URL): string | undefined { return httpUrl; } +/** Bound a pending inline wsOrigin like readyOrigin bounds a bootstrap: a + * wedged bridge boot settles undefined (→ DIRECT degrade) instead of + * pinning a DeferredWebSocket in CONNECTING forever. */ +function raceWsOriginBound(pending: Promise): Promise { + return new Promise(resolve => { + const timer = setTimeout(() => resolve(undefined), 20_000); + pending.then( + value => { + clearTimeout(timer); + resolve(value); + }, + () => { + clearTimeout(timer); + resolve(undefined); + }, + ); + }); +} + /** `` + `/bili/` + the normalized http(s) upstream — the proxy-connect * side uses ws(s):, the embedded upstream keeps http(s): (codec contract). */ export function rewriteCodexResponsesWsUrl(target: string, origin: string): string { @@ -403,15 +422,21 @@ export function installNativeWebSocketIntercept(initialState: NativeInterceptSta state.onDispatch?.(rewritten, "rewrite"); return new RoutedWebSocket(rewritten, ...rest); }; - const origin = state.origin; - if (origin !== undefined) return rewrite(origin); + const origin = state.wsOrigin ?? state.origin; + if (typeof origin === "string") return rewrite(origin); // Origin still settling → defer the real connection (bounded by - // readyOrigin's timeout, same hold the fetch lane applies). + // readyOrigin's timeout, same hold the fetch lane applies). A + // pending wsOrigin (inline bridge booting) rides the same shell: + // settle undefined → DIRECT degrade below. return new DeferredWebSocket( urlStr, rest, async () => { - const settled = await readyOrigin(state); + // A pending wsOrigin is the inline bridge booting; bound it + // like readyOrigin bounds a bootstrap (20s) so a wedged + // cold start cannot hold the construction in CONNECTING + // forever — it degrades DIRECT below instead. + const settled = state.wsOrigin instanceof Promise ? await raceWsOriginBound(state.wsOrigin) : await readyOrigin(state); if (settled === undefined) { if (!warnedDirect) { warnedDirect = true; diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index 277cdc7d4a..d8dfaf21be 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -66,8 +66,12 @@ export function planNativePi(env: NodeJS.ProcessEnv, fileInline = false): { mode 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" }; + // Truthy set deliberately wide: "on"/"yes" are common shells' habit and a + // miss here silently fell through to spawn mode — the user saw a proxy + // server start, concluded inline "didn't work", and never learned why. + const inlineEnvOff = inlineEnv === "0" || inlineEnv === "false" || inlineEnv === "no" || inlineEnv === "off"; + const inlineEnvOn = inlineEnv !== undefined && inlineEnv !== "" && !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 +274,19 @@ export function armNativePi(p: typeof plan): void { state.origin = INLINE_ORIGIN; process.env.BILLION_CONTEXT_PROXY = INLINE_ORIGIN; warnLegacyBcpCoResident(); + logHostEvent("info", "[native-client]", "bili-native(pi): inline mode — compression runs in-process, no proxy server will be started"); const start = singleFlight(async () => { await getInlineEngine(); return INLINE_ORIGIN; }); state.respawn = start; + // Codex Responses WS lane (#3278 follow-up): the pseudo-origin is + // never dialable, so the WS wrapper rewrites to the engine's loopback + // bridge instead. Assigned SYNCHRONOUSLY as a promise — any WS + // constructed before the bridge listens defers (bounded), and a failed + // bridge settles undefined → the wrapper degrades that lane DIRECT + // (warned) while HTTP compression keeps running in-process. + state.wsOrigin = (async () => (await getInlineEngine()).wsOrigin)().catch(() => undefined); setNativeOriginWaiter({ wait: () => readyOrigin(state) }); state.onGiveUp = () => { delete process.env.BILLION_CONTEXT_PROXY; @@ -311,9 +323,10 @@ 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); + // Inline mode rewrites WS traffic to the engine's loopback bridge + // (state.wsOrigin) — the lane was previously left unpatched there, which + // let codex WS traffic bypass compression entirely. + installNativeWebSocketIntercept(state); } /** Test hook: expose the armed runtime-recovery seam (mirrors opencode/dsh). */ diff --git a/src/inline-engine.ts b/src/inline-engine.ts index a332e65a5c..d27d400c09 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -3,15 +3,18 @@ // 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), port bind + announce, request watchdog (in-process callers +// are bounded by upstream timeouts). The WebSocket bridge is NO longer +// skipped: the engine boots a loopback-only listener (inline-ws.ts) so the +// codex Responses WS lane can be intercepted in-process too (#3278 follow-up). import http from "node:http"; import { EventEmitter } from "node:events"; import { Buffer } from "node:buffer"; import { ensureConfigTemplate, loadOptions } 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 { startInlineWsBridge } from "./inline-ws.js"; import { createEngineContext, type EngineContext } from "./server.js"; import { handle } from "./server/handle.js"; import { withFetchTransport } from "./fetch-transport.js"; @@ -34,6 +37,15 @@ export interface InlineEngine { * withFetchTransport) so pipeline egress never re-enters the patched * fetch chain. */ dispatch(input: FetchInput, init?: RequestInit, rawFetch?: typeof globalThis.fetch): Promise; + /** Bare HTTP-shaped pipeline drive for WS-bridge envelopes (real or + * synthetic req/res). NO transport wrap of its own: the WS session wraps + * this call in withFetchTransport(its WS-backed fetch) and an inner wrap + * would override the session's upstream egress. */ + dispatchHttp(req: http.IncomingMessage, res: http.ServerResponse): Promise; + /** Loopback WS-bridge origin once the listener is up, or undefined if the + * bridge failed to boot (the native WS wrapper then degrades the codex + * lane to DIRECT, warned — same posture as an unsettled bootstrap). */ + wsOrigin: Promise; } type HeaderPair = [string, string]; @@ -292,6 +304,32 @@ function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { }; } +/** HTTP-shaped pipeline drive for the loopback WS bridge. Bare by design — + * see InlineEngine.dispatchHttp. Error classification mirrors makeDispatcher + * (same client-abort split, same error frames on the wire). */ +function makeHttpDispatch(ctx: EngineContext): InlineEngine["dispatchHttp"] { + return async (req, res) => { + try { + await handle(req, res, ctx.opts, ctx.core, ctx.config, ctx.log, ctx.instanceId, ctx.instanceStartedAt, ctx.proxyWatchers, ctx.initialWatcherPid, ctx.adminCtx); + } 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) ctx.log("info", `client aborted mid-stream: ${msg}`); + else 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 (res.destroyed) return; + if (!res.headersSent) { + const status = msg.includes("exceeds") ? 413 : 502; + res.writeHead(status, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "acp-proxy failure", detail: msg })); + } else { + res.end(); + } + } + }; +} + let engineFactory: () => Promise = startInlineEngine; /** Test seam: replace the bootstrap (fake engine) without touching config or @@ -340,5 +378,22 @@ async function startInlineEngine(): Promise { // 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) }; + const dispatchHttp = makeHttpDispatch(ctx); + // Codex Responses WS lane (#3278 follow-up): boot the loopback-only WS + // bridge alongside the engine. Failure is non-fatal — the native WS + // wrapper degrades that lane to DIRECT (warned) while HTTP compression + // keeps running in-process. + const bridge = startInlineWsBridge({ + dispatchHttp, + log: ctx.log, + isEgressProxied: (url) => resolveProxyDecision(opts.routes, opts.proxy, url, opts.proxyFallback).proxy !== undefined, + instanceId: ctx.instanceId, + }); + const wsOrigin = bridge + .then(b => b.origin) + .catch((err: unknown) => { + ctx.log("warn", `[inline] WS bridge failed to start — codex WebSocket traffic goes DIRECT (uncompressed): ${String(err)}`); + return undefined; + }); + return { origin: INLINE_ORIGIN, dispatch: makeDispatcher(ctx), dispatchHttp, wsOrigin }; } diff --git a/src/inline-ws.ts b/src/inline-ws.ts new file mode 100644 index 0000000000..cf893035e0 --- /dev/null +++ b/src/inline-ws.ts @@ -0,0 +1,82 @@ +// Loopback-only WebSocket bridge for the inline engine (#3278 follow-up). +// +// The pi inline lane compresses in-process: model HTTP traffic rides the +// pseudo-origin (INLINE_ORIGIN) through installInlineTransport. Codex's +// Responses WebSocket lane cannot — a WS connect needs a REAL, dialable +// listener, and "billion-context.inline" never resolves. So the inline engine +// now boots the SAME ws-bridge the spawned server serves (src/ws-bridge.ts), +// bound to 127.0.0.1 on an ephemeral port. Admission is loopback-only inside +// installWebSocketBridge itself (isLoopbackAddress on the peer), the tunnel +// destination guard still applies, and the server is unref'd — it can never +// keep the host process alive nor be reached from another machine. +import http from "node:http"; +import type { AddressInfo } from "node:net"; +import { installWebSocketBridge } from "./ws-bridge.js"; +import { codexResponsesCodec, responsesCodec } from "./responses-ws.js"; + +interface InlineWsBridge { + /** HTTP-shaped origin of the loopback listener ("http://127.0.0.1:PORT"). + * The native WS wrapper rewrites codex Responses URLs to + * ws:///bili/ — scheme split per rewriteCodexResponsesWsUrl. */ + origin: string; + /** Stop the listener and every live session (server.close is wrapped by + * installWebSocketBridge to shut sessions down). Never throws. */ + close(): Promise; +} + +/** + * Boot the loopback WS bridge on 127.0.0.1:0. + * + * `dispatchHttp` drives the shared request pipeline for WS-lane envelopes + * (the codecs rebuild in-process HTTP envelopes and hand them here). It must + * be the BARE pipeline drive — sessions wrap their dispatch call in + * withFetchTransport(their own WS-backed fetch), and an inner transport wrap + * here would override the session's upstream and push the model turn onto + * plain HTTP, silently changing the lane's wire shape. + */ +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; + /** #300/#3073 hop-chain instance identity, same as the spawned server. */ + instanceId?: string; +}): Promise { + const server = http.createServer((req, res) => { + // The bridge serves upgrades only. Plain HTTP landing here means a + // client dialed the loopback port without an Upgrade header — answer + // with the protocol's own "use WebSocket" status, not a stack trace. + res.writeHead(426, { "content-type": "text/plain; charset=utf-8" }); + res.end("bili inline WS bridge: WebSocket upgrade required\n"); + }); + const upgrade = installWebSocketBridge( + server, + options.dispatchHttp, + options.log, + [responsesCodec, codexResponsesCodec], + options.isEgressProxied, + options.instanceId, + ); + server.on("upgrade", (source, socket, head) => { + if (upgrade(source, socket, head)) return; + socket.end("HTTP/1.1 426 Upgrade Required\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); + }); + await new Promise((resolve, reject) => { + server.once("error", reject); + server.listen(0, "127.0.0.1", () => resolve()); + }); + // Never pin the host's event loop: the engine dies with its process. + server.unref(); + const { port } = server.address() as AddressInfo; + options.log("info", `[inline] codex WS bridge listening on 127.0.0.1:${port} (loopback only)`); + return { + origin: `http://127.0.0.1:${port}`, + close: () => + new Promise(resolve => { + server.close(() => resolve()); + // installWebSocketBridge wraps close() to also shut sessions; + // the wrapped close still calls through, so one call suffices. + }), + }; +} diff --git a/tests/pi-inline-ws.test.ts b/tests/pi-inline-ws.test.ts new file mode 100644 index 0000000000..8401e83fbd --- /dev/null +++ b/tests/pi-inline-ws.test.ts @@ -0,0 +1,258 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import test from "node:test"; +import { WebSocket as WsClient, WebSocketServer } from "ws"; +import { + _resetForTest, + installNativeWebSocketIntercept, +} from "../src/agent/native-ws-intercept.ts"; +import type { NativeInterceptState } from "../src/agent/native-intercept.ts"; +import { startInlineWsBridge } from "../src/inline-ws.ts"; +import { currentFetchTransport } from "../src/fetch-transport.ts"; + +// #3278 follow-up: the inline lane's codex Responses WebSocket coverage. +// Inline mode freezes a never-dialable pseudo-origin for HTTP, so the WS +// wrapper must rewrite to the engine's loopback bridge instead. Exercised +// end to end in-process, zero network beyond 127.0.0.1: +// A wsOrigin promise (bridge booting) → construction defers, then routes +// through the loopback bridge: claim (session-id), envelope dispatch, +// session-wrapped WS transport egress to a fake upstream, SSE back, +// frames re-framed to the client. +// B plain HTTP against the bridge port → 426 (upgrades only). +// C wsOrigin settling undefined → DIRECT degrade (warned once). +// Run: node --import tsx --test tests/pi-inline-ws.test.ts + +const TMO = 20_000; +function withTimeout(p: Promise, ms: number, label: string): Promise { + let timer: ReturnType | undefined; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`timeout: ${label}`)), ms); + }); + return Promise.race([p, timeout]).finally(() => { + if (timer !== undefined) clearTimeout(timer); + }); +} + +interface Frame { + type: string; + [key: string]: unknown; +} + +/** Minimal fake codex upstream: one Responses exchange per connection. */ +function startFakeUpstream(): Promise<{ url: string; close: () => Promise; seen: Frame[] }> { + return new Promise(resolve => { + const seen: Frame[] = []; + const server = http.createServer((_req, res) => { + res.writeHead(404); + res.end(); + }); + const wss = new WebSocketServer({ server }); + wss.on("connection", ws => { + ws.on("message", (data: Buffer) => { + let frame: Frame; + try { + frame = JSON.parse(data.toString("utf8")) as Frame; + } catch { + return; + } + seen.push(frame); + if (frame.type !== "response.create") return; + ws.send(JSON.stringify({ type: "response.created", response: { id: "resp_inline_ws" } })); + ws.send(JSON.stringify({ type: "response.output_text.delta", delta: "hello from ws upstream" })); + ws.send(JSON.stringify({ + type: "response.completed", + response: { id: "resp_inline_ws", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "hello from ws upstream" }] }] }, + })); + }); + }); + server.listen(0, "127.0.0.1", () => { + const { port } = server.address() as { port: number }; + resolve({ + url: `http://127.0.0.1:${port}/v1/responses`, + close: () => new Promise(r => wss.close(() => server.close(() => r()))), + seen, + }); + }); + server.unref(); + }); +} + +function readBody(req: http.IncomingMessage): Promise { + return new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8"))); + req.on("error", reject); + }); +} + +function savedDescriptor(): PropertyDescriptor | undefined { + return Object.getOwnPropertyDescriptor(globalThis, "WebSocket"); +} + +function restoreDescriptor(descriptor: PropertyDescriptor | undefined): void { + if (descriptor === undefined) delete (globalThis as { WebSocket?: unknown }).WebSocket; + else Object.defineProperty(globalThis, "WebSocket", descriptor); +} + +test("inline WS: deferred wsOrigin routes codex Responses through the loopback bridge end to end", async (t) => { + const upstream = await startFakeUpstream(); + const logs: string[] = []; + const log = (level: string, message: string): void => { + logs.push(`${level} ${message}`); + }; + // The bridge's dispatch stands in for the pipeline: the ambient + // transport (installed by the WS session around dispatch) IS the + // session's WS-backed fetch — using it here is the whole point. + const envelopes: string[] = []; + const bridge = await startInlineWsBridge({ + dispatchHttp: async (req, res) => { + envelopes.push(`${req.method} ${req.url}`); + const upstreamUrl = (req.url ?? "").replace(/^\/bili\//, ""); + const body = await readBody(req); + const transport = currentFetchTransport() ?? ((url: string, options: { method?: string; headers?: Record; body?: string }) => fetch(url, options)); + const response = await transport(upstreamUrl, { method: "POST", headers: { "content-type": "application/json" }, body }); + res.writeHead(response.status, { "content-type": response.headers.get("content-type") ?? "text/event-stream" }); + if (response.body !== null) { + const reader = response.body.getReader(); + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + res.write(Buffer.from(value)); + } + } else { + res.write(await response.text()); + } + res.end(); + }, + log, + }); + t.after(() => void bridge.close().then(() => upstream.close())); + + const descriptor = savedDescriptor(); + _resetForTest(); + // Real WS client base (headers-capable, like the codex CLI's own). + Object.defineProperty(globalThis, "WebSocket", { value: WsClient, configurable: true, writable: true }); + try { + const state: NativeInterceptState = { + origin: "http://billion-context.inline", + ready: Promise.resolve("http://billion-context.inline"), + // Bridge still "booting" at construction time → deferral path. + wsOrigin: Promise.resolve(bridge.origin), + }; + installNativeWebSocketIntercept(state); + + const client = new globalThis.WebSocket( + `ws://${new URL(upstream.url).host}/v1/responses`, + { headers: { "session-id": "conv-inline-ws-1" } }, + ) as unknown as InstanceType; + client.addEventListener("error", () => { /* surfaced via the done promise */ }); + const frames: Frame[] = []; + const decode = (data: unknown): string => { + if (typeof data === "string") return data; + if (data instanceof Buffer) return data.toString("utf8"); + if (data instanceof ArrayBuffer) return Buffer.from(data).toString("utf8"); + if (Array.isArray(data)) return Buffer.concat(data as Buffer[]).toString("utf8"); + return String(data); + }; + const done = new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(new Error(`timeout waiting for response.completed; frames=${JSON.stringify(frames)}`)), TMO); + client.addEventListener("open", () => { + client.send(JSON.stringify({ + type: "response.create", + model: "gpt-test", + instructions: "You are a test.", + input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "hi" }] }], + stream: true, + })); + }); + client.addEventListener("message", (event: unknown) => { + try { + frames.push(JSON.parse(decode((event as { data?: unknown }).data)) as Frame); + } catch { + return; + } + if (frames.at(-1)?.type === "response.completed") { + clearTimeout(timer); + resolve(); + } + }); + client.addEventListener("error", () => { + clearTimeout(timer); + reject(new Error(`client errored; frames=${JSON.stringify(frames)}`)); + }); + }); + await withTimeout(done, TMO, "response.completed frame"); + + // The model turn reached the fake upstream as a codex WS frame with + // the conversation input preserved… + assert.equal(upstream.seen.length, 1); + assert.equal(upstream.seen[0].type, "response.create"); + assert.equal((upstream.seen[0].input as unknown[]).length, 1); + // …the envelope was dispatched into the (stub) pipeline… + assert.deepEqual(envelopes, [`POST /bili/${upstream.url}`]); + // …and the client saw the full event sequence re-framed. + assert.deepEqual(frames.map(f => f.type), ["response.created", "response.output_text.delta", "response.completed"]); + assert.match(JSON.stringify(frames.at(-1)), /hello from ws upstream/); + client.close(); + } finally { + restoreDescriptor(descriptor); + _resetForTest(); + } +}); + +test("inline WS: plain HTTP against the bridge port is refused with 426", async (t) => { + const bridge = await startInlineWsBridge({ + dispatchHttp: async () => { + throw new Error("plain HTTP must never reach dispatch"); + }, + log: () => {}, + }); + t.after(() => void bridge.close()); + const response = await fetch(`${bridge.origin}/anything`); + assert.equal(response.status, 426); + assert.match(await response.text(), /WebSocket upgrade required/); +}); + +test("inline WS: wsOrigin settling undefined degrades the codex lane DIRECT (warned once)", async () => { + const descriptor = savedDescriptor(); + _resetForTest(); + const constructed: string[] = []; + // Spy base: records the URL it was constructed with, never connects. + class SpyWebSocket extends WsClient { + constructor(url: string) { + super(url); + constructed.push(url); + } + } + Object.defineProperty(globalThis, "WebSocket", { value: SpyWebSocket, configurable: true, writable: true }); + try { + const state: NativeInterceptState = { + origin: "http://billion-context.inline", + ready: Promise.resolve("http://billion-context.inline"), + wsOrigin: Promise.resolve(undefined), // bridge failed to boot + }; + installNativeWebSocketIntercept(state); + const target = "ws://127.0.0.1:49999/v1/responses"; + const spy = new globalThis.WebSocket(target, { headers: { "session-id": "conv-2" } }) as unknown as InstanceType; + spy.addEventListener("error", () => { /* expected: refused DIRECT connect */ }); + await new Promise(r => setTimeout(r, 200)); + // Constructed at the ORIGINAL url = DIRECT (uncompressed), exactly one. + assert.deepEqual(constructed, [target]); + } finally { + restoreDescriptor(descriptor); + _resetForTest(); + } +}); + +test("inline WS: planNativePi accepts the widened truthy/falsy BILI_PI_INLINE set", async () => { + const { planNativePi } = await import("../src/agent/pi-native.ts"); + assert.equal(planNativePi({ BILI_PI_INLINE: "on" }).mode, "inline"); + assert.equal(planNativePi({ BILI_PI_INLINE: "yes" }).mode, "inline"); + assert.equal(planNativePi({ BILI_PI_INLINE: "TRUE" }).mode, "inline"); + assert.equal(planNativePi({ BILI_PI_INLINE: "no" }).mode, "spawn"); + assert.equal(planNativePi({ BILI_PI_INLINE: "off" }).mode, "spawn"); + // file config still honored when env unset, overridden when env off + assert.equal(planNativePi({}, true).mode, "inline"); + assert.equal(planNativePi({ BILI_PI_INLINE: "false" }, true).mode, "spawn"); +}); diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 6e5a83c054..3ebd8d3007 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -121,6 +121,8 @@ test("#3278 transport: pseudo-origin URLs are served in-process, foreign URLs pa calls.push(String(input)); return new Response("inline-ok"); }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), })); try { await freshChain(); From 4d32b49c76735f0caba9bcb729da9e915078f70f Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 19:15:56 +0800 Subject: [PATCH 02/11] docs(config): seed the pi.inline/BILI_PI_INLINE WS-bridge wording; restore the missing pluginLaneServerHistory seed (#4491 drift) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit docs-sync regenerates CONFIGURATION*.md from website/config-reference yaml seeds — hand edits to the generated blocks drift. The #4491 row was hand-added without its seed, so every generate run (incl. docs-sync in CI) wanted to delete it; seeding it keeps the row and the env index entry. Backticks are forbidden in seed descs, hence the de-ticked row text. --- CONFIGURATION.zh-CN.md | 49 ++++++-------------------- website/config-reference/env-only.yaml | 4 +-- website/config-reference/server.yaml | 4 +-- 3 files changed, 14 insertions(+), 43 deletions(-) diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 77c116c811..f5d03cc84e 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -100,7 +100,7 @@ | `rollout.features.percent` | number | the version's baked-in plan | — | 看到该特性的安装占比(0–100,支持小数)。单调:上调比例不会把已入组的安装踢出;每台安装的桶值是前缀稳定的。 | | `rollout.features.salt` | string | the feature name | — | 分桶哈希盐。修改它会重新随机整个分组(唯一的洗牌方式;仅上调 percent 永不洗牌)。 | | `rollout.features.base` | string | unset (roll out over everyone) | — | 嵌套灰度:仅在所指特性的已入组人群内评估本特性,percent 相对该人群(B 只放量给 A 的用户;允许链式,环路判为关)。 | -| `rollout.features.pluginLaneServerHistory` | boolean \| number | 0(关) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 `messages` 平行的内部字段 `biliMsgIds`,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 `POST /__bili/plugin/render` 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 `serverHistory`。默认 0%,放量前无行为变化。 | +| `rollout.features.pluginLaneServerHistory` | boolean \| number | 0 (off) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 messages 平行的内部字段 biliMsgIds,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 POST /__bili/plugin/render 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 serverHistory。默认 0%,放量前无行为变化。 | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | 日志中把非公开目标主机遮成 ;凭据头无论此开关如何总是遮盖。 | | `security.adminRelay` | boolean | false | — | 允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。 | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude 子代理拥有独立会话命名空间(\|sub:),不再排队在主会话锁后。 | @@ -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 通道由随引擎一起启动的仅回环 WS 桥服务。解析顺序 env > 文件 > 默认(off):除 0/false/no/off 外任何非空值均开启(BILI_PI_INLINE=1/true/on/yes),显式关闭值优先于本文件值。 | **MITM 通道** @@ -162,10 +162,6 @@ | `network.preflightDeadEndCooldownMs` | number | 300000 | BILI_PREFLIGHT_DEAD_END_COOLDOWN_MS | 死胡同 preflight 后,对同一会话重试校准的冷却时长。 | | `network.replayRetryMax` | number | 3 (1 disables replays) | BILI_REPLAY_RETRY_MAX | 同一请求在向上游暴露错误前的最大传输失败重放次数。 | | `network.replayRetryBaseMs` | number | 1500 (0 disables the delay) | BILI_REPLAY_RETRY_BASE_MS | 重放之间的基础退避延迟(指数增长)。 | -| `network.throttle429Enabled` | boolean | true | BILI_THROTTLE_429 | 出站 429 治理器(#4204):同一 origin+凭据连续收到上游 429 后,后续请求在本地应答(零出站)而不再转发;设为 false 恢复旧的总是转发行为。 | -| `network.throttle429MaxConsecutive` | number | 5 | BILI_THROTTLE_429_MAX_CONSECUTIVE | 连续上游 429 达到该次数后 hold 转为终局(本地 424,不在任何已知 SDK 重试集合内);每个退避窗口仍放行一次探测,直到出现非 429 响应清除。 | -| `network.throttle429BaseDelayMs` | number | 2000 | BILI_THROTTLE_429_BASE_DELAY_MS | 429 hold 的基础退避(每连续一次 429 翻倍、抖动到 [0.5×, 1×]、以 throttle429MaxDelayMs 封顶;上游 Retry-After 更大时优先)。 | -| `network.throttle429MaxDelayMs` | number | 30000 | BILI_THROTTLE_429_MAX_DELAY_MS | 429 hold 退避的上限,也是终局阶段探测间隔的上限。 | | `network.maxShrinkPerCompress` | number | unset | BILI_MAX_SHRINK_PER_COMPRESS | 单次压缩允许收缩历史的上限(防塌缩护栏)。 | | `network.proxyKeepAliveMaxMs` | number | 55000 (0 = one-shot connections) | BILI_PROXY_KEEPALIVE_MAX_MS | 经出站代理的连接最大存活时长,到期强制重建(规避陈旧 NAT 映射)。 | | `network.postResponseLingerMs` | number | 5000 | BILI_POST_RESPONSE_LINGER_MS | 代理主动发起的响应后关闭的优雅关闭预算:socket 挂起等待对端 FIN/TLS close_notify,到点销毁(reason=linger-backstop)。 | @@ -230,10 +226,10 @@ | Key | Type | Default | Env | Description | |-----|------|---------|-----|-------------| | `diagnostics` | object | {} (all defaults) | — | 本地排障旋钮(转储、渲染/注入开关、count-tokens 透传)。 | -| `diagnostics.dumpBody` | boolean | false | ACP_DUMP_BODY | 把完整请求/响应体转储到日志用于排障。转储内容为明文,可能包含凭据——分享前必须清洗(#4482)。 | -| `diagnostics.dumpReq` | boolean | true | ACP_DUMP_REQ | 逐次调用记录发往上游的请求元数据;开启 dumpBody 时同时是明文请求体转储的闸门(分享前必须清洗,#4482)。 | +| `diagnostics.dumpBody` | boolean | false | ACP_DUMP_BODY | 把完整请求/响应体转储到日志用于排障。 | +| `diagnostics.dumpReq` | boolean | true | ACP_DUMP_REQ | 逐次调用记录发往上游的请求元数据。 | | `diagnostics.rawDumpDir` | string | /raw | ACP_RAW_DUMP_DIR | 原始线路转储目录。 | -| `diagnostics.dump4xx` | boolean | false | BILI_DUMP_4XX | 把上游 4xx 响应落盘以便事后检查。转储内容为明文,可能包含凭据——分享前必须清洗(#4482)。 | +| `diagnostics.dump4xx` | boolean | false | BILI_DUMP_4XX | 把上游 4xx 响应落盘以便事后检查。 | | `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | 单个 4xx 转储文件的大小上限。 | | `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | 调试转储目录(dumps/、raw/、SSE dump 目录)的总大小上限;超限时按最旧优先删除(下限 1 MiB)。转储默认无任何轮转,持续捕获下每天可增长数十 GB(#2412)。 | | `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | 调试转储目录的保留天数上限;超过该天数的文件被删除(下限 1 小时)。默认关闭。 | @@ -267,8 +263,6 @@ | `compress.preserveRecentMessages` | number | 3 | — | 最近的消息软保护、免于折叠。 | | `compress.preserveRecentTokens` | number | kernel ≈5000 | — | 最近的 token 软保护、免于折叠。 | | `compress.minCompressRangeChars` | number (deprecated alias: minCompressRange) | kernel ≈5000 | — | 可折叠片段的最小字符数;更短的永不折叠。 | -| `compress.maxSummaryLength` | number | kernel 20000 | — | 单条生成摘要的硬字符上限;超长的回复被整段拒绝(不截断、不重试)。外部摘要模型会被提前告知这个确切上限(#3635)。0 = 不限制。 | -| `compress.minSummaryLength` | number | kernel 50 | — | 生成摘要的字符下限;过短的回复被整段拒绝。0 = 禁用。外部摘要模型受同一下限约束(#3635)。 | | `compress.reconcile` | "off" \| "warn" \| "repair" | "repair" | BILI_FOLD_RECONCILE | 客户端在轮次之间回退或改写历史时校准已折叠状态。 | | `compress.promptPack` | string (builtin: "default", "lean") | builtin "default" | — | 压缩提示词包,按 项目包 → 用户包 → 内置 解析;不受 acknowledgePromptsRisk 门控。 | | `compress.tiers` | boolean | true | — | T1→T3 分级蒸馏把折叠成本摊到多代。 | @@ -289,7 +283,6 @@ | `compress.reasoningGuard` | object | off | — | gpt-5.x/6.x 推理格截断自动修复(最多 3 轮继续提示)。 | | `compress.outputSteering` | object { enabled?, verbosityLevel?, effortRouting? } | enabled false · verbosityLevel 2 | — | 向系统提示词尾部附加简洁度指令;只收紧机械延续请求。 | | `compress.priceProfile` | { w?, r?, q? } | unset (registry, kernel fallback {1, 0.1, 4}) | — | w/r/q 相对输入价的比率(写入/缓存读/输出);仅供报告,不影响触发条件或线上行为。 | -| `compress.externalSummary` | object | opt-in (disabled) | — | 把压缩摘要交给 providers 表引用的独立配置模型;三级 compress 均为整链替换语义。 | | `compress.acknowledgePromptsRisk` | boolean | false | — | 必须先置 true,自定义 prompts 才会生效。 | | `compress.absorb.enabled` | boolean | false | — | 启用 absorb 蒸馏块。 | | `compress.absorb.minToolTokens` | number | 1000 | — | 可被吸收的工具结果的最小估算 token 大小。 | @@ -354,7 +347,6 @@ |-----|------|---------|-----|-------------| | `BILI_DSH_AUTHORED_COMPACTION` | boolean | true (on, #3901 flip) | — | 让 bili 本地应答 dsh 压缩摘要请求(返回折叠快照而非拒绝,#3733):身份头 + 内核渲染格式的块摘要,零出口流量;重放经 #2432 再基线识别。 | | `BILI_DSH_CONTEXT_TAB` | boolean | true | — | dsh.contextTab 的 env 层(#4237):0/false 移除 DSH 顶栏「上下文」标签页(其余一切照常工作);其他取值强制开启并覆盖文件层;缺省 = 按文件层取值(默认 true)。下次页面加载 / dsh 重启生效。 | -| `BILI_DSH_MAX_TESTED_VERSION` | string | 0.2.0-rc.2 (CI e2e anchor) | — | “dsh native lane 启动提示的“我们测过的最高版本”上限(#4545):从注入包上方最近的 node_modules/@deepseek-ai/dsh/package.json 读出运行中的构建,**严格高于**上限才弹一条启动提示(持久日志 + 横幅),建议先回滚到上限版本。只覆盖"更新"一个方向:≤ 上限保持静默(旧版已知坏法有专门的失锁探测)。只提示、绝不拦截或阻断流量;新版本验证通过后抬高上限,空字符串 = 关闭本 lane 检查;读不到/解析不了的版本按静默处理。” | | `BILI_STORAGE_BACKEND` | sqlite\|mysql | sqlite | — | storage.backend 的环境变量层(#3931):sqlite(默认)或 mysql(已接线,#3388 第二+三步 —— 保存经异步影子队列入 mysql,同步读走 JSON 信封,replicas/ext/GC 走异步孪生;export 同样支持)。优先于文件层。 | | `BILI_MYSQL_DSN` | string | unset | — | storage.backend="mysql" 的 MySQL DSN(mysql://user:pass@host:3306/db)(#3388,#3931)。优先于文件键 storage.mysql.dsn。留空 = mysql 后端无法打开;文档化错误会列出两个旋钮。连接池惰性打开;closeQuietlyAsync 排空入队事务并关闭。 | | `BILI_STORAGE_ARCHIVE_AFTER_DAYS` | integer | 365 | — | GC 年龄清扫窗口(天,#3463):闲置超过该时长的会话被归档(单文件归档日志 archive/archive.jsonl 追加一行 + 墓碑行保留标签 + 释放内容/媒体引用)。0 关闭年龄清扫;手动入口 POST /__bili/storage/gc 始终可用。配置文件键 storage.archiveAfterDays 为持久形式。 | @@ -373,7 +365,6 @@ | `BILI_ZCODE_PORT` | number | unset | — | zcode 车道的端口钉死。 | | `BILI_ZCODE_SIGNING_FIXED` | boolean-ish | off | — | zcode 凭据车道的固定签名模式。 | | `BILI_CLAUDE_UPSTREAM` | string | unset | — | claude 车道的上游钉死。 | -| `BILI_CODEX_STATE_MODE` | string | auto | — | codex 状态处理方式:auto(新版 codex 走 CODEX_SQLITE_HOME 共享库,旧版回退 overlay 副本)、shared、legacy。shared 模式最多 16 个并发会话,每会话一个独立 overlay 槽位;legacy 保持一次仅一个独占 overlay。 | | `BILI_ATTACH_HEALTH_DEADLINE_MS` | number | (built-in) | — | 挂接外部守护进程时健康检查的截止时间。 | | `BILI_ATTACH_EVIDENCE_GRACE_MS` | number | (built-in) | — | 挂接归属证据的宽限期。 | | `BILI_PROVIDER_REWRITES` | string | unset | — | 路由前应用的 provider URL 重写规则。 | @@ -396,7 +387,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 附着目标。除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可;旧版不认这些写法会静默回退成另起代理)。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。 | | `BILI_NATIVE_ZCODE` | boolean-ish | off | — | 宿主姿态:标记 zcode 原生插件进程。 | | `BILI_PI_BIN` | string | pi (PATH lookup) | — | 宿主姿态:pi 客户端二进制的显式路径。 | | `BILI_DSH_BIN` | string | dsh (PATH lookup) | — | 宿主姿态:dsh 客户端二进制的显式路径。 | @@ -1388,20 +1379,6 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 - **状态:** DEPRECATED(`minCompressRangeChars` 的弃用别名,向后兼容保留) - **说明:** `minCompressRangeChars` 的旧名,内核映射(`compress.minCompressRange`)与单位(字符)完全相同。同层两键并存时新名优先;跨层时更深层优先,与键名无关。 -#### `maxSummaryLength` - -- **类型:** `number` -- **默认值:** *(内核默认值 `20000`)* -- **状态:** ACTIVE(#3635) -- **说明:** 单条生成摘要的硬上限,单位为**字符**。超长的回复被整段拒绝——模型自产摘要由内核的全有或全无长度检查拒绝,配置的摘要链由外部摘要执行器拒绝(不截断、不重试)。preflight 与模型工具两条压缩路径都会把这个确切上限提前告知外部摘要模型(preflight 自 #1775 起,compress 路径自 #3635 起),因此超长从「无声丢弃」变成有明确日志原因的模型合规问题。`0` 禁用上限。映射到内核字段 `compress.maxSummaryLength`。#3635 之前该键在任何层面都不被接受:面板 PUT 会静默丢弃它,手改配置文件也到不了生效配置,实际生效值永远是内核默认值。 - -#### `minSummaryLength` - -- **类型:** `number` -- **默认值:** *(内核默认值 `50`)* -- **状态:** ACTIVE(#3635) -- **说明:** 生成摘要的下限,单位为**字符**;过短的回复被整段拒绝。`0` 禁下限。映射到内核字段 `compress.minSummaryLength`。与 `maxSummaryLength` 相同的接受历史(#3635)。 - #### `tiers` - **类型:** `boolean` @@ -1858,10 +1835,6 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_STREAM_ERROR_SHAPE` | `compat.streamErrorShape` | protocol | | `BILI_STREAM_KEEPALIVE_MS` | `network.streamKeepAliveMs` | 15000 (0 disables) | | `BILI_SUBAGENT_SPLIT` | `subagentSplit` | true | -| `BILI_THROTTLE_429` | `network.throttle429Enabled` | true | -| `BILI_THROTTLE_429_BASE_DELAY_MS` | `network.throttle429BaseDelayMs` | 2000 | -| `BILI_THROTTLE_429_MAX_CONSECUTIVE` | `network.throttle429MaxConsecutive` | 5 | -| `BILI_THROTTLE_429_MAX_DELAY_MS` | `network.throttle429MaxDelayMs` | 30000 | | `BILI_UPDATE_CHECK_INTERVAL_MS` | `update.checkIntervalMs` | 180000 | | `BILI_UPDATE_POLICY_URL` | `update.policyUrl` | official repo raw URL | | `BILI_UPDATE_REGISTRY` | `update.registry` | "npmjs" (registry.npmjs.org) | @@ -1912,7 +1885,6 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_MODEL_INFO_RETRY_MS` | dsh 原生模型窗口解析失败(或解析结果不带窗口)后的重试冷却(毫秒,#1812/#1836):匹配的缓存条目没有 context window 时不视为最终结果 —— 不再让整个进程生命周期 latching 成无 header 状态,而是该冷却过期后由下一个请求触发重新解析。默认 `30000`;非数字或负值回退 `30000`。测试钩子 —— dsh-native 单元测试用它缩短冷却、避免真实等待;生产环境保持 unset。 | | `BILI_DSH_RETRY_INTERVAL_MS` | dsh 原生插件工具注册重试的退避冷却(毫秒,#2082):manifest 拉取或工具注册瞬时失败后,下一次重试不早于该冷却发生。默认 `10000`;非数字或非正值回退 `10000`。测试钩子 —— dsh-native 单元测试用它缩短冷却、避免真实等待;生产环境保持 unset。 | | `BILI_DSH_RECOVERY_INTERVAL_MS` | dsh 原生插件独立恢复定时器的间隔(毫秒,#2082):工具处于 down 状态时,即使零模型流量也持续驱动注册重试 —— 正是模型已放弃缺失工具、其他路径都不会再触发重试的场景。默认跟随 `BILI_DSH_RETRY_INTERVAL_MS`(`10000`)。测试钩子 —— NODE_TEST_CONTEXT 下除非显式设置本变量,否则定时器永不启动;生产环境保持 unset。 | -| `BILI_DSH_MAX_TESTED_VERSION` | dsh native lane 的“高于已测最高版”启动提示的上限:插件从注入包上方最近的 `node_modules/@deepseek-ai/dsh/package.json` 读出运行中的 dsh 版本,按 semver(含 pre-release 标识符)与本上限比较;**严格高于**上限才在启动时弹**一条**持久提示(dsh 会话内可见),建议先回滚到上限版本、等适配。只覆盖“更新”一个方向:≤ 上限的旧构建保持静默——旧版的已知坏法本来就有专门的失锁探测(#3806 boot 告警 + #2474 fallback),不必重复报。只提示、绝不拦截或阻断任何流量(猜错也不能弄坏能跑的会话)。默认 = `0.2.0-rc.2`(CI e2e 锚点,.github/workflows/ci-e2e-dsh-native.yml);新版本验证通过后把上限抬过去即可;设为**空字符串** = 彻底关掉本 lane 的检查。读不到/解析不了的版本按“不比高”处理,保持静默(“没读到”不等于“坏了”)。 | `BILI_ADVISORY_CHECK` | 设为 `0` 禁用严重缺陷公告监视器(#1481)。默认开启 —— 它独立于 `ACP_AUTO_UPDATE` 运行,确保关闭了自动更新的安装也能被强制移出已知缺陷版本范围。fail-open:公告源不可达/格式错误只告警,绝不阻断模型流量。文件配置键:`advisoryCheck`。 | | `BILI_ADVISORY_URL` | 公告文档 URL 覆盖。默认:已配置 registry(感知 `BILI_UPDATE_REGISTRY`)上的伴生包 `billion-context-advisories`。文件配置键:`advisoryUrl`。 | | `BILI_RELEASE_NOTES_CHECK` | 设为 `0` 禁用发版说明可见性监视器(#1870)。默认开启 —— 纯可见性,且默认静默(#1977):拉取伴生发版说明文档,仅当待更新跨度内含 `critical` 级条目时,才在 `acp_status` 与 `/acp` 面板提示「CRITICAL 更新已就绪待重启」(磁盘版本新于运行版本)或「存在 critical 更新」;routine/recommended 级发版永不上屏。绝不安装、绝不重启;fail-open。文件配置键:`releaseNotesCheck`。 | @@ -1993,7 +1965,6 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_CLAUDE_UPSTREAM` | claude 直连模式:当 `ANTHROPIC_BASE_URL` 已指向某个 relay 时,用它指定你的 relay 端点(否则会被旁路)。 | | `BILI_CLAUDE_NATIVE_PORT` | 为 claude 原生通道 hook 拉起的代理钉死**确切端口**(#964/#1660):严格端口语义——口上的占用者被大声拒绝而不是跳口——烘进受管 `ANTHROPIC_BASE_URL`。不设则 hook 骑自管区(`BILI_ZONE_PORT` 基准 + 每 lane 粘性),且每次会话把受管 URL 重钉到存活 origin,端口漂移自愈(#1660)。 | | `BILI_CODEX_COMPACT` | codex 原生压缩处理。默认 `intercept`:安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)拦截 codex 的压缩请求,在本地伪造向 ACP 状态的交接——trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}`——且不接触上游。作用范围(#3008):覆盖显式形态(compaction_trigger + /responses/compact)与以普通请求形态过线的内联自动压缩(按 SUMMARIZATION_PROMPT 模板检测)——用户自定义 compact_prompt 后锚点失配、按原样转发;bili 仅旁路观察到的压缩(pass/安全阀放行)走事后 rebase(#2372)。伪造的 ACP 摘要经历史承载交接消息注入(缺席时 developer 消息兜底),保证 codex 截断历史后压缩内容仍可见。设为 `pass` 可退出,把 codex 的压缩请求转发给上游(原生压缩兜底)。任一安全门失败则原样透传。codex 客户端判定(本项生效范围,同时用于窗口 clamp 与会话身份指纹):User-Agent 须以已注册前缀开头(`codex_cli_rs/`、`codex_exec/`、`codex desktop/`——大小写不敏感,因 Codex Desktop 等变体首字母大写,#1169),或含某个以**小写** `codex` 开头的空白分隔组件(未知变体如 `codex_sdk_ts/…`,#645——#1641 起从裸子串收窄为 token 级前缀,UA 中仅在括号内/路径段提及 "codex" 的非 codex 客户端不再被误判;兜底有意保持大小写敏感,排除 "Codex"-形中继,#1106)。 | - | `BILI_CODEX_STATE_MODE` | codex 状态处理(#2222、#3203)。默认 `auto`:当 codex 二进制支持 `CODEX_SQLITE_HOME`(codex-cli ≥ 0.147.0,经缓存的 `--version` 探测判定)时,bili 将其指向**真实** `$CODEX_HOME` —— codex 的 WAL 模式状态库留在真实 home 内的单一物理路径上,bili 与原生运行共享同一个活库。每次启动从有界池(`-bili-s1..s16`)取一个独立的每会话 overlay 槽位:生成文件(.env、config.toml)与代理绑定都按槽位隔离,因此同一个 `$CODEX_HOME` 下最多可有 16 个独立会话并发运行,各自走自己的代理(代理各跑在独立 lane `codex-s` 上 —— 同构建的两次启动若共享一个 lane,会在端口层互相拒绝,#3044)。槽位持久保留并复用 —— 绝不自动删除,因为冷启动的线程记录要经由槽位解析(#43bf59fc9)—— 崩溃会话的槽位会被下一次启动直接回收。没有逐次启动的数据库拷贝,就没有退出时的合并回写、也没有 `.bili-conflict` 隔离代(#381/#1917/#1918/#1919/#1951/#1965/#2195 补丁族在此模式下退役)。#3203 之前的固定名 `-bili` overlay 在首次 shared 模式启动时按同一套溯源仲裁一次性迁回真实 home;若仍有存活的旧版 launch 持有它,本次启动会被拒绝。`shared` 强制此模式(二进制过旧时大声降级为 legacy);`legacy` 强制旧的逐次拷贝 / 合并回写机制及其单一独占 overlay(一次只能一个会话)。仅环境变量的逃生舱 —— **没有**文件等效项。用户自设的 `CODEX_SQLITE_HOME` 原样尊重(原生与 bili 运行必须共享同一逻辑库);managed requirements.toml 的 `sqlite_home` 压过两者。 | --- @@ -2126,7 +2097,7 @@ openai_base_url = "http://localhost:8787/bili/https://chatgpt.com/backend-api/co "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com" ``` -**Claude Code** —— 把 `ANTHROPIC_BASE_URL` 环境变量设成 `/bili/` URL。(`/bili/` 形式是手动方式:旧版 claude 完全忽略代理环境变量,而新版(如 2.1.301)会遵守 `HTTPS_PROXY` —— 如果你的环境把 loopback 流量走代理,请把 loopback 主机加进 `NO_PROXY`,让这个 URL 直连而不是绕回代理(#3446)。) +**Claude Code** —— 把 `ANTHROPIC_BASE_URL` 环境变量设成 `/bili/` URL。(claude 的 undici fetch 忽略 `HTTPS_PROXY`,所以 `/bili/` URL 形式是唯一的手动方式 —— 证书 MITM 拦不到它。) ```bash export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com" @@ -2196,7 +2167,7 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a - **HTTP 代理**: `http://127.0.0.1:8787` - **代理 CA 证书路径**: 本机 bili 实际生成的 CA 文件 —— Linux/macOS 为 `~/.local/share/billion-context/ca/root-ca.pem`,Windows 为 `%USERPROFILE%\.local\share\billion-context\ca\root-ca.pem`。Web UI「接入」页的 ZCode 卡片直接显示本机实际路径并提供复制按钮,照抄即可。 - (可选)**No-proxy 列表**: `localhost,127.0.0.1` - - (ZCode 具体位置:**Settings → Network**。Claude Code 无需任何一步:启动器用 `ANTHROPIC_BASE_URL` 路由,模型流量走回环 http,子进程环境也不再带 `HTTPS_PROXY`/`NODE_EXTRA_CA_CERTS`——自己设上只会把会话内的工具指向一个并不需要的代理。) + - (ZCode 具体位置:**Settings → Network**。Claude Code 则设 `HTTPS_PROXY` 环境变量、`NODE_EXTRA_CA_CERTS` 指向 CA 路径。) > **Windows 注意:** ZCode 在 Windows 上**不会展开 `~`**,填 `~/...` 形式的路径会找不到文件(与当前工作目录无关,每个目录都识别不了)。必须填完整绝对路径,例如 `C:\Users\<用户名>\.local\share\billion-context\ca\root-ca.pem`(#342)。 @@ -2224,14 +2195,14 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`) | `NODE_EXTRA_CA_CERTS` | | omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`)+ 用户未设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC` 时的 `PI_PROXY_ANTHROPIC=`(将 OMP 的 Cowork anthropic 通道经代理路由而非 node:https 直连,#2583/#3556) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` 覆盖 | `SSL_CERT_FILE` → `combined-ca.pem` | -| claude | `ANTHROPIC_BASE_URL` = `/bili/` URL(Vertex:`ANTHROPIC_VERTEX_BASE_URL`,当 `CLAUDE_CODE_USE_VERTEX=1`;+ `NO_PROXY`/`no_proxy` loopback 合并,#3446) | 无需 | +| claude | `ANTHROPIC_BASE_URL` = `/bili/` URL(Vertex:`ANTHROPIC_VERTEX_BASE_URL`,当 `CLAUDE_CODE_USE_VERTEX=1`) | 无需 | | opencode | `HTTPS_PROXY` + 隔离 `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | | hermes | `HTTPS_PROXY`(明文 http 走 absolute-form 正向代理请求) | `SSL_CERT_FILE` → `combined-ca.pem`(另设旧版 `HERMES_CA_BUNDLE` → `root-ca.pem`) | | dsh | `HTTPS_PROXY`(明文 http 另加 `HTTP_PROXY`)+ `DEEPSEEK_BASE_URL`;**仅回环**隔离 `DSH_HOME` | `SSL_CERT_FILE` → `combined-ca.pem` | `NODE_EXTRA_CA_CERTS` 是**追加**到内置信任库,所以只指向 MITM 根证书(`root-ca.pem`)即可。`SSL_CERT_FILE` 会**替换**默认 CA bundle,所以 codex/dsh/hermes 指向 `combined-ca.pem` —— 包含 MITM 根证书**加上**操作系统信任库与 Node 公共根。合并 OS 信任库很关键:部分被启动的客户端把这个文件当作**整个**信任池(codex 的 rustls HTTP 栈是替换而非追加,#1807),它们的直连(不经代理)连接只能靠这个文件校验,因此文件必须是 OS 自身信任集合的超集(Windows 经 PowerShell 读证书库、macOS 经 `security(1)` 读钥匙串;Linux 本就直读文件系统 bundle;最多每 24 小时重建一次)。最初引入该 bundle 是为了让子进程环境里 pip/git/curl 类 TLS(盲转发、真证书)不受影响(#152;hermes 自 #1375 起,因为当前 hermes 只经 `SSL_CERT_FILE` 解析环境信任;OS 信任库合并自 #1807 起)。 -Claude Code 不走证书 MITM,一律改走 `/bili/` URL 形式、通过客户端自己的 base URL 变量下发 —— 覆盖所有上游,包括预先配置的 `ANTHROPIC_BASE_URL` relay,以及 `CLAUDE_CODE_USE_VERTEX=1` 下的 Vertex 端点;无需任何 CA 信任。旧版构建完全忽略代理环境变量,但新版(如 2.1.301)会遵守 `HTTPS_PROXY`,因此 launcher 会把 loopback 主机合并进 `NO_PROXY`/`no_proxy`(保留已有值),确保无论子进程是哪种行为,`/bili/` 这一腿都保持直连(#3446)。 +Claude Code 的 undici fetch 忽略 `HTTPS_PROXY`,所以证书 MITM 拦不到它。claude 的所有上游 —— 包括预先配置的 `ANTHROPIC_BASE_URL` relay,以及 `CLAUDE_CODE_USE_VERTEX=1` 下的 Vertex 端点 —— 一律改走 `/bili/` URL 形式,通过客户端自己的 base URL 变量下发;无需任何 CA 信任。 上游从哪里发现(只读): diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index 69e2d89682..29a8328ddf 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. Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable; a miss used to silently fall back to spawning a proxy). Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process." + desc_zh: "宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可;旧版不认这些写法会静默回退成另起代理)。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。" - 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..5f86613242 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, no ephemeral port; model traffic and the /__bili//__acp tool surface are served by an in-process engine through a pseudo-origin. The codex Responses WebSocket lane is served by a loopback-only WS bridge booted with the engine. Resolved env > file > default(off): any non-empty value except 0/false/no/off enables (BILI_PI_INLINE=1/true/on/yes), an explicit off value disables over this file value." + desc_zh: "pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务。codex 的 Responses WebSocket 通道由随引擎一起启动的仅回环 WS 桥服务。解析顺序 env > 文件 > 默认(off):除 0/false/no/off 外任何非空值均开启(BILI_PI_INLINE=1/true/on/yes),显式关闭值优先于本文件值。" - key: mitm.enabled type: boolean default: "true" From 4977e49ae553f5f98d67df062ee46da8691c4e85 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 20:41:38 +0800 Subject: [PATCH 03/11] =?UTF-8?q?docs(config):=20regenerate=20zh=20doc=20f?= =?UTF-8?q?rom=20seeds=20=E2=80=94=20master=20carried=20seed=20rows=20(thr?= =?UTF-8?q?ottle429=20#4204,=20BILI=5FCODEX=5FSTATE=5FMODE,=20#4524=20zh?= =?UTF-8?q?=20punctuation)=20without=20regenerating=20CONFIGURATION.zh-CN.?= =?UTF-8?q?md,=20leaving=20docs-sync=20drifted=20for=20every=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CONFIGURATION.zh-CN.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index f5d03cc84e..f90bb00ba1 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -100,7 +100,7 @@ | `rollout.features.percent` | number | the version's baked-in plan | — | 看到该特性的安装占比(0–100,支持小数)。单调:上调比例不会把已入组的安装踢出;每台安装的桶值是前缀稳定的。 | | `rollout.features.salt` | string | the feature name | — | 分桶哈希盐。修改它会重新随机整个分组(唯一的洗牌方式;仅上调 percent 永不洗牌)。 | | `rollout.features.base` | string | unset (roll out over everyone) | — | 嵌套灰度:仅在所指特性的已入组人群内评估本特性,percent 相对该人群(B 只放量给 A 的用户;允许链式,环路判为关)。 | -| `rollout.features.pluginLaneServerHistory` | boolean \| number | 0 (off) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 messages 平行的内部字段 biliMsgIds,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 POST /__bili/plugin/render 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 serverHistory。默认 0%,放量前无行为变化。 | +| `rollout.features.pluginLaneServerHistory` | boolean \| number | 0 (off) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 messages 平行的内部字段 biliMsgIds,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 POST /__bili/plugin/render 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 serverHistory。默认 0%,放量前无行为变化。 | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | 日志中把非公开目标主机遮成 ;凭据头无论此开关如何总是遮盖。 | | `security.adminRelay` | boolean | false | — | 允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。 | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude 子代理拥有独立会话命名空间(\|sub:),不再排队在主会话锁后。 | @@ -162,6 +162,10 @@ | `network.preflightDeadEndCooldownMs` | number | 300000 | BILI_PREFLIGHT_DEAD_END_COOLDOWN_MS | 死胡同 preflight 后,对同一会话重试校准的冷却时长。 | | `network.replayRetryMax` | number | 3 (1 disables replays) | BILI_REPLAY_RETRY_MAX | 同一请求在向上游暴露错误前的最大传输失败重放次数。 | | `network.replayRetryBaseMs` | number | 1500 (0 disables the delay) | BILI_REPLAY_RETRY_BASE_MS | 重放之间的基础退避延迟(指数增长)。 | +| `network.throttle429Enabled` | boolean | true | BILI_THROTTLE_429 | 出站 429 治理器(#4204):同一 origin+凭据连续收到上游 429 后,后续请求在本地应答(零出站)而不再转发;设为 false 恢复旧的总是转发行为。 | +| `network.throttle429MaxConsecutive` | number | 5 | BILI_THROTTLE_429_MAX_CONSECUTIVE | 连续上游 429 达到该次数后 hold 转为终局(本地 424,不在任何已知 SDK 重试集合内);每个退避窗口仍放行一次探测,直到出现非 429 响应清除。 | +| `network.throttle429BaseDelayMs` | number | 2000 | BILI_THROTTLE_429_BASE_DELAY_MS | 429 hold 的基础退避(每连续一次 429 翻倍、抖动到 [0.5×, 1×]、以 throttle429MaxDelayMs 封顶;上游 Retry-After 更大时优先)。 | +| `network.throttle429MaxDelayMs` | number | 30000 | BILI_THROTTLE_429_MAX_DELAY_MS | 429 hold 退避的上限,也是终局阶段探测间隔的上限。 | | `network.maxShrinkPerCompress` | number | unset | BILI_MAX_SHRINK_PER_COMPRESS | 单次压缩允许收缩历史的上限(防塌缩护栏)。 | | `network.proxyKeepAliveMaxMs` | number | 55000 (0 = one-shot connections) | BILI_PROXY_KEEPALIVE_MAX_MS | 经出站代理的连接最大存活时长,到期强制重建(规避陈旧 NAT 映射)。 | | `network.postResponseLingerMs` | number | 5000 | BILI_POST_RESPONSE_LINGER_MS | 代理主动发起的响应后关闭的优雅关闭预算:socket 挂起等待对端 FIN/TLS close_notify,到点销毁(reason=linger-backstop)。 | @@ -365,6 +369,7 @@ | `BILI_ZCODE_PORT` | number | unset | — | zcode 车道的端口钉死。 | | `BILI_ZCODE_SIGNING_FIXED` | boolean-ish | off | — | zcode 凭据车道的固定签名模式。 | | `BILI_CLAUDE_UPSTREAM` | string | unset | — | claude 车道的上游钉死。 | +| `BILI_CODEX_STATE_MODE` | string | auto | — | codex 状态处理方式:auto(新版 codex 走 CODEX_SQLITE_HOME 共享库,旧版回退 overlay 副本)、shared、legacy。shared 模式最多 16 个并发会话,每会话一个独立 overlay 槽位;legacy 保持一次仅一个独占 overlay。 | | `BILI_ATTACH_HEALTH_DEADLINE_MS` | number | (built-in) | — | 挂接外部守护进程时健康检查的截止时间。 | | `BILI_ATTACH_EVIDENCE_GRACE_MS` | number | (built-in) | — | 挂接归属证据的宽限期。 | | `BILI_PROVIDER_REWRITES` | string | unset | — | 路由前应用的 provider URL 重写规则。 | @@ -1835,6 +1840,10 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_STREAM_ERROR_SHAPE` | `compat.streamErrorShape` | protocol | | `BILI_STREAM_KEEPALIVE_MS` | `network.streamKeepAliveMs` | 15000 (0 disables) | | `BILI_SUBAGENT_SPLIT` | `subagentSplit` | true | +| `BILI_THROTTLE_429` | `network.throttle429Enabled` | true | +| `BILI_THROTTLE_429_BASE_DELAY_MS` | `network.throttle429BaseDelayMs` | 2000 | +| `BILI_THROTTLE_429_MAX_CONSECUTIVE` | `network.throttle429MaxConsecutive` | 5 | +| `BILI_THROTTLE_429_MAX_DELAY_MS` | `network.throttle429MaxDelayMs` | 30000 | | `BILI_UPDATE_CHECK_INTERVAL_MS` | `update.checkIntervalMs` | 180000 | | `BILI_UPDATE_POLICY_URL` | `update.policyUrl` | official repo raw URL | | `BILI_UPDATE_REGISTRY` | `update.registry` | "npmjs" (registry.npmjs.org) | From 486183c562183fb24df1d3f52f5a1e6f8475a2d6 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 22:25:05 +0800 Subject: [PATCH 04/11] =?UTF-8?q?feat(cli):=20bili=20pi-inline=20=E2=80=94?= =?UTF-8?q?=20launch=20pi=20on=20the=20in-process=20engine=20(no=20proxy,?= =?UTF-8?q?=20BILI=5FPI=5FINLINE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - launcher: pi-inline ClientName; refuses (exit 2) when the bili pi extension cannot load (no wire-mode fallback without a proxy), strips inherited BILLION_CONTEXT_PROXY/HTTPS_PROXY/BILI_MITM_HOSTS so planNativePi arms the in-process engine instead of attaching, sets BILI_PI_INLINE=1, same -e single-load rule as the proxy lane - cli help + examples - scripts/ci-pretest-cache.d.mts: declare refreshDistMtimes (runtime export existed; the declaration file lagged and typecheck flagged the test) - tests/launcher-pi-inline.test.ts: no-proxy/no-fetch assertions, env stripping under a hostile inherited proxy posture, -e only when settings has no entry, refusal path spawns nothing --- scripts/ci-pretest-cache.d.mts | 1 + src/cli.ts | 2 + src/launcher.ts | 50 +++++++- tests/launcher-pi-inline.test.ts | 212 +++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 tests/launcher-pi-inline.test.ts diff --git a/scripts/ci-pretest-cache.d.mts b/scripts/ci-pretest-cache.d.mts index 83e96ded50..e49fa052d6 100644 --- a/scripts/ci-pretest-cache.d.mts +++ b/scripts/ci-pretest-cache.d.mts @@ -14,6 +14,7 @@ export const MARKER_NAME: string; export const COMPONENTS: PretestCacheComponent[]; export function hashComponent(root: string, comp: PretestCacheComponent, platform?: string): string; export function markerIsFresh(distDir: string, hash: string): boolean; +export function refreshDistMtimes(distDir: string, now?: Date): number; export function runPretestCache(opts?: { root?: string; components?: PretestCacheComponent[]; diff --git a/src/cli.ts b/src/cli.ts index e8f0ed8cfc..1a1ce550dc 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -51,6 +51,7 @@ Usage: bili [start] [options] start the proxy (default: reads ${defaultConfigFile()}) bili pi [opts --] [args] start a proxy + launch pi against it (cert-MITM) bili pi-test [opts --] [args] like bili pi but injects --no-extensions (clean test) + bili pi-inline [opts --] [args] launch pi on the in-process engine — no proxy at all (BILI_PI_INLINE) bili codex [opts --] [args] start a proxy + launch codex against it (cert-MITM) bili claude [opts --] [args] start a proxy + launch claude against it (cert-MITM) bili omp [opts --] [args] start a proxy + launch omp against it (cert-MITM) @@ -132,6 +133,7 @@ Launcher (bili pi / bili codex / bili claude / bili omp / bili opencode / bili h bili pi # launch pi through the proxy bili pi -- print "hi" # args after the client are passed through bili pi-test # pi through the proxy with extensions off (proxy owns compression) + bili pi-inline # pi with the in-process engine — no proxy, no MITM certs bili codex # launch codex through the proxy bili claude # launch claude through the proxy bili omp # launch omp through the proxy (pi-based; /bili/ rewrite) diff --git a/src/launcher.ts b/src/launcher.ts index 3b520a9714..c48e99028b 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -163,7 +163,7 @@ import { conflictRemediation } from "./conflict-watch.js"; import { conflictScanEnabled, isDesignBenign, scanClientPlugins } from "./thirdparty-scan.js"; export const LAUNCHER_DEFAULT_HOST = "127.0.0.1"; -export const LAUNCH_CLIENTS = ["pi", "codex", "claude", "omp", "opencode", "hermes", "dsh", "codebuddy", "qoder", "trae", "jcode", "kimi", "gemini", "iflow", "qwen", "mcode", "aider", "copilot", "amp", "crush", "zed", "goose", "antigravity", "pi-test"] as const; +export const LAUNCH_CLIENTS = ["pi", "codex", "claude", "omp", "opencode", "hermes", "dsh", "codebuddy", "qoder", "trae", "jcode", "kimi", "gemini", "iflow", "qwen", "mcode", "aider", "copilot", "amp", "crush", "zed", "goose", "antigravity", "pi-test", "pi-inline"] as const; export type ClientName = (typeof LAUNCH_CLIENTS)[number]; type BaseClientName = "claude" | "codex" | "pi" | "omp" | "opencode" | "hermes" | "dsh" | "codebuddy" | "qoder" | "trae" | "jcode" | "kimi" | "gemini" | "iflow" | "qwen" | "mcode" | "aider" | "copilot" | "amp" | "crush" | "zed" | "goose" | "antigravity"; @@ -333,7 +333,7 @@ export function isLaunchClient(value: string): value is ClientName { } export function baseClientName(client: ClientName): BaseClientName { - return client === "pi-test" ? "pi" : client; + return client === "pi-test" || client === "pi-inline" ? "pi" : client; } /** `pi-test` injects `--no-extensions` so the billion-context-pi client extension doesn't double-compress alongside the proxy. */ @@ -5775,6 +5775,52 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} ); } } + // `bili pi-inline` (#3278/#4526): launch pi on the IN-PROCESS engine — no + // proxy is spawned at all. BILI_PI_INLINE outranks ambient attach envs in + // planNativePi, so the child's own bili extension arms inline mode and + // (with the loopback WS bridge) also takes the codex WebSocket lane. The + // extension MUST be loadable here: with no proxy there is no wire-mode + // fallback, so a missing extension would silently run uncompressed. + if (params.client === "pi-inline") { + const piExt = selfDistFile("agent/pi.js"); + const extAvailable = (piExt !== undefined && fs.existsSync(piExt)) || piPluginInstalled(piRealHome); + if (!extAvailable) { + console.error( + "bili: pi-inline needs the bili pi extension — run `bili plugin install pi` or reinstall billion-context " + + "(dist/agent/pi.js missing and the plugin is not installed in " + piRealHome + "/settings.json)", + ); + process.exit(2); + return; + } + // Clean slate for the child: a stale inherited BILLION_CONTEXT_PROXY + // would make planNativePi ATTACH instead of arming the in-process + // engine, and inherited proxy/MITM env has nothing to do inline (no + // TLS is terminated in this lane). + const inlineEnv = stripInheritedProxy(process.env); + delete inlineEnv.BILLION_CONTEXT_PROXY; + delete inlineEnv.BILI_MITM_HOSTS; + inlineEnv.BILI_PI_INLINE = "1"; + let inlineArgs = params.clientArgs; + // Same single-load rule as the proxy lane: when settings.json already + // loads the plugin, `-e` on top would double-register it. + if (piExt && fs.existsSync(piExt) && !piPluginInstalled(piRealHome)) { + inlineArgs = ["-e", piExt, ...inlineArgs]; + } + console.error("bili: pi-inline — compression runs inside pi (in-process engine + loopback WS bridge); no proxy is started."); + const { command, prefixArgs } = resolveClientCommand("pi", process.env, deps.platform ?? process.platform); + let inlineCode = 0; + try { + inlineCode = await runClient(command, [...prefixArgs, ...inlineArgs], inlineEnv, { + spawnImpl: deps.spawnImpl, + platform: deps.platform, + }); + } catch (err) { + console.error(`bili: failed to launch pi-inline: ${err instanceof Error ? err.message : String(err)}`); + inlineCode = 1; + } + process.exit(inlineCode ?? 0); + return; + } if (base === "omp" && routes.httpRewrites.length > 0) { const ompExt = selfDistFile("agent/omp.js"); const extAvailable = (ompExt !== undefined && fs.existsSync(ompExt)) || ompPluginLoadedFrom(ompRealHome); diff --git a/tests/launcher-pi-inline.test.ts b/tests/launcher-pi-inline.test.ts new file mode 100644 index 0000000000..23470a3598 --- /dev/null +++ b/tests/launcher-pi-inline.test.ts @@ -0,0 +1,212 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { rmrf } from "./tmp-rm.ts"; +import { runLaunch } from "../src/launcher.js"; +import type { SpawnChild, SpawnFn } from "../src/launcher.js"; +import { selfPackageRoot } from "../src/plugin-install.js"; + +function makeFakeChild(pid: number): SpawnChild { + const handlers = new Map void)[]>(); + return { + pid, + unref() {}, + kill() { + return true; + }, + on(event, listener) { + const list = handlers.get(event) ?? []; + list.push(listener); + handlers.set(event, list); + }, + }; +} + +interface SpawnRecord { + command: string; + args: string[]; + env: NodeJS.ProcessEnv | undefined; +} + +// `bili pi-inline` (#3278/#4526): the launcher must start NO proxy, hand the +// child a clean env with BILI_PI_INLINE=1 (no BILLION_CONTEXT_PROXY/HTTPS_PROXY +// — planNativePi would otherwise attach instead of arming the in-process +// engine), and follow the same single-load rule as the proxy lane for `-e`. +test("runLaunch pi-inline: no proxy spawned, BILI_PI_INLINE=1, attach/proxy env stripped, -e only when plugin not installed", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-piinline-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevPiBin = process.env.PI_BIN; + const prevPiDir = process.env.PI_CODING_AGENT_DIR; + 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; + delete process.env.PI_CODING_AGENT_DIR; + // Hostile inherited posture: these would flip planNativePi to attach (or + // MITM-shape routing) if the launcher failed to strip them. + process.env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:9"; + process.env.HTTPS_PROXY = "http://127.0.0.1:9"; + process.env.BILI_MITM_HOSTS = "example.com"; + const fakePi = path.join(home, process.platform === "win32" ? "fake-pi.exe" : "fake-pi"); + fs.writeFileSync(fakePi, ""); + process.env.PI_BIN = fakePi; + const piHome = path.join(home, ".pi/agent"); + fs.mkdirSync(piHome, { recursive: true }); + fs.writeFileSync(path.join(piHome, "models.json"), JSON.stringify({ providers: {} })); + + // runLaunch only injects -e when dist/agent/pi.js exists; create a stub + // when running tests from a checkout without a prior build (same seam as + // tests/launcher.test.ts). + const root = selfPackageRoot(); + const distAgent = path.join(root, "dist", "agent", "pi.js"); + const stubbed = !fs.existsSync(distAgent); + if (stubbed) { + fs.mkdirSync(path.dirname(distAgent), { recursive: true }); + fs.writeFileSync(distAgent, ""); + } + + const spawns: SpawnRecord[] = []; + const spawnImpl: SpawnFn = (cmd, args, options) => { + spawns.push({ command: cmd, args: [...args], env: options?.env }); + const child = makeFakeChild(0); + const orig = child.on!.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + }; + // No proxy is started on this lane — any health probe or fetch is a bug. + const fetched: string[] = []; + const fetchImpl = async (url: string) => { + fetched.push(String(url)); + return { ok: true }; + }; + const fetchHealthInfo = async (origin: string) => { + fetched.push(`health:${origin}`); + return { ok: true, pid: 42422 }; + }; + + const prevExit = process.exit; + const exitCalls: number[] = []; + process.exit = ((code?: number) => { + exitCalls.push(code ?? 0); + return undefined as never; + }) as typeof process.exit; + + try { + await runLaunch( + { client: "pi-inline", clientArgs: [], overrides: {} }, + { fetchImpl, fetchHealthInfo, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(spawns.length, 1, "exactly one child (pi itself) — no proxy spawn"); + assert.equal(spawns[0].command, fakePi); + assert.deepEqual(spawns[0].args.slice(0, 2), ["-e", distAgent], "plugin injected via -e when settings.json has no entry"); + const env = spawns[0].env!; + assert.equal(env.BILI_PI_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)"); + assert.equal(env.BILI_MITM_HOSTS, undefined); + assert.deepEqual(fetched, [], "no proxy health/fetch traffic on the inline lane"); + assert.deepEqual(exitCalls, [0]); + + // settings.json packages already points at this install → no -e + fs.writeFileSync(path.join(piHome, "settings.json"), JSON.stringify({ packages: [root] })); + spawns.length = 0; + exitCalls.length = 0; + await runLaunch( + { client: "pi-inline", clientArgs: [], overrides: {} }, + { fetchImpl, fetchHealthInfo, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(spawns.length, 1); + assert.ok(!spawns[0].args.includes("-e"), "no double-register when settings.json already loads the plugin"); + assert.equal(spawns[0].env!.BILI_PI_INLINE, "1"); + assert.deepEqual(exitCalls, [0]); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevPiBin === undefined) delete process.env.PI_BIN; + else process.env.PI_BIN = prevPiBin; + if (prevPiDir === undefined) delete process.env.PI_CODING_AGENT_DIR; + else process.env.PI_CODING_AGENT_DIR = prevPiDir; + if (prevBcp === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevBcp; + if (prevHttpsProxy === undefined) delete process.env.HTTPS_PROXY; + else process.env.HTTPS_PROXY = prevHttpsProxy; + if (prevMitmHosts === undefined) delete process.env.BILI_MITM_HOSTS; + else process.env.BILI_MITM_HOSTS = prevMitmHosts; + if (stubbed) fs.rmSync(distAgent, { force: true }); + rmrf(home); + } +}); + +// Without a loadable extension the inline lane must refuse BEFORE spawning +// anything: there is no wire-mode fallback when no proxy exists, so a silent +// pass-through would run the whole session uncompressed. +test("runLaunch pi-inline: refuses (exit 2) when the bili extension cannot load", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-piinlineref-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevPiBin = process.env.PI_BIN; + const prevPiDir = process.env.PI_CODING_AGENT_DIR; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.PI_CODING_AGENT_DIR; + const fakePi = path.join(home, process.platform === "win32" ? "fake-pi.exe" : "fake-pi"); + fs.writeFileSync(fakePi, ""); + process.env.PI_BIN = fakePi; + const piHome = path.join(home, ".pi/agent"); + fs.mkdirSync(piHome, { recursive: true }); + fs.writeFileSync(path.join(piHome, "models.json"), JSON.stringify({ providers: {} })); + + const root = selfPackageRoot(); + const distAgent = path.join(root, "dist", "agent", "pi.js"); + const distBackup = `${distAgent}.bak-test`; + const distExisted = fs.existsSync(distAgent); + if (distExisted) fs.renameSync(distAgent, distBackup); + else { + // A stub that exists would make the launch succeed — ensure absence. + fs.rmSync(distAgent, { force: true }); + } + + const spawns: SpawnRecord[] = []; + const spawnImpl: SpawnFn = (cmd, args, options) => { + spawns.push({ command: cmd, args: [...args], env: options?.env }); + return makeFakeChild(42422); + }; + + const prevExit = process.exit; + const exitCalls: number[] = []; + process.exit = ((code?: number) => { + exitCalls.push(code ?? 0); + return undefined as never; + }) as typeof process.exit; + + try { + await runLaunch( + { client: "pi-inline", clientArgs: [], overrides: {} }, + { spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(exitCalls.length, 1); + assert.equal(exitCalls[0], 2, "must refuse with exit code 2"); + assert.equal(spawns.length, 0, "nothing may be spawned — not even pi"); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevPiBin === undefined) delete process.env.PI_BIN; + else process.env.PI_BIN = prevPiBin; + if (prevPiDir === undefined) delete process.env.PI_CODING_AGENT_DIR; + else process.env.PI_CODING_AGENT_DIR = prevPiDir; + if (distExisted) fs.renameSync(distBackup, distAgent); + rmrf(home); + } +}); From eec7db542c906595a15a6f3b313484e3b11fdc3e Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 23:20:47 +0800 Subject: [PATCH 05/11] =?UTF-8?q?fix(inline):=20mute=20the=20engine's=20st?= =?UTF-8?q?derr=20leg=20=E2=80=94=20the=20inline=20host=20owns=20that=20te?= =?UTF-8?q?rminal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Field report on bili pi-inline: at boot every [storage]/[persist]/[inline] line scrolled the pi input box away ('启动的时候都是日志 输入框没了'). The tee logger's stderr leg exists for the foreground proxy process; in the inline lane (#3278) the engine runs INSIDE the host agent, so stderr is the interactive TUI. Mute the terminal leg at engine start (bili.log file leg stays; the logger re-enables stderr itself if the file leg dies, #2864); BILI_INLINE_DEBUG=1 opts back in for live troubleshooting (documented in the env-only seed). Regression test: real engine boot under a stderr spy must write zero bytes. --- CONFIGURATION.md | 1 + CONFIGURATION.zh-CN.md | 1 + src/inline-engine.ts | 12 +++++++++++- tests/pi-inline.test.ts | 25 +++++++++++++++++++++++++ website/config-reference/env-only.yaml | 8 ++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 2dc97c8008..c63119b9c4 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -397,6 +397,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `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. Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable; a miss used to silently fall back to spawning a proxy). Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process. | +| `BILI_INLINE_DEBUG` | boolean-ish | off | — | Inline lane (#3278) only: keep the engine's log lines on stderr after muting them (the inline engine runs inside the pi process, where stderr is the interactive TUI — every [persist]/[storage]/[inline] line would scroll the input box away; default keeps them in bili.log only). | | `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 f90bb00ba1..ddc3a9976e 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -393,6 +393,7 @@ | `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 附着目标。除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可;旧版不认这些写法会静默回退成另起代理)。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。 | +| `BILI_INLINE_DEBUG` | boolean-ish | off | — | 仅内联通道(#3278):把引擎日志保留在 stderr 上(默认关闭——内联引擎跑在 pi 进程里,stderr 就是交互终端,每行 [persist]/[storage]/[inline] 都会把输入框挤掉;默认只写 bili.log)。 | | `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/src/inline-engine.ts b/src/inline-engine.ts index d27d400c09..6cfdd63952 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -19,7 +19,7 @@ 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 { log as loggerLog, muteStderrLeg } from "./logger.js"; import { buildProxyFailureResponse, isExpectedFailFast } from "./upstream-fail.js"; /** Pseudo-origin of the inline lane. Never DNS-resolved: any request reaching @@ -366,6 +366,16 @@ export function _resetInlineEngineForTest(): void { } async function startInlineEngine(): Promise { + // The engine runs INSIDE the host agent's process (#3278 inline lane), + // and that process's stderr IS the interactive TUI terminal (pi, dsh, + // …). The tee logger's stderr leg would print every engine line — + // storage mode, persist warm-up, the background index scan 1-2 s after + // boot — straight onto the TUI, scrolling the input box away (field + // report: "启动的时候都是日志 输入框没了"). Keep the durable bili.log file + // leg, mute the terminal leg; the logger re-enables stderr itself if the + // file leg ever dies (#2864), and BILI_INLINE_DEBUG=1 opts back in for + // live troubleshooting. + if (!process.env.BILI_INLINE_DEBUG) muteStderrLeg(); // Same pre-flight as the CLI server path (cli.ts `start`): template // config, resolved options, sqlite engine self-bootstrap (#2833). ensureConfigTemplate(); diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 3ebd8d3007..5406ac2a6c 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -209,6 +209,31 @@ test("#3278 engine: real pipeline runs in-process — one upstream round trip, s assert.equal(h.status, 200); }); +test("#3278 engine: boot is TUI-safe — zero stderr writes while the interactive host owns the terminal", { timeout: 90_000 }, async () => { + // Field report (2026-10-24): `bili pi-inline` scrolled the pi input box + // away — the inline engine runs INSIDE the host process, so the tee + // logger's stderr leg printed every [storage]/[persist]/[inline] line + // straight onto the interactive terminal. The engine must mute that leg + // (file-only; BILI_INLINE_DEBUG opts back in) at boot. + const ie = await import("../src/inline-engine.ts"); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + const realWrite = process.stderr.write.bind(process.stderr); + const seen: string[] = []; + process.stderr.write = ((chunk: unknown, ...rest: unknown[]) => { + seen.push(typeof chunk === "string" ? chunk : String(chunk)); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + return (realWrite as any)(chunk, ...rest); + }) as typeof process.stderr.write; + try { + const engine = await withTimeout(ie.getInlineEngine(), 60_000, "engine boot"); + assert.equal(engine.origin, ie.INLINE_ORIGIN); + assert.equal(seen.length, 0, `inline boot must not write to the host's TUI stderr, got: ${seen.join(" | ").slice(0, 400)}`); + } finally { + process.stderr.write = realWrite; + } +}); + test("#3278 engine: caller abort cancels the in-process round trip (spawn-mode parity)", { timeout: 60_000 }, async () => { const ie = await import("../src/inline-engine.ts"); const engine = await withTimeout(ie.getInlineEngine(), 60_000, "engine boot"); diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index 29a8328ddf..e262d837c9 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -362,6 +362,14 @@ entries: 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. Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable; a miss used to silently fall back to spawning a proxy). Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process." desc_zh: "宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可;旧版不认这些写法会静默回退成另起代理)。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。" + - key: BILI_INLINE_DEBUG + type: boolean-ish + default: "off" + kind: env-only + audience: internal + group: envonly + desc_en: "Inline lane (#3278) only: keep the engine's log lines on stderr after muting them (the inline engine runs inside the pi process, where stderr is the interactive TUI — every [persist]/[storage]/[inline] line would scroll the input box away; default keeps them in bili.log only)." + desc_zh: "仅内联通道(#3278):把引擎日志保留在 stderr 上(默认关闭——内联引擎跑在 pi 进程里,stderr 就是交互终端,每行 [persist]/[storage]/[inline] 都会把输入框挤掉;默认只写 bili.log)。" - key: BILI_NATIVE_ZCODE type: boolean-ish default: "off" From 3868136298b961f0e34938c06a1ff91b5f4bb295 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 23:43:44 +0800 Subject: [PATCH 06/11] =?UTF-8?q?fix(chain):=20let=20bili's=20own=20stacke?= =?UTF-8?q?d=20links=20hand=20off=20legitimately=20=E2=80=94=20the=20inlin?= =?UTF-8?q?e=20lane's=20pseudo-origin=20rewrite=20was=20dying=20on=20real?= =?UTF-8?q?=20DNS=20(#4527)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The native intercept rewrites model URLs to ${INLINE_ORIGIN}/bili/ and hands them DOWN its send() to the inline transport link. But the inline link's patched wrapper runs inside the #1662 depth protocol: depth===1 means 're-entry', so it skipped its own dispatch and the pseudo-origin URL fell through to the raw fetch — ENOTFOUND billion-context.inline. Every model AND management fetch in the real inline lane failed this way since #3278 (no e2e ever exercised the two links stacked like the production arm). Fix: each link's send() now stamps the exact downstream function it calls (chainHop AsyncLocalStorage); a patched wrapper entered with the hop pointing at ITSELF is a legitimate descent from the bili link directly above and runs its dispatch one level deeper. A foreign wrapper in between re-issues the call as its own hop, so the identity match fails and #1662 truncation keeps protecting the descent exactly as before. Adds the missing composition test: inline link + native intercept installed in production order; a plain model URL must land on the engine and the real upstream must stay untouched. --- src/agent/fetch-chain/index.ts | 22 ++++++++++++- tests/pi-inline.test.ts | 57 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index ecf3b50dfd..0fa3b689c5 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -60,6 +60,15 @@ let warnedReanchor = false; // further re-entry falls back to the plain walk (degenerate multi-plugin // nesting) so the termination itself can never loop. const dispatchDepth = new AsyncLocalStorage(); +// #4527: one-hop descent marker. Each link's `send` stamps the EXACT +// downstream function it is about to call; a bili link whose `patched` +// wrapper is entered with this marker pointing at ITSELF is being called by +// the bili link directly above it (e.g. the native intercept handing its +// INLINE_ORIGIN rewrite down to the inline transport) — a legitimate hop +// through our own stack that must still run this link's dispatch. A foreign +// wrapper in between re-issues the call as ITS own hop, so the identity +// match fails and #1662's truncation below keeps protecting the descent. +const chainHop = new AsyncLocalStorage(); let warnedReentry = false; /** #1410: the dead-closure signature. A wrapper whose owner nulled its @@ -95,7 +104,10 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) let cur = ds; for (;;) { try { - return await cur(input, init); + // #4527: stamp the hop so the downstream link (when it is + // one of ours) can tell a legitimate descent from a + // foreign wrapper's re-entry (#1662). + return await chainHop.run(cur, () => cur(input, init)); } catch (err) { const next = isDeadClosureError(err) ? nextLiveAnchor(cur) : undefined; if (next === undefined) throw err; @@ -126,6 +138,14 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) // ponytail: unknown wrappers retain the #1662 cutoff; preserving their // deeper hooks requires a shared chain protocol. const patched = async (input: string | URL | Request, init?: RequestInit): Promise => { + // #4527: legitimate descent through our own link stack — the link + // directly above called THIS wrapper by identity (see chainHop). + // The #1662 depth logic below exists to terminate FOREIGN + // re-wraps, whose hop identity can never match; run our dispatch + // at one deeper level instead of skipping or truncating it. + if (chainHop.getStore() === patched) { + return dispatchDepth.run((dispatchDepth.getStore() ?? 0) + 1, () => dispatchBody(input, init)); + } const depth = dispatchDepth.getStore(); if (depth === 1) { if (Reflect.get(ds, "__piWebAccessProxyFetch") === true) return send(input, init); diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 5406ac2a6c..e810c08bfa 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -145,6 +145,63 @@ test("#3278 transport: pseudo-origin URLs are served in-process, foreign URLs pa } }); +test("#4527 composition: native intercept + inline link stacked like the real arm — rewrites land on the inline engine, never on real DNS", async () => { + // The production arm (pi-native.ts) installs the inline transport FIRST, + // then the native fetch intercept ON TOP. The intercept rewrites model + // URLs to `${INLINE_ORIGIN}/bili/` and hands them DOWN via its + // send(); pre-#4527 the inline link's `patched` wrapper saw depth===1 + // (#1662 protocol) and skipped its dispatch entirely — the pseudo-origin + // URL fell through to the raw fetch and died on real DNS + // (ENOTFOUND billion-context.inline). This is the composition shape every + // earlier test bypassed by calling the engine directly. + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + const calls: string[] = []; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (input: string | URL | Request): Promise => { + calls.push(String(input)); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + })); + try { + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + const before = hits.length; + // A PLAIN model URL — what the host SDK actually calls. The intercept + // rewrites it to the pseudo-origin; the inline link must serve it. + const r = await withTimeout(fetch(`${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "model via chain"); + assert.equal(r.status, 200); + assert.equal(await r.text(), "inline-ok"); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`]); + // The real upstream must stay untouched — a DNS-bound pseudo-origin + // leak would never reach it anyway (ENOTFOUND). + assert.equal(hits.length, before); + // Management URLs take the intercept's control-passthrough branch and + // must land on the inline engine too (the manifest/status fetches). + const m = await withTimeout(fetch(`${ie.INLINE_ORIGIN}/__bili/health`), 10_000, "mgmt via chain"); + assert.equal(m.status, 200); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, `${ie.INLINE_ORIGIN}/__bili/health`]); + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + test("#3278 transport: engine failure degrades model traffic direct, management fails loud", async () => { const ie = await import("../src/inline-engine.ts"); ie._setEngineFactoryForTest(async () => { From c6ddafe8c79a8221e4969759681d70c66988e0ba Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 23:56:55 +0800 Subject: [PATCH 07/11] feat(inline): name the lane in the status panel + serve the Web UI on a loopback listener (#4571) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Field report: the inline status panel printed a dead 'Web UI: http://127.0.0.1:0/...' link (no listener exists inline — the fake request sockets hardcode localPort 0) and nothing in the panel said the engine was the in-process inline lane. - startInlineEngine now binds a loopback-only, unref'd, admin-only web listener (127.0.0.1:) that drives the same pipeline; the panel's Web UI deep link becomes a real browser-clickable URL. Model lanes (/bili/...) are deliberately NOT exposed on it; bind failure is fail-soft (link hidden). - the fake inline sockets report the real port as localPort so admin routes compute the true origin even for in-process status calls. - ProxyOptions.inlineLane marks the lane at boot; /__bili/plugin/status and /__bili/plugin/tool pass it through and the panel version line appends ' · inline'. - webSessionUrl hides ':0' origins instead of printing dead links. --- src/config.ts | 5 +++ src/inline-engine.ts | 69 +++++++++++++++++++++++++++++++++++++++-- src/plugin.ts | 9 ++++-- src/server/admin.ts | 4 +-- tests/pi-inline.test.ts | 43 +++++++++++++++++++++++++ 5 files changed, 123 insertions(+), 7 deletions(-) diff --git a/src/config.ts b/src/config.ts index 077ec0286b..d821dce54c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -782,6 +782,11 @@ export type ProxyOptions = { port: number; host: string; upstream: string; + /** #4571: set by the inline lane at boot (startInlineEngine) — true when + * these options belong to the in-process engine instead of a spawned + * proxy server. Display-only downstream (the status panel appends + * " · inline"), never read for control flow. */ + inlineLane?: boolean; routes: ProviderRoutes; /** Named dialing recipes from the providers table (pi/opencode idiom) — * referenced by `compress.externalSummary.targets` ("glm/glm-4.9-flash"). diff --git a/src/inline-engine.ts b/src/inline-engine.ts index 6cfdd63952..2e53cb65b1 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -46,6 +46,11 @@ export interface InlineEngine { * bridge failed to boot (the native WS wrapper then degrades the codex * lane to DIRECT, warned — same posture as an unsettled bootstrap). */ wsOrigin: Promise; + /** Loopback WEB listener origin (http://127.0.0.1:) serving the + * built-in Web UI + admin API for a real BROWSER, or undefined when the + * bind failed (panel then hides the Web UI line). Same loopback-only, + * unref'd, fail-soft posture as the WS bridge. */ + webOrigin: Promise; } type HeaderPair = [string, string]; @@ -85,11 +90,20 @@ async function bodyToBuffer(body: BodyLike | null | undefined): Promise * Body delivery is LAZY (microtask after listener registration) — readBody * attaches its data/end handlers after construction, so emitting eagerly * would resolve nothing and hang the request. */ +/** Real port of the loopback web listener once it is up (0 until then). The + * fake sockets below report it as localPort so admin routes compute a REAL + * browser-clickable webOrigin (http://127.0.0.1:) for panel links — + * pre-#4571 they hardcoded 0 and the panel printed a dead :0 URL. */ +let webPort = 0; +/** The current listener (tracked so a re-boot closes the previous one + * instead of leaking an ephemeral port that serves a stale engine). */ +let webServer: http.Server | undefined; + class InlineReq extends EventEmitter { url: string; method: string; headers: Record = {}; - socket = { localPort: 0, remoteAddress: "127.0.0.1" }; + socket = { get localPort() { return webPort; }, remoteAddress: "127.0.0.1" }; private chunks: Buffer[]; private deliveryScheduled = false; @@ -130,7 +144,7 @@ class InlineRes extends EventEmitter { headersSent = false; writableEnded = false; destroyed = false; - socket = { localPort: 0, remoteAddress: "127.0.0.1", destroyed: false, bytesWritten: 0 }; + socket = { get localPort() { return webPort; }, remoteAddress: "127.0.0.1", destroyed: false, bytesWritten: 0 }; readonly bodyStream: ReadableStream; headHeaders: HeaderPair[] = []; private controller: ReadableStreamDefaultController | undefined; @@ -363,6 +377,15 @@ export function getInlineEngine(): Promise { export function _resetInlineEngineForTest(): void { enginePromise = undefined; failedAt = 0; + closeWebListener(); +} + +function closeWebListener(): void { + if (webServer !== undefined) { + webServer.close(); + webServer = undefined; + } + webPort = 0; } async function startInlineEngine(): Promise { @@ -380,6 +403,10 @@ async function startInlineEngine(): Promise { // config, resolved options, sqlite engine self-bootstrap (#2833). ensureConfigTemplate(); const opts = loadOptions(); + // Mark every downstream consumer of these options (admin router, plugin + // status panel) that THIS engine is the in-process inline lane — the + // status panel appends " · inline" so a screenshot says which mode it is. + opts.inlineLane = true; const bootstrapOutcome = await ensureSqliteEngine({ resolveProxy: (url) => resolveProxy(opts.routes, opts.proxy, url, opts.proxyFallback), }); @@ -405,5 +432,41 @@ async function startInlineEngine(): Promise { ctx.log("warn", `[inline] WS bridge failed to start — codex WebSocket traffic goes DIRECT (uncompressed): ${String(err)}`); return undefined; }); - return { origin: INLINE_ORIGIN, dispatch: makeDispatcher(ctx), dispatchHttp, wsOrigin }; + // Loopback WEB listener (#4571 follow-up): the Web UI deep links the + // status panel prints need a port a BROWSER can dial — the pseudo-origin + // only exists inside this process. Binds 127.0.0.1:0 (ephemeral, unref'd, + // fail-soft like the WS bridge) and serves ONLY the /__bili/ + /__acp/ + // admin/web routes through the same pipeline drive; model lanes (/bili/…) + // are intentionally NOT exposed on it. + const webOrigin = new Promise((resolve) => { + closeWebListener(); + const web = http.createServer((req, res) => { + const url = req.url ?? "/"; + if (url !== "/__bili/" && !url.startsWith("/__bili/") && url !== "/__acp/" && !url.startsWith("/__acp/")) { + res.writeHead(404, { "content-type": "text/plain" }); + res.end("inline web listener serves /__bili/ only"); + return; + } + void dispatchHttp(req, res); + }); + web.once("error", (err: NodeJS.ErrnoException) => { + ctx.log("warn", `[inline] web listener failed to start — the Web UI link stays hidden: ${String(err)}`); + resolve(undefined); + }); + web.listen(0, "127.0.0.1", () => { + const addr = web.address(); + const port = typeof addr === "object" && addr !== null ? addr.port : 0; + if (typeof port === "number" && port > 0) { + webPort = port; + webServer = web; + web.unref(); + ctx.log("info", `[inline] web UI listening on http://127.0.0.1:${port} (loopback only)`); + resolve(`http://127.0.0.1:${port}`); + } else { + ctx.log("warn", "[inline] web listener bound without a port — the Web UI link stays hidden"); + resolve(undefined); + } + }); + }); + return { origin: INLINE_ORIGIN, dispatch: makeDispatcher(ctx), dispatchHttp, wsOrigin, webOrigin }; } diff --git a/src/plugin.ts b/src/plugin.ts index c1a076fc1e..29a7de3d90 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -975,6 +975,9 @@ export type PluginToolDeps = { // Browser-reachable origin of THIS proxy (http://host:port) for the human-facing // Web UI deep links inside panels/reports; absent in test harnesses/embeds. webOrigin?: string; + // #4571: true when the answering engine is the in-process inline lane — + // the status panel appends " · inline" so a screenshot names its mode. + inlineLane?: boolean; signal?: AbortSignal; }; @@ -1624,7 +1627,9 @@ function chainAdvisoryPanel(v: ChainVerdict): string { * usable origin was provided (tests/embeds) or the id is empty. */ function webSessionUrl(origin: string | undefined, sessionId: string): string | undefined { const o = typeof origin === "string" ? origin.trim().replace(/\/+$/, "") : ""; - if (o.length === 0 || sessionId.length === 0) return undefined; + // ":0" = no real listener (the inline lane's web bind failed, or a test + // fixture) — printing a dead port is worse than printing nothing. + if (o.length === 0 || o.endsWith(":0") || sessionId.length === 0) return undefined; return `${o}/__bili/#/session/${encodeURIComponent(sessionId)}`; } @@ -1747,7 +1752,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt // same addition so the Session-only derivation (unpruned − sent) still // isolates pruned originals on one scale. panel = buildStatusPanel({ - version: `billion-context@${PROXY_VERSION}${BUILD_COMMIT && BUILD_COMMIT !== "unknown" ? ` (${BUILD_COMMIT})` : ""} · pack: ${session.meta.activePack ?? "default"}`, + version: `billion-context@${PROXY_VERSION}${BUILD_COMMIT && BUILD_COMMIT !== "unknown" ? ` (${BUILD_COMMIT})` : ""} · pack: ${session.meta.activePack ?? "default"}${deps.inlineLane ? " · inline" : ""}`, // #2671: effective storage mode — legacy/shadow/full, including // the encryption-blocked downgrade. Display-only, from the env the // store actually resolved. diff --git a/src/server/admin.ts b/src/server/admin.ts index 6f57a24bf2..180dd05a3c 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -477,7 +477,7 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv // back for cross-origin browser readers (the dsh client Context tab); // only origins the gate above admitted ever reach this line. const reqOrigin = req.headers.origin; - return handlePluginStatus(conversationId, res, { core, config, log, webOrigin }, params.get("fallback") === "latest", typeof reqOrigin === "string" && reqOrigin.length > 0 ? reqOrigin : undefined); + return handlePluginStatus(conversationId, res, { core, config, log, webOrigin, inlineLane: opts.inlineLane === true }, params.get("fallback") === "latest", typeof reqOrigin === "string" && reqOrigin.length > 0 ? reqOrigin : undefined); } if (req.method === "POST" && req.url === "/__bili/watcher") { // #7: an ATTACHING claude session registers its host pid so the shared @@ -715,7 +715,7 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv try { const body = await readBody(req); const webOrigin = `http://${opts.host === "0.0.0.0" ? "localhost" : opts.host}:${req.socket?.localPort ?? opts.port}`; - return await handlePluginTool(body.toString("utf8"), res, { core, config, log, webOrigin }); + return await handlePluginTool(body.toString("utf8"), res, { core, config, log, webOrigin, inlineLane: opts.inlineLane === true }); } catch (err) { res.writeHead(err instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: false, error: String(err) })); diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index e810c08bfa..2658294e37 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -123,6 +123,7 @@ test("#3278 transport: pseudo-origin URLs are served in-process, foreign URLs pa }, dispatchHttp: async () => {}, wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), })); try { await freshChain(); @@ -167,6 +168,7 @@ test("#4527 composition: native intercept + inline link stacked like the real ar }, dispatchHttp: async () => {}, wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), })); try { ni._resetForTest(); @@ -291,6 +293,47 @@ test("#3278 engine: boot is TUI-safe — zero stderr writes while the interactiv } }); +test("#4571 inline web: loopback Web UI listener + status panel names its lane", { timeout: 90_000 }, async () => { + // Field report (2026-10-24): the inline status panel printed + // `Web UI: http://127.0.0.1:0/...` (dead link — no listener exists in the + // inline lane) and nothing said the engine was inline. Fix: a loopback, + // unref'd, admin-only web listener whose real port feeds both the panel + // link and a " · inline" lane marker. + const ie = await import("../src/inline-engine.ts"); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + try { + const engine = await withTimeout(ie.getInlineEngine(), 60_000, "engine boot"); + // one model round trip first: gives the status endpoint a live session + // and proves the dispatcher still works with the listener bound + const body = JSON.stringify({ model: "test-model", max_tokens: 1024, messages: [{ role: "user", content: "hi" }] }); + const r = await withTimeout(engine.dispatch( + `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, + { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "pi", "x-bili-plugin-conversation": "inline-web-panel-1" }, body }, + globalThis.fetch, + ), 30_000, "model dispatch"); + assert.equal(r.status, 200); + const wo = await withTimeout(engine.webOrigin, 10_000, "webOrigin"); + assert.ok(typeof wo === "string" && /^http:\/\/127\.0\.0\.1:\d+$/.test(wo), `webOrigin=${String(wo)}`); + // 1. the BROWSER path: a real TCP round trip against the loopback listener + const o = await withTimeout(fetch(`${wo}/__bili/overview`), 10_000, "overview via loopback"); + assert.equal(o.status, 200); + // 2. the listener is admin-only: model lanes are NOT exposed on it + const nf = await withTimeout(fetch(`${wo}/bili/${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "model lane refused"); + assert.equal(nf.status, 404); + // 3. the status panel names its lane and prints the REAL link + const s = await withTimeout(engine.dispatch(`${ie.INLINE_ORIGIN}/__bili/plugin/status?conversationId=inline-web-panel-1`, {}, globalThis.fetch), 10_000, "status"); + assert.equal(s.status, 200); + const j = await s.json() as { ok: boolean; panel: string | null }; + assert.equal(j.ok, true); + assert.match(j.panel ?? "", /· inline/); + assert.match(j.panel ?? "", new RegExp(`${wo}/__bili/#/session/`)); + } finally { + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + test("#3278 engine: caller abort cancels the in-process round trip (spawn-mode parity)", { timeout: 60_000 }, async () => { const ie = await import("../src/inline-engine.ts"); const engine = await withTimeout(ie.getInlineEngine(), 60_000, "engine boot"); From e0befa79df69d684bbc0c86b170bc11f46e1278e Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 25 Oct 2026 00:04:11 +0800 Subject: [PATCH 08/11] =?UTF-8?q?feat(pi):=20default=20the=20pi=20lane=20t?= =?UTF-8?q?o=20the=20in-process=20engine=20=E2=80=94=20no=20proxy=20server?= =?UTF-8?q?=20spawn=20unless=20opted=20back=20in=20(#4571)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Plain `pi` with the plugin installed now compresses IN-PROCESS (inline lane, #3278): no proxy server process, no ephemeral port. The spawned-server mode is one opt-away: "pi": {"inline": false} in billion-context.json, or BILI_PI_INLINE=0/false/no/off in the env. Precedence is otherwise unchanged: kill switches and /bili/ launches still stand down; an explicit env/config inline choice still outranks everything; an announced attach target (launcher-set BILLION_CONTEXT_PROXY/ATTACH) still beats the new DEFAULT — wrapped launches keep attaching to their server. An explicit env OFF still suppresses a file-true value (env > file). planNativePi's fileInline parameter becomes tri-state (true/false/undefined) so 'not configured' (default inline) and 'explicitly disabled' (server) are distinct. Config reference seeds + CONFIGURATION.md regenerated. --- CONFIGURATION.md | 6 +++--- CONFIGURATION.zh-CN.md | 6 +++--- src/agent/pi-native.ts | 12 +++++++++--- src/config.ts | 10 ++++++---- tests/pi-inline-ws.test.ts | 4 ++++ tests/pi-native.test.ts | 17 +++++++++++------ website/config-reference/env-only.yaml | 6 +++--- website/config-reference/server.yaml | 6 +++--- 8 files changed, 42 insertions(+), 25 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index c63119b9c4..96a2f3fff3 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. The codex Responses WebSocket lane is served by a loopback-only WS bridge booted with the engine. Resolved env > file > default(off): any non-empty value except 0/false/no/off enables (BILI_PI_INLINE=1/true/on/yes), an explicit off value disables over this file value. | +| `pi.inline` | boolean | true | BILI_PI_INLINE | Pi inline lane (#3278; DEFAULT since #4571): the pi extension's compression runs 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, the codex Responses WebSocket lane by a loopback-only WS bridge, and the Web UI by a loopback-only web listener booted with the engine. Set false (or BILI_PI_INLINE=0/false/no/off) to get the spawned proxy server back. Resolved env > file; an announced attach target (launcher-set BILLION_CONTEXT_PROXY/ATTACH) still beats the default, but never an explicit inline choice. | **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. Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable; a miss used to silently fall back to spawning a proxy). Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process. | +| `BILI_PI_INLINE` | boolean-ish | on (pi default since #4571) | — | 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. Since #4571 the pi lane defaults to this in-process mode; Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable); an explicit 0/false/no/off opts back into the spawned proxy server. Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process. | | `BILI_INLINE_DEBUG` | boolean-ish | off | — | Inline lane (#3278) only: keep the engine's log lines on stderr after muting them (the inline engine runs inside the pi process, where stderr is the interactive TUI — every [persist]/[storage]/[inline] line would scroll the input box away; default keeps them in bili.log only). | | `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. | @@ -1827,7 +1827,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_PERSIST_EPERM_ALERT_THRESHOLD` | `persist.epermAlertThreshold` | 5 | | `BILI_PERSIST_TAIL_TOKENS` | `persist.tailTokens` | 16384 (0 disables message persistence) | | `BILI_PERSIST_ZSTD` | `persist.zstd` | false | -| `BILI_PI_INLINE` | `pi.inline` | false | +| `BILI_PI_INLINE` | `pi.inline` | true | | `BILI_POST_RESPONSE_LINGER_MS` | `network.postResponseLingerMs` | 5000 | | `BILI_PREFLIGHT_DEAD_END_COOLDOWN_MS` | `network.preflightDeadEndCooldownMs` | 300000 | | `BILI_PREFLIGHT_HOLD_MS` | `network.preflightHoldMs` | 30000 | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index ddc3a9976e..e40d1858cf 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 工具面由进程内引擎经伪源站服务。codex 的 Responses WebSocket 通道由随引擎一起启动的仅回环 WS 桥服务。解析顺序 env > 文件 > 默认(off):除 0/false/no/off 外任何非空值均开启(BILI_PI_INLINE=1/true/on/yes),显式关闭值优先于本文件值。 | +| `pi.inline` | boolean | true | BILI_PI_INLINE | pi 内联压缩通道(#3278;#4571 起默认开启):pi 扩展的压缩在进程内完成——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务,codex 的 Responses WebSocket 通道走随引擎启动的仅回环 WS 桥,Web UI 走随引擎启动的仅回环监听。设为 false(或 BILI_PI_INLINE=0/false/no/off)即可换回派生代理服务器的模式。解析顺序 env > 文件;启动器预置的附着目标(BILLION_CONTEXT_PROXY/ATTACH)仍优先于默认值,但永远不覆盖显式开启。 | **MITM 通道** @@ -392,7 +392,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 附着目标。除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可;旧版不认这些写法会静默回退成另起代理)。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。 | +| `BILI_PI_INLINE` | boolean-ish | on (pi default since #4571) | — | 宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。#4571 起 pi 通道默认就是进程内模式;除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可);显式 0/false/no/off 则换回另起代理进程。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。 | | `BILI_INLINE_DEBUG` | boolean-ish | off | — | 仅内联通道(#3278):把引擎日志保留在 stderr 上(默认关闭——内联引擎跑在 pi 进程里,stderr 就是交互终端,每行 [persist]/[storage]/[inline] 都会把输入框挤掉;默认只写 bili.log)。 | | `BILI_NATIVE_ZCODE` | boolean-ish | off | — | 宿主姿态:标记 zcode 原生插件进程。 | | `BILI_PI_BIN` | string | pi (PATH lookup) | — | 宿主姿态:pi 客户端二进制的显式路径。 | @@ -1806,7 +1806,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_PERSIST_EPERM_ALERT_THRESHOLD` | `persist.epermAlertThreshold` | 5 | | `BILI_PERSIST_TAIL_TOKENS` | `persist.tailTokens` | 16384 (0 disables message persistence) | | `BILI_PERSIST_ZSTD` | `persist.zstd` | false | -| `BILI_PI_INLINE` | `pi.inline` | false | +| `BILI_PI_INLINE` | `pi.inline` | true | | `BILI_POST_RESPONSE_LINGER_MS` | `network.postResponseLingerMs` | 5000 | | `BILI_PREFLIGHT_DEAD_END_COOLDOWN_MS` | `network.preflightDeadEndCooldownMs` | 300000 | | `BILI_PREFLIGHT_HOLD_MS` | `network.preflightHoldMs` | 30000 | diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index d8dfaf21be..a916d8aa16 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -62,7 +62,7 @@ export function shouldBootstrapNative(env: NodeJS.ProcessEnv): boolean { * 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. */ -export function planNativePi(env: NodeJS.ProcessEnv, fileInline = false): { mode: "off" | "attach" | "spawn" | "inline"; attachOrigin?: string } { +export function planNativePi(env: NodeJS.ProcessEnv, fileInline: boolean | undefined = undefined): { 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(); @@ -71,9 +71,15 @@ export function planNativePi(env: NodeJS.ProcessEnv, fileInline = false): { mode // server start, concluded inline "didn't work", and never learned why. const inlineEnvOff = inlineEnv === "0" || inlineEnv === "false" || inlineEnv === "no" || inlineEnv === "off"; const inlineEnvOn = inlineEnv !== undefined && inlineEnv !== "" && !inlineEnvOff; - if (inlineEnvOn || (!inlineEnvOff && fileInline)) return { mode: "inline" }; + // Explicit choices keep the historical precedence — an explicit env OFF + // still suppresses a file-true value (env > file, #3278 contract): + if (!inlineEnvOff && (inlineEnvOn || fileInline === true)) return { mode: "inline" }; const attachOrigin = nativeAttachOrigin(env) ?? proxyEnvOrigin(env); if (attachOrigin !== undefined) return { mode: "attach", attachOrigin }; + // …then the #4571 default: the pi lane compresses IN-PROCESS. A proxy + // server is spawned only on explicit opt-out (pi.inline=false in + // billion-context.json, or BILI_PI_INLINE=0/false/no/off). + if (!inlineEnvOff && fileInline !== false) return { mode: "inline" }; return { mode: "spawn" }; } @@ -223,7 +229,7 @@ export async function verifyAttachAndRecover(attachOrigin: string): Promise file > default(off); BILI_PI_INLINE=0/false explicitly disables - * it over the file. */ + * `inline` (#3278, default ON since #4571): run the pi lane's compression + * IN-PROCESS — the pi host no longer spawns a proxy server by default. + * Set `false` (or BILI_PI_INLINE=0/false/no/off) to get the spawned + * server back. Resolved env > file; an announced attach target + * (launcher-set BILLION_CONTEXT_PROXY/ATTACH) still beats the DEFAULT + * but never an explicit inline choice. */ 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 diff --git a/tests/pi-inline-ws.test.ts b/tests/pi-inline-ws.test.ts index 8401e83fbd..03b04e1657 100644 --- a/tests/pi-inline-ws.test.ts +++ b/tests/pi-inline-ws.test.ts @@ -255,4 +255,8 @@ test("inline WS: planNativePi accepts the widened truthy/falsy BILI_PI_INLINE se // file config still honored when env unset, overridden when env off assert.equal(planNativePi({}, true).mode, "inline"); assert.equal(planNativePi({ BILI_PI_INLINE: "false" }, true).mode, "spawn"); + // #4571: no signals at all = the in-process default; explicit file opt-out plans the server + assert.equal(planNativePi({}).mode, "inline"); + assert.equal(planNativePi({}, undefined).mode, "inline"); + assert.equal(planNativePi({}, false).mode, "spawn"); }); diff --git a/tests/pi-native.test.ts b/tests/pi-native.test.ts index 9ef5a2b285..fbba00ccf3 100644 --- a/tests/pi-native.test.ts +++ b/tests/pi-native.test.ts @@ -134,8 +134,12 @@ test("legacyBcpEntriesIn: reads packages[], tolerates missing/broken files", () assert.deepEqual(legacyBcpEntriesIn(path.join(dir, "missing.json")), []); }); -test("planNativePi: default is spawn; opt-out and /bili/ launches are off", () => { - assert.deepEqual(planNativePi({}), { mode: "spawn" }); +test("planNativePi: default is the in-process inline lane (#4571); opt-out and /bili/ launches are off", () => { + // plain `pi` with the plugin and no signals compresses IN-PROCESS — no + // proxy server spawn. Explicit opt-out paths still plan the server. + assert.deepEqual(planNativePi({}), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "0" }), { mode: "spawn" }); + assert.deepEqual(planNativePi({}, false), { mode: "spawn" }); assert.deepEqual(planNativePi({ BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); assert.deepEqual(planNativePi({ BILI_NATIVE_PI: "0" }), { mode: "off" }); assert.deepEqual(planNativePi({ BILI_PROVIDER_REWRITES: '{"vllm":"http://127.0.0.1:1/bili/http://x"}' }), { mode: "off" }); @@ -156,9 +160,10 @@ test("planNativePi: a preset BILLION_CONTEXT_PROXY is an attach target, not a st // kill switches and a /bili/ launch still win over the preset assert.deepEqual(planNativePi({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); assert.deepEqual(planNativePi({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485", BILI_PROVIDER_REWRITES: "{}" }), { mode: "off" }); - // a garbage / blank preset falls back to spawn (self-managed), never dead-off - assert.deepEqual(planNativePi({ BILLION_CONTEXT_PROXY: "garbage" }), { mode: "spawn" }); - assert.deepEqual(planNativePi({ BILLION_CONTEXT_PROXY: " " }), { mode: "spawn" }); + // a garbage / blank preset falls back to self-managed — the in-process + // inline lane since #4571 — never dead-off + assert.deepEqual(planNativePi({ BILLION_CONTEXT_PROXY: "garbage" }), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILLION_CONTEXT_PROXY: " " }), { mode: "inline" }); }); test("planNativePi: explicit BILLION_CONTEXT_ATTACH wins over the env preset", () => { @@ -187,7 +192,7 @@ test("#3278: BILI_PI_INLINE plans the no-server lane and outranks ambient attach assert.deepEqual(planNativePi({ BILI_PI_INLINE: "1", BILI_PROVIDER_REWRITES: "{}" }), { mode: "off" }); // unset / off values keep today's postures untouched assert.deepEqual(planNativePi({ BILI_PI_INLINE: "0" }), { mode: "spawn" }); - assert.deepEqual(planNativePi({}), { mode: "spawn" }); + assert.deepEqual(planNativePi({}), { mode: "inline" }); }); test("#3278: pi.inline (config file) enables the lane; env resolves over it", () => { diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index e262d837c9..fd96e9291a 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -356,12 +356,12 @@ entries: desc_zh: "宿主姿态:标记 pi 原生插件进程。" - key: BILI_PI_INLINE type: boolean-ish - default: "off" + default: "on (pi default since #4571)" 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. Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable; a miss used to silently fall back to spawning a proxy). Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process." - desc_zh: "宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可;旧版不认这些写法会静默回退成另起代理)。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。" + 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. Since #4571 the pi lane defaults to this in-process mode; Truthy = any non-empty value except 0/false/no/off (on/yes/TRUE all enable); an explicit 0/false/no/off opts back into the spawned proxy server. Codex Responses WS traffic is routed through a loopback-only bridge booted with the engine; if that bridge fails, the WS lane degrades DIRECT (warned) while HTTP compression stays in-process." + desc_zh: "宿主姿态:pi 内联压缩通道(#3278)——pi 扩展在进程内通过伪源站直接压缩,不再另起代理进程;优先于预置的 BILLION_CONTEXT_PROXY 附着目标。#4571 起 pi 通道默认就是进程内模式;除 0/false/no/off 外任何非空值均算开启(on/yes/TRUE 均可);显式 0/false/no/off 则换回另起代理进程。codex 的 Responses WS 流量改走随引擎启动的仅回环桥;桥起不来时该通道退回直连(打 warn),HTTP 压缩仍留在进程内。" - key: BILI_INLINE_DEBUG type: boolean-ish default: "off" diff --git a/website/config-reference/server.yaml b/website/config-reference/server.yaml index 5f86613242..5e3f8f7a12 100644 --- a/website/config-reference/server.yaml +++ b/website/config-reference/server.yaml @@ -480,11 +480,11 @@ entries: desc_zh: "内置 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 段。" - key: pi.inline type: boolean - default: "false" + default: "true" 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. The codex Responses WebSocket lane is served by a loopback-only WS bridge booted with the engine. Resolved env > file > default(off): any non-empty value except 0/false/no/off enables (BILI_PI_INLINE=1/true/on/yes), an explicit off value disables over this file value." - desc_zh: "pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务。codex 的 Responses WebSocket 通道由随引擎一起启动的仅回环 WS 桥服务。解析顺序 env > 文件 > 默认(off):除 0/false/no/off 外任何非空值均开启(BILI_PI_INLINE=1/true/on/yes),显式关闭值优先于本文件值。" + desc_en: "Pi inline lane (#3278; DEFAULT since #4571): the pi extension's compression runs 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, the codex Responses WebSocket lane by a loopback-only WS bridge, and the Web UI by a loopback-only web listener booted with the engine. Set false (or BILI_PI_INLINE=0/false/no/off) to get the spawned proxy server back. Resolved env > file; an announced attach target (launcher-set BILLION_CONTEXT_PROXY/ATTACH) still beats the default, but never an explicit inline choice." + desc_zh: "pi 内联压缩通道(#3278;#4571 起默认开启):pi 扩展的压缩在进程内完成——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务,codex 的 Responses WebSocket 通道走随引擎启动的仅回环 WS 桥,Web UI 走随引擎启动的仅回环监听。设为 false(或 BILI_PI_INLINE=0/false/no/off)即可换回派生代理服务器的模式。解析顺序 env > 文件;启动器预置的附着目标(BILLION_CONTEXT_PROXY/ATTACH)仍优先于默认值,但永远不覆盖显式开启。" - key: mitm.enabled type: boolean default: "true" From f590da94499008fb0f2488a9db7c34c100adfbb7 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 25 Oct 2026 11:43:49 +0800 Subject: [PATCH 09/11] =?UTF-8?q?feat(web):=20show=20which=20engine=20serv?= =?UTF-8?q?ed=20a=20session=20=E2=80=94=20inline=20vs=20server=20badge=20(?= =?UTF-8?q?#4571)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Field report: a pi session opened from the inline panel showed 基本信息 with no hint of which engine served it, so "这个是 inline 模式吧?" could only be answered from bili.log (the session in question had in fact attached to a running server, not gone inline). Stamps session.metadata.engineLane at request time (handle.ts, first hit wins like clientHint): "inline" when the engine runs in-process (opts.inlineLane), "server" for a standalone proxy process (spawned or attached). The web session list chips it beside the client hint (inline / srv) and 基本信息 gains an 引擎 row — in-process (inline) vs server process. Pre-stamp sessions render nothing (absent = unknown, not guessed). Tests: pi-inline.test.ts inline-web case now asserts the pi session carries engineLane:"inline" in /__bili/sessions. --- src/server/handle.ts | 8 ++++++++ src/web/client.ts | 11 +++++++++-- src/web/i18n.ts | 6 ++++++ src/web/sessions-data.ts | 12 ++++++++++++ tests/pi-inline.test.ts | 9 +++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/server/handle.ts b/src/server/handle.ts index 79d088af05..1ec676fcd4 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -1531,6 +1531,14 @@ async function handleRequest( const hint = sniffScanClient(req.headers) ?? (ua ? ua.slice(0, 120) : undefined); if (hint) session.metadata.clientHint = hint; } + // #4571: which engine served this session — "inline" (pi\u2019s in-process + // engine, no server process) or "server" (a standalone proxy process, + // spawned or attached). First hit wins like clientHint so the badge + // reflects the lane the session was born in; the web UI surfaces it so + // “did this run inline?” is answerable at a glance instead of from logs. + if (session.metadata.engineLane === undefined) { + session.metadata.engineLane = opts.inlineLane === true ? "inline" : "server"; + } // [#1333] Real pi plugin traffic arrives pre-stamped: `x-bili-plugin` // + `x-bili-plugin-conversation` (set by the extension, pi.ts:127) // set pluginAgent/pluginConversation from headers above, so the diff --git a/src/web/client.ts b/src/web/client.ts index 419a216ef9..67d6a44763 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -629,9 +629,14 @@ export const WEB_CLIENT = `(function () { tr.innerHTML = "" + sessionTitleCell(s) + '' + protoBadge(s.protocol) + '' + fmtW(s.contextBest ? s.contextBest.tokens : s.contextTokens) + '' + savedTd(s) + '' + timeAgo(s.lastSeen) + ""; } else { const clientHint = s.clientHint ? String(s.clientHint) : ""; + // #4571: inline sessions carry a lane chip beside the client hint so + // "did this run in-process?" is visible in the list, not just detail. + const laneChip = s.engineLane === "inline" + ? ' inline' + : s.engineLane === "server" ? ' srv' : ""; const clientCell = clientHint - ? '' + escapeHtml(clientHint) + "" - : '' + t("common.none") + ""; + ? '' + escapeHtml(clientHint) + "" + laneChip + : '' + t("common.none") + "" + laneChip; tr.innerHTML = "" + sessionTitleCell(s) + '' + clientCell + '' + protoBadge(s.protocol) + '' + escapeHtml(hostOf(s.upstreamOrigin)) + '' + (s.requests ? s.requests : t("common.none")) + '' + fmtW(s.contextBest ? s.contextBest.tokens : s.contextTokens) + '' + (s.inputTokens ? fmtW(s.inputTokens) : '' + t("common.none") + "") + "" + savedTd(s) + hitTd(s) + switchTd(s) + '' + (s.foldCount || 0) + '' + (s.blocks || 0) + '' + timeAgo(s.lastSeen) + ""; } let navTimer = null; @@ -2516,6 +2521,8 @@ function changelogCatOf(rawLine) { if (d.label && d.label !== d.id) kv(parts, t("common.label"), d.label); kv(parts, t("common.protocol"), d.protocol || null, true); kv(parts, t("det.client_hint"), d.clientHint || null, true); + // #4571: engine lane — in-process (inline) vs standalone proxy process. + kv(parts, t("det.engine_lane"), d.engineLane === "inline" ? t("det.engine_lane_inline") : d.engineLane === "server" ? t("det.engine_lane_server") : null, true); if (d.selfHeal) kv(parts, t("det.self_heal"), (d.selfHeal.action === "degrade-to-proxy" ? t("det.self_heal_degrade_to_proxy") : t("det.self_heal_suppress_nudge")) + " · " + d.selfHeal.detected + " · " + timeAgo(d.selfHeal.since), true); // #2740: session-level notice — auto-mode verdicts here are bili-local, not model judgments. if (d.autoModeForge) kv(parts, t("det.auto_mode_forge"), t("det.auto_mode_forge_local") + " · " + d.autoModeForge.count + "×" + (d.autoModeForge.lastVariant || "?") + " · " + timeAgo(d.autoModeForge.lastAt), true); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index cd941b96ab..3369fbf3b4 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -403,6 +403,9 @@ export const MESSAGES: Record> = { "det.fold_h": "折叠后命中率", "det.fold_t": "累计回补 T", "det.client_hint": "客户端", + "det.engine_lane": "引擎", + "det.engine_lane_inline": "进程内·inline", + "det.engine_lane_server": "独立进程·server", "det.self_heal": "自愈状态", "det.self_heal_tip": "会话卡死自愈已接管:详见会话详情(#2155)", "det.self_heal_short": "自愈", @@ -1004,6 +1007,9 @@ export const MESSAGES: Record> = { "det.fold_h": "post-fold hit %", "det.fold_t": "cumulative repay T", "det.client_hint": "client", + "det.engine_lane": "engine", + "det.engine_lane_inline": "in-process (inline)", + "det.engine_lane_server": "server process", "det.self_heal": "Self-heal", "det.self_heal_tip": "Stuck-session self-heal has taken over — see session detail (#2155)", "det.self_heal_short": "heal", diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index b6059f6b83..aa6949a919 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -66,6 +66,9 @@ interface WebSessionSummary { /** Which client produced this session: plugin agents stamp metadata.pluginAgent, * non-plugin clients get a sniffed/UA-derived metadata.clientHint (server.ts #1426). */ clientHint?: string; + /** #4571: which engine served this session — "inline" (pi in-process) or + * "server" (standalone proxy process). Absent on pre-stamp sessions. */ + engineLane?: "inline" | "server"; /** Number of compression events recorded in the per-request cache ledger (folds). */ foldCount?: number; /** Non-cached token attribution from ledger samples (acp-kernel decomposeSample): @@ -171,6 +174,8 @@ interface WebSessionDetail extends WebSessionSummary { activePack?: string; /** Which client produced this session (plugin agent name or header/UA hint). */ clientHint?: string; + /** #4571: which engine served this session (see WebSessionSummary). */ + engineLane?: "inline" | "server"; /** bili build that last persisted this session file (absent on pre-stamp files). */ biliVersion?: string; /** #2671: effective storage mode at the session file's last write — @@ -728,6 +733,9 @@ function summaryOf(s: SummarySource, live: boolean): WebSessionSummary { const clientHint = typeof metaRec["pluginAgent"] === "string" && metaRec["pluginAgent"] ? metaRec["pluginAgent"] as string : typeof metaRec["clientHint"] === "string" && metaRec["clientHint"] ? metaRec["clientHint"] as string : ""; + // #4571: engine-lane stamp (handle.ts); only the two known values pass — + // anything else in a hand-edited file renders as "not stamped". + const engineLane = metaRec["engineLane"] === "inline" || metaRec["engineLane"] === "server" ? metaRec["engineLane"] : undefined; // #3376: idle cache-keepalive state — memory-only, so live rows only. const keepalive = live ? keepaliveStatusFor(s.id) : undefined; const best = displayContextBest(s); @@ -780,6 +788,7 @@ function summaryOf(s: SummarySource, live: boolean): WebSessionSummary { ? { keySwitches: agg.agg.keySwitches, keySwitchMissedTokens: agg?.agg?.keySwitchMissed ?? 0 } : {}), ...(clientHint ? { clientHint } : {}), + ...(engineLane ? { engineLane } : {}), }; } @@ -1093,6 +1102,8 @@ function renderDetail(session: Session, live: boolean): WebSessionDetail { // clients carry a header-sniffed or User-Agent hint (metadata.clientHint). const pluginAgent = typeof session.metadata["pluginAgent"] === "string" ? session.metadata["pluginAgent"] : undefined; const clientHint = pluginAgent ?? (typeof session.metadata["clientHint"] === "string" ? session.metadata["clientHint"] : undefined); + // #4571 engine-lane badge (handle.ts stamp) — same shape as summaryOf's. + const engineLane = session.metadata["engineLane"] === "inline" || session.metadata["engineLane"] === "server" ? session.metadata["engineLane"] : undefined; const sysPrompt = typeof session.metadata["systemPromptTokens"] === "number" ? session.metadata["systemPromptTokens"] : 0; // #2430/#2545/#2969: bili's own siblings (billion-context-pi / opencode-acp) are // compatible family and verified non-compressing plugins (KNOWN_NON_COMPRESSORS) @@ -1164,6 +1175,7 @@ function renderDetail(session: Session, live: boolean): WebSessionDetail { rangeRestores: session.stats.rangeRestores, ...(session.meta.activePack ? { activePack: session.meta.activePack } : {}), ...(clientHint ? { clientHint } : {}), + ...(engineLane ? { engineLane } : {}), ...(typeof session.metadata["biliVersion"] === "string" ? { biliVersion: session.metadata["biliVersion"] as string } : {}), ...(typeof session.metadata["storageMode"] === "string" ? { storageMode: session.metadata["storageMode"] as string } : {}), // #3756 follow-up: snapshot-render shadow observability (compute the diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 2658294e37..00b175a4ff 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -328,6 +328,15 @@ test("#4571 inline web: loopback Web UI listener + status panel names its lane", assert.equal(j.ok, true); assert.match(j.panel ?? "", /· inline/); assert.match(j.panel ?? "", new RegExp(`${wo}/__bili/#/session/`)); + // 4. #4571 engine-lane stamp: the session this engine served is badged + // "inline" in the session list — the web answers "did this run + // in-process?" without digging through logs. + const ses = await withTimeout(engine.dispatch(`${ie.INLINE_ORIGIN}/__bili/sessions`, {}, globalThis.fetch), 10_000, "sessions"); + assert.equal(ses.status, 200); + const raw = await ses.json() as { sessions?: Array<{ id: string; engineLane?: string; clientHint?: string }>, hiddenEmpty?: number } | Array<{ id: string; engineLane?: string; clientHint?: string }>; + const list = Array.isArray(raw) ? raw : raw.sessions ?? []; + const mine = list.find((x) => x.engineLane === "inline" && x.clientHint === "pi"); + assert.ok(mine, `no pi session carries the inline engineLane badge: ${JSON.stringify(list).slice(0, 300)}`); } finally { ie._setEngineFactoryForTest(undefined); ie._resetInlineEngineForTest(); From 35bae3d1ac85b9c53e80bf974e8f047cb4236a77 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 25 Oct 2026 18:03:36 +0800 Subject: [PATCH 10/11] chore: drop the stale refreshDistMtimes declaration (master landed its own restampMtimes in #4599) + regen docs --- CONFIGURATION.zh-CN.md | 12 ++++++++---- scripts/ci-pretest-cache.d.mts | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index e40d1858cf..00263685b2 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -100,7 +100,7 @@ | `rollout.features.percent` | number | the version's baked-in plan | — | 看到该特性的安装占比(0–100,支持小数)。单调:上调比例不会把已入组的安装踢出;每台安装的桶值是前缀稳定的。 | | `rollout.features.salt` | string | the feature name | — | 分桶哈希盐。修改它会重新随机整个分组(唯一的洗牌方式;仅上调 percent 永不洗牌)。 | | `rollout.features.base` | string | unset (roll out over everyone) | — | 嵌套灰度:仅在所指特性的已入组人群内评估本特性,percent 相对该人群(B 只放量给 A 的用户;允许链式,环路判为关)。 | -| `rollout.features.pluginLaneServerHistory` | boolean \| number | 0 (off) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 messages 平行的内部字段 biliMsgIds,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 POST /__bili/plugin/render 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 serverHistory。默认 0%,放量前无行为变化。 | +| `rollout.features.pluginLaneServerHistory` | boolean \| number | 0(关) | BILI_ROLLOUT_PLUGIN_LANE_SERVER_HISTORY | 插件车道服务端正史(#4491):对已放量的安装,(a) 插件车道请求可携带与 `messages` 平行的内部字段 `biliMsgIds`,把消息身份钉在客户端提供的 id 上(转发上游前一律剥掉;格式错误的数组告警后忽略,身份投毒形态在增量车道直接硬失败);(b) 仅回环可达的 `POST /__bili/plugin/render` 端点从统一存储渲染出站前缀(折叠区收敛为摘要锚点;任何失步都返回非 2xx,shim 退回本地历史);(c) 增量车道探测响应通告 `serverHistory`。默认 0%,放量前无行为变化。 | | `maskHosts` | boolean | true | BILI_LOG_MASK_HOSTS | 日志中把非公开目标主机遮成 ;凭据头无论此开关如何总是遮盖。 | | `security.adminRelay` | boolean | false | — | 允许宿主(当前为 dsh web)把它自己入口域名下的整个 /__bili/ 管理面中继到本机代理,使远程浏览器能打开面板(#3373)。默认关闭、仅配置文件。中继跳到达代理时是本机非浏览器客户端(回环源地址 + 回环 Host、无 Origin 头),管理端门槛不变;开启后,凡能访问宿主 web 入口的人都能改配置、写凭据——请先给入口加鉴权/反代。 | | `subagentSplit` | boolean | true | BILI_SUBAGENT_SPLIT | Claude 子代理拥有独立会话命名空间(\|sub:),不再排队在主会话锁后。 | @@ -230,10 +230,10 @@ | Key | Type | Default | Env | Description | |-----|------|---------|-----|-------------| | `diagnostics` | object | {} (all defaults) | — | 本地排障旋钮(转储、渲染/注入开关、count-tokens 透传)。 | -| `diagnostics.dumpBody` | boolean | false | ACP_DUMP_BODY | 把完整请求/响应体转储到日志用于排障。 | -| `diagnostics.dumpReq` | boolean | true | ACP_DUMP_REQ | 逐次调用记录发往上游的请求元数据。 | +| `diagnostics.dumpBody` | boolean | false | ACP_DUMP_BODY | 把完整请求/响应体转储到日志用于排障。转储内容为明文,可能包含凭据——分享前必须清洗(#4482)。 | +| `diagnostics.dumpReq` | boolean | true | ACP_DUMP_REQ | 逐次调用记录发往上游的请求元数据;开启 dumpBody 时同时是明文请求体转储的闸门(分享前必须清洗,#4482)。 | | `diagnostics.rawDumpDir` | string | /raw | ACP_RAW_DUMP_DIR | 原始线路转储目录。 | -| `diagnostics.dump4xx` | boolean | false | BILI_DUMP_4XX | 把上游 4xx 响应落盘以便事后检查。 | +| `diagnostics.dump4xx` | boolean | false | BILI_DUMP_4XX | 把上游 4xx 响应落盘以便事后检查。转储内容为明文,可能包含凭据——分享前必须清洗(#4482)。 | | `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | 单个 4xx 转储文件的大小上限。 | | `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | 调试转储目录(dumps/、raw/、SSE dump 目录)的总大小上限;超限时按最旧优先删除(下限 1 MiB)。转储默认无任何轮转,持续捕获下每天可增长数十 GB(#2412)。 | | `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | 调试转储目录的保留天数上限;超过该天数的文件被删除(下限 1 小时)。默认关闭。 | @@ -267,6 +267,8 @@ | `compress.preserveRecentMessages` | number | 3 | — | 最近的消息软保护、免于折叠。 | | `compress.preserveRecentTokens` | number | kernel ≈5000 | — | 最近的 token 软保护、免于折叠。 | | `compress.minCompressRangeChars` | number (deprecated alias: minCompressRange) | kernel ≈5000 | — | 可折叠片段的最小字符数;更短的永不折叠。 | +| `compress.maxSummaryLength` | number | kernel 20000 | — | 单条生成摘要的硬字符上限;超长的回复被整段拒绝(不截断、不重试)。外部摘要模型会被提前告知这个确切上限(#3635)。0 = 不限制。 | +| `compress.minSummaryLength` | number | kernel 50 | — | 生成摘要的字符下限;过短的回复被整段拒绝。0 = 禁用。外部摘要模型受同一下限约束(#3635)。 | | `compress.reconcile` | "off" \| "warn" \| "repair" | "repair" | BILI_FOLD_RECONCILE | 客户端在轮次之间回退或改写历史时校准已折叠状态。 | | `compress.promptPack` | string (builtin: "default", "lean") | builtin "default" | — | 压缩提示词包,按 项目包 → 用户包 → 内置 解析;不受 acknowledgePromptsRisk 门控。 | | `compress.tiers` | boolean | true | — | T1→T3 分级蒸馏把折叠成本摊到多代。 | @@ -287,6 +289,7 @@ | `compress.reasoningGuard` | object | off | — | gpt-5.x/6.x 推理格截断自动修复(最多 3 轮继续提示)。 | | `compress.outputSteering` | object { enabled?, verbosityLevel?, effortRouting? } | enabled false · verbosityLevel 2 | — | 向系统提示词尾部附加简洁度指令;只收紧机械延续请求。 | | `compress.priceProfile` | { w?, r?, q? } | unset (registry, kernel fallback {1, 0.1, 4}) | — | w/r/q 相对输入价的比率(写入/缓存读/输出);仅供报告,不影响触发条件或线上行为。 | +| `compress.externalSummary` | object | opt-in (disabled) | — | 把压缩摘要交给 providers 表引用的独立配置模型;三级 compress 均为整链替换语义。 | | `compress.acknowledgePromptsRisk` | boolean | false | — | 必须先置 true,自定义 prompts 才会生效。 | | `compress.absorb.enabled` | boolean | false | — | 启用 absorb 蒸馏块。 | | `compress.absorb.minToolTokens` | number | 1000 | — | 可被吸收的工具结果的最小估算 token 大小。 | @@ -351,6 +354,7 @@ |-----|------|---------|-----|-------------| | `BILI_DSH_AUTHORED_COMPACTION` | boolean | true (on, #3901 flip) | — | 让 bili 本地应答 dsh 压缩摘要请求(返回折叠快照而非拒绝,#3733):身份头 + 内核渲染格式的块摘要,零出口流量;重放经 #2432 再基线识别。 | | `BILI_DSH_CONTEXT_TAB` | boolean | true | — | dsh.contextTab 的 env 层(#4237):0/false 移除 DSH 顶栏「上下文」标签页(其余一切照常工作);其他取值强制开启并覆盖文件层;缺省 = 按文件层取值(默认 true)。下次页面加载 / dsh 重启生效。 | +| `BILI_DSH_MAX_TESTED_VERSION` | string | 0.2.0-rc.2 (CI e2e anchor) | — | “dsh native lane 启动提示的“我们测过的最高版本”上限(#4545):从注入包上方最近的 node_modules/@deepseek-ai/dsh/package.json 读出运行中的构建,**严格高于**上限才弹一条启动提示(持久日志 + 横幅),建议先回滚到上限版本。只覆盖"更新"一个方向:≤ 上限保持静默(旧版已知坏法有专门的失锁探测)。只提示、绝不拦截或阻断流量;新版本验证通过后抬高上限,空字符串 = 关闭本 lane 检查;读不到/解析不了的版本按静默处理。” | | `BILI_STORAGE_BACKEND` | sqlite\|mysql | sqlite | — | storage.backend 的环境变量层(#3931):sqlite(默认)或 mysql(已接线,#3388 第二+三步 —— 保存经异步影子队列入 mysql,同步读走 JSON 信封,replicas/ext/GC 走异步孪生;export 同样支持)。优先于文件层。 | | `BILI_MYSQL_DSN` | string | unset | — | storage.backend="mysql" 的 MySQL DSN(mysql://user:pass@host:3306/db)(#3388,#3931)。优先于文件键 storage.mysql.dsn。留空 = mysql 后端无法打开;文档化错误会列出两个旋钮。连接池惰性打开;closeQuietlyAsync 排空入队事务并关闭。 | | `BILI_STORAGE_ARCHIVE_AFTER_DAYS` | integer | 365 | — | GC 年龄清扫窗口(天,#3463):闲置超过该时长的会话被归档(单文件归档日志 archive/archive.jsonl 追加一行 + 墓碑行保留标签 + 释放内容/媒体引用)。0 关闭年龄清扫;手动入口 POST /__bili/storage/gc 始终可用。配置文件键 storage.archiveAfterDays 为持久形式。 | diff --git a/scripts/ci-pretest-cache.d.mts b/scripts/ci-pretest-cache.d.mts index e49fa052d6..83e96ded50 100644 --- a/scripts/ci-pretest-cache.d.mts +++ b/scripts/ci-pretest-cache.d.mts @@ -14,7 +14,6 @@ export const MARKER_NAME: string; export const COMPONENTS: PretestCacheComponent[]; export function hashComponent(root: string, comp: PretestCacheComponent, platform?: string): string; export function markerIsFresh(distDir: string, hash: string): boolean; -export function refreshDistMtimes(distDir: string, now?: Date): number; export function runPretestCache(opts?: { root?: string; components?: PretestCacheComponent[]; From 024282c178380ab6723a2679667b41dc7bfd7273 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 25 Oct 2026 18:10:30 +0800 Subject: [PATCH 11/11] docs(lifecycle): register BILI_INLINE_DEBUG + update BILI_PI_INLINE row for the #4669 default flip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lifecycle-ledger-guard red: BILI_INLINE_DEBUG was read from src/ but not in the ledger. While here, the BILI_PI_INLINE row still said 默认 off — this stack flips the default, so the row now says so (and the file/env opt-out spelling that gets the spawned server back). --- LIFECYCLE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LIFECYCLE.md b/LIFECYCLE.md index 3c72995136..1b1b796380 100644 --- a/LIFECYCLE.md +++ b/LIFECYCLE.md @@ -48,7 +48,8 @@ | `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(#4669 默认翻转) | pi inline 模式(默认 ON,#4669 起):不起 proxy 进程,fetch 链在 intercept 之下的一个环节把伪 origin 流量路由进进程内引擎(inline-engine.ts,跑同 startServer 的每请求管线);file 家 `"pi": {"inline": false}` 或 `=0/false/no/off` 显式退回 spawn | inline 车道稳定一个版本周期后转 §2 常设 | 默认开启(attach 目标仍压过默认值) | +| `BILI_INLINE_DEBUG` | #4669 | 仅 inline 车道:引擎 stderr 日志腿默认静默(stderr 是 pi 的交互终端,日志会挤掉输入框;默认只写 bili.log),设 1 恢复 stderr 输出用于排查 | 随 inline 车道(#3278)走 | 排查逃生口 | | 测试钩子:`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_* 环境变量)