Skip to content

[PR] fix(dsh): DSH Remote long sessions skip compression due to stale fetch-chain re-entry state - #5616

Merged
ranxianglei merged 2 commits into
masterfrom
2026-11-03_dsh-remote-compression-bypass
Nov 7, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-11-03_dsh-remote-compression-bypass

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Problem

On Windows + DSH Desktop + ds-harness-remote (Go client), a long session skips compression: a 2-character turn still sends ~1.44M input tokens to the model and trips CONTEXT_WINDOW_EXCEEDED (HTTP 400). The same session on desktop compresses normally (~139K input tokens). Reported by @dongyun-666 with local repro metadata and a candidate patch.

Root cause

src/agent/fetch-chain/index.ts routes each request through dispatchBody (the compression entry) only when it sees a fresh top-level dispatch or a legit same-stack descent (chainHop.getStore() === patched). It tracks re-entry depth with two AsyncLocalStorage stores (dispatchDepth, chainHop).

AsyncLocalStorage values persist in ANY callback minted inside a .run() scope even after that fetch has settled. A NEW dsh model operation whose transport callbacks were created during an earlier fetch therefore inherits a stale non-undefined dispatchDepth (with a non-matching chainHop). Its first request lands in the depth-1 / depth>=2 branches and bypasses dispatchBody, so the full history goes straight to the model uncompressed. This matches the reporter's log signature (the llm/stream event fired but no fetch-entry ran inside dispatchBody).

The desktop path didn't hit it because its transport callbacks are minted by the client event loop as fresh top-level dispatches; the remote/harness path reuses callbacks across operations.

Fix

Add runOutsideFetchChain in fetch-chain/index.ts: run an operation with BOTH re-entry stores cleared via .exit(), scoped to the operation's own subtree. Because .exit() only affects the operation's subtree, #1662's foreign-wrapper cutoff still fires INSIDE each single-fetch descent and resumes on return — the anti-loop protection is preserved, not defeated.

Wrap every dsh llm/stream boundary (iterator factory, next, return, throw, and the hook entry) in runLlmDispatch = runOutsideFetchChain(() => dispatchScope.run(scope, op)). This enforces the invariant "a new model operation starts outside any transport fetch's re-entry scope" while keeping session attribution (sid/msgIds) and host ALS intact.

This fixes the invariant at its source (the boundary where a new operation begins), not a payload mask — robust to whichever wrapper leaks the stale context, since every operation boundary resets.

Scope

DSH lane only. pi/opencode/omp intercept each model request as a passive fresh top-level fetch from the client event loop and have no bili-controlled "new operation" boundary hook; resetting at their raw-fetch level would break the legitimate nested-descent mechanism the two stores exist for. Not changed.

Tests

tests/issue5616-dsh-remote-fetch-boundary.test.ts:

  1. primitive — runOutsideFetchChain turns a stale-context fetch back into a fresh dispatch; parent cutoff restored after;
  2. integration — a real llm/stream waterfall driven from a stale transport context still compresses at all five boundaries (creation/iterator-factory/next/return/throw), DSH0.1.7-rc.2中billion-context与dsh-codex-subscription的冲突? #1662 cutoff preserved on ordinary re-entry, parent context restored, session attribution + host ALS intact;
  3. concurrency — concurrent model ops keep their own scope (no cross-talk).

Negative control: test 2 fails when the dsh-native change is reverted (verified locally).

Behavior change

None. Pure availability fix — restores the compression entry that was being skipped. No threshold/default/timing/wire-format/output change.

Closes #5614

A new DSH model operation whose transport callbacks were minted during an
earlier fetch inherits a stale dispatchDepth/chainHop AsyncLocalStorage
context. Its first request then lands in the fetch-chain depth-1 / depth>=2
re-entry branches and skips dispatchBody (the compression entry), so the long
session's full history goes straight to the model uncompressed. On Windows +
DSH Desktop + ds-harness-remote (Go client) a 2-char turn still sent ~1.44M
input tokens and tripped CONTEXT_WINDOW_EXCEEDED (HTTP 400), while the same
session on desktop compressed normally.

Add runOutsideFetchChain (fetch-chain): runs an operation with BOTH re-entry
stores cleared via .exit(), for the operation's own subtree only, so #1662's
foreign-wrapper cutoff still fires inside each single-fetch descent and resumes
on return. Wrap every dsh llm/stream boundary (iterator factory, next, return,
throw, and the hook entry) in runLlmDispatch = runOutsideFetchChain +
dispatchScope.run, preserving session attribution (sid/msgIds) and host ALS.

Regression tests: tests/issue5616-dsh-remote-fetch-boundary.test.ts (primitive
reset semantics; integration stale-context op still compresses at all five
boundaries with #1662 cutoff preserved and parent context restored; concurrent
ops keep their own scope). Negative control: integration test fails without the
dsh-native change.

Closes #5614
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-03_dsh-remote-compression-bypass (b2a3269)

Latest build — install this exact version

npm install -g billion-context@0.0.8027-pr.5616.ec07082

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

Recent builds (commit → version)

commit version build
b2a3269 billion-context@0.0.8027-pr.5616.ec07082 build
209239a billion-context@0.0.8021-pr.5616.dbab8d5 build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix(dsh): DSH Remote long sessions skip compression due to stale fetch-chain re-entry state [PR] fix(dsh): DSH Remote long sessions skip compression due to stale fetch-chain re-entry state Nov 3, 2026
… fix markers

The fix comments and regression test referenced #5616, which is this PR;
the bug report is upstream issue #5614. Repo convention (issueNNNN test
names, #NNNN comment markers) cites the GitHub ISSUE the marker documents.
Rename tests/issue5616-* to tests/issue5614-* and re-point the three
comment sites. Comment/provenance only — no behavior change.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

PR 独立审核结论:可合并(等 CI 在新 head 上全绿;合并按惯例由人执行)。 我没有照搬原 issue 线程的结论——把 base(9dc4c94) 与 head 两侧的 src/agent/fetch-chain/index.ts、src/agent/dsh-native.ts 全文重读了一遍,自己推演了门控逻辑与 ALS 语义,本地跑了定向测试(含负向对照)、typecheck 和相邻套件。查重:本 PR 一一对应上游 issue #5614,无平行修复在途。

我独立验证过的事:

顺手修了一个小问题(已作为独立标注 commit 推到 PR 分支):代码注释与测试文件名引用的 #5616 其实是本 PR 自己的编号,bug 报告是上游 issue #5614(已用 GitHub API 核实:5614=issue、5616=本 PR)。仓库惯例(issueNNNN 测试名、#NNNN 注释标记)指向 issue 编号,故把测试文件改名为 tests/issue5614-dsh-remote-fetch-boundary.test.ts 并修正三处注释标记。纯注释/命名,零行为变化,改名后测试重跑 3/3 绿。commit:b2a3269fa(新 head)。

结构化八问:

  1. 是什么问题:DSH Desktop + ds-harness-remote(Go 客户端)形态下长会话压缩被跳过——发两个字就把整段历史(约 144 万 input tokens)直发模型,触发 CONTEXT_WINDOW_EXCEEDED(HTTP 400);同一会话桌面端正常压缩(约 139K)。
  2. 什么原因导致的:fetch-chain 用两个 AsyncLocalStorage(dispatchDepth/chainHop)判定「是否在一次 transport fetch 内部下探」,而 ALS 的值会留在该作用域里铸造出的任何回调中、跨 fetch 存活。Remote/harness 这条路径跨模型操作复用 transport 回调,于是新操作继承了上一轮 fetch 遗留的非 undefined dispatchDepth(配一个对不上的 chainHop),首个请求被门当成「中途重入」,绕开压缩入口直发。桌面端回调由事件循环全新铸造,不踩。
  3. 影响范围与严重程度:仅 DSH 车道且宿主会跨操作复用 transport 回调的形态(Remote/harness);命中即长会话不可用(窗口撑爆、无法自愈)——p1。桌面形态与 pi/opencode/omp 等以全新顶层 fetch 拦截的车道不受影响。
  4. 怎么修复的:在不变量的源头强制它——「一次新模型操作必须从任何 transport fetch 的重入作用域之外开始」。新增 runOutsideFetchChain(两个 store 都 .exit(),只作用于操作自身子树),把 dsh llm/stream 的全部五个边界包成「清场后重新进入 sid/msgIds 作用域」。不是载荷掩码:无论哪个 wrapper 泄漏旧状态,每个操作边界都会重置;父级重入上下文不被触碰,所以 DSH0.1.7-rc.2中billion-context与dsh-codex-subscription的冲突? #1662 防循环截断在单次 fetch 下探内部照常生效、返回后恢复。
  5. 是否有回归问题:逐项审过既有行为——DSH0.1.7-rc.2中billion-context与dsh-codex-subscription的冲突? #1662 外层包装器截断(保留,测试正反双向钉住);[PR] pi inline mode: add Codex WebSocket channel — local loopback WS bridge + relaxed toggle value constraints | pi inline 模式补上 codex WebSocket 通道:本地回环 WS 桥 + 开关取值放宽 #4527 同栈下探(逻辑未动);bili-native 安装/re-arm globalThis.fetch 不校验所有权,与网络 scope 类插件交错后全局 fetch 悬空 → 全进程 "baseFetch is not a function" #1410/方式一安装的dsh 原生插件:llm-pi-ai 传输(OpenAI SDK + options.fetch 注入)绕过 globalThis.fetch 拦截,模型请求不进代理、压缩完全不生效 #1158 死闭包重锚/自愈重装(fetch-chain diff 纯新增,未触碰);会话归属 sid/msgIds(保留,五边界断言钉住);宿主自有 ALS(保留,断言钉住);agentless 侧车直通(不变);其他车道(未触碰)。高危模式逐项过:无阈值/默认值/时序漂移、无 fail-fast 变吞错、出站字节格式不变。桌面路径上 .exit() 对干净上下文是空操作,行为逐字节一致。
  6. 是否破坏缓存:不涉及。未碰缓存键/重试/增量游标/去重键;恢复被跳过的压缩入口本身就是长会话前缀缓存健康的前提。
  7. 是否改了设计方向:不改。沿用既有 fetch-chain 重入模型与 dsh waterfall 结构,只补一条操作边界不变量。作用域限定 DSH 车道是对的:pi/opencode/omp 没有 bili 可控的「新操作边界」钩子,在裸 fetch 层重置反而会破坏这两个 store 要支持的合法嵌套下探。
  8. 是否引入了新的配置:不涉及。无新 env/config/CLI/默认值。

判定:方向对(修的是根因层的不变量,不是症状掩码)、实现层次正确、测试有牙且双向、diff 干净、无回归发现。等 CI 在 b2a3269fa 上全绿后即可由人合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

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

Review verdict: ✅ APPROVE / recommend merge (posted as a comment — GitHub blocks a formal self-approval of the author's own PR). Verified end-to-end against the PR head b2a3269fa.

What it does. A new DSH model op whose transport callbacks were minted during an earlier fetch inherits a stale dispatchDepth/chainHop AsyncLocalStorage context, so its first request lands in the fetch-chain depth-1 re-entry branch and skips dispatchBody (the compression entry) — the long session then ships uncompressed (repro: Windows + DSH Remote + Go client, a 2-char turn sent ~1.44M tokens → CONTEXT_WINDOW_EXCEEDED). The fix adds runOutsideFetchChain (clears BOTH stores via .exit(), scoped to the operation's own subtree) and wraps every dsh llm/stream boundary (hook entry, iterator factory, next, return, throw) in runLlmDispatch = runOutsideFetchChain + dispatchScope.run.

Correctness. Traced the patched gate before/after: a fresh op now sees depth===undefined → dispatchBody runs. .exit() affects only the op's subtree, so #1662's foreign-wrapper cutoff still fires inside each single-fetch descent and resumes on return — anti-loop preserved, not defeated. Session attribution (sid/msgIds) and host ALS ride every boundary. DSH-only scope is correct: pi/opencode/omp intercept each model request as a passive fresh top-level fetch with no bili-controlled "new operation" boundary, so resetting at their raw-fetch level would break the legit nested descent these stores exist for.

Tests. Ran the new suite on the PR head: 3/3 pass. Proved the negative control — disabling the clearing (runLlmDispatch → plain dispatchScope.run) flips the integration test red while the primitive + concurrency tests stay green, so the test pins exactly this regression. Both directions asserted (dispatch-when-should AND cutoff-when-should-not), plus concurrency no-cross-talk and host-ALS non-clearing. Meets the gate-discipline bar.

Regression surface. None: DSH-lane only; no config schema / env / CLI change, no kernel/, no wire/message-shape change, no threshold/default/timing drift, no version bump (version-guard green). Pure availability restoration.

Conventions. Clean 3-file diff (+378/−6); comments explain WHY with #5614 refs; no dead code / as any; agent module uses host logging correctly; test named issue5614-* per convention; two linear commits (fix + docs citation rename #5616→#5614, zero behavior).

Non-blocking scope note: verification exercised the reported full-resend (llm/stream) path; I did not independently drive the anthropic-wire incremental lane, so this covers the reported bug's path rather than asserting completeness of that separate transport.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

PR #5616 reviewed end-to-end → APPROVE / recommend merge (head b2a3269fa).

Ran the new suite directly on the PR head: 3/3 pass. Proved the negative control — disabling the fetch-boundary clearing (runLlmDispatch → plain dispatchScope.run) flips the integration test red while the primitive + concurrency tests stay green, so the test pins exactly this regression. Fix is DSH-lane-only: no config schema/env/CLI change, no kernel/, no wire/message-shape change, no threshold/default/timing drift, no version bump (version-guard green). Pure availability restoration of compression for DSH Remote long sessions.

Verdict posted as a GitHub review comment on the PR (GitHub blocks a formal self-approval of the author's own PR, so it's recorded as a comment rather than an APPROVE event).

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

That is the upstream-synced copy of the review I already posted on PR #5616 — no new input here. Review stands: APPROVE / recommend merge; verdict is recorded on the PR itself. Nothing further pending on my side.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1054-master.6db0e84 (rolling master tag).

npm install -g billion-context@0.0.1054-master.6db0e84  # 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.

Louie0806 added a commit to Louie0806/billion-context that referenced this pull request Nov 7, 2026
ranxianglei added a commit that referenced this pull request Nov 7, 2026
…4241 teeth at the byte-promised tail

CI red on this PR had three causes:

1. storage-render + wire-contract goldens were regenerated against a kernel
   WITHOUT this PR's config (the committed metadata digest matches what
   master produces, not this branch): effectiveConfig in the persisted
   metadata now carries nudge.requireUsageSampleForPressure + nudge.
   maxEstimateHoldRounds, so every metadata digest moved again. Regenerated
   both golden sets with the local 0.0.185 kernel.

2. #4241 teeth: the fault dropped outbound[length-2] on the assumption the
   engine appends a trailing nudge as the final wire message. Since #5772
   the snapshot render covers the previous turn's tail too (tailStart
   advanced), so length-2 lands INSIDE the exempt rendered prefix and the
   teeth never fire. The fault now drops the client's newest tail row
   (located by content) — the part the render lane actually owns
   byte-intact. Verified the same fix also passes on current master, where
   this test is broken since #5772.

Note: storage-render/#4241/wire-contract/storage-engine-proofs also fail on
master HEAD (run 42358603178) — #5772/#5616-era fallout, not this PR; the
nudge e2e subset that master broke (#728A/C, #1119, #1137, zero-baseline)
is fixed by this PR's backstop.
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