Skip to content

[PR] fix(#4121): opencode V2 direct-degrade gate — media-aware two-stage byte judgment - #4382

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-23_opencode-v2-media-direct-gate
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-23_opencode-v2-media-direct-gate

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Third unpatched instance of the #4111 bytes/4 media-as-text false-refusal, on the opencode V2 native lane. Reuses the exact two-stage predicate that #4119 landed for the dsh takeover gate, so both lanes now share ONE media-aware judgment.

What was wrong

oversizeDirectRefusal (src/agent/opencode-native.ts) priced the raw request-body byte count / 4 as an optimistic token floor and refused locally whenever that floor exceeded the model window. Inline base64 images cost ~2–4 KB/token upstream (resolution-billed) vs ~6.8 B/token for text, so bytes/4 wildly overbilled them: during the direct-degrade window (proxy down, or busy-exhausted / saturated event loop) a media-heavy session was refused locally ("body ~N tok exceeds context window") even though upstream would accept it. Pure-text bodies were unaffected. Both wording branches shared the math (#3654 busy-exhausted and #3308 proxy-down).

Fix

Reuse #4119's exported mediaBytesOfWireBody + bodyProvablyOverWindow from src/agent/dsh-native.ts:

  • stage 1 keeps the raw bytes/4 fast screen (byte-identical result for pure-text bodies);
  • stage 2 runs only on the would-refuse path and re-prices on media-excluded core bytes, disarming the refusal when the text alone fits.

bodyProvablyOverWindow gains an optional { applyFloor } flag:

  • dsh callers pass nothing → the #4079 max(declared, 1M) floor still applies → zero behavior change on the dsh arm;
  • opencode passes applyFloor:false because s.windows here is a measured lane window, not a host declaration → preserves this lane's existing bar verbatim.

Both refusal branches updated; the error text now reports core bytes and the media mass excluded (when nonzero).

Behavior changes (old → new)

Surface Old New Why
opencode V2 degrade gate, media-heavy body, proxy down / busy-exhausted locally refused rides the direct send upstream bills images by resolution; bytes/4 overbilled them (the bug)
same gate, pure-text over-limit body refused refused (unchanged number) teeth kept
refusal message floor estimate N B/4 core est N B/4[ , X B media excluded] reports what was actually priced
dsh takeover gate unchanged unchanged applyFloor defaults to the floored path

No config surface change. No new env var. No kernel change.

Open question for owner (decision point #1)

Shipped without a window floor on the opencode arm (minimal surface — fixes only the media mispricing). The dsh arm carries a max(declared, 1M) floor (#4079); the opencode arm uses the measured lane window with no floor. Adding a floor to a measured window would be a behavior change beyond this bug. If wanted, it is a one-line flip (applyFloor:false → omitted). Recommend keeping it off for now.

Related observation (recorded in #4121, not fixed here)

Server-side estimateRawBodyTokens (src/preflight.ts) exclusion set does not recognize the responses-wire input_image.image_url string form, so such images double-count in the #554 side-request adaptation (conservative side-request, not a false-refusal; different subsystem). Same root mechanism (media-shape recognition missing the string form); align together later.

Closes #4121

…ia as text

The opencode V2 native degrade gate (oversizeDirectRefusal) priced the raw
body byte count / 4 as an optimistic token floor, so inline base64 images
(resolution-billed upstream, ~2-4 KB/token vs ~6.8 B/token text) minted
phantom text tokens and false-refused media-heavy requests during the
direct-degrade window (proxy down / busy-exhausted) even though upstream
would accept them. Third instance of the same root mechanism as the dsh
takeover gate fixed in #4119/#4111.

Reuse #4119's media-aware two-stage predicate (mediaBytesOfWireBody +
bodyProvablyOverWindow) so both lanes share ONE judgment: stage 1 keeps the
raw bytes/4 fast screen (unchanged for pure-text bodies); stage 2 runs only
on the would-refuse path and re-prices on media-EXCLUDED core bytes,
disarming the refusal when the text alone fits.

bodyProvablyOverWindow gains an optional { applyFloor } flag. dsh callers
pass nothing -> the #4079 max(declared, 1M) floor still applies (zero change).
The opencode arm passes applyFloor:false because s.windows is a MEASURED lane
window, not a host declaration -> preserves this lane's existing bar verbatim
(flooring a measured window would be a behavior change, tracked separately).

Both refusal wording branches (busy-exhausted #3654 and proxy-down #3308)
share the math and the fix; error text now reports core bytes and the media
mass excluded when nonzero.

Tests: media-heavy body rides direct (both branches); over-limit text still
refuses (teeth); image + huge-text still refuses and reports excluded media.
@ranxianglei ranxianglei changed the title fix(#4121): opencode V2 direct-degrade gate — media-aware two-stage byte judgment [PR] fix(#4121): opencode V2 direct-degrade gate — media-aware two-stage byte judgment Oct 23, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重:不重复。 本 PR 是 #4119(dsh 车道)同机制的第三处实例,落在 opencode V2 native 车道的 oversizeDirectRefusal。两车道共享同一个 media-aware 谓词(bodyProvablyOverWindow),但调用点不同、车道不同——不是重复 PR;仓库内也没有其他在途 PR 改同一 opencode V2 降级门。(#4119 修 dsh、本 PR 修 opencode V2,属「同一缺陷家族、不同车道」,各自独立成立。)

结构化八问

  1. 是什么问题:opencode V2 原生车道在降级窗口(proxy down 或 busy-exhausted)里,把请求体按 raw bytes/4 当 token 下限来估;内联 base64 图片被当文本计费,导致带图会话被本地误拒(upstream 其实收得下)。纯文本不受影响。是 DSH's unattributed over-limit gate prices base64 images as text (bytes/4) — media-heavy requests are falsely rejected wi | dsh 无归属超限门把 base64 图片当文本计价(bytes/4)——媒体重的请求被假拒 422,上游本会正常接受 #4111 家族的第三处未修补实例。
  2. 什么原因导致的:根因是 byte→token 下限估算不区分「按分辨率计费的媒体」与「按字符计费的文本」。base64 图 ~2–4 KB/token,bytes/4 严重高估 → 触发「provably over window」误拒。修在判定处(decision point),不是过滤 payload(疏非堵)。
  3. 影响范围与严重程度:仅 opencode V2 native 车道、且仅在降级窗口 + 媒体重会话时触发;表现为本地误拒(可用性受损,但限定在降级窗口这一窄条件)。纯文本行为不变;dsh 车道零变化。
  4. 怎么修复的:复用 [PR] fix(#4111): dsh unattributed gate — core-text byte math, stop billing base64 media as text #4119 已导出的 mediaBytesOfWireBody + bodyProvablyOverWindow,两车道共用一套判定。stage1 保留 raw bytes/4 快筛(纯文本逐字节等价);stage2 只在会拒绝的路径上按「去媒体后的 core bytes」重计价,文本本身放得下就解除拒绝。bodyProvablyOverWindow 加可选 { applyFloor }:dsh 不传 → 保持 Context Window Overflow Issue Summary: Raw Requests Sent Directly to Upstream Rejected + Local Intercept Cooldown Lockin | 上下文超窗问题汇总:请求裸冲上游被拒 + 本地拦截冷却锁死会话(统一分析入口) #4079 的 max(declared, 1M) 地板;opencode 传 applyFloor:false → 用实测车道窗口原样。两个拒绝分支都更新,错误文案改为报告 core bytes + 被排除媒体量。
  5. 是否有回归问题:审过的既有行为——(a) dsh takeover 门两处调用点(src/agent/dsh-native.ts:1934 / :1956)均传 3 参 → 地板路径不变,零行为变化;(b) opencode 纯文本超限:stage1 判定与旧代码逐字节一致,拒绝数字不变(仅文案 label 从 floor estimate N B/4 变 core est N B/4,N 相同);(c) 未知/缺失 window、不可读 body 一律 fail-open 直发,不变;(d) 仍通过 CLONE 量尺寸,原 Request 可发送([omp-native] fetch recovery that lands on the same healthy origin is treated as give-up: BILLION_CONTEXT_PROXY is deleted for the whole process #2496 纪律保留)。无 fail-fast 变吞错、无阈值漂移(纯文本阈值未动)、无缓存/重试策略变化。
  6. 是否破坏缓存:不涉及。纯客户端降级门判定,不动出站前缀 / 缓存键 / 增量游标 / 去重。
  7. 是否改了设计方向:否。是把 opencode 车道对齐到 dsh 已有的共享判定(收敛为一套 media-aware 逻辑),减少重复、统一口径,无架构转向。
  8. 是否引入新配置:无新 env、无新 config key、无 kernel 改动。

决策点 #1(opencode 臂要不要加窗口地板)

我独立评估后认同 PR 的建议:保持不加(applyFloor:false)。#4079 的 max(declared, 1M) 地板是为了防「声明值错误偏小」(stale 配置 / 目录回退)——它把窗口向上钳到 ≥1M,让门更宽松、更少误拒。但 opencode V2 的 s.windows 是实测车道窗口;若对它加地板,反而会把一个小窗口的实测值(如 128K 模型)强行抬到 1M,让门对小窗口模型失去牙齿(本该拒的直发出去、upstream 才 400)。所以给实测窗口加地板是错的,应原样保留。判断与 PR 一致。

相关观察(PR 已记录于 #4121,不在本 PR 修)

服务端 estimateRawBodyTokens(src/preflight.ts)的排除集不认识 responses-wire 的 input_image.image_url 字符串形态,会在 #554 side-request 适配里双倍计数。这是不同子系统 / 不同症状(保守 side-request,非误拒),机制同源(媒体形态识别漏了字符串形态)。同意单独跟踪、后续一起对齐,不塞进本 PR。

CI 状态与合并判定

结论:代码与测试我判可合(逻辑成立、双向有测试、typecheck 干净、dsh 臂零回归)。但合并门禁要求 rebase 到最新 master + CI test 车道跑绿——当前 head 的 CI 全 cancelled,建议 rebase 后等 CI 绿再合。我不会代合(merge 仅人工)。需要的话我可以在你的 PR 分支上做 rebase(不改动代码内容),你点头即可。

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

陈旧票清理(第三批动作①):目标 issue #4121 已关。为避免重复劳动关闭本 PR;如判断有误请直接 reopen 并留言。

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