@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.
Files changed (38) hide show
  1. package/client.js +1511 -1
  2. package/cordis.patch.yml +9 -0
  3. package/package.json +6 -2
  4. package/src/compose.mjs +4 -1
  5. package/src/kernel/paths.mjs +9 -1
  6. package/src/kernel/role.mjs +1 -0
  7. package/src/kernel/types.mjs +5 -1
  8. package/src/lib/ai-secret-store.mjs +202 -0
  9. package/src/lib/boot-guard.mjs +182 -0
  10. package/src/lib/business-producers.mjs +109 -0
  11. package/src/lib/compat.mjs +1 -1
  12. package/src/lib/composer-journal.mjs +8 -0
  13. package/src/lib/decision-events.mjs +129 -0
  14. package/src/lib/decision-ingest-client.mjs +41 -0
  15. package/src/lib/decision-outbox.mjs +129 -0
  16. package/src/lib/desktop-launch.mjs +138 -0
  17. package/src/lib/fork-supervision-settings.mjs +58 -0
  18. package/src/lib/mirror-official-source.mjs +93 -0
  19. package/src/lib/mirror.mjs +10 -3
  20. package/src/lib/producer-contract.mjs +162 -0
  21. package/src/lib/restart-contract.mjs +650 -0
  22. package/src/lib/rule-registry.mjs +69 -0
  23. package/src/lib/ssrf-guard.mjs +170 -0
  24. package/src/lib/umbrella.mjs +143 -18
  25. package/src/plugins/adapter.mjs +31 -2
  26. package/src/plugins/approver.mjs +423 -40
  27. package/src/plugins/decision-center.mjs +2255 -0
  28. package/src/plugins/forker.mjs +20 -1
  29. package/src/plugins/healer.mjs +53 -20
  30. package/src/plugins/imports.mjs +48 -0
  31. package/src/plugins/orchestrator.mjs +814 -42
  32. package/src/plugins/restart-guard.mjs +645 -52
  33. package/src/plugins/scorer.mjs +227 -15
  34. package/src/plugins/umbrella.mjs +18 -14
  35. package/tools/boot-guard.mjs +95 -0
  36. package/tools/decision-ingest.mjs +53 -0
  37. package/tools/desktop-launch.mjs +71 -0
  38. package/tools/fangctl.mjs +229 -1
@@ -0,0 +1,162 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // Copyright (c) 2026 dsh-Fang contributors — see LICENSE-CORE.
3
+ //
4
+ // producer-contract — 业务生产者 → 决策中心的统一调用契约(v1)。
5
+ //
6
+ // 这五个系统本体不在本仓:本模块只定义「它们该怎么调用」,并提供:
7
+ // * normalizeProducerPayload(input) 字段校验 + 默认值 + 稳定 dedupeKey
8
+ // * createProducerSubmitter({submit}) 统一提交封装(返回统一 envelope)
9
+ // * producerCatalog() 机器可读契约目录(UI/外部系统可发现)
10
+ import { nowMs } from '../kernel/index.mjs'
11
+
12
+ export const PRODUCER_CONTRACT_VERSION = 1
13
+ export const PRODUCER_LEVELS = Object.freeze(['red', 'yellow', 'blue', 'green'])
14
+ export const PRODUCER_NAMES = Object.freeze(['HarnessMM', 'PluginUpdater', 'SuperAgent', 'PJM', 'Worker'])
15
+
16
+ export const PRODUCER_CONTRACTS = Object.freeze({
17
+ HarnessMM: Object.freeze({
18
+ kinds: ['harnessmm-suggestion'],
19
+ defaultLevel: 'blue',
20
+ sourceRole: 'worker',
21
+ required: ['title'],
22
+ defaultActions: [
23
+ { id: 'accept', label: '采纳', type: 'default' },
24
+ { id: 'ignore', label: '忽略', type: 'default' },
25
+ ],
26
+ }),
27
+ PluginUpdater: Object.freeze({
28
+ kinds: ['plugin-updater-backup'],
29
+ defaultLevel: 'green',
30
+ sourceRole: 'worker',
31
+ required: ['title'],
32
+ defaultActions: [],
33
+ }),
34
+ SuperAgent: Object.freeze({
35
+ kinds: ['superagent-completion'],
36
+ defaultLevel: 'green',
37
+ sourceRole: 'worker',
38
+ required: ['title'],
39
+ defaultActions: [],
40
+ }),
41
+ PJM: Object.freeze({
42
+ kinds: ['pjm-timeout', 'pjm-fatal'],
43
+ defaultLevel: 'yellow',
44
+ sourceRole: 'pjm',
45
+ required: ['title'],
46
+ defaultActions: [
47
+ { id: 'retry', label: '重试', type: 'primary' },
48
+ { id: 'abandon', label: '放弃', type: 'default' },
49
+ ],
50
+ }),
51
+ Worker: Object.freeze({
52
+ kinds: ['worker-deviation'],
53
+ defaultLevel: 'yellow',
54
+ sourceRole: 'worker',
55
+ required: ['title'],
56
+ defaultActions: [
57
+ { id: 'review', label: '查看', type: 'primary' },
58
+ { id: 'ignore', label: '忽略', type: 'default' },
59
+ ],
60
+ }),
61
+ })
62
+
63
+ const fail = (code, extra = {}) => ({ ok: false, code, ...extra })
64
+
65
+ /** 稳定默认 dedupeKey:调用方显式传优先,否则用常见业务 id 派生。 */
66
+ function deriveDedupeKey(producer, kind, raw) {
67
+ if (raw.dedupeKey !== undefined && raw.dedupeKey !== null && String(raw.dedupeKey).trim()) return String(raw.dedupeKey).trim().slice(0, 200)
68
+ const ref = raw.id ?? raw.backupId ?? raw.suggestionId ?? raw.taskId ?? raw.projectId ?? raw.workerId
69
+ if (ref === undefined || ref === null || String(ref).trim() === '') return undefined
70
+ return `${producer}:${kind}:${String(ref).trim()}`.slice(0, 200)
71
+ }
72
+
73
+ /**
74
+ * 校验并补全统一 payload。成功返回 { ok:true, payload };失败返回 { ok:false, code, errors }。
75
+ * 不写盘、不提交。
76
+ */
77
+ export function normalizeProducerPayload(input = {}) {
78
+ if (!input || typeof input !== 'object' || Array.isArray(input)) return fail('invalid-payload')
79
+ const producer = String(input.producer ?? '').trim()
80
+ const contract = PRODUCER_CONTRACTS[producer]
81
+ if (!contract) return fail('unknown-producer', { allowedProducers: [...PRODUCER_NAMES] })
82
+ const kind = String(input.kind ?? contract.kinds[0]).trim()
83
+ if (!contract.kinds.includes(kind)) {
84
+ return fail('unknown-kind', { producer, allowedKinds: [...contract.kinds] })
85
+ }
86
+ const level = input.level === undefined || input.level === null ? contract.defaultLevel : String(input.level).trim().toLowerCase()
87
+ if (!PRODUCER_LEVELS.includes(level)) return fail('bad-level', { allowedLevels: [...PRODUCER_LEVELS] })
88
+ const title = String(input.title ?? '').trim()
89
+ if (!title) return fail('title-required')
90
+ const actions = Array.isArray(input.actions) ? input.actions.slice(0, 12)
91
+ : (Array.isArray(contract.defaultActions) && contract.defaultActions.length > 0 ? contract.defaultActions.map((item) => ({ ...item })) : undefined)
92
+ const options = Array.isArray(input.options) ? input.options.slice(0, 12) : undefined
93
+ if (level !== 'green' && !(actions?.length) && !(options?.length)) {
94
+ return fail('options-or-actions-required', { level })
95
+ }
96
+ const timeoutMinutes = Number.isFinite(Number(input.timeoutMinutes))
97
+ ? Math.max(0, Number(input.timeoutMinutes))
98
+ : kind === 'pjm-timeout' ? 5 : undefined
99
+ const payload = {
100
+ producer,
101
+ kind,
102
+ level,
103
+ title: title.slice(0, 500),
104
+ context: String(input.context ?? input.description ?? `${producer}/${kind}`).slice(0, 12000),
105
+ ...(actions ? { actions } : {}),
106
+ ...(options ? { options } : {}),
107
+ ...(timeoutMinutes === undefined ? {} : { timeoutMinutes }),
108
+ ...(deriveDedupeKey(producer, kind, input) ? { dedupeKey: deriveDedupeKey(producer, kind, input) } : {}),
109
+ sourceRole: String(input.sourceRole ?? contract.sourceRole),
110
+ ...(input.sessionId ? { sessionId: String(input.sessionId).slice(0, 200) } : {}),
111
+ ...(input.metadata && typeof input.metadata === 'object' ? { metadata: input.metadata } : {}),
112
+ producerContractVersion: PRODUCER_CONTRACT_VERSION,
113
+ at: nowMs(),
114
+ }
115
+ return { ok: true, payload }
116
+ }
117
+
118
+ /** 统一提交封装:返回固定 envelope,外部系统不需要理解 decision-center 内部形状。 */
119
+ export function createProducerSubmitter({ submit } = {}) {
120
+ if (typeof submit !== 'function') throw new TypeError('createProducerSubmitter: submit required')
121
+ return async function submitProducerEvent(input = {}) {
122
+ const normalized = normalizeProducerPayload(input)
123
+ if (!normalized.ok) {
124
+ return { ok: false, code: normalized.code, errors: normalized.errors ?? null, ...normalized }
125
+ }
126
+ let out = null
127
+ try {
128
+ out = await submit(normalized.payload)
129
+ } catch (error) {
130
+ return { ok: false, code: 'submit-failed', error: String(error?.message ?? error).slice(0, 500) }
131
+ }
132
+ return {
133
+ ok: out?.ok !== false,
134
+ producer: normalized.payload.producer,
135
+ kind: normalized.payload.kind,
136
+ level: normalized.payload.level,
137
+ ticketId: out?.ticket?.id ?? null,
138
+ deduped: out?.deduped === true,
139
+ queued: out?.queued === true,
140
+ outboxId: out?.outboxId ?? null,
141
+ reason: out?.reason ?? null,
142
+ }
143
+ }
144
+ }
145
+
146
+ export function producerCatalog() {
147
+ return {
148
+ ok: true,
149
+ version: PRODUCER_CONTRACT_VERSION,
150
+ producers: PRODUCER_NAMES.map((producer) => {
151
+ const contract = PRODUCER_CONTRACTS[producer]
152
+ return {
153
+ producer,
154
+ kinds: [...contract.kinds],
155
+ defaultLevel: contract.defaultLevel,
156
+ sourceRole: contract.sourceRole,
157
+ required: [...contract.required],
158
+ payload: ['producer', 'kind', 'level?', 'title', 'context', 'actions|options', 'timeoutMinutes?', 'dedupeKey?', 'sourceRole?', 'sessionId?'],
159
+ }
160
+ }),
161
+ }
162
+ }