flow-comet 1.6.3 → 1.6.4
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/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 +6 -6
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +67 -18878
- package/.flow-comet/skills/flow-comet/scripts/health.mjs +1 -1
- package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +1030 -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 +815 -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 +614 -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-evidence-gates.mjs +553 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +837 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +404 -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 +302 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +133 -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 +141 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +700 -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 +106 -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 +189 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +1441 -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 +1228 -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 +2484 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +2 -2
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +72 -1123
- package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +966 -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 +1388 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +1466 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +1364 -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 +1484 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +971 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +1 -1
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +58 -7559
- package/.flow-comet/skills/flow-comet/scripts/time-utils.mjs +4 -4
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +151 -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-plan/GUIDANCE.md +1 -1
- 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 +119 -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,1427 @@
|
|
|
1
|
+
// items/k.mjs — 条目分区:K 组条目(安装器与平台)
|
|
2
|
+
//
|
|
3
|
+
// 数据层约定:同 items/a.mjs 头注(只承载 ENTRIES;夹具从 lib/system-fixtures.mjs 取;组文件之间零 import)。
|
|
4
|
+
// 组界:K 组尾部三项(状态迁移 / 分发面 / 契约核对)落在 items/l.mjs 头部——见 items/a.mjs 头注。
|
|
5
|
+
// 分发面标记契约的实现(标记表 / 三态映射 / 条目录入 / 运行器校验)在 `lib/system-fixtures.mjs`;
|
|
6
|
+
// 套件运行器的取用面(`from './items/k.mjs'`)保持不变——本文件按转出枢纽转出这三个符号,
|
|
7
|
+
// 不是第二份实现。
|
|
8
|
+
|
|
9
|
+
import fs from 'fs';
|
|
10
|
+
import path from 'path';
|
|
11
|
+
import os from 'os';
|
|
12
|
+
import { execFileSync, spawnSync } from 'child_process';
|
|
13
|
+
import { pathToFileURL } from 'url';
|
|
14
|
+
import { assertExit, assertNotOut, assertOut, baseState, copyTreePortable, runHook, runState, writeFile, writeState } from '../lib/test-helpers.mjs';
|
|
15
|
+
import { SCRIPTS_DIR, collectTreeFiles, resolvePackageContext, K14_COPY_DETAIL, copyDistributionOutcome, assertDistributionOutcomeContract, readBridgeSourceVersion, stampBridgeLoader, assertInstalledCopyBridgeVersionSemantics, seedForeignFlowKit, isManagedHookCommand, hasProjectRootRef, assertManagedHookEntry, collectManagedHookCommands, CHANGE_ID, gitInit, DISTRIBUTION_MARKERS, applyDistributionSurface } from '../lib/system-fixtures.mjs';
|
|
16
|
+
|
|
17
|
+
const RAW_ENTRIES = [
|
|
18
|
+
// ---------- K. 安装器 ----------
|
|
19
|
+
|
|
20
|
+
{
|
|
21
|
+
name: 'K1 安装器:版本标识由安装器生成且精确反映源仓库状态',
|
|
22
|
+
surface: '版本标识解析(包内直跑 + git describe 信任边界)仅权威源侧可判',
|
|
23
|
+
run: (dir) => {
|
|
24
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
25
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
26
|
+
if (!fs.existsSync(installer)) throw new Error('缺少 prepare-env.mjs');
|
|
27
|
+
const target = path.join(dir, 'j3-target');
|
|
28
|
+
fs.mkdirSync(target, { recursive: true });
|
|
29
|
+
const res = spawnSync(process.execPath, [installer, '--target', target], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
30
|
+
if (res.status !== 0) throw new Error('prepare-env 失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
31
|
+
// 分发主干:权威源随技能包分发的静态文件(手动复制也有效,不依赖安装脚本)
|
|
32
|
+
const srcVersionFile = path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
33
|
+
if (!fs.existsSync(srcVersionFile)) throw new Error('缺少权威源 INSTALLED_VERSION(随技能包分发)');
|
|
34
|
+
const srcVersion = fs.readFileSync(srcVersionFile, 'utf8').trim();
|
|
35
|
+
if (!srcVersion) throw new Error('权威源版本标识为空');
|
|
36
|
+
// 权威源须与 CHANGELOG 首个版本段一致(发布批次更新;CI release-consistency 同规则);
|
|
37
|
+
// 版本段可带 prerelease 后缀(如 -rc.1 / -beta-1)
|
|
38
|
+
const changelog = fs.readFileSync(path.join(repoRoot, 'CHANGELOG.md'), 'utf8');
|
|
39
|
+
const m = changelog.match(/^## \[(\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?)\]/m);
|
|
40
|
+
const expected = m ? m[1] : 'unreleased';
|
|
41
|
+
if (srcVersion !== expected) throw new Error('权威源版本标识不符: ' + srcVersion + ' ≠ CHANGELOG 版本 ' + expected);
|
|
42
|
+
const versionFile = path.join(target, '.claude', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
43
|
+
if (!fs.existsSync(versionFile)) throw new Error('缺少 INSTALLED_VERSION(版本标识文件)');
|
|
44
|
+
const installed = fs.readFileSync(versionFile, 'utf8').trim();
|
|
45
|
+
if (!installed) throw new Error('版本标识为空');
|
|
46
|
+
// 格式:发布版本号(1.3.1) / prerelease(1.5.0-rc.1 或 1.5.0-beta-1) / git describe 开发态(1.3.1-N-g<hash>) / unreleased(无 tag 兜底)
|
|
47
|
+
const ok = /^\d+\.\d+\.\d+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(-\d+-g[0-9a-f]+)?$/.test(installed) || installed === 'unreleased';
|
|
48
|
+
if (!ok) throw new Error('版本标识格式异常: ' + installed);
|
|
49
|
+
// 信任边界:安装器目录**不是仓库根**时(包被装在别人的仓库里——本地安装进项目即是此形态),
|
|
50
|
+
// 不得采信 git describe:它会向上找到宿主仓库,把**宿主 tag** 当成流工具版本。
|
|
51
|
+
// 该解析值是单一来源——`--version` 输出它,`init` 写进目标项目的版本标识也是它,
|
|
52
|
+
// 故此处断言它即覆盖两条路径。修复前实测:输出宿主 tag(9.9.9),且 init 把它写进项目。
|
|
53
|
+
const host = path.join(dir, 'trust-host');
|
|
54
|
+
const pkg = path.join(host, 'node_modules', 'flow-comet');
|
|
55
|
+
fs.mkdirSync(path.join(pkg, '.flow-comet', 'skills', 'flow-comet'), { recursive: true });
|
|
56
|
+
// 假包载体**按真实包形态落位**:scripts/ 面照 `package.json` 的 `files` 白名单搬运
|
|
57
|
+
// (目录级条目递归),不照仓库 scripts/ 目录整搬。载体比真实包更宽时,「模块掉出白名单」
|
|
58
|
+
// 这类缺口在载体里不会 ERR_MODULE_NOT_FOUND——包内直跑照样成功,断言静默失明。
|
|
59
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
|
|
60
|
+
const packedScripts = (manifest.files || []).filter((e) => typeof e === 'string' && e.startsWith('scripts/'));
|
|
61
|
+
if (packedScripts.length === 0) {
|
|
62
|
+
throw new Error('package.json 的 files 白名单未声明任何 scripts/ 条目(包内直跑无从构造)');
|
|
63
|
+
}
|
|
64
|
+
for (const rel of packedScripts) {
|
|
65
|
+
const from = path.join(repoRoot, ...rel.split('/'));
|
|
66
|
+
const to = path.join(pkg, ...rel.split('/'));
|
|
67
|
+
if (!fs.existsSync(from)) throw new Error('files 白名单声明的路径不在场: ' + rel);
|
|
68
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
69
|
+
if (fs.statSync(from).isDirectory()) copyTreePortable(from, to);
|
|
70
|
+
else fs.copyFileSync(from, to);
|
|
71
|
+
}
|
|
72
|
+
fs.copyFileSync(srcVersionFile, path.join(pkg, '.flow-comet', 'skills', 'flow-comet', 'INSTALLED_VERSION'));
|
|
73
|
+
execFileSync('git', ['init', '-q'], { cwd: host, stdio: 'ignore' });
|
|
74
|
+
execFileSync('git', ['-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-q', '--allow-empty', '-m', 'init'], { cwd: host, stdio: 'ignore' });
|
|
75
|
+
execFileSync('git', ['tag', 'v9.9.9'], { cwd: host, stdio: 'ignore' });
|
|
76
|
+
const nested = spawnSync(process.execPath, [path.join(pkg, 'scripts', 'prepare-env.mjs'), '--version'], { cwd: host, encoding: 'utf8', timeout: 60000 });
|
|
77
|
+
const printed = String(nested.stdout || '').trim();
|
|
78
|
+
if (nested.status !== 0) throw new Error('包内直跑 --version 应退 0,实际 ' + nested.status + '\n' + nested.stderr);
|
|
79
|
+
if (printed === '9.9.9' || printed.startsWith('9.9.9')) {
|
|
80
|
+
throw new Error('版本解析越过了信任边界:安装器目录不是仓库根,却采信了宿主仓库的 tag(输出 ' + printed + ')');
|
|
81
|
+
}
|
|
82
|
+
if (printed !== srcVersion) {
|
|
83
|
+
throw new Error('越界情形应回退随包分发的权威源标识 ' + srcVersion + ',实际 ' + printed);
|
|
84
|
+
}
|
|
85
|
+
if (/fatal:/.test(String(nested.stderr || ''))) {
|
|
86
|
+
throw new Error('版本解析不应把 git 的 fatal 输出漏给用户:\n' + nested.stderr);
|
|
87
|
+
}
|
|
88
|
+
console.log(' 版本标识 = ' + installed + '(git describe 或权威源兜底——多人协作精确检测)✓');
|
|
89
|
+
console.log(' 信任边界: 包装在他人仓库内 → 取随包标识 ' + srcVersion + '(不采信宿主 tag)✓');
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
// ---------- K 扩展:多平台安装器框架 ----------
|
|
94
|
+
|
|
95
|
+
// K2: codex 平台安装冒烟——技能落 .agents/skills/ + SKILL 路径平台化替换 + .codex/hooks.json 注入
|
|
96
|
+
// (含 --platform codex 托管命令)+ AGENTS.md 托管区内联 + 纯 codex 不生成 .claude/ + 版本标识
|
|
97
|
+
{
|
|
98
|
+
name: 'K2 安装器:codex 平台安装(技能/路径替换/hooks.json/AGENTS 托管区)',
|
|
99
|
+
surface: 'codex 平台安装产物断言仅权威源侧可判(副本内无安装器源码面)',
|
|
100
|
+
run: (dir) => {
|
|
101
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
102
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
103
|
+
const target = path.join(dir, 'k2-target');
|
|
104
|
+
fs.mkdirSync(target, { recursive: true });
|
|
105
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
106
|
+
if (res.status !== 0) throw new Error('prepare-env --platform codex 失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
107
|
+
// ① 技能落 .agents/skills/(Codex 自动发现位置)
|
|
108
|
+
const skillMd = path.join(target, '.agents', 'skills', 'flow-comet', 'SKILL.md');
|
|
109
|
+
if (!fs.existsSync(skillMd)) throw new Error('codex 平台技能未安装到 .agents/skills/');
|
|
110
|
+
// ② SKILL 路径平台化替换生效(命令路径 .claude/skills → .agents/skills)
|
|
111
|
+
const text = fs.readFileSync(skillMd, 'utf8');
|
|
112
|
+
if (!text.includes('.agents/skills/flow-comet/scripts/')) throw new Error('SKILL 命令路径未替换为 .agents/skills/');
|
|
113
|
+
if (text.includes('.claude/skills/flow-comet/scripts/')) throw new Error('SKILL 仍含 .claude/skills/flow-comet/scripts/ 命令路径');
|
|
114
|
+
// ③ .codex/hooks.json 注入托管 hook(顶层 hooks 包裹层 + matcher * + 平台标记)
|
|
115
|
+
const hooksFile = path.join(target, '.codex', 'hooks.json');
|
|
116
|
+
if (!fs.existsSync(hooksFile)) throw new Error('缺少 .codex/hooks.json');
|
|
117
|
+
const hooksParsed = JSON.parse(fs.readFileSync(hooksFile, 'utf8'));
|
|
118
|
+
const preToolUse = hooksParsed.hooks && hooksParsed.hooks.PreToolUse;
|
|
119
|
+
if (!Array.isArray(preToolUse) || preToolUse.length === 0) throw new Error('hooks.json 缺 hooks.PreToolUse 包裹层');
|
|
120
|
+
// 托管条目断言形态无关化(断言放宽决策——意图为托管条目在场且指向本项目脚本):脚本 basename 在位 + 项目根引用特征二选一——
|
|
121
|
+
// 兼容新旧两种托管形态,注入命令实测微调(%VAR% 引号/变量词形)不再破坏断言
|
|
122
|
+
const managedCmds = collectManagedHookCommands(hooksParsed);
|
|
123
|
+
if (managedCmds.length === 0) throw new Error('hooks.json 未注入托管 hook 命令');
|
|
124
|
+
for (const c of managedCmds) assertManagedHookEntry(c, 'codex hooks.json 托管命令');
|
|
125
|
+
if (!JSON.stringify(hooksParsed).includes('--platform codex')) throw new Error('hooks.json hook 命令缺平台标记');
|
|
126
|
+
if (preToolUse[0].matcher !== '*') throw new Error('hooks.json matcher 应为 * (Codex PreToolUse 只拦截 Bash 工具): ' + JSON.stringify(preToolUse[0]));
|
|
127
|
+
// ③b .codex/config.toml hooks 启用(features——Codex hooks 默认关闭)
|
|
128
|
+
const configFile = path.join(target, '.codex', 'config.toml');
|
|
129
|
+
if (!fs.existsSync(configFile)) throw new Error('缺少 .codex/config.toml');
|
|
130
|
+
const configText = fs.readFileSync(configFile, 'utf8');
|
|
131
|
+
if (!/\[features\]/.test(configText) || !/hooks\s*=\s*true/.test(configText)) throw new Error('config.toml 未启用 hooks(features)');
|
|
132
|
+
// ④ AGENTS.md 托管区(内联 orchestration 全文——Codex 指令唯一自动加载路径)
|
|
133
|
+
const agentsFile = path.join(target, 'AGENTS.md');
|
|
134
|
+
if (!fs.existsSync(agentsFile)) throw new Error('缺少 AGENTS.md');
|
|
135
|
+
const agents = fs.readFileSync(agentsFile, 'utf8');
|
|
136
|
+
if (!agents.includes('Managed by flow-comet')) throw new Error('AGENTS.md 无托管标记');
|
|
137
|
+
if (!agents.includes('flow-comet Orchestration')) throw new Error('AGENTS.md 未内联 orchestration 内容');
|
|
138
|
+
// ④b 分发词面锁:orchestration 是**每个项目自动加载**的文本(CC 走 .claude/rules/、Codex/dsh 走
|
|
139
|
+
// AGENTS.md 托管区,三者同源),其用词必须与工具当下的自称一致——不得残留 Comet 时代词汇
|
|
140
|
+
// (本仓早已移除 bundles/ 双目录、权威源路径也从 bundle-drafts/ 迁走),也不得把**只有两行的
|
|
141
|
+
// 兼容别名壳**列进脚本清单(它不是任何流程的入口,列出来只会让读者去找错文件)。
|
|
142
|
+
for (const banned of ['Bundle', 'control plane']) {
|
|
143
|
+
if (agents.includes(banned)) throw new Error('内联的 orchestration 含 Comet 时代词汇(' + banned + ')——分发文本须与工具当下的自称一致');
|
|
144
|
+
}
|
|
145
|
+
if (agents.includes('comet-plan.mjs')) throw new Error('内联的 orchestration 仍列兼容别名壳 comet-plan.mjs(它不是流程入口)');
|
|
146
|
+
// ⑤ 纯 codex 平台不生成 .claude/
|
|
147
|
+
if (fs.existsSync(path.join(target, '.claude'))) throw new Error('codex 平台不应生成 .claude/');
|
|
148
|
+
// ⑥ 版本标识随技能包分发(平台化路径)
|
|
149
|
+
const verFile = path.join(target, '.agents', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
150
|
+
if (!fs.existsSync(verFile)) throw new Error('缺少 INSTALLED_VERSION(版本标识文件)');
|
|
151
|
+
const installed = fs.readFileSync(verFile, 'utf8').trim();
|
|
152
|
+
if (!installed) throw new Error('版本标识为空');
|
|
153
|
+
// ⑦ .codex/hooks.json 非法 JSON → fail-safe(不覆盖用户配置,报错退出)
|
|
154
|
+
const badTarget = path.join(dir, 'k2-bad-hooks');
|
|
155
|
+
fs.mkdirSync(path.join(badTarget, '.codex'), { recursive: true });
|
|
156
|
+
fs.writeFileSync(path.join(badTarget, '.codex', 'hooks.json'), '{ not json', 'utf8');
|
|
157
|
+
const badRes = spawnSync(process.execPath, [installer, '--target', badTarget, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
158
|
+
if (badRes.status === 0) throw new Error('非法 hooks.json 应导致安装失败(fail-safe)');
|
|
159
|
+
const badOut = String(badRes.stderr || '') + String(badRes.stdout || '');
|
|
160
|
+
if (!badOut.includes('已存在但内容非法')) throw new Error('非法 hooks.json 应输出 fail-safe 提示: ' + badOut);
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
// K3: hook 平台分支输出契约——codex 分支 stdout 为合法 JSON(拦截含 decision:"block";放行可解析非 block);
|
|
165
|
+
// claude-code 分支(无平台标记)输出与现状一致(文本 workflow-hook-guard-ok)
|
|
166
|
+
{
|
|
167
|
+
name: 'K3 hook:codex 平台分支 JSON 契约(拦截/放行)+ CC 分支不变',
|
|
168
|
+
run: (dir) => {
|
|
169
|
+
// ① codex 分支:写越权路径(源码)→ stdout 合法 JSON + decision block
|
|
170
|
+
const st = baseState('open');
|
|
171
|
+
st.status = 'running';
|
|
172
|
+
writeState(dir, st);
|
|
173
|
+
fs.mkdirSync(path.join(dir, '.specs', CHANGE_ID), { recursive: true });
|
|
174
|
+
const block = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
175
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'evil.py') } });
|
|
176
|
+
assertExit(block, 0);
|
|
177
|
+
const blockParsed = JSON.parse(block.output.trim()); // stdout 非 JSON → throw(RED)
|
|
178
|
+
if (blockParsed.decision !== 'block') throw new Error('codex 拦截输出应含 decision:"block": ' + JSON.stringify(blockParsed));
|
|
179
|
+
// ② codex 分支:写 .specs/ 工件 → 放行(输出可解析、非 block)
|
|
180
|
+
const ok = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
181
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'CHANGE.md') } });
|
|
182
|
+
assertExit(ok, 0);
|
|
183
|
+
const okParsed = JSON.parse(ok.output.trim()); // 放行输出须为合法 JSON(空串解析失败 → RED)
|
|
184
|
+
if (okParsed.decision === 'block') throw new Error('codex 放行输出不应含 decision block');
|
|
185
|
+
// ③ codex 分支:无活跃 workflow → 放行(JSON 可解析)
|
|
186
|
+
fs.rmSync(path.join(dir, '.flow-comet'), { recursive: true, force: true });
|
|
187
|
+
const noActive = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
188
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, 'src', 'x.py') } });
|
|
189
|
+
assertExit(noActive, 0);
|
|
190
|
+
JSON.parse(noActive.output.trim());
|
|
191
|
+
// ④ claude-code 分支(无平台标记):输出与现状一致(文本 workflow-hook-guard-ok)
|
|
192
|
+
writeState(dir, { ...baseState('open'), status: 'running' });
|
|
193
|
+
const cc = runHook(['before_tool'], dir,
|
|
194
|
+
{ tool_name: 'Write', tool_input: { file_path: path.join(dir, '.specs', CHANGE_ID, 'CHANGE.md') } });
|
|
195
|
+
assertExit(cc, 0);
|
|
196
|
+
assertOut(cc, 'workflow-hook-guard-ok');
|
|
197
|
+
assertNotOut(cc, 'decision');
|
|
198
|
+
// ⑤ codex 分支:Bash 写入检测(PowerShell 命令字符串——Codex 写路径经 Bash 工具,无 file_path)
|
|
199
|
+
const bashBlock = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
200
|
+
{ tool_name: 'Bash', tool_input: { command: 'Set-Content -LiteralPath "src/evil.py" -Value "x=1"' } });
|
|
201
|
+
assertExit(bashBlock, 0);
|
|
202
|
+
const bashBlockParsed = JSON.parse(bashBlock.output.trim());
|
|
203
|
+
if (bashBlockParsed.decision !== 'block') throw new Error('codex Bash 越权写入应输出 decision block: ' + JSON.stringify(bashBlockParsed));
|
|
204
|
+
// ⑥ codex 分支:Bash 合法写 .specs → 放行
|
|
205
|
+
const bashOk = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
206
|
+
{ tool_name: 'Bash', tool_input: { command: 'Set-Content -LiteralPath ".specs/ch/CHANGE.md" -Value "# CHANGE"' } });
|
|
207
|
+
assertExit(bashOk, 0);
|
|
208
|
+
const bashOkParsed = JSON.parse(bashOk.output.trim());
|
|
209
|
+
if (bashOkParsed.decision === 'block') throw new Error('codex Bash 合法写入不应 block');
|
|
210
|
+
// ⑦ codex 分支:Bash 无写入模式(纯命令)→ 放行
|
|
211
|
+
const bashNone = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
212
|
+
{ tool_name: 'Bash', tool_input: { command: 'python -m pytest test_calculator.py -q' } });
|
|
213
|
+
assertExit(bashNone, 0);
|
|
214
|
+
JSON.parse(bashNone.output.trim());
|
|
215
|
+
// ⑧ codex 分支:读类 cmdlet(Get-Content -Path)→ 无写入语义,放行
|
|
216
|
+
const bashRead = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
217
|
+
{ tool_name: 'Bash', tool_input: { command: 'Get-Content -Path "src/readme.md"' } });
|
|
218
|
+
assertExit(bashRead, 0);
|
|
219
|
+
const bashReadParsed = JSON.parse(bashRead.output.trim());
|
|
220
|
+
if (bashReadParsed.decision === 'block') throw new Error('codex 读类 cmdlet(Get-Content)不应 block');
|
|
221
|
+
// ⑨ codex 分支:.NET File.Copy 目标 = 第二参数(越权目标 → block;合法目标 → 放行)
|
|
222
|
+
const copyBlock = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
223
|
+
{ tool_name: 'Bash', tool_input: { command: '[System.IO.File]::Copy("src/a.txt", "src/b.txt")' } });
|
|
224
|
+
assertExit(copyBlock, 0);
|
|
225
|
+
const copyParsed = JSON.parse(copyBlock.output.trim());
|
|
226
|
+
if (copyParsed.decision !== 'block') throw new Error('codex File.Copy 越权目标(第二参数)应 block');
|
|
227
|
+
const copyOk = runHook(['before_tool', '--platform', 'codex'], dir,
|
|
228
|
+
{ tool_name: 'Bash', tool_input: { command: '[System.IO.File]::Copy("src/a.txt", ".specs/ch/b.txt")' } });
|
|
229
|
+
assertExit(copyOk, 0);
|
|
230
|
+
const copyOkParsed = JSON.parse(copyOk.output.trim());
|
|
231
|
+
if (copyOkParsed.decision === 'block') throw new Error('codex File.Copy 合法目标不应 block');
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
|
|
235
|
+
// K4: 平台选择链——--platform 显式(两平台互不串扰)/ 无 TTY 探测(项目已有 .codex/ 或 .claude/)/ 两者皆无默认
|
|
236
|
+
{
|
|
237
|
+
name: 'K4 安装器:平台选择链(显式/无 TTY 探测/默认)',
|
|
238
|
+
surface: '平台选择链断言仅权威源侧可判(需真实安装器与探测环境)',
|
|
239
|
+
run: (dir) => {
|
|
240
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
241
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
242
|
+
const run = (target, envOverrides = {}) => spawnSync(process.execPath, [installer, '--target', target], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, ...envOverrides } });
|
|
243
|
+
// ① --platform claude-code 显式 → .claude/skills 生成,.agents 不生成
|
|
244
|
+
const t1 = path.join(dir, 'k4-cc');
|
|
245
|
+
fs.mkdirSync(t1, { recursive: true });
|
|
246
|
+
const r1 = spawnSync(process.execPath, [installer, '--target', t1, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
247
|
+
if (r1.status !== 0) throw new Error('--platform claude-code 失败: ' + (r1.stderr || JSON.stringify(r1.output)));
|
|
248
|
+
if (!fs.existsSync(path.join(t1, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('claude-code 平台未生成 .claude/skills/');
|
|
249
|
+
if (fs.existsSync(path.join(t1, '.agents'))) throw new Error('claude-code 平台不应生成 .agents/');
|
|
250
|
+
// ② --platform codex 显式 → .agents/skills 生成,.claude 不生成
|
|
251
|
+
const t2 = path.join(dir, 'k4-codex');
|
|
252
|
+
fs.mkdirSync(t2, { recursive: true });
|
|
253
|
+
const r2 = spawnSync(process.execPath, [installer, '--target', t2, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
254
|
+
if (r2.status !== 0) throw new Error('--platform codex 失败: ' + (r2.stderr || JSON.stringify(r2.output)));
|
|
255
|
+
if (!fs.existsSync(path.join(t2, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('codex 平台未生成 .agents/skills/');
|
|
256
|
+
if (fs.existsSync(path.join(t2, '.claude'))) throw new Error('codex 平台不应生成 .claude/');
|
|
257
|
+
// ③ 无 --platform(spawnSync 无 TTY)+ 目标已有 .codex/ → 探测为 codex
|
|
258
|
+
const t3 = path.join(dir, 'k4-probe-codex');
|
|
259
|
+
fs.mkdirSync(path.join(t3, '.codex'), { recursive: true });
|
|
260
|
+
const r3 = run(t3);
|
|
261
|
+
if (r3.status !== 0) throw new Error('探测 codex 安装失败: ' + (r3.stderr || JSON.stringify(r3.output)));
|
|
262
|
+
if (!fs.existsSync(path.join(t3, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('已有 .codex/ 的项目应探测为 codex 平台');
|
|
263
|
+
// ④ 无 --platform + 目标已有 .claude/ → 探测为 claude-code
|
|
264
|
+
const t4 = path.join(dir, 'k4-probe-cc');
|
|
265
|
+
fs.mkdirSync(path.join(t4, '.claude'), { recursive: true });
|
|
266
|
+
const r4 = run(t4);
|
|
267
|
+
if (r4.status !== 0) throw new Error('探测 claude-code 安装失败: ' + (r4.stderr || JSON.stringify(r4.output)));
|
|
268
|
+
if (!fs.existsSync(path.join(t4, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('已有 .claude/ 的项目应探测为 claude-code 平台');
|
|
269
|
+
if (fs.existsSync(path.join(t4, '.agents'))) throw new Error('探测为 claude-code 时不应生成 .agents/');
|
|
270
|
+
// ⑤ 无 --platform + 两者皆无 → 默认 claude-code
|
|
271
|
+
const t5 = path.join(dir, 'k4-default');
|
|
272
|
+
fs.mkdirSync(t5, { recursive: true });
|
|
273
|
+
const r5 = run(t5);
|
|
274
|
+
if (r5.status !== 0) throw new Error('默认平台安装失败: ' + (r5.stderr || JSON.stringify(r5.output)));
|
|
275
|
+
if (!fs.existsSync(path.join(t5, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('两者皆无应默认 claude-code');
|
|
276
|
+
// ⑥ --platform 未知平台 → 报错拒绝(不静默回退)
|
|
277
|
+
const t6 = path.join(dir, 'k4-unknown');
|
|
278
|
+
fs.mkdirSync(t6, { recursive: true });
|
|
279
|
+
const r6 = spawnSync(process.execPath, [installer, '--target', t6, '--platform', 'bogus'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
280
|
+
if (r6.status === 0) throw new Error('未知平台应报错拒绝');
|
|
281
|
+
const out6 = String(r6.stderr || '') + String(r6.stdout || '');
|
|
282
|
+
if (!out6.includes('未知平台')) throw new Error('未知平台错误信息应含"未知平台": ' + out6);
|
|
283
|
+
// ⑦ 无 --platform + 目标同时有 .claude/ 与 .codex/ 痕迹 → 不武断二选一:
|
|
284
|
+
// 默认 claude-code(主平台)+ 输出多痕迹提示(修复前探测 .codex/ 优先会武断只装 codex)
|
|
285
|
+
const t7 = path.join(dir, 'k4-dual-trace');
|
|
286
|
+
fs.mkdirSync(path.join(t7, '.claude'), { recursive: true });
|
|
287
|
+
fs.mkdirSync(path.join(t7, '.codex'), { recursive: true });
|
|
288
|
+
const r7 = run(t7);
|
|
289
|
+
if (r7.status !== 0) throw new Error('双痕迹项目安装失败: ' + (r7.stderr || JSON.stringify(r7.output)));
|
|
290
|
+
if (!fs.existsSync(path.join(t7, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) {
|
|
291
|
+
throw new Error('双痕迹项目应默认安装 claude-code(主平台),而非武断只装 codex');
|
|
292
|
+
}
|
|
293
|
+
if (fs.existsSync(path.join(t7, '.agents'))) throw new Error('双痕迹项目默认 claude-code 时不应生成 .agents/');
|
|
294
|
+
const out7 = String(r7.stdout || '') + String(r7.stderr || '');
|
|
295
|
+
if (!out7.includes('同时有 .claude/、.codex/ 或 .dsh/ 中的多个痕迹')) throw new Error('多痕迹无 TTY 应输出多痕迹提示: ' + out7);
|
|
296
|
+
// ⑧ TTY 多选(交互模拟——平台选择链缺省路径):覆盖 stdin.isTTY + 管道喂入 '1,3'
|
|
297
|
+
// (Claude Code + dsh——三平台选项,旧 both 已移除)→ 双平台产物同时生成;
|
|
298
|
+
// dsh 副本命令路径平台化(.dsh 形态,不含 .claude 路径);DSH_HOME 指向临时目录
|
|
299
|
+
// (断言全程不写真实 ~/.dsh)
|
|
300
|
+
const t8 = path.join(dir, 'k4-multiselect');
|
|
301
|
+
fs.mkdirSync(t8, { recursive: true });
|
|
302
|
+
const dshHome8 = path.join(dir, 'k4-dsh-home');
|
|
303
|
+
const installerUrl = pathToFileURL(installer).href;
|
|
304
|
+
const selectScript =
|
|
305
|
+
`Object.defineProperty(process.stdin, 'isTTY', { value: true, configurable: true });` +
|
|
306
|
+
`process.argv = ['node', 'prepare-env', '--target', ${JSON.stringify(t8)}];` +
|
|
307
|
+
`await import(${JSON.stringify(installerUrl)});`;
|
|
308
|
+
const r8 = spawnSync(process.execPath, ['--input-type=module', '-e', selectScript], { cwd: repoRoot, encoding: 'utf8', input: '1,3\n', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome8 } });
|
|
309
|
+
if (r8.status !== 0) throw new Error('TTY 多选安装失败: ' + (r8.stderr || JSON.stringify(r8.output)));
|
|
310
|
+
if (!fs.existsSync(path.join(t8, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('TTY 多选后 .claude/skills 应生成');
|
|
311
|
+
if (!fs.existsSync(path.join(t8, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('TTY 多选后 .dsh/skills 应生成');
|
|
312
|
+
if (fs.existsSync(path.join(t8, '.agents'))) throw new Error('TTY 多选(1,3)不应生成 .agents/(未选 codex)');
|
|
313
|
+
const dshSkillText8 = fs.readFileSync(path.join(t8, '.dsh', 'skills', 'flow-comet', 'SKILL.md'), 'utf8');
|
|
314
|
+
if (dshSkillText8.includes('.claude/skills/flow-comet/scripts/')) {
|
|
315
|
+
throw new Error('TTY 多选后 dsh 副本命令路径应为 .dsh 形态');
|
|
316
|
+
}
|
|
317
|
+
if (!fs.existsSync(path.join(dshHome8, 'plugins', 'dsh-flow-comet-bridge.mjs'))) {
|
|
318
|
+
throw new Error('TTY 多选后桥接 loader 应复制到临时 DSH_HOME(不写真实 ~/.dsh)');
|
|
319
|
+
}
|
|
320
|
+
// ⑨ 仅 .dsh/ 痕迹 → 无 TTY 探测为 dsh(三痕迹探测语义)
|
|
321
|
+
const t9 = path.join(dir, 'k4-probe-dsh');
|
|
322
|
+
fs.mkdirSync(path.join(t9, '.dsh'), { recursive: true });
|
|
323
|
+
const r9 = run(t9, { DSH_HOME: path.join(dir, 'k4-probe-dsh-home') });
|
|
324
|
+
if (r9.status !== 0) throw new Error('探测 dsh 安装失败: ' + (r9.stderr || JSON.stringify(r9.output)));
|
|
325
|
+
if (!fs.existsSync(path.join(t9, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('已有 .dsh/ 的项目应探测为 dsh 平台');
|
|
326
|
+
// ⑩ 显式逗号多平台:--platform claude-code,dsh → 两平台产物同时生成(CI/无 TTY 路径)
|
|
327
|
+
const t10 = path.join(dir, 'k4-comma');
|
|
328
|
+
fs.mkdirSync(t10, { recursive: true });
|
|
329
|
+
const r10 = spawnSync(process.execPath, [installer, '--target', t10, '--platform', 'claude-code,dsh'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: path.join(dir, 'k4-comma-home') } });
|
|
330
|
+
if (r10.status !== 0) throw new Error('--platform claude-code,dsh 失败: ' + (r10.stderr || JSON.stringify(r10.output)));
|
|
331
|
+
if (!fs.existsSync(path.join(t10, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('逗号多平台应生成 .claude/skills');
|
|
332
|
+
if (!fs.existsSync(path.join(t10, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('逗号多平台应生成 .dsh/skills');
|
|
333
|
+
if (fs.existsSync(path.join(t10, '.agents'))) throw new Error('逗号多平台(claude-code,dsh)不应生成 .agents/');
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
|
|
337
|
+
// K5: purge 语义——缺 --yes 拒绝(不破坏)/ --purge --yes 清理重建 / 用户内容(生成物外)保留
|
|
338
|
+
{
|
|
339
|
+
name: 'K5 安装器:purge 语义(缺 --yes 拒绝/重建/用户内容保留)',
|
|
340
|
+
surface: 'purge 语义断言仅权威源侧可判(需真实安装器命令序列)',
|
|
341
|
+
run: (dir) => {
|
|
342
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
343
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
344
|
+
const run = (target, extra) => spawnSync(process.execPath, [installer, '--target', target, ...extra], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
345
|
+
// ① codex 平台:首次安装 + 用户内容(项目根,非生成物)
|
|
346
|
+
const target = path.join(dir, 'k5-codex');
|
|
347
|
+
fs.mkdirSync(target, { recursive: true });
|
|
348
|
+
const first = run(target, ['--platform', 'codex']);
|
|
349
|
+
if (first.status !== 0) throw new Error('codex 首次安装失败: ' + (first.stderr || JSON.stringify(first.output)));
|
|
350
|
+
fs.writeFileSync(path.join(target, 'USER_KEEP.md'), '# 用户内容(保留)\n', 'utf8');
|
|
351
|
+
// ② 缺 --yes → 拒绝且不破坏(错误提示含 --yes;生成物与用户内容仍在)
|
|
352
|
+
const dry = run(target, ['--platform', 'codex', '--purge']);
|
|
353
|
+
if (dry.status === 0) throw new Error('--purge 缺 --yes 应失败');
|
|
354
|
+
const dryOut = String(dry.stderr || '') + String(dry.stdout || '');
|
|
355
|
+
if (!dryOut.includes('--yes')) throw new Error('--purge 缺 --yes 错误信息应提示 --yes: ' + dryOut);
|
|
356
|
+
if (!fs.existsSync(path.join(target, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge 拒绝后生成物不应被删除');
|
|
357
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge 拒绝后用户内容不应被删除');
|
|
358
|
+
// ②b 写入用户自有资产(.agents/skills/ 用户技能 + hooks.json 用户条目)——purge 后须保留
|
|
359
|
+
fs.mkdirSync(path.join(target, '.agents', 'skills', 'user-own-skill'), { recursive: true });
|
|
360
|
+
fs.writeFileSync(path.join(target, '.agents', 'skills', 'user-own-skill', 'SKILL.md'), '# user skill\n', 'utf8');
|
|
361
|
+
const userHooksPath = path.join(target, '.codex', 'hooks.json');
|
|
362
|
+
const userHooks = JSON.parse(fs.readFileSync(userHooksPath, 'utf8'));
|
|
363
|
+
userHooks.hooks.PreToolUse.push({ matcher: 'UserOwn', hooks: [{ type: 'command', command: 'node user-own-hook.mjs' }] });
|
|
364
|
+
fs.writeFileSync(userHooksPath, JSON.stringify(userHooks, null, 2) + '\n', 'utf8');
|
|
365
|
+
// ③ --purge --yes → 清理重建(仅 flow-comet 技能,用户资产保留)
|
|
366
|
+
const ok = run(target, ['--platform', 'codex', '--purge', '--yes']);
|
|
367
|
+
if (ok.status !== 0) throw new Error('--purge --yes 失败: ' + (ok.stderr || JSON.stringify(ok.output)));
|
|
368
|
+
if (!fs.existsSync(path.join(target, '.agents', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge --yes 后 flow-comet 技能应重新生成');
|
|
369
|
+
if (!fs.existsSync(path.join(target, '.agents', 'skills', 'user-own-skill', 'SKILL.md'))) throw new Error('--purge --yes 后用户技能应保留(.agents/ 为共享目录)');
|
|
370
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge --yes 后用户内容应保留');
|
|
371
|
+
const hooksText = fs.readFileSync(path.join(target, '.codex', 'hooks.json'), 'utf8');
|
|
372
|
+
if ((hooksText.match(/comet-hook-guard\.mjs/g) || []).length !== 1) throw new Error('--purge --yes 后 hooks.json 应含 1 条重建托管条目: ' + hooksText);
|
|
373
|
+
if (!hooksText.includes('user-own-hook.mjs')) throw new Error('--purge --yes 后用户 hook 条目应保留: ' + hooksText);
|
|
374
|
+
const agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
375
|
+
if (!agents.includes('Managed by flow-comet')) throw new Error('--purge --yes 后 AGENTS.md 托管区应重建');
|
|
376
|
+
// ④ claude-code 平台同样:缺 --yes 拒绝 + --purge --yes 重建 + 用户内容保留
|
|
377
|
+
const tcc = path.join(dir, 'k5-cc');
|
|
378
|
+
fs.mkdirSync(tcc, { recursive: true });
|
|
379
|
+
const ccFirst = run(tcc, []);
|
|
380
|
+
if (ccFirst.status !== 0) throw new Error('claude-code 首次安装失败: ' + (ccFirst.stderr || JSON.stringify(ccFirst.output)));
|
|
381
|
+
fs.writeFileSync(path.join(tcc, 'USER_KEEP.md'), '# 用户内容(保留)\n', 'utf8');
|
|
382
|
+
// ⑤ matcher 演进幂等:把托管组 matcher 改为旧形态(Write|Edit——模拟历史版本注入),
|
|
383
|
+
// 重装(非 purge)后旧托管组应被清理——settings 仅剩当前 matcher 组、无空 hooks 组
|
|
384
|
+
const settingsPath = path.join(tcc, '.claude', 'settings.local.json');
|
|
385
|
+
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
386
|
+
for (const g of settings.hooks.PreToolUse) {
|
|
387
|
+
if (g.matcher === 'Write|Edit|Bash') g.matcher = 'Write|Edit';
|
|
388
|
+
}
|
|
389
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
|
|
390
|
+
const reinstall = run(tcc, []);
|
|
391
|
+
if (reinstall.status !== 0) throw new Error('matcher 演进重装失败: ' + (reinstall.stderr || JSON.stringify(reinstall.output)));
|
|
392
|
+
const after = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
393
|
+
const groups = after.hooks.PreToolUse;
|
|
394
|
+
if (groups.some((g) => Array.isArray(g.hooks) && g.hooks.length === 0)) {
|
|
395
|
+
throw new Error('重装后存在空 hooks 组(matcher 演进残留): ' + JSON.stringify(groups));
|
|
396
|
+
}
|
|
397
|
+
if (!groups.some((g) => g.matcher === 'Write|Edit|Bash' && Array.isArray(g.hooks) && g.hooks.length > 0)) {
|
|
398
|
+
throw new Error('重装后缺当前 matcher 组(Write|Edit|Bash)');
|
|
399
|
+
}
|
|
400
|
+
// ⑥ 历史残留空组清理:手动把当前组 hooks 清空(模拟历史版本残留的空组),
|
|
401
|
+
// 再次重装后空组应被删除,settings 仅剩有效组
|
|
402
|
+
const settings2 = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
403
|
+
for (const g of settings2.hooks.PreToolUse) g.hooks = [];
|
|
404
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings2, null, 2) + '\n', 'utf8');
|
|
405
|
+
const reinstall2 = run(tcc, []);
|
|
406
|
+
if (reinstall2.status !== 0) throw new Error('空组清理重装失败: ' + (reinstall2.stderr || JSON.stringify(reinstall2.output)));
|
|
407
|
+
const after2 = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
|
408
|
+
const groups2 = after2.hooks.PreToolUse;
|
|
409
|
+
if (groups2.some((g) => Array.isArray(g.hooks) && g.hooks.length === 0)) {
|
|
410
|
+
throw new Error('重装后仍存在空 hooks 组(历史残留未清理): ' + JSON.stringify(groups2));
|
|
411
|
+
}
|
|
412
|
+
if (!groups2.some((g) => g.matcher === 'Write|Edit|Bash' && Array.isArray(g.hooks) && g.hooks.length > 0)) {
|
|
413
|
+
throw new Error('空组清理后缺当前 matcher 组(Write|Edit|Bash)');
|
|
414
|
+
}
|
|
415
|
+
const ccDry = run(tcc, ['--purge']);
|
|
416
|
+
if (ccDry.status === 0) throw new Error('claude-code --purge 缺 --yes 应失败');
|
|
417
|
+
if (!fs.existsSync(path.join(tcc, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('claude-code --purge 拒绝后生成物不应被删除');
|
|
418
|
+
if (!fs.existsSync(path.join(tcc, 'USER_KEEP.md'))) throw new Error('claude-code --purge 拒绝后用户内容不应被删除');
|
|
419
|
+
const ccOk = run(tcc, ['--purge', '--yes']);
|
|
420
|
+
if (ccOk.status !== 0) throw new Error('claude-code --purge --yes 失败: ' + (ccOk.stderr || JSON.stringify(ccOk.output)));
|
|
421
|
+
if (!fs.existsSync(path.join(tcc, '.claude', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('claude-code --purge --yes 后技能应重新生成');
|
|
422
|
+
if (!fs.existsSync(path.join(tcc, 'USER_KEEP.md'))) throw new Error('claude-code --purge --yes 后用户内容应保留');
|
|
423
|
+
},
|
|
424
|
+
},
|
|
425
|
+
|
|
426
|
+
// K6: 平台描述符驱动防回归——PLATFORMS 每平台条目含安装/清理函数(installHooks/installRules/
|
|
427
|
+
// purge/overwriteDescription);main 统一调度(源码无平台分支标识——新增平台 = 描述符条目,
|
|
428
|
+
// main 零改动);对源码提取的全部平台逐一真实安装冒烟(未来新增平台自动纳入测试)
|
|
429
|
+
{
|
|
430
|
+
name: 'K6 安装器:平台描述符驱动(全平台安装冒烟 + main 统一调度)',
|
|
431
|
+
surface: '平台描述符与全平台安装冒烟仅权威源侧可判(需安装器模块树)',
|
|
432
|
+
run: (dir) => {
|
|
433
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
434
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
435
|
+
// ① main 统一调度:平台分支标识已消除(新增平台 = 描述符条目,main 零改动)。
|
|
436
|
+
// 受检面 = 入口 + scripts/lib 模块树——平台分支若以同形标识长回模块里,只扫入口会静默失明。
|
|
437
|
+
const libDir = path.join(path.dirname(installer), 'lib');
|
|
438
|
+
const branchSurface = [
|
|
439
|
+
['入口', installer],
|
|
440
|
+
...fs.readdirSync(libDir).filter((f) => f.endsWith('.mjs')).sort()
|
|
441
|
+
.map((f) => ['lib/' + f, path.join(libDir, f)]),
|
|
442
|
+
];
|
|
443
|
+
for (const [label, file] of branchSurface) {
|
|
444
|
+
if (fs.readFileSync(file, 'utf8').includes('isClaudeCode')) {
|
|
445
|
+
throw new Error('安装器 ' + label + ' 仍含平台分支标识 isClaudeCode(应迁入描述符)');
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
// ② 描述符完整性:平台 id 从描述符块提取(防块外误匹配);聚合块须含 4 个平台函数
|
|
449
|
+
// (installHooks/installRules/purge/overwriteDescription——未来新增平台缺任一函数时,
|
|
450
|
+
// 下方逐平台 purge 冒烟会因 platform.purge 未定义而真实失败)
|
|
451
|
+
// 描述符随安装器分模块:按**模块身份**读 lib/installer-platforms.mjs(描述符职责的归属模块,
|
|
452
|
+
// 见设计模块图),不再平铺扫描模块树——扫到谁由目录顺序决定,模块搬家即静默失明。
|
|
453
|
+
const descriptorModule = path.join(libDir, 'installer-platforms.mjs');
|
|
454
|
+
if (!fs.existsSync(descriptorModule)) {
|
|
455
|
+
throw new Error('缺少平台描述符模块(安装器模块图的描述符归属文件): ' + descriptorModule);
|
|
456
|
+
}
|
|
457
|
+
const platformBlock = fs.readFileSync(descriptorModule, 'utf8').match(/const PLATFORMS = \{[\s\S]*?\n\};/);
|
|
458
|
+
if (!platformBlock) throw new Error('平台描述符模块未找到 PLATFORMS 描述符表: ' + descriptorModule);
|
|
459
|
+
const ids = [...platformBlock[0].matchAll(/^\s{2}'([a-z-]+)':\s*\{/gm)].map((m) => m[1]);
|
|
460
|
+
if (!ids.includes('claude-code') || !ids.includes('codex')) {
|
|
461
|
+
throw new Error('PLATFORMS 应含 claude-code/codex: ' + ids.join(', '));
|
|
462
|
+
}
|
|
463
|
+
for (const fn of ['installHooks', 'installRules', 'purge', 'overwriteDescription']) {
|
|
464
|
+
if (!platformBlock[0].includes(fn)) throw new Error('描述符缺平台函数: ' + fn);
|
|
465
|
+
}
|
|
466
|
+
// ③ 全平台逐一安装 + purge 冒烟(从描述符块提取 id——未来新增平台自动纳入;
|
|
467
|
+
// purge 冒烟验证清理函数真实可用,缺函数/清理失败即红);
|
|
468
|
+
// DSH_HOME 指向临时目录——dsh 平台 installHooks 写 $DSH_HOME(plugins/loader +
|
|
469
|
+
// cordis.patch.yml 托管块),断言全程不污染真实 ~/.dsh
|
|
470
|
+
const dshHome = path.join(dir, 'k6-dsh-home');
|
|
471
|
+
for (const id of ids) {
|
|
472
|
+
const target = path.join(dir, 'k6-' + id);
|
|
473
|
+
fs.mkdirSync(target, { recursive: true });
|
|
474
|
+
const env = { ...process.env, DSH_HOME: dshHome };
|
|
475
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', id], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env });
|
|
476
|
+
if (res.status !== 0) throw new Error('平台 ' + id + ' 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
477
|
+
const purgeRes = spawnSync(process.execPath, [installer, '--target', target, '--platform', id, '--purge', '--yes'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000, env });
|
|
478
|
+
if (purgeRes.status !== 0) throw new Error('平台 ' + id + ' purge 失败: ' + (purgeRes.stderr || JSON.stringify(purgeRes.output)));
|
|
479
|
+
}
|
|
480
|
+
console.log(' 描述符驱动: ' + ids.length + ' 个平台逐一安装 + purge 冒烟通过✓');
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
// ---------- K 扩展:dsh 平台断言(安装器多平台) ----------
|
|
484
|
+
// 以下断言覆盖 prepare-env --platform dsh 的项目级安装 / AGENTS.md 托管区 / 桥接 loader /
|
|
485
|
+
// purge 清理恢复;断言全程 DSH_HOME=临时目录环境变量,禁止污染真实 ~/.dsh。
|
|
486
|
+
|
|
487
|
+
// K7: dsh 描述符安装产物——--platform dsh 项目级安装(.dsh/skills rank 100 自动发现) +
|
|
488
|
+
// pathReplacements 平台化(.dsh/skills/flow-comet/scripts/ 形态) + INSTALLED_VERSION +
|
|
489
|
+
// 安装树清单与权威源一致;全程 DSH_HOME=临时目录(AC-1)
|
|
490
|
+
{
|
|
491
|
+
name: 'K7 安装器:dsh 平台安装(技能/pathReplacements/版本标识/树一致)',
|
|
492
|
+
surface: 'dsh 平台安装产物与树一致断言仅权威源侧可判',
|
|
493
|
+
run: (dir) => {
|
|
494
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
495
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
496
|
+
const dshHome = path.join(dir, 'k7-dsh-home');
|
|
497
|
+
const target = path.join(dir, 'k7-target');
|
|
498
|
+
fs.mkdirSync(target, { recursive: true });
|
|
499
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
500
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
501
|
+
});
|
|
502
|
+
if (res.status !== 0) throw new Error('prepare-env --platform dsh 失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
503
|
+
// ① 技能安装到 <项目>/.dsh/skills/flow-comet/(dsh rank 100 项目级发现位置)
|
|
504
|
+
const skillMd = path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md');
|
|
505
|
+
if (!fs.existsSync(skillMd)) throw new Error('dsh 平台技能未安装到 .dsh/skills/flow-comet/');
|
|
506
|
+
// ② guard 判定核心随技能分发(comet-hook-guard 平台无关 CLI,一行不动)
|
|
507
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'scripts', 'comet-hook-guard.mjs'))) {
|
|
508
|
+
throw new Error('comet-hook-guard.mjs 未随技能分发到 .dsh/skills/flow-comet/scripts/');
|
|
509
|
+
}
|
|
510
|
+
// ③ 版本标识写入(随技能包分发)
|
|
511
|
+
const verFile = path.join(target, '.dsh', 'skills', 'flow-comet', 'INSTALLED_VERSION');
|
|
512
|
+
if (!fs.existsSync(verFile)) throw new Error('缺少 INSTALLED_VERSION(版本标识文件)');
|
|
513
|
+
if (!fs.readFileSync(verFile, 'utf8').trim()) throw new Error('版本标识为空');
|
|
514
|
+
// ④ pathReplacements 生效:SKILL 命令路径为 .dsh 形态,无 .claude 残留
|
|
515
|
+
const text = fs.readFileSync(skillMd, 'utf8');
|
|
516
|
+
if (!text.includes('.dsh/skills/flow-comet/scripts/')) throw new Error('SKILL 命令路径未替换为 .dsh/skills/flow-comet/scripts/');
|
|
517
|
+
if (text.includes('.claude/skills/flow-comet/scripts/')) throw new Error('SKILL 仍含 .claude/skills/flow-comet/scripts/ 命令路径');
|
|
518
|
+
// ⑤ 安装树文件清单与权威源一致(pathReplacements 只改内容不改清单)
|
|
519
|
+
const authoritative = path.join(repoRoot, '.flow-comet', 'skills', 'flow-comet');
|
|
520
|
+
const installedTree = collectTreeFiles(path.join(target, '.dsh', 'skills', 'flow-comet'));
|
|
521
|
+
const srcTree = collectTreeFiles(authoritative);
|
|
522
|
+
if (installedTree.length === 0) throw new Error('安装技能树为空');
|
|
523
|
+
if (JSON.stringify(installedTree) !== JSON.stringify(srcTree)) {
|
|
524
|
+
throw new Error('安装树清单与权威源不一致\n权威源: ' + srcTree.join(', ') + '\n安装: ' + installedTree.join(', '));
|
|
525
|
+
}
|
|
526
|
+
console.log(' .dsh/skills/flow-comet 安装树 = ' + installedTree.length + ' files 与权威源一致 ✓');
|
|
527
|
+
},
|
|
528
|
+
},
|
|
529
|
+
|
|
530
|
+
// K8: AGENTS.md 托管区注入——托管区存在 + 托管区外用户内容保留 + 幂等重装不重复
|
|
531
|
+
// (codex/dsh 共用注入函数与托管区标记——AC-4)
|
|
532
|
+
{
|
|
533
|
+
name: 'K8 安装器:dsh AGENTS.md 托管区(用户内容保留 + 幂等重装)',
|
|
534
|
+
surface: 'AGENTS.md 托管区注入与幂等断言仅权威源侧可判',
|
|
535
|
+
run: (dir) => {
|
|
536
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
537
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
538
|
+
const dshHome = path.join(dir, 'k8-dsh-home');
|
|
539
|
+
const target = path.join(dir, 'k8-target');
|
|
540
|
+
fs.mkdirSync(target, { recursive: true });
|
|
541
|
+
// 预置用户内容(托管区外,安装不得覆盖)
|
|
542
|
+
fs.writeFileSync(path.join(target, 'AGENTS.md'), '# 用户自定义指令\n\n保留这段内容。\n', 'utf8');
|
|
543
|
+
const run = () => spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
544
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
545
|
+
});
|
|
546
|
+
// ① 首次安装:托管区注入(内联 orchestration 全文)+ 用户内容保留
|
|
547
|
+
const first = run();
|
|
548
|
+
if (first.status !== 0) throw new Error('dsh 安装失败: ' + (first.stderr || JSON.stringify(first.output)));
|
|
549
|
+
let agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
550
|
+
if (!agents.includes('<!-- Managed by flow-comet prepare-env -->')) throw new Error('AGENTS.md 无托管区标记');
|
|
551
|
+
if (!agents.includes('flow-comet Orchestration')) throw new Error('AGENTS.md 未内联 orchestration 内容');
|
|
552
|
+
if (!agents.includes('用户自定义指令') || !agents.includes('保留这段内容。')) throw new Error('托管区外用户内容被覆盖');
|
|
553
|
+
// ② 幂等重装:托管区不重复注入(start+end 标记合计 2 处 = 1 个托管区)
|
|
554
|
+
const second = run();
|
|
555
|
+
if (second.status !== 0) throw new Error('dsh 重装失败: ' + (second.stderr || JSON.stringify(second.output)));
|
|
556
|
+
agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
557
|
+
if (!agents.includes('保留这段内容。')) throw new Error('重装后用户内容丢失');
|
|
558
|
+
const markerCount = (agents.match(/Managed by flow-comet prepare-env/g) || []).length;
|
|
559
|
+
if (markerCount !== 2) throw new Error('幂等重装后托管区应只有 1 组标记(实际 ' + markerCount + ' 个)——重复注入');
|
|
560
|
+
console.log(' AGENTS.md 托管区注入 + 幂等重装不重复 ✓');
|
|
561
|
+
},
|
|
562
|
+
},
|
|
563
|
+
|
|
564
|
+
// K9: 桥接 loader 就位——权威源文件存在 + node --check 语法 + 安装时复制到
|
|
565
|
+
// $DSH_HOME/plugins/ + cordis.patch.yml 托管块注入(insert 形态 + file:// 引用 loader 路径,
|
|
566
|
+
// 读-合并-写保留既有块;insert 形态是 home patch 追加新插件的唯一正确形态——patch 形态
|
|
567
|
+
// id-targeted 对不存在的 id 报 entry not found 跳过,loader 不加载,拦截整链静默失效;
|
|
568
|
+
// $DSH_HOME 解析 = 显式 DSH_HOME > ~/.dsh——测试隔离到临时目录)
|
|
569
|
+
{
|
|
570
|
+
name: 'K9 桥接 loader:源文件/语法/复制与托管块注入(读-合并-写)',
|
|
571
|
+
surface: '桥接 loader 就位与托管块注入断言仅权威源侧可判',
|
|
572
|
+
run: (dir) => {
|
|
573
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
574
|
+
// ① 权威源 loader 存在 + 语法检查
|
|
575
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
576
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
577
|
+
const check = spawnSync(process.execPath, ['--check', loader], { encoding: 'utf8', timeout: 60000 });
|
|
578
|
+
if (check.status !== 0) throw new Error('dsh-bridge.mjs 语法检查失败: ' + (check.stderr || ''));
|
|
579
|
+
// ② 安装后:loader 复制到 $DSH_HOME/plugins/dsh-flow-comet-bridge.mjs + 托管块注入
|
|
580
|
+
const dshHome = path.join(dir, 'k9-dsh-home');
|
|
581
|
+
const target = path.join(dir, 'k9-target');
|
|
582
|
+
fs.mkdirSync(target, { recursive: true });
|
|
583
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
584
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
585
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
586
|
+
});
|
|
587
|
+
if (res.status !== 0) throw new Error('dsh 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
588
|
+
if (!fs.existsSync(path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs'))) {
|
|
589
|
+
throw new Error('loader 未复制到 $DSH_HOME/plugins/');
|
|
590
|
+
}
|
|
591
|
+
const patchPath = path.join(dshHome, 'cordis.patch.yml');
|
|
592
|
+
if (!fs.existsSync(patchPath)) throw new Error('缺少 $DSH_HOME/cordis.patch.yml');
|
|
593
|
+
const patch = fs.readFileSync(patchPath, 'utf8');
|
|
594
|
+
if (!patch.includes('# --- flow-comet managed ---') || !patch.includes('# --- end flow-comet managed ---')) {
|
|
595
|
+
throw new Error('cordis.patch.yml 无托管块标记');
|
|
596
|
+
}
|
|
597
|
+
// insert 形态断言(套件全绿 ≠ 真实 dsh 生效——patch 形态 id-targeted 语义对
|
|
598
|
+
// 不存在的 id 输出 entry not found 并跳过,loader 从不加载;insert 形态才追加新插件行)
|
|
599
|
+
if (!patch.includes('- insert:')) throw new Error('cordis.patch.yml 托管块应为 insert 形态(- insert: 顶层条目)');
|
|
600
|
+
if (!patch.includes('dsh-flow-comet-bridge')) throw new Error('cordis.patch.yml 缺 loader 条目');
|
|
601
|
+
const loaderUrl = pathToFileURL(path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs')).href;
|
|
602
|
+
if (!patch.includes(loaderUrl)) throw new Error('cordis.patch.yml 托管块应 file:// 引用 loader 路径: ' + loaderUrl);
|
|
603
|
+
// ③ 读-合并-写非破坏:预置 dsh-skin 既有块 → 重装后保留(托管块幂等唯一)
|
|
604
|
+
fs.writeFileSync(patchPath, '# --- dsh-skin managed ---\n- id: dsh-skin\n name: file:///skin\n# --- end dsh-skin managed ---\n' + patch, 'utf8');
|
|
605
|
+
const re = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
606
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
607
|
+
});
|
|
608
|
+
if (re.status !== 0) throw new Error('dsh 重装失败: ' + (re.stderr || JSON.stringify(re.output)));
|
|
609
|
+
const after = fs.readFileSync(patchPath, 'utf8');
|
|
610
|
+
if (!after.includes('dsh-skin managed')) throw new Error('重装后 dsh-skin 既有块丢失(读-合并-写非破坏)');
|
|
611
|
+
if ((after.match(/flow-comet managed/g) || []).length !== 2) {
|
|
612
|
+
throw new Error('重装后 flow-comet 托管块应幂等唯一: ' + after);
|
|
613
|
+
}
|
|
614
|
+
console.log(' loader 复制 + cordis.patch.yml 托管块(读-合并-写保留既有块)✓');
|
|
615
|
+
},
|
|
616
|
+
},
|
|
617
|
+
|
|
618
|
+
// K10: dsh purge 清理恢复——缺 --yes 拒绝(不破坏)/ --purge --yes 删除后重新生成
|
|
619
|
+
// (既有 main 语义:purge 后重新生成到最终态)/ 用户内容保留 / 空 .dsh 目录清理
|
|
620
|
+
// (唯一 flow-comet 技能移除后 .dsh/skills 与 .dsh 变空 → 一并清理)
|
|
621
|
+
{
|
|
622
|
+
name: 'K10 安装器:dsh purge 清理恢复(缺 --yes 拒绝/删除后重建/用户内容保留/空目录清理)',
|
|
623
|
+
surface: 'dsh purge 清理恢复断言仅权威源侧可判',
|
|
624
|
+
run: (dir) => {
|
|
625
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
626
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
627
|
+
const dshHome = path.join(dir, 'k10-dsh-home');
|
|
628
|
+
const run = (target, extra) => spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh', ...extra], {
|
|
629
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
630
|
+
});
|
|
631
|
+
// ① 首次安装 + 用户内容(项目根,非生成物)
|
|
632
|
+
const target = path.join(dir, 'k10-target');
|
|
633
|
+
fs.mkdirSync(target, { recursive: true });
|
|
634
|
+
const first = run(target, []);
|
|
635
|
+
if (first.status !== 0) throw new Error('dsh 首次安装失败: ' + (first.stderr || JSON.stringify(first.output)));
|
|
636
|
+
fs.writeFileSync(path.join(target, 'USER_KEEP.md'), '# 用户内容(保留)\n', 'utf8');
|
|
637
|
+
// ② 缺 --yes → 拒绝且不破坏(错误提示含 --yes;生成物与用户内容仍在)
|
|
638
|
+
const dry = run(target, ['--purge']);
|
|
639
|
+
if (dry.status === 0) throw new Error('--purge 缺 --yes 应失败');
|
|
640
|
+
const dryOut = String(dry.stderr || '') + String(dry.stdout || '');
|
|
641
|
+
if (!dryOut.includes('--yes')) throw new Error('--purge 缺 --yes 错误信息应提示 --yes: ' + dryOut);
|
|
642
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge 拒绝后生成物不应被删除');
|
|
643
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge 拒绝后用户内容不应被删除');
|
|
644
|
+
// ③ --purge --yes → 删除后重新生成(最终态 = 重新生成的安装态);移除清单含空 .dsh 目录
|
|
645
|
+
// 清理条目(.dsh/skills 移除后 .dsh 变空——行尾锚定防与 skillRoot 打印行混淆)
|
|
646
|
+
const ok = run(target, ['--purge', '--yes']);
|
|
647
|
+
if (ok.status !== 0) throw new Error('--purge --yes 失败: ' + (ok.stderr || JSON.stringify(ok.output)));
|
|
648
|
+
const okOut = String(ok.stderr || '') + String(ok.stdout || '');
|
|
649
|
+
const dshDirLine = '- ' + path.join(target, '.dsh');
|
|
650
|
+
if (!okOut.includes(dshDirLine + '\r\n') && !okOut.includes(dshDirLine + '\n')) {
|
|
651
|
+
throw new Error('purge 移除清单应含空 .dsh 目录清理条目: ' + okOut);
|
|
652
|
+
}
|
|
653
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge --yes 后技能应重新生成(删除后重新生成语义)');
|
|
654
|
+
if (!fs.existsSync(path.join(target, 'USER_KEEP.md'))) throw new Error('--purge --yes 后用户内容应保留');
|
|
655
|
+
const agents = fs.readFileSync(path.join(target, 'AGENTS.md'), 'utf8');
|
|
656
|
+
if (!agents.includes('<!-- Managed by flow-comet prepare-env -->')) throw new Error('--purge --yes 后 AGENTS.md 托管区应重建');
|
|
657
|
+
if (!fs.existsSync(path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs'))) throw new Error('--purge --yes 后桥接 loader 应重建');
|
|
658
|
+
// ④ 共享位置边界:.dsh/skills 用户技能(purge 只清 flow-comet*,其余保留)
|
|
659
|
+
fs.mkdirSync(path.join(target, '.dsh', 'skills', 'user-own-skill'), { recursive: true });
|
|
660
|
+
fs.writeFileSync(path.join(target, '.dsh', 'skills', 'user-own-skill', 'SKILL.md'), '# user skill\n', 'utf8');
|
|
661
|
+
const ok2 = run(target, ['--purge', '--yes']);
|
|
662
|
+
if (ok2.status !== 0) throw new Error('--purge --yes(第二次) 失败: ' + (ok2.stderr || JSON.stringify(ok2.output)));
|
|
663
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'user-own-skill', 'SKILL.md'))) throw new Error('--purge --yes 后用户技能应保留(.dsh/skills 共享位置)');
|
|
664
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) throw new Error('--purge --yes(第二次) 后 flow-comet 技能应重新生成');
|
|
665
|
+
console.log(' dsh purge 缺 --yes 拒绝 + 删除后重建 + 用户内容保留 + 空目录清理 ✓');
|
|
666
|
+
},
|
|
667
|
+
},
|
|
668
|
+
|
|
669
|
+
// K11: 桥接 loader 行为断言(dsh-bridge.mjs export 直测——参数映射别名矩阵/
|
|
670
|
+
// 形状不符 fail-closed/项目根包含性含 8.3 短路径/退出码映射/guard 路径存在漂移防护;
|
|
671
|
+
// apply 集成:tools/pre-execute 监听器分派——子代理放行/协调者走 guard/越界·形状 deny 不受身份)
|
|
672
|
+
{
|
|
673
|
+
name: 'K11 桥接 loader:纯函数与 apply 分派断言(映射/包含性/退出映射/身份分派/最小保护集)',
|
|
674
|
+
surface: '桥接 loader 纯函数与 pre-execute 分派断言仅权威源侧可判',
|
|
675
|
+
run: async (dir, outcomes) => {
|
|
676
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
677
|
+
// 动态 import(套件运行器逐项 await)——dsh-bridge.mjs 仅在权威源仓库根 scripts/,
|
|
678
|
+
// 安装副本缺失时整体跳过(与 K 组其余项同语义);静态 import 会让安装副本套件整体崩
|
|
679
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
680
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
681
|
+
const bridge = await import(pathToFileURL(loader).href);
|
|
682
|
+
// ① 工具名归一化别名矩阵:write/edit/bash 全部别名 → Write/Edit/Bash
|
|
683
|
+
for (const alias of ['write', 'Write', 'WRITE', 'writefile', 'file-write']) {
|
|
684
|
+
if (bridge.normalizeToolName(alias) !== 'Write') throw new Error('normalizeToolName 别名未归一化: ' + alias);
|
|
685
|
+
}
|
|
686
|
+
for (const alias of ['edit', 'Edit', 'editfile', 'file-edit']) {
|
|
687
|
+
if (bridge.normalizeToolName(alias) !== 'Edit') throw new Error('normalizeToolName 别名未归一化: ' + alias);
|
|
688
|
+
}
|
|
689
|
+
for (const alias of ['bash', 'Bash', 'shell', 'powershell', 'pwsh', 'Pwsh', 'PWSH', 'run_command', 'run-command']) {
|
|
690
|
+
if (bridge.normalizeToolName(alias) !== 'Bash') throw new Error('normalizeToolName 别名未归一化: ' + alias);
|
|
691
|
+
}
|
|
692
|
+
// 非 Write/Edit/Bash 或非字符串 → null(监听侧按放行语义跳过)
|
|
693
|
+
for (const v of ['read', 'grep', '', null, undefined, 123, {}]) {
|
|
694
|
+
if (bridge.normalizeToolName(v) !== null) throw new Error('非写工具应归一化为 null: ' + JSON.stringify(v));
|
|
695
|
+
}
|
|
696
|
+
// ② 参数映射别名提取:Write/Edit 的 file_path/filePath/path;Bash 的 command/cmd/script
|
|
697
|
+
const m1 = bridge.mapToolInput('Write', { file_path: 'a.md' });
|
|
698
|
+
if (!m1.ok || m1.target !== 'a.md' || m1.input.file_path !== 'a.md') throw new Error('Write file_path 映射失败: ' + JSON.stringify(m1));
|
|
699
|
+
const m2 = bridge.mapToolInput('Edit', { filePath: 'b.md' });
|
|
700
|
+
if (!m2.ok || m2.target !== 'b.md') throw new Error('Edit filePath 别名映射失败: ' + JSON.stringify(m2));
|
|
701
|
+
const m3 = bridge.mapToolInput('Write', { path: 'c.md' });
|
|
702
|
+
if (!m3.ok || m3.target !== 'c.md') throw new Error('Write path 别名映射失败: ' + JSON.stringify(m3));
|
|
703
|
+
const mPrec = bridge.mapToolInput('Write', { file_path: 'a.md', filePath: 'b.md', path: 'c.md' });
|
|
704
|
+
if (!mPrec.ok || mPrec.target !== 'a.md') throw new Error('file_path 应优先于 filePath/path: ' + JSON.stringify(mPrec));
|
|
705
|
+
for (const alias of ['command', 'cmd', 'script']) {
|
|
706
|
+
const mb = bridge.mapToolInput('Bash', { [alias]: 'npm test' });
|
|
707
|
+
if (!mb.ok || mb.target !== 'npm test' || mb.input.command !== 'npm test') {
|
|
708
|
+
throw new Error('Bash ' + alias + ' 映射失败: ' + JSON.stringify(mb));
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
// ②b 形状不符/缺关键字段 → ok:false 可识别(fail-closed 前置,不静默放行)
|
|
712
|
+
const bad1 = bridge.mapToolInput('Write', {});
|
|
713
|
+
if (bad1.ok || !bad1.reason.includes('缺少 file_path')) throw new Error('Write 缺 file_path 应 fail-closed 且原因可识别: ' + JSON.stringify(bad1));
|
|
714
|
+
const bad2 = bridge.mapToolInput('Bash', {});
|
|
715
|
+
if (bad2.ok || !bad2.reason.includes('缺少 command')) throw new Error('Bash 缺 command 应 fail-closed 且原因可识别: ' + JSON.stringify(bad2));
|
|
716
|
+
if (bridge.mapToolInput('Write', { file_path: ' ' }).ok) throw new Error('空白 file_path 应 fail-closed');
|
|
717
|
+
if (bridge.mapToolInput('Bash', null).ok) throw new Error('null 参数应 fail-closed');
|
|
718
|
+
if (bridge.mapToolInput('Bash', ['x']).ok) throw new Error('数组参数应 fail-closed');
|
|
719
|
+
if (bridge.mapToolInput('Read', { file_path: 'a.md' }).ok) throw new Error('未支持工具名应 fail-closed');
|
|
720
|
+
// ③ isPathInsideProjectRoot:项目内长路径 true / 项目外 false(含 .. 越界)
|
|
721
|
+
const projRoot = path.join(dir, 'k11-proj');
|
|
722
|
+
fs.mkdirSync(projRoot, { recursive: true });
|
|
723
|
+
if (!bridge.isPathInsideProjectRoot(projRoot, path.join(projRoot, 'src', 'a.md'))) throw new Error('项目内长路径应判定为 true');
|
|
724
|
+
if (!bridge.isPathInsideProjectRoot(projRoot, 'src/a.md')) throw new Error('项目内相对路径应判定为 true');
|
|
725
|
+
const outside = path.join(dir, 'k11-out');
|
|
726
|
+
fs.mkdirSync(outside, { recursive: true });
|
|
727
|
+
if (bridge.isPathInsideProjectRoot(projRoot, path.join(outside, 'x.md'))) throw new Error('项目外绝对路径应判定为 false');
|
|
728
|
+
if (bridge.isPathInsideProjectRoot(projRoot, path.join(projRoot, '..', 'k11-out', 'x.md'))) throw new Error('.. 越界路径应判定为 false');
|
|
729
|
+
// ③b 8.3 短路径别名(Windows 8dot3name):os.tmpdir() 本机为 C:\Users\LONGYI~1
|
|
730
|
+
// 短形态,realpathSync.native 归一化为长形态——词法不同但实路径相同;
|
|
731
|
+
// 短形态不可解析的平台(禁用 8dot3name/POSIX)显式跳过
|
|
732
|
+
const realRoot = bridge.realpathExistingPath(projRoot);
|
|
733
|
+
if (realRoot !== projRoot) {
|
|
734
|
+
const longFormTarget = path.join(realRoot, 'src', 'a.md');
|
|
735
|
+
if (!bridge.isPathInsideProjectRoot(projRoot, longFormTarget)) {
|
|
736
|
+
throw new Error('8.3 短路径别名(长形态目标)应归一化后判定为 true');
|
|
737
|
+
}
|
|
738
|
+
const normalized = bridge.realpathExistingPath(path.join(projRoot, 'src'));
|
|
739
|
+
if (normalized !== path.join(realRoot, 'src')) {
|
|
740
|
+
throw new Error('realpathExistingPath 应返回规范化长路径: ' + normalized + ' ≠ ' + path.join(realRoot, 'src'));
|
|
741
|
+
}
|
|
742
|
+
console.log(' 8.3 短路径别名经 realpath 归一化为长形态判定 ✓');
|
|
743
|
+
} else {
|
|
744
|
+
outcomes.notApplicablePlatform('K11 ③b 8.3 短路径别名归一化断言',
|
|
745
|
+
'本载体的临时目录不产生 8.3 短形态(POSIX / 已禁用 8dot3name)');
|
|
746
|
+
}
|
|
747
|
+
// ④ mapGuardExit:exit 0 → allow / exit 2 → deny(恢复指引 + detail 透传)/ 其它 → fail-closed
|
|
748
|
+
if (bridge.mapGuardExit(0, '', '').kind !== 'allow') throw new Error('exit 0 应 allow');
|
|
749
|
+
const d2 = bridge.mapGuardExit(2, 'BLOCKED detail', '');
|
|
750
|
+
if (d2.kind !== 'deny') throw new Error('exit 2 应 deny');
|
|
751
|
+
if (!d2.reason.includes('白名单拦截') || !d2.reason.includes('BLOCKED detail')) {
|
|
752
|
+
throw new Error('exit 2 deny 应透传恢复指引与 detail: ' + JSON.stringify(d2));
|
|
753
|
+
}
|
|
754
|
+
for (const code of [1, 3, null, '0']) {
|
|
755
|
+
const err = bridge.mapGuardExit(code, 'boom', '');
|
|
756
|
+
if (err.kind !== 'error') throw new Error('其它退出码应 fail-closed error: code=' + String(code));
|
|
757
|
+
if (!err.message.includes('code=' + String(code))) throw new Error('error 消息应含退出码: ' + JSON.stringify(err));
|
|
758
|
+
}
|
|
759
|
+
// ⑤ guard 路径存在断言(M4 漂移防护):HOOK_GUARD_REL('.dsh/skills/flow-comet/scripts/
|
|
760
|
+
// comet-hook-guard.mjs')与安装器独立维护同一知识——任一侧漂移(常量改/安装布局改)
|
|
761
|
+
// → 此断言失败,拦截层即失效(guard 缺失 → 桥接 WARN + 放行 fail-open)
|
|
762
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
763
|
+
const dshHome = path.join(dir, 'k11-dsh-home');
|
|
764
|
+
const target = path.join(dir, 'k11-target');
|
|
765
|
+
fs.mkdirSync(target, { recursive: true });
|
|
766
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
767
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
768
|
+
});
|
|
769
|
+
if (res.status !== 0) throw new Error('dsh 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
770
|
+
const guardRel = '.dsh/skills/flow-comet/scripts/comet-hook-guard.mjs';
|
|
771
|
+
if (!fs.existsSync(path.join(target, guardRel))) {
|
|
772
|
+
throw new Error('HOOK_GUARD_REL 相对项目根解析的 guard 不存在(安装布局漂移): ' + path.join(target, guardRel));
|
|
773
|
+
}
|
|
774
|
+
console.log(' HOOK_GUARD_REL guard 安装后存在(与安装产物绑定)✓');
|
|
775
|
+
// ⑥ delegationDepth 代理身份分派(B1——dsh 子代理=执行者):dsh 子代理 spawn
|
|
776
|
+
// provider 的 childSessionMeta 把 delegationDepth=parentDepth+1 写入子代理 session
|
|
777
|
+
// header —— 协调者=0/缺失,子代理>0。桥接层据此分派:子代理写源码=执行者职责
|
|
778
|
+
// (对应 CC worktree 物理隔离),协调者走 guard 白名单拦截。旧桥接对所有工具
|
|
779
|
+
// 一视同仁送 guard → subagent 模式子代理写源码被 .specs/ 白名单误拦 = B1 根因。
|
|
780
|
+
// 本断言锚定 agentDepth 纯函数(守 exec 结构读取语义——header.delegationDepth)。
|
|
781
|
+
if (typeof bridge.agentDepth !== 'function') {
|
|
782
|
+
throw new Error('bridge.agentDepth 未导出(B1 修复应新增代理身份读取纯函数)');
|
|
783
|
+
}
|
|
784
|
+
// ① 子代理(depth=1)→ 识别为执行者(返回 1)
|
|
785
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: 1 } } } }) !== 1) {
|
|
786
|
+
throw new Error('子代理 delegationDepth=1 应识别为执行者: ' + String(bridge.agentDepth({ agent: { session: { header: { delegationDepth: 1 } } } })));
|
|
787
|
+
}
|
|
788
|
+
// ② 深层子代理(depth=2)→ 识别为执行者
|
|
789
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: 2 } } } }) !== 2) {
|
|
790
|
+
throw new Error('子代理 delegationDepth=2 应识别为执行者');
|
|
791
|
+
}
|
|
792
|
+
// ③ 协调者(depth=0)→ 协调者(非执行者)
|
|
793
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: 0 } } } }) !== 0) {
|
|
794
|
+
throw new Error('协调者 delegationDepth=0 应返回 0');
|
|
795
|
+
}
|
|
796
|
+
// ④ 缺省(undefined/缺字段)→ 协调者(旧语义兼容——协调者 depth 缺失按 0)
|
|
797
|
+
if (bridge.agentDepth({ agent: { session: { header: {} } } }) !== 0) {
|
|
798
|
+
throw new Error('缺 delegationDepth 应按协调者处理(0)');
|
|
799
|
+
}
|
|
800
|
+
if (bridge.agentDepth({}) !== 0) {
|
|
801
|
+
throw new Error('空 exec 应按协调者处理(0)');
|
|
802
|
+
}
|
|
803
|
+
if (bridge.agentDepth(undefined) !== 0) {
|
|
804
|
+
throw new Error('undefined exec 应按协调者处理(0)');
|
|
805
|
+
}
|
|
806
|
+
// ⑤ 非数字/负数/非法值 → 0(fail-closed 协调者语义,防 NaN/字符串穿透)
|
|
807
|
+
for (const bad of [null, '1', -1, NaN, Infinity, {}, 'abc']) {
|
|
808
|
+
if (bridge.agentDepth({ agent: { session: { header: { delegationDepth: bad } } } }) !== 0) {
|
|
809
|
+
throw new Error('非法 delegationDepth 应按协调者处理(0): ' + JSON.stringify(bad));
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
console.log(' delegationDepth 代理身份分派(子代理=执行者/协调者原链)✓');
|
|
813
|
+
// ⑦ apply 集成喂测(5.5 分派分支自动化回归——此前仅有真实会话人工覆盖):
|
|
814
|
+
// mock ctx 捕获 tools/pre-execute 监听器,构造 exec 断言插件级行为、
|
|
815
|
+
// 子代理(agentDepth>0)写源码放行 / 协调者走 guard 白名单 / 越界·形状 deny 不受身份、
|
|
816
|
+
// 非 flow-comet 项目窄监听放行。复用 ⑤ 安装产物(target 为真实 dsh flow-comet 项目):
|
|
817
|
+
// 写活跃 execute 态 state → 协调者写源码被真实 guard 子进程拦(BLOCK 链真实执行)。
|
|
818
|
+
const applyEvents = {};
|
|
819
|
+
bridge.apply({ on: (event, fn) => { applyEvents[event] = fn; } });
|
|
820
|
+
const preExec = applyEvents['tools/pre-execute'];
|
|
821
|
+
if (typeof preExec !== 'function') throw new Error('bridge.apply 应注册 tools/pre-execute 监听器');
|
|
822
|
+
gitInit(target);
|
|
823
|
+
fs.mkdirSync(path.join(target, 'src'), { recursive: true });
|
|
824
|
+
writeState(target, {
|
|
825
|
+
activeChange: 'apply-lock',
|
|
826
|
+
currentNode: 'execute',
|
|
827
|
+
completedNodes: [],
|
|
828
|
+
evidence: {},
|
|
829
|
+
verifyFailures: 0,
|
|
830
|
+
status: 'running',
|
|
831
|
+
executionMode: 'subagent',
|
|
832
|
+
directOverride: false,
|
|
833
|
+
});
|
|
834
|
+
// 项目根常规用长形态(与桥接对 file_path 的规范化一致,对应真实会话规范 cwd);
|
|
835
|
+
// 短形态 cwd 的场景由桥接的项目根规范化修复,⑦f 断言锁死(fail-open 已封闭)。
|
|
836
|
+
const longTarget = bridge.realpathExistingPath(target); // realpathSync.native——展开 8.3 短名
|
|
837
|
+
// ⑦a 子代理(depth=1)写项目内源码 → next() 被调(守卫按身份分派放行——桥接不短路,
|
|
838
|
+
// 身份深度随 env 透传,判定与最小保护集统一由守卫拥有)
|
|
839
|
+
{
|
|
840
|
+
let usedNext = false;
|
|
841
|
+
const res = await preExec(
|
|
842
|
+
{ name: 'Write', arguments: { file_path: path.join(longTarget, 'src', 'a.mjs') }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
843
|
+
() => { usedNext = true; },
|
|
844
|
+
);
|
|
845
|
+
if (!usedNext) throw new Error('子代理写源码应放行(next 被调)——身份分派分支缺失');
|
|
846
|
+
if (res) throw new Error('子代理写源码不应返回 deny: ' + JSON.stringify(res));
|
|
847
|
+
}
|
|
848
|
+
// ⑦b 协调者(delegationDepth 缺失=0)同目标写 → 走真实 guard 白名单 → BLOCK deny
|
|
849
|
+
{
|
|
850
|
+
let usedNext = false;
|
|
851
|
+
const res = await preExec(
|
|
852
|
+
{ name: 'Write', arguments: { file_path: path.join(longTarget, 'src', 'a.mjs') }, agent: { cwd: longTarget, session: { header: {} } } },
|
|
853
|
+
() => { usedNext = true; },
|
|
854
|
+
);
|
|
855
|
+
if (usedNext) throw new Error('协调者写源码不应 next——协调者禁令物理拦截保留');
|
|
856
|
+
if (!res || res.kind !== 'deny') throw new Error('协调者写源码应走 guard 白名单拦截(deny): ' + JSON.stringify(res));
|
|
857
|
+
if (!res.reason.includes('白名单拦截')) throw new Error('协调者 deny 应含白名单拦截语义: ' + JSON.stringify(res));
|
|
858
|
+
}
|
|
859
|
+
// ⑦c 非 flow-comet 项目(无 .dsh/skills/flow-comet) → 窄监听放行(不分身份)
|
|
860
|
+
{
|
|
861
|
+
const plain = path.join(dir, 'k11-plain');
|
|
862
|
+
fs.mkdirSync(plain, { recursive: true });
|
|
863
|
+
let usedNext = false;
|
|
864
|
+
const res = await preExec(
|
|
865
|
+
{ name: 'Write', arguments: { file_path: path.join(plain, 'x.md') }, agent: { cwd: plain, session: { header: { delegationDepth: 1 } } } },
|
|
866
|
+
() => { usedNext = true; },
|
|
867
|
+
);
|
|
868
|
+
if (!usedNext) throw new Error('非 flow-comet 项目应窄监听放行(next)');
|
|
869
|
+
}
|
|
870
|
+
// ⑦d 越界写不受身份影响:子代理(depth=1)写项目根外 → 包含性 fail-closed deny
|
|
871
|
+
{
|
|
872
|
+
const outside = path.join(dir, 'k11-out2');
|
|
873
|
+
fs.mkdirSync(outside, { recursive: true });
|
|
874
|
+
let usedNext = false;
|
|
875
|
+
const res = await preExec(
|
|
876
|
+
{ name: 'Write', arguments: { file_path: path.join(outside, 'evil.md') }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
877
|
+
() => { usedNext = true; },
|
|
878
|
+
);
|
|
879
|
+
if (usedNext) throw new Error('子代理越界写不应 next——包含性不因身份放宽');
|
|
880
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('不在项目根')) {
|
|
881
|
+
throw new Error('子代理越界写应 fail-closed deny(含越界语义): ' + JSON.stringify(res));
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
// ⑦e 形状不符不受身份影响:子代理(depth=1)参数形状不符 → fail-closed deny
|
|
885
|
+
{
|
|
886
|
+
let usedNext = false;
|
|
887
|
+
const res = await preExec(
|
|
888
|
+
{ name: 'Write', arguments: {}, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
889
|
+
() => { usedNext = true; },
|
|
890
|
+
);
|
|
891
|
+
if (usedNext) throw new Error('子代理形状不符不应 next——形状校验不因身份放宽');
|
|
892
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('参数形状不符')) {
|
|
893
|
+
throw new Error('子代理形状不符应 fail-closed deny(含形状语义): ' + JSON.stringify(res));
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
// ⑦f 短形态项目根不得 fail-open:会话 cwd 为 8.3 短路径时,桥接须把项目根
|
|
897
|
+
// 归一化为长形态再送 guard——否则 guard 词法 path.relative 得 target=null
|
|
898
|
+
// 跳过白名单(协调者写源码被放行)。本断言在短形态 TMP 机器上即回归锁死。
|
|
899
|
+
{
|
|
900
|
+
let usedNext = false;
|
|
901
|
+
const res = await preExec(
|
|
902
|
+
{ name: 'Write', arguments: { file_path: path.join(target, 'src', 'b.mjs') }, agent: { cwd: target, session: { header: {} } } },
|
|
903
|
+
() => { usedNext = true; },
|
|
904
|
+
);
|
|
905
|
+
if (usedNext) throw new Error('短形态 cwd 协调者写源码不应 next——8.3 短路径不得绕过白名单(fail-open 已封闭)');
|
|
906
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('白名单拦截')) {
|
|
907
|
+
throw new Error('短形态 cwd 协调者写源码应走 guard 白名单拦截(deny): ' + JSON.stringify(res));
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
// ⑦g 最小保护集在桥接侧真实生效(身份放行不放行机器面):子代理(depth=1)写项目内
|
|
911
|
+
// 机器状态文件 → 桥接透传身份深度、守卫保护集 BLOCK → deny;对照 = ⑦a 同身份写源码
|
|
912
|
+
// 放行。反向构造:桥接恢复「身份短路直接 next」⇒ 本断言必红(保护集形同虚设)。
|
|
913
|
+
{
|
|
914
|
+
let usedNext = false;
|
|
915
|
+
const res = await preExec(
|
|
916
|
+
{ name: 'Write', arguments: { file_path: path.join(longTarget, '.flow-comet', 'flow-comet-state.json') }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
917
|
+
() => { usedNext = true; },
|
|
918
|
+
);
|
|
919
|
+
if (usedNext) throw new Error('子代理写机器状态文件不应 next——身份放行不得放行最小保护集');
|
|
920
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('state 文件')) {
|
|
921
|
+
throw new Error('子代理写机器状态文件应 deny(含 state 文件语义): ' + JSON.stringify(res));
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
// ⑦h 最小保护集的协议目标同判:子代理(depth=1)写本次运行实际生效的协议文件
|
|
925
|
+
// (守卫按自身 packageRoot 解析——安装副本内的 reference/workflow-protocol.json)
|
|
926
|
+
// → deny;该路径在项目根内,故本断言与 ⑦d 的越界 deny 相互独立(保护集非越界判定)。
|
|
927
|
+
{
|
|
928
|
+
const protocolInTarget = path.join(longTarget, '.dsh', 'skills', 'flow-comet', 'reference', 'workflow-protocol.json');
|
|
929
|
+
let usedNext = false;
|
|
930
|
+
const res = await preExec(
|
|
931
|
+
{ name: 'Write', arguments: { file_path: protocolInTarget }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
932
|
+
() => { usedNext = true; },
|
|
933
|
+
);
|
|
934
|
+
if (usedNext) throw new Error('子代理写协议保护路径不应 next——身份放行不得放行协议面');
|
|
935
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes('协议文件')) {
|
|
936
|
+
throw new Error('子代理写协议保护路径应 deny(含协议文件语义): ' + JSON.stringify(res));
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
// ⑦i 最小保护集对盘符根拼写(Windows 上 Git-Bash 类 shell 的 `/d/<剩余>` 形态)同样成立:
|
|
940
|
+
// Bash 命令级写入的目标经既有提取器抽出后按写入目标解析的字面归一处理 ⇒ 与盘符拼写
|
|
941
|
+
// 同判 deny。归一前该拼写被错解到「当前盘根之下」⇒ 保护集两道判据同时空转(身份放行
|
|
942
|
+
// 下静默 next = fail-open)。对照 = 同一身份的盘符根拼写源码仍放行(保护集不扩大)。
|
|
943
|
+
// 该拼写是 Windows 上 Git-Bash 类 shell 的自然产物;非 win32 载体上它是普通 POSIX 绝对
|
|
944
|
+
// 路径,语义不同 ⇒ 显式不适用(不静默跳过——未验证 ≠ 通过)。
|
|
945
|
+
const hasDriveRoot = /^[A-Za-z]:[\\/]/.test(longTarget);
|
|
946
|
+
if (process.platform === 'win32' && hasDriveRoot) {
|
|
947
|
+
const driveRootSpelling = (p) => '/' + p[0].toLowerCase() + p.slice(2).replaceAll('\\', '/');
|
|
948
|
+
const spellingCases = [
|
|
949
|
+
['机器状态文件', driveRootSpelling(path.join(longTarget, '.flow-comet', 'flow-comet-state.json')), 'state 文件'],
|
|
950
|
+
['项目内源码', driveRootSpelling(path.join(longTarget, 'src', 'a.mjs')), null],
|
|
951
|
+
];
|
|
952
|
+
for (const [label, spelled, denyMarker] of spellingCases) {
|
|
953
|
+
let usedNext = false;
|
|
954
|
+
const res = await preExec(
|
|
955
|
+
{ name: 'Bash', arguments: { command: 'cp "' + spelled + '" "' + spelled + '"' }, agent: { cwd: longTarget, session: { header: { delegationDepth: 1 } } } },
|
|
956
|
+
() => { usedNext = true; },
|
|
957
|
+
);
|
|
958
|
+
if (denyMarker === null) {
|
|
959
|
+
if (!usedNext) throw new Error(label + '的盘符根拼写应放行(next)——保护集不得扩大: ' + JSON.stringify(res));
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
if (usedNext) throw new Error(label + '的盘符根拼写不应 next——身份放行不得放行最小保护集');
|
|
963
|
+
if (!res || res.kind !== 'deny' || !res.reason.includes(denyMarker)) {
|
|
964
|
+
throw new Error(label + '的盘符根拼写应 deny(含 ' + denyMarker + ' 语义): ' + JSON.stringify(res));
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
console.log(' 盘符根拼写与盘符拼写同判(机器面 deny / 源码放行)✓');
|
|
968
|
+
} else {
|
|
969
|
+
outcomes.notApplicablePlatform('K11 ⑦i 最小保护集盘符根拼写断言',
|
|
970
|
+
'非 win32 载体上 `/d/<剩余>` 是普通 POSIX 绝对路径,保护集语义不同');
|
|
971
|
+
}
|
|
972
|
+
// ⑦j 桥接通道标记的作用域收紧(同型真实链路格):CC / Codex 生成的 hook **直接**调用守卫、
|
|
973
|
+
// 不经桥接——同一目标、同一深度值,经桥接 ⇒ 放行(⑦a 已覆盖),绕过桥接直接调守卫
|
|
974
|
+
// ⇒ 深度变量虽在场也不作数:守卫按协调者语义走白名单 ⇒ deny;同一调用补上通道标记
|
|
975
|
+
// ⇒ 按键控的身份通道放行(证明判据键在标记上,不是「直接调用一律拦」)。
|
|
976
|
+
// 反向构造:守卫去掉标记要求 ⇒ 继承形态必红(继承来的深度变量被当成子代理身份 ⇒ 源码写入放行)。
|
|
977
|
+
{
|
|
978
|
+
const guardAbs = path.join(longTarget, guardRel);
|
|
979
|
+
const probeInput = JSON.stringify({
|
|
980
|
+
tool_name: 'Write',
|
|
981
|
+
tool_input: { file_path: path.join(longTarget, 'src', 'c.mjs') },
|
|
982
|
+
});
|
|
983
|
+
const directEnvs = [
|
|
984
|
+
['继承形态(无通道标记)', { FLOW_COMET_AGENT_DEPTH: '1' }, 2],
|
|
985
|
+
['通道标记在场', { FLOW_COMET_AGENT_DEPTH: '1', FLOW_COMET_AGENT_DEPTH_SOURCE: 'dsh-bridge' }, 0],
|
|
986
|
+
['标记值不符(近形状)', { FLOW_COMET_AGENT_DEPTH: '1', FLOW_COMET_AGENT_DEPTH_SOURCE: 'dsh' }, 2],
|
|
987
|
+
];
|
|
988
|
+
for (const [label, overrides, expected] of directEnvs) {
|
|
989
|
+
const res = spawnSync(process.execPath, [guardAbs, 'before_tool'], {
|
|
990
|
+
cwd: longTarget,
|
|
991
|
+
input: probeInput,
|
|
992
|
+
env: { ...process.env, ...overrides },
|
|
993
|
+
encoding: 'utf8',
|
|
994
|
+
timeout: 60000,
|
|
995
|
+
});
|
|
996
|
+
const out = String(res.stdout || '') + String(res.stderr || '');
|
|
997
|
+
if ((res.status ?? 1) !== expected) {
|
|
998
|
+
throw new Error('桥接通道标记直接调用格「' + label + '」预期 exit ' + expected
|
|
999
|
+
+ ',实际 exit ' + String(res.status) + ': ' + out.trim().slice(0, 160));
|
|
1000
|
+
}
|
|
1001
|
+
if (expected === 2 && !out.includes('BLOCKED')) {
|
|
1002
|
+
throw new Error('桥接通道标记直接调用格「' + label + '」拦截报文缺 BLOCKED: ' + out.trim().slice(0, 160));
|
|
1003
|
+
}
|
|
1004
|
+
if (expected === 0 && !out.includes('(subagent identity)')) {
|
|
1005
|
+
throw new Error('桥接通道标记直接调用格「' + label + '」应走身份通道放行: ' + out.trim().slice(0, 160));
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
console.log(' 桥接通道标记:继承形态按协调者拦 / 标记在场走身份放行 / 近形状标记不认 ✓');
|
|
1009
|
+
}
|
|
1010
|
+
console.log(' bridge.apply 分派集成(子代理放行/协调者拦/越界·形状 deny 不受身份/保护集拦)✓');
|
|
1011
|
+
},
|
|
1012
|
+
},
|
|
1013
|
+
|
|
1014
|
+
// K12: 桥接 loader 流程态门断言(B 方案 TDD RED 载体——空闲/异常态行为面,与 K11 运行态分开):
|
|
1015
|
+
// AC-1 无 state 文件 / AC-2 activeChange 缺失 / AC-3 status completed → 项目外 Write 应
|
|
1016
|
+
// next() 放行(空闲放行语义由流程态门实现);AC-7 非法 JSON / AC-8 未知 status →
|
|
1017
|
+
// fail-closed deny(reason 含拒绝语义;解析失败/未知状态不得当空闲放行)。
|
|
1018
|
+
// 复用 K11 安装产物形态:target 为真实 dsh flow-comet 项目(.dsh/skills/flow-comet 就位,
|
|
1019
|
+
// 窄监听不跳过——保证当前实现走到包含性 deny 而非被窄监听放行);mock exec 取协调者身份
|
|
1020
|
+
// (delegationDepth 缺失=0,与 K11 ⑦b 同语义)。import 用 ?query 缓存破除——K11 已在本进程
|
|
1021
|
+
// apply 过,模块级幂等标记会让二次 apply 跳过(applied=true),必须全新模块实例才能捕获监听器。
|
|
1022
|
+
{
|
|
1023
|
+
name: 'K12 桥接 loader:流程态门断言(无state/无activeChange/completed放行·非法JSON/未知status deny)',
|
|
1024
|
+
surface: '桥接流程态门(active-change 门与 fail-closed 分支)断言仅权威源侧可判',
|
|
1025
|
+
run: async (dir) => {
|
|
1026
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
1027
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
1028
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
1029
|
+
const bridge = await import(pathToFileURL(loader).href + '?k12=' + Date.now()); // 全新模块实例(applied=false)
|
|
1030
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
1031
|
+
const dshHome = path.join(dir, 'k12-dsh-home');
|
|
1032
|
+
const target = path.join(dir, 'k12-target');
|
|
1033
|
+
fs.mkdirSync(target, { recursive: true });
|
|
1034
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
1035
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
1036
|
+
});
|
|
1037
|
+
if (res.status !== 0) throw new Error('dsh 安装失败: ' + (res.stderr || JSON.stringify(res.output)));
|
|
1038
|
+
// 窄监听不跳过前置:目标项目必须真的装有 .dsh/skills/flow-comet——否则当前实现会因窄监听
|
|
1039
|
+
// 直接 next(),空闲态断言假绿,RED 失真
|
|
1040
|
+
if (!fs.existsSync(path.join(target, '.dsh', 'skills', 'flow-comet', 'SKILL.md'))) {
|
|
1041
|
+
throw new Error('dsh flow-comet 项目安装缺失(.dsh/skills/flow-comet/SKILL.md 不存在)');
|
|
1042
|
+
}
|
|
1043
|
+
gitInit(target);
|
|
1044
|
+
const longTarget = bridge.realpathExistingPath(target); // 8.3 短形态归一为长形态(与 K11 一致)
|
|
1045
|
+
const outside = path.join(dir, 'k12-out');
|
|
1046
|
+
fs.mkdirSync(outside, { recursive: true });
|
|
1047
|
+
// 项目外 Write(协调者身份——delegationDepth 缺失=0)
|
|
1048
|
+
const mkOutsideWrite = () => ({
|
|
1049
|
+
name: 'Write',
|
|
1050
|
+
arguments: { file_path: path.join(outside, 'idle-leak.md') },
|
|
1051
|
+
agent: { cwd: longTarget, session: { header: {} } },
|
|
1052
|
+
});
|
|
1053
|
+
const ctxEvents = {};
|
|
1054
|
+
bridge.apply({ on: (event, fn) => { ctxEvents[event] = fn; } });
|
|
1055
|
+
const preExec = ctxEvents['tools/pre-execute'];
|
|
1056
|
+
if (typeof preExec !== 'function') throw new Error('bridge.apply 应注册 tools/pre-execute 监听器');
|
|
1057
|
+
const statePath = path.join(target, '.flow-comet', 'flow-comet-state.json');
|
|
1058
|
+
// AC-1 无 state:无 .flow-comet/flow-comet-state.json + 项目外 Write → next() 放行(空闲放行已生效)
|
|
1059
|
+
{
|
|
1060
|
+
if (fs.existsSync(statePath)) throw new Error('AC-1 前置:目标项目不应有 state 文件');
|
|
1061
|
+
let usedNext = false;
|
|
1062
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1063
|
+
if (!usedNext) throw new Error('AC-1 无 state 应放行(next 被调): ' + JSON.stringify(r));
|
|
1064
|
+
if (r) throw new Error('AC-1 无 state 不应返回 deny: ' + JSON.stringify(r));
|
|
1065
|
+
}
|
|
1066
|
+
// AC-2 无 activeChange:writeState { activeChange:null, status:'running' } + 项目外 Write → next()(空闲放行已生效)
|
|
1067
|
+
{
|
|
1068
|
+
writeState(target, { activeChange: null, status: 'running' });
|
|
1069
|
+
let usedNext = false;
|
|
1070
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1071
|
+
if (!usedNext) throw new Error('AC-2 activeChange 为空应放行(next 被调): ' + JSON.stringify(r));
|
|
1072
|
+
if (r) throw new Error('AC-2 无 activeChange 不应返回 deny: ' + JSON.stringify(r));
|
|
1073
|
+
}
|
|
1074
|
+
// AC-3 completed:writeState { activeChange:'x', status:'completed' } + 项目外 Write → next()(空闲放行已生效)
|
|
1075
|
+
{
|
|
1076
|
+
writeState(target, { activeChange: 'x', status: 'completed' });
|
|
1077
|
+
let usedNext = false;
|
|
1078
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1079
|
+
if (!usedNext) throw new Error('AC-3 status completed 应放行(next 被调): ' + JSON.stringify(r));
|
|
1080
|
+
if (r) throw new Error('AC-3 completed 不应返回 deny: ' + JSON.stringify(r));
|
|
1081
|
+
}
|
|
1082
|
+
// AC-7 解析失败:state 写成非法 JSON + 项目外 Write → deny(reason 含 fail-closed 拒绝语义;
|
|
1083
|
+
// 当前无条件 deny 已通过、T02 流程态门落地后必须保持 deny,不得当空闲放行)
|
|
1084
|
+
{
|
|
1085
|
+
writeFile(target, '.flow-comet/flow-comet-state.json', '{ not-valid-json ');
|
|
1086
|
+
let usedNext = false;
|
|
1087
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1088
|
+
if (usedNext) throw new Error('AC-7 非法 JSON 不应 next——解析失败必须 fail-closed deny');
|
|
1089
|
+
if (!r || r.kind !== 'deny') throw new Error('AC-7 非法 JSON 应 deny: ' + JSON.stringify(r));
|
|
1090
|
+
if (!r.reason.includes('拒绝')) throw new Error('AC-7 deny 应含 fail-closed(拒绝)语义: ' + JSON.stringify(r.reason));
|
|
1091
|
+
}
|
|
1092
|
+
// AC-8 未知 status:writeState { activeChange:'x', status:'weird' } + 项目外 Write → deny
|
|
1093
|
+
// (当前通过、T02 流程态门落地后必须保持 deny,不得当空闲放行)
|
|
1094
|
+
{
|
|
1095
|
+
writeState(target, { activeChange: 'x', status: 'weird' });
|
|
1096
|
+
let usedNext = false;
|
|
1097
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1098
|
+
if (usedNext) throw new Error('AC-8 未知 status 不应 next——未知 status 必须 fail-closed deny');
|
|
1099
|
+
if (!r || r.kind !== 'deny') throw new Error('AC-8 未知 status 应 deny: ' + JSON.stringify(r));
|
|
1100
|
+
if (!r.reason.includes('拒绝')) throw new Error('AC-8 deny 应含 fail-closed(拒绝)语义: ' + JSON.stringify(r.reason));
|
|
1101
|
+
}
|
|
1102
|
+
// AC-7b 非对象 JSON(null/标量/数组/字符串):合法 JSON 但非对象 = 损坏 state → deny(fail-closed)
|
|
1103
|
+
for (const bad of [null, 123, ['x'], 'str']) {
|
|
1104
|
+
writeFile(target, '.flow-comet/flow-comet-state.json',JSON.stringify(bad));
|
|
1105
|
+
let usedNext = false;
|
|
1106
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1107
|
+
if (usedNext) throw new Error('AC-7b 非对象 state(' + JSON.stringify(bad) + ') 不应 next——必须 fail-closed deny');
|
|
1108
|
+
if (!r || r.kind !== 'deny') throw new Error('AC-7b 非对象 state 应 deny: ' + JSON.stringify(r));
|
|
1109
|
+
if (!r.reason.includes('拒绝')) throw new Error('AC-7b deny 应含 fail-closed(拒绝)语义: ' + JSON.stringify(r.reason));
|
|
1110
|
+
}
|
|
1111
|
+
// AC-3b BOM 前缀 + completed:BOM 容错断言——应被解析并视为空闲放行
|
|
1112
|
+
{
|
|
1113
|
+
writeFile(target, '.flow-comet/flow-comet-state.json', '\uFEFF' + JSON.stringify({ activeChange: 'x', status: 'completed' }));
|
|
1114
|
+
let usedNext = false;
|
|
1115
|
+
const r = await preExec(mkOutsideWrite(), () => { usedNext = true; });
|
|
1116
|
+
if (!usedNext) throw new Error('AC-3b BOM+completed 应放行(next 被调)——BOM 容错: ' + JSON.stringify(r));
|
|
1117
|
+
if (r) throw new Error('AC-3b BOM+completed 不应返回 deny: ' + JSON.stringify(r));
|
|
1118
|
+
}
|
|
1119
|
+
console.log(' 流程态门断言:AC-1/2/3 空闲放行·AC-3b BOM 容错·AC-7/8/7b 异常态 fail-closed deny✓');
|
|
1120
|
+
},
|
|
1121
|
+
},
|
|
1122
|
+
|
|
1123
|
+
// hook 注入形态无关断言 + 旧条目幂等升级(AC-5/AC-7;安装产物根引用特征用例 + 升级替换用例)——
|
|
1124
|
+
// 安装产物托管条目满足「脚本 basename 在位 + 项目根引用特征(CLAUDE_PROJECT_DIR 或绝对路径)
|
|
1125
|
+
// 二选一」;新旧托管形态识别兼容(basename 同源 isManagedHookCommand 语义);把托管命令改回
|
|
1126
|
+
// 旧相对路径形态后重跑安装器 → 被识别替换(恰余 1 条,不重复不残留)且新条目形态达标。
|
|
1127
|
+
// 断言不锁定 %VAR% 引号/变量词形——注入命令实测微调不再破坏套件。
|
|
1128
|
+
{
|
|
1129
|
+
name: 'K13 安装器:hook 注入形态无关断言(basename+项目根引用)+旧条目幂等升级',
|
|
1130
|
+
surface: 'hook 注入形态与幂等升级断言仅权威源侧可判',
|
|
1131
|
+
run: (dir) => {
|
|
1132
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
1133
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
1134
|
+
// ① 形态分类器:新旧托管形态均被 basename 识别;项目根引用特征仅根引用形态命中
|
|
1135
|
+
for (const legacy of [
|
|
1136
|
+
'node .claude/skills/flow-comet/scripts/comet-hook-guard.mjs',
|
|
1137
|
+
'node .agents/skills/flow-comet/scripts/comet-hook-guard.mjs before_tool --platform codex',
|
|
1138
|
+
]) {
|
|
1139
|
+
if (!isManagedHookCommand(legacy)) throw new Error('旧托管形态应被 basename 识别(幂等升级识别面): ' + legacy);
|
|
1140
|
+
if (hasProjectRootRef(legacy)) throw new Error('旧相对路径形态不应命中项目根引用特征: ' + legacy);
|
|
1141
|
+
}
|
|
1142
|
+
for (const modern of [
|
|
1143
|
+
'node "%CLAUDE_PROJECT_DIR%/.claude/skills/flow-comet/scripts/comet-hook-guard.mjs"',
|
|
1144
|
+
'node "$env:CLAUDE_PROJECT_DIR/.claude/skills/flow-comet/scripts/comet-hook-guard.mjs"',
|
|
1145
|
+
'node "${CLAUDE_PROJECT_DIR}/.claude/skills/flow-comet/scripts/comet-hook-guard.mjs"',
|
|
1146
|
+
]) {
|
|
1147
|
+
assertManagedHookEntry(modern, '变量引用形态');
|
|
1148
|
+
}
|
|
1149
|
+
assertManagedHookEntry(path.join(dir, 'k13-abs', '.claude', 'skills', 'flow-comet', 'scripts', 'comet-hook-guard.mjs'), '绝对路径形态');
|
|
1150
|
+
// ② claude-code 安装产物:托管条目在场且指向本项目脚本(项目根引用特征在位)
|
|
1151
|
+
const ccTarget = path.join(dir, 'k13-cc');
|
|
1152
|
+
fs.mkdirSync(ccTarget, { recursive: true });
|
|
1153
|
+
const rcc = spawnSync(process.execPath, [installer, '--target', ccTarget, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
1154
|
+
if (rcc.status !== 0) throw new Error('claude-code 安装失败: ' + (rcc.stderr || JSON.stringify(rcc.output)));
|
|
1155
|
+
const ccSettingsPath = path.join(ccTarget, '.claude', 'settings.local.json');
|
|
1156
|
+
const ccSettings = JSON.parse(fs.readFileSync(ccSettingsPath, 'utf8'));
|
|
1157
|
+
const ccManaged = collectManagedHookCommands(ccSettings);
|
|
1158
|
+
if (ccManaged.length === 0) throw new Error('.claude/settings.local.json 未注入托管 hook 条目');
|
|
1159
|
+
for (const c of ccManaged) assertManagedHookEntry(c, 'claude-code 托管命令');
|
|
1160
|
+
// ③ 可执行性(补「形态可识别但不可执行」盲区):识别层与执行层是两个维度——
|
|
1161
|
+
// 前者只要求 basename 可辨,后者要求命令在 CC 的真实执行语义下可达。CC 全平台以
|
|
1162
|
+
// /bin/bash -c 执行 hook 命令(含 Windows),并注入 CLAUDE_PROJECT_DIR 环境变量;
|
|
1163
|
+
// cmd 变量语法 %VAR% 在 bash 下既不展开、路径反斜杠还会被转义吞掉 → node 静默
|
|
1164
|
+
// 找不到脚本(fail-open,仅报 non-blocking hook error)。
|
|
1165
|
+
// (a) 形态约束(全平台可判):托管命令不得含 %VAR% cmd 语法;
|
|
1166
|
+
// (b) 端到端(需 bash):以 bash -c + 注入 CLAUDE_PROJECT_DIR 跑完整命令,验证可达;
|
|
1167
|
+
// 无 bash 环境 → 显式失败(未验证不等于通过——宿主本身以 bash 语义执行 hook,
|
|
1168
|
+
// 不具备 bash 的环境无法完成本验证)。
|
|
1169
|
+
for (const c of ccManaged) {
|
|
1170
|
+
if (/%[A-Za-z_][A-Za-z0-9_]*%/.test(c)) {
|
|
1171
|
+
throw new Error('claude-code 托管命令含 cmd 变量语法(%VAR%)——bash 执行语义下不可用: ' + c);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
const hasBash = (() => {
|
|
1175
|
+
try {
|
|
1176
|
+
return spawnSync('bash', ['--version'], { encoding: 'utf8', timeout: 30000 }).status === 0;
|
|
1177
|
+
} catch {
|
|
1178
|
+
return false;
|
|
1179
|
+
}
|
|
1180
|
+
})();
|
|
1181
|
+
if (hasBash) {
|
|
1182
|
+
for (const c of ccManaged) {
|
|
1183
|
+
const execRes = spawnSync('bash', ['-c', c], {
|
|
1184
|
+
cwd: ccTarget,
|
|
1185
|
+
encoding: 'utf8',
|
|
1186
|
+
timeout: 60000,
|
|
1187
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: ccTarget },
|
|
1188
|
+
input: JSON.stringify({ tool_name: 'Write', tool_input: { file_path: path.join(ccTarget, 'k13-probe.txt') } }),
|
|
1189
|
+
});
|
|
1190
|
+
if (execRes.status !== 0 || /Cannot find module/.test(String(execRes.stderr || ''))) {
|
|
1191
|
+
throw new Error(
|
|
1192
|
+
'claude-code 托管命令在 CC 执行语义(bash -c + CLAUDE_PROJECT_DIR)下不可执行: ' +
|
|
1193
|
+
c + ' —— ' + String(execRes.stderr || '').split('\n').slice(0, 3).join(' | ')
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
} else {
|
|
1198
|
+
throw new Error(
|
|
1199
|
+
'K13 端到端执行探针需要 bash(宿主以 bash 语义执行 hook 命令),当前环境 PATH 中不可用——' +
|
|
1200
|
+
'无法验证生成命令可执行;请在具备 bash 的环境运行套件(Windows 请确认 Git Bash 在 PATH 中)'
|
|
1201
|
+
);
|
|
1202
|
+
}
|
|
1203
|
+
// ③b 含空格项目根路径:同一端到端验证——未加引号的命令在变量展开后的空格处被词法分词
|
|
1204
|
+
// 切断(静默失效,与反斜杠转义同类);含引号形态须可执行,且仍被幂等识别(识别层去引号)。
|
|
1205
|
+
const ccSpaceTarget = path.join(dir, 'k13 cc space');
|
|
1206
|
+
fs.mkdirSync(ccSpaceTarget, { recursive: true });
|
|
1207
|
+
const rccSpace = spawnSync(process.execPath, [installer, '--target', ccSpaceTarget, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
1208
|
+
if (rccSpace.status !== 0) throw new Error('claude-code 含空格路径安装失败: ' + (rccSpace.stderr || ''));
|
|
1209
|
+
const ccSpaceManaged = collectManagedHookCommands(JSON.parse(fs.readFileSync(path.join(ccSpaceTarget, '.claude', 'settings.local.json'), 'utf8')));
|
|
1210
|
+
if (ccSpaceManaged.length === 0) throw new Error('含空格路径下未注入托管 hook 条目');
|
|
1211
|
+
for (const c of ccSpaceManaged) {
|
|
1212
|
+
assertManagedHookEntry(c, '含空格路径托管命令');
|
|
1213
|
+
const execRes = spawnSync('bash', ['-c', c], {
|
|
1214
|
+
cwd: ccSpaceTarget,
|
|
1215
|
+
encoding: 'utf8',
|
|
1216
|
+
timeout: 60000,
|
|
1217
|
+
env: { ...process.env, CLAUDE_PROJECT_DIR: ccSpaceTarget },
|
|
1218
|
+
input: JSON.stringify({ tool_name: 'Write', tool_input: { file_path: path.join(ccSpaceTarget, 'k13-space-probe.txt') } }),
|
|
1219
|
+
});
|
|
1220
|
+
if (execRes.status !== 0 || /Cannot find module/.test(String(execRes.stderr || ''))) {
|
|
1221
|
+
throw new Error(
|
|
1222
|
+
'含空格路径托管命令在 CC 执行语义下不可执行: ' + c +
|
|
1223
|
+
' —— ' + String(execRes.stderr || '').split('\n').slice(0, 3).join(' | ')
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
// ④ 幂等升级(claude-code):托管命令改回旧相对路径形态 → 重跑安装器 → 识别替换:
|
|
1228
|
+
// 恰余 1 条托管条目(无重复无残留)且形态达标(项目根引用特征在位)
|
|
1229
|
+
for (const g of ccSettings.hooks.PreToolUse) {
|
|
1230
|
+
for (const h of (Array.isArray(g.hooks) ? g.hooks : [])) {
|
|
1231
|
+
if (isManagedHookCommand(h.command)) h.command = 'node .claude/skills/flow-comet/scripts/comet-hook-guard.mjs';
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
fs.writeFileSync(ccSettingsPath, JSON.stringify(ccSettings, null, 2) + '\n', 'utf8');
|
|
1235
|
+
const rccUp = spawnSync(process.execPath, [installer, '--target', ccTarget, '--platform', 'claude-code'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
1236
|
+
if (rccUp.status !== 0) throw new Error('claude-code 升级重装失败: ' + (rccUp.stderr || JSON.stringify(rccUp.output)));
|
|
1237
|
+
const ccAfter = collectManagedHookCommands(JSON.parse(fs.readFileSync(ccSettingsPath, 'utf8')));
|
|
1238
|
+
if (ccAfter.length !== 1) throw new Error('claude-code 升级后应恰余 1 条托管条目(旧条目被替换): ' + JSON.stringify(ccAfter));
|
|
1239
|
+
assertManagedHookEntry(ccAfter[0], 'claude-code 升级后托管命令');
|
|
1240
|
+
// ⑤ codex 平台同断言:安装产物形态达标 + 旧条目幂等升级
|
|
1241
|
+
const cxTarget = path.join(dir, 'k13-codex');
|
|
1242
|
+
fs.mkdirSync(cxTarget, { recursive: true });
|
|
1243
|
+
const rcx = spawnSync(process.execPath, [installer, '--target', cxTarget, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
1244
|
+
if (rcx.status !== 0) throw new Error('codex 安装失败: ' + (rcx.stderr || JSON.stringify(rcx.output)));
|
|
1245
|
+
const cxHooksPath = path.join(cxTarget, '.codex', 'hooks.json');
|
|
1246
|
+
const cxHooks = JSON.parse(fs.readFileSync(cxHooksPath, 'utf8'));
|
|
1247
|
+
const cxManaged = collectManagedHookCommands(cxHooks);
|
|
1248
|
+
if (cxManaged.length === 0) throw new Error('.codex/hooks.json 未注入托管 hook 条目');
|
|
1249
|
+
for (const c of cxManaged) assertManagedHookEntry(c, 'codex 托管命令');
|
|
1250
|
+
for (const g of cxHooks.hooks.PreToolUse) {
|
|
1251
|
+
for (const h of (Array.isArray(g.hooks) ? g.hooks : [])) {
|
|
1252
|
+
if (isManagedHookCommand(h.command)) h.command = 'node .agents/skills/flow-comet/scripts/comet-hook-guard.mjs before_tool --platform codex';
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
fs.writeFileSync(cxHooksPath, JSON.stringify(cxHooks, null, 2) + '\n', 'utf8');
|
|
1256
|
+
const rcxUp = spawnSync(process.execPath, [installer, '--target', cxTarget, '--platform', 'codex'], { cwd: repoRoot, encoding: 'utf8', timeout: 120000 });
|
|
1257
|
+
if (rcxUp.status !== 0) throw new Error('codex 升级重装失败: ' + (rcxUp.stderr || JSON.stringify(rcxUp.output)));
|
|
1258
|
+
const cxAfter = collectManagedHookCommands(JSON.parse(fs.readFileSync(cxHooksPath, 'utf8')));
|
|
1259
|
+
if (cxAfter.length !== 1) throw new Error('codex 升级后应恰余 1 条托管条目(旧条目被替换): ' + JSON.stringify(cxAfter));
|
|
1260
|
+
assertManagedHookEntry(cxAfter[0], 'codex 升级后托管命令');
|
|
1261
|
+
console.log(' hook 注入形态无关断言(basename+项目根引用特征)+ 新旧幂等升级 ✓');
|
|
1262
|
+
},
|
|
1263
|
+
},
|
|
1264
|
+
|
|
1265
|
+
// K14~K16: 桥接 loader 版本戳重装断言(安装器覆盖前版本比对明示的系统级锚定)——
|
|
1266
|
+
// 临时 DSH_HOME 预置不同版本戳 loader,真实安装器重装断言方向措辞(升级/降级/版本一致)、
|
|
1267
|
+
// 覆盖后磁盘为源新戳、同源重装收敛、purge 清除重建后路径回归。
|
|
1268
|
+
{
|
|
1269
|
+
name: 'K14 桥接 loader:版本戳重装断言(升级/降级方向与覆盖后新戳)',
|
|
1270
|
+
distributionSurface: true,
|
|
1271
|
+
run: (dir) => {
|
|
1272
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
1273
|
+
const pkgCtx = resolvePackageContext();
|
|
1274
|
+
// 分发面回传契约夹具先跑(环境无关):在副本 early-return 之前由夹具驱动断言——
|
|
1275
|
+
// copy 文案/marker 与三类失败路径负例在权威源与安装副本两侧都真跑,不依赖「装到副本才验证」。
|
|
1276
|
+
assertDistributionOutcomeContract(dir);
|
|
1277
|
+
const marker = DISTRIBUTION_MARKERS[pkgCtx.kind];
|
|
1278
|
+
if (pkgCtx.kind === 'copy') {
|
|
1279
|
+
const outcome = copyDistributionOutcome(pkgCtx, K14_COPY_DETAIL);
|
|
1280
|
+
console.log(outcome.message);
|
|
1281
|
+
return outcome.marker;
|
|
1282
|
+
}
|
|
1283
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
1284
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
1285
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
1286
|
+
const srcVersion = readBridgeSourceVersion(loader);
|
|
1287
|
+
// 权威基准 = INSTALLED_VERSION:loader 标记行须与其同值——跨文件分叉时安装副本的
|
|
1288
|
+
// bridge-check 会报版本偏斜(本守卫让系统测试独立捕获分叉,不依赖另一套件)。夹具派生以该基准为准。
|
|
1289
|
+
const installedVersion = fs.readFileSync(path.join(SCRIPTS_DIR, '..', 'INSTALLED_VERSION'), 'utf8').trim();
|
|
1290
|
+
if (srcVersion !== installedVersion) {
|
|
1291
|
+
throw new Error(
|
|
1292
|
+
'loader 版本戳(' + srcVersion + ')与 INSTALLED_VERSION(' + installedVersion +
|
|
1293
|
+
')不一致——发布同步遗漏(bridge-check 会在安装副本报版本偏斜)'
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1296
|
+
// 旧戳/新戳从基准动态派生(固定值会随发布线演进失效——预发布语义下同主版本比较
|
|
1297
|
+
// 可能反转,如 1.5.0-rc.2 < 1.5.0):
|
|
1298
|
+
// 旧戳 = 主版本低一位 → 恒定「升级」方向;
|
|
1299
|
+
// 新戳 = 同主版本、次版本 +5 → 数值序高于源而字典序低于源(保持「数值序比较」护栏:
|
|
1300
|
+
// 字典序会误判为升级,断言「降级」即语义正确;与实现侧冒烟同口径)
|
|
1301
|
+
const [srcMajor, srcMinor] = installedVersion.split('.').map((p) => parseInt(p, 10));
|
|
1302
|
+
const OLD_STAMP = (srcMajor - 1) + '.0.0';
|
|
1303
|
+
const NEWER_STAMP = srcMajor + '.' + (srcMinor + 5) + '.0';
|
|
1304
|
+
const dshHome = path.join(dir, 'k14-dsh-home');
|
|
1305
|
+
const target = path.join(dir, 'k14-target');
|
|
1306
|
+
fs.mkdirSync(target, { recursive: true });
|
|
1307
|
+
seedForeignFlowKit(target);
|
|
1308
|
+
const pluginPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
1309
|
+
const runInstall = () => {
|
|
1310
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
1311
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
1312
|
+
});
|
|
1313
|
+
// 统一包装为 { status, output }(与 runState 等助手同形态——assertOut 期望字符串 output)
|
|
1314
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
1315
|
+
};
|
|
1316
|
+
// ① 已装旧戳 loader → 重装输出「升级 A → B」逐字措辞 + 覆盖后磁盘为源新戳
|
|
1317
|
+
fs.mkdirSync(path.dirname(pluginPath), { recursive: true });
|
|
1318
|
+
fs.writeFileSync(pluginPath, stampBridgeLoader(loader, OLD_STAMP), 'utf8');
|
|
1319
|
+
const upgrade = runInstall();
|
|
1320
|
+
assertExit(upgrade, 0);
|
|
1321
|
+
assertOut(upgrade, '桥接 loader 升级 ' + OLD_STAMP + ' → ' + srcVersion);
|
|
1322
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
1323
|
+
throw new Error('升级重装后已装 loader 应被覆盖为源新戳');
|
|
1324
|
+
}
|
|
1325
|
+
// ② 已装新戳 loader(数值序高于源)→ 重装输出「降级 A → B」+ 覆盖后仍为源戳
|
|
1326
|
+
fs.writeFileSync(pluginPath, stampBridgeLoader(loader, NEWER_STAMP), 'utf8');
|
|
1327
|
+
const downgrade = runInstall();
|
|
1328
|
+
assertExit(downgrade, 0);
|
|
1329
|
+
assertOut(downgrade, '桥接 loader 降级 ' + NEWER_STAMP + ' → ' + srcVersion);
|
|
1330
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
1331
|
+
throw new Error('降级重装后已装 loader 应为源戳');
|
|
1332
|
+
}
|
|
1333
|
+
// ③④ 安装副本载体版本比较语义(dev 态同基础健康 / 基础失配仍 FAIL)——助手单点实现
|
|
1334
|
+
assertInstalledCopyBridgeVersionSemantics(target, dshHome, srcVersion);
|
|
1335
|
+
console.log(' 版本戳重装断言:升级/降级方向措辞与覆盖后新戳 ✓');
|
|
1336
|
+
console.log(' dev 态载体版本比较:同基础健康 + 基础失配仍 FAIL ✓');
|
|
1337
|
+
console.log(' source 形态:既有断言全跑 → ' + marker + '(copy 文案/marker 与失败路径负例已由分发面回传契约夹具断言)✓');
|
|
1338
|
+
return marker;
|
|
1339
|
+
},
|
|
1340
|
+
},
|
|
1341
|
+
|
|
1342
|
+
{
|
|
1343
|
+
name: 'K15 桥接 loader:同源重装断言(首次安装后版本一致)',
|
|
1344
|
+
surface: '同源重装后的 loader 版本一致断言仅权威源侧可判',
|
|
1345
|
+
run: (dir) => {
|
|
1346
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
1347
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
1348
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
1349
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
1350
|
+
const srcVersion = readBridgeSourceVersion(loader);
|
|
1351
|
+
const dshHome = path.join(dir, 'k15-dsh-home');
|
|
1352
|
+
const target = path.join(dir, 'k15-target');
|
|
1353
|
+
fs.mkdirSync(target, { recursive: true });
|
|
1354
|
+
seedForeignFlowKit(target);
|
|
1355
|
+
const pluginPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
1356
|
+
const runInstall = () => {
|
|
1357
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh'], {
|
|
1358
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
1359
|
+
});
|
|
1360
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
1361
|
+
};
|
|
1362
|
+
// ① 无已装 loader → 首次安装措辞(比对的空态前提)
|
|
1363
|
+
const first = runInstall();
|
|
1364
|
+
assertExit(first, 0);
|
|
1365
|
+
assertOut(first, '桥接 loader 首次安装');
|
|
1366
|
+
if (!fs.existsSync(pluginPath)) throw new Error('首次安装后 loader 应就位');
|
|
1367
|
+
// ② 同源重装(已装 = 源版本)→ 「版本一致(A)」逐字措辞 + 磁盘保持源戳
|
|
1368
|
+
const re = runInstall();
|
|
1369
|
+
assertExit(re, 0);
|
|
1370
|
+
assertOut(re, '桥接 loader 版本一致(' + srcVersion + ')');
|
|
1371
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
1372
|
+
throw new Error('同源重装后 loader 应保持源戳');
|
|
1373
|
+
}
|
|
1374
|
+
console.log(' 同源重装断言:首次安装 → 版本一致收敛 ✓');
|
|
1375
|
+
},
|
|
1376
|
+
},
|
|
1377
|
+
|
|
1378
|
+
{
|
|
1379
|
+
name: 'K16 桥接 loader:purge 后重建路径回归(loader 恢复与再装收敛)',
|
|
1380
|
+
surface: 'purge 后 loader 重建路径回归断言仅权威源侧可判',
|
|
1381
|
+
run: (dir) => {
|
|
1382
|
+
const repoRoot = path.resolve(SCRIPTS_DIR, '..', '..', '..', '..');
|
|
1383
|
+
const installer = path.join(repoRoot, 'scripts', 'prepare-env.mjs');
|
|
1384
|
+
const loader = path.join(repoRoot, 'scripts', 'dsh-bridge.mjs');
|
|
1385
|
+
if (!fs.existsSync(loader)) throw new Error('缺少 scripts/dsh-bridge.mjs');
|
|
1386
|
+
const srcVersion = readBridgeSourceVersion(loader);
|
|
1387
|
+
const dshHome = path.join(dir, 'k16-dsh-home');
|
|
1388
|
+
const target = path.join(dir, 'k16-target');
|
|
1389
|
+
fs.mkdirSync(target, { recursive: true });
|
|
1390
|
+
seedForeignFlowKit(target);
|
|
1391
|
+
const pluginPath = path.join(dshHome, 'plugins', 'dsh-flow-comet-bridge.mjs');
|
|
1392
|
+
const patchPath = path.join(dshHome, 'cordis.patch.yml');
|
|
1393
|
+
const runInstall = (extra) => {
|
|
1394
|
+
const res = spawnSync(process.execPath, [installer, '--target', target, '--platform', 'dsh', ...(extra || [])], {
|
|
1395
|
+
cwd: repoRoot, encoding: 'utf8', timeout: 120000, env: { ...process.env, DSH_HOME: dshHome },
|
|
1396
|
+
});
|
|
1397
|
+
return { status: res.status ?? 1, output: String(res.stdout || '') + String(res.stderr || '') };
|
|
1398
|
+
};
|
|
1399
|
+
// ① 首次安装:loader 与 cordis.patch.yml 托管块就位
|
|
1400
|
+
const first = runInstall();
|
|
1401
|
+
assertExit(first, 0);
|
|
1402
|
+
if (!fs.existsSync(pluginPath)) throw new Error('首次安装后 loader 应就位');
|
|
1403
|
+
if (!fs.existsSync(patchPath)) throw new Error('首次安装后 cordis.patch.yml 应就位');
|
|
1404
|
+
// ② purge --yes:dsh 平台生成物清除后删除并重建——loader/托管块恢复为源形态
|
|
1405
|
+
const purgeRes = runInstall(['--purge', '--yes']);
|
|
1406
|
+
assertExit(purgeRes, 0);
|
|
1407
|
+
if (!fs.existsSync(pluginPath)) throw new Error('purge 后 loader 应重建恢复');
|
|
1408
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
1409
|
+
throw new Error('purge 后 loader 应为源戳');
|
|
1410
|
+
}
|
|
1411
|
+
if (!fs.existsSync(patchPath)) throw new Error('purge 后 cordis.patch.yml 托管块应重建');
|
|
1412
|
+
// ③ 清除后再重装:成功且「版本一致」收敛(重建成果可复装、版本比对路径回归)
|
|
1413
|
+
const re = runInstall();
|
|
1414
|
+
assertExit(re, 0);
|
|
1415
|
+
assertOut(re, '桥接 loader 版本一致(' + srcVersion + ')');
|
|
1416
|
+
if (!fs.readFileSync(pluginPath, 'utf8').includes('// BRIDGE_VERSION: ' + srcVersion)) {
|
|
1417
|
+
throw new Error('purge 后再装后 loader 应为源戳');
|
|
1418
|
+
}
|
|
1419
|
+
console.log(' purge 后重建路径回归:loader/托管块恢复 + 再装版本一致收敛 ✓');
|
|
1420
|
+
},
|
|
1421
|
+
},
|
|
1422
|
+
];
|
|
1423
|
+
|
|
1424
|
+
export const ENTRIES = RAW_ENTRIES.map(applyDistributionSurface);
|
|
1425
|
+
|
|
1426
|
+
// 套件运行器从本文件取用的三个符号:转出 lib 的单源实现(转出枢纽;定义处只有一处)。
|
|
1427
|
+
export { assertDistributionOutcome, createOutcomeRecorder, distributionState } from '../lib/system-fixtures.mjs';
|