@twinsearth/dsh-fang 3.1.21 → 3.1.22
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/LICENSES/LICENSES/cordis-MIT.txt +39 -0
- package/client.js +197 -3
- package/defaults/defaults/umbrella-rules.yaml +113 -0
- package/package.json +2 -2
- package/sdk/sdk/README.md +65 -0
- package/sdk/sdk/examples/status-badge/README.md +16 -0
- package/sdk/sdk/examples/status-badge/client.js +60 -0
- package/sdk/sdk/examples/status-badge/cordis.patch.yml +7 -0
- package/sdk/sdk/examples/status-badge/index.js +12 -0
- package/sdk/sdk/examples/status-badge/package.json +24 -0
- package/sdk/sdk/templates/plugin/README.md +36 -0
- package/sdk/sdk/templates/plugin/client.js +35 -0
- package/sdk/sdk/templates/plugin/cordis.patch.yml +8 -0
- package/sdk/sdk/templates/plugin/icon.svg +5 -0
- package/sdk/sdk/templates/plugin/index.js +23 -0
- package/sdk/sdk/templates/plugin/locale/en.json +6 -0
- package/sdk/sdk/templates/plugin/package.json +36 -0
- package/src/kernel/types.mjs +1 -1
- package/src/src/compose.mjs +67 -0
- package/src/src/contract/client.js +86 -0
- package/src/src/contract/constants.js +79 -0
- package/src/src/contract/envelope.js +83 -0
- package/src/src/contract/index.js +19 -0
- package/src/src/contract/negotiate.js +106 -0
- package/src/src/contract/server.js +77 -0
- package/src/src/contract/transports.js +301 -0
- package/src/src/index.mjs +11 -0
- package/src/src/kernel/core.mjs +100 -0
- package/src/src/kernel/extensions.mjs +61 -0
- package/src/src/kernel/fsx.mjs +349 -0
- package/src/src/kernel/index.mjs +21 -0
- package/src/src/kernel/ledger.mjs +94 -0
- package/src/src/kernel/paths.mjs +182 -0
- package/src/src/kernel/role.mjs +68 -0
- package/src/src/kernel/rounds.mjs +172 -0
- package/src/src/kernel/schema.mjs +5 -0
- package/src/src/kernel/types.mjs +264 -0
- package/src/src/kernel/vendor/cosmokit.mjs +421 -0
- package/src/src/kernel/vendor/schemastery.mjs +638 -0
- package/src/src/lib/adapter-client.mjs +383 -0
- package/src/src/lib/adapter-protocol.mjs +243 -0
- package/src/src/lib/ai-secret-store.mjs +202 -0
- package/src/src/lib/boot-guard.mjs +182 -0
- package/src/src/lib/branch-store.mjs +84 -0
- package/src/src/lib/branch.mjs +74 -0
- package/src/src/lib/bridge-client.mjs +133 -0
- package/src/src/lib/broker-adapter.mjs +67 -0
- package/src/src/lib/broker.mjs +45 -0
- package/src/src/lib/business-producers.mjs +109 -0
- package/src/src/lib/command-cache.mjs +209 -0
- package/src/src/lib/compat.mjs +170 -0
- package/src/src/lib/composer-journal.mjs +128 -0
- package/src/src/lib/container/index.mjs +806 -0
- package/src/src/lib/decision-events.mjs +129 -0
- package/src/src/lib/decision-ingest-client.mjs +41 -0
- package/src/src/lib/decision-outbox.mjs +129 -0
- package/src/src/lib/default-essence.mjs +61 -0
- package/src/src/lib/desktop-launch.mjs +138 -0
- package/src/src/lib/essence.mjs +1211 -0
- package/src/src/lib/fork-client.mjs +410 -0
- package/src/src/lib/fork-config-lock.mjs +153 -0
- package/src/src/lib/fork-seed.mjs +304 -0
- package/src/src/lib/fork-supervision-settings.mjs +58 -0
- package/src/src/lib/fork-supervisor.mjs +122 -0
- package/src/src/lib/fs-guard.mjs +28 -0
- package/src/src/lib/http-auth.mjs +162 -0
- package/src/src/lib/idle-audit.mjs +178 -0
- package/src/src/lib/interaction-poke.mjs +61 -0
- package/src/src/lib/mirror-etag.mjs +92 -0
- package/src/src/lib/mirror-official-source.mjs +93 -0
- package/src/src/lib/mirror.mjs +478 -0
- package/src/src/lib/mother-link.mjs +156 -0
- package/src/src/lib/mutex.mjs +31 -0
- package/src/src/lib/permissions.mjs +115 -0
- package/src/src/lib/pnpm-store.mjs +189 -0
- package/src/src/lib/preferences.mjs +212 -0
- package/src/src/lib/producer-contract.mjs +162 -0
- package/src/src/lib/reflow.mjs +41 -0
- package/src/src/lib/remote.mjs +408 -0
- package/src/src/lib/restart-contract.mjs +650 -0
- package/src/src/lib/roots.mjs +30 -0
- package/src/src/lib/rule-registry.mjs +1101 -0
- package/src/src/lib/runtime-capture.mjs +307 -0
- package/src/src/lib/semaphore.mjs +34 -0
- package/src/src/lib/session-bundle.mjs +138 -0
- package/src/src/lib/session-format.mjs +658 -0
- package/src/src/lib/sse-parser.mjs +102 -0
- package/src/src/lib/ssrf-guard.mjs +170 -0
- package/src/src/lib/status-store.mjs +257 -0
- package/src/src/lib/task-handle.mjs +622 -0
- package/src/src/lib/ui-actions.mjs +55 -0
- package/src/src/lib/umbrella.mjs +1661 -0
- package/src/src/lib/upgrade-coordinator.mjs +107 -0
- package/src/src/lib/upgrade-status.mjs +43 -0
- package/src/src/plugins/adapter.mjs +1468 -0
- package/src/src/plugins/approver.mjs +1210 -0
- package/src/src/plugins/auditor.mjs +330 -0
- package/src/src/plugins/buffer-keeper.mjs +522 -0
- package/src/src/plugins/decision-center.mjs +2265 -0
- package/src/src/plugins/forker.mjs +1594 -0
- package/src/src/plugins/healer.mjs +664 -0
- package/src/src/plugins/hot-reloader.mjs +274 -0
- package/src/src/plugins/imports.mjs +247 -0
- package/src/src/plugins/orchestrator.mjs +3806 -0
- package/src/src/plugins/restart-guard.mjs +948 -0
- package/src/src/plugins/router.mjs +714 -0
- package/src/src/plugins/scorer.mjs +624 -0
- package/src/src/plugins/session-bridge.mjs +166 -0
- package/src/src/plugins/session-catalog.mjs +771 -0
- package/src/src/plugins/session-router.mjs +125 -0
- package/src/src/plugins/session-sync.mjs +171 -0
- package/src/src/plugins/umbrella.mjs +419 -0
- package/tools/recover-queue.mjs +133 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// v3.0.9 read-only broker adapter: issues only read capabilities. Write is intentionally absent.
|
|
3
|
+
const READ_ROOTS = new Set(['essence', 'sessions'])
|
|
4
|
+
|
|
5
|
+
export function issueReadCapability(broker, identity, root) {
|
|
6
|
+
if (!READ_ROOTS.has(root)) throw new Error('read-root-not-allowed')
|
|
7
|
+
const role = identity?.role
|
|
8
|
+
if (!role) throw new Error('identity-missing')
|
|
9
|
+
return broker.issue({
|
|
10
|
+
role,
|
|
11
|
+
root,
|
|
12
|
+
op: 'read',
|
|
13
|
+
essenceId: String(identity.essenceId || ''),
|
|
14
|
+
forkId: String(identity.forkId || ''),
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function checkReadCapability(broker, token, scope) {
|
|
19
|
+
if (!READ_ROOTS.has(scope?.root)) return { ok: false, error: 'access-denied', reason: 'read-root-not-allowed' }
|
|
20
|
+
return broker.resolve({
|
|
21
|
+
token,
|
|
22
|
+
root: scope.root,
|
|
23
|
+
op: 'read',
|
|
24
|
+
essenceId: scope.essenceId || '',
|
|
25
|
+
ownerEssenceId: scope.ownerEssenceId || '',
|
|
26
|
+
forkId: scope.forkId || '',
|
|
27
|
+
ownerForkId: scope.ownerForkId || '',
|
|
28
|
+
path: scope.path || '',
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function headerValue(headers, name) {
|
|
33
|
+
const key = Object.keys(headers || {}).find((k) => k.toLowerCase() === name)
|
|
34
|
+
return key ? headers[key] : ''
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveIdentity({ headers = {}, url = '/' }, resolveToken) {
|
|
38
|
+
const auth = String(headerValue(headers, 'x-fang-auth') || '')
|
|
39
|
+
const bearer = /^Bearer\s+(.+)$/i.exec(String(headerValue(headers, 'authorization') || ''))?.[1] || ''
|
|
40
|
+
let query = ''
|
|
41
|
+
try { query = new URL(url, 'http://127.0.0.1').searchParams.get('token') || '' } catch { /* ignore */ }
|
|
42
|
+
const token = auth || bearer || query
|
|
43
|
+
if (!token) return { ok: false, status: 401, error: 'unauthorized' }
|
|
44
|
+
if (typeof resolveToken !== 'function') return { ok: false, status: 401, error: 'unauthorized' }
|
|
45
|
+
const identity = resolveToken(token)
|
|
46
|
+
if (!identity) return { ok: false, status: 401, error: 'unauthorized' }
|
|
47
|
+
if (identity.denied) return { ok: false, status: 403, error: 'access-denied', reason: String(identity.reason || 'denied') }
|
|
48
|
+
if (!identity.role) return { ok: false, status: 401, error: 'unauthorized' }
|
|
49
|
+
return {
|
|
50
|
+
ok: true,
|
|
51
|
+
token,
|
|
52
|
+
role: String(identity.role),
|
|
53
|
+
essenceId: String(identity.essenceId || ''),
|
|
54
|
+
forkId: String(identity.forkId || ''),
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function denialToHttp(result) {
|
|
59
|
+
if (result?.ok) return null
|
|
60
|
+
const status = result?.status === 403 ? 403 : 401
|
|
61
|
+
return {
|
|
62
|
+
status,
|
|
63
|
+
body: status === 403
|
|
64
|
+
? { ok: false, error: 'access-denied', reason: String(result?.reason || 'denied') }
|
|
65
|
+
: { ok: false, error: 'unauthorized' },
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
|
|
3
|
+
import crypto from 'node:crypto'
|
|
4
|
+
import { canAccess } from './roots.mjs'
|
|
5
|
+
|
|
6
|
+
export class Broker {
|
|
7
|
+
constructor(secret) {
|
|
8
|
+
this.secret = secret || crypto.randomBytes(32)
|
|
9
|
+
this.audit = []
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
issue({ role, root, op, essenceId = '', forkId = '', ttlMs = 60_000 }) {
|
|
13
|
+
const payload = Buffer.from(JSON.stringify({ role, root, op, essenceId, forkId, exp: Date.now() + ttlMs })).toString('base64url')
|
|
14
|
+
const sig = crypto.createHmac('sha256', this.secret).update(payload).digest('base64url')
|
|
15
|
+
return `${payload}.${sig}`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
verify(token) {
|
|
19
|
+
const [payload, sig] = String(token || '').split('.')
|
|
20
|
+
if (!payload || !sig) return null
|
|
21
|
+
const expected = crypto.createHmac('sha256', this.secret).update(payload).digest('base64url')
|
|
22
|
+
if (sig.length !== expected.length || !crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected))) return null
|
|
23
|
+
try {
|
|
24
|
+
const caps = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8'))
|
|
25
|
+
if (!caps.exp || caps.exp < Date.now()) return null
|
|
26
|
+
return caps
|
|
27
|
+
} catch { return null }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
resolve({ token, root, op, essenceId = '', ownerEssenceId = '', forkId = '', ownerForkId = '', path = '' }) {
|
|
31
|
+
const caps = this.verify(token)
|
|
32
|
+
const deny = (reason) => {
|
|
33
|
+
this.audit.push({ ok: false, reason, root, op, essenceId, forkId, at: Date.now() })
|
|
34
|
+
return { ok: false, error: 'access-denied', reason }
|
|
35
|
+
}
|
|
36
|
+
if (!caps) return deny('bad-capability')
|
|
37
|
+
if (caps.root !== root || caps.op !== op) return deny('capability-mismatch')
|
|
38
|
+
if (caps.role !== 'coordinator' && caps.essenceId && ownerEssenceId && caps.essenceId !== ownerEssenceId) return deny('wrong-essence')
|
|
39
|
+
if (caps.role === 'fork' && caps.forkId && ownerForkId && caps.forkId !== ownerForkId) return deny('wrong-fork')
|
|
40
|
+
const allowed = canAccess({ role: caps.role, root, op, essenceId: caps.essenceId || essenceId, ownerEssenceId, forkId: caps.forkId || forkId, ownerForkId, path })
|
|
41
|
+
if (!allowed) return deny('matrix')
|
|
42
|
+
this.audit.push({ ok: true, root, op, essenceId: caps.essenceId, forkId: caps.forkId, at: Date.now() })
|
|
43
|
+
return { ok: true, role: caps.role }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
//
|
|
4
|
+
// business-producers — 业务生产者 → 决策中心的命名适配层(P4 业务接入契约)。
|
|
5
|
+
//
|
|
6
|
+
// 这不是“外部系统已接”:HarnessMM/PluginUpdater/SuperAgent/PJM/Worker 的本体不在
|
|
7
|
+
// 本仓。本模块给它们一个**明确、稳定、可测试**的调用面;只要在各自进程里调用对应
|
|
8
|
+
// 方法(或等价 ingest payload),就会生成正确等级/kind/dedupeKey 的 ticket。
|
|
9
|
+
import { nowMs } from '../kernel/index.mjs'
|
|
10
|
+
|
|
11
|
+
/** 把外部系统的动作映射成 decision-center ingest/submit 的稳定 payload。 */
|
|
12
|
+
export function createBusinessProducers({ submit, now = nowMs } = {}) {
|
|
13
|
+
if (typeof submit !== 'function') throw new TypeError('createBusinessProducers: submit required')
|
|
14
|
+
const send = (payload) => submit({ ...payload, at: now() })
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
/** MirrorUpgrade 成功但需要重启宿主:红色,动作 confirm_restart。 */
|
|
18
|
+
mirrorUpgradeRestartRequired({ from = 'unknown', to = 'unknown', reason = '' } = {}) {
|
|
19
|
+
return send({
|
|
20
|
+
producer: 'MirrorUpgrade', kind: 'mirror-upgrade', level: 'red',
|
|
21
|
+
title: '镜像升级完成,需重启宿主',
|
|
22
|
+
context: `镜像已从 ${from} 切换到 ${to}${reason ? `(${reason})` : ''};重启后新镜像才会完全生效。`,
|
|
23
|
+
actions: [
|
|
24
|
+
{ id: 'confirm_restart', label: '立即重启', type: 'primary' },
|
|
25
|
+
{ id: 'later', label: '稍后', type: 'default' },
|
|
26
|
+
],
|
|
27
|
+
dedupeKey: `mirror-upgrade:restart:${from}->${to}`,
|
|
28
|
+
})
|
|
29
|
+
},
|
|
30
|
+
/** MirrorUpgrade 失败/exception。 */
|
|
31
|
+
mirrorUpgradeFailed({ from = 'unknown', to = null, reason = 'unknown', level = 'yellow' } = {}) {
|
|
32
|
+
return send({
|
|
33
|
+
producer: 'MirrorUpgrade', kind: 'mirror-upgrade', level,
|
|
34
|
+
title: '母体镜像升级失败',
|
|
35
|
+
context: `镜像升级未成功:${reason}${from ? `(from ${from}${to ? ` -> ${to}` : ''})` : ''}`,
|
|
36
|
+
actions: [{ id: 'retry', label: '重试升级', type: 'primary' }, { id: 'ignore', label: '忽略', type: 'default' }],
|
|
37
|
+
dedupeKey: `mirror-upgrade:failed:${reason}`.slice(0, 200),
|
|
38
|
+
})
|
|
39
|
+
},
|
|
40
|
+
/** EssenceManager 合并冲突:黄色,合并同一 essence 的冲突。 */
|
|
41
|
+
essenceConflict({ essenceId = 'unknown', conflicts = [], severity = 'warning' } = {}) {
|
|
42
|
+
return send({
|
|
43
|
+
producer: 'EssenceManager', kind: 'essence-conflict', level: severity === 'fatal' ? 'red' : 'yellow',
|
|
44
|
+
title: `元主合并冲突:${essenceId}`,
|
|
45
|
+
context: `essence=${essenceId}; conflicts=${Array.isArray(conflicts) ? conflicts.length : 0}`,
|
|
46
|
+
actions: [{ id: 'review', label: '查看冲突', type: 'primary' }, { id: 'ignore', label: '忽略', type: 'default' }],
|
|
47
|
+
dedupeKey: `essence-conflict:${essenceId}`,
|
|
48
|
+
})
|
|
49
|
+
},
|
|
50
|
+
/** HarnessMM 优化建议:蓝色(异步建议,不阻塞)。 */
|
|
51
|
+
harnessmmSuggestion({ id = null, summary = '', score = null } = {}) {
|
|
52
|
+
return send({
|
|
53
|
+
producer: 'HarnessMM', kind: 'harnessmm-suggestion', level: 'blue',
|
|
54
|
+
title: summary || 'HarnessMM 优化建议',
|
|
55
|
+
context: `suggestion=${id ?? 'n/a'}; score=${score ?? 'n/a'}`,
|
|
56
|
+
actions: [{ id: 'accept', label: '采纳', type: 'default' }, { id: 'ignore', label: '忽略', type: 'default' }],
|
|
57
|
+
dedupeKey: id ? `harnessmm:${id}` : undefined,
|
|
58
|
+
})
|
|
59
|
+
},
|
|
60
|
+
/** PluginUpdater 备份完成通知:绿色(FYI)。 */
|
|
61
|
+
pluginUpdaterBackup({ pluginId = 'unknown', backupId = null } = {}) {
|
|
62
|
+
return send({
|
|
63
|
+
producer: 'PluginUpdater', kind: 'plugin-updater-backup', level: 'green',
|
|
64
|
+
title: `插件更新前备份完成:${pluginId}`,
|
|
65
|
+
context: `plugin=${pluginId}; backup=${backupId ?? 'n/a'}`,
|
|
66
|
+
dedupeKey: backupId ? `plugin-updater:backup:${backupId}` : undefined,
|
|
67
|
+
})
|
|
68
|
+
},
|
|
69
|
+
/** SuperAgent 任务完成汇报:绿色。 */
|
|
70
|
+
superagentCompletion({ taskId = null, summary = '' } = {}) {
|
|
71
|
+
return send({
|
|
72
|
+
producer: 'SuperAgent', kind: 'superagent-completion', level: 'green',
|
|
73
|
+
title: summary || 'SuperAgent 任务完成',
|
|
74
|
+
context: `task=${taskId ?? 'n/a'}`,
|
|
75
|
+
dedupeKey: taskId ? `superagent:completed:${taskId}` : undefined,
|
|
76
|
+
})
|
|
77
|
+
},
|
|
78
|
+
/** PJM 超时:黄色 + 默认 5 分钟自动转灰。 */
|
|
79
|
+
pjmTimeout({ projectId = null, phase = 'unknown', timeoutMinutes = 5 } = {}) {
|
|
80
|
+
return send({
|
|
81
|
+
producer: 'PJM', kind: 'pjm-timeout', level: 'yellow', timeoutMinutes,
|
|
82
|
+
title: `PJM 超时:${projectId ?? 'unknown'}`,
|
|
83
|
+
context: `project=${projectId ?? 'n/a'}; phase=${phase}`,
|
|
84
|
+
actions: [{ id: 'retry', label: '重试', type: 'primary' }, { id: 'abandon', label: '放弃', type: 'default' }],
|
|
85
|
+
dedupeKey: projectId ? `pjm:timeout:${projectId}:${phase}` : undefined,
|
|
86
|
+
})
|
|
87
|
+
},
|
|
88
|
+
/** PJM 致命错误:黄色(需要人看,但不阻塞整个 Agent)。 */
|
|
89
|
+
pjmFatal({ projectId = null, error = 'unknown' } = {}) {
|
|
90
|
+
return send({
|
|
91
|
+
producer: 'PJM', kind: 'pjm-fatal', level: 'yellow',
|
|
92
|
+
title: `PJM 致命错误:${projectId ?? 'unknown'}`,
|
|
93
|
+
context: `project=${projectId ?? 'n/a'}; error=${String(error).slice(0, 500)}`,
|
|
94
|
+
actions: [{ id: 'retry', label: '重试', type: 'primary' }, { id: 'abandon', label: '放弃', type: 'default' }],
|
|
95
|
+
dedupeKey: projectId ? `pjm:fatal:${projectId}` : undefined,
|
|
96
|
+
})
|
|
97
|
+
},
|
|
98
|
+
/** Worker 跑偏:黄色(可在 payload 带偏离详情)。 */
|
|
99
|
+
workerDeviation({ workerId = null, taskId = null, detail = '' } = {}) {
|
|
100
|
+
return send({
|
|
101
|
+
producer: 'Worker', kind: 'worker-deviation', level: 'yellow',
|
|
102
|
+
title: `Worker 跑偏:${workerId ?? 'unknown'}`,
|
|
103
|
+
context: `worker=${workerId ?? 'n/a'}; task=${taskId ?? 'n/a'}; ${String(detail).slice(0, 500)}`,
|
|
104
|
+
actions: [{ id: 'review', label: '查看', type: 'primary' }, { id: 'ignore', label: '忽略', type: 'default' }],
|
|
105
|
+
dedupeKey: taskId ? `worker:deviation:${taskId}` : undefined,
|
|
106
|
+
})
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
|
|
3
|
+
// dsh-Fang v2.6.3 — 幂等命令缓存(in-flight 合并 + completed LRU 淘汰)。
|
|
4
|
+
//
|
|
5
|
+
// 契约:docs/COMMAND-CACHE.md、docs/PROTOCOL-V3.md §6.2。
|
|
6
|
+
// 设计要点:
|
|
7
|
+
// * 两态:running(value 是同一个 promise)/ completed(value 是结果);
|
|
8
|
+
// * 同一 commandId 并发提交 → 第二个调用等待同一个 promise,不重复执行副作用;
|
|
9
|
+
// * LRU:Map 插入顺序即使用顺序;run() 命中会 touch,纯 get() 不 touch;
|
|
10
|
+
// * 淘汰:只淘汰 completed,跳过 running;全是 running 时允许临时超限,
|
|
11
|
+
// 等 in-flight 完成后自动收敛;
|
|
12
|
+
// * 失败契约:fn 返回的业务失败(CommandResult.status='rejected')**缓存**;
|
|
13
|
+
// fn 抛错(传输层/编程错误,命令可能没执行)**不缓存并移除**,允许重试重新执行;
|
|
14
|
+
// * 缓存不实现超时:超时属于命令层(AbortController),缓存层只在得到最终结果后决策。
|
|
15
|
+
export const COMMAND_CACHE_DEFAULT_CAPACITY = 500
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {{state:'running', promise:Promise<any>|null, startedAt:number}} RunningEntry
|
|
19
|
+
* @typedef {{state:'completed', result:any, completedAt:number}} CompletedEntry
|
|
20
|
+
* @typedef {RunningEntry|CompletedEntry} CacheEntry
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {object} CommandCacheOptions
|
|
25
|
+
* @property {number} [capacity=500] 容量上限(>=1);超限时只淘汰 completed 项
|
|
26
|
+
* @property {(commandId:string, entry:CompletedEntry)=>void} [onEvict] 淘汰回调(异常被隔离)
|
|
27
|
+
* @property {()=>number} [now=Date.now] 时钟注入(测试用)
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
export class CommandCache {
|
|
31
|
+
/** @param {CommandCacheOptions} [options] */
|
|
32
|
+
constructor({ capacity = COMMAND_CACHE_DEFAULT_CAPACITY, onEvict = null, now = Date.now } = {}) {
|
|
33
|
+
const cap = Math.floor(Number(capacity))
|
|
34
|
+
if (!Number.isFinite(cap) || cap < 1) {
|
|
35
|
+
throw new RangeError('command cache capacity must be an integer >= 1')
|
|
36
|
+
}
|
|
37
|
+
this.capacity = cap
|
|
38
|
+
this.onEvict = typeof onEvict === 'function' ? onEvict : null
|
|
39
|
+
this.now = typeof now === 'function' ? now : Date.now
|
|
40
|
+
/** @type {Map<string, CacheEntry>} */
|
|
41
|
+
this.entries = new Map()
|
|
42
|
+
this.counters = { executions: 0, hits: 0, dedupHits: 0, evictions: 0, droppedOnError: 0 }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 幂等执行:同一 commandId 至多执行一次。
|
|
47
|
+
* - completed 命中 → 返回缓存结果(并 touch);
|
|
48
|
+
* - running 命中 → 返回同一个 promise(并 touch,不重复执行 fn);
|
|
49
|
+
* - 未命中 → 执行 fn,完成后写 completed;fn 抛错则移除并抛出。
|
|
50
|
+
*
|
|
51
|
+
* 说明:fn 必须把「业务失败」转成结果对象返回;只有「命令没有执行到」的
|
|
52
|
+
* 传输层/编程错误才允许抛错(这样重试才会重新执行)。
|
|
53
|
+
*
|
|
54
|
+
* @param {string} commandId
|
|
55
|
+
* @param {() => Promise<any>} fn
|
|
56
|
+
* @returns {Promise<any>}
|
|
57
|
+
*/
|
|
58
|
+
run(commandId, fn) {
|
|
59
|
+
const id = normalizeCommandId(commandId)
|
|
60
|
+
if (typeof fn !== 'function') throw new TypeError('command cache run() requires a function')
|
|
61
|
+
|
|
62
|
+
const existing = this.entries.get(id)
|
|
63
|
+
if (existing) {
|
|
64
|
+
this.counters.hits += 1
|
|
65
|
+
this.touch(id)
|
|
66
|
+
if (existing.state === 'completed') return Promise.resolve(existing.result)
|
|
67
|
+
this.counters.dedupHits += 1
|
|
68
|
+
return existing.promise
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @type {RunningEntry} */
|
|
72
|
+
const entry = { state: 'running', promise: null, startedAt: this.now() }
|
|
73
|
+
const promise = Promise.resolve()
|
|
74
|
+
.then(() => {
|
|
75
|
+
this.counters.executions += 1
|
|
76
|
+
return fn()
|
|
77
|
+
})
|
|
78
|
+
.then(
|
|
79
|
+
(result) => {
|
|
80
|
+
// 只有「本次执行」仍然占着这个 commandId 时才落缓存(防御未来 running 失效语义变化)
|
|
81
|
+
const current = this.entries.get(id)
|
|
82
|
+
if (current === entry) {
|
|
83
|
+
this.entries.set(id, { state: 'completed', result, completedAt: this.now() })
|
|
84
|
+
this.evictIfNeeded()
|
|
85
|
+
}
|
|
86
|
+
return result
|
|
87
|
+
},
|
|
88
|
+
(error) => {
|
|
89
|
+
const current = this.entries.get(id)
|
|
90
|
+
if (current === entry) {
|
|
91
|
+
this.entries.delete(id)
|
|
92
|
+
this.counters.droppedOnError += 1
|
|
93
|
+
}
|
|
94
|
+
throw error
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
entry.promise = promise
|
|
99
|
+
this.entries.set(id, entry)
|
|
100
|
+
this.evictIfNeeded()
|
|
101
|
+
// 给 promise 挂一个空 handler:调用方忽略返回值时不会出现 unhandled rejection;
|
|
102
|
+
// 错误仍会通过上面返回的 promise 交给调用方。
|
|
103
|
+
promise.catch(() => {})
|
|
104
|
+
return promise
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 纯查询:只返回 completed 的结果,不改变 LRU 顺序。 */
|
|
108
|
+
get(commandId) {
|
|
109
|
+
const entry = this.entries.get(String(commandId ?? ''))
|
|
110
|
+
return entry && entry.state === 'completed' ? entry.result : undefined
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 是否已有缓存项(running 或 completed 都算)。 */
|
|
114
|
+
has(commandId) {
|
|
115
|
+
return this.entries.has(String(commandId ?? ''))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** 手动失效:只对 completed 生效;running 不可失效(避免副作用重复)。 */
|
|
119
|
+
invalidate(commandId) {
|
|
120
|
+
const id = String(commandId ?? '')
|
|
121
|
+
const entry = this.entries.get(id)
|
|
122
|
+
if (!entry || entry.state !== 'completed') return false
|
|
123
|
+
this.entries.delete(id)
|
|
124
|
+
return true
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 清空所有 completed 项,保留 running。
|
|
129
|
+
* running 的 promise 还在飞,完成后仍会落缓存(它确实已经执行过)。
|
|
130
|
+
* @returns {number} 被清掉的 completed 数量
|
|
131
|
+
*/
|
|
132
|
+
clear() {
|
|
133
|
+
const completedIds = []
|
|
134
|
+
for (const [id, entry] of this.entries) {
|
|
135
|
+
if (entry.state === 'completed') completedIds.push(id)
|
|
136
|
+
}
|
|
137
|
+
for (const id of completedIds) this.entries.delete(id)
|
|
138
|
+
return completedIds.length
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** 当前项数(可能暂时超过 capacity:全是 running 时)。 */
|
|
142
|
+
size() {
|
|
143
|
+
return this.entries.size
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 容量与状态统计(用于诊断/测试)。 */
|
|
147
|
+
stats() {
|
|
148
|
+
let running = 0
|
|
149
|
+
let completed = 0
|
|
150
|
+
for (const entry of this.entries.values()) {
|
|
151
|
+
if (entry.state === 'running') running += 1
|
|
152
|
+
else completed += 1
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
total: this.entries.size,
|
|
156
|
+
running,
|
|
157
|
+
completed,
|
|
158
|
+
capacity: this.capacity,
|
|
159
|
+
overCapacity: Math.max(0, this.entries.size - this.capacity),
|
|
160
|
+
...this.counters,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 把项移到 Map 末尾(最近使用)。 */
|
|
165
|
+
touch(commandId) {
|
|
166
|
+
const id = String(commandId ?? '')
|
|
167
|
+
const entry = this.entries.get(id)
|
|
168
|
+
if (!entry) return false
|
|
169
|
+
this.entries.delete(id)
|
|
170
|
+
this.entries.set(id, entry)
|
|
171
|
+
return true
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 容量收敛:从 Map 头开始找最久未用的 completed 项淘汰。
|
|
176
|
+
* running 项不可淘汰;全是 running 时直接返回(允许临时超限)。
|
|
177
|
+
*/
|
|
178
|
+
evictIfNeeded() {
|
|
179
|
+
while (this.entries.size > this.capacity) {
|
|
180
|
+
let victimId = null
|
|
181
|
+
for (const [id, entry] of this.entries) {
|
|
182
|
+
if (entry.state === 'completed') {
|
|
183
|
+
victimId = id
|
|
184
|
+
break
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (victimId === null) return
|
|
188
|
+
const victim = this.entries.get(victimId)
|
|
189
|
+
this.entries.delete(victimId)
|
|
190
|
+
this.counters.evictions += 1
|
|
191
|
+
if (this.onEvict) {
|
|
192
|
+
try {
|
|
193
|
+
this.onEvict(victimId, victim)
|
|
194
|
+
} catch {
|
|
195
|
+
/* 淘汰回调异常不得中断容量收敛 */
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** commandId 必须是非空字符串(Adapter 侧已用正则校验格式)。 */
|
|
203
|
+
function normalizeCommandId(commandId) {
|
|
204
|
+
const id = String(commandId ?? '').trim()
|
|
205
|
+
if (!id) throw new TypeError('command cache requires a non-empty commandId')
|
|
206
|
+
return id
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export const _internals = { normalizeCommandId }
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// dsh-Fang v3.0.3 — 客户端升级后的兼容性门禁(compat gate)。
|
|
3
|
+
//
|
|
4
|
+
// 触发时机:母体镜像(客户端版本)升级完成后。
|
|
5
|
+
// 目的:更新客户端版本后,默认不启用子插件;先验证
|
|
6
|
+
// * 全部分子插件模块可导入且导出 apply(当前 16 个,后续增多自动纳入);
|
|
7
|
+
// * 内核入口可导入;
|
|
8
|
+
// * 路由契约未破(数量 + 必需路径);
|
|
9
|
+
// 通过 → 允许启用子插件并继续自动适配;失败 → 保持子插件关闭,
|
|
10
|
+
// 写入 compat-status.json,等开发者更新插件版本后再次检查。
|
|
11
|
+
//
|
|
12
|
+
// 纯逻辑 + 动态 import;测试可注入 importModule 与 routePaths。
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs'
|
|
15
|
+
import path from 'node:path'
|
|
16
|
+
import { pathToFileURL } from 'node:url'
|
|
17
|
+
|
|
18
|
+
import { ensureDir, readJson, writeJsonAtomic } from '../kernel/index.mjs'
|
|
19
|
+
|
|
20
|
+
export const COMPAT_STATUS_FILE = 'compat-status.json'
|
|
21
|
+
export const COMPAT_KERNEL_ENTRY = 'src/kernel/index.mjs'
|
|
22
|
+
export const COMPAT_PLUGIN_DIR = 'src/plugins'
|
|
23
|
+
export const COMPAT_EXCLUDED_PLUGINS = Object.freeze(['imports.mjs'])
|
|
24
|
+
/** 路由契约:客户端升级后仍必须存在的核心路由(诊断/升级/镜像)。 */
|
|
25
|
+
export const REQUIRED_ROUTE_PATHS = Object.freeze([
|
|
26
|
+
'/fang/api/status',
|
|
27
|
+
'/fang/api/auth-token',
|
|
28
|
+
'/fang/api/mirror/status',
|
|
29
|
+
'/fang/api/mirror/upgrade',
|
|
30
|
+
'/fang/api/mirror/recompute-status',
|
|
31
|
+
'/fang/api/compat',
|
|
32
|
+
'/fang/api/essence/list',
|
|
33
|
+
'/fang/api/upgrade/status',
|
|
34
|
+
'/fang/api/tasks/branches',
|
|
35
|
+
'/fang/api/reflow/policy',
|
|
36
|
+
'/fang/api/composer/journal',
|
|
37
|
+
])
|
|
38
|
+
export const DEFAULT_EXPECTED_ROUTE_COUNT = 85
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 列出需要做导入检查的插件模块文件(排序稳定)。
|
|
42
|
+
* @param {string} rootDir 仓库/安装根目录
|
|
43
|
+
* @returns {string[]} 相对 rootDir 的路径
|
|
44
|
+
*/
|
|
45
|
+
export function listCompatModules(rootDir) {
|
|
46
|
+
const dir = path.join(rootDir, COMPAT_PLUGIN_DIR)
|
|
47
|
+
const files = fs.readdirSync(dir, { withFileTypes: true })
|
|
48
|
+
.filter(entry => entry.isFile() && entry.name.endsWith('.mjs') && !COMPAT_EXCLUDED_PLUGINS.includes(entry.name))
|
|
49
|
+
.map(entry => entry.name)
|
|
50
|
+
.sort()
|
|
51
|
+
return files.map(file => `${COMPAT_PLUGIN_DIR}/${file}`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 运行兼容性门禁。
|
|
56
|
+
* @param {{rootDir: string, expectedRouteCount?: number, routePaths?: string[],
|
|
57
|
+
* requiredRoutes?: string[], importModule?: (url: string) => Promise<any>}} options
|
|
58
|
+
* @returns {Promise<{ok:boolean, checkedAt:number, moduleCount:number, routeCount:number,
|
|
59
|
+
* expectedRouteCount:number, findings:Array<{code:string, severity:'error'|'warning', message:string}>}>}
|
|
60
|
+
*/
|
|
61
|
+
export async function runCompatGate(options) {
|
|
62
|
+
const rootDir = String(options?.rootDir ?? '')
|
|
63
|
+
if (rootDir.length === 0) throw new Error('runCompatGate: rootDir required')
|
|
64
|
+
const importModule = typeof options?.importModule === 'function'
|
|
65
|
+
? options.importModule
|
|
66
|
+
: (url) => import(url)
|
|
67
|
+
const expectedRouteCount = Math.max(0, Math.floor(Number(options?.expectedRouteCount) || DEFAULT_EXPECTED_ROUTE_COUNT))
|
|
68
|
+
const requiredRoutes = Array.isArray(options?.requiredRoutes) ? options.requiredRoutes : [...REQUIRED_ROUTE_PATHS]
|
|
69
|
+
const routePaths = Array.isArray(options?.routePaths) ? options.routePaths : null
|
|
70
|
+
/** @type {Array<{code:string, severity:'error'|'warning', message:string}>} */
|
|
71
|
+
const findings = []
|
|
72
|
+
|
|
73
|
+
// 1) 内核入口可导入
|
|
74
|
+
try {
|
|
75
|
+
await importModule(pathToFileURL(path.join(rootDir, COMPAT_KERNEL_ENTRY)).href)
|
|
76
|
+
} catch (error) {
|
|
77
|
+
findings.push({ code: 'kernel-import-failed', severity: 'error', message: `内核入口导入失败:${errorText(error)}` })
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 2) 每个子插件可导入且导出 apply(模块数量即“当前 16 个后续更多”)
|
|
81
|
+
let moduleCount = 0
|
|
82
|
+
let moduleFiles = []
|
|
83
|
+
try {
|
|
84
|
+
moduleFiles = listCompatModules(rootDir)
|
|
85
|
+
} catch (error) {
|
|
86
|
+
findings.push({ code: 'plugin-dir-unreadable', severity: 'error', message: `插件目录不可读:${errorText(error)}` })
|
|
87
|
+
}
|
|
88
|
+
for (const relative of moduleFiles) {
|
|
89
|
+
try {
|
|
90
|
+
const mod = await importModule(pathToFileURL(path.join(rootDir, relative)).href)
|
|
91
|
+
moduleCount += 1
|
|
92
|
+
if (typeof mod?.apply !== 'function') {
|
|
93
|
+
findings.push({ code: 'plugin-apply-missing', severity: 'error', message: `${relative} 未导出 apply()` })
|
|
94
|
+
}
|
|
95
|
+
if (typeof mod?.name !== 'string' || mod.name.length === 0) {
|
|
96
|
+
findings.push({ code: 'plugin-name-missing', severity: 'warning', message: `${relative} 未导出 name` })
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
findings.push({ code: 'plugin-import-failed', severity: 'error', message: `${relative} 导入失败:${errorText(error)}` })
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 3) 路由契约:数量 + 必需路径
|
|
104
|
+
const routeCount = routePaths === null ? expectedRouteCount : routePaths.length
|
|
105
|
+
// 只把「路由凭空少了」当错误:新增路由(例如带进程 token 的连接信息)不应
|
|
106
|
+
// 让整个子插件集被关掉;数量增长记一条 warning,保持可见但不拦。
|
|
107
|
+
if (routePaths !== null && routeCount < expectedRouteCount) {
|
|
108
|
+
findings.push({
|
|
109
|
+
code: 'route-count-mismatch', severity: 'error',
|
|
110
|
+
message: `路由数量 ${routeCount} < 期望 ${expectedRouteCount}`,
|
|
111
|
+
})
|
|
112
|
+
} else if (routePaths !== null && routeCount > expectedRouteCount) {
|
|
113
|
+
findings.push({
|
|
114
|
+
code: 'route-count-growth', severity: 'warning',
|
|
115
|
+
message: `路由数量 ${routeCount} > 期望 ${expectedRouteCount}(新增路由,非错误)`,
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
if (routePaths !== null) {
|
|
119
|
+
for (const required of requiredRoutes) {
|
|
120
|
+
if (!routePaths.includes(required)) {
|
|
121
|
+
findings.push({ code: 'route-missing', severity: 'error', message: `缺少必需路由 ${required}` })
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const ok = !findings.some(finding => finding.severity === 'error')
|
|
127
|
+
return {
|
|
128
|
+
ok,
|
|
129
|
+
checkedAt: Date.now(),
|
|
130
|
+
moduleCount,
|
|
131
|
+
routeCount,
|
|
132
|
+
expectedRouteCount,
|
|
133
|
+
findings,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 把门禁结果落盘;`subPluginsEnabled` 是唯一允许启用子插件的判据。 */
|
|
138
|
+
export function writeCompatStatus(stateDir, status) {
|
|
139
|
+
ensureDir(stateDir)
|
|
140
|
+
const payload = {
|
|
141
|
+
ok: status?.ok === true,
|
|
142
|
+
subPluginsEnabled: status?.ok === true,
|
|
143
|
+
checkedAt: Number(status?.checkedAt) || Date.now(),
|
|
144
|
+
moduleCount: Number(status?.moduleCount) || 0,
|
|
145
|
+
routeCount: Number(status?.routeCount) || 0,
|
|
146
|
+
expectedRouteCount: Number(status?.expectedRouteCount) || DEFAULT_EXPECTED_ROUTE_COUNT,
|
|
147
|
+
findings: Array.isArray(status?.findings) ? status.findings : [],
|
|
148
|
+
}
|
|
149
|
+
writeJsonAtomic(path.join(stateDir, COMPAT_STATUS_FILE), payload)
|
|
150
|
+
return payload
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 读取门禁状态;从未检查过时返回 subPluginsEnabled=false(安全默认)。 */
|
|
154
|
+
export function readCompatStatus(stateDir) {
|
|
155
|
+
const raw = readJson(path.join(stateDir, COMPAT_STATUS_FILE), null)
|
|
156
|
+
if (raw === null || typeof raw !== 'object') {
|
|
157
|
+
return {
|
|
158
|
+
ok: false, subPluginsEnabled: false, checkedAt: null,
|
|
159
|
+
moduleCount: 0, routeCount: 0, expectedRouteCount: DEFAULT_EXPECTED_ROUTE_COUNT,
|
|
160
|
+
findings: [], reason: 'not-checked',
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { ...raw, subPluginsEnabled: raw.subPluginsEnabled === true }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function errorText(error) {
|
|
167
|
+
return String(error instanceof Error ? error.message : error)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const _internals = Object.freeze({ errorText })
|