flow-comet 1.6.3 → 1.6.5
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/skills/flow-comet/INSTALLED_VERSION +1 -1
- package/.flow-comet/skills/flow-comet/SKILL.md +21 -3
- package/.flow-comet/skills/flow-comet/reference/artifact-template-overlay.md +154 -0
- package/.flow-comet/skills/flow-comet/reference/artifact-templates.json +387 -0
- package/.flow-comet/skills/flow-comet/reference/entry-detail.md +8 -2
- package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +22 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +11 -13
- package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +2 -2
- package/.flow-comet/skills/flow-comet/scripts/evolve.mjs +180 -6
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +69 -18878
- package/.flow-comet/skills/flow-comet/scripts/health.mjs +29 -5
- package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +1053 -0
- package/.flow-comet/skills/flow-comet/scripts/items/b.mjs +830 -0
- package/.flow-comet/skills/flow-comet/scripts/items/c.mjs +781 -0
- package/.flow-comet/skills/flow-comet/scripts/items/d.mjs +99 -0
- package/.flow-comet/skills/flow-comet/scripts/items/e.mjs +185 -0
- package/.flow-comet/skills/flow-comet/scripts/items/f.mjs +90 -0
- package/.flow-comet/skills/flow-comet/scripts/items/g.mjs +59 -0
- package/.flow-comet/skills/flow-comet/scripts/items/h.mjs +817 -0
- package/.flow-comet/skills/flow-comet/scripts/items/i.mjs +77 -0
- package/.flow-comet/skills/flow-comet/scripts/items/j.mjs +75 -0
- package/.flow-comet/skills/flow-comet/scripts/items/k.mjs +1427 -0
- package/.flow-comet/skills/flow-comet/scripts/items/l.mjs +275 -0
- package/.flow-comet/skills/flow-comet/scripts/items/m.mjs +618 -0
- package/.flow-comet/skills/flow-comet/scripts/items/n.mjs +154 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/count-derivation.mjs +97 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/fix-loop-node.mjs +196 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-artifact-skeleton.mjs +406 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-evidence-gates.mjs +756 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +1235 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +384 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-state-io.mjs +404 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-template-fidelity.mjs +714 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +204 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/reentry-node.mjs +237 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/replan-node.mjs +209 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-branch.mjs +49 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-bridgecheck.mjs +201 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-admin.mjs +179 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +714 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-recovery.mjs +578 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-determine.mjs +52 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-evolve-hint.mjs +55 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-explain.mjs +123 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-intelscan.mjs +58 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-io.mjs +147 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-skillmarkers.mjs +249 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +1505 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/task-graph.mjs +471 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-assertions.mjs +509 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-fixtures.mjs +1248 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-helpers.mjs +237 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/text-anchor-checks.mjs +2493 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +214 -2
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +72 -1123
- package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +976 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/b.mjs +1233 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/c.mjs +1382 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/d.mjs +483 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/e.mjs +1398 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +1466 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +1367 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/h.mjs +1287 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/i.mjs +1436 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/j.mjs +1384 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/k.mjs +1338 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/l.mjs +1492 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +971 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/n.mjs +2125 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +1 -1
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +59 -7559
- package/.flow-comet/skills/flow-comet/scripts/time-utils.mjs +4 -4
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +223 -2476
- package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +5 -5
- package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +27 -2190
- package/.flow-comet/skills/flow-comet-archive/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-change/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-design/SKILL.md +15 -8
- package/.flow-comet/skills/flow-comet-dev/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-evolve/SKILL.md +1 -0
- package/.flow-comet/skills/flow-comet-execute/SKILL.md +13 -10
- package/.flow-comet/skills/flow-comet-health/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-integration/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +14 -4
- package/.flow-comet/skills/flow-comet-open/SKILL.md +11 -9
- package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-plan/SKILL.md +11 -7
- package/.flow-comet/skills/flow-comet-requirement/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-review/SKILL.md +15 -9
- package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +11 -8
- package/.flow-comet/skills/flow-comet-task/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-test/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +2 -2
- package/.flow-comet/skills/flow-comet-verify/SKILL.md +12 -8
- package/README.md +52 -69
- package/package.json +3 -2
- package/scripts/dsh-bridge.mjs +2 -2
- package/scripts/lib/installer-cleanup.mjs +152 -0
- package/scripts/lib/installer-flowkit.mjs +159 -0
- package/scripts/lib/installer-hooks.mjs +363 -0
- package/scripts/lib/installer-loader.mjs +220 -0
- package/scripts/lib/installer-migrate.mjs +236 -0
- package/scripts/lib/installer-platforms.mjs +314 -0
- package/scripts/prepare-env.mjs +21 -1313
|
@@ -0,0 +1,1505 @@
|
|
|
1
|
+
// system-fixtures.mjs — 系统测试集的夹具与判据助手(单一来源)
|
|
2
|
+
//
|
|
3
|
+
// 定位:`system-test.mjs` 的十三个条目组文件(`items/*.mjs`)共用的夹具。条目组文件是**数据层**——
|
|
4
|
+
// 只承载 `export const ENTRIES = [...]`(计数派生按此聚合,见 `count-derivation.mjs`),夹具一律从本
|
|
5
|
+
// 模块取;组文件之间零 import,兄弟组之间没有边。
|
|
6
|
+
//
|
|
7
|
+
// 为什么单独成册:`test-fixtures.mjs` 是 `guard-self-test.mjs` 的场景夹具——与 `test-helpers.mjs`
|
|
8
|
+
// 的分工同形,同名同责的工具汇在 `test-helpers`(单一来源),**各套件特有**的夹具各自成册。
|
|
9
|
+
// 本模块与 `test-fixtures.mjs` 并列,两者互不 import。
|
|
10
|
+
//
|
|
11
|
+
// 内容分族(自上而下):① 状态 / git / 工件与 Return Contract 夹具;② 修复回路真实命令链路夹具;
|
|
12
|
+
// ③ explain 契约对账夹具;④ 多趟链路材料;⑤ 分发面(npm 包形态)判据与包体断言;⑥ 安装器升级
|
|
13
|
+
// 路径清理与镜像根 fail-closed;⑦ 桥接 loader 版本戳重装;⑧ hook 注入命令形态判定。
|
|
14
|
+
//
|
|
15
|
+
// 路径推导:本模块位于 `<技能包>/scripts/lib/`,`SCRIPTS_DIR` 由 import.meta.url 相对推导——技能树内
|
|
16
|
+
// 兄弟脚本路径一律由它拼接,取用方不再各自推导同一条路径。
|
|
17
|
+
|
|
18
|
+
import fs from 'fs';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
import os from 'os';
|
|
21
|
+
import { execFileSync, spawnSync } from 'child_process';
|
|
22
|
+
import { fileURLToPath } from 'url';
|
|
23
|
+
import { assertExit, assertOut, copyTreePortable, runGuard, runState, writeFile, writeState } from './test-helpers.mjs';
|
|
24
|
+
import { NODE_ARTIFACT_TEMPLATES } from '../protocol-utils.mjs';
|
|
25
|
+
|
|
26
|
+
// 路径推导基准:技能包 scripts/ 目录(与套件入口同基准)
|
|
27
|
+
export const SCRIPTS_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
28
|
+
|
|
29
|
+
export const CHANGE_ID = 'ch';
|
|
30
|
+
|
|
31
|
+
// 读取 state 文件(测试断言用)
|
|
32
|
+
export function readStateFile(root) {
|
|
33
|
+
return JSON.parse(fs.readFileSync(path.join(root, '.flow-comet', 'flow-comet-state.json'), 'utf8'));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 读取 state 原始字节(「字节零改写」断言用——不解析,直接逐字节比较)
|
|
37
|
+
export function readStateBytes(root) {
|
|
38
|
+
return fs.readFileSync(path.join(root, '.flow-comet', 'flow-comet-state.json'));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// git 工具:临时目录内初始化仓库(含初始 commit——unborn HEAD 下 init 不会建分支)
|
|
42
|
+
export function gitInit(root) {
|
|
43
|
+
execFileSync('git', ['init', '-q'], { cwd: root, stdio: 'ignore' });
|
|
44
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '--allow-empty', '-m', 'init'], { cwd: root, stdio: 'ignore' });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function gitBranch(root) {
|
|
48
|
+
return execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: root, encoding: 'utf8' }).trim();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// open 阶段工件(CHANGE 含 Why;REQUIREMENT 含 用户故事 + 验收准则——通过出口校验的完整形态)。
|
|
53
|
+
// 「视觉调性」是 CHANGE 模板的条件段且新 change 要求在场:夹具按其结构形态标注「不适用」
|
|
54
|
+
//(独立行 + 理由),使前端判据稳定地判为非前端——不写这段会让 ui-design 门对新 change 报「段缺席」。
|
|
55
|
+
// visualTone 覆盖该段正文:需要判为「前端」的夹具(或需要构造段内自然句子负例的夹具)**改这一段**,
|
|
56
|
+
// 不要另追加第二个「视觉调性」段——同名段重复时判据取首段,追加对首段没有影响。
|
|
57
|
+
export const DEFAULT_VISUAL_TONE = '不适用(夹具:后端任务,无用户可见界面)。';
|
|
58
|
+
|
|
59
|
+
export function writeIntakeArtifacts(root, changeId = CHANGE_ID, { visualTone = DEFAULT_VISUAL_TONE } = {}) {
|
|
60
|
+
writeFile(root, '.specs/' + changeId + '/CHANGE.md', '# CHANGE\n\n- **Change ID**: ' + changeId
|
|
61
|
+
+ '\n\n## Why(为什么做)\n\n变更目标。\n\n## 视觉调性\n\n' + visualTone + '\n');
|
|
62
|
+
writeFile(root, '.specs/' + changeId + '/REQUIREMENT.md', '# REQUIREMENT\n\n- **Change ID**: ' + changeId + '\n\n## 用户故事(User Story)\n\n用户需求。\n\n## 验收准则(AC)\n\n- 通过标准\n');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 自定义协议(compose-demo):3 节点 brainstorm/tdd/codereview(避开内置 8 节点 id)——
|
|
66
|
+
// 协议数据化路由 + 通用层防线对自定义节点生效;无 writeWhitelist(hook 回退内置缺省表)
|
|
67
|
+
|
|
68
|
+
// handoff 完整 Return Contract(W1-D 严格校验通过形态:commitHash + greenEvidence + completedChecks)
|
|
69
|
+
export function fullContract(commitHash, taskId) {
|
|
70
|
+
return JSON.stringify({
|
|
71
|
+
status: 'DONE',
|
|
72
|
+
taskId,
|
|
73
|
+
commitHash,
|
|
74
|
+
completedChecks: ['required-skill:subagent-execute.flow-comet-dev'],
|
|
75
|
+
greenEvidence: { command: 'node --check src/' + taskId.toLowerCase() + '.mjs', output: 'ok' },
|
|
76
|
+
redEvidence: { command: 'node --check src/' + taskId.toLowerCase() + '.mjs' },
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 仓库本地验证工具执行(仅主仓存在、不随克隆分发;副本上文件缺失自动跳过):
|
|
81
|
+
// 返回 { status, output }(spawnSync 原始结果 output 是数组,统一拼接成字符串供断言)
|
|
82
|
+
|
|
83
|
+
// archive 记录的证据 JSON:含归档提名三要素(扫描份数 / 提名编号 / 无提名理由)。新 change 的 archive
|
|
84
|
+
// 出口会核对 evidence.lessonsNomination——夹具只写 summary 会被提名门拦下,掩盖各条目真正被测的
|
|
85
|
+
// 归档/路由判据。summaryIds = 该夹具已写入的 `*-SUMMARY.md` 任务号(扫描份数按此对账,不另抄数字)。
|
|
86
|
+
export function archiveRecordJson(summary, summaryIds = []) {
|
|
87
|
+
return JSON.stringify({
|
|
88
|
+
summary,
|
|
89
|
+
lessonsNomination: {
|
|
90
|
+
scanned: summaryIds.length,
|
|
91
|
+
nominated: [],
|
|
92
|
+
reasonIfNone: '夹具:本 change 无沉淀候选入库',
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 多趟链路 SUMMARY 夹具(SUMMARY 模板保真全量满足:标题首行 # SUMMARY: + 首部 4 字段 +
|
|
98
|
+
// 段序 做了什么→改动文件→verify 输出→6 维自查→越界检查→自检方法;6 维声明 brooks-review)
|
|
99
|
+
export function execSummaryFixture(taskId) {
|
|
100
|
+
return [
|
|
101
|
+
'# SUMMARY: ' + taskId + ' - 多趟链路任务',
|
|
102
|
+
'',
|
|
103
|
+
'- **Change ID**: ' + CHANGE_ID,
|
|
104
|
+
'- **Task ID**: ' + taskId,
|
|
105
|
+
'- **完成时间**: 2026-08-25 10:00',
|
|
106
|
+
'- **AI 角色**: Dev',
|
|
107
|
+
'',
|
|
108
|
+
'## 做了什么',
|
|
109
|
+
'',
|
|
110
|
+
'完成 ' + taskId + ' 的实现并回传证据。',
|
|
111
|
+
'',
|
|
112
|
+
'## 改动文件',
|
|
113
|
+
'',
|
|
114
|
+
'| 文件 | 性质 | 说明 |',
|
|
115
|
+
'|---|---|---|',
|
|
116
|
+
'| src/' + taskId.toLowerCase() + '.mjs | 修改 | 实现任务 |',
|
|
117
|
+
'',
|
|
118
|
+
'## verify 输出',
|
|
119
|
+
'',
|
|
120
|
+
'```text',
|
|
121
|
+
'$ node --check src/' + taskId.toLowerCase() + '.mjs',
|
|
122
|
+
'ok',
|
|
123
|
+
'```',
|
|
124
|
+
'',
|
|
125
|
+
'## 6 维自查',
|
|
126
|
+
'',
|
|
127
|
+
'- 功能: 通过(brooks-review 已跑)',
|
|
128
|
+
'- 性能: 无影响',
|
|
129
|
+
'- 安全: 无影响',
|
|
130
|
+
'- 兼容: 通过',
|
|
131
|
+
'- 可观测: 通过',
|
|
132
|
+
'- 可维护: 通过',
|
|
133
|
+
'',
|
|
134
|
+
'## 越界检查',
|
|
135
|
+
'',
|
|
136
|
+
'仅修改 write_files 范围内文件,无越界。',
|
|
137
|
+
'',
|
|
138
|
+
'## 自检方法',
|
|
139
|
+
'',
|
|
140
|
+
'brooks-review',
|
|
141
|
+
'',
|
|
142
|
+
'## LESSONS 扫描',
|
|
143
|
+
'',
|
|
144
|
+
'无 active 条目命中(夹具:本任务 write_files 与 .specs/LESSONS.md 关键词行无交集)。',
|
|
145
|
+
'',
|
|
146
|
+
].join('\n');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 同夹具的「段内大写 Z 早于方法名」变体(真实机检标记形态):6 维自查段体首行是含大写 Z 的机检
|
|
150
|
+
// 标记说明,方法声明行在其后。段尾判定若把大写 Z 当串尾(惰性段体在段内首个 Z 处提前收尾),
|
|
151
|
+
// 方法声明被截掉 ⇒ execute 家族出口误判「6 维自查未声明自检方法」而 BLOCK。
|
|
152
|
+
// 夹具前提不成立(段标题形态变化)即抛错——不让变体静默退化成与基线逐字节相同的形态。
|
|
153
|
+
|
|
154
|
+
// ---------- 修复回路真实命令链路夹具(A21~A23 共用) ----------
|
|
155
|
+
|
|
156
|
+
// 串行任务块(字段齐备:可过 task-parsing 解析与 guard 结构校验)
|
|
157
|
+
export function serialTaskBlock(id, status) {
|
|
158
|
+
return '<task id="' + id + '" parallel="false" status="' + status + '">' +
|
|
159
|
+
'<action>实现 ' + id + '</action><write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
160
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify></task>';
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Fix 批次任务集:既有任务 T01 done +「Fix 任务」段追加修复任务(追加纪律与节点技能一致)
|
|
164
|
+
export function fixBatchTaskText(fixStatus) {
|
|
165
|
+
return '# TASK\n\n## 任务清单\n\n' + serialTaskBlock('T01', 'done') +
|
|
166
|
+
'\n\n## Fix 任务(来自 REVIEW / INTEGRATION)\n\n' + serialTaskBlock('T-FIX-01', fixStatus) + '\n';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 并行 Fix 任务集(无依赖 → 可委托):既有任务 T01 done + parallel 修复任务(状态由参数指定)。
|
|
170
|
+
export function fixBatchParallelTaskText(fixStatus) {
|
|
171
|
+
return '# TASK\n\n## 任务清单\n\n' + serialTaskBlock('T01', 'done') +
|
|
172
|
+
'\n\n## Fix 任务(来自 REVIEW / INTEGRATION)\n\n'
|
|
173
|
+
+ '<task id="P-FIX-01" parallel="true" status="' + fixStatus + '">'
|
|
174
|
+
+ '<action>实现 P-FIX-01</action><write_files>src/p-fix-01.mjs</write_files>'
|
|
175
|
+
+ '<verify>node --check src/p-fix-01.mjs</verify></task>\n';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 两波修复任务集(多波次真实 Fix 链路夹具):既有任务 T01 done + Fix 段内两个修复任务,
|
|
179
|
+
// 两个修复任务的状态由入参分别指定——第一波收口后追加第二波,任务集签名随之发散。
|
|
180
|
+
export function fixBatchTwoFixTaskText(fixStatus1, fixStatus2) {
|
|
181
|
+
return '# TASK\n\n## 任务清单\n\n' + serialTaskBlock('T01', 'done') +
|
|
182
|
+
'\n\n## Fix 任务(来自 REVIEW / INTEGRATION)\n\n' +
|
|
183
|
+
serialTaskBlock('T-FIX-01', fixStatus1) + '\n' + serialTaskBlock('T-FIX-02', fixStatus2) + '\n';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 审查文档:disposed=false 时发现条目缺处置标记(钉住 review 出口的真实校验与恢复)
|
|
187
|
+
export function fixBatchReviewDoc(disposed) {
|
|
188
|
+
const finding = disposed
|
|
189
|
+
? '- **状态机路径未闭环** —— 已追加修复任务并回修。[已修]'
|
|
190
|
+
: '- **状态机路径未闭环** —— 已追加修复任务,处置标记待补。';
|
|
191
|
+
return '# REVIEW\n\n## 发现\n\n### Critical\n\n- 无\n\n### Major\n\n' + finding +
|
|
192
|
+
'\n\n### Minor\n\n- 无\n\n## 结论\n\nFix 批次在 execute 生命周期内完成后回源节点重新跑出口。\n';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// done-but-unclosed 回放夹具:history 最新 exit-applied execute 记录的任务集
|
|
196
|
+
// 签名仍是追加修复任务之前的占位值(与当前「既有任务 + 追加修复任务」任务集签名必然不等)——
|
|
197
|
+
// 机器信号 =「修复任务已 done,但本批 execute 出口从未重新通过」。判据只比较是否相等,占位常量
|
|
198
|
+
// 与哈希实现解耦;旧 state 无该字段 → 渐进放行。
|
|
199
|
+
const STALE_TASK_SET_SIGNATURE = '0'.repeat(64);
|
|
200
|
+
export function fixBatchHistoryWithStaleExecuteExit() {
|
|
201
|
+
return [{ event: 'exit-applied', node: 'execute', at: '2026-09-20T00:00:00.000Z', taskSetSignature: STALE_TASK_SET_SIGNATURE }];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Fix 批次夹具写入:state + TASK + 既有任务 SUMMARY + REVIEW.md(verify 源另备 TEST/UAT)。
|
|
205
|
+
// kind='review'(审查驻留,execute 已首次完成)/ 'verify'(验证驻留,review 已完成);
|
|
206
|
+
// newChange=false 时 state 不带新 change 标记(旧 change 渐进形态)。
|
|
207
|
+
export function writeFixBatchFixture(dir, { kind, fixStatus, newChange = true, parallelFix = false }) {
|
|
208
|
+
const completed = kind === 'verify'
|
|
209
|
+
? ['open', 'design', 'plan', 'execute', 'subagent-execute', 'review']
|
|
210
|
+
: ['open', 'design', 'plan', 'execute', 'subagent-execute'];
|
|
211
|
+
const state = {
|
|
212
|
+
activeChange: CHANGE_ID,
|
|
213
|
+
currentNode: kind,
|
|
214
|
+
completedNodes: completed,
|
|
215
|
+
enteredNodes: [...completed, kind],
|
|
216
|
+
evidence: {
|
|
217
|
+
execute: { summary: 'first pass executed' },
|
|
218
|
+
review: kind === 'review' ? { summary: 'review in progress' } : { summary: 'review complete' },
|
|
219
|
+
},
|
|
220
|
+
verifyFailures: 0,
|
|
221
|
+
executionMode: 'subagent',
|
|
222
|
+
directOverride: false,
|
|
223
|
+
};
|
|
224
|
+
if (kind === 'verify') state.evidence.verify = { summary: 'verify in progress' };
|
|
225
|
+
if (newChange) state.newChange = true;
|
|
226
|
+
writeState(dir, state);
|
|
227
|
+
// 前置节点产物齐备(open/design/plan)——共享路由在任务特判前先过前置产物门控
|
|
228
|
+
writeIntakeArtifacts(dir);
|
|
229
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/DESIGN.md',
|
|
230
|
+
'# DESIGN\n\n- **Change ID**: ' + CHANGE_ID + '\n\n## 0. 技术栈选定\n\nNode.js(ESM)\n\n## 决策清单\n\n- [ ] 循环路由\n');
|
|
231
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md',
|
|
232
|
+
parallelFix ? fixBatchParallelTaskText(fixStatus) : fixBatchTaskText(fixStatus));
|
|
233
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/T01-SUMMARY.md', execSummaryFixture('T01'));
|
|
234
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/REVIEW.md', fixBatchReviewDoc(true));
|
|
235
|
+
if (kind === 'verify') {
|
|
236
|
+
// 真实验证命令:落盘副作用文件——出口是否执行验证命令以文件系统事实判定(不只看退出码)
|
|
237
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TEST.md',
|
|
238
|
+
'# TEST\n\n## 验证命令\n\n```\nnode -e "require(\'fs\').writeFileSync(\'verify-gate-ran.txt\', \'GATE-RAN\')"\n```\n');
|
|
239
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/UAT.md', '# UAT\n\n## 验收\n\n- 通过\n');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// 完成修复任务的后半段:标记 done + 逐任务 SUMMARY + 重载 execute 声明 + record execute。
|
|
244
|
+
// record 的新 change 前置门要求本节点加载声明;takeover 豁免回传显式落库,供出口校验读取。
|
|
245
|
+
export function completeFixTaskAndRecord(dir, env) {
|
|
246
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/TASK.md', fixBatchTaskText('done'));
|
|
247
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/T-FIX-01-SUMMARY.md', execSummaryFixture('T-FIX-01'));
|
|
248
|
+
assertExit(runState(['skill-load', 'execute', 'flow-comet-execute', '--prompt', 'flow-kit/prompts/4-dev.md', '--templates', 'SUMMARY.md'], dir, env), 0);
|
|
249
|
+
const rec = runState(['record', 'execute', '{"summary":"fix batch executed","parallelTakeoverApproved":true}'], dir, env);
|
|
250
|
+
assertExit(rec, 0);
|
|
251
|
+
assertOut(rec, 'EVIDENCE: execute');
|
|
252
|
+
const st = readStateFile(dir);
|
|
253
|
+
if (st.evidence?.execute?.summary !== 'fix batch executed') {
|
|
254
|
+
throw new Error('record execute 未刷新证据: ' + JSON.stringify(st.evidence?.execute));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 节点行断言(行锚定):防「关键词作为子串出现在其它行」的弱断言
|
|
259
|
+
export function assertNodeLine(res, nodeId) {
|
|
260
|
+
if (!new RegExp('^NODE: ' + nodeId + '$', 'm').test(res.output)) {
|
|
261
|
+
throw new Error('输出缺少行 NODE: ' + nodeId + '(exit ' + res.status + ')\n实际输出:\n' + res.output);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function assertNotNodeLine(res, nodeId) {
|
|
266
|
+
if (new RegExp('^NODE: ' + nodeId + '$', 'm').test(res.output)) {
|
|
267
|
+
throw new Error('输出不应包含行 NODE: ' + nodeId + '(exit ' + res.status + ')\n实际输出:\n' + res.output);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ---------- explain <node> 契约对账(节点现问) ----------
|
|
272
|
+
|
|
273
|
+
// 期望值一律**现场从协议 JSON 派生**(本段不手抄第二份字段表):解析实现输出后逐字段与
|
|
274
|
+
// `nodes[] / outputSchemas[]` 的声明比对。行前缀与分隔符是契约形态的一部分——不可解析即报问题
|
|
275
|
+
// (不静默跳过)。反向构造探针复用同一判据(声明侧抽掉一条 guardrail / 一个 artifact / 改描述)。
|
|
276
|
+
|
|
277
|
+
// ARTIFACT 行:`<id> [<kind>] required|optional → <路径 | 路径>`(kind 可缺省;无路径 → 未声明路径)。
|
|
278
|
+
function parseExplainArtifactLine(value) {
|
|
279
|
+
const arrow = value.split(' → ');
|
|
280
|
+
if (arrow.length !== 2) throw new Error('ARTIFACT 行缺「… → 路径」形态: ' + value);
|
|
281
|
+
const requiredMatch = arrow[0].match(/ (required|optional)$/);
|
|
282
|
+
if (!requiredMatch) throw new Error('ARTIFACT 行缺 required/optional 标注: ' + value);
|
|
283
|
+
const head = arrow[0].slice(0, arrow[0].length - requiredMatch[0].length);
|
|
284
|
+
const kindMatch = head.match(/^(.*) \[([^\]]+)\]$/);
|
|
285
|
+
return {
|
|
286
|
+
id: kindMatch ? kindMatch[1] : head,
|
|
287
|
+
kind: kindMatch ? kindMatch[2] : null,
|
|
288
|
+
required: requiredMatch[1] === 'required',
|
|
289
|
+
paths: arrow[1] === '(未声明路径)' ? [] : arrow[1].split(' | '),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// GUARDRAIL 行:`<id> | <label> | <validation>`。
|
|
294
|
+
function parseExplainGuardrailLine(value) {
|
|
295
|
+
const parts = value.split(' | ');
|
|
296
|
+
if (parts.length !== 3) throw new Error('GUARDRAIL 行缺三段(id | label | validation): ' + value);
|
|
297
|
+
return { id: parts[0], label: parts[1], validation: parts[2] };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// REQUIRED-SKILL 行:`<skill> | scope=<…> | enforcement=<…> | reason=<…>`。
|
|
301
|
+
function parseExplainSkillLine(value) {
|
|
302
|
+
const parts = value.split(' | ');
|
|
303
|
+
if (parts.length !== 4) throw new Error('REQUIRED-SKILL 行缺四段(skill | scope= | enforcement= | reason=): ' + value);
|
|
304
|
+
const [skill, scope, enforcement, reason] = parts;
|
|
305
|
+
if (!scope.startsWith('scope=') || !enforcement.startsWith('enforcement=') || !reason.startsWith('reason=')) {
|
|
306
|
+
throw new Error('REQUIRED-SKILL 行的键名形态不符: ' + value);
|
|
307
|
+
}
|
|
308
|
+
return { skill, scope: scope.slice(6), enforcement: enforcement.slice(12), reason: reason.slice(7) };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function parseExplainOutput(output) {
|
|
312
|
+
const parsed = {
|
|
313
|
+
attributes: {}, schemas: [], artifacts: [], evidence: [], guardrails: [], guardrailsNone: false,
|
|
314
|
+
skills: [], skillsNone: false, templates: [], commands: [],
|
|
315
|
+
};
|
|
316
|
+
let schemaIndex = -1;
|
|
317
|
+
for (const rawLine of String(output).split(/\r?\n/)) {
|
|
318
|
+
const line = rawLine.trim();
|
|
319
|
+
if (line === '') continue;
|
|
320
|
+
const value = line.slice(line.indexOf(': ') + 2);
|
|
321
|
+
if (line.startsWith('NODE: ')) parsed.attributes.node = value;
|
|
322
|
+
else if (line.startsWith('LABEL: ')) parsed.attributes.label = value;
|
|
323
|
+
else if (line.startsWith('KIND: ')) parsed.attributes.kind = value;
|
|
324
|
+
else if (line.startsWith('DISABLED: ')) parsed.attributes.disabled = value;
|
|
325
|
+
else if (line.startsWith('OUTPUT-SCHEMA: ')) {
|
|
326
|
+
parsed.schemas.push(value);
|
|
327
|
+
schemaIndex += 1;
|
|
328
|
+
parsed.artifacts.push([]);
|
|
329
|
+
parsed.evidence.push([]);
|
|
330
|
+
} else if (line.startsWith('ARTIFACT: ')) {
|
|
331
|
+
if (schemaIndex < 0) throw new Error('ARTIFACT 行先于 OUTPUT-SCHEMA 行: ' + line);
|
|
332
|
+
parsed.artifacts[schemaIndex].push(parseExplainArtifactLine(value));
|
|
333
|
+
} else if (line.startsWith('EVIDENCE: ')) {
|
|
334
|
+
if (schemaIndex < 0) throw new Error('EVIDENCE 行先于 OUTPUT-SCHEMA 行: ' + line);
|
|
335
|
+
parsed.evidence[schemaIndex].push(value);
|
|
336
|
+
} else if (line.startsWith('GUARDRAIL: ')) {
|
|
337
|
+
if (value === '(none)') parsed.guardrailsNone = true;
|
|
338
|
+
else parsed.guardrails.push(parseExplainGuardrailLine(value));
|
|
339
|
+
} else if (line.startsWith('REQUIRED-SKILL: ')) {
|
|
340
|
+
if (value === '(none)') parsed.skillsNone = true;
|
|
341
|
+
else parsed.skills.push(parseExplainSkillLine(value));
|
|
342
|
+
} else if (/^TEMPLATE(?:(条件模板))?: /.test(line)) {
|
|
343
|
+
parsed.templates.push(line);
|
|
344
|
+
} else if (line.startsWith('COMMAND ')) parsed.commands.push(line);
|
|
345
|
+
else throw new Error('explain 输出含未识别的行形态: ' + line);
|
|
346
|
+
}
|
|
347
|
+
return parsed;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 期望值比对(解析结果 vs 协议声明;两侧同源,锚里不手抄字段表)。
|
|
351
|
+
function expectExplainField(problems, nodeId, actual, declared, field) {
|
|
352
|
+
if (actual !== declared) {
|
|
353
|
+
problems.push('节点 ' + nodeId + ' 的 ' + field + ' 与协议声明不一致: 输出 '
|
|
354
|
+
+ JSON.stringify(actual) + ' / 声明 ' + JSON.stringify(declared));
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// ① outputSchema 名与描述 + ② 每个 artifact 的 id·kind·required·路径(pathBase: specs-root ⇒ .specs/
|
|
359
|
+
// 前缀)与每个 evidence 的 id (required)。
|
|
360
|
+
function explainSchemaProblems(node, parsed, schemaById) {
|
|
361
|
+
const problems = [];
|
|
362
|
+
const declaredSchemas = node.outputSchemas ?? [];
|
|
363
|
+
const expectedSchemaLines = declaredSchemas.length === 0
|
|
364
|
+
? ['(none)']
|
|
365
|
+
: declaredSchemas.map((schemaId) => {
|
|
366
|
+
const schema = schemaById.get(schemaId);
|
|
367
|
+
return schemaId + (schema && schema.description ? ' — ' + schema.description : '');
|
|
368
|
+
});
|
|
369
|
+
expectExplainField(problems, node.id, parsed.schemas.join(' ␟ '), expectedSchemaLines.join(' ␟ '), 'OUTPUT-SCHEMA 行');
|
|
370
|
+
declaredSchemas.forEach((schemaId, index) => {
|
|
371
|
+
const schema = schemaById.get(schemaId);
|
|
372
|
+
const declaredArtifacts = (schema?.artifacts ?? []).map((artifact) => ({
|
|
373
|
+
id: artifact.id ?? '(unnamed)',
|
|
374
|
+
kind: artifact.kind ?? null,
|
|
375
|
+
required: artifact.required !== false,
|
|
376
|
+
paths: (artifact.paths ?? []).map((declaredPath) =>
|
|
377
|
+
(artifact.pathBase === 'specs-root' ? '.specs/' : '') + String(declaredPath)),
|
|
378
|
+
}));
|
|
379
|
+
const actualArtifacts = parsed.artifacts[index] ?? [];
|
|
380
|
+
const artifactsMatch = actualArtifacts.length === declaredArtifacts.length
|
|
381
|
+
&& actualArtifacts.every((actual, position) => actual.id === declaredArtifacts[position].id
|
|
382
|
+
&& actual.kind === declaredArtifacts[position].kind
|
|
383
|
+
&& actual.required === declaredArtifacts[position].required
|
|
384
|
+
&& actual.paths.join(' | ') === declaredArtifacts[position].paths.join(' | '));
|
|
385
|
+
if (!artifactsMatch) {
|
|
386
|
+
problems.push('节点 ' + node.id + '/' + schemaId + ' 的 artifact 对账不一致: 输出 '
|
|
387
|
+
+ JSON.stringify(actualArtifacts) + ' / 声明 ' + JSON.stringify(declaredArtifacts));
|
|
388
|
+
}
|
|
389
|
+
const declaredEvidence = (schema?.evidence ?? []).length === 0
|
|
390
|
+
? '(none)'
|
|
391
|
+
: (schema.evidence ?? []).map((evidence) =>
|
|
392
|
+
evidence.id + (evidence.required === false ? ' (optional)' : ' (required)')).join(' | ');
|
|
393
|
+
expectExplainField(problems, node.id, (parsed.evidence[index] ?? []).join(' | '),
|
|
394
|
+
declaredEvidence, schemaId + ' 的 EVIDENCE 行');
|
|
395
|
+
});
|
|
396
|
+
return problems;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ③ 每条 guardrail 的 id·label·validation 与 ④ 每条 requiredSkillCall 的 skill·scope·enforcement·reason
|
|
400
|
+
// (空集合 → '(none)' 行的在场性同判)。
|
|
401
|
+
function explainGuardrailAndSkillProblems(node, parsed) {
|
|
402
|
+
const problems = [];
|
|
403
|
+
const declaredGuardrails = (node.guardrails ?? []).map((guardrail) => ({
|
|
404
|
+
id: guardrail.id ?? '(unnamed)', label: guardrail.label ?? '-', validation: guardrail.validation ?? '-',
|
|
405
|
+
}));
|
|
406
|
+
if ((declaredGuardrails.length === 0) !== parsed.guardrailsNone) {
|
|
407
|
+
problems.push('节点 ' + node.id + ' 的 guardrail 空集合形态与声明不一致((none) 行在场性不符)');
|
|
408
|
+
}
|
|
409
|
+
expectExplainField(problems, node.id, JSON.stringify(parsed.guardrails), JSON.stringify(declaredGuardrails), 'guardrail');
|
|
410
|
+
const declaredSkills = (node.requiredSkillCalls ?? []).map((call) => ({
|
|
411
|
+
skill: call.skill ?? '(unnamed)', scope: call.scope ?? '-',
|
|
412
|
+
enforcement: call.enforcement ?? '-', reason: call.reason ?? '-',
|
|
413
|
+
}));
|
|
414
|
+
if ((declaredSkills.length === 0) !== parsed.skillsNone) {
|
|
415
|
+
problems.push('节点 ' + node.id + ' 的 requiredSkillCall 空集合形态与声明不一致((none) 行在场性不符)');
|
|
416
|
+
}
|
|
417
|
+
expectExplainField(problems, node.id, JSON.stringify(parsed.skills), JSON.stringify(declaredSkills), 'requiredSkillCall');
|
|
418
|
+
return problems;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// ⑤ 模板指针行:内置节点取引擎 C3 映射、自定义协议节点取 node.templates,零模板节点不打印。
|
|
422
|
+
// 期望值在这里**独立构造**(不调 explain 侧的格式化函数):措辞、路径根或 .md 后缀的漂移
|
|
423
|
+
// 才能被这条锥抓到。
|
|
424
|
+
function explainTemplateProblems(node, parsed) {
|
|
425
|
+
const builtin = NODE_ARTIFACT_TEMPLATES[node.id];
|
|
426
|
+
const required = builtin ? builtin.required : (node.templates ?? []);
|
|
427
|
+
const optional = builtin ? (builtin.optional ?? []) : [];
|
|
428
|
+
const expected = [];
|
|
429
|
+
for (const name of required) expected.push('TEMPLATE: flow-kit/templates/' + name + '.md');
|
|
430
|
+
for (const name of optional) expected.push('TEMPLATE(条件模板): flow-kit/templates/' + name + '.md(仅前端 change)');
|
|
431
|
+
if (JSON.stringify(parsed.templates) === JSON.stringify(expected)) return [];
|
|
432
|
+
return ['节点 ' + node.id + ' 的 TEMPLATE 行与节点模板声明不一致: 输出 '
|
|
433
|
+
+ JSON.stringify(parsed.templates) + ' / 期望 ' + JSON.stringify(expected)];
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// ⑥ 三条命令:节点 id 与 --apply 形态 + 脚本根形态(三条同根、指向技能包 scripts 目录)+ 脚本
|
|
437
|
+
// 路径整体带双引号(含空格的安装根下不被 shell 分词——引号是契约形态的一部分,缺引号即红)。
|
|
438
|
+
function explainCommandProblems(node, parsed) {
|
|
439
|
+
const commandPatterns = [
|
|
440
|
+
[/^COMMAND entry: node "(.+?)\/workflow-guard\.mjs" entry (.+)$/, 'entry'],
|
|
441
|
+
[/^COMMAND exit: node "(.+?)\/workflow-guard\.mjs" exit (.+) --apply$/, 'exit'],
|
|
442
|
+
[/^COMMAND record: node "(.+?)\/workflow-state\.mjs" record (.+) '\{"summary":"<完成摘要>"\}'$/, 'record'],
|
|
443
|
+
];
|
|
444
|
+
if (parsed.commands.length !== commandPatterns.length) {
|
|
445
|
+
return ['节点 ' + node.id + ' 的 COMMAND 行数 ' + parsed.commands.length + ' ≠ 3'];
|
|
446
|
+
}
|
|
447
|
+
const problems = [];
|
|
448
|
+
const scriptsRoots = new Set();
|
|
449
|
+
parsed.commands.forEach((line, index) => {
|
|
450
|
+
const [pattern, kind] = commandPatterns[index];
|
|
451
|
+
const match = line.match(pattern);
|
|
452
|
+
if (!match) {
|
|
453
|
+
problems.push('节点 ' + node.id + ' 的 ' + kind + ' 命令形态不符: ' + line);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
scriptsRoots.add(match[1]);
|
|
457
|
+
if (match[2] !== node.id) problems.push('节点 ' + node.id + ' 的 ' + kind + ' 命令节点 id 不符: ' + match[2]);
|
|
458
|
+
});
|
|
459
|
+
if (scriptsRoots.size !== 1) {
|
|
460
|
+
problems.push('节点 ' + node.id + ' 的三条命令脚本根不一致: ' + [...scriptsRoots].join(' / '));
|
|
461
|
+
}
|
|
462
|
+
const scriptsRoot = [...scriptsRoots][0] ?? '';
|
|
463
|
+
if (!/skills\/flow-comet\/scripts$/.test(scriptsRoot)) {
|
|
464
|
+
problems.push('节点 ' + node.id + ' 的命令脚本根不指向技能包 scripts 目录: ' + scriptsRoot);
|
|
465
|
+
} else if (SCRIPTS_DIR.replace(/\\/g, '/').includes('/.flow-comet/skills/')
|
|
466
|
+
&& scriptsRoot !== '.claude/skills/flow-comet/scripts') {
|
|
467
|
+
// 权威源检出:命令路径 = 设计形态(安装副本按运行根形态化,已由上面同一判据约束)
|
|
468
|
+
problems.push('权威源检出的命令脚本根应为设计形态 .claude/skills/flow-comet/scripts: ' + scriptsRoot);
|
|
469
|
+
}
|
|
470
|
+
return problems;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// 逐字段对账(单一实现:真实判据与反向构造探针共用):五类字段 + 三条命令。
|
|
474
|
+
export function explainContractProblems(protocol, explainOutputs) {
|
|
475
|
+
const problems = [];
|
|
476
|
+
const schemaById = new Map((protocol.outputSchemas ?? []).map((schema) => [schema.id, schema]));
|
|
477
|
+
for (const node of protocol.nodes ?? []) {
|
|
478
|
+
const output = explainOutputs.get(node.id);
|
|
479
|
+
if (typeof output !== 'string') {
|
|
480
|
+
problems.push('节点 ' + node.id + ' 缺 explain 输出(未对账)');
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
let parsed;
|
|
484
|
+
try {
|
|
485
|
+
parsed = parseExplainOutput(output);
|
|
486
|
+
} catch (error) {
|
|
487
|
+
problems.push('节点 ' + node.id + ' 的 explain 输出形态不可解析: ' + error.message);
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
expectExplainField(problems, node.id, parsed.attributes.node, node.id, 'NODE');
|
|
491
|
+
expectExplainField(problems, node.id, parsed.attributes.label ?? null, node.label ?? null, 'LABEL');
|
|
492
|
+
expectExplainField(problems, node.id, parsed.attributes.kind ?? null, node.kind ?? null, 'KIND');
|
|
493
|
+
expectExplainField(problems, node.id, parsed.attributes.disabled ?? null,
|
|
494
|
+
node.disabled === true ? 'true(该节点在当前协议中被停用)' : null, 'DISABLED');
|
|
495
|
+
problems.push(...explainSchemaProblems(node, parsed, schemaById));
|
|
496
|
+
problems.push(...explainGuardrailAndSkillProblems(node, parsed));
|
|
497
|
+
problems.push(...explainTemplateProblems(node, parsed));
|
|
498
|
+
problems.push(...explainCommandProblems(node, parsed));
|
|
499
|
+
}
|
|
500
|
+
return problems;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// explain 的三种 fail-closed 形态(非法节点 / 缺参 / 拿选项当节点名):都非零退出并列出全部合法节点。
|
|
504
|
+
export function assertExplainFailClosed(dir, protocolDecl) {
|
|
505
|
+
const legalNodes = protocolDecl.nodes.map((protocolNode) => protocolNode.id).join('/');
|
|
506
|
+
const forms = [
|
|
507
|
+
['非法节点', ['explain', 'not-a-node'], 'explain node 非法: not-a-node'],
|
|
508
|
+
['缺参', ['explain'], 'explain requires a <node> id'],
|
|
509
|
+
['选项当节点名', ['explain', '--help'], 'explain requires a <node> id'],
|
|
510
|
+
];
|
|
511
|
+
for (const [form, args, expected] of forms) {
|
|
512
|
+
const res = runState(args, dir);
|
|
513
|
+
assertExit(res, 1);
|
|
514
|
+
assertOut(res, expected);
|
|
515
|
+
assertOut(res, legalNodes);
|
|
516
|
+
if (form === '非法节点') assertOut(res, '(协议节点: ' + legalNodes + ')');
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// 夹具协议(声明副本)下的 explain 输出——真实协议 env 通道;夹具文件跑完即清。
|
|
521
|
+
function explainOutputsForDeclaration(dir, decl) {
|
|
522
|
+
const probeProtocolFile = path.join(dir, 'reference', 'workflow-protocol-probe.json');
|
|
523
|
+
fs.writeFileSync(probeProtocolFile, JSON.stringify(decl, null, 2), 'utf8');
|
|
524
|
+
const outputs = new Map();
|
|
525
|
+
for (const protocolNode of decl.nodes) {
|
|
526
|
+
const res = runState(['explain', protocolNode.id], dir, { FLOW_COMET_PROTOCOL: probeProtocolFile });
|
|
527
|
+
assertExit(res, 0);
|
|
528
|
+
outputs.set(protocolNode.id, res.output);
|
|
529
|
+
}
|
|
530
|
+
fs.rmSync(probeProtocolFile, { force: true });
|
|
531
|
+
return outputs;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// 反向构造:把协议声明副本的一条 guardrail / 一个 artifact 抽掉、改一条 schema 描述 ⇒ 对账
|
|
535
|
+
// 必报对应字段;再把真实输出里的脚本路径引号去掉 ⇒ 必报命令形态不符(证明断言不恒真空过——
|
|
536
|
+
// 没有这一侧,把引号从实现里拿掉也能全绿)。返回问题描述数组(空 = 判别力齐备)。
|
|
537
|
+
export function explainReverseConstructionProblems(dir, protocolDecl, explainOutputs) {
|
|
538
|
+
const probeNode = protocolDecl.nodes.find((protocolNode) => (protocolNode.guardrails ?? []).length > 0);
|
|
539
|
+
const probeSchema = (protocolDecl.outputSchemas ?? []).find((schema) => (schema.artifacts ?? []).length > 0);
|
|
540
|
+
const owner = probeSchema === undefined ? undefined
|
|
541
|
+
: protocolDecl.nodes.find((protocolNode) => (protocolNode.outputSchemas ?? []).includes(probeSchema.id));
|
|
542
|
+
if (probeNode === undefined || probeSchema === undefined || owner === undefined) {
|
|
543
|
+
return ['反向构造前提不成立:协议声明里找不到带 guardrail 的节点 / 带 artifact 的 schema / schema 归属节点'];
|
|
544
|
+
}
|
|
545
|
+
const variants = [
|
|
546
|
+
['抽掉一条 guardrail', (decl) => {
|
|
547
|
+
decl.nodes.find((protocolNode) => protocolNode.id === probeNode.id).guardrails = [];
|
|
548
|
+
}, probeNode.id, 'guardrail'],
|
|
549
|
+
['抽掉一个 artifact', (decl) => {
|
|
550
|
+
decl.outputSchemas.find((schema) => schema.id === probeSchema.id).artifacts = [];
|
|
551
|
+
}, owner.id, 'artifact'],
|
|
552
|
+
['改掉一条 schema 描述', (decl) => {
|
|
553
|
+
const target = decl.outputSchemas.find((schema) => schema.id === probeSchema.id);
|
|
554
|
+
target.description = (probeSchema.description ?? '') + '(反向构造探针)';
|
|
555
|
+
}, owner.id, 'OUTPUT-SCHEMA'],
|
|
556
|
+
];
|
|
557
|
+
const problems = [];
|
|
558
|
+
for (const [form, mutate, affectedNodeId, expectedField] of variants) {
|
|
559
|
+
const mutatedDecl = JSON.parse(JSON.stringify(protocolDecl));
|
|
560
|
+
mutate(mutatedDecl);
|
|
561
|
+
const probeOutputs = explainOutputsForDeclaration(dir, mutatedDecl);
|
|
562
|
+
if (probeOutputs.get(affectedNodeId) === explainOutputs.get(affectedNodeId)) {
|
|
563
|
+
problems.push('反向构造前提不成立(' + form + ':夹具协议未改变 explain 输出——协议 env 通道未生效)');
|
|
564
|
+
continue;
|
|
565
|
+
}
|
|
566
|
+
const probeProblems = explainContractProblems(protocolDecl, probeOutputs);
|
|
567
|
+
if (!probeProblems.some((problem) => problem.includes(expectedField))) {
|
|
568
|
+
problems.push('反向构造判别力缺失(' + form + ' 未被判分叉): ' + (probeProblems.join('; ') || '(零问题)'));
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
// ④ 引号形态(③ 的另一面,不需重跑):真实输出去掉脚本路径引号 ⇒ 逐节点必报命令形态不符。
|
|
572
|
+
const dequoted = new Map();
|
|
573
|
+
for (const [nodeId, output] of explainOutputs) {
|
|
574
|
+
const text = String(output);
|
|
575
|
+
const stripped = text.replace(/(node )"([^"\n]*\.mjs)"/g, '$1$2');
|
|
576
|
+
if (stripped === text) {
|
|
577
|
+
problems.push('反向构造前提不成立(节点 ' + nodeId + ' 的输出里没有带引号的脚本路径——引号判据无从判别)');
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
dequoted.set(nodeId, stripped);
|
|
581
|
+
}
|
|
582
|
+
if (dequoted.size > 0) {
|
|
583
|
+
const dequotedProblems = explainContractProblems(protocolDecl, new Map([...explainOutputs, ...dequoted]));
|
|
584
|
+
for (const nodeId of dequoted.keys()) {
|
|
585
|
+
if (!dequotedProblems.some((problem) => problem.includes('节点 ' + nodeId + ' 的')
|
|
586
|
+
&& problem.includes('命令形态不符'))) {
|
|
587
|
+
problems.push('反向构造判别力缺失(去掉节点 ' + nodeId + ' 的脚本路径引号未被判命令形态不符)');
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
// ⑤ 模板指针行的另一面(不需重跑):真实输出去掉首条 TEMPLATE 行 ⇒ 逐节点必报 TEMPLATE 不一致。
|
|
592
|
+
// 没有这一侧,把模板行从实现里拿掉也能全绿(对账恒真空过)。
|
|
593
|
+
const strippedTemplates = new Map();
|
|
594
|
+
for (const [nodeId, output] of explainOutputs) {
|
|
595
|
+
const lines = String(output).split(/\r?\n/);
|
|
596
|
+
const index = lines.findIndex((line) => line.startsWith('TEMPLATE: '));
|
|
597
|
+
if (index < 0) continue;
|
|
598
|
+
strippedTemplates.set(nodeId, [...lines.slice(0, index), ...lines.slice(index + 1)].join('\n'));
|
|
599
|
+
}
|
|
600
|
+
if (strippedTemplates.size === 0) {
|
|
601
|
+
problems.push('反向构造前提不成立:没有任何节点的 explain 输出含 TEMPLATE: 行(模板指针判据无从判别)');
|
|
602
|
+
} else {
|
|
603
|
+
const templateProblems = explainContractProblems(protocolDecl, new Map([...explainOutputs, ...strippedTemplates]));
|
|
604
|
+
for (const nodeId of strippedTemplates.keys()) {
|
|
605
|
+
if (!templateProblems.some((problem) => problem.includes('节点 ' + nodeId + ' 的')
|
|
606
|
+
&& problem.includes('TEMPLATE 行'))) {
|
|
607
|
+
problems.push('反向构造判别力缺失(去掉节点 ' + nodeId + ' 的 TEMPLATE 行未被判不一致)');
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return problems;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// 混排多波 TASK:双并行开路 → 串行衔接(依赖双并行)→ 收尾并行(依赖串行衔接)。
|
|
615
|
+
// 各任务 status 由入参映射(任务 id → 'pending'|'done')控制;status 变更不影响任务集签名
|
|
616
|
+
// (签名仅保留 id/parallel 与块内容,剥离 status 类属性)。串行衔接置于两并行波之间——
|
|
617
|
+
// 引擎契约下 execute 出口要求全部串行已消化,跨趟多次交替由「并行波→串行波→并行波」承载。
|
|
618
|
+
export function multiWaveTaskContent(statusMap) {
|
|
619
|
+
const blk = (id, parallel, deps) =>
|
|
620
|
+
'<task id="' + id + '"' + (parallel ? ' parallel="true"' : '') + (statusMap[id] === 'done' ? ' status="done"' : ' status="pending"') + '>' +
|
|
621
|
+
'<action>实现 ' + id + '</action>' +
|
|
622
|
+
'<write_files>src/' + id.toLowerCase() + '.mjs</write_files>' +
|
|
623
|
+
'<verify>node --check src/' + id.toLowerCase() + '.mjs</verify>' +
|
|
624
|
+
(deps ? '<depends_on>' + deps + '</depends_on>' : '') +
|
|
625
|
+
'</task>\n';
|
|
626
|
+
return '# TASK\n\n' +
|
|
627
|
+
blk('P01', true, '') +
|
|
628
|
+
blk('P02', true, '') +
|
|
629
|
+
blk('S01', false, 'P01,P02') +
|
|
630
|
+
blk('P03', true, 'S01');
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// 真实链路前奏(多趟/依赖环用例共用):init → open 出口 → design 出口。
|
|
634
|
+
// 新 change 两层加载模型下逐节点 skill-load → record → entry → exit --apply 全程驱动。
|
|
635
|
+
export function driveThroughDesign(dir) {
|
|
636
|
+
assertExit(runState(['init', CHANGE_ID], dir), 0);
|
|
637
|
+
assertExit(runState(['skill-load', 'open', 'flow-comet-change', '--prompt', 'flow-kit/prompts/0-change.md', '--templates', 'CHANGE.md,REQUIREMENT.md'], dir), 0);
|
|
638
|
+
writeIntakeArtifacts(dir);
|
|
639
|
+
assertExit(runState(['record', 'open', '{"summary":"intake complete"}'], dir), 0);
|
|
640
|
+
assertExit(runGuard(['entry', 'open'], dir), 0);
|
|
641
|
+
assertExit(runGuard(['exit', 'open', '--apply'], dir), 0);
|
|
642
|
+
assertExit(runState(['skill-load', 'design', 'flow-comet-design', '--prompt', 'flow-kit/prompts/2-design.md', '--templates', 'DESIGN.md'], dir), 0);
|
|
643
|
+
writeFile(dir, '.specs/' + CHANGE_ID + '/DESIGN.md',
|
|
644
|
+
'# DESIGN\n\n- **Change ID**: ' + CHANGE_ID + '\n\n## 0. 技术栈选定\n\nNode.js(ESM)\n\n## 决策清单\n\n- [ ] 循环路由\n');
|
|
645
|
+
assertExit(runState(['record', 'design', '{"summary":"design done"}'], dir), 0);
|
|
646
|
+
assertExit(runGuard(['entry', 'design'], dir), 0);
|
|
647
|
+
assertExit(runGuard(['exit', 'design', '--apply'], dir), 0);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// ---------- 受控重入真实命令链路夹具(H5~H8 共用) ----------
|
|
651
|
+
|
|
652
|
+
// archive 已 entry 形态的完成集(built-in 协议:open → verify 全部完成;archive 已进入未出口)
|
|
653
|
+
|
|
654
|
+
// status 输出的 JSON 块解析(命令行尾巴行不进 JSON)
|
|
655
|
+
export function parseStatusOutput(root) {
|
|
656
|
+
const res = runState(['status'], root);
|
|
657
|
+
assertExit(res, 0);
|
|
658
|
+
const start = res.output.indexOf('{');
|
|
659
|
+
const end = res.output.lastIndexOf('}');
|
|
660
|
+
if (start < 0 || end <= start) throw new Error('status 输出缺少 JSON 块:\n' + res.output);
|
|
661
|
+
return JSON.parse(res.output.slice(start, end + 1));
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
export const CONTRACT = path.join(SCRIPTS_DIR, 'contract-check.mjs');
|
|
666
|
+
|
|
667
|
+
// 递归收集目录下相对文件清单(排序稳定,供 K8/K10 技能整树清单/内容比对)
|
|
668
|
+
export function collectTreeFiles(root) {
|
|
669
|
+
const files = [];
|
|
670
|
+
if (!fs.existsSync(root)) return files;
|
|
671
|
+
(function walk(dir) {
|
|
672
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
673
|
+
const absolute = path.join(dir, entry.name);
|
|
674
|
+
if (entry.isDirectory()) walk(absolute);
|
|
675
|
+
else if (entry.isFile()) files.push(path.relative(root, absolute));
|
|
676
|
+
}
|
|
677
|
+
})(root);
|
|
678
|
+
return files.sort();
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// ---------- 分发面包体断言助手(npm 包形态:环境判据 / 结果标记 / 可发布性清单 / 包体边界 / bin 双入口) ----------
|
|
682
|
+
|
|
683
|
+
// 环境判据(结构化事实,不做路径字符串猜测):本套件在权威源与安装副本内都会运行,
|
|
684
|
+
// 而包体质检只有权威源侧可判——判据落空时输出显式「不适用」行并计通过,禁止静默跳过。
|
|
685
|
+
// 权威源侧 = 脚本上溯 4 层(包根)**同时**满足:存在 `name === 'flow-comet'` 的 package.json
|
|
686
|
+
// **且** `.flow-comet/skills/flow-comet` 权威树在位;
|
|
687
|
+
// 安装副本侧 = <target>/.claude|.agents|.dsh/skills/flow-comet/scripts 上溯 4 层 = 目标项目根
|
|
688
|
+
// → 判「不适用」(判定写反会让每个已安装项目假红)。
|
|
689
|
+
// 两个条件是**并列必要**的:只看 manifest 名字会把「恰好同名的消费项目」(依赖里带同名包、
|
|
690
|
+
// 或项目自身就叫 flow-comet 但无权威树)判成权威源——于是包体质检在该项目里跑 npm pack 而不是
|
|
691
|
+
// 输出「不适用」(判据过宽的另一失败模式)。权威树在场时 manifest 缺失/不可解析/name 不符
|
|
692
|
+
// → 必须失败(判定过宽会让权威源侧的包体断言静默漏检)。
|
|
693
|
+
// root 可覆盖(默认真实包根)——仅为可测性接缝:使上述四种形态能在夹具目录上被逐条驱动
|
|
694
|
+
// (否则判据的判别力只能靠人工实验验证,回归时可能被改宽而套件仍全绿)。生产调用不传该参数。
|
|
695
|
+
export function resolvePackageContext(root = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..')) {
|
|
696
|
+
const authoritative = fs.existsSync(path.join(root, '.flow-comet', 'skills', 'flow-comet'));
|
|
697
|
+
const notApplicable = (reason) => ({ kind: 'copy', reason: reason || '根目录无本包权威源(name=flow-comet 的 package.json + .flow-comet/skills/flow-comet 权威树)' });
|
|
698
|
+
const manifest = path.join(root, 'package.json');
|
|
699
|
+
if (!fs.existsSync(manifest)) {
|
|
700
|
+
if (authoritative) throw new Error('权威源包根缺少 package.json: ' + manifest);
|
|
701
|
+
return notApplicable();
|
|
702
|
+
}
|
|
703
|
+
let pkg = null;
|
|
704
|
+
try {
|
|
705
|
+
pkg = JSON.parse(fs.readFileSync(manifest, 'utf8'));
|
|
706
|
+
} catch (e) {
|
|
707
|
+
if (authoritative) throw new Error('权威源 package.json 不可解析: ' + e.message);
|
|
708
|
+
return notApplicable();
|
|
709
|
+
}
|
|
710
|
+
if (!pkg || pkg.name !== 'flow-comet') {
|
|
711
|
+
if (authoritative) throw new Error('权威源 package.json 的 name 应为 flow-comet,实际 ' + JSON.stringify(pkg && pkg.name));
|
|
712
|
+
return notApplicable();
|
|
713
|
+
}
|
|
714
|
+
if (!authoritative) {
|
|
715
|
+
return notApplicable('根目录有 name=flow-comet 的 package.json 但权威树(.flow-comet/skills/flow-comet)不在位——同名不构成权威源,按安装副本判定');
|
|
716
|
+
}
|
|
717
|
+
return { kind: 'source', root, pkg };
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
// 环境判据的判别力锚(正反两向逐形态驱动):判据写宽(manifest 名字相符即认权威源)会让同名
|
|
721
|
+
// 消费项目被当成权威源;写反/写严则两侧互串(副本侧假红 / 权威源侧静默漏检)。夹具形态:
|
|
722
|
+
// ① 权威树不在 + 同名 manifest → copy(同名 ≠ 权威源)
|
|
723
|
+
// ② 权威树不在 + 无 manifest → copy
|
|
724
|
+
// ③ 权威树在场 + 同名 manifest → source
|
|
725
|
+
// ④ 权威树在场 + 无 manifest / manifest 不可解析 / name 不符 → 抛错(既有行为保持,不得放宽)
|
|
726
|
+
// 夹具建在调用方给定的临时目录内(随该项临时目录一并清理,不留残留)。
|
|
727
|
+
export function assertPackageContextJudgment(scratch) {
|
|
728
|
+
const fixture = (name, options) => {
|
|
729
|
+
const root = path.join(scratch, name);
|
|
730
|
+
const tree = path.join(root, '.flow-comet', 'skills', 'flow-comet');
|
|
731
|
+
if (options.tree) fs.mkdirSync(tree, { recursive: true });
|
|
732
|
+
else fs.mkdirSync(root, { recursive: true });
|
|
733
|
+
if (options.manifest !== undefined) fs.writeFileSync(path.join(root, 'package.json'), options.manifest, 'utf8');
|
|
734
|
+
return root;
|
|
735
|
+
};
|
|
736
|
+
const expectKind = (root, expected, label) => {
|
|
737
|
+
const ctx = resolvePackageContext(root);
|
|
738
|
+
if (ctx.kind !== expected) {
|
|
739
|
+
throw new Error('环境判据失配(' + label + '):期望 ' + expected + ',实际 ' + ctx.kind);
|
|
740
|
+
}
|
|
741
|
+
};
|
|
742
|
+
const expectThrow = (root, label) => {
|
|
743
|
+
let threw = null;
|
|
744
|
+
try { resolvePackageContext(root); } catch (e) { threw = e; }
|
|
745
|
+
if (!threw) {
|
|
746
|
+
throw new Error('环境判据失配(' + label + '):权威树在场而 manifest 畸形时应显式失败,实际未抛错');
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
expectKind(fixture('same-name-consumer', { tree: false, manifest: '{"name":"flow-comet"}' }), 'copy', '权威树不在 + 同名 manifest');
|
|
750
|
+
expectKind(fixture('plain-consumer', { tree: false, manifest: undefined }), 'copy', '权威树不在 + 无 manifest');
|
|
751
|
+
expectKind(fixture('authoritative', { tree: true, manifest: '{"name":"flow-comet"}' }), 'source', '权威树在场 + 同名 manifest');
|
|
752
|
+
expectThrow(fixture('authoritative-no-manifest', { tree: true, manifest: undefined }), '权威树在场 + 无 manifest');
|
|
753
|
+
expectThrow(fixture('authoritative-broken', { tree: true, manifest: '{ not json' }), '权威树在场 + manifest 不可解析');
|
|
754
|
+
expectThrow(fixture('authoritative-other-name', { tree: true, manifest: '{"name":"other"}' }), '权威树在场 + name 不符');
|
|
755
|
+
console.log(' 环境判据: 副本侧同名/无名两形态判「不适用」,权威源侧判「权威源」,畸形 manifest 三形态显式失败 ✓');
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// 分发面结果标记(机器可读):本套件在权威源与安装副本两侧都会运行,而断言只在权威源侧真跑——
|
|
759
|
+
// 两侧的区别(「已执行」/「不适用」/「未执行」)必须留在运行器可捕获的结果里,不能只打 console 行
|
|
760
|
+
// 靠人工核验。前两行即两侧的正反锚点:权威源侧回传 executed、安装副本侧回传 not-applicable,
|
|
761
|
+
// 写反则该侧判失败。另两态是「真跑了但没验成」的两类原因,只在权威源侧出现:
|
|
762
|
+
// platform 平台条件不满足(条目内某条子断言在本环境无法构造前置);
|
|
763
|
+
// toolMissing 本地工具缺失(仓库本地检查器不随克隆分发,整项无从执行)。
|
|
764
|
+
export const DISTRIBUTION_MARKERS = {
|
|
765
|
+
source: 'distribution:executed@source',
|
|
766
|
+
copy: 'distribution:not-applicable@copy',
|
|
767
|
+
platform: 'distribution:not-applicable@platform',
|
|
768
|
+
toolMissing: 'distribution:not-executed@tool-missing',
|
|
769
|
+
};
|
|
770
|
+
|
|
771
|
+
// 三态读数(运行器收尾行用):条目级标记 → 状态,**不适用 / 未执行不得并入「通过」**。
|
|
772
|
+
// platform 不在表内——它是条目内**子断言**的态(条目自身的面已真跑),不是条目的态;
|
|
773
|
+
// 它与条目级三态分区互不混淆(子断言由运行器逐条打印,见 createOutcomeRecorder)。
|
|
774
|
+
const DISTRIBUTION_ITEM_STATES = {
|
|
775
|
+
[DISTRIBUTION_MARKERS.source]: 'passed',
|
|
776
|
+
[DISTRIBUTION_MARKERS.copy]: 'not-applicable',
|
|
777
|
+
[DISTRIBUTION_MARKERS.toolMissing]: 'not-executed',
|
|
778
|
+
};
|
|
779
|
+
export function distributionState(marker) {
|
|
780
|
+
return DISTRIBUTION_ITEM_STATES[marker] || null;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// K14 安装副本侧「不适用」说明:K14 分支与契约夹具共用同一字面量来源(防两处文案漂移)。
|
|
784
|
+
export const K14_COPY_DETAIL = '桥接 loader 版本戳重装断言仅权威源侧可判';
|
|
785
|
+
|
|
786
|
+
// 运行器侧标记契约校验(分发面项):标记必须显式在场且与结构化环境判据推出的期望值一致。
|
|
787
|
+
// 缺标记 = 该项走了静默跳过(「未验证 ≠ 通过」,与显式「不适用」是两回事)→ 失败;
|
|
788
|
+
// 标记与环境不符 = 环境判据写反(副本侧假红 / 权威源侧静默漏检)→ 失败。
|
|
789
|
+
// 两个原因态(platform / toolMissing)只在权威源侧真跑时产生——副本侧在早期分支已回传
|
|
790
|
+
// not-applicable@copy,故它们出现在副本环境即判定写错(把原因态当成了副本态)。
|
|
791
|
+
// ctx 是可注入接缝(默认按真实环境判据解析):使两侧错配与非法 marker 的失败路径能由夹具
|
|
792
|
+
// 合成 ctx 真实驱动(否则这些分支没有调用者,判定被改宽也没有断言能拦住)。
|
|
793
|
+
export function assertDistributionOutcome(itemName, marker, ctx = resolvePackageContext()) {
|
|
794
|
+
const legal = Object.values(DISTRIBUTION_MARKERS);
|
|
795
|
+
if (!legal.includes(marker)) {
|
|
796
|
+
throw new Error('分发面结果标记缺席或非法(应回传 ' + legal.join(' 或 ') + ',实际 ' + JSON.stringify(marker)
|
|
797
|
+
+ ')——禁止静默跳过');
|
|
798
|
+
}
|
|
799
|
+
const expected = DISTRIBUTION_MARKERS[ctx.kind];
|
|
800
|
+
const reasonState = marker === DISTRIBUTION_MARKERS.platform || marker === DISTRIBUTION_MARKERS.toolMissing;
|
|
801
|
+
// 原因态(platform / toolMissing)只允许在权威源侧出现——副本侧在早期分支就回传了
|
|
802
|
+
// not-applicable@copy,此处出现原因态即判定写错(原因态被当成了副本态的替代)。
|
|
803
|
+
if (marker !== expected && !(reasonState && ctx.kind === 'source')) {
|
|
804
|
+
throw new Error('分发面结果标记与环境判据不符:本环境应 ' + expected + ',实际 ' + marker
|
|
805
|
+
+ '(判定写反会让该侧静默漏检或假红)');
|
|
806
|
+
}
|
|
807
|
+
console.log('OUTCOME: ' + itemName + ' → ' + marker);
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// 平台条件子断言的结果登记(「不适用(平台原因)」面):子断言不是条目本身——它的不适用不改写条目
|
|
811
|
+
// 自己的读数,而是**自己出声**(相邻的显式行 + 运行器收尾的机器可读 OUTCOME 行),使「未验证」
|
|
812
|
+
// 在日志与证据里可逐条核对,不再无痕计入通过。记录器由运行器按项创建、作为 run(dir, outcomes) 的
|
|
813
|
+
// 第二参数注入(无跨文件可变全局状态,也不会串项)。
|
|
814
|
+
export function createOutcomeRecorder() {
|
|
815
|
+
const recorded = [];
|
|
816
|
+
return {
|
|
817
|
+
notApplicablePlatform(assertion, reason) {
|
|
818
|
+
if (typeof assertion !== 'string' || assertion.trim() === ''
|
|
819
|
+
|| typeof reason !== 'string' || reason.trim() === '') {
|
|
820
|
+
throw new Error('平台不适用回传契约要求非空 assertion 与 reason,实际 '
|
|
821
|
+
+ JSON.stringify({ assertion, reason }));
|
|
822
|
+
}
|
|
823
|
+
recorded.push({ assertion, reason, marker: DISTRIBUTION_MARKERS.platform });
|
|
824
|
+
console.log(' 不适用(平台原因:' + reason + ')——' + assertion + '(该子断言本环境无法验证;未验证 ≠ 通过)');
|
|
825
|
+
},
|
|
826
|
+
drain() { return recorded.splice(0, recorded.length); },
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// 分发面条目的统一接入(安装器/平台链路断言):权威源侧真跑,安装副本侧回传显式
|
|
831
|
+
// 「不适用(安装副本侧)」标记——两侧都出声,杜绝「裸 return 静默跳过仍计通过」。
|
|
832
|
+
// 条目在自己的定义处用 `surface: '<说明>'` 声明「我属分发面、不适用的是哪个面」,导出前经本函数
|
|
833
|
+
// 接入:接入后条目上的是既有契约字段 `distributionSurface: true`(运行器按该字段断言标记)。
|
|
834
|
+
// copy 判定与「已执行」回传只写一份(单源不复制),声明就在条目旁边(不另立名单——名单会与
|
|
835
|
+
// 条目漂移且缺失时无人发现)。
|
|
836
|
+
export function applyDistributionSurface(entry) {
|
|
837
|
+
if (!entry || entry.surface === undefined) return entry;
|
|
838
|
+
const { surface, ...rest } = entry;
|
|
839
|
+
if (typeof surface !== 'string' || surface.trim() === '') {
|
|
840
|
+
throw new Error('分发面声明 surface 必须是非空说明(条目 ' + entry.name + '),实际 ' + JSON.stringify(surface));
|
|
841
|
+
}
|
|
842
|
+
if (typeof rest.run !== 'function' || typeof rest.name !== 'string') {
|
|
843
|
+
throw new Error('分发面条目接入契约要求 name 与 run,实际 ' + JSON.stringify(Object.keys(rest)));
|
|
844
|
+
}
|
|
845
|
+
return {
|
|
846
|
+
name: rest.name,
|
|
847
|
+
distributionSurface: true,
|
|
848
|
+
// async:条目自身可能是 async(K11/K12)——不 await 会把 Promise 当结果回传(marker 丢失)
|
|
849
|
+
run: async (dir, outcomes) => {
|
|
850
|
+
const ctx = resolvePackageContext();
|
|
851
|
+
if (ctx.kind === 'copy') {
|
|
852
|
+
const outcome = copyDistributionOutcome(ctx, surface);
|
|
853
|
+
console.log(outcome.message);
|
|
854
|
+
return outcome.marker;
|
|
855
|
+
}
|
|
856
|
+
const result = await rest.run(dir, outcomes);
|
|
857
|
+
// 权威源侧:条目跑完即回传「已执行」——除非条目自己回传了更具体的态(原因态)
|
|
858
|
+
return result === undefined ? DISTRIBUTION_MARKERS.source : result;
|
|
859
|
+
},
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// copy 形态「不适用」回传:文案与机器可读结果标记同源产出(文案是「非静默跳过」的证据)。
|
|
864
|
+
// 返回 { message, marker } 供 early-return 分支直接消费,也使夹具无需捕获 stdout 即可逐字驱动断言。
|
|
865
|
+
// 非 copy ctx 必须 fail-closed——不允许把 source 形态的 marker 与「不适用」文案拼在一起。
|
|
866
|
+
export function copyDistributionOutcome(ctx, detail) {
|
|
867
|
+
if (!ctx || ctx.kind !== 'copy' || typeof ctx.reason !== 'string' || ctx.reason.trim() === '') {
|
|
868
|
+
throw new Error('copy 回传契约要求 kind=copy 且含非空 reason,实际 ' + JSON.stringify(ctx));
|
|
869
|
+
}
|
|
870
|
+
return {
|
|
871
|
+
message: ' 不适用(安装副本侧:' + ctx.reason + ')——' + detail + ',本项计通过',
|
|
872
|
+
marker: DISTRIBUTION_MARKERS[ctx.kind],
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
// 分发面回传契约夹具(环境无关;K14 在副本 early-return 之前调用,故两侧都会真跑):
|
|
877
|
+
// ① 失败路径负例必须 throw——copy ctx + source marker / source ctx + copy marker /
|
|
878
|
+
// undefined 或非法 marker / 副本环境回传两个「原因态」(缺可注入 ctx 接缝时这些路径没有调用者);
|
|
879
|
+
// ② 接缝正例:两侧 marker 与 ctx 匹配时必须通过(含两个原因态);
|
|
880
|
+
// ③ copy 形态真实判据 + helper 文案/marker 同源产出;
|
|
881
|
+
// ④ 子断言记录器契约:登记 → 摘要行 → 取空;空 assertion/reason 必须 fail-closed。
|
|
882
|
+
export function assertDistributionOutcomeContract(scratch) {
|
|
883
|
+
const expectThrow = (fn, label) => {
|
|
884
|
+
let threw = null;
|
|
885
|
+
try { fn(); } catch (e) { threw = e; }
|
|
886
|
+
if (!threw) throw new Error('分发面回传契约失配(' + label + '):失败路径应抛错而未抛错');
|
|
887
|
+
};
|
|
888
|
+
// ① 失败路径负例先驱动:接缝缺失时先在此以「应抛错而未抛错」暴露,不被后续环境相关失败掩盖
|
|
889
|
+
expectThrow(() => assertDistributionOutcome('分发面契约夹具:copy ctx + source marker', DISTRIBUTION_MARKERS.source, { kind: 'copy', reason: '夹具' }), 'copy ctx + source marker');
|
|
890
|
+
expectThrow(() => assertDistributionOutcome('分发面契约夹具:source ctx + copy marker', DISTRIBUTION_MARKERS.copy, { kind: 'source' }), 'source ctx + copy marker');
|
|
891
|
+
expectThrow(() => assertDistributionOutcome('分发面契约夹具:undefined marker', undefined, { kind: 'copy', reason: '夹具' }), 'undefined marker');
|
|
892
|
+
expectThrow(() => assertDistributionOutcome('分发面契约夹具:非法 marker', 'distribution:unknown', { kind: 'copy', reason: '夹具' }), '非法 marker');
|
|
893
|
+
expectThrow(() => assertDistributionOutcome('分发面契约夹具:copy ctx + platform marker', DISTRIBUTION_MARKERS.platform, { kind: 'copy', reason: '夹具' }), 'copy ctx + platform marker');
|
|
894
|
+
expectThrow(() => assertDistributionOutcome('分发面契约夹具:copy ctx + tool-missing marker', DISTRIBUTION_MARKERS.toolMissing, { kind: 'copy', reason: '夹具' }), 'copy ctx + tool-missing marker');
|
|
895
|
+
// ② 接缝正例
|
|
896
|
+
assertDistributionOutcome('分发面契约夹具:copy 正例', DISTRIBUTION_MARKERS.copy, { kind: 'copy', reason: '夹具' });
|
|
897
|
+
assertDistributionOutcome('分发面契约夹具:source 正例', DISTRIBUTION_MARKERS.source, { kind: 'source' });
|
|
898
|
+
assertDistributionOutcome('分发面契约夹具:platform 正例', DISTRIBUTION_MARKERS.platform, { kind: 'source' });
|
|
899
|
+
assertDistributionOutcome('分发面契约夹具:tool-missing 正例', DISTRIBUTION_MARKERS.toolMissing, { kind: 'source' });
|
|
900
|
+
// ③ copy 形态真实判据 + helper 文案/marker 同源
|
|
901
|
+
const copyCtx = resolvePackageContext(path.join(scratch, 'k14-copy-probe'));
|
|
902
|
+
if (copyCtx.kind !== 'copy' || typeof copyCtx.reason !== 'string' || copyCtx.reason.trim() === '') {
|
|
903
|
+
throw new Error('分发面回传契约夹具 copy 判据失配: ' + JSON.stringify(copyCtx));
|
|
904
|
+
}
|
|
905
|
+
const copyOutcome = copyDistributionOutcome(copyCtx, K14_COPY_DETAIL);
|
|
906
|
+
if (copyOutcome.marker !== DISTRIBUTION_MARKERS.copy) {
|
|
907
|
+
throw new Error('分发面回传契约 copy marker 失配: ' + JSON.stringify(copyOutcome.marker));
|
|
908
|
+
}
|
|
909
|
+
if (!copyOutcome.message.includes('不适用(安装副本侧:' + copyCtx.reason + ')')
|
|
910
|
+
|| !copyOutcome.message.includes(K14_COPY_DETAIL)
|
|
911
|
+
|| !copyOutcome.message.includes('本项计通过')) {
|
|
912
|
+
throw new Error('分发面回传契约 copy 文案失配: ' + JSON.stringify(copyOutcome.message));
|
|
913
|
+
}
|
|
914
|
+
// ④ helper 非 copy ctx 必须 fail-closed(不得把 source marker 伪装成「不适用」)
|
|
915
|
+
expectThrow(() => copyDistributionOutcome({ kind: 'source' }, 'x'), 'copy helper 非 copy ctx');
|
|
916
|
+
// ⑤ 子断言记录器契约:登记即出声(显式行)+ 摘要可逐条取回 + 取空后不再重复;畸形入参 fail-closed。
|
|
917
|
+
const recorder = createOutcomeRecorder();
|
|
918
|
+
if (recorder.drain().length !== 0) throw new Error('子断言记录器初始应为空');
|
|
919
|
+
expectThrow(() => recorder.notApplicablePlatform('', 'x'), '记录器空 assertion');
|
|
920
|
+
expectThrow(() => recorder.notApplicablePlatform('x', ' '), '记录器空 reason');
|
|
921
|
+
recorder.notApplicablePlatform('分发面契约夹具:平台子断言', '夹具平台原因');
|
|
922
|
+
const drained = recorder.drain();
|
|
923
|
+
if (drained.length !== 1 || drained[0].marker !== DISTRIBUTION_MARKERS.platform
|
|
924
|
+
|| drained[0].assertion !== '分发面契约夹具:平台子断言' || drained[0].reason !== '夹具平台原因') {
|
|
925
|
+
throw new Error('子断言记录器回传失配: ' + JSON.stringify(drained));
|
|
926
|
+
}
|
|
927
|
+
if (recorder.drain().length !== 0) throw new Error('子断言记录器取空后不得重复产出(跨项串味)');
|
|
928
|
+
// ⑥ 三态读数映射:三个条目级标记各有归属,「未执行 / 不适用」不得落回 passed
|
|
929
|
+
const stateOf = [
|
|
930
|
+
[DISTRIBUTION_MARKERS.source, 'passed'],
|
|
931
|
+
[DISTRIBUTION_MARKERS.copy, 'not-applicable'],
|
|
932
|
+
[DISTRIBUTION_MARKERS.toolMissing, 'not-executed'],
|
|
933
|
+
[DISTRIBUTION_MARKERS.platform, null],
|
|
934
|
+
[undefined, null],
|
|
935
|
+
];
|
|
936
|
+
for (const [marker, expected] of stateOf) {
|
|
937
|
+
if (distributionState(marker) !== expected) {
|
|
938
|
+
throw new Error('三态读数映射失配: ' + JSON.stringify(marker) + ' 应 ' + JSON.stringify(expected)
|
|
939
|
+
+ ',实际 ' + JSON.stringify(distributionState(marker)));
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
console.log(' 分发面回传契约: copy 文案/marker + 两侧与两原因态正例 + 四类失败路径负例 + 子断言记录器/三态映射 ✓');
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// 跑 `npm pack --dry-run --json`(只读,不产出 tarball)并解析出产物相对路径清单。
|
|
946
|
+
// 命令以字符串形态经 shell 执行(与 workflow-guard verify 命令执行同形态):Windows 上 npm 是
|
|
947
|
+
// npm.cmd(CreateProcess 不解析 .cmd 后缀,且 Node 对 .cmd/.bat 直接 spawn 属 EINVAL)——
|
|
948
|
+
// 字符串 + shell 是全平台唯一稳妥形态。解析失败即报错(不做文本兜底:静默回退会掩盖 npm 形态漂移)。
|
|
949
|
+
function runNpmPackDryRun(root) {
|
|
950
|
+
const res = spawnSync('npm pack --dry-run --json', {
|
|
951
|
+
cwd: root,
|
|
952
|
+
encoding: 'utf8',
|
|
953
|
+
timeout: 120000,
|
|
954
|
+
shell: true,
|
|
955
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
956
|
+
});
|
|
957
|
+
const status = res.status ?? 1;
|
|
958
|
+
const stdout = String(res.stdout || '');
|
|
959
|
+
const stderr = String(res.stderr || '');
|
|
960
|
+
if (status !== 0) {
|
|
961
|
+
throw new Error('npm pack --dry-run 退出码 ' + status + '(npm 缺失或环境异常)\nstdout:\n' + stdout + '\nstderr:\n' + stderr);
|
|
962
|
+
}
|
|
963
|
+
let parsed = null;
|
|
964
|
+
try {
|
|
965
|
+
parsed = JSON.parse(stdout);
|
|
966
|
+
} catch (e) {
|
|
967
|
+
throw new Error('npm pack --dry-run --json 输出不可解析: ' + e.message + '\nstdout(前 500 字):\n' + stdout.slice(0, 500) + '\nstderr:\n' + stderr);
|
|
968
|
+
}
|
|
969
|
+
const entry = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
970
|
+
const files = entry && Array.isArray(entry.files) ? entry.files.map((f) => String(f.path).replaceAll('\\', '/')) : null;
|
|
971
|
+
if (!files || files.length === 0) throw new Error('npm pack --dry-run 产物清单为空(--json 未列出任何文件)');
|
|
972
|
+
return files;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// ① 清单可发布性:`private` 在场即 npm 拒绝发布——被误加回会让包不可发布,而包体边界断言
|
|
976
|
+
// (按效果覆盖 files/version)与 CI 版本对账都察觉不到,故独立成锚。允许缺席或显式非真值。
|
|
977
|
+
// 同时守住公开身份与运行时前提(license / engines.node):这些元数据随版本固化在发布物上,
|
|
978
|
+
// 发布后才察觉漏声明只能等下一个版本补救,故与 private 同点做在场锚定。
|
|
979
|
+
export function assertPublishableManifest(pkg) {
|
|
980
|
+
if (pkg.private) {
|
|
981
|
+
throw new Error('package.json 的 private 应缺席(或显式非真值),实际 ' + JSON.stringify(pkg.private)
|
|
982
|
+
+ '——置真值会让 npm 拒绝发布');
|
|
983
|
+
}
|
|
984
|
+
if (pkg.license !== 'MIT') {
|
|
985
|
+
throw new Error('package.json 的 license 应为 MIT(与仓库 LICENSE 文件一致),实际 '
|
|
986
|
+
+ JSON.stringify(pkg.license) + '——包被 npm 打进发布物时该字段随版本固化');
|
|
987
|
+
}
|
|
988
|
+
const nodeFloor = pkg.engines ? pkg.engines.node : undefined;
|
|
989
|
+
if (nodeFloor !== '>=18') {
|
|
990
|
+
throw new Error('package.json 的 engines.node 应为 >=18(文档与 CI 声明的运行时前提),实际 '
|
|
991
|
+
+ JSON.stringify(nodeFloor) + '(engines 整段缺失时为 undefined)');
|
|
992
|
+
}
|
|
993
|
+
console.log(' 清单可发布性: private 缺席 / license=' + pkg.license + ' / engines.node=' + nodeFloor + ' ✓');
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
// 随包 scripts/ 面的唯一白名单(包体边界与本项反向构造的载体共用同一常量):bin 双入口两个 +
|
|
997
|
+
// `scripts/lib/` 下安装器的六个职责模块(入口静态 import 全部六个,缺一即启动失败)。
|
|
998
|
+
// `package.json` 的 `files` 以**目录级**纳入 `scripts/lib`(新增模块不登记即被判越界,fail-closed),
|
|
999
|
+
// 故「实际进包集合」由本白名单判等:多一件 = 未纳管进包,少一件 = 发布后 ERR_MODULE_NOT_FOUND。
|
|
1000
|
+
// CI 的 installer job 是同一判据的**独立实现**(两侧没有任何机制能发现彼此失同步,改动任一侧必须同步)。
|
|
1001
|
+
export const PACKED_SCRIPTS = [
|
|
1002
|
+
'scripts/prepare-env.mjs',
|
|
1003
|
+
'scripts/dsh-bridge.mjs',
|
|
1004
|
+
'scripts/lib/installer-platforms.mjs',
|
|
1005
|
+
'scripts/lib/installer-migrate.mjs',
|
|
1006
|
+
'scripts/lib/installer-hooks.mjs',
|
|
1007
|
+
'scripts/lib/installer-loader.mjs',
|
|
1008
|
+
'scripts/lib/installer-cleanup.mjs',
|
|
1009
|
+
'scripts/lib/installer-flowkit.mjs',
|
|
1010
|
+
];
|
|
1011
|
+
|
|
1012
|
+
// ② 包体边界:必含面在场 / 私有面零命中 / scripts 面精确等于白名单 / 顶层白名单 fail-closed /
|
|
1013
|
+
// 权威源技能树逐文件在场(漏收 = 安装期炸或平台静默退化——协议 JSON 也在内)。
|
|
1014
|
+
export function assertPackageBoundary(root) {
|
|
1015
|
+
const files = runNpmPackDryRun(root);
|
|
1016
|
+
const packedUnder = (prefix) => files.some((p) => p.startsWith(prefix + '/'));
|
|
1017
|
+
const required = [
|
|
1018
|
+
['.flow-comet/skills', () => packedUnder('.flow-comet/skills')],
|
|
1019
|
+
['.flow-comet/rules/flow-comet-orchestration.md', () => files.includes('.flow-comet/rules/flow-comet-orchestration.md')],
|
|
1020
|
+
...PACKED_SCRIPTS.map((p) => [p, () => files.includes(p)]),
|
|
1021
|
+
];
|
|
1022
|
+
for (const [label, ok] of required) {
|
|
1023
|
+
if (!ok()) throw new Error('包体缺少必需路径: ' + label);
|
|
1024
|
+
}
|
|
1025
|
+
// 私有面零命中(提交钩子与本地词表 / 仓库工作笔记 / 流程工件 / 仓库级 AI 上下文与 CI 资产 / 演示图)
|
|
1026
|
+
const forbidden = ['.githooks/', 'docs/internal/', '.specs/', 'CLAUDE.md', 'AGENTS.md', '.github/', 'images/'];
|
|
1027
|
+
for (const prefix of forbidden) {
|
|
1028
|
+
const hit = files.filter((p) => p === prefix.replace(/\/$/, '') || p.startsWith(prefix));
|
|
1029
|
+
if (hit.length > 0) throw new Error('包体含私有面路径 ' + prefix + ': ' + hit.slice(0, 5).join(', '));
|
|
1030
|
+
}
|
|
1031
|
+
// 随包 scripts/ 面精确等于白名单(本地工具脚本——提交钩子安装器/本地检查器一类——不得进包)
|
|
1032
|
+
const extraScripts = files.filter((p) => p.startsWith('scripts/') && !PACKED_SCRIPTS.includes(p));
|
|
1033
|
+
if (extraScripts.length > 0) {
|
|
1034
|
+
throw new Error('包体含未纳管的 scripts/ 条目(本地工具脚本不得进包): ' + extraScripts.join(', '));
|
|
1035
|
+
}
|
|
1036
|
+
// 顶层条目仅允许已声明的两棵子树 + npm 恒定携带项(白名单 fail-closed:新增顶层目录默认不得进包)
|
|
1037
|
+
const allowedTopLevel = ['.flow-comet', 'scripts', 'package.json', 'README.md', 'LICENSE'];
|
|
1038
|
+
const unexpectedTop = [...new Set(files.map((p) => p.split('/')[0]))].filter((t) => !allowedTopLevel.includes(t));
|
|
1039
|
+
if (unexpectedTop.length > 0) {
|
|
1040
|
+
throw new Error('包体顶层出现白名单外条目(新增顶层目录默认不得进包): ' + unexpectedTop.join(', '));
|
|
1041
|
+
}
|
|
1042
|
+
// 仅忽略 npm 恒定排除的操作系统垃圾文件(跨平台检出物,非包体声明面)
|
|
1043
|
+
const osJunk = ['.DS_Store', 'Thumbs.db', 'desktop.ini'];
|
|
1044
|
+
const skillsOnDisk = collectTreeFiles(path.join(root, '.flow-comet', 'skills'))
|
|
1045
|
+
.filter((p) => !osJunk.includes(path.basename(p)))
|
|
1046
|
+
.map((p) => '.flow-comet/skills/' + p.replaceAll('\\', '/'));
|
|
1047
|
+
const missingSkills = skillsOnDisk.filter((p) => !files.includes(p));
|
|
1048
|
+
if (missingSkills.length > 0) throw new Error('包体漏收技能树文件: ' + missingSkills.slice(0, 5).join(', '));
|
|
1049
|
+
console.log(' 包体边界: ' + files.length + ' 文件,技能树 ' + skillsOnDisk.length + ' 文件逐文件在场,'
|
|
1050
|
+
+ 'scripts 面 ' + PACKED_SCRIPTS.length + ' 项精确在场(零越界),私有面零命中 ✓');
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
// ③b workflow contract 检查(`comet-check.mjs`):包内**必含面清单**的自动锚。它此前全仓无任何
|
|
1054
|
+
// CI/套件调用——清单漂移没有红灯(本批移出两个参考文件时只能靠手工同步清单)。判据两层:
|
|
1055
|
+
// ① 真实执行权威源的该脚本 → exit 0 且输出 `workflow-contract-ok`(清单与真实树一致);
|
|
1056
|
+
// ② 反向构造(同一判据驱动):把技能树复制到临时载体、删掉清单里的一个必含文件 → 该脚本必须
|
|
1057
|
+
// exit ≠ 0 且报 `Missing required workflow contract files`(清单是真在查,不是恒真输出)。
|
|
1058
|
+
export function assertWorkflowContractCheck(scratch) {
|
|
1059
|
+
const checker = path.join(SCRIPTS_DIR, 'comet-check.mjs');
|
|
1060
|
+
if (!fs.existsSync(checker)) throw new Error('缺少 comet-check.mjs: ' + checker);
|
|
1061
|
+
const runChecker = (script) => {
|
|
1062
|
+
const res = spawnSync(process.execPath, [script], { cwd: path.dirname(script), encoding: 'utf8', timeout: 60000 });
|
|
1063
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
1064
|
+
};
|
|
1065
|
+
const real = runChecker(checker);
|
|
1066
|
+
if (real.status !== 0 || !real.output.includes('workflow-contract-ok')) {
|
|
1067
|
+
throw new Error('comet-check.mjs 未通过(包内必含面清单与真实树漂移)\n退出码 ' + real.status
|
|
1068
|
+
+ '\n输出:\n' + real.output);
|
|
1069
|
+
}
|
|
1070
|
+
// 反向构造:临时载体上一份技能树副本,删掉清单中的一个必含文件 ⇒ 必失败。
|
|
1071
|
+
const carrier = path.join(scratch, 'contract-carrier');
|
|
1072
|
+
fs.mkdirSync(carrier, { recursive: true });
|
|
1073
|
+
copyTreePortable(path.join(SCRIPTS_DIR, '..', '..'), path.join(carrier, 'skills'));
|
|
1074
|
+
const copiedChecker = path.join(carrier, 'skills', 'flow-comet', 'scripts', 'comet-check.mjs');
|
|
1075
|
+
const removedRel = path.join('skills', 'flow-comet', 'reference', 'decision-points.md');
|
|
1076
|
+
const removedAbs = path.join(carrier, removedRel);
|
|
1077
|
+
if (!fs.existsSync(removedAbs)) {
|
|
1078
|
+
throw new Error('反向构造前提不成立:清单必含文件不在副本内 ' + removedRel);
|
|
1079
|
+
}
|
|
1080
|
+
fs.rmSync(removedAbs);
|
|
1081
|
+
const broken = runChecker(copiedChecker);
|
|
1082
|
+
if (broken.status === 0 || !broken.output.includes('Missing required workflow contract files')) {
|
|
1083
|
+
throw new Error('comet-check.mjs 反向构造判别力缺失(删掉清单必含文件后仍通过)\n退出码 '
|
|
1084
|
+
+ broken.status + '\n输出:\n' + broken.output);
|
|
1085
|
+
}
|
|
1086
|
+
console.log(' workflow contract 检查: 权威源 exit 0(workflow-contract-ok)+ 缺件副本必失败 ✓');
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
// ③ bin 双入口契约:两个命令名同指同一安装器(同一实现 = 行为一致的结构事实)。返回安装器绝对路径。
|
|
1090
|
+
export function assertBinContract(pkg, root) {
|
|
1091
|
+
const binTarget = 'scripts/prepare-env.mjs';
|
|
1092
|
+
const bin = pkg.bin && typeof pkg.bin === 'object' ? pkg.bin : null;
|
|
1093
|
+
if (!bin) throw new Error('package.json 缺 bin 声明(双入口契约)');
|
|
1094
|
+
for (const name of ['fcomet', 'flow-comet']) {
|
|
1095
|
+
if (bin[name] !== binTarget) {
|
|
1096
|
+
throw new Error('bin["' + name + '"] 应指向 ' + binTarget + ',实际 ' + JSON.stringify(bin[name]));
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
const installer = path.join(root, ...binTarget.split('/'));
|
|
1100
|
+
if (!fs.existsSync(installer)) throw new Error('bin 目标文件不存在: ' + binTarget);
|
|
1101
|
+
console.log(' bin 双入口: fcomet / flow-comet → ' + binTarget + '(同一实现)✓');
|
|
1102
|
+
return installer;
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
// ④ init 词元形态与红线(负例常驻:变形/重复词元绝不被静默吞成默认安装)。cwd 用场景临时目录。
|
|
1106
|
+
export function assertInitTokenContract(installer, cwd) {
|
|
1107
|
+
const runInstaller = (args) => {
|
|
1108
|
+
const res = spawnSync(process.execPath, [installer, ...args], { cwd, encoding: 'utf8', timeout: 60000 });
|
|
1109
|
+
return { status: res.status ?? 1, stdout: String(res.stdout || ''), stderr: String(res.stderr || '') };
|
|
1110
|
+
};
|
|
1111
|
+
const withInit = runInstaller(['init', '--help']);
|
|
1112
|
+
if (withInit.status !== 0) {
|
|
1113
|
+
throw new Error('`init` 词元形态应退 0(`fcomet init --help`),实际 ' + withInit.status + '\n' + withInit.stderr);
|
|
1114
|
+
}
|
|
1115
|
+
if (!withInit.stdout.includes('fcomet init')) throw new Error('用法输出未含 `fcomet init` 形态:\n' + withInit.stdout);
|
|
1116
|
+
const bare = runInstaller(['--help']);
|
|
1117
|
+
if (bare.status !== 0) throw new Error('裸形态(无 init 词元)`--help` 行为应不变(退 0),实际 ' + bare.status + '\n' + bare.stderr);
|
|
1118
|
+
for (const bad of ['inti', 'int', 'install', 'init init']) {
|
|
1119
|
+
const res = runInstaller(bad.split(' '));
|
|
1120
|
+
if (res.status !== 1) throw new Error('变形/重复词元 `' + bad + '` 必须退 1,实际 ' + res.status + '\n' + res.stdout);
|
|
1121
|
+
if (!res.stderr.includes('未知参数')) {
|
|
1122
|
+
throw new Error('变形/重复词元 `' + bad + '` 的 stderr 应含「未知参数」,实际:\n' + res.stderr);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
// ⑤ 版本查询:`--version` / `-v` 退 0 且输出本工具的版本标识(同一个解析来源,不另设常量)。
|
|
1126
|
+
// 断言做成**状态无关**,因为「版本面已 bump、tag 尚未打」是发布准备的常态:此时随包标记
|
|
1127
|
+
// 领先于 git describe(标记 = 待发版本,describe = 旧 tag + 提交数)。两条合法来源:
|
|
1128
|
+
// ① 随包分发的权威源标记(包形态 / 发布后命中);② 当前 tag 的开发态后缀 `<tag>-N-g<hash>`。
|
|
1129
|
+
// 等值断言会在发布准备期误红——那不是缺陷,是顺序(先改版本面、后打 tag)。
|
|
1130
|
+
const versionShape = /^\d+\.\d+\.\d+(-[0-9A-Za-z][0-9A-Za-z.-]*)?$/;
|
|
1131
|
+
const installedVersion = fs.readFileSync(path.join(SCRIPTS_DIR, '..', 'INSTALLED_VERSION'), 'utf8').trim();
|
|
1132
|
+
let headTag = '';
|
|
1133
|
+
try {
|
|
1134
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
1135
|
+
headTag = execFileSync('git', ['describe', '--tags', '--abbrev=0'], { cwd: repoRoot, encoding: 'utf8', timeout: 30000 }).trim().replace(/^v/, '');
|
|
1136
|
+
} catch { /* 无 git 或无 tag:只按随包标记比对 */ }
|
|
1137
|
+
for (const flag of ['--version', '-v']) {
|
|
1138
|
+
const res = runInstaller([flag]);
|
|
1139
|
+
if (res.status !== 0) {
|
|
1140
|
+
throw new Error('`' + flag + '` 应退 0(版本查询不是错误),实际 ' + res.status + '\n' + res.stderr);
|
|
1141
|
+
}
|
|
1142
|
+
const printed = res.stdout.trim();
|
|
1143
|
+
if (!versionShape.test(printed)) {
|
|
1144
|
+
throw new Error('`' + flag + '` 的输出不是版本标识形态: ' + JSON.stringify(printed));
|
|
1145
|
+
}
|
|
1146
|
+
// 等值、或**以「版本 + `-`」为界**的开发态后缀——不能裸用前缀匹配:`1.5.10` 会被
|
|
1147
|
+
// `startsWith('1.5.1')` 收下,把「族外的更长版本号」误判为一致(实测:注入 1.5.10 时
|
|
1148
|
+
// 裸前缀谓词全绿通过)。边界字符取 `-`,正合 git describe 的后缀形态 `<tag>-N-g<hash>`。
|
|
1149
|
+
const matchesMarker = printed === installedVersion || printed.startsWith(installedVersion + '-');
|
|
1150
|
+
const matchesHeadTag = headTag !== '' && (printed === headTag || printed.startsWith(headTag + '-'));
|
|
1151
|
+
if (!matchesMarker && !matchesHeadTag) {
|
|
1152
|
+
throw new Error('`' + flag + '` 的输出与「随包标记 / 当前 tag」都不一致(标记=' + installedVersion + ' tag=' + headTag + '): ' + JSON.stringify(printed));
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
console.log(' 版本查询: `--version` / `-v` 退 0 且输出为版本标识形态(标记=' + installedVersion + ' / 当前 tag=' + headTag + ')✓');
|
|
1156
|
+
// ⑥ 零参数裸调用不得安装到当前目录(本轮修正):无参数 = 没说要做什么,
|
|
1157
|
+
// 应当在当前目录留下零产物并给出用法与非零退出码。修复前它会做一次完整安装
|
|
1158
|
+
// (.claude/ + flow-kit/ + 改写 .gitignore)→ 误敲即在当前目录铺开一套环境。
|
|
1159
|
+
const emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'no-action-'));
|
|
1160
|
+
try {
|
|
1161
|
+
seedForeignFlowKit(emptyDir); // 预置同名非上游目录:走「已跳过」路径,零网络接触
|
|
1162
|
+
const bareRun = spawnSync(process.execPath, [installer], { cwd: emptyDir, encoding: 'utf8', timeout: 60000 });
|
|
1163
|
+
const status = bareRun.status ?? 1;
|
|
1164
|
+
const stderr = String(bareRun.stderr || '');
|
|
1165
|
+
if (status === 0) {
|
|
1166
|
+
throw new Error('零参数裸调用不得退 0(它做了/声称做了一次安装);实际退 0\n' + String(bareRun.stdout || '').slice(-300));
|
|
1167
|
+
}
|
|
1168
|
+
if (!stderr.includes('用法')) {
|
|
1169
|
+
throw new Error('零参数裸调用的 stderr 应给出用法,实际:\n' + stderr);
|
|
1170
|
+
}
|
|
1171
|
+
// 用法必须**整段**在 stderr:只断言「stderr 含用法」会漏掉「部分行仍写 stdout」的实现
|
|
1172
|
+
// (首行在 stderr 即满足该断言,其余行可能被劈到 stdout——实测出现过)。故此处两侧同判:
|
|
1173
|
+
// stderr 末尾行在场,且 stdout 为空。
|
|
1174
|
+
if (!/--version 打印版本标识/.test(stderr)) {
|
|
1175
|
+
throw new Error('用法末行不在 stderr(用法输出被劈到 stdout):stderr 实际:\n' + stderr);
|
|
1176
|
+
}
|
|
1177
|
+
if (String(bareRun.stdout || '').trim() !== '') {
|
|
1178
|
+
throw new Error('零参数裸调用的 stdout 应为空(用法整段走 stderr),实际:\n' + String(bareRun.stdout));
|
|
1179
|
+
}
|
|
1180
|
+
if (fs.existsSync(path.join(emptyDir, '.claude'))) {
|
|
1181
|
+
throw new Error('零参数裸调用不得在当前目录留下安装产物(.claude/ 被创建)');
|
|
1182
|
+
}
|
|
1183
|
+
console.log(' 零参数裸调用: 非零退出 + 用法整段在 stderr + stdout 空 + 当前目录零产物 ✓');
|
|
1184
|
+
} finally {
|
|
1185
|
+
fs.rmSync(emptyDir, { recursive: true, force: true });
|
|
1186
|
+
}
|
|
1187
|
+
console.log(' init 词元: `init --help` 退 0 且含 fcomet init / 裸形态行为不变 / 变形·重复词元 4 例退 1 且报未知参数 ✓');
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
// ---------- 安装器升级路径清理断言助手(生成物镜像面:权威源已删除 / 改名的条目不得残留) ----------
|
|
1191
|
+
|
|
1192
|
+
// 升级路径清理断言(分发面:安装器的生成物镜像面)——判据 = 真实安装器命令序列 + 文件系统事实,
|
|
1193
|
+
// 而不是「安装器退出码为 0」(后者会被「安装照常成功」恒真满足,清理实现整体缺席也不会变红):
|
|
1194
|
+
// ① 删该删的:镜像目录内「权威源已不存在」的条目,重装后必须消失(含清空后的目录本身);
|
|
1195
|
+
// ② 不该删的必不删(反向探针,逐条):权威源仍存在的文件 / 用户自有技能(无 flow-comet
|
|
1196
|
+
// 命名空间)/ 命名空间内但权威源整个技能目录不在场(安装器没有生成清单,无法判定该目录
|
|
1197
|
+
// 「确由上次生成写入」)/ copyTree 目标但路径不含命名空间(.claude/rules)/ 项目根用户文件;
|
|
1198
|
+
// ③ 输出可核对:被清理条目逐条打印;④ 幂等:第二次重装零清理项、零报错。
|
|
1199
|
+
// 夹具名刻意取合成形态,不占用真实工件名(将来权威源重新引入同名文件时夹具前提不失配)——
|
|
1200
|
+
// 同时以「权威源确实不含该名」为前提断言,防止夹具退化成恒真空过。
|
|
1201
|
+
export function assertInstallerUpgradePathCleanup(scratch) {
|
|
1202
|
+
const pkgCtx = resolvePackageContext();
|
|
1203
|
+
if (pkgCtx.kind === 'copy') {
|
|
1204
|
+
console.log(' 不适用(安装副本侧:' + pkgCtx.reason + ')——安装器升级路径清理断言仅权威源侧可判,本项计通过');
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
const installer = path.join(pkgCtx.root, 'scripts', 'prepare-env.mjs');
|
|
1208
|
+
const target = path.join(scratch, 'upgrade-path');
|
|
1209
|
+
fs.mkdirSync(target, { recursive: true });
|
|
1210
|
+
seedForeignFlowKit(target); // 预置同名非上游目录:flow-kit 走「已跳过」路径,零网络接触
|
|
1211
|
+
const skillsRoot = path.join(target, '.claude', 'skills');
|
|
1212
|
+
const runInstall = () => runInstallerOnce(installer, target);
|
|
1213
|
+
const mustNotExist = (rel, label) => {
|
|
1214
|
+
const p = path.join(skillsRoot, rel);
|
|
1215
|
+
if (fs.existsSync(p)) throw new Error('升级重装后权威源已不存在的生成物仍残留(' + label + '): ' + p);
|
|
1216
|
+
};
|
|
1217
|
+
const mustExist = (rel, label) => {
|
|
1218
|
+
const p = path.join(skillsRoot, rel);
|
|
1219
|
+
if (!fs.existsSync(p)) throw new Error('升级重装误删了不该删的条目(' + label + '): ' + p);
|
|
1220
|
+
};
|
|
1221
|
+
// ① 首次安装(等价「上一代生成物」:内容与权威源一致)
|
|
1222
|
+
assertExit(runInstall(), 0);
|
|
1223
|
+
// ② 夹具:模拟上一代生成、而权威源已删除 / 改名的三类形态(文件 / 嵌套文件 / 目录树 + 空目录)
|
|
1224
|
+
const staleFile = 'flow-comet/legacy-guidance.md';
|
|
1225
|
+
const staleNested = 'flow-comet/reference/legacy-notes.md';
|
|
1226
|
+
const staleDir = 'flow-comet/reference/legacy-subagents';
|
|
1227
|
+
const staleEmptyDir = 'flow-comet/reference/legacy-empty';
|
|
1228
|
+
writeFile(target, path.posix.join('.claude/skills', staleFile), 'stale\n');
|
|
1229
|
+
writeFile(target, path.posix.join('.claude/skills', staleNested), 'stale\n');
|
|
1230
|
+
writeFile(target, path.posix.join('.claude/skills', staleDir, 'author.md'), 'stale\n');
|
|
1231
|
+
fs.mkdirSync(path.join(skillsRoot, staleEmptyDir), { recursive: true });
|
|
1232
|
+
for (const rel of [staleFile, staleNested, staleDir, staleEmptyDir]) {
|
|
1233
|
+
if (fs.existsSync(path.join(pkgCtx.root, '.flow-comet', 'skills', rel))) {
|
|
1234
|
+
throw new Error('夹具前提失效:权威源已含 ' + rel + '——请改用其它合成名');
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
// ③ 反向探针夹具(不该删的必不删)
|
|
1238
|
+
writeFile(target, '.claude/skills/my-own-skill/SKILL.md', 'user skill\n');
|
|
1239
|
+
writeFile(target, '.claude/skills/flow-comet-fork/SKILL.md', 'unattributable dir\n');
|
|
1240
|
+
writeFile(target, '.claude/rules/user-rule.md', 'user rule\n');
|
|
1241
|
+
writeFile(target, 'USER_KEEP.md', 'user file\n');
|
|
1242
|
+
// ④ 升级重装(真实安装器第二次运行同一平台)
|
|
1243
|
+
const upgrade = runInstall();
|
|
1244
|
+
assertExit(upgrade, 0);
|
|
1245
|
+
// ⑤ 删该删的(三类形态逐条)
|
|
1246
|
+
mustNotExist(staleFile, '改名残留文件');
|
|
1247
|
+
mustNotExist(staleNested, '移出残留文件');
|
|
1248
|
+
mustNotExist(staleDir, '残留目录(内容清空后应一并移除)');
|
|
1249
|
+
mustNotExist(staleEmptyDir, '残留空目录');
|
|
1250
|
+
// ⑥ 不该删的必不删(逐条反向探针)
|
|
1251
|
+
mustExist('flow-comet/reference/entry-detail.md', '权威源仍存在的文件');
|
|
1252
|
+
mustExist('flow-comet/SKILL.md', '权威源技能主体');
|
|
1253
|
+
mustExist('my-own-skill/SKILL.md', '用户自有技能(无 flow-comet 命名空间)');
|
|
1254
|
+
mustExist('flow-comet-fork/SKILL.md', '命名空间内但权威源整个技能目录不在场——归属不可判定,fail-closed 保留');
|
|
1255
|
+
if (!fs.existsSync(path.join(target, '.claude', 'rules', 'user-rule.md'))) {
|
|
1256
|
+
throw new Error('白名单外生成面(.claude/rules)的用户文件被误删');
|
|
1257
|
+
}
|
|
1258
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('项目根用户文件被误删');
|
|
1259
|
+
// ⑦ 输出可核对:被清理条目逐条打印(人可复核「删了什么」)
|
|
1260
|
+
for (const rel of [staleFile, staleNested, staleDir, staleEmptyDir]) {
|
|
1261
|
+
const name = path.posix.basename(rel);
|
|
1262
|
+
if (!upgrade.output.includes(name)) {
|
|
1263
|
+
throw new Error('清理输出未逐条打印被清理条目(缺 ' + name + '):\n' + upgrade.output);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
// ⑦b 播报标记钉死:⑧ 的「重跑无清理项」是**负向**断言——标记一旦改名它便恒真;正向锚在此
|
|
1267
|
+
// (标记须在首次升级输出里在场),标记契约的漂移因此必然红,而不是静默失效。
|
|
1268
|
+
if (!upgrade.output.includes('镜像清理: 已删除')) {
|
|
1269
|
+
throw new Error('升级重装输出缺清理播报标记「镜像清理: 已删除」:\n' + upgrade.output);
|
|
1270
|
+
}
|
|
1271
|
+
// ⑧ 幂等:第二次重装零清理项、零报错(重复跑不删多、不重复打印)
|
|
1272
|
+
const again = runInstall();
|
|
1273
|
+
assertExit(again, 0);
|
|
1274
|
+
if (again.output.includes('镜像清理')) {
|
|
1275
|
+
throw new Error('幂等失配:第二次重装仍报告清理动作:\n' + again.output);
|
|
1276
|
+
}
|
|
1277
|
+
console.log(' 升级路径清理: 残留 4 项消失(文件 / 嵌套文件 / 目录树 / 空目录)+ 6 条反向探针保留 + 输出可核对 + 幂等 ✓');
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
// 安装器单次真实运行(两处安装器分发面断言共用同一命令形态——同一知识只有一份实现)
|
|
1281
|
+
function runInstallerOnce(script, target) {
|
|
1282
|
+
const res = spawnSync(process.execPath, [script, '--target', target, '--platform', 'claude-code'], {
|
|
1283
|
+
cwd: target, encoding: 'utf8', timeout: 120000,
|
|
1284
|
+
});
|
|
1285
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
// 把 linkPath 置为指向 targetDir 的链接(夹具构造与清理共用):递归删除会跟随链接目标,
|
|
1289
|
+
// 故先按链接自身解链,再删可能残留的普通目录。
|
|
1290
|
+
function replaceWithLink(linkPath, targetDir) {
|
|
1291
|
+
try {
|
|
1292
|
+
if (fs.lstatSync(linkPath).isSymbolicLink()) fs.unlinkSync(linkPath);
|
|
1293
|
+
} catch { /* 不在场——无需解链 */ }
|
|
1294
|
+
fs.rmSync(linkPath, { recursive: true, force: true });
|
|
1295
|
+
fs.mkdirSync(path.dirname(linkPath), { recursive: true });
|
|
1296
|
+
fs.symlinkSync(targetDir, linkPath, 'junction');
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
// 镜像清理的**根级** fail-closed 断言(同上一条属同一分发面项)——判据 = 真实安装器命令序列 + 文件系统事实:
|
|
1300
|
+
// 根以下的条目层检查拦不住「镜像根自身是链接」:readdir 会跟随进链接目标,清理动作落在**项目之外**,
|
|
1301
|
+
// 而播报打印的仍是项目内的表象路径(真实落点不可见)。两条判据各需一个变体驱动,缺任一变体则
|
|
1302
|
+
// 对应那条判据整体缺席也不会有红灯:
|
|
1303
|
+
// 变体 A「镜像根自身是指向项目外目录的链接」 ⇒ 根级链接判据(不跟随);
|
|
1304
|
+
// 变体 B「父级目录是链接、镜像根自身是普通目录」 ⇒ 真实路径包含性判据(词法路径仍在项目内,
|
|
1305
|
+
// 只有解析真实路径才能证否;此变体下 A 判据必然不命中,故它是包含性判据的独占探针)。
|
|
1306
|
+
// 断言(两变体同):链接目标内的项目外文件**必不消失** + 输出含逐条保留原因 + 重复运行零删除项。
|
|
1307
|
+
// 反向构造见 assertMirrorRootReverseConstruction(同一判据驱动,证明断言不恒真空过)。
|
|
1308
|
+
export function assertMirrorRootLinkFailClosed(scratch, pkgCtx) {
|
|
1309
|
+
if (pkgCtx.kind === 'copy') {
|
|
1310
|
+
console.log(' 不适用(安装副本侧:' + pkgCtx.reason + ')——镜像清理根级判据的探针仅权威源侧可判,本组计通过');
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
const installer = path.join(pkgCtx.root, 'scripts', 'prepare-env.mjs');
|
|
1314
|
+
// 变体 A:镜像根自身 = 指向项目外目录的链接
|
|
1315
|
+
const targetA = path.join(scratch, 'mirror-root-link-self');
|
|
1316
|
+
const outsideA = path.join(scratch, 'mirror-root-link-self-outside');
|
|
1317
|
+
seedForeignFlowKit(targetA);
|
|
1318
|
+
writeFile(outsideA, 'EXTERNAL-NOTE.md', 'out-of-tree user content\n');
|
|
1319
|
+
writeFile(outsideA, path.posix.join('nested', 'DEEP-NOTE.md'), 'out-of-tree user content\n');
|
|
1320
|
+
replaceWithLink(path.join(targetA, '.claude', 'skills', 'flow-comet'), outsideA);
|
|
1321
|
+
// 变体 B:父级目录 = 链接(镜像根自身是目标内的普通目录)
|
|
1322
|
+
const targetB = path.join(scratch, 'mirror-root-link-ancestor');
|
|
1323
|
+
const outsideB = path.join(scratch, 'mirror-root-link-ancestor-outside');
|
|
1324
|
+
seedForeignFlowKit(targetB);
|
|
1325
|
+
writeFile(outsideB, path.posix.join('flow-comet', 'EXTERNAL-NOTE.md'), 'out-of-tree user content\n');
|
|
1326
|
+
replaceWithLink(path.join(targetB, '.claude', 'skills'), outsideB);
|
|
1327
|
+
const mirrorB = path.join(targetB, '.claude', 'skills', 'flow-comet');
|
|
1328
|
+
if (!fs.existsSync(mirrorB) || fs.lstatSync(mirrorB).isSymbolicLink()) {
|
|
1329
|
+
throw new Error('变体 B 前提失效:镜像根自身不是普通目录(应只有父级是链接): ' + mirrorB);
|
|
1330
|
+
}
|
|
1331
|
+
const variants = [
|
|
1332
|
+
{ label: '镜像根自身为链接', target: targetA, outsideFile: path.join(outsideA, 'EXTERNAL-NOTE.md'), reasonPattern: /镜像根是符号链接\/junction/ },
|
|
1333
|
+
{ label: '镜像根真实路径越界', target: targetB, outsideFile: path.join(outsideB, 'flow-comet', 'EXTERNAL-NOTE.md'), reasonPattern: /越出目标项目根/ },
|
|
1334
|
+
];
|
|
1335
|
+
for (const v of variants) assertLinkTargetPreserved(installer, v);
|
|
1336
|
+
assertMirrorRootReverseConstruction(scratch, pkgCtx, installer, variants);
|
|
1337
|
+
console.log(' 镜像清理根级判据: 根自身为链接 / 真实路径越界 两变体下项目外文件保全 + 保留原因可见 + 幂等;'
|
|
1338
|
+
+ '反向构造(判据改恒真)两变体必被越界删除 ✓');
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// 「链接目标内的项目外文件必不消失」断言(两变体同形):项目外文件仍在 + 输出含逐条保留原因 +
|
|
1342
|
+
// 原因指明命中的判据 + 重复运行零删除项且文件仍在(保留是**可见的决定**,不是静默跳过)。
|
|
1343
|
+
function assertLinkTargetPreserved(installer, { label, target, outsideFile, reasonPattern }) {
|
|
1344
|
+
const first = runInstallerOnce(installer, target);
|
|
1345
|
+
assertExit(first, 0);
|
|
1346
|
+
if (!fs.existsSync(outsideFile)) {
|
|
1347
|
+
throw new Error(label + ':镜像清理越界删除了项目之外的文件(真实落点 ' + outsideFile + ')\n输出:\n' + first.output);
|
|
1348
|
+
}
|
|
1349
|
+
if (!first.output.includes('镜像清理保留')) {
|
|
1350
|
+
throw new Error(label + ':输出未逐条打印保留原因——fail-closed 保留必须是可见的决定\n输出:\n' + first.output);
|
|
1351
|
+
}
|
|
1352
|
+
if (!reasonPattern.test(first.output)) {
|
|
1353
|
+
throw new Error(label + ':保留原因未指明命中判据(缺 ' + reasonPattern + ')\n输出:\n' + first.output);
|
|
1354
|
+
}
|
|
1355
|
+
const second = runInstallerOnce(installer, target);
|
|
1356
|
+
assertExit(second, 0);
|
|
1357
|
+
if (second.output.includes('镜像清理: 已删除')) {
|
|
1358
|
+
throw new Error(label + ':重复运行仍报告删除动作(幂等失配)\n输出:\n' + second.output);
|
|
1359
|
+
}
|
|
1360
|
+
if (!fs.existsSync(outsideFile)) {
|
|
1361
|
+
throw new Error(label + ':重复运行后项目之外的文件消失(' + outsideFile + ')');
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
// 反向构造(同一判据驱动):把两处根级判据分别中和为恒真后重跑同一变体 ⇒ 项目外文件**必被删除**;
|
|
1366
|
+
// 判据落点漂移(判据句被改写)时「前提不成立」先行报错,避免变异无处落点而假绿。
|
|
1367
|
+
// 变异目标按**模块身份**定位(两处判据都在 lib/installer-cleanup.mjs——描述符/清理职责的归属模块,
|
|
1368
|
+
// 见设计模块图),不再平铺扫描全部模块;判据句仍按**内容**确认在场(句子漂移 ⇒ 前提不成立,红)。
|
|
1369
|
+
// 载体按**真实包内 scripts 面**落位(PACKED_SCRIPTS,与包体边界判据同集合):照着整个 scripts/ 目录
|
|
1370
|
+
// 搬运会把仓库本地工具也带进载体,使载体比真实包更宽——被拆出的模块即使掉出包体白名单,
|
|
1371
|
+
// 载体里仍在场(安装照跑),反向构造因此对该类缺口零判别力。
|
|
1372
|
+
function assertMirrorRootReverseConstruction(scratch, pkgCtx, installer, variants) {
|
|
1373
|
+
const carrier = path.join(scratch, 'mirror-root-carrier');
|
|
1374
|
+
for (const rel of PACKED_SCRIPTS) {
|
|
1375
|
+
const dest = path.join(carrier, ...rel.split('/'));
|
|
1376
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
1377
|
+
fs.copyFileSync(path.join(pkgCtx.root, ...rel.split('/')), dest);
|
|
1378
|
+
}
|
|
1379
|
+
const cleanupModule = path.join(carrier, 'scripts', 'lib', 'installer-cleanup.mjs');
|
|
1380
|
+
if (!fs.existsSync(cleanupModule)) {
|
|
1381
|
+
throw new Error('反向构造前提不成立:随包 scripts 面缺安装器清理模块 ' + cleanupModule);
|
|
1382
|
+
}
|
|
1383
|
+
const neutralize = (sentence) => {
|
|
1384
|
+
const text = fs.readFileSync(cleanupModule, 'utf8');
|
|
1385
|
+
if (!text.includes(sentence)) return false;
|
|
1386
|
+
fs.writeFileSync(cleanupModule, text.replace(sentence, sentence.replace('if (', 'if (false && ')), 'utf8');
|
|
1387
|
+
return true;
|
|
1388
|
+
};
|
|
1389
|
+
if (!neutralize('if (!isRealPathInside(dstRoot, projectRoot)) {')) {
|
|
1390
|
+
throw new Error('反向构造前提不成立:真实路径包含性判据的落点未找到(判据句已漂移,变异无处落点)');
|
|
1391
|
+
}
|
|
1392
|
+
if (!neutralize('if (rootStat.isSymbolicLink()) {')) {
|
|
1393
|
+
throw new Error('反向构造前提不成立:根级链接判据的落点未找到(判据句已漂移,变异无处落点)');
|
|
1394
|
+
}
|
|
1395
|
+
copyTreePortable(path.join(pkgCtx.root, '.flow-comet', 'skills'), path.join(carrier, '.flow-comet', 'skills'));
|
|
1396
|
+
const mutated = path.join(carrier, 'scripts', 'prepare-env.mjs');
|
|
1397
|
+
for (const v of variants) {
|
|
1398
|
+
const res = runInstallerOnce(mutated, v.target);
|
|
1399
|
+
assertExit(res, 0);
|
|
1400
|
+
if (fs.existsSync(v.outsideFile)) {
|
|
1401
|
+
throw new Error(v.label + ' 反向构造判别力缺失:判据被中和后项目之外的文件仍在(断言可能是恒真空过)\n输出:\n' + res.output);
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
// ---------- 桥接 loader 版本戳重装断言助手(临时 DSH_HOME 重装链路) ----------
|
|
1407
|
+
|
|
1408
|
+
// 从权威源 loader 提取版本戳(与安装器/检查器同一锚点标记行;动态取值,版本演进零维护)
|
|
1409
|
+
export function readBridgeSourceVersion(loader) {
|
|
1410
|
+
const text = fs.readFileSync(loader, 'utf8');
|
|
1411
|
+
const m = /^\/\/ BRIDGE_VERSION: (\S+)$/m.exec(text);
|
|
1412
|
+
if (!m) throw new Error('权威源 loader 缺版本戳标记行(契约锚点): ' + loader);
|
|
1413
|
+
return m[1];
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
// 构造指定版本戳的 loader 副本内容(仅替换标记行,其余逐字保持——MISS 检测:替换未命中即报错)
|
|
1417
|
+
export function stampBridgeLoader(sourceLoader, version) {
|
|
1418
|
+
const text = fs.readFileSync(sourceLoader, 'utf8');
|
|
1419
|
+
const replaced = text.replace(/^\/\/ BRIDGE_VERSION: \S+$/m, '// BRIDGE_VERSION: ' + version);
|
|
1420
|
+
if (!replaced.includes('// BRIDGE_VERSION: ' + version)) {
|
|
1421
|
+
throw new Error('版本戳替换未命中(MISS): 目标版本 ' + version);
|
|
1422
|
+
}
|
|
1423
|
+
return replaced;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
// 安装副本载体的 bridge-check 版本比较断言:dev 态载体(INSTALLED_VERSION = <发布>-<N>-g<hash>)
|
|
1427
|
+
// 与同基础版本 loader 判健康,基础版本失配仍 FAIL。以副本自身脚本运行——bridge-check 读取脚本
|
|
1428
|
+
// 同包 INSTALLED_VERSION,与真实安装载体同构(权威源检出路径固定为发布标记,无法构造 dev 态)。
|
|
1429
|
+
export function assertInstalledCopyBridgeVersionSemantics(target, dshHome, srcVersion) {
|
|
1430
|
+
const installedSkillRoot = path.join(target, '.dsh', 'skills', 'flow-comet');
|
|
1431
|
+
const installedVersionPath = path.join(installedSkillRoot, 'INSTALLED_VERSION');
|
|
1432
|
+
const runBridgeCheck = () => {
|
|
1433
|
+
const res = spawnSync(process.execPath, [path.join(installedSkillRoot, 'scripts', 'workflow-state.mjs'), 'bridge-check'], {
|
|
1434
|
+
cwd: target, encoding: 'utf8', timeout: 60000, env: { ...process.env, DSH_HOME: dshHome },
|
|
1435
|
+
});
|
|
1436
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
1437
|
+
};
|
|
1438
|
+
const devVersion = srcVersion + '-11-g93d96c0';
|
|
1439
|
+
fs.writeFileSync(installedVersionPath, devVersion + '\n', 'utf8');
|
|
1440
|
+
const devHealthy = runBridgeCheck();
|
|
1441
|
+
assertExit(devHealthy, 0);
|
|
1442
|
+
assertOut(devHealthy, '[OK] 版本一致性: loader BRIDGE_VERSION=' + srcVersion + ' ~= 项目 INSTALLED_VERSION=' + devVersion);
|
|
1443
|
+
assertOut(devHealthy, '(dev 态后缀归一后基础版本 ' + srcVersion + ' 一致)');
|
|
1444
|
+
assertOut(devHealthy, 'bridge-check: 健康(全部检查通过)——exit 0');
|
|
1445
|
+
const [major] = srcVersion.split('.').map((p) => parseInt(p, 10));
|
|
1446
|
+
const otherBase = (major + 9) + '.0.0';
|
|
1447
|
+
const otherVersion = otherBase + '-3-gabcdef0';
|
|
1448
|
+
fs.writeFileSync(installedVersionPath, otherVersion + '\n', 'utf8');
|
|
1449
|
+
const baseMismatch = runBridgeCheck();
|
|
1450
|
+
assertExit(baseMismatch, 1);
|
|
1451
|
+
assertOut(baseMismatch, '[FAIL] 版本偏斜: loader BRIDGE_VERSION=' + srcVersion + ' != 项目 INSTALLED_VERSION=' + otherVersion + '(归一基础版本: loader=' + srcVersion + ' / installed=' + otherBase + ')——两值如上');
|
|
1452
|
+
assertOut(baseMismatch, 'bridge-check: 失配 1 项——exit 1');
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
// 预置目标项目 flow-kit/ 为同名非上游目录(安装器走「非上游克隆,已跳过」路径——
|
|
1456
|
+
// 网络零接触;与安装器冒烟同手法)
|
|
1457
|
+
export function seedForeignFlowKit(root) {
|
|
1458
|
+
const flowKitDir = path.join(root, 'flow-kit');
|
|
1459
|
+
fs.mkdirSync(flowKitDir, { recursive: true });
|
|
1460
|
+
execFileSync('git', ['init', '-q'], { cwd: flowKitDir, stdio: 'ignore' });
|
|
1461
|
+
execFileSync('git', ['remote', 'add', 'origin', 'https://example.com/not-flow-kit.git'], { cwd: flowKitDir, stdio: 'ignore' });
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
// ---------- hook 注入命令形态判定(托管条目断言放宽决策:断言意图 = 托管条目在场且指向本项目脚本) ----------
|
|
1465
|
+
// 托管识别 = 脚本 basename(comet-hook-guard.mjs) 在位——与 prepare-env isManagedHookCommand
|
|
1466
|
+
// 同源语义,新旧托管形态均命中(幂等升级识别面)。
|
|
1467
|
+
// 项目根引用特征 = CLAUDE_PROJECT_DIR 变量引用(%VAR%/$env:/$_ENV/${VAR} 词形)或绝对路径特征
|
|
1468
|
+
// (盘符 / 引号·空白·等号后的 POSIX 绝对段)二选一在位——cwd 漂移免疫的形态面;相对路径旧形态
|
|
1469
|
+
// 两者皆无 → 不达标(升级替换判据)。断言不锁定具体引号/变量写法(形态无关化,以特征断言补偿
|
|
1470
|
+
// 精度——注入命令实测微调不再破坏套件)。
|
|
1471
|
+
export function isManagedHookCommand(cmd) {
|
|
1472
|
+
return typeof cmd === 'string' && cmd.includes('comet-hook-guard.mjs');
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
export function hasProjectRootRef(cmd) {
|
|
1476
|
+
if (typeof cmd !== 'string') return false;
|
|
1477
|
+
if (/CLAUDE_PROJECT_DIR/i.test(cmd)) return true;
|
|
1478
|
+
return /[A-Za-z]:[\\/]/.test(cmd) || /["'\s=]\/[^\s"']/.test(cmd);
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
export function assertManagedHookEntry(cmd, label) {
|
|
1482
|
+
if (!isManagedHookCommand(cmd)) {
|
|
1483
|
+
throw new Error(label + ' 缺托管脚本 basename(comet-hook-guard.mjs): ' + String(cmd));
|
|
1484
|
+
}
|
|
1485
|
+
if (!hasProjectRootRef(cmd)) {
|
|
1486
|
+
throw new Error(label + ' 缺项目根引用特征(CLAUDE_PROJECT_DIR 或绝对路径二选一在位;相对路径旧形态不具备 cwd 漂移免疫): ' + String(cmd));
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
// 从 settings.local.json / hooks.json 结构提取全部托管 hook 命令(hooks.PreToolUse[].hooks[].command)
|
|
1491
|
+
export function collectManagedHookCommands(configObj) {
|
|
1492
|
+
const cmds = [];
|
|
1493
|
+
const groups = configObj && configObj.hooks && Array.isArray(configObj.hooks.PreToolUse) ? configObj.hooks.PreToolUse : [];
|
|
1494
|
+
for (const g of groups) {
|
|
1495
|
+
for (const h of (Array.isArray(g && g.hooks) ? g.hooks : [])) {
|
|
1496
|
+
if (isManagedHookCommand(h && h.command)) cmds.push(h.command);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
return cmds;
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
// 多趟链路 SUMMARY 夹具(SUMMARY 模板保真全量满足:标题首行 # SUMMARY: + 首部 4 字段 +
|
|
1503
|
+
// 段序 做了什么→改动文件→verify 输出→6 维自查→越界检查→自检方法;6 维声明 brooks-review)
|
|
1504
|
+
|
|
1505
|
+
// ③ K 组条目表。带 `surface` 声明的条目在导出前统一接入分发面契约(见 applyDistributionSurface)。
|