flow-comet 1.5.0-rc.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/.flow-comet/rules/flow-comet-orchestration.md +13 -0
- package/.flow-comet/skills/flow-comet/GUIDANCE.md +196 -0
- package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -0
- package/.flow-comet/skills/flow-comet/SKILL.md +196 -0
- package/.flow-comet/skills/flow-comet/reference/KNOWN-ISSUES-template.md +19 -0
- package/.flow-comet/skills/flow-comet/reference/TEST-MATRIX.md +115 -0
- package/.flow-comet/skills/flow-comet/reference/authoring-lanes.json +33 -0
- package/.flow-comet/skills/flow-comet/reference/composition-report.md +65 -0
- package/.flow-comet/skills/flow-comet/reference/decision-points.md +64 -0
- package/.flow-comet/skills/flow-comet/reference/dirty-worktree.md +30 -0
- package/.flow-comet/skills/flow-comet/reference/dsh-platform.md +169 -0
- package/.flow-comet/skills/flow-comet/reference/init-detection.md +58 -0
- package/.flow-comet/skills/flow-comet/reference/recovery.md +20 -0
- package/.flow-comet/skills/flow-comet/reference/resolved-skills.json +661 -0
- package/.flow-comet/skills/flow-comet/reference/skill-review.md +13 -0
- package/.flow-comet/skills/flow-comet/reference/subagents/script-author.md +7 -0
- package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +681 -0
- package/.flow-comet/skills/flow-comet/reference/worktree-notes.md +52 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-check.mjs +61 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +909 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-plan.mjs +2 -0
- package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +312 -0
- package/.flow-comet/skills/flow-comet/scripts/contract-check.mjs +136 -0
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +7331 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +376 -0
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +230 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +99 -0
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +4017 -0
- package/.flow-comet/skills/flow-comet/scripts/task-parsing.mjs +28 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +2396 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +378 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +1263 -0
- package/.flow-comet/skills/flow-comet-archive/SKILL.md +195 -0
- package/.flow-comet/skills/flow-comet-change/SKILL.md +29 -0
- package/.flow-comet/skills/flow-comet-compose/SKILL.md +249 -0
- package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +23 -0
- package/.flow-comet/skills/flow-comet-design/SKILL.md +161 -0
- package/.flow-comet/skills/flow-comet-dev/SKILL.md +51 -0
- package/.flow-comet/skills/flow-comet-evolve/SKILL.md +26 -0
- package/.flow-comet/skills/flow-comet-execute/SKILL.md +182 -0
- package/.flow-comet/skills/flow-comet-health/SKILL.md +31 -0
- package/.flow-comet/skills/flow-comet-integration/SKILL.md +45 -0
- package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +36 -0
- package/.flow-comet/skills/flow-comet-open/SKILL.md +161 -0
- package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +33 -0
- package/.flow-comet/skills/flow-comet-plan/SKILL.md +169 -0
- package/.flow-comet/skills/flow-comet-requirement/SKILL.md +27 -0
- package/.flow-comet/skills/flow-comet-review/SKILL.md +204 -0
- package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +29 -0
- package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +214 -0
- package/.flow-comet/skills/flow-comet-task/SKILL.md +33 -0
- package/.flow-comet/skills/flow-comet-test/SKILL.md +41 -0
- package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +26 -0
- package/.flow-comet/skills/flow-comet-verify/SKILL.md +164 -0
- package/LICENSE +21 -0
- package/README.md +255 -0
- package/package.json +27 -0
- package/scripts/dsh-bridge.mjs +461 -0
- package/scripts/prepare-env.mjs +1526 -0
|
@@ -0,0 +1,4017 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// system-test.mjs — flow-comet 系统测试集(与 guard-self-test 同构的载体,测试内容为系统级全链路)
|
|
3
|
+
//
|
|
4
|
+
// 定位:guard-self-test 是引擎脚本的单元/场景级回归(fixture 构造为主;场景数以该套件运行输出的 ALL <n> SCENARIOS PASSED 为准);
|
|
5
|
+
// 本套件是**系统级**测试——每个测试项走真实命令序列(init → record → guard exit → handoff →
|
|
6
|
+
// hook …),覆盖 flow-comet 全部机制面(A~L 十二类):
|
|
7
|
+
// A. 状态机与路由(init/status/next/select/advance/record/execution-mode/config/
|
|
8
|
+
// 多趟真实链路:串并交替 NODE 行多次交替 + plan 出口依赖环负例与恢复指引)
|
|
9
|
+
// B. 声明机制(skill-load 标记/record 校验/时间序/损坏 fail-closed/委托范围豁免/exit 协议标记/旧兼容)
|
|
10
|
+
// C. 委托链路(handoff request/result/status、RED 先于 GREEN、Return Contract、证据键名契约)
|
|
11
|
+
// D. hook 写白名单(open 阶段/execute 动态收窄/direct 放宽/归档阶段)
|
|
12
|
+
// E. 自定义协议(--protocol/env 加载、自定义节点声明机制与出口、内置特化校验不误触发)
|
|
13
|
+
// F. 自动初始化(缺失提示/跳过记忆/新鲜静默/生成协作全链路)
|
|
14
|
+
// G. 分支模式(init 建分支/归档入口分支校验/一致性失配警告)
|
|
15
|
+
// H. verify 与归档(验证命令真实执行 + 超时配置/完整归档流程/归档路径声明标记查找)
|
|
16
|
+
// I. 异常路径(损坏状态/缺工件出口/非法参数/状态字段类型非法)
|
|
17
|
+
// J. 文档一致性(双语健康检查/公开产物零代号检查——调用仓库本地工具)
|
|
18
|
+
// K. 安装器与平台(版本标识/多平台安装与平台化路径/codex hook JSON 契约/平台选择链/
|
|
19
|
+
// purge 语义/描述符驱动/dsh 平台断言/loader 版本戳重装断言/hook 注入形态无关断言与旧条目幂等升级/
|
|
20
|
+
// 旧布局状态迁移后状态可读与流程可继续/分发面包体边界·可发布性清单与 bin 双入口(权威源真跑·
|
|
21
|
+
// 副本回传显式「不适用」结果标记)与 init 词元红线)
|
|
22
|
+
// L. 执行遗漏防护(entry 进入证据/空退出豁免/空仓库提示)
|
|
23
|
+
//
|
|
24
|
+
// 载体(与 guard-self-test 同构):每项 = 独立临时目录(fs.mkdtemp)+ 内置协议副本复制到
|
|
25
|
+
// <tmp>/reference/(受保护路径要求协议文件位于 runRoot 内)+ spawnSync 真实命令序列 +
|
|
26
|
+
// 断言退出码与输出关键词。测试项跑完 rmSync 清理。
|
|
27
|
+
//
|
|
28
|
+
// 输出纪律:逐项 PASS/FAIL + 汇总(SYSTEM TEST: N/M passed);全过 exit 0,有 FAIL exit 1。
|
|
29
|
+
// 结果标记:分发面项(distributionSurface)必须回传机器可读标记,运行器按结构化环境判据断言——
|
|
30
|
+
// 权威源侧 `distribution:executed@source` / 安装副本侧 `distribution:not-applicable@copy`;
|
|
31
|
+
// 缺标记(静默跳过)或与环境不符(判定写反)都判失败(「未验证 ≠ 通过」)。
|
|
32
|
+
// 测试项命名与输出为公开面——零过程代号(场景编号/修复编号/批次/缺陷编号/未公开概念)。
|
|
33
|
+
//
|
|
34
|
+
// 运行: node .flow-comet/skills/flow-comet/scripts/system-test.mjs
|
|
35
|
+
// 仅 node 内置模块(child_process/fs/os/path);无网络。
|
|
36
|
+
|
|
37
|
+
import { execFileSync, spawnSync } from 'child_process';
|
|
38
|
+
import fs from 'fs';
|
|
39
|
+
import os from 'os';
|
|
40
|
+
import path from 'path';
|
|
41
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
42
|
+
|
|
43
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
44
|
+
const GUARD = path.join(__dirname, 'workflow-guard.mjs');
|
|
45
|
+
const STATE = path.join(__dirname, 'workflow-state.mjs');
|
|
46
|
+
const HOOK = path.join(__dirname, 'comet-hook-guard.mjs');
|
|
47
|
+
const HANDOFF = path.join(__dirname, 'workflow-handoff.mjs');
|
|
48
|
+
// 内置协议源文件(脚本所在技能包 reference/):复制到 <tmp>/reference/ 内(受保护路径要求 runRoot 内)
|
|
49
|
+
const BUILTIN_PROTOCOL_SOURCE = path.join(__dirname, '..', 'reference', 'workflow-protocol.json');
|
|
50
|
+
const CHANGE_ID = 'ch';
|
|
51
|
+
|
|
52
|
+
let passed = 0;
|
|
53
|
+
const failures = [];
|
|
54
|
+
const createdDirs = [];
|
|
55
|
+
|
|
56
|
+
// ---------- 工具函数(与 guard-self-test 同构) ----------
|
|
57
|
+
|
|
58
|
+
function makeTmp() {
|
|
59
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'flow-comet-system-test-'));
|
|
60
|
+
createdDirs.push(dir);
|
|
61
|
+
// 内置协议副本——真实项目协议位于 <项目根>/reference/workflow-protocol.json(runRoot 内);
|
|
62
|
+
// readProtocolFile 受保护路径要求协议文件在 runRoot 内,故复制进场景目录
|
|
63
|
+
const builtinCopy = path.join(dir, 'reference', 'workflow-protocol.json');
|
|
64
|
+
fs.mkdirSync(path.dirname(builtinCopy), { recursive: true });
|
|
65
|
+
fs.copyFileSync(BUILTIN_PROTOCOL_SOURCE, builtinCopy);
|
|
66
|
+
return dir;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function writeFile(root, rel, content) {
|
|
70
|
+
const p = path.join(root, rel);
|
|
71
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
72
|
+
fs.writeFileSync(p, content, 'utf8');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function writeState(root, state) {
|
|
76
|
+
writeFile(root, path.posix.join('.flow-comet', 'flow-comet-state.json'), JSON.stringify(state, null, 2) + '\n');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 跑 workflow-state.mjs:runRoot = process.cwd()(spawn cwd=临时目录);
|
|
80
|
+
// FLOW_COMET_PROTOCOL 默认指向场景内协议副本(envOverrides 可覆盖——自定义协议场景)
|
|
81
|
+
function runState(args, root, envOverrides = {}) {
|
|
82
|
+
const res = spawnSync(process.execPath, [STATE, ...args], {
|
|
83
|
+
cwd: root,
|
|
84
|
+
env: {
|
|
85
|
+
...process.env,
|
|
86
|
+
FLOW_COMET_RUN_ROOT: root,
|
|
87
|
+
FLOW_COMET_PROTOCOL: path.join(root, 'reference', 'workflow-protocol.json'),
|
|
88
|
+
...envOverrides,
|
|
89
|
+
},
|
|
90
|
+
encoding: 'utf8',
|
|
91
|
+
timeout: 60000,
|
|
92
|
+
});
|
|
93
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 跑 workflow-guard.mjs:FLOW_COMET_RUN_ROOT=临时目录;同时捕获 stdout+stderr(WARN/BLOCKED 走 stderr)
|
|
97
|
+
function runGuard(args, root, envOverrides = {}) {
|
|
98
|
+
const res = spawnSync(process.execPath, [GUARD, ...args], {
|
|
99
|
+
cwd: root,
|
|
100
|
+
env: {
|
|
101
|
+
...process.env,
|
|
102
|
+
FLOW_COMET_RUN_ROOT: root,
|
|
103
|
+
FLOW_COMET_PROTOCOL: path.join(root, 'reference', 'workflow-protocol.json'),
|
|
104
|
+
...envOverrides,
|
|
105
|
+
},
|
|
106
|
+
encoding: 'utf8',
|
|
107
|
+
timeout: 60000,
|
|
108
|
+
});
|
|
109
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 跑 workflow-handoff.mjs:runRoot = process.cwd()
|
|
113
|
+
function runHandoff(args, root, envOverrides = {}) {
|
|
114
|
+
const res = spawnSync(process.execPath, [HANDOFF, ...args], {
|
|
115
|
+
cwd: root,
|
|
116
|
+
env: { ...process.env, FLOW_COMET_RUN_ROOT: root, ...envOverrides },
|
|
117
|
+
encoding: 'utf8',
|
|
118
|
+
timeout: 60000,
|
|
119
|
+
});
|
|
120
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 跑 comet-hook-guard.mjs:PreToolUse 事件从 stdin 传 JSON({ tool_name, tool_input: { file_path } })
|
|
124
|
+
function runHook(args, root, input, envOverrides = {}) {
|
|
125
|
+
const res = spawnSync(process.execPath, [HOOK, ...args], {
|
|
126
|
+
cwd: root,
|
|
127
|
+
input: input === undefined ? '' : JSON.stringify(input),
|
|
128
|
+
env: {
|
|
129
|
+
...process.env,
|
|
130
|
+
FLOW_COMET_RUN_ROOT: root,
|
|
131
|
+
FLOW_COMET_PROTOCOL: path.join(root, 'reference', 'workflow-protocol.json'),
|
|
132
|
+
...envOverrides,
|
|
133
|
+
},
|
|
134
|
+
encoding: 'utf8',
|
|
135
|
+
timeout: 60000,
|
|
136
|
+
});
|
|
137
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function assertExit(res, expected) {
|
|
141
|
+
if (res.status !== expected) {
|
|
142
|
+
throw new Error('期望 exit ' + expected + ',实际 exit ' + res.status + '\n实际输出:\n' + res.output);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function assertOut(res, keyword) {
|
|
147
|
+
if (!res.output.includes(keyword)) {
|
|
148
|
+
throw new Error('输出缺少关键词 ' + JSON.stringify(keyword) + '(exit ' + res.status + ')\n实际输出:\n' + res.output);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function assertNotOut(res, keyword) {
|
|
153
|
+
if (res.output.includes(keyword)) {
|
|
154
|
+
throw new Error('输出不应包含关键词 ' + JSON.stringify(keyword) + '(exit ' + res.status + ')\n实际输出:\n' + res.output);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 读取 state 文件(测试断言用)
|
|
159
|
+
function readStateFile(root) {
|
|
160
|
+
return JSON.parse(fs.readFileSync(path.join(root, '.flow-comet', 'flow-comet-state.json'), 'utf8'));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// git 工具:临时目录内初始化仓库(含初始 commit——unborn HEAD 下 init 不会建分支)
|
|
164
|
+
function gitInit(root) {
|
|
165
|
+
execFileSync('git', ['init', '-q'], { cwd: root, stdio: 'ignore' });
|
|
166
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '--allow-empty', '-m', 'init'], { cwd: root, stdio: 'ignore' });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function gitBranch(root) {
|
|
170
|
+
return execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// ---------- 伪造材料 ----------
|
|
174
|
+
|
|
175
|
+
function baseState(node) {
|
|
176
|
+
return {
|
|
177
|
+
activeChange: CHANGE_ID,
|
|
178
|
+
currentNode: node,
|
|
179
|
+
completedNodes: [],
|
|
180
|
+
evidence: {},
|
|
181
|
+
verifyFailures: 0,
|
|
182
|
+
executionMode: 'subagent',
|
|
183
|
+
directOverride: false,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// open 阶段工件(CHANGE 含 Why;REQUIREMENT 含 用户故事 + 验收准则——通过出口校验的完整形态)
|
|
188
|
+
function writeIntakeArtifacts(root, changeId = CHANGE_ID) {
|
|
189
|
+
writeFile(root, '.specs/' + changeId + '/CHANGE.md', '# CHANGE\n\n- **Change ID**: ' + changeId + '\n\n## Why(为什么做)\n\n变更目标。\n');
|
|
190
|
+
writeFile(root, '.specs/' + changeId + '/REQUIREMENT.md', '# REQUIREMENT\n\n- **Change ID**: ' + changeId + '\n\n## 用户故事(User Story)\n\n用户需求。\n\n## 验收准则(AC)\n\n- 通过标准\n');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// 自定义协议(compose-demo):3 节点 brainstorm/tdd/codereview(避开内置 8 节点 id)——
|
|
194
|
+
// 协议数据化路由 + 通用层防线对自定义节点生效;无 writeWhitelist(hook 回退内置缺省表)
|
|
195
|
+
function customProtocol() {
|
|
196
|
+
return {
|
|
197
|
+
schemaVersion: 1,
|
|
198
|
+
kind: 'workflow-kernel',
|
|
199
|
+
name: 'compose-demo',
|
|
200
|
+
goal: '系统测试自定义协议:数据化路由 + 声明机制 + 通用层防线。',
|
|
201
|
+
nodes: [
|
|
202
|
+
{
|
|
203
|
+
id: 'brainstorm',
|
|
204
|
+
label: 'Brainstorm',
|
|
205
|
+
kind: 'control',
|
|
206
|
+
responsibility: '产出 notes.md。',
|
|
207
|
+
outputSchemas: ['compose.notes.v1'],
|
|
208
|
+
requiredSkillCalls: [],
|
|
209
|
+
augmentations: [],
|
|
210
|
+
disabled: false,
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
id: 'tdd',
|
|
214
|
+
label: 'TDD',
|
|
215
|
+
kind: 'control',
|
|
216
|
+
responsibility: '产出 *-SUMMARY.md。',
|
|
217
|
+
outputSchemas: ['compose.tdd.v1'],
|
|
218
|
+
requiredSkillCalls: [],
|
|
219
|
+
augmentations: [],
|
|
220
|
+
disabled: false,
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
id: 'codereview',
|
|
224
|
+
label: 'Code Review',
|
|
225
|
+
kind: 'control',
|
|
226
|
+
responsibility: '产出 verdict.md。',
|
|
227
|
+
outputSchemas: ['compose.verdict.v1'],
|
|
228
|
+
requiredSkillCalls: [],
|
|
229
|
+
augmentations: [],
|
|
230
|
+
disabled: false,
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
outputSchemas: [
|
|
234
|
+
{
|
|
235
|
+
id: 'compose.notes.v1',
|
|
236
|
+
description: 'notes.md',
|
|
237
|
+
artifacts: [
|
|
238
|
+
{
|
|
239
|
+
id: 'notes',
|
|
240
|
+
kind: 'file',
|
|
241
|
+
required: true,
|
|
242
|
+
paths: ['<change-id>/notes.md'],
|
|
243
|
+
pathBase: 'specs-root',
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
evidence: [{ id: 'notes-summary', required: true }],
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
id: 'compose.tdd.v1',
|
|
250
|
+
description: '*-SUMMARY.md',
|
|
251
|
+
artifacts: [
|
|
252
|
+
{
|
|
253
|
+
id: 'summaries',
|
|
254
|
+
kind: 'file',
|
|
255
|
+
required: true,
|
|
256
|
+
paths: ['<change-id>/*-SUMMARY.md'],
|
|
257
|
+
pathBase: 'specs-root',
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
evidence: [{ id: 'tdd-summary', required: true }],
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
id: 'compose.verdict.v1',
|
|
264
|
+
description: 'verdict.md',
|
|
265
|
+
artifacts: [
|
|
266
|
+
{
|
|
267
|
+
id: 'verdict',
|
|
268
|
+
kind: 'file',
|
|
269
|
+
required: true,
|
|
270
|
+
paths: ['<change-id>/verdict.md'],
|
|
271
|
+
pathBase: 'specs-root',
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
evidence: [{ id: 'verdict-summary', required: true }],
|
|
275
|
+
},
|
|
276
|
+
],
|
|
277
|
+
state: {
|
|
278
|
+
kind: 'workflow-run',
|
|
279
|
+
statePath: '.flow-comet/flow-comet-state.json',
|
|
280
|
+
currentNodeField: 'currentNode',
|
|
281
|
+
completedNodesField: 'completedNodes',
|
|
282
|
+
evidenceField: 'evidence',
|
|
283
|
+
},
|
|
284
|
+
edges: [],
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 含 requiredSkillCalls 的变体:brainstorm 带 main scope 绑定(协调者加载 → 需 skill-load 声明标记)
|
|
289
|
+
function customProtocolWithSkillCall() {
|
|
290
|
+
const p = customProtocol();
|
|
291
|
+
p.nodes[0].requiredSkillCalls = [{ skill: 'flow-comet-brainstorm', scope: 'main' }];
|
|
292
|
+
return p;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// 写入自定义协议文件,返回绝对路径
|
|
296
|
+
function writeCustomProtocol(dir, withSkillCall = false) {
|
|
297
|
+
const proto = withSkillCall ? customProtocolWithSkillCall() : customProtocol();
|
|
298
|
+
const file = path.join(dir, withSkillCall ? 'custom-protocol-skill.json' : 'custom-protocol.json');
|
|
299
|
+
fs.writeFileSync(file, JSON.stringify(proto, null, 2) + '\n', 'utf8');
|
|
300
|
+
return file;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// CONTEXT 7 段完整模板(通过校验的形态)
|
|
304
|
+
const CONTEXT_FULL =
|
|
305
|
+
'# CONTEXT\n## 项目概要\nx\n## 技术栈\nx\n## 域语言\n| 术语 | 定义 |\n|---|---|\n| 例 | 定义 |\n' +
|
|
306
|
+
'## 已锁决策\n- [2026-08-01] 决策一\n## 默认偏好\nx\n## 既有抽象索引\nx\n' +
|
|
307
|
+
'## intel-scan 元数据\n- **last_intel_scan**: x\n- **scanner**: x\n- **下次重扫建议**: x\n';
|
|
308
|
+
|
|
309
|
+
// handoff 完整 Return Contract(W1-D 严格校验通过形态:commitHash + greenEvidence + completedChecks)
|
|
310
|
+
function fullContract(commitHash, taskId) {
|
|
311
|
+
return JSON.stringify({
|
|
312
|
+
status: 'DONE',
|
|
313
|
+
taskId,
|
|
314
|
+
commitHash,
|
|
315
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
316
|
+
greenEvidence: { command: 'node --check src/' + taskId.toLowerCase() + '.mjs', output: 'ok' },
|
|
317
|
+
redEvidence: { command: 'node --check src/' + taskId.toLowerCase() + '.mjs' },
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// 仓库本地验证工具执行(仅主仓存在、不随克隆分发;副本上文件缺失自动跳过):
|
|
322
|
+
// 返回 { status, output }(spawnSync 原始结果 output 是数组,统一拼接成字符串供断言)
|
|
323
|
+
function runLocalTool(checker, repoRoot) {
|
|
324
|
+
const res = spawnSync(process.execPath, [checker], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
325
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
326
|
+
}
|
|
327
|
+
// 递归收集目录下相对文件清单(排序稳定,供 K8/K10 技能整树清单/内容比对)
|
|
328
|
+
function collectTreeFiles(root) {
|
|
329
|
+
const files = [];
|
|
330
|
+
if (!fs.existsSync(root)) return files;
|
|
331
|
+
(function walk(dir) {
|
|
332
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
333
|
+
const absolute = path.join(dir, entry.name);
|
|
334
|
+
if (entry.isDirectory()) walk(absolute);
|
|
335
|
+
else if (entry.isFile()) files.push(path.relative(root, absolute));
|
|
336
|
+
}
|
|
337
|
+
})(root);
|
|
338
|
+
return files.sort();
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ---------- 分发面包体断言助手(npm 包形态:环境判据 / 结果标记 / 可发布性清单 / 包体边界 / bin 双入口) ----------
|
|
342
|
+
|
|
343
|
+
// 环境判据(结构化事实,不做路径字符串猜测):本套件在权威源与安装副本内都会运行,
|
|
344
|
+
// 而包体质检只有权威源侧可判——判据落空时输出显式「不适用」行并计通过,禁止静默跳过。
|
|
345
|
+
// 权威源侧 = 脚本上溯 4 层(包根)**同时**满足:存在 `name === 'flow-comet'` 的 package.json
|
|
346
|
+
// **且** `.flow-comet/skills/flow-comet` 权威树在位;
|
|
347
|
+
// 安装副本侧 = <target>/.claude|.agents|.dsh/skills/flow-comet/scripts 上溯 4 层 = 目标项目根
|
|
348
|
+
// → 判「不适用」(判定写反会让每个已安装项目假红)。
|
|
349
|
+
// 两个条件是**并列必要**的:只看 manifest 名字会把「恰好同名的消费项目」(依赖里带同名包、
|
|
350
|
+
// 或项目自身就叫 flow-comet 但无权威树)判成权威源——于是包体质检在该项目里跑 npm pack 而不是
|
|
351
|
+
// 输出「不适用」(判据过宽的另一失败模式)。权威树在场时 manifest 缺失/不可解析/name 不符
|
|
352
|
+
// → 必须失败(判定过宽会让权威源侧的包体断言静默漏检)。
|
|
353
|
+
// root 可覆盖(默认真实包根)——仅为可测性接缝:使上述四种形态能在夹具目录上被逐条驱动
|
|
354
|
+
// (否则判据的判别力只能靠人工实验验证,回归时可能被改宽而套件仍全绿)。生产调用不传该参数。
|
|
355
|
+
function resolvePackageContext(root = path.resolve(__dirname, '..', '..', '..', '..')) {
|
|
356
|
+
const authoritative = fs.existsSync(path.join(root, '.flow-comet', 'skills', 'flow-comet'));
|
|
357
|
+
const notApplicable = (reason) => ({ kind: 'copy', reason: reason || '根目录无本包权威源(name=flow-comet 的 package.json + .flow-comet/skills/flow-comet 权威树)' });
|
|
358
|
+
const manifest = path.join(root, 'package.json');
|
|
359
|
+
if (!fs.existsSync(manifest)) {
|
|
360
|
+
if (authoritative) throw new Error('权威源包根缺少 package.json: ' + manifest);
|
|
361
|
+
return notApplicable();
|
|
362
|
+
}
|
|
363
|
+
let pkg = null;
|
|
364
|
+
try {
|
|
365
|
+
pkg = JSON.parse(fs.readFileSync(manifest, 'utf8'));
|
|
366
|
+
} catch (e) {
|
|
367
|
+
if (authoritative) throw new Error('权威源 package.json 不可解析: ' + e.message);
|
|
368
|
+
return notApplicable();
|
|
369
|
+
}
|
|
370
|
+
if (!pkg || pkg.name !== 'flow-comet') {
|
|
371
|
+
if (authoritative) throw new Error('权威源 package.json 的 name 应为 flow-comet,实际 ' + JSON.stringify(pkg && pkg.name));
|
|
372
|
+
return notApplicable();
|
|
373
|
+
}
|
|
374
|
+
if (!authoritative) {
|
|
375
|
+
return notApplicable('根目录有 name=flow-comet 的 package.json 但权威树(.flow-comet/skills/flow-comet)不在位——同名不构成权威源,按安装副本判定');
|
|
376
|
+
}
|
|
377
|
+
return { kind: 'source', root, pkg };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// 环境判据的判别力锚(正反两向逐形态驱动):判据写宽(manifest 名字相符即认权威源)会让同名
|
|
381
|
+
// 消费项目被当成权威源;写反/写严则两侧互串(副本侧假红 / 权威源侧静默漏检)。夹具形态:
|
|
382
|
+
// ① 权威树不在 + 同名 manifest → copy(同名 ≠ 权威源)
|
|
383
|
+
// ② 权威树不在 + 无 manifest → copy
|
|
384
|
+
// ③ 权威树在场 + 同名 manifest → source
|
|
385
|
+
// ④ 权威树在场 + 无 manifest / manifest 不可解析 / name 不符 → 抛错(既有行为保持,不得放宽)
|
|
386
|
+
// 夹具建在调用方给定的临时目录内(随该项临时目录一并清理,不留残留)。
|
|
387
|
+
function assertPackageContextJudgment(scratch) {
|
|
388
|
+
const fixture = (name, options) => {
|
|
389
|
+
const root = path.join(scratch, name);
|
|
390
|
+
const tree = path.join(root, '.flow-comet', 'skills', 'flow-comet');
|
|
391
|
+
if (options.tree) fs.mkdirSync(tree, { recursive: true });
|
|
392
|
+
else fs.mkdirSync(root, { recursive: true });
|
|
393
|
+
if (options.manifest !== undefined) fs.writeFileSync(path.join(root, 'package.json'), options.manifest, 'utf8');
|
|
394
|
+
return root;
|
|
395
|
+
};
|
|
396
|
+
const expectKind = (root, expected, label) => {
|
|
397
|
+
const ctx = resolvePackageContext(root);
|
|
398
|
+
if (ctx.kind !== expected) {
|
|
399
|
+
throw new Error('环境判据失配(' + label + '):期望 ' + expected + ',实际 ' + ctx.kind);
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
const expectThrow = (root, label) => {
|
|
403
|
+
let threw = null;
|
|
404
|
+
try { resolvePackageContext(root); } catch (e) { threw = e; }
|
|
405
|
+
if (!threw) {
|
|
406
|
+
throw new Error('环境判据失配(' + label + '):权威树在场而 manifest 畸形时应显式失败,实际未抛错');
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
expectKind(fixture('same-name-consumer', { tree: false, manifest: '{"name":"flow-comet"}' }), 'copy', '权威树不在 + 同名 manifest');
|
|
410
|
+
expectKind(fixture('plain-consumer', { tree: false, manifest: undefined }), 'copy', '权威树不在 + 无 manifest');
|
|
411
|
+
expectKind(fixture('authoritative', { tree: true, manifest: '{"name":"flow-comet"}' }), 'source', '权威树在场 + 同名 manifest');
|
|
412
|
+
expectThrow(fixture('authoritative-no-manifest', { tree: true, manifest: undefined }), '权威树在场 + 无 manifest');
|
|
413
|
+
expectThrow(fixture('authoritative-broken', { tree: true, manifest: '{ not json' }), '权威树在场 + manifest 不可解析');
|
|
414
|
+
expectThrow(fixture('authoritative-other-name', { tree: true, manifest: '{"name":"other"}' }), '权威树在场 + name 不符');
|
|
415
|
+
console.log(' 环境判据: 副本侧同名/无名两形态判「不适用」,权威源侧判「权威源」,畸形 manifest 三形态显式失败 ✓');
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// 分发面结果标记(机器可读):本套件在权威源与安装副本两侧都会运行,而断言只在权威源侧真跑——
|
|
419
|
+
// 两侧的区别(「已执行」/「不适用」)必须留在运行器可捕获的结果里,不能只打 console 行靠人工核验。
|
|
420
|
+
// 映射两行即两侧的正反锚点:权威源侧回传 executed、安装副本侧回传 not-applicable,写反则该侧判失败。
|
|
421
|
+
const DISTRIBUTION_MARKERS = {
|
|
422
|
+
source: 'distribution:executed@source',
|
|
423
|
+
copy: 'distribution:not-applicable@copy',
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
// 运行器侧标记契约校验(分发面项):标记必须显式在场且与结构化环境判据推出的期望值一致。
|
|
427
|
+
// 缺标记 = 该项走了静默跳过(「未验证 ≠ 通过」,与显式「不适用」是两回事)→ 失败;
|
|
428
|
+
// 标记与环境不符 = 环境判据写反(副本侧假红 / 权威源侧静默漏检)→ 失败。
|
|
429
|
+
function assertDistributionOutcome(itemName, marker) {
|
|
430
|
+
const legal = Object.values(DISTRIBUTION_MARKERS);
|
|
431
|
+
if (!legal.includes(marker)) {
|
|
432
|
+
throw new Error('分发面结果标记缺席或非法(应回传 ' + legal.join(' 或 ') + ',实际 ' + JSON.stringify(marker)
|
|
433
|
+
+ ')——禁止静默跳过');
|
|
434
|
+
}
|
|
435
|
+
const expected = DISTRIBUTION_MARKERS[resolvePackageContext().kind];
|
|
436
|
+
if (marker !== expected) {
|
|
437
|
+
throw new Error('分发面结果标记与环境判据不符:本环境应 ' + expected + ',实际 ' + marker
|
|
438
|
+
+ '(判定写反会让该侧静默漏检或假红)');
|
|
439
|
+
}
|
|
440
|
+
console.log('OUTCOME: ' + itemName + ' → ' + marker);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// 跑 `npm pack --dry-run --json`(只读,不产出 tarball)并解析出产物相对路径清单。
|
|
444
|
+
// 命令以字符串形态经 shell 执行(与 workflow-guard verify 命令执行同形态):Windows 上 npm 是
|
|
445
|
+
// npm.cmd(CreateProcess 不解析 .cmd 后缀,且 Node 对 .cmd/.bat 直接 spawn 属 EINVAL)——
|
|
446
|
+
// 字符串 + shell 是全平台唯一稳妥形态。解析失败即报错(不做文本兜底:静默回退会掩盖 npm 形态漂移)。
|
|
447
|
+
function runNpmPackDryRun(root) {
|
|
448
|
+
const res = spawnSync('npm pack --dry-run --json', {
|
|
449
|
+
cwd: root,
|
|
450
|
+
encoding: 'utf8',
|
|
451
|
+
timeout: 120000,
|
|
452
|
+
shell: true,
|
|
453
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
454
|
+
});
|
|
455
|
+
const status = res.status ?? 1;
|
|
456
|
+
const stdout = String(res.stdout || '');
|
|
457
|
+
const stderr = String(res.stderr || '');
|
|
458
|
+
if (status !== 0) {
|
|
459
|
+
throw new Error('npm pack --dry-run 退出码 ' + status + '(npm 缺失或环境异常)\nstdout:\n' + stdout + '\nstderr:\n' + stderr);
|
|
460
|
+
}
|
|
461
|
+
let parsed = null;
|
|
462
|
+
try {
|
|
463
|
+
parsed = JSON.parse(stdout);
|
|
464
|
+
} catch (e) {
|
|
465
|
+
throw new Error('npm pack --dry-run --json 输出不可解析: ' + e.message + '\nstdout(前 500 字):\n' + stdout.slice(0, 500) + '\nstderr:\n' + stderr);
|
|
466
|
+
}
|
|
467
|
+
const entry = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
468
|
+
const files = entry && Array.isArray(entry.files) ? entry.files.map((f) => String(f.path).replaceAll('\\', '/')) : null;
|
|
469
|
+
if (!files || files.length === 0) throw new Error('npm pack --dry-run 产物清单为空(--json 未列出任何文件)');
|
|
470
|
+
return files;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// ① 清单可发布性:`private` 在场即 npm 拒绝发布——被误加回会让包不可发布,而包体边界断言
|
|
474
|
+
// (按效果覆盖 files/version)与 CI 版本对账都察觉不到,故独立成锚。允许缺席或显式非真值。
|
|
475
|
+
// 同时守住公开身份与运行时前提(license / engines.node):这些元数据随版本固化在发布物上,
|
|
476
|
+
// 发布后才察觉漏声明只能等下一个版本补救,故与 private 同点做在场锚定。
|
|
477
|
+
function assertPublishableManifest(pkg) {
|
|
478
|
+
if (pkg.private) {
|
|
479
|
+
throw new Error('package.json 的 private 应缺席(或显式非真值),实际 ' + JSON.stringify(pkg.private)
|
|
480
|
+
+ '——置真值会让 npm 拒绝发布');
|
|
481
|
+
}
|
|
482
|
+
if (pkg.license !== 'MIT') {
|
|
483
|
+
throw new Error('package.json 的 license 应为 MIT(与仓库 LICENSE 文件一致),实际 '
|
|
484
|
+
+ JSON.stringify(pkg.license) + '——包被 npm 打进发布物时该字段随版本固化');
|
|
485
|
+
}
|
|
486
|
+
const nodeFloor = pkg.engines ? pkg.engines.node : undefined;
|
|
487
|
+
if (nodeFloor !== '>=18') {
|
|
488
|
+
throw new Error('package.json 的 engines.node 应为 >=18(文档与 CI 声明的运行时前提),实际 '
|
|
489
|
+
+ JSON.stringify(nodeFloor) + '(engines 整段缺失时为 undefined)');
|
|
490
|
+
}
|
|
491
|
+
console.log(' 清单可发布性: private 缺席 / license=' + pkg.license + ' / engines.node=' + nodeFloor + ' ✓');
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// ② 包体边界:必含面在场 / 私有面零命中 / scripts 面精确等于两项 / 顶层白名单 fail-closed /
|
|
495
|
+
// 权威源技能树逐文件在场(漏收 = 安装期炸或平台静默退化——协议 JSON 也在内)。
|
|
496
|
+
function assertPackageBoundary(root) {
|
|
497
|
+
const files = runNpmPackDryRun(root);
|
|
498
|
+
const packedUnder = (prefix) => files.some((p) => p.startsWith(prefix + '/'));
|
|
499
|
+
const required = [
|
|
500
|
+
['.flow-comet/skills', () => packedUnder('.flow-comet/skills')],
|
|
501
|
+
['.flow-comet/rules/flow-comet-orchestration.md', () => files.includes('.flow-comet/rules/flow-comet-orchestration.md')],
|
|
502
|
+
['scripts/prepare-env.mjs', () => files.includes('scripts/prepare-env.mjs')],
|
|
503
|
+
['scripts/dsh-bridge.mjs', () => files.includes('scripts/dsh-bridge.mjs')],
|
|
504
|
+
];
|
|
505
|
+
for (const [label, ok] of required) {
|
|
506
|
+
if (!ok()) throw new Error('包体缺少必需路径: ' + label);
|
|
507
|
+
}
|
|
508
|
+
// 私有面零命中(提交钩子与本地词表 / 仓库工作笔记 / 流程工件 / 仓库级 AI 上下文与 CI 资产 / 演示图)
|
|
509
|
+
const forbidden = ['.githooks/', 'docs/internal/', '.specs/', 'CLAUDE.md', 'AGENTS.md', '.github/', 'images/'];
|
|
510
|
+
for (const prefix of forbidden) {
|
|
511
|
+
const hit = files.filter((p) => p === prefix.replace(/\/$/, '') || p.startsWith(prefix));
|
|
512
|
+
if (hit.length > 0) throw new Error('包体含私有面路径 ' + prefix + ': ' + hit.slice(0, 5).join(', '));
|
|
513
|
+
}
|
|
514
|
+
// 随包 scripts/ 面精确等于两项(本地工具脚本——提交钩子安装器/本地检查器一类——不得进包)
|
|
515
|
+
const allowedScripts = ['scripts/dsh-bridge.mjs', 'scripts/prepare-env.mjs'];
|
|
516
|
+
const extraScripts = files.filter((p) => p.startsWith('scripts/') && !allowedScripts.includes(p));
|
|
517
|
+
if (extraScripts.length > 0) {
|
|
518
|
+
throw new Error('包体含未纳管的 scripts/ 条目(本地工具脚本不得进包): ' + extraScripts.join(', '));
|
|
519
|
+
}
|
|
520
|
+
// 顶层条目仅允许已声明的两棵子树 + npm 恒定携带项(白名单 fail-closed:新增顶层目录默认不得进包)
|
|
521
|
+
const allowedTopLevel = ['.flow-comet', 'scripts', 'package.json', 'README.md', 'LICENSE'];
|
|
522
|
+
const unexpectedTop = [...new Set(files.map((p) => p.split('/')[0]))].filter((t) => !allowedTopLevel.includes(t));
|
|
523
|
+
if (unexpectedTop.length > 0) {
|
|
524
|
+
throw new Error('包体顶层出现白名单外条目(新增顶层目录默认不得进包): ' + unexpectedTop.join(', '));
|
|
525
|
+
}
|
|
526
|
+
// 仅忽略 npm 恒定排除的操作系统垃圾文件(跨平台检出物,非包体声明面)
|
|
527
|
+
const osJunk = ['.DS_Store', 'Thumbs.db', 'desktop.ini'];
|
|
528
|
+
const skillsOnDisk = collectTreeFiles(path.join(root, '.flow-comet', 'skills'))
|
|
529
|
+
.filter((p) => !osJunk.includes(path.basename(p)))
|
|
530
|
+
.map((p) => '.flow-comet/skills/' + p.replaceAll('\\', '/'));
|
|
531
|
+
const missingSkills = skillsOnDisk.filter((p) => !files.includes(p));
|
|
532
|
+
if (missingSkills.length > 0) throw new Error('包体漏收技能树文件: ' + missingSkills.slice(0, 5).join(', '));
|
|
533
|
+
console.log(' 包体边界: ' + files.length + ' 文件,技能树 ' + skillsOnDisk.length + ' 文件逐文件在场,私有面零命中 ✓');
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// ③ bin 双入口契约:两个命令名同指同一安装器(同一实现 = 行为一致的结构事实)。返回安装器绝对路径。
|
|
537
|
+
function assertBinContract(pkg, root) {
|
|
538
|
+
const binTarget = 'scripts/prepare-env.mjs';
|
|
539
|
+
const bin = pkg.bin && typeof pkg.bin === 'object' ? pkg.bin : null;
|
|
540
|
+
if (!bin) throw new Error('package.json 缺 bin 声明(双入口契约)');
|
|
541
|
+
for (const name of ['fcomet', 'flow-comet']) {
|
|
542
|
+
if (bin[name] !== binTarget) {
|
|
543
|
+
throw new Error('bin["' + name + '"] 应指向 ' + binTarget + ',实际 ' + JSON.stringify(bin[name]));
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
const installer = path.join(root, ...binTarget.split('/'));
|
|
547
|
+
if (!fs.existsSync(installer)) throw new Error('bin 目标文件不存在: ' + binTarget);
|
|
548
|
+
console.log(' bin 双入口: fcomet / flow-comet → ' + binTarget + '(同一实现)✓');
|
|
549
|
+
return installer;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// ④ init 词元形态与红线(负例常驻:变形/重复词元绝不被静默吞成默认安装)。cwd 用场景临时目录。
|
|
553
|
+
function assertInitTokenContract(installer, cwd) {
|
|
554
|
+
const runInstaller = (args) => {
|
|
555
|
+
const res = spawnSync(process.execPath, [installer, ...args], { cwd, encoding: 'utf8', timeout: 60000 });
|
|
556
|
+
return { status: res.status ?? 1, stdout: String(res.stdout || ''), stderr: String(res.stderr || '') };
|
|
557
|
+
};
|
|
558
|
+
const withInit = runInstaller(['init', '--help']);
|
|
559
|
+
if (withInit.status !== 0) {
|
|
560
|
+
throw new Error('`init` 词元形态应退 0(`fcomet init --help`),实际 ' + withInit.status + '\n' + withInit.stderr);
|
|
561
|
+
}
|
|
562
|
+
if (!withInit.stdout.includes('fcomet init')) throw new Error('用法输出未含 `fcomet init` 形态:\n' + withInit.stdout);
|
|
563
|
+
const bare = runInstaller(['--help']);
|
|
564
|
+
if (bare.status !== 0) throw new Error('裸形态(无 init 词元)`--help` 行为应不变(退 0),实际 ' + bare.status + '\n' + bare.stderr);
|
|
565
|
+
for (const bad of ['inti', 'int', 'install', 'init init']) {
|
|
566
|
+
const res = runInstaller(bad.split(' '));
|
|
567
|
+
if (res.status !== 1) throw new Error('变形/重复词元 `' + bad + '` 必须退 1,实际 ' + res.status + '\n' + res.stdout);
|
|
568
|
+
if (!res.stderr.includes('未知参数')) {
|
|
569
|
+
throw new Error('变形/重复词元 `' + bad + '` 的 stderr 应含「未知参数」,实际:\n' + res.stderr);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
console.log(' init 词元: `init --help` 退 0 且含 fcomet init / 裸形态行为不变 / 变形·重复词元 4 例退 1 且报未知参数 ✓');
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// ---------- 桥接 loader 版本戳重装断言助手(临时 DSH_HOME 重装链路) ----------
|
|
576
|
+
|
|
577
|
+
// 从权威源 loader 提取版本戳(与安装器/检查器同一锚点标记行;动态取值,版本演进零维护)
|
|
578
|
+
function readBridgeSourceVersion(loader) {
|
|
579
|
+
const text = fs.readFileSync(loader, 'utf8');
|
|
580
|
+
const m = /^\/\/ BRIDGE_VERSION: (\S+)$/m.exec(text);
|
|
581
|
+
if (!m) throw new Error('权威源 loader 缺版本戳标记行(契约锚点): ' + loader);
|
|
582
|
+
return m[1];
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// 构造指定版本戳的 loader 副本内容(仅替换标记行,其余逐字保持——MISS 检测:替换未命中即报错)
|
|
586
|
+
function stampBridgeLoader(sourceLoader, version) {
|
|
587
|
+
const text = fs.readFileSync(sourceLoader, 'utf8');
|
|
588
|
+
const replaced = text.replace(/^\/\/ BRIDGE_VERSION: \S+$/m, '// BRIDGE_VERSION: ' + version);
|
|
589
|
+
if (!replaced.includes('// BRIDGE_VERSION: ' + version)) {
|
|
590
|
+
throw new Error('版本戳替换未命中(MISS): 目标版本 ' + version);
|
|
591
|
+
}
|
|
592
|
+
return replaced;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// 预置目标项目 flow-kit/ 为同名非上游目录(安装器走「非上游克隆,已跳过」路径——
|
|
596
|
+
// 网络零接触;与安装器冒烟同手法)
|
|
597
|
+
function seedForeignFlowKit(root) {
|
|
598
|
+
const flowKitDir = path.join(root, 'flow-kit');
|
|
599
|
+
fs.mkdirSync(flowKitDir, { recursive: true });
|
|
600
|
+
execFileSync('git', ['init', '-q'], { cwd: flowKitDir, stdio: 'ignore' });
|
|
601
|
+
execFileSync('git', ['remote', 'add', 'origin', 'https://example.com/not-flow-kit.git'], { cwd: flowKitDir, stdio: 'ignore' });
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// ---------- hook 注入命令形态判定(托管条目断言放宽决策:断言意图 = 托管条目在场且指向本项目脚本) ----------
|
|
605
|
+
// 托管识别 = 脚本 basename(comet-hook-guard.mjs) 在位——与 prepare-env isManagedHookCommand
|
|
606
|
+
// 同源语义,新旧托管形态均命中(幂等升级识别面)。
|
|
607
|
+
// 项目根引用特征 = CLAUDE_PROJECT_DIR 变量引用(%VAR%/$env:/$_ENV/${VAR} 词形)或绝对路径特征
|
|
608
|
+
// (盘符 / 引号·空白·等号后的 POSIX 绝对段)二选一在位——cwd 漂移免疫的形态面;相对路径旧形态
|
|
609
|
+
// 两者皆无 → 不达标(升级替换判据)。断言不锁定具体引号/变量写法(形态无关化,以特征断言补偿
|
|
610
|
+
// 精度——注入命令实测微调不再破坏套件)。
|
|
611
|
+
function isManagedHookCommand(cmd) {
|
|
612
|
+
return typeof cmd === 'string' && cmd.includes('comet-hook-guard.mjs');
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function hasProjectRootRef(cmd) {
|
|
616
|
+
if (typeof cmd !== 'string') return false;
|
|
617
|
+
if (/CLAUDE_PROJECT_DIR/i.test(cmd)) return true;
|
|
618
|
+
return /[A-Za-z]:[\\/]/.test(cmd) || /["'\s=]\/[^\s"']/.test(cmd);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function assertManagedHookEntry(cmd, label) {
|
|
622
|
+
if (!isManagedHookCommand(cmd)) {
|
|
623
|
+
throw new Error(label + ' 缺托管脚本 basename(comet-hook-guard.mjs): ' + String(cmd));
|
|
624
|
+
}
|
|
625
|
+
if (!hasProjectRootRef(cmd)) {
|
|
626
|
+
throw new Error(label + ' 缺项目根引用特征(CLAUDE_PROJECT_DIR 或绝对路径二选一在位;相对路径旧形态不具备 cwd 漂移免疫): ' + String(cmd));
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// 从 settings.local.json / hooks.json 结构提取全部托管 hook 命令(hooks.PreToolUse[].hooks[].command)
|
|
631
|
+
function collectManagedHookCommands(configObj) {
|
|
632
|
+
const cmds = [];
|
|
633
|
+
const groups = configObj && configObj.hooks && Array.isArray(configObj.hooks.PreToolUse) ? configObj.hooks.PreToolUse : [];
|
|
634
|
+
for (const g of groups) {
|
|
635
|
+
for (const h of (Array.isArray(g && g.hooks) ? g.hooks : [])) {
|
|
636
|
+
if (isManagedHookCommand(h && h.command)) cmds.push(h.command);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
return cmds;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// 多趟链路 SUMMARY 夹具(SUMMARY 模板保真全量满足:标题首行 # SUMMARY: + 首部 4 字段 +
|
|
643
|
+
// 段序 做了什么→改动文件→verify 输出→6 维自查→越界检查→自检方法;6 维声明 brooks-review)
|
|
644
|
+
function execSummaryFixture(taskId) {
|
|
645
|
+
return [
|
|
646
|
+
'# SUMMARY: ' + taskId + ' - 多趟链路任务',
|
|
647
|
+
'',
|
|
648
|
+
'- **Change ID**: ' + CHANGE_ID,
|
|
649
|
+
'- **Task ID**: ' + taskId,
|
|
650
|
+
'- **完成时间**: 2026-08-25 10:00',
|
|
651
|
+
'- **AI 角色**: Dev',
|
|
652
|
+
'',
|
|
653
|
+
'## 做了什么',
|
|
654
|
+
'',
|
|
655
|
+
'完成 ' + taskId + ' 的实现并回传证据。',
|
|
656
|
+
'',
|
|
657
|
+
'## 改动文件',
|
|
658
|
+
'',
|
|
659
|
+
'| 文件 | 性质 | 说明 |',
|
|
660
|
+
'|---|---|---|',
|
|
661
|
+
'| src/' + taskId.toLowerCase() + '.mjs | 修改 | 实现任务 |',
|
|
662
|
+
'',
|
|
663
|
+
'## verify 输出',
|
|
664
|
+
'',
|
|
665
|
+
'```text',
|
|
666
|
+
'$ node --check src/' + taskId.toLowerCase() + '.mjs',
|
|
667
|
+
'ok',
|
|
668
|
+
'```',
|
|
669
|
+
'',
|
|
670
|
+
'## 6 维自查',
|
|
671
|
+
'',
|
|
672
|
+
'- 功能: 通过(brooks-review 已跑)',
|
|
673
|
+
'- 性能: 无影响',
|
|
674
|
+
'- 安全: 无影响',
|
|
675
|
+
'- 兼容: 通过',
|
|
676
|
+
'- 可观测: 通过',
|
|
677
|
+
'- 可维护: 通过',
|
|
678
|
+
'',
|
|
679
|
+
'## 越界检查',
|
|
680
|
+
'',
|
|
681
|
+
'仅修改 write_files 范围内文件,无越界。',
|
|
682
|
+
'',
|
|
683
|
+
'## 自检方法',
|
|
684
|
+
'',
|
|
685
|
+
'brooks-review',
|
|
686
|
+
'',
|
|
687
|
+
].join('\n');
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// 混排多波 TASK:双并行开路 → 串行衔接(依赖双并行)→ 收尾并行(依赖串行衔接)。
|
|
691
|
+
// 各任务 status 由入参映射(任务 id → 'pending'|'done')控制;status 变更不影响任务集签名
|
|
692
|
+
// (签名仅保留 id/parallel 与块内容,剥离 status 类属性)。串行衔接置于两并行波之间——
|
|
693
|
+
// 引擎契约下 execute 出口要求全部串行已消化,跨趟多次交替由「并行波→串行波→并行波」承载。
|
|
694
|
+
function multiWaveTaskContent(statusMap) {
|
|
695
|
+
const blk = (id, parallel, deps) =>
|
|
696
|
+
'<task id="' + id + '"' + (parallel ? ' parallel="true"' : '') + (statusMap[id] === 'done' ? ' status="done"' : ' status="pending"') + '>' +
|
|
697
|
+
'<action>实现 ' + id + '</action>' +
|
|
698
|
+
'<write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
699
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify>' +
|
|
700
|
+
(deps ? '<depends_on>' + deps + '</depends_on>' : '') +
|
|
701
|
+
'</task>\n';
|
|
702
|
+
return '# TASK\n\n' +
|
|
703
|
+
blk('P01', true, '') +
|
|
704
|
+
blk('P02', true, '') +
|
|
705
|
+
blk('S01', false, 'P01,P02') +
|
|
706
|
+
blk('P03', true, 'S01');
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
// 真实链路前奏(多趟/依赖环用例共用):init → open 出口 → design 出口。
|
|
710
|
+
// 新 change 两层加载模型下逐节点 skill-load → record → entry → exit --apply 全程驱动。
|
|
711
|
+
function driveThroughDesign(dir) {
|
|
712
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
713
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
714
|
+
writeIntakeArtifacts(dir);
|
|
715
|
+
assertExit(runState(['record', 'open', '{"summary":"intake complete"}'], dir), 0);
|
|
716
|
+
assertExit(runGuard(['entry', 'open'], dir), 0);
|
|
717
|
+
assertExit(runGuard(['exit', 'open', '--apply'], dir), 0);
|
|
718
|
+
assertExit(runState(['skill-load', 'design', 'flow-comet-design', '--prompt', 'flow-kit/prompts/2-design.md'], dir), 0);
|
|
719
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/DESIGN.md',
|
|
720
|
+
'# DESIGN\n\n- **Change ID**: ' + CHANGE_ID + '\n\n## 0. 技术栈选定\n\nNode.js(ESM)\n\n## 决策清单\n\n- [ ] 循环路由\n');
|
|
721
|
+
assertExit(runState(['record', 'design', '{"summary":"design done"}'], dir), 0);
|
|
722
|
+
assertExit(runGuard(['entry', 'design'], dir), 0);
|
|
723
|
+
assertExit(runGuard(['exit', 'design', '--apply'], dir), 0);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// ---------- 系统测试项(A~L 十二类) ----------
|
|
727
|
+
|
|
728
|
+
const TEST_ITEMS = [
|
|
729
|
+
// ---------- A. 状态机与路由 ----------
|
|
730
|
+
|
|
731
|
+
{
|
|
732
|
+
name: 'A1 init:状态写入与工件目录创建',
|
|
733
|
+
run: (dir) => {
|
|
734
|
+
const res = runState(['init', CHANGE_ID], dir);
|
|
735
|
+
assertExit(res, 0);
|
|
736
|
+
assertOut(res, 'Initialized: ' + CHANGE_ID);
|
|
737
|
+
assertOut(res, 'NODE: open');
|
|
738
|
+
assertOut(res, 'BRANCH: none');
|
|
739
|
+
const st = readStateFile(dir);
|
|
740
|
+
if (st.activeChange !== CHANGE_ID) throw new Error('activeChange 应为 ' + CHANGE_ID);
|
|
741
|
+
if (st.currentNode !== 'open') throw new Error('currentNode 应为 open: ' + JSON.stringify(st.currentNode));
|
|
742
|
+
if (st.status !== 'running') throw new Error('status 应为 running');
|
|
743
|
+
if (st.branchMode !== false) throw new Error('非 git 仓库 branchMode 应为 false');
|
|
744
|
+
if (!fs.existsSync(path.join(dir, '.specs', CHANGE_ID))) throw new Error('.specs/<id>/ 目录未创建');
|
|
745
|
+
},
|
|
746
|
+
},
|
|
747
|
+
|
|
748
|
+
{
|
|
749
|
+
name: 'A2 init 分支模式:--branch-prefix 创建指定前缀分支',
|
|
750
|
+
run: (dir) => {
|
|
751
|
+
gitInit(dir);
|
|
752
|
+
const res = runState(['init', 'br-test', '--branch-prefix', 'feat/'], dir);
|
|
753
|
+
assertExit(res, 0);
|
|
754
|
+
assertOut(res, 'BRANCH: feat/br-test');
|
|
755
|
+
if (gitBranch(dir) !== 'feat/br-test') throw new Error('期望分支 feat/br-test,实际 ' + gitBranch(dir));
|
|
756
|
+
const st = readStateFile(dir);
|
|
757
|
+
if (st.branchPrefix !== 'feat/') throw new Error('branchPrefix 应为 feat/: ' + JSON.stringify(st.branchPrefix));
|
|
758
|
+
// 分支一致性:status 显示 ok
|
|
759
|
+
const status = runState(['status'], dir);
|
|
760
|
+
assertExit(status, 0);
|
|
761
|
+
assertOut(status, 'BRANCH: feat/br-test | 一致性: ok');
|
|
762
|
+
// 非法前缀拒绝
|
|
763
|
+
const bad = runState(['init', 'br-bad', '--branch-prefix', ''], dir);
|
|
764
|
+
assertExit(bad, 1);
|
|
765
|
+
assertOut(bad, '--branch-prefix requires a non-empty prefix');
|
|
766
|
+
},
|
|
767
|
+
},
|
|
768
|
+
|
|
769
|
+
{
|
|
770
|
+
name: 'A3 init 同 id 重跑:防护提示不阻断',
|
|
771
|
+
run: (dir) => {
|
|
772
|
+
writeFile(dir, 'package.json', '{"name":"x"}');
|
|
773
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
774
|
+
const res = runState(['init', CHANGE_ID], dir);
|
|
775
|
+
assertExit(res, 0);
|
|
776
|
+
assertOut(res, 'WARN: change ' + CHANGE_ID + ' 已存在');
|
|
777
|
+
assertOut(res, '重置节点状态');
|
|
778
|
+
},
|
|
779
|
+
},
|
|
780
|
+
|
|
781
|
+
{
|
|
782
|
+
name: 'A4 status:节点推导与无活跃兜底',
|
|
783
|
+
run: (dir) => {
|
|
784
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
785
|
+
// 无产物 → 首节点
|
|
786
|
+
const s1 = runState(['status'], dir);
|
|
787
|
+
assertExit(s1, 0);
|
|
788
|
+
assertOut(s1, '"currentNode": "open"');
|
|
789
|
+
// 上游工件齐 → 推导下一节点
|
|
790
|
+
writeIntakeArtifacts(dir);
|
|
791
|
+
const s2 = runState(['status'], dir);
|
|
792
|
+
assertExit(s2, 0);
|
|
793
|
+
assertOut(s2, '"currentNode": "design"');
|
|
794
|
+
// 无 state 无工件 → no-change 兜底
|
|
795
|
+
fs.rmSync(path.join(dir, '.flow-comet'), { recursive: true });
|
|
796
|
+
const s3 = runState(['status'], dir);
|
|
797
|
+
assertExit(s3, 0);
|
|
798
|
+
assertOut(s3, '"status": "no-change"');
|
|
799
|
+
},
|
|
800
|
+
},
|
|
801
|
+
|
|
802
|
+
{
|
|
803
|
+
name: 'A5 next:推进与状态漂移校正',
|
|
804
|
+
run: (dir) => {
|
|
805
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
806
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
807
|
+
writeIntakeArtifacts(dir);
|
|
808
|
+
assertExit(runState(['record', 'open', '{"summary":"intake complete"}'], dir), 0);
|
|
809
|
+
// 真实出口:exit open --apply(未激活声明机制 → 兼容警告 + 通过)
|
|
810
|
+
assertExit(runGuard(['entry', 'open'], dir), 0);
|
|
811
|
+
const exitRes = runGuard(['exit', 'open', '--apply'], dir);
|
|
812
|
+
assertExit(exitRes, 0);
|
|
813
|
+
assertOut(exitRes, 'ALL CHECKS PASSED');
|
|
814
|
+
let st = readStateFile(dir);
|
|
815
|
+
if (!st.completedNodes.includes('open')) throw new Error('completedNodes 应含 open: ' + JSON.stringify(st.completedNodes));
|
|
816
|
+
// 正常推进:next → design
|
|
817
|
+
const n1 = runState(['next'], dir);
|
|
818
|
+
assertExit(n1, 0);
|
|
819
|
+
assertOut(n1, 'NODE: design');
|
|
820
|
+
assertNotOut(n1, 'BLOCKED');
|
|
821
|
+
// 漂移校正:手工把 currentNode 改回 open(已完成节点)→ next 以文件推导为准校正
|
|
822
|
+
st = readStateFile(dir);
|
|
823
|
+
st.currentNode = 'open';
|
|
824
|
+
writeState(dir, st);
|
|
825
|
+
const n2 = runState(['next'], dir);
|
|
826
|
+
assertExit(n2, 0);
|
|
827
|
+
assertOut(n2, 'NODE: design');
|
|
828
|
+
const stAfter = readStateFile(dir);
|
|
829
|
+
if (stAfter.currentNode !== 'design') throw new Error('漂移应被校正为 design: ' + JSON.stringify(stAfter.currentNode));
|
|
830
|
+
},
|
|
831
|
+
},
|
|
832
|
+
|
|
833
|
+
{
|
|
834
|
+
name: 'A6 next:未出口节点严格拦截',
|
|
835
|
+
run: (dir) => {
|
|
836
|
+
writeState(dir, baseState('open'));
|
|
837
|
+
fs.mkdirSync(path.join(dir, '.specs', CHANGE_ID), { recursive: true });
|
|
838
|
+
const res = runState(['next'], dir);
|
|
839
|
+
assertExit(res, 1);
|
|
840
|
+
assertOut(res, 'BLOCKED: 疑似未 exit 节点 open');
|
|
841
|
+
assertOut(res, 'workflow-guard.mjs exit open --apply');
|
|
842
|
+
},
|
|
843
|
+
},
|
|
844
|
+
|
|
845
|
+
{
|
|
846
|
+
name: 'A7 select:切换项目与缺失拒绝',
|
|
847
|
+
run: (dir) => {
|
|
848
|
+
assertExit(runState(['init', 'sel-a'], dir), 0);
|
|
849
|
+
// 第二个活跃 change 目录(模拟多 change 并存)
|
|
850
|
+
writeFile(dir, '.specs/sel-b/CHANGE.md', '# CHANGE\n## Why\nx\n');
|
|
851
|
+
const res = runState(['select', 'sel-b'], dir);
|
|
852
|
+
assertExit(res, 0);
|
|
853
|
+
assertOut(res, 'Selected: sel-b');
|
|
854
|
+
const st = readStateFile(dir);
|
|
855
|
+
if (st.activeChange !== 'sel-b') throw new Error('activeChange 应为 sel-b: ' + JSON.stringify(st.activeChange));
|
|
856
|
+
// 不存在的 change → 拒绝
|
|
857
|
+
const missing = runState(['select', 'no-such'], dir);
|
|
858
|
+
assertExit(missing, 1);
|
|
859
|
+
assertOut(missing, 'Change not found');
|
|
860
|
+
},
|
|
861
|
+
},
|
|
862
|
+
|
|
863
|
+
{
|
|
864
|
+
name: 'A8 advance:节点推进',
|
|
865
|
+
run: (dir) => {
|
|
866
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
867
|
+
writeIntakeArtifacts(dir);
|
|
868
|
+
const res = runState(['advance'], dir);
|
|
869
|
+
assertExit(res, 0);
|
|
870
|
+
assertOut(res, 'Advanced to: design');
|
|
871
|
+
const st = readStateFile(dir);
|
|
872
|
+
if (!st.completedNodes.includes('open')) throw new Error('completedNodes 应含 open: ' + JSON.stringify(st.completedNodes));
|
|
873
|
+
if (st.currentNode !== 'design') throw new Error('currentNode 应为 design');
|
|
874
|
+
// 无活跃 change → 提示不推进
|
|
875
|
+
const st2 = readStateFile(dir);
|
|
876
|
+
st2.activeChange = null;
|
|
877
|
+
writeState(dir, st2);
|
|
878
|
+
const none = runState(['advance'], dir);
|
|
879
|
+
assertExit(none, 0);
|
|
880
|
+
assertOut(none, 'No active change. Use select first.');
|
|
881
|
+
},
|
|
882
|
+
},
|
|
883
|
+
|
|
884
|
+
{
|
|
885
|
+
name: 'A9 record:基础证据记录',
|
|
886
|
+
run: (dir) => {
|
|
887
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
888
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change'], dir), 0);
|
|
889
|
+
// JSON payload → evidence 写入 + next 提示
|
|
890
|
+
const res = runState(['record', 'open', '{"summary":"recorded"}'], dir);
|
|
891
|
+
assertExit(res, 0);
|
|
892
|
+
assertOut(res, 'EVIDENCE: open');
|
|
893
|
+
assertOut(res, 'NODE: open');
|
|
894
|
+
const st = readStateFile(dir);
|
|
895
|
+
if (!st.evidence.open || st.evidence.open.summary !== 'recorded') {
|
|
896
|
+
throw new Error('record 未写入 evidence: ' + JSON.stringify(st.evidence));
|
|
897
|
+
}
|
|
898
|
+
if (typeof st.evidence.open.recordedAt !== 'string') throw new Error('evidence 应附带 recordedAt');
|
|
899
|
+
// 非 JSON payload → 按 summary 字符串记录
|
|
900
|
+
assertExit(runState(['record', 'open', 'plain text'], dir), 0);
|
|
901
|
+
const st2 = readStateFile(dir);
|
|
902
|
+
if (st2.evidence.open.summary !== 'plain text') throw new Error('非 JSON payload 应按 summary 记录');
|
|
903
|
+
// --json-file:从文件读 JSON payload(规避 Windows PowerShell 引号剥离)
|
|
904
|
+
fs.writeFileSync(path.join(dir, 'payload.json'), '{"summary":"from-file"}', 'utf8');
|
|
905
|
+
const rf = runState(['record', 'open', '--json-file', 'payload.json'], dir);
|
|
906
|
+
assertExit(rf, 0);
|
|
907
|
+
const st3 = readStateFile(dir);
|
|
908
|
+
if (st3.evidence.open.summary !== 'from-file') throw new Error('--json-file payload 未生效');
|
|
909
|
+
// --json-file 越界路径(解析后出项目根)→ 拒绝(防读取任意文件进 evidence)
|
|
910
|
+
const outsideFile = path.join(dir, '..', 'outside.json');
|
|
911
|
+
fs.writeFileSync(outsideFile, '{"summary":"outside"}', 'utf8');
|
|
912
|
+
const rfBad = runState(['record', 'open', '--json-file', outsideFile], dir);
|
|
913
|
+
assertExit(rfBad, 1);
|
|
914
|
+
assertOut(rfBad, '必须在项目根内');
|
|
915
|
+
// --json-file 符号链接越界(realpath 校验):链接到项目根外的符号链接 → 拒绝。
|
|
916
|
+
// 平台限制:Windows 创建符号链接需开发者模式/管理员——创建失败时显式跳过并说明,
|
|
917
|
+
// 不静默(POSIX/CI 真实覆盖该负例)
|
|
918
|
+
const outsideTarget = path.join(dir, '..', 'outside-symlink-target.json');
|
|
919
|
+
fs.writeFileSync(outsideTarget, '{"summary":"outside"}', 'utf8');
|
|
920
|
+
const symlinkPath = path.join(dir, 'payload-link.json');
|
|
921
|
+
// 仅 symlink 创建包在 try 内——断言移出 catch,防断言失败被误报为平台跳过
|
|
922
|
+
// (修复前 try 包住 runState/断言,真实回归会被吞为"平台不支持跳过")
|
|
923
|
+
let symlinkCreated = false;
|
|
924
|
+
try {
|
|
925
|
+
fs.symlinkSync(outsideTarget, symlinkPath);
|
|
926
|
+
symlinkCreated = true;
|
|
927
|
+
} catch (e) {
|
|
928
|
+
console.log(' (symlink 负例跳过——当前平台不支持创建符号链接: ' + e.code + ')');
|
|
929
|
+
}
|
|
930
|
+
if (symlinkCreated) {
|
|
931
|
+
const rfSym = runState(['record', 'open', '--json-file', 'payload-link.json'], dir);
|
|
932
|
+
assertExit(rfSym, 1);
|
|
933
|
+
assertOut(rfSym, '符号链接');
|
|
934
|
+
}
|
|
935
|
+
// 缺 node 参数 → 拒绝
|
|
936
|
+
const noNode = runState(['record'], dir);
|
|
937
|
+
assertExit(noNode, 1);
|
|
938
|
+
assertOut(noNode, 'record requires a Node id.');
|
|
939
|
+
// --json-file 缺值(最后一个参数)→ 用法错误而非类型错误(修复前 path.resolve
|
|
940
|
+
// 对 undefined 抛 TypeError——报类型错误而非用法错误,此处应 RED)
|
|
941
|
+
const missingVal = runState(['record', 'open', '--json-file'], dir);
|
|
942
|
+
assertExit(missingVal, 1);
|
|
943
|
+
assertOut(missingVal, '--json-file requires a path argument');
|
|
944
|
+
if (missingVal.output.includes('ERR_INVALID_ARG_TYPE')) {
|
|
945
|
+
throw new Error('--json-file 缺值不应报类型错误');
|
|
946
|
+
}
|
|
947
|
+
// --json-file 空串(--json-file= 形式)→ 同样用法错误(修复前解析为项目根报 EISDIR)
|
|
948
|
+
const emptyVal = runState(['record', 'open', '--json-file='], dir);
|
|
949
|
+
assertExit(emptyVal, 1);
|
|
950
|
+
assertOut(emptyVal, '--json-file requires a path argument');
|
|
951
|
+
},
|
|
952
|
+
},
|
|
953
|
+
|
|
954
|
+
{
|
|
955
|
+
name: 'A10 execution-mode:direct 切换与记录',
|
|
956
|
+
run: (dir) => {
|
|
957
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
958
|
+
const d = runState(['execution-mode', 'direct'], dir);
|
|
959
|
+
assertExit(d, 0);
|
|
960
|
+
assertOut(d, 'EXECUTION-MODE: direct (directOverride)');
|
|
961
|
+
let st = readStateFile(dir);
|
|
962
|
+
if (st.executionMode !== 'direct' || st.directOverride !== true) {
|
|
963
|
+
throw new Error('direct 模式应记录 directOverride=true: ' + JSON.stringify({ executionMode: st.executionMode, directOverride: st.directOverride }));
|
|
964
|
+
}
|
|
965
|
+
const s = runState(['execution-mode', 'subagent'], dir);
|
|
966
|
+
assertExit(s, 0);
|
|
967
|
+
assertOut(s, 'EXECUTION-MODE: subagent');
|
|
968
|
+
st = readStateFile(dir);
|
|
969
|
+
if (st.executionMode !== 'subagent' || st.directOverride !== false) {
|
|
970
|
+
throw new Error('切回 subagent 应清除 directOverride: ' + JSON.stringify(st.directOverride));
|
|
971
|
+
}
|
|
972
|
+
// 非法参数拒绝
|
|
973
|
+
const bad = runState(['execution-mode', 'bogus'], dir);
|
|
974
|
+
assertExit(bad, 1);
|
|
975
|
+
assertOut(bad, 'BLOCKED: execution-mode 参数必须为 subagent 或 direct');
|
|
976
|
+
},
|
|
977
|
+
},
|
|
978
|
+
|
|
979
|
+
{
|
|
980
|
+
name: 'A11 config:配置设置与非法值拒绝',
|
|
981
|
+
run: (dir) => {
|
|
982
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
983
|
+
const ok = runState(['config', 'set', 'enablePrReview', 'true'], dir);
|
|
984
|
+
assertExit(ok, 0);
|
|
985
|
+
assertOut(ok, 'CONFIG: enablePrReview = true');
|
|
986
|
+
const st = readStateFile(dir);
|
|
987
|
+
if (st.enablePrReview !== true) throw new Error('enablePrReview 应为 true');
|
|
988
|
+
// branchMode 只读拒绝
|
|
989
|
+
const branch = runState(['config', 'set', 'branchMode', 'true'], dir);
|
|
990
|
+
assertExit(branch, 1);
|
|
991
|
+
assertOut(branch, 'BLOCKED: branchMode 由 init 自动判定');
|
|
992
|
+
// 值非法拒绝
|
|
993
|
+
const bad = runState(['config', 'set', 'enablePrReview', 'maybe'], dir);
|
|
994
|
+
assertExit(bad, 1);
|
|
995
|
+
assertOut(bad, 'BLOCKED: config set 值非法');
|
|
996
|
+
// 未知键拒绝
|
|
997
|
+
const unknown = runState(['config', 'set', 'foo', 'bar'], dir);
|
|
998
|
+
assertExit(unknown, 1);
|
|
999
|
+
assertOut(unknown, 'BLOCKED: 未知配置键');
|
|
1000
|
+
},
|
|
1001
|
+
},
|
|
1002
|
+
|
|
1003
|
+
{
|
|
1004
|
+
name: 'A12 verifyFailures:切换 change 后计数独立(按 change 存储)',
|
|
1005
|
+
run: (dir) => {
|
|
1006
|
+
// ① init ch-a + verify-fail 两次(2/3,未超限)
|
|
1007
|
+
assertExit(runState(['init', 'ch-a', '--init-skip'], dir), 0);
|
|
1008
|
+
assertExit(runState(['verify-fail'], dir), 0);
|
|
1009
|
+
const r2 = runState(['verify-fail'], dir);
|
|
1010
|
+
assertExit(r2, 0);
|
|
1011
|
+
assertOut(r2, 'VERIFY-FAIL: 2/3');
|
|
1012
|
+
// ② 切换到 ch-b(目录需存在)→ verify-fail 从 1 起——按 change 存储后计数独立
|
|
1013
|
+
// (若实现仍用全局计数 2,此处输出 3/3——断言 1/3 即 RED)
|
|
1014
|
+
writeFile(dir, '.specs/ch-b/CHANGE.md', '# CHANGE\n## Why\nx\n');
|
|
1015
|
+
assertExit(runState(['select', 'ch-b'], dir), 0);
|
|
1016
|
+
const r3 = runState(['verify-fail'], dir);
|
|
1017
|
+
assertExit(r3, 0);
|
|
1018
|
+
assertOut(r3, 'VERIFY-FAIL: 1/3');
|
|
1019
|
+
// ③ ch-b 独立计数:连续 3 次后第 4 次 BLOCK
|
|
1020
|
+
assertExit(runState(['verify-fail'], dir), 0);
|
|
1021
|
+
assertExit(runState(['verify-fail'], dir), 0);
|
|
1022
|
+
const r7 = runState(['verify-fail'], dir);
|
|
1023
|
+
assertExit(r7, 1);
|
|
1024
|
+
assertOut(r7, '超限');
|
|
1025
|
+
// ④ 切回 ch-a:原计数保留(2 → 可再失败 1 次,第 4 次 BLOCK;不串扰不回零)
|
|
1026
|
+
assertExit(runState(['select', 'ch-a'], dir), 0);
|
|
1027
|
+
const r8 = runState(['verify-fail'], dir);
|
|
1028
|
+
assertExit(r8, 0);
|
|
1029
|
+
assertOut(r8, 'VERIFY-FAIL: 3/3');
|
|
1030
|
+
const r9 = runState(['verify-fail'], dir);
|
|
1031
|
+
assertExit(r9, 1);
|
|
1032
|
+
assertOut(r9, '超限');
|
|
1033
|
+
},
|
|
1034
|
+
},
|
|
1035
|
+
|
|
1036
|
+
{
|
|
1037
|
+
name: 'A13 多趟路由真实链路:并→串→并交替与多波依赖,NODE 行在 execute/subagent-execute 间多次交替直至全部 done',
|
|
1038
|
+
run: (dir) => {
|
|
1039
|
+
driveThroughDesign(dir);
|
|
1040
|
+
// 混排 TASK:双并行开路 → 串行衔接(依赖双并行)→ 收尾并行(依赖串行衔接)——依赖驱动的
|
|
1041
|
+
// 分趟序列:并行趟 → 串行趟 → 并行趟 → review 门控(多趟正向行为)。
|
|
1042
|
+
// 驱动遵循真实协调者流程:execute 趟内先消化其全部串行 pending(标 done 并委托留证)再 exit;
|
|
1043
|
+
// 委托趟内委派全部可并行后才能退(每趟合取完成判定)。
|
|
1044
|
+
// 对未实现循环路由的引擎本用例 RED 属预期(单趟限制下委托节点完成后不再路由回)。
|
|
1045
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md',
|
|
1046
|
+
multiWaveTaskContent({ P01: 'pending', P02: 'pending', S01: 'pending', P03: 'pending' }));
|
|
1047
|
+
assertExit(runState(['skill-load', 'plan', 'flow-comet-plan', '--prompt', 'flow-kit/prompts/3-task.md'], dir), 0);
|
|
1048
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan done"}'], dir), 0);
|
|
1049
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1050
|
+
const rPlan = runGuard(['exit', 'plan', '--apply'], dir);
|
|
1051
|
+
assertExit(rPlan, 0);
|
|
1052
|
+
assertOut(rPlan, 'ALL CHECKS PASSED');
|
|
1053
|
+
// 首趟路由:首波双并行无前置依赖 → 直接进入委托节点
|
|
1054
|
+
assertOut(rPlan, 'NODE: subagent-execute');
|
|
1055
|
+
// 两层加载模型:执行域两节点的手动声明标记(record 前置门;一次声明后续趟次复用)
|
|
1056
|
+
assertExit(runState(['skill-load', 'execute', 'flow-comet-dev', '--prompt', 'flow-kit/prompts/4-dev.md'], dir), 0);
|
|
1057
|
+
assertExit(runState(['skill-load', 'subagent-execute', 'flow-comet-dev', '--prompt', 'flow-kit/prompts/4-dev.md'], dir), 0);
|
|
1058
|
+
|
|
1059
|
+
// 单趟完成动作:TASK.md 标 done(status 变更不影响任务集签名)+ SUMMARY 夹具 + 真实 handoff 委托回传
|
|
1060
|
+
const completeTasks = (ids) => {
|
|
1061
|
+
for (const id of ids) {
|
|
1062
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/' + id + '-SUMMARY.md', execSummaryFixture(id));
|
|
1063
|
+
assertExit(runHandoff(['request', id, id + ' 委托', '--write-files', 'src/' + id.toLowerCase() + '.mjs'], dir), 0);
|
|
1064
|
+
assertExit(runHandoff(['result', id, fullContract('abcd1234abcd1234abcd1234abcd1234abcd1234', id)], dir), 0);
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
const markDone = (statusMap) => writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', multiWaveTaskContent(statusMap));
|
|
1068
|
+
// 单趟驱动:record → entry → exit --apply,断言 NODE 行路由到期望节点(可观测性不变式)
|
|
1069
|
+
const drivePass = (node, expectNext, label) => {
|
|
1070
|
+
assertExit(runState(['record', node, '{"summary":"' + label + '"}'], dir), 0);
|
|
1071
|
+
assertExit(runGuard(['entry', node], dir), 0);
|
|
1072
|
+
const res = runGuard(['exit', node, '--apply'], dir);
|
|
1073
|
+
assertExit(res, 0);
|
|
1074
|
+
assertOut(res, 'ALL CHECKS PASSED');
|
|
1075
|
+
const m = res.output.match(/^NODE: ([a-z-]+)\s*$/m);
|
|
1076
|
+
if (!m) throw new Error('exit ' + node + '(' + label + ')输出缺 NODE 行:\n' + res.output);
|
|
1077
|
+
if (m[1] !== expectNext) {
|
|
1078
|
+
throw new Error(label + ' 后路由期望 NODE: ' + expectNext + ',实际 NODE: ' + m[1] +
|
|
1079
|
+
'(多趟循环路由未生效——委托节点应可重入直至无剩余可并行)\n实际输出:\n' + res.output);
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
|
|
1083
|
+
// 趟 1 · subagent-execute:首波双并行委托完成 → 趟间回串行(串行衔接依赖此时已满足)
|
|
1084
|
+
markDone({ P01: 'done', P02: 'done', S01: 'pending', P03: 'pending' });
|
|
1085
|
+
completeTasks(['P01', 'P02']);
|
|
1086
|
+
drivePass('subagent-execute', 'execute', 'parallel-wave-1');
|
|
1087
|
+
|
|
1088
|
+
// 趟 2 · execute:先消化本趟全部串行 pending(串行衔接,标 done 并委托留证)——
|
|
1089
|
+
// 确认无串行残留后才 exit → 收尾并行依赖满足 → 再次进入委托节点
|
|
1090
|
+
// (旧引擎单趟限制下此处不再路由回——本断言即多趟语义回归锚)
|
|
1091
|
+
markDone({ P01: 'done', P02: 'done', S01: 'done', P03: 'pending' });
|
|
1092
|
+
completeTasks(['S01']);
|
|
1093
|
+
drivePass('execute', 'subagent-execute', 'serial-wave-1');
|
|
1094
|
+
|
|
1095
|
+
// 趟 3 · subagent-execute:收尾并行委托完成 → 全部 done → 出口照常门控进 review(循环终止语义不变)
|
|
1096
|
+
markDone({ P01: 'done', P02: 'done', S01: 'done', P03: 'done' });
|
|
1097
|
+
completeTasks(['P03']);
|
|
1098
|
+
drivePass('subagent-execute', 'review', 'parallel-wave-2');
|
|
1099
|
+
|
|
1100
|
+
const st = readStateFile(dir);
|
|
1101
|
+
if (st.currentNode !== 'review') {
|
|
1102
|
+
throw new Error('全部任务 done 后应停在 review 门控前: ' + JSON.stringify(st.currentNode));
|
|
1103
|
+
}
|
|
1104
|
+
console.log(' 多趟路由:subagent-execute→execute→subagent-execute→review 三趟两交替 ✓');
|
|
1105
|
+
},
|
|
1106
|
+
},
|
|
1107
|
+
|
|
1108
|
+
{
|
|
1109
|
+
name: 'A14 plan 出口依赖环负例:BLOCK 含 depends_on 恢复指引,按指引调环后恢复通过',
|
|
1110
|
+
run: (dir) => {
|
|
1111
|
+
driveThroughDesign(dir);
|
|
1112
|
+
// 负例:X1↔X2 互相依赖成环(结构死锁——任何一趟都无法开工)→ plan 出口前置拦截。
|
|
1113
|
+
// 校验语义:依赖图无环 + 趟次可满足;BLOCK 消息附 depends_on 调整恢复指引(恢复类断言面)。
|
|
1114
|
+
const cyclic =
|
|
1115
|
+
'<task id="C1" status="pending"><action>实现 C1</action><write_files>src/c1.mjs</write_files><verify>node --check src/c1.mjs</verify></task>\n' +
|
|
1116
|
+
'<task id="X1" parallel="true" status="pending"><action>实现 X1</action><write_files>src/x1.mjs</write_files><verify>node --check src/x1.mjs</verify><depends_on>X2</depends_on></task>\n' +
|
|
1117
|
+
'<task id="X2" parallel="true" status="pending"><action>实现 X2</action><write_files>src/x2.mjs</write_files><verify>node --check src/x2.mjs</verify><depends_on>X1</depends_on></task>\n';
|
|
1118
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' + cyclic);
|
|
1119
|
+
assertExit(runState(['skill-load', 'plan', 'flow-comet-plan', '--prompt', 'flow-kit/prompts/3-task.md'], dir), 0);
|
|
1120
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan drafted"}'], dir), 0);
|
|
1121
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1122
|
+
const blocked = runGuard(['exit', 'plan', '--apply'], dir);
|
|
1123
|
+
assertExit(blocked, 1);
|
|
1124
|
+
assertOut(blocked, 'BLOCKED');
|
|
1125
|
+
// 恢复指引关键词:指明调整 depends_on(而非笼统报错)
|
|
1126
|
+
assertOut(blocked, 'depends_on');
|
|
1127
|
+
if (!/(环|循环|cycle|拓扑|topolog)/i.test(blocked.output)) {
|
|
1128
|
+
throw new Error('plan 出口 BLOCK 应含依赖环/拓扑校验语义关键词(环/循环/cycle/拓扑):\n' + blocked.output);
|
|
1129
|
+
}
|
|
1130
|
+
// 恢复类:按指引调整 depends_on(X1/X2 改依赖 C1)消环 → 同一出口重跑通过
|
|
1131
|
+
const recovered =
|
|
1132
|
+
'<task id="C1" status="pending"><action>实现 C1</action><write_files>src/c1.mjs</write_files><verify>node --check src/c1.mjs</verify></task>\n' +
|
|
1133
|
+
'<task id="X1" parallel="true" status="pending"><action>实现 X1</action><write_files>src/x1.mjs</write_files><verify>node --check src/x1.mjs</verify><depends_on>C1</depends_on></task>\n' +
|
|
1134
|
+
'<task id="X2" parallel="true" status="pending"><action>实现 X2</action><write_files>src/x2.mjs</write_files><verify>node --check src/x2.mjs</verify><depends_on>C1</depends_on></task>\n';
|
|
1135
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' + recovered);
|
|
1136
|
+
const ok = runGuard(['exit', 'plan', '--apply'], dir);
|
|
1137
|
+
assertExit(ok, 0);
|
|
1138
|
+
assertOut(ok, 'ALL CHECKS PASSED');
|
|
1139
|
+
// 恢复后路由:并行依赖未满足(C1 仍 pending)→ execute 开工
|
|
1140
|
+
assertOut(ok, 'NODE: execute');
|
|
1141
|
+
},
|
|
1142
|
+
},
|
|
1143
|
+
|
|
1144
|
+
{
|
|
1145
|
+
// 路由时序收敛(真实命令链路):多趟拓扑(并行开路 → 串行衔接 → 并行收尾 → 串行收尾)逐节点
|
|
1146
|
+
// guard exit --apply 后运行 workflow-state next——断言两侧 NODE 逐节点一致(修复前 guard 出口
|
|
1147
|
+
// 镜像缺「串行 pending → execute」回流,并行收尾后直接落到 review,与权威 next 偏差)。
|
|
1148
|
+
name: 'A15 多趟路由时序收敛:逐节点 exit --apply 后 next NODE 与 guard 出口 NODE 一致',
|
|
1149
|
+
run: (dir) => {
|
|
1150
|
+
const env = { FLOW_COMET_PROTOCOL: path.join(dir, 'reference', 'workflow-protocol.json') };
|
|
1151
|
+
const writeTask = (statusMap) => {
|
|
1152
|
+
const blk = (id, parallel, deps) =>
|
|
1153
|
+
'<task id="' + id + '"' + (parallel ? ' parallel="true"' : '') +
|
|
1154
|
+
' status="' + (statusMap[id] === 'done' ? 'done' : 'pending') + '">' +
|
|
1155
|
+
'<action>实现 ' + id + '</action><write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
1156
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify>' +
|
|
1157
|
+
(deps ? '<depends_on>' + deps + '</depends_on>' : '') + '</task>\n';
|
|
1158
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' +
|
|
1159
|
+
blk('P01', true, '') + blk('P02', true, '') +
|
|
1160
|
+
blk('S01', false, 'P01,P02') +
|
|
1161
|
+
blk('P03', true, 'S01') + blk('P04', true, 'S01') +
|
|
1162
|
+
blk('S02', false, 'P03,P04'));
|
|
1163
|
+
};
|
|
1164
|
+
const runNext = () => runState(['next'], dir, env);
|
|
1165
|
+
const nodeOf = (out) => (out.match(/^NODE: ([a-z-]+)$/m) || [])[1] ?? null;
|
|
1166
|
+
const completeTasks = (ids) => {
|
|
1167
|
+
for (const id of ids) {
|
|
1168
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/' + id + '-SUMMARY.md', execSummaryFixture(id));
|
|
1169
|
+
assertExit(runHandoff(['request', id, id + ' 委托', '--write-files', 'src/' + id.toLowerCase() + '.mjs'], dir), 0);
|
|
1170
|
+
assertExit(runHandoff(['result', id, fullContract('abcd1234abcd1234abcd1234abcd1234abcd1234', id)], dir), 0);
|
|
1171
|
+
}
|
|
1172
|
+
};
|
|
1173
|
+
// drive 收敛面参数化:委托跳转/趟间回串行态(guard 出口 NODE 与 next 可确认面不一致——
|
|
1174
|
+
// next 对「currentNode 无 evidence 且非固定路由后继」的委托跳转态按正常推进豁免不放行,
|
|
1175
|
+
// 仅断言 guard 侧 NODE;直接后继/回流态(next 可确认)才做双侧收敛断言。
|
|
1176
|
+
const drive = (nodeId, skill, prompt, prepare, { expectedNode, converge = true } = {}) => {
|
|
1177
|
+
assertExit(runState(['skill-load', nodeId, skill, '--prompt', prompt], dir, env), 0);
|
|
1178
|
+
prepare();
|
|
1179
|
+
assertExit(runGuard(['entry', nodeId], dir), 0);
|
|
1180
|
+
const res = runGuard(['exit', nodeId, '--apply'], dir);
|
|
1181
|
+
assertExit(res, 0);
|
|
1182
|
+
const n1 = nodeOf(res.output);
|
|
1183
|
+
if (expectedNode && n1 !== expectedNode) {
|
|
1184
|
+
throw new Error('节点 ' + nodeId + ' 出口后 guard NODE(' + n1 + ') != 期望(' + expectedNode +
|
|
1185
|
+
')\nguard:\n' + res.output);
|
|
1186
|
+
}
|
|
1187
|
+
if (!converge) return;
|
|
1188
|
+
const nxt = runNext();
|
|
1189
|
+
assertExit(nxt, 0);
|
|
1190
|
+
const n2 = nodeOf(nxt.output);
|
|
1191
|
+
if (n1 !== n2) {
|
|
1192
|
+
throw new Error('节点 ' + nodeId + ' 出口后 guard NODE(' + n1 + ') != next NODE(' + n2 +
|
|
1193
|
+
')\nguard:\n' + res.output + '\nnext:\n' + nxt.output);
|
|
1194
|
+
}
|
|
1195
|
+
};
|
|
1196
|
+
const writeSummary = (ids) => { for (const id of ids) writeFile(dir, '.specs/' + CHANGE_ID + '/' + id + '-SUMMARY.md', execSummaryFixture(id)); };
|
|
1197
|
+
driveThroughDesign(dir);
|
|
1198
|
+
// 预置多趟 TASK 并推进到 review 门控前(逐节点收敛断言)
|
|
1199
|
+
writeTask({ P01: 'pending', P02: 'pending', S01: 'pending', P03: 'pending', P04: 'pending', S02: 'pending' });
|
|
1200
|
+
drive('plan', 'flow-comet-plan', 'flow-kit/prompts/3-task.md', () => assertExit(runState(['record', 'plan', '{"summary":"plan complete"}'], dir, env), 0),
|
|
1201
|
+
{ expectedNode: 'subagent-execute', converge: false });
|
|
1202
|
+
drive('subagent-execute', 'flow-comet-subagent-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1203
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'pending', P03: 'pending', P04: 'pending', S02: 'pending' });
|
|
1204
|
+
writeSummary(['P01', 'P02']);
|
|
1205
|
+
completeTasks(['P01', 'P02']);
|
|
1206
|
+
// 委托收集后按真实链路 record 节点总结(浅合并保留 handoffResult)——guard exit
|
|
1207
|
+
// 的 schema evidence 前置需要非空 summary(缺则 BLOCK,与场景级委托出口同形态)。
|
|
1208
|
+
assertExit(runState(['record', 'subagent-execute', '{"summary":"wave 1 delegated and collected"}'], dir, env), 0);
|
|
1209
|
+
}, { expectedNode: 'execute', converge: false });
|
|
1210
|
+
drive('execute', 'flow-comet-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1211
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'done', P03: 'pending', P04: 'pending', S02: 'pending' });
|
|
1212
|
+
writeSummary(['S01']);
|
|
1213
|
+
completeTasks(['S01']);
|
|
1214
|
+
// execute 节点自身证据(guard exit 校验节点 evidence 须有记录)——真实链路中
|
|
1215
|
+
// 串行任务完成后协调者 record execute 节点总结。
|
|
1216
|
+
assertExit(runState(['record', 'execute', '{"summary":"serial wave complete"}'], dir, env), 0);
|
|
1217
|
+
}, { expectedNode: 'subagent-execute', converge: true });
|
|
1218
|
+
drive('subagent-execute', 'flow-comet-subagent-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1219
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'done', P03: 'done', P04: 'done', S02: 'pending' });
|
|
1220
|
+
writeSummary(['P03', 'P04']);
|
|
1221
|
+
completeTasks(['P03', 'P04']);
|
|
1222
|
+
assertExit(runState(['record', 'subagent-execute', '{"summary":"wave 2 delegated and collected"}'], dir, env), 0);
|
|
1223
|
+
}, { expectedNode: 'execute', converge: true });
|
|
1224
|
+
drive('execute', 'flow-comet-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1225
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'done', P03: 'done', P04: 'done', S02: 'done' });
|
|
1226
|
+
writeSummary(['S02']);
|
|
1227
|
+
completeTasks(['S02']);
|
|
1228
|
+
assertExit(runState(['record', 'execute', '{"summary":"final serial wave complete"}'], dir, env), 0);
|
|
1229
|
+
}, { expectedNode: 'review', converge: true });
|
|
1230
|
+
drive('review', 'flow-comet-review', 'flow-kit/prompts/6-review.md', () => {
|
|
1231
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/REVIEW.md',
|
|
1232
|
+
'# REVIEW\n\n## 发现\n\n### Critical\n\n- 无\n\n### Major\n\n- 无\n\n### Minor\n\n- 无\n\n## 结论\n\nreview passed,无遗留问题。\n');
|
|
1233
|
+
assertExit(runState(['record', 'review', '{"summary":"review complete"}'], dir, env), 0);
|
|
1234
|
+
}, { expectedNode: 'verify', converge: true });
|
|
1235
|
+
drive('verify', 'flow-comet-verify', 'flow-kit/prompts/7-integration.md', () => {
|
|
1236
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TEST.md', '# TEST\n\n## 验证命令\n\n```\necho ok\n```\n');
|
|
1237
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/UAT.md', '# UAT\n\n## 验收\n\n- 通过\n');
|
|
1238
|
+
assertExit(runState(['record', 'verify', '{"summary":"verify complete"}'], dir, env), 0);
|
|
1239
|
+
}, { expectedNode: 'archive', converge: true });
|
|
1240
|
+
// 归档出口:两侧均 NEXT: done
|
|
1241
|
+
assertExit(runState(['skill-load', 'archive', 'flow-comet-archive', '--prompt', 'flow-kit/prompts/7-integration.md'], dir, env), 0);
|
|
1242
|
+
writeFile(dir, '.specs/archive/2026-08-28-' + CHANGE_ID + '/KNOWN-ISSUES.md', '# 遗留问题\n\n无。\n');
|
|
1243
|
+
assertExit(runState(['record', 'archive', '{"summary":"archive complete"}'], dir, env), 0);
|
|
1244
|
+
assertExit(runGuard(['entry', 'archive'], dir), 0);
|
|
1245
|
+
const res = runGuard(['exit', 'archive', '--apply'], dir);
|
|
1246
|
+
assertExit(res, 0);
|
|
1247
|
+
assertOut(res, 'NEXT: done');
|
|
1248
|
+
const nxt = runNext();
|
|
1249
|
+
assertExit(nxt, 0);
|
|
1250
|
+
assertOut(nxt, 'NEXT: done');
|
|
1251
|
+
},
|
|
1252
|
+
},
|
|
1253
|
+
|
|
1254
|
+
{
|
|
1255
|
+
// 并行文件依赖检出(真实命令链路):write∩write 重叠强判前移 plan 出口——同波次并行任务
|
|
1256
|
+
// 写同一路径(链式重命名的 .txt 事故面)→ 新 change BLOCKED(含重叠路径与 depends_on
|
|
1257
|
+
// 恢复指引);按指引补显式 depends_on 后同出口恢复通过。read∩write 弱判:无显式关联并行对
|
|
1258
|
+
// 一方读取对方写路径 → WARN 提示不 BLOCK(渐进)。修复前 plan 出口无此检测 = 预期 RED。
|
|
1259
|
+
name: 'A16 plan 出口文件依赖检出:写写重叠 BLOCK 与恢复 + 读写弱判 WARN(真实命令)',
|
|
1260
|
+
run: (dir) => {
|
|
1261
|
+
driveThroughDesign(dir);
|
|
1262
|
+
const planEnv = { FLOW_COMET_PROTOCOL: path.join(dir, 'reference', 'workflow-protocol.json') };
|
|
1263
|
+
// 非 --apply 出口(弱判/强判阶段不推进状态,避免 apply 后 currentNode 偏离 plan)
|
|
1264
|
+
const exitPlanNoApply = () => runGuard(['exit', 'plan'], dir, planEnv);
|
|
1265
|
+
const exitPlanApply = () => runGuard(['exit', 'plan', '--apply'], dir, planEnv);
|
|
1266
|
+
assertExit(runState(['skill-load', 'plan', 'flow-comet-plan', '--prompt', 'flow-kit/prompts/3-task.md'], dir), 0);
|
|
1267
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan drafted"}'], dir), 0);
|
|
1268
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1269
|
+
const planTask = (taskBody) => writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' + taskBody);
|
|
1270
|
+
const C1 = '<task id="C1" status="pending"><action>实现 C1</action><write_files>src/c1.mjs</write_files><verify>node --check src/c1.mjs</verify></task>\n';
|
|
1271
|
+
// 弱判(渐进):无显式关联并行对 read∩write → WARN 提示不 BLOCK
|
|
1272
|
+
planTask(
|
|
1273
|
+
C1 +
|
|
1274
|
+
'<task id="P01" parallel="true" status="pending"><action>实现 P01</action><read_files>shared-context.md</read_files><write_files>src/p1.mjs</write_files><verify>node --check src/p1.mjs</verify></task>\n' +
|
|
1275
|
+
'<task id="P02" parallel="true" status="pending"><action>实现 P02</action><write_files>shared-context.md</write_files><verify>node --check shared-context.md</verify></task>\n');
|
|
1276
|
+
const weakRes = exitPlanNoApply();
|
|
1277
|
+
assertExit(weakRes, 0);
|
|
1278
|
+
assertOut(weakRes, 'read∩write');
|
|
1279
|
+
assertOut(weakRes, 'shared-context.md');
|
|
1280
|
+
assertNotOut(weakRes, 'BLOCKED');
|
|
1281
|
+
// 强判(负例):P01/P02 同波次并行写同一 .txt 路径(无 depends_on)→ BLOCKED 含恢复指引
|
|
1282
|
+
planTask(
|
|
1283
|
+
C1 +
|
|
1284
|
+
'<task id="P01" parallel="true" status="pending"><action>实现 P01</action><write_files>bak_a.txt</write_files><verify>node --check bak_a.txt</verify></task>\n' +
|
|
1285
|
+
'<task id="P02" parallel="true" status="pending"><action>实现 P02</action><write_files>bak_a.txt</write_files><verify>node --check bak_a.txt</verify></task>\n');
|
|
1286
|
+
const blocked = exitPlanNoApply();
|
|
1287
|
+
assertExit(blocked, 1);
|
|
1288
|
+
assertOut(blocked, 'BLOCKED');
|
|
1289
|
+
assertOut(blocked, 'bak_a.txt');
|
|
1290
|
+
assertOut(blocked, 'depends_on');
|
|
1291
|
+
// 恢复(CodeRabbit 采纳):按指引补显式 depends_on,且 **P02 保持写原重叠路径 bak_a.txt**
|
|
1292
|
+
// (不改路径避开重叠)——证明「补 depends_on 解决原重叠」而非「改路径避开重叠」;补依赖后
|
|
1293
|
+
// P02 跨趟(dep 未满足,不在本趟可运行集合)→ 同波次并行对只剩 P01 → 重叠消失 → 同出口通过。
|
|
1294
|
+
planTask(
|
|
1295
|
+
C1 +
|
|
1296
|
+
'<task id="P01" parallel="true" status="pending"><action>实现 P01</action><write_files>bak_a.txt</write_files><verify>node --check bak_a.txt</verify></task>\n' +
|
|
1297
|
+
'<task id="P02" parallel="true" status="pending"><action>实现 P02</action><write_files>bak_a.txt</write_files><verify>node --check bak_a.txt</verify><depends_on>P01</depends_on></task>\n');
|
|
1298
|
+
const ok = exitPlanApply();
|
|
1299
|
+
assertExit(ok, 0);
|
|
1300
|
+
assertOut(ok, 'ALL CHECKS PASSED');
|
|
1301
|
+
assertNotOut(ok, 'BLOCKED');
|
|
1302
|
+
},
|
|
1303
|
+
},
|
|
1304
|
+
|
|
1305
|
+
{
|
|
1306
|
+
// 多趟平行转换后 next 可达且与 guard 出口一致(真实命令链路):在 A15 的收敛链路之上,
|
|
1307
|
+
// 把此前排除在收敛断言面外的两个平行转换点(plan→subagent-execute、subagent-execute→execute
|
|
1308
|
+
// 趟间回流)也纳入 next 收敛断言——修复前 next 在这些转换点被误拦为“疑似未 exit”,预期失败。
|
|
1309
|
+
name: 'A17 多趟平行转换后 next 可达:各转换点 next 与 guard 出口 NODE 一致(真实命令)',
|
|
1310
|
+
run: (dir) => {
|
|
1311
|
+
const env = { FLOW_COMET_PROTOCOL: path.join(dir, 'reference', 'workflow-protocol.json') };
|
|
1312
|
+
const writeTask = (statusMap) => {
|
|
1313
|
+
const blk = (id, parallel, deps) =>
|
|
1314
|
+
'<task id="' + id + '"' + (parallel ? ' parallel="true"' : '') +
|
|
1315
|
+
' status="' + (statusMap[id] === 'done' ? 'done' : 'pending') + '">' +
|
|
1316
|
+
'<action>实现 ' + id + '</action><write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
1317
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify>' +
|
|
1318
|
+
(deps ? '<depends_on>' + deps + '</depends_on>' : '') + '</task>\n';
|
|
1319
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' +
|
|
1320
|
+
blk('P01', true, '') + blk('P02', true, '') +
|
|
1321
|
+
blk('S01', false, 'P01,P02') +
|
|
1322
|
+
blk('P03', true, 'S01') + blk('P04', true, 'S01') +
|
|
1323
|
+
blk('S02', false, 'P03,P04'));
|
|
1324
|
+
};
|
|
1325
|
+
const runNext = () => runState(['next'], dir, env);
|
|
1326
|
+
const nodeOf = (out) => (out.match(/^NODE: ([a-z-]+)$/m) || [])[1] ?? null;
|
|
1327
|
+
const completeTasks = (ids) => {
|
|
1328
|
+
for (const id of ids) {
|
|
1329
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/' + id + '-SUMMARY.md', execSummaryFixture(id));
|
|
1330
|
+
assertExit(runHandoff(['request', id, id + ' 委托', '--write-files', 'src/' + id.toLowerCase() + '.mjs'], dir), 0);
|
|
1331
|
+
assertExit(runHandoff(['result', id, fullContract('abcd1234abcd1234abcd1234abcd1234abcd1234', id)], dir), 0);
|
|
1332
|
+
}
|
|
1333
|
+
};
|
|
1334
|
+
// drive 收敛面全量:每个转换点(含平行跳转与趟间回流)都断言 next 可达且与 guard 出口 NODE 一致
|
|
1335
|
+
const drive = (nodeId, skill, prompt, prepare, { expectedNode } = {}) => {
|
|
1336
|
+
assertExit(runState(['skill-load', nodeId, skill, '--prompt', prompt], dir, env), 0);
|
|
1337
|
+
prepare();
|
|
1338
|
+
assertExit(runGuard(['entry', nodeId], dir), 0);
|
|
1339
|
+
const res = runGuard(['exit', nodeId, '--apply'], dir);
|
|
1340
|
+
assertExit(res, 0);
|
|
1341
|
+
const n1 = nodeOf(res.output);
|
|
1342
|
+
if (expectedNode && n1 !== expectedNode) {
|
|
1343
|
+
throw new Error('节点 ' + nodeId + ' 出口后 guard NODE(' + n1 + ') != 期望(' + expectedNode +
|
|
1344
|
+
')\nguard:\n' + res.output);
|
|
1345
|
+
}
|
|
1346
|
+
const nxt = runNext();
|
|
1347
|
+
assertExit(nxt, 0);
|
|
1348
|
+
const n2 = nodeOf(nxt.output);
|
|
1349
|
+
if (n1 !== n2) {
|
|
1350
|
+
throw new Error('节点 ' + nodeId + ' 出口后 guard NODE(' + n1 + ') != next NODE(' + n2 +
|
|
1351
|
+
')\nguard:\n' + res.output + '\nnext:\n' + nxt.output);
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
const writeSummary = (ids) => { for (const id of ids) writeFile(dir, '.specs/' + CHANGE_ID + '/' + id + '-SUMMARY.md', execSummaryFixture(id)); };
|
|
1355
|
+
driveThroughDesign(dir);
|
|
1356
|
+
writeTask({ P01: 'pending', P02: 'pending', S01: 'pending', P03: 'pending', P04: 'pending', S02: 'pending' });
|
|
1357
|
+
// ① plan 出口 → subagent-execute(平行跳转——修复前 next 误拦)
|
|
1358
|
+
drive('plan', 'flow-comet-plan', 'flow-kit/prompts/3-task.md', () => assertExit(runState(['record', 'plan', '{"summary":"plan complete"}'], dir, env), 0),
|
|
1359
|
+
{ expectedNode: 'subagent-execute' });
|
|
1360
|
+
// ② 首波并行委托完成 → 趟间回串行 execute(平行回流——修复前 next 误拦)
|
|
1361
|
+
drive('subagent-execute', 'flow-comet-subagent-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1362
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'pending', P03: 'pending', P04: 'pending', S02: 'pending' });
|
|
1363
|
+
writeSummary(['P01', 'P02']);
|
|
1364
|
+
completeTasks(['P01', 'P02']);
|
|
1365
|
+
assertExit(runState(['record', 'subagent-execute', '{"summary":"wave 1 delegated and collected"}'], dir, env), 0);
|
|
1366
|
+
}, { expectedNode: 'execute' });
|
|
1367
|
+
// ③ 串行衔接完成 → 第二波并行可委托
|
|
1368
|
+
drive('execute', 'flow-comet-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1369
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'done', P03: 'pending', P04: 'pending', S02: 'pending' });
|
|
1370
|
+
writeSummary(['S01']);
|
|
1371
|
+
completeTasks(['S01']);
|
|
1372
|
+
assertExit(runState(['record', 'execute', '{"summary":"serial wave complete"}'], dir, env), 0);
|
|
1373
|
+
}, { expectedNode: 'subagent-execute' });
|
|
1374
|
+
// ④ 第二波并行委托完成 → 收尾串行回流 execute
|
|
1375
|
+
drive('subagent-execute', 'flow-comet-subagent-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1376
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'done', P03: 'done', P04: 'done', S02: 'pending' });
|
|
1377
|
+
writeSummary(['P03', 'P04']);
|
|
1378
|
+
completeTasks(['P03', 'P04']);
|
|
1379
|
+
assertExit(runState(['record', 'subagent-execute', '{"summary":"wave 2 delegated and collected"}'], dir, env), 0);
|
|
1380
|
+
}, { expectedNode: 'execute' });
|
|
1381
|
+
// ⑤ 收尾串行完成 → review(后随 review→verify→archive 同 A15 形态)
|
|
1382
|
+
drive('execute', 'flow-comet-execute', 'flow-kit/prompts/4-dev.md', () => {
|
|
1383
|
+
writeTask({ P01: 'done', P02: 'done', S01: 'done', P03: 'done', P04: 'done', S02: 'done' });
|
|
1384
|
+
writeSummary(['S02']);
|
|
1385
|
+
completeTasks(['S02']);
|
|
1386
|
+
assertExit(runState(['record', 'execute', '{"summary":"final serial wave complete"}'], dir, env), 0);
|
|
1387
|
+
}, { expectedNode: 'review' });
|
|
1388
|
+
drive('review', 'flow-comet-review', 'flow-kit/prompts/6-review.md', () => {
|
|
1389
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/REVIEW.md',
|
|
1390
|
+
'# REVIEW\n\n## 发现\n\n### Critical\n\n- 无\n\n### Major\n\n- 无\n\n### Minor\n\n- 无\n\n## 结论\n\nreview passed,无遗留问题。\n');
|
|
1391
|
+
assertExit(runState(['record', 'review', '{"summary":"review complete"}'], dir, env), 0);
|
|
1392
|
+
}, { expectedNode: 'verify' });
|
|
1393
|
+
drive('verify', 'flow-comet-verify', 'flow-kit/prompts/7-integration.md', () => {
|
|
1394
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TEST.md', '# TEST\n\n## 验证命令\n\n```\necho ok\n```\n');
|
|
1395
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/UAT.md', '# UAT\n\n## 验收\n\n- 通过\n');
|
|
1396
|
+
assertExit(runState(['record', 'verify', '{"summary":"verify complete"}'], dir, env), 0);
|
|
1397
|
+
}, { expectedNode: 'archive' });
|
|
1398
|
+
// 归档出口:两侧均 NEXT: done
|
|
1399
|
+
assertExit(runState(['skill-load', 'archive', 'flow-comet-archive', '--prompt', 'flow-kit/prompts/7-integration.md'], dir, env), 0);
|
|
1400
|
+
writeFile(dir, '.specs/archive/2026-08-28-' + CHANGE_ID + '/KNOWN-ISSUES.md', '# 遗留问题\n\n无。\n');
|
|
1401
|
+
assertExit(runState(['record', 'archive', '{"summary":"archive complete"}'], dir, env), 0);
|
|
1402
|
+
assertExit(runGuard(['entry', 'archive'], dir), 0);
|
|
1403
|
+
const resArch = runGuard(['exit', 'archive', '--apply'], dir);
|
|
1404
|
+
assertExit(resArch, 0);
|
|
1405
|
+
assertOut(resArch, 'NEXT: done');
|
|
1406
|
+
const nxt = runNext();
|
|
1407
|
+
assertExit(nxt, 0);
|
|
1408
|
+
assertOut(nxt, 'NEXT: done');
|
|
1409
|
+
},
|
|
1410
|
+
},
|
|
1411
|
+
|
|
1412
|
+
{
|
|
1413
|
+
// M2(2026-08-29)漂移容忍 exit 系统锚(真实命令链路):execute 产物/证据齐(单任务完成 +
|
|
1414
|
+
// SUMMARY + record execute + entry execute)→ 构造欠账漂移态(currentNode 漂移到文件推导
|
|
1415
|
+
// 下一节点 review,execute 未进 completedNodes)→ 容错 exit execute --apply 补齐欠账。
|
|
1416
|
+
// 修复前 currentNode 校验直接 BLOCK → 预期失败(RED 锚);场景级同语义锚见自测套件。
|
|
1417
|
+
name: 'A18 漂移容忍 exit:execute 欠账经容错路径补齐(真实命令)',
|
|
1418
|
+
run: (dir) => {
|
|
1419
|
+
const env = { FLOW_COMET_PROTOCOL: path.join(dir, 'reference', 'workflow-protocol.json') };
|
|
1420
|
+
const writeTask = (statusMap) => {
|
|
1421
|
+
const blk = (id, status) =>
|
|
1422
|
+
'<task id="' + id + '" status="' + status + '">' +
|
|
1423
|
+
'<action>实现 ' + id + '</action><write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
1424
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify></task>\n';
|
|
1425
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' + blk('S01', statusMap.S01 ?? 'pending'));
|
|
1426
|
+
};
|
|
1427
|
+
// 驱动至 plan 出口(真实命令:init→open→design 见 driveThroughDesign,随后 plan 三连)
|
|
1428
|
+
driveThroughDesign(dir);
|
|
1429
|
+
writeTask({ S01: 'pending' });
|
|
1430
|
+
assertExit(runState(['skill-load', 'plan', 'flow-comet-plan', '--prompt', 'flow-kit/prompts/3-task.md'], dir, env), 0);
|
|
1431
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan complete"}'], dir, env), 0);
|
|
1432
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1433
|
+
assertExit(runGuard(['exit', 'plan', '--apply'], dir), 0);
|
|
1434
|
+
// execute 完整完成(产物 + record + entry,真实命令)——仅差 completedNodes(欠账)
|
|
1435
|
+
writeTask({ S01: 'done' });
|
|
1436
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/S01-SUMMARY.md', execSummaryFixture('S01'));
|
|
1437
|
+
assertExit(runState(['skill-load', 'execute', 'flow-comet-execute', '--prompt', 'flow-kit/prompts/4-dev.md'], dir, env), 0);
|
|
1438
|
+
assertExit(runState(['record', 'execute', '{"summary":"executed","parallelTakeoverApproved":true}'], dir, env), 0);
|
|
1439
|
+
assertExit(runGuard(['entry', 'execute'], dir), 0);
|
|
1440
|
+
// R-1 欠账漂移态:currentNode 漂移到文件推导下一节点 review(execute 未 completed)
|
|
1441
|
+
const st = readStateFile(dir);
|
|
1442
|
+
st.currentNode = 'review';
|
|
1443
|
+
writeState(dir, st);
|
|
1444
|
+
// 容错 exit execute --apply → 补齐欠账(completedNodes 补 execute)
|
|
1445
|
+
const res = runGuard(['exit', 'execute', '--apply'], dir);
|
|
1446
|
+
assertExit(res, 0);
|
|
1447
|
+
assertOut(res, 'ALL CHECKS PASSED');
|
|
1448
|
+
const stAfter = readStateFile(dir);
|
|
1449
|
+
if (!stAfter.completedNodes.includes('execute')) {
|
|
1450
|
+
throw new Error('容错 exit execute --apply 后 completedNodes 应含 execute(欠账补齐);实际: ' + stAfter.completedNodes.join(','));
|
|
1451
|
+
}
|
|
1452
|
+
},
|
|
1453
|
+
},
|
|
1454
|
+
|
|
1455
|
+
{
|
|
1456
|
+
// M4(2026-08-29)路由诊断静默扩展系统锚(真实命令链路):波 1 并行完成后剩余串行 pending
|
|
1457
|
+
// (P→S 收尾转换)→ subagent-execute 出口无 ROUTE WARN 噪音;畸形 parallel(parallel=true
|
|
1458
|
+
// 缺 status)+ 串行 pending → ROUTE WARN 仍报(结构校验严格保持)。场景级同语义见自测套件对应锚。
|
|
1459
|
+
name: 'A19 路由诊断静默扩展:P→S 收尾无噪音 / 畸形块仍报(真实命令)',
|
|
1460
|
+
run: (dir) => {
|
|
1461
|
+
const env = { FLOW_COMET_PROTOCOL: path.join(dir, 'reference', 'workflow-protocol.json') };
|
|
1462
|
+
const writeTask = (statusMap, extra) => {
|
|
1463
|
+
const blk = (id, parallel, status, deps) =>
|
|
1464
|
+
'<task id="' + id + '"' + (parallel ? ' parallel="true"' : ' parallel="false"') +
|
|
1465
|
+
' status="' + status + '">' +
|
|
1466
|
+
'<name>任务-' + id + '</name><read_files>src/*</read_files>' +
|
|
1467
|
+
'<action>实现 ' + id + '</action><write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
1468
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify>' +
|
|
1469
|
+
'<done>AC-' + id + '</done><depends_on>' + (deps || '') + '</depends_on></task>\n';
|
|
1470
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' +
|
|
1471
|
+
(extra ? extra
|
|
1472
|
+
: blk('P01', true, statusMap.P01 ?? 'pending', '') + blk('S01', false, statusMap.S01 ?? 'pending', 'P01')));
|
|
1473
|
+
};
|
|
1474
|
+
driveThroughDesign(dir);
|
|
1475
|
+
writeTask({ P01: 'pending', S01: 'pending' });
|
|
1476
|
+
assertExit(runState(['skill-load', 'plan', 'flow-comet-plan', '--prompt', 'flow-kit/prompts/3-task.md'], dir, env), 0);
|
|
1477
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan complete"}'], dir, env), 0);
|
|
1478
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1479
|
+
assertExit(runGuard(['exit', 'plan', '--apply'], dir), 0);
|
|
1480
|
+
// 波 1:并行任务完成(SUMMARY+handoff)→ P→S 收尾(串行尾任务 pending);
|
|
1481
|
+
// 发起委托前先 skill-load 声明(handoff request 前置门——先声明再发起委托,与既有委托链同序)
|
|
1482
|
+
writeTask({ P01: 'done', S01: 'pending' });
|
|
1483
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/P01-SUMMARY.md', execSummaryFixture('P01'));
|
|
1484
|
+
assertExit(runState(['skill-load', 'subagent-execute', 'flow-comet-subagent-execute', '--prompt', 'flow-kit/prompts/4-dev.md'], dir, env), 0);
|
|
1485
|
+
assertExit(runHandoff(['request', 'P01', 'P01 委托', '--write-files', 'src/p01.mjs'], dir), 0);
|
|
1486
|
+
assertExit(runHandoff(['result', 'P01', fullContract('abcd1234abcd1234abcd1234abcd1234abcd1234', 'P01')], dir), 0);
|
|
1487
|
+
assertExit(runState(['record', 'subagent-execute', '{"summary":"wave 1 delegated and collected"}'], dir, env), 0);
|
|
1488
|
+
assertExit(runGuard(['entry', 'subagent-execute'], dir), 0);
|
|
1489
|
+
const resWave = runGuard(['exit', 'subagent-execute', '--apply'], dir);
|
|
1490
|
+
assertExit(resWave, 0);
|
|
1491
|
+
assertNotOut(resWave, 'ROUTE WARN'); // 剩余 pending 全串行(P→S 收尾)→ M4 静默
|
|
1492
|
+
// 畸形块仍报:parallel 缺 status(但 7 字段齐)+ 串行 pending → ROUTE WARN 保持
|
|
1493
|
+
// (构造 plan 出口态,既有出口同款夹具形态;新 change 下畸形块亦须过结构门再达诊断)
|
|
1494
|
+
const st = readStateFile(dir);
|
|
1495
|
+
st.currentNode = 'plan';
|
|
1496
|
+
st.completedNodes = ['open', 'design'];
|
|
1497
|
+
writeState(dir, st);
|
|
1498
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' +
|
|
1499
|
+
'<task id="P03" parallel="true"><name>任务-P03(畸形并行,缺 status)</name><read_files>src/*</read_files>' +
|
|
1500
|
+
'<action>实现 P03</action><write_files>src/p03.mjs</write_files><verify>node --check src/p03.mjs</verify>' +
|
|
1501
|
+
'<done>AC-P03</done><depends_on></depends_on></task>\n' +
|
|
1502
|
+
'<task id="S02" parallel="false" status="pending"><name>任务-S02(串行)</name><read_files>src/*</read_files>' +
|
|
1503
|
+
'<action>实现 S02</action><write_files>src/s02.mjs</write_files><verify>node --check src/s02.mjs</verify>' +
|
|
1504
|
+
'<done>AC-S02</done><depends_on></depends_on></task>\n');
|
|
1505
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan complete"}'], dir, env), 0);
|
|
1506
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1507
|
+
const resMal = runGuard(['exit', 'plan', '--apply'], dir);
|
|
1508
|
+
assertExit(resMal, 0);
|
|
1509
|
+
assertOut(resMal, 'ROUTE WARN'); // 畸形块仍报(结构校验严格保持)
|
|
1510
|
+
},
|
|
1511
|
+
},
|
|
1512
|
+
|
|
1513
|
+
{
|
|
1514
|
+
// directOverride 授权约束系统锚(真实命令链路):执行者自切 direct 无授权(绕过脚本手改
|
|
1515
|
+
// state——修复后 hook 会拦截此类工具写;此处直接构造越权形态验证 guard 出口拦截)→
|
|
1516
|
+
// execute 出口 BLOCKED + 恢复指引;协调者授权路径(execution-mode direct 真实命令写入
|
|
1517
|
+
// 授权留痕)→ 出口通过。场景级同语义见自测套件对应锚。
|
|
1518
|
+
name: 'A20 directOverride 授权约束:自切 direct 无授权 BLOCK / 协调者授权通过(真实命令)',
|
|
1519
|
+
run: (dir) => {
|
|
1520
|
+
const env = { FLOW_COMET_PROTOCOL: path.join(dir, 'reference', 'workflow-protocol.json') };
|
|
1521
|
+
const writeTask = (statusMap) => {
|
|
1522
|
+
const blk = (id, status) =>
|
|
1523
|
+
'<task id="' + id + '" status="' + status + '">' +
|
|
1524
|
+
'<action>实现 ' + id + '</action><write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
1525
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify></task>\n';
|
|
1526
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n' + blk('S01', statusMap.S01 ?? 'pending'));
|
|
1527
|
+
};
|
|
1528
|
+
// 驱动至 execute(真实命令:init→open→design→plan 出口)
|
|
1529
|
+
driveThroughDesign(dir);
|
|
1530
|
+
writeTask({ S01: 'pending' });
|
|
1531
|
+
assertExit(runState(['skill-load', 'plan', 'flow-comet-plan', '--prompt', 'flow-kit/prompts/3-task.md'], dir, env), 0);
|
|
1532
|
+
assertExit(runState(['record', 'plan', '{"summary":"plan complete"}'], dir, env), 0);
|
|
1533
|
+
assertExit(runGuard(['entry', 'plan'], dir), 0);
|
|
1534
|
+
assertExit(runGuard(['exit', 'plan', '--apply'], dir), 0);
|
|
1535
|
+
// execute 完整完成(产物 + record + entry,真实命令)
|
|
1536
|
+
writeTask({ S01: 'done' });
|
|
1537
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/S01-SUMMARY.md', execSummaryFixture('S01'));
|
|
1538
|
+
assertExit(runState(['skill-load', 'execute', 'flow-comet-execute', '--prompt', 'flow-kit/prompts/4-dev.md'], dir, env), 0);
|
|
1539
|
+
assertExit(runState(['record', 'execute', '{"summary":"executed"}'], dir, env), 0);
|
|
1540
|
+
assertExit(runGuard(['entry', 'execute'], dir), 0);
|
|
1541
|
+
// 执行者自切 direct(directOverride=true 但无授权审计记录——B 方案前的自切/遗留形态)
|
|
1542
|
+
const st = readStateFile(dir);
|
|
1543
|
+
st.executionMode = 'direct';
|
|
1544
|
+
st.directOverride = true;
|
|
1545
|
+
writeState(dir, st);
|
|
1546
|
+
const rBlock = runGuard(['exit', 'execute', '--apply'], dir);
|
|
1547
|
+
assertExit(rBlock, 1);
|
|
1548
|
+
assertOut(rBlock, 'BLOCKED');
|
|
1549
|
+
assertOut(rBlock, 'directOverride');
|
|
1550
|
+
// 协调者授权路径:execution-mode direct(脚本写入授权留痕)→ exit 通过
|
|
1551
|
+
const auth = runState(['execution-mode', 'direct'], dir, env);
|
|
1552
|
+
assertExit(auth, 0);
|
|
1553
|
+
assertOut(auth, 'DIRECT-AUTH');
|
|
1554
|
+
const stAuth = readStateFile(dir);
|
|
1555
|
+
if (typeof stAuth.directOverrideAt !== 'string' || stAuth.directOverrideAt.trim() === '') {
|
|
1556
|
+
throw new Error('协调者授权后 state 应含 directOverrideAt 审计字段: ' + JSON.stringify(stAuth.directOverrideAt));
|
|
1557
|
+
}
|
|
1558
|
+
const rPass = runGuard(['exit', 'execute', '--apply'], dir);
|
|
1559
|
+
assertExit(rPass, 0);
|
|
1560
|
+
assertOut(rPass, 'ALL CHECKS PASSED');
|
|
1561
|
+
},
|
|
1562
|
+
},
|
|
1563
|
+
|
|
1564
|
+
// ---------- B. 声明机制(skill-load / record / exit 校验) ----------
|
|
1565
|
+
|
|
1566
|
+
{
|
|
1567
|
+
name: 'B1 skill-load:声明标记写入(协议归属)',
|
|
1568
|
+
run: (dir) => {
|
|
1569
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1570
|
+
writeFile(dir, 'flow-kit/prompts/0-change.md', '# 阶段 0 · CHANGE\n\n## 角色\n\n你是 Changeer。\n');
|
|
1571
|
+
const res = runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir);
|
|
1572
|
+
assertExit(res, 0);
|
|
1573
|
+
assertOut(res, 'SKILL-LOAD: open flow-comet-change');
|
|
1574
|
+
const markerPath = path.join(dir, '.specs', CHANGE_ID, '.skill-loads', 'open-flow-comet-change.json');
|
|
1575
|
+
if (!fs.existsSync(markerPath)) throw new Error('标记文件未生成: ' + markerPath);
|
|
1576
|
+
const marker = JSON.parse(fs.readFileSync(markerPath, 'utf8'));
|
|
1577
|
+
if (marker.node !== 'open' || marker.skill !== 'flow-comet-change') {
|
|
1578
|
+
throw new Error('标记 node/skill 字段不符: ' + JSON.stringify(marker));
|
|
1579
|
+
}
|
|
1580
|
+
if (marker.protocol !== '0-change.md') {
|
|
1581
|
+
throw new Error('标记 protocol 应为 --prompt 参数的 basename: ' + JSON.stringify(marker.protocol));
|
|
1582
|
+
}
|
|
1583
|
+
if (typeof marker.at !== 'string' || Number.isNaN(Date.parse(marker.at))) {
|
|
1584
|
+
throw new Error('标记缺 ISO 时间戳: ' + JSON.stringify(marker));
|
|
1585
|
+
}
|
|
1586
|
+
},
|
|
1587
|
+
},
|
|
1588
|
+
|
|
1589
|
+
{
|
|
1590
|
+
name: 'B2 skill-load:非法参数拒绝且不落标记',
|
|
1591
|
+
run: (dir) => {
|
|
1592
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1593
|
+
const noArgs = runState(['skill-load'], dir);
|
|
1594
|
+
assertExit(noArgs, 1);
|
|
1595
|
+
assertOut(noArgs, 'skill-load requires <node> <skill>');
|
|
1596
|
+
const badNode = runState(['skill-load', 'bogus', 'flow-comet-change'], dir);
|
|
1597
|
+
assertExit(badNode, 1);
|
|
1598
|
+
assertOut(badNode, 'skill-load node 非法');
|
|
1599
|
+
const badSkill = runState(['skill-load', 'open', 'bad/name'], dir);
|
|
1600
|
+
assertExit(badSkill, 1);
|
|
1601
|
+
assertOut(badSkill, 'skill-load skill 名非法');
|
|
1602
|
+
// --prompt 越界(不在 flow-kit/prompts/ 下)
|
|
1603
|
+
const badPrompt = runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'reference/workflow-protocol.json'], dir);
|
|
1604
|
+
assertExit(badPrompt, 1);
|
|
1605
|
+
assertOut(badPrompt, 'skill-load --prompt 路径必须位于 flow-kit/prompts/ 下');
|
|
1606
|
+
// 全部拒绝后 .skill-loads/ 不产生任何标记
|
|
1607
|
+
const loadsDir = path.join(dir, '.specs', CHANGE_ID, '.skill-loads');
|
|
1608
|
+
if (fs.existsSync(loadsDir) && fs.readdirSync(loadsDir).length > 0) {
|
|
1609
|
+
throw new Error('非法参数不应写入标记: ' + fs.readdirSync(loadsDir).join(', '));
|
|
1610
|
+
}
|
|
1611
|
+
},
|
|
1612
|
+
},
|
|
1613
|
+
|
|
1614
|
+
{
|
|
1615
|
+
name: 'B3 record:缺声明标记拦截',
|
|
1616
|
+
run: (dir) => {
|
|
1617
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1618
|
+
const res = runState(['record', 'open', JSON.stringify({ summary: 'done', completedChecks: ['required-skill:open.flow-comet-change'] })], dir);
|
|
1619
|
+
assertExit(res, 1);
|
|
1620
|
+
assertOut(res, 'BLOCKED');
|
|
1621
|
+
assertOut(res, '缺少对应声明标记');
|
|
1622
|
+
assertOut(res, 'workflow-state.mjs skill-load open flow-comet-change');
|
|
1623
|
+
// 拦截先于写入——evidence 不得落库
|
|
1624
|
+
const st = readStateFile(dir);
|
|
1625
|
+
if (st.evidence && st.evidence.open) throw new Error('拦截后不应写入 evidence: ' + JSON.stringify(st.evidence.open));
|
|
1626
|
+
},
|
|
1627
|
+
},
|
|
1628
|
+
|
|
1629
|
+
{
|
|
1630
|
+
name: 'B4 record:声明后记录通过',
|
|
1631
|
+
run: (dir) => {
|
|
1632
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1633
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change'], dir), 0);
|
|
1634
|
+
const res = runState(['record', 'open', JSON.stringify({ summary: 'done', completedChecks: ['required-skill:open.flow-comet-change'] })], dir);
|
|
1635
|
+
assertExit(res, 0);
|
|
1636
|
+
assertOut(res, 'EVIDENCE: open');
|
|
1637
|
+
const st = readStateFile(dir);
|
|
1638
|
+
if (!st.evidence.open || st.evidence.open.summary !== 'done') {
|
|
1639
|
+
throw new Error('record 未写入 evidence: ' + JSON.stringify(st.evidence));
|
|
1640
|
+
}
|
|
1641
|
+
},
|
|
1642
|
+
},
|
|
1643
|
+
|
|
1644
|
+
{
|
|
1645
|
+
name: 'B5 record:声明时间序自洽拦截',
|
|
1646
|
+
run: (dir) => {
|
|
1647
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1648
|
+
// 标记 at 晚于记录时间(手工构造未来时间戳)→ 标记必须先于记录声明
|
|
1649
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/.skill-loads/open-flow-comet-change.json',
|
|
1650
|
+
JSON.stringify({ node: 'open', skill: 'flow-comet-change', protocol: '0-change.md', at: '2999-12-31T00:00:00.000Z' }, null, 2) + '\n');
|
|
1651
|
+
const res = runState(['record', 'open', JSON.stringify({ summary: 'done', completedChecks: ['required-skill:open.flow-comet-change'] })], dir);
|
|
1652
|
+
assertExit(res, 1);
|
|
1653
|
+
assertOut(res, 'BLOCKED');
|
|
1654
|
+
assertOut(res, '标记必须先于记录声明');
|
|
1655
|
+
},
|
|
1656
|
+
},
|
|
1657
|
+
|
|
1658
|
+
{
|
|
1659
|
+
name: 'B6 record:损坏声明标记 fail-closed',
|
|
1660
|
+
run: (dir) => {
|
|
1661
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1662
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/.skill-loads/open-flow-comet-change.json', '{oops\n');
|
|
1663
|
+
const res = runState(['record', 'open', JSON.stringify({ summary: 'done', completedChecks: ['required-skill:open.flow-comet-change'] })], dir);
|
|
1664
|
+
assertExit(res, 1);
|
|
1665
|
+
assertOut(res, 'BLOCKED');
|
|
1666
|
+
assertOut(res, '声明标记损坏');
|
|
1667
|
+
},
|
|
1668
|
+
},
|
|
1669
|
+
|
|
1670
|
+
{
|
|
1671
|
+
name: 'B7 record:委托范围条目豁免(有委托通过 / 无委托拦截)',
|
|
1672
|
+
run: (dir) => {
|
|
1673
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1674
|
+
assertExit(runState(['skill-load', 'subagent-execute', 'flow-comet-dev'], dir), 0);
|
|
1675
|
+
const payload = JSON.stringify({ summary: 'delegated', completedChecks: ['required-skill:subagent-execute.flow-comet-dev'] });
|
|
1676
|
+
// 无委托记录 → 拦截(handoff scope 条目以共用证据库的委托记录为证据)
|
|
1677
|
+
const blocked = runState(['record', 'subagent-execute', payload], dir);
|
|
1678
|
+
assertExit(blocked, 1);
|
|
1679
|
+
assertOut(blocked, 'BLOCKED');
|
|
1680
|
+
assertOut(blocked, '无委托记录');
|
|
1681
|
+
// 委托并回传契约后 → 通过(request 声明边界——新 change 强制 write_files 来源)
|
|
1682
|
+
assertExit(runHandoff(['request', 'T01', 'T01 委托', '--write-files', 'src/t1.mjs'], dir), 0);
|
|
1683
|
+
assertExit(runHandoff(['result', 'T01', JSON.stringify({
|
|
1684
|
+
commitHash: 'deadbeef',
|
|
1685
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1686
|
+
greenEvidence: { command: 'node --check src/t1.mjs' },
|
|
1687
|
+
redEvidence: { command: 'node --check src/t1.mjs' },
|
|
1688
|
+
})], dir), 0);
|
|
1689
|
+
const pass = runState(['record', 'subagent-execute', payload], dir);
|
|
1690
|
+
assertExit(pass, 0);
|
|
1691
|
+
assertOut(pass, 'EVIDENCE: subagent-execute');
|
|
1692
|
+
},
|
|
1693
|
+
},
|
|
1694
|
+
|
|
1695
|
+
{
|
|
1696
|
+
name: 'B8 exit:协议声明标记校验(record 自动补通过 / 手动声明 / 空协议·损坏拦截)',
|
|
1697
|
+
run: (dir) => {
|
|
1698
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1699
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
1700
|
+
writeIntakeArtifacts(dir);
|
|
1701
|
+
writeFile(dir, 'flow-kit/prompts/0-change.md', '# 阶段 0 · CHANGE\n\n## 角色\n\n你是 Changeer。\n');
|
|
1702
|
+
// ① M5: record 自动补声明标记 → 出口通过(无需手动 skill-load)
|
|
1703
|
+
assertExit(runState(['record', 'open', '{"summary":"intake complete"}'], dir), 0);
|
|
1704
|
+
const autoMarker = path.join(dir, '.specs', CHANGE_ID, '.skill-loads', 'open-flow-comet-change.json');
|
|
1705
|
+
if (!fs.existsSync(autoMarker)) throw new Error('record 自动声明标记缺失: open-flow-comet-change.json');
|
|
1706
|
+
assertExit(runGuard(['entry', 'open'], dir), 0);
|
|
1707
|
+
const rAuto = runGuard(['exit', 'open'], dir);
|
|
1708
|
+
assertExit(rAuto, 0);
|
|
1709
|
+
assertOut(rAuto, 'ALL CHECKS PASSED');
|
|
1710
|
+
assertNotOut(rAuto, 'BLOCKED');
|
|
1711
|
+
// ② 手动 skill-load --prompt(protocol = basename)覆盖自动标记 → 出口通过
|
|
1712
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
1713
|
+
const rPass = runGuard(['exit', 'open'], dir);
|
|
1714
|
+
assertExit(rPass, 0);
|
|
1715
|
+
assertOut(rPass, 'ALL CHECKS PASSED');
|
|
1716
|
+
assertNotOut(rPass, 'BLOCKED');
|
|
1717
|
+
// ③ 未传 --prompt → 标记 protocol = null → 拦截(fail-closed;open 两个 requiredSkillCalls 均覆盖为 null)
|
|
1718
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change'], dir), 0);
|
|
1719
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-requirement'], dir), 0);
|
|
1720
|
+
const rNull = runGuard(['exit', 'open'], dir);
|
|
1721
|
+
assertExit(rNull, 1);
|
|
1722
|
+
assertOut(rNull, 'exit 缺协议声明标记');
|
|
1723
|
+
// ④ 损坏标记(protocol 非本节点协议集)→ 拦截(fail-closed)
|
|
1724
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/.skill-loads/open-flow-comet-change.json',
|
|
1725
|
+
JSON.stringify({ node: 'open', skill: 'flow-comet-change', protocol: '9-other.md', at: '2026-08-01T00:00:00.000Z' }, null, 2) + '\n');
|
|
1726
|
+
const rCorrupt = runGuard(['exit', 'open'], dir);
|
|
1727
|
+
assertExit(rCorrupt, 1);
|
|
1728
|
+
assertOut(rCorrupt, 'exit 缺协议声明标记');
|
|
1729
|
+
},
|
|
1730
|
+
},
|
|
1731
|
+
|
|
1732
|
+
{
|
|
1733
|
+
name: 'B9 旧项目兼容:record 自动补声明后照常通过',
|
|
1734
|
+
run: (dir) => {
|
|
1735
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1736
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
1737
|
+
// 旧格式记录:completedChecks 无 required-skill 条目 → 不校验标记,通过
|
|
1738
|
+
const rA = runState(['record', 'open', JSON.stringify({ summary: 'legacy', completedChecks: ['unit-tests'] })], dir);
|
|
1739
|
+
assertExit(rA, 0);
|
|
1740
|
+
assertOut(rA, 'EVIDENCE: open');
|
|
1741
|
+
// 无 completedChecks 的纯 summary 记录 → 通过(M5: record 自动补声明标记)
|
|
1742
|
+
assertExit(runState(['record', 'open', JSON.stringify({ summary: 'plain' })], dir), 0);
|
|
1743
|
+
// 出口:record 已自动补标记 → 无兼容警告,正常通过
|
|
1744
|
+
writeIntakeArtifacts(dir);
|
|
1745
|
+
assertExit(runGuard(['entry', 'open'], dir), 0);
|
|
1746
|
+
const rC = runGuard(['exit', 'open'], dir);
|
|
1747
|
+
assertExit(rC, 0);
|
|
1748
|
+
assertOut(rC, 'ALL CHECKS PASSED');
|
|
1749
|
+
assertNotOut(rC, 'SKILL-LOAD WARN');
|
|
1750
|
+
},
|
|
1751
|
+
},
|
|
1752
|
+
|
|
1753
|
+
// ---------- C. 委托链路 ----------
|
|
1754
|
+
|
|
1755
|
+
{
|
|
1756
|
+
name: 'C1 handoff:请求/结果/状态全链路(含提交校验)',
|
|
1757
|
+
run: (dir) => {
|
|
1758
|
+
gitInit(dir);
|
|
1759
|
+
writeState(dir, baseState('execute'));
|
|
1760
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md',
|
|
1761
|
+
'# TASK\n\n<task id="T01" status="done" parallel="true">\n <action>实现 T01</action>\n <write_files>src/t1.mjs</write_files>\n <verify>node --check src/t1.mjs</verify>\n</task>\n');
|
|
1762
|
+
assertExit(runState(['skill-load', 'execute', 'flow-comet-dev', '--prompt', 'flow-kit/prompts/4-dev.md'], dir), 0);
|
|
1763
|
+
writeFile(dir, 'src/t1.mjs', 'export const x = 1;\n');
|
|
1764
|
+
execFileSync('git', ['add', 'src/t1.mjs'], { cwd: dir });
|
|
1765
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'feat: t1'], { cwd: dir });
|
|
1766
|
+
const hash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1767
|
+
// ① 请求(无 --write-files → 从 TASK.md 自动解析)
|
|
1768
|
+
const req = runHandoff(['request', 'T01', '委托实现 T01'], dir);
|
|
1769
|
+
assertExit(req, 0);
|
|
1770
|
+
assertOut(req, 'HANDOFF REQUEST: T01');
|
|
1771
|
+
// ② 结果:真实提交哈希 + write_files 范围内文件 → 记录且无越界警告
|
|
1772
|
+
const result = runHandoff(['result', 'T01', JSON.stringify({
|
|
1773
|
+
status: 'DONE', taskId: 'T01', commitHash: hash,
|
|
1774
|
+
changedFiles: ['src/t1.mjs'],
|
|
1775
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1776
|
+
greenEvidence: { command: 'node --check src/t1.mjs', output: 'ok' },
|
|
1777
|
+
redEvidence: { command: 'node --check src/t1.mjs' },
|
|
1778
|
+
})], dir);
|
|
1779
|
+
assertExit(result, 0);
|
|
1780
|
+
assertOut(result, 'HANDOFF RESULT: T01');
|
|
1781
|
+
assertNotOut(result, 'HANDOFF WARN');
|
|
1782
|
+
// 新 change 提交越界(writeFiles 范围外文件)→ BLOCKED(记录时校验)
|
|
1783
|
+
const stForP4 = readStateFile(dir);
|
|
1784
|
+
stForP4.newChange = true;
|
|
1785
|
+
writeState(dir, stForP4);
|
|
1786
|
+
writeFile(dir, 'src/evil.mjs', 'export const evil = 1;\n');
|
|
1787
|
+
execFileSync('git', ['add', 'src/evil.mjs'], { cwd: dir });
|
|
1788
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'chore: out-of-bound file'], { cwd: dir });
|
|
1789
|
+
const evilHash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1790
|
+
const reqT02 = runHandoff(['request', 'T02', 'T02 委托', '--write-files', 'src/t2.mjs'], dir);
|
|
1791
|
+
assertExit(reqT02, 0);
|
|
1792
|
+
const overBound = runHandoff(['result', 'T02', JSON.stringify({
|
|
1793
|
+
status: 'DONE', taskId: 'T02', commitHash: evilHash,
|
|
1794
|
+
changedFiles: ['src/evil.mjs'],
|
|
1795
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1796
|
+
greenEvidence: { command: 'node --check src/evil.mjs', output: 'ok' },
|
|
1797
|
+
redEvidence: { command: 'node --check src/evil.mjs' },
|
|
1798
|
+
})], dir);
|
|
1799
|
+
assertExit(overBound, 1);
|
|
1800
|
+
assertOut(overBound, 'BLOCKED');
|
|
1801
|
+
assertOut(overBound, 'writeFiles');
|
|
1802
|
+
// ③ 状态:请求与结果齐可见
|
|
1803
|
+
const status = runHandoff(['status'], dir);
|
|
1804
|
+
assertExit(status, 0);
|
|
1805
|
+
assertOut(status, '"handoffRequests"');
|
|
1806
|
+
assertOut(status, '"handoffResults"');
|
|
1807
|
+
assertOut(status, 'T01');
|
|
1808
|
+
// ④ 无效提交哈希 → 错误提示但记录不阻断(结果仍入库)
|
|
1809
|
+
const badHash = runHandoff(['result', 'T02', JSON.stringify({
|
|
1810
|
+
commitHash: 'deadbeef00000000000000000000000000000000',
|
|
1811
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1812
|
+
greenEvidence: { command: 'node --check src/t2.mjs' },
|
|
1813
|
+
})], dir);
|
|
1814
|
+
assertExit(badHash, 0);
|
|
1815
|
+
assertOut(badHash, 'HANDOFF ERROR: commitHash 无效或 git show 失败');
|
|
1816
|
+
// ⑤ --json-file 读取 JSON payload(与 record 对齐——规避 Windows PowerShell
|
|
1817
|
+
// 引号剥离导致 JSON 损坏;须正确解析为对象而非存成字符串)
|
|
1818
|
+
assertExit(runHandoff(['request', 'T03', 'T03 委托', '--write-files', 'src/t1.mjs'], dir), 0);
|
|
1819
|
+
const payloadFile = path.join(dir, 'contract.json');
|
|
1820
|
+
fs.writeFileSync(payloadFile, JSON.stringify({
|
|
1821
|
+
status: 'DONE', taskId: 'T03', commitHash: hash,
|
|
1822
|
+
changedFiles: ['src/t1.mjs'],
|
|
1823
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1824
|
+
greenEvidence: { command: 'node --check src/t1.mjs', output: 'ok' },
|
|
1825
|
+
}), 'utf8');
|
|
1826
|
+
const viaFile = runHandoff(['result', 'T03', '--json-file', payloadFile], dir);
|
|
1827
|
+
assertExit(viaFile, 0);
|
|
1828
|
+
assertOut(viaFile, 'HANDOFF RESULT: T03');
|
|
1829
|
+
const st5 = readStateFile(dir);
|
|
1830
|
+
const rec5 = st5.evidence['subagent-execute'].handoffResult['T03'];
|
|
1831
|
+
if (!rec5 || typeof rec5.result !== 'object' || rec5.result.commitHash !== hash) {
|
|
1832
|
+
throw new Error('--json-file 未正确解析契约(应存为对象): ' + JSON.stringify(rec5));
|
|
1833
|
+
}
|
|
1834
|
+
// ⑥ --json-file 越界路径 → 拒绝(与 record 同规则)
|
|
1835
|
+
const outsideContract = path.join(dir, '..', 'outside-contract.json');
|
|
1836
|
+
fs.writeFileSync(outsideContract, '{"summary":"outside"}', 'utf8');
|
|
1837
|
+
const viaBad = runHandoff(['result', 'T04', '--json-file', outsideContract], dir);
|
|
1838
|
+
assertExit(viaBad, 1);
|
|
1839
|
+
assertOut(viaBad, '必须在项目根内');
|
|
1840
|
+
// ⑦ 零提交正式语义:write_files 为空 + 显式零提交 → 跳过提交子集校验 + 可审计提示;
|
|
1841
|
+
// write_files 非空 + 声称 noCommit → 仍完整提交子集校验(不可借零提交绕过越界检查)
|
|
1842
|
+
const stEmpty = readStateFile(dir);
|
|
1843
|
+
stEmpty.newChange = true;
|
|
1844
|
+
writeState(dir, stEmpty);
|
|
1845
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', '# TASK\n\n<task id="T05" status="pending"><action>无边界任务</action><write_files></write_files><verify>echo ok</verify></task>\n');
|
|
1846
|
+
assertExit(runHandoff(['request', 'T05'], dir), 0);
|
|
1847
|
+
const resZero = runHandoff(['result', 'T05', JSON.stringify({
|
|
1848
|
+
status: 'DONE', taskId: 'T05', noCommit: true,
|
|
1849
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1850
|
+
redEvidence: { command: 'echo ok' },
|
|
1851
|
+
greenEvidence: { command: 'echo ok', output: 'ok' },
|
|
1852
|
+
})], dir);
|
|
1853
|
+
assertExit(resZero, 0);
|
|
1854
|
+
assertOut(resZero, 'HANDOFF RESULT: T05');
|
|
1855
|
+
assertOut(resZero, '零提交');
|
|
1856
|
+
// ⑦b 零提交滥用负例:write_files 非空 + 契约声称 noCommit → 仍完整校验(越界 BLOCK + 矛盾提示)
|
|
1857
|
+
const stNonEmpty = readStateFile(dir);
|
|
1858
|
+
stNonEmpty.newChange = true;
|
|
1859
|
+
writeState(dir, stNonEmpty);
|
|
1860
|
+
writeFile(dir, 'src/rogue-zero.mjs', 'export const rogue = 1;\n');
|
|
1861
|
+
execFileSync('git', ['add', 'src/rogue-zero.mjs'], { cwd: dir });
|
|
1862
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'chore: rogue zero'], { cwd: dir });
|
|
1863
|
+
const rogueHash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1864
|
+
assertExit(runHandoff(['request', 'T05b', 'T05b 委托', '--write-files', 'src/t2.mjs'], dir), 0);
|
|
1865
|
+
const resAbuse = runHandoff(['result', 'T05b', JSON.stringify({
|
|
1866
|
+
status: 'DONE', taskId: 'T05b', noCommit: true, commitHash: rogueHash,
|
|
1867
|
+
changedFiles: ['src/rogue-zero.mjs'],
|
|
1868
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1869
|
+
redEvidence: { command: 'echo ok' },
|
|
1870
|
+
greenEvidence: { command: 'echo ok', output: 'ok' },
|
|
1871
|
+
})], dir);
|
|
1872
|
+
assertExit(resAbuse, 1);
|
|
1873
|
+
assertOut(resAbuse, '超出 writeFiles 范围');
|
|
1874
|
+
assertOut(resAbuse, '零提交');
|
|
1875
|
+
// ⑧ 段感知精确匹配:allowed=[src/foo] 不匹配提交 src/foobar.mjs(前缀匹配会放行,
|
|
1876
|
+
// 精确匹配判越界——修复前前缀匹配误放行,此处应 RED)
|
|
1877
|
+
writeFile(dir, 'src/foobar.mjs', 'export const x = 1;\n');
|
|
1878
|
+
execFileSync('git', ['add', 'src/foobar.mjs'], { cwd: dir });
|
|
1879
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'chore: foobar'], { cwd: dir });
|
|
1880
|
+
const foobarHash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1881
|
+
assertExit(runHandoff(['request', 'T06', '--write-files', 'src/foo'], dir), 0);
|
|
1882
|
+
const resExact = runHandoff(['result', 'T06', JSON.stringify({
|
|
1883
|
+
status: 'DONE', taskId: 'T06', commitHash: foobarHash,
|
|
1884
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1885
|
+
greenEvidence: { command: 'echo ok', output: 'ok' },
|
|
1886
|
+
})], dir);
|
|
1887
|
+
assertExit(resExact, 1);
|
|
1888
|
+
assertOut(resExact, '超出 writeFiles 范围');
|
|
1889
|
+
// ⑨ --json-file 缺值(最后一个参数)→ 用法错误(与 record 对齐;修复前报类型 TypeError)
|
|
1890
|
+
const missingVal = runHandoff(['result', 'T07', '--json-file'], dir);
|
|
1891
|
+
assertExit(missingVal, 1);
|
|
1892
|
+
assertOut(missingVal, '--json-file requires a path argument');
|
|
1893
|
+
// ⑨b --json-file 空串(--json-file= 形式)→ 同样用法错误(修复前解析为 runRoot 报 EISDIR)
|
|
1894
|
+
const emptyVal = runHandoff(['result', 'T07', '--json-file='], dir);
|
|
1895
|
+
assertExit(emptyVal, 1);
|
|
1896
|
+
assertOut(emptyVal, '--json-file requires a path argument');
|
|
1897
|
+
// ⑪ 部分通配负例:allowed=[src/*.mjs] 不得匹配 src/b.ts(段内正则锚定,
|
|
1898
|
+
// 扩展名不匹配 → 新 change BLOCKED;若实现放宽为任意匹配即 RED)
|
|
1899
|
+
writeFile(dir, 'src/b.ts', 'export const b = 1;\n');
|
|
1900
|
+
execFileSync('git', ['add', 'src/b.ts'], { cwd: dir });
|
|
1901
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'chore: b.ts'], { cwd: dir });
|
|
1902
|
+
const bHash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1903
|
+
assertExit(runHandoff(['request', 'T08', '--write-files', 'src/*.mjs'], dir), 0);
|
|
1904
|
+
const resGlobNeg = runHandoff(['result', 'T08', JSON.stringify({
|
|
1905
|
+
status: 'DONE', taskId: 'T08', commitHash: bHash,
|
|
1906
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1907
|
+
greenEvidence: { command: 'echo ok', output: 'ok' },
|
|
1908
|
+
})], dir);
|
|
1909
|
+
assertExit(resGlobNeg, 1);
|
|
1910
|
+
assertOut(resGlobNeg, '超出 writeFiles 范围');
|
|
1911
|
+
// ⑩ 部分段通配:allowed=[src/*.mjs] 应匹配提交 src/a.mjs → 无越界(修复前
|
|
1912
|
+
// 段内 glob 被字面比较,新 change 误 BLOCKED——此处应 RED)
|
|
1913
|
+
writeFile(dir, 'src/a.mjs', 'export const a = 1;\n');
|
|
1914
|
+
execFileSync('git', ['add', 'src/a.mjs'], { cwd: dir });
|
|
1915
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'chore: a.mjs'], { cwd: dir });
|
|
1916
|
+
const aHash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1917
|
+
assertExit(runHandoff(['request', 'T07', '--write-files', 'src/*.mjs'], dir), 0);
|
|
1918
|
+
const resGlob = runHandoff(['result', 'T07', JSON.stringify({
|
|
1919
|
+
status: 'DONE', taskId: 'T07', commitHash: aHash,
|
|
1920
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
1921
|
+
greenEvidence: { command: 'echo ok', output: 'ok' },
|
|
1922
|
+
})], dir);
|
|
1923
|
+
assertExit(resGlob, 0);
|
|
1924
|
+
assertNotOut(resGlob, '超出 writeFiles 范围');
|
|
1925
|
+
},
|
|
1926
|
+
},
|
|
1927
|
+
|
|
1928
|
+
{
|
|
1929
|
+
name: 'C2 handoff:红绿证据时间序(先红后绿通过 / 补录拦截)',
|
|
1930
|
+
run: (dir) => {
|
|
1931
|
+
writeState(dir, baseState('subagent-execute'));
|
|
1932
|
+
fs.mkdirSync(path.join(dir, '.specs', CHANGE_ID), { recursive: true });
|
|
1933
|
+
// ① 先记录 RED(TDD 红)
|
|
1934
|
+
const red = '{"redEvidence":{"command":"node --check src/p1.mjs"}}';
|
|
1935
|
+
const r1 = runHandoff(['result', 'P01', red], dir);
|
|
1936
|
+
assertExit(r1, 0);
|
|
1937
|
+
assertOut(r1, 'HANDOFF RESULT: P01');
|
|
1938
|
+
// ② 同批补 GREEN → 通过,且附带 recordedAt 时间戳(时序可审计)
|
|
1939
|
+
const both = '{"redEvidence":{"command":"node --check src/p1.mjs"},"greenEvidence":{"command":"node --check src/p1.mjs"}}';
|
|
1940
|
+
const r2 = runHandoff(['result', 'P01', both], dir);
|
|
1941
|
+
assertExit(r2, 0);
|
|
1942
|
+
const st = readStateFile(dir);
|
|
1943
|
+
const rec = st.evidence['subagent-execute'].handoffResult['P01'].result;
|
|
1944
|
+
if (!rec.redEvidence.recordedAt || typeof rec.redEvidence.recordedAt !== 'string') {
|
|
1945
|
+
throw new Error('redEvidence 未附带 recordedAt: ' + JSON.stringify(rec.redEvidence));
|
|
1946
|
+
}
|
|
1947
|
+
if (!rec.greenEvidence.recordedAt || typeof rec.greenEvidence.recordedAt !== 'string') {
|
|
1948
|
+
throw new Error('greenEvidence 未附带 recordedAt: ' + JSON.stringify(rec.greenEvidence));
|
|
1949
|
+
}
|
|
1950
|
+
// ③ 新任务:先 GREEN 后补 RED → 事后补录拦截
|
|
1951
|
+
const greenOnly = '{"greenEvidence":{"command":"node --check src/p2.mjs"}}';
|
|
1952
|
+
assertExit(runHandoff(['result', 'P02', greenOnly], dir), 0);
|
|
1953
|
+
const backfill = '{"greenEvidence":{"command":"node --check src/p2.mjs"},"redEvidence":{"command":"node --check src/p2.mjs"}}';
|
|
1954
|
+
const r3 = runHandoff(['result', 'P02', backfill], dir);
|
|
1955
|
+
assertExit(r3, 1);
|
|
1956
|
+
assertOut(r3, 'redEvidence 事后补录');
|
|
1957
|
+
},
|
|
1958
|
+
},
|
|
1959
|
+
|
|
1960
|
+
{
|
|
1961
|
+
name: 'C3 委托后 exit:证据键名契约与契约校验(通过 / 空交接拦截)',
|
|
1962
|
+
run: (dir) => {
|
|
1963
|
+
gitInit(dir);
|
|
1964
|
+
writeFile(dir, 'src/p1.mjs', 'export const x = 1;\n');
|
|
1965
|
+
execFileSync('git', ['add', 'src/p1.mjs'], { cwd: dir });
|
|
1966
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-qm', 'feat: p1'], { cwd: dir });
|
|
1967
|
+
const hash = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: dir, encoding: 'utf8' }).trim();
|
|
1968
|
+
// ① 空交接拦截:record 仅写空 handoffResult → 出口缺证据(无委托不可 exit)
|
|
1969
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
1970
|
+
assertExit(runState(['skill-load', 'subagent-execute', 'flow-comet-dev', '--prompt', 'flow-kit/prompts/4-dev.md'], dir), 0);
|
|
1971
|
+
assertExit(runState(['record', 'subagent-execute', '{"handoffResult":{}}'], dir), 0);
|
|
1972
|
+
const stEmpty = readStateFile(dir);
|
|
1973
|
+
stEmpty.currentNode = 'subagent-execute';
|
|
1974
|
+
writeState(dir, stEmpty);
|
|
1975
|
+
assertExit(runGuard(['entry', 'subagent-execute'], dir), 0);
|
|
1976
|
+
const blocked = runGuard(['exit', 'subagent-execute'], dir);
|
|
1977
|
+
assertExit(blocked, 1);
|
|
1978
|
+
assertOut(blocked, 'requires evidence: handoff-result');
|
|
1979
|
+
// ② 真实委托链路:request 声明边界(新 change 强制 write_files 来源)→ handoff result
|
|
1980
|
+
// 回传完整契约(键名契约:嵌套 handoffResult 识别 + Return Contract 校验)→ 出口通过
|
|
1981
|
+
const st2 = readStateFile(dir);
|
|
1982
|
+
st2.evidence['subagent-execute'] = { summary: 'delegated and collected' };
|
|
1983
|
+
writeState(dir, st2);
|
|
1984
|
+
const reqP01 = runHandoff(['request', 'P01', '委托实现 P01', '--write-files', 'src/p1.mjs'], dir);
|
|
1985
|
+
assertExit(reqP01, 0);
|
|
1986
|
+
const result = runHandoff(['result', 'P01', fullContract(hash, 'P01')], dir);
|
|
1987
|
+
assertExit(result, 0);
|
|
1988
|
+
assertOut(result, 'HANDOFF RESULT: P01');
|
|
1989
|
+
const pass = runGuard(['exit', 'subagent-execute'], dir);
|
|
1990
|
+
assertExit(pass, 0);
|
|
1991
|
+
assertOut(pass, 'ALL CHECKS PASSED');
|
|
1992
|
+
// ② M5 自动补不得为 handoff scope 技能写协调者标记(flow-comet-dev 由子代理加载,
|
|
1993
|
+
// 协调者不声明——修复前 record 无条件写标记,语义误导;此处应 RED)
|
|
1994
|
+
const markerDev = path.join(dir, '.specs', CHANGE_ID, '.skill-loads', 'subagent-execute-flow-comet-dev.json');
|
|
1995
|
+
if (fs.existsSync(markerDev)) {
|
|
1996
|
+
const markerDevObj = JSON.parse(fs.readFileSync(markerDev, 'utf8'));
|
|
1997
|
+
if (markerDevObj.auto === true) throw new Error('M5 不应为 handoff scope 技能自动补标记');
|
|
1998
|
+
}
|
|
1999
|
+
// ③ 契约缺完成检查清单 → 出口拦截(严格校验无豁免)——重写证据时保留委托请求
|
|
2000
|
+
// (新 change 强制 write_files 边界来源;直接 result 无 request 会被边界检查拦截)
|
|
2001
|
+
const st3 = readStateFile(dir);
|
|
2002
|
+
const keepRequests = st3.evidence['subagent-execute'].handoffRequests;
|
|
2003
|
+
st3.evidence['subagent-execute'] = { summary: 'delegated', ...(keepRequests ? { handoffRequests: keepRequests } : {}) };
|
|
2004
|
+
writeState(dir, st3);
|
|
2005
|
+
const weak = JSON.parse(fullContract(hash, 'P01'));
|
|
2006
|
+
delete weak.completedChecks;
|
|
2007
|
+
assertExit(runHandoff(['result', 'P01', JSON.stringify(weak)], dir), 0);
|
|
2008
|
+
const reenter = runGuard(['exit', 'subagent-execute'], dir);
|
|
2009
|
+
assertExit(reenter, 1);
|
|
2010
|
+
assertOut(reenter, 'Return Contract 校验失败');
|
|
2011
|
+
// 移除手动声明标记,回到「仅有 open 声明、无 subagent-execute 声明」的激活机制形态
|
|
2012
|
+
fs.rmSync(markerDev, { force: true });
|
|
2013
|
+
// ⑫ 声明机制激活下的 subagent-execute 声明要求(自动补标记跳过 handoff scope 后的
|
|
2014
|
+
// 固化行为):有 open 标记(声明机制已激活)但无 subagent-execute 声明 → exit BLOCKED;
|
|
2015
|
+
// 手动 skill-load 声明(协调者按 4-dev.md 构造 handoff prompt 的真实动作)后 → 通过
|
|
2016
|
+
writeFile(dir, 'flow-kit/prompts/4-dev.md', '# 阶段 4 · DEV\n\n## 角色\n\n你是 Developer。\n');
|
|
2017
|
+
const stDecl = readStateFile(dir);
|
|
2018
|
+
stDecl.evidence['subagent-execute'] = { summary: 'delegated', handoffResult: { P01: { result: JSON.parse(fullContract(hash, 'P01')) } } };
|
|
2019
|
+
writeState(dir, stDecl);
|
|
2020
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/.skill-loads/open-flow-comet-change.json',
|
|
2021
|
+
JSON.stringify({ node: 'open', skill: 'flow-comet-change', protocol: '0-change.md', at: '2026-08-01T00:00:00.000Z' }, null, 2) + '\n');
|
|
2022
|
+
const rDeclBlock = runGuard(['exit', 'subagent-execute'], dir);
|
|
2023
|
+
assertExit(rDeclBlock, 1);
|
|
2024
|
+
assertOut(rDeclBlock, 'exit 缺协议声明标记');
|
|
2025
|
+
assertExit(runState(['skill-load', 'subagent-execute', 'flow-comet-dev', '--prompt', 'flow-kit/prompts/4-dev.md'], dir), 0);
|
|
2026
|
+
const rDeclPass = runGuard(['exit', 'subagent-execute'], dir);
|
|
2027
|
+
assertExit(rDeclPass, 0);
|
|
2028
|
+
assertOut(rDeclPass, 'ALL CHECKS PASSED');
|
|
2029
|
+
},
|
|
2030
|
+
},
|
|
2031
|
+
|
|
2032
|
+
// ---------- D. hook 写白名单 ----------
|
|
2033
|
+
|
|
2034
|
+
{
|
|
2035
|
+
name: 'D1 hook:open 阶段工件放行与源码拦截',
|
|
2036
|
+
run: (dir) => {
|
|
2037
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
2038
|
+
// 工件放行
|
|
2039
|
+
const ok = runHook(['before_tool'], dir,
|
|
2040
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'CHANGE.md') } });
|
|
2041
|
+
assertExit(ok, 0);
|
|
2042
|
+
assertOut(ok, 'workflow-hook-guard-ok');
|
|
2043
|
+
assertOut(ok, 'NODE: open');
|
|
2044
|
+
// 源码拦截(exit 2)
|
|
2045
|
+
const blocked = runHook(['before_tool'], dir,
|
|
2046
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'evil.py') } });
|
|
2047
|
+
assertExit(blocked, 2);
|
|
2048
|
+
assertOut(blocked, 'BLOCKED: phase "open" 不允许写入');
|
|
2049
|
+
},
|
|
2050
|
+
},
|
|
2051
|
+
|
|
2052
|
+
{
|
|
2053
|
+
name: 'D2 hook:execute 按执行模式收窄(direct 放宽)',
|
|
2054
|
+
run: (dir) => {
|
|
2055
|
+
// subagent 模式:execute 协调者只写 .specs/ → 写源码拦截
|
|
2056
|
+
const st = baseState('execute');
|
|
2057
|
+
st.status = 'running';
|
|
2058
|
+
writeState(dir, st);
|
|
2059
|
+
const blocked = runHook(['before_tool'], dir,
|
|
2060
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'evil.py') } });
|
|
2061
|
+
assertExit(blocked, 2);
|
|
2062
|
+
assertOut(blocked, 'BLOCKED: phase "execute" 不允许写入');
|
|
2063
|
+
// subagent 模式:写工件放行
|
|
2064
|
+
const ok = runHook(['before_tool'], dir,
|
|
2065
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'T01-SUMMARY.md') } });
|
|
2066
|
+
assertExit(ok, 0);
|
|
2067
|
+
assertOut(ok, 'NODE: execute');
|
|
2068
|
+
// direct 模式(逃生口):主代理直写源码放行
|
|
2069
|
+
const stDirect = readStateFile(dir);
|
|
2070
|
+
stDirect.executionMode = 'direct';
|
|
2071
|
+
stDirect.directOverride = true;
|
|
2072
|
+
writeState(dir, stDirect);
|
|
2073
|
+
const direct = runHook(['before_tool'], dir,
|
|
2074
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'evil.py') } });
|
|
2075
|
+
assertExit(direct, 0);
|
|
2076
|
+
assertOut(direct, 'workflow-hook-guard-ok');
|
|
2077
|
+
},
|
|
2078
|
+
},
|
|
2079
|
+
|
|
2080
|
+
{
|
|
2081
|
+
name: 'D3 hook:归档阶段白名单与完成态放行',
|
|
2082
|
+
run: (dir) => {
|
|
2083
|
+
const st = baseState('archive');
|
|
2084
|
+
st.status = 'running';
|
|
2085
|
+
writeState(dir, st);
|
|
2086
|
+
// 归档路径放行
|
|
2087
|
+
const ok = runHook(['before_tool'], dir,
|
|
2088
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', 'archive', '2026-08-11-' + CHANGE_ID, 'CHANGE.md') } });
|
|
2089
|
+
assertExit(ok, 0);
|
|
2090
|
+
assertOut(ok, 'NODE: archive');
|
|
2091
|
+
// 归档流程写 change 目录工件(遗留清单 KNOWN-ISSUES.md 先写后移)放行——
|
|
2092
|
+
// 修复前白名单仅 .specs/archive/ 导致按文档流程被 BLOCK(此处应 RED)
|
|
2093
|
+
const okChange = runHook(['before_tool'], dir,
|
|
2094
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'KNOWN-ISSUES.md') } });
|
|
2095
|
+
assertExit(okChange, 0);
|
|
2096
|
+
assertOut(okChange, 'NODE: archive');
|
|
2097
|
+
// 白名单收窄:归档阶段仅遗留清单可写——change 目录其他工件(如 TASK.md)应 BLOCK
|
|
2098
|
+
// (放宽到整个 change 目录会让归档阶段可改任意工件且无校验——防线变宽)
|
|
2099
|
+
const blockedArtifact = runHook(['before_tool'], dir,
|
|
2100
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'TASK.md') } });
|
|
2101
|
+
assertExit(blockedArtifact, 2);
|
|
2102
|
+
assertOut(blockedArtifact, 'BLOCKED');
|
|
2103
|
+
// 归档阶段写源码拦截
|
|
2104
|
+
const blocked = runHook(['before_tool'], dir,
|
|
2105
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'evil.py') } });
|
|
2106
|
+
assertExit(blocked, 2);
|
|
2107
|
+
// 完成态(归档后,活跃项目已清空)→ 无活跃工作流放行
|
|
2108
|
+
const done = readStateFile(dir);
|
|
2109
|
+
done.status = 'completed';
|
|
2110
|
+
done.activeChange = null;
|
|
2111
|
+
done.currentNode = null;
|
|
2112
|
+
writeState(dir, done);
|
|
2113
|
+
const after = runHook(['before_tool'], dir,
|
|
2114
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'anything.md') } });
|
|
2115
|
+
assertExit(after, 0);
|
|
2116
|
+
assertOut(after, 'no active workflow');
|
|
2117
|
+
},
|
|
2118
|
+
},
|
|
2119
|
+
|
|
2120
|
+
// ---------- E. 自定义协议 ----------
|
|
2121
|
+
|
|
2122
|
+
{
|
|
2123
|
+
name: 'E1 自定义协议:加载路由与节点推导',
|
|
2124
|
+
run: (dir) => {
|
|
2125
|
+
const custom = writeCustomProtocol(dir);
|
|
2126
|
+
// init 按协议首节点
|
|
2127
|
+
const init = runState(['init', 'cp-demo', '--protocol', custom], dir);
|
|
2128
|
+
assertExit(init, 0);
|
|
2129
|
+
assertOut(init, 'NODE: brainstorm');
|
|
2130
|
+
const st = readStateFile(dir);
|
|
2131
|
+
if (st.currentNode !== 'brainstorm') throw new Error('init currentNode 应为协议首节点 brainstorm');
|
|
2132
|
+
// 无产物 → 首节点;仅 notes.md → 第二节点;产物齐全 → 最后节点
|
|
2133
|
+
const s1 = runState(['status', '--protocol', custom], dir);
|
|
2134
|
+
assertExit(s1, 0);
|
|
2135
|
+
assertOut(s1, '"currentNode": "brainstorm"');
|
|
2136
|
+
writeFile(dir, '.specs/cp-demo/notes.md', '# notes\n');
|
|
2137
|
+
const s2 = runState(['status', '--protocol', custom], dir);
|
|
2138
|
+
assertExit(s2, 0);
|
|
2139
|
+
assertOut(s2, '"currentNode": "tdd"');
|
|
2140
|
+
writeFile(dir, '.specs/cp-demo/T01-SUMMARY.md', '# T01-SUMMARY\n');
|
|
2141
|
+
writeFile(dir, '.specs/cp-demo/verdict.md', '# verdict\n');
|
|
2142
|
+
const s3 = runState(['status', '--protocol', custom], dir);
|
|
2143
|
+
assertExit(s3, 0);
|
|
2144
|
+
assertOut(s3, '"currentNode": "codereview"');
|
|
2145
|
+
},
|
|
2146
|
+
},
|
|
2147
|
+
|
|
2148
|
+
{
|
|
2149
|
+
name: 'E2 自定义协议:声明机制与出口全链路(含技能绑定)',
|
|
2150
|
+
run: (dir) => {
|
|
2151
|
+
const custom = writeCustomProtocol(dir, true);
|
|
2152
|
+
assertExit(runState(['init', 'cp-skill', '--protocol', custom], dir), 0);
|
|
2153
|
+
const env = { FLOW_COMET_PROTOCOL: custom };
|
|
2154
|
+
// 自定义节点 skill-load 声明(协议节点集合动态校验)
|
|
2155
|
+
const sl = runState(['skill-load', 'brainstorm', 'flow-comet-brainstorm'], dir, env);
|
|
2156
|
+
assertExit(sl, 0);
|
|
2157
|
+
assertOut(sl, 'SKILL-LOAD: brainstorm flow-comet-brainstorm');
|
|
2158
|
+
// 自定义节点 record(main scope 绑定 + 声明标记 → 通过)
|
|
2159
|
+
const rec = runState(['record', 'brainstorm', JSON.stringify({ summary: 'brainstorm done', completedChecks: ['required-skill:brainstorm.flow-comet-brainstorm'] })], dir, env);
|
|
2160
|
+
assertExit(rec, 0);
|
|
2161
|
+
assertOut(rec, 'EVIDENCE: brainstorm');
|
|
2162
|
+
// 出口:产物齐 + 诱饵内置工件缺段 → 通过且不误触发内置特化校验
|
|
2163
|
+
writeFile(dir, '.specs/cp-skill/notes.md', '# notes\n');
|
|
2164
|
+
writeFile(dir, '.specs/cp-skill/CHANGE.md', '# CHANGE\n\n## 变更目标\n\n## 方案\n');
|
|
2165
|
+
writeFile(dir, '.specs/cp-skill/REQUIREMENT.md', '# REQUIREMENT\n\n## 用户故事\n\n## 需求分析\n');
|
|
2166
|
+
assertExit(runGuard(['entry', 'brainstorm'], dir, env), 0);
|
|
2167
|
+
const exitRes = runGuard(['exit', 'brainstorm', '--apply'], dir, env);
|
|
2168
|
+
assertExit(exitRes, 0);
|
|
2169
|
+
assertOut(exitRes, 'ALL CHECKS PASSED');
|
|
2170
|
+
assertNotOut(exitRes, '缺必填段');
|
|
2171
|
+
assertNotOut(exitRes, '缺少验收标准');
|
|
2172
|
+
const st = readStateFile(dir);
|
|
2173
|
+
if (!st.completedNodes.includes('brainstorm') || st.currentNode !== 'tdd') {
|
|
2174
|
+
throw new Error('apply 后应完成 brainstorm 并推进到 tdd: ' + JSON.stringify({ completedNodes: st.completedNodes, currentNode: st.currentNode }));
|
|
2175
|
+
}
|
|
2176
|
+
// 协议 env 加载(无 --protocol CLI)同样生效
|
|
2177
|
+
const envLoad = runGuard(['exit', 'bogus'], dir, env);
|
|
2178
|
+
assertExit(envLoad, 1);
|
|
2179
|
+
assertOut(envLoad, 'Unknown workflow Node');
|
|
2180
|
+
},
|
|
2181
|
+
},
|
|
2182
|
+
|
|
2183
|
+
// ---------- F. 自动初始化 ----------
|
|
2184
|
+
|
|
2185
|
+
{
|
|
2186
|
+
name: 'F1 初始化检测:上下文缺失提示且不自动生成',
|
|
2187
|
+
run: (dir) => {
|
|
2188
|
+
writeFile(dir, 'package.json', '{"name":"x"}');
|
|
2189
|
+
const res = runState(['init', CHANGE_ID], dir);
|
|
2190
|
+
assertExit(res, 0);
|
|
2191
|
+
assertOut(res, 'INIT-NEEDED');
|
|
2192
|
+
if (fs.existsSync(path.join(dir, '.specs', 'CONTEXT.md'))) {
|
|
2193
|
+
throw new Error('CONTEXT 不应由脚本自动生成(生成职责在 agent)');
|
|
2194
|
+
}
|
|
2195
|
+
},
|
|
2196
|
+
},
|
|
2197
|
+
|
|
2198
|
+
{
|
|
2199
|
+
name: 'F2 初始化检测:跳过记忆',
|
|
2200
|
+
run: (dir) => {
|
|
2201
|
+
writeFile(dir, 'package.json', '{"name":"x"}');
|
|
2202
|
+
const first = runState(['init', CHANGE_ID, '--init-skip'], dir);
|
|
2203
|
+
assertExit(first, 0);
|
|
2204
|
+
assertOut(first, 'INIT-SKIPPED');
|
|
2205
|
+
const st1 = readStateFile(dir);
|
|
2206
|
+
if (st1.ai_context_doc !== 'none') throw new Error('跳过后 ai_context_doc 应为 none');
|
|
2207
|
+
// 下次 init(新 id)尊重既有决策 → 静默
|
|
2208
|
+
const second = runState(['init', CHANGE_ID + '-2'], dir);
|
|
2209
|
+
assertExit(second, 0);
|
|
2210
|
+
assertNotOut(second, 'INIT-NEEDED');
|
|
2211
|
+
assertNotOut(second, 'INIT-HINT');
|
|
2212
|
+
},
|
|
2213
|
+
},
|
|
2214
|
+
|
|
2215
|
+
{
|
|
2216
|
+
name: 'F3 初始化检测:新鲜上下文静默',
|
|
2217
|
+
run: (dir) => {
|
|
2218
|
+
writeState(dir, { ...baseState('open'), last_intel_scan: new Date(Date.now() - 10 * 864e5).toISOString() });
|
|
2219
|
+
writeFile(dir, '.specs/CONTEXT.md', '# CONTEXT\n## 项目概要\nx\n');
|
|
2220
|
+
const res = runState(['init', CHANGE_ID], dir);
|
|
2221
|
+
assertExit(res, 0);
|
|
2222
|
+
assertNotOut(res, 'INIT-NEEDED');
|
|
2223
|
+
assertNotOut(res, 'INIT-HINT');
|
|
2224
|
+
assertOut(res, 'Initialized: ' + CHANGE_ID);
|
|
2225
|
+
},
|
|
2226
|
+
},
|
|
2227
|
+
|
|
2228
|
+
{
|
|
2229
|
+
name: 'F4 初始化检测:生成协作全链路',
|
|
2230
|
+
run: (dir) => {
|
|
2231
|
+
writeFile(dir, 'package.json', '{"name":"x"}');
|
|
2232
|
+
// ① 缺失 + 授权 → 生成指引,不生成不写扫描时间
|
|
2233
|
+
const gen = runState(['init', CHANGE_ID, '--init-context'], dir);
|
|
2234
|
+
assertExit(gen, 0);
|
|
2235
|
+
assertOut(gen, 'INIT-GENERATE');
|
|
2236
|
+
if (fs.existsSync(path.join(dir, '.specs', 'CONTEXT.md'))) throw new Error('CONTEXT 不应由脚本生成');
|
|
2237
|
+
let st = readStateFile(dir);
|
|
2238
|
+
if (st.last_intel_scan) throw new Error('校验通过前不应写扫描时间');
|
|
2239
|
+
// ② agent 生成 7 段模板后重跑 → 校验通过 + 记录扫描时间
|
|
2240
|
+
writeFile(dir, '.specs/CONTEXT.md', CONTEXT_FULL);
|
|
2241
|
+
const done = runState(['init', CHANGE_ID, '--init-context'], dir);
|
|
2242
|
+
assertExit(done, 0);
|
|
2243
|
+
assertOut(done, 'INIT-DONE');
|
|
2244
|
+
st = readStateFile(dir);
|
|
2245
|
+
if (!st.last_intel_scan) throw new Error('校验通过后应写扫描时间');
|
|
2246
|
+
// ③ 已存在但缺段 → 重写指引,不记录新扫描时间(保持既有)
|
|
2247
|
+
writeFile(dir, '.specs/CONTEXT.md', '# CONTEXT\n## 项目概要\nx\n');
|
|
2248
|
+
const failed = runState(['init', CHANGE_ID, '--init-context'], dir);
|
|
2249
|
+
assertExit(failed, 0);
|
|
2250
|
+
assertOut(failed, 'INIT-VALIDATE-FAILED');
|
|
2251
|
+
assertOut(failed, '重写');
|
|
2252
|
+
},
|
|
2253
|
+
},
|
|
2254
|
+
|
|
2255
|
+
// ---------- G. 分支模式 ----------
|
|
2256
|
+
|
|
2257
|
+
{
|
|
2258
|
+
name: 'G1 分支模式:init 建分支与一致性',
|
|
2259
|
+
run: (dir) => {
|
|
2260
|
+
gitInit(dir);
|
|
2261
|
+
assertExit(runState(['init', 'g1-br'], dir), 0);
|
|
2262
|
+
if (gitBranch(dir) !== 'change/g1-br') throw new Error('期望分支 change/g1-br,实际 ' + gitBranch(dir));
|
|
2263
|
+
const status = runState(['status'], dir);
|
|
2264
|
+
assertExit(status, 0);
|
|
2265
|
+
assertOut(status, 'BRANCH: change/g1-br | 一致性: ok');
|
|
2266
|
+
},
|
|
2267
|
+
},
|
|
2268
|
+
|
|
2269
|
+
{
|
|
2270
|
+
name: 'G2 分支模式:归档入口分支校验',
|
|
2271
|
+
run: (dir) => {
|
|
2272
|
+
gitInit(dir);
|
|
2273
|
+
const st = baseState('archive');
|
|
2274
|
+
st.branchMode = true;
|
|
2275
|
+
writeState(dir, st);
|
|
2276
|
+
// 非 change/<id> 分支 → 拦截
|
|
2277
|
+
const blocked = runGuard(['entry', 'archive'], dir);
|
|
2278
|
+
assertExit(blocked, 1);
|
|
2279
|
+
assertOut(blocked, 'BLOCKED');
|
|
2280
|
+
assertOut(blocked, '归档必须在 change/' + CHANGE_ID + ' 分支上进行');
|
|
2281
|
+
// 切到正确分支 → 通过
|
|
2282
|
+
execFileSync('git', ['checkout', '-b', 'change/' + CHANGE_ID], { cwd: dir, stdio: 'ignore' });
|
|
2283
|
+
const ok = runGuard(['entry', 'archive'], dir);
|
|
2284
|
+
assertExit(ok, 0);
|
|
2285
|
+
assertOut(ok, 'ENTRY OK: archive');
|
|
2286
|
+
},
|
|
2287
|
+
},
|
|
2288
|
+
|
|
2289
|
+
{
|
|
2290
|
+
name: 'G3 分支模式:一致性失配警告',
|
|
2291
|
+
run: (dir) => {
|
|
2292
|
+
gitInit(dir);
|
|
2293
|
+
execFileSync('git', ['checkout', '-b', 'topic-x'], { cwd: dir, stdio: 'ignore' });
|
|
2294
|
+
writeState(dir, { ...baseState('open'), branchPrefix: 'change/' });
|
|
2295
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/CHANGE.md', '# CHANGE\n## Why\nx\n');
|
|
2296
|
+
const status = runState(['status'], dir);
|
|
2297
|
+
assertExit(status, 0);
|
|
2298
|
+
assertOut(status, '一致性: mismatch');
|
|
2299
|
+
assertOut(status, 'WARN: 分支与 activeChange 不一致');
|
|
2300
|
+
},
|
|
2301
|
+
},
|
|
2302
|
+
|
|
2303
|
+
// ---------- H. verify 与归档 ----------
|
|
2304
|
+
|
|
2305
|
+
{
|
|
2306
|
+
name: 'H1 verify:验证命令真实执行与超时配置',
|
|
2307
|
+
run: (dir) => {
|
|
2308
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
2309
|
+
assertExit(runState(['skill-load', 'verify', 'flow-comet-integration', '--prompt', 'flow-kit/prompts/7-integration.md'], dir), 0);
|
|
2310
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/UAT.md', '# UAT\n\n通过\n');
|
|
2311
|
+
assertExit(runState(['record', 'verify', '{"summary":"verified"}'], dir), 0);
|
|
2312
|
+
const st = readStateFile(dir);
|
|
2313
|
+
st.currentNode = 'verify';
|
|
2314
|
+
writeState(dir, st);
|
|
2315
|
+
// ① 快速命令 → 出口通过
|
|
2316
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TEST.md', '# TEST\n\n## 验证命令\n\n```bash\nnode -e "1"\n```\n');
|
|
2317
|
+
assertExit(runGuard(['entry', 'verify'], dir), 0);
|
|
2318
|
+
const pass = runGuard(['exit', 'verify'], dir);
|
|
2319
|
+
assertExit(pass, 0);
|
|
2320
|
+
assertOut(pass, 'ALL CHECKS PASSED');
|
|
2321
|
+
// ② 缺省超时(未设 env)保持大值:耗时命令通过
|
|
2322
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TEST.md', '# TEST\n\n## 验证命令\n\n```bash\nnode -e "setTimeout(()=>{}, 1500)"\n```\n');
|
|
2323
|
+
const defaultTimeout = runGuard(['exit', 'verify'], dir);
|
|
2324
|
+
assertExit(defaultTimeout, 0);
|
|
2325
|
+
assertOut(defaultTimeout, 'ALL CHECKS PASSED');
|
|
2326
|
+
// ③ 环境覆盖生效:小超时 → 超时拦截(不设 env 时同命令走缺省会通过——覆盖被忽略即红)
|
|
2327
|
+
const timedOut = runGuard(['exit', 'verify'], dir, { FLOW_COMET_VERIFY_TIMEOUT_MS: '500' });
|
|
2328
|
+
// 超时 kill 的 cmd 孙进程(node)孤儿化后短暂存活,其 cwd 锁定场景目录
|
|
2329
|
+
// (Windows:父 cmd 被杀、孙进程继续跑完自身定时器)——断言前同步等待其自然退出
|
|
2330
|
+
execFileSync(process.execPath, ['-e', 'setTimeout(()=>{}, 2000)']);
|
|
2331
|
+
assertExit(timedOut, 1);
|
|
2332
|
+
assertOut(timedOut, 'BLOCKED: verify 命令失败');
|
|
2333
|
+
assertOut(timedOut, 'timeout 500ms');
|
|
2334
|
+
// 子断言:失败计数按 change 写入(guard 侧)
|
|
2335
|
+
const stTimed = readStateFile(dir);
|
|
2336
|
+
if (!stTimed.verifyFailuresByChange || stTimed.verifyFailuresByChange[CHANGE_ID] < 1) {
|
|
2337
|
+
throw new Error('exit verify 失败计数未按 change 写入: ' + JSON.stringify(stTimed.verifyFailuresByChange));
|
|
2338
|
+
}
|
|
2339
|
+
// ④ 无验证命令 → 拦截(严格版要求)
|
|
2340
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TEST.md', '# TEST\n\n无验证命令段。\n');
|
|
2341
|
+
const noCmd = runGuard(['exit', 'verify'], dir);
|
|
2342
|
+
assertExit(noCmd, 1);
|
|
2343
|
+
assertOut(noCmd, 'BLOCKED: TEST.md 需声明 ## 验证命令 段');
|
|
2344
|
+
},
|
|
2345
|
+
},
|
|
2346
|
+
|
|
2347
|
+
{
|
|
2348
|
+
name: 'H2 归档:完整流程(遗留清单 + 移动 + 完成态)',
|
|
2349
|
+
run: (dir) => {
|
|
2350
|
+
assertExit(runState(['init', 'arch-demo'], dir), 0);
|
|
2351
|
+
assertExit(runState(['skill-load', 'archive', 'flow-comet-integration', '--prompt', 'flow-kit/prompts/7-integration.md'], dir), 0);
|
|
2352
|
+
// 节点推进到归档(模拟前序节点出口已完成的状态——被测机制是归档出口本身)
|
|
2353
|
+
const st0 = readStateFile(dir);
|
|
2354
|
+
st0.currentNode = 'archive';
|
|
2355
|
+
writeState(dir, st0);
|
|
2356
|
+
assertExit(runState(['record', 'archive', '{"summary":"archived"}'], dir), 0);
|
|
2357
|
+
// ① 未移动(无归档目录)→ 出口拦截(缺归档产物)
|
|
2358
|
+
assertExit(runGuard(['entry', 'archive'], dir), 0);
|
|
2359
|
+
const blocked = runGuard(['exit', 'archive'], dir);
|
|
2360
|
+
assertExit(blocked, 1);
|
|
2361
|
+
assertOut(blocked, 'missing Output Schema artifacts');
|
|
2362
|
+
// ② 遗留问题清单(显式无遗留)随工件写入
|
|
2363
|
+
writeFile(dir, '.specs/arch-demo/KNOWN-ISSUES.md', '# KNOWN-ISSUES\n\n无遗留问题\n');
|
|
2364
|
+
// ③ 移动目录到归档路径(真实归档流程:先移动后出口)
|
|
2365
|
+
fs.mkdirSync(path.join(dir, '.specs', 'archive'), { recursive: true });
|
|
2366
|
+
fs.renameSync(path.join(dir, '.specs', 'arch-demo'), path.join(dir, '.specs', 'archive', '2026-08-11-arch-demo'));
|
|
2367
|
+
if (fs.existsSync(path.join(dir, '.specs', 'arch-demo'))) throw new Error('移动后源目录应不存在');
|
|
2368
|
+
if (!fs.existsSync(path.join(dir, '.specs', 'archive', '2026-08-11-arch-demo', 'KNOWN-ISSUES.md'))) {
|
|
2369
|
+
throw new Error('遗留问题清单应随工件归档');
|
|
2370
|
+
}
|
|
2371
|
+
// ④ 出口 --apply → 通过 + 归档完成态(清空活跃项目)
|
|
2372
|
+
const pass = runGuard(['exit', 'archive', '--apply'], dir);
|
|
2373
|
+
assertExit(pass, 0);
|
|
2374
|
+
assertOut(pass, 'ALL CHECKS PASSED');
|
|
2375
|
+
const st = readStateFile(dir);
|
|
2376
|
+
if (st.activeChange !== null) throw new Error('归档后 activeChange 应为 null: ' + JSON.stringify(st.activeChange));
|
|
2377
|
+
if (st.currentNode !== null) throw new Error('归档后 currentNode 应为 null');
|
|
2378
|
+
if (st.status !== 'completed') throw new Error('归档后 status 应为 completed');
|
|
2379
|
+
const status = runState(['status'], dir);
|
|
2380
|
+
assertExit(status, 0);
|
|
2381
|
+
assertOut(status, '"status": "no-change"');
|
|
2382
|
+
},
|
|
2383
|
+
},
|
|
2384
|
+
|
|
2385
|
+
{
|
|
2386
|
+
name: 'H3 归档:归档路径声明标记查找',
|
|
2387
|
+
run: (dir) => {
|
|
2388
|
+
assertExit(runState(['init', 'arch-mk'], dir), 0);
|
|
2389
|
+
writeFile(dir, 'flow-kit/prompts/0-change.md', '# 阶段 0 · CHANGE\n\n## 角色\n\n你是 Changeer。\n');
|
|
2390
|
+
writeFile(dir, 'flow-kit/prompts/7-integration.md', '# 阶段 7 · INTEGRATION\n\n## 角色\n\n你是 Integrationer。\n');
|
|
2391
|
+
// 遗留清单(协议 required 产物——随目录移动进归档)
|
|
2392
|
+
writeFile(dir, '.specs/arch-mk/KNOWN-ISSUES.md', '# KNOWN-ISSUES\n\n无遗留问题\n');
|
|
2393
|
+
// 归档前声明标记(open + archive 节点)
|
|
2394
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
2395
|
+
assertExit(runState(['skill-load', 'archive', 'flow-comet-integration', '--prompt', 'flow-kit/prompts/7-integration.md'], dir), 0);
|
|
2396
|
+
// 节点推进到归档(模拟前序节点出口已完成的状态)
|
|
2397
|
+
const st0 = readStateFile(dir);
|
|
2398
|
+
st0.currentNode = 'archive';
|
|
2399
|
+
writeState(dir, st0);
|
|
2400
|
+
assertExit(runState(['record', 'archive', '{"summary":"archived"}'], dir), 0);
|
|
2401
|
+
// 移动(标记随目录进入归档路径)
|
|
2402
|
+
fs.mkdirSync(path.join(dir, '.specs', 'archive'), { recursive: true });
|
|
2403
|
+
fs.renameSync(path.join(dir, '.specs', 'arch-mk'), path.join(dir, '.specs', 'archive', '2026-08-11-arch-mk'));
|
|
2404
|
+
const markerInArchive = path.join(dir, '.specs', 'archive', '2026-08-11-arch-mk', '.skill-loads', 'archive-flow-comet-integration.json');
|
|
2405
|
+
if (!fs.existsSync(markerInArchive)) throw new Error('声明标记应随目录移入归档路径');
|
|
2406
|
+
// 出口:标记经归档路径解析命中(活动路径已不存在)→ 通过且无兼容警告
|
|
2407
|
+
assertExit(runGuard(['entry', 'archive'], dir), 0);
|
|
2408
|
+
const pass = runGuard(['exit', 'archive', '--apply'], dir);
|
|
2409
|
+
assertExit(pass, 0);
|
|
2410
|
+
assertOut(pass, 'ALL CHECKS PASSED');
|
|
2411
|
+
assertNotOut(pass, 'SKILL-LOAD WARN');
|
|
2412
|
+
assertNotOut(pass, 'exit 缺协议声明标记');
|
|
2413
|
+
},
|
|
2414
|
+
},
|
|
2415
|
+
|
|
2416
|
+
{
|
|
2417
|
+
name: 'H4 归档:M5 自动补标记写归档路径,不重建活动目录',
|
|
2418
|
+
run: (dir) => {
|
|
2419
|
+
// 归档顺序(先移目录后 record)下,record archive 的 M5 自动补声明标记应写归档路径
|
|
2420
|
+
// (.skill-loads/ 随目录移动),不得重建已归档的活动目录(归档移动语义)
|
|
2421
|
+
assertExit(runState(['init', 'arch-m5'], dir), 0);
|
|
2422
|
+
writeFile(dir, 'flow-kit/prompts/0-change.md', '# 阶段 0 · CHANGE\n\n## 角色\n\n你是 Changeer。\n');
|
|
2423
|
+
writeFile(dir, 'flow-kit/prompts/7-integration.md', '# 阶段 7 · INTEGRATION\n\n## 角色\n\n你是 Integrationer。\n');
|
|
2424
|
+
// 手动 skill-load(活动路径标记,随目录移动进归档)
|
|
2425
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
2426
|
+
assertExit(runState(['skill-load', 'archive', 'flow-comet-integration', '--prompt', 'flow-kit/prompts/7-integration.md'], dir), 0);
|
|
2427
|
+
// 推进到 archive
|
|
2428
|
+
const st0 = readStateFile(dir);
|
|
2429
|
+
st0.currentNode = 'archive';
|
|
2430
|
+
writeState(dir, st0);
|
|
2431
|
+
// 移动目录到归档(真实归档流程:先移动后 record/exit)
|
|
2432
|
+
fs.mkdirSync(path.join(dir, '.specs', 'archive'), { recursive: true });
|
|
2433
|
+
fs.renameSync(path.join(dir, '.specs', 'arch-m5'), path.join(dir, '.specs', 'archive', '2026-08-14-arch-m5'));
|
|
2434
|
+
// record archive(移动后)——M5 自动补 archive 声明标记
|
|
2435
|
+
const rec = runState(['record', 'archive', '{"summary":"archived"}'], dir);
|
|
2436
|
+
assertExit(rec, 0);
|
|
2437
|
+
// 断言 1:活动目录不得重建(归档移动语义——M5 不得重建已归档的活动目录)
|
|
2438
|
+
if (fs.existsSync(path.join(dir, '.specs', 'arch-m5'))) {
|
|
2439
|
+
throw new Error('M5 在归档后重建了活动目录(残留): .specs/arch-m5/');
|
|
2440
|
+
}
|
|
2441
|
+
// 断言 2:归档路径有 M5 自动补的 archive 标记(标记随目录移动后的归属路径)
|
|
2442
|
+
const markerInArchive = path.join(dir, '.specs', 'archive', '2026-08-14-arch-m5', '.skill-loads', 'archive-flow-comet-integration.json');
|
|
2443
|
+
if (!fs.existsSync(markerInArchive)) {
|
|
2444
|
+
throw new Error('归档路径缺 M5 自动补标记: ' + markerInArchive);
|
|
2445
|
+
}
|
|
2446
|
+
// 变体:change 从未 skill-load(活动与归档路径皆无 .skill-loads)→ record 不得重建
|
|
2447
|
+
// 活动目录(修复前 targetLoadsDir 回退活动路径,writeJson 的 mkdir 会重建已归档目录)
|
|
2448
|
+
assertExit(runState(['init', 'arch-m5b'], dir), 0);
|
|
2449
|
+
assertExit(runState(['skill-load', 'archive', 'flow-comet-integration'], dir), 0);
|
|
2450
|
+
const stB = readStateFile(dir);
|
|
2451
|
+
stB.currentNode = 'archive';
|
|
2452
|
+
writeState(dir, stB);
|
|
2453
|
+
fs.renameSync(path.join(dir, '.specs', 'arch-m5b'), path.join(dir, '.specs', 'archive', '2026-08-14-arch-m5b'));
|
|
2454
|
+
const recB = runState(['record', 'archive', '{"summary":"archived"}'], dir);
|
|
2455
|
+
assertExit(recB, 0);
|
|
2456
|
+
if (fs.existsSync(path.join(dir, '.specs', 'arch-m5b'))) {
|
|
2457
|
+
throw new Error('M5 在双路径皆无时重建了活动目录(残留): .specs/arch-m5b/');
|
|
2458
|
+
}
|
|
2459
|
+
// 变体:归档后 skill-load 报错须引导 record 自动补路径(级 4 实证:归档后手动声明
|
|
2460
|
+
// 不可行,消息须指明 M5 自动补仍可写归档路径标记)
|
|
2461
|
+
const slArchive = runState(['skill-load', 'archive', 'flow-comet-integration'], dir);
|
|
2462
|
+
assertExit(slArchive, 1);
|
|
2463
|
+
assertOut(slArchive, 'record 自动补写');
|
|
2464
|
+
},
|
|
2465
|
+
},
|
|
2466
|
+
|
|
2467
|
+
// ---------- I. 异常路径 ----------
|
|
2468
|
+
|
|
2469
|
+
{
|
|
2470
|
+
name: 'I1 异常路径:损坏状态文件 fail-closed',
|
|
2471
|
+
run: (dir) => {
|
|
2472
|
+
writeFile(dir, '.flow-comet/flow-comet-state.json', '{broken json');
|
|
2473
|
+
const res = runState(['status'], dir);
|
|
2474
|
+
assertExit(res, 1);
|
|
2475
|
+
},
|
|
2476
|
+
},
|
|
2477
|
+
|
|
2478
|
+
{
|
|
2479
|
+
name: 'I2 异常路径:缺工件出口拦截',
|
|
2480
|
+
run: (dir) => {
|
|
2481
|
+
const st = baseState('open');
|
|
2482
|
+
st.evidence.open = { summary: 'intake complete' };
|
|
2483
|
+
writeState(dir, st);
|
|
2484
|
+
// 工件根目录存在但工件缺失 → 缺产物拦截(工件根缺失走更前置的路径基座报错)
|
|
2485
|
+
fs.mkdirSync(path.join(dir, '.specs', CHANGE_ID), { recursive: true });
|
|
2486
|
+
const res = runGuard(['exit', 'open'], dir);
|
|
2487
|
+
assertExit(res, 1);
|
|
2488
|
+
assertOut(res, 'BLOCKED');
|
|
2489
|
+
assertOut(res, 'missing Output Schema artifacts');
|
|
2490
|
+
},
|
|
2491
|
+
},
|
|
2492
|
+
|
|
2493
|
+
{
|
|
2494
|
+
name: 'I3 异常路径:非法参数拒绝',
|
|
2495
|
+
run: (dir) => {
|
|
2496
|
+
// guard 缺节点 / 未知节点
|
|
2497
|
+
const noNode = runGuard(['exit'], dir);
|
|
2498
|
+
assertExit(noNode, 1);
|
|
2499
|
+
assertOut(noNode, 'exit requires a Node id.');
|
|
2500
|
+
const unknownNode = runGuard(['exit', 'bogus'], dir);
|
|
2501
|
+
assertExit(unknownNode, 1);
|
|
2502
|
+
assertOut(unknownNode, 'Unknown workflow Node: bogus');
|
|
2503
|
+
// state 未知命令
|
|
2504
|
+
const unknownCmd = runState(['bogus-cmd'], dir);
|
|
2505
|
+
assertExit(unknownCmd, 1);
|
|
2506
|
+
assertOut(unknownCmd, 'Unknown command');
|
|
2507
|
+
// state 无活跃项目
|
|
2508
|
+
const noActive = runState(['next'], dir);
|
|
2509
|
+
assertExit(noActive, 0);
|
|
2510
|
+
assertOut(noActive, 'No active change');
|
|
2511
|
+
// handoff 未知动作
|
|
2512
|
+
const badAction = runHandoff(['bogus'], dir);
|
|
2513
|
+
assertExit(badAction, 1);
|
|
2514
|
+
assertOut(badAction, 'Unknown action');
|
|
2515
|
+
},
|
|
2516
|
+
},
|
|
2517
|
+
|
|
2518
|
+
{
|
|
2519
|
+
name: 'I4 异常路径:状态字段类型非法拦截',
|
|
2520
|
+
run: (dir) => {
|
|
2521
|
+
const st = baseState('open');
|
|
2522
|
+
st.currentNode = 123; // 类型非法(number)
|
|
2523
|
+
writeState(dir, st);
|
|
2524
|
+
// advance 先按文件推导重写 currentNode(漂移校正),写入时校验非法字段(completedNodes
|
|
2525
|
+
// 残留了数值条目)→ fail-closed 拦截——非法字段名随路径而异,断言通用消息
|
|
2526
|
+
const res = runState(['advance'], dir);
|
|
2527
|
+
assertExit(res, 1);
|
|
2528
|
+
assertOut(res, 'BLOCKED: state 字段类型非法');
|
|
2529
|
+
},
|
|
2530
|
+
},
|
|
2531
|
+
|
|
2532
|
+
// ---------- J. 文档一致性 ----------
|
|
2533
|
+
|
|
2534
|
+
{
|
|
2535
|
+
name: 'J1 文档一致性:双语健康检查(本地工具)',
|
|
2536
|
+
run: (dir) => {
|
|
2537
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2538
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无仓库文档
|
|
2539
|
+
const checker = path.join(repoRoot, 'scripts', 'check-docs-local.mjs');
|
|
2540
|
+
if (!fs.existsSync(checker)) {
|
|
2541
|
+
console.log(' (本地工具缺失,跳过——gitignore 不随克隆分发)');
|
|
2542
|
+
return;
|
|
2543
|
+
}
|
|
2544
|
+
const res = runLocalTool(checker, repoRoot);
|
|
2545
|
+
if (res.status === 0) {
|
|
2546
|
+
assertOut(res, '文档健康检查通过');
|
|
2547
|
+
return;
|
|
2548
|
+
}
|
|
2549
|
+
// 已知本地工具快照局限:其「场景数与版本一致性」子检查用发布时快照(1.3.0 → 97),
|
|
2550
|
+
// 开发窗口期(版本未发布、场景数已增)会误报——结构检查(双语/对称)全部通过即放行;
|
|
2551
|
+
// 场景数一致性的权威判定在引擎自测(guard-self-test 场景数自检按实时场景数核对)。
|
|
2552
|
+
const structuralOk = res.output.includes('PASS: 英文文档无中文')
|
|
2553
|
+
&& res.output.includes('PASS: 中文文档无英文长句')
|
|
2554
|
+
&& res.output.includes('PASS: 双语镜像对称');
|
|
2555
|
+
if (!structuralOk || !res.output.includes('场景数与版本一致性')) {
|
|
2556
|
+
throw new Error('期望 exit 0,实际 exit ' + res.status + '\n实际输出:\n' + res.output);
|
|
2557
|
+
}
|
|
2558
|
+
console.log(' (场景数一致性子检查按发布快照——当前开发窗口由引擎自测的实时自检覆盖,放行)');
|
|
2559
|
+
},
|
|
2560
|
+
},
|
|
2561
|
+
|
|
2562
|
+
{
|
|
2563
|
+
name: 'J2 文档一致性:公开产物零代号检查(本地工具)',
|
|
2564
|
+
run: (dir) => {
|
|
2565
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2566
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return;
|
|
2567
|
+
const checker = path.join(repoRoot, 'scripts', 'check-codes-local.mjs');
|
|
2568
|
+
if (!fs.existsSync(checker)) {
|
|
2569
|
+
console.log(' (本地工具缺失,跳过——gitignore 不随克隆分发)');
|
|
2570
|
+
return;
|
|
2571
|
+
}
|
|
2572
|
+
const res = runLocalTool(checker, repoRoot);
|
|
2573
|
+
assertExit(res, 0);
|
|
2574
|
+
assertOut(res, '过程代号检查通过');
|
|
2575
|
+
},
|
|
2576
|
+
},
|
|
2577
|
+
|
|
2578
|
+
// ---------- K. 安装器 ----------
|
|
2579
|
+
|
|
2580
|
+
{
|
|
2581
|
+
name: 'K1 安装器:版本标识由安装器生成且精确反映源仓库状态',
|
|
2582
|
+
run: (dir) => {
|
|
2583
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2584
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
2585
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
2586
|
+
if (!fs.existsSync(installer)) throw new Error('缺少 prepare-env.mjs');
|
|
2587
|
+
const target = path.join(dir, 'j3-target');
|
|
2588
|
+
fs.mkdirSync(target, { recursive: true });
|
|
2589
|
+
const res = spawnSync(process.execPath, [installer, '--target', target], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2590
|
+
if (res.status !== 0) throw new Error('prepare-env 失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
2591
|
+
// 分发主干:权威源随技能包分发的静态文件(手动复制也有效,不依赖安装脚本)
|
|
2592
|
+
const srcVersionFile = path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
2593
|
+
if (!fs.existsSync(srcVersionFile)) throw new Error('缺少权威源 INSTALLED_VERSION(随技能包分发)');
|
|
2594
|
+
const srcVersion = fs.readFileSync(srcVersionFile, 'utf8').trim();
|
|
2595
|
+
if (!srcVersion) throw new Error('权威源版本标识为空');
|
|
2596
|
+
// 权威源须与 CHANGELOG 首个版本段一致(发布批次更新;CI release-consistency 同规则);
|
|
2597
|
+
// 版本段可带 prerelease 后缀(如 -rc.1 / -beta-1)
|
|
2598
|
+
const changelog = fs.readFileSync(path.join(repoRoot, 'CHANGELOG.md'), 'utf8');
|
|
2599
|
+
const m = changelog.match(/^## \[(\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?)\]/m);
|
|
2600
|
+
const expected = m ? m[1] : 'unreleased';
|
|
2601
|
+
if (srcVersion !== expected) throw new Error('权威源版本标识不符: ' + srcVersion + ' ≠ CHANGELOG 版本 ' + expected);
|
|
2602
|
+
const versionFile = path.join(target, '.claude', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
2603
|
+
if (!fs.existsSync(versionFile)) throw new Error('缺少 INSTALLED_VERSION(版本标识文件)');
|
|
2604
|
+
const installed = fs.readFileSync(versionFile, 'utf8').trim();
|
|
2605
|
+
if (!installed) throw new Error('版本标识为空');
|
|
2606
|
+
// 格式:发布版本号(1.3.1) / prerelease(1.5.0-rc.1 或 1.5.0-beta-1) / git describe 开发态(1.3.1-N-g<hash>) / unreleased(无 tag 兜底)
|
|
2607
|
+
const ok = /^\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(-\d+-g[0-9a-f]+)?$/.test(installed) || installed === 'unreleased';
|
|
2608
|
+
if (!ok) throw new Error('版本标识格式异常: ' + installed);
|
|
2609
|
+
console.log(' 版本标识 = ' + installed + '(git describe 或权威源兜底——多人协作精确检测)✓');
|
|
2610
|
+
},
|
|
2611
|
+
},
|
|
2612
|
+
|
|
2613
|
+
// ---------- K 扩展:多平台安装器框架(1.4.0) ----------
|
|
2614
|
+
|
|
2615
|
+
// K2: codex 平台安装冒烟——技能落 .agents/skills/ + SKILL 路径平台化替换 + .codex/hooks.json 注入
|
|
2616
|
+
// (含 --platform codex 托管命令)+ AGENTS.md 托管区内联 + 纯 codex 不生成 .claude/ + 版本标识
|
|
2617
|
+
{
|
|
2618
|
+
name: 'K2 安装器:codex 平台安装(技能/路径替换/hooks.json/AGENTS 托管区)',
|
|
2619
|
+
run: (dir) => {
|
|
2620
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2621
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
2622
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
2623
|
+
const target = path.join(dir, 'k2-target');
|
|
2624
|
+
fs.mkdirSync(target, { recursive: true });
|
|
2625
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2626
|
+
if (res.status !== 0) throw new Error('prepare-env --platform codex 失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
2627
|
+
// ① 技能落 .agents/skills/(Codex 自动发现位置)
|
|
2628
|
+
const skillMd = path.join(target, '.agents', 'skills', 'flow-comet', 'SKILL.md');
|
|
2629
|
+
if (!fs.existsSync(skillMd)) throw new Error('codex 平台技能未安装到 .agents/skills/');
|
|
2630
|
+
// ② SKILL 路径平台化替换生效(命令路径 .claude/skills → .agents/skills)
|
|
2631
|
+
const text = fs.readFileSync(skillMd, 'utf8');
|
|
2632
|
+
if (!text.includes('.agents/skills/flow-comet/scripts/')) throw new Error('SKILL 命令路径未替换为 .agents/skills/');
|
|
2633
|
+
if (text.includes('.claude/skills/flow-comet/scripts/')) throw new Error('SKILL 仍含 .claude/skills/flow-comet/scripts/ 命令路径');
|
|
2634
|
+
// ③ .codex/hooks.json 注入托管 hook(顶层 hooks 包裹层 + matcher * + 平台标记)
|
|
2635
|
+
const hooksFile = path.join(target, '.codex', 'hooks.json');
|
|
2636
|
+
if (!fs.existsSync(hooksFile)) throw new Error('缺少 .codex/hooks.json');
|
|
2637
|
+
const hooksParsed = JSON.parse(fs.readFileSync(hooksFile, 'utf8'));
|
|
2638
|
+
const preToolUse = hooksParsed.hooks && hooksParsed.hooks.PreToolUse;
|
|
2639
|
+
if (!Array.isArray(preToolUse) || preToolUse.length === 0) throw new Error('hooks.json 缺 hooks.PreToolUse 包裹层');
|
|
2640
|
+
// 托管条目断言形态无关化(断言放宽决策——意图为托管条目在场且指向本项目脚本):脚本 basename 在位 + 项目根引用特征二选一——
|
|
2641
|
+
// 兼容新旧两种托管形态,注入命令实测微调(%VAR% 引号/变量词形)不再破坏断言
|
|
2642
|
+
const managedCmds = collectManagedHookCommands(hooksParsed);
|
|
2643
|
+
if (managedCmds.length === 0) throw new Error('hooks.json 未注入托管 hook 命令');
|
|
2644
|
+
for (const c of managedCmds) assertManagedHookEntry(c, 'codex hooks.json 托管命令');
|
|
2645
|
+
if (!JSON.stringify(hooksParsed).includes('--platform codex')) throw new Error('hooks.json hook 命令缺平台标记');
|
|
2646
|
+
if (preToolUse[0].matcher !== '*') throw new Error('hooks.json matcher 应为 * (Codex PreToolUse 只拦截 Bash 工具): ' + JSON.stringify(preToolUse[0]));
|
|
2647
|
+
// ③b .codex/config.toml hooks 启用(features——Codex hooks 默认关闭)
|
|
2648
|
+
const configFile = path.join(target, '.codex', 'config.toml');
|
|
2649
|
+
if (!fs.existsSync(configFile)) throw new Error('缺少 .codex/config.toml');
|
|
2650
|
+
const configText = fs.readFileSync(configFile, 'utf8');
|
|
2651
|
+
if (!/\[features\]/.test(configText) || !/hooks\s*=\s*true/.test(configText)) throw new Error('config.toml 未启用 hooks(features)');
|
|
2652
|
+
// ④ AGENTS.md 托管区(内联 orchestration 全文——Codex 指令唯一自动加载路径)
|
|
2653
|
+
const agentsFile = path.join(target, 'AGENTS.md');
|
|
2654
|
+
if (!fs.existsSync(agentsFile)) throw new Error('缺少 AGENTS.md');
|
|
2655
|
+
const agents = fs.readFileSync(agentsFile, 'utf8');
|
|
2656
|
+
if (!agents.includes('Managed by flow-comet')) throw new Error('AGENTS.md 无托管标记');
|
|
2657
|
+
if (!agents.includes('flow-comet Orchestration')) throw new Error('AGENTS.md 未内联 orchestration 内容');
|
|
2658
|
+
// ⑤ 纯 codex 平台不生成 .claude/
|
|
2659
|
+
if (fs.existsSync(path.join(target, '.claude'))) throw new Error('codex 平台不应生成 .claude/');
|
|
2660
|
+
// ⑥ 版本标识随技能包分发(平台化路径)
|
|
2661
|
+
const verFile = path.join(target, '.agents', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
2662
|
+
if (!fs.existsSync(verFile)) throw new Error('缺少 INSTALLED_VERSION(版本标识文件)');
|
|
2663
|
+
const installed = fs.readFileSync(verFile, 'utf8').trim();
|
|
2664
|
+
if (!installed) throw new Error('版本标识为空');
|
|
2665
|
+
// ⑦ .codex/hooks.json 非法 JSON → fail-safe(不覆盖用户配置,报错退出)
|
|
2666
|
+
const badTarget = path.join(dir, 'k2-bad-hooks');
|
|
2667
|
+
fs.mkdirSync(path.join(badTarget, '.codex'), { recursive: true });
|
|
2668
|
+
fs.writeFileSync(path.join(badTarget, '.codex', 'hooks.json'), '{ not json', 'utf8');
|
|
2669
|
+
const badRes = spawnSync(process.execPath, [installer, '--target', badTarget, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2670
|
+
if (badRes.status === 0) throw new Error('非法 hooks.json 应导致安装失败(fail-safe)');
|
|
2671
|
+
const badOut = String(badRes.stderr || '') + String(badRes.stdout || '');
|
|
2672
|
+
if (!badOut.includes('已存在但内容非法')) throw new Error('非法 hooks.json 应输出 fail-safe 提示: ' + badOut);
|
|
2673
|
+
},
|
|
2674
|
+
},
|
|
2675
|
+
|
|
2676
|
+
// K3: hook 平台分支输出契约——codex 分支 stdout 为合法 JSON(拦截含 decision:"block";放行可解析非 block);
|
|
2677
|
+
// claude-code 分支(无平台标记)输出与现状一致(文本 workflow-hook-guard-ok)
|
|
2678
|
+
{
|
|
2679
|
+
name: 'K3 hook:codex 平台分支 JSON 契约(拦截/放行)+ CC 分支不变',
|
|
2680
|
+
run: (dir) => {
|
|
2681
|
+
// ① codex 分支:写越权路径(源码)→ stdout 合法 JSON + decision block
|
|
2682
|
+
const st = baseState('open');
|
|
2683
|
+
st.status = 'running';
|
|
2684
|
+
writeState(dir, st);
|
|
2685
|
+
fs.mkdirSync(path.join(dir, '.specs', CHANGE_ID), { recursive: true });
|
|
2686
|
+
const block = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2687
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'evil.py') } });
|
|
2688
|
+
assertExit(block, 0);
|
|
2689
|
+
const blockParsed = JSON.parse(block.output.trim()); // stdout 非 JSON → throw(RED)
|
|
2690
|
+
if (blockParsed.decision !== 'block') throw new Error('codex 拦截输出应含 decision:"block": ' + JSON.stringify(blockParsed));
|
|
2691
|
+
// ② codex 分支:写 .specs/ 工件 → 放行(输出可解析、非 block)
|
|
2692
|
+
const ok = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2693
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'CHANGE.md') } });
|
|
2694
|
+
assertExit(ok, 0);
|
|
2695
|
+
const okParsed = JSON.parse(ok.output.trim()); // 放行输出须为合法 JSON(空串解析失败 → RED)
|
|
2696
|
+
if (okParsed.decision === 'block') throw new Error('codex 放行输出不应含 decision block');
|
|
2697
|
+
// ③ codex 分支:无活跃 workflow → 放行(JSON 可解析)
|
|
2698
|
+
fs.rmSync(path.join(dir, '.flow-comet'), { recursive: true, force: true });
|
|
2699
|
+
const noActive = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2700
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'x.py') } });
|
|
2701
|
+
assertExit(noActive, 0);
|
|
2702
|
+
JSON.parse(noActive.output.trim());
|
|
2703
|
+
// ④ claude-code 分支(无平台标记):输出与现状一致(文本 workflow-hook-guard-ok)
|
|
2704
|
+
writeState(dir, { ...baseState('open'), status: 'running' });
|
|
2705
|
+
const cc = runHook(['before_tool'], dir,
|
|
2706
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'CHANGE.md') } });
|
|
2707
|
+
assertExit(cc, 0);
|
|
2708
|
+
assertOut(cc, 'workflow-hook-guard-ok');
|
|
2709
|
+
assertNotOut(cc, 'decision');
|
|
2710
|
+
// ⑤ codex 分支:Bash 写入检测(PowerShell 命令字符串——Codex 写路径经 Bash 工具,无 file_path)
|
|
2711
|
+
const bashBlock = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2712
|
+
{ tool_name: 'Bash', tool_input: { command: 'Set-Content -LiteralPath "src/evil.py" -Value "x=1"' } });
|
|
2713
|
+
assertExit(bashBlock, 0);
|
|
2714
|
+
const bashBlockParsed = JSON.parse(bashBlock.output.trim());
|
|
2715
|
+
if (bashBlockParsed.decision !== 'block') throw new Error('codex Bash 越权写入应输出 decision block: ' + JSON.stringify(bashBlockParsed));
|
|
2716
|
+
// ⑥ codex 分支:Bash 合法写 .specs → 放行
|
|
2717
|
+
const bashOk = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2718
|
+
{ tool_name: 'Bash', tool_input: { command: 'Set-Content -LiteralPath ".specs/ch/CHANGE.md" -Value "# CHANGE"' } });
|
|
2719
|
+
assertExit(bashOk, 0);
|
|
2720
|
+
const bashOkParsed = JSON.parse(bashOk.output.trim());
|
|
2721
|
+
if (bashOkParsed.decision === 'block') throw new Error('codex Bash 合法写入不应 block');
|
|
2722
|
+
// ⑦ codex 分支:Bash 无写入模式(纯命令)→ 放行
|
|
2723
|
+
const bashNone = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2724
|
+
{ tool_name: 'Bash', tool_input: { command: 'python -m pytest test_calculator.py -q' } });
|
|
2725
|
+
assertExit(bashNone, 0);
|
|
2726
|
+
JSON.parse(bashNone.output.trim());
|
|
2727
|
+
// ⑧ codex 分支:读类 cmdlet(Get-Content -Path)→ 无写入语义,放行
|
|
2728
|
+
const bashRead = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2729
|
+
{ tool_name: 'Bash', tool_input: { command: 'Get-Content -Path "src/readme.md"' } });
|
|
2730
|
+
assertExit(bashRead, 0);
|
|
2731
|
+
const bashReadParsed = JSON.parse(bashRead.output.trim());
|
|
2732
|
+
if (bashReadParsed.decision === 'block') throw new Error('codex 读类 cmdlet(Get-Content)不应 block');
|
|
2733
|
+
// ⑨ codex 分支:.NET File.Copy 目标 = 第二参数(越权目标 → block;合法目标 → 放行)
|
|
2734
|
+
const copyBlock = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2735
|
+
{ tool_name: 'Bash', tool_input: { command: '[System.IO.File]::Copy("src/a.txt", "src/b.txt")' } });
|
|
2736
|
+
assertExit(copyBlock, 0);
|
|
2737
|
+
const copyParsed = JSON.parse(copyBlock.output.trim());
|
|
2738
|
+
if (copyParsed.decision !== 'block') throw new Error('codex File.Copy 越权目标(第二参数)应 block');
|
|
2739
|
+
const copyOk = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
2740
|
+
{ tool_name: 'Bash', tool_input: { command: '[System.IO.File]::Copy("src/a.txt", ".specs/ch/b.txt")' } });
|
|
2741
|
+
assertExit(copyOk, 0);
|
|
2742
|
+
const copyOkParsed = JSON.parse(copyOk.output.trim());
|
|
2743
|
+
if (copyOkParsed.decision === 'block') throw new Error('codex File.Copy 合法目标不应 block');
|
|
2744
|
+
},
|
|
2745
|
+
},
|
|
2746
|
+
|
|
2747
|
+
// K4: 平台选择链——--platform 显式(两平台互不串扰)/ 无 TTY 探测(项目已有 .codex/ 或 .claude/)/ 两者皆无默认
|
|
2748
|
+
{
|
|
2749
|
+
name: 'K4 安装器:平台选择链(显式/无 TTY 探测/默认)',
|
|
2750
|
+
run: (dir) => {
|
|
2751
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2752
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return;
|
|
2753
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
2754
|
+
const run = (target, envOverrides = {}) => spawnSync(process.execPath, [installer, '--target', target], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, ...envOverrides } });
|
|
2755
|
+
// ① --platform claude-code 显式 → .claude/skills 生成,.agents 不生成
|
|
2756
|
+
const t1 = path.join(dir, 'k4-cc');
|
|
2757
|
+
fs.mkdirSync(t1, { recursive: true });
|
|
2758
|
+
const r1 = spawnSync(process.execPath, [installer, '--target', t1, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2759
|
+
if (r1.status !== 0) throw new Error('--platform claude-code 失败: ' + (r1.stderr || JSON.stringify(r1.output)));
|
|
2760
|
+
if (!fs.existsSync(path.join(t1, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('claude-code 平台未生成 .claude/skills/');
|
|
2761
|
+
if (fs.existsSync(path.join(t1, '.agents'))) throw new Error('claude-code 平台不应生成 .agents/');
|
|
2762
|
+
// ② --platform codex 显式 → .agents/skills 生成,.claude 不生成
|
|
2763
|
+
const t2 = path.join(dir, 'k4-codex');
|
|
2764
|
+
fs.mkdirSync(t2, { recursive: true });
|
|
2765
|
+
const r2 = spawnSync(process.execPath, [installer, '--target', t2, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2766
|
+
if (r2.status !== 0) throw new Error('--platform codex 失败: ' + (r2.stderr || JSON.stringify(r2.output)));
|
|
2767
|
+
if (!fs.existsSync(path.join(t2, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('codex 平台未生成 .agents/skills/');
|
|
2768
|
+
if (fs.existsSync(path.join(t2, '.claude'))) throw new Error('codex 平台不应生成 .claude/');
|
|
2769
|
+
// ③ 无 --platform(spawnSync 无 TTY)+ 目标已有 .codex/ → 探测为 codex
|
|
2770
|
+
const t3 = path.join(dir, 'k4-probe-codex');
|
|
2771
|
+
fs.mkdirSync(path.join(t3, '.codex'), { recursive: true });
|
|
2772
|
+
const r3 = run(t3);
|
|
2773
|
+
if (r3.status !== 0) throw new Error('探测 codex 安装失败: ' + (r3.stderr || JSON.stringify(r3.output)));
|
|
2774
|
+
if (!fs.existsSync(path.join(t3, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('已有 .codex/ 的项目应探测为 codex 平台');
|
|
2775
|
+
// ④ 无 --platform + 目标已有 .claude/ → 探测为 claude-code
|
|
2776
|
+
const t4 = path.join(dir, 'k4-probe-cc');
|
|
2777
|
+
fs.mkdirSync(path.join(t4, '.claude'), { recursive: true });
|
|
2778
|
+
const r4 = run(t4);
|
|
2779
|
+
if (r4.status !== 0) throw new Error('探测 claude-code 安装失败: ' + (r4.stderr || JSON.stringify(r4.output)));
|
|
2780
|
+
if (!fs.existsSync(path.join(t4, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('已有 .claude/ 的项目应探测为 claude-code 平台');
|
|
2781
|
+
if (fs.existsSync(path.join(t4, '.agents'))) throw new Error('探测为 claude-code 时不应生成 .agents/');
|
|
2782
|
+
// ⑤ 无 --platform + 两者皆无 → 默认 claude-code
|
|
2783
|
+
const t5 = path.join(dir, 'k4-default');
|
|
2784
|
+
fs.mkdirSync(t5, { recursive: true });
|
|
2785
|
+
const r5 = run(t5);
|
|
2786
|
+
if (r5.status !== 0) throw new Error('默认平台安装失败: ' + (r5.stderr || JSON.stringify(r5.output)));
|
|
2787
|
+
if (!fs.existsSync(path.join(t5, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('两者皆无应默认 claude-code');
|
|
2788
|
+
// ⑥ --platform 未知平台 → 报错拒绝(不静默回退)
|
|
2789
|
+
const t6 = path.join(dir, 'k4-unknown');
|
|
2790
|
+
fs.mkdirSync(t6, { recursive: true });
|
|
2791
|
+
const r6 = spawnSync(process.execPath, [installer, '--target', t6, '--platform', 'bogus'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2792
|
+
if (r6.status === 0) throw new Error('未知平台应报错拒绝');
|
|
2793
|
+
const out6 = String(r6.stderr || '') + String(r6.stdout || '');
|
|
2794
|
+
if (!out6.includes('未知平台')) throw new Error('未知平台错误信息应含"未知平台": ' + out6);
|
|
2795
|
+
// ⑦ 无 --platform + 目标同时有 .claude/ 与 .codex/ 痕迹 → 不武断二选一:
|
|
2796
|
+
// 默认 claude-code(主平台)+ 输出多痕迹提示(修复前探测 .codex/ 优先会武断只装 codex)
|
|
2797
|
+
const t7 = path.join(dir, 'k4-dual-trace');
|
|
2798
|
+
fs.mkdirSync(path.join(t7, '.claude'), { recursive: true });
|
|
2799
|
+
fs.mkdirSync(path.join(t7, '.codex'), { recursive: true });
|
|
2800
|
+
const r7 = run(t7);
|
|
2801
|
+
if (r7.status !== 0) throw new Error('双痕迹项目安装失败: ' + (r7.stderr || JSON.stringify(r7.output)));
|
|
2802
|
+
if (!fs.existsSync(path.join(t7, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) {
|
|
2803
|
+
throw new Error('双痕迹项目应默认安装 claude-code(主平台),而非武断只装 codex');
|
|
2804
|
+
}
|
|
2805
|
+
if (fs.existsSync(path.join(t7, '.agents'))) throw new Error('双痕迹项目默认 claude-code 时不应生成 .agents/');
|
|
2806
|
+
const out7 = String(r7.stdout || '') + String(r7.stderr || '');
|
|
2807
|
+
if (!out7.includes('同时有 .claude/、.codex/ 或 .dsh/ 中的多个痕迹')) throw new Error('多痕迹无 TTY 应输出多痕迹提示: ' + out7);
|
|
2808
|
+
// ⑧ TTY 多选(交互模拟——平台选择链缺省路径):覆盖 stdin.isTTY + 管道喂入 '1,3'
|
|
2809
|
+
// (Claude Code + dsh——三平台选项,旧 both 已移除)→ 双平台产物同时生成;
|
|
2810
|
+
// dsh 副本命令路径平台化(.dsh 形态,不含 .claude 路径);DSH_HOME 指向临时目录
|
|
2811
|
+
// (断言全程不写真实 ~/.dsh)
|
|
2812
|
+
const t8 = path.join(dir, 'k4-multiselect');
|
|
2813
|
+
fs.mkdirSync(t8, { recursive: true });
|
|
2814
|
+
const dshHome8 = path.join(dir, 'k4-dsh-home');
|
|
2815
|
+
const installerUrl = pathToFileURL(installer).href;
|
|
2816
|
+
const selectScript =
|
|
2817
|
+
`Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true });` +
|
|
2818
|
+
`process.argv = ['node', 'prepare-env', '--target', ${JSON.stringify(t8)}];` +
|
|
2819
|
+
`await import(${JSON.stringify(installerUrl)});`;
|
|
2820
|
+
const r8 = spawnSync(process.execPath, ['--input-type=module', '-e', selectScript], { cwd: repoRoot, encoding: 'utf8', input: '1,3\n', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome8 } });
|
|
2821
|
+
if (r8.status !== 0) throw new Error('TTY 多选安装失败: ' + (r8.stderr || JSON.stringify(r8.output)));
|
|
2822
|
+
if (!fs.existsSync(path.join(t8, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('TTY 多选后 .claude/skills 应生成');
|
|
2823
|
+
if (!fs.existsSync(path.join(t8, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('TTY 多选后 .dsh/skills 应生成');
|
|
2824
|
+
if (fs.existsSync(path.join(t8, '.agents'))) throw new Error('TTY 多选(1,3)不应生成 .agents/(未选 codex)');
|
|
2825
|
+
const dshSkillText8 = fs.readFileSync(path.join(t8, '.dsh', 'skills', 'flow-comet', 'SKILL.md'), 'utf8');
|
|
2826
|
+
if (dshSkillText8.includes('.claude/skills/flow-comet/scripts/')) {
|
|
2827
|
+
throw new Error('TTY 多选后 dsh 副本命令路径应为 .dsh 形态');
|
|
2828
|
+
}
|
|
2829
|
+
if (!fs.existsSync(path.join(dshHome8, 'plugins', 'dsh-flow-comet-bridge.mjs'))) {
|
|
2830
|
+
throw new Error('TTY 多选后桥接 loader 应复制到临时 DSH_HOME(不写真实 ~/.dsh)');
|
|
2831
|
+
}
|
|
2832
|
+
// ⑨ 仅 .dsh/ 痕迹 → 无 TTY 探测为 dsh(三痕迹探测语义)
|
|
2833
|
+
const t9 = path.join(dir, 'k4-probe-dsh');
|
|
2834
|
+
fs.mkdirSync(path.join(t9, '.dsh'), { recursive: true });
|
|
2835
|
+
const r9 = run(t9, { DSH_HOME: path.join(dir, 'k4-probe-dsh-home') });
|
|
2836
|
+
if (r9.status !== 0) throw new Error('探测 dsh 安装失败: ' + (r9.stderr || JSON.stringify(r9.output)));
|
|
2837
|
+
if (!fs.existsSync(path.join(t9, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('已有 .dsh/ 的项目应探测为 dsh 平台');
|
|
2838
|
+
// ⑩ 显式逗号多平台:--platform claude-code,dsh → 两平台产物同时生成(CI/无 TTY 路径)
|
|
2839
|
+
const t10 = path.join(dir, 'k4-comma');
|
|
2840
|
+
fs.mkdirSync(t10, { recursive: true });
|
|
2841
|
+
const r10 = spawnSync(process.execPath, [installer, '--target', t10, '--platform', 'claude-code,dsh'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: path.join(dir, 'k4-comma-home') } });
|
|
2842
|
+
if (r10.status !== 0) throw new Error('--platform claude-code,dsh 失败: ' + (r10.stderr || JSON.stringify(r10.output)));
|
|
2843
|
+
if (!fs.existsSync(path.join(t10, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('逗号多平台应生成 .claude/skills');
|
|
2844
|
+
if (!fs.existsSync(path.join(t10, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('逗号多平台应生成 .dsh/skills');
|
|
2845
|
+
if (fs.existsSync(path.join(t10, '.agents'))) throw new Error('逗号多平台(claude-code,dsh)不应生成 .agents/');
|
|
2846
|
+
},
|
|
2847
|
+
},
|
|
2848
|
+
|
|
2849
|
+
// K5: purge 语义——缺 --yes 拒绝(不破坏)/ --purge --yes 清理重建 / 用户内容(生成物外)保留
|
|
2850
|
+
{
|
|
2851
|
+
name: 'K5 安装器:purge 语义(缺 --yes 拒绝/重建/用户内容保留)',
|
|
2852
|
+
run: (dir) => {
|
|
2853
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2854
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return;
|
|
2855
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
2856
|
+
const run = (target, extra) => spawnSync(process.execPath, [installer, '--target', target, ...extra], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
2857
|
+
// ① codex 平台:首次安装 + 用户内容(项目根,非生成物)
|
|
2858
|
+
const target = path.join(dir, 'k5-codex');
|
|
2859
|
+
fs.mkdirSync(target, { recursive: true });
|
|
2860
|
+
const first = run(target, ['--platform', 'codex']);
|
|
2861
|
+
if (first.status !== 0) throw new Error('codex 首次安装失败: ' + (first.stderr || JSON.stringify(first.output)));
|
|
2862
|
+
fs.writeFileSync(path.join(target, 'USER_KEEP.md'), '# 用户内容(保留)\n', 'utf8');
|
|
2863
|
+
// ② 缺 --yes → 拒绝且不破坏(错误提示含 --yes;生成物与用户内容仍在)
|
|
2864
|
+
const dry = run(target, ['--platform', 'codex', '--purge']);
|
|
2865
|
+
if (dry.status === 0) throw new Error('--purge 缺 --yes 应失败');
|
|
2866
|
+
const dryOut = String(dry.stderr || '') + String(dry.stdout || '');
|
|
2867
|
+
if (!dryOut.includes('--yes')) throw new Error('--purge 缺 --yes 错误信息应提示 --yes: ' + dryOut);
|
|
2868
|
+
if (!fs.existsSync(path.join(target, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge 拒绝后生成物不应被删除');
|
|
2869
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge 拒绝后用户内容不应被删除');
|
|
2870
|
+
// ②b 写入用户自有资产(.agents/skills/ 用户技能 + hooks.json 用户条目)——purge 后须保留
|
|
2871
|
+
fs.mkdirSync(path.join(target, '.agents', 'skills', 'user-own-skill'), { recursive: true });
|
|
2872
|
+
fs.writeFileSync(path.join(target, '.agents', 'skills', 'user-own-skill', 'SKILL.md'), '# user skill\n', 'utf8');
|
|
2873
|
+
const userHooksPath = path.join(target, '.codex', 'hooks.json');
|
|
2874
|
+
const userHooks = JSON.parse(fs.readFileSync(userHooksPath, 'utf8'));
|
|
2875
|
+
userHooks.hooks.PreToolUse.push({ matcher: 'UserOwn', hooks: [{ type: 'command', command: 'node user-own-hook.mjs' }] });
|
|
2876
|
+
fs.writeFileSync(userHooksPath, JSON.stringify(userHooks, null, 2) + '\n', 'utf8');
|
|
2877
|
+
// ③ --purge --yes → 清理重建(仅 flow-comet 技能,用户资产保留)
|
|
2878
|
+
const ok = run(target, ['--platform', 'codex', '--purge', '--yes']);
|
|
2879
|
+
if (ok.status !== 0) throw new Error('--purge --yes 失败: ' + (ok.stderr || JSON.stringify(ok.output)));
|
|
2880
|
+
if (!fs.existsSync(path.join(target, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge --yes 后 flow-comet 技能应重新生成');
|
|
2881
|
+
if (!fs.existsSync(path.join(target, '.agents', 'skills', 'user-own-skill', 'SKILL.md'))) throw new Error('--purge --yes 后用户技能应保留(.agents/ 为共享目录)');
|
|
2882
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge --yes 后用户内容应保留');
|
|
2883
|
+
const hooksText = fs.readFileSync(path.join(target, '.codex', 'hooks.json'), 'utf8');
|
|
2884
|
+
if ((hooksText.match(/comet-hook-guard\.mjs/g) || []).length !== 1) throw new Error('--purge --yes 后 hooks.json 应含 1 条重建托管条目: ' + hooksText);
|
|
2885
|
+
if (!hooksText.includes('user-own-hook.mjs')) throw new Error('--purge --yes 后用户 hook 条目应保留: ' + hooksText);
|
|
2886
|
+
const agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
2887
|
+
if (!agents.includes('Managed by flow-comet')) throw new Error('--purge --yes 后 AGENTS.md 托管区应重建');
|
|
2888
|
+
// ④ claude-code 平台同样:缺 --yes 拒绝 + --purge --yes 重建 + 用户内容保留
|
|
2889
|
+
const tcc = path.join(dir, 'k5-cc');
|
|
2890
|
+
fs.mkdirSync(tcc, { recursive: true });
|
|
2891
|
+
const ccFirst = run(tcc, []);
|
|
2892
|
+
if (ccFirst.status !== 0) throw new Error('claude-code 首次安装失败: ' + (ccFirst.stderr || JSON.stringify(ccFirst.output)));
|
|
2893
|
+
fs.writeFileSync(path.join(tcc, 'USER_KEEP.md'), '# 用户内容(保留)\n', 'utf8');
|
|
2894
|
+
// ⑤ matcher 演进幂等:把托管组 matcher 改为旧形态(Write|Edit——模拟历史版本注入),
|
|
2895
|
+
// 重装(非 purge)后旧托管组应被清理——settings 仅剩当前 matcher 组、无空 hooks 组
|
|
2896
|
+
const settingsPath = path.join(tcc, '.claude', 'settings.local.json');
|
|
2897
|
+
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
2898
|
+
for (const g of settings.hooks.PreToolUse) {
|
|
2899
|
+
if (g.matcher === 'Write|Edit|Bash') g.matcher = 'Write|Edit';
|
|
2900
|
+
}
|
|
2901
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
|
|
2902
|
+
const reinstall = run(tcc, []);
|
|
2903
|
+
if (reinstall.status !== 0) throw new Error('matcher 演进重装失败: ' + (reinstall.stderr || JSON.stringify(reinstall.output)));
|
|
2904
|
+
const after = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
2905
|
+
const groups = after.hooks.PreToolUse;
|
|
2906
|
+
if (groups.some((g) => Array.isArray(g.hooks) && g.hooks.length === 0)) {
|
|
2907
|
+
throw new Error('重装后存在空 hooks 组(matcher 演进残留): ' + JSON.stringify(groups));
|
|
2908
|
+
}
|
|
2909
|
+
if (!groups.some((g) => g.matcher === 'Write|Edit|Bash' && Array.isArray(g.hooks) && g.hooks.length > 0)) {
|
|
2910
|
+
throw new Error('重装后缺当前 matcher 组(Write|Edit|Bash)');
|
|
2911
|
+
}
|
|
2912
|
+
// ⑥ 历史残留空组清理:手动把当前组 hooks 清空(模拟历史版本残留的空组),
|
|
2913
|
+
// 再次重装后空组应被删除,settings 仅剩有效组
|
|
2914
|
+
const settings2 = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
2915
|
+
for (const g of settings2.hooks.PreToolUse) g.hooks = [];
|
|
2916
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings2, null, 2) + '\n', 'utf8');
|
|
2917
|
+
const reinstall2 = run(tcc, []);
|
|
2918
|
+
if (reinstall2.status !== 0) throw new Error('空组清理重装失败: ' + (reinstall2.stderr || JSON.stringify(reinstall2.output)));
|
|
2919
|
+
const after2 = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
2920
|
+
const groups2 = after2.hooks.PreToolUse;
|
|
2921
|
+
if (groups2.some((g) => Array.isArray(g.hooks) && g.hooks.length === 0)) {
|
|
2922
|
+
throw new Error('重装后仍存在空 hooks 组(历史残留未清理): ' + JSON.stringify(groups2));
|
|
2923
|
+
}
|
|
2924
|
+
if (!groups2.some((g) => g.matcher === 'Write|Edit|Bash' && Array.isArray(g.hooks) && g.hooks.length > 0)) {
|
|
2925
|
+
throw new Error('空组清理后缺当前 matcher 组(Write|Edit|Bash)');
|
|
2926
|
+
}
|
|
2927
|
+
const ccDry = run(tcc, ['--purge']);
|
|
2928
|
+
if (ccDry.status === 0) throw new Error('claude-code --purge 缺 --yes 应失败');
|
|
2929
|
+
if (!fs.existsSync(path.join(tcc, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('claude-code --purge 拒绝后生成物不应被删除');
|
|
2930
|
+
if (!fs.existsSync(path.join(tcc, 'USER_KEEP.md'))) throw new Error('claude-code --purge 拒绝后用户内容不应被删除');
|
|
2931
|
+
const ccOk = run(tcc, ['--purge', '--yes']);
|
|
2932
|
+
if (ccOk.status !== 0) throw new Error('claude-code --purge --yes 失败: ' + (ccOk.stderr || JSON.stringify(ccOk.output)));
|
|
2933
|
+
if (!fs.existsSync(path.join(tcc, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('claude-code --purge --yes 后技能应重新生成');
|
|
2934
|
+
if (!fs.existsSync(path.join(tcc, 'USER_KEEP.md'))) throw new Error('claude-code --purge --yes 后用户内容应保留');
|
|
2935
|
+
},
|
|
2936
|
+
},
|
|
2937
|
+
|
|
2938
|
+
// K6: 平台描述符驱动防回归——PLATFORMS 每平台条目含安装/清理函数(installHooks/installRules/
|
|
2939
|
+
// purge/overwriteDescription);main 统一调度(源码无平台分支标识——新增平台 = 描述符条目,
|
|
2940
|
+
// main 零改动);对源码提取的全部平台逐一真实安装冒烟(未来新增平台自动纳入测试)
|
|
2941
|
+
{
|
|
2942
|
+
name: 'K6 安装器:平台描述符驱动(全平台安装冒烟 + main 统一调度)',
|
|
2943
|
+
run: (dir) => {
|
|
2944
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2945
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return;
|
|
2946
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
2947
|
+
const src = fs.readFileSync(installer, 'utf8');
|
|
2948
|
+
// ① main 统一调度:平台分支标识已消除(新增平台 = 描述符条目,main 零改动)
|
|
2949
|
+
if (src.includes('isClaudeCode')) {
|
|
2950
|
+
throw new Error('prepare-env main 仍含平台分支标识 isClaudeCode(应迁入描述符)');
|
|
2951
|
+
}
|
|
2952
|
+
// ② 描述符完整性:平台 id 从描述符块提取(防块外误匹配);聚合块须含 4 个平台函数
|
|
2953
|
+
// (installHooks/installRules/purge/overwriteDescription——未来新增平台缺任一函数时,
|
|
2954
|
+
// 下方逐平台 purge 冒烟会因 platform.purge 未定义而真实失败)
|
|
2955
|
+
const platformBlock = src.match(/const PLATFORMS = \{[\s\S]*?\n\};/);
|
|
2956
|
+
if (!platformBlock) throw new Error('未找到 PLATFORMS 描述符表');
|
|
2957
|
+
const ids = [...platformBlock[0].matchAll(/^\s{2}'([a-z-]+)':\s*\{/gm)].map((m) => m[1]);
|
|
2958
|
+
if (!ids.includes('claude-code') || !ids.includes('codex')) {
|
|
2959
|
+
throw new Error('PLATFORMS 应含 claude-code/codex: ' + ids.join(', '));
|
|
2960
|
+
}
|
|
2961
|
+
for (const fn of ['installHooks', 'installRules', 'purge', 'overwriteDescription']) {
|
|
2962
|
+
if (!platformBlock[0].includes(fn)) throw new Error('描述符缺平台函数: ' + fn);
|
|
2963
|
+
}
|
|
2964
|
+
// ③ 全平台逐一安装 + purge 冒烟(从描述符块提取 id——未来新增平台自动纳入;
|
|
2965
|
+
// purge 冒烟验证清理函数真实可用,缺函数/清理失败即红);
|
|
2966
|
+
// DSH_HOME 指向临时目录——dsh 平台 installHooks 写 $DSH_HOME(plugins/loader +
|
|
2967
|
+
// cordis.patch.yml 托管块),断言全程不污染真实 ~/.dsh
|
|
2968
|
+
const dshHome = path.join(dir, 'k6-dsh-home');
|
|
2969
|
+
for (const id of ids) {
|
|
2970
|
+
const target = path.join(dir, 'k6-' + id);
|
|
2971
|
+
fs.mkdirSync(target, { recursive: true });
|
|
2972
|
+
const env = { ...process.env, DSH_HOME: dshHome };
|
|
2973
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', id], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env });
|
|
2974
|
+
if (res.status !== 0) throw new Error('平台 ' + id + ' 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
2975
|
+
const purgeRes = spawnSync(process.execPath, [installer, '--target', target, '--platform', id, '--purge', '--yes'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env });
|
|
2976
|
+
if (purgeRes.status !== 0) throw new Error('平台 ' + id + ' purge 失败: ' + (purgeRes.stderr || JSON.stringify(purgeRes.output)));
|
|
2977
|
+
}
|
|
2978
|
+
console.log(' 描述符驱动: ' + ids.length + ' 个平台逐一安装 + purge 冒烟通过✓');
|
|
2979
|
+
},
|
|
2980
|
+
},
|
|
2981
|
+
// ---------- K 扩展:dsh 平台断言(1.4.2 deepseek-harness-platform——安装器多平台) ----------
|
|
2982
|
+
// 旧 dsh-plugin npm 包已废弃(对象 dsh-plugin/ 目录另行废弃清理)——以下断言覆盖
|
|
2983
|
+
// prepare-env --platform dsh 的项目级安装/AGENTS.md 托管区/桥接 loader/purge 清理恢复;
|
|
2984
|
+
// 断言全程 DSH_HOME=临时目录环境变量,禁止污染真实 ~/.dsh(AC-1/AC-4)
|
|
2985
|
+
|
|
2986
|
+
// K7: dsh 描述符安装产物——--platform dsh 项目级安装(.dsh/skills rank 100 自动发现) +
|
|
2987
|
+
// pathReplacements 平台化(.dsh/skills/flow-comet/scripts/ 形态) + INSTALLED_VERSION +
|
|
2988
|
+
// 安装树清单与权威源一致;全程 DSH_HOME=临时目录(AC-1)
|
|
2989
|
+
{
|
|
2990
|
+
name: 'K7 安装器:dsh 平台安装(技能/pathReplacements/版本标识/树一致)',
|
|
2991
|
+
run: (dir) => {
|
|
2992
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
2993
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
2994
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
2995
|
+
const dshHome = path.join(dir, 'k7-dsh-home');
|
|
2996
|
+
const target = path.join(dir, 'k7-target');
|
|
2997
|
+
fs.mkdirSync(target, { recursive: true });
|
|
2998
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
2999
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3000
|
+
});
|
|
3001
|
+
if (res.status !== 0) throw new Error('prepare-env --platform dsh 失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
3002
|
+
// ① 技能安装到 <项目>/.dsh/skills/flow-comet/(dsh rank 100 项目级发现位置)
|
|
3003
|
+
const skillMd = path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md');
|
|
3004
|
+
if (!fs.existsSync(skillMd)) throw new Error('dsh 平台技能未安装到 .dsh/skills/flow-comet/');
|
|
3005
|
+
// ② guard 判定核心随技能分发(comet-hook-guard 平台无关 CLI,一行不动)
|
|
3006
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'scripts', 'comet-hook-guard.mjs'))) {
|
|
3007
|
+
throw new Error('comet-hook-guard.mjs 未随技能分发到 .dsh/skills/flow-comet/scripts/');
|
|
3008
|
+
}
|
|
3009
|
+
// ③ 版本标识写入(随技能包分发)
|
|
3010
|
+
const verFile = path.join(target, '.dsh', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
3011
|
+
if (!fs.existsSync(verFile)) throw new Error('缺少 INSTALLED_VERSION(版本标识文件)');
|
|
3012
|
+
if (!fs.readFileSync(verFile, 'utf8').trim()) throw new Error('版本标识为空');
|
|
3013
|
+
// ④ pathReplacements 生效:SKILL 命令路径为 .dsh 形态,无 .claude 残留
|
|
3014
|
+
const text = fs.readFileSync(skillMd, 'utf8');
|
|
3015
|
+
if (!text.includes('.dsh/skills/flow-comet/scripts/')) throw new Error('SKILL 命令路径未替换为 .dsh/skills/flow-comet/scripts/');
|
|
3016
|
+
if (text.includes('.claude/skills/flow-comet/scripts/')) throw new Error('SKILL 仍含 .claude/skills/flow-comet/scripts/ 命令路径');
|
|
3017
|
+
// ⑤ 安装树文件清单与权威源一致(pathReplacements 只改内容不改清单)
|
|
3018
|
+
const authoritative = path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet');
|
|
3019
|
+
const installedTree = collectTreeFiles(path.join(target, '.dsh', 'skills', 'flow-comet'));
|
|
3020
|
+
const srcTree = collectTreeFiles(authoritative);
|
|
3021
|
+
if (installedTree.length === 0) throw new Error('安装技能树为空');
|
|
3022
|
+
if (JSON.stringify(installedTree) !== JSON.stringify(srcTree)) {
|
|
3023
|
+
throw new Error('安装树清单与权威源不一致\n权威源: ' + srcTree.join(', ') + '\n安装: ' + installedTree.join(', '));
|
|
3024
|
+
}
|
|
3025
|
+
console.log(' .dsh/skills/flow-comet 安装树 = ' + installedTree.length + ' files 与权威源一致 ✓');
|
|
3026
|
+
},
|
|
3027
|
+
},
|
|
3028
|
+
|
|
3029
|
+
// K8: AGENTS.md 托管区注入——托管区存在 + 托管区外用户内容保留 + 幂等重装不重复
|
|
3030
|
+
// (codex/dsh 共用注入函数与托管区标记——AC-4)
|
|
3031
|
+
{
|
|
3032
|
+
name: 'K8 安装器:dsh AGENTS.md 托管区(用户内容保留 + 幂等重装)',
|
|
3033
|
+
run: (dir) => {
|
|
3034
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3035
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3036
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3037
|
+
const dshHome = path.join(dir, 'k8-dsh-home');
|
|
3038
|
+
const target = path.join(dir, 'k8-target');
|
|
3039
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3040
|
+
// 预置用户内容(托管区外,安装不得覆盖)
|
|
3041
|
+
fs.writeFileSync(path.join(target, 'AGENTS.md'), '# 用户自定义指令\n\n保留这段内容。\n', 'utf8');
|
|
3042
|
+
const run = () => spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3043
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3044
|
+
});
|
|
3045
|
+
// ① 首次安装:托管区注入(内联 orchestration 全文)+ 用户内容保留
|
|
3046
|
+
const first = run();
|
|
3047
|
+
if (first.status !== 0) throw new Error('dsh 安装失败: ' + (first.stderr || JSON.stringify(first.output)));
|
|
3048
|
+
let agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
3049
|
+
if (!agents.includes('<!-- Managed by flow-comet prepare-env -->')) throw new Error('AGENTS.md 无托管区标记');
|
|
3050
|
+
if (!agents.includes('flow-comet Orchestration')) throw new Error('AGENTS.md 未内联 orchestration 内容');
|
|
3051
|
+
if (!agents.includes('用户自定义指令') || !agents.includes('保留这段内容。')) throw new Error('托管区外用户内容被覆盖');
|
|
3052
|
+
// ② 幂等重装:托管区不重复注入(start+end 标记合计 2 处 = 1 个托管区)
|
|
3053
|
+
const second = run();
|
|
3054
|
+
if (second.status !== 0) throw new Error('dsh 重装失败: ' + (second.stderr || JSON.stringify(second.output)));
|
|
3055
|
+
agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
3056
|
+
if (!agents.includes('保留这段内容。')) throw new Error('重装后用户内容丢失');
|
|
3057
|
+
const markerCount = (agents.match(/Managed by flow-comet prepare-env/g) || []).length;
|
|
3058
|
+
if (markerCount !== 2) throw new Error('幂等重装后托管区应只有 1 组标记(实际 ' + markerCount + ' 个)——重复注入');
|
|
3059
|
+
console.log(' AGENTS.md 托管区注入 + 幂等重装不重复 ✓');
|
|
3060
|
+
},
|
|
3061
|
+
},
|
|
3062
|
+
|
|
3063
|
+
// K9: 桥接 loader 就位——权威源文件存在 + node --check 语法 + 安装时复制到
|
|
3064
|
+
// $DSH_HOME/plugins/ + cordis.patch.yml 托管块注入(insert 形态 + file:// 引用 loader 路径,
|
|
3065
|
+
// 读-合并-写保留既有块;insert 形态是 home patch 追加新插件的唯一正确形态——patch 形态
|
|
3066
|
+
// id-targeted 对不存在的 id 报 entry not found 跳过,loader 不加载,拦截整链静默失效;
|
|
3067
|
+
// $DSH_HOME 解析 = 显式 DSH_HOME > ~/.dsh——测试隔离到临时目录)
|
|
3068
|
+
{
|
|
3069
|
+
name: 'K9 桥接 loader:源文件/语法/复制与托管块注入(读-合并-写)',
|
|
3070
|
+
run: (dir) => {
|
|
3071
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3072
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3073
|
+
// ① 权威源 loader 存在 + 语法检查
|
|
3074
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
3075
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
3076
|
+
const check = spawnSync(process.execPath, ['--check', loader], { encoding: 'utf8', timeout: 60000 });
|
|
3077
|
+
if (check.status !== 0) throw new Error('dsh-bridge.mjs 语法检查失败: ' + (check.stderr || ''));
|
|
3078
|
+
// ② 安装后:loader 复制到 $DSH_HOME/plugins/dsh-flow-comet-bridge.mjs + 托管块注入
|
|
3079
|
+
const dshHome = path.join(dir, 'k9-dsh-home');
|
|
3080
|
+
const target = path.join(dir, 'k9-target');
|
|
3081
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3082
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3083
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3084
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3085
|
+
});
|
|
3086
|
+
if (res.status !== 0) throw new Error('dsh 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
3087
|
+
if (!fs.existsSync(path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs'))) {
|
|
3088
|
+
throw new Error('loader 未复制到 $DSH_HOME/plugins/');
|
|
3089
|
+
}
|
|
3090
|
+
const patchPath = path.join(dshHome, 'cordis.patch.yml');
|
|
3091
|
+
if (!fs.existsSync(patchPath)) throw new Error('缺少 $DSH_HOME/cordis.patch.yml');
|
|
3092
|
+
const patch = fs.readFileSync(patchPath, 'utf8');
|
|
3093
|
+
if (!patch.includes('# --- flow-comet managed ---') || !patch.includes('# --- end flow-comet managed ---')) {
|
|
3094
|
+
throw new Error('cordis.patch.yml 无托管块标记');
|
|
3095
|
+
}
|
|
3096
|
+
// insert 形态断言(L-015 教训:套件全绿 ≠ 真实 dsh 生效——patch 形态 id-targeted 语义对
|
|
3097
|
+
// 不存在的 id 输出 entry not found 并跳过,loader 从不加载;insert 形态才追加新插件行)
|
|
3098
|
+
if (!patch.includes('- insert:')) throw new Error('cordis.patch.yml 托管块应为 insert 形态(- insert: 顶层条目)');
|
|
3099
|
+
if (!patch.includes('dsh-flow-comet-bridge')) throw new Error('cordis.patch.yml 缺 loader 条目');
|
|
3100
|
+
const loaderUrl = pathToFileURL(path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs')).href;
|
|
3101
|
+
if (!patch.includes(loaderUrl)) throw new Error('cordis.patch.yml 托管块应 file:// 引用 loader 路径: ' + loaderUrl);
|
|
3102
|
+
// ③ 读-合并-写非破坏:预置 dsh-skin 既有块 → 重装后保留(托管块幂等唯一)
|
|
3103
|
+
fs.writeFileSync(patchPath, '# --- dsh-skin managed ---\n- id: dsh-skin\n name: file:///skin\n# --- end dsh-skin managed ---\n' + patch, 'utf8');
|
|
3104
|
+
const re = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3105
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3106
|
+
});
|
|
3107
|
+
if (re.status !== 0) throw new Error('dsh 重装失败: ' + (re.stderr || JSON.stringify(re.output)));
|
|
3108
|
+
const after = fs.readFileSync(patchPath, 'utf8');
|
|
3109
|
+
if (!after.includes('dsh-skin managed')) throw new Error('重装后 dsh-skin 既有块丢失(读-合并-写非破坏)');
|
|
3110
|
+
if ((after.match(/flow-comet managed/g) || []).length !== 2) {
|
|
3111
|
+
throw new Error('重装后 flow-comet 托管块应幂等唯一: ' + after);
|
|
3112
|
+
}
|
|
3113
|
+
console.log(' loader 复制 + cordis.patch.yml 托管块(读-合并-写保留既有块)✓');
|
|
3114
|
+
},
|
|
3115
|
+
},
|
|
3116
|
+
|
|
3117
|
+
// K10: dsh purge 清理恢复——缺 --yes 拒绝(不破坏)/ --purge --yes 删除后重新生成
|
|
3118
|
+
// (既有 main 语义:purge 后重新生成到最终态)/ 用户内容保留 / 空 .dsh 目录清理
|
|
3119
|
+
// (唯一 flow-comet 技能移除后 .dsh/skills 与 .dsh 变空 → 一并清理)
|
|
3120
|
+
{
|
|
3121
|
+
name: 'K10 安装器:dsh purge 清理恢复(缺 --yes 拒绝/删除后重建/用户内容保留/空目录清理)',
|
|
3122
|
+
run: (dir) => {
|
|
3123
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3124
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3125
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3126
|
+
const dshHome = path.join(dir, 'k10-dsh-home');
|
|
3127
|
+
const run = (target, extra) => spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh', ...extra], {
|
|
3128
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3129
|
+
});
|
|
3130
|
+
// ① 首次安装 + 用户内容(项目根,非生成物)
|
|
3131
|
+
const target = path.join(dir, 'k10-target');
|
|
3132
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3133
|
+
const first = run(target, []);
|
|
3134
|
+
if (first.status !== 0) throw new Error('dsh 首次安装失败: ' + (first.stderr || JSON.stringify(first.output)));
|
|
3135
|
+
fs.writeFileSync(path.join(target, 'USER_KEEP.md'), '# 用户内容(保留)\n', 'utf8');
|
|
3136
|
+
// ② 缺 --yes → 拒绝且不破坏(错误提示含 --yes;生成物与用户内容仍在)
|
|
3137
|
+
const dry = run(target, ['--purge']);
|
|
3138
|
+
if (dry.status === 0) throw new Error('--purge 缺 --yes 应失败');
|
|
3139
|
+
const dryOut = String(dry.stderr || '') + String(dry.stdout || '');
|
|
3140
|
+
if (!dryOut.includes('--yes')) throw new Error('--purge 缺 --yes 错误信息应提示 --yes: ' + dryOut);
|
|
3141
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge 拒绝后生成物不应被删除');
|
|
3142
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge 拒绝后用户内容不应被删除');
|
|
3143
|
+
// ③ --purge --yes → 删除后重新生成(最终态 = 重新生成的安装态);移除清单含空 .dsh 目录
|
|
3144
|
+
// 清理条目(.dsh/skills 移除后 .dsh 变空——行尾锚定防与 skillRoot 打印行混淆)
|
|
3145
|
+
const ok = run(target, ['--purge', '--yes']);
|
|
3146
|
+
if (ok.status !== 0) throw new Error('--purge --yes 失败: ' + (ok.stderr || JSON.stringify(ok.output)));
|
|
3147
|
+
const okOut = String(ok.stderr || '') + String(ok.stdout || '');
|
|
3148
|
+
const dshDirLine = '- ' + path.join(target, '.dsh');
|
|
3149
|
+
if (!okOut.includes(dshDirLine + '\r\n') && !okOut.includes(dshDirLine + '\n')) {
|
|
3150
|
+
throw new Error('purge 移除清单应含空 .dsh 目录清理条目: ' + okOut);
|
|
3151
|
+
}
|
|
3152
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge --yes 后技能应重新生成(删除后重新生成语义)');
|
|
3153
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge --yes 后用户内容应保留');
|
|
3154
|
+
const agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
3155
|
+
if (!agents.includes('<!-- Managed by flow-comet prepare-env -->')) throw new Error('--purge --yes 后 AGENTS.md 托管区应重建');
|
|
3156
|
+
if (!fs.existsSync(path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs'))) throw new Error('--purge --yes 后桥接 loader 应重建');
|
|
3157
|
+
// ④ 共享位置边界:.dsh/skills 用户技能(purge 只清 flow-comet*,其余保留)
|
|
3158
|
+
fs.mkdirSync(path.join(target, '.dsh', 'skills', 'user-own-skill'), { recursive: true });
|
|
3159
|
+
fs.writeFileSync(path.join(target, '.dsh', 'skills', 'user-own-skill', 'SKILL.md'), '# user skill\n', 'utf8');
|
|
3160
|
+
const ok2 = run(target, ['--purge', '--yes']);
|
|
3161
|
+
if (ok2.status !== 0) throw new Error('--purge --yes(第二次) 失败: ' + (ok2.stderr || JSON.stringify(ok2.output)));
|
|
3162
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'user-own-skill', 'SKILL.md'))) throw new Error('--purge --yes 后用户技能应保留(.dsh/skills 共享位置)');
|
|
3163
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge --yes(第二次) 后 flow-comet 技能应重新生成');
|
|
3164
|
+
console.log(' dsh purge 缺 --yes 拒绝 + 删除后重建 + 用户内容保留 + 空目录清理 ✓');
|
|
3165
|
+
},
|
|
3166
|
+
},
|
|
3167
|
+
|
|
3168
|
+
// K11: 桥接 loader 行为断言(dsh-bridge.mjs export 直测——参数映射别名矩阵/
|
|
3169
|
+
// 形状不符 fail-closed/项目根包含性含 8.3 短路径/退出码映射/guard 路径存在漂移防护;
|
|
3170
|
+
// apply 集成:tools/pre-execute 监听器分派——子代理放行/协调者走 guard/越界·形状 deny 不受身份)
|
|
3171
|
+
{
|
|
3172
|
+
name: 'K11 桥接 loader:纯函数与 apply 分派断言(映射/包含性/退出映射/身份分派)',
|
|
3173
|
+
run: async (dir) => {
|
|
3174
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3175
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3176
|
+
// 动态 import(套件运行器逐项 await)——dsh-bridge.mjs 仅在权威源仓库根 scripts/,
|
|
3177
|
+
// 安装副本缺失时整体跳过(与 K 组其余项同语义);静态 import 会让安装副本套件整体崩
|
|
3178
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
3179
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
3180
|
+
const bridge = await import(pathToFileURL(loader).href);
|
|
3181
|
+
// ① 工具名归一化别名矩阵:write/edit/bash 全部别名 → Write/Edit/Bash
|
|
3182
|
+
for (const alias of ['write', 'Write', 'WRITE', 'writefile', 'file-write']) {
|
|
3183
|
+
if (bridge.normalizeToolName(alias) !== 'Write') throw new Error('normalizeToolName 别名未归一化: ' + alias);
|
|
3184
|
+
}
|
|
3185
|
+
for (const alias of ['edit', 'Edit', 'editfile', 'file-edit']) {
|
|
3186
|
+
if (bridge.normalizeToolName(alias) !== 'Edit') throw new Error('normalizeToolName 别名未归一化: ' + alias);
|
|
3187
|
+
}
|
|
3188
|
+
for (const alias of ['bash', 'Bash', 'shell', 'powershell', 'pwsh', 'Pwsh', 'PWSH', 'run_command', 'run-command']) {
|
|
3189
|
+
if (bridge.normalizeToolName(alias) !== 'Bash') throw new Error('normalizeToolName 别名未归一化: ' + alias);
|
|
3190
|
+
}
|
|
3191
|
+
// 非 Write/Edit/Bash 或非字符串 → null(监听侧按放行语义跳过)
|
|
3192
|
+
for (const v of ['read', 'grep', '', null, undefined, 123, {}]) {
|
|
3193
|
+
if (bridge.normalizeToolName(v) !== null) throw new Error('非写工具应归一化为 null: ' + JSON.stringify(v));
|
|
3194
|
+
}
|
|
3195
|
+
// ② 参数映射别名提取:Write/Edit 的 file_path/filePath/path;Bash 的 command/cmd/script
|
|
3196
|
+
const m1 = bridge.mapToolInput('Write', { file_path: 'a.md' });
|
|
3197
|
+
if (!m1.ok || m1.target !== 'a.md' || m1.input.file_path !== 'a.md') throw new Error('Write file_path 映射失败: ' + JSON.stringify(m1));
|
|
3198
|
+
const m2 = bridge.mapToolInput('Edit', { filePath: 'b.md' });
|
|
3199
|
+
if (!m2.ok || m2.target !== 'b.md') throw new Error('Edit filePath 别名映射失败: ' + JSON.stringify(m2));
|
|
3200
|
+
const m3 = bridge.mapToolInput('Write', { path: 'c.md' });
|
|
3201
|
+
if (!m3.ok || m3.target !== 'c.md') throw new Error('Write path 别名映射失败: ' + JSON.stringify(m3));
|
|
3202
|
+
const mPrec = bridge.mapToolInput('Write', { file_path: 'a.md', filePath: 'b.md', path: 'c.md' });
|
|
3203
|
+
if (!mPrec.ok || mPrec.target !== 'a.md') throw new Error('file_path 应优先于 filePath/path: ' + JSON.stringify(mPrec));
|
|
3204
|
+
for (const alias of ['command', 'cmd', 'script']) {
|
|
3205
|
+
const mb = bridge.mapToolInput('Bash', { [alias]: 'npm test' });
|
|
3206
|
+
if (!mb.ok || mb.target !== 'npm test' || mb.input.command !== 'npm test') {
|
|
3207
|
+
throw new Error('Bash ' + alias + ' 映射失败: ' + JSON.stringify(mb));
|
|
3208
|
+
}
|
|
3209
|
+
}
|
|
3210
|
+
// ②b 形状不符/缺关键字段 → ok:false 可识别(fail-closed 前置,不静默放行)
|
|
3211
|
+
const bad1 = bridge.mapToolInput('Write', {});
|
|
3212
|
+
if (bad1.ok || !bad1.reason.includes('缺少 file_path')) throw new Error('Write 缺 file_path 应 fail-closed 且原因可识别: ' + JSON.stringify(bad1));
|
|
3213
|
+
const bad2 = bridge.mapToolInput('Bash', {});
|
|
3214
|
+
if (bad2.ok || !bad2.reason.includes('缺少 command')) throw new Error('Bash 缺 command 应 fail-closed 且原因可识别: ' + JSON.stringify(bad2));
|
|
3215
|
+
if (bridge.mapToolInput('Write', { file_path: ' ' }).ok) throw new Error('空白 file_path 应 fail-closed');
|
|
3216
|
+
if (bridge.mapToolInput('Bash', null).ok) throw new Error('null 参数应 fail-closed');
|
|
3217
|
+
if (bridge.mapToolInput('Bash', ['x']).ok) throw new Error('数组参数应 fail-closed');
|
|
3218
|
+
if (bridge.mapToolInput('Read', { file_path: 'a.md' }).ok) throw new Error('未支持工具名应 fail-closed');
|
|
3219
|
+
// ③ isPathInsideProjectRoot:项目内长路径 true / 项目外 false(含 .. 越界)
|
|
3220
|
+
const projRoot = path.join(dir, 'k11-proj');
|
|
3221
|
+
fs.mkdirSync(projRoot, { recursive: true });
|
|
3222
|
+
if (!bridge.isPathInsideProjectRoot(projRoot, path.join(projRoot, 'src', 'a.md'))) throw new Error('项目内长路径应判定为 true');
|
|
3223
|
+
if (!bridge.isPathInsideProjectRoot(projRoot, 'src/a.md')) throw new Error('项目内相对路径应判定为 true');
|
|
3224
|
+
const outside = path.join(dir, 'k11-out');
|
|
3225
|
+
fs.mkdirSync(outside, { recursive: true });
|
|
3226
|
+
if (bridge.isPathInsideProjectRoot(projRoot, path.join(outside, 'x.md'))) throw new Error('项目外绝对路径应判定为 false');
|
|
3227
|
+
if (bridge.isPathInsideProjectRoot(projRoot, path.join(projRoot, '..', 'k11-out', 'x.md'))) throw new Error('.. 越界路径应判定为 false');
|
|
3228
|
+
// ③b 8.3 短路径别名(Windows 8dot3name):os.tmpdir() 本机为 C:\Users\LONGYI~1
|
|
3229
|
+
// 短形态,realpathSync.native 归一化为长形态——词法不同但实路径相同;
|
|
3230
|
+
// 短形态不可解析的平台(禁用 8dot3name/POSIX)显式跳过
|
|
3231
|
+
const realRoot = bridge.realpathExistingPath(projRoot);
|
|
3232
|
+
if (realRoot !== projRoot) {
|
|
3233
|
+
const longFormTarget = path.join(realRoot, 'src', 'a.md');
|
|
3234
|
+
if (!bridge.isPathInsideProjectRoot(projRoot, longFormTarget)) {
|
|
3235
|
+
throw new Error('8.3 短路径别名(长形态目标)应归一化后判定为 true');
|
|
3236
|
+
}
|
|
3237
|
+
const normalized = bridge.realpathExistingPath(path.join(projRoot, 'src'));
|
|
3238
|
+
if (normalized !== path.join(realRoot, 'src')) {
|
|
3239
|
+
throw new Error('realpathExistingPath 应返回规范化长路径: ' + normalized + ' ≠ ' + path.join(realRoot, 'src'));
|
|
3240
|
+
}
|
|
3241
|
+
console.log(' 8.3 短路径别名经 realpath 归一化为长形态判定 ✓');
|
|
3242
|
+
} else {
|
|
3243
|
+
console.log(' (8.3 短路径别名不可解析——平台跳过)');
|
|
3244
|
+
}
|
|
3245
|
+
// ④ mapGuardExit:exit 0 → allow / exit 2 → deny(恢复指引 + detail 透传)/ 其它 → fail-closed
|
|
3246
|
+
if (bridge.mapGuardExit(0, '', '').kind !== 'allow') throw new Error('exit 0 应 allow');
|
|
3247
|
+
const d2 = bridge.mapGuardExit(2, 'BLOCKED detail', '');
|
|
3248
|
+
if (d2.kind !== 'deny') throw new Error('exit 2 应 deny');
|
|
3249
|
+
if (!d2.reason.includes('白名单拦截') || !d2.reason.includes('BLOCKED detail')) {
|
|
3250
|
+
throw new Error('exit 2 deny 应透传恢复指引与 detail: ' + JSON.stringify(d2));
|
|
3251
|
+
}
|
|
3252
|
+
for (const code of [1, 3, null, '0']) {
|
|
3253
|
+
const err = bridge.mapGuardExit(code, 'boom', '');
|
|
3254
|
+
if (err.kind !== 'error') throw new Error('其它退出码应 fail-closed error: code=' + String(code));
|
|
3255
|
+
if (!err.message.includes('code=' + String(code))) throw new Error('error 消息应含退出码: ' + JSON.stringify(err));
|
|
3256
|
+
}
|
|
3257
|
+
// ⑤ guard 路径存在断言(M4 漂移防护):HOOK_GUARD_REL('.dsh/skills/flow-comet/scripts/
|
|
3258
|
+
// comet-hook-guard.mjs')与安装器独立维护同一知识——任一侧漂移(常量改/安装布局改)
|
|
3259
|
+
// → 此断言失败,拦截层即失效(guard 缺失 → 桥接 WARN + 放行 fail-open)
|
|
3260
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3261
|
+
const dshHome = path.join(dir, 'k11-dsh-home');
|
|
3262
|
+
const target = path.join(dir, 'k11-target');
|
|
3263
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3264
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3265
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3266
|
+
});
|
|
3267
|
+
if (res.status !== 0) throw new Error('dsh 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
3268
|
+
const guardRel = '.dsh/skills/flow-comet/scripts/comet-hook-guard.mjs';
|
|
3269
|
+
if (!fs.existsSync(path.join(target, guardRel))) {
|
|
3270
|
+
throw new Error('HOOK_GUARD_REL 相对项目根解析的 guard 不存在(安装布局漂移): ' + path.join(target, guardRel));
|
|
3271
|
+
}
|
|
3272
|
+
console.log(' HOOK_GUARD_REL guard 安装后存在(与安装产物绑定)✓');
|
|
3273
|
+
// ⑥ delegationDepth 代理身份分派(B1——dsh 子代理=执行者):dsh 子代理 spawn
|
|
3274
|
+
// provider 的 childSessionMeta 把 delegationDepth=parentDepth+1 写入子代理 session
|
|
3275
|
+
// header —— 协调者=0/缺失,子代理>0。桥接层据此分派:子代理写源码=执行者职责
|
|
3276
|
+
// (对应 CC worktree 物理隔离),协调者走 guard 白名单拦截。旧桥接对所有工具
|
|
3277
|
+
// 一视同仁送 guard → subagent 模式子代理写源码被 .specs/ 白名单误拦 = B1 根因。
|
|
3278
|
+
// 本断言锚定 agentDepth 纯函数(守 exec 结构读取语义——header.delegationDepth)。
|
|
3279
|
+
if (typeof bridge.agentDepth !== 'function') {
|
|
3280
|
+
throw new Error('bridge.agentDepth 未导出(B1 修复应新增代理身份读取纯函数)');
|
|
3281
|
+
}
|
|
3282
|
+
// ① 子代理(depth=1)→ 识别为执行者(返回 1)
|
|
3283
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: 1 } } } }) !== 1) {
|
|
3284
|
+
throw new Error('子代理 delegationDepth=1 应识别为执行者: ' + String(bridge.agentDepth({ agent: { session: { header: { delegationDepth: 1 } } } })));
|
|
3285
|
+
}
|
|
3286
|
+
// ② 深层子代理(depth=2)→ 识别为执行者
|
|
3287
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: 2 } } } }) !== 2) {
|
|
3288
|
+
throw new Error('子代理 delegationDepth=2 应识别为执行者');
|
|
3289
|
+
}
|
|
3290
|
+
// ③ 协调者(depth=0)→ 协调者(非执行者)
|
|
3291
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: 0 } } } }) !== 0) {
|
|
3292
|
+
throw new Error('协调者 delegationDepth=0 应返回 0');
|
|
3293
|
+
}
|
|
3294
|
+
// ④ 缺省(undefined/缺字段)→ 协调者(旧语义兼容——协调者 depth 缺失按 0)
|
|
3295
|
+
if (bridge.agentDepth({ agent: { session: { header: {} } } }) !== 0) {
|
|
3296
|
+
throw new Error('缺 delegationDepth 应按协调者处理(0)');
|
|
3297
|
+
}
|
|
3298
|
+
if (bridge.agentDepth({}) !== 0) {
|
|
3299
|
+
throw new Error('空 exec 应按协调者处理(0)');
|
|
3300
|
+
}
|
|
3301
|
+
if (bridge.agentDepth(undefined) !== 0) {
|
|
3302
|
+
throw new Error('undefined exec 应按协调者处理(0)');
|
|
3303
|
+
}
|
|
3304
|
+
// ⑤ 非数字/负数/非法值 → 0(fail-closed 协调者语义,防 NaN/字符串穿透)
|
|
3305
|
+
for (const bad of [null, '1', -1, NaN, Infinity, {}, 'abc']) {
|
|
3306
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: bad } } } }) !== 0) {
|
|
3307
|
+
throw new Error('非法 delegationDepth 应按协调者处理(0): ' + JSON.stringify(bad));
|
|
3308
|
+
}
|
|
3309
|
+
}
|
|
3310
|
+
console.log(' delegationDepth 代理身份分派(子代理=执行者/协调者原链)✓');
|
|
3311
|
+
// ⑦ apply 集成喂测(5.5 分派分支自动化回归——此前仅级 3 真实会话人工覆盖):
|
|
3312
|
+
// mock ctx 捕获 tools/pre-execute 监听器,构造 exec 断言插件级行为、
|
|
3313
|
+
// 子代理(agentDepth>0)写源码放行 / 协调者走 guard 白名单 / 越界·形状 deny 不受身份、
|
|
3314
|
+
// 非 flow-comet 项目窄监听放行。复用 ⑤ 安装产物(target 为真实 dsh flow-comet 项目):
|
|
3315
|
+
// 写活跃 execute 态 state → 协调者写源码被真实 guard 子进程拦(BLOCK 链真实执行)。
|
|
3316
|
+
const applyEvents = {};
|
|
3317
|
+
bridge.apply({ on: (event, fn) => { applyEvents[event] = fn; } });
|
|
3318
|
+
const preExec = applyEvents['tools/pre-execute'];
|
|
3319
|
+
if (typeof preExec !== 'function') throw new Error('bridge.apply 应注册 tools/pre-execute 监听器');
|
|
3320
|
+
gitInit(target);
|
|
3321
|
+
fs.mkdirSync(path.join(target, 'src'), { recursive: true });
|
|
3322
|
+
writeState(target, {
|
|
3323
|
+
activeChange: 'apply-lock',
|
|
3324
|
+
currentNode: 'execute',
|
|
3325
|
+
completedNodes: [],
|
|
3326
|
+
evidence: {},
|
|
3327
|
+
verifyFailures: 0,
|
|
3328
|
+
status: 'running',
|
|
3329
|
+
executionMode: 'subagent',
|
|
3330
|
+
directOverride: false,
|
|
3331
|
+
});
|
|
3332
|
+
// 项目根常规用长形态(与桥接对 file_path 的规范化一致,对应真实会话规范 cwd);
|
|
3333
|
+
// 短形态 cwd 的场景由桥接的项目根规范化修复,⑦f 断言锁死(fail-open 已封闭)。
|
|
3334
|
+
const longTarget = bridge.realpathExistingPath(target); // realpathSync.native——展开 8.3 短名
|
|
3335
|
+
// ⑦a 子代理(depth=1)写项目内源码 → next() 被调(跳过 guard 白名单放行)
|
|
3336
|
+
{
|
|
3337
|
+
let usedNext = false;
|
|
3338
|
+
const res = await preExec(
|
|
3339
|
+
{ name: 'Write', arguments: { file_path: path.join(longTarget, 'src', 'a.mjs') }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
3340
|
+
() => { usedNext = true; },
|
|
3341
|
+
);
|
|
3342
|
+
if (!usedNext) throw new Error('子代理写源码应放行(next 被调)——5.5 分派分支缺失');
|
|
3343
|
+
if (res) throw new Error('子代理写源码不应返回 deny: ' + JSON.stringify(res));
|
|
3344
|
+
}
|
|
3345
|
+
// ⑦b 协调者(delegationDepth 缺失=0)同目标写 → 走真实 guard 白名单 → BLOCK deny
|
|
3346
|
+
{
|
|
3347
|
+
let usedNext = false;
|
|
3348
|
+
const res = await preExec(
|
|
3349
|
+
{ name: 'Write', arguments: { file_path: path.join(longTarget, 'src', 'a.mjs') }, agent: { cwd: longTarget, session: { header: {} } } },
|
|
3350
|
+
() => { usedNext = true; },
|
|
3351
|
+
);
|
|
3352
|
+
if (usedNext) throw new Error('协调者写源码不应 next——协调者禁令物理拦截保留');
|
|
3353
|
+
if (!res || res.kind !== 'deny') throw new Error('协调者写源码应走 guard 白名单拦截(deny): ' + JSON.stringify(res));
|
|
3354
|
+
if (!res.reason.includes('白名单拦截')) throw new Error('协调者 deny 应含白名单拦截语义: ' + JSON.stringify(res));
|
|
3355
|
+
}
|
|
3356
|
+
// ⑦c 非 flow-comet 项目(无 .dsh/skills/flow-comet) → 窄监听放行(不分身份)
|
|
3357
|
+
{
|
|
3358
|
+
const plain = path.join(dir, 'k11-plain');
|
|
3359
|
+
fs.mkdirSync(plain, { recursive: true });
|
|
3360
|
+
let usedNext = false;
|
|
3361
|
+
const res = await preExec(
|
|
3362
|
+
{ name: 'Write', arguments: { file_path: path.join(plain, 'x.md') }, agent: { cwd: plain, session: { header: { delegationDepth: 1 } } } },
|
|
3363
|
+
() => { usedNext = true; },
|
|
3364
|
+
);
|
|
3365
|
+
if (!usedNext) throw new Error('非 flow-comet 项目应窄监听放行(next)');
|
|
3366
|
+
}
|
|
3367
|
+
// ⑦d 越界写不受身份影响:子代理(depth=1)写项目根外 → 包含性 fail-closed deny
|
|
3368
|
+
{
|
|
3369
|
+
const outside = path.join(dir, 'k11-out2');
|
|
3370
|
+
fs.mkdirSync(outside, { recursive: true });
|
|
3371
|
+
let usedNext = false;
|
|
3372
|
+
const res = await preExec(
|
|
3373
|
+
{ name: 'Write', arguments: { file_path: path.join(outside, 'evil.md') }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
3374
|
+
() => { usedNext = true; },
|
|
3375
|
+
);
|
|
3376
|
+
if (usedNext) throw new Error('子代理越界写不应 next——包含性不因身份放宽');
|
|
3377
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('不在项目根')) {
|
|
3378
|
+
throw new Error('子代理越界写应 fail-closed deny(含越界语义): ' + JSON.stringify(res));
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
// ⑦e 形状不符不受身份影响:子代理(depth=1)参数形状不符 → fail-closed deny
|
|
3382
|
+
{
|
|
3383
|
+
let usedNext = false;
|
|
3384
|
+
const res = await preExec(
|
|
3385
|
+
{ name: 'Write', arguments: {}, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
3386
|
+
() => { usedNext = true; },
|
|
3387
|
+
);
|
|
3388
|
+
if (usedNext) throw new Error('子代理形状不符不应 next——形状校验不因身份放宽');
|
|
3389
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('参数形状不符')) {
|
|
3390
|
+
throw new Error('子代理形状不符应 fail-closed deny(含形状语义): ' + JSON.stringify(res));
|
|
3391
|
+
}
|
|
3392
|
+
}
|
|
3393
|
+
// ⑦f 短形态项目根不得 fail-open:会话 cwd 为 8.3 短路径时,桥接须把项目根
|
|
3394
|
+
// 归一化为长形态再送 guard——否则 guard 词法 path.relative 得 target=null
|
|
3395
|
+
// 跳过白名单(协调者写源码被放行)。本断言在短形态 TMP 机器上即回归锁死。
|
|
3396
|
+
{
|
|
3397
|
+
let usedNext = false;
|
|
3398
|
+
const res = await preExec(
|
|
3399
|
+
{ name: 'Write', arguments: { file_path: path.join(target, 'src', 'b.mjs') }, agent: { cwd: target, session: { header: {} } } },
|
|
3400
|
+
() => { usedNext = true; },
|
|
3401
|
+
);
|
|
3402
|
+
if (usedNext) throw new Error('短形态 cwd 协调者写源码不应 next——8.3 短路径不得绕过白名单(fail-open 已封闭)');
|
|
3403
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('白名单拦截')) {
|
|
3404
|
+
throw new Error('短形态 cwd 协调者写源码应走 guard 白名单拦截(deny): ' + JSON.stringify(res));
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
console.log(' bridge.apply 分派集成(子代理放行/协调者拦/越界·形状 deny 不受身份)✓');
|
|
3408
|
+
},
|
|
3409
|
+
},
|
|
3410
|
+
|
|
3411
|
+
// K12: 桥接 loader 流程态门断言(B 方案 TDD RED 载体——空闲/异常态行为面,与 K11 运行态分开):
|
|
3412
|
+
// AC-1 无 state 文件 / AC-2 activeChange 缺失 / AC-3 status completed → 项目外 Write 应
|
|
3413
|
+
// next() 放行(空闲放行语义由流程态门实现);AC-7 非法 JSON / AC-8 未知 status →
|
|
3414
|
+
// fail-closed deny(reason 含拒绝语义;解析失败/未知状态不得当空闲放行)。
|
|
3415
|
+
// 复用 K11 安装产物形态:target 为真实 dsh flow-comet 项目(.dsh/skills/flow-comet 就位,
|
|
3416
|
+
// 窄监听不跳过——保证当前实现走到包含性 deny 而非被窄监听放行);mock exec 取协调者身份
|
|
3417
|
+
// (delegationDepth 缺失=0,与 K11 ⑦b 同语义)。import 用 ?query 缓存破除——K11 已在本进程
|
|
3418
|
+
// apply 过,模块级幂等标记会让二次 apply 跳过(applied=true),必须全新模块实例才能捕获监听器。
|
|
3419
|
+
{
|
|
3420
|
+
name: 'K12 桥接 loader:流程态门断言(无state/无activeChange/completed放行·非法JSON/未知status deny)',
|
|
3421
|
+
run: async (dir) => {
|
|
3422
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3423
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3424
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
3425
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
3426
|
+
const bridge = await import(pathToFileURL(loader).href + '?k12=' + Date.now()); // 全新模块实例(applied=false)
|
|
3427
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3428
|
+
const dshHome = path.join(dir, 'k12-dsh-home');
|
|
3429
|
+
const target = path.join(dir, 'k12-target');
|
|
3430
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3431
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3432
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3433
|
+
});
|
|
3434
|
+
if (res.status !== 0) throw new Error('dsh 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
3435
|
+
// 窄监听不跳过前置:目标项目必须真的装有 .dsh/skills/flow-comet——否则当前实现会因窄监听
|
|
3436
|
+
// 直接 next(),空闲态断言假绿,RED 失真
|
|
3437
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) {
|
|
3438
|
+
throw new Error('dsh flow-comet 项目安装缺失(.dsh/skills/flow-comet/SKILL.md 不存在)');
|
|
3439
|
+
}
|
|
3440
|
+
gitInit(target);
|
|
3441
|
+
const longTarget = bridge.realpathExistingPath(target); // 8.3 短形态归一为长形态(与 K11 一致)
|
|
3442
|
+
const outside = path.join(dir, 'k12-out');
|
|
3443
|
+
fs.mkdirSync(outside, { recursive: true });
|
|
3444
|
+
// 项目外 Write(协调者身份——delegationDepth 缺失=0)
|
|
3445
|
+
const mkOutsideWrite = () => ({
|
|
3446
|
+
name: 'Write',
|
|
3447
|
+
arguments: { file_path: path.join(outside, 'idle-leak.md') },
|
|
3448
|
+
agent: { cwd: longTarget, session: { header: {} } },
|
|
3449
|
+
});
|
|
3450
|
+
const ctxEvents = {};
|
|
3451
|
+
bridge.apply({ on: (event, fn) => { ctxEvents[event] = fn; } });
|
|
3452
|
+
const preExec = ctxEvents['tools/pre-execute'];
|
|
3453
|
+
if (typeof preExec !== 'function') throw new Error('bridge.apply 应注册 tools/pre-execute 监听器');
|
|
3454
|
+
const statePath = path.join(target, '.flow-comet', 'flow-comet-state.json');
|
|
3455
|
+
// AC-1 无 state:无 .flow-comet/flow-comet-state.json + 项目外 Write → next() 放行(空闲放行已生效)
|
|
3456
|
+
{
|
|
3457
|
+
if (fs.existsSync(statePath)) throw new Error('AC-1 前置:目标项目不应有 state 文件');
|
|
3458
|
+
let usedNext = false;
|
|
3459
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3460
|
+
if (!usedNext) throw new Error('AC-1 无 state 应放行(next 被调): ' + JSON.stringify(r));
|
|
3461
|
+
if (r) throw new Error('AC-1 无 state 不应返回 deny: ' + JSON.stringify(r));
|
|
3462
|
+
}
|
|
3463
|
+
// AC-2 无 activeChange:writeState { activeChange:null, status:'running' } + 项目外 Write → next()(空闲放行已生效)
|
|
3464
|
+
{
|
|
3465
|
+
writeState(target, { activeChange: null, status: 'running' });
|
|
3466
|
+
let usedNext = false;
|
|
3467
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3468
|
+
if (!usedNext) throw new Error('AC-2 activeChange 为空应放行(next 被调): ' + JSON.stringify(r));
|
|
3469
|
+
if (r) throw new Error('AC-2 无 activeChange 不应返回 deny: ' + JSON.stringify(r));
|
|
3470
|
+
}
|
|
3471
|
+
// AC-3 completed:writeState { activeChange:'x', status:'completed' } + 项目外 Write → next()(空闲放行已生效)
|
|
3472
|
+
{
|
|
3473
|
+
writeState(target, { activeChange: 'x', status: 'completed' });
|
|
3474
|
+
let usedNext = false;
|
|
3475
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3476
|
+
if (!usedNext) throw new Error('AC-3 status completed 应放行(next 被调): ' + JSON.stringify(r));
|
|
3477
|
+
if (r) throw new Error('AC-3 completed 不应返回 deny: ' + JSON.stringify(r));
|
|
3478
|
+
}
|
|
3479
|
+
// AC-7 解析失败:state 写成非法 JSON + 项目外 Write → deny(reason 含 fail-closed 拒绝语义;
|
|
3480
|
+
// 当前无条件 deny 已通过、T02 流程态门落地后必须保持 deny,不得当空闲放行)
|
|
3481
|
+
{
|
|
3482
|
+
writeFile(target, '.flow-comet/flow-comet-state.json', '{ not-valid-json ');
|
|
3483
|
+
let usedNext = false;
|
|
3484
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3485
|
+
if (usedNext) throw new Error('AC-7 非法 JSON 不应 next——解析失败必须 fail-closed deny');
|
|
3486
|
+
if (!r || r.kind !== 'deny') throw new Error('AC-7 非法 JSON 应 deny: ' + JSON.stringify(r));
|
|
3487
|
+
if (!r.reason.includes('拒绝')) throw new Error('AC-7 deny 应含 fail-closed(拒绝)语义: ' + JSON.stringify(r.reason));
|
|
3488
|
+
}
|
|
3489
|
+
// AC-8 未知 status:writeState { activeChange:'x', status:'weird' } + 项目外 Write → deny
|
|
3490
|
+
// (当前通过、T02 流程态门落地后必须保持 deny,不得当空闲放行)
|
|
3491
|
+
{
|
|
3492
|
+
writeState(target, { activeChange: 'x', status: 'weird' });
|
|
3493
|
+
let usedNext = false;
|
|
3494
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3495
|
+
if (usedNext) throw new Error('AC-8 未知 status 不应 next——未知 status 必须 fail-closed deny');
|
|
3496
|
+
if (!r || r.kind !== 'deny') throw new Error('AC-8 未知 status 应 deny: ' + JSON.stringify(r));
|
|
3497
|
+
if (!r.reason.includes('拒绝')) throw new Error('AC-8 deny 应含 fail-closed(拒绝)语义: ' + JSON.stringify(r.reason));
|
|
3498
|
+
}
|
|
3499
|
+
// AC-7b 非对象 JSON(null/标量/数组/字符串):合法 JSON 但非对象 = 损坏 state → deny(fail-closed)
|
|
3500
|
+
for (const bad of [null, 123, ['x'], 'str']) {
|
|
3501
|
+
writeFile(target, '.flow-comet/flow-comet-state.json',JSON.stringify(bad));
|
|
3502
|
+
let usedNext = false;
|
|
3503
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3504
|
+
if (usedNext) throw new Error('AC-7b 非对象 state(' + JSON.stringify(bad) + ') 不应 next——必须 fail-closed deny');
|
|
3505
|
+
if (!r || r.kind !== 'deny') throw new Error('AC-7b 非对象 state 应 deny: ' + JSON.stringify(r));
|
|
3506
|
+
if (!r.reason.includes('拒绝')) throw new Error('AC-7b deny 应含 fail-closed(拒绝)语义: ' + JSON.stringify(r.reason));
|
|
3507
|
+
}
|
|
3508
|
+
// AC-3b BOM 前缀 + completed:BOM 容错断言——应被解析并视为空闲放行
|
|
3509
|
+
{
|
|
3510
|
+
writeFile(target, '.flow-comet/flow-comet-state.json', '\uFEFF' + JSON.stringify({ activeChange: 'x', status: 'completed' }));
|
|
3511
|
+
let usedNext = false;
|
|
3512
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
3513
|
+
if (!usedNext) throw new Error('AC-3b BOM+completed 应放行(next 被调)——BOM 容错: ' + JSON.stringify(r));
|
|
3514
|
+
if (r) throw new Error('AC-3b BOM+completed 不应返回 deny: ' + JSON.stringify(r));
|
|
3515
|
+
}
|
|
3516
|
+
console.log(' 流程态门断言:AC-1/2/3 空闲放行·AC-3b BOM 容错·AC-7/8/7b 异常态 fail-closed deny✓');
|
|
3517
|
+
},
|
|
3518
|
+
},
|
|
3519
|
+
|
|
3520
|
+
// hook 注入形态无关断言 + 旧条目幂等升级(AC-5/AC-7;安装产物根引用特征用例 + 升级替换用例)——
|
|
3521
|
+
// 安装产物托管条目满足「脚本 basename 在位 + 项目根引用特征(CLAUDE_PROJECT_DIR 或绝对路径)
|
|
3522
|
+
// 二选一」;新旧托管形态识别兼容(basename 同源 isManagedHookCommand 语义);把托管命令改回
|
|
3523
|
+
// 旧相对路径形态后重跑安装器 → 被识别替换(恰余 1 条,不重复不残留)且新条目形态达标。
|
|
3524
|
+
// 断言不锁定 %VAR% 引号/变量词形——注入命令实测微调不再破坏套件。
|
|
3525
|
+
{
|
|
3526
|
+
name: 'K13 安装器:hook 注入形态无关断言(basename+项目根引用)+旧条目幂等升级',
|
|
3527
|
+
run: (dir) => {
|
|
3528
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3529
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3530
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3531
|
+
// ① 形态分类器:新旧托管形态均被 basename 识别;项目根引用特征仅根引用形态命中
|
|
3532
|
+
for (const legacy of [
|
|
3533
|
+
'node .claude/skills/flow-comet/scripts/comet-hook-guard.mjs',
|
|
3534
|
+
'node .agents/skills/flow-comet/scripts/comet-hook-guard.mjs before_tool --platform codex',
|
|
3535
|
+
]) {
|
|
3536
|
+
if (!isManagedHookCommand(legacy)) throw new Error('旧托管形态应被 basename 识别(幂等升级识别面): ' + legacy);
|
|
3537
|
+
if (hasProjectRootRef(legacy)) throw new Error('旧相对路径形态不应命中项目根引用特征: ' + legacy);
|
|
3538
|
+
}
|
|
3539
|
+
for (const modern of [
|
|
3540
|
+
'node "%CLAUDE_PROJECT_DIR%/.claude/skills/flow-comet/scripts/comet-hook-guard.mjs"',
|
|
3541
|
+
'node "$env:CLAUDE_PROJECT_DIR/.claude/skills/flow-comet/scripts/comet-hook-guard.mjs"',
|
|
3542
|
+
'node "${CLAUDE_PROJECT_DIR}/.claude/skills/flow-comet/scripts/comet-hook-guard.mjs"',
|
|
3543
|
+
]) {
|
|
3544
|
+
assertManagedHookEntry(modern, '变量引用形态');
|
|
3545
|
+
}
|
|
3546
|
+
assertManagedHookEntry(path.join(dir, 'k13-abs', '.claude', 'skills', 'flow-comet', 'scripts', 'comet-hook-guard.mjs'), '绝对路径形态');
|
|
3547
|
+
// ② claude-code 安装产物:托管条目在场且指向本项目脚本(项目根引用特征在位)
|
|
3548
|
+
const ccTarget = path.join(dir, 'k13-cc');
|
|
3549
|
+
fs.mkdirSync(ccTarget, { recursive: true });
|
|
3550
|
+
const rcc = spawnSync(process.execPath, [installer, '--target', ccTarget, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
3551
|
+
if (rcc.status !== 0) throw new Error('claude-code 安装失败: ' + (rcc.stderr || JSON.stringify(rcc.output)));
|
|
3552
|
+
const ccSettingsPath = path.join(ccTarget, '.claude', 'settings.local.json');
|
|
3553
|
+
const ccSettings = JSON.parse(fs.readFileSync(ccSettingsPath, 'utf8'));
|
|
3554
|
+
const ccManaged = collectManagedHookCommands(ccSettings);
|
|
3555
|
+
if (ccManaged.length === 0) throw new Error('.claude/settings.local.json 未注入托管 hook 条目');
|
|
3556
|
+
for (const c of ccManaged) assertManagedHookEntry(c, 'claude-code 托管命令');
|
|
3557
|
+
// ③ 可执行性(补「形态可识别但不可执行」盲区,2026-09-10):识别层与执行层是两个维度——
|
|
3558
|
+
// 前者只要求 basename 可辨,后者要求命令在 CC 的真实执行语义下可达。CC 全平台以
|
|
3559
|
+
// /bin/bash -c 执行 hook 命令(含 Windows),并注入 CLAUDE_PROJECT_DIR 环境变量;
|
|
3560
|
+
// cmd 变量语法 %VAR% 在 bash 下既不展开、路径反斜杠还会被转义吞掉 → node 静默
|
|
3561
|
+
// 找不到脚本(fail-open,仅报 non-blocking hook error)。
|
|
3562
|
+
// (a) 形态约束(全平台可判):托管命令不得含 %VAR% cmd 语法;
|
|
3563
|
+
// (b) 端到端(需 bash):以 bash -c + 注入 CLAUDE_PROJECT_DIR 跑完整命令,验证可达;
|
|
3564
|
+
// 无 bash 环境 → 显式失败(未验证不等于通过——宿主本身以 bash 语义执行 hook,
|
|
3565
|
+
// 不具备 bash 的环境无法完成本验证)。
|
|
3566
|
+
for (const c of ccManaged) {
|
|
3567
|
+
if (/%[A-Za-z_][A-Za-z0-9_]*%/.test(c)) {
|
|
3568
|
+
throw new Error('claude-code 托管命令含 cmd 变量语法(%VAR%)——bash 执行语义下不可用: ' + c);
|
|
3569
|
+
}
|
|
3570
|
+
}
|
|
3571
|
+
const hasBash = (() => {
|
|
3572
|
+
try {
|
|
3573
|
+
return spawnSync('bash', ['--version'], { encoding: 'utf8', timeout: 30000 }).status === 0;
|
|
3574
|
+
} catch {
|
|
3575
|
+
return false;
|
|
3576
|
+
}
|
|
3577
|
+
})();
|
|
3578
|
+
if (hasBash) {
|
|
3579
|
+
for (const c of ccManaged) {
|
|
3580
|
+
const execRes = spawnSync('bash', ['-c', c], {
|
|
3581
|
+
cwd: ccTarget,
|
|
3582
|
+
encoding: 'utf8',
|
|
3583
|
+
timeout: 60000,
|
|
3584
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: ccTarget },
|
|
3585
|
+
input: JSON.stringify({ tool_name: 'Write', tool_input: { file_path: path.join(ccTarget, 'k13-probe.txt') } }),
|
|
3586
|
+
});
|
|
3587
|
+
if (execRes.status !== 0 || /Cannot find module/.test(String(execRes.stderr || ''))) {
|
|
3588
|
+
throw new Error(
|
|
3589
|
+
'claude-code 托管命令在 CC 执行语义(bash -c + CLAUDE_PROJECT_DIR)下不可执行: ' +
|
|
3590
|
+
c + ' —— ' + String(execRes.stderr || '').split('\n').slice(0, 3).join(' | ')
|
|
3591
|
+
);
|
|
3592
|
+
}
|
|
3593
|
+
}
|
|
3594
|
+
} else {
|
|
3595
|
+
throw new Error(
|
|
3596
|
+
'K13 端到端执行探针需要 bash(宿主以 bash 语义执行 hook 命令),当前环境 PATH 中不可用——' +
|
|
3597
|
+
'无法验证生成命令可执行;请在具备 bash 的环境运行套件(Windows 请确认 Git Bash 在 PATH 中)'
|
|
3598
|
+
);
|
|
3599
|
+
}
|
|
3600
|
+
// ③b 含空格项目根路径:同一端到端验证——未加引号的命令在变量展开后的空格处被词法分词
|
|
3601
|
+
// 切断(静默失效,与反斜杠转义同类);含引号形态须可执行,且仍被幂等识别(识别层去引号)。
|
|
3602
|
+
const ccSpaceTarget = path.join(dir, 'k13 cc space');
|
|
3603
|
+
fs.mkdirSync(ccSpaceTarget, { recursive: true });
|
|
3604
|
+
const rccSpace = spawnSync(process.execPath, [installer, '--target', ccSpaceTarget, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
3605
|
+
if (rccSpace.status !== 0) throw new Error('claude-code 含空格路径安装失败: ' + (rccSpace.stderr || ''));
|
|
3606
|
+
const ccSpaceManaged = collectManagedHookCommands(JSON.parse(fs.readFileSync(path.join(ccSpaceTarget, '.claude', 'settings.local.json'), 'utf8')));
|
|
3607
|
+
if (ccSpaceManaged.length === 0) throw new Error('含空格路径下未注入托管 hook 条目');
|
|
3608
|
+
for (const c of ccSpaceManaged) {
|
|
3609
|
+
assertManagedHookEntry(c, '含空格路径托管命令');
|
|
3610
|
+
const execRes = spawnSync('bash', ['-c', c], {
|
|
3611
|
+
cwd: ccSpaceTarget,
|
|
3612
|
+
encoding: 'utf8',
|
|
3613
|
+
timeout: 60000,
|
|
3614
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: ccSpaceTarget },
|
|
3615
|
+
input: JSON.stringify({ tool_name: 'Write', tool_input: { file_path: path.join(ccSpaceTarget, 'k13-space-probe.txt') } }),
|
|
3616
|
+
});
|
|
3617
|
+
if (execRes.status !== 0 || /Cannot find module/.test(String(execRes.stderr || ''))) {
|
|
3618
|
+
throw new Error(
|
|
3619
|
+
'含空格路径托管命令在 CC 执行语义下不可执行: ' + c +
|
|
3620
|
+
' —— ' + String(execRes.stderr || '').split('\n').slice(0, 3).join(' | ')
|
|
3621
|
+
);
|
|
3622
|
+
}
|
|
3623
|
+
}
|
|
3624
|
+
// ④ 幂等升级(claude-code):托管命令改回旧相对路径形态 → 重跑安装器 → 识别替换:
|
|
3625
|
+
// 恰余 1 条托管条目(无重复无残留)且形态达标(项目根引用特征在位)
|
|
3626
|
+
for (const g of ccSettings.hooks.PreToolUse) {
|
|
3627
|
+
for (const h of (Array.isArray(g.hooks) ? g.hooks : [])) {
|
|
3628
|
+
if (isManagedHookCommand(h.command)) h.command = 'node .claude/skills/flow-comet/scripts/comet-hook-guard.mjs';
|
|
3629
|
+
}
|
|
3630
|
+
}
|
|
3631
|
+
fs.writeFileSync(ccSettingsPath, JSON.stringify(ccSettings, null, 2) + '\n', 'utf8');
|
|
3632
|
+
const rccUp = spawnSync(process.execPath, [installer, '--target', ccTarget, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
3633
|
+
if (rccUp.status !== 0) throw new Error('claude-code 升级重装失败: ' + (rccUp.stderr || JSON.stringify(rccUp.output)));
|
|
3634
|
+
const ccAfter = collectManagedHookCommands(JSON.parse(fs.readFileSync(ccSettingsPath, 'utf8')));
|
|
3635
|
+
if (ccAfter.length !== 1) throw new Error('claude-code 升级后应恰余 1 条托管条目(旧条目被替换): ' + JSON.stringify(ccAfter));
|
|
3636
|
+
assertManagedHookEntry(ccAfter[0], 'claude-code 升级后托管命令');
|
|
3637
|
+
// ⑤ codex 平台同断言:安装产物形态达标 + 旧条目幂等升级
|
|
3638
|
+
const cxTarget = path.join(dir, 'k13-codex');
|
|
3639
|
+
fs.mkdirSync(cxTarget, { recursive: true });
|
|
3640
|
+
const rcx = spawnSync(process.execPath, [installer, '--target', cxTarget, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
3641
|
+
if (rcx.status !== 0) throw new Error('codex 安装失败: ' + (rcx.stderr || JSON.stringify(rcx.output)));
|
|
3642
|
+
const cxHooksPath = path.join(cxTarget, '.codex', 'hooks.json');
|
|
3643
|
+
const cxHooks = JSON.parse(fs.readFileSync(cxHooksPath, 'utf8'));
|
|
3644
|
+
const cxManaged = collectManagedHookCommands(cxHooks);
|
|
3645
|
+
if (cxManaged.length === 0) throw new Error('.codex/hooks.json 未注入托管 hook 条目');
|
|
3646
|
+
for (const c of cxManaged) assertManagedHookEntry(c, 'codex 托管命令');
|
|
3647
|
+
for (const g of cxHooks.hooks.PreToolUse) {
|
|
3648
|
+
for (const h of (Array.isArray(g.hooks) ? g.hooks : [])) {
|
|
3649
|
+
if (isManagedHookCommand(h.command)) h.command = 'node .agents/skills/flow-comet/scripts/comet-hook-guard.mjs before_tool --platform codex';
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
fs.writeFileSync(cxHooksPath, JSON.stringify(cxHooks, null, 2) + '\n', 'utf8');
|
|
3653
|
+
const rcxUp = spawnSync(process.execPath, [installer, '--target', cxTarget, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
3654
|
+
if (rcxUp.status !== 0) throw new Error('codex 升级重装失败: ' + (rcxUp.stderr || JSON.stringify(rcxUp.output)));
|
|
3655
|
+
const cxAfter = collectManagedHookCommands(JSON.parse(fs.readFileSync(cxHooksPath, 'utf8')));
|
|
3656
|
+
if (cxAfter.length !== 1) throw new Error('codex 升级后应恰余 1 条托管条目(旧条目被替换): ' + JSON.stringify(cxAfter));
|
|
3657
|
+
assertManagedHookEntry(cxAfter[0], 'codex 升级后托管命令');
|
|
3658
|
+
console.log(' hook 注入形态无关断言(basename+项目根引用特征)+ 新旧幂等升级 ✓');
|
|
3659
|
+
},
|
|
3660
|
+
},
|
|
3661
|
+
|
|
3662
|
+
// K14~K16: 桥接 loader 版本戳重装断言(安装器覆盖前版本比对明示的系统级锚定)——
|
|
3663
|
+
// 临时 DSH_HOME 预置不同版本戳 loader,真实安装器重装断言方向措辞(升级/降级/版本一致)、
|
|
3664
|
+
// 覆盖后磁盘为源新戳、同源重装收敛、purge 清除重建后路径回归。
|
|
3665
|
+
{
|
|
3666
|
+
name: 'K14 桥接 loader:版本戳重装断言(升级/降级方向与覆盖后新戳)',
|
|
3667
|
+
run: (dir) => {
|
|
3668
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3669
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3670
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3671
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
3672
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
3673
|
+
const srcVersion = readBridgeSourceVersion(loader);
|
|
3674
|
+
// 权威基准 = INSTALLED_VERSION:loader 标记行须与其同值——跨文件分叉时安装副本的
|
|
3675
|
+
// bridge-check 会报版本偏斜(本守卫让系统测试独立捕获分叉,不依赖另一套件)。夹具派生以该基准为准。
|
|
3676
|
+
const installedVersion = fs.readFileSync(path.join(__dirname, '..', 'INSTALLED_VERSION'), 'utf8').trim();
|
|
3677
|
+
if (srcVersion !== installedVersion) {
|
|
3678
|
+
throw new Error(
|
|
3679
|
+
'loader 版本戳(' + srcVersion + ')与 INSTALLED_VERSION(' + installedVersion +
|
|
3680
|
+
')不一致——发布同步遗漏(bridge-check 会在安装副本报版本偏斜)'
|
|
3681
|
+
);
|
|
3682
|
+
}
|
|
3683
|
+
// 旧戳/新戳从基准动态派生(固定值会随发布线演进失效——预发布语义下同主版本比较
|
|
3684
|
+
// 可能反转,如 1.5.0-rc.2 < 1.5.0):
|
|
3685
|
+
// 旧戳 = 主版本低一位 → 恒定「升级」方向;
|
|
3686
|
+
// 新戳 = 同主版本、次版本 +5 → 数值序高于源而字典序低于源(保持「数值序比较」护栏:
|
|
3687
|
+
// 字典序会误判为升级,断言「降级」即语义正确;与实现侧冒烟同口径)
|
|
3688
|
+
const [srcMajor, srcMinor] = installedVersion.split('.').map((p) => parseInt(p, 10));
|
|
3689
|
+
const OLD_STAMP = (srcMajor - 1) + '.0.0';
|
|
3690
|
+
const NEWER_STAMP = srcMajor + '.' + (srcMinor + 5) + '.0';
|
|
3691
|
+
const dshHome = path.join(dir, 'k14-dsh-home');
|
|
3692
|
+
const target = path.join(dir, 'k14-target');
|
|
3693
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3694
|
+
seedForeignFlowKit(target);
|
|
3695
|
+
const pluginPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
3696
|
+
const runInstall = () => {
|
|
3697
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3698
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3699
|
+
});
|
|
3700
|
+
// 统一包装为 { status, output }(与 runState 等助手同形态——assertOut 期望字符串 output)
|
|
3701
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
3702
|
+
};
|
|
3703
|
+
// ① 已装旧戳 loader → 重装输出「升级 A → B」逐字措辞 + 覆盖后磁盘为源新戳
|
|
3704
|
+
fs.mkdirSync(path.dirname(pluginPath), { recursive: true });
|
|
3705
|
+
fs.writeFileSync(pluginPath, stampBridgeLoader(loader, OLD_STAMP), 'utf8');
|
|
3706
|
+
const upgrade = runInstall();
|
|
3707
|
+
assertExit(upgrade, 0);
|
|
3708
|
+
assertOut(upgrade, '桥接 loader 升级 ' + OLD_STAMP + ' → ' + srcVersion);
|
|
3709
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
3710
|
+
throw new Error('升级重装后已装 loader 应被覆盖为源新戳');
|
|
3711
|
+
}
|
|
3712
|
+
// ② 已装新戳 loader(数值序高于源)→ 重装输出「降级 A → B」+ 覆盖后仍为源戳
|
|
3713
|
+
fs.writeFileSync(pluginPath, stampBridgeLoader(loader, NEWER_STAMP), 'utf8');
|
|
3714
|
+
const downgrade = runInstall();
|
|
3715
|
+
assertExit(downgrade, 0);
|
|
3716
|
+
assertOut(downgrade, '桥接 loader 降级 ' + NEWER_STAMP + ' → ' + srcVersion);
|
|
3717
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
3718
|
+
throw new Error('降级重装后已装 loader 应为源戳');
|
|
3719
|
+
}
|
|
3720
|
+
console.log(' 版本戳重装断言:升级/降级方向措辞与覆盖后新戳 ✓');
|
|
3721
|
+
},
|
|
3722
|
+
},
|
|
3723
|
+
|
|
3724
|
+
{
|
|
3725
|
+
name: 'K15 桥接 loader:同源重装断言(首次安装后版本一致)',
|
|
3726
|
+
run: (dir) => {
|
|
3727
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3728
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3729
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3730
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
3731
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
3732
|
+
const srcVersion = readBridgeSourceVersion(loader);
|
|
3733
|
+
const dshHome = path.join(dir, 'k15-dsh-home');
|
|
3734
|
+
const target = path.join(dir, 'k15-target');
|
|
3735
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3736
|
+
seedForeignFlowKit(target);
|
|
3737
|
+
const pluginPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
3738
|
+
const runInstall = () => {
|
|
3739
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
3740
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3741
|
+
});
|
|
3742
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
3743
|
+
};
|
|
3744
|
+
// ① 无已装 loader → 首次安装措辞(比对的空态前提)
|
|
3745
|
+
const first = runInstall();
|
|
3746
|
+
assertExit(first, 0);
|
|
3747
|
+
assertOut(first, '桥接 loader 首次安装');
|
|
3748
|
+
if (!fs.existsSync(pluginPath)) throw new Error('首次安装后 loader 应就位');
|
|
3749
|
+
// ② 同源重装(已装 = 源版本)→ 「版本一致(A)」逐字措辞 + 磁盘保持源戳
|
|
3750
|
+
const re = runInstall();
|
|
3751
|
+
assertExit(re, 0);
|
|
3752
|
+
assertOut(re, '桥接 loader 版本一致(' + srcVersion + ')');
|
|
3753
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
3754
|
+
throw new Error('同源重装后 loader 应保持源戳');
|
|
3755
|
+
}
|
|
3756
|
+
console.log(' 同源重装断言:首次安装 → 版本一致收敛 ✓');
|
|
3757
|
+
},
|
|
3758
|
+
},
|
|
3759
|
+
|
|
3760
|
+
{
|
|
3761
|
+
name: 'K16 桥接 loader:purge 后重建路径回归(loader 恢复与再装收敛)',
|
|
3762
|
+
run: (dir) => {
|
|
3763
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3764
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3765
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3766
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
3767
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
3768
|
+
const srcVersion = readBridgeSourceVersion(loader);
|
|
3769
|
+
const dshHome = path.join(dir, 'k16-dsh-home');
|
|
3770
|
+
const target = path.join(dir, 'k16-target');
|
|
3771
|
+
fs.mkdirSync(target, { recursive: true });
|
|
3772
|
+
seedForeignFlowKit(target);
|
|
3773
|
+
const pluginPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
3774
|
+
const patchPath = path.join(dshHome, 'cordis.patch.yml');
|
|
3775
|
+
const runInstall = (extra) => {
|
|
3776
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh', ...(extra || [])], {
|
|
3777
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
3778
|
+
});
|
|
3779
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
3780
|
+
};
|
|
3781
|
+
// ① 首次安装:loader 与 cordis.patch.yml 托管块就位
|
|
3782
|
+
const first = runInstall();
|
|
3783
|
+
assertExit(first, 0);
|
|
3784
|
+
if (!fs.existsSync(pluginPath)) throw new Error('首次安装后 loader 应就位');
|
|
3785
|
+
if (!fs.existsSync(patchPath)) throw new Error('首次安装后 cordis.patch.yml 应就位');
|
|
3786
|
+
// ② purge --yes:dsh 平台生成物清除后删除并重建——loader/托管块恢复为源形态
|
|
3787
|
+
const purgeRes = runInstall(['--purge', '--yes']);
|
|
3788
|
+
assertExit(purgeRes, 0);
|
|
3789
|
+
if (!fs.existsSync(pluginPath)) throw new Error('purge 后 loader 应重建恢复');
|
|
3790
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
3791
|
+
throw new Error('purge 后 loader 应为源戳');
|
|
3792
|
+
}
|
|
3793
|
+
if (!fs.existsSync(patchPath)) throw new Error('purge 后 cordis.patch.yml 托管块应重建');
|
|
3794
|
+
// ③ 清除后再重装:成功且「版本一致」收敛(重建成果可复装、版本比对路径回归)
|
|
3795
|
+
const re = runInstall();
|
|
3796
|
+
assertExit(re, 0);
|
|
3797
|
+
assertOut(re, '桥接 loader 版本一致(' + srcVersion + ')');
|
|
3798
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
3799
|
+
throw new Error('purge 后再装后 loader 应为源戳');
|
|
3800
|
+
}
|
|
3801
|
+
console.log(' purge 后重建路径回归:loader/托管块恢复 + 再装版本一致收敛 ✓');
|
|
3802
|
+
},
|
|
3803
|
+
},
|
|
3804
|
+
|
|
3805
|
+
// K17: 旧布局项目(状态文件在旧命名空间 + 同目录 Comet 资产/用户文件)经安装器自动迁移后,
|
|
3806
|
+
// 真实安装副本上的流程可继续——状态逐字节搬移且可读、合规节点链路 entry → record → exit
|
|
3807
|
+
// 正常推进、Comet 资产与用户文件原位未动。端到端执行断言(真实命令序列,非形态断言)。
|
|
3808
|
+
{
|
|
3809
|
+
name: 'K17 状态迁移:旧布局项目安装后状态可读且流程可继续',
|
|
3810
|
+
run: (dir) => {
|
|
3811
|
+
const repoRoot = path.resolve(__dirname, '..', '..', '..', '..');
|
|
3812
|
+
if (!fs.existsSync(path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet'))) return; // 安装副本无权威源
|
|
3813
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
3814
|
+
const target = path.join(dir, 'k17-target');
|
|
3815
|
+
const legacyDir = path.join(target, '.comet');
|
|
3816
|
+
fs.mkdirSync(legacyDir, { recursive: true });
|
|
3817
|
+
// 迁移前状态(进行中的 change:design 节点待完成)
|
|
3818
|
+
const stateBytes = Buffer.from(JSON.stringify({
|
|
3819
|
+
activeChange: 'mig-ch',
|
|
3820
|
+
currentNode: 'design',
|
|
3821
|
+
completedNodes: ['open'],
|
|
3822
|
+
evidence: { open: { summary: 'intake complete' } },
|
|
3823
|
+
verifyFailures: 0,
|
|
3824
|
+
executionMode: 'subagent',
|
|
3825
|
+
directOverride: false,
|
|
3826
|
+
}, null, 2) + '\n', 'utf8');
|
|
3827
|
+
fs.writeFileSync(path.join(legacyDir, 'flow-comet-state.json'), stateBytes);
|
|
3828
|
+
// 同目录的非 flow-comet 内容(旧目录三方共占实测形态):迁移不得处置它们
|
|
3829
|
+
fs.writeFileSync(path.join(legacyDir, 'config.yaml'), 'workflow:\n projectPath: docs/openspec\n');
|
|
3830
|
+
const userBytes = Buffer.from('user backup\n', 'utf8');
|
|
3831
|
+
fs.writeFileSync(path.join(legacyDir, 'flow-comet-state.json.bak-20260901-user'), userBytes);
|
|
3832
|
+
// 活动 change 工件(迁移后流程可继续的前提)
|
|
3833
|
+
writeFile(target, '.specs/mig-ch/CHANGE.md', '# CHANGE\n\n- **Change ID**: mig-ch\n\n## Why(为什么做)\n\nx\n');
|
|
3834
|
+
writeFile(target, '.specs/mig-ch/REQUIREMENT.md', '# REQUIREMENT\n\n- **Change ID**: mig-ch\n\n## 用户故事(User Story)\n\nx\n\n## 验收准则(AC)\n\n- y\n');
|
|
3835
|
+
writeFile(target, '.specs/mig-ch/DESIGN.md', '# DESIGN\n\n- **Change ID**: mig-ch\n\n## 0. 技术栈\n\nNode\n\n## 决策清单\n\n| # | D | R |\n|---|---|---|\n| D1 | x | y |\n');
|
|
3836
|
+
seedForeignFlowKit(target); // 安装器不再走网络克隆(与 K14~K16 同手法)
|
|
3837
|
+
const install = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'claude-code'], {
|
|
3838
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000,
|
|
3839
|
+
});
|
|
3840
|
+
const installRes = { status: install.status ?? 1, output: String(install.stdout || '') + String(install.stderr || '') };
|
|
3841
|
+
assertExit(installRes, 0);
|
|
3842
|
+
assertOut(installRes, '已迁移');
|
|
3843
|
+
// ① 状态逐字节搬到新命名空间;旧位置移除;迁移前备份在场且逐字节一致
|
|
3844
|
+
const migrated = path.join(target, '.flow-comet', 'flow-comet-state.json');
|
|
3845
|
+
if (!fs.existsSync(migrated)) throw new Error('迁移后新位置状态文件缺失: ' + migrated);
|
|
3846
|
+
if (!fs.readFileSync(migrated).equals(stateBytes)) throw new Error('迁移后状态与迁移前不逐字节一致');
|
|
3847
|
+
if (fs.existsSync(path.join(legacyDir, 'flow-comet-state.json'))) throw new Error('迁移后旧位置状态文件应已移除');
|
|
3848
|
+
const backups = fs.readdirSync(path.join(target, '.flow-comet')).filter((n) => /^flow-comet-state\.json\.bak-/.test(n));
|
|
3849
|
+
if (backups.length !== 1) throw new Error('迁移前备份应恰一份: ' + JSON.stringify(backups));
|
|
3850
|
+
if (!fs.readFileSync(path.join(target, '.flow-comet', backups[0])).equals(stateBytes)) {
|
|
3851
|
+
throw new Error('迁移前备份与迁移前状态不逐字节一致');
|
|
3852
|
+
}
|
|
3853
|
+
// ② 非 flow-comet 内容原位未动
|
|
3854
|
+
if (!fs.readFileSync(path.join(legacyDir, 'config.yaml')).equals(Buffer.from('workflow:\n projectPath: docs/openspec\n', 'utf8'))) {
|
|
3855
|
+
throw new Error('同目录 Comet 资产被改写');
|
|
3856
|
+
}
|
|
3857
|
+
if (!fs.readFileSync(path.join(legacyDir, 'flow-comet-state.json.bak-20260901-user')).equals(userBytes)) {
|
|
3858
|
+
throw new Error('同目录用户文件被改写');
|
|
3859
|
+
}
|
|
3860
|
+
// ③ 迁移后状态可读(真实安装副本,cwd = 项目根)
|
|
3861
|
+
const installed = (script, args) => {
|
|
3862
|
+
const res = spawnSync(process.execPath, [path.join(target, '.claude', 'skills', 'flow-comet', 'scripts', script), ...args], {
|
|
3863
|
+
cwd: target, encoding: 'utf8', timeout: 60000, env: { ...process.env, FLOW_COMET_RUN_ROOT: target },
|
|
3864
|
+
});
|
|
3865
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
3866
|
+
};
|
|
3867
|
+
const before = installed('workflow-state.mjs', ['status']);
|
|
3868
|
+
assertExit(before, 0);
|
|
3869
|
+
assertOut(before, '"change": "mig-ch"');
|
|
3870
|
+
assertOut(before, '"stateCurrentNode": "design"'); // 迁移搬移的状态字段原值可读
|
|
3871
|
+
// ④ 流程可继续:entry → record → exit 推进到下一节点
|
|
3872
|
+
assertExit(installed('workflow-guard.mjs', ['entry', 'design']), 0);
|
|
3873
|
+
assertExit(installed('workflow-state.mjs', ['record', 'design', '{"summary":"design done"}']), 0);
|
|
3874
|
+
const exitDesign = installed('workflow-guard.mjs', ['exit', 'design', '--apply']);
|
|
3875
|
+
assertExit(exitDesign, 0);
|
|
3876
|
+
assertOut(exitDesign, 'ALL CHECKS PASSED');
|
|
3877
|
+
const after = installed('workflow-state.mjs', ['status']);
|
|
3878
|
+
assertExit(after, 0);
|
|
3879
|
+
assertOut(after, '"currentNode": "plan"');
|
|
3880
|
+
const finalState = JSON.parse(fs.readFileSync(migrated, 'utf8'));
|
|
3881
|
+
if (!Array.isArray(finalState.completedNodes) || !finalState.completedNodes.includes('design')) {
|
|
3882
|
+
throw new Error('exit design 后 completedNodes 应含 design: ' + JSON.stringify(finalState.completedNodes));
|
|
3883
|
+
}
|
|
3884
|
+
console.log(' 迁移后链路:状态逐字节搬移 + 备份在场 + status 可读 + 节点推进 design → plan ✓');
|
|
3885
|
+
},
|
|
3886
|
+
},
|
|
3887
|
+
|
|
3888
|
+
// K18: 分发面(npm 包形态)——可发布性清单 + 包体边界 + bin 双入口 + init 词元形态与红线。
|
|
3889
|
+
// 权威源侧真跑断言(npm pack --dry-run 的真实产物清单在场才算通过);安装副本侧输出显式
|
|
3890
|
+
// 「不适用」行并回传同语义的结果标记计通过(禁止静默跳过——「未验证 ≠ 通过」)。
|
|
3891
|
+
// 四组断言见上方同名助手;结果标记由运行器按环境判据断言(distributionSurface 契约)。
|
|
3892
|
+
{
|
|
3893
|
+
name: 'K18 分发面:环境判据与包体边界·bin 双入口(权威源真跑/副本显式不适用)与 init 词元红线',
|
|
3894
|
+
distributionSurface: true,
|
|
3895
|
+
run: (dir) => {
|
|
3896
|
+
// 环境判据的判别力锚先跑(与所在环境无关的夹具驱动):判据判错会让下面整段走错分支——
|
|
3897
|
+
// 副本侧假红,或权威源侧静默漏检,故判据本身必须先被证明有条判别力。
|
|
3898
|
+
assertPackageContextJudgment(dir);
|
|
3899
|
+
const pkgCtx = resolvePackageContext();
|
|
3900
|
+
const marker = DISTRIBUTION_MARKERS[pkgCtx.kind];
|
|
3901
|
+
if (pkgCtx.kind === 'copy') {
|
|
3902
|
+
console.log(' 不适用(安装副本侧:' + pkgCtx.reason + ')——包体边界/bin 断言仅权威源侧可判,本项计通过');
|
|
3903
|
+
return marker;
|
|
3904
|
+
}
|
|
3905
|
+
assertPublishableManifest(pkgCtx.pkg);
|
|
3906
|
+
assertPackageBoundary(pkgCtx.root);
|
|
3907
|
+
assertInitTokenContract(assertBinContract(pkgCtx.pkg, pkgCtx.root), dir);
|
|
3908
|
+
return marker;
|
|
3909
|
+
},
|
|
3910
|
+
},
|
|
3911
|
+
|
|
3912
|
+
// ---------- L. 执行遗漏防护(M1~M8 真实链路) ----------
|
|
3913
|
+
|
|
3914
|
+
{
|
|
3915
|
+
name: 'L1 进入证据:entry 记录 enteredNodes,正常流程 exit 无误报',
|
|
3916
|
+
run: (dir) => {
|
|
3917
|
+
gitInit(dir);
|
|
3918
|
+
assertExit(runState(['init', CHANGE_ID, '--init-skip'], dir), 0);
|
|
3919
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md'], dir), 0);
|
|
3920
|
+
assertExit(runState(['skill-load', 'design', 'flow-comet-design', '--prompt', 'flow-kit/prompts/2-design.md'], dir), 0);
|
|
3921
|
+
writeIntakeArtifacts(dir);
|
|
3922
|
+
// ① entry open 记录进入标记
|
|
3923
|
+
assertExit(runGuard(['entry', 'open'], dir), 0);
|
|
3924
|
+
const st = readStateFile(dir);
|
|
3925
|
+
if (!Array.isArray(st.enteredNodes) || !st.enteredNodes.includes('open')) {
|
|
3926
|
+
throw new Error('entry 未记录 enteredNodes: ' + JSON.stringify(st.enteredNodes));
|
|
3927
|
+
}
|
|
3928
|
+
// ② 未 entry 直接 exit(构造:跳过 design 的 entry,state 已激活)→ ENTER WARN 渐进
|
|
3929
|
+
assertExit(runState(['record', 'open', '{"summary":"intake complete"}'], dir), 0);
|
|
3930
|
+
assertExit(runGuard(['exit', 'open', '--apply'], dir), 0);
|
|
3931
|
+
// 推进到 design 后不 entry 直接 exit?→ currentNode 检查会拦;验证 enter 证据在真实链路不误报
|
|
3932
|
+
assertExit(runGuard(['entry', 'design'], dir), 0);
|
|
3933
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/DESIGN.md', '# DESIGN\n\n- **Change ID**: ' + CHANGE_ID + '\n\n## 0. 技术栈\n\npython\n\n## 决策清单\n\n- [ ] D1\n');
|
|
3934
|
+
assertExit(runState(['record', 'design', '{"summary":"design done"}'], dir), 0);
|
|
3935
|
+
const rDesign = runGuard(['exit', 'design', '--apply'], dir);
|
|
3936
|
+
assertExit(rDesign, 0);
|
|
3937
|
+
assertNotOut(rDesign, 'ENTER WARN');
|
|
3938
|
+
},
|
|
3939
|
+
},
|
|
3940
|
+
|
|
3941
|
+
{
|
|
3942
|
+
name: 'L2 空退出豁免:显式 emptyExitApproved 后全 parallel execute 通过',
|
|
3943
|
+
run: (dir) => {
|
|
3944
|
+
gitInit(dir);
|
|
3945
|
+
// fixture state(currentNode=execute + enter 机制激活)+ 真实命令序列:
|
|
3946
|
+
// 全 parallel 任务在 plan exit 后正常路由 subagent-execute(M6 豁免用于已进入 execute 且无串行可做的场景)
|
|
3947
|
+
const taskAllParallel = '# TASK\n\n<task id="P01" parallel="true" status="pending"><action>p</action><write_files>a</write_files><verify>t</verify><done>d</done></task>\n';
|
|
3948
|
+
// ① 显式豁免 → 空退出通过
|
|
3949
|
+
const st = baseState('execute');
|
|
3950
|
+
st.enteredNodes = ['open', 'design', 'plan'];
|
|
3951
|
+
writeState(dir, st);
|
|
3952
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', taskAllParallel);
|
|
3953
|
+
assertExit(runGuard(['entry', 'execute'], dir), 0);
|
|
3954
|
+
assertExit(runState(['record', 'execute', '{"summary":"no serial tasks","emptyExitApproved":true}'], dir), 0);
|
|
3955
|
+
const r = runGuard(['exit', 'execute', '--apply'], dir);
|
|
3956
|
+
assertExit(r, 0);
|
|
3957
|
+
assertOut(r, 'ALL CHECKS PASSED');
|
|
3958
|
+
// ② 无豁免 → 仍 BLOCKED(默认防规划错误)
|
|
3959
|
+
const st2 = baseState('execute');
|
|
3960
|
+
st2.enteredNodes = ['open', 'design', 'plan'];
|
|
3961
|
+
writeState(dir, st2);
|
|
3962
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', taskAllParallel);
|
|
3963
|
+
assertExit(runGuard(['entry', 'execute'], dir), 0);
|
|
3964
|
+
assertExit(runState(['record', 'execute', '{"summary":"no serial tasks"}'], dir), 0);
|
|
3965
|
+
const r2 = runGuard(['exit', 'execute', '--apply'], dir);
|
|
3966
|
+
assertExit(r2, 1);
|
|
3967
|
+
assertOut(r2, 'BLOCKED');
|
|
3968
|
+
},
|
|
3969
|
+
},
|
|
3970
|
+
|
|
3971
|
+
{
|
|
3972
|
+
name: 'L3 空仓库:init 在无提交仓库输出 EMPTY-REPO 提示',
|
|
3973
|
+
run: (dir) => {
|
|
3974
|
+
execFileSync('git', ['init', '-q'], { cwd: dir, stdio: 'ignore' }); // 无提交
|
|
3975
|
+
const r = runState(['init', CHANGE_ID, '--init-skip'], dir);
|
|
3976
|
+
assertExit(r, 0);
|
|
3977
|
+
assertOut(r, 'EMPTY-REPO');
|
|
3978
|
+
},
|
|
3979
|
+
},
|
|
3980
|
+
];
|
|
3981
|
+
|
|
3982
|
+
// ---------- 运行 ----------
|
|
3983
|
+
|
|
3984
|
+
for (const item of TEST_ITEMS) {
|
|
3985
|
+
const dir = makeTmp();
|
|
3986
|
+
try {
|
|
3987
|
+
const marker = await item.run(dir);
|
|
3988
|
+
// 分发面项的机器可读结果标记:缺标记(静默跳过)或与环境不符(判定写反)都判该项失败
|
|
3989
|
+
if (item.distributionSurface) assertDistributionOutcome(item.name, marker);
|
|
3990
|
+
passed += 1;
|
|
3991
|
+
console.log('PASS: ' + item.name);
|
|
3992
|
+
} catch (e) {
|
|
3993
|
+
failures.push({ name: item.name, error: e.message });
|
|
3994
|
+
console.error('FAIL: ' + item.name + '\n' + e.message);
|
|
3995
|
+
} finally {
|
|
3996
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
3997
|
+
}
|
|
3998
|
+
}
|
|
3999
|
+
|
|
4000
|
+
console.log('SYSTEM TEST: ' + passed + '/' + TEST_ITEMS.length + ' passed');
|
|
4001
|
+
|
|
4002
|
+
// 清理验证:套件自身创建的临时目录不留残留
|
|
4003
|
+
const residue = createdDirs.filter((d) => fs.existsSync(d));
|
|
4004
|
+
if (residue.length > 0) {
|
|
4005
|
+
failures.push({ name: '临时目录清理', error: '残留目录: ' + residue.join(', ') });
|
|
4006
|
+
console.error('FAIL: 临时目录清理\n残留目录: ' + residue.join(', '));
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
if (failures.length > 0) {
|
|
4010
|
+
console.error('FAILED ITEMS: ' + failures.length);
|
|
4011
|
+
for (const f of failures) {
|
|
4012
|
+
console.error('- ' + f.name + '\n' + f.error);
|
|
4013
|
+
}
|
|
4014
|
+
process.exit(1);
|
|
4015
|
+
}
|
|
4016
|
+
|
|
4017
|
+
console.log('ALL SYSTEM TESTS PASSED');
|