flow-comet 1.6.3 → 1.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -1
- package/.flow-comet/skills/flow-comet/SKILL.md +21 -3
- package/.flow-comet/skills/flow-comet/reference/artifact-template-overlay.md +154 -0
- package/.flow-comet/skills/flow-comet/reference/artifact-templates.json +387 -0
- package/.flow-comet/skills/flow-comet/reference/entry-detail.md +8 -2
- package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +22 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +11 -13
- package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +2 -2
- package/.flow-comet/skills/flow-comet/scripts/evolve.mjs +180 -6
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +69 -18878
- package/.flow-comet/skills/flow-comet/scripts/health.mjs +29 -5
- package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +1053 -0
- package/.flow-comet/skills/flow-comet/scripts/items/b.mjs +830 -0
- package/.flow-comet/skills/flow-comet/scripts/items/c.mjs +781 -0
- package/.flow-comet/skills/flow-comet/scripts/items/d.mjs +99 -0
- package/.flow-comet/skills/flow-comet/scripts/items/e.mjs +185 -0
- package/.flow-comet/skills/flow-comet/scripts/items/f.mjs +90 -0
- package/.flow-comet/skills/flow-comet/scripts/items/g.mjs +59 -0
- package/.flow-comet/skills/flow-comet/scripts/items/h.mjs +817 -0
- package/.flow-comet/skills/flow-comet/scripts/items/i.mjs +77 -0
- package/.flow-comet/skills/flow-comet/scripts/items/j.mjs +75 -0
- package/.flow-comet/skills/flow-comet/scripts/items/k.mjs +1427 -0
- package/.flow-comet/skills/flow-comet/scripts/items/l.mjs +275 -0
- package/.flow-comet/skills/flow-comet/scripts/items/m.mjs +618 -0
- package/.flow-comet/skills/flow-comet/scripts/items/n.mjs +154 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/count-derivation.mjs +97 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/fix-loop-node.mjs +196 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-artifact-skeleton.mjs +406 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-evidence-gates.mjs +756 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +1235 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +384 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-state-io.mjs +404 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-template-fidelity.mjs +714 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +204 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/reentry-node.mjs +237 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/replan-node.mjs +209 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-branch.mjs +49 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-bridgecheck.mjs +201 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-admin.mjs +179 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +714 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-recovery.mjs +578 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-determine.mjs +52 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-evolve-hint.mjs +55 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-explain.mjs +123 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-intelscan.mjs +58 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-io.mjs +147 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-skillmarkers.mjs +249 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +1505 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/task-graph.mjs +471 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-assertions.mjs +509 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-fixtures.mjs +1248 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-helpers.mjs +237 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/text-anchor-checks.mjs +2493 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +214 -2
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +72 -1123
- package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +976 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/b.mjs +1233 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/c.mjs +1382 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/d.mjs +483 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/e.mjs +1398 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +1466 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +1367 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/h.mjs +1287 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/i.mjs +1436 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/j.mjs +1384 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/k.mjs +1338 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/l.mjs +1492 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +971 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/n.mjs +2125 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +1 -1
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +59 -7559
- package/.flow-comet/skills/flow-comet/scripts/time-utils.mjs +4 -4
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +223 -2476
- package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +5 -5
- package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +27 -2190
- package/.flow-comet/skills/flow-comet-archive/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-change/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-design/SKILL.md +15 -8
- package/.flow-comet/skills/flow-comet-dev/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-evolve/SKILL.md +1 -0
- package/.flow-comet/skills/flow-comet-execute/SKILL.md +13 -10
- package/.flow-comet/skills/flow-comet-health/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-integration/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +14 -4
- package/.flow-comet/skills/flow-comet-open/SKILL.md +11 -9
- package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-plan/SKILL.md +11 -7
- package/.flow-comet/skills/flow-comet-requirement/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-review/SKILL.md +15 -9
- package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +11 -8
- package/.flow-comet/skills/flow-comet-task/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-test/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +2 -2
- package/.flow-comet/skills/flow-comet-verify/SKILL.md +12 -8
- package/README.md +52 -69
- package/package.json +3 -2
- package/scripts/dsh-bridge.mjs +2 -2
- package/scripts/lib/installer-cleanup.mjs +152 -0
- package/scripts/lib/installer-flowkit.mjs +159 -0
- package/scripts/lib/installer-hooks.mjs +363 -0
- package/scripts/lib/installer-loader.mjs +220 -0
- package/scripts/lib/installer-migrate.mjs +236 -0
- package/scripts/lib/installer-platforms.mjs +314 -0
- package/scripts/prepare-env.mjs +21 -1313
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* installer-flowkit.mjs — flow-kit 上游快照获取链路(prepare-env 的逻辑层模块)
|
|
3
|
+
*
|
|
4
|
+
* 非破坏是这条链路的全部要点:已存在的目录一律只读不动(只读 HEAD、只读 origin 判定归属),
|
|
5
|
+
* 归属不可确认即跳过并给手动指引;clone / checkout 的失败**不抛错**——外网故障不得绑架安装器
|
|
6
|
+
* 的其余职责(技能树、规则、hook 注入照常完成,进程照常 exit 0)。把这条链路独立成模块,是为
|
|
7
|
+
* 了让「什么情况下会改动用户目录」只有一个落点可审——答案必须是「只有目标不存在时」。
|
|
8
|
+
* 降级态的统一口径是「未校验(模板缺失)」:获取点给怎么补(命令),进程结尾单独明示后果与
|
|
9
|
+
* 修复方法(两处分工见 FLOW_KIT_DEGRADED_NOTICE 上方的说明)。
|
|
10
|
+
*
|
|
11
|
+
* 依赖方向:本模块是叶子(只用 node 内置模块与 git 命令)。
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { execFileSync } from 'node:child_process';
|
|
16
|
+
|
|
17
|
+
// ---------- flow-kit 获取链路(ADR-008 / DESIGN D1~D6——平台无关,main 平台循环前调用一次) ----------
|
|
18
|
+
|
|
19
|
+
// 锁定点常量(D2):上游 github.com/rihebty/flow-kit 实测无 tag,commit 是唯一精确锚;
|
|
20
|
+
// 升级锁定点 = 显式修改此常量并记 CHANGELOG,不做自动跟随(2026-08-23 ls-remote 实测 == 上游 HEAD)。
|
|
21
|
+
const FLOW_KIT_UPSTREAM_URL = 'https://github.com/rihebty/flow-kit.git';
|
|
22
|
+
const FLOW_KIT_LOCKED_COMMIT = '9b5dda7206ae841230f118348d660ad8d0ae2830';
|
|
23
|
+
// git 网络调用统一超时(D5——沿用 resolveInstalledVersion 的 execFileSync timeout 先例风格)
|
|
24
|
+
const FLOW_KIT_GIT_TIMEOUT_MS = 60000;
|
|
25
|
+
// 摘要展示用短 sha 长度(git 短哈希惯例——单一来源,clone 成功/已有/指引三处引用)
|
|
26
|
+
const FLOW_KIT_SHORT_SHA_LEN = 7;
|
|
27
|
+
|
|
28
|
+
/** 提取 git 调用失败信息首行(execFileSync 错误消息含多行 stderr——警告只留首行) */
|
|
29
|
+
function gitErrorFirstLine(err) {
|
|
30
|
+
return String(err && err.message ? err.message : err).split('\n')[0].trim();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 手动获取指引(clone/checkout 失败或目录非上游克隆时打印——含上游 URL 与目标路径;不抛错):
|
|
35
|
+
* 这里只负责「怎么补」,用户可见的结论(影响 / 原因 / 修复方法)留到进程结尾单独明示——
|
|
36
|
+
* 获取段在平台摘要之前执行,结论留在这一段里会被后面的平台清单与摘要顶出视野。
|
|
37
|
+
*/
|
|
38
|
+
function printFlowKitManualGuide(target) {
|
|
39
|
+
const flowKitDir = path.join(target, 'flow-kit');
|
|
40
|
+
console.warn(`[flow-comet] 手动获取指引: git clone ${FLOW_KIT_UPSTREAM_URL} "${flowKitDir}"`);
|
|
41
|
+
console.warn(`[flow-comet] 然后锁定快照: git -C "${flowKitDir}" checkout --detach ${FLOW_KIT_LOCKED_COMMIT.slice(0, FLOW_KIT_SHORT_SHA_LEN)}`);
|
|
42
|
+
console.warn('[flow-comet] 后果: 模板校验将不可用——各节点出口输出「未校验(模板缺失)」而不比对模板段形;技能册的模板引用同时悬空。其余安装职责不受影响。');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 安装结尾的降级结论(未校验(模板缺失)):影响 / 原因 / 修复方法三行,挂在进程结尾打印。
|
|
47
|
+
* 与获取点的手动指引分工:那里给命令(现在就能照做),这里给结论(安装结束时的状态与后果)。
|
|
48
|
+
* 只有真的拿不到模板时才挂(获取成功、HEAD 只读失败但目录在场都不挂——模板仍在磁盘上,
|
|
49
|
+
* 结构校验照常可用)。调用契约:只由 prepare-env CLI 进程调用——结尾提示挂在 process 'exit'
|
|
50
|
+
* 上,若将来被库内导入复用,提示会跟着宿主进程退出一起打印(不可断言、会污染输出)。
|
|
51
|
+
* 修复行的措辞约束:`flow-kit/` 已存在但归属无法确认时,重跑会落回同一条跳过分支(安装器绝不
|
|
52
|
+
* 改动已有目录)——所以「重跑」只在移走/处理该目录或按手动步骤补齐之后才是修复,不能单说重跑。
|
|
53
|
+
*/
|
|
54
|
+
const FLOW_KIT_DEGRADED_NOTICE = [
|
|
55
|
+
'[flow-comet] 提醒: 本次安装未获取 flow-kit 模板——产出物不会做模板校验。',
|
|
56
|
+
'[flow-comet] 影响: 各节点出口输出「未校验(模板缺失)」而不拦缺段 / 乱序 / 空段(结构校验不可用)。',
|
|
57
|
+
'[flow-comet] 原因: flow-kit/templates 未就位(自动获取失败,或同名目录归属无法确认已跳过——安装器绝不改动已有目录)。',
|
|
58
|
+
'[flow-comet] 修复: 先移走或处理现有 flow-kit/ 目录,或按上方手动获取指引补齐,再重跑本安装器。',
|
|
59
|
+
].join('\n') + '\n';
|
|
60
|
+
|
|
61
|
+
let flowKitDegradedNoticeScheduled = false;
|
|
62
|
+
|
|
63
|
+
/** 挂一次结尾结论(重复调用幂等;退出阶段只能用同步写——console 在 exit 阶段不保证刷出) */
|
|
64
|
+
function scheduleFlowKitDegradedNotice() {
|
|
65
|
+
if (flowKitDegradedNoticeScheduled) return;
|
|
66
|
+
flowKitDegradedNoticeScheduled = true;
|
|
67
|
+
process.once('exit', () => {
|
|
68
|
+
try {
|
|
69
|
+
fs.writeSync(2, FLOW_KIT_DEGRADED_NOTICE);
|
|
70
|
+
} catch {
|
|
71
|
+
// 结尾提示是尽力而为:写不出去也不得改变退出码(exit 0 / 非破坏契约的底线)
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 归属判定(D3):origin remote URL 匹配上游——本地 config 读取,无网络。
|
|
78
|
+
* 兼容 https / ssh 形态与可选 .git 后缀;读取失败视为不匹配(保守落入"非上游克隆"分支,
|
|
79
|
+
* 绝不改动用户目录)。返回 'match' | 'mismatch' | 'unreadable'。
|
|
80
|
+
*/
|
|
81
|
+
function flowKitOriginStatus(flowKitDir) {
|
|
82
|
+
try {
|
|
83
|
+
const url = execFileSync(
|
|
84
|
+
'git',
|
|
85
|
+
['-C', flowKitDir, 'config', '--get', 'remote.origin.url'],
|
|
86
|
+
{ encoding: 'utf8', timeout: FLOW_KIT_GIT_TIMEOUT_MS }
|
|
87
|
+
).trim().replace(/\.git$/i, '').replace(/\/+$/, '');
|
|
88
|
+
return /github\.com[:/]rihebty\/flow-kit$/i.test(url) ? 'match' : 'mismatch';
|
|
89
|
+
} catch {
|
|
90
|
+
return 'unreadable';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 确保 <target>/flow-kit/ 就位(ADR-008 / DESIGN D1~D6——L-008 非破坏哲学:已存在路径一律只读不动):
|
|
96
|
+
* 路径 1 目标不存在 → execFileSync('git',…) 数组参数形态 clone 上游后 detached checkout 到锁定 commit;
|
|
97
|
+
* 路径 2 已存在且 .git 存在且 origin 匹配上游 → 只读 rev-parse HEAD 并输出与锁定点差异影响,绝不改动;
|
|
98
|
+
* 路径 3 已存在但不满足归属判定 → 摘要"非上游克隆,已跳过"+ 手动指引,不改动;
|
|
99
|
+
* 路径 4 clone/checkout 任一失败 → 警告 + 手动获取指引,**不抛错**(D5——外网故障不得绑架安装器
|
|
100
|
+
* 其余职责,进程照常 exit 0);3、4 两条降级路径都在安装结尾补一句「未校验(模板缺失)」
|
|
101
|
+
* 的影响 / 原因 / 修复方法;各平台 purge 清单永不包含 flow-kit(D6,见 PLATFORMS 表头注释)。
|
|
102
|
+
*/
|
|
103
|
+
export function ensureFlowKit(target) {
|
|
104
|
+
const flowKitDir = path.join(target, 'flow-kit');
|
|
105
|
+
const shortLock = FLOW_KIT_LOCKED_COMMIT.slice(0, FLOW_KIT_SHORT_SHA_LEN);
|
|
106
|
+
if (!fs.existsSync(flowKitDir)) {
|
|
107
|
+
// 路径 1:目标缺失 → clone + detached checkout 到锁定 commit(D1/D2)
|
|
108
|
+
try {
|
|
109
|
+
execFileSync('git', ['clone', FLOW_KIT_UPSTREAM_URL, flowKitDir], {
|
|
110
|
+
encoding: 'utf8',
|
|
111
|
+
timeout: FLOW_KIT_GIT_TIMEOUT_MS,
|
|
112
|
+
});
|
|
113
|
+
execFileSync('git', ['-C', flowKitDir, 'checkout', '--detach', FLOW_KIT_LOCKED_COMMIT], {
|
|
114
|
+
encoding: 'utf8',
|
|
115
|
+
timeout: FLOW_KIT_GIT_TIMEOUT_MS,
|
|
116
|
+
});
|
|
117
|
+
console.log(`[flow-comet] 已获取 flow-kit(锁定 ${shortLock})`);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
// 路径 4:clone/checkout 任一失败 → WARN + 手动指引,不抛错
|
|
120
|
+
console.warn(`[flow-comet] 警告: flow-kit 自动获取失败(${gitErrorFirstLine(err)})`);
|
|
121
|
+
printFlowKitManualGuide(target);
|
|
122
|
+
scheduleFlowKitDegradedNotice();
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
// 目录已存在 → 归属判定(D3:.git 存在 且 origin remote 匹配上游)
|
|
127
|
+
const hasDotGit = fs.existsSync(path.join(flowKitDir, '.git'));
|
|
128
|
+
const originStatus = hasDotGit ? flowKitOriginStatus(flowKitDir) : 'mismatch';
|
|
129
|
+
if (originStatus !== 'match') {
|
|
130
|
+
// 路径 3:同名非克隆目录(或 remote 无法确认归属)→ 跳过 + 指引,绝不改动
|
|
131
|
+
if (originStatus === 'unreadable') {
|
|
132
|
+
console.warn('[flow-comet] 警告: flow-kit 目录存在且含 .git,但 origin remote 读取失败——无法确认归属');
|
|
133
|
+
}
|
|
134
|
+
console.log('[flow-comet] 目录存在但非上游克隆,已跳过');
|
|
135
|
+
printFlowKitManualGuide(target);
|
|
136
|
+
scheduleFlowKitDegradedNotice();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// 路径 2:上游克隆 → 只读检测并输出差异影响(D4——绝不 fetch/checkout 改动用户克隆)
|
|
140
|
+
try {
|
|
141
|
+
const head = execFileSync('git', ['-C', flowKitDir, 'rev-parse', 'HEAD'], {
|
|
142
|
+
encoding: 'utf8',
|
|
143
|
+
timeout: FLOW_KIT_GIT_TIMEOUT_MS,
|
|
144
|
+
}).trim();
|
|
145
|
+
const shortHead = head.slice(0, FLOW_KIT_SHORT_SHA_LEN);
|
|
146
|
+
console.log(`[flow-comet] 已有 flow-kit(HEAD=${shortHead},推荐锁定点=${shortLock})`);
|
|
147
|
+
if (head === FLOW_KIT_LOCKED_COMMIT) {
|
|
148
|
+
console.log('[flow-comet] 当前 HEAD 与推荐锁定点一致。');
|
|
149
|
+
} else {
|
|
150
|
+
console.warn(
|
|
151
|
+
`[flow-comet] 差异影响: 当前 HEAD 与推荐锁定点不一致——guard 段名基准与协议引用可能偏离安装器锁定内容` +
|
|
152
|
+
`(安装器绝不改动已有克隆;如需对齐请手动执行 git -C "${flowKitDir}" fetch 后 checkout --detach ${shortLock})`
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
} catch (err) {
|
|
156
|
+
// HEAD 只读失败同样不改动、不中断安装(保守降级为警告)
|
|
157
|
+
console.warn(`[flow-comet] 警告: flow-kit HEAD 读取失败(${gitErrorFirstLine(err)}),已跳过比对`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* installer-hooks.mjs — 各平台的配置注入与 .gitignore 纳管(prepare-env 的逻辑层模块)
|
|
3
|
+
*
|
|
4
|
+
* 「注入」在本模块的统一语义(也是本模块的边界):读现有文件 → 保留用户既有内容 → 只替换
|
|
5
|
+
* 本安装器管理的条目 → 写回。解析不了(非法 JSON / 非 YAML / 结构不符)一律中止并给人工处置
|
|
6
|
+
* 指引,绝不静默覆盖——安装器写的是用户的配置文件,失败方向必须是「不动」而不是「清空」,
|
|
7
|
+
* 否则一次手写变体就会让用户的权限与 hook 配置消失。
|
|
8
|
+
*
|
|
9
|
+
* 依赖方向:installer-migrate(路径探测原语与运行时目录名)与 installer-loader(托管块移除:
|
|
10
|
+
* AGENTS.md 托管区与 cordis.patch.yml 托管块共用同一幂等替换边界);installer-platforms 依赖
|
|
11
|
+
* 本模块。反向依赖不存在。
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import os from 'node:os';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
|
|
17
|
+
import { RUNTIME_DIR, isMissingPathError } from './installer-migrate.mjs';
|
|
18
|
+
import { stripManagedBlock } from './installer-loader.mjs';
|
|
19
|
+
|
|
20
|
+
// comet-hook-guard 的 command 特征(用于识别"已管理的 hook 命令",注入时替换避免重复)
|
|
21
|
+
const MANAGED_HOOK_COMMAND_MARKER = 'comet-hook-guard.mjs';
|
|
22
|
+
|
|
23
|
+
// ---------- hook 命令项目根锚定(单一事实来源) ----------
|
|
24
|
+
// Claude Code 运行 hook 时注入 CLAUDE_PROJECT_DIR 环境变量(值 = 项目根绝对路径,官方 hooks
|
|
25
|
+
// 文档契约)。变量名是唯一事实来源;命令形态**全平台统一**为 POSIX/bash 词形
|
|
26
|
+
// (`${VAR}` + 正斜杠——CC 在所有平台含 Windows 都以 /bin/bash -c 执行 hook 命令),
|
|
27
|
+
// 由 projectRootGuardScriptRef 派生,不按宿主 OS 分叉。
|
|
28
|
+
const PROJECT_ROOT_ENV_VAR = 'CLAUDE_PROJECT_DIR';
|
|
29
|
+
|
|
30
|
+
// hook 命令加整体外层引号(2026-09-10 演进——原「不加引号」取舍的前提是识别函数按空白分词
|
|
31
|
+
// 判等、外层引号会并入 token 使 basename 失配;识别函数已演进为容忍引号,故放开):未加
|
|
32
|
+
// 引号时,含空格的项目根路径在变量展开后会被词法分词切断(静默失效,与反斜杠转义同类)。
|
|
33
|
+
function projectRootGuardScriptRef(rootDirName) {
|
|
34
|
+
// CC 全平台以 /bin/bash -c 执行 hook 命令(含 Windows——Git Bash 语义),并注入
|
|
35
|
+
// CLAUDE_PROJECT_DIR 环境变量;故统一 POSIX 形态:${VAR} 花括号词形(CC 自身亦做该词形
|
|
36
|
+
// 插值——字符串替换,双保险)+ 正斜杠路径(反斜杠在 bash 词法里会被当转义吞掉,如 \. \f)。
|
|
37
|
+
// 整体加引号:含空格的项目根路径不因词法分词断裂;识别层容忍引号(isManagedHookCommand
|
|
38
|
+
// 去引号后取 basename 判等),幂等升级不受影响。
|
|
39
|
+
const segments = [rootDirName, 'skills', 'flow-comet', 'scripts', MANAGED_HOOK_COMMAND_MARKER];
|
|
40
|
+
return `"\${${PROJECT_ROOT_ENV_VAR}}/${segments.join('/')}"`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// AGENTS.md 托管区标记(Codex/dsh 平台 rules 注入共用——幂等替换边界,任一平台卸载可清)
|
|
44
|
+
export const MANAGED_AGENTS_START = '<!-- Managed by flow-comet prepare-env -->';
|
|
45
|
+
export const MANAGED_AGENTS_END = '<!-- /Managed by flow-comet prepare-env -->';
|
|
46
|
+
// ---------- .gitignore 保守纳管(目标项目;读-合并-写幂等,参照 injectSettingsHook 模式) ----------
|
|
47
|
+
|
|
48
|
+
/** 判断一行是否为 `.flow-comet/` 忽略条目(容忍首尾空白与可选的首尾斜杠变体)。 */
|
|
49
|
+
function isRuntimeDirIgnoreLine(line) {
|
|
50
|
+
return String(line).trim().replace(/^\/+/, '').replace(/\/+$/, '') === RUNTIME_DIR;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* gitignore 保守纳管(读-合并-写幂等):既有条目一律保留原样——安装器无法可靠推断用户
|
|
55
|
+
* 写下 `.comet/` 条目的原始意图(实测载体用整目录形态,该目录中仍有 Comet 资产),
|
|
56
|
+
* 故只做「缺失时追加」,不改写用户其他内容(无关行/注释/既有行尾风格)。
|
|
57
|
+
* 源仓库自身跳过:`.flow-comet/` 在源仓库是权威源(需被版本控制跟踪),
|
|
58
|
+
* 整目录忽略会把权威源新增文件静默排除出版本控制。
|
|
59
|
+
*/
|
|
60
|
+
export function manageGitignore(target) {
|
|
61
|
+
if (fs.existsSync(path.join(target, RUNTIME_DIR, 'skills', 'flow-comet', 'SKILL.md'))) {
|
|
62
|
+
console.log(`[flow-comet] .gitignore 纳管跳过: 当前目录是 flow-comet 源仓库(${RUNTIME_DIR}/ 为权威源,需保持被版本控制跟踪)`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const gitignorePath = path.join(target, '.gitignore');
|
|
66
|
+
let existing = '';
|
|
67
|
+
let fileExists = false;
|
|
68
|
+
try {
|
|
69
|
+
existing = fs.readFileSync(gitignorePath, 'utf8');
|
|
70
|
+
fileExists = true;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
// 只有「确实不存在」才是首次纳管;访问类故障(EACCES/EPERM/EIO…)必须原样抛出中止——
|
|
73
|
+
// 按空内容继续会让下面的写回把用户既有 .gitignore **截断**为仅含受管条目
|
|
74
|
+
// (判定语义见 isMissingPathError;调用点契约即「失败即中止,可重跑自愈」)。
|
|
75
|
+
if (!isMissingPathError(err)) {
|
|
76
|
+
throw err;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (existing.split(/\r?\n/).some(isRuntimeDirIgnoreLine)) {
|
|
80
|
+
console.log(`[flow-comet] .gitignore 已含 ${RUNTIME_DIR}/ 条目——保持原样(幂等)`);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const eol = existing.includes('\r\n') ? '\r\n' : '\n';
|
|
84
|
+
const appended = ['# flow-comet 运行时文件(不随仓库分发)', `${RUNTIME_DIR}/`].join(eol);
|
|
85
|
+
const separator = !fileExists || existing === '' || existing.endsWith('\n') ? '' : eol;
|
|
86
|
+
fs.writeFileSync(gitignorePath, existing + separator + appended + eol, 'utf8');
|
|
87
|
+
console.log(`[flow-comet] .gitignore 追加 ${RUNTIME_DIR}/ 条目(既有内容原样保留): ${gitignorePath}`);
|
|
88
|
+
}
|
|
89
|
+
// ---------- settings.local.json 注入(Claude Code 平台;参考 comet installClaudeCodeHooks) ----------
|
|
90
|
+
|
|
91
|
+
/** 判断 hook 命令是否为"已管理的 comet hook"(注入时替换,保证幂等)。
|
|
92
|
+
* 精确匹配:归一化命令(去前缀、统一分隔符)后必须包含
|
|
93
|
+
* 'comet-hook-guard.mjs' 作为脚本路径(而非任意位置子串),防误伤用户自定义 hook。
|
|
94
|
+
*/
|
|
95
|
+
function isManagedHookCommand(command) {
|
|
96
|
+
if (typeof command !== 'string' || command.trim() === '') {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const normalized = command.replace(/\\/g, '/').trim();
|
|
100
|
+
// 提取命令中的脚本路径(node <path> 或直接 <path>)——容忍外层引号(含空格路径防护形态):
|
|
101
|
+
// 引号由可选的 ["']? 吸收、不并入捕获组,basename 判等保持形态无关(旧相对形态 / 变量引用
|
|
102
|
+
// 形态 / 绝对路径形态 / 带引号形态均识别)。
|
|
103
|
+
const pathMatch = normalized.match(/(?:^|\s)["']?([^\s"']+\.mjs)["']?(?:\s|$)/);
|
|
104
|
+
if (!pathMatch) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const scriptPath = pathMatch[1].split('/').pop();
|
|
108
|
+
return scriptPath === MANAGED_HOOK_COMMAND_MARKER;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 合并 hook 组:已有组中过滤掉已管理的 comet hook 命令;新组按 matcher 合并进
|
|
113
|
+
* 同 matcher 的既有组(或新建组)。参考 comet mergeHookGroups。
|
|
114
|
+
*/
|
|
115
|
+
function mergeHookGroups(existingGroups, newGroups) {
|
|
116
|
+
const mergedGroups = (Array.isArray(existingGroups) ? existingGroups : []).map((group) => {
|
|
117
|
+
if (!group || typeof group !== 'object' || Array.isArray(group) || !Array.isArray(group.hooks)) {
|
|
118
|
+
return group;
|
|
119
|
+
}
|
|
120
|
+
const hadManagedHook = group.hooks.some((hook) => isManagedHookCommand(hook && hook.command));
|
|
121
|
+
const filtered = group.hooks.filter((hook) => !isManagedHookCommand(hook && hook.command));
|
|
122
|
+
// 只删除「含托管 hook 且过滤后为空」的组:matcher 演进(如 Write|Edit → Write|Edit|Bash)
|
|
123
|
+
// 会使旧托管组过滤为空,此时删除防配置污染(与 Codex purge 路径一致);
|
|
124
|
+
// 用户空组/纯用户组保留(不动用户配置——2026-08-16 修复:此前统一删空组会误删用户空 matcher 组)
|
|
125
|
+
if (hadManagedHook && filtered.length === 0) return null;
|
|
126
|
+
return { ...group, hooks: filtered };
|
|
127
|
+
}).filter(Boolean);
|
|
128
|
+
for (const newGroup of newGroups) {
|
|
129
|
+
const existingIndex = mergedGroups.findIndex(
|
|
130
|
+
(group) =>
|
|
131
|
+
group &&
|
|
132
|
+
typeof group === 'object' &&
|
|
133
|
+
!Array.isArray(group) &&
|
|
134
|
+
group.matcher === newGroup.matcher &&
|
|
135
|
+
Array.isArray(group.hooks)
|
|
136
|
+
);
|
|
137
|
+
if (existingIndex >= 0) {
|
|
138
|
+
const existing = mergedGroups[existingIndex];
|
|
139
|
+
mergedGroups[existingIndex] = { ...existing, hooks: [...existing.hooks, ...newGroup.hooks] };
|
|
140
|
+
} else {
|
|
141
|
+
mergedGroups.push(newGroup);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return mergedGroups;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 注入 comet hook 到 settings.local.json(Claude Code 平台):
|
|
149
|
+
* 读现有 settings(缺失 → {})→ 保留全部既有字段(permissions 等)→
|
|
150
|
+
* 只更新 hooks.PreToolUse(过滤已管理 comet hook + 合并新 hook)→ 写回。
|
|
151
|
+
*
|
|
152
|
+
* fail-safe(T-FIX-13 审查修复):
|
|
153
|
+
* - settings 文件存在但 JSON 非法 → 抛错退出(不静默覆盖,避免用户内容丢失)
|
|
154
|
+
* - hooks.PreToolUse 非数组(手写变体/对象形式)→ 保留原值 + 警告(不注入,
|
|
155
|
+
* 避免破坏用户结构;提示手动配置)
|
|
156
|
+
*/
|
|
157
|
+
export function injectSettingsHook(claudeDir) {
|
|
158
|
+
const settingsPath = path.join(claudeDir, 'settings.local.json');
|
|
159
|
+
let settings = {};
|
|
160
|
+
let fileExists = false;
|
|
161
|
+
try {
|
|
162
|
+
const raw = fs.readFileSync(settingsPath, 'utf8');
|
|
163
|
+
fileExists = true;
|
|
164
|
+
const parsed = JSON.parse(raw);
|
|
165
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
166
|
+
settings = parsed;
|
|
167
|
+
} else {
|
|
168
|
+
throw new Error('settings 顶层必须是 JSON 对象');
|
|
169
|
+
}
|
|
170
|
+
} catch (err) {
|
|
171
|
+
if (fileExists) {
|
|
172
|
+
// 文件存在但非法:fail-safe——不覆盖用户内容,报错退出
|
|
173
|
+
throw new Error(
|
|
174
|
+
`settings.local.json 已存在但内容非法(${err.message})——为保护用户配置,中止注入。` +
|
|
175
|
+
`请修复该文件后重试,或手动添加 hook 配置(见 README 方案 B)。`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
// 文件缺失:视为空对象(首次安装)
|
|
179
|
+
settings = {};
|
|
180
|
+
}
|
|
181
|
+
const existingHooks = settings.hooks && typeof settings.hooks === 'object' && !Array.isArray(settings.hooks)
|
|
182
|
+
? settings.hooks
|
|
183
|
+
: {};
|
|
184
|
+
const newGroup = {
|
|
185
|
+
// R5: matcher 含 Bash——协调者禁令物理化(Bash 写源码命令同样被 hook 检测拦截;
|
|
186
|
+
// hook 对无写路径的 Bash 命令(如 git/查询)放行)
|
|
187
|
+
matcher: 'Write|Edit|Bash',
|
|
188
|
+
hooks: [
|
|
189
|
+
{
|
|
190
|
+
type: 'command',
|
|
191
|
+
// 项目根锚定(全平台统一形态):node ${CLAUDE_PROJECT_DIR}/.claude/skills/.../comet-hook-guard.mjs
|
|
192
|
+
// —— CC 以 /bin/bash -c 执行 hook 命令(含 Windows),POSIX 词形 + 正斜杠为唯一可用形态。
|
|
193
|
+
// 运行期由 CC 注入的变量展开;单一事实来源 = PROJECT_ROOT_ENV_VAR + projectRootGuardScriptRef。
|
|
194
|
+
command: `node ${projectRootGuardScriptRef('.claude')}`,
|
|
195
|
+
},
|
|
196
|
+
],
|
|
197
|
+
};
|
|
198
|
+
if (existingHooks.PreToolUse !== undefined && !Array.isArray(existingHooks.PreToolUse)) {
|
|
199
|
+
// PreToolUse 非数组(手写变体):保留原值 + 警告,不注入(避免破坏用户结构)
|
|
200
|
+
console.error(
|
|
201
|
+
'[flow-comet] 警告: settings.local.json 的 hooks.PreToolUse 不是数组(可能是手写变体)——' +
|
|
202
|
+
'为保护用户配置,未注入 comet hook。请手动添加(见 README 方案 B)。'
|
|
203
|
+
);
|
|
204
|
+
return settings;
|
|
205
|
+
}
|
|
206
|
+
const merged = mergeHookGroups(existingHooks.PreToolUse, [newGroup]);
|
|
207
|
+
settings.hooks = { ...existingHooks, PreToolUse: merged };
|
|
208
|
+
fs.mkdirSync(claudeDir, { recursive: true });
|
|
209
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf8');
|
|
210
|
+
return settings;
|
|
211
|
+
}
|
|
212
|
+
// ---------- Codex 平台注入 + AGENTS.md 托管区(codex/dsh 共用) ----------
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 注入 comet hook 到 .codex/hooks.json(Codex 平台):
|
|
216
|
+
* 读现有 hooks(缺失 → {})→ 过滤已管理 comet hook(命令含 comet-hook-guard.mjs)→
|
|
217
|
+
* 合并新组(matcher "*"——Codex PreToolUse 只拦截 Bash 工具,写路径经 PowerShell/Bash 命令,
|
|
218
|
+
* 必须匹配所有 Bash 调用;命令带 --platform codex 平台标记)→ 写回。
|
|
219
|
+
* 结构:顶层 "hooks" 包裹层(Codex 与 Claude Code 同构——{"hooks":{"PreToolUse":[...]}};
|
|
220
|
+
* 实测缺包裹层 hook 不加载)。
|
|
221
|
+
* fail-safe:文件存在但 JSON 非法 → 抛错退出(保护用户配置)。
|
|
222
|
+
*/
|
|
223
|
+
export function injectCodexHook(codexDir, target) {
|
|
224
|
+
const hooksPath = path.join(codexDir, 'hooks.json');
|
|
225
|
+
let hooks = {};
|
|
226
|
+
let fileExists = false;
|
|
227
|
+
try {
|
|
228
|
+
const raw = fs.readFileSync(hooksPath, 'utf8');
|
|
229
|
+
fileExists = true;
|
|
230
|
+
const parsed = JSON.parse(raw);
|
|
231
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
232
|
+
hooks = parsed;
|
|
233
|
+
} else {
|
|
234
|
+
throw new Error('hooks.json 顶层必须是 JSON 对象');
|
|
235
|
+
}
|
|
236
|
+
} catch (err) {
|
|
237
|
+
if (fileExists) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
`.codex/hooks.json 已存在但内容非法(${err.message})——为保护用户配置,中止注入。请修复该文件后重试。`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
hooks = {};
|
|
243
|
+
}
|
|
244
|
+
const existingHooks = hooks.hooks && typeof hooks.hooks === 'object' && !Array.isArray(hooks.hooks)
|
|
245
|
+
? hooks.hooks
|
|
246
|
+
: {};
|
|
247
|
+
const existingGroups = Array.isArray(existingHooks.PreToolUse) ? existingHooks.PreToolUse : [];
|
|
248
|
+
// D6 查证结论(2026-08,openai/codex 官方仓库 main 分支):
|
|
249
|
+
// - hooks.json 的 command 经 shell 执行:Windows = %COMSPEC% 回退 cmd.exe /C;POSIX = $SHELL
|
|
250
|
+
// 回退 /bin/sh -lc(codex-rs/hooks/src/engine/command_runner.rs 的 build_command /
|
|
251
|
+
// default_shell_command);子进程环境 = 会话环境快照 + 条目级 env 映射,current_dir = 会话工作目录。
|
|
252
|
+
// - 无等价项目根变量:全仓 Rust 检索 CODEX_PROJECT_DIR / CLAUDE_PROJECT_DIR / project_dir
|
|
253
|
+
// 均 0 命中(GitHub code search,openai/codex);$PWD/%CD% 展开的是"会话启动目录"而非
|
|
254
|
+
// 项目根契约,子目录启动即漂移——不构成 CLAUDE_PROJECT_DIR 的等价锚点。
|
|
255
|
+
// → 按 D6 预授权退化分支:安装期把 target 绝对路径字面注入(path.join)。
|
|
256
|
+
// ⚠️ 迁移目录需重跑安装器:绝对路径随安装位置固化,项目移动/换 checkout 后必须重新运行
|
|
257
|
+
// 本安装器刷新 hooks.json(旧条目按脚本 basename 仍被 isManagedHookCommand 识别并在
|
|
258
|
+
// 重跑时被过滤替换为新位置——幂等自愈,不会产生重复条目)。
|
|
259
|
+
// 注:上游 HookHandlerConfig::Command 另有 command_windows 平台分字段(declarations.rs);
|
|
260
|
+
// 本安装器逐机注入,写入路径与宿主 OS 天然一致,无需分字段双写。
|
|
261
|
+
const guardAbsolutePath = path.join(target, '.agents', 'skills', 'flow-comet', 'scripts', MANAGED_HOOK_COMMAND_MARKER);
|
|
262
|
+
const newGroup = {
|
|
263
|
+
matcher: '*',
|
|
264
|
+
hooks: [
|
|
265
|
+
{
|
|
266
|
+
type: 'command',
|
|
267
|
+
command: `node ${guardAbsolutePath} before_tool --platform codex`,
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
};
|
|
271
|
+
const merged = mergeHookGroups(existingGroups, [newGroup]);
|
|
272
|
+
hooks.hooks = { ...existingHooks, PreToolUse: merged };
|
|
273
|
+
fs.mkdirSync(codexDir, { recursive: true });
|
|
274
|
+
fs.writeFileSync(hooksPath, JSON.stringify(hooks, null, 2) + '\n', 'utf8');
|
|
275
|
+
return hooks;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* 注入 Codex hook 启用配置到 .codex/config.toml(Codex 平台):
|
|
279
|
+
* hooks 默认关闭——需 [features] hooks = true(部分版本 codex_hooks = true;双写兼容)。
|
|
280
|
+
* 幂等:已有 [features] 段时合并缺失键,不覆盖用户其他配置。
|
|
281
|
+
*/
|
|
282
|
+
export function injectCodexConfig(codexDir) {
|
|
283
|
+
const configPath = path.join(codexDir, 'config.toml');
|
|
284
|
+
let content = '';
|
|
285
|
+
try {
|
|
286
|
+
content = fs.readFileSync(configPath, 'utf8');
|
|
287
|
+
} catch { /* 文件缺失 = 首次安装 */ }
|
|
288
|
+
if (content.includes('[features]')) {
|
|
289
|
+
// 已有 [features] 段:一次性补齐全部缺失键(分次 replace 会因插入点被前次消耗而失配;
|
|
290
|
+
// CRLF 行尾与文件尾无换行均须匹配——(\[features\])(?:\r?\n|$) 覆盖)
|
|
291
|
+
const missing = [];
|
|
292
|
+
if (!/^hooks\s*=/m.test(content)) missing.push('hooks = true');
|
|
293
|
+
if (!/^codex_hooks\s*=/m.test(content)) missing.push('codex_hooks = true');
|
|
294
|
+
if (missing.length > 0) {
|
|
295
|
+
content = content.replace(/(\[features\])(?:\r?\n|$)/, '$1\r\n' + missing.join('\r\n') + '\r\n');
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
const trimmed = content.trim();
|
|
299
|
+
content = (trimmed ? trimmed + '\n\n' : '') + '[features]\nhooks = true\ncodex_hooks = true\n';
|
|
300
|
+
}
|
|
301
|
+
fs.mkdirSync(codexDir, { recursive: true });
|
|
302
|
+
fs.writeFileSync(configPath, content, 'utf8');
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* 注入 rules 到 AGENTS.md 托管区(Codex/dsh 平台共用——AGENTS.md 是两平台指令
|
|
306
|
+
* 唯一自动加载路径;.codex/rules/ 是命令批准规则目录,不混用)。
|
|
307
|
+
* 读现有 AGENTS.md(缺失 → 空)→ 移除旧托管区(幂等)→ 托管区内联 orchestration 全文
|
|
308
|
+
* → 保留托管区外用户内容 → 写回。托管区标记不变(任一平台卸载可清,另一平台重装恢复)。
|
|
309
|
+
* `rulesSrc` 由调用方给出(本模块不推导仓库路径——那会与 installer-platforms 形成循环依赖)。
|
|
310
|
+
*/
|
|
311
|
+
export function injectManagedRules(target, stats, rulesSrc) {
|
|
312
|
+
if (!fs.existsSync(rulesSrc)) return false;
|
|
313
|
+
const rulesFile = path.join(rulesSrc, 'flow-comet-orchestration.md');
|
|
314
|
+
if (!fs.existsSync(rulesFile)) return false;
|
|
315
|
+
const rulesText = fs.readFileSync(rulesFile, 'utf8');
|
|
316
|
+
const agentsPath = path.join(target, 'AGENTS.md');
|
|
317
|
+
let existing = '';
|
|
318
|
+
try {
|
|
319
|
+
existing = fs.readFileSync(agentsPath, 'utf8');
|
|
320
|
+
} catch { /* 文件缺失 = 首次安装 */ }
|
|
321
|
+
const stripped = stripManagedBlock(existing, MANAGED_AGENTS_START, MANAGED_AGENTS_END);
|
|
322
|
+
const managedBlock =
|
|
323
|
+
MANAGED_AGENTS_START + '\n' +
|
|
324
|
+
rulesText.trim() + '\n' +
|
|
325
|
+
MANAGED_AGENTS_END + '\n';
|
|
326
|
+
const merged = (stripped.trim() ? stripped.trim() + '\n\n' : '') + managedBlock;
|
|
327
|
+
fs.writeFileSync(agentsPath, merged, 'utf8');
|
|
328
|
+
stats.files++;
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* 移除 .codex/hooks.json 中的托管 hook 条目(purge 语义:移除而非注入——保留用户条目;
|
|
333
|
+
* 重建流程会重新注入新托管条目)。文件非法 JSON 时不动(保护用户配置)。
|
|
334
|
+
*/
|
|
335
|
+
export function removeManagedCodexHooks(codexDir) {
|
|
336
|
+
const hooksPath = path.join(codexDir, 'hooks.json');
|
|
337
|
+
if (!fs.existsSync(hooksPath)) return;
|
|
338
|
+
let hooks;
|
|
339
|
+
try {
|
|
340
|
+
hooks = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
|
|
341
|
+
} catch { return; } // 非法 JSON 不触碰(保护用户配置)
|
|
342
|
+
const outer = hooks && typeof hooks === 'object' && !Array.isArray(hooks) ? hooks : {};
|
|
343
|
+
const inner = outer.hooks && typeof outer.hooks === 'object' && !Array.isArray(outer.hooks) ? outer.hooks : {};
|
|
344
|
+
if (!Array.isArray(inner.PreToolUse)) return;
|
|
345
|
+
inner.PreToolUse = inner.PreToolUse
|
|
346
|
+
.map((group) => (
|
|
347
|
+
group && typeof group === 'object' && !Array.isArray(group) && Array.isArray(group.hooks)
|
|
348
|
+
? { ...group, hooks: group.hooks.filter((hook) => !isManagedHookCommand(hook && hook.command)) }
|
|
349
|
+
: group
|
|
350
|
+
))
|
|
351
|
+
.filter((group) => group && typeof group === 'object' && Array.isArray(group.hooks) && group.hooks.length > 0);
|
|
352
|
+
outer.hooks = inner;
|
|
353
|
+
fs.writeFileSync(hooksPath, JSON.stringify(outer, null, 2) + '\n', 'utf8');
|
|
354
|
+
}
|
|
355
|
+
// ---------- dsh 平台注入 ----------
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* $DSH_HOME 解析(dsh 平台——与 dsh CLI 同款默认):显式 DSH_HOME 环境变量 > ~/.dsh。
|
|
359
|
+
*/
|
|
360
|
+
export function resolveDshHome() {
|
|
361
|
+
if (process.env.DSH_HOME) return path.resolve(process.env.DSH_HOME);
|
|
362
|
+
return path.join(os.homedir(), '.dsh');
|
|
363
|
+
}
|