dsh-verify-gate 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,23 +1,36 @@
1
- # Changelog
2
-
3
- ## 0.1.0 — 2026-11-21
4
-
5
- First release.
6
-
7
- - Observe tool calls through the `tools/execute` waterfall; track which code files a
8
- turn touched and whether anything verified them afterwards.
9
- - Nudge through the `agent/pre-step` waterfall: when a turn edited code and nothing was
10
- run, one system message is appended to the next step's context.
11
- - `verify_exemption(reason)` tool so an agent can declare why no check applies
12
- (reasons under 12 characters are rejected).
13
- - Single ledger by design: `agent/pre-step` carries no agent identity, so per-agent
14
- isolation would be fake. Concurrent agents share one conservative judgement.
15
- - 19 tests: 11 for the ledger, 8 for the wiring.
16
-
17
- Verified live against DSH 0.2.2-rc.5:
18
-
19
- | arm | behaviour | nudges |
20
- |---|---|---:|
21
- | A | `write` a .js, verify nothing | 1 |
22
- | B | `write` a .js, then `node --check` | 0 |
23
- | C | `write` a .mjs, then `verify_exemption` | 0 |
1
+ # Changelog
2
+
3
+ ## 0.1.1 — 2026-11-21
4
+
5
+ - **Fix: the nudge no longer corrupts session history.** The reminder was injected as a
6
+ `system`-role message; the harness records every non-`runtime-context` `agent/pre-step`
7
+ message as a `user/message` event, so that payload produced logs rejected on load
8
+ (`stored session ... failed validation: message must have role "user"`) and the session
9
+ could not be opened again. The reminder is now a `user`-role message wrapped in
10
+ `<system-reminder>` with `source.kind = "verify-gate"`.
11
+ - Added `assertInjectable()`: an injected message that is not user-role (or that carries a
12
+ retired `plugin`/`system-prompt` source) throws instead of reaching the log.
13
+ - Stop duplicating the call-to-action: the verdict text already carries it (the reminder
14
+ used to repeat the sentence twice).
15
+
16
+ ## 0.1.0 — 2026-11-21
17
+
18
+ First release. Published to npm as `dsh-verify-gate@0.1.0`.
19
+
20
+ - Observe tool calls through the `tools/execute` waterfall; track which code files a
21
+ turn touched and whether anything verified them afterwards.
22
+ - Nudge through the `agent/pre-step` waterfall: when a turn edited code and nothing was
23
+ run, one system message is appended to the next step's context.
24
+ - `verify_exemption(reason)` tool so an agent can declare why no check applies
25
+ (reasons under 12 characters are rejected).
26
+ - Single ledger by design: `agent/pre-step` carries no agent identity, so per-agent
27
+ isolation would be fake. Concurrent agents share one conservative judgement.
28
+ - 19 tests: 11 for the ledger, 8 for the wiring.
29
+
30
+ Verified live against DSH 0.2.2-rc.5:
31
+
32
+ | arm | behaviour | nudges |
33
+ |---|---|---:|
34
+ | A | `write` a .js, verify nothing | 1 |
35
+ | B | `write` a .js, then `node --check` | 0 |
36
+ | C | `write` a .mjs, then `verify_exemption` | 0 |
package/README.md CHANGED
@@ -1,105 +1,112 @@
1
- # dsh-verify-gate
2
-
3
- **A completion gate for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (DSH).**
4
- When a turn changed code files and nothing was run to check them, it says so — once —
5
- at the next step, and accepts an exemption when no check applies.
6
-
7
- English · [简体中文](README.zh.md)
8
-
9
- ---
10
-
11
- ## Why
12
-
13
- Measured on a real machine, 49 sessions / 937 turns:
14
-
15
- | metric | value |
16
- |---|---:|
17
- | turns that edited **code** files | 394 |
18
- | …that never ran a test or type check | **355 (90.1%)** |
19
-
20
- That is the failure mode Can Bölük catalogued in
21
- [_The Harness Problem_](https://blog.can.ac/2026/02/12/the-harness-problem/):
22
- the agent writes, re-reads, decides it looks fine, and stops — without ever asking reality.
23
-
24
- DSH ships a safety reviewer (`dsh-experimental-auto-review`) and advisory loop reminders,
25
- but nothing guards the *completion* boundary. This plugin is that guard.
26
-
27
- ## How it works
28
-
29
- Three harness seams, read out of `@deepseek-ai/dsh-agent-loop/lib/index.js` on DSH
30
- `0.2.2-rc.5` — not guessed:
31
-
32
- | seam | kind | role |
33
- |---|---|---|
34
- | `tools/execute` | waterfall `(exec, next)` | observe calls; `exec.name` + `exec.arguments` |
35
- | `agent/pre-step` | waterfall `(payload, next)` | **inject** the reminder into `decision.messages` |
36
- | `agent/turn-stopping` | serial | settle the turn, reset the ledger |
37
-
38
- > `turn/start` and `turn/end` are **session records** (`session.append(...)`), not context
39
- > events. Listening for them silently never fires.
40
-
41
- The injected message lands in the model's context for the very next step:
42
-
43
- ```json
44
- { "role": "system", "content": [{ "type": "text", "text": "This turn changed 1 code file(s) but nothing was run to check them. Run the project tests (or a type check) before calling the task done, or call verify_exemption with a concrete reason why no check applies." }], "source": { "kind": "system-prompt" } }
45
- ```
46
-
47
- ## Design choices
48
-
49
- - **Warn, never block.** Blocking turns makes every task slower; a reminder costs one line.
50
- - **Code files only.** `.md` / `.json` edits do not count, otherwise every doc commit screams.
51
- - **A failing check still counts as verified.** The plugin cares whether the agent looked at
52
- reality, not whether reality said yes.
53
- - **An exemption exists.** `verify_exemption(reason)`, reasons under 12 characters rejected.
54
- A gate that cries wolf gets uninstalled.
55
-
56
- ## Live verification (DSH 0.2.2-rc.5)
57
-
58
- | arm | behaviour | nudges |
59
- |---|---|---:|
60
- | A | `write` a `.js`, verify nothing | **1** |
61
- | B | `write` a `.js`, then `node --check` | 0 |
62
- | C | `write` a `.mjs`, then `verify_exemption` | 0 |
63
-
64
- The plugin loaded without restarting the host.
65
-
66
- ## Known limits
67
-
68
- - **Direct `fs` writes inside PTC bypass tool dispatch** and are not seen. Only
69
- `tools.write` / `tools.edit` inner calls are observed.
70
- - **Single ledger.** `agent/pre-step` carries no agent identity, so concurrent agents
71
- share one conservative judgement rather than a fake per-agent isolation.
72
- - **Verification is a command regex.** A project-specific check script may be missed —
73
- the failure direction is one extra reminder, never a false pass.
74
-
75
- ## Install
76
-
77
- ```sh
78
- dsh plugin --profile web add dsh-verify-gate
79
- ```
80
-
81
- Or from a source checkout:
82
-
83
- ```sh
84
- dsh plugin --profile web add /absolute/path/to/dsh-verify-gate
85
- ```
86
-
87
- ## Test
88
-
89
- ```sh
90
- node test/run.mjs # 11 ledger cases
91
- node test/wiring.test.mjs # 8 wiring cases (fake host context)
92
- ```
93
-
94
- `node --test` spawns a child per file; on a sandboxed host that fails with `EPERM`,
95
- so these are plain in-process runners.
96
-
97
- ## Reproduce the evidence
98
-
99
- ```sh
100
- node scripts/evidence.mjs # reads ~/.dsh/sessions/*.jsonl.zstd
101
- ```
102
-
103
- ## License
104
-
105
- MIT
1
+ # dsh-verify-gate
2
+
3
+ **A completion gate for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (DSH).**
4
+ When a turn changed code files and nothing was run to check them, it says so — once —
5
+ at the next step, and accepts an exemption when no check applies.
6
+
7
+ English · [简体中文](README.zh.md)
8
+
9
+ ---
10
+
11
+ ## Why
12
+
13
+ Measured on a real machine, 49 sessions / 937 turns:
14
+
15
+ | metric | value |
16
+ |---|---:|
17
+ | turns that edited **code** files | 394 |
18
+ | …that never ran a test or type check | **355 (90.1%)** |
19
+
20
+ That is the failure mode Can Bölük catalogued in
21
+ [_The Harness Problem_](https://blog.can.ac/2026/02/12/the-harness-problem/):
22
+ the agent writes, re-reads, decides it looks fine, and stops — without ever asking reality.
23
+
24
+ DSH ships a safety reviewer (`dsh-experimental-auto-review`) and advisory loop reminders,
25
+ but nothing guards the *completion* boundary. This plugin is that guard.
26
+
27
+ ## How it works
28
+
29
+ Three harness seams, read out of `@deepseek-ai/dsh-agent-loop/lib/index.js` on DSH
30
+ `0.2.2-rc.5` — not guessed:
31
+
32
+ | seam | kind | role |
33
+ |---|---|---|
34
+ | `tools/execute` | waterfall `(exec, next)` | observe calls; `exec.name` + `exec.arguments` |
35
+ | `agent/pre-step` | waterfall `(payload, next)` | **inject** the reminder into `decision.messages` |
36
+ | `agent/turn-stopping` | serial | settle the turn, reset the ledger |
37
+
38
+ > `turn/start` and `turn/end` are **session records** (`session.append(...)`), not context
39
+ > events. Listening for them silently never fires.
40
+
41
+ The injected message lands in the model's context for the very next step:
42
+
43
+ ```json
44
+ { "role": "user", "content": [{ "type": "text", "text": "<system-reminder>\nThis turn changed 1 code file(s) but nothing was run to check them. Run the project tests (or a type check) before calling the task done, or call verify_exemption with a concrete reason why no check applies.\n</system-reminder>" }], "source": { "kind": "verify-gate" } }
45
+ ```
46
+
47
+ > **It must be a `user` message.** `agent/pre-step` messages that are not `runtime-context`
48
+ > are recorded by the harness as `user/message` session events no matter their role, so a
49
+ > `system`-role payload there writes a log that fails validation on load
50
+ > (`... message must have role "user"`) and can no longer be opened. The
51
+ > `<system-reminder>` wrapper is the harness's own convention for mid-conversation
52
+ > injections, so the model still reads it as harness guidance, not user speech.
53
+
54
+ ## Design choices
55
+
56
+ - **Warn, never block.** Blocking turns makes every task slower; a reminder costs one line.
57
+ - **Code files only.** `.md` / `.json` edits do not count, otherwise every doc commit screams.
58
+ - **A failing check still counts as verified.** The plugin cares whether the agent looked at
59
+ reality, not whether reality said yes.
60
+ - **An exemption exists.** `verify_exemption(reason)`, reasons under 12 characters rejected.
61
+ A gate that cries wolf gets uninstalled.
62
+
63
+ ## Live verification (DSH 0.2.2-rc.5)
64
+
65
+ | arm | behaviour | nudges |
66
+ |---|---|---:|
67
+ | A | `write` a `.js`, verify nothing | **1** |
68
+ | B | `write` a `.js`, then `node --check` | 0 |
69
+ | C | `write` a `.mjs`, then `verify_exemption` | 0 |
70
+
71
+ The plugin loaded without restarting the host.
72
+
73
+ ## Known limits
74
+
75
+ - **Direct `fs` writes inside PTC bypass tool dispatch** and are not seen. Only
76
+ `tools.write` / `tools.edit` inner calls are observed.
77
+ - **Single ledger.** `agent/pre-step` carries no agent identity, so concurrent agents
78
+ share one conservative judgement rather than a fake per-agent isolation.
79
+ - **Verification is a command regex.** A project-specific check script may be missed —
80
+ the failure direction is one extra reminder, never a false pass.
81
+
82
+ ## Install
83
+
84
+ ```sh
85
+ dsh plugin --profile web add dsh-verify-gate # from npm
86
+ ```
87
+
88
+ Or from a source checkout:
89
+
90
+ ```sh
91
+ dsh plugin --profile web add /absolute/path/to/dsh-verify-gate
92
+ ```
93
+
94
+ ## Test
95
+
96
+ ```sh
97
+ node test/run.mjs # 11 ledger cases
98
+ node test/wiring.test.mjs # 8 wiring cases (fake host context)
99
+ ```
100
+
101
+ `node --test` spawns a child per file; on a sandboxed host that fails with `EPERM`,
102
+ so these are plain in-process runners.
103
+
104
+ ## Reproduce the evidence
105
+
106
+ ```sh
107
+ node scripts/evidence.mjs # reads ~/.dsh/sessions/*.jsonl.zstd
108
+ ```
109
+
110
+ ## License
111
+
112
+ MIT
package/README.zh.md CHANGED
@@ -1,80 +1,85 @@
1
- # dsh-verify-gate
2
-
3
- **DeepSeek Harness(DSH)的完成闸门。**
4
- 一个 turn 里改过代码文件、却从头到尾没跑过任何检查时,它会在下一步提醒一次;
5
- 确实不需要检查时,可以申报豁免。
6
-
7
- [English](README.md) · 简体中文
8
-
9
- ---
10
-
11
- ## 为什么有这个东西
12
-
13
- 在一台真实机器上实测,49 个会话 / 937 个 turn:
14
-
15
- | 指标 | 数值 |
16
- |---|---:|
17
- | 改过**代码文件**的 turn | 394 |
18
- | 其中一次测试/类型检查都没跑 | **355(90.1%)** |
19
-
20
- 这正是 Can Bölük《The Harness Problem》列的头号失败模式:
21
- 模型写完、读一遍、觉得没问题、收工。
22
-
23
- DSH 自带安全审阅(`dsh-experimental-auto-review`)和循环提示,
24
- 但**完成边界**上没有任何守卫。这个插件补的就是这一块。
25
-
26
- ## 工作方式
27
-
28
- 三个接缝,全部取自 DSH `0.2.2-rc.5` 的 `@deepseek-ai/dsh-agent-loop/lib/index.js` 源码:
29
-
30
- | 接缝 | 类型 | 作用 |
31
- |---|---|---|
32
- | `tools/execute` | waterfall `(exec, next)` | 观察调用;`exec.name` 与 `exec.arguments` |
33
- | `agent/pre-step` | waterfall `(payload, next)` | **注入**提醒到 `decision.messages` |
34
- | `agent/turn-stopping` | serial | 收尾结账、清空账本 |
35
-
36
- > `turn/start` 与 `turn/end` 是 **session 记录**(`session.append(...)`),不是上下文事件。
37
- > 监听它们永远不会触发——第一版探针就踩了这个坑。
38
-
39
- ## 设计取舍
40
-
41
- - **只警告,不阻断。** 阻断让每条任务都变慢;提醒只花一行。
42
- - **只认代码后缀。** 改 `.md` / `.json` 不算,否则文档提交天天刷警告。
43
- - **验证失败也算验证过。** 在乎的是模型抬头看了现实,不是现实一定说好。
44
- - **留了豁免出口。** `verify_exemption(reason)`,理由少于 12 字符直接拒。
45
- 会乱叫的闸门只有一个下场:被卸载。
46
-
47
- ## 实测(DSH 0.2.2-rc.5)
48
-
49
- | 组 | 行为 | 提醒次数 |
50
- |---|---|---:|
51
- | A | `write` 一个 `.js`,不做任何验证 | **1** |
52
- | B | `write` 一个 `.js`,然后 `node --check` | 0 |
53
- | C | `write` 一个 `.mjs`,然后调 `verify_exemption` | 0 |
54
-
55
- 插件**无需重启宿主**即被加载。
56
-
57
- ## 已知限制
58
-
59
- - **PTC 里直接 `fs` 写盘绕过工具派发**,看不到。只观察 `tools.write` / `tools.edit` 的内层调用。
60
- - **单账本。** `agent/pre-step` 的 payload 不含 agent 身份,并发代理共享一次保守判断,
61
- 而不是假装做了隔离。
62
- - **“验证”靠命令正则。** 项目自定义检查脚本可能漏判——方向是多提醒一次,绝不误放过。
63
-
64
- ## 安装
65
-
66
- ```sh
67
- dsh plugin --profile web add dsh-verify-gate
68
- ```
69
-
70
- ## 测试与复现
71
-
72
- ```sh
73
- node test/run.mjs # 11 项账本逻辑
74
- node test/wiring.test.mjs # 8 项接线(伪宿主上下文)
75
- node scripts/evidence.mjs # 复现 90.1% 那份统计
76
- ```
77
-
78
- ## 许可
79
-
80
- MIT
1
+ # dsh-verify-gate
2
+
3
+ **DeepSeek Harness(DSH)的完成闸门。**
4
+ 一个 turn 里改过代码文件、却从头到尾没跑过任何检查时,它会在下一步提醒一次;
5
+ 确实不需要检查时,可以申报豁免。
6
+
7
+ [English](README.md) · 简体中文
8
+
9
+ ---
10
+
11
+ ## 为什么有这个东西
12
+
13
+ 在一台真实机器上实测,49 个会话 / 937 个 turn:
14
+
15
+ | 指标 | 数值 |
16
+ |---|---:|
17
+ | 改过**代码文件**的 turn | 394 |
18
+ | 其中一次测试/类型检查都没跑 | **355(90.1%)** |
19
+
20
+ 这正是 Can Bölük《The Harness Problem》列的头号失败模式:
21
+ 模型写完、读一遍、觉得没问题、收工。
22
+
23
+ DSH 自带安全审阅(`dsh-experimental-auto-review`)和循环提示,
24
+ 但**完成边界**上没有任何守卫。这个插件补的就是这一块。
25
+
26
+ ## 工作方式
27
+
28
+ 三个接缝,全部取自 DSH `0.2.2-rc.5` 的 `@deepseek-ai/dsh-agent-loop/lib/index.js` 源码:
29
+
30
+ | 接缝 | 类型 | 作用 |
31
+ |---|---|---|
32
+ | `tools/execute` | waterfall `(exec, next)` | 观察调用;`exec.name` 与 `exec.arguments` |
33
+ | `agent/pre-step` | waterfall `(payload, next)` | **注入**提醒到 `decision.messages` |
34
+ | `agent/turn-stopping` | serial | 收尾结账、清空账本 |
35
+
36
+ > `turn/start` 与 `turn/end` 是 **session 记录**(`session.append(...)`),不是上下文事件。
37
+ > 监听它们永远不会触发——第一版探针就踩了这个坑。
38
+
39
+ > 注入的消息必须是 `user` 角色:`agent/pre-step` 里除 `runtime-context` 以外的消息,
40
+ > 宿主一律记成 `user/message` 会话事件,角色是 `system` 时写出来的日志在加载校验时会被拒
41
+ > (`... message must have role "user"`),整个会话从此打不开。提醒文本包在
42
+ > `<system-reminder>` 里,模型读到的仍是宿主提醒,不是用户发言。
43
+
44
+ ## 设计取舍
45
+
46
+ - **只警告,不阻断。** 阻断让每条任务都变慢;提醒只花一行。
47
+ - **只认代码后缀。** 改 `.md` / `.json` 不算,否则文档提交天天刷警告。
48
+ - **验证失败也算验证过。** 在乎的是模型抬头看了现实,不是现实一定说好。
49
+ - **留了豁免出口。** `verify_exemption(reason)`,理由少于 12 字符直接拒。
50
+ 会乱叫的闸门只有一个下场:被卸载。
51
+
52
+ ## 实测(DSH 0.2.2-rc.5)
53
+
54
+ | 组 | 行为 | 提醒次数 |
55
+ |---|---|---:|
56
+ | A | `write` 一个 `.js`,不做任何验证 | **1** |
57
+ | B | `write` 一个 `.js`,然后 `node --check` | 0 |
58
+ | C | `write` 一个 `.mjs`,然后调 `verify_exemption` | 0 |
59
+
60
+ 插件**无需重启宿主**即被加载。
61
+
62
+ ## 已知限制
63
+
64
+ - **PTC 里直接 `fs` 写盘绕过工具派发**,看不到。只观察 `tools.write` / `tools.edit` 的内层调用。
65
+ - **单账本。** `agent/pre-step` 的 payload 不含 agent 身份,并发代理共享一次保守判断,
66
+ 而不是假装做了隔离。
67
+ - **“验证”靠命令正则。** 项目自定义检查脚本可能漏判——方向是多提醒一次,绝不误放过。
68
+
69
+ ## 安装
70
+
71
+ ```sh
72
+ dsh plugin --profile web add dsh-verify-gate # 从 npm 安装
73
+ ```
74
+
75
+ ## 测试与复现
76
+
77
+ ```sh
78
+ node test/run.mjs # 11 项账本逻辑
79
+ node test/wiring.test.mjs # 8 项接线(伪宿主上下文)
80
+ node scripts/evidence.mjs # 复现 90.1% 那份统计
81
+ ```
82
+
83
+ ## 许可
84
+
85
+ MIT
package/index.js CHANGED
@@ -1,122 +1,164 @@
1
- /**
2
- * dsh-verify-gate — 完成闸门(Host 半边)
3
- *
4
- * 背景(本机实测 49 个会话 / 937 个 turn):改过代码文件的 394 个 turn 里,
5
- * 355 个(90.1%)从头到尾没跑过任何测试或类型检查。
6
- *
7
- * 接缝取自 dsh 0.2.2-rc.5 的真实源码(@deepseek-ai/dsh-agent-loop/lib/index.js):
8
- * - tools/execute waterfall (exec, next);exec.name / exec.arguments
9
- * - agent/pre-step waterfall (payload, next);可改写 decision.messages
10
- * - agent/turn-stopping serial (payload)
11
- *
12
- * 注意:turn/start、turn/end 是 session 记录(session.append),不是 ctx 事件。
13
- *
14
- * 单账本说明:agent/pre-step 的 payload 不含 agent 身份(源码里只有 turn / step /
15
- * signal / modelSelection),如果记账侧按 agent 分账、读取侧只能读到一份,
16
- * 那写出来的"隔离"是假的。这里如实使用单账本,并发多代理时会保守地合并判断。
17
- */
18
-
19
- import { randomUUID } from 'node:crypto';
20
- import {
21
- createLedger, recordWrite, recordVerification, recordDeclaration,
22
- verdict, resetLedger, DECLARE_TOOL,
23
- } from './lib/ledger.js';
24
-
25
- export const inject = ['tools'];
26
-
27
- const DECLARE_HINT =
28
- 'Run the project tests (or a type check) before calling the task done, ' +
29
- 'or call verify_exemption with a concrete reason why no check applies.';
30
-
31
- export function apply(ctx, config = {}) {
32
- const enabled = config.enabled !== false;
33
- const stats = { nudged: 0, clean: 0, exempted: 0 };
34
- const book = { ledger: createLedger(), nudgedTurn: -1 };
35
-
36
- // ---- 1. 观察:工具执行后记账 ----
37
- ctx.effect(() => {
38
- const dispose = ctx.on('tools/execute', async (exec, next) => {
39
- if (!enabled) return next();
40
- const name = String(exec?.name ?? exec?.tool?.name ?? '');
41
- const args = exec?.arguments ?? exec?.args ?? {};
42
- const result = await next();
43
- try {
44
- if (name === DECLARE_TOOL) recordDeclaration(book.ledger, args?.reason);
45
- else recordWrite(book.ledger, name, args);
46
- const text = typeof result === 'string' ? result : JSON.stringify(result ?? '');
47
- recordVerification(book.ledger, name, args, text);
48
- } catch {
49
- // 账本自身出错绝不影响工具结果
50
- }
51
- return result;
52
- });
53
- return () => dispose?.();
54
- }, 'verify-gate: tool observer');
55
-
56
- // ---- 2. 下一步之前:把提醒送进模型上下文 ----
57
- ctx.effect(() => {
58
- const dispose = ctx.on('agent/pre-step', async (payload, next) => {
59
- const decision = await next();
60
- if (!enabled || !decision || decision.kind !== 'enter') return decision;
61
- try {
62
- const v = verdict(book.ledger);
63
- if (v.action !== 'warn') return decision;
64
- const turn = Number(payload?.turn ?? -1);
65
- if (book.nudgedTurn === turn) return decision; // 一个 turn 最多提醒一次
66
- book.nudgedTurn = turn;
67
- stats.nudged += 1;
68
- return {
69
- ...decision,
70
- messages: [...decision.messages, {
71
- id: randomUUID(),
72
- role: 'system',
73
- content: [{ type: 'text', text: (v.reason ?? '') + ' ' + DECLARE_HINT }],
74
- source: { kind: 'system-prompt' },
75
- }],
76
- };
77
- } catch {
78
- return decision; // 注入失败一律放行,绝不打断正常流程
79
- }
80
- });
81
- return () => dispose?.();
82
- }, 'verify-gate: pre-step nudge');
83
-
84
- // ---- 3. 收工结账 ----
85
- ctx.effect(() => {
86
- const dispose = ctx.on('agent/turn-stopping', () => {
87
- try {
88
- const v = verdict(book.ledger);
89
- if (v.code === 'DECLARED') stats.exempted += 1;
90
- else if (v.code === 'VERIFIED') stats.clean += 1;
91
- resetLedger(book.ledger);
92
- book.nudgedTurn = -1;
93
- } catch { /* 结账出错不影响收工 */ }
94
- });
95
- return () => dispose?.();
96
- }, 'verify-gate: turn settlement');
97
-
98
- // ---- 4. 豁免工具 ----
99
- if (config.exemptionTool !== false) {
100
- ctx.effect(() => ctx.tools.register({
101
- name: DECLARE_TOOL,
102
- description:
103
- "Declare that this turn's code changes need no automated check, with a concrete reason. " +
104
- 'Use only when no test, type check or runnable path applies (generated files, docs-only code, ' +
105
- 'or the check lives in another repository). A vague reason is rejected.',
106
- parameters: {
107
- type: 'object',
108
- properties: { reason: { type: 'string', description: 'Why no verification applies, concretely.' } },
109
- required: ['reason'],
110
- additionalProperties: false,
111
- },
112
- output: {
113
- schema: { type: 'object', properties: { accepted: { type: 'boolean' }, code: { type: 'string' } } },
114
- render: (_a, v) => [{ type: 'text', text: v.accepted ? 'Exemption recorded.' : 'Exemption rejected: reason too vague (need >= 12 chars).' }],
115
- },
116
- async execute(args) {
117
- const accepted = recordDeclaration(book.ledger, args?.reason);
118
- return { accepted, code: accepted ? 'DECLARED' : 'REASON_TOO_VAGUE' };
119
- },
120
- }), 'verify-gate: exemption tool');
121
- }
122
- }
1
+ /**
2
+ * dsh-verify-gate — 完成闸门(Host 半边)
3
+ *
4
+ * 背景(本机实测 49 个会话 / 937 个 turn):改过代码文件的 394 个 turn 里,
5
+ * 355 个(90.1%)从头到尾没跑过任何测试或类型检查。
6
+ *
7
+ * 接缝取自 dsh 0.2.2-rc.5 的真实源码(@deepseek-ai/dsh-agent-loop/lib/index.js):
8
+ * - tools/execute waterfall (exec, next);exec.name / exec.arguments
9
+ * - agent/pre-step waterfall (payload, next);可改写 decision.messages
10
+ * - agent/turn-stopping serial (payload)
11
+ *
12
+ * 注意:turn/start、turn/end 是 session 记录(session.append),不是 ctx 事件。
13
+ *
14
+ * 单账本说明:agent/pre-step 的 payload 不含 agent 身份(源码里只有 turn / step /
15
+ * signal / modelSelection),如果记账侧按 agent 分账、读取侧只能读到一份,
16
+ * 那写出来的"隔离"是假的。这里如实使用单账本,并发多代理时会保守地合并判断。
17
+ */
18
+
19
+ import { randomUUID } from 'node:crypto';
20
+ import {
21
+ createLedger, recordWrite, recordVerification, recordDeclaration,
22
+ verdict, resetLedger, DECLARE_TOOL,
23
+ } from './lib/ledger.js';
24
+
25
+ export const inject = ['tools'];
26
+
27
+ const DECLARE_HINT =
28
+ 'Run the project tests (or a type check) before calling the task done, ' +
29
+ 'or call verify_exemption with a concrete reason why no check applies.';
30
+
31
+ /**
32
+ * Refuse to inject anything the session log cannot hold.
33
+ *
34
+ * DSH persists every `agent/pre-step` message whose source is not
35
+ * `runtime-context` as a `user/message` event, *whatever the message role is*
36
+ * (agent-loop: `this.session.append("user/message", message, ...)`). A
37
+ * `system`-role payload therefore writes a row that the loader rejects —
38
+ * `session event at seq N message must have role "user"` — and the session can
39
+ * no longer be opened at all. This has already happened once (0.1.0); the check
40
+ * lives next to the message shape so a future edit cannot quietly reintroduce it.
41
+ *
42
+ * @param message - candidate message for the next step's context.
43
+ * @returns the same message, guaranteed to be loggable.
44
+ */
45
+ export function assertInjectable(message) {
46
+ if (message?.role !== 'user') {
47
+ throw new Error('verify-gate: injected messages must use role "user", got ' + JSON.stringify(message?.role));
48
+ }
49
+ const kind = message?.source?.kind;
50
+ if (typeof kind !== 'string' || kind === '' || kind === 'plugin') {
51
+ throw new Error('verify-gate: injected messages need a producer-owned source kind');
52
+ }
53
+ return message;
54
+ }
55
+
56
+ /**
57
+ * Build the single reminder message this plugin injects.
58
+ * @param text - already-composed reminder body.
59
+ * @returns a frozen-free user-role message wrapped in <system-reminder>.
60
+ */
61
+ function nudgeMessage(text) {
62
+ return assertInjectable({
63
+ id: randomUUID(),
64
+ role: 'user',
65
+ content: [{ type: 'text', text: '<system-reminder>\n' + text + '\n</system-reminder>' }],
66
+ source: { kind: 'verify-gate' },
67
+ });
68
+ }
69
+
70
+ export function apply(ctx, config = {}) {
71
+ const enabled = config.enabled !== false;
72
+ const stats = { nudged: 0, clean: 0, exempted: 0 };
73
+ const book = { ledger: createLedger(), nudgedTurn: -1 };
74
+
75
+ // ---- 1. 观察:工具执行后记账 ----
76
+ ctx.effect(() => {
77
+ const dispose = ctx.on('tools/execute', async (exec, next) => {
78
+ if (!enabled) return next();
79
+ const name = String(exec?.name ?? exec?.tool?.name ?? '');
80
+ const args = exec?.arguments ?? exec?.args ?? {};
81
+ const result = await next();
82
+ try {
83
+ if (name === DECLARE_TOOL) recordDeclaration(book.ledger, args?.reason);
84
+ else recordWrite(book.ledger, name, args);
85
+ const text = typeof result === 'string' ? result : JSON.stringify(result ?? '');
86
+ recordVerification(book.ledger, name, args, text);
87
+ } catch {
88
+ // 账本自身出错绝不影响工具结果
89
+ }
90
+ return result;
91
+ });
92
+ return () => dispose?.();
93
+ }, 'verify-gate: tool observer');
94
+
95
+ // ---- 2. 下一步之前:把提醒送进模型上下文 ----
96
+ ctx.effect(() => {
97
+ const dispose = ctx.on('agent/pre-step', async (payload, next) => {
98
+ const decision = await next();
99
+ if (!enabled || !decision || decision.kind !== 'enter') return decision;
100
+ try {
101
+ const v = verdict(book.ledger);
102
+ if (v.action !== 'warn') return decision;
103
+ const turn = Number(payload?.turn ?? -1);
104
+ if (book.nudgedTurn === turn) return decision; // 一个 turn 最多提醒一次
105
+ book.nudgedTurn = turn;
106
+ stats.nudged += 1;
107
+ // A user-role reminder, never a system-role one: the harness records every
108
+ // agent/pre-step message that is not runtime-context as a `user/message`
109
+ // session event, so a system-role payload there writes a log that can never
110
+ // be loaded again ("message must have role \"user\""). Wrapping the text in
111
+ // <system-reminder> is the harness's own convention for mid-conversation
112
+ // injections (skill catalog, runtime context), so the model still reads it
113
+ // as harness guidance rather than user speech.
114
+ const text = (v.reason ?? '').trim() || DECLARE_HINT;
115
+ return {
116
+ ...decision,
117
+ messages: [...decision.messages, nudgeMessage(text)],
118
+ };
119
+ } catch {
120
+ return decision; // 注入失败一律放行,绝不打断正常流程
121
+ }
122
+ });
123
+ return () => dispose?.();
124
+ }, 'verify-gate: pre-step nudge');
125
+
126
+ // ---- 3. 收工结账 ----
127
+ ctx.effect(() => {
128
+ const dispose = ctx.on('agent/turn-stopping', () => {
129
+ try {
130
+ const v = verdict(book.ledger);
131
+ if (v.code === 'DECLARED') stats.exempted += 1;
132
+ else if (v.code === 'VERIFIED') stats.clean += 1;
133
+ resetLedger(book.ledger);
134
+ book.nudgedTurn = -1;
135
+ } catch { /* 结账出错不影响收工 */ }
136
+ });
137
+ return () => dispose?.();
138
+ }, 'verify-gate: turn settlement');
139
+
140
+ // ---- 4. 豁免工具 ----
141
+ if (config.exemptionTool !== false) {
142
+ ctx.effect(() => ctx.tools.register({
143
+ name: DECLARE_TOOL,
144
+ description:
145
+ "Declare that this turn's code changes need no automated check, with a concrete reason. " +
146
+ 'Use only when no test, type check or runnable path applies (generated files, docs-only code, ' +
147
+ 'or the check lives in another repository). A vague reason is rejected.',
148
+ parameters: {
149
+ type: 'object',
150
+ properties: { reason: { type: 'string', description: 'Why no verification applies, concretely.' } },
151
+ required: ['reason'],
152
+ additionalProperties: false,
153
+ },
154
+ output: {
155
+ schema: { type: 'object', properties: { accepted: { type: 'boolean' }, code: { type: 'string' } } },
156
+ render: (_a, v) => [{ type: 'text', text: v.accepted ? 'Exemption recorded.' : 'Exemption rejected: reason too vague (need >= 12 chars).' }],
157
+ },
158
+ async execute(args) {
159
+ const accepted = recordDeclaration(book.ledger, args?.reason);
160
+ return { accepted, code: accepted ? 'DECLARED' : 'REASON_TOO_VAGUE' };
161
+ },
162
+ }), 'verify-gate: exemption tool');
163
+ }
164
+ }
package/package.json CHANGED
@@ -1,57 +1,57 @@
1
- {
2
- "name": "dsh-verify-gate",
3
- "version": "0.1.0",
4
- "description": "Verification gate for DeepSeek Harness — reminds once when a turn changed code but nothing checked it. 改完代码没验证就在收工处叫住你,可申报豁免。",
5
- "type": "module",
6
- "main": "index.js",
7
- "exports": {
8
- ".": "./index.js",
9
- "./lib/ledger.js": "./lib/ledger.js",
10
- "./package.json": "./package.json"
11
- },
12
- "files": [
13
- "index.js",
14
- "lib/",
15
- "locale/",
16
- "test/",
17
- "scripts/",
18
- "cordis.patch.yml",
19
- "README.md",
20
- "README.zh.md",
21
- "LICENSE",
22
- "CHANGELOG.md"
23
- ],
24
- "keywords": [
25
- "dsh",
26
- "dsh-plugin",
27
- "deepseek-harness",
28
- "deepseek",
29
- "plugin",
30
- "cordis",
31
- "harness",
32
- "verification",
33
- "gate",
34
- "testing",
35
- "test-runner",
36
- "devtools",
37
- "developer-tools",
38
- "agent"
39
- ],
40
- "license": "MIT",
41
- "repository": {
42
- "type": "git",
43
- "url": "git+https://github.com/1250589385/dsh-verify-gate.git"
44
- },
45
- "bugs": {
46
- "url": "https://github.com/1250589385/dsh-verify-gate/issues"
47
- },
48
- "homepage": "https://github.com/1250589385/dsh-verify-gate#readme",
49
- "engines": {
50
- "node": ">=22"
51
- },
52
- "dsh": {
53
- "bundle": {
54
- "patch": "cordis.patch.yml"
55
- }
56
- }
57
- }
1
+ {
2
+ "name": "dsh-verify-gate",
3
+ "version": "0.1.1",
4
+ "description": "Verification gate for DeepSeek Harness — reminds once when a turn changed code but nothing checked it. 改完代码没验证就在收工处叫住你,可申报豁免。",
5
+ "type": "module",
6
+ "main": "index.js",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./lib/ledger.js": "./lib/ledger.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "index.js",
14
+ "lib/",
15
+ "locale/",
16
+ "test/",
17
+ "scripts/",
18
+ "cordis.patch.yml",
19
+ "README.md",
20
+ "README.zh.md",
21
+ "LICENSE",
22
+ "CHANGELOG.md"
23
+ ],
24
+ "keywords": [
25
+ "dsh",
26
+ "dsh-plugin",
27
+ "deepseek-harness",
28
+ "deepseek",
29
+ "plugin",
30
+ "cordis",
31
+ "harness",
32
+ "verification",
33
+ "gate",
34
+ "testing",
35
+ "test-runner",
36
+ "devtools",
37
+ "developer-tools",
38
+ "agent"
39
+ ],
40
+ "license": "MIT",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/Raysnows/dsh-verify-gate.git"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/Raysnows/dsh-verify-gate/issues"
47
+ },
48
+ "homepage": "https://github.com/Raysnows/dsh-verify-gate#readme",
49
+ "engines": {
50
+ "node": ">=22"
51
+ },
52
+ "dsh": {
53
+ "bundle": {
54
+ "patch": "cordis.patch.yml"
55
+ }
56
+ }
57
+ }
@@ -1,105 +1,139 @@
1
- import assert from 'node:assert/strict';
2
- import { apply } from '../index.js';
3
- import { createFakeCtx } from './fake-ctx.mjs';
4
-
5
- const cases = [];
6
- const test = (name, fn) => cases.push([name, fn]);
7
-
8
- const exec = (name, args) => ({ name, arguments: args, agent: { id: 'a1' } });
9
-
10
- test('插件加载后注册了豁免工具,并挂上三个接缝', () => {
11
- const ctx = createFakeCtx();
12
- apply(ctx, {});
13
- assert.equal(ctx.__registered.length, 1);
14
- assert.equal(ctx.__registered[0].name, 'verify_exemption');
15
- assert.equal(ctx.__listeners('tools/execute'), 1);
16
- assert.equal(ctx.__listeners('agent/pre-step'), 1);
17
- assert.equal(ctx.__listeners('agent/turn-stopping'), 1);
18
- });
19
-
20
- test('改了代码不验证 -> 下一个 pre-step 注入一条 system 消息', async () => {
21
- const ctx = createFakeCtx();
22
- apply(ctx, {});
23
- await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
24
- const decision = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
25
- assert.equal(decision.kind, 'enter');
26
- const injected = decision.messages.at(-1);
27
- assert.equal(injected.role, 'system');
28
- assert.match(injected.content[0].text, /changed 1 code file\(s\)/);
29
- assert.ok(injected.id);
30
- });
31
-
32
- test('同一 turn 只提醒一次', async () => {
33
- const ctx = createFakeCtx();
34
- apply(ctx, {});
35
- await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
36
- const d1 = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
37
- const d2 = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 3 });
38
- assert.equal(d1.messages.length, 2);
39
- assert.equal(d2.messages.length, 1);
40
- });
41
-
42
- test('跑了测试 -> 不注入', async () => {
43
- const ctx = createFakeCtx();
44
- apply(ctx, {});
45
- await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
46
- await ctx.__emitWaterfall('tools/execute', exec('pwsh', { command: 'node --test' }));
47
- const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 3 });
48
- assert.equal(d.messages.length, 1);
49
- });
50
-
51
- test('申报豁免 -> 不注入,且工具返回 accepted', async () => {
52
- const ctx = createFakeCtx();
53
- apply(ctx, {});
54
- await ctx.__emitWaterfall('tools/execute', exec('write', { file_path: 'src/a.ts' }));
55
- const tool = ctx.__registered[0];
56
- const res = await tool.execute({ reason: '这是生成产物,测试在上游仓库' }, { agent: { id: 'a1' } });
57
- assert.equal(res.accepted, true);
58
- const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 3 });
59
- assert.equal(d.messages.length, 1);
60
- });
61
-
62
- test('理由太短 -> 拒绝,仍然注入', async () => {
63
- const ctx = createFakeCtx();
64
- apply(ctx, {});
65
- await ctx.__emitWaterfall('tools/execute', exec('write', { file_path: 'src/a.ts' }));
66
- const tool = ctx.__registered[0];
67
- const res = await tool.execute({ reason: '短' }, { agent: { id: 'a1' } });
68
- assert.equal(res.accepted, false);
69
- const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
70
- assert.equal(d.messages.length, 2);
71
- });
72
-
73
- test('turn-stopping 结账后账本清空,下一轮不再提醒', async () => {
74
- const ctx = createFakeCtx();
75
- apply(ctx, {});
76
- await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
77
- await ctx.__emit('agent/turn-stopping', { turn: 1 });
78
- const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 2, step: 1 });
79
- assert.equal(d.messages.length, 1);
80
- });
81
-
82
- test('工具抛错时账本不影响异常传递', async () => {
83
- const ctx = createFakeCtx();
84
- apply(ctx, {});
85
- const failing = { name: 'edit', arguments: { file_path: 'src/a.ts' }, agent: { id: 'a1' } };
86
- // 直接给一个会抛错的 next:用一个会 throw 的监听器模拟工具失败
87
- const boom = ctx.on;
88
- let threw = false;
89
- try {
90
- const listeners = [];
91
- const fake = { ...ctx, on: (e, f) => { if (e === 'tools/execute') listeners.push(f); return () => {}; } };
92
- apply(fake, {});
93
- const fn = listeners[0];
94
- await fn(failing, async () => { throw new Error('tool failed'); }).catch(() => { threw = true; });
95
- } catch { threw = true; }
96
- assert.equal(threw, true);
97
- });
98
-
99
- let pass = 0, fail = 0;
100
- for (const [name, fn] of cases) {
101
- try { await fn(); pass++; console.log(' ok ' + name); }
102
- catch (e) { fail++; console.log(' FAIL ' + name + '\n ' + e.message); }
103
- }
104
- console.log('\n' + pass + ' passed, ' + fail + ' failed, ' + cases.length + ' total');
105
- process.exit(fail ? 1 : 0);
1
+ import assert from 'node:assert/strict';
2
+ import { apply, assertInjectable } from '../index.js';
3
+ import { createFakeCtx } from './fake-ctx.mjs';
4
+
5
+ const cases = [];
6
+ const test = (name, fn) => cases.push([name, fn]);
7
+
8
+ const exec = (name, args) => ({ name, arguments: args, agent: { id: 'a1' } });
9
+
10
+ test('插件加载后注册了豁免工具,并挂上三个接缝', () => {
11
+ const ctx = createFakeCtx();
12
+ apply(ctx, {});
13
+ assert.equal(ctx.__registered.length, 1);
14
+ assert.equal(ctx.__registered[0].name, 'verify_exemption');
15
+ assert.equal(ctx.__listeners('tools/execute'), 1);
16
+ assert.equal(ctx.__listeners('agent/pre-step'), 1);
17
+ assert.equal(ctx.__listeners('agent/turn-stopping'), 1);
18
+ });
19
+
20
+ test('改了代码不验证 -> 下一个 pre-step 注入一条 user 消息', async () => {
21
+ const ctx = createFakeCtx();
22
+ apply(ctx, {});
23
+ await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
24
+ const decision = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
25
+ assert.equal(decision.kind, 'enter');
26
+ const injected = decision.messages.at(-1);
27
+ assert.equal(injected.role, 'user');
28
+ assert.match(injected.content[0].text, /changed 1 code file\(s\)/);
29
+ assert.ok(injected.id);
30
+ });
31
+
32
+ test('注入的消息永远是 user 角色(system 角色会写坏会话日志)', async () => {
33
+ // 回归:0.1.0 注入 role:'system',而宿主把每条非 runtime-context 的
34
+ // pre-step 消息一律记成 user/message,导致日志校验失败、整个会话打不开。
35
+ const ctx = createFakeCtx();
36
+ apply(ctx, {});
37
+ await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
38
+ const decision = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
39
+ for (const message of decision.messages) assert.equal(message.role, 'user');
40
+ const injected = decision.messages.at(-1);
41
+ assert.ok(
42
+ injected.source.kind !== 'system-prompt' && injected.source.kind !== 'plugin',
43
+ 'source.kind 必须是 producer 自有值,不能是 system-prompt / plugin',
44
+ );
45
+ assert.match(injected.content[0].text, /^<system-reminder>[\s\S]*<\/system-reminder>$/);
46
+ });
47
+
48
+ test('assertInjectable 硬拒 system 角色与非法 source', () => {
49
+ const base = { id: 'x', content: [], source: { kind: 'verify-gate' } };
50
+ assert.equal(assertInjectable({ ...base, role: 'user' }).role, 'user');
51
+ assert.throws(() => assertInjectable({ ...base, role: 'system' }), /must use role "user"/);
52
+ assert.throws(() => assertInjectable({ ...base, role: 'assistant' }), /must use role "user"/);
53
+ assert.throws(() => assertInjectable({ id: 'x', role: 'user', content: [], source: { kind: 'plugin' } }), /producer-owned source/);
54
+ assert.throws(() => assertInjectable({ id: 'x', role: 'user', content: [] }), /producer-owned source/);
55
+ });
56
+
57
+ test('提醒语只出现一次,不重复拼接', async () => {
58
+ const ctx = createFakeCtx();
59
+ apply(ctx, {});
60
+ await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
61
+ const decision = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
62
+ const text = decision.messages.at(-1).content[0].text;
63
+ assert.equal((text.match(/verify_exemption with a concrete reason/g) ?? []).length, 1);
64
+ });
65
+
66
+ test('同一 turn 只提醒一次', async () => {
67
+ const ctx = createFakeCtx();
68
+ apply(ctx, {});
69
+ await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
70
+ const d1 = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
71
+ const d2 = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 3 });
72
+ assert.equal(d1.messages.length, 2);
73
+ assert.equal(d2.messages.length, 1);
74
+ });
75
+
76
+ test('跑了测试 -> 不注入', async () => {
77
+ const ctx = createFakeCtx();
78
+ apply(ctx, {});
79
+ await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
80
+ await ctx.__emitWaterfall('tools/execute', exec('pwsh', { command: 'node --test' }));
81
+ const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 3 });
82
+ assert.equal(d.messages.length, 1);
83
+ });
84
+
85
+ test('申报豁免 -> 不注入,且工具返回 accepted', async () => {
86
+ const ctx = createFakeCtx();
87
+ apply(ctx, {});
88
+ await ctx.__emitWaterfall('tools/execute', exec('write', { file_path: 'src/a.ts' }));
89
+ const tool = ctx.__registered[0];
90
+ const res = await tool.execute({ reason: '这是生成产物,测试在上游仓库' }, { agent: { id: 'a1' } });
91
+ assert.equal(res.accepted, true);
92
+ const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 3 });
93
+ assert.equal(d.messages.length, 1);
94
+ });
95
+
96
+ test('理由太短 -> 拒绝,仍然注入', async () => {
97
+ const ctx = createFakeCtx();
98
+ apply(ctx, {});
99
+ await ctx.__emitWaterfall('tools/execute', exec('write', { file_path: 'src/a.ts' }));
100
+ const tool = ctx.__registered[0];
101
+ const res = await tool.execute({ reason: '短' }, { agent: { id: 'a1' } });
102
+ assert.equal(res.accepted, false);
103
+ const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 1, step: 2 });
104
+ assert.equal(d.messages.length, 2);
105
+ });
106
+
107
+ test('turn-stopping 结账后账本清空,下一轮不再提醒', async () => {
108
+ const ctx = createFakeCtx();
109
+ apply(ctx, {});
110
+ await ctx.__emitWaterfall('tools/execute', exec('edit', { file_path: 'src/a.ts' }));
111
+ await ctx.__emit('agent/turn-stopping', { turn: 1 });
112
+ const d = await ctx.__emitWaterfall('agent/pre-step', { turn: 2, step: 1 });
113
+ assert.equal(d.messages.length, 1);
114
+ });
115
+
116
+ test('工具抛错时账本不影响异常传递', async () => {
117
+ const ctx = createFakeCtx();
118
+ apply(ctx, {});
119
+ const failing = { name: 'edit', arguments: { file_path: 'src/a.ts' }, agent: { id: 'a1' } };
120
+ // 直接给一个会抛错的 next:用一个会 throw 的监听器模拟工具失败
121
+ const boom = ctx.on;
122
+ let threw = false;
123
+ try {
124
+ const listeners = [];
125
+ const fake = { ...ctx, on: (e, f) => { if (e === 'tools/execute') listeners.push(f); return () => {}; } };
126
+ apply(fake, {});
127
+ const fn = listeners[0];
128
+ await fn(failing, async () => { throw new Error('tool failed'); }).catch(() => { threw = true; });
129
+ } catch { threw = true; }
130
+ assert.equal(threw, true);
131
+ });
132
+
133
+ let pass = 0, fail = 0;
134
+ for (const [name, fn] of cases) {
135
+ try { await fn(); pass++; console.log(' ok ' + name); }
136
+ catch (e) { fail++; console.log(' FAIL ' + name + '\n ' + e.message); }
137
+ }
138
+ console.log('\n' + pass + ' passed, ' + fail + ' failed, ' + cases.length + ' total');
139
+ process.exit(fail ? 1 : 0);