返回目录
Agent 与会话 插件

dsh-empty-turn-guard

Paradox1278/dsh-empty-turn-guard

A DeepSeek Harness plugin that stops silent turn death: when the upstream ends with stop_reason=tool_use but emits no tool call or text, DSH closes the turn as completed and the agent stops without reporting. This plugin steers the turn back open.

Stars
0
Forks
0
Issues
0
更新
11 天前

PROJECT TOPICS

项目标签

INSTALL REFERENCE

安装参考

未验证
dsh plugin --profile web add github:Paradox1278/dsh-empty-turn-guard

该命令指向仓库当前默认分支;尚无绑定当前 commit 的完整验证结果。

PROJECT README

README

dsh-empty-turn-guard

DSH 插件:拦截「上游声称有工具调用、却一个 tool-call 都没发」的残缺轮次,把「静默完成」改成「继续执行」。

它解决什么问题

DSH 里出现过这样的现象:一轮回复什么都没做,界面直接显示「已完成」,思考也停了。

根因链(已逐行核实):

  1. _asar2/@deepseek-ai/dsh-llm-deepseek/lib/index.js:1927-1935 把上游的 stop_reason: "tool_use" 翻译成 { kind: "tool-calls" }。
  2. _asar2/@deepseek-ai/dsh-agent-loop/lib/index.js:956-958 只看消息里有没有 tool-call 块,没有就 return { kind: "completed" },把残缺响应当成正常收尾。
  3. :790-797 于是写出 turn/end { kind: "completed" },界面显示完成。

也就是说:「结束原因是工具调用」这个信息在 agent-loop 里被丢弃了,没有 tool-call 也不报错。

实测分布(本项目 12 个会话、817 个 assistant step)

形态 命中数 占比
finish=tool-calls 但无 tool-call 块 57 7.0%
其中 只有 reasoning 块 43 75%
其中 完全空(除 usage/finish 外零块) 11 19%
其中 只有正文 text 3 5%

结论:判据不能只数「内容块数量」,那样会漏掉 75% 的 reasoning-only 情况。 必须问的是「上游说 tool-calls,但到底有没有发出 tool-call」。

它怎么修

不改 app.asar,只用官方公开扩展点:

扩展点 用途
agent/assistant-stream 观察每个 attempt 的 start / chunk / end 帧,统计内容块与 tool-call 块,记下 finish.reason.kind
agent/turn-stopping 轮次即将关闭时介入(此时 loop 还没 break)
agent.steer(msg) 往 inbox.next-step 塞一条消息,使 :797 的第二道判空失效,轮次继续

判定条件(见 index.js 的 classifyAttempt):

  • 该 attempt 的 end.outcome.kind === 'committed'(报错 / 取消 / abandoned 一律放行)
  • 且满足下列之一:
    • contentBlocks === 0(除 usage/finish 外零块)
    • finishKind === 'tool-calls' 且 toolCallBlocks === 0
  • 当前 turn-stopping 的 turn 与残缺 attempt 的 turn 相同

StreamChunk 七种类型依据 _asar2/@deepseek-ai/dsh-llm/lib/typert.host.js:596; FinishReason 五种依据同文件 :356(stop / tool-calls / max-tokens / aborted / error)。

安全设计(fail-open 优先)

  1. 默认关闭:enabled 默认 false,必须显式配置 enabled: true 才注册任何监听。
  2. 异常吞掉:每个监听器内部 try/catch,异常只记日志,绝不打断正常轮次。
  3. 不限次数(0.4.0 起的行为):每一次「没有有效输出」的轮次都注入补救, 不设任何次数上限,一直持续到模型真正产出(发出 tool-call 或写出正文汇报)为止。 停止条件不是某个阈值,而是「模型恢复干活/汇报」这一事实本身。
  4. 用户中断优先:signal.aborted 为真时绝不复活轮次(每轮都检查)。
  5. 唯一保险丝 absoluteLimit:默认 10000,仅防判定逻辑自身出错导致的程序性死循环。 正常场景永远碰不到(每次注入都对应一次上游 API 调用,1 万次时花费早已失控)。 设为 0 表示连这道闸也不要。
  6. 零副作用:纯内存状态(WeakMap),不写文件、不发网络请求、不申请权限。

配置

- insert:
    - id: dsh-empty-turn-guard
      name: dsh-empty-turn-guard
      config:
        enabled: true
        maxConsecutiveEmptySteps: 0
        maxRemediesPerTurn: 0
        maxRemediesPerAgent: 0
        absoluteLimit: 10000
        log: true
        steerText: '[empty-turn-guard] 上一步没有任何有效输出……请立刻继续执行。'
字段 默认值 说明
enabled false 总开关,必须显式 true
maxConsecutiveEmptySteps 0 连续空轮次上限;0 = 不限。设 N 表示允许连续 N 次,第 N+1 次放行
maxRemediesPerTurn 0 同一 turn 内补救次数上限;0 = 不限
maxRemediesPerAgent 0 单个 agent 生命周期补救总数上限;0 = 不限
absoluteLimit 10000 程序性死循环保险丝;0 = 关闭
log true 是否通过 ctx.logger.warn 记录介入与放行
steerText 内置中文提示 注入给模型的内容(0.4.0 起要求「立刻继续执行,不要回复本条消息」)

安装

A. 分发包(推荐给别的机器 / 别的 DSH 客户端)

# 打包(在开发机上执行一次)
powershell -NoProfile -ExecutionPolicy Bypass -File .\build-package.ps1
# -> .\dist\dsh-empty-turn-guard-v0.4.0.zip

把 zip 拷到目标机器,解压后有两条安装路径:

# Windows:双击 install.cmd,或
powershell -NoProfile -ExecutionPolicy Bypass -File .\install.ps1 -ProfileDir "<profile>"

# 任意平台(零依赖,Node 22+):Windows / Linux / macOS 都能用
node install.mjs
node install.mjs --profile ~/.dsh/profiles/desktop

然后重启 DSH。安装器会自动探测 profile(%DSH_HOME%\profiles\* 优先,其次 %USERPROFILE%\.dsh\profiles\*);有多个 profile 时会拒绝猜测并要求显式指定。 详见包内 INSTALL.md。

分发包做成离线 zip而不是 npm 包的三个原因:

  1. 目标机器不一定能从 npm 拉到本包(且本包不在任何 registry 上);
  2. DSH 的 profile 用 file: 依赖 + dsh.profile.bundles 装配,本来就不需要 registry;
  3. zip 里带 install.cmd,双击即可,无需用户敲命令。

B. 在本机源码目录直接装

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
.\install.ps1                      # 自动探测 profile($HOME\.dsh\profiles\desktop)
.\install.ps1 -ProfileDir "D:\x"   # 显式指定
.\install.ps1 -Uninstall           # 回滚(备份保留)

两个安装器(install.ps1 与 install.mjs)做的事完全一致,都是五步: 备份 package.json / cordis.patch.yml → 复制到

\vendor\dsh-empty-turn-guard → 建立 \node_modules\dsh-empty-turn-guard 解析入口 → 在 dependencies 与 dsh.profile.bundles 登记本包 → 在 cordis.patch.yml 写入/更新插件行。

为什么需要 node_modules 那一步(旧版 install.ps1 漏了,会导致装了但不生效): DSH 启动时用 createRequire(<profile>/package.json).resolve.paths(包名) 定位 bundle (dsh-app-boot/lib/index.js:1774 resolveBundleDir → :1755 packageDirFromAnchor), 只在 package.json 里写 dependencies 并不会让包变得可解析 —— 文件必须真的落在 <profile>/node_modules/ 下。安装器优先建 junction,失败自动降级为复制目录。

升级已有安装

推荐:双击 run-sync.cmd(等价于 powershell -NoProfile -ExecutionPolicy Bypass -File sync-latest.ps1)。

脚本只做四件事,且全程 BOM-free:

  1. 给 vendor 里的 index.js / package.json 与 profile 的 cordis.patch.yml 打时间戳备份;
  2. 把本目录的 index.js / package.json / README.md 复制进 %USERPROFILE%\.dsh\profiles\desktop\vendor\dsh-empty-turn-guard;
  3. 用正则定位 - id: dsh-empty-turn-guard 块并整块替换成新配置(不限模式);
  4. 跑 10 项自检(含 node --check、BOM 检测、三项上限是否为 0、安装版本是否与源一致)。

看到 VERDICT: OK -- empty-turn-guard 0.4.0 deployed (unlimited mode) 后重启 DeepSeek Harness。

手动等价做法:

Copy-Item .\index.js,.\package.json,.\README.md "$HOME\.dsh\profiles\desktop\vendor\dsh-empty-turn-guard\" -Force
node .\preflight.mjs --profile "$HOME\.dsh\profiles\desktop"

vendor 目录是 junction 的目标实体,复制即生效;改完仍需重启 DSH。 注意 patch 里的三个上限必须同时改成 0,否则仍是旧行为。

⚠️ 0.2.0 只救了一次(真实事故记录)

0.2.0 用 maxRemediesPerTurn=1 作为主闸门,实测在同一个 turn 里失效:

  • turn 39 step 10(14:06:41)只有 reasoning、finish=tool-calls → 注入补救,step 11 被救回;
  • turn 39 step 14(14:07:15)又是同样形态 → 计数已用完,直接放行 → turn/end completed,用户看到的是「停下且没汇报」。

关键:这两次空轮之间隔了三步正常执行(step 11/12/13 都带 tool-call),并不是上游持续异常, 单纯按「每 turn 一次」限流属于误伤。

  • 0.3.0:改成以 maxConsecutiveEmptySteps 为主闸门(默认 3 / 单轮 8 / 全局 30), 同一 turn 中间隔出现的空轮可以救第二次。回归用例 test.mjs 的 [3b]。
  • 0.4.0:按用户明确要求,三个上限默认全部改为 0 = 不限, 「每一次没有有效输出都注入,直到模型真正产出为止」。 只保留 absoluteLimit(默认 10000)作程序性死循环保险丝。 回归用例 test.mjs 的 [3d](同 turn 连 25 次空轮全救)、[3e](跨 15 个 turn 全救)、 [3f](absoluteLimit=5 时救 5 次停手)、[3g](signal.aborted 不救)、[3h](恢复产出即停)。

⚠️ PowerShell 5.1 的 BOM 陷阱(真实事故记录)

第一版 install.ps1 用了 Set-Content -Encoding UTF8 写 package.json。 Windows PowerShell 5.1 的 -Encoding UTF8 会写入 BOM,而 DSH 是这样读清单的:

raw = readFileSync(path, "utf8")   // dsh-app-boot/lib/index.js:1688
const parsed = JSON.parse(raw)     // :1692

BOM 让 JSON.parse 抛错,整个桌面宿主在启动阶段直接退出:

DesktopHostFatalError: Unexpected token '<BOM>', "<BOM>{" is not valid JSON
  at readProfileManifest (dsh-app-boot/lib/index.js:1692:22)
  at loadProfileDirectory (dsh-app-boot/lib/index.js:1796:42)

现在的写法:所有写入走 [System.Text.UTF8Encoding]::new($false) + File::WriteAllText / File::AppendAllText,永不产生 BOM。两个防复发工具:

.\repair-profile.ps1    # 逐字节检测并剥离 BOM;健康 profile 返回 "nothing to repair"
node preflight.mjs      # 用纯 Node 复刻 dsh 启动读取路径(manifest/bundles/patch/import)

重启前请务必先跑 node preflight.mjs,exit 0 才重启。

测试与验证

node test.mjs           # 单元测试:43 项断言
node preflight.mjs      # 启动前置检查:profile 能否被 dsh 正确加载(9 项)
.\verify.ps1            # 安装完整性:8 项文件/配置/语法/加载检查

test.mjs 覆盖:classifyAttempt 判定表(7 项)、零块注入、仅 reasoning + tool-calls 补救、 仅 usage/finish 仍判零产出、有 tool-call 放行、finish=stop 的 reasoning 放行、 逐轮上限、跨 turn 全局熔断、abandoned 放行、turn 不匹配放行、默认关闭完全惰性、 steer 抛错不崩、多 agent 状态隔离、异常 payload 不崩、补救消息形状(id/role/source/唯一性), 以及 0.4.0 不限模式的回归:[3d] 同 turn 连 25 次空轮全部补救、 [3e] 跨 15 个 turn 全部补救、[3f] absoluteLimit=5 时救满 5 次停手、 [3g] signal.aborted 放行、[3h] 模型恢复产出后停止补救。

真实帧回放(用线上会话里 turn 39 的真实 step 序列重放):

node replay.mjs      # 期望输出 REPLAY OK,补救 2 次
node replay-old.mjs  # 对照:已安装的旧版只救 1 次

怎么确认它在真实会话里生效

插件用 ctx.logger.warn 记录每次介入,日志行形如:

[empty-turn-guard] turn 12 step 3 命中(tool-calls-without-tool-call:finish=tool-calls,但没有出现任何 tool-call 块),已注入第 1 次补救(本 turn 第 1 次,连续第 1 次)

并且在命中时,会话里会立刻出现一条用户消息,内容就是 steerText (以 [empty-turn-guard] 开头)。看到这条消息 = 插件真的拦截并救回了一个残缺轮次。

已知边界

  • 依赖 agent/turn-stopping 在 loop break 之前触发;若上游改了触发时机需要重新核对。
  • 若上游返回 finish=stop 且确实什么都没做,本插件不介入(那是模型自己的收尾,不该硬拉回来)。
  • 注入的补救会消耗一次额外的模型调用。0.4.0 采用「不限次数」策略后, 上游若长时间持续吐空,会持续消耗调用与上下文;absoluteLimit 就是为这种情况留的保险丝。 注入文案刻意压到最短,以降低高频补救对上下文的压力。
  • 插件只「救回轮次」,不修正上游为什么丢 tool-call;那属于适配器层(dsh-llm-deepseek)的修复范围。

CLASSIFICATION EVIDENCE

分类依据

项目类型插件
功能分类Agent 与会话
规则置信度高

系统优先读取 GitHub Topics,再与站内分类词典和词根规则比对。当前命中: ai-agent。