Skip to content

[PR] fix: account for deeply nested media without recursive stack growth - #6048

Merged
ranxianglei merged 2 commits into
ranxianglei:masterfrom
Afloat16:2026-11-09_fix-media-walk-depth
Nov 10, 2026
Merged

ranxianglei merged 2 commits into
ranxianglei:masterfrom
Afloat16:2026-11-09_fix-media-walk-depth

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Problem

Valid 20,000-level request JSON overflows the recursive media-byte walk.

Changes

Traverse pending JSON values iteratively, preserving all existing media-shape accounting.

Regression coverage

20,000 nested containers, non-media text, malformed JSON and mixed media-wire siblings.

Validation

Passed against exact fork commit 01e42ad79402ea62220e7ac60c2d54075da8c4ef in an isolated GitHub Actions checkout: validation job. The targeted regression and related suites, npm run typecheck, and npm run build all passed on Node 22. This is targeted verification, not the full upstream CI matrix.

node --import ./tests/preload-storage-baseline.mjs --import ./tests/preload-test-env.mjs --import tsx --test tests/media-walk-depth.test.ts
npm run typecheck
npm run build

Related work

Related #4119, #4382 and #4923 address repricing/maximum-string handling, not traversal depth.

Closes #6038.

@ranxianglei ranxianglei changed the title fix: account for deeply nested media without recursive stack growth [PR] fix: account for deeply nested media without recursive stack growth Nov 9, 2026
@ranxianglei ranxianglei added bug Something isn't working p2 scope/wiring labels Nov 9, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷
(ai: vllm-qwen/qwen3.8-27b)

Duplicate screen: not a duplicate. Independent search of open+closed issues/PRs found no overlapping work on recursive media-walk depth. Nearest hits are different mechanisms: #5627 was a RangeError: Maximum call stack size exceeded in unified-store ingest (storage subsystem, closed); #4075/#2614/#3412 are media-shape counting inside folding decisions. None touch traversal depth. The author's own cross-check (#4119/#4382/#4923 = repricing / max-string handling) is accurate and unrelated to depth.

Verdict: MERGEABLE — after a routine rebase to current master plus CI green. Clean, surgical external contribution that meets the quality bar; no refactoring needed.

Verified on the rebased head (cherry-picked the single commit 01e42ad onto current master 9bbc2a4d; applies cleanly — media-bytes.ts had not drifted in the 64 commits since the PR branched):

Code quality (⑤b): naming is clear (pending as the explicit stack), every media-shape check stays byte-identical with only the traversal mechanism swapped, the doc comment is honestly updated (recursive→iterative), and the tests are deterministic with real assertions. Meets the bar.

One non-blocking observation, same mechanism, one line for completeness: guard status for mediaBytesOfWireBody is asymmetric across consumers — src/agent/opencode-native.ts:137 wireBodyMediaBytes already wraps it in try/catch (degrades to all-core billing), but the shared arm src/agent/native-intercept.ts:759 (and dsh's delegate at src/agent/dsh-native.ts:620) do not, so today an unguarded path surfaces the RangeError as a hard request failure. This PR fixes the producer (the walk can no longer throw), which makes that asymmetry moot — noted only so it isn't mistaken for a remaining gap. No follow-up required. I labeled the PR bug / p2 / scope/wiring (it was unlabeled).

Note: the branch is 64 commits behind master — please rebase before merge so CI runs against the current base.

--- 结构化八问 ---

  1. 是什么问题:一个合法的超深嵌套 JSON(约 2 万层)会让 agent 车道的「媒体字节遍历」递归爆栈。
  2. 什么原因导致的:mediaBytesOfWireBody 用递归 DFS 遍历待解析 JSON,每层对象/数组各压一个调用帧;嵌套深度吃到 V8 默认栈容量(约 2 万帧)时抛 RangeError: Maximum call stack size exceeded。根因是「用调用栈承载遍历深度」这个机制本身,而非某条形状判断写错。
  3. 影响范围与严重程度:触发面窄——body 须先过 oversize 拒绝闸的最坏校准估算门(ceil(ceil(bytes/4)*1.14) > window,且 window 下限 1M ⇒ 约需 ≥3.5MB),同时嵌套深到 ~1 万+ 层才走到遍历。命中时:opencode-native V2 路由已有 try/catch(降级为全按正文计费,安全);native-intercept 共享臂与 dsh 委托路径未加保护,未捕获 RangeError 会沿 fetch 链上抛,使该次模型请求硬失败而非被拒/放行。普通流量不受影响 → p2(特定大+深 body 下的个别功能异常,非数据丢失/会话不可恢复/缓存泄露)。
  4. 怎么修复的:把递归改成显式栈迭代 DFS(pending 数组 push/pop),所有媒体形状判定点逐字保留、只换遍历机制——从根上消除「遍历深度吃调用栈」这一生产者,而不是在各调用点再补 try/catch 遮症状(疏,不是堵)。
  5. 是否有回归问题:无。逐形状字节核算与旧实现完全一致(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 精确字节数测试全绿);DFS 访问顺序虽变但不影响求和(加法可交换);JSON.parse 不产生共享引用或环,每个节点仍恰好访问一次,无重复计数。高危模式逐项过:无阈值/时序/输出格式漂移、无 fail-fast 变吞错(反而把唯一会抛的路径改成 total)、无默认值变化。
  6. 是否破坏缓存:不涉及。纯客户端本地计费函数,不触碰缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及。仍是同一套「结构键控、协议无关」的媒体字节遍历,仅遍历方式由递归转迭代。
  8. 是否引入了新的配置:不涉及。无新 env/config/默认值。

收工判定:可合 —— 修复正确、回归测试有牙、typecheck 与定向回归全绿、diff 干净;建议 rebase 到当前 master 后交 CI 全量把关,人工合并。

…ei#6038)

Reviewer-added complement to the media-bytes walk fix in this PR: the same
unbounded-recursion mechanism lives in estimateRawBodyTokens and
estimateRawBodyBreakdown (src/preflight.ts), which walk every string leaf of
the parsed client body on the proxy main path (handle.ts rawInputTokens
stamping) and the side-request guard. A 20,000-level body overflows their
recursive walks with RangeError exactly as it did mediaBytesOfWireBody.

Converted both to the same iterative explicit-stack traversal; key
inheritance, the NON_TEXT_BODY_KEYS exclusion set, top-level bucketing and
the total invariant are preserved byte-for-byte. Adds
tests/raw-body-walk-depth.test.ts: 20,000-level object and array chains with
media-shaped bottoms (fail with RangeError on the recursive versions), plus
shallow plumbing pins for exclusion/inheritance/bucketing.
@ranxianglei
ranxianglei merged commit 25b1671 into ranxianglei:master Nov 10, 2026
52 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1142-master.25b1671 (rolling master tag).

npm install -g billion-context@0.0.1142-master.25b1671  # 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

bug Something isn't working p2 scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Account for deeply nested media without recursive stack growth

2 participants