flow-comet 1.5.0-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.flow-comet/rules/flow-comet-orchestration.md +13 -0
- package/.flow-comet/skills/flow-comet/GUIDANCE.md +196 -0
- package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -0
- package/.flow-comet/skills/flow-comet/SKILL.md +196 -0
- package/.flow-comet/skills/flow-comet/reference/KNOWN-ISSUES-template.md +19 -0
- package/.flow-comet/skills/flow-comet/reference/TEST-MATRIX.md +115 -0
- package/.flow-comet/skills/flow-comet/reference/authoring-lanes.json +33 -0
- package/.flow-comet/skills/flow-comet/reference/composition-report.md +65 -0
- package/.flow-comet/skills/flow-comet/reference/decision-points.md +64 -0
- package/.flow-comet/skills/flow-comet/reference/dirty-worktree.md +30 -0
- package/.flow-comet/skills/flow-comet/reference/dsh-platform.md +169 -0
- package/.flow-comet/skills/flow-comet/reference/init-detection.md +58 -0
- package/.flow-comet/skills/flow-comet/reference/recovery.md +20 -0
- package/.flow-comet/skills/flow-comet/reference/resolved-skills.json +661 -0
- package/.flow-comet/skills/flow-comet/reference/skill-review.md +13 -0
- package/.flow-comet/skills/flow-comet/reference/subagents/script-author.md +7 -0
- package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +681 -0
- package/.flow-comet/skills/flow-comet/reference/worktree-notes.md +52 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-check.mjs +61 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +909 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-plan.mjs +2 -0
- package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +312 -0
- package/.flow-comet/skills/flow-comet/scripts/contract-check.mjs +136 -0
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +7331 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +376 -0
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +230 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +99 -0
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +4017 -0
- package/.flow-comet/skills/flow-comet/scripts/task-parsing.mjs +28 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +2396 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +378 -0
- package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +1263 -0
- package/.flow-comet/skills/flow-comet-archive/SKILL.md +195 -0
- package/.flow-comet/skills/flow-comet-change/SKILL.md +29 -0
- package/.flow-comet/skills/flow-comet-compose/SKILL.md +249 -0
- package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +23 -0
- package/.flow-comet/skills/flow-comet-design/SKILL.md +161 -0
- package/.flow-comet/skills/flow-comet-dev/SKILL.md +51 -0
- package/.flow-comet/skills/flow-comet-evolve/SKILL.md +26 -0
- package/.flow-comet/skills/flow-comet-execute/SKILL.md +182 -0
- package/.flow-comet/skills/flow-comet-health/SKILL.md +31 -0
- package/.flow-comet/skills/flow-comet-integration/SKILL.md +45 -0
- package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +36 -0
- package/.flow-comet/skills/flow-comet-open/SKILL.md +161 -0
- package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +33 -0
- package/.flow-comet/skills/flow-comet-plan/SKILL.md +169 -0
- package/.flow-comet/skills/flow-comet-requirement/SKILL.md +27 -0
- package/.flow-comet/skills/flow-comet-review/SKILL.md +204 -0
- package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +29 -0
- package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +214 -0
- package/.flow-comet/skills/flow-comet-task/SKILL.md +33 -0
- package/.flow-comet/skills/flow-comet-test/SKILL.md +41 -0
- package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +26 -0
- package/.flow-comet/skills/flow-comet-verify/SKILL.md +164 -0
- package/LICENSE +21 -0
- package/README.md +255 -0
- package/package.json +27 -0
- package/scripts/dsh-bridge.mjs +461 -0
- package/scripts/prepare-env.mjs +1526 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
// dsh-flow-comet-bridge —— 薄桥接 loader。
|
|
2
|
+
//
|
|
3
|
+
// 职责(迁移既有 dsh 插件拦截经验):
|
|
4
|
+
// 1. 监听 tools/pre-execute(dsh 官方 waterfall 事件)。
|
|
5
|
+
// 2. 会话项目判定:仅当会话 cwd 的最近 .git 项目根下存在
|
|
6
|
+
// .dsh/skills/flow-comet 目录时才处理;否则直接 next()——
|
|
7
|
+
// 「窄监听」硬性契约:非 flow-comet 项目零拦截零开销。
|
|
8
|
+
// 3. 参数映射:把 dsh 工具名归一化到 guard CLI 契约名(Write/Edit/Bash),
|
|
9
|
+
// 形状不符/缺关键字段 → WARN + fail-closed deny(不静默放行)。
|
|
10
|
+
// 4. 包含性校验:Write/Edit 的 file_path 必须解析后位于项目根内——
|
|
11
|
+
// realpathSync.native 展开 Windows 8.3 短路径,越界直接
|
|
12
|
+
// deny;通过后传规范化长路径给 guard:
|
|
13
|
+
// 避免短路径导致 guard 侧 target=null 跳过白名单判定 = fail-open)。
|
|
14
|
+
// 5. 子进程调用项目本地 guard:node <项目根>/.dsh/skills/flow-comet/scripts/
|
|
15
|
+
// comet-hook-guard.mjs + stdin JSON {tool_name, tool_input};
|
|
16
|
+
// spawn cwd 必须 = 会话项目根(相对 file_path 按 cwd 解析——
|
|
17
|
+
// cwd≠项目根会 fail-open;不得以设 env 替代 cwd)。协议文件天然在
|
|
18
|
+
// 项目内(skill 包 reference/ 随树复制)——无需 FLOW_COMET_PROTOCOL 机制。
|
|
19
|
+
// 6. 决策映射:exit 0 → next() 放行;exit 2 → deny(BLOCK 消息 + 恢复指引
|
|
20
|
+
// 透传);其它/异常 → fail-closed deny + WARN。
|
|
21
|
+
//
|
|
22
|
+
// 明确不做的(旧 dsh 插件功能废弃):
|
|
23
|
+
// - 不注册 fs/write-intent 槽位(single-slot 守卫瀑布无 deny 面,
|
|
24
|
+
// 不 shadow 官方 dsh-fs-observation-policy);
|
|
25
|
+
// - 不做 fs/observed 审计(v1 极薄);
|
|
26
|
+
// - 不做技能注册/AGENTS.md 注入(安装器职责)。
|
|
27
|
+
//
|
|
28
|
+
// 纯 ESM、零第三方依赖,仅使用 Node.js 内置模块。dsh 官方插件形态:
|
|
29
|
+
// ESM 模块导出 { name, apply },ctx 由 dsh 注入。
|
|
30
|
+
//
|
|
31
|
+
// BRIDGE_VERSION: 1.5.0-rc.3
|
|
32
|
+
|
|
33
|
+
import { spawn } from 'node:child_process';
|
|
34
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
35
|
+
import path from 'node:path';
|
|
36
|
+
|
|
37
|
+
// 插件元信息:dsh 官方插件形态(name / apply;ctx 由 dsh 注入)。
|
|
38
|
+
// 版本戳(DESIGN D7 / §9.3 契约):与文件头 BRIDGE_VERSION 标记行同值——
|
|
39
|
+
// 安装器与 bridge-check 双侧正则提取的锚点,标记行格式不得改动(§9.5 禁动)。
|
|
40
|
+
// 值语义 = 当前发布版本(随发布同步更新,与技能包 INSTALLED_VERSION 同值——
|
|
41
|
+
// bridge-check 的版本一致性检查据此判定 loader 与项目副本同版)。
|
|
42
|
+
export const name = 'dsh-flow-comet-bridge';
|
|
43
|
+
export const version = '1.5.0-rc.3';
|
|
44
|
+
|
|
45
|
+
// guard 在项目内的相对路径常量(随 skill 包分发)。
|
|
46
|
+
const HOOK_GUARD_REL = '.dsh/skills/flow-comet/scripts/comet-hook-guard.mjs';
|
|
47
|
+
|
|
48
|
+
// 模块级幂等标记:同一进程内 apply() 被重复调用(热重载/多会话/插件重入)时
|
|
49
|
+
// 跳过已注册的监听,避免重复监听(幂等防重入)。
|
|
50
|
+
let applied = false;
|
|
51
|
+
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// 工具名归一化:dsh 实际工具名 -> guard CLI 契约名(Write/Edit/Bash)
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
export function normalizeToolName(toolName) {
|
|
56
|
+
if (typeof toolName !== 'string') return null;
|
|
57
|
+
const lower = toolName.toLowerCase();
|
|
58
|
+
if (lower === 'write' || lower === 'writefile' || lower === 'file-write') return 'Write';
|
|
59
|
+
if (lower === 'edit' || lower === 'editfile' || lower === 'file-edit') return 'Edit';
|
|
60
|
+
// Bash 写命令拦截必须覆盖 Windows 默认 PowerShell 工具形态——dsh 注册名为
|
|
61
|
+
// 'pwsh'(dsh-tool-pwsh 源码实证),缺失会 canonicalName=null → next() 放行,
|
|
62
|
+
// 模型用 Set-Content 等 PowerShell 写命令即可绕过整链拦截(fail-open 缺口)。
|
|
63
|
+
if (
|
|
64
|
+
lower === 'bash' ||
|
|
65
|
+
lower === 'shell' ||
|
|
66
|
+
lower === 'powershell' ||
|
|
67
|
+
lower === 'pwsh' ||
|
|
68
|
+
lower === 'run_command' ||
|
|
69
|
+
lower === 'run-command'
|
|
70
|
+
) {
|
|
71
|
+
return 'Bash';
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 参数映射:归一化工具名 + 工具参数 -> guard 契约输入。
|
|
77
|
+
// 形状不符/缺关键字段 -> { ok: false, reason }(fail-closed,不静默放行)。
|
|
78
|
+
export function mapToolInput(canonicalName, args) {
|
|
79
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) {
|
|
80
|
+
return { ok: false, reason: '工具参数缺失或非对象' };
|
|
81
|
+
}
|
|
82
|
+
if (canonicalName === 'Write' || canonicalName === 'Edit') {
|
|
83
|
+
const filePath = args.file_path ?? args.filePath ?? args.path;
|
|
84
|
+
if (typeof filePath !== 'string' || filePath.trim() === '') {
|
|
85
|
+
return { ok: false, reason: '缺少 file_path(Write/Edit 必须提供写入目标)' };
|
|
86
|
+
}
|
|
87
|
+
return { ok: true, target: filePath, input: { file_path: filePath } };
|
|
88
|
+
}
|
|
89
|
+
if (canonicalName === 'Bash') {
|
|
90
|
+
const command = args.command ?? args.cmd ?? args.script;
|
|
91
|
+
if (typeof command !== 'string' || command.trim() === '') {
|
|
92
|
+
return { ok: false, reason: '缺少 command(Bash 必须提供命令内容)' };
|
|
93
|
+
}
|
|
94
|
+
return { ok: true, target: command, input: { command } };
|
|
95
|
+
}
|
|
96
|
+
return { ok: false, reason: '未支持的归一化工具名' };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// 项目根包含性:Write/Edit 的 file_path 必须解析后仍位于 projectRoot 内。
|
|
101
|
+
// 越界路径若交给 guard 子进程,writeTargetFromHookInput 会因 target=null
|
|
102
|
+
// 跳过白名单判定(fail-open),因此必须在插件侧直接 fail-closed deny。
|
|
103
|
+
// Windows 8.3 短路径(如 LONGYI~1)与长路径在词法上不同,直接 path.relative
|
|
104
|
+
// 会把项目内短路径误判为越界;因此先 realpath 展开已存在部分再执行包含性
|
|
105
|
+
// 判断。realpathSync.native:Windows 上 fs.realpathSync(libuv)不展开 8.3
|
|
106
|
+
// 短名,native 变体才会把 LONGYI~1 规范化为 LongYinHaHa(Windows 实证)。
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
export function realpathExistingPath(p) {
|
|
109
|
+
try {
|
|
110
|
+
return realpathSync.native(p);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error && (error.code === 'ENOENT' || error.code === 'ENOTDIR')) {
|
|
113
|
+
const parent = path.dirname(p);
|
|
114
|
+
if (parent === p) return p;
|
|
115
|
+
try {
|
|
116
|
+
return path.join(realpathExistingPath(parent), path.basename(p));
|
|
117
|
+
} catch {
|
|
118
|
+
return p;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return p;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function isPathInsideProjectRoot(projectRoot, filePath) {
|
|
126
|
+
const root = path.resolve(projectRoot);
|
|
127
|
+
const resolved = path.resolve(root, filePath);
|
|
128
|
+
const realRoot = realpathExistingPath(root);
|
|
129
|
+
const realTarget = realpathExistingPath(resolved);
|
|
130
|
+
const relative = path.relative(realRoot, realTarget);
|
|
131
|
+
return (
|
|
132
|
+
relative === '' ||
|
|
133
|
+
(relative !== '..' && !relative.startsWith('..' + path.sep) && !path.isAbsolute(relative))
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
// 流程态门(DESIGN D1/D2——空闲态项目外写放行、运行态维持现状、解析失败/未知状态
|
|
139
|
+
// fail-closed)。
|
|
140
|
+
//
|
|
141
|
+
// 语义锚定 guard comet-hook-guard.mjs L1882-1895 判定规则(DESIGN D3 复用,不另造一套):
|
|
142
|
+
// ① 无 activeChange(无论 status;与「无 state 文件」同语义,覆盖旧 state 归档后)→ 放行
|
|
143
|
+
// ② running(status==='running' || undefined,且有 activeChange,fail-closed 向后兼容)→ 白名单校验
|
|
144
|
+
// ③ completed(归档后)→ 放行;其它 status → fail-closed
|
|
145
|
+
//
|
|
146
|
+
// 读状态对齐 guard readStateJson(comet-hook-guard.mjs L1682-1694):
|
|
147
|
+
// BOM 容错(外部写入可能带 UTF-8 BOM -> .toString('utf8').replace(/^\uFEFF/,''))+ JSON.parse。
|
|
148
|
+
// 返回 { ok:true, state };无文件 -> { ok:false, reason:'no-state' };
|
|
149
|
+
// JSON 解析失败 / 其它读失败 -> { ok:false, reason:'parse-error' }(fail-closed 标记,
|
|
150
|
+
// 不静默当作无 state 放行)。
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
/**
|
|
153
|
+
* Read the project flow state with UTF-8 BOM tolerance (mirrors guard readStateJson).
|
|
154
|
+
* Returns { ok:false, reason:'no-state' } when the file is absent, or
|
|
155
|
+
* { ok:false, reason:'parse-error' } on read/parse failure or non-object JSON
|
|
156
|
+
* (fail-closed — never treated as idle), else { ok:true, state }.
|
|
157
|
+
*/
|
|
158
|
+
export function readFlowState(projectRoot) {
|
|
159
|
+
const stateFile = path.join(projectRoot, '.flow-comet', 'flow-comet-state.json');
|
|
160
|
+
let raw;
|
|
161
|
+
try {
|
|
162
|
+
raw = readFileSync(stateFile);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (error && error.code === 'ENOENT') {
|
|
165
|
+
// 无 state 文件(无活跃 workflow / 新克隆仓库)——与 guard「(no active workflow)」同语义
|
|
166
|
+
return { ok: false, reason: 'no-state' };
|
|
167
|
+
}
|
|
168
|
+
// 其它读失败(EISDIR 目录等)同样 fail-closed,不当作空闲放行
|
|
169
|
+
return { ok: false, reason: 'parse-error' };
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
// 容忍 UTF-8 BOM(对齐 guard readStateJson L1682-1694 的 BOM strip)
|
|
173
|
+
const parsed = JSON.parse(raw.toString('utf8').replace(/^\uFEFF/, ''));
|
|
174
|
+
// 非对象(null/标量/数组)= 损坏 state:fail-closed,不得当空闲放行
|
|
175
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
176
|
+
return { ok: false, reason: 'parse-error' };
|
|
177
|
+
}
|
|
178
|
+
return { ok: true, state: parsed };
|
|
179
|
+
} catch {
|
|
180
|
+
return { ok: false, reason: 'parse-error' };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 判定:复用 guard L1882-1895 规则 -> 'idle' | 'running' | 'error'(fail-closed)。
|
|
185
|
+
/**
|
|
186
|
+
* Classify flow state into 'idle' | 'running' | 'error' (fail-closed),
|
|
187
|
+
* mirroring comet-hook-guard.mjs L1882-1895.
|
|
188
|
+
*/
|
|
189
|
+
export function judgeFlowState(readResult) {
|
|
190
|
+
if (!readResult.ok) {
|
|
191
|
+
// 无 state 文件 = 无活跃 workflow -> idle;JSON 解析失败 -> error(fail-closed 不视为空闲)
|
|
192
|
+
return readResult.reason === 'no-state' ? 'idle' : 'error';
|
|
193
|
+
}
|
|
194
|
+
const state = readResult.state;
|
|
195
|
+
// ① 无 activeChange(无论 status;与「无 state 文件」同语义)-> idle
|
|
196
|
+
if (!state.activeChange) return 'idle';
|
|
197
|
+
// ② running(status==='running' || undefined,且有 activeChange)-> running
|
|
198
|
+
const running = state.status === 'running' || state.status === undefined;
|
|
199
|
+
if (running) return 'running';
|
|
200
|
+
// ③ completed(归档后)-> idle;其它未知 status -> error(fail-closed)
|
|
201
|
+
if (state.status === 'completed') return 'idle';
|
|
202
|
+
return 'error';
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
// 会话 cwd 与项目判定
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
// 从 exec 上下文逐级回退取得会话 cwd。锚定字段 = exec.agent.session.header.cwd
|
|
209
|
+
// (dsh 源码核实,rc.6——逐会话);缺失时逐级回退 exec 上下文其它路径字段。
|
|
210
|
+
// 无法确定会话 cwd -> null(监听侧按窄监听语义 fail-open 于非判定场景)。
|
|
211
|
+
export function sessionCwd(exec) {
|
|
212
|
+
const candidates = [
|
|
213
|
+
exec?.agent?.session?.header?.cwd,
|
|
214
|
+
exec?.agent?.session?.cwd,
|
|
215
|
+
exec?.agent?.cwd,
|
|
216
|
+
exec?.session?.header?.cwd,
|
|
217
|
+
exec?.session?.cwd,
|
|
218
|
+
];
|
|
219
|
+
for (const candidate of candidates) {
|
|
220
|
+
if (typeof candidate === 'string' && candidate.trim() !== '') return candidate;
|
|
221
|
+
}
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// 代理身份分派(dsh 子代理=执行者):dsh 子代理 spawn/fork provider 的
|
|
226
|
+
// childSessionMeta 把 delegationDepth=parentDepth+1 写入子代理 session header
|
|
227
|
+
// (dsh-subagent 源码锚定,rc.6)——协调者=0/缺失,子代理>0。从 exec.agent.session
|
|
228
|
+
// 读取(与 sessionCwd 同级字段),供监听侧区分执行者与协调者——子代理写源码是
|
|
229
|
+
// 执行者职责(对应 CC worktree 子代理物理自由写),协调者走 guard 白名单拦截。
|
|
230
|
+
// 非法值(NaN/负数/字符串/null/缺字段)一律按协调者处理(fail-closed 语义)。
|
|
231
|
+
export function agentDepth(exec) {
|
|
232
|
+
const depth = exec?.agent?.session?.header?.delegationDepth;
|
|
233
|
+
return typeof depth === 'number' && Number.isFinite(depth) && depth > 0 ? depth : 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 最近 .git 祖先 = 项目根(.git 可为目录或文件——worktree/submodule 形态)。
|
|
237
|
+
// 上溯到文件系统根仍未找到 -> cwd 本身。
|
|
238
|
+
export function findProjectRoot(cwd) {
|
|
239
|
+
let current = path.resolve(cwd);
|
|
240
|
+
for (;;) {
|
|
241
|
+
if (existsSync(path.join(current, '.git'))) return current;
|
|
242
|
+
const parent = path.dirname(current);
|
|
243
|
+
if (parent === current) return path.resolve(cwd);
|
|
244
|
+
current = parent;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 项目本地 guard 绝对路径。
|
|
249
|
+
export function resolveGuardPath(projectRoot) {
|
|
250
|
+
return path.join(projectRoot, HOOK_GUARD_REL);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// ---------------------------------------------------------------------------
|
|
254
|
+
// 决策映射:guard 子进程 exit code -> 插件决策(可测纯函数)
|
|
255
|
+
// exit 0 放行 / exit 2 拦截(BLOCK 消息 + 恢复指引透传)/ 其它 fail-closed。
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
export function mapGuardExit(code, stderr, stdout) {
|
|
258
|
+
if (code === 0) return { kind: 'allow' };
|
|
259
|
+
if (code === 2) {
|
|
260
|
+
const detail = (stderr || stdout || '').trim();
|
|
261
|
+
return {
|
|
262
|
+
kind: 'deny',
|
|
263
|
+
reason:
|
|
264
|
+
'dsh-flow-comet-bridge: 写入被 flow-comet 白名单拦截\n' +
|
|
265
|
+
(detail ? detail + '\n' : '') +
|
|
266
|
+
'恢复指引:请将写入目标调整到当前节点允许的路径前缀,或先完成当前节点流程后再写;如认为判定有误,请检查 .flow-comet/flow-comet-state.json 与协议白名单。',
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
kind: 'error',
|
|
271
|
+
message: '判定脚本异常退出(code=' + String(code) + '): ' + (stderr || stdout || '').trim(),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
// 判定核心子进程调用(stdin JSON {tool_name, tool_input} -> exit code)
|
|
277
|
+
// ---------------------------------------------------------------------------
|
|
278
|
+
function runGuard(projectRoot, canonicalName, toolInput, signal) {
|
|
279
|
+
return new Promise((resolve) => {
|
|
280
|
+
const guardPath = resolveGuardPath(projectRoot);
|
|
281
|
+
const child = spawn(
|
|
282
|
+
process.execPath,
|
|
283
|
+
[guardPath, 'before_tool'],
|
|
284
|
+
{
|
|
285
|
+
// cwd 必须 = 会话项目根(硬性):相对 file_path 按 cwd 解析,
|
|
286
|
+
// cwd≠项目根会 fail-open;不得以设 env 替代 cwd。
|
|
287
|
+
cwd: projectRoot,
|
|
288
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
289
|
+
windowsHide: true,
|
|
290
|
+
signal,
|
|
291
|
+
},
|
|
292
|
+
);
|
|
293
|
+
let stdout = '';
|
|
294
|
+
let stderr = '';
|
|
295
|
+
// 超时与中止 fail-closed:guard 阻塞 / 调用方取消 → error 决策(不静默、不永久挂起;EPIPE 兜底防宿主崩溃)
|
|
296
|
+
const GUARD_TIMEOUT_MS = 15000;
|
|
297
|
+
let settled = false;
|
|
298
|
+
const finish = (decision) => {
|
|
299
|
+
if (settled) return;
|
|
300
|
+
settled = true;
|
|
301
|
+
clearTimeout(timer);
|
|
302
|
+
resolve(decision);
|
|
303
|
+
};
|
|
304
|
+
const timer = setTimeout(() => {
|
|
305
|
+
child.kill('SIGKILL');
|
|
306
|
+
finish({ kind: 'error', message: '判定脚本超时(' + GUARD_TIMEOUT_MS + 'ms)' });
|
|
307
|
+
}, GUARD_TIMEOUT_MS);
|
|
308
|
+
timer.unref?.();
|
|
309
|
+
child.stdout.on('data', (chunk) => {
|
|
310
|
+
stdout += chunk.toString();
|
|
311
|
+
});
|
|
312
|
+
child.stderr.on('data', (chunk) => {
|
|
313
|
+
stderr += chunk.toString();
|
|
314
|
+
});
|
|
315
|
+
// 子进程先退出时 stdin 异步抛 EPIPE——兜底监听,避免未处理流错误
|
|
316
|
+
child.stdin.on('error', () => { /* 写入失败可忽略——close 兜底决策 */ });
|
|
317
|
+
child.on('error', (error) => {
|
|
318
|
+
finish({ kind: 'error', message: error.message });
|
|
319
|
+
});
|
|
320
|
+
child.on('close', (code) => {
|
|
321
|
+
finish(mapGuardExit(code, stderr, stdout));
|
|
322
|
+
});
|
|
323
|
+
child.stdin.end(JSON.stringify({ tool_name: canonicalName, tool_input: toolInput }));
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ---------------------------------------------------------------------------
|
|
328
|
+
// 插件入口
|
|
329
|
+
// ---------------------------------------------------------------------------
|
|
330
|
+
export function apply(ctx) {
|
|
331
|
+
if (applied) {
|
|
332
|
+
console.log('[dsh-flow-comet-bridge] apply skipped: already applied in this process');
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
applied = true;
|
|
336
|
+
|
|
337
|
+
ctx.on('tools/pre-execute', async (exec, next) => {
|
|
338
|
+
// 整链 try/catch(fail-closed 语义延伸):从会话项目判定到 spawn 决策映射的任何
|
|
339
|
+
// 内部异常(如 spawn 异常竞态下 child.stdin.end 抛 TypeError)→ WARN + fail-closed
|
|
340
|
+
// deny——不静默、不挂起、不放行(监听器未捕获异常对 dsh 未定义,可能挂起或 fail-open)。
|
|
341
|
+
try {
|
|
342
|
+
// 1. 工具名归一化:非 Write/Edit/Bash 不处理(放行)。
|
|
343
|
+
const canonicalName = normalizeToolName(exec?.name);
|
|
344
|
+
if (!canonicalName) {
|
|
345
|
+
return next();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// 2. 会话 cwd:无法确定 -> next()(窄监听 fail-open 于非判定场景)。
|
|
349
|
+
const cwd = sessionCwd(exec);
|
|
350
|
+
if (!cwd) {
|
|
351
|
+
return next();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// 3. 项目判定:最近 .git 祖先 = 项目根;根下必须存在 .dsh/skills/flow-comet
|
|
355
|
+
// (安装器安装的项目级 skill)才处理;不存在 -> next()(窄监听硬性契约——
|
|
356
|
+
// 非 flow-comet 项目零拦截零开销)。
|
|
357
|
+
// 项目根同样归一化为长形态:短形态(8.3)项目根 + 长形态 file_path 会让 guard 的
|
|
358
|
+
// 词法 path.relative 解析出 target=null → 白名单跳过 = 协调者写源码 fail-open。
|
|
359
|
+
const projectRoot = realpathExistingPath(findProjectRoot(cwd));
|
|
360
|
+
if (!existsSync(path.join(projectRoot, '.dsh', 'skills', 'flow-comet'))) {
|
|
361
|
+
return next();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// 4. 参数映射:形状不符/缺关键字段 -> WARN + fail-closed deny(不静默放行)。
|
|
365
|
+
// D5:形状 fail-closed 任意态生效(含空闲态)——与流程态无关,保持现状。
|
|
366
|
+
const mapped = mapToolInput(canonicalName, exec?.arguments);
|
|
367
|
+
if (!mapped.ok) {
|
|
368
|
+
const reason =
|
|
369
|
+
'dsh-flow-comet-bridge: 工具 ' +
|
|
370
|
+
String(exec?.name) +
|
|
371
|
+
' 参数形状不符(' +
|
|
372
|
+
mapped.reason +
|
|
373
|
+
')——fail-closed 拒绝';
|
|
374
|
+
console.warn(reason);
|
|
375
|
+
return { kind: 'deny', reason };
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// 4.5 流程态门(DESIGN D1/D2,2026-08-20)——修复 v1「包含性校验无条件生效」的
|
|
379
|
+
// 设计偏差:空闲态的项目外写入本应对齐 CC/Codex 的 active-change 门语义放行,
|
|
380
|
+
// 却因无条件包含性校验被误 deny。现仅运行中的活跃 workflow 才参与包含性/guard
|
|
381
|
+
// 拦截;解析失败/未知状态 fail-closed 不视为空闲放行。
|
|
382
|
+
// 语义锚定 guard comet-hook-guard.mjs L1882-1895(①无 activeChange 放行 /
|
|
383
|
+
// ② activeChange+running/undefined 走白名单校验 / ③ completed 放行、其它 fail-closed)
|
|
384
|
+
// ——判定逻辑在 readFlowState/judgeFlowState 中以等价小工具复用(D3),注释锚定防漂移。
|
|
385
|
+
const flowState = judgeFlowState(readFlowState(projectRoot));
|
|
386
|
+
if (flowState === 'idle') {
|
|
387
|
+
// 空闲态(无 state / 无 activeChange / completed):直接放行,跳过包含性 deny 与
|
|
388
|
+
// guard 白名单(D4——与 CC/Codex 一致,空闲态零拦截开销)。
|
|
389
|
+
return next();
|
|
390
|
+
}
|
|
391
|
+
if (flowState === 'error') {
|
|
392
|
+
// 解析失败 / 状态异常:fail-closed deny,不得当空闲放行(D2/R2 缓解——防异常被静默放行)。
|
|
393
|
+
const reason =
|
|
394
|
+
'dsh-flow-comet-bridge: .flow-comet/flow-comet-state.json 解析失败或状态异常——fail-closed 拒绝';
|
|
395
|
+
console.warn(reason);
|
|
396
|
+
return { kind: 'deny', reason };
|
|
397
|
+
}
|
|
398
|
+
// running(activeChange + status running/undefined):继续走既有第 5 步包含性校验、
|
|
399
|
+
// 5.5 身份分派、第 6 步 guard 白名单——全部现状不变。
|
|
400
|
+
|
|
401
|
+
// 5. 包含性校验(Write/Edit):越界直接 deny,不进 guard。
|
|
402
|
+
if (canonicalName === 'Write' || canonicalName === 'Edit') {
|
|
403
|
+
if (!isPathInsideProjectRoot(projectRoot, mapped.target)) {
|
|
404
|
+
const reason =
|
|
405
|
+
'dsh-flow-comet-bridge: 写入目标 "' +
|
|
406
|
+
mapped.target +
|
|
407
|
+
'" 不在项目根 "' +
|
|
408
|
+
projectRoot +
|
|
409
|
+
'" 内——越界写入已拒绝,未进入 guard 判定';
|
|
410
|
+
console.warn(reason);
|
|
411
|
+
return { kind: 'deny', reason };
|
|
412
|
+
}
|
|
413
|
+
// 通过后传规范化长路径(realpath 展开 8.3 短路径)——guard 的
|
|
414
|
+
// writeTargetFromHookInput 只做词法 path.relative,短路径会解析出
|
|
415
|
+
// target=null 从而跳过白名单判定(fail-open)。
|
|
416
|
+
const normalizedTarget = realpathExistingPath(path.resolve(projectRoot, mapped.target));
|
|
417
|
+
mapped.target = normalizedTarget;
|
|
418
|
+
mapped.input.file_path = normalizedTarget;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// 5.5 代理身份分派:delegationDepth > 0 = 子代理(执行者)——其写源码是
|
|
422
|
+
// 执行者职责(对应 CC worktree 子代理物理隔离),跳过 guard 白名单判定直接
|
|
423
|
+
// 放行;协调者(0/缺失)走原 guard 白名单(协调者禁令物理拦截保留)。
|
|
424
|
+
// 形状 fail-closed 与项目根包含性校验在上方已对子代理同样执行——子代理也不得
|
|
425
|
+
// 越界写项目根外/参数形状不符(fail-closed 纪律不因身份放宽)。
|
|
426
|
+
if (agentDepth(exec) > 0) {
|
|
427
|
+
return next();
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// 6. 项目本地 guard 调用。guard 文件缺失(安装未完成/被删除)-> WARN +
|
|
431
|
+
// next() 放行(不阻断非 flow-comet 语义);spawn 异常(ENOENT 等)->
|
|
432
|
+
// fail-closed deny + WARN(与错误即阻断语义对齐)。
|
|
433
|
+
const guardPath = resolveGuardPath(projectRoot);
|
|
434
|
+
if (!existsSync(guardPath)) {
|
|
435
|
+
console.warn(
|
|
436
|
+
'[dsh-flow-comet-bridge] WARN: 项目本地 guard 缺失(安装未完成或已被删除)——放行: ' +
|
|
437
|
+
guardPath,
|
|
438
|
+
);
|
|
439
|
+
return next();
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const decision = await runGuard(projectRoot, canonicalName, mapped.input, exec?.signal);
|
|
443
|
+
if (decision.kind === 'allow') {
|
|
444
|
+
return next();
|
|
445
|
+
}
|
|
446
|
+
if (decision.kind === 'deny') {
|
|
447
|
+
return { kind: 'deny', reason: decision.reason };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// 其它错误态:fail-closed deny + WARN。
|
|
451
|
+
const reason =
|
|
452
|
+
'dsh-flow-comet-bridge: 判定核心调用失败(fail-closed 拒绝): ' + decision.message;
|
|
453
|
+
console.warn(reason);
|
|
454
|
+
return { kind: 'deny', reason };
|
|
455
|
+
} catch (error) {
|
|
456
|
+
const detail = error && error.message ? error.message : String(error);
|
|
457
|
+
console.warn('[dsh-flow-comet-bridge] WARN: 桥接器内部异常: ' + detail);
|
|
458
|
+
return { kind: 'deny', reason: '桥接器内部异常' };
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
}
|