dsh-verify-gate 0.1.0 → 0.1.2

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.
@@ -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);