@twinsearth/dsh-fang 3.1.0 → 3.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.
- package/client.js +1511 -1
- package/cordis.patch.yml +9 -0
- package/package.json +6 -2
- package/src/compose.mjs +4 -1
- package/src/kernel/paths.mjs +9 -1
- package/src/kernel/role.mjs +1 -0
- package/src/kernel/types.mjs +5 -1
- package/src/lib/ai-secret-store.mjs +202 -0
- package/src/lib/boot-guard.mjs +182 -0
- package/src/lib/business-producers.mjs +109 -0
- package/src/lib/compat.mjs +1 -1
- package/src/lib/composer-journal.mjs +8 -0
- package/src/lib/decision-events.mjs +129 -0
- package/src/lib/decision-ingest-client.mjs +41 -0
- package/src/lib/decision-outbox.mjs +129 -0
- package/src/lib/desktop-launch.mjs +138 -0
- package/src/lib/fork-supervision-settings.mjs +58 -0
- package/src/lib/mirror-official-source.mjs +93 -0
- package/src/lib/mirror.mjs +10 -3
- package/src/lib/producer-contract.mjs +162 -0
- package/src/lib/restart-contract.mjs +650 -0
- package/src/lib/rule-registry.mjs +69 -0
- package/src/lib/ssrf-guard.mjs +170 -0
- package/src/lib/umbrella.mjs +143 -18
- package/src/plugins/adapter.mjs +31 -2
- package/src/plugins/approver.mjs +423 -40
- package/src/plugins/decision-center.mjs +2255 -0
- package/src/plugins/forker.mjs +20 -1
- package/src/plugins/healer.mjs +53 -20
- package/src/plugins/imports.mjs +48 -0
- package/src/plugins/orchestrator.mjs +814 -42
- package/src/plugins/restart-guard.mjs +645 -52
- package/src/plugins/scorer.mjs +227 -15
- package/src/plugins/umbrella.mjs +18 -14
- package/tools/boot-guard.mjs +95 -0
- package/tools/decision-ingest.mjs +53 -0
- package/tools/desktop-launch.mjs +71 -0
- package/tools/fangctl.mjs +229 -1
package/tools/fangctl.mjs
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
// node tools/fangctl.mjs verify # 校验审计哈希链
|
|
8
8
|
// node tools/fangctl.mjs tail [n] # 最近 n 条审计
|
|
9
9
|
// node tools/fangctl.mjs doctor # 环境自检(不修改任何东西)
|
|
10
|
+
// node tools/fangctl.mjs restart-status # 只读:重启守卫状态 + 授权清单摘要(--json 可机读;--url 读运行中宿主)
|
|
11
|
+
// node tools/fangctl.mjs approvals [--json] --url <host> [--token <t>]
|
|
12
|
+
// node tools/fangctl.mjs approve <source:id> --url <host> [--token <t>] [--note text]
|
|
13
|
+
// node tools/fangctl.mjs deny <source:id> --url <host> [--token <t>] [--note text]
|
|
10
14
|
//
|
|
11
15
|
// 离线只读:fork/reap/promote 由宿主内的 orchestrator 服务与 /fang 路由完成,
|
|
12
16
|
// 这里不模拟它们,避免"CLI 说成功、宿主没执行"的假象。
|
|
@@ -17,6 +21,7 @@ import {
|
|
|
17
21
|
resolveFangLayout, resolveProfileDir, resolveDshHome, parseWebUrl,
|
|
18
22
|
exists, readJson, readJsonl, isLink, verifyChain, FANG_VERSION,
|
|
19
23
|
} from '../src/kernel/index.mjs'
|
|
24
|
+
import { readRestartStatus } from '../src/lib/restart-contract.mjs'
|
|
20
25
|
|
|
21
26
|
function parseArgs(argv) {
|
|
22
27
|
const args = { _: [] }
|
|
@@ -26,6 +31,15 @@ function parseArgs(argv) {
|
|
|
26
31
|
if (a.startsWith('--profile=')) { args.profile = a.slice(10); continue }
|
|
27
32
|
if (a === '--data-root' && argv[i + 1]) { args.dataRoot = argv[++i]; continue }
|
|
28
33
|
if (a.startsWith('--data-root=')) { args.dataRoot = a.slice(12); continue }
|
|
34
|
+
if (a === '--url' && argv[i + 1]) { args.url = argv[++i]; continue }
|
|
35
|
+
if (a.startsWith('--url=')) { args.url = a.slice(6); continue }
|
|
36
|
+
if (a === '--token' && argv[i + 1]) { args.token = argv[++i]; continue }
|
|
37
|
+
if (a.startsWith('--token=')) { args.token = a.slice(8); continue }
|
|
38
|
+
if (a === '--note' && argv[i + 1]) { args.note = argv[++i]; continue }
|
|
39
|
+
if (a.startsWith('--note=')) { args.note = a.slice(7); continue }
|
|
40
|
+
if (a === '--by' && argv[i + 1]) { args.by = argv[++i]; continue }
|
|
41
|
+
if (a.startsWith('--by=')) { args.by = a.slice(5); continue }
|
|
42
|
+
if (a === '--json') { args.json = true; continue }
|
|
29
43
|
args._.push(a)
|
|
30
44
|
}
|
|
31
45
|
return args
|
|
@@ -41,6 +55,128 @@ function fmtTime(ms) {
|
|
|
41
55
|
try { return new Date(ms).toISOString().replace('T', ' ').slice(0, 19) } catch { return String(ms) }
|
|
42
56
|
}
|
|
43
57
|
|
|
58
|
+
/** HTTP base for approval commands; accepts host, host/fang, or host/fang/api. */
|
|
59
|
+
function apiBase(args) {
|
|
60
|
+
const raw = String(args.url ?? process.env.FANG_URL ?? '').trim()
|
|
61
|
+
if (!raw) return ''
|
|
62
|
+
try {
|
|
63
|
+
const url = new URL(raw)
|
|
64
|
+
let pathname = url.pathname.replace(/\/+$/, '')
|
|
65
|
+
if (pathname.endsWith('/fang/api')) pathname = pathname.slice(0, -9)
|
|
66
|
+
else if (pathname.endsWith('/fang')) pathname = pathname.slice(0, -5)
|
|
67
|
+
else pathname = ''
|
|
68
|
+
return `${url.origin}${pathname}`
|
|
69
|
+
} catch { return '' }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function resolveToken(args, base) {
|
|
73
|
+
const direct = String(args.token ?? process.env.FANG_TOKEN ?? '').trim()
|
|
74
|
+
if (direct) return direct
|
|
75
|
+
try {
|
|
76
|
+
const res = await fetch(`${base}/fang/api/auth-token`, { headers: { accept: 'application/json' } })
|
|
77
|
+
const body = await res.json()
|
|
78
|
+
if (res.ok && body?.ok && body.token) return String(body.token)
|
|
79
|
+
} catch { /* fall through */ }
|
|
80
|
+
return ''
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function approvalRequest(args, pathname, options = {}) {
|
|
84
|
+
const base = apiBase(args)
|
|
85
|
+
if (!base) throw new Error('--url is required for HTTP commands')
|
|
86
|
+
const token = await resolveToken(args, base)
|
|
87
|
+
if (!token) throw new Error('token unavailable; pass --token or FANG_TOKEN')
|
|
88
|
+
const controller = new AbortController()
|
|
89
|
+
const timer = setTimeout(() => controller.abort(), 10000)
|
|
90
|
+
try {
|
|
91
|
+
const res = await fetch(`${base}${pathname}`, {
|
|
92
|
+
method: options.method ?? 'GET',
|
|
93
|
+
headers: {
|
|
94
|
+
accept: 'application/json',
|
|
95
|
+
'content-type': 'application/json',
|
|
96
|
+
'x-fang-auth': token,
|
|
97
|
+
},
|
|
98
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
99
|
+
signal: controller.signal,
|
|
100
|
+
})
|
|
101
|
+
const text = await res.text()
|
|
102
|
+
let body = null
|
|
103
|
+
try { body = text ? JSON.parse(text) : null } catch { body = null }
|
|
104
|
+
return { status: res.status, body, text }
|
|
105
|
+
} finally {
|
|
106
|
+
clearTimeout(timer)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function printApprovals(body, json) {
|
|
111
|
+
if (json) {
|
|
112
|
+
console.log(JSON.stringify(body, null, 2))
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
const items = Array.isArray(body?.items) ? body.items : []
|
|
116
|
+
if (!items.length) { console.log('(no pending approvals)'); return }
|
|
117
|
+
console.log(`${'SOURCE'.padEnd(7)} ${'APPROVAL'.padEnd(28)} ${'RISK'.padEnd(7)} ${'DEADLINE'.padEnd(12)} TITLE`)
|
|
118
|
+
for (const item of items) {
|
|
119
|
+
console.log([
|
|
120
|
+
String(item.source ?? '').padEnd(7),
|
|
121
|
+
String(item.approvalId ?? '').padEnd(28),
|
|
122
|
+
String(item.risk ?? '').padEnd(7),
|
|
123
|
+
String(fmtTime(Number(item.deadlineAt))).padEnd(12),
|
|
124
|
+
String(item.title ?? ''),
|
|
125
|
+
].join(' '))
|
|
126
|
+
}
|
|
127
|
+
for (const failed of Array.isArray(body?.failed) ? body.failed : []) {
|
|
128
|
+
console.log(`! unavailable: ${failed.taskId ?? 'mother'} ${failed.error ?? ''}`)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function parseApprovalTarget(raw, items) {
|
|
133
|
+
const parts = String(raw ?? '').split(':').filter(Boolean)
|
|
134
|
+
const source = parts[0]
|
|
135
|
+
if (source !== 'mother' && source !== 'fork') return null
|
|
136
|
+
if (source === 'mother') {
|
|
137
|
+
const approvalId = parts.slice(1).join(':')
|
|
138
|
+
return approvalId ? { source, taskId: null, approvalId } : null
|
|
139
|
+
}
|
|
140
|
+
if (parts.length >= 3) return { source, taskId: parts[1], approvalId: parts.slice(2).join(':') }
|
|
141
|
+
const approvalId = parts.slice(1).join(':')
|
|
142
|
+
if (!approvalId) return null
|
|
143
|
+
const matches = (items ?? []).filter((item) => item.source === 'fork' && item.approvalId === approvalId)
|
|
144
|
+
if (matches.length !== 1) return null
|
|
145
|
+
return { source: 'fork', taskId: matches[0].taskId, approvalId }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function cmdApprovals(args) {
|
|
149
|
+
const result = await approvalRequest(args, '/fang/api/approvals')
|
|
150
|
+
if (result.status !== 200 || result.body?.ok === false) {
|
|
151
|
+
console.error(JSON.stringify(result.body ?? { status: result.status, text: result.text }))
|
|
152
|
+
return 1
|
|
153
|
+
}
|
|
154
|
+
printApprovals(result.body, args.json)
|
|
155
|
+
return result.body?.degraded ? 2 : 0
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function cmdDecide(args, outcome) {
|
|
159
|
+
const raw = args._[1]
|
|
160
|
+
if (!raw) { console.error('usage: fangctl approve|deny <source:id> [--note ...]'); return 64 }
|
|
161
|
+
const listed = await approvalRequest(args, '/fang/api/approvals')
|
|
162
|
+
const target = parseApprovalTarget(raw, Array.isArray(listed.body?.items) ? listed.body.items : [])
|
|
163
|
+
if (!target) { console.error(`cannot resolve approval target: ${raw}`); return 64 }
|
|
164
|
+
const result = await approvalRequest(args, '/fang/api/approvals/decide', {
|
|
165
|
+
method: 'POST',
|
|
166
|
+
body: {
|
|
167
|
+
...target,
|
|
168
|
+
outcome,
|
|
169
|
+
note: args.note ?? null,
|
|
170
|
+
by: args.by ?? 'fangctl',
|
|
171
|
+
},
|
|
172
|
+
})
|
|
173
|
+
if (args.json) console.log(JSON.stringify(result.body ?? { status: result.status }, null, 2))
|
|
174
|
+
else if (result.status === 200 && result.body?.ok !== false) console.log(`${outcome}: ${target.source}:${target.approvalId}`)
|
|
175
|
+
else console.error(`${result.status}: ${result.body?.error ?? result.text ?? 'failed'}`)
|
|
176
|
+
if (result.status === 200 && result.body?.ok !== false) return 0
|
|
177
|
+
return result.status === 409 ? 2 : 1
|
|
178
|
+
}
|
|
179
|
+
|
|
44
180
|
function readForks(layout) {
|
|
45
181
|
const raw = readJson(layout.forksFile, [])
|
|
46
182
|
const list = Array.isArray(raw) ? raw : Array.isArray(raw?.forks) ? raw.forks : []
|
|
@@ -97,6 +233,85 @@ function cmdForks(layout) {
|
|
|
97
233
|
return 0
|
|
98
234
|
}
|
|
99
235
|
|
|
236
|
+
/**
|
|
237
|
+
* 只读:重启守卫状态 + 授权清单摘要。
|
|
238
|
+
* 有 --url:走 GET /fang/api/restart(验证运行中的宿主是否已载入新 bundle)
|
|
239
|
+
* 无 --url:offline 读 <dataDir>/restart(与 HTTP/UI 同一投影)
|
|
240
|
+
* 两种模式都不触发任何执行。
|
|
241
|
+
*/
|
|
242
|
+
async function cmdRestartStatus(layout, args) {
|
|
243
|
+
if (args.url) {
|
|
244
|
+
const result = await approvalRequest(args, '/fang/api/restart')
|
|
245
|
+
if (args.json) {
|
|
246
|
+
console.log(JSON.stringify({ status: result.status, ...(result.body ?? {}) }, null, 2))
|
|
247
|
+
return result.status === 200 ? 0 : 1
|
|
248
|
+
}
|
|
249
|
+
if (result.status !== 200) {
|
|
250
|
+
console.error(`HTTP ${result.status}: ${result.body?.error ?? result.text ?? 'restart route unavailable'}`)
|
|
251
|
+
console.error('提示:旧 bundle 没有 /fang/api/restart;需重启 DSH Desktop 载入新代码。')
|
|
252
|
+
return 1
|
|
253
|
+
}
|
|
254
|
+
return printRemoteRestartStatus(result.body)
|
|
255
|
+
}
|
|
256
|
+
const limitArg = Number(args._[1])
|
|
257
|
+
const status = readRestartStatus(layout.dataDir, {
|
|
258
|
+
limit: Number.isFinite(limitArg) && limitArg > 0 ? limitArg : 50,
|
|
259
|
+
})
|
|
260
|
+
if (args.json) {
|
|
261
|
+
console.log(JSON.stringify(status, null, 2))
|
|
262
|
+
return 0
|
|
263
|
+
}
|
|
264
|
+
printLocalRestartStatus(status)
|
|
265
|
+
return 0
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function printRemoteRestartStatus(body) {
|
|
269
|
+
if (!body || body.ok === false || body.available === false) {
|
|
270
|
+
console.error(`restart-guard unavailable: ${body?.error ?? 'service missing'}`)
|
|
271
|
+
return 1
|
|
272
|
+
}
|
|
273
|
+
const s = body.state ?? {}
|
|
274
|
+
const p = body.policy ?? {}
|
|
275
|
+
const counts = body.approvals?.counts ?? {}
|
|
276
|
+
console.log('restart guard (remote, read-only)')
|
|
277
|
+
const pending = s.pending?.id ? ` (pending ${s.pending.id} risk=${s.pending.risk ?? '?'} phase=${s.pending.phase ?? '?'})` : ''
|
|
278
|
+
console.log(` state : ${s.state ?? 'unknown'}${pending}`)
|
|
279
|
+
console.log(` mode : ${p.mode ?? '—'} authMode=${p.authMode ?? '—'} strictRedLine=${p.strictRedLine === false ? 'false(红灯可自动放行)' : 'true'}`)
|
|
280
|
+
const armedNow = p.mode === 'enforce' && p.allowProcessRestart === true
|
|
281
|
+
console.log(` armed : ${armedNow ? 'yes' : 'no'} (enforce + allowProcessRestart=true)`)
|
|
282
|
+
console.log(` approvals : total=${counts.total ?? 0} authorized=${counts.authorized ?? 0} executed=${counts.executed ?? 0} stale=${counts.stale ?? 0} notArmed=${counts.notArmed ?? 0} invalid=${counts.invalid ?? 0}`)
|
|
283
|
+
console.log(` contract : decision-only; no execution; executed-proof = receipt execution.executed===true`)
|
|
284
|
+
return 0
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function printLocalRestartStatus(status) {
|
|
288
|
+
const counts = status.approvals.counts
|
|
289
|
+
console.log(`restart guard @ ${status.stateDir}${exists(status.stateDir) ? '' : ' (不存在:守卫尚未启动或未产生状态)'}`)
|
|
290
|
+
const pending = status.pending?.id ? ` (pending ${status.pending.id} risk=${status.pending.risk ?? '?'} phase=${status.pending.phase ?? '?'})` : ''
|
|
291
|
+
console.log(` state : ${status.state}${pending}`)
|
|
292
|
+
console.log(` state file : ${exists(path.join(status.stateDir, 'state.json')) ? 'present' : 'missing'}${status.stateAt ? ` (at ${fmtTime(status.stateAt)})` : ''}`)
|
|
293
|
+
if (status.lastInterrupted) {
|
|
294
|
+
console.log(` interrupted: ${status.lastInterrupted.id ?? '?'} previousState=${status.lastInterrupted.previousState ?? '?'} at=${fmtTime(status.lastInterrupted.at)}`)
|
|
295
|
+
}
|
|
296
|
+
console.log(` approvals : total=${counts.total} authorized=${counts.authorized} executed=${counts.executed} stale=${counts.stale} notArmed=${counts.notArmed} invalid=${counts.invalid}`)
|
|
297
|
+
if (status.approvals.items.length) {
|
|
298
|
+
console.log(`${'ID'.padEnd(22)} ${'RISK'.padEnd(7)} ${'OUTCOME'.padEnd(11)} ${'STATE'.padEnd(14)} ${'APPROVAL'.padEnd(20)} EXPIRES`)
|
|
299
|
+
for (const item of status.approvals.items) {
|
|
300
|
+
console.log([
|
|
301
|
+
String(item.id ?? '').padEnd(22),
|
|
302
|
+
String(item.risk ?? '').padEnd(7),
|
|
303
|
+
String(item.outcome ?? '').padEnd(11),
|
|
304
|
+
String(item.state ?? '').padEnd(14),
|
|
305
|
+
String(item.approvalId ?? '—').padEnd(20),
|
|
306
|
+
item.expiresAt ? fmtTime(item.expiresAt) : '—',
|
|
307
|
+
].join(' '))
|
|
308
|
+
}
|
|
309
|
+
} else {
|
|
310
|
+
console.log(' (no approval manifests)')
|
|
311
|
+
}
|
|
312
|
+
console.log(' contract : decision-only; no execution; executed-proof = receipt execution.executed===true')
|
|
313
|
+
}
|
|
314
|
+
|
|
100
315
|
function cmdDoctor(layout) {
|
|
101
316
|
const checks = []
|
|
102
317
|
const check = (id, ok, detail) => checks.push({ id, ok, detail })
|
|
@@ -145,9 +360,22 @@ let code = 0
|
|
|
145
360
|
switch (cmd) {
|
|
146
361
|
case 'status': code = cmdStatus(layout); break
|
|
147
362
|
case 'forks': code = cmdForks(layout); break
|
|
363
|
+
case 'restart-status': {
|
|
364
|
+
try { code = await cmdRestartStatus(layout, args) } catch (error) { console.error(String(error?.message ?? error)); code = 1 }
|
|
365
|
+
break
|
|
366
|
+
}
|
|
148
367
|
case 'verify': code = cmdVerify(layout); break
|
|
149
368
|
case 'tail': code = cmdTail(layout, Number(args._[1] ?? 20)); break
|
|
150
369
|
case 'doctor': code = cmdDoctor(layout); break
|
|
370
|
+
case 'approvals': {
|
|
371
|
+
try { code = await cmdApprovals(args) } catch (error) { console.error(String(error?.message ?? error)); code = 1 }
|
|
372
|
+
break
|
|
373
|
+
}
|
|
374
|
+
case 'approve':
|
|
375
|
+
case 'deny': {
|
|
376
|
+
try { code = await cmdDecide(args, cmd === 'approve' ? 'approved' : 'denied') } catch (error) { console.error(String(error?.message ?? error)); code = 1 }
|
|
377
|
+
break
|
|
378
|
+
}
|
|
151
379
|
case 'parse-url': {
|
|
152
380
|
const parsed = parseWebUrl(args._.slice(1).join(' '))
|
|
153
381
|
console.log(JSON.stringify(parsed, null, 2))
|
|
@@ -155,7 +383,7 @@ switch (cmd) {
|
|
|
155
383
|
break
|
|
156
384
|
}
|
|
157
385
|
default:
|
|
158
|
-
console.log('usage: fangctl.mjs <status|forks|verify|tail [n]|doctor|parse-url <line>> [--profile name] [--data-root dir]')
|
|
386
|
+
console.log('usage: fangctl.mjs <status|forks|verify|tail [n]|doctor|restart-status [limit]|parse-url <line>|approvals|approve <source:id>|deny <source:id>> [--profile name] [--data-root dir] [--url url] [--token token] [--note text] [--by who] [--json]')
|
|
159
387
|
code = 64
|
|
160
388
|
}
|
|
161
389
|
process.exitCode = code
|