dsh-punky-swarm 0.3.6
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/CHANGELOG.md +120 -0
- package/LICENSE +661 -0
- package/README.en.md +281 -0
- package/README.md +281 -0
- package/cordis.patch.yml +59 -0
- package/lib/acps/certs.js +310 -0
- package/lib/acps/discovery-client.js +283 -0
- package/lib/acps/registry-client.js +527 -0
- package/lib/acps/server.js +302 -0
- package/lib/aip/agent-descriptor.js +170 -0
- package/lib/aip/identity.js +469 -0
- package/lib/aip/tool-descriptor.js +93 -0
- package/lib/api.js +266 -0
- package/lib/artifact-types.js +48 -0
- package/lib/assembly/audit-blind-review.js +170 -0
- package/lib/assembly/schema.js +253 -0
- package/lib/assembly.js +41 -0
- package/lib/assets.js +103 -0
- package/lib/bridge/trajectory.js +202 -0
- package/lib/client.js +704 -0
- package/lib/comms/acps-bridge.js +219 -0
- package/lib/comms/aip-format.js +141 -0
- package/lib/comms/budget.js +124 -0
- package/lib/comms/mailbox.js +221 -0
- package/lib/comms/topic-runtime.js +76 -0
- package/lib/comms/topic.js +115 -0
- package/lib/discovery/filter.js +199 -0
- package/lib/discovery/schema.js +157 -0
- package/lib/discovery/service.js +251 -0
- package/lib/hot/config-watch.js +252 -0
- package/lib/index.js +433 -0
- package/lib/lock.js +69 -0
- package/lib/panel/batch-detail.js +163 -0
- package/lib/panel/batch-list.js +84 -0
- package/lib/panel/locales.js +93 -0
- package/lib/panel/main.js +270 -0
- package/lib/panel/stream.js +348 -0
- package/lib/panel/theme.js +103 -0
- package/lib/panel/widgets.js +63 -0
- package/lib/schema.d.ts +192 -0
- package/lib/schema.js +259 -0
- package/lib/schema.ts +398 -0
- package/lib/state/archive.js +174 -0
- package/lib/state/command-exec.js +148 -0
- package/lib/state/constants.js +37 -0
- package/lib/state/corrupt-registry.js +103 -0
- package/lib/state/event-types.js +65 -0
- package/lib/state/gates.d.ts +226 -0
- package/lib/state/gates.js +428 -0
- package/lib/state/gates.ts +348 -0
- package/lib/state/machine-rules.d.ts +14 -0
- package/lib/state/machine-rules.js +76 -0
- package/lib/state/machine-rules.ts +88 -0
- package/lib/state/machine.js +68 -0
- package/lib/state/resume.js +201 -0
- package/lib/state/schema-v3.d.ts +10 -0
- package/lib/state/schema-v3.js +57 -0
- package/lib/state/schema-v3.ts +80 -0
- package/lib/state/store.js +607 -0
- package/lib/state/task-utils.js +40 -0
- package/lib/tools/core.js +323 -0
- package/lib/tools/git-utils.js +44 -0
- package/lib/tools/lane-tools.js +455 -0
- package/lib/tools/log-tools.js +173 -0
- package/lib/tools/mailbox-tools.js +121 -0
- package/lib/tools/merge-agent.js +127 -0
- package/lib/tools/register.js +85 -0
- package/lib/tools/shared.js +32 -0
- package/lib/types/contracts.d.ts +277 -0
- package/lib/types/contracts.js +17 -0
- package/lib/types/contracts.ts +279 -0
- package/lib/verify/evidence.js +249 -0
- package/lib/verify/gate.js +161 -0
- package/lib/verify/mount.js +48 -0
- package/lib/verify/selector.js +101 -0
- package/lib/watch/lane-heartbeat.js +333 -0
- package/lib/wave-plan.d.ts +48 -0
- package/lib/wave-plan.js +473 -0
- package/lib/wave-plan.ts +465 -0
- package/package.json +71 -0
- package/presets/jiufeng/NOTICE +5 -0
- package/presets/jiufeng/agent.cordis.yml +275 -0
- package/presets/jiufeng/preset.yml +3 -0
- package/skills/jiufeng-team/SKILL.md +214 -0
- package/skills/jiufeng-team/references/constitution.md +62 -0
- package/skills/jiufeng-team/references/roles/coder.md +17 -0
- package/skills/jiufeng-team/references/roles/coordinator.md +13 -0
- package/skills/jiufeng-team/references/roles/designer.md +14 -0
- package/skills/jiufeng-team/references/roles/doc-manager.md +13 -0
- package/skills/jiufeng-team/references/roles/manager.md +22 -0
- package/skills/jiufeng-team/references/roles/reviewer.md +19 -0
- package/skills/jiufeng-team/references/roles/supervisor.md +13 -0
- package/skills/jiufeng-team/references/roles/tester.md +13 -0
- package/skills/jiufeng-team/references/templates/call-chain-matrix-template.md +121 -0
- package/skills/jiufeng-team/references/templates/endpoint-behavior-template.md +60 -0
- package/skills/jiufeng-team/references/templates/gap-list.json +33 -0
- package/skills/jiufeng-team/references/templates/leader-decision-pack.md +92 -0
- package/skills/jiufeng-team/references/templates/plan-template.md +61 -0
- package/skills/jiufeng-team/references/templates/success-pattern-seeds.md +126 -0
- package/skills/jiufeng-team/references/workflow.md +98 -0
- package/skills/jiufeng-team/scripts/check-v3-density.mjs +98 -0
package/lib/schema.ts
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (C) 2025-2026 Punky
|
|
3
|
+
|
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Affero General Public License as
|
|
6
|
+
published by the Free Software Foundation, either version 3 of the
|
|
7
|
+
License, or (at your option) any later version.
|
|
8
|
+
|
|
9
|
+
This program is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Affero General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Affero General Public License
|
|
15
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// 蟛蜞模式状态机 schema(纯逻辑,零依赖)
|
|
19
|
+
// 成员状态:
|
|
20
|
+
// pending -> running -> review -> merged | failed | skipped | conflict
|
|
21
|
+
// idle:恢复语义(重启后 in-flight 成员落位 idle,可重新指派)
|
|
22
|
+
// Phase 2 类型化:常量 as const 派生字面量联合(单一事实源=常量);迁移表 satisfies 绑定 contracts 通用形态防漂移;
|
|
23
|
+
// 守卫函数 unknown 参数 + 类型谓词(运行期行为与 JS 源逐字节等价,as const/satisfies 纯类型层)。
|
|
24
|
+
import type { TransitionTable, BatchTransitionTable } from './types/contracts.js';
|
|
25
|
+
|
|
26
|
+
export const MEMBER_STATES = [
|
|
27
|
+
'pending', 'running', 'review', 'merged', 'failed', 'skipped', 'conflict', 'idle',
|
|
28
|
+
] as const;
|
|
29
|
+
export type MemberState = typeof MEMBER_STATES[number];
|
|
30
|
+
|
|
31
|
+
// 批次阶段:planning -> running -> paused -> aborted | complete
|
|
32
|
+
export const BATCH_PHASES = ['planning', 'running', 'paused', 'aborted', 'complete'] as const;
|
|
33
|
+
export type BatchPhase = typeof BATCH_PHASES[number];
|
|
34
|
+
|
|
35
|
+
export const SETTLE_STATES = ['merged', 'failed', 'skipped'] as const;
|
|
36
|
+
export type SettleState = typeof SETTLE_STATES[number];
|
|
37
|
+
|
|
38
|
+
export const MEMBER_TRANSITIONS = {
|
|
39
|
+
pending: ['running', 'failed', 'skipped'],
|
|
40
|
+
running: ['review', 'failed', 'skipped'],
|
|
41
|
+
review: ['merged', 'conflict', 'failed', 'running'], // running = REWORK 返工(attempt+1,由事件计数)
|
|
42
|
+
idle: ['running'],
|
|
43
|
+
merged: [],
|
|
44
|
+
failed: [],
|
|
45
|
+
skipped: [],
|
|
46
|
+
conflict: [],
|
|
47
|
+
} as const satisfies TransitionTable;
|
|
48
|
+
|
|
49
|
+
export const BATCH_TRANSITIONS = {
|
|
50
|
+
planning: ['running', 'aborted'],
|
|
51
|
+
running: ['paused', 'complete', 'aborted'],
|
|
52
|
+
paused: ['running', 'aborted'],
|
|
53
|
+
aborted: [],
|
|
54
|
+
complete: [],
|
|
55
|
+
} as const satisfies BatchTransitionTable;
|
|
56
|
+
|
|
57
|
+
// 守卫函数:unknown 入参 + 类型谓词(下游收窄);内部 includes 单点断言——
|
|
58
|
+
// 运行期等价:Array.includes 接受任意值,断言仅类型层。
|
|
59
|
+
export function isMemberState(s: unknown): s is MemberState {
|
|
60
|
+
return MEMBER_STATES.includes(s as MemberState);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function isBatchPhase(p: unknown): p is BatchPhase {
|
|
64
|
+
return BATCH_PHASES.includes(p as BatchPhase);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function isMemberTerminal(s: unknown): s is MemberState {
|
|
68
|
+
return SETTLE_STATES.includes(s as SettleState) || s === 'conflict';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isBatchTerminal(p: unknown): p is BatchPhase {
|
|
72
|
+
return p === 'aborted' || p === 'complete';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 参数类型化为 MemberState/BatchPhase;内部运行时守卫(isMemberState 等)保留不动
|
|
76
|
+
// (防御 JS 调用方非法值——类型只约束编译期)。
|
|
77
|
+
export function canTransitionMember(from: MemberState, to: MemberState) {
|
|
78
|
+
if (!isMemberState(from) || !isMemberState(to)) return false;
|
|
79
|
+
// 中间变量标注:satisfies 保留字面量类型,联合键索引得元组联合 → 统一为通用只读数组后再 includes(运行期零变更)
|
|
80
|
+
const tos: readonly MemberState[] = MEMBER_TRANSITIONS[from];
|
|
81
|
+
return tos.includes(to);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function canTransitionBatch(from: BatchPhase, to: BatchPhase) {
|
|
85
|
+
if (!isBatchPhase(from) || !isBatchPhase(to)) return false;
|
|
86
|
+
const tos: readonly BatchPhase[] = BATCH_TRANSITIONS[from];
|
|
87
|
+
return tos.includes(to);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function assertMemberTransition(from: MemberState, to: MemberState) {
|
|
91
|
+
if (!canTransitionMember(from, to)) {
|
|
92
|
+
throw new Error('invalid member transition: ' + from + ' -> ' + to);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function assertBatchTransition(from: BatchPhase, to: BatchPhase) {
|
|
97
|
+
if (!canTransitionBatch(from, to)) {
|
|
98
|
+
throw new Error('invalid batch phase transition: ' + from + ' -> ' + to);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function assertMemberState(s: MemberState) {
|
|
103
|
+
if (!isMemberState(s)) throw new Error('unknown member state: ' + String(s));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function assertBatchPhase(p: BatchPhase) {
|
|
107
|
+
if (!isBatchPhase(p)) throw new Error('unknown batch phase: ' + String(p));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ── 可选配置键默认值(装配默认开——AIP 为主线 + 治理能力全开;
|
|
111
|
+
// 显式 enabled:false 可逐键关闭。缺省 = 全能力启用,行为按各能力语义)──
|
|
112
|
+
// capabilities.trajectory:诊断桥接(lib/bridge/trajectory.js)——订阅 trajectory 异常 → sessionId→lane 映射 → notify;
|
|
113
|
+
// enabled=true(默认):桥接挂载;autoFail=false(默认):异常只 notify,不自动 member_settle failed;
|
|
114
|
+
// failConfidence:loop_deadlock 自动 failed 的置信阈值(默认 0.85,可配);
|
|
115
|
+
// poll:可选 HTTP 轮询 fallback(默认关,插件未装/无事件时静默降级)
|
|
116
|
+
export const TRAJECTORY_DEFAULTS = {
|
|
117
|
+
enabled: true,
|
|
118
|
+
autoFail: false,
|
|
119
|
+
failConfidence: 0.85,
|
|
120
|
+
poll: { enabled: false, baseUrl: null, intervalMs: 60_000 },
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// ── capabilities.watch:lane 过期检测(lib/watch/lane-heartbeat.js)──
|
|
124
|
+
// enabled=true(默认)时 watchdog 挂载、lane_heartbeat 注册
|
|
125
|
+
export const WATCH_DEFAULTS = Object.freeze({
|
|
126
|
+
enabled: true,
|
|
127
|
+
intervalsMinutes: [10, 20, 30], // 退避档位(分钟):冷场越久追问间隔越长
|
|
128
|
+
maxMissed: 3, // 硬停拍数:连续 N 拍无活动 → lane.stalled 事件,停止追问
|
|
129
|
+
scanIntervalMinutes: 1, // watchdog 扫描间隔(index.js 挂载 setInterval 用)
|
|
130
|
+
probeTemplate: null, // 追问模板(可选覆写;缺省用内置 ≤5 句模板,支持 {lane}/{batchId}/{missed} 占位)
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// 配置解析:宽松输入接口(字段 unknown,只声明存在性,不预判合法值——
|
|
134
|
+
// 运行期已有 Number.isFinite/typeof 归一化守卫兜底);返回类型显式声明(与现有返回对象结构一致)。
|
|
135
|
+
interface ConfigWatchInput {
|
|
136
|
+
enabled?: unknown;
|
|
137
|
+
intervalsMinutes?: unknown[];
|
|
138
|
+
maxMissed?: unknown;
|
|
139
|
+
scanIntervalMinutes?: unknown;
|
|
140
|
+
probeTemplate?: unknown;
|
|
141
|
+
}
|
|
142
|
+
interface ConfigVerifyInput {
|
|
143
|
+
enabled?: unknown;
|
|
144
|
+
mode?: unknown;
|
|
145
|
+
}
|
|
146
|
+
interface ConfigDiscoveryInput {
|
|
147
|
+
enabled?: unknown;
|
|
148
|
+
nodes?: unknown;
|
|
149
|
+
}
|
|
150
|
+
interface ConfigAcpsDiscoveryInput {
|
|
151
|
+
enabled?: unknown;
|
|
152
|
+
baseUrl?: unknown;
|
|
153
|
+
timeout?: unknown;
|
|
154
|
+
limit?: unknown;
|
|
155
|
+
scope?: unknown;
|
|
156
|
+
}
|
|
157
|
+
interface ConfigBridgeInput {
|
|
158
|
+
enabled?: unknown;
|
|
159
|
+
mode?: unknown;
|
|
160
|
+
inbound?: unknown;
|
|
161
|
+
}
|
|
162
|
+
interface ConfigAcpsEndpointInput {
|
|
163
|
+
enabled?: unknown;
|
|
164
|
+
port?: unknown;
|
|
165
|
+
host?: unknown;
|
|
166
|
+
cert?: unknown;
|
|
167
|
+
key?: unknown;
|
|
168
|
+
ca?: unknown;
|
|
169
|
+
certDir?: unknown;
|
|
170
|
+
aic?: unknown;
|
|
171
|
+
agentName?: unknown;
|
|
172
|
+
minVersion?: unknown;
|
|
173
|
+
devInsecure?: unknown;
|
|
174
|
+
}
|
|
175
|
+
interface ConfigCapabilitiesInput {
|
|
176
|
+
watch?: ConfigWatchInput;
|
|
177
|
+
verify?: ConfigVerifyInput;
|
|
178
|
+
discovery?: ConfigDiscoveryInput;
|
|
179
|
+
}
|
|
180
|
+
interface ConfigAcpsInput {
|
|
181
|
+
enabled?: unknown; // acps 能力总开关(resolveAcpsConfig 读)
|
|
182
|
+
discovery?: ConfigAcpsDiscoveryInput;
|
|
183
|
+
bridge?: ConfigBridgeInput;
|
|
184
|
+
endpoint?: ConfigAcpsEndpointInput;
|
|
185
|
+
}
|
|
186
|
+
interface ConfigInput {
|
|
187
|
+
capabilities?: ConfigCapabilitiesInput;
|
|
188
|
+
acps?: ConfigAcpsInput;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
interface ResolvedWatchConfig {
|
|
192
|
+
enabled: boolean;
|
|
193
|
+
intervalsMinutes: readonly number[];
|
|
194
|
+
maxMissed: number;
|
|
195
|
+
scanIntervalMinutes: number;
|
|
196
|
+
probeTemplate: string | null;
|
|
197
|
+
}
|
|
198
|
+
interface ResolvedVerifyConfig {
|
|
199
|
+
enabled: boolean;
|
|
200
|
+
mode: 'advisory' | 'enforce';
|
|
201
|
+
}
|
|
202
|
+
interface ResolvedDiscoveryConfig {
|
|
203
|
+
enabled: boolean;
|
|
204
|
+
nodes: object;
|
|
205
|
+
}
|
|
206
|
+
interface ResolvedAcpsDiscoveryConfig {
|
|
207
|
+
enabled: boolean;
|
|
208
|
+
baseUrl: string;
|
|
209
|
+
timeout: number;
|
|
210
|
+
limit: number;
|
|
211
|
+
scope: 'local' | 'external' | 'both';
|
|
212
|
+
}
|
|
213
|
+
interface ResolvedBridgeConfig {
|
|
214
|
+
enabled: boolean;
|
|
215
|
+
mode: 'inprocess' | 'http';
|
|
216
|
+
inbound: boolean;
|
|
217
|
+
}
|
|
218
|
+
interface ResolvedAcpsEndpointConfig {
|
|
219
|
+
enabled: boolean;
|
|
220
|
+
port: number;
|
|
221
|
+
host: string;
|
|
222
|
+
cert: string | null;
|
|
223
|
+
key: string | null;
|
|
224
|
+
ca: string | null;
|
|
225
|
+
certDir: string | null;
|
|
226
|
+
aic: string | null;
|
|
227
|
+
agentName: string;
|
|
228
|
+
minVersion: 'TLSv1.2' | 'TLSv1.3';
|
|
229
|
+
devInsecure: boolean;
|
|
230
|
+
}
|
|
231
|
+
interface ResolvedAcpsConfig {
|
|
232
|
+
enabled: boolean;
|
|
233
|
+
endpoint: ResolvedAcpsEndpointConfig;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function resolveWatchConfig(config: ConfigInput | null | undefined): ResolvedWatchConfig {
|
|
237
|
+
const c = config?.capabilities?.watch ?? {};
|
|
238
|
+
const intervals = Array.isArray(c.intervalsMinutes) && c.intervalsMinutes.length > 0
|
|
239
|
+
? c.intervalsMinutes.map((m: unknown) => (Number.isFinite(Number(m)) && Number(m) >= 0 ? Number(m) : null)).filter((x): x is number => x !== null)
|
|
240
|
+
: null;
|
|
241
|
+
return {
|
|
242
|
+
// P1-01 等价默认合并:缺省 = WATCH_DEFAULTS.enabled(true)(与 readCapability 对注册表 default 的
|
|
243
|
+
// deepMerge 语义等价——显式 enabled:false 才关)。不直接 import readCapability 以避免
|
|
244
|
+
// lib/schema.ts ⟷ lib/assembly/schema.ts 顶层循环依赖(assembly 顶层初始化需本文件常量,静态 import 会 TDZ)。
|
|
245
|
+
enabled: c.enabled !== false,
|
|
246
|
+
intervalsMinutes: intervals && intervals.length > 0 ? intervals : WATCH_DEFAULTS.intervalsMinutes,
|
|
247
|
+
maxMissed: Number.isFinite(Number(c.maxMissed)) && Number(c.maxMissed) >= 1 ? Math.floor(Number(c.maxMissed)) : WATCH_DEFAULTS.maxMissed,
|
|
248
|
+
scanIntervalMinutes: Number.isFinite(Number(c.scanIntervalMinutes)) && Number(c.scanIntervalMinutes) >= 0.1
|
|
249
|
+
? Number(c.scanIntervalMinutes)
|
|
250
|
+
: WATCH_DEFAULTS.scanIntervalMinutes,
|
|
251
|
+
probeTemplate: typeof c.probeTemplate === 'string' && c.probeTemplate.length > 0
|
|
252
|
+
? c.probeTemplate
|
|
253
|
+
: WATCH_DEFAULTS.probeTemplate,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ── capabilities.verify:验收证据(lib/verify/evidence.js + lib/verify/gate.js)──
|
|
258
|
+
// enabled=true(默认)捕获 hook 挂载;
|
|
259
|
+
// mode=advisory(默认)只记录不拦截;mode=enforce 显式启用则审计裁决拦截(createCompletionGate 读本键)
|
|
260
|
+
// as const:mode 字面量派生返回类型('advisory'|'enforce'),纯类型层、运行期等价。
|
|
261
|
+
export const VERIFY_DEFAULTS = Object.freeze({
|
|
262
|
+
enabled: true,
|
|
263
|
+
mode: 'advisory',
|
|
264
|
+
} as const);
|
|
265
|
+
|
|
266
|
+
export function resolveVerifyConfig(config: ConfigInput | null | undefined): ResolvedVerifyConfig {
|
|
267
|
+
const c = config?.capabilities?.verify ?? {};
|
|
268
|
+
return {
|
|
269
|
+
// P1-01 等价默认合并:缺省 = VERIFY_DEFAULTS.enabled(true),显式 enabled:false 才关(同 resolveWatchConfig 注释)
|
|
270
|
+
enabled: c.enabled !== false,
|
|
271
|
+
mode: c.mode === 'enforce' ? 'enforce' : VERIFY_DEFAULTS.mode,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ── capabilities.discovery:发现服务(lib/discovery/service.js)──
|
|
276
|
+
// ADP(Agent Discovery Protocol)统一 POST /discover + /.well-known/aip 预置;
|
|
277
|
+
// enabled=true(默认):webServer 存在时挂载发现服务(消费 tool catalog + agent-descriptor 目录);
|
|
278
|
+
// nodes:节点级 active 覆写——{ <aic|name>: { active: false } } 时该节点不出现在查询结果(默认 active=true)。
|
|
279
|
+
export const DISCOVERY_DEFAULTS = Object.freeze({
|
|
280
|
+
enabled: true,
|
|
281
|
+
nodes: {},
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
export function resolveDiscoveryConfig(config: ConfigInput | null | undefined): ResolvedDiscoveryConfig {
|
|
285
|
+
const c = config?.capabilities?.discovery ?? {};
|
|
286
|
+
return {
|
|
287
|
+
// P1-01 等价默认合并:缺省 = DISCOVERY_DEFAULTS.enabled(true),显式 enabled:false 才关(同 resolveWatchConfig 注释)
|
|
288
|
+
enabled: c.enabled !== false,
|
|
289
|
+
nodes: (c.nodes && typeof c.nodes === 'object' && !Array.isArray(c.nodes)) ? c.nodes : {},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// ── acps.discovery:外部 ADP 发现客户端(lib/acps/discovery-client.js)──
|
|
294
|
+
// 插件 Leader 经外部 discovery-server 发现 partner(POST {baseUrl}/discover);
|
|
295
|
+
// 默认关(对外能力显式开启)——enabled=false 不创建客户端实例,零运行时路径。
|
|
296
|
+
// baseUrl:外部 discovery-server 根地址(空 = 未配置,discover 调用抛错)
|
|
297
|
+
// timeout/limit:单次请求超时 ms(默认 10000)与默认返回上限(默认 5)
|
|
298
|
+
// scope:查询范围 local(仅既有本地目录)/ external(仅外部)/ both(本地+外部合并)
|
|
299
|
+
// as const:scope 字面量派生返回类型('local'|'external'|'both'),纯类型层、运行期等价。
|
|
300
|
+
export const ACPS_DISCOVERY_DEFAULTS = Object.freeze({
|
|
301
|
+
enabled: false,
|
|
302
|
+
baseUrl: '',
|
|
303
|
+
timeout: 10_000,
|
|
304
|
+
limit: 5,
|
|
305
|
+
scope: 'local',
|
|
306
|
+
} as const);
|
|
307
|
+
|
|
308
|
+
export function resolveAcpsDiscoveryConfig(config: ConfigInput | null | undefined): ResolvedAcpsDiscoveryConfig {
|
|
309
|
+
const c = config?.acps?.discovery ?? {};
|
|
310
|
+
// 单点断言:includes 命中即 c.scope 必为三值之一(运行期 includes 语义不变,断言仅类型层)
|
|
311
|
+
const scope: 'local' | 'external' | 'both' = (['local', 'external', 'both'] as string[]).includes(c.scope as string)
|
|
312
|
+
? (c.scope as 'local' | 'external' | 'both')
|
|
313
|
+
: ACPS_DISCOVERY_DEFAULTS.scope;
|
|
314
|
+
return {
|
|
315
|
+
enabled: c.enabled === true,
|
|
316
|
+
baseUrl: typeof c.baseUrl === 'string' ? c.baseUrl : ACPS_DISCOVERY_DEFAULTS.baseUrl,
|
|
317
|
+
timeout: Number.isFinite(Number(c.timeout)) && Number(c.timeout) > 0
|
|
318
|
+
? Number(c.timeout)
|
|
319
|
+
: ACPS_DISCOVERY_DEFAULTS.timeout,
|
|
320
|
+
limit: Number.isFinite(Number(c.limit)) && Number(c.limit) >= 1
|
|
321
|
+
? Math.floor(Number(c.limit))
|
|
322
|
+
: ACPS_DISCOVERY_DEFAULTS.limit,
|
|
323
|
+
scope,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ── config.acps.bridge:内部 ACPs 桥接(lib/comms/acps-bridge.js,进程内双向)──
|
|
328
|
+
// 桥只经 mailbox.js 公共接口投递/投影,绝不绕过;enabled=false 时不加载不实例化(零路径)。
|
|
329
|
+
// inbound 默认关:外部 ACPs 消息写 mailbox 需显式开启。
|
|
330
|
+
// as const:mode 字面量派生返回类型('inprocess'|'http'),纯类型层、运行期等价。
|
|
331
|
+
export const BRIDGE_DEFAULTS = Object.freeze({
|
|
332
|
+
enabled: false,
|
|
333
|
+
mode: 'inprocess', // 进程内双向(本机 HTTP 桥不实现)
|
|
334
|
+
inbound: false, // inbound 默认关:外部 ACPs 消息写 mailbox 需显式开启
|
|
335
|
+
} as const);
|
|
336
|
+
|
|
337
|
+
export function resolveBridgeConfig(config: ConfigInput | null | undefined): ResolvedBridgeConfig {
|
|
338
|
+
const c = config?.acps?.bridge ?? {};
|
|
339
|
+
return {
|
|
340
|
+
enabled: c.enabled === true,
|
|
341
|
+
mode: c.mode === 'http' ? 'http' : BRIDGE_DEFAULTS.mode,
|
|
342
|
+
inbound: c.inbound === true,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// ── config.acps:ACPs 通讯能力 ──
|
|
347
|
+
// 默认关(acps.enabled 与 acps.endpoint.enabled 均默认 false——显式开启才加载监听,关闭时零运行时路径)。
|
|
348
|
+
// endpoint:对外 mTLS 服务端点(lib/acps/server.js)——
|
|
349
|
+
// port 9443;host 默认 127.0.0.1(config 可配);
|
|
350
|
+
// cert/key/ca:三路径(null=自动生成到 certDir,默认 <root>/acps/certs);aic:ACS 身份码
|
|
351
|
+
// (缺省=agent-descriptor 派生占位,注册后经 config 覆盖);minVersion TLSv1.3;
|
|
352
|
+
// devInsecure:显式开发开关(默认 false,生产不允许降级)。
|
|
353
|
+
// 消费方:lib/index.js 装配(enabled 双真才实例化 createAcpsServer);lib/assembly/schema.js CAPABILITY_REGISTRY。
|
|
354
|
+
// as const:minVersion 字面量派生返回类型('TLSv1.2'|'TLSv1.3'),纯类型层、运行期等价。
|
|
355
|
+
export const ACPS_DEFAULTS = Object.freeze({
|
|
356
|
+
enabled: false, // 能力总开关(默认关)
|
|
357
|
+
endpoint: {
|
|
358
|
+
enabled: false, // 对外端点开关(默认关)
|
|
359
|
+
port: 9443, // 对外监听端口
|
|
360
|
+
host: '127.0.0.1',
|
|
361
|
+
cert: null, // 三路径(null=自动生成 certDir)
|
|
362
|
+
key: null,
|
|
363
|
+
ca: null,
|
|
364
|
+
certDir: null, // 缺省 <root>/acps/certs(index.js 注入 root)
|
|
365
|
+
aic: null, // ACS aic(缺省=agent-descriptor 派生占位)
|
|
366
|
+
agentName: 'dsh-punky-swarm',
|
|
367
|
+
minVersion: 'TLSv1.3', // TLS 最低版本
|
|
368
|
+
devInsecure: false, // 开发模式开关(默认关)
|
|
369
|
+
},
|
|
370
|
+
} as const);
|
|
371
|
+
|
|
372
|
+
export function resolveAcpsConfig(config: ConfigInput | null | undefined): ResolvedAcpsConfig {
|
|
373
|
+
const c = config?.acps ?? {};
|
|
374
|
+
const e = (c.endpoint && typeof c.endpoint === 'object' && !Array.isArray(c.endpoint)) ? c.endpoint : {};
|
|
375
|
+
return {
|
|
376
|
+
enabled: c.enabled === true,
|
|
377
|
+
endpoint: {
|
|
378
|
+
enabled: e.enabled === true,
|
|
379
|
+
port: Number.isInteger(Number(e.port)) && Number(e.port) > 0 ? Number(e.port) : ACPS_DEFAULTS.endpoint.port,
|
|
380
|
+
host: typeof e.host === 'string' && e.host.length > 0 ? e.host : ACPS_DEFAULTS.endpoint.host,
|
|
381
|
+
cert: typeof e.cert === 'string' && e.cert.length > 0 ? e.cert : null,
|
|
382
|
+
key: typeof e.key === 'string' && e.key.length > 0 ? e.key : null,
|
|
383
|
+
ca: typeof e.ca === 'string' && e.ca.length > 0 ? e.ca : null,
|
|
384
|
+
certDir: typeof e.certDir === 'string' && e.certDir.length > 0 ? e.certDir : null,
|
|
385
|
+
aic: typeof e.aic === 'string' && e.aic.length > 0 ? e.aic : null,
|
|
386
|
+
agentName: typeof e.agentName === 'string' && e.agentName.length > 0 ? e.agentName : ACPS_DEFAULTS.endpoint.agentName,
|
|
387
|
+
// 单点断言:双条件命中即 e.minVersion 必为二值之一(运行期比较语义不变,断言仅类型层)
|
|
388
|
+
minVersion: e.minVersion === 'TLSv1.2' || e.minVersion === 'TLSv1.3' ? (e.minVersion as 'TLSv1.2' | 'TLSv1.3') : ACPS_DEFAULTS.endpoint.minVersion,
|
|
389
|
+
devInsecure: e.devInsecure === true,
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// ── config.ratchet:棘轮规则表配置键(可选;缺省 = 默认规则 = 本文件 MEMBER_TRANSITIONS/BATCH_TRANSITIONS,零运行时开销)──
|
|
395
|
+
// 消费方 lib/state/machine-rules.ts(loadRules):
|
|
396
|
+
// { ratchet: { memberRules?: { from: [to...] }, batchRules?: { from: [to...] }, allowRelax?: false } }
|
|
397
|
+
// 棘轮语义:配置覆盖只许删(收紧)不许增(放宽)——覆盖表出现本文件默认表不存在的迁移 → loadRules throw(fail-closed);
|
|
398
|
+
// allowRelax: true 为显式逃生门(默认 false,仅在部署侧明确授权时可用,不推荐)。
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (C) 2025-2026 Punky
|
|
3
|
+
|
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU Affero General Public License as
|
|
6
|
+
published by the Free Software Foundation, either version 3 of the
|
|
7
|
+
License, or (at your option) any later version.
|
|
8
|
+
|
|
9
|
+
This program is distributed in the hope that it will be useful,
|
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
+
GNU Affero General Public License for more details.
|
|
13
|
+
|
|
14
|
+
You should have received a copy of the GNU Affero General Public License
|
|
15
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// Archive:done→archive——complete 后自动归档、单向(产物打包保留可查 + 不可回滚标记)
|
|
19
|
+
// 职责(单文件 = 归档,不含状态迁移判定,迁移判定见 machine.js):
|
|
20
|
+
// archiveBatch —— 把 sessions/<sessionId>/artifacts/<batchId>/ 快照复制到 sessions/<sessionId>/archive/<batchId>/
|
|
21
|
+
// (只复制不移动,保留原产物引用)+ 写 manifest.json(批次元数据 + 产物清单 + lanes 终态快照)
|
|
22
|
+
// + 批次 JSON 置 archived:true + archive.done 事件
|
|
23
|
+
// readManifest —— 只读:归档 manifest(无则 null)
|
|
24
|
+
// listArchived —— 只读:已归档批次列表
|
|
25
|
+
// 单向三重锁定:archived 字段 + 无 unarchive 入口(grep 可证无反向 API)+ complete 终态拒写(batch_phase/appendEvent 既有语义)
|
|
26
|
+
// 幂等:manifest 已存在或 archived===true → no-op 返回既有记录(不重复复制、不覆盖 manifest)
|
|
27
|
+
// 失败语义:归档失败 → archive.failed 事件(含原因),不阻断 complete(complete 已置终态;归档为可重试伴随动作)
|
|
28
|
+
// 打包形态:目录快照复制 + manifest 索引(Node 内置 fs/path/crypto,零运行时依赖,不做 zip)
|
|
29
|
+
import fs from 'node:fs';
|
|
30
|
+
import path from 'node:path';
|
|
31
|
+
import { createHash } from 'node:crypto';
|
|
32
|
+
import { migrateV2toV3 } from './schema-v3.js';
|
|
33
|
+
import { SESSION_RE } from './constants.js'; // P1-07 单点(原 :34 定义迁出)
|
|
34
|
+
// R-01 发端收敛:archive.failed/archive.done 事件字面量改引 EVT 常量单点
|
|
35
|
+
import * as EVT from './event-types.js';
|
|
36
|
+
|
|
37
|
+
const BATCH_RE = SESSION_RE; // 同款正则(batchId 与 sessionId 共用 SAFE_ID 字符集)
|
|
38
|
+
|
|
39
|
+
export function createArchive(root) {
|
|
40
|
+
const sessionsDir = path.join(root, 'sessions');
|
|
41
|
+
|
|
42
|
+
function sessionDir(sessionId) {
|
|
43
|
+
if (!SESSION_RE.test(sessionId)) throw new Error('invalid sessionId: ' + sessionId);
|
|
44
|
+
return path.join(sessionsDir, sessionId);
|
|
45
|
+
}
|
|
46
|
+
function batchFileOf(sessionId, batchId) {
|
|
47
|
+
if (!BATCH_RE.test(batchId)) throw new Error('invalid batchId');
|
|
48
|
+
return path.join(sessionDir(sessionId), 'batches', batchId + '.json');
|
|
49
|
+
}
|
|
50
|
+
function artifactsDirOf(sessionId, batchId) {
|
|
51
|
+
return path.join(sessionDir(sessionId), 'artifacts', batchId);
|
|
52
|
+
}
|
|
53
|
+
// 归档目标:sessions/<sessionId>/archive/<batchId>/(与 batches/、artifacts/ 平级)
|
|
54
|
+
function archiveDirOf(sessionId, batchId) {
|
|
55
|
+
return path.join(sessionDir(sessionId), 'archive', batchId);
|
|
56
|
+
}
|
|
57
|
+
function manifestFileOf(sessionId, batchId) {
|
|
58
|
+
return path.join(archiveDirOf(sessionId, batchId), 'manifest.json');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function readBatch(sessionId, batchId) {
|
|
62
|
+
const file = batchFileOf(sessionId, batchId);
|
|
63
|
+
if (!fs.existsSync(file)) return null;
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
66
|
+
} catch {
|
|
67
|
+
// 损坏批次隔离(v2-node-robustness ②,AC-1 读路径不 throw):损坏 → null(登记在 store 旁路清单,本文件不重复)
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function atomicWrite(file, data) {
|
|
72
|
+
const dir = path.dirname(file);
|
|
73
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
74
|
+
const tmp = path.join(dir, '.' + path.basename(file) + '.' + process.pid + '.tmp');
|
|
75
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 2));
|
|
76
|
+
fs.renameSync(tmp, file); // Windows: MoveFileEx REPLACE_EXISTING
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 只读:归档 manifest(无则 null)
|
|
80
|
+
function readManifest(sessionId, batchId) {
|
|
81
|
+
const file = manifestFileOf(sessionId, batchId);
|
|
82
|
+
if (!fs.existsSync(file)) return null;
|
|
83
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 只读:已归档批次列表(有 manifest 的批次目录,稳定排序)
|
|
87
|
+
function listArchived(sessionId) {
|
|
88
|
+
const dir = path.join(sessionDir(sessionId), 'archive');
|
|
89
|
+
if (!fs.existsSync(dir)) return [];
|
|
90
|
+
return fs.readdirSync(dir)
|
|
91
|
+
.filter((f) => BATCH_RE.test(f) && fs.existsSync(path.join(dir, f, 'manifest.json')))
|
|
92
|
+
.sort();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 产物清单:相对路径 + size + sha256(快照完整性可校验)
|
|
96
|
+
function sha256Of(file) {
|
|
97
|
+
return createHash('sha256').update(fs.readFileSync(file)).digest('hex');
|
|
98
|
+
}
|
|
99
|
+
function collectArtifacts(dir) {
|
|
100
|
+
const out = [];
|
|
101
|
+
const walk = (d, rel) => {
|
|
102
|
+
for (const name of fs.readdirSync(d)) {
|
|
103
|
+
const abs = path.join(d, name);
|
|
104
|
+
const relPath = rel ? rel + '/' + name : name;
|
|
105
|
+
const st = fs.statSync(abs);
|
|
106
|
+
if (st.isDirectory()) walk(abs, relPath);
|
|
107
|
+
else if (st.isFile()) out.push({ path: relPath, size: st.size, sha256: sha256Of(abs) });
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
walk(dir, '');
|
|
111
|
+
out.sort((a, b) => a.path.localeCompare(b.path));
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 失败留痕:archive.failed 事件(best-effort,不抛——complete 已置终态,归档为可重试伴随动作)
|
|
116
|
+
function recordFailure(sessionId, batchId, reason) {
|
|
117
|
+
try {
|
|
118
|
+
const b = readBatch(sessionId, batchId);
|
|
119
|
+
if (!b) return;
|
|
120
|
+
const nb = migrateV2toV3(b); // v2 存量批次经迁移兜底(schema 升 3 + chains/archived 补齐)
|
|
121
|
+
nb.events = nb.events ?? [];
|
|
122
|
+
nb.events.push({ ts: new Date().toISOString(), type: EVT.EVT_ARCHIVE_FAILED, reason });
|
|
123
|
+
nb.updatedAt = new Date().toISOString();
|
|
124
|
+
atomicWrite(batchFileOf(sessionId, batchId), nb);
|
|
125
|
+
} catch { /* 兜底也失败:静默(审计可经 archive/ 目录状态判断) */ }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 归档动作(幂等;失败记录 archive.failed 后返回 { ok:false },不 throw)
|
|
129
|
+
function archiveBatch(sessionId, batchId) {
|
|
130
|
+
const batch = readBatch(sessionId, batchId);
|
|
131
|
+
if (!batch) throw new Error('batch not found: ' + batchId);
|
|
132
|
+
const next = migrateV2toV3(batch); // 归一化视图:schema 3 + chains/archived 兜底(chains 逻辑不动)
|
|
133
|
+
// 幂等:archived 已置 或 manifest 已存在 → no-op 返回既有记录
|
|
134
|
+
if (next.archived === true) return readManifest(sessionId, batchId) ?? { batchId, sessionId, archived: true };
|
|
135
|
+
const existing = readManifest(sessionId, batchId);
|
|
136
|
+
if (existing) return existing;
|
|
137
|
+
|
|
138
|
+
const srcDir = artifactsDirOf(sessionId, batchId);
|
|
139
|
+
if (!fs.existsSync(srcDir)) {
|
|
140
|
+
recordFailure(sessionId, batchId, 'artifacts dir missing: ' + srcDir);
|
|
141
|
+
return { ok: false, reason: 'artifacts dir missing: ' + srcDir };
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
const dstDir = archiveDirOf(sessionId, batchId);
|
|
145
|
+
fs.cpSync(srcDir, dstDir, { recursive: true }); // 快照复制(只复制不移动,保留原产物引用)
|
|
146
|
+
const artifacts = collectArtifacts(dstDir);
|
|
147
|
+
const archivedAt = new Date().toISOString();
|
|
148
|
+
const manifest = {
|
|
149
|
+
batchId,
|
|
150
|
+
sessionId,
|
|
151
|
+
archivedAt,
|
|
152
|
+
schema: next.schema,
|
|
153
|
+
phase: next.phase,
|
|
154
|
+
wavePlan: { waves: (next.wavePlan ?? []).length, lanes: Object.keys(next.lanes ?? {}).length },
|
|
155
|
+
lanes: { ...(next.lanes ?? {}) },
|
|
156
|
+
artifacts,
|
|
157
|
+
eventCount: (next.events ?? []).length,
|
|
158
|
+
};
|
|
159
|
+
atomicWrite(manifestFileOf(sessionId, batchId), manifest);
|
|
160
|
+
// 单向标记:批次 JSON 置 archived:true + archive.done 事件(无 unarchive 入口 = 不可回滚)
|
|
161
|
+
next.archived = true;
|
|
162
|
+
next.events = next.events ?? [];
|
|
163
|
+
next.events.push({ ts: archivedAt, type: EVT.EVT_ARCHIVE_DONE, archivedAt });
|
|
164
|
+
next.updatedAt = archivedAt;
|
|
165
|
+
atomicWrite(batchFileOf(sessionId, batchId), next);
|
|
166
|
+
return manifest;
|
|
167
|
+
} catch (err) {
|
|
168
|
+
recordFailure(sessionId, batchId, String((err && err.message) || err));
|
|
169
|
+
return { ok: false, reason: String((err && err.message) || err) };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return { archiveBatch, readManifest, listArchived };
|
|
174
|
+
}
|