Repository navigation
fix: bound the native fetch chain against coexisting re-wrapping plugins (#1662) - #1672
Conversation
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.1676-pr.1672.5c39bb8This exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr1672.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 Review 完成:可以合并(人工合并) 按规范五关走完,逐项结论如下。本地验证分支 ① 原 issue 方向关 ✅ ② 回归审查(diff 触碰路径的既有行为清单)
③ 代码审查 ✅
④ 测试有效性 ✅ 三个新用例都有牙:churn 用例断言 sink 收到精确 URL(模型 URL 恰好改写一次/非模型原样一次),且我实测它在旧代码上复现崩溃;并发用例钉住双请求全量派发+顺序;稳态用例钉住外置 hook 恰好一次且看到改写后 URL。既有 10 个 #1158/#1410 用例全部原样通过。小缺口:双存活包装器降级与发现 (a) 边角无用例钉住(与 (a) 同源,若决定加固可顺手补一个)。 ⑤ diff 整洁度 ✅ 2 文件 +159/-10,每一行都服务于本 PR 目的;唯一附带改动是改名行顺带修正了 master 上既有的缩进错位。无空白重排、无锁文件/生成物、version 未动。 CI:head 本地复验(rebase 后):typecheck ✅;npm test 3243 例 / 3231 pass / 0 fail / 12 skip ✅;build ✅。 合并注意:按 §7.4 不可 auto-merge——REARM_LIMIT 超限语义变更属 fallback/默认值的产品决策,且模块是所有原生 lane 共用的基础设施,需人工合并。发布说明请带上两条已披露变更(超 16 次后持续采纳而非交出顶层;新增一次性重入日志)。master 已在 v0.1.172,本修复将随 0.1.173+ 生效。 一句话总结:修复了共存插件反复包装 globalThis.fetch 导致拦截链无界增长、最终栈溢出打崩宿主进程的问题(根因:重入遍历无深度上限 + 超限后静默交出顶层),回归测试经我独立验证可在旧代码上复现原崩溃,代码与测试均通过审查,可以合并(人工合并)。 |
[bot] 🏷 CI 状态更新:此前唯一红的 job( |
Closes #1662
Problem
With
dsh-codex-subscriptioninstalled alongsidebili plugin install dsh, the dsh web process dies withRangeError: Maximum call stack size exceeded(repro report: bili 0.1.164 / DSH 0.1.7-rc.2 / Windows; latent in current master too). The existing #1158 guard + #1410 re-anchor were both present in the failing version — this is a gap beyond #1410.Root cause
dsh-codex-subscription repeatedly performs {read current
globalThis.fetch→ wrap it with its injection logic → write back}. The #1158 guard adopts every third-party install as downstream (top = makeChain(v)), so each cycle stacks one (wrapper, bili-link) pair onto the chain:Every request then walks the ENTIRE accumulated history — each stacked bili link re-running the full dispatch (URL parse, routing decision, header stamping, gate waits). Stack depth grows with session lifetime. Measured on Node v22.23.2/Linux: 4K churn cycles still OK, 8K cycles →
RangeError(the user's live crash). #1410's re-anchor only repairs links whose downstream DIES; it never prunes live-but-redundant stacked pairs. Worse, pastREARM_LIMIT = 16the setter surrendered the top slot (top = v) and stopped logging entirely — so growth continued silently with zero visibility after the first 16 evict-attempt lines.Reproduced deterministically before the fix (probe: N churn cycles of read-top/wrap/write-back, then one model request): N=8000 →
RangeError: Maximum call stack size exceeded; after the fix N=50000 → OK, request routed correctly, single one-time warning.Fix (
src/agent/native-intercept.ts, shared by all native lanes)AsyncLocalStorage(not a global counter — concurrent top-level requests must not see each other). A bili link called from within our own dispatch can only be reached through a foreign wrapper that captured one of our older tops — that is the proof of accumulated nesting. At that point everything below is redundant (the top link already rewrote/stamped model URLs; everything else passes through unchanged), so the descent terminates at the oldest still-live anchor (the host baseline that survived every prior teardown) instead of walking the history. Request depth becomes constant no matter how many times the coexisting plugin re-wraps. Depth ≥ 2 falls back to the plain walk so the termination itself can never loop (degenerate multi-plugin nesting).REARM_LIMITthe warning stops (log spam) but adoption CONTINUES instead of surrendering the top slot. With (1), each further adopt costs O(1) per request regardless of history length, so the cap now bounds logging only; routing authority never leaves the guard.The newest foreign wrapper's behavior (e.g. ChatGPT auth injection) is preserved: it sits below the top link and its hook still runs exactly once per request (pinned by test).
Behavior changes (disclosure)
top = v), routing continued only via the orphaned stale chain, no further logging. new = guard keeps adopting every further install as downstream and stays active forever; warnings suppressed after 16. WHY: the surrender left unbounded silent chain growth (this bug); with re-entry termination continued adoption is O(1) per request.[bili-native] nested re-entry into the bili fetch chain detected (#1662) — … terminating the descent at the oldest live anchor…. Purely additive visibility.BILI_RECLAIM_FETCH_PATCH=0escape hatch). The real-world churn case (one live wrapper) is fully preserved.Tests
tests/native-intercept-selfheal.test.ts(+96 lines):All 10 pre-existing 方式一安装的dsh 原生插件:llm-pi-ai 传输(OpenAI SDK + options.fetch 注入)绕过 globalThis.fetch 拦截,模型请求不进代理、压缩完全不生效 #1158/bili-native 安装/re-arm globalThis.fetch 不校验所有权,与网络 scope 类插件交错后全局 fetch 悬空 → 全进程 "baseFetch is not a function" #1410 suites pass unchanged.
Pre-flight
npm run typecheck✅npm test: 3177 pass / 0 fail / 16 skipped (by design) ✅npm run build✅Note: #1666 (same reporter, follow-up after upgrading everything) describes a DIFFERENT symptom — connect timeout to chatgpt.com through the proxy, not a crash — and is handled in its own thread.