dsh-swarm-console 0.1.2 → 0.1.3
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/index.js +16 -5
- package/lib/core/verify.js +135 -126
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -281,13 +281,24 @@ function registerVerifyTools(ctx) {
|
|
|
281
281
|
output: {
|
|
282
282
|
schema: {
|
|
283
283
|
type: 'object',
|
|
284
|
-
properties: {
|
|
284
|
+
properties: {
|
|
285
|
+
known: { type: 'boolean' },
|
|
286
|
+
decision: { type: 'string' },
|
|
287
|
+
at: { type: 'string' },
|
|
288
|
+
reproCmd: { type: 'string' },
|
|
289
|
+
missing: { type: 'array', items: { type: 'string' } },
|
|
290
|
+
approved: { type: 'boolean' },
|
|
291
|
+
approvedAt: { type: 'string' },
|
|
292
|
+
},
|
|
285
293
|
required: ['known'],
|
|
286
294
|
},
|
|
287
|
-
render: (args, v) =>
|
|
288
|
-
type: 'text',
|
|
289
|
-
|
|
290
|
-
|
|
295
|
+
render: (args, v) => {
|
|
296
|
+
if (!v.known) return [{ type: 'text', text: '未知 contractId,尚无门禁记录' }]
|
|
297
|
+
const lines = [`最近决定 ${v.decision}${v.at ? ' @ ' + v.at : ''}${v.approved ? `;已由用户放行 @ ${v.approvedAt}` : ';尚未放行'}`]
|
|
298
|
+
if (v.reproCmd) lines.push(`复现:${v.reproCmd}`)
|
|
299
|
+
if (Array.isArray(v.missing) && v.missing.length > 0) lines.push(`缺失证据:${v.missing.join(';')}`)
|
|
300
|
+
return [{ type: 'text', text: lines.join('\n') }]
|
|
301
|
+
},
|
|
291
302
|
},
|
|
292
303
|
execute: async (args) => verifyStatus({ contractId: args.contractId }),
|
|
293
304
|
},
|
package/lib/core/verify.js
CHANGED
|
@@ -1,126 +1,135 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* D5/D6 · verify v1 的宿主无关核心。
|
|
3
|
-
* 三工具的纯逻辑 + 卡片 act 的调用者判定,便于不装 DSH 就能单测。
|
|
4
|
-
* 状态用进程内 Map(v1,不做持久化;D8 明确不做多阶段状态机)。
|
|
5
|
-
*/
|
|
6
|
-
import { freezeContract } from './contract.js'
|
|
7
|
-
import { makeEvidence, evidenceHash } from './evidence.js'
|
|
8
|
-
import { decide } from './gate.js'
|
|
9
|
-
|
|
10
|
-
const contracts = new Map()
|
|
11
|
-
const runs = new Map()
|
|
12
|
-
|
|
13
|
-
/** verify_contract:冻结契约,返回 id。 */
|
|
14
|
-
export function verifyContract(input, store = contracts) {
|
|
15
|
-
const c = freezeContract(input)
|
|
16
|
-
store.set(c.id, { contract: c, frozenAt: c.frozenAt })
|
|
17
|
-
return { contractId: c.id }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* verify_run:跑策略 → 三态判定。
|
|
22
|
-
* H1 证据先于验收:evidence 缺失时不判 GREEN(gate 的 R1 已保证)。
|
|
23
|
-
* policy runner 由调用方注入(composePolicy),这里只做编排与落盘。
|
|
24
|
-
*/
|
|
25
|
-
export function verifyRun({ contractId, evidenceInput, runPolicy }, store = contracts, runStore = runs) {
|
|
26
|
-
const entry = store.get(contractId)
|
|
27
|
-
if (!entry) return { decision: 'RED', reasons: [`未知 contractId ${contractId}`], evidenceHash: '' }
|
|
28
|
-
|
|
29
|
-
// H1:既没有证据输入、也没有策略来产出证据 → 直接判 YELLOW,不伪造证据
|
|
30
|
-
if (!evidenceInput && typeof runPolicy !== 'function') {
|
|
31
|
-
const d = decide(entry.contract, { checks: [], artifacts: [] })
|
|
32
|
-
const ev = makeEvidence({ contractId })
|
|
33
|
-
const hash = evidenceHash(ev)
|
|
34
|
-
runStore.set(contractId, { decision: d.decision, at: new Date().toISOString(), evidenceHash: hash })
|
|
35
|
-
return {
|
|
36
|
-
decision: d.decision, reasons: d.reasons, evidenceHash: hash,
|
|
37
|
-
missing: d.missing,
|
|
38
|
-
...(d.reproCmd ? { reproCmd: d.reproCmd } : {}),
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const evidence = typeof runPolicy === 'function'
|
|
43
|
-
? runPolicy(entry.contract, evidenceInput)
|
|
44
|
-
: makeEvidence({ contractId, ...evidenceInput })
|
|
45
|
-
|
|
46
|
-
const d = decide(entry.contract, evidence)
|
|
47
|
-
const hash = evidenceHash(evidence)
|
|
48
|
-
runStore.set(contractId, { decision: d.decision, at: new Date().toISOString(), evidenceHash: hash })
|
|
49
|
-
return {
|
|
50
|
-
decision: d.decision,
|
|
51
|
-
reasons: d.reasons,
|
|
52
|
-
evidenceHash: hash,
|
|
53
|
-
missing: d.missing,
|
|
54
|
-
...(d.reproCmd ? { reproCmd: d.reproCmd } : {}),
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** verify_status:最近一次 decision 与时间。 */
|
|
59
|
-
export function verifyStatus({ contractId }, runStore = runs) {
|
|
60
|
-
const r = runStore.get(contractId)
|
|
61
|
-
// 未知契约返回 known:false,不落进 GREEN/YELLOW/RED 三态(评审必改3)
|
|
62
|
-
if (!r) return { known: false }
|
|
63
|
-
return {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return { kind: '
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
1
|
+
/**
|
|
2
|
+
* D5/D6 · verify v1 的宿主无关核心。
|
|
3
|
+
* 三工具的纯逻辑 + 卡片 act 的调用者判定,便于不装 DSH 就能单测。
|
|
4
|
+
* 状态用进程内 Map(v1,不做持久化;D8 明确不做多阶段状态机)。
|
|
5
|
+
*/
|
|
6
|
+
import { freezeContract } from './contract.js'
|
|
7
|
+
import { makeEvidence, evidenceHash } from './evidence.js'
|
|
8
|
+
import { decide } from './gate.js'
|
|
9
|
+
|
|
10
|
+
const contracts = new Map()
|
|
11
|
+
const runs = new Map()
|
|
12
|
+
|
|
13
|
+
/** verify_contract:冻结契约,返回 id。 */
|
|
14
|
+
export function verifyContract(input, store = contracts) {
|
|
15
|
+
const c = freezeContract(input)
|
|
16
|
+
store.set(c.id, { contract: c, frozenAt: c.frozenAt })
|
|
17
|
+
return { contractId: c.id }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* verify_run:跑策略 → 三态判定。
|
|
22
|
+
* H1 证据先于验收:evidence 缺失时不判 GREEN(gate 的 R1 已保证)。
|
|
23
|
+
* policy runner 由调用方注入(composePolicy),这里只做编排与落盘。
|
|
24
|
+
*/
|
|
25
|
+
export function verifyRun({ contractId, evidenceInput, runPolicy }, store = contracts, runStore = runs) {
|
|
26
|
+
const entry = store.get(contractId)
|
|
27
|
+
if (!entry) return { decision: 'RED', reasons: [`未知 contractId ${contractId}`], evidenceHash: '' }
|
|
28
|
+
|
|
29
|
+
// H1:既没有证据输入、也没有策略来产出证据 → 直接判 YELLOW,不伪造证据
|
|
30
|
+
if (!evidenceInput && typeof runPolicy !== 'function') {
|
|
31
|
+
const d = decide(entry.contract, { checks: [], artifacts: [] })
|
|
32
|
+
const ev = makeEvidence({ contractId })
|
|
33
|
+
const hash = evidenceHash(ev)
|
|
34
|
+
runStore.set(contractId, { decision: d.decision, at: new Date().toISOString(), evidenceHash: hash, missing: d.missing, reproCmd: d.reproCmd })
|
|
35
|
+
return {
|
|
36
|
+
decision: d.decision, reasons: d.reasons, evidenceHash: hash,
|
|
37
|
+
missing: d.missing,
|
|
38
|
+
...(d.reproCmd ? { reproCmd: d.reproCmd } : {}),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const evidence = typeof runPolicy === 'function'
|
|
43
|
+
? runPolicy(entry.contract, evidenceInput)
|
|
44
|
+
: makeEvidence({ contractId, ...evidenceInput })
|
|
45
|
+
|
|
46
|
+
const d = decide(entry.contract, evidence)
|
|
47
|
+
const hash = evidenceHash(evidence)
|
|
48
|
+
runStore.set(contractId, { decision: d.decision, at: new Date().toISOString(), evidenceHash: hash, missing: d.missing, reproCmd: d.reproCmd })
|
|
49
|
+
return {
|
|
50
|
+
decision: d.decision,
|
|
51
|
+
reasons: d.reasons,
|
|
52
|
+
evidenceHash: hash,
|
|
53
|
+
missing: d.missing,
|
|
54
|
+
...(d.reproCmd ? { reproCmd: d.reproCmd } : {}),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** verify_status:最近一次 decision 与时间。 */
|
|
59
|
+
export function verifyStatus({ contractId }, runStore = runs) {
|
|
60
|
+
const r = runStore.get(contractId)
|
|
61
|
+
// 未知契约返回 known:false,不落进 GREEN/YELLOW/RED 三态(评审必改3)
|
|
62
|
+
if (!r) return { known: false }
|
|
63
|
+
return {
|
|
64
|
+
known: true,
|
|
65
|
+
decision: r.decision,
|
|
66
|
+
at: r.at,
|
|
67
|
+
...(r.reproCmd ? { reproCmd: r.reproCmd } : {}),
|
|
68
|
+
...(Array.isArray(r.missing) && r.missing.length > 0 ? { missing: r.missing } : {}),
|
|
69
|
+
...(r.approved === true ? { approved: true, approvedAt: r.approvedAt } : {}),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* H3 · act() 的调用者守卫。
|
|
75
|
+
* 非用户(subagent / 代码发起 / 无 session)→ refused + 记拒绝事件。
|
|
76
|
+
* @returns {{status:'refused',reason}|null} null 表示允许继续
|
|
77
|
+
*/
|
|
78
|
+
export function guardCaller(agent, record) {
|
|
79
|
+
const origin = agent?.session?.header?.origin
|
|
80
|
+
const fromAgent = origin === 'subagent' || agent?.fromAgent === true || agent?.origin === 'subagent'
|
|
81
|
+
const live = agent && typeof agent === 'object' && !agent.disposed
|
|
82
|
+
if (!live || fromAgent) {
|
|
83
|
+
const reason = !live ? 'not-live-root' : 'agent-initiated-not-user'
|
|
84
|
+
if (typeof record === 'function') {
|
|
85
|
+
record(agent, { callId: agent?.callId ?? 'unknown', action: 'dsh-swarm-console/gate', phase: 'refused', data: { reason } })
|
|
86
|
+
}
|
|
87
|
+
return { status: 'refused', reason }
|
|
88
|
+
}
|
|
89
|
+
return null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 供测试隔离用的空 store。 */
|
|
93
|
+
export function newStores() {
|
|
94
|
+
return { contracts: new Map(), runs: new Map() }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ── v1 放行入口:斜杠命令核心(宿主无关)─────────────────────────────
|
|
98
|
+
// 命令只能由用户触发(斜杠命令不走模型工具),H3 由结构保证,不靠 act 守卫。
|
|
99
|
+
// 状态与 verify 同源:契约在 contracts,最近决定在 runs,approved 也写回 runs。
|
|
100
|
+
|
|
101
|
+
/** /gate-approve:非 GREEN 拒绝;GREEN 记 approved 返回成功。 */
|
|
102
|
+
export function gateApprove({ contractId }, store = contracts, runStore = runs) {
|
|
103
|
+
if (!store.has(contractId)) return { kind: 'error', text: `未知契约 ${contractId}` }
|
|
104
|
+
const r = runStore.get(contractId)
|
|
105
|
+
if (!r) return { kind: 'error', text: '尚无门禁决定,请先执行 verify_run' }
|
|
106
|
+
if (r.decision !== 'GREEN') {
|
|
107
|
+
return { kind: 'error', text: `非 GREEN 不可放行,请先 /gate-retry ${contractId}` }
|
|
108
|
+
}
|
|
109
|
+
runStore.set(contractId, { ...r, approved: true, approvedAt: new Date().toISOString(), by: 'user' })
|
|
110
|
+
return { kind: 'success', text: `契约 ${contractId} 已由用户放行(approved)` }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** /gate-retry:空证据重算 → YELLOW,刷新决定时间戳,提示补证据。 */
|
|
114
|
+
export function gateRetry({ contractId }, store = contracts, runStore = runs) {
|
|
115
|
+
if (!store.has(contractId)) return { kind: 'error', text: `未知契约 ${contractId}` }
|
|
116
|
+
const r = verifyRun({ contractId, evidenceInput: undefined }, store, runStore)
|
|
117
|
+
return {
|
|
118
|
+
kind: 'success',
|
|
119
|
+
text: `契约 ${contractId} 已重算:${r.decision} @ ${verifyStatus({ contractId }, runStore).at}。需重新执行 verify_run 补证据后才可能转 GREEN。`,
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** /gate-status:最近决定 + 时间 + 是否 approved;未知契约 error。 */
|
|
124
|
+
export function gateStatus({ contractId }, store = contracts, runStore = runs) {
|
|
125
|
+
if (!store.has(contractId)) return { kind: 'error', text: `未知契约 ${contractId}` }
|
|
126
|
+
const r = runStore.get(contractId)
|
|
127
|
+
if (!r) return { kind: 'success', text: `契约 ${contractId} 尚无门禁决定` }
|
|
128
|
+
const approved = r.approved === true
|
|
129
|
+
const head = `契约 ${contractId}:最近决定 ${r.decision} @ ${r.at}${approved ? `;已由 ${r.by ?? 'user'} 放行 @ ${r.approvedAt}` : ';尚未放行'}`
|
|
130
|
+
// H2 可见性:RED 必须把复现命令亮给用户,YELLOW 必须把缺失证据亮出来
|
|
131
|
+
const extra = []
|
|
132
|
+
if (r.reproCmd) extra.push(`复现:${r.reproCmd}`)
|
|
133
|
+
if (Array.isArray(r.missing) && r.missing.length > 0) extra.push(`缺失证据:${r.missing.join(';')}`)
|
|
134
|
+
return { kind: 'success', text: [head, ...extra].join('\n') }
|
|
135
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-swarm-console",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Run one external agent from DSH, with a visible gate (v0.1 路 闂ㄧ鍙敤锛岀敾甯冨紑鍙戜腑).",
|
|
6
6
|
"keywords": ["deepseek-harness", "dsh-plugin", "agent", "swarm", "verification", "gate"],
|