diff --git a/CONFIGURATION.md b/CONFIGURATION.md index c926e577dc..f0d3b3440c 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -123,6 +123,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. | **MITM lane** @@ -347,6 +348,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_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. | @@ -1670,6 +1672,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_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 02da80ab87..475f7d23f1 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -123,6 +123,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 显式关闭且优先于本文件值。 | **MITM 通道** @@ -347,6 +348,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_NATIVE_ZCODE` | boolean-ish | off | — | 宿主姿态:标记 zcode 原生插件进程。 | | `BILI_PI_BIN` | string | pi (PATH lookup) | — | 宿主姿态:pi 客户端二进制的显式路径。 | | `BILI_DSH_BIN` | string | dsh (PATH lookup) | — | 宿主姿态:dsh 客户端二进制的显式路径。 | @@ -1669,6 +1671,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_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-inline.ts b/src/agent/pi-inline.ts new file mode 100644 index 0000000000..238d3c73d1 --- /dev/null +++ b/src/agent/pi-inline.ts @@ -0,0 +1,72 @@ +// #3278: pi inline lane — innermost fetch-chain link that serves +// INLINE_ORIGIN traffic IN-PROCESS (no spawned proxy). Installed BEFORE +// installNativeFetchIntercept, so the intercept's `/bili/` rewrite +// lands here: model URLs decode to their embedded upstream and run the shared +// pipeline locally; management URLs (/__bili/, /__acp/) take the same pipeline +// through its loopback-gated admin routes. The fetch captured below the link +// is the host's PRE-PATCH fetch — handed to the engine as its upstream +// transport so pipeline egress never re-enters the chain. +import { installFetchChain } from "./fetch-chain/index.js"; +import { getInlineEngine, INLINE_ORIGIN, type InlineEngine } from "../inline-engine.js"; +import { logHostEvent } from "./host-log.js"; + +const MODEL_PREFIX = `${INLINE_ORIGIN}/bili/`; + +let rawSend: typeof globalThis.fetch | undefined; +let installed = false; +let degradeWarned = false; + +function urlOf(input: string | URL | Request): string | undefined { + try { + if (typeof input === "string") return input; + if (input instanceof URL) return input.href; + return input.url; + } catch { + return undefined; + } +} + +/** Install the inline transport as the innermost link of the fetch chain. + * Must run before installNativeFetchIntercept (chain order = install order). */ +export function installInlineTransport(): boolean { + if (installed) return true; + installFetchChain((send) => { + installed = true; + rawSend = send; + return async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = urlOf(input); + if (url === undefined || !url.startsWith(INLINE_ORIGIN)) return send(input, init); + try { + const engine = await getInlineEngine(); + return await engine.dispatch(input, init, rawSend); + } catch (err) { + // Caller-side cancel is not an engine failure — propagate it: + // degrading would consume the one-shot warning and re-send a + // request the caller already abandoned. + if (err instanceof Error && err.name === "AbortError") throw err; + const msg = err instanceof Error ? err.message : String(err); + if (url.startsWith(MODEL_PREFIX)) { + // Engine down → degrade exactly like a spawn-mode bootstrap + // failure: strip the pseudo-origin prefix (the embedded + // upstream URL is absolute) and send direct, uncompressed. + if (!degradeWarned) { + degradeWarned = true; + logHostEvent("error", "[native-client]", `[bili-native] inline engine unavailable — model traffic goes direct (uncompressed): ${msg}`); + } + return await send(url.slice(MODEL_PREFIX.length), init); + } + throw err; + } + }; + }); + return true; +} + +/** Test seam: reset install state so suites can re-install on a fresh chain. */ +export function _resetInlineTransportForTest(): void { + installed = false; + rawSend = undefined; + degradeWarned = false; +} + +export type { InlineEngine }; diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index f184df063b..216590b440 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -10,6 +10,10 @@ // 3. set BILLION_CONTEXT_PROXY so the shared plugin (pi.ts) detects the // proxy through its existing env fallback — tools, headers, /acp and // the compaction cancel all reuse the launcher-mode code paths. +// Inline mode (#3278, BILI_PI_INLINE=1 or "pi": {"inline": true}, default off): NO proxy process at all +// — a fetch-chain link below the intercept routes the pseudo-origin traffic +// into an in-process engine (inline-engine.ts) that runs the same per-request +// pipeline startServer serves over HTTP. // Posture (planNativePi — #886/#983 mechanism, ported to the pi lane in // #1795): a preset BILLION_CONTEXT_PROXY (or explicit BILLION_CONTEXT_ATTACH) // is an ATTACH target, not a stand-down — the pseudo-attach hole used to disarm @@ -27,7 +31,10 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { piPackageStrings } from "../plugin-install.js"; import { createBiliPlugin } from "./pi.js"; import { isLegacyBcpEntry, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; +import { loadConfigFile } from "../config.js"; +import { getInlineEngine, INLINE_ORIGIN } from "../inline-engine.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, type NativeInterceptState } from "./native-intercept.js"; +import { installInlineTransport } from "./pi-inline.js"; import { installNativeWebSocketIntercept } from "./native-ws-intercept.js"; import { fetchProxyVersion, fetchStatus, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; @@ -42,18 +49,25 @@ export function shouldBootstrapNative(env: NodeJS.ProcessEnv): boolean { } /** Decide this process's native posture (#886/#983 mechanism, ported to the pi - * lane in #1795). Precedence kill-switch > /bili/ rewrite launch > attach > - * spawn. A preset BILLION_CONTEXT_PROXY is an ATTACH target, not a stand-down: - * the pseudo-attach hole (preset env + bare pi) used to disarm routing entirely - * while tools still found the proxy through the env — model traffic went direct - * and uncompressed, the proxy never saw the session, and every tool forward - * 404'd. Attach keeps the user's intent ("route through THIS proxy"); explicit - * BILLION_CONTEXT_ATTACH wins when both are set. A /bili/ launch - * (BILI_PROVIDER_REWRITES) still stands us down — its URLs are already - * proxy-shaped. */ -export function planNativePi(env: NodeJS.ProcessEnv): { mode: "off" | "attach" | "spawn"; attachOrigin?: string } { + * lane in #1795). Precedence kill-switch > /bili/ rewrite launch > inline > + * attach > spawn. A preset BILLION_CONTEXT_PROXY is an ATTACH target, not a + * stand-down: the pseudo-attach hole (preset env + bare pi) used to disarm + * routing entirely while tools still found the proxy through the env — model + * traffic went direct and uncompressed, the proxy never saw the session, and + * every tool forward 404'd. Attach keeps the user's intent ("route through + * THIS proxy"); explicit BILLION_CONTEXT_ATTACH wins when both are set. A + * /bili/ launch (BILI_PROVIDER_REWRITES) still stands us down — its URLs are + * already proxy-shaped. BILI_PI_INLINE (#3278) outranks ambient attach envs: + * it is a deliberate choice about WHERE compression runs (in this process), + * not just which proxy to use. Resolved env > file > default(off): the + * config-file home is `"pi": {"inline": true}`; an explicit BILI_PI_INLINE=0/ + * false disables over the file. */ +export function planNativePi(env: NodeJS.ProcessEnv, fileInline = false): { mode: "off" | "attach" | "spawn" | "inline"; attachOrigin?: string } { if (env.BILLION_CONTEXT_PLUGIN === "0" || env.BILI_NATIVE_PI === "0") return { mode: "off" }; if (env.BILI_PROVIDER_REWRITES !== undefined) return { mode: "off" }; + const inlineEnv = env.BILI_PI_INLINE?.trim().toLowerCase(); + const inlineEnvOff = inlineEnv === "0" || inlineEnv === "false"; + if ((inlineEnv === "1" || inlineEnv === "true") || (!inlineEnvOff && fileInline)) return { mode: "inline" }; const attachOrigin = nativeAttachOrigin(env) ?? proxyEnvOrigin(env); if (attachOrigin !== undefined) return { mode: "attach", attachOrigin }; return { mode: "spawn" }; @@ -191,7 +205,7 @@ export async function verifyAttachAndRecover(attachOrigin: string): Promise { + await getInlineEngine(); + return INLINE_ORIGIN; + }); + state.respawn = start; + setNativeOriginWaiter({ wait: () => readyOrigin(state) }); + state.onGiveUp = () => { + delete process.env.BILLION_CONTEXT_PROXY; + state.origin = undefined; + }; + // Production mirrors spawn mode's fail-fast posture (bootstrap at arm + // time); tests stay lazy so nothing starts until one asks for it. + state.ready = process.env.NODE_TEST_CONTEXT === undefined ? start() : Promise.resolve(undefined); } else if (process.env.NODE_TEST_CONTEXT === undefined) { const start = singleFlight(bootstrap); state.respawn = start; @@ -253,11 +289,17 @@ if (plan.mode !== "off") armNativePi(plan); // node:test imports this module for shouldBootstrapNative/nativeProxyScriptPath — // never patch globalThis.fetch or bootstrap a real proxy from inside a test run. if (process.env.NODE_TEST_CONTEXT === undefined && plan.mode !== "off") { + // #3278: chain order is install order — the inline transport must sit + // BELOW the fetch intercept so its `/bili/` rewrite lands on + // this link instead of egressing toward a proxy port that does not exist. + if (plan.mode === "inline") installInlineTransport(); installNativeFetchIntercept(state); // #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. - installNativeWebSocketIntercept(state); + // 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); } /** Test hook: expose the armed runtime-recovery seam (mirrors opencode/dsh). */ @@ -282,6 +324,7 @@ export function _resetNativeStateForTest(): void { state.ready = Promise.resolve(undefined); _spawnForTest = undefined; delete process.env.BILLION_CONTEXT_PROXY; + delete process.env.BILI_PI_INLINE; } export default createBiliPlugin(); diff --git a/src/config.ts b/src/config.ts index 572c55e6c2..e46b4b1af1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1712,8 +1712,12 @@ type FileConfig = { * collision with the top-level proxy `debug`). Boolean shorthand: * `"pi": {"subagents": false}` disables the whole surface. The four * acp.json keys (delegate/delegatePrompt/displayUsage/debug) are a - * deprecated fallback, read only while this section is absent. */ - pi?: { subagents?: PiSubagentsFileConfig | boolean }; + * deprecated fallback, read only while this section is absent. + * `inline` (#3278): run the pi lane's compression IN-PROCESS (no spawned + * proxy) — the config-file home of the BILI_PI_INLINE knob, resolved + * env > file > default(off); BILI_PI_INLINE=0/false explicitly disables + * it over the file. */ + 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 * (ROLLOUT_FEATURES). `installId` pins the bucketing identity instead of diff --git a/src/inline-engine.ts b/src/inline-engine.ts new file mode 100644 index 0000000000..308c5ce00c --- /dev/null +++ b/src/inline-engine.ts @@ -0,0 +1,337 @@ +// #3278: in-process compression engine — the SAME per-request pipeline +// startServer() serves over HTTP, driven directly inside this process. The pi +// 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). +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 { 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"; + +/** Pseudo-origin of the inline lane. Never DNS-resolved: any request reaching + * this host is served in-process by installInlineTransport before egress. */ +export const INLINE_ORIGIN = "http://billion-context.inline"; +const INLINE_HOSTNAME = new URL(INLINE_ORIGIN).hostname; + +type FetchInput = string | URL | Request; +type BodyLike = string | URLSearchParams | Blob | ArrayBuffer | ArrayBufferView | ReadableStream | AsyncIterable; + +export interface InlineEngine { + origin: string; + /** Run one request through the shared pipeline. `rawFetch` is the host's + * pre-patch fetch: it becomes this engine's upstream transport (via + * withFetchTransport) so pipeline egress never re-enters the patched + * fetch chain. */ + dispatch(input: FetchInput, init?: RequestInit, rawFetch?: typeof globalThis.fetch): Promise; +} + +type HeaderPair = [string, string]; + +function normalizeHeaderPairs(value: Record | [string, string][]): HeaderPair[] { + if (Array.isArray(value)) return value.map(([k, v]) => [k, String(v)]); + const out: HeaderPair[] = []; + for (const [k, v] of Object.entries(value)) { + if (v === undefined) continue; + out.push([k, Array.isArray(v) ? v.join(", ") : String(v)]); + } + return out; +} + +/** Hop-by-hop / framing headers that must not ride a reconstructed Response: + * undici recomputes length and framing from the actual stream. */ +const HOP_HEADERS = new Set(["connection", "keep-alive", "proxy-authenticate", "proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade", "host", "content-length"]); + +async function bodyToBuffer(body: BodyLike | null | undefined): Promise { + if (body === null || body === undefined) return Buffer.alloc(0); + if (typeof body === "string") return Buffer.from(body); + if (body instanceof URLSearchParams) return Buffer.from(body.toString()); + if (body instanceof Blob) return Buffer.from(await body.arrayBuffer()); + if (body instanceof ArrayBuffer) return Buffer.from(body); + if (ArrayBuffer.isView(body)) return Buffer.from(body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength)); + const iterator = (body as { [Symbol.asyncIterator]?: unknown })[Symbol.asyncIterator]; + if (typeof iterator === "function") { + const parts: Buffer[] = []; + for await (const chunk of body as AsyncIterable) parts.push(Buffer.from(chunk)); + return Buffer.concat(parts); + } + throw new Error(`inline engine: unsupported request body type ${Object.prototype.toString.call(body)}`); +} + +/** Minimal duck-type of http.IncomingMessage covering everything the pipeline + * reads: url/method/headers/socket identity plus the readBody event surface. + * 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. */ +class InlineReq extends EventEmitter { + url: string; + method: string; + headers: Record = {}; + socket = { localPort: 0, remoteAddress: "127.0.0.1" }; + private chunks: Buffer[]; + private deliveryScheduled = false; + + constructor(url: string, method: string, headers: Record, body: Buffer) { + super(); + this.url = url; + this.method = method; + this.headers = headers; + this.chunks = body.length > 0 ? [body] : []; + } + + override on(event: string, fn: (...args: unknown[]) => void): this { + super.on(event, fn); + if (event === "data" || event === "end" || event === "error") this.scheduleDelivery(); + return this; + } + + destroy(): void {} + + private scheduleDelivery(): void { + if (this.deliveryScheduled) return; + this.deliveryScheduled = true; + queueMicrotask(() => { + for (const c of this.chunks) this.emit("data", c); + this.emit("end"); + }); + } +} + +/** Minimal duck-type of http.ServerResponse: writeHead/write/end plus the + * finish/close events the pipeline arms, and a socket surface (bytesWritten, + * destroyed) read by registerRequestAbort/beginStreamKeepalive/observability. + * The body streams into a web ReadableStream so SSE responses reach the + * caller incrementally instead of arriving in one buffered blob. */ +class InlineRes extends EventEmitter { + statusCode = 200; + statusMessage = ""; + headersSent = false; + writableEnded = false; + destroyed = false; + socket = { localPort: 0, remoteAddress: "127.0.0.1", destroyed: false, bytesWritten: 0 }; + readonly bodyStream: ReadableStream; + headHeaders: HeaderPair[] = []; + private controller: ReadableStreamDefaultController | undefined; + + constructor() { + super(); + this.bodyStream = new ReadableStream({ + start: (c) => { this.controller = c; }, + }); + } + + writeHead(status: number, reasonOrHeaders?: string | Record | [string, string][]): this { + if (this.headersSent) throw new Error("ERR_HTTP_HEADERS_SENT: Cannot change headers after they are sent to the client"); + this.statusCode = status; + if (typeof reasonOrHeaders === "string") this.statusMessage = reasonOrHeaders; + else if (reasonOrHeaders !== undefined) this.headHeaders.push(...normalizeHeaderPairs(reasonOrHeaders)); + this.headersSent = true; + return this; + } + + write(chunk: string | Uint8Array, cb?: () => void): boolean { + if (this.destroyed) { cb?.(); return false; } + const buf = typeof chunk === "string" ? Buffer.from(chunk) : Buffer.from(chunk); + this.socket.bytesWritten += buf.length; + this.controller?.enqueue(buf); + cb?.(); + return true; + } + + end(chunk?: string | Uint8Array, cb?: () => void): void { + if (this.destroyed) { cb?.(); return; } + if (chunk !== undefined) this.write(chunk); + if (!this.writableEnded) { + this.writableEnded = true; + try { this.controller?.close(); } catch { /* stream already errored */ } + this.emit("finish"); + this.emit("close"); + cb?.(); + } + } + + /** Socket-death semantics, mirroring http.ServerResponse: destroy does NOT + * set writableEnded — the pipeline's res "close" listeners use + * `!res.writableEnded` to mean "client vanished mid-stream" and abort the + * upstream round trip (relay's clientAbort). Marking the response ended + * here would look like normal completion and let the upstream run to + * completion, burning tokens after the caller already went away. */ + destroy(err?: Error): void { + if (this.destroyed) return; + this.destroyed = true; + this.socket.destroyed = true; + try { this.controller?.error(err ?? new Error("client aborted")); } catch { /* already closed */ } + if (!this.writableEnded) this.emit("close"); + } +} + +async function toInlineReq(input: FetchInput, init?: RequestInit): Promise { + let url: string; + let baseHeaders: Headers | [string, string][] | Record | undefined; + let requestMethod: string | undefined; + let requestBody: BodyLike | null | undefined; + if (typeof input === "string") url = input; + else if (input instanceof URL) url = input.href; + else { + url = input.url; + baseHeaders = input.headers; + requestMethod = input.method; + requestBody = await input.arrayBuffer().catch(() => null); + } + const headers = new Headers(baseHeaders); + if (init?.headers !== undefined) new Headers(init.headers).forEach((v, k) => headers.set(k, v)); + const headerMap: Record = {}; + headers.forEach((value, key) => { + const existing = headerMap[key]; + headerMap[key] = existing === undefined ? value : Array.isArray(existing) ? [...existing, value] : [existing, value]; + }); + const method = (init?.method ?? requestMethod ?? "GET").toUpperCase(); + const bodySource = init?.body !== undefined ? init.body as BodyLike | null : requestBody; + const body = await bodyToBuffer(bodySource); + // The pipeline reads req.url as an ORIGIN-FORM request target ("/bili/…" — + // what node:http hands a server for a normal connection). An absolute-form + // URL would fall into resolveUpstream's forward-proxy branch and be routed + // to its own authority — the pseudo-origin — instead of the embedded host. + const parsed = new URL(url); + // Pseudo-origin traffic is same-process by definition — present the + // loopback identity so the admin surface's DNS-rebinding Host gate passes + // without weakening the gate itself. Other origins keep their real host. + if (headerMap["host"] === undefined) headerMap["host"] = parsed.hostname === INLINE_HOSTNAME ? "127.0.0.1" : parsed.host; + return new InlineReq(parsed.pathname + parsed.search, method, headerMap, body); +} + +function responseFrom(res: InlineRes): Response { + const headers = new Headers(); + for (const [k, v] of res.headHeaders) { + if (HOP_HEADERS.has(k.toLowerCase())) continue; + headers.append(k, v); + } + return new Response(res.bodyStream, { status: res.statusCode, statusText: res.statusMessage || undefined, headers }); +} + +function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] { + return async (input, init, rawFetch) => { + const req = await toInlineReq(input, init); + const res = new InlineRes(); + // Spawn-mode cancel parity (#3278 review): honor the caller's + // AbortSignal. Destroying res drives the pipeline's own close plumbing + // (handle()'s decodingAbort, relay's clientAbort) exactly like a real + // socket close does over HTTP, stopping the upstream round trip; the + // raced promise makes the caller reject with the abort reason like + // undici would instead of running to completion and burning tokens. + const signal = init?.signal; + let onAbort: (() => void) | undefined; + let abortPromise: Promise | undefined; + if (signal != null) { + abortPromise = new Promise((_, reject) => { + onAbort = () => { + try { res.destroy(new Error("client aborted")); } catch { /* already closed */ } + reject(signal.reason ?? new DOMException("The operation was aborted.", "AbortError")); + }; + if (signal.aborted) onAbort(); + else signal.addEventListener("abort", onAbort, { once: true }); + }); + void abortPromise.catch(() => { /* consumed by the race below when the abort wins */ }); + } + try { + // The whole dispatch runs under the host's PRE-PATCH fetch: every + // upstream call the pipeline makes (forward, count-tokens, + // registry…) takes this transport instead of globalThis.fetch, + // which in the inline lane is bili's own patch chain — following + // it would rewrite the upstream URL back onto the pseudo-origin + // and loop forever. + const transport = (url: string, options: FetchOptions): Promise => (rawFetch ?? globalThis.fetch)(url, options as RequestInit); + const pipeline = withFetchTransport( + transport, + () => handle(req as unknown as http.IncomingMessage, res as unknown as http.ServerResponse, ctx.opts, ctx.core, ctx.config, ctx.log, ctx.instanceId, ctx.instanceStartedAt, ctx.proxyWatchers, ctx.initialWatcherPid, ctx.adminCtx), + ); + if (abortPromise !== undefined) await Promise.race([pipeline, abortPromise]); + else await pipeline; + } catch (err) { + // Mirror of startServer()'s dispatch catch (server.ts): same + // client-abort classification, same error shape on the wire. + const msg = String(err); + const e = err as { name?: string; message?: string }; + const clientAbort = (e?.name === "AbortError" || /abort/i.test(String(e?.message ?? ""))) && (res.destroyed || res.writableEnded); + if (clientAbort) ctx.log("info", `client aborted mid-stream: ${msg}`); + 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); + // A destroyed res belongs to an aborted caller whose promise + // carries the abort reason — the body stream is errored, so writing + // an error frame here would throw on the dead controller; rethrow + // so the caller sees the abort instead of a phantom response. + if (res.destroyed) throw err; + 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(); + } + } finally { + if (signal != null && onAbort != null) signal.removeEventListener("abort", onAbort); + } + if (!res.writableEnded) res.end(); + return responseFrom(res); + }; +} + +let engineFactory: () => Promise = startInlineEngine; + +/** Test seam: replace the bootstrap (fake engine) without touching config or + * storage. Pass undefined to restore. */ +export function _setEngineFactoryForTest(fn: (() => Promise) | undefined): void { + engineFactory = fn ?? startInlineEngine; +} + +let enginePromise: Promise | undefined; +let failedAt = 0; +const RETRY_COOLDOWN_MS = 30_000; + +/** Memoized engine startup. A failed bootstrap stays cached for + * RETRY_COOLDOWN_MS so a broken environment degrades fast instead of + * re-attempting (and re-logging) on every model request; after the cooldown + * the next request retries once. */ +export function getInlineEngine(): Promise { + if (enginePromise !== undefined) { + if (failedAt !== 0 && Date.now() - failedAt >= RETRY_COOLDOWN_MS) enginePromise = undefined; + else return enginePromise; + } + enginePromise = engineFactory().catch((err) => { + failedAt = Date.now(); + loggerLog("error", `[inline] compression engine bootstrap failed (retry in ${RETRY_COOLDOWN_MS / 1000}s): ${String(err)}`); + throw err; + }); + return enginePromise; +} + +/** Test seam: drop the memo so the next getInlineEngine() restarts. */ +export function _resetInlineEngineForTest(): void { + enginePromise = undefined; + failedAt = 0; +} + +async function startInlineEngine(): Promise { + // Same pre-flight as the CLI server path (cli.ts `start`): template + // config, resolved options, sqlite engine self-bootstrap (#2833). + ensureConfigTemplate(); + const opts = loadOptions(); + const bootstrapOutcome = await ensureSqliteEngine({ + resolveProxy: (url) => resolveProxy(opts.routes, opts.proxy, url, opts.proxyFallback), + }); + recordEngineBootstrap(bootstrapOutcome); + const ctx = await createEngineContext(opts); + // No bind/announce step exists inline — ready the moment context is up. + ctx.markBootReady(); + ctx.log("info", `[inline] compression engine running in-process (${INLINE_ORIGIN}) — no proxy spawned`); + return { origin: INLINE_ORIGIN, dispatch: makeDispatcher(ctx) }; +} diff --git a/src/server.ts b/src/server.ts index 77684f32ac..c91002d970 100644 --- a/src/server.ts +++ b/src/server.ts @@ -436,7 +436,28 @@ export function restartForceCloseOf(server: http.Server): (() => number) | undef return (server as RestartCapableServer)[RESTART_FORCE_CLOSE_KEY]; } -export async function startServer(opts: ProxyOptions): Promise { +/** #3278: the pre-traffic half of startServer() — everything that runs before + * a socket exists (tee logger, kernel core, instance identity, watcher seed, + * session reload / lineage reconcile / GC timer, registry prefetch, admin ctx). + * The HTTP listener is ONE consumer of this context; the in-process inline + * engine (#3278) builds the SAME context without a socket, so both lanes share + * identical session/persist/logger setup. */ +export interface EngineContext { + opts: ProxyOptions; + core: CompressionCore; + config: Config; + log: (level: string, msg: string) => void; + instanceId: string; + instanceStartedAt: number; + proxyWatchers: Set; + initialWatcherPid: number | null; + /** #2941 health gate — the owning lane flips this once ITS announce step is done. */ + markBootReady(): void; + bootReady(): boolean; + adminCtx: Parameters[2]; +} + +export async function createEngineContext(opts: ProxyOptions): Promise { // Configure the tee logger (file + stderr) BEFORE any logging so the very // first line (persist status) lands in the file too. const filePath = configureLogger(opts.logFile ?? defaultLogFile()); @@ -525,6 +546,24 @@ export async function startServer(opts: ProxyOptions): Promise { // per-model config. A miss falls back to the prefix table + default. void loadRegistry(); const adminCtx = { opts, core, config, log, instanceId, instanceStartedAt, proxyWatchers, initialWatcherPid, bootReady: () => bootReady }; + return { + opts, + core, + config, + log, + instanceId, + instanceStartedAt, + proxyWatchers, + initialWatcherPid, + markBootReady: () => { bootReady = true; }, + bootReady: () => bootReady, + adminCtx, + }; +} + +export async function startServer(opts: ProxyOptions): Promise { + const engine = await createEngineContext(opts); + const { core, config, log, instanceId, instanceStartedAt, proxyWatchers, initialWatcherPid, adminCtx } = engine; const dispatch = async (req: http.IncomingMessage, res: http.ServerResponse): Promise => { armRequestWatchdog(req, res, log); @@ -1118,7 +1157,7 @@ export async function startServer(opts: ProxyOptions): Promise { } // #2941: LAST statement — registration above is done; degraded boots // (best-effort write failed) still flip ready, serving as before. - bootReady = true; + engine.markBootReady(); }; // #2941: after a successful bind, verify we are the SOLE owner of the // port before registering. Where socket semantics tolerate a concurrent diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts new file mode 100644 index 0000000000..6e5a83c054 --- /dev/null +++ b/tests/pi-inline.test.ts @@ -0,0 +1,238 @@ +// #3278: pi inline lane — the no-server mode. Two layers under test: +// 1. the fetch-chain transport link (pi-inline.ts): pseudo-origin traffic is +// served in-process, foreign traffic passes through, engine failure +// degrades model traffic to direct upstream while management calls fail loud; +// 2. the in-process engine itself (inline-engine.ts): the real pipeline boots +// without any proxy process, serves model + admin traffic against a local +// mock upstream, makes exactly ONE upstream round trip per request (loop +// safety), and tracks the session in-process. +import { after, 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 http from "node:http"; +import type { AddressInfo } from "node:net"; + +// Hermetic storage BEFORE anything store-touching loads (same pattern as +// opencode-native.test.ts): the real engine boot writes sessions/logs here. +// All THREE XDG homes: sessions live under XDG_DATA_HOME, the conversation +// routing table under XDG_STATE_HOME — a missing one leaks debounced writes +// into the real store after env restore. +const STATE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pi-inline-state-")); +const DATA_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pi-inline-data-")); +const CONFIG_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pi-inline-config-")); +const PREV_STATE = process.env.XDG_STATE_HOME; +const PREV_DATA = process.env.XDG_DATA_HOME; +const PREV_CONFIG = process.env.XDG_CONFIG_HOME; +process.env.XDG_STATE_HOME = STATE_ROOT; +process.env.XDG_DATA_HOME = DATA_ROOT; +process.env.XDG_CONFIG_HOME = CONFIG_ROOT; +after(async () => { + // Force the debounced writers out while the hermetic dirs are still bound. + try { + const { flushConversations } = await import("../src/plugin.ts"); + const { flushAllSessions } = await import("../src/session.ts"); + flushConversations(); + await flushAllSessions(); + } catch { /* engine never booted in this run */ } + if (PREV_STATE === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = PREV_STATE; + if (PREV_DATA === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = PREV_DATA; + if (PREV_CONFIG === undefined) delete process.env.XDG_CONFIG_HOME; + else process.env.XDG_CONFIG_HOME = PREV_CONFIG; + fs.rmSync(STATE_ROOT, { recursive: true, force: true }); + fs.rmSync(DATA_ROOT, { recursive: true, force: true }); + fs.rmSync(CONFIG_ROOT, { recursive: true, force: true }); +}); + +const hits: string[] = []; +let slowCompleted = false; +const upstream = http.createServer((req, res) => { + let body = ""; + req.on("data", (c) => (body += c)); + req.on("end", () => { + hits.push(`${req.method} ${req.url}`); + if (req.url === "/v1/messages") { + const reply = () => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "msg_1", + type: "message", + role: "assistant", + model: "test-model", + content: [{ type: "text", text: "hello-inline" }], + stop_reason: "end_turn", + stop_sequence: null, + usage: { input_tokens: 10, output_tokens: 5 }, + })); + }; + if (body.includes("slow-please")) { + // Cancel-parity probe (#3278 review): a deliberately slow + // completion on the real model lane — if the in-process round + // trip is truly aborted, this never fires. + setTimeout(() => { + if (res.destroyed) return; + slowCompleted = true; + reply(); + }, 2500); + } else { + reply(); + } + } else { + res.writeHead(404, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "not found" })); + } + }); +}); +await new Promise((r) => upstream.listen(0, "127.0.0.1", r)); +const upstreamOrigin = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}`; +after(() => { + upstream.closeAllConnections(); + upstream.close(); +}); + +function withTimeout(p: Promise, ms: number, label: string): Promise { + return Promise.race([ + p, + new Promise((_, rej) => { + const t = setTimeout(() => rej(new Error(`timeout: ${label}`)), ms); + t.unref?.(); + }), + ]); +} + +async function freshChain() { + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + return piInline; +} + +test("#3278 transport: pseudo-origin URLs are served in-process, foreign URLs pass through", async () => { + const ie = await import("../src/inline-engine.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"); + }, + })); + try { + await freshChain(); + const r = await withTimeout(fetch(`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`), 10_000, "inline dispatch"); + assert.equal(r.status, 200); + assert.equal(await r.text(), "inline-ok"); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`]); + // non-pseudo-origin traffic must reach its real destination untouched + const before = hits.length; + const p = await withTimeout(fetch(`${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "passthrough"); + assert.equal(p.status, 200); + assert.deepEqual(hits.slice(before), ["POST /v1/messages"]); + } finally { + const piInline = await import("../src/agent/pi-inline.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + piInline._resetInlineTransportForTest(); + fc.resetFetchChainForTest(); + 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 () => { + throw new Error("boom"); + }); + try { + await freshChain(); + const before = hits.length; + // prefix stripped → the request lands on the REAL upstream, uncompressed + const r = await withTimeout(fetch(`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "degrade"); + assert.equal(r.status, 200); + assert.match(await r.text(), /hello-inline/); + assert.deepEqual(hits.slice(before), ["POST /v1/messages"]); + // management traffic has no direct meaning — it must reject, not wander + await assert.rejects(withTimeout(fetch(`${ie.INLINE_ORIGIN}/__bili/health`), 10_000, "mgmt reject")); + } finally { + const piInline = await import("../src/agent/pi-inline.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + piInline._resetInlineTransportForTest(); + fc.resetFetchChainForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + +test("#3278 engine: real pipeline runs in-process — one upstream round trip, session tracked, admin reachable", { timeout: 90_000 }, async () => { + const ie = await import("../src/inline-engine.ts"); + const { listSessions } = await import("../src/session.ts"); + const before = hits.length; + const engine = await withTimeout(ie.getInlineEngine(), 60_000, "engine boot"); + assert.equal(engine.origin, ie.INLINE_ORIGIN); + // max_tokens > 200 keeps this on the main model lane (processTurn), not + // the side-request passthrough lane. + 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-e2e-session-1", + }, + body, + }, + globalThis.fetch, + ), + 30_000, + "model dispatch", + ); + assert.equal(r.status, 200); + const j = (await r.json()) as { content: { text: string }[] }; + assert.equal(j.content[0].text, "hello-inline"); + // loop safety: exactly ONE upstream round trip — a re-entrant rewrite into + // the patched fetch would double the hit count or hang until timeout + assert.deepEqual(hits.slice(before), ["POST /v1/messages"]); + // compression bookkeeping ran in this process + assert.ok(listSessions().length >= 1, `expected an in-process session, got ${listSessions().length}`); + // the loopback-gated admin surface answers through the pseudo-origin + const h = await withTimeout(engine.dispatch(`${ie.INLINE_ORIGIN}/__bili/health`, {}, globalThis.fetch), 10_000, "health"); + assert.equal(h.status, 200); +}); + +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"); + const ac = new AbortController(); + const settled = engine.dispatch( + // real model lane (max_tokens > 200): the slow-please marker makes the + // mock hold the response 2.5s so the abort lands mid-round-trip + `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, + { + method: "POST", + headers: { + "content-type": "application/json", + "x-bili-plugin": "pi", + "x-bili-plugin-conversation": "inline-abort-session", + }, + body: JSON.stringify({ model: "test-model", max_tokens: 1024, messages: [{ role: "user", content: "hi slow-please" }] }), + signal: ac.signal, + }, + globalThis.fetch, + ).then( + (r) => { throw new Error(`expected abort rejection, got HTTP ${r.status}`); }, + (e: unknown) => e, + ); + setTimeout(() => ac.abort(), 150); + const err = await withTimeout(settled, 10_000, "abort propagation") as Error; + assert.equal(err.name, "AbortError"); + await new Promise((r) => setTimeout(r, 300)); + assert.equal(slowCompleted, false, "upstream round trip must not complete after the caller aborted"); +}); diff --git a/tests/pi-native.test.ts b/tests/pi-native.test.ts index 523d5cbb5c..71c061c356 100644 --- a/tests/pi-native.test.ts +++ b/tests/pi-native.test.ts @@ -7,6 +7,7 @@ import { pathToFileURL } from "node:url"; import { createServer } from "node:http"; import type { AddressInfo } from "node:net"; import { shouldBootstrapNative, nativeProxyScriptPath, singleFlight, isLegacyBcpEntry, legacyBcpEntriesIn, planNativePi, armNativePi, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest } from "../src/agent/pi-native.ts"; +import { INLINE_ORIGIN } from "../src/inline-engine.ts"; import { ensureProxyRunning, type SpawnChild, type SpawnFn } from "../src/launcher.ts"; // #1135 wiring tests drive verifyAttachAndRecover against a dead target — cap @@ -160,6 +161,64 @@ test("planNativePi: explicit BILLION_CONTEXT_ATTACH wins over the env preset", ( ); }); +test("#3278: BILI_PI_INLINE plans the no-server lane and outranks ambient attach envs", () => { + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "1" }), { mode: "inline" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "true" }), { mode: "inline" }); + // explicit opt-in beats a preset proxy / explicit attach — it decides WHERE + // compression runs, not which proxy to use + assert.deepEqual( + planNativePi({ BILI_PI_INLINE: "1", BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }), + { mode: "inline" }, + ); + assert.deepEqual( + planNativePi({ BILI_PI_INLINE: "1", BILLION_CONTEXT_ATTACH: "http://10.0.0.5:9000" }), + { mode: "inline" }, + ); + // kill switches and /bili/ launches still win over everything + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "1", BILLION_CONTEXT_PLUGIN: "0" }), { mode: "off" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "1", BILI_NATIVE_PI: "0" }), { mode: "off" }); + 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" }); +}); + +test("#3278: pi.inline (config file) enables the lane; env resolves over it", () => { + // file value alone enables; it also outranks ambient attach envs like the env does + assert.deepEqual(planNativePi({}, true), { mode: "inline" }); + assert.deepEqual( + planNativePi({ BILLION_CONTEXT_PROXY: "http://127.0.0.1:36485" }, true), + { mode: "inline" }, + ); + // explicit env off disables over the file + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "0" }, true), { mode: "spawn" }); + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "false" }, true), { mode: "spawn" }); + // explicit env on enables over a false file value + assert.deepEqual(planNativePi({ BILI_PI_INLINE: "1" }, false), { mode: "inline" }); + assert.deepEqual(planNativePi({}, false), { mode: "spawn" }); + // kill switches and /bili/ launches still win over the file + assert.deepEqual(planNativePi({ BILLION_CONTEXT_PLUGIN: "0" }, true), { mode: "off" }); + assert.deepEqual(planNativePi({ BILI_NATIVE_PI: "0" }, true), { mode: "off" }); + assert.deepEqual(planNativePi({ BILI_PROVIDER_REWRITES: "{}" }, true), { mode: "off" }); +}); + +test("#3278: armNativePi(inline) freezes the pseudo-origin synchronously without spawning", () => { + const savedProxy = process.env.BILLION_CONTEXT_PROXY; + try { + _resetNativeStateForTest(); + armNativePi({ mode: "inline" }); + assert.equal(process.env.BILLION_CONTEXT_PROXY, INLINE_ORIGIN); + assert.equal(process.env.BILLION_CONTEXT_NATIVE, "pi"); + // armed but LAZY under NODE_TEST_CONTEXT — invoking it here would + // bootstrap a real engine against the ambient XDG dirs + assert.equal(typeof _stateRespawnForTest(), "function", "respawn seam armed"); + } finally { + _resetNativeStateForTest(); + if (savedProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = savedProxy; + } +}); + test("#1135 wiring: a dead attach target falls back to a spawned proxy", async () => { const savedProxy = process.env.BILLION_CONTEXT_PROXY; try { diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index 7b4d0c7b4e..97857b2964 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -282,6 +282,14 @@ entries: group: envonly desc_en: "Host posture: marks a pi-native plugin process." desc_zh: "宿主姿态:标记 pi 原生插件进程。" + - key: BILI_PI_INLINE + type: boolean-ish + default: "off" + 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 附着目标。" - key: BILI_NATIVE_ZCODE type: boolean-ish default: "off" diff --git a/website/config-reference/server.yaml b/website/config-reference/server.yaml index ba6a029432..9c46aba1d8 100644 --- a/website/config-reference/server.yaml +++ b/website/config-reference/server.yaml @@ -388,6 +388,13 @@ entries: group: behavior desc_en: "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." 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" + 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 显式关闭且优先于本文件值。" - key: mitm.enabled type: boolean default: "true"