carlos-flow 2.6.3 → 2.8.0

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.
@@ -1,75 +1,75 @@
1
- #!/usr/bin/env node
2
- // 变异测试:在临时副本上注入退化,确认测试套件能抓住。
3
- // 目的:验证「激活只打印日志」「闸门失效」「会话撞名」这类退化无法蒙混过关。
4
- //
5
- // 用法: node scripts/mutation-check.mjs [源仓路径]
6
- // 产物: <仓库>/.mutation-report.md(可用 MUTATION_REPORT 覆盖)
7
- //
8
- // 跨平台:原先硬编码了某台开发机的本机路径与 Git Bash 路径,CI(ubuntu)必失败。
9
- // 现在:源仓默认取当前仓库根目录;工作副本/报告路径走临时目录或环境变量;
10
- // shell 按平台选择(Windows 用 cmd,POSIX 用默认 sh)。
11
- import { execSync } from "node:child_process";
12
- import { cpSync, rmSync, mkdirSync, readFileSync, writeFileSync, existsSync, symlinkSync, readdirSync } from "node:fs";
13
- import { join, resolve, dirname } from "node:path";
14
- import { tmpdir } from "node:os";
15
- import { fileURLToPath } from "node:url";
16
-
17
- const HERE = dirname(fileURLToPath(import.meta.url));
18
- const REPO_ROOT = resolve(HERE, "..");
19
- const SRC = resolve(process.argv[2] || process.env.MUTATION_SRC || REPO_ROOT);
20
- const WORK = resolve(process.env.MUTATION_WORK || join(tmpdir(), "carlos-flow-mutant-work"));
21
- const REPORT = resolve(process.env.MUTATION_REPORT || join(REPO_ROOT, ".mutation-report.md"));
22
-
23
- // 变异定义:id / 描述 / 文件 / 旧文本 / 新文本(精确字符串替换)
24
- const MUTANTS = [
25
- {
26
- id: "M1-激活只打印日志",
27
- desc: "把 checkTrigger 里的 callMCP 换成假返回(激活只剩日志)",
28
- file: "client/carlos.js",
29
- // 单行匹配,不受 CRLF/LF 影响;兼容单双引号两种写法
30
- pattern: /const r = await callMCP\((['"])tools\/call\1, \{ name: step\.tool, arguments: step\.args \}\);/,
31
- to: `const r = { result: { isError: false, content: [{ text: '{"ok":true}' }] } }; // MUTANT`,
32
- },
33
- {
34
- id: "M2-claim闸门失效",
35
- desc: "移除 flow_claim 的 activation_pending 闸门",
36
- file: "src/index.js",
37
- from: ` // 激活占位卡闸门:占位卡未补齐字段时不允许领取派发名额
38
- if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
39
- return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
40
- ")。请先补齐后重新调用 flow_submit_requirement,再来领取派发名额。");
41
- }`,
42
- to: ` // MUTANT: claim 闸门被移除`,
43
- },
44
- {
45
- id: "M3-verdict闸门失效",
46
- desc: "移除 flow_verdict 的 activation_pending 闸门(占位卡可记 PASS)",
47
- file: "src/index.js",
48
- from: ` if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
49
- audit("decision", sessionId, {
50
- kind: "verdict_denied",
51
- reason: "激活占位卡未补齐",
52
- pending: state.activation_pending,
53
- });
54
- return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
55
- "),不能记录裁定。请先补齐后重新调用 flow_submit_requirement。");
56
- }`,
57
- to: ` // MUTANT: verdict 闸门被移除`,
58
- }, {
59
- id: "M4-会话撞名",
60
- desc: "去掉 sanitizeSessionId 的哈希后缀(清洗后同形的 id 会撞名)",
61
- file: "src/trigger.js",
62
- from: ` const suffix = h.toString(16).padStart(8, '0').slice(0, 8);
63
- return safe + '-' + suffix;`,
64
- to: ` return safe; // MUTANT: 无哈希后缀`,
65
- },
66
- {
67
- id: "M5-scope不算补齐项",
68
- desc: "把 scope 从「当前缺失项」里剔除(scope 未补也不算缺)",
69
- file: "src/index.js",
70
- from: ` ...(!cardHasScope ? ["scope"] : []),`,
71
- to: ` ...(false ? ["scope"] : []), // MUTANT: scope 未补也不算缺`,
72
- },
1
+ #!/usr/bin/env node
2
+ // 变异测试:在临时副本上注入退化,确认测试套件能抓住。
3
+ // 目的:验证「激活只打印日志」「闸门失效」「会话撞名」这类退化无法蒙混过关。
4
+ //
5
+ // 用法: node scripts/mutation-check.mjs [源仓路径]
6
+ // 产物: <仓库>/.mutation-report.md(可用 MUTATION_REPORT 覆盖)
7
+ //
8
+ // 跨平台:原先硬编码了某台开发机的本机路径与 Git Bash 路径,CI(ubuntu)必失败。
9
+ // 现在:源仓默认取当前仓库根目录;工作副本/报告路径走临时目录或环境变量;
10
+ // shell 按平台选择(Windows 用 cmd,POSIX 用默认 sh)。
11
+ import { execSync } from "node:child_process";
12
+ import { cpSync, rmSync, mkdirSync, readFileSync, writeFileSync, existsSync, symlinkSync, readdirSync } from "node:fs";
13
+ import { join, resolve, dirname } from "node:path";
14
+ import { tmpdir } from "node:os";
15
+ import { fileURLToPath } from "node:url";
16
+
17
+ const HERE = dirname(fileURLToPath(import.meta.url));
18
+ const REPO_ROOT = resolve(HERE, "..");
19
+ const SRC = resolve(process.argv[2] || process.env.MUTATION_SRC || REPO_ROOT);
20
+ const WORK = resolve(process.env.MUTATION_WORK || join(tmpdir(), "carlos-flow-mutant-work"));
21
+ const REPORT = resolve(process.env.MUTATION_REPORT || join(REPO_ROOT, ".mutation-report.md"));
22
+
23
+ // 变异定义:id / 描述 / 文件 / 旧文本 / 新文本(精确字符串替换)
24
+ const MUTANTS = [
25
+ {
26
+ id: "M1-激活只打印日志",
27
+ desc: "把 checkTrigger 里的 callMCP 换成假返回(激活只剩日志)",
28
+ file: "client/carlos.js",
29
+ // 单行匹配,不受 CRLF/LF 影响;兼容单双引号两种写法
30
+ pattern: /const r = await callMCP\((['"])tools\/call\1, \{ name: step\.tool, arguments: step\.args \}\);/,
31
+ to: `const r = { result: { isError: false, content: [{ text: '{"ok":true}' }] } }; // MUTANT`,
32
+ },
33
+ {
34
+ id: "M2-claim闸门失效",
35
+ desc: "移除 flow_claim 的 activation_pending 闸门",
36
+ file: "src/index.js",
37
+ from: ` // 激活占位卡闸门:占位卡未补齐字段时不允许领取派发名额
38
+ if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
39
+ return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
40
+ ")。请先补齐后重新调用 flow_submit_requirement,再来领取派发名额。");
41
+ }`,
42
+ to: ` // MUTANT: claim 闸门被移除`,
43
+ },
44
+ {
45
+ id: "M3-verdict闸门失效",
46
+ desc: "移除 flow_verdict 的 activation_pending 闸门(占位卡可记 PASS)",
47
+ file: "src/index.js",
48
+ from: ` if (Array.isArray(state.activation_pending) && state.activation_pending.length > 0) {
49
+ audit("decision", sessionId, {
50
+ kind: "verdict_denied",
51
+ reason: "激活占位卡未补齐",
52
+ pending: state.activation_pending,
53
+ });
54
+ return fail("激活占位卡尚未补齐(待补:" + state.activation_pending.join("、") +
55
+ "),不能记录裁定。请先补齐后重新调用 flow_submit_requirement。");
56
+ }`,
57
+ to: ` // MUTANT: verdict 闸门被移除`,
58
+ }, {
59
+ id: "M4-会话撞名",
60
+ desc: "去掉 sanitizeSessionId 的哈希后缀(清洗后同形的 id 会撞名)",
61
+ file: "src/trigger.js",
62
+ from: ` const suffix = h.toString(16).padStart(8, '0').slice(0, 8);
63
+ return safe + '-' + suffix;`,
64
+ to: ` return safe; // MUTANT: 无哈希后缀`,
65
+ },
66
+ {
67
+ id: "M5-scope不算补齐项",
68
+ desc: "把 scope 从「当前缺失项」里剔除(scope 未补也不算缺)",
69
+ file: "src/index.js",
70
+ from: ` ...(!cardHasScope ? ["scope"] : []),`,
71
+ to: ` ...(false ? ["scope"] : []), // MUTANT: scope 未补也不算缺`,
72
+ },
73
73
  {
74
74
  id: "M6-半补齐绕过闸门",
75
75
  desc: "已激活会话改成「只要提交就解闸」(移除幂等派生)",
@@ -118,323 +118,366 @@ const MUTANTS = [
118
118
  activationPending = currentMissing; // MUTANT: 普通卡也上闸
119
119
  }`,
120
120
  },
121
- {
122
- id: "M11-占位卡硬编码三项",
123
- desc: "占位卡待补项退回硬编码三项(服务端派生失效)",
124
- file: "src/index.js",
125
- from: ` activationPending = currentMissing;
126
- } else if (wasActivatedBefore) {`,
127
- to: ` activationPending = ["scope", "acceptance", "verify.cmd"]; // MUTANT: 硬编码
128
- } else if (wasActivatedBefore) {`,
129
- },
130
- {
131
- id: "M12-跨进程锁失效",
132
- desc: "把锁文件改成非排他创建('wx' → 'a'),跨进程排他性失效 → 读-改-写重新变成非原子",
133
- file: "src/state.js",
134
- from: ` const fd = fs.openSync(lockFile, "wx");`,
135
- to: ` const fd = fs.openSync(lockFile, "a"); // MUTANT: 非排他`,
136
- },
137
- {
138
- id: "M13-只读意图误判",
139
- desc: "把只读意图判定改为「命中只读词就算只读」(含改动动词时误判 → 会永久锁死)",
140
- file: "src/trigger.js",
141
- from: ` const hasMutating = mut.some((w) => s.includes(String(w).toLowerCase()));
142
- return !hasMutating;`,
143
- to: ` const hasMutating = mut.some((w) => s.includes(String(w).toLowerCase()));
144
- return true; // MUTANT: 无视改动动词`,
145
- },
146
- {
147
- id: "M7-空scope fail-open",
148
- desc: "enforceScope 在空 scope 时直接返回 ok:true(任意路径都算未越界)",
149
- file: "src/dispatch.js",
150
- from: ` if (!Array.isArray(scope) || scope.length === 0) {
151
- return {
152
- ok: false,
153
- violations: diffFiles.slice(),
154
- message: \`越界改动(卡片 scope 为空,不允许修改任何文件):\${diffFiles.join(", ")}\`,
155
- };
156
- }`,
157
- to: ` if (!Array.isArray(scope) || scope.length === 0) {
158
- return { ok: true, violations: [] }; // MUTANT: fail-open
159
- }`,
160
- },
161
- {
162
- id: "M8-未派发可记PASS",
163
- desc: "移除 flow_verdict 的 dispatch_count 检查(未派发也能记 PASS)",
164
- file: "src/index.js",
165
- from: ` if (verdict === "PASS" && !(Number(state.dispatch_count) > 0)) {`,
166
- to: ` if (false) { // MUTANT: 允许未派发就记 PASS`,
167
- },
168
- ];
169
-
170
- function sh(cmd, cwd, env = {}) {
171
- try {
172
- // Windows 下走 Git Bash(npm 是 .cmd 包装器),POSIX 下用系统 sh
173
- const isWin = process.platform === "win32";
174
- const shell = isWin ? "C:/Program Files/Git/bin/bash.exe" : "/bin/sh";
175
- return execSync(cmd, {
176
- cwd,
177
- encoding: "utf8",
178
- shell,
179
- env: { ...process.env, ...env },
180
- stdio: ["ignore", "pipe", "pipe"],
181
- timeout: 600000,
182
- });
183
- } catch (e) {
184
- return (e.stdout || "") + (e.stderr || "") + "\nEXIT=" + (e.status ?? "err");
185
- }
186
- }
187
-
188
- const lines = [];
189
- lines.push("# 变异测试报告");
190
- lines.push("");
191
- lines.push(`- 源仓库:\`carlos-flow-cindy\` / \`carlos-flow-opencode\`(工作副本路径已改写为占位符)`);
192
- lines.push(`- 变异副本:临时目录(用完自动删除,不入库)`);
193
- lines.push(`- 目的:确认测试套件能抓住「激活只打印日志」「闸门失效」「会话撞名」等退化`);
194
- lines.push("");
195
-
196
- // ---- 工具:安全删目录 / 清理残留 bridge ------------------------------------
197
- // Windows 上临时目录里若有后台 bridge 进程占用文件,rmSync 会抛 EBUSY 直接崩溃。
198
- // 这里改成「重试若干次 → 仍失败则给出可读错误并改用新工作目录继续」,不裸崩。
199
- const rmDirSafe = (dir) => {
200
- for (let i = 0; i < 6; i++) {
201
- try {
202
- rmSync(dir, { recursive: true, force: true });
203
- return true;
204
- } catch (e) {
205
- const code = e && e.code;
206
- if (code === "EBUSY" || code === "EPERM" || code === "ENOTEMPTY") {
207
- // 占用的进程还没退出:等一下再试
208
- try { execSync(process.platform === "win32" ? "ping -n 2 127.0.0.1 >nul" : "sleep 1"); } catch { /* ignore */ }
209
- continue;
210
- }
211
- console.error(`⚠ 清理临时目录失败(${dir}):${e && e.message ? e.message : e}`);
212
- return false;
213
- }
214
- }
215
- console.error(
216
- `⚠ 清理临时目录失败(${dir}):Windows 上仍有进程占用该目录(EBUSY)。` +
217
- `已改用新的工作目录继续,避免整轮变异测试崩溃。`
218
- );
219
- return false;
220
- };
221
-
222
- // 测试期间由副本拉起的 bridge 会写 <dir>/bridge-<port>.pid;
223
- // 只清理**本次新增**的 pid 文件(不碰用户自己的 bridge)。
224
- const bridgePidFiles = (dir) => {
225
- try {
226
- return readdirSync(dir).filter((f) => /^bridge-\d+\.pid$/.test(f));
227
- } catch {
228
- return [];
229
- }
230
- };
231
- const killBridge = (pid) => {
232
- try {
233
- if (process.platform === "win32") {
234
- execSync(`taskkill /PID ${pid} /T /F`, { stdio: "ignore" });
235
- } else {
236
- process.kill(Number(pid), "SIGTERM");
237
- }
238
- return true;
239
- } catch {
240
- return false; // 进程已退出
241
- }
242
- };
243
- const cleanupBridges = (dir, before) => {
244
- for (const f of bridgePidFiles(dir)) {
245
- if (before && before.has(f)) continue; // 源仓已存在的 pid 文件(如用户自己的 bridge)绝不碰
246
- try {
247
- const pid = readFileSync(join(dir, f), "utf8").trim();
248
- if (/^\d+$/.test(pid) && killBridge(pid)) {
249
- console.log(` [cleanup] 已终止残留 bridge(pid=${pid}, ${f})`);
250
- }
251
- } catch {
252
- /* ignore */
253
- }
254
- }
255
- };
256
-
257
- // 基线:源仓里已存在的 bridge pid 文件(用户自己开的、或上一轮遗留的)。
258
- // 工作目录是从源仓复制出来的,里面的 pid 文件天然都在这个集合里 → 只有**本轮新产生**的
259
- // 端口对应的 pid 文件才会被清理,绝不会误杀源仓/用户已在运行的 bridge。
260
- const SRC_BRIDGES = new Set(bridgePidFiles(SRC));
261
-
262
- // 每个变异体的 BRIDGE_PORT 基准(可用 MUTATION_PORT_BASE 调整)。
263
- // 注意:**不要并发跑两个 mutation-check 实例** —— 端口段固定,并发实例会互相抢端口,
264
- // 导致套件启动失败/结果失真(表现为 concurrency 套件输出异常)。
265
- const PORT_BASE = Number(process.env.MUTATION_PORT_BASE || 20100);
266
-
267
- const base = sh("npm run test:e2e 2>&1; echo E2E_EXIT=$?; npm run test:trigger 2>&1; echo TR_EXIT=$?; npm run test:concurrency 2>&1; echo CONC_EXIT=$?", SRC, {
268
- CARLOS_FLOW_HOME: join(tmpdir(), "carlos-flow-mutant-home", "base"),
269
- });
270
- const baseE2e = /════ 结果: (\d+) 通过 \/ (\d+) 失败/.exec(base);
271
- const baseTr = /总测试数: (\d+)[\s\S]*?✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(base);
272
- const baseConc = /✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(base.slice(base.lastIndexOf("跨进程并发")));
273
-
274
- // 基线必须全绿:否则每个变异体「注入后仍然失败」会被误判成「被抓住」,
275
- // 脚本就会在基线本身是红的情况下报「全部被抓住」并 exit 0(假绿)。
276
- const baseProblems = [];
277
- if (!baseE2e) baseProblems.push("e2e 未取到结果");
278
- else if (Number(baseE2e[2]) > 0) baseProblems.push(`e2e ${baseE2e[2]} 条失败`);
279
- if (!baseTr) baseProblems.push("trigger 未取到结果");
280
- else if (Number(baseTr[3]) > 0) baseProblems.push(`trigger ${baseTr[3]} 条失败`);
281
- if (!baseConc) baseProblems.push("concurrency 未取到结果");
282
- else if (Number(baseConc[2]) > 0) baseProblems.push(`concurrency ${baseConc[2]} 条失败`);
283
- const baseGreen = baseProblems.length === 0;
284
-
285
- lines.push(`## 基线(未变异)`);
286
- lines.push("");
287
- lines.push(`- e2e:${baseE2e ? `${baseE2e[1]} 通过 / ${baseE2e[2]} 失败` : "未取到"}`);
288
- lines.push(`- trigger:${baseTr ? `${baseTr[2]} 通过 / ${baseTr[3]} 失败` : "未取到"}`);
289
- lines.push(`- concurrency:${baseConc ? `${baseConc[1]} 通过 / ${baseConc[2]} 失败` : "未取到"}`);
290
- lines.push("");
291
- if (!baseGreen) {
292
- lines.push(`> ❌ **基线不绿**(${baseProblems.join(";")})——基线红时无法区分「变异被抓住」与「基线本来就红」,`);
293
- lines.push(`> 本次变异结果**不可信**,脚本已以退出码 1 终止。请先修好基线再跑变异测试。`);
294
- lines.push("");
295
- writeFileSync(REPORT, lines.join("\n") + "\n", "utf8");
296
- console.error(`✗ 基线不绿:${baseProblems.join(";")}`);
297
- console.error(" 基线红时,任何变异体都会「注入后仍然失败」→ 会被误判成被抓住(假绿)。");
298
- console.error(" 请先修复基线(见报告 " + REPORT + ")。");
299
- process.exit(1);
300
- }
301
-
302
- const results = [];
303
-
304
- let workDir = WORK;
305
-
306
- for (const [mi, m] of MUTANTS.entries()) {
307
- // 删目录失败(Windows 上后台 bridge 占用 → EBUSY)时不裸崩:换新工作目录继续
308
- if (!rmDirSafe(workDir)) workDir = WORK + "-" + (mi + 1);
309
- mkdirSync(workDir, { recursive: true });
310
- cpSync(SRC, workDir, {
311
- recursive: true,
312
- filter: (src) => !src.includes("node_modules") && !src.includes(".git"),
313
- });
314
- // 复用依赖(必须成功,否则 bridge 找不到 MCP SDK,测试会全超时)
315
- const nm = join(workDir, "node_modules");
316
- if (!existsSync(nm)) {
317
- // 优先 junction/symlink(快);POSIX 上若不支持则退化为拷贝(CI 稳但慢)
318
- let linked = false;
319
- try {
320
- symlinkSync(join(SRC, "node_modules"), nm, process.platform === "win32" ? "junction" : "dir");
321
- linked = true;
322
- } catch (e) {
323
- console.warn("symlink 失败,退化为拷贝 node_modules:", e.message);
324
- }
325
- if (!linked) {
326
- try {
327
- cpSync(join(SRC, "node_modules"), nm, { recursive: true, dereference: true });
328
- } catch (e2) {
329
- console.error("✗ 无法准备 node_modules(symlink 与拷贝均失败):", e2.message);
330
- process.exit(1);
331
- }
332
- }
333
- }
334
-
335
- const target = join(workDir, m.file);
336
- // 统一换行符:仓库里 index.js 等是 CRLF,trigger.js 等是 LF,不归一化会匹配不上
337
- const text = readFileSync(target, "utf8").replace(/\r\n/g, "\n");
338
- let mutated;
339
- if (m.pattern) {
340
- if (!m.pattern.test(text)) {
341
- results.push({ ...m, verdict: "SKIP", detail: "未匹配到变异点(源码已变)" });
342
- lines.push(`## ${m.id}`);
343
- lines.push("");
344
- lines.push("- 结果:**SKIP** —— 未匹配到变异点");
345
- lines.push("");
346
- continue;
347
- }
348
- mutated = text.replace(m.pattern, m.to);
349
- } else {
350
- if (!text.includes(m.from)) {
351
- results.push({ ...m, verdict: "SKIP", detail: "未匹配到变异点(源码已变)" });
352
- lines.push(`## ${m.id}`);
353
- lines.push("");
354
- lines.push("- 结果:**SKIP** —— 未匹配到变异点");
355
- lines.push("");
356
- continue;
357
- }
358
- mutated = text.split(m.from).join(m.to);
359
- }
360
- writeFileSync(target, mutated, "utf8");
361
-
362
- const out = sh("npm run test:e2e 2>&1; echo E2E_EXIT=$?; npm run test:trigger 2>&1; echo TR_EXIT=$?; npm run test:concurrency 2>&1; echo CONC_EXIT=$?", workDir, {
363
- CARLOS_FLOW_HOME: join(tmpdir(), "carlos-flow-mutant-home", m.id.replace(/[^A-Za-z0-9]/g, "_")),
364
- // 每个变异体用**独立端口**:否则会复用上一轮副本拉起的旧 bridge,
365
- // 旧 bridge 内存里跑的是旧代码 → 测试结果失真(可能假绿 / 假红)。
366
- BRIDGE_PORT: String(PORT_BASE + mi),
367
- });
368
- const e2eFail = /════ 结果: (\d+) 通过 \/ (\d+) 失败/.exec(out);
369
- const trFail = /总测试数: (\d+)[\s\S]*?✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(out);
370
- const concFail = /✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(out.slice(out.lastIndexOf("跨进程并发")));
371
-
372
- // fail-closed:套件**没跑起来**(输出里取不到结果)时,既不能算「被抓住」,
373
- // 也不能算「逃逸」——那是测试没运行,不是退化蒙混过关。
374
- // 高负载下并发套件偶尔启动失败,若当成 0 失败就会把变异体误判成 NOT CAUGHT(假红且误导)。
375
- const unparsed = [];
376
- if (!e2eFail) unparsed.push("e2e");
377
- if (!trFail) unparsed.push("trigger");
378
- if (!concFail) unparsed.push("concurrency");
379
- if (unparsed.length) {
380
- lines.push(`## ${m.id}`);
381
- lines.push("");
382
- lines.push(`- 注入:${m.desc}`);
383
- lines.push(`- 文件:\`${m.file}\``);
384
- lines.push("- 结果:**套件未成功运行(结果不可信)**");
385
- lines.push(`- 详情:无法解析结果的套件:${unparsed.join("、")}`);
386
- lines.push("");
387
- writeFileSync(REPORT, lines.join("\n") + "\n", "utf8");
388
- console.error(`✗ 变异体 ${m.id}:套件输出无法解析(${unparsed.join("、")})—— 测试没有真正运行,结果不可信。`);
389
- console.error(" (不是「变异体逃逸」,而是套件没跑起来;请在低负载下重跑,或看 mutation-run.log)");
390
- console.error(" 报告已写入: " + REPORT);
391
- process.exit(1);
392
- }
393
-
394
- const caught =
395
- (e2eFail && Number(e2eFail[2]) > 0) ||
396
- (trFail && Number(trFail[3]) > 0) ||
397
- (concFail && Number(concFail[2]) > 0);
398
- const verdict = caught ? "CAUGHT ✅" : "NOT CAUGHT ❌";
399
- results.push({
400
- ...m,
401
- verdict,
402
- detail: `e2e ${e2eFail ? e2eFail[1] + "/" + e2eFail[2] : "?"} 失败, trigger ${trFail ? trFail[3] : "?"} 失败, concurrency ${concFail ? concFail[2] : "?"} 失败`,
403
- });
404
-
405
- lines.push(`## ${m.id}`);
406
- lines.push("");
407
- lines.push(`- 注入:${m.desc}`);
408
- lines.push(`- 文件:\`${m.file}\``);
409
- lines.push(`- 结果:**${verdict}**`);
410
- lines.push(`- 详情:e2e ${e2eFail ? e2eFail[1] + " 通过 / " + e2eFail[2] + " 失败" : "?"};trigger ${trFail ? trFail[2] + " 通过 / " + trFail[3] + " 失败" : "?"};concurrency ${concFail ? concFail[1] + " 通过 / " + concFail[2] + " 失败" : "?"}`);
411
- lines.push("");
412
- const failed = (out.match(/✗[^\n]*/g) || []).slice(0, 6);
413
- if (failed.length) {
414
- lines.push("失败用例:");
415
- for (const f of failed) lines.push("```\n" + f.trim() + "\n```");
416
- lines.push("");
417
- }
418
-
419
- // 本轮收尾:先终止本轮拉起的 bridge,再删工作目录(否则 Windows 上删不掉 → EBUSY)
420
- cleanupBridges(workDir, SRC_BRIDGES);
421
- rmDirSafe(workDir);
422
- }
423
-
424
- cleanupBridges(workDir, SRC_BRIDGES);
425
- rmDirSafe(workDir);
426
-
427
- lines.unshift("## 总览");
428
- lines.unshift("");
429
- lines.push("| 变异 | 结果 | 详情 |");
430
- lines.push("|---|---|---|");
431
- for (const r of results) {
432
- lines.push(`| ${r.id} | ${r.verdict} | ${r.detail} |`);
433
- }
434
- lines.push("");
435
-
436
- writeFileSync(REPORT, lines.join("\n"), "utf8");
437
- console.log(lines.slice(0, 40).join("\n"));
438
- console.log("\n报告已写入:", REPORT);
439
- const notCaught = results.filter((r) => r.verdict !== "CAUGHT ✅");
440
- process.exit(notCaught.length === 0 ? 0 : 1);
121
+ {
122
+ id: "M11-占位卡硬编码三项",
123
+ desc: "占位卡待补项退回硬编码三项(服务端派生失效)",
124
+ file: "src/index.js",
125
+ from: ` activationPending = currentMissing;
126
+ } else if (wasActivatedBefore) {`,
127
+ to: ` activationPending = ["scope", "acceptance", "verify.cmd"]; // MUTANT: 硬编码
128
+ } else if (wasActivatedBefore) {`,
129
+ },
130
+ {
131
+ id: "M12-跨进程锁失效",
132
+ desc: "把锁文件改成非排他创建('wx' → 'a'),跨进程排他性失效 → 读-改-写重新变成非原子",
133
+ file: "src/state.js",
134
+ from: ` const fd = fs.openSync(lockFile, "wx");`,
135
+ to: ` const fd = fs.openSync(lockFile, "a"); // MUTANT: 非排他`,
136
+ },
137
+ {
138
+ id: "M13-只读意图误判",
139
+ desc: "把只读意图判定改为「命中只读词就算只读」(含改动动词时误判 → 会永久锁死)",
140
+ file: "src/trigger.js",
141
+ from: ` const hasMutating = mut.some((w) => s.includes(String(w).toLowerCase()));
142
+ return !hasMutating;`,
143
+ to: ` const hasMutating = mut.some((w) => s.includes(String(w).toLowerCase()));
144
+ return true; // MUTANT: 无视改动动词`,
145
+ },
146
+ {
147
+ id: "M14-审计链断链",
148
+ desc: "审计链去掉 prevHash 链接(每条记录各自独立哈希 → 删改中间记录也不会被发现)",
149
+ file: "src/state.js",
150
+ from: ` rec.prevHash = prev;
151
+ rec.hash = computeAuditHash(rec, prev);`,
152
+ to: ` rec.prevHash = prev;
153
+ rec.hash = computeAuditHash(rec, ""); // MUTANT: 断开链式链接`,
154
+ },
155
+ {
156
+ id: "M15-审计链校验恒真",
157
+ desc: "verifyAuditChain 永远返回 ok:true → 任何篡改都发现不了",
158
+ file: "src/state.js",
159
+ from: ` return { ok: broken === 0, total, broken, legacy, brokenAt, file, mode: auditHashMode() };`,
160
+ to: ` return { ok: true, total, broken, legacy, brokenAt, file, mode: auditHashMode() }; // MUTANT: 恒真`,
161
+ },
162
+ {
163
+ id: "M16-越界检查退回 fail-open",
164
+ desc: "enforceScope 遇到任何越界都返回 ok:true → 越界改动直接放行",
165
+ file: "src/dispatch.js",
166
+ from: ` if (violations.length > 0) {
167
+ return { ok: false, violations, checked: diffFiles.slice(), message: \`越界改动(超出 scope):\${violations.join(", ")}\` };
168
+ }`,
169
+ to: ` if (violations.length > 0) {
170
+ return { ok: true, violations, checked: diffFiles.slice(), message: \`越界改动(超出 scope):\${violations.join(", ")}\` }; // MUTANT: fail-open
171
+ }`,
172
+ },
173
+ {
174
+ id: "M17-date 参数失效",
175
+ desc: "readAudit 用 localDate() 代替 rawDate → date 参数静默失效(读到的永远是今天)",
176
+ file: "src/state.js",
177
+ from: ` // ⚠️ 必须用 rawDate(而不是 localDate())—— 否则 opts.date 完全失效。
178
+ const file = path.join(AUDIT_DIR, \`\${rawDate}.jsonl\`);`,
179
+ to: ` // MUTANT: 用回 localDate() → date 参数静默失效
180
+ const file = path.join(AUDIT_DIR, \`\${localDate()}.jsonl\`);`,
181
+ },
182
+ {
183
+ id: "M7-空scope fail-open",
184
+ desc: "enforceScope 在空 scope 时直接返回 ok:true(任意路径都算未越界)",
185
+ file: "src/dispatch.js",
186
+ from: ` if (!Array.isArray(scope) || scope.length === 0) {
187
+ return {
188
+ ok: false,
189
+ violations: diffFiles.slice(),
190
+ checked: diffFiles.slice(),
191
+ message: \`越界改动(卡片 scope 为空,不允许修改任何文件):\${diffFiles.join(", ")}\`,
192
+ };
193
+ }`,
194
+ to: ` if (!Array.isArray(scope) || scope.length === 0) {
195
+ return { ok: true, violations: [] }; // MUTANT: fail-open
196
+ }`,
197
+ },
198
+ {
199
+ id: "M8-未派发可记PASS",
200
+ desc: "移除 flow_verdict 的 dispatch_count 检查(未派发也能记 PASS)",
201
+ file: "src/index.js",
202
+ from: ` if (verdict === "PASS" && !(Number(state.dispatch_count) > 0)) {`,
203
+ to: ` if (false) { // MUTANT: 允许未派发就记 PASS`,
204
+ },
205
+ ];
206
+
207
+ function sh(cmd, cwd, env = {}) {
208
+ try {
209
+ // Windows 下走 Git Bash(npm 是 .cmd 包装器),POSIX 下用系统 sh
210
+ const isWin = process.platform === "win32";
211
+ const shell = isWin ? "C:/Program Files/Git/bin/bash.exe" : "/bin/sh";
212
+ return execSync(cmd, {
213
+ cwd,
214
+ encoding: "utf8",
215
+ shell,
216
+ env: { ...process.env, ...env },
217
+ stdio: ["ignore", "pipe", "pipe"],
218
+ timeout: 600000,
219
+ });
220
+ } catch (e) {
221
+ return (e.stdout || "") + (e.stderr || "") + "\nEXIT=" + (e.status ?? "err");
222
+ }
223
+ }
224
+
225
+ const lines = [];
226
+ lines.push("# 变异测试报告");
227
+ lines.push("");
228
+ lines.push(`- 源仓库:\`carlos-flow-cindy\` / \`carlos-flow-opencode\`(工作副本路径已改写为占位符)`);
229
+ lines.push(`- 变异副本:临时目录(用完自动删除,不入库)`);
230
+ lines.push(`- 目的:确认测试套件能抓住「激活只打印日志」「闸门失效」「会话撞名」等退化`);
231
+ lines.push("");
232
+
233
+ // ---- 工具:安全删目录 / 清理残留 bridge ------------------------------------
234
+ // Windows 上临时目录里若有后台 bridge 进程占用文件,rmSync 会抛 EBUSY 直接崩溃。
235
+ // 这里改成「重试若干次 → 仍失败则给出可读错误并改用新工作目录继续」,不裸崩。
236
+ const rmDirSafe = (dir) => {
237
+ for (let i = 0; i < 6; i++) {
238
+ try {
239
+ rmSync(dir, { recursive: true, force: true });
240
+ return true;
241
+ } catch (e) {
242
+ const code = e && e.code;
243
+ if (code === "EBUSY" || code === "EPERM" || code === "ENOTEMPTY") {
244
+ // 占用的进程还没退出:等一下再试
245
+ try { execSync(process.platform === "win32" ? "ping -n 2 127.0.0.1 >nul" : "sleep 1"); } catch { /* ignore */ }
246
+ continue;
247
+ }
248
+ console.error(`⚠ 清理临时目录失败(${dir}):${e && e.message ? e.message : e}`);
249
+ return false;
250
+ }
251
+ }
252
+ console.error(
253
+ `⚠ 清理临时目录失败(${dir}):Windows 上仍有进程占用该目录(EBUSY)。` +
254
+ `已改用新的工作目录继续,避免整轮变异测试崩溃。`
255
+ );
256
+ return false;
257
+ };
258
+
259
+ // 测试期间由副本拉起的 bridge 会写 <dir>/bridge-<port>.pid;
260
+ // 只清理**本次新增**的 pid 文件(不碰用户自己的 bridge)。
261
+ const bridgePidFiles = (dir) => {
262
+ try {
263
+ return readdirSync(dir).filter((f) => /^bridge-\d+\.pid$/.test(f));
264
+ } catch {
265
+ return [];
266
+ }
267
+ };
268
+ const killBridge = (pid) => {
269
+ try {
270
+ if (process.platform === "win32") {
271
+ execSync(`taskkill /PID ${pid} /T /F`, { stdio: "ignore" });
272
+ } else {
273
+ process.kill(Number(pid), "SIGTERM");
274
+ }
275
+ return true;
276
+ } catch {
277
+ return false; // 进程已退出
278
+ }
279
+ };
280
+ const cleanupBridges = (dir, before) => {
281
+ for (const f of bridgePidFiles(dir)) {
282
+ if (before && before.has(f)) continue; // 源仓已存在的 pid 文件(如用户自己的 bridge)绝不碰
283
+ try {
284
+ const pid = readFileSync(join(dir, f), "utf8").trim();
285
+ if (/^\d+$/.test(pid) && killBridge(pid)) {
286
+ console.log(` [cleanup] 已终止残留 bridge(pid=${pid}, ${f})`);
287
+ }
288
+ } catch {
289
+ /* ignore */
290
+ }
291
+ }
292
+ };
293
+
294
+ // 基线:源仓里已存在的 bridge pid 文件(用户自己开的、或上一轮遗留的)。
295
+ // 工作目录是从源仓复制出来的,里面的 pid 文件天然都在这个集合里 → 只有**本轮新产生**的
296
+ // 端口对应的 pid 文件才会被清理,绝不会误杀源仓/用户已在运行的 bridge。
297
+ const SRC_BRIDGES = new Set(bridgePidFiles(SRC));
298
+
299
+ // 每个变异体的 BRIDGE_PORT 基准(可用 MUTATION_PORT_BASE 调整)。
300
+ // 注意:**不要并发跑两个 mutation-check 实例** —— 端口段固定,并发实例会互相抢端口,
301
+ // 导致套件启动失败/结果失真(表现为 concurrency 套件输出异常)。
302
+ const PORT_BASE = Number(process.env.MUTATION_PORT_BASE || 20100);
303
+
304
+ const base = sh("npm run test:e2e 2>&1; echo E2E_EXIT=$?; npm run test:trigger 2>&1; echo TR_EXIT=$?; npm run test:concurrency 2>&1; echo CONC_EXIT=$?; npm run test:scope 2>&1; echo SCOPE_EXIT=$?; npm run test:audit 2>&1; echo AUDIT_EXIT=$?", SRC, {
305
+ CARLOS_FLOW_HOME: join(tmpdir(), "carlos-flow-mutant-home", "base"),
306
+ });
307
+ const baseE2e = /════ 结果: (\d+) 通过 \/ (\d+) 失败/.exec(base);
308
+ const baseTr = /总测试数: (\d+)[\s\S]*?✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(base);
309
+ const baseConc = /✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(base.slice(base.lastIndexOf("跨进程并发")));
310
+
311
+ // 基线必须全绿:否则每个变异体「注入后仍然失败」会被误判成「被抓住」,
312
+ // 脚本就会在基线本身是红的情况下报「全部被抓住」并 exit 0(假绿)。
313
+ const baseProblems = [];
314
+ if (!baseE2e) baseProblems.push("e2e 未取到结果");
315
+ else if (Number(baseE2e[2]) > 0) baseProblems.push(`e2e ${baseE2e[2]} 条失败`);
316
+ if (!baseTr) baseProblems.push("trigger 未取到结果");
317
+ else if (Number(baseTr[3]) > 0) baseProblems.push(`trigger ${baseTr[3]} 条失败`);
318
+ if (!baseConc) baseProblems.push("concurrency 未取到结果");
319
+ else if (Number(baseConc[2]) > 0) baseProblems.push(`concurrency ${baseConc[2]} 条失败`);
320
+ const baseGreen = baseProblems.length === 0;
321
+
322
+ lines.push(`## 基线(未变异)`);
323
+ lines.push("");
324
+ lines.push(`- e2e:${baseE2e ? `${baseE2e[1]} 通过 / ${baseE2e[2]} 失败` : "未取到"}`);
325
+ lines.push(`- trigger:${baseTr ? `${baseTr[2]} 通过 / ${baseTr[3]} 失败` : "未取到"}`);
326
+ lines.push(`- concurrency:${baseConc ? `${baseConc[1]} 通过 / ${baseConc[2]} 失败` : "未取到"}`);
327
+ lines.push("");
328
+ if (!baseGreen) {
329
+ lines.push(`> ❌ **基线不绿**(${baseProblems.join(";")})——基线红时无法区分「变异被抓住」与「基线本来就红」,`);
330
+ lines.push(`> 本次变异结果**不可信**,脚本已以退出码 1 终止。请先修好基线再跑变异测试。`);
331
+ lines.push("");
332
+ writeFileSync(REPORT, lines.join("\n") + "\n", "utf8");
333
+ console.error(`✗ 基线不绿:${baseProblems.join(";")}`);
334
+ console.error(" 基线红时,任何变异体都会「注入后仍然失败」→ 会被误判成被抓住(假绿)。");
335
+ console.error(" 请先修复基线(见报告 " + REPORT + ")。");
336
+ process.exit(1);
337
+ }
338
+
339
+ const results = [];
340
+
341
+ let workDir = WORK;
342
+
343
+ for (const [mi, m] of MUTANTS.entries()) {
344
+ // 删目录失败(Windows 上后台 bridge 占用 → EBUSY)时不裸崩:换新工作目录继续
345
+ if (!rmDirSafe(workDir)) workDir = WORK + "-" + (mi + 1);
346
+ mkdirSync(workDir, { recursive: true });
347
+ cpSync(SRC, workDir, {
348
+ recursive: true,
349
+ filter: (src) => !src.includes("node_modules") && !src.includes(".git"),
350
+ });
351
+ // 复用依赖(必须成功,否则 bridge 找不到 MCP SDK,测试会全超时)
352
+ const nm = join(workDir, "node_modules");
353
+ if (!existsSync(nm)) {
354
+ // 优先 junction/symlink(快);POSIX 上若不支持则退化为拷贝(CI 稳但慢)
355
+ let linked = false;
356
+ try {
357
+ symlinkSync(join(SRC, "node_modules"), nm, process.platform === "win32" ? "junction" : "dir");
358
+ linked = true;
359
+ } catch (e) {
360
+ console.warn("symlink 失败,退化为拷贝 node_modules:", e.message);
361
+ }
362
+ if (!linked) {
363
+ try {
364
+ cpSync(join(SRC, "node_modules"), nm, { recursive: true, dereference: true });
365
+ } catch (e2) {
366
+ console.error("✗ 无法准备 node_modules(symlink 与拷贝均失败):", e2.message);
367
+ process.exit(1);
368
+ }
369
+ }
370
+ }
371
+
372
+ const target = join(workDir, m.file);
373
+ // 统一换行符:仓库里 index.js 等是 CRLF,trigger.js 等是 LF,不归一化会匹配不上
374
+ const text = readFileSync(target, "utf8").replace(/\r\n/g, "\n");
375
+ let mutated;
376
+ if (m.pattern) {
377
+ if (!m.pattern.test(text)) {
378
+ results.push({ ...m, verdict: "SKIP", detail: "未匹配到变异点(源码已变)" });
379
+ lines.push(`## ${m.id}`);
380
+ lines.push("");
381
+ lines.push("- 结果:**SKIP** —— 未匹配到变异点");
382
+ lines.push("");
383
+ continue;
384
+ }
385
+ mutated = text.replace(m.pattern, m.to);
386
+ } else {
387
+ if (!text.includes(m.from)) {
388
+ results.push({ ...m, verdict: "SKIP", detail: "未匹配到变异点(源码已变)" });
389
+ lines.push(`## ${m.id}`);
390
+ lines.push("");
391
+ lines.push("- 结果:**SKIP** —— 未匹配到变异点");
392
+ lines.push("");
393
+ continue;
394
+ }
395
+ mutated = text.split(m.from).join(m.to);
396
+ }
397
+ writeFileSync(target, mutated, "utf8");
398
+
399
+ const out = sh("npm run test:e2e 2>&1; echo E2E_EXIT=$?; npm run test:trigger 2>&1; echo TR_EXIT=$?; npm run test:concurrency 2>&1; echo CONC_EXIT=$?; npm run test:scope 2>&1; echo SCOPE_EXIT=$?; npm run test:audit 2>&1; echo AUDIT_EXIT=$?", workDir, {
400
+ CARLOS_FLOW_HOME: join(tmpdir(), "carlos-flow-mutant-home", m.id.replace(/[^A-Za-z0-9]/g, "_")),
401
+ // 每个变异体用**独立端口**:否则会复用上一轮副本拉起的旧 bridge,
402
+ // 旧 bridge 内存里跑的是旧代码 → 测试结果失真(可能假绿 / 假红)。
403
+ BRIDGE_PORT: String(PORT_BASE + mi),
404
+ });
405
+ const e2eFail = /════ 结果: (\d+) 通过 \/ (\d+) 失败/.exec(out);
406
+ const trFail = /总测试数: (\d+)[\s\S]*?✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(out);
407
+ const concFail = /✅ 通过: (\d+)[\s\S]*?❌ 失败: (\d+)/.exec(out.slice(out.lastIndexOf("跨进程并发")));
408
+
409
+ // fail-closed:套件**没跑起来**(输出里取不到结果)时,既不能算「被抓住」,
410
+ // 也不能算「逃逸」——那是测试没运行,不是退化蒙混过关。
411
+ // 高负载下并发套件偶尔启动失败,若当成 0 失败就会把变异体误判成 NOT CAUGHT(假红且误导)。
412
+ const unparsed = [];
413
+ if (!e2eFail) unparsed.push("e2e");
414
+ if (!trFail) unparsed.push("trigger");
415
+ if (!concFail) unparsed.push("concurrency");
416
+ const scopeOut = out.slice(out.lastIndexOf("越界拦截真实验证"));
417
+ const auditOut = out.slice(out.lastIndexOf("审计链篡改检测验证"));
418
+ if (!/════ 结果: \d+ 通过 \/ \d+ 失败/.test(scopeOut)) unparsed.push("scope");
419
+ if (!/════ 结果: \d+ 通过 \/ \d+ 失败/.test(auditOut)) unparsed.push("audit");
420
+ if (unparsed.length) {
421
+ lines.push(`## ${m.id}`);
422
+ lines.push("");
423
+ lines.push(`- 注入:${m.desc}`);
424
+ lines.push(`- 文件:\`${m.file}\``);
425
+ lines.push("- 结果:**套件未成功运行(结果不可信)**");
426
+ lines.push(`- 详情:无法解析结果的套件:${unparsed.join("、")}`);
427
+ lines.push("");
428
+ writeFileSync(REPORT, lines.join("\n") + "\n", "utf8");
429
+ console.error(`✗ 变异体 ${m.id}:套件输出无法解析(${unparsed.join("、")})—— 测试没有真正运行,结果不可信。`);
430
+ console.error(" (不是「变异体逃逸」,而是套件没跑起来;请在低负载下重跑,或看 mutation-run.log)");
431
+ console.error(" 报告已写入: " + REPORT);
432
+ process.exit(1);
433
+ }
434
+
435
+ const caught =
436
+ (e2eFail && Number(e2eFail[2]) > 0) ||
437
+ (trFail && Number(trFail[3]) > 0) ||
438
+ (concFail && Number(concFail[2]) > 0) ||
439
+ /SCOPE_EXIT=[1-9]/.test(out) ||
440
+ /AUDIT_EXIT=[1-9]/.test(out);
441
+ const verdict = caught ? "CAUGHT ✅" : "NOT CAUGHT ❌";
442
+ results.push({
443
+ ...m,
444
+ verdict,
445
+ detail: `e2e ${e2eFail ? e2eFail[1] + "/" + e2eFail[2] : "?"} 失败, trigger ${trFail ? trFail[3] : "?"} 失败, concurrency ${concFail ? concFail[2] : "?"} 失败`,
446
+ });
447
+
448
+ lines.push(`## ${m.id}`);
449
+ lines.push("");
450
+ lines.push(`- 注入:${m.desc}`);
451
+ lines.push(`- 文件:\`${m.file}\``);
452
+ lines.push(`- 结果:**${verdict}**`);
453
+ lines.push(`- 详情:e2e ${e2eFail ? e2eFail[1] + " 通过 / " + e2eFail[2] + " 失败" : "?"};trigger ${trFail ? trFail[2] + " 通过 / " + trFail[3] + " 失败" : "?"};concurrency ${concFail ? concFail[1] + " 通过 / " + concFail[2] + " 失败" : "?"}`);
454
+ lines.push("");
455
+ const failed = (out.match(/✗[^\n]*/g) || []).slice(0, 6);
456
+ if (failed.length) {
457
+ lines.push("失败用例:");
458
+ for (const f of failed) lines.push("```\n" + f.trim() + "\n```");
459
+ lines.push("");
460
+ }
461
+
462
+ // 本轮收尾:先终止本轮拉起的 bridge,再删工作目录(否则 Windows 上删不掉 → EBUSY)
463
+ cleanupBridges(workDir, SRC_BRIDGES);
464
+ rmDirSafe(workDir);
465
+ }
466
+
467
+ cleanupBridges(workDir, SRC_BRIDGES);
468
+ rmDirSafe(workDir);
469
+
470
+ lines.unshift("## 总览");
471
+ lines.unshift("");
472
+ lines.push("| 变异 | 结果 | 详情 |");
473
+ lines.push("|---|---|---|");
474
+ for (const r of results) {
475
+ lines.push(`| ${r.id} | ${r.verdict} | ${r.detail} |`);
476
+ }
477
+ lines.push("");
478
+
479
+ writeFileSync(REPORT, lines.join("\n"), "utf8");
480
+ console.log(lines.slice(0, 40).join("\n"));
481
+ console.log("\n报告已写入:", REPORT);
482
+ const notCaught = results.filter((r) => r.verdict !== "CAUGHT ✅");
483
+ process.exit(notCaught.length === 0 ? 0 : 1);