Skip to content

[PR] fix(dsh): restore tools after registration owner teardown - #6706

Merged
ranxianglei merged 2 commits into
ranxianglei:masterfrom
Delthin:2026-11-14_dsh-tool-registration-lifetime
Nov 15, 2026
Merged

ranxianglei merged 2 commits into
ranxianglei:masterfrom
Delthin:2026-11-14_dsh-tool-registration-lifetime

Conversation

@Delthin

@Delthin Delthin commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #6702. Follow-up to #2101; preserves the registration-adoption / proxy-respawn behavior from #3380 and #3393.

Root cause

register.toolsReady outlives effect-owned host tool registrations. A Cordis fiber restart can reuse the same Context after disposing those registrations, so the context-identity-only reset leaves the latch true and skips recovery. A newer activation can also adopt names whose registrations still belong to an older activation; disposing that owner removes the adopted tools.

Changes

  • Track readiness per activation and invalidate it when its Cordis effects are disposed.
  • Check live registrations and restore only missing manifest tools after borrowed registrations disappear, preserving registrations owned by other contexts.
  • Fence manifest completion, error handling and pending-flight cleanup so a disposed or superseded activation cannot mutate the current activation.
  • Keep surviving tool registrations usable across proxy respawns; execution closures continue reading the current proxy origin.

Validation

The original local diagnosis used real Cordis, ToolsService, agent-scope SDK rendering and the shared tool-execution pipeline; a guarded local patch was additionally verified with successful native acp_status and compress calls after a managed restart. No live service changes are part of this upstream PR.

No config, wire protocol, persistence format, package version, or runtime dependency changes.

@ranxianglei ranxianglei changed the title fix(dsh): restore tools after registration owner teardown [PR] fix(dsh): restore tools after registration owner teardown Nov 14, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。本 PR 针对 #6702 的 dsh 工具注册生命周期问题;已核对 open [PR] 列表,无同主题并行 PR。

Review verdict: mergeable (high quality).

Root cause (independently verified)

register.toolsReady is a module-level latch whose lifetime outlives the effect-owned host tool registrations it tracks. A Cordis fiber restart can reuse the same Context after those registrations have been disposed, so the old context-identity-only reset (if (register.toolsReady && activeCtx !== undefined && activeCtx !== ctx)) does not fire (same ctx) — the latch stays true and recovery is skipped. A second, related path: a newer activation can adopt tool names whose registrations still belong to an older activation; disposing that older owner then removes the adopted tools.

The PR's stated root cause matches my independent analysis.

The fix (src/agent/dsh-native.ts)

  • Introduces a per-activation ToolsOwner ({ ctx, closed, restoring, definitions }) tracked by module let toolsOwner.
  • toolsAreReady() now checks live registrations via ctx.tools.get (when the host exposes it) rather than trusting the stamp; hosts without tools.get fall back to the old stamp (no-op).
  • restoreTools(owner) restores only missing manifest tools, preserving borrowed registrations owned by other contexts.
  • beginToolsOwner(ctx) reuses an open owner for the same ctx (no effect), otherwise closes the old owner and starts fresh; wires a ctx.effect disposer and a tools/change listener.
  • Fences manifest completion, error handling, and pending-flight cleanup so a disposed/superseded activation cannot mutate the current one.
  • apply() replaces the old context-identity reset with beginToolsOwner(ctx).

This is a sound root-cause fix (疏, not 堵): it removes the mechanism that produces the stale latch (the latch outliving its registrations) rather than masking the symptom at a cheaper layer.

Tests (verified)

CI status (head d1b02b3)

Minor concerns (non-blocking)

  1. Import placement: import { Context, Service, type Plugin } from "@deepseek-ai/cordis" was at line 162 (mid-file, after a test). I moved it to the top (line 3) locally and re-verified the 4 tests still pass — ESM hoisting means no behavior change. (Local edit on my review branch, not pushed to the PR branch.)
  2. toolsAreReady() side-effect: the predicate name implies a pure read, but it mutates register.toolsReady to the live value. Code smell, though consistent with the file's existing mutation style.
  3. Fiber-state magic numbers: fiber.state 1 (LOADING) / 2 (ACTIVE) are read via a cast to an internal Cordis property not in the PluginContext type; explained in a comment.
  4. New dev dependency: @deepseek-ai/cordis@4.1.0 (test-only, exact-pinned). Justified for the real-Cordis regression; the test file imports it at top level, so the whole file fails if it's unavailable (it's a devDep, so CI has it).
  5. Host capability dependency: the fix's live-check path only works for hosts exposing tools.get; hosts without it silently degrade to the old stamp (no-op). Graceful degradation, not a regression, but worth noting.

结构化八问

  1. 是什么问题:dsh 宿主在 Cordis 纤维重启后,工具注册被销毁但 toolsReady 闩锁仍为真,恢复逻辑被跳过,工具丢失。
  2. 什么原因导致的:register.toolsReady 是模块级闩锁,生命周期长于它所跟踪的 effect 拥有的工具注册;同一 Context 复用后,旧的「仅按 Context 身份」重置不触发(同一 ctx),闩锁保持为真。
  3. 影响范围与严重程度:仅影响 dsh 原生车道在「纤维重启复用同一 Context / 借用注册被旧 owner 销毁」的场景;严重度——工具完全丢失(不可用),但触发条件特定。
  4. 怎么修复的:按激活(activation)跟踪就绪状态,在其 Cordis effect 被销毁时失效;只恢复缺失的 manifest 工具,保留其他 Context 拥有的注册;用 fence 防止已销毁/被取代的激活修改当前激活。
  5. 是否有回归问题:审过 dsh中工具会突然消失 #2082/dsh-native: apply() on a re-activated context doesn't reset register.toolsReady — tools stay registered only on the dead context #2101/Windows/dsh 宿主重启:代理就绪超出 SPAWN_WAIT_MS(20s) 导致 bootstrap 失败,本进程内无重试 → /bili/origin 恒为 null、设置面板永久"未绑定"、整场会话直连(压缩失效) #2187/Plugin tool registration self-lock: after a failed proxy respawn, re-registration is blocked by its own prior registrations — identity headers lost forever (wire mode) → 413 preflight_compress_failed on large sessions | [Bug] 插件工具注册状态自锁:proxy respawn 失败后重注册被自己挡死,永久失去身份头(wire 模式)→ 大会话 413 preflight_compress_failed #3380/dsh-native never reconnects after respawn gave up (healthy proxy listening) → unattributed over-window body refused locally (422) → session unrecoverable | [dsh-native] respawn 放弃后客户端永不重连(代理已健康监听)→ 未归属超窗 body 被本地 422 拒绝、会话不可恢复 #4755 既有行为——全部保持(95 passed 0 failed);dsh-native: apply() on a re-activated context doesn't reset register.toolsReady — tools stay registered only on the dead context #2101 健康同 Context 重入与 Plugin tool registration self-lock: after a failed proxy respawn, re-registration is blocked by its own prior registrations — identity headers lost forever (wire mode) → 413 preflight_compress_failed on large sessions | [Bug] 插件工具注册状态自锁:proxy respawn 失败后重注册被自己挡死,永久失去身份头(wire 模式)→ 大会话 413 preflight_compress_failed #3380 代理重生/采纳测试不变且通过。无回归。
  6. 是否破坏缓存:不涉及缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及架构/边界/数据流转向;是 dsh 工具注册生命周期的局部修复。
  8. 是否引入了新的配置:无新配置/env/默认值。新增 @deepseek-ai/cordis@4.1.0 为 devDependency(仅测试用,不参与运行时/打包)。

判定:可合并(mergeable)。 根因修复扎实,测试充分(含真实 Cordis 回归),既有行为全部保持。CI 红为继承性失败(pi-subagents win-22,非本 PR 触碰范围),建议查 #4599。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

CI 更新(head d1b02b3 已全部跑完):

结论不变:可合并(mergeable)。本 PR 自身的全部测试车道通过,唯一红为继承性 pi-subagents win-22。

Minor style nit from review: the @deepseek-ai/cordis import was placed
mid-file (line 162, after a test). ESM hoisting means no behavior change;
moving it to the top follows the repo convention of imports-first.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

小修已推到 PR 分支: 前面提到的 import 位置风格问题(@deepseek-ai/cordis import 原在文件中部 line 162),已作为独立标注 commit 推到 PR 分支(ESM hoisting,无行为变化,4 个新测试本地复验通过)。新 head:2561732211(2 commits)。这会触发一次新的 CI 跑;结论不变——可合并。

@ranxianglei
ranxianglei merged commit ad8600f into ranxianglei:master Nov 15, 2026
54 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner

Closes pair #6702 was auto-closed by the batch merge.

Superseded by #6765 (merged to master as ad8600f).

Your commit 2561732 is in master verbatim — same content, same authorship, only the object ID changed (the batch used a real git merge, no rebase or cherry-pick rewrite). Merged under a different branch name, so GitHub does not flip this PR by itself.

Thanks for the fix — it is live in master from ad8600f onward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dsh: stale tool readiness after same-context reload or adopted-owner disposal

2 participants