dsh-swarm-console 0.2.0 → 0.2.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.
@@ -1,97 +1,98 @@
1
- /**
2
- * D2 · 证据结构。
3
- * level: E0 无证据 / E1 有产物但无执行检查 / E2 有可复现执行检查
4
- * status: verified / failed / partial
5
- * 零运行时依赖。
6
- */
7
- import { createHash } from 'node:crypto'
8
-
9
- /**
10
- * @param {{contractId:string, artifacts?:Array, checks?:Array, level?:string, status?:string, at?:string}} input
11
- */
12
- export function makeEvidence(input) {
13
- if (!input || typeof input.contractId !== 'string' || input.contractId === '') {
14
- throw new Error('contractId 必填')
15
- }
16
- const artifacts = normalizeArtifacts(input.artifacts)
17
- const checks = normalizeChecks(input.checks)
18
- const level = ['E0', 'E1', 'E2'].includes(input.level) ? input.level : inferLevel(checks, artifacts)
19
- const status = ['verified', 'failed', 'partial'].includes(input.status)
20
- ? input.status
21
- : inferStatus(checks)
22
- const at = typeof input.at === 'string' && input.at !== '' ? input.at : new Date().toISOString()
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 }
26
- }
27
-
28
- /** 证据内容哈希(用于 verify_run 返回的 evidenceHash)。 */
29
- export function evidenceHash(evidence) {
30
- const canonical = JSON.stringify({
31
- contractId: evidence.contractId,
32
- artifacts: evidence.artifacts,
33
- checks: evidence.checks,
34
- })
35
- return createHash('sha256').update(canonical).digest('hex').slice(0, 12)
36
- }
37
-
38
- export function validateEvidence(evidence) {
39
- const errs = []
40
- if (!evidence || typeof evidence !== 'object') return ['evidence 必须是对象']
41
- if (typeof evidence.contractId !== 'string' || evidence.contractId === '') errs.push('contractId 必填')
42
- if (!Array.isArray(evidence.artifacts)) errs.push('artifacts 必须是数组')
43
- if (!Array.isArray(evidence.checks)) errs.push('checks 必须是数组')
44
- else for (const [i, c] of evidence.checks.entries()) {
45
- if (!c || typeof c.id !== 'string' || c.id === '') errs.push(`checks[${i}].id 必填`)
46
- if (typeof c.ok !== 'boolean') errs.push(`checks[${i}].ok 必须是布尔`)
47
- if (typeof c.type !== 'string' || c.type === '') errs.push(`checks[${i}].type 必填`)
48
- }
49
- if (!['E0', 'E1', 'E2'].includes(evidence.level)) errs.push('level 必须是 E0/E1/E2')
50
- if (!['verified', 'failed', 'partial'].includes(evidence.status)) errs.push('status 必须是 verified/failed/partial')
51
- return errs
52
- }
53
-
54
- function inferLevel(checks, artifacts) {
55
- if (checks.some((c) => c.exitCode !== undefined || c.cmd)) return 'E2'
56
- if (checks.length > 0 || artifacts.length > 0) return 'E1'
57
- return 'E0'
58
- }
59
-
60
- function inferStatus(checks) {
61
- if (checks.length === 0) return 'partial'
62
- if (checks.every((c) => c.ok)) return 'verified'
63
- if (checks.some((c) => !c.ok)) return 'failed'
64
- return 'partial'
65
- }
66
-
67
- function normalizeArtifacts(value) {
68
- if (value === undefined || value === null) return []
69
- if (!Array.isArray(value)) throw new Error('artifacts 必须是数组')
70
- return value.map((a, i) => {
71
- if (!a || typeof a !== 'object') throw new Error(`artifacts[${i}] 必须是对象`)
72
- if (typeof a.path !== 'string' || a.path === '') throw new Error(`artifacts[${i}].path 必填`)
73
- return {
74
- path: a.path,
75
- sha256: typeof a.sha256 === 'string' ? a.sha256 : '',
76
- size: Number.isFinite(a.size) ? a.size : 0,
77
- }
78
- })
79
- }
80
-
81
- function normalizeChecks(value) {
82
- if (value === undefined || value === null) return []
83
- if (!Array.isArray(value)) throw new Error('checks 必须是数组')
84
- return value.map((c, i) => {
85
- if (!c || typeof c !== 'object') throw new Error(`checks[${i}] 必须是对象`)
86
- if (typeof c.id !== 'string' || c.id === '') throw new Error(`checks[${i}].id 必填`)
87
- if (typeof c.type !== 'string' || c.type === '') throw new Error(`checks[${i}].type 必填`)
88
- if (typeof c.ok !== 'boolean') throw new Error(`checks[${i}].ok 必须是布尔`)
89
- // exec 类 check 必须有可执行 cmd —— 缺 cmd 的失败无法复现,H2 要求策略层就拒绝。
90
- if (c.type === 'exec' && c.ok === false && (typeof c.cmd !== 'string' || c.cmd.trim() === '')) {
91
- throw new Error(`checks[${i}] exec 类失败 check 必须提供可执行 cmd(H2 reproCmd)`)
92
- }
93
- const out = { id: c.id, type: c.type, ok: c.ok }
94
- for (const k of ['detail', 'exitCode', 'cmd']) if (c[k] !== undefined) out[k] = c[k]
95
- return out
96
- })
97
- }
1
+ /**
2
+ * D2 · 证据结构。
3
+ * level: E0 无证据 / E1 有产物但无执行检查 / E2 有可复现执行检查
4
+ * status: verified / failed / partial
5
+ * 零运行时依赖。
6
+ */
7
+ import { createHash } from 'node:crypto'
8
+
9
+ /**
10
+ * @param {{contractId:string, artifacts?:Array, checks?:Array, level?:string, status?:string, at?:string}} input
11
+ */
12
+ export function makeEvidence(input) {
13
+ if (!input || typeof input.contractId !== 'string' || input.contractId === '') {
14
+ throw new Error('contractId 必填')
15
+ }
16
+ const artifacts = normalizeArtifacts(input.artifacts)
17
+ const checks = normalizeChecks(input.checks)
18
+ const level = ['E0', 'E1', 'E2'].includes(input.level) ? input.level : inferLevel(checks, artifacts)
19
+ const status = ['verified', 'failed', 'partial'].includes(input.status)
20
+ ? input.status
21
+ : inferStatus(checks)
22
+ const at = typeof input.at === 'string' && input.at !== '' ? input.at : new Date().toISOString()
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 }
26
+ }
27
+
28
+ /** 证据内容哈希(用于 verify_run 返回的 evidenceHash)。 */
29
+ export function evidenceHash(evidence) {
30
+ const canonical = JSON.stringify({
31
+ contractId: evidence.contractId,
32
+ artifacts: evidence.artifacts,
33
+ checks: evidence.checks,
34
+ })
35
+ return createHash('sha256').update(canonical).digest('hex').slice(0, 12)
36
+ }
37
+
38
+ export function validateEvidence(evidence) {
39
+ const errs = []
40
+ if (!evidence || typeof evidence !== 'object') return ['evidence 必须是对象']
41
+ if (typeof evidence.contractId !== 'string' || evidence.contractId === '') errs.push('contractId 必填')
42
+ if (!Array.isArray(evidence.artifacts)) errs.push('artifacts 必须是数组')
43
+ if (!Array.isArray(evidence.checks)) errs.push('checks 必须是数组')
44
+ else for (const [i, c] of evidence.checks.entries()) {
45
+ if (!c || typeof c.id !== 'string' || c.id === '') errs.push(`checks[${i}].id 必填`)
46
+ if (typeof c.ok !== 'boolean') errs.push(`checks[${i}].ok 必须是布尔`)
47
+ if (typeof c.type !== 'string' || c.type === '') errs.push(`checks[${i}].type 必填`)
48
+ }
49
+ if (!['E0', 'E1', 'E2'].includes(evidence.level)) errs.push('level 必须是 E0/E1/E2')
50
+ if (!['verified', 'failed', 'partial'].includes(evidence.status)) errs.push('status 必须是 verified/failed/partial')
51
+ return errs
52
+ }
53
+
54
+ function inferLevel(checks, artifacts) {
55
+ if (checks.some((c) => c.exitCode !== undefined || c.cmd)) return 'E2'
56
+ if (checks.length > 0 || artifacts.length > 0) return 'E1'
57
+ return 'E0'
58
+ }
59
+
60
+ function inferStatus(checks) {
61
+ if (checks.length === 0) return 'partial'
62
+ if (checks.every((c) => c.ok)) return 'verified'
63
+ if (checks.some((c) => !c.ok)) return 'failed'
64
+ return 'partial'
65
+ }
66
+
67
+ function normalizeArtifacts(value) {
68
+ if (value === undefined || value === null) return []
69
+ if (!Array.isArray(value)) throw new Error('artifacts 必须是数组')
70
+ return value.map((a, i) => {
71
+ if (!a || typeof a !== 'object') throw new Error(`artifacts[${i}] 必须是对象`)
72
+ if (typeof a.path !== 'string' || a.path === '') throw new Error(`artifacts[${i}].path 必填`)
73
+ return {
74
+ path: a.path,
75
+ sha256: typeof a.sha256 === 'string' ? a.sha256 : '',
76
+ size: Number.isFinite(a.size) ? a.size : 0,
77
+ }
78
+ })
79
+ }
80
+
81
+ function normalizeChecks(value) {
82
+ if (value === undefined || value === null) return []
83
+ if (!Array.isArray(value)) throw new Error('checks 必须是数组')
84
+ return value.map((c, i) => {
85
+ if (!c || typeof c !== 'object') throw new Error(`checks[${i}] 必须是对象`)
86
+ if (typeof c.id !== 'string' || c.id === '') throw new Error(`checks[${i}].id 必填`)
87
+ if (typeof c.type !== 'string' || c.type === '') throw new Error(`checks[${i}].type 必填`)
88
+ if (typeof c.ok !== 'boolean') throw new Error(`checks[${i}].ok 必须是布尔`)
89
+ // exec 类 check 必须有可执行 cmd —— 缺 cmd 的失败无法复现,H2 要求策略层就拒绝。
90
+ if (c.type === 'exec' && c.ok === false && (typeof c.cmd !== 'string' || c.cmd.trim() === '')) {
91
+ throw new Error(`checks[${i}] exec 类失败 check 必须提供可执行 cmd(H2 reproCmd)`)
92
+ }
93
+ const out = { id: c.id, type: c.type, ok: c.ok }
94
+ // path 必须保留:file.exists 失败时 gate 要用它生成 `test -f <path>` 复现命令
95
+ for (const k of ['detail', 'exitCode', 'cmd', 'path']) if (c[k] !== undefined) out[k] = c[k]
96
+ return out
97
+ })
98
+ }
package/lib/core/gate.js CHANGED
@@ -1,69 +1,77 @@
1
- /**
2
- * D3 · 三态门禁纯函数。
3
- * decide(contract, evidence) → { decision, reasons, missing, reproCmd? }
4
- *
5
- * R1 任一 criterion 的 checkIds 为空,或没有对应 evidence.checks → 最高 YELLOW
6
- * R2 任一 check.ok === false → RED,reasons 必须包含 reproCmd(可复制执行)
7
- * R3 全部 criteria 有证据且通过 → GREEN
8
- *
9
- * 优先级:RED > YELLOW > GREEN。纯函数,零副作用、零依赖。
10
- */
11
-
12
- /**
13
- * @param {object} contract TaskContract
14
- * @param {object} evidence Evidence
15
- */
16
- export function decide(contract, evidence) {
17
- const reasons = []
18
- const missing = []
19
-
20
- if (!contract || !Array.isArray(contract.criteria) || contract.criteria.length === 0) {
21
- return { decision: 'RED', reasons: ['契约没有任何验收条件,不可判通过'], missing: [] }
22
- }
23
- const checks = Array.isArray(evidence?.checks) ? evidence.checks : []
24
- const byId = new Map(checks.map((c) => [c.id, c]))
25
-
26
- // R2 先行:任一 check 失败即 RED,并给出最小复现命令
27
- const failed = checks.filter((c) => c.ok === false)
28
- if (failed.length > 0) {
29
- const reproCmd = failed.map((c) => reproFor(c)).filter(Boolean).join(' && ')
30
- for (const c of failed) {
31
- reasons.push(`check "${c.id}" 失败:${c.detail || c.type}${c.cmd ? `(${c.cmd})` : ''}`)
32
- }
33
- if (!reproCmd) reasons.push('未提供可复现命令(reproCmd 为空)')
34
- return { decision: 'RED', reasons, missing, reproCmd: reproCmd || undefined }
35
- }
36
-
37
- // R1:逐条 criterion 查证据覆盖
38
- for (const criterion of contract.criteria) {
39
- const ids = Array.isArray(criterion.checkIds) ? criterion.checkIds : []
40
- if (ids.length === 0) {
41
- missing.push(`${criterion.id}: 未绑定 checkIds`)
42
- continue
43
- }
44
- const hit = ids.filter((id) => byId.has(id))
45
- if (hit.length === 0) {
46
- missing.push(`${criterion.id}: 没有对应 evidence.checks(需要 ${ids.join(', ')})`)
47
- } else if (hit.length < ids.length) {
48
- const absent = ids.filter((id) => !byId.has(id))
49
- missing.push(`${criterion.id}: 缺少检查 ${absent.join(', ')}`)
50
- }
51
- }
52
-
53
- if (missing.length > 0) {
54
- reasons.push(`${missing.length} 条验收条件缺少证据,不能判为通过`)
55
- return { decision: 'YELLOW', reasons, missing }
56
- }
57
-
58
- // R3:全部覆盖且全通过
59
- reasons.push(`全部 ${contract.criteria.length} 条验收条件均有通过证据`)
60
- return { decision: 'GREEN', reasons, missing }
61
- }
62
-
63
- /** 从失败 check 构造可复制执行的最小复现命令。 */
64
- function reproFor(check) {
65
- if (typeof check.cmd === 'string' && check.cmd.trim() !== '') return check.cmd
66
- // H2 兜底(正常路径下 exec 缺 cmd 已在 evidence 层被拒,见 normalizeChecks):
67
- // 给一条 sh -c 可直接执行且退出非 0 的真命令,不是装饰性注释。
68
- return `echo "check ${check.id} 未提供 cmd" && exit 1`
69
- }
1
+ /**
2
+ * D3 · 三态门禁纯函数。
3
+ * decide(contract, evidence) → { decision, reasons, missing, reproCmd? }
4
+ *
5
+ * R1 任一 criterion 的 checkIds 为空,或没有对应 evidence.checks → 最高 YELLOW
6
+ * R2 任一 check.ok === false → RED,reasons 必须包含 reproCmd(可复制执行)
7
+ * R3 全部 criteria 有证据且通过 → GREEN
8
+ *
9
+ * 优先级:RED > YELLOW > GREEN。纯函数,零副作用、零依赖。
10
+ */
11
+
12
+ /**
13
+ * @param {object} contract TaskContract
14
+ * @param {object} evidence Evidence
15
+ */
16
+ export function decide(contract, evidence) {
17
+ const reasons = []
18
+ const missing = []
19
+
20
+ if (!contract || !Array.isArray(contract.criteria) || contract.criteria.length === 0) {
21
+ return { decision: 'RED', reasons: ['契约没有任何验收条件,不可判通过'], missing: [] }
22
+ }
23
+ const checks = Array.isArray(evidence?.checks) ? evidence.checks : []
24
+ const byId = new Map(checks.map((c) => [c.id, c]))
25
+
26
+ // R2 先行:任一 check 失败即 RED,并给出最小复现命令
27
+ const failed = checks.filter((c) => c.ok === false)
28
+ if (failed.length > 0) {
29
+ const reproCmd = failed.map((c) => reproFor(c)).filter(Boolean).join(' && ')
30
+ for (const c of failed) {
31
+ reasons.push(`check "${c.id}" 失败:${c.detail || c.type}${c.cmd ? `(${c.cmd})` : ''}`)
32
+ }
33
+ if (!reproCmd) reasons.push('未提供可复现命令(reproCmd 为空)')
34
+ return { decision: 'RED', reasons, missing, reproCmd: reproCmd || undefined }
35
+ }
36
+
37
+ // R1:逐条 criterion 查证据覆盖
38
+ for (const criterion of contract.criteria) {
39
+ const ids = Array.isArray(criterion.checkIds) ? criterion.checkIds : []
40
+ if (ids.length === 0) {
41
+ missing.push(`${criterion.id}: 未绑定 checkIds`)
42
+ continue
43
+ }
44
+ const hit = ids.filter((id) => byId.has(id))
45
+ if (hit.length === 0) {
46
+ missing.push(`${criterion.id}: 没有对应 evidence.checks(需要 ${ids.join(', ')})`)
47
+ } else if (hit.length < ids.length) {
48
+ const absent = ids.filter((id) => !byId.has(id))
49
+ missing.push(`${criterion.id}: 缺少检查 ${absent.join(', ')}`)
50
+ }
51
+ }
52
+
53
+ if (missing.length > 0) {
54
+ reasons.push(`${missing.length} 条验收条件缺少证据,不能判为通过`)
55
+ return { decision: 'YELLOW', reasons, missing }
56
+ }
57
+
58
+ // R3:全部覆盖且全通过
59
+ reasons.push(`全部 ${contract.criteria.length} 条验收条件均有通过证据`)
60
+ return { decision: 'GREEN', reasons, missing }
61
+ }
62
+
63
+ /** 从失败 check 构造可复制执行的最小复现命令(按检查类型给对应命令)。 */
64
+ function reproFor(check) {
65
+ if (typeof check.cmd === 'string' && check.cmd.trim() !== '') return check.cmd
66
+ // file.exists:文件缺失时 `test -f` 退出 1,正好复现这次失败
67
+ if (check.type === 'file.exists' && typeof check.path === 'string' && check.path !== '') {
68
+ return `test -f ${shellQuote(check.path)}`
69
+ }
70
+ // 其余情况兜底:给一条 sh -c 可直接执行且退出非 0 的真命令,不是装饰性注释
71
+ return `echo "check ${check.id} 无可执行复现命令(该检查类型未提供路径/命令)" && exit 1`
72
+ }
73
+
74
+ /** POSIX 单引号包裹,内部单引号转义为 '\'' */
75
+ function shellQuote(value) {
76
+ return "'" + String(value).replace(/'/g, "'\\''") + "'"
77
+ }
package/package.json CHANGED
@@ -1,47 +1,47 @@
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
- }
1
+ {
2
+ "name": "dsh-swarm-console",
3
+ "version": "0.2.1",
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
+ }