Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down Expand Up @@ -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. |
Expand Down Expand Up @@ -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 |
Expand Down
3 changes: 3 additions & 0 deletions CONFIGURATION.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 通道**

Expand Down Expand Up @@ -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 客户端二进制的显式路径。 |
Expand Down Expand Up @@ -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 |
Expand Down
72 changes: 72 additions & 0 deletions src/agent/pi-inline.ts
Original file line number Diff line number Diff line change
@@ -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/<upstream>` 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<Response> => {
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 };
67 changes: 55 additions & 12 deletions src/agent/pi-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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" };
Expand Down Expand Up @@ -191,7 +205,7 @@ export async function verifyAttachAndRecover(attachOrigin: string): Promise<stri
return landed;
}

const plan = planNativePi(process.env);
const plan = planNativePi(process.env, loadConfigFile().pi?.inline === true);

/** Wire the shared intercept state for the planned mode. Attach mode arms
* even under NODE_TEST_CONTEXT (it installs no global patches and spawns
Expand Down Expand Up @@ -233,6 +247,28 @@ export function armNativePi(p: typeof plan): void {
} else {
state.ready = Promise.resolve(undefined);
}
} else if (p.mode === "inline") {
// #3278: no proxy process exists. The pseudo-origin is frozen
// SYNCHRONOUSLY so readyOrigin() short-circuits per request and no
// health probe can ever target a host that never listens; the fetch
// link installed below routes its traffic into the in-process engine.
state.attach = false;
state.origin = INLINE_ORIGIN;
process.env.BILLION_CONTEXT_PROXY = INLINE_ORIGIN;
warnLegacyBcpCoResident();
const start = singleFlight(async () => {
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;
Expand All @@ -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/<upstream>` 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). */
Expand All @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Loading