dsh-swarm-console 0.1.4 → 0.2.0
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/README.md +10 -4
- package/docs/DETAIL.md +6 -4
- package/docs/V1-ACCEPTANCE.md +54 -54
- package/index.js +21 -6
- package/lib/adapter.js +4 -7
- package/lib/core/checks.js +64 -0
- package/lib/core/evidence.js +3 -1
- package/lib/core/gate.js +2 -2
- package/lib/core/policy.js +36 -0
- package/lib/core/verify.js +60 -18
- package/package.json +47 -24
- package/policies/code-change.json +25 -0
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ dsh-swarm-console
|
|
|
33
33
|
plugin_manager install_bundle dsh-swarm-console
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
> 已在 DSH 0.2.2-rc.5 上实测:profile 依赖解析为 `^0.1.
|
|
36
|
+
> 已在 DSH 0.2.2-rc.5 上实测:profile 依赖解析为 `^0.1.5`,安装后 `verify_*` 工具与三条斜杠命令均可用。
|
|
37
37
|
|
|
38
38
|
### 从本地目录安装(已验证)
|
|
39
39
|
|
|
@@ -72,11 +72,17 @@ contract 5bc483324d46 已冻结
|
|
|
72
72
|
已实现:
|
|
73
73
|
|
|
74
74
|
- 契约冻结与内容哈希(`contract.id`)
|
|
75
|
-
-
|
|
75
|
+
- 证据由插件执行:`verify_run` 的 `checks` 是**检查请求**而非结论——`file.exists` 由插件 `existsSync` 判定,`exec` 由插件实际执行;模型自报的 `ok` 一律忽略
|
|
76
|
+
- 证据分级与三态门禁(`GREEN` / `YELLOW` / `RED`);空契约直接 `RED`
|
|
76
77
|
- `RED` 必带可执行复现命令;缺证据不判通过
|
|
77
|
-
- 用户专属放行:`/gate-approve` `/gate-retry` `/gate-status`
|
|
78
|
+
- 用户专属放行:`/gate-approve` `/gate-retry` `/gate-status`;重跑后旧放行标记 `stale`
|
|
78
79
|
- 外部 agent 适配层(OpenAI 兼容端点,作为原生 subagent run 使用)
|
|
79
80
|
|
|
81
|
+
⚠️ 两条必须知道的行为:
|
|
82
|
+
|
|
83
|
+
- **`exec` 检查以当前用户权限执行命令**,等同于你自己在终端敲那条命令。请只把可信命令写进契约,不要接受来源不明的检查请求。
|
|
84
|
+
- **执行期间阻塞宿主**:v0.2 采用同步执行,单条检查最长 120 秒,期间宿主事件循环被占用,界面可能短暂无响应。异步执行排进 0.3。
|
|
85
|
+
|
|
80
86
|
尚未实现(不承诺):
|
|
81
87
|
|
|
82
88
|
- 可视化画布
|
|
@@ -87,7 +93,7 @@ contract 5bc483324d46 已冻结
|
|
|
87
93
|
## 开发与门禁
|
|
88
94
|
|
|
89
95
|
```bash
|
|
90
|
-
npm run gate # probe / selftest / plugin:test / smoke / pack / wiring / dryrun
|
|
96
|
+
npm run gate # probe / selftest / plugin:test / smoke / pack / wiring / dryrun / encoding 八段
|
|
91
97
|
```
|
|
92
98
|
|
|
93
99
|
验收记录见 [`docs/V1-ACCEPTANCE.md`](https://github.com/whyissodifficult/dsh-agent-swarm-console/blob/main/plugin/docs/V1-ACCEPTANCE.md):包含斜杠命令链路的日志证据(`command/run`、`command/done`)与三条硬规则的反证。
|
package/docs/DETAIL.md
CHANGED
|
@@ -52,9 +52,11 @@ capabilityRouting: true
|
|
|
52
52
|
|
|
53
53
|
## 已知限制
|
|
54
54
|
|
|
55
|
+
- **`exec` 检查以当前用户权限执行命令**:等同于你在终端敲那条命令。只把可信命令写进契约;不要接受来源不明的检查请求。
|
|
56
|
+
- **同步执行会阻塞宿主**:v0.2 的检查由 `spawnSync` 同步跑,单条最长 120 秒,期间宿主事件循环被占用,界面可能短暂无响应。异步执行排进 0.3。
|
|
57
|
+
- 证据存**进程内 Map**,DSH 重启即丢,不做持久化(重启后 `/gate-status` 返回「未知契约」)
|
|
55
58
|
- 还没有画布 —— 看不到 agent 节点、连线或过程可视化
|
|
56
|
-
-
|
|
57
|
-
-
|
|
58
|
-
- 门禁卡 GUI 截图与 approve/retry 事件流:当前为**未验证项**,待宿主重启后补
|
|
59
|
-
- 图标未提供 —— 当前回落 DSH 默认图标
|
|
59
|
+
- 多阶段评审状态机未实现
|
|
60
|
+
- 动作卡片:代码保留为可选增强,需目标 profile 挂载 `action-cards` 服务;v1 的放行入口是斜杠命令
|
|
60
61
|
- `swarm_run` 只支持一次调用,不支持多轮对话或子 agent 编排
|
|
62
|
+
|
package/docs/V1-ACCEPTANCE.md
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
# v1 验收记录
|
|
2
|
-
|
|
3
|
-
## 1. 验收范围
|
|
4
|
-
|
|
5
|
-
- 版本:0.1.0,基准提交 `58dc124`
|
|
6
|
-
- 覆盖:阶段 3A(可安装 Host 插件骨架)、阶段 4(verify v1 三态门禁)、阶段 5(发布配置)
|
|
7
|
-
- 本文事实以 `58dc124` 为准;放行入口为斜杠命令,门禁卡为可选增强(当前 profile 未挂载 action-cards)
|
|
8
|
-
|
|
9
|
-
## 2. 机器证据(会话日志 session.v4.jsonl.zstd,3186 事件)
|
|
10
|
-
|
|
11
|
-
评审侧逐帧解出的五条 command 事件:
|
|
12
|
-
|
|
13
|
-
| seq | 事件对 | 命令 | 结果 |
|
|
14
|
-
|---|---|---|---|
|
|
15
|
-
| 3150 / 3151 | command/run → command/done | `gate-status` | success:GREEN,尚未放行 |
|
|
16
|
-
| 3152 / 3153 | command/run → command/done | `gate-approve` | success:已由用户放行(approved) |
|
|
17
|
-
| 3154 / 3155 | command/run → command/done | `gate-status` | success:已由 user 放行 @ 时间戳 |
|
|
18
|
-
| 3156 / 3157 | command/run → command/done | `gate-approve` | error:非 GREEN 不可放行,请先 /gate-retry |
|
|
19
|
-
| 3158 / 3159 | command/run → command/done | `gate-retry` | success:重算 YELLOW,需补证据 |
|
|
20
|
-
|
|
21
|
-
**全日志模型侧 gate 类工具调用 0 次** —— H3(放行只能由用户触发)由结构保证:斜杠命令由用户在交互界面输入,模型中不存在对应工具。
|
|
22
|
-
|
|
23
|
-
## 3. 三条硬规则与反证
|
|
24
|
-
|
|
25
|
-
| 规则 | 内容 | 反证(已实测) |
|
|
26
|
-
|---|---|---|
|
|
27
|
-
| H1 | 证据先于验收:没有 evidence 的 criterion 不算通过 | 某 criterion 的 `checkIds` 为空 → 判定必为 YELLOW,不判 GREEN |
|
|
28
|
-
| H2 | RED 必须给可执行的最小复现命令 | 失败 check 未提供 `cmd` → `makeEvidence` 抛错;gate 兜底给 `echo "check X 未提供 cmd" && exit 1`,经 `sh -c` 执行退出非 0 |
|
|
29
|
-
| H3 | 放行只能由用户触发 | agent 身份调 `act(approve)` → refused 并留拒绝事件;实机全日志模型侧 gate 工具调用 0 次 |
|
|
30
|
-
|
|
31
|
-
## 4. 发布与安装验证(2026-11-22 补充)
|
|
32
|
-
|
|
33
|
-
- **npm**:`dsh-swarm-console@0.1.
|
|
34
|
-
- **npm 直装已验证**:在 DSH 0.2.2-rc.5 的「插件 → 添加插件」输入包名安装成功,profile 依赖解析为 `^0.1.
|
|
35
|
-
安装后 `verify_contract` / `verify_run` / `verify_status` 与三条斜杠命令均可用
|
|
36
|
-
- **Plugins 列表截图**:已由用户补(图标、名称、版本、启用开关齐全)
|
|
37
|
-
- **市场/意向表**:已提交(用户侧)
|
|
38
|
-
|
|
39
|
-
仍待补:
|
|
40
|
-
- 真实第三方用户的使用记录(加分项,非阻塞)
|
|
41
|
-
|
|
42
|
-
## 5. 复现命令
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
npm run gate #
|
|
46
|
-
node --test plugin/test/*.test.mjs # core / verify / gate-skill / commands 单测
|
|
47
|
-
|
|
48
|
-
# 手动在 DSH 会话里输入(把 <id> 换成 verify_contract 返回的 contractId)
|
|
49
|
-
/gate-status <id>
|
|
50
|
-
/gate-retry <id>
|
|
51
|
-
/gate-approve <id>
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
每次命令执行会在会话日志留下 `command/run` 与 `command/done` 事件,可作为放行/重试的机器证据。
|
|
1
|
+
# v1 验收记录
|
|
2
|
+
|
|
3
|
+
## 1. 验收范围
|
|
4
|
+
|
|
5
|
+
- 版本:0.1.0,基准提交 `58dc124`
|
|
6
|
+
- 覆盖:阶段 3A(可安装 Host 插件骨架)、阶段 4(verify v1 三态门禁)、阶段 5(发布配置)
|
|
7
|
+
- 本文事实以 `58dc124` 为准;放行入口为斜杠命令,门禁卡为可选增强(当前 profile 未挂载 action-cards)
|
|
8
|
+
|
|
9
|
+
## 2. 机器证据(会话日志 session.v4.jsonl.zstd,3186 事件)
|
|
10
|
+
|
|
11
|
+
评审侧逐帧解出的五条 command 事件:
|
|
12
|
+
|
|
13
|
+
| seq | 事件对 | 命令 | 结果 |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| 3150 / 3151 | command/run → command/done | `gate-status` | success:GREEN,尚未放行 |
|
|
16
|
+
| 3152 / 3153 | command/run → command/done | `gate-approve` | success:已由用户放行(approved) |
|
|
17
|
+
| 3154 / 3155 | command/run → command/done | `gate-status` | success:已由 user 放行 @ 时间戳 |
|
|
18
|
+
| 3156 / 3157 | command/run → command/done | `gate-approve` | error:非 GREEN 不可放行,请先 /gate-retry |
|
|
19
|
+
| 3158 / 3159 | command/run → command/done | `gate-retry` | success:重算 YELLOW,需补证据 |
|
|
20
|
+
|
|
21
|
+
**全日志模型侧 gate 类工具调用 0 次** —— H3(放行只能由用户触发)由结构保证:斜杠命令由用户在交互界面输入,模型中不存在对应工具。
|
|
22
|
+
|
|
23
|
+
## 3. 三条硬规则与反证
|
|
24
|
+
|
|
25
|
+
| 规则 | 内容 | 反证(已实测) |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| H1 | 证据先于验收:没有 evidence 的 criterion 不算通过 | 某 criterion 的 `checkIds` 为空 → 判定必为 YELLOW,不判 GREEN |
|
|
28
|
+
| H2 | RED 必须给可执行的最小复现命令 | 失败 check 未提供 `cmd` → `makeEvidence` 抛错;gate 兜底给 `echo "check X 未提供 cmd" && exit 1`,经 `sh -c` 执行退出非 0 |
|
|
29
|
+
| H3 | 放行只能由用户触发 | agent 身份调 `act(approve)` → refused 并留拒绝事件;实机全日志模型侧 gate 工具调用 0 次 |
|
|
30
|
+
|
|
31
|
+
## 4. 发布与安装验证(2026-11-22 补充)
|
|
32
|
+
|
|
33
|
+
- **npm**:`dsh-swarm-console@0.1.5` 已发布,`latest` 指向 0.1.5;tarball 逐文件核对通过(16 文件,含 README / LICENSE / icon.svg / docs)
|
|
34
|
+
- **npm 直装已验证**:在 DSH 0.2.2-rc.5 的「插件 → 添加插件」输入包名安装成功,profile 依赖解析为 `^0.1.5`,
|
|
35
|
+
安装后 `verify_contract` / `verify_run` / `verify_status` 与三条斜杠命令均可用
|
|
36
|
+
- **Plugins 列表截图**:已由用户补(图标、名称、版本、启用开关齐全)
|
|
37
|
+
- **市场/意向表**:已提交(用户侧)
|
|
38
|
+
|
|
39
|
+
仍待补:
|
|
40
|
+
- 真实第三方用户的使用记录(加分项,非阻塞)
|
|
41
|
+
|
|
42
|
+
## 5. 复现命令
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm run gate # 八段门禁一键验证(含 encoding)
|
|
46
|
+
node --test plugin/test/*.test.mjs # core / verify / gate-skill / commands 单测
|
|
47
|
+
|
|
48
|
+
# 手动在 DSH 会话里输入(把 <id> 换成 verify_contract 返回的 contractId)
|
|
49
|
+
/gate-status <id>
|
|
50
|
+
/gate-retry <id>
|
|
51
|
+
/gate-approve <id>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
每次命令执行会在会话日志留下 `command/run` 与 `command/done` 事件,可作为放行/重试的机器证据。
|
package/index.js
CHANGED
|
@@ -119,6 +119,12 @@ export function apply(ctx, config) {
|
|
|
119
119
|
* commands 由 dsh base 自带;仍走 inject 保持挂载时机安全。
|
|
120
120
|
* 命令名 kebab-case、不带斜杠;handler 返回 {kind:'success'|'error', text}。
|
|
121
121
|
*/
|
|
122
|
+
/** P0-4 · 从工具/命令调用上下文取 sessionId,拿不到用 default。 */
|
|
123
|
+
function sessionOf(invocation) {
|
|
124
|
+
const id = invocation?.agent?.session?.id
|
|
125
|
+
return typeof id === 'string' && id !== '' ? id : 'default'
|
|
126
|
+
}
|
|
127
|
+
|
|
122
128
|
function registerGateCommands(ctx) {
|
|
123
129
|
if (typeof ctx.inject !== 'function') return
|
|
124
130
|
ctx.inject(['commands'], (ctx) => {
|
|
@@ -128,20 +134,28 @@ function registerGateCommands(ctx) {
|
|
|
128
134
|
{
|
|
129
135
|
name: 'gate-approve',
|
|
130
136
|
description: '放行一个已 GREEN 的门禁契约(仅用户可触发)',
|
|
131
|
-
input: { hint: '<contractId>' },
|
|
132
|
-
|
|
137
|
+
input: { hint: '<contractId> [evidenceHash]' },
|
|
138
|
+
// 可传第二参数 evidenceHash 做过期校验;不传时以 runs 里的为最新
|
|
139
|
+
handler: ({ agent, rawInput }) => {
|
|
140
|
+
const parts = String(rawInput ?? '').trim().split(/\s+/)
|
|
141
|
+
return gateApprove({
|
|
142
|
+
contractId: parts[0] ?? '',
|
|
143
|
+
sessionId: sessionOf({ agent }),
|
|
144
|
+
...(parts[1] ? { expectedHash: parts[1] } : {}),
|
|
145
|
+
})
|
|
146
|
+
},
|
|
133
147
|
},
|
|
134
148
|
{
|
|
135
149
|
name: 'gate-retry',
|
|
136
150
|
description: '重跑某契约的门禁校验并刷新决定',
|
|
137
151
|
input: { hint: '<contractId>' },
|
|
138
|
-
handler: ({ rawInput }) => gateRetry({ contractId: String(rawInput ?? '').trim() }),
|
|
152
|
+
handler: ({ agent, rawInput }) => gateRetry({ contractId: String(rawInput ?? '').trim(), sessionId: sessionOf({ agent }) }),
|
|
139
153
|
},
|
|
140
154
|
{
|
|
141
155
|
name: 'gate-status',
|
|
142
156
|
description: '查看某契约的最近门禁决定与放行状态',
|
|
143
157
|
input: { hint: '<contractId>' },
|
|
144
|
-
handler: ({ rawInput }) => gateStatus({ contractId: String(rawInput ?? '').trim() }),
|
|
158
|
+
handler: ({ agent, rawInput }) => gateStatus({ contractId: String(rawInput ?? '').trim(), sessionId: sessionOf({ agent }) }),
|
|
145
159
|
},
|
|
146
160
|
]
|
|
147
161
|
for (const def of defs) ctx.effect(() => commands.register(def))
|
|
@@ -266,7 +280,8 @@ function registerVerifyTools(ctx) {
|
|
|
266
280
|
const wc = exec?.workspaceChanges ?? {}
|
|
267
281
|
const diffEvidence = typeof wc.summary === 'string' ? wc.summary : (typeof wc.diff === 'string' ? wc.diff : undefined)
|
|
268
282
|
const evidenceInput = args.checks ? { checks: args.checks, diff: diffEvidence } : undefined
|
|
269
|
-
|
|
283
|
+
// 0.2.0 P0-3:未显式给 checks 时按 code-change 策略生成检查请求
|
|
284
|
+
return verifyRun({ contractId: args.contractId, sessionId: sessionOf(exec), evidenceInput, usePolicy: !evidenceInput })
|
|
270
285
|
},
|
|
271
286
|
},
|
|
272
287
|
{
|
|
@@ -300,7 +315,7 @@ function registerVerifyTools(ctx) {
|
|
|
300
315
|
return [{ type: 'text', text: lines.join('\n') }]
|
|
301
316
|
},
|
|
302
317
|
},
|
|
303
|
-
execute: async (args) => verifyStatus({ contractId: args.contractId }),
|
|
318
|
+
execute: async (args, exec) => verifyStatus({ contractId: args.contractId, sessionId: sessionOf(exec) }),
|
|
304
319
|
},
|
|
305
320
|
]
|
|
306
321
|
for (const def of defs) ctx.effect(() => ctx.tools.register(def))
|
package/lib/adapter.js
CHANGED
|
@@ -191,11 +191,7 @@ export function buildPrompt(opts) {
|
|
|
191
191
|
if (opts.outlinePlan) {
|
|
192
192
|
parts.push(
|
|
193
193
|
'',
|
|
194
|
-
'
|
|
195
|
-
'```json',
|
|
196
|
-
'{"plan":[{"title":"第一步"},{"title":"第二步"}]}',
|
|
197
|
-
'```',
|
|
198
|
-
'',
|
|
194
|
+
'开始前先输出你的计划:写一个 json 代码块,内含 plan 数组,每个元素有 title 字段。',
|
|
199
195
|
'然后逐步执行。每完成一步,用一行 `TODO: [done] 描述` 或 `TODO: [todo] 描述` 汇报进度。',
|
|
200
196
|
'全部完成后,输出最终结果,并以一行 `RESULT:` 开头给出总结。'
|
|
201
197
|
)
|
|
@@ -211,8 +207,9 @@ export function buildPrompt(opts) {
|
|
|
211
207
|
* @returns {{title:string}[]|undefined}
|
|
212
208
|
*/
|
|
213
209
|
export function parsePlanFromOutput(output) {
|
|
214
|
-
|
|
215
|
-
const
|
|
210
|
+
// P1-4:取最后一个 ```json 块(agent 可能多次输出计划,最后一份才是定稿)
|
|
211
|
+
const blocks = [...output.matchAll(/```json\s*([\s\S]*?)```/gi)]
|
|
212
|
+
const target = blocks.length > 0 ? blocks[blocks.length - 1][1] : output
|
|
216
213
|
const start = target.indexOf('{"plan"')
|
|
217
214
|
if (start === -1) return undefined
|
|
218
215
|
let depth = 0
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P0-2 · 检查请求执行器(0.2.0:checks 语义从「自报结论」改为「检查请求」)。
|
|
3
|
+
*
|
|
4
|
+
* 模型传入的 ok 一律忽略 —— 由插件真实执行后判定,并记一条 warning。
|
|
5
|
+
* 纯函数 + 可注入 existsSync / exec,单测不必真的碰文件或跑命令。
|
|
6
|
+
*/
|
|
7
|
+
import { existsSync as fsExistsSync } from 'node:fs'
|
|
8
|
+
import { spawnSync as cpSpawnSync } from 'node:child_process'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {Array} requests 检查请求:[{id, type:'file.exists'|'exec', path|cmd, ok?}]
|
|
12
|
+
* @param {{existsSync?:Function, exec?:Function}} deps 可注入的执行实现
|
|
13
|
+
* @returns {{checks:Array, warnings:string[]}}
|
|
14
|
+
*/
|
|
15
|
+
export function runChecks(requests, deps = {}) {
|
|
16
|
+
if (!Array.isArray(requests)) throw new Error('checks 必须是数组')
|
|
17
|
+
const existsSync = deps.existsSync ?? fsExistsSync
|
|
18
|
+
const exec = deps.exec ?? ((cmd) => cpSpawnSync(cmd, {
|
|
19
|
+
shell: true,
|
|
20
|
+
timeout: 120000,
|
|
21
|
+
encoding: 'utf8',
|
|
22
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
23
|
+
}))
|
|
24
|
+
|
|
25
|
+
const warnings = []
|
|
26
|
+
const checks = requests.map((req, i) => {
|
|
27
|
+
if (!req || typeof req !== 'object') throw new Error(`checks[${i}] 必须是对象`)
|
|
28
|
+
const { id, type } = req
|
|
29
|
+
if (typeof id !== 'string' || id === '') throw new Error(`checks[${i}].id 必填`)
|
|
30
|
+
if (typeof type !== 'string' || type === '') throw new Error(`checks[${i}].type 必填`)
|
|
31
|
+
|
|
32
|
+
// 模型自报 ok 一律忽略,记 warning,不因此判 RED/GREEN
|
|
33
|
+
if (req.ok !== undefined) {
|
|
34
|
+
warnings.push(`check ${id} 自报 ok=${req.ok} 已忽略,改由插件实际执行判定`)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (type === 'file.exists') {
|
|
38
|
+
if (typeof req.path !== 'string' || req.path === '') {
|
|
39
|
+
throw new Error(`check ${id} file.exists 必带 path`)
|
|
40
|
+
}
|
|
41
|
+
const ok = existsSync(req.path) === true
|
|
42
|
+
return { id, type, ok, path: req.path, detail: ok ? `文件存在:${req.path}` : `文件不存在:${req.path}` }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (type === 'exec') {
|
|
46
|
+
if (typeof req.cmd !== 'string' || req.cmd === '') {
|
|
47
|
+
throw new Error(`check ${id} exec 必带 cmd`)
|
|
48
|
+
}
|
|
49
|
+
const r = exec(req.cmd)
|
|
50
|
+
// 关键:ok 只看真实退出码,不看模型自报,也不看 stdout 内容
|
|
51
|
+
const ok = r.error === undefined && r.status === 0
|
|
52
|
+
const out = { id, type, ok, cmd: req.cmd, exitCode: r.status }
|
|
53
|
+
if (!ok) {
|
|
54
|
+
const msg = r.error?.message ?? r.stderr ?? r.stdout ?? `exit ${r.status}`
|
|
55
|
+
out.detail = String(msg).slice(0, 300)
|
|
56
|
+
}
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
throw new Error(`check ${id} 未知检查类型 ${type}`)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
return { checks, warnings }
|
|
64
|
+
}
|
package/lib/core/evidence.js
CHANGED
|
@@ -20,7 +20,9 @@ export function makeEvidence(input) {
|
|
|
20
20
|
? input.status
|
|
21
21
|
: inferStatus(checks)
|
|
22
22
|
const at = typeof input.at === 'string' && input.at !== '' ? input.at : new Date().toISOString()
|
|
23
|
-
|
|
23
|
+
// 0.2.0:模型自报 ok 被忽略时记下的 warning(checks 是请求,不是结论)
|
|
24
|
+
const warnings = Array.isArray(input.warnings) ? input.warnings.filter((w) => typeof w === 'string') : []
|
|
25
|
+
return { contractId: input.contractId, artifacts, checks, level, status, at, warnings }
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
/** 证据内容哈希(用于 verify_run 返回的 evidenceHash)。 */
|
package/lib/core/gate.js
CHANGED
|
@@ -17,8 +17,8 @@ export function decide(contract, evidence) {
|
|
|
17
17
|
const reasons = []
|
|
18
18
|
const missing = []
|
|
19
19
|
|
|
20
|
-
if (!contract || !Array.isArray(contract.criteria)) {
|
|
21
|
-
return { decision: 'RED', reasons: ['
|
|
20
|
+
if (!contract || !Array.isArray(contract.criteria) || contract.criteria.length === 0) {
|
|
21
|
+
return { decision: 'RED', reasons: ['契约没有任何验收条件,不可判通过'], missing: [] }
|
|
22
22
|
}
|
|
23
23
|
const checks = Array.isArray(evidence?.checks) ? evidence.checks : []
|
|
24
24
|
const byId = new Map(checks.map((c) => [c.id, c]))
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P0-3 · 策略加载与转换(0.2.0)。
|
|
3
|
+
* loadPolicy(id) 读 plugin/policies/<id>.json;
|
|
4
|
+
* policyToChecks(policy, contract) 把描述性 checks 转成可执行的检查请求。
|
|
5
|
+
* 说明:策略里的 file.exists / workspace.diff 需要具体输入,不能由策略层伪造,
|
|
6
|
+
* 因此 policy runner 只生成能确定的 exec 检查(tests),其余留给调用方显式提供。
|
|
7
|
+
*/
|
|
8
|
+
import { readFileSync } from 'node:fs'
|
|
9
|
+
import { fileURLToPath } from 'node:url'
|
|
10
|
+
import { join, dirname } from 'node:path'
|
|
11
|
+
|
|
12
|
+
const POLICIES_DIR = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'policies')
|
|
13
|
+
|
|
14
|
+
/** 读取策略 JSON。id 限定小写/数字/连字符,防路径穿越。 */
|
|
15
|
+
export function loadPolicy(id = 'code-change', baseDir = POLICIES_DIR) {
|
|
16
|
+
if (typeof id !== 'string' || !/^[a-z0-9-]+$/.test(id)) throw new Error('policy id 非法')
|
|
17
|
+
const data = JSON.parse(readFileSync(join(baseDir, `${id}.json`), 'utf8'))
|
|
18
|
+
if (data.id !== id) throw new Error(`策略 id 不一致:${data.id} !== ${id}`)
|
|
19
|
+
return data
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 把策略的 checks 描述转成可执行的检查请求(只生成能确定的 exec 检查)。 */
|
|
23
|
+
export function policyToChecks(policy, contract = {}) {
|
|
24
|
+
const out = []
|
|
25
|
+
for (const c of policy.checks ?? []) {
|
|
26
|
+
if (c.type === 'exec') {
|
|
27
|
+
const testCmd = (Array.isArray(contract.constraints) ? contract.constraints : [])
|
|
28
|
+
.map((s) => String(s))
|
|
29
|
+
.find((s) => s.startsWith('testCmd:'))
|
|
30
|
+
const cmd = testCmd ? testCmd.slice('testCmd:'.length).trim() : 'npm test'
|
|
31
|
+
out.push({ id: c.id, type: 'exec', cmd })
|
|
32
|
+
}
|
|
33
|
+
// file.exists / workspace.diff 需要路径或宿主 workspaceChanges,不在此伪造
|
|
34
|
+
}
|
|
35
|
+
return out
|
|
36
|
+
}
|
package/lib/core/verify.js
CHANGED
|
@@ -6,10 +6,20 @@
|
|
|
6
6
|
import { freezeContract } from './contract.js'
|
|
7
7
|
import { makeEvidence, evidenceHash } from './evidence.js'
|
|
8
8
|
import { decide } from './gate.js'
|
|
9
|
+
import { runChecks } from './checks.js'
|
|
10
|
+
import { loadPolicy, policyToChecks } from './policy.js'
|
|
9
11
|
|
|
10
12
|
const contracts = new Map()
|
|
11
13
|
const runs = new Map()
|
|
12
14
|
|
|
15
|
+
/**
|
|
16
|
+
* P0-4 · 运行状态键 = `${sessionId}:${contractId}`,隔离不同会话的同号契约。
|
|
17
|
+
* sessionId 缺省 'default'(宿主拿不到时用)。
|
|
18
|
+
*/
|
|
19
|
+
export function runKey(contractId, sessionId = 'default') {
|
|
20
|
+
return `${sessionId}:${contractId}`
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
/** verify_contract:冻结契约,返回 id。 */
|
|
14
24
|
export function verifyContract(input, store = contracts) {
|
|
15
25
|
const c = freezeContract(input)
|
|
@@ -22,16 +32,23 @@ export function verifyContract(input, store = contracts) {
|
|
|
22
32
|
* H1 证据先于验收:evidence 缺失时不判 GREEN(gate 的 R1 已保证)。
|
|
23
33
|
* policy runner 由调用方注入(composePolicy),这里只做编排与落盘。
|
|
24
34
|
*/
|
|
25
|
-
export function verifyRun({ contractId, evidenceInput, runPolicy }, store = contracts, runStore = runs) {
|
|
35
|
+
export function verifyRun({ contractId, sessionId, evidenceInput, runPolicy, checkDeps, usePolicy }, store = contracts, runStore = runs) {
|
|
36
|
+
const rk = runKey(contractId, sessionId)
|
|
26
37
|
const entry = store.get(contractId)
|
|
27
38
|
if (!entry) return { decision: 'RED', reasons: [`未知 contractId ${contractId}`], evidenceHash: '' }
|
|
28
39
|
|
|
40
|
+
// 0.2.0 P0-3:显式要求用策略且未给 checks 时,按 policy 生成检查请求
|
|
41
|
+
if (usePolicy === true && !evidenceInput && typeof runPolicy !== 'function') {
|
|
42
|
+
const policy = loadPolicy(entry.contract.policyId || 'code-change')
|
|
43
|
+
evidenceInput = { checks: policyToChecks(policy, entry.contract) }
|
|
44
|
+
}
|
|
45
|
+
|
|
29
46
|
// H1:既没有证据输入、也没有策略来产出证据 → 直接判 YELLOW,不伪造证据
|
|
30
47
|
if (!evidenceInput && typeof runPolicy !== 'function') {
|
|
31
48
|
const d = decide(entry.contract, { checks: [], artifacts: [] })
|
|
32
49
|
const ev = makeEvidence({ contractId })
|
|
33
50
|
const hash = evidenceHash(ev)
|
|
34
|
-
runStore.set(
|
|
51
|
+
runStore.set(rk, { decision: d.decision, at: new Date().toISOString(), evidenceHash: hash, missing: d.missing, reproCmd: d.reproCmd })
|
|
35
52
|
return {
|
|
36
53
|
decision: d.decision, reasons: d.reasons, evidenceHash: hash,
|
|
37
54
|
missing: d.missing,
|
|
@@ -39,25 +56,35 @@ export function verifyRun({ contractId, evidenceInput, runPolicy }, store = cont
|
|
|
39
56
|
}
|
|
40
57
|
}
|
|
41
58
|
|
|
59
|
+
// 0.2.0:checks 是请求,由插件真实执行(模型自报 ok 被忽略并记 warning)
|
|
42
60
|
const evidence = typeof runPolicy === 'function'
|
|
43
61
|
? runPolicy(entry.contract, evidenceInput)
|
|
44
|
-
:
|
|
62
|
+
: (() => {
|
|
63
|
+
const executed = runChecks(evidenceInput?.checks ?? [], checkDeps)
|
|
64
|
+
return makeEvidence({
|
|
65
|
+
contractId,
|
|
66
|
+
...evidenceInput,
|
|
67
|
+
checks: executed.checks,
|
|
68
|
+
warnings: [...(evidenceInput?.warnings ?? []), ...executed.warnings],
|
|
69
|
+
})
|
|
70
|
+
})()
|
|
45
71
|
|
|
46
72
|
const d = decide(entry.contract, evidence)
|
|
47
73
|
const hash = evidenceHash(evidence)
|
|
48
|
-
runStore.set(
|
|
74
|
+
runStore.set(rk, { decision: d.decision, at: new Date().toISOString(), evidenceHash: hash, missing: d.missing, reproCmd: d.reproCmd })
|
|
49
75
|
return {
|
|
50
76
|
decision: d.decision,
|
|
51
77
|
reasons: d.reasons,
|
|
52
78
|
evidenceHash: hash,
|
|
53
79
|
missing: d.missing,
|
|
80
|
+
...(Array.isArray(evidence.warnings) && evidence.warnings.length > 0 ? { warnings: evidence.warnings } : {}),
|
|
54
81
|
...(d.reproCmd ? { reproCmd: d.reproCmd } : {}),
|
|
55
82
|
}
|
|
56
83
|
}
|
|
57
84
|
|
|
58
85
|
/** verify_status:最近一次 decision 与时间。 */
|
|
59
|
-
export function verifyStatus({ contractId }, runStore = runs) {
|
|
60
|
-
const r = runStore.get(contractId)
|
|
86
|
+
export function verifyStatus({ contractId, sessionId }, runStore = runs) {
|
|
87
|
+
const r = runStore.get(runKey(contractId, sessionId))
|
|
61
88
|
// 未知契约返回 known:false,不落进 GREEN/YELLOW/RED 三态(评审必改3)
|
|
62
89
|
if (!r) return { known: false }
|
|
63
90
|
return {
|
|
@@ -67,6 +94,7 @@ export function verifyStatus({ contractId }, runStore = runs) {
|
|
|
67
94
|
...(r.reproCmd ? { reproCmd: r.reproCmd } : {}),
|
|
68
95
|
...(Array.isArray(r.missing) && r.missing.length > 0 ? { missing: r.missing } : {}),
|
|
69
96
|
...(r.approved === true ? { approved: true, approvedAt: r.approvedAt } : {}),
|
|
97
|
+
...(r.stale === true ? { stale: true } : {}),
|
|
70
98
|
}
|
|
71
99
|
}
|
|
72
100
|
|
|
@@ -98,35 +126,49 @@ export function newStores() {
|
|
|
98
126
|
// 命令只能由用户触发(斜杠命令不走模型工具),H3 由结构保证,不靠 act 守卫。
|
|
99
127
|
// 状态与 verify 同源:契约在 contracts,最近决定在 runs,approved 也写回 runs。
|
|
100
128
|
|
|
101
|
-
/** /gate-approve:非 GREEN
|
|
102
|
-
export function gateApprove({ contractId }, store = contracts, runStore = runs) {
|
|
129
|
+
/** /gate-approve:非 GREEN 拒绝;证据过期拒绝;GREEN 记 approved 返回成功。 */
|
|
130
|
+
export function gateApprove({ contractId, sessionId, expectedHash }, store = contracts, runStore = runs) {
|
|
103
131
|
if (!store.has(contractId)) return { kind: 'error', text: `未知契约 ${contractId}` }
|
|
104
|
-
const
|
|
132
|
+
const rk = runKey(contractId, sessionId)
|
|
133
|
+
const r = runStore.get(rk)
|
|
105
134
|
if (!r) return { kind: 'error', text: '尚无门禁决定,请先执行 verify_run' }
|
|
106
135
|
if (r.decision !== 'GREEN') {
|
|
107
136
|
return { kind: 'error', text: `非 GREEN 不可放行,请先 /gate-retry ${contractId}` }
|
|
108
137
|
}
|
|
109
|
-
|
|
138
|
+
// P0-4:evidenceHash 与最近一次 verify_run 不一致 → 证据已过期
|
|
139
|
+
if (expectedHash !== undefined && r.evidenceHash !== expectedHash) {
|
|
140
|
+
return { kind: 'error', text: '证据已过期,请重新 verify_run' }
|
|
141
|
+
}
|
|
142
|
+
runStore.set(rk, { ...r, approved: true, approvedAt: new Date().toISOString(), by: 'user', stale: false })
|
|
110
143
|
return { kind: 'success', text: `契约 ${contractId} 已由用户放行(approved)` }
|
|
111
144
|
}
|
|
112
145
|
|
|
113
|
-
/** /gate-retry:空证据重算 → YELLOW
|
|
114
|
-
export function gateRetry({ contractId }, store = contracts, runStore = runs) {
|
|
146
|
+
/** /gate-retry:空证据重算 → YELLOW;保留 approved 但置 stale、清 approvedAt。 */
|
|
147
|
+
export function gateRetry({ contractId, sessionId }, store = contracts, runStore = runs) {
|
|
115
148
|
if (!store.has(contractId)) return { kind: 'error', text: `未知契约 ${contractId}` }
|
|
116
|
-
const
|
|
149
|
+
const rk = runKey(contractId, sessionId)
|
|
150
|
+
const before = runStore.get(rk)
|
|
151
|
+
const wasApproved = before?.approved === true
|
|
152
|
+
const r = verifyRun({ contractId, sessionId, evidenceInput: undefined }, store, runStore)
|
|
153
|
+
// verifyRun 覆盖了整条记录,把旧的 approved/stale 语义补回来
|
|
154
|
+
const after = runStore.get(rk)
|
|
155
|
+
if (wasApproved) runStore.set(rk, { ...after, approved: true, stale: true, approvedAt: undefined, by: before.by })
|
|
156
|
+
else runStore.set(rk, { ...after, stale: true })
|
|
157
|
+
const now = verifyStatus({ contractId, sessionId }, runStore)
|
|
117
158
|
return {
|
|
118
159
|
kind: 'success',
|
|
119
|
-
text: `契约 ${contractId} 已重算:${r.decision} @ ${
|
|
160
|
+
text: `契约 ${contractId} 已重算:${r.decision} @ ${now.at}${wasApproved ? ' [stale](原放行已失效)' : ''}。需重新执行 verify_run 补证据后才可能转 GREEN。`,
|
|
120
161
|
}
|
|
121
162
|
}
|
|
122
163
|
|
|
123
|
-
/** /gate-status:最近决定 + 时间 + 是否 approved;未知契约 error。 */
|
|
124
|
-
export function gateStatus({ contractId }, store = contracts, runStore = runs) {
|
|
164
|
+
/** /gate-status:最近决定 + 时间 + 是否 approved / stale;未知契约 error。 */
|
|
165
|
+
export function gateStatus({ contractId, sessionId }, store = contracts, runStore = runs) {
|
|
125
166
|
if (!store.has(contractId)) return { kind: 'error', text: `未知契约 ${contractId}` }
|
|
126
|
-
const r = runStore.get(contractId)
|
|
167
|
+
const r = runStore.get(runKey(contractId, sessionId))
|
|
127
168
|
if (!r) return { kind: 'success', text: `契约 ${contractId} 尚无门禁决定` }
|
|
128
169
|
const approved = r.approved === true
|
|
129
|
-
const
|
|
170
|
+
const stale = r.stale === true
|
|
171
|
+
const head = `契约 ${contractId}:最近决定 ${r.decision} @ ${r.at}${stale ? ' [stale]' : ''}${approved ? `;已由 ${r.by ?? 'user'} 放行${stale ? '(已过期)' : ` @ ${r.approvedAt}`}` : ';尚未放行'}`
|
|
130
172
|
// H2 可见性:RED 必须把复现命令亮给用户,YELLOW 必须把缺失证据亮出来
|
|
131
173
|
const extra = []
|
|
132
174
|
if (r.reproCmd) extra.push(`复现:${r.reproCmd}`)
|
package/package.json
CHANGED
|
@@ -1,24 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-swarm-console",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Run one external agent from DSH, with a visible gate (v0.1
|
|
6
|
-
"keywords": [
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-swarm-console",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Run one external agent from DSH, with a visible gate (v0.1 · 门禁可用,画布开发中).",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"deepseek-harness",
|
|
8
|
+
"dsh-plugin",
|
|
9
|
+
"agent",
|
|
10
|
+
"swarm",
|
|
11
|
+
"verification",
|
|
12
|
+
"gate"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/whyissodifficult/dsh-agent-swarm-console.git",
|
|
17
|
+
"directory": "plugin"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "郑志阳 (Walk in the white)",
|
|
21
|
+
"homepage": "https://github.com/whyissodifficult/dsh-agent-swarm-console",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/whyissodifficult/dsh-agent-swarm-console/issues"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20.3"
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
".": "./index.js",
|
|
30
|
+
"./locale/*.json": "./locale/*.json",
|
|
31
|
+
"./icon": "./icon.svg"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"index.js",
|
|
35
|
+
"lib",
|
|
36
|
+
"policies",
|
|
37
|
+
"cordis.patch.yml",
|
|
38
|
+
"locale",
|
|
39
|
+
"docs",
|
|
40
|
+
"icon.svg"
|
|
41
|
+
],
|
|
42
|
+
"dsh": {
|
|
43
|
+
"bundle": {
|
|
44
|
+
"patch": "./cordis.patch.yml"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "code-change",
|
|
3
|
+
"label": "代码改动策略",
|
|
4
|
+
"checks": [
|
|
5
|
+
{
|
|
6
|
+
"id": "exists",
|
|
7
|
+
"type": "file.exists",
|
|
8
|
+
"description": "验收条件指定的文件存在",
|
|
9
|
+
"input": "criteria[].checkIds -> artifacts[].path"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"id": "diff",
|
|
13
|
+
"type": "workspace.diff",
|
|
14
|
+
"description": "取宿主 workspaceChanges.summary|diff,不自建 diff",
|
|
15
|
+
"input": "workspaceChanges.summary || workspaceChanges.diff"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"id": "tests",
|
|
19
|
+
"type": "exec",
|
|
20
|
+
"description": "执行测试命令,退出码必须为 0",
|
|
21
|
+
"input": "constraints[].testCmd || 'npm test'",
|
|
22
|
+
"successExitCode": 0
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|