Skip to content

fix(opencode-native): proxy 不可用时超窗请求本地拒发,不再静默直连撞上游 400 - #3318

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-16_opencode-native-overwindow-refusal
Oct 16, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-16_opencode-native-overwindow-refusal

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #3308

问题

opencode v2 plugin-native 车道(createNativeRoute)在 proxy attach/spawn 未就绪时,把所有模型请求静默直连(未压缩)——文件头文档化的可用性 fail-open。长会话的 raw 历史(~1.05–1.09M tok)超过 deepseek-flash 1,048,576 窗口后,直发必然被上游 400;且历史只增不减,此后每一轮都 400,直到重启进程。字段证据:18 次降级信号、5 次配对上游 400、失败回合在 bili.log 零痕迹。

根因与修法

#2924 已确立裁定「要么一定压缩,或者一定拒绝」,dsh fetch 车道按此实现了 refuse-oversized 臂;但 opencode V2 车道的降级分支完全绕过了 gate 机制,漏掉了同一裁定。本 PR 把该裁定补到这条车道:

  1. src/agent/opencode-native.ts — createNativeRoute 降级分支加超窗拒发臂:resolveLive() 无活 proxy 时,克隆请求体测字节数(原始 Request 保持可发送),对照插件自己从 ctx.model.list() 采集的模型窗口做与 [dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 完全相同的保守判定——ceil(bytes/4) > window 才拒(floor 估计不会误拒;[dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 ledger 实证真实 body 高于 floor ~1.14×)。命中则抛出带完整信息的错误(估算 token vs 窗口 + 「恢复中,稍后重试或开新会话」)+ 一次性 bili.log 诊断。窗口未知 / body 不可读 / 未超窗 → 保持原 fail-open 直连(可用性设计不动)。
    • hook 抛错在 opencode v2 里转成该回合可见的模型请求失败——已读 @opencode/cli 2.0.29 源码 packages/core/src/session/model-request.ts:343-364 确认:hook 触发与发送在同一 Effect 内,失败即请求失败(与上游 400 同通道),不会崩进程。
  2. src/agent/opencode-v2.ts — 原生模式把窗口采集提前到 route 之前:覆盖「bootstrap 从未成功的新会话」——catalog 应答快时第 1 个请求即可判定,慢时第 2 个请求起生效。launcher 模式顺序不变(无 proxy 时采集只会产生误导性告警)。

行为变更披露(旧 → 新 + 原因)

  • 旧:proxy 不可用 + 请求体可证明超过模型窗口 → 静默直发 → 上游 400(长会话下每轮重复直至重启)。
  • 新:同样条件 → 本地抛出 bili: proxy unavailable and this request cannot be sent uncompressed — body ~N tok … exceeds the <model> context window (M); … retry shortly, or start a fresh session,该回合以可见错误结束;bili.log 记一条一次性诊断。
  • 原因:执行 [dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 既有裁定到漏掉它的车道;直发在此条件下没有任何成功可能,本地拒发严格优于必然 400,且恢复是自动的(proxy 健康后下一请求即回路由+压缩,无永久 latch)。
  • 不变:未超窗 / 窗口未知 / body 不可读时的直连 fail-open;attach/spawn/重生节奏(HEALTH_TIMEOUT_MS / RESPAWN_COOLDOWN_MS 均未动);routed(/bili/-prefixed)URL 路径;launcher 模式全部行为。

Config surface

不涉及——无新增 env/config/CLI flag。判定用的窗口来自插件既有的 ctx.model.list() 采集通道(x-bili-plugin-context-window 同源)。

验证

  • 新增 5 条回归测试钉死:超窗拒绝(含 body 不被消费)、未超窗放行、窗口未知/错配放行(不误拒)、首请求 priming 端到端、空 catalog 放行。
  • 本地:typecheck ✓、build ✓、定向套件 75/75 ✓(opencode-native / opencode-v2 / responses-websocket / issue1958-routed-warn,含既有 degrade/respawn 用例全绿)。全量回归交 CI。

不在本 PR 范围(已在本 issue 记录为子缺陷清单)

  • 其余 fetch 车道(pi/omp/kimi/hermes/zcode/opencode V1)的同族缺口:同一症状家族,但窗口来源需逐车道设计,不混入本 PR。
  • 未超窗直连的 TUI 级可见化、全量 fail-loud、探测预算自适应:产品决策,等 owner 拍板。

…he proxy is unavailable (#3308)

The V2 native route's degrade branch sent every model request direct
(uncompressed) whenever no live proxy existed — documented as an
availability fail-open. But per the #2924 ruling (always compress OR
always refuse), a body that provably cannot fit the model's context
window must not ride that fallback into a guaranteed upstream 400; on an
over-window session history that 400 then repeats on every later turn
until restart (the reported field failure).

Apply #2924's refuse-oversized arm to this lane: when resolveLive()
finds nothing healthy, measure the body through a clone (original stays
sendable) and compare the FLOOR estimate (bytes/4) against the model's
window from the plugin's own catalog harvest — same conservative math as
the dsh gate, so unknown window/model/body keeps the fail-open and the
guard cannot false-refuse. A hit throws a host-visible retryable error
(opencode v2 turns http.request hook failures into model-request
failures, verified against @opencode/cli 2.0.29 source) plus a one-time
bili.log diagnostic. The window map is primed before the route decides
(native mode only) so the guard arms from the second request even when
bootstrap never landed.
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-16_opencode-native-overwindow-refusal (98285ee)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3916-pr.3318.59509fa

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-pr3318.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)

查重:不重复。检索 open/closed issues 与全部 open PR:无其他条目处理「opencode V2 native 车道 proxy 不可用 + 超窗直发 400」;#3292 是增量 turn 压缩(机制不同),#3308 是本 PR 的原 issue。

① 是什么问题
opencode V2 plugin-native 车道(createNativeRoute)在 proxy attach/spawn 未就绪时有文档化的 fail-open:所有模型请求未压缩直连上游。长会话 raw 历史超过模型窗口(deepseek-flash 1,048,576)后,每一轮都必然上游 400,直到重启进程。字段证据(#3308):18 次降级信号、5 次配对上游 400、bili.log 零痕迹。

② 什么原因导致的
#2924 已确立裁定「要么一定压缩,或者一定拒绝」,dsh fetch 车道按此实现了 refuse-oversized 臂(src/agent/dsh-native.ts:1149-1152);但 opencode V2 车道的降级分支完全绕过了 gate 机制——同一裁定在这条车道漏掉了。次因:窗口采集(refreshWindows)原本只在 route 成功后调用,degraded 会话里 windows 从未被采集,想做超窗判定也没有数据。

③ 影响范围与严重程度
仅 opencode V2 plugin-native 车道,且需同时满足三条件:proxy 暂不可用 + 模型窗口已采集 + 请求体可证明超窗(floor 估计 bytes/4)。该条件下直发没有任何成功可能(必然 400),所以变更实质是「必然的不可见失败」→「可见的本地错误」;不满足任一条件的用户行为零变化(fail-open 原样保留)。launcher 模式、routed /bili/ 路径、其余车道不受影响。

④ 怎么修复的
两步:

  1. createNativeRoute 降级分支加超窗拒发臂(src/agent/opencode-native.ts:119,调用点 :192):无活 proxy 时克隆请求体测字节数(原始 Request 保持可发送),对照插件自己 ctx.model.list() 采集通道的窗口做与 [dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 完全相同的保守判定——ceil(bytes/4) > window 才拒(floor 估计不会误拒;[dsh-native] takeover-gate refusal (threw state) fails open with the FULL uncompressed transcript; a >window local history dies upstream with 400 (maximum context length) #2924 ledger 实证真实 body 高于 floor ~1.14×)。命中则抛完整错误(估算 token vs 窗口 + 「恢复中,稍后重试或开新会话」)+ 一次性 bili.log 诊断。窗口未知 / body 不可读 / 未超窗 → 保持原 fail-open。
  2. 原生模式把窗口采集提前到 route 之前(src/agent/opencode-v2.ts:351,既有 post-route 调用点 :361/:399 不动):覆盖「bootstrap 从未成功的新会话」——catalog 应答快时第 1 个请求即可判定,慢时第 2 个请求起生效。launcher 模式顺序不变。
    hook 抛错在 opencode v2 里转成该回合可见的模型请求失败(已读 @opencode/cli 2.0.29 源码 packages/core/src/session/model-request.ts:343-364 确认:hook 触发与发送在同一 Effect 内,失败即请求失败,与上游 400 同通道,不崩进程)。新增 5 条回归测试钉死:超窗拒绝(含 body 不被消费断言)、未超窗放行、窗口未知/错配放行(不误拒)、首请求 priming 端到端、空 catalog 放行。

⑤ 是否有回归问题
回归清单(diff 触碰路径承担的既有行为逐项过):

⑥ 是否破坏缓存
不涉及——缓存/重试/增量游标/去重键均未触碰;成功请求的 wire shape 不变,前缀缓存不受影响。

⑦ 是否改了设计方向
不变——是把既有 #2924 裁定(要么一定压缩,或者一定拒绝)落到漏掉它的车道,不是新方向。其余 fetch 车道(pi/omp/kimi/hermes/zcode/opencode V1)的同族缺口已在 issue 记录为子缺陷清单,不混入本 PR。

⑧ 是否引入新配置
无——无新增 env/config/CLI flag;判定用的窗口来自插件既有的 ctx.model.list() 采集通道(与 x-bili-plugin-context-window 同源)。

结论:代码可合。方向正确(疏而非堵:修的是「裁定漏落」这个根因,判定标准与 #2924 逐字一致,保守 floor 不误拒,fail-open 面完整保留)、测试有牙(guard 或 ordering 任一回归都会红)、本地 typecheck clean + 定向套件 75/75、CI 红全部为既有签名(#3314/#3317 待合 + AC12 flake)与本 diff 无关。本 PR 涉及 fallback 行为变更,按 §7.4 属人工合并范畴——请自行合并:#3318

@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.483-master.d8c0e50 (rolling master tag).

npm install -g billion-context@0.0.483-master.d8c0e50  # 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.

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