Skip to content

fix: hold dsh first model request until ACP tool registration (+ real-dsh headless native-lane E2E) - #1275

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-24_dsh-r1-stamp-gate
Sep 26, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-09-24_dsh-r1-stamp-gate

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1268
Fixes #1274

What this does

1. Fix — R1 stamp race on the dsh native spawn lane (#1274, found while building the E2E).

A fresh dsh native session fires its first model request as soon as the proxy origin is ready, but ACP manifest registration lands ~90ms later (proxy spawn dominates boot, ~850ms). The first request short-circuits at headersFor (!toolsReady) before the session id is even read and rides wire mode un-stamped — no x-bili-plugin header, anonymous pfa-* session, no named conversation. That is exactly the #1158 "zero proxy traffic silently unclaimed" failure class, and it is deterministic for every single-shot headless invocation (dsh --profile headless "<task>"), where the whole session IS round one.

Observed timeline (BILI_DBG + bili.log):

apply@06.666 → inject-cb@06.689 → bili proxy up@07.518 →
R1 intercepted@07.700-709 → ACP manifest registered@07.792-796

The fix adds an optional toolsReady?: Promise<unknown> to NativeInterceptState. Model-API requests await it (bounded by readyTimeoutMs, default 15s) before headersFor is consulted — in both the routed (/bili/) and main rewrite paths of src/agent/native-intercept.ts. Only the dsh lane arms it (src/agent/dsh-native.ts apply() resolves it when the first registration attempt finishes, success or failure); every other lane leaves it undefined and is behaviorally unchanged (one extra microtask on the routed path at most). Timeout or failed registration falls through to exactly today's wire-mode behavior; a later maybeRetry success flips stamping on without re-arming. A one-shot warn is logged when a request was held ≥50ms: [bili-native] held model request ${held}ms for ACP tool registration (#1268).

2. Test — the issue's actual deliverable: real dsh + bili profile vs deterministic fake upstream (#1239's four acceptance items).

tests/e2e/e2e-native-dsh.test.ts drives a real @deepseek-ai/dsh (pinned 0.1.5-rc.3) in its production profile-install form — hermetic DSH_HOME, wired headless profile (local build into the profile node_modules via the shipped dsh.bundle.patch.yml), llm-pi-ai fake provider pointing at tests/e2e/fake-upstream-chat.mjs, cwd outside the repo — and asserts on the JSONL oracle:

  1. EVERY request hop carries x-bili-plugin=dsh + one stable x-bili-plugin-conversation=session-* id — direct defense of 方式一安装的dsh 原生插件:llm-pi-ai 传输(OpenAI SDK + options.fetch 注入)绕过 globalThis.fetch 拦截,模型请求不进代理、压缩完全不生效 #1158;
  2. /acp binding + status endpoint reachable live (GET /__bili/plugin/status?conversationId=<conv> against the instance-record origin);
  3. acp_status really executes client-side (tool call + panel result visible in re-sent history — plugin-mode carrier);
  4. compress really folds: persisted kernel session blocks>=1, metadata.pluginAgent=dsh, [Compressed m…–m… → N block(s), … tokens saved.] result row.

The scripted sequence (filler ×~63KB → acp_status ×3 → compress of the oldest user message) runs inside ONE headless process because headless has no --resume — same driving style as the #1158 floor-8 harness.

dsh-specific risks answered in-thread: (a) headless drives fine non-interactively — no HTTP-only degraded harness needed; (b) dsh-http-proxy coexistence: rc.3 installs an undici dispatcher, not a globalThis.fetch replacement, so it structurally cannot evict the patch — the E2E ships with proxy env vars pointed at a dead loopback port and asserts every hop stays stamped, pinning that layering against future drift; unit-level re-arm behavior remains pinned by tests/native-intercept-selfheal.test.ts (#1187/v0.1.140 guarded accessor).

Gating mirrors the pi lane (#1246): ACP_TEST_E2E_DSH_NATIVE=1 (skip by default), E2E_CHECK=1 zero-token preflight; .github/workflows/ci-e2e-dsh-native.yml mirrors ci-e2e-native.yml (manual-dispatch + PR trigger).

⚠️ Behavior change (old → new)

  • OLD: the first model request of a fresh dsh native session could arrive un-stamped (wire mode) whenever it beat manifest registration (~90ms gap); single-shot headless invocations were always wire mode.
  • NEW: such requests are held ≤ readyTimeoutMs (default 15000ms; in practice <100ms) and arrive stamped (plugin=dsh, named session-* conversation) — plugin mode from round 1. On timeout or failed registration the request proceeds exactly as before (wire mode fallback). Only the dsh lane arms the gate; pi/opencode/other lanes are unchanged.
  • E2E-only: the test's bili config sets compress.preserveRecentTokens: 0 so the kernel's default 5K-token protection window doesn't swallow the scripted filler; precedent = src/preflight.ts:900 auto-relaxes exactly these knobs on overflow. Production defaults untouched.

Verification (rebased head, kernel 0.0.86)

  • npm run typecheck clean
  • npm test: 2495/2504 pass, 0 fail (9 skipped by design)
  • npm run build OK
  • New E2E: 2/2 green (preflight + full one-shot scenario, zero tokens)
  • Negative control: temporarily reverting the gate arming to the pre-fix registration kick makes the E2E RED at the first-hop stamp assertion (request must be plugin-stamped (x-bili-plugin), got null); restored → green. The assertions have teeth.

Commits: fix: (gate, 3 files +172/-5 incl. unit tests) + test: (E2E + workflow + fake-upstream oracle fields, +815/-2).

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-24_dsh-r1-stamp-gate (d9e6a26)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1275

Each push to this PR publishes a new version under the pr-1275 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1275.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

[bot] 🏷 收到。开始 review PR #1275(Closes #1268,Fixes #1274)。初步检查已过:分支已 rebase 到当前 master(088bb34),diff 干净(6 文件 +987/-7,无无关改动),方向正确——hold 首请求直到注册完成是决策点级的根因修复(R1 无法事后补章,#1158 同类失效类别),#1274 带合规来源标记。继续:E2E 测试有效性、native-intercept 共享路径的回归审计(headersFor 纯度 / state.ready 失败模式)、rebased head 本地 typecheck+test+build、CI 状态。结论稍后回贴。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 结论:可以合并(合并由人来)。PR #1275(Closes #1268,Fixes #1274)五项审查全部通过,证据如下。

① 原 issue 方向关:通过。#1268 要求真 dsh + 确定性假上游零 token 断言 #1239 四项验收——PR 逐项落地且无降级方案(headless 可非交互驱动已实测)。#1274 的修复方向正确:盖章决策必须发生在「注册尝试完成」这一事实之后,R1 无法事后补章,hold 是决策点级根因修复而非症状掩蔽;有界(readyTimeoutMs 默认 15s)+ 超时/失败精确回落旧 wire mode,符合「疏不堵」。

② 回归审查(diff 触碰路径的既有行为清点,逐项结论):

  • 非 dsh lanes(pi/opencode/omp/…):toolsReady === undefined → gate 立即返回(至多一个微任务)。行为保持(代码核对 + 全量测试 + pi/opencode e2e CI 绿三重确认)。
  • routed /bili/ unattributed 分支:输出与旧版逐字节一致(passthrough header + 早退);headersFor 求值时机未变——旧三元是惰性的,unattributed 时同样不求值,故其内部副作用(maybeRetry、refreshModelInfo)触发条件不变。保持。
  • main rewrite 路径:非 dsh 仅多一个微任务;dsh hold 到注册完成——本 PR 的有意变更。
  • withTimeout 泛型化:向后兼容;timer 在 finally 清理(无泄漏,不会拖住 headless 进程退出);既有 catch/log 行为保留。保持。
  • 退化路径核查:state.ready 不可能 reject(bootstrap 与 verifyAttachAndRecover 均内吞所有错误并 resolve undefined)→ gate 必然 settle;bootstrap 彻底失败时 gate 立即以 undefined 解除 → 精确等于今天的 wire-mode 直连,不存在每请求挂 15s 的退化。
  • 共享文件 tests/e2e/fake-upstream-chat.mjs(pi lane 也用):directive 源回退仅在「最后一条 user 无标记 && 首条有标记」时触发;pi 的 prompt 恒占末位 → queueKey 不变。CI e2e-native-pi 在本 PR head 上绿,实证无回归。保持。

③ 代码审查:gate 设计正确——首次注册尝试完成(成功或失败)即解除,后续 maybeRetry 成功无需重新 arm 即可翻转换章;一次性 warn(held ≥50ms)便于观测竞争窗口;测试钩子 _stateToolsReadyForTest 最小化;console.warn 在宿主侧插件模块合规(非 proxy server 模块)。未发现逻辑/错误处理问题。

④ 测试有效性:单元 3 例断言有牙(精确 header 值 + 时序边界 sink[0].at >= releasedAt - 5,钉住 hold-until-stamp、timeout 回落、routed 路径三态)。E2E 四项验收均为多层真实证据:① 逐跳遍历 oracle 断言 plugin==="dsh" + 单一稳定 session-* conv(非只测首跳);② mid-run 探测 /__bili/plugin/status(避开 parent-watched proxy 随 dsh 退出的竞态);③ acp_status 结果匹配真实 status report 形状;④ compress 折叠正则 + proxy 日志 tool compress executed via plugin + 持久化 session blocks>=1 + pluginAgent=dsh(wire/log/disk 三层)。hermetic(mkdtemp DSH_HOME/XDG、listen(0) 端口、cleanEnv 剥离 NODE_TEST_CONTEXT 与 bili 侧信道、TMO SIGKILL 兜底、teardown 清残留)。作者负向对照可信:还原 gate → 恰在首跳盖章断言变红(got null),正是 #1274 的失效形态。

⑤ diff 整洁度:6 文件全部与 PR 目的相关,无空行/重排噪音,无锁文件扰动,无 version 改动。

⚠️ 行为变更披露(按要求):

验证证据(rebased head a5d744d,当前 master 之上):本地 typecheck 干净;npm test 2504 例(2495 pass / 0 fail / 9 skip)——与作者报告一致;build OK。GitHub CI 在该 head 上全绿:CI、e2e-native-dsh(新 workflow,真 dsh 跑通)、e2e-native-pi、e2e-fake-codex、Windows codex overlay、PR Build Artifact。PR mergeable_state: clean。

热文件协调提醒(不阻塞本 PR):#1272(opencode native E2E)也改 tests/e2e/fake-upstream-chat.mjs(+50/-10),与本 PR 在 answerFor 相邻行和 oracle JSONL 字段处重叠。两者语义互补(opencode title-call 早退位于 dsh directive 源回退之上;#1272 的平衡括号解析器与本 PR 脚本兼容)——但谁后合谁需要小 rebase + 重跑自己 lane 的 CI,按 intent 并集解决即可,风险低。另注:master 现行 parseDirectives 正则不匹配嵌套花括号,本 PR 脚本里显式 pin 的 compress 参数目前会落入 no-args harvest 路径(测试仍有效,#1272 落地后 pin 才真正生效)——属既有行为,不另立项。

小观察(非阻塞,不立项):stopProxiesGracefully 的 SIGTERM 后固定 500ms flush 等待沿袭 pi suite 模式,极慢 runner 上有理论竞态,CI 已验证可用。

一句话中文摘要:review 通过——PR 修复了 dsh native lane 首请求抢跑导致整会话静默不认领的盖章竞争(#1274,根因级 hold-gate 修复,仅影响 dsh lane,超时精确回落旧行为),并按 #1239 四项验收落地真 dsh headless 零 token E2E(逐跳盖章断言有牙、负向对照已验证),本地与 CI 全绿、diff 干净,可以合并;唯一注意项是与 #1272 共享 fake-upstream 文件的合并顺序协调。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Independent deep review complete — executed for real, locally: rebased onto current master (clean, head c7f345f), built, ran the unit gates AND the full e2e against the real dsh 0.1.5-rc.2 binary with the deterministic fake upstream (zero tokens):

✔ preflight (E2E_CHECK)
✔ headless one-shot: intercepted + claimed on EVERY hop, /acp status live,
  acp_status + compress really execute  (2.7s)
unit: native-intercept 27/27 · full suite 0 fail · typecheck ✓ · build ✓

The fix is correct and narrow:

  • Race: dsh fires R1 ~90ms before the manifest lands → the first model request rode wire mode silently because headersFor froze an un-stamped decision. Fix = optional toolsReady gate on NativeInterceptState; model requests hold (bounded by readyTimeoutMs, default 15s) until the host's ACP tool registration finished its FIRST attempt — success or failure.
  • Ordering nailed: the gate clears before headersFor is consulted (evaluating it pre-gate would freeze the wrong decision forever); unattributed/passthrough traffic skips the wait; steady state costs nothing (resolved promise); timeout or failed registration falls through un-stamped = exactly today's behavior; later maybeRetry success flips stamping on without re-arming.
  • Scope: only dsh arms the gate (where the race was observed); every other lane leaves it undefined = byte-identical behavior.
  • 3 new unit tests pin the semantics (hold-then-stamp, timeout fallback + later stamp, routed /bili/ hop also holds); e2e asserts EVERY forwarded hop carries the plugin claim.

Ready to merge when CI is green.

@ranxianglei

Copy link
Copy Markdown
Owner Author

独立复核完成 — 可合并

验证链(全在 c7f345f 上)

  • typecheck ✓;native-intercept + dsh 定向 59/59;全量 2639 / 0 fail(11 个 e2e 门控 skip);build ✓

修复本体(#1274 R1 印记竞速)逐点核过

  1. 门的位置是对的:await waitToolsGate() 在 headersFor 之前——注释把陷阱写透了:headersFor 读宿主实时注册态,若先求值会把"未盖章"的决定永久冻结。归因路径与初始 bootstrap 路径都过门,unattributed 路径不过门(它本来就不是模型请求,盖章无意义)——三条路径的区分正确。
  2. 失败语义保持原状:注册首次尝试无论成败都 resolve 门;失败时 headersFor 仍返回 undefined(wire mode,与 PR 前逐字节相同),之后 maybeRetry 成功可再翻开盖章,无需重臂门。state.ready 为 undefined(代理没起来)也 resolve——门不会卡死。
  3. 无死锁、有界等待:最坏 15s(readyTimeoutMs)后照旧放行;稳态零成本(已 resolve 的 promise)。
  4. 测试钉死三种形状:hold→盖章 / 超时→wire mode+后续盖章 / 已路由 /bili/ URL 也先等门再盖。时序断言(sent only after release)是真的在验"held"而非只验结果。
  5. dsh-native 接线:toolsReady 在 apply() 同步置上;register.toolsReady 已完成则立即 resolve(无谓等待窗口为零)。

E2E(#1268 车道)

非阻塞观察

  • gateLogged 是拦截器实例级单次日志(首个被 hold ≥50ms 的请求记一条,后续静默)——boot 竞速场景一条就够,合理。

无阻塞项,建议合并。(#1274 自动关;#1268 本体由这条的 E2E 车道覆盖,合并后可一并关;#1346 同步。)

ework-agent added 2 commits September 25, 2026 23:50
A fresh dsh native session fires its first model request as soon as the
proxy origin is ready, but ACP manifest registration lands ~90ms later
(proxy spawn dominates boot). The first request then short-circuits at
headersFor (!toolsReady) and rides wire mode un-stamped — the #1158
'zero proxy traffic silently unclaimed' failure class, deterministic for
single-shot headless invocations where the whole session is round one.

Add an optional toolsReady gate to NativeInterceptState: model-API
requests await it (bounded by readyTimeoutMs) before headersFor is
consulted, in both the routed (/bili/) and main rewrite paths. Only the
dsh lane arms it (apply() resolves it when the first registration attempt
finishes, success or failure); every other lane leaves it undefined and
is behaviorally unchanged. Timeout or failed registration falls through
to exactly today's wire-mode behavior; a later maybeRetry success flips
stamping on without re-arming.
#1268)

Drives a real @deepseek-ai/dsh (pinned 0.1.5-rc.3) in its production
profile-install form — hermetic DSH_HOME, headless profile, local build
wired into the profile node_modules with the shipped dsh.bundle.patch.yml,
llm-pi-ai fake provider pointing at tests/e2e/fake-upstream-chat.mjs — and
asserts the four #1239 acceptance items on the JSONL oracle:

1. EVERY request hop carries x-bili-plugin=dsh + one stable
   x-bili-plugin-conversation=session-* id (direct defense of #1158);
2. /acp session binding + status endpoint reachable live
   (GET /__bili/plugin/status?conversationId=<conv>);
3. acp_status really executes client-side (tool call + panel result in
   re-sent history);
4. compress really folds (persisted kernel session blocks>=1,
   metadata.pluginAgent=dsh, '[Compressed ... N block(s)' result row).

The dsh-http-proxy bundle coexistence case ships as env-only (proxy vars
pointed at a dead loopback port): rc.3 installs an undici dispatcher, not
a globalThis.fetch replacement, so it cannot evict the patch — the
every-hop stamp assertion pins that layering against future drift.
Gate ACP_TEST_E2E_DSH_NATIVE=1 (skip by default), E2E_CHECK=1 preflight.
CI workflow mirrors ci-e2e-native.yml.
@ranxianglei
ranxianglei force-pushed the 2026-09-24_dsh-r1-stamp-gate branch from c7f345f to d9e6a26 Compare September 25, 2026 15:51
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 确认收到你的独立复核——结论一致。补一个针对当前 head 的核验(我上一轮 review 的是 a5d744d7,此后分支经过两次 rebase:c7f345f(你本地复核的版本)→ 现 head d9e6a26d,已落在当前 master tip d3c8af84(#1337 native-attach-gate 合入后)之上):

  • 内容零漂移:逐文件比对新旧两版 diff 的增删行序列(有序)+ 新增行集合(sorted)→ 完全一致。三个「DIFFERS」文件(native-intercept.ts、dsh-native.ts、native-intercept.test.ts)只是 rebase 后 hunk 位置/上下文行号移动,修复与测试的实质内容与我 review 过的版本逐字节相同。
  • CI 全绿 @ d9e6a26(8/8):CI、e2e-native-dsh(真 dsh headless lane)、e2e-native-pi、E2E (real codex)、e2e-fake-codex、e2e-local-registry、Windows codex overlay、PR Build Artifact。前一个 head c7f345f 同样绿。
  • 你本地跑的是 dsh 0.1.5-rc.2,CI pin 是 0.1.5-rc.3——两个版本各自验证过,套件在 rc 区间内无版本敏感。
  • PR 当前状态:open、未合并、mergeable。

至此双重独立验证齐了(我的 review + 你的本地执行),无遗留问题。合并由你来(系统规则禁止 agent 合并):#1275

一句话中文摘要:针对 rebase 后的最新 head d9e6a26 复验通过——修复与测试内容相对我原 review 版本零漂移,8 条 CI(含真 dsh e2e)全绿,与你本地独立执行结果一致,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 确认。两轮独立 review(我的 + 你的)结论完全对齐,逐点无分歧——门位置(headersFor 之前求值会永久冻结未盖章决定)、失败语义(resolve-on-first-attempt、失败回落与 PR 前逐字节相同、maybeRetry 无需重臂)、有界等待(15s 上限、稳态零成本)、三条路径区分(routed/main 过门、unattributed 不过门)、三种测试形状 + 真时序断言、gateLogged 单次日志的合理性——全部一致。

合并目标 head 的最终状态:d9e6a26d(在你复核的 c7f345f 之上又 rebase 了一次到当前 master tip d3c8af84)。我已证明 c7f345f→d9e6a26d 零内容漂移(diff 增删行序列逐字节一致,仅 hunk 位置移动),且该 head CI 8/8 绿(含真 dsh e2e lane)——所以你的验证链 + CI 完整覆盖实际要合入的版本。(测试总数差异说明:我上轮报的 2504 是在旧 base 088bb348 上跑的,你的 2639 在更新的 base 上,中间 master 新增了测试,非不一致。)

PR 当前:open、mergeable_state: clean、head d9e6a26d。按系统规则我不能执行合并,由你来:#1275 —— 合并后 #1274 自动关闭,#1268 可按你所述随 E2E 车道落地一并关闭。

一句话中文摘要:两轮独立复核结论完全一致、无阻塞项,合并目标 head d9e6a26 已验证零漂移且 CI 全绿,万事俱备,等你手动合并即可。

ranxianglei pushed a commit that referenced this pull request Sep 25, 2026
The six integration regressions drive noteRoutedOrigin through the
_test hooks, bypassing the two real recording sites:

- fetch-patch routed branch (native-intercept): add a pre-gate proof
  (first attributed /bili/ request records state.routedOrigin while the
  toolsReady gate is still pending — the exact #1275 self-deadlock race),
  an unattributed-rider boundary test (#1117: another plugin's channel
  must not pin ours), and unit coverage for noteRoutedOrigin
  (sticky/transition-hook/malformed) plus observeRoutedOrigin
  (immediate on pre-set evidence, clean expiry, mid-window arrival).
- opencode V2 route (createNativeRoute): already-routed /bili/ URLs are
  skipped by isModelApiUrl by design, so assert the route records the
  pinned origin before that gate.

Also consumes the previously dangling noteRoutedOrigin import.
@ranxianglei
ranxianglei merged commit cf39893 into master Sep 26, 2026
12 checks passed
antclt pushed a commit to antclt/billion-context that referenced this pull request Sep 26, 2026
…unreachable attach target (ranxianglei#1365)

When a dsh/opencode attach target (BILLION_CONTEXT_PROXY) is transiently unreachable, the old verifyAttachAndRecover unconditionally fell back to spawning a second proxy instance. If the process's provider baseURL statically routes model traffic through that target (/bili/<upstream>), the model channel stays pinned to the original origin while bili tools move to the spawned one — every tool call then 404s 'unknown plugin conversation', the context can no longer be compressed, and preflight fails with 502 until the session is abandoned.

Fix: observe routed-channel evidence (state.routedOrigin, recorded by the shared fetch patch and the opencode V2 route before any gate await). With evidence, a dead attach target is WAITED ON (BILI_ATTACH_HEALTH_DEADLINE_MS, default 15s) instead of migrated away from; on timeout the failure is loud and self-healing (register base cleared, re-probed on every model request, ~10s cadence) and the env stays frozen. Without evidence, a grace window (BILI_ATTACH_EVIDENCE_GRACE_MS, default 5s) covers the decision-before-first-request race; expiry keeps the legacy ranxianglei#983/ranxianglei#1130 spawn fallback byte-for-byte. Late evidence rebinds the tool channel to where the models actually go (convergence rule).

Stacked on PR ranxianglei#1275 (toolsReady gate); rebases onto master once that lands. Closes ranxianglei#1365
@ranxianglei
ranxianglei deleted the 2026-09-24_dsh-r1-stamp-gate branch October 15, 2026 05:32
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