Skip to content

fix: native lanes reuse the shared proxy instead of spawning one per process (#3436) - #3540

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-17_native-attach-reuse
Oct 18, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-10-17_native-attach-reuse

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

Closes #3436

What & why

Native lanes spawned one proxy per process instead of reusing the shared instance — instances grew without bound and ports drifted (+1 each). Root cause: instanceCompatible compares attach-side opts against what the running proxy records, but native bootstraps left mitmDomains / modelWindows / modelMaxOutputs unset while the child records resolved values, so every live candidate failed and each host process spawned its own proxy (user log: another bili instance ×103, 15 instances in 1.5h).

Fix

The compatibility check now wants, for each tier, exactly what the spawned child records — resolved centrally in instanceCompatible, so every attach path (main probe / starter-poll / squatter check / findLiveAttachableInstance) and both native and CLI lanes are covered by one change, with no per-call-site edits:

  • mitm tier — resolveRecordedMitmDomains(passed?, env) (src/config.ts): file-first / env-second union, gated on BILI_MITM / config.mitm.enabled. A disabled mitm records [] and wants [], so configured-but-disabled domains no longer cause a permanent mismatch.
  • model windows / max-outputs — recordedModelMap(passed, env.BILI_LAUNCHER_MODEL_*, parser) (src/launcher.ts): an explicit non-empty caller value wins; otherwise the value is parsed from the same ambient env a spawned child inherits.

Native bootstraps therefore need no changes (they pass nothing, and the check resolves the recorded tier from env); the kimi config.toml path is handled explicitly.

Zero runtime change: children already inherit and use these env values (stripInheritedProxy strips only http/https/all/no_proxy, not the BILI_* vars). Only the attach decision now sees the same values the proxy actually runs with.

⚠️ Behavior change (old → new)

No new config/env/flag. No wire/message-shape change. No kernel/ touch.

Tests

tests/issue3436-native-proxy-reuse.test.ts covers same-config attach, the caller-env layer, CLI file-first ordering, the disabled-tier []↔[] case, default-empty, and genuine-mismatch spawn; plus kimi-path coverage. Local: typecheck clean; the #3436 suite is green (full-suite verdict left to CI).

查重 / consolidation

Not a duplicate of any open item. This consolidates the parallel work in #3551 (same #3436 root cause): its decision-point mitm resolution is adopted here and extended to the model-window / max-output tiers, which #3551 left open. #3551 is closed as the folded duplicate.

Closes

Closes #3436 · Closes #3557

#3557's two residual facets (CLI-lane config-file mitm tier; kimi cross-path model-windows) are both resolved by this branch's decision-point consolidation (4338f14e) — every attach path funnels through instanceCompatible, which now projects all three cells onto what the spawned child records. Independently verified in #3557.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-17_native-attach-reuse (4338f14)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.4226-pr.3540.31c598e

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr3540.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重:不重复(与 #3308/#3318 的 over-window refusal 机制不同源;#1365/#2924/#2187/#928 相邻但根因不同)。但发现一个必须处理的冲突:#3436 下有两个在途 PR 都声称 Closes #3436——本 PR(#3540,native 调用点补传 compat 字段)和 #3551(改 instanceCompatible 判定表达式,调用点零改动)。按「一 issue 至多一 PR」的循环护栏,两者不能都活。我先独立审完两个方案再给处置建议(合并哪个 / 哪个吸收另一个),结论稍后按八问体例发在本线程。

ranxianglei pushed a commit that referenced this pull request Oct 17, 2026
…e raw union

server.ts records [] when mitm is disabled even with domains configured, but nativeAttachCompat used resolveMitmDomains which ignores the BILI_MITM / file mitm.enabled gate — on such machines every native process still failed the mitm-domains-mismatch cell and spawned its own proxy. New config.resolveEffectiveMitmDomains applies loadOptions' enabled gate on top of the same file-first/env-second union; regression test pins disabled→[] and default-enabled→union.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复(首条回复已述)。结论先行:方向正确,实现成立;我独立验证时找到一个边界缺陷已直接修进本分支(commit 56a191a21),CI 转绿后可合并。同时建议关闭 #3551(理由见下)——两个 PR 都声称 Closes #3436,需要拍板一个。

方向关(审原 issue)

#3436 的根因判断我独立复核后确认成立:attach 判定(instanceCompatible,src/launcher.ts:3605-3621)逐字段比对「调用侧 opts」与「运行中 proxy 记录的字段」,而 10 个 native bootstrap 调 ensureProxyRunning 时三个字段全缺 → attach 侧恒为 []/空,record 侧在配置了 MITM domains / model windows 的机器上非空 → mitm-domains-mismatch cell 永远不匹配 → pickAttachable 跳过 → 每进程 spawn。修的是判据输入缺失这个根因,不是症状层拦截。owner 在 #3436 floor 4 批准的方案 A(native 侧传与 spawn child 相同的值)+ 两点要求(纳入 claude-native-bootstrap、同批对齐 windows/maxOutputs),本 PR 全部覆盖:grep 确认 src 下共 12 处 ensureProxyRunning( 调用点,10 个 native 全部加了 spread,2 个 CLI 车道(src/launcher.ts:4973/:5615)本来就有显式传参、有意不动。

独立验证证据(不是复读 PR 描述)

  • spawn 继承链:stripInheritedProxy(src/launcher.ts:3885-3896)只剥 HTTP(S)_PROXY/ALL_PROXY/no_proxy 一族,BILI_MITM_DOMAINS/BILI_LAUNCHER_MODEL_* 原样进子进程 env;默认 spawn 分支(src/launcher.ts:4231-4245)再把非空的 opts.mitmDomains/opts.modelWindows 覆写回 child env → 同环境 peer 的 record 侧 == attach 侧,PR 的核心主张在该范围内成立。
  • record 侧公式对表:server.ts:1046-1048 记录 mitm.enabled ? mitm.domains : [] + LAUNCHER_MODEL_WINDOWS/MAX_OUTPUTS(模块加载时 parse 自同名 env,src/server/context-window.ts:35/56);helper 的三个输入与之逐项同源(resolveMitmDomains 与 loadOptions 的 mitm.domains 是同一 file∪env 去重表达式,src/config.ts:1449-1455 vs :1475-1480)。
  • 无旁路:所有 attach 路径(pickAttachable :3708、findLiveAttachableInstance :3840、squatter 检查 :4124)都过同一个 instanceCompatible,单判定入口,改调用点输入即全生效。
  • kimi 特例:config.toml 推导的 modelWindows 保持在 spread 之后覆盖(diff 核实),且该值经 spawn env 覆写块进 child → record 侧也是 config.toml 值,同路径收敛 ✓。

发现并已直接修复的缺陷(小问题直接修,commit 56a191a21)

BILI_MITM=0(或文件 mitm.enabled:false)+ 配置了 domains 的机器上,本 PR 原样仍会每进程 spawn。 server 记录的是「enabled 门控后的 tier」——禁用时记 [](server.ts:1046),但原 nativeAttachCompat() 用的 resolveMitmDomains 不看 enabled 门控,返回非空并集 → mismatch 依旧。PR 描述的「attach-side == record-side exactly」在这个边界上为假。修法:新增 config.resolveEffectiveMitmDomains(env)(loadOptions 同款 enabled 门控叠在同一并集上),helper 改用之,加回归测试钉死 disabled→[]、default→union。本地定向:tests/launcher.test.ts 317 pass / 0 fail / 1 pre-existing skip;typecheck clean。该 commit 推到了你的 PR 分支(additive,未动你的提交)。

回归审查(触碰路径的既有行为清点)

既有行为 结论
默认空配置机器(无任何 domains/windows 配置) 保持:before/after 都是 want [] vs record [] → attach,字节级不变
不同配置的进程隔离(lane/host/passthrough/fingerprint/domains/windows/maxOutputs 任一不同 → 各起 proxy,#1225/#1232/#1335 契约) 保持:只补了输入,未放宽任何比对;test 2 钉死不同 domains 不 attach
配置了 MITM/windows 的机器上 native 多进程 有意变更(PR 已披露 old→new:N→1 归并)——披露准确,我复核无误;我的 fix commit 进一步把 disabled-mitm 机器也纳入收敛(old: 恒 spawn → new: attach)
wire/message-shape、kernel/、config surface、persistence、端口语义(zcode/claude strictPort 路径) 不涉及:零触碰,无新 env/config key

测试有效性

三个新测试是真断言:fake deps 驱动真实 ensureProxyRunning,断言 spawnCalls/attached/watcher 注册数,能抓住 attach 机制回归和 helper 契约漂移。诚实声明局限:单测钉不住「10 个 native 模块确实 spread 了 helper」(它们是副作用 bootstrap,import 级不可测)——这层靠本次代码审查逐点核对(10/10)+ CI e2e native 套件兜底,可接受。

diff 整洁度 & 代码质量

12 文件 +109/-19,每一行都在目的内;无空白 churn、无 lockfile/version 变动;命名达意、docstring 带 #3436 出处、无死代码。达标。

⚠️ 与 #3551 的关系(需要 owner 拍板)

#3551 后到,同样声称 Closes #3436,设计不同(判定层修 mitm cell:resolveRecordedMitmDomains 收口进 instanceCompatible)。独立对比:

维度 #3540(本 PR,含我的 fix commit) #3551
owner floor-4 方案 A ✅ 按批准实现 ✅ 等价达成(判定层视角)
floor-4 第 2 点:同批对齐 windows/maxOutputs ✅ 三字段全对齐 ❌ 只动 mitm cell——配了 BILI_LAUNCHER_MODEL_* 的机器 native 车道仍然每进程 spawn,不满足验收
BILI_MITM=0 边界 ✅(我的 56a191a21 吸收其 enabled-gate 思路) ✅
CLI 车道 facet(routes 列表缺 config-file 基础层) ❌ 未覆盖 ✅ 顺带修掉
回归面 小:只改 10 个调用点输入,共享比较器不动 中:改所有车道(含 CLI)共用的判定函数
CI 重跑中(新 head 56a191a21,gates 已过,test lanes in_progress) 全绿

建议:合并 #3540,关闭 #3551 为 superseded——它的独有贡献已被吸收:enabled-gate → 我的 fix commit;CLI facet + 「三 cell 统一收口到判定层」的加固方向 → 已立 #3557([no-dispatch],带来源标记,含两面残留缺陷 + 机制加固备注)。判定层收口是更稳的长期形态,但那是 follow-up 重构,不应阻塞本次可用性修复(可用性 > 优雅)。若你更想以 #3551 架构为准,请在 thread 里说一声,我把 windows/maxOutputs 对齐补进去再反转建议。

结构化八问

  1. 是什么问题:native 车道启动时 attach 判定拿不到运行中 proxy 实际记录的三个配置字段,导致配置了 MITM/windows 的机器上每个 native 进程各 spawn 一个 proxy,实例只增不减、端口逐个漂移。
  2. 什么原因导致的:ensureProxyRunning 的 attach 侧入参(10 个 native 调用点)与 spawn child 实际记录到 session metadata 的值脱节——child 从 ambient env + spawn 覆写拿到完整三字段并记录,attach 侧却从不传,逐字段比对必败于 mitm-domains-mismatch。根因在判据输入缺失,不在比较逻辑本身。
  3. 影响范围与严重程度:所有配置了 mitm.domains(文件或 env)或 BILI_LAUNCHER_MODEL_* 的机器上跑多个 native 进程的用户;资源泄漏类(实例累积、端口漂移、日志刷屏),功能不受损,长时间使用体验明显恶化(报告者 1.5h 挂 15 个实例)。
  4. 怎么修复的:单一共享 helper nativeAttachCompat() 提供与 record 侧同源的三字段值,spread 进全部 10 个 native 调用点;review 中发现的 disabled-mitm 边界用 resolveEffectiveMitmDomains(叠加 enabled 门控)补齐。
  5. 是否有回归问题:无意外回归。默认空配置行为字节级不变;隔离契约(不同配置 → 各起 proxy)保持且有测试钉住;唯一变更即披露的 N→1 归并 + 我补充的 disabled-mitm 收敛。高危模式逐项过:无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式变化。
  6. 是否破坏缓存:不涉及(无缓存/重试/游标/去重键触碰)。
  7. 是否改了设计方向:不改。仍在「调用点对齐输入」路线内;判定层收口的长期形态立 [no-dispatch] Residual attach-criteria mismatches after the #3436 fix: CLI lanes and kimi cross-path still spawn extra proxies #3557 跟踪,等 native lanes suspected of not reusing instances: one proxy per process, instances only grow (v0.1.197 / opencode v2) | [Bug?] native 车道疑似不复用实例:一进程一代理,实例只增不减(v0.1.197 / opencode v2) #3436 收敛后再决策。
  8. 是否引入了新的配置:无。复用既有 BILI_MITM_DOMAINS/BILI_LAUNCHER_MODEL_*,无新 env/config/flag。

判定:fix commit 已推、本地定向绿;等 CI test lanes 在新 head 上转绿即可合并(§7.4:触碰 #1225/#1623 compat 契约且跨 10+ 文件,人工合并)。合并前请一并处置 #3551(建议 supersede 关闭)。

awork and others added 2 commits October 17, 2026 20:31
…process (#3436)

Native bootstraps (opencode, opencode-native, dsh, omp, pi, zcode x2, kimi x2, claude) passed no mitmDomains/modelWindows/modelMaxOutputs to ensureProxyRunning, so every live candidate failed instanceCompatible's mitm-domains-mismatch cell and each process spawned a fresh proxy: instances only grew and ports drifted.

Add nativeAttachCompat() in launcher.ts (computes the three fields from the same env a spawned child inherits) and spread it into all 10 native call sites, so attach-side == record-side exactly. Zero change to what the proxy does; only the attach decision now sees the values. kimi keeps its config.toml modelWindows override after the spread.

Regression tests pin same-config attach, different-domains spawn, and spawn-side==record-side convergence.
…e raw union

server.ts records [] when mitm is disabled even with domains configured, but nativeAttachCompat used resolveMitmDomains which ignores the BILI_MITM / file mitm.enabled gate — on such machines every native process still failed the mitm-domains-mismatch cell and spawned its own proxy. New config.resolveEffectiveMitmDomains applies loadOptions' enabled gate on top of the same file-first/env-second union; regression test pins disabled→[] and default-enabled→union.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

补充:CI 判定 —— 已转绿(确认为 flake,非回归)

唯一红过的分片 test (windows-latest, 22, 2) 连续红了两次,同一 head (56a191a) 第三次重跑全绿,CI 现已全部通过。

两次失败签名(均为实时敏感测试,且都与本 PR 的代码路径无交集):

  • 第 1 次:tests/lane-decisions-matrix.test.ts — 末尾 5 个子测试被 Promise resolution is still pending but the event loop has already resolved 取消;TAP # fail 0 / # cancelled 5(零断言失败);同文件输出可见 MaxListenersExceededWarning (11 listeners)。
  • 第 2 次(重跑):tests/issue3380-dsh-rereg-selflock.test.ts:283 — retryAt sits on the 80ms ladder step (got 16ms)。该断言在「第 3 次注册失败把 retryAt 设为 now+80ms」与「读快照」之间量墙钟时间(窗口 30–95ms,基础间隔仅 20ms)——arm 到观察之间任何 >15ms 的调度漂移都会挂。

判定为环境性而非本 PR 引入的证据链:

  1. 两个失败文件对本 PR 改动符号(nativeAttachCompat / resolveEffectiveMitmDomains / resolveMitmDomains)grep = 0 命中;Plugin tool registration self-lock: after a failed proxy respawn, re-registration is blocked by its own prior registrations — identity headers lost forever (wire mode) → 413 preflight_compress_failed on large sessions | [Bug] 插件工具注册状态自锁:proxy respawn 失败后重注册被自己挡死,永久失去身份头(wire 模式)→ 大会话 413 preflight_compress_failed #3380 文件 import 的是 src/agent/dsh-native.ts,不是被改的 launcher/config 模块;
  2. 本地 A/B(同机交错运行以排除机器负载混淆):master b10c525 8/8 + 交错 6/6 全过、旧 PR head aca6473 8/8 全过;本 PR head 上同一测试仅在并发负载下偶发(约 11 次中 2 次),其余重复通过 —— 符合零松弛墙钟断言的特征,不是确定性破坏;
  3. 同一时间窗内无关分支也红:feat(Two read_original_image follow-ups: settings-panel toggle + acp_-prefixed tool name | read_original_image 功能两处改进:设置面板加开关;工具名改 acp_ 前缀 #3546)×3、docs(High compression cost on DeepSeek Flash: model-level efficiency config, output control and A/B validation proposal | [pi native][0.1.197] DeepSeek Flash 压缩成本较高:模型级效率配置、输出控制与对照验证建议 #3548)×1(runs 39030012540 / 39030554341 / 39030593809 / 39031315530)。

flaky 测试本身已按规范单独立项跟踪:#3571([no-dispatch],带来源标记)——核心是把那个零松弛的墙钟断言改成状态型/fake-timer 型。不阻塞本 PR。

最终判定(不变,现已被 CI 解锁)

可合并 —— head 56a191a CI 全绿。按 §7.4 需人工合并(触碰 #1225/#1623 attach 兼容契约、跨 10 个 native lane)。建议维持前述结论:合并 #3540,关闭 #3551 为 superseded(其独有贡献已被吸收:mitm-disabled gate 在本 PR 的 56a191a commit 中;CLI lane 面 + 决策点强化由 #3557 跟踪)。

@ranxianglei
ranxianglei force-pushed the 2026-10-17_native-attach-reuse branch from 56a191a to dcfba3c Compare October 17, 2026 12:40
…ecords

Consolidate the #3436 fix into the single decision point (instanceCompatible)
instead of spreading compat fields across ten native call sites (this branch's
original design): each of the three config cells now wants the value a child
spawned from these opts will RECORD — resolveRecordedMitmDomains for the mitm
tier (loadOptions' file-first/env-second union under its enabled gate; the
caller's explicit list when the launcher injects it at spawn time, ambient
inheritance otherwise) and, for windows/max-outputs, the same parse the child
applies to its BILI_LAUNCHER_MODEL_* env.

Every attach path funnels through instanceCompatible (main probe, starter-poll,
squatter check, findLiveAttachableInstance behind zcode handoff / watchdog
drift-repair), so one projection covers all lanes: native lanes that pass
nothing, CLI lanes whose explicit list missed the config-file layer, and the
minimal-opts paths a call-site spread cannot reach (kimi MCP watchdog respawn,
zcode drift repair). Call sites revert to their pre-#3436 shape; kimi's two
proxy paths share ONE windows source (kimiConfigModelWindows) so a respawn
presents the same map as the original proxy (#3557 facet 2).

Supersedes #3551 (its resolveRecordedMitmDomains + hermetic regression suite
absorbed here); closes #3557 (facet 1 was a misread of extMitmHosts — the
recorded tier never includes DEFAULT_MITM_DOMAINS/discovery layers; facet 2
fixed by the shared kimi windows source).
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.567-master.cb02753 (rolling master tag).

npm install -g billion-context@0.0.567-master.cb02753  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

@github-actions github-actions Bot mentioned this pull request Oct 18, 2026
ranxianglei pushed a commit that referenced this pull request Oct 18, 2026
…N.md

In-passing fix found while reviewing #3737: since the #3540 merge, master has
carried two adjacent BILI_ALLOW_DSH_COMPACTION detail rows — one with the
#3384/#2490 output-budget paragraph but a stale pre-#3354 scope note, one with
the current #3354-overlay scope note (the overlay marks preset-nested rows at
any depth, pinned by tests/issue3354-dsh-compaction-disable.test.ts). Merge
them into one canonical row keeping both unique paragraphs; the new
BILI_DSH_AUTHORED_COMPACTION row from #3733 now sits in a single place.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant