diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 471913adc4..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. 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 | 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,8 @@ 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 | 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. | | `BILI_DSH_BIN` | string | dsh (PATH lookup) | — | Host posture: explicit binary path for the dsh client binary. | @@ -1826,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 77c116c811..00263685b2 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -136,7 +136,7 @@ | `native.attachUrl` | string | unset (discover per lane) | BILI_MCP_PROXY | native hook 附着的显式目标源(任意 URL)。设置后,hook 只附着到该精确 origin 而不是自动发现,若其不在线则自行派生;仍需代码兼容并通过生命周期门禁。BILI_MCP_PROXY 环境变量(CLI --origin 亦会设置它)优先于此文件值;空/空白视为未设置。 | | `claude.nativePort` | number | unset (lane sticky zone port) | BILI_CLAUDE_NATIVE_PORT | claude 原生车道钩子派生代理的精确端口钉死(严格端口:被占用时响亮拒绝而非跳端口)。 | | `pi.subagents` | object \| boolean | {} (acp_delegate enabled with package defaults) | PI_ACP_DELEGATE_FORCE_ENABLE, PI_ACP_DELEGATE_MAX_DEPTH, PI_ACP_DELEGATE_SYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_IDLE_TIMEOUT_MINUTES, PI_ACP_DELEGATE_ASYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_MAX_CONCURRENT | 内置 pi lane 子代理(acp_delegate)面(#2230 配置搬家)。billion-context.json 的 pi.subagents 段拥有 delegate 配置;acp.json 四键(delegate/delegatePrompt/displayUsage/debug)为已废弃回退源,仅段缺失时读取。prompt 取代 delegatePrompt;debug 限定子代理子系统。布尔简写 subagents: false 整体关闭。完整字段表见 CONFIGURATION.md 的 pi 段。 | -| `pi.inline` | boolean | false | BILI_PI_INLINE | pi 内联压缩通道(#3278):pi 扩展在进程内直接压缩——不派生代理进程、无临时端口;模型流量与 /__bili//__acp 工具面由进程内引擎经伪源站服务。解析顺序 env > 文件 > 默认(off):BILI_PI_INLINE=1/true 开启,BILI_PI_INLINE=0/false 显式关闭且优先于本文件值。 | +| `pi.inline` | boolean | 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 通道** @@ -396,7 +396,8 @@ | `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 | 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 客户端二进制的显式路径。 | | `BILI_DSH_BIN` | string | dsh (PATH lookup) | — | 宿主姿态:dsh 客户端二进制的显式路径。 | @@ -1388,20 +1389,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` @@ -1823,7 +1810,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 | @@ -1912,7 +1899,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 +1979,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 +2111,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 +2181,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 +2209,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/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_* 环境变量) 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/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..a916d8aa16 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -62,14 +62,24 @@ 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(); - 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; + // 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" }; } @@ -219,7 +229,7 @@ export async function verifyAttachAndRecover(attachOrigin: string): Promise { 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 +329,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/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/config.ts b/src/config.ts index 077ec0286b..e40b821f7d 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"). @@ -1898,10 +1903,12 @@ type FileConfig = { * `"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. - * `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. */ + * `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/src/inline-engine.ts b/src/inline-engine.ts index a332e65a5c..2e53cb65b1 100644 --- a/src/inline-engine.ts +++ b/src/inline-engine.ts @@ -3,20 +3,23 @@ // 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"; 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 @@ -34,6 +37,20 @@ 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; + /** 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]; @@ -73,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; @@ -118,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; @@ -292,6 +318,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 @@ -325,13 +377,36 @@ 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 { + // 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(); 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), }); @@ -340,5 +415,58 @@ 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; + }); + // 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/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/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/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/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/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); + } +}); diff --git a/tests/pi-inline-ws.test.ts b/tests/pi-inline-ws.test.ts new file mode 100644 index 0000000000..03b04e1657 --- /dev/null +++ b/tests/pi-inline-ws.test.ts @@ -0,0 +1,262 @@ +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"); + // #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-inline.test.ts b/tests/pi-inline.test.ts index 6e5a83c054..00b175a4ff 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -121,6 +121,9 @@ 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), + webOrigin: Promise.resolve(undefined), })); try { await freshChain(); @@ -143,6 +146,64 @@ 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), + webOrigin: 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 () => { @@ -207,6 +268,81 @@ 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("#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/`)); + // 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(); + } +}); + 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/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 69e2d89682..fd96e9291a 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -355,13 +355,21 @@ entries: desc_en: "Host posture: marks a pi-native plugin process." desc_zh: "宿主姿态:标记 pi 原生插件进程。" - key: BILI_PI_INLINE + type: boolean-ish + 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. 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" 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: "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" diff --git a/website/config-reference/server.yaml b/website/config-reference/server.yaml index 114ff39d5b..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. 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; 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"