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,404 @@
|
|
|
1
|
+
// 运行上下文 + 受保护状态 I/O(守卫模块)。
|
|
2
|
+
//
|
|
3
|
+
// 运行上下文(runRoot / packageRoot / 协议路径绑定)是本模块的**单一来源**:各守卫模块不再各自
|
|
4
|
+
// 从环境变量推导同一事实——两份推导会在形态变化时静默分叉。
|
|
5
|
+
//
|
|
6
|
+
// 受保护读写通道(路径段判据 → 逐段目录校验 → 快照复核 → 唯一临时名原子落盘)在写入前与落盘前
|
|
7
|
+
// 都复核目标未被替换;任何一步不符即抛错(fail-closed),绝不退化为尽力而为的写。
|
|
8
|
+
//
|
|
9
|
+
// statePath / readStateJson / writeJson 是标准运行时状态文件的唯一读写入口。
|
|
10
|
+
|
|
11
|
+
import { constants as fsConstants, promises as fs } from 'fs';
|
|
12
|
+
import path from 'path';
|
|
13
|
+
import { fileURLToPath } from 'url';
|
|
14
|
+
import { RUNTIME_STATE_PATH, LEGACY_RUNTIME_STATE_PATH } from '../state-schema.mjs';
|
|
15
|
+
import {
|
|
16
|
+
inspectWorkflowProtectedPath,
|
|
17
|
+
readWorkflowProtectedFile,
|
|
18
|
+
workflowFileObjectIdentity,
|
|
19
|
+
workflowPathInside,
|
|
20
|
+
workflowSameFileObject,
|
|
21
|
+
workflowSameFileStat,
|
|
22
|
+
} from '../protocol-utils.mjs';
|
|
23
|
+
|
|
24
|
+
const scriptsRoot = path.dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
// 技能包根:scripts/lib → scripts → skills/flow-comet(协议默认路径与包内资源以此为基准)
|
|
26
|
+
export const packageRoot = path.resolve(scriptsRoot, '..', '..');
|
|
27
|
+
export const runRoot = process.env.FLOW_COMET_RUN_ROOT
|
|
28
|
+
? path.resolve(process.env.FLOW_COMET_RUN_ROOT)
|
|
29
|
+
: process.cwd();
|
|
30
|
+
|
|
31
|
+
// 协议路径解析:显式 --protocol 全局参数(command=argv[2] 之后的剩余参数)> state.protocolPath
|
|
32
|
+
// (init 持久化绑定)> FLOW_COMET_PROTOCOL 环境变量 > 内置默认。选择在入口 main() 内执行
|
|
33
|
+
// (state.protocolPath 需先读标准运行时状态文件引导,协议决定 statePath 时存在先后次序),
|
|
34
|
+
// 本模块只持有解析结果供错误文案与后续模块读取。
|
|
35
|
+
export let protocolPath = null;
|
|
36
|
+
|
|
37
|
+
export function setProtocolPath(value) {
|
|
38
|
+
protocolPath = value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const WORKFLOW_PROJECT_FILE_MAX_BYTES = 2 * 1024 * 1024;
|
|
42
|
+
|
|
43
|
+
export async function fileExists(file) {
|
|
44
|
+
try { await fs.access(file); return true; } catch { return false; }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function workflowRelativeSegments(value, label, allowWildcards = false) {
|
|
48
|
+
if (typeof value !== 'string') throw new Error(label + ' must be a string');
|
|
49
|
+
const trimmed = value.trim().replaceAll('\\', '/');
|
|
50
|
+
if (
|
|
51
|
+
trimmed.length === 0 ||
|
|
52
|
+
path.posix.isAbsolute(trimmed) ||
|
|
53
|
+
path.win32.isAbsolute(trimmed) ||
|
|
54
|
+
trimmed.startsWith('~') ||
|
|
55
|
+
trimmed.startsWith('/') ||
|
|
56
|
+
trimmed.startsWith('\\')
|
|
57
|
+
) {
|
|
58
|
+
throw new Error(label + ' must be relative to its declared path base');
|
|
59
|
+
}
|
|
60
|
+
const segments = trimmed.split('/');
|
|
61
|
+
if (segments.some((segment) => segment === '..')) {
|
|
62
|
+
const boundary = label === 'workflow-run statePath' ? 'the project root' : 'its declared path base';
|
|
63
|
+
throw new Error(label + ' must stay inside ' + boundary);
|
|
64
|
+
}
|
|
65
|
+
if (segments.some((segment) => segment === '' || segment === '.')) {
|
|
66
|
+
throw new Error(label + ' must not contain empty or dot path segments');
|
|
67
|
+
}
|
|
68
|
+
if (!allowWildcards && (trimmed.includes('*') || trimmed.includes('?'))) {
|
|
69
|
+
throw new Error(label + ' cannot contain wildcards');
|
|
70
|
+
}
|
|
71
|
+
return segments;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function resolveWorkflowRelativePath(base, value, label, allowWildcards = false) {
|
|
75
|
+
const segments = workflowRelativeSegments(value, label, allowWildcards);
|
|
76
|
+
const target = path.resolve(base, ...segments);
|
|
77
|
+
const relative = path.relative(path.resolve(base), target);
|
|
78
|
+
if (
|
|
79
|
+
path.isAbsolute(relative) ||
|
|
80
|
+
relative === '..' ||
|
|
81
|
+
relative.startsWith('..' + path.sep)
|
|
82
|
+
) {
|
|
83
|
+
const boundary = label === 'workflow-run statePath' ? 'the project root' : 'its declared path base';
|
|
84
|
+
throw new Error(label + ' must stay inside ' + boundary);
|
|
85
|
+
}
|
|
86
|
+
return { target, segments };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function workflowProtectedDirectorySnapshot(projectRoot, directory, label) {
|
|
90
|
+
const inspection = await inspectWorkflowProtectedPath(
|
|
91
|
+
projectRoot,
|
|
92
|
+
directory,
|
|
93
|
+
label,
|
|
94
|
+
'directory',
|
|
95
|
+
);
|
|
96
|
+
if (!inspection.exists) {
|
|
97
|
+
throw new Error(label + ' does not exist');
|
|
98
|
+
}
|
|
99
|
+
const stat = await fs.lstat(directory, { bigint: true });
|
|
100
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) {
|
|
101
|
+
throw new Error(label + ' must be a real directory');
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
stat,
|
|
105
|
+
realPath: await fs.realpath(directory),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function assertWorkflowProtectedDirectorySnapshot(
|
|
110
|
+
projectRoot,
|
|
111
|
+
directory,
|
|
112
|
+
label,
|
|
113
|
+
expected,
|
|
114
|
+
) {
|
|
115
|
+
const actual = await workflowProtectedDirectorySnapshot(
|
|
116
|
+
projectRoot,
|
|
117
|
+
directory,
|
|
118
|
+
label,
|
|
119
|
+
);
|
|
120
|
+
if (
|
|
121
|
+
actual.realPath !== expected.realPath ||
|
|
122
|
+
!workflowSameFileObject(
|
|
123
|
+
workflowFileObjectIdentity(actual.stat),
|
|
124
|
+
workflowFileObjectIdentity(expected.stat),
|
|
125
|
+
)
|
|
126
|
+
) {
|
|
127
|
+
throw new Error(label + ' changed before commit');
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function ensureWorkflowProtectedDirectory(projectRoot, directory, label) {
|
|
132
|
+
const root = path.resolve(projectRoot);
|
|
133
|
+
const target = path.resolve(directory);
|
|
134
|
+
if (!workflowPathInside(root, target)) {
|
|
135
|
+
throw new Error(label + ' must stay inside the project root');
|
|
136
|
+
}
|
|
137
|
+
const relative = path.relative(root, target);
|
|
138
|
+
const segments = relative === '' ? [] : relative.split(path.sep);
|
|
139
|
+
let cursor = root;
|
|
140
|
+
await inspectWorkflowProtectedPath(root, cursor, label, 'directory');
|
|
141
|
+
for (const segment of segments) {
|
|
142
|
+
const parent = cursor;
|
|
143
|
+
cursor = path.join(cursor, segment);
|
|
144
|
+
const inspection = await inspectWorkflowProtectedPath(
|
|
145
|
+
root,
|
|
146
|
+
cursor,
|
|
147
|
+
label,
|
|
148
|
+
'directory',
|
|
149
|
+
);
|
|
150
|
+
if (!inspection.exists) {
|
|
151
|
+
const parentSnapshot = await workflowProtectedDirectorySnapshot(
|
|
152
|
+
root,
|
|
153
|
+
parent,
|
|
154
|
+
label + ' parent',
|
|
155
|
+
);
|
|
156
|
+
try {
|
|
157
|
+
await fs.mkdir(cursor);
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (!error || typeof error !== 'object' || error.code !== 'EEXIST') {
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
await assertWorkflowProtectedDirectorySnapshot(
|
|
164
|
+
root,
|
|
165
|
+
parent,
|
|
166
|
+
label + ' parent',
|
|
167
|
+
parentSnapshot,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
const created = await inspectWorkflowProtectedPath(
|
|
171
|
+
root,
|
|
172
|
+
cursor,
|
|
173
|
+
label,
|
|
174
|
+
'directory',
|
|
175
|
+
);
|
|
176
|
+
if (!created.exists) {
|
|
177
|
+
throw new Error(label + ' could not be created safely');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async function workflowProtectedTargetSnapshot(projectRoot, file, label) {
|
|
183
|
+
const inspection = await inspectWorkflowProtectedPath(
|
|
184
|
+
projectRoot,
|
|
185
|
+
file,
|
|
186
|
+
label,
|
|
187
|
+
'file',
|
|
188
|
+
);
|
|
189
|
+
if (!inspection.exists) return { exists: false };
|
|
190
|
+
const stat = await fs.lstat(file, { bigint: true });
|
|
191
|
+
if (!stat.isFile() || stat.isSymbolicLink()) {
|
|
192
|
+
throw new Error(label + ' must be a real file');
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
exists: true,
|
|
196
|
+
stat,
|
|
197
|
+
realPath: await fs.realpath(file),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function assertWorkflowProtectedTargetSnapshot(
|
|
202
|
+
projectRoot,
|
|
203
|
+
file,
|
|
204
|
+
label,
|
|
205
|
+
expected,
|
|
206
|
+
) {
|
|
207
|
+
const actual = await workflowProtectedTargetSnapshot(projectRoot, file, label);
|
|
208
|
+
if (actual.exists !== expected.exists) {
|
|
209
|
+
throw new Error(label + ' changed before commit');
|
|
210
|
+
}
|
|
211
|
+
if (
|
|
212
|
+
actual.exists &&
|
|
213
|
+
expected.exists &&
|
|
214
|
+
(actual.realPath !== expected.realPath ||
|
|
215
|
+
!workflowSameFileStat(actual.stat, expected.stat))
|
|
216
|
+
) {
|
|
217
|
+
throw new Error(label + ' changed before commit');
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function writeWorkflowProtectedFile(
|
|
222
|
+
projectRoot,
|
|
223
|
+
file,
|
|
224
|
+
label,
|
|
225
|
+
value,
|
|
226
|
+
maxBytes,
|
|
227
|
+
) {
|
|
228
|
+
const bytes = Buffer.isBuffer(value) ? value : Buffer.from(String(value), 'utf8');
|
|
229
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) {
|
|
230
|
+
throw new Error(label + ' byte limit must be a positive integer');
|
|
231
|
+
}
|
|
232
|
+
if (bytes.length > maxBytes) {
|
|
233
|
+
throw new Error(label + ' exceeds ' + String(maxBytes) + ' bytes');
|
|
234
|
+
}
|
|
235
|
+
const root = path.resolve(projectRoot);
|
|
236
|
+
const target = path.resolve(file);
|
|
237
|
+
const directory = path.dirname(target);
|
|
238
|
+
await ensureWorkflowProtectedDirectory(root, directory, label + ' directory');
|
|
239
|
+
const directorySnapshot = await workflowProtectedDirectorySnapshot(
|
|
240
|
+
root,
|
|
241
|
+
directory,
|
|
242
|
+
label + ' directory',
|
|
243
|
+
);
|
|
244
|
+
const targetSnapshot = await workflowProtectedTargetSnapshot(root, target, label);
|
|
245
|
+
await assertWorkflowProtectedDirectorySnapshot(
|
|
246
|
+
root,
|
|
247
|
+
directory,
|
|
248
|
+
label + ' directory',
|
|
249
|
+
directorySnapshot,
|
|
250
|
+
);
|
|
251
|
+
await assertWorkflowProtectedTargetSnapshot(root, target, label, targetSnapshot);
|
|
252
|
+
|
|
253
|
+
const temporary = path.join(
|
|
254
|
+
directory,
|
|
255
|
+
'.' +
|
|
256
|
+
path.basename(target) +
|
|
257
|
+
'.' +
|
|
258
|
+
String(process.pid) +
|
|
259
|
+
'.' +
|
|
260
|
+
String(Date.now()) +
|
|
261
|
+
'.' +
|
|
262
|
+
Math.random().toString(16).slice(2) +
|
|
263
|
+
'.tmp',
|
|
264
|
+
);
|
|
265
|
+
const flags =
|
|
266
|
+
fsConstants.O_WRONLY |
|
|
267
|
+
fsConstants.O_CREAT |
|
|
268
|
+
fsConstants.O_EXCL |
|
|
269
|
+
(process.platform === 'win32' ? 0 : fsConstants.O_NOFOLLOW);
|
|
270
|
+
let handle;
|
|
271
|
+
let committed = false;
|
|
272
|
+
try {
|
|
273
|
+
await assertWorkflowProtectedDirectorySnapshot(
|
|
274
|
+
root,
|
|
275
|
+
directory,
|
|
276
|
+
label + ' directory',
|
|
277
|
+
directorySnapshot,
|
|
278
|
+
);
|
|
279
|
+
handle = await fs.open(temporary, flags, 0o600);
|
|
280
|
+
await handle.writeFile(bytes);
|
|
281
|
+
await handle.sync();
|
|
282
|
+
const temporaryStat = await handle.stat({ bigint: true });
|
|
283
|
+
if (!temporaryStat.isFile()) {
|
|
284
|
+
throw new Error(label + ' temporary path must be a real file');
|
|
285
|
+
}
|
|
286
|
+
await handle.close();
|
|
287
|
+
handle = undefined;
|
|
288
|
+
await inspectWorkflowProtectedPath(
|
|
289
|
+
root,
|
|
290
|
+
temporary,
|
|
291
|
+
label + ' temporary file',
|
|
292
|
+
'file',
|
|
293
|
+
);
|
|
294
|
+
await assertWorkflowProtectedDirectorySnapshot(
|
|
295
|
+
root,
|
|
296
|
+
directory,
|
|
297
|
+
label + ' directory',
|
|
298
|
+
directorySnapshot,
|
|
299
|
+
);
|
|
300
|
+
await assertWorkflowProtectedTargetSnapshot(root, target, label, targetSnapshot);
|
|
301
|
+
await fs.rename(temporary, target);
|
|
302
|
+
committed = true;
|
|
303
|
+
await inspectWorkflowProtectedPath(root, target, label, 'file');
|
|
304
|
+
await assertWorkflowProtectedDirectorySnapshot(
|
|
305
|
+
root,
|
|
306
|
+
directory,
|
|
307
|
+
label + ' directory',
|
|
308
|
+
directorySnapshot,
|
|
309
|
+
);
|
|
310
|
+
} finally {
|
|
311
|
+
if (handle) {
|
|
312
|
+
await handle.close().catch(() => {});
|
|
313
|
+
}
|
|
314
|
+
if (!committed) {
|
|
315
|
+
try {
|
|
316
|
+
await assertWorkflowProtectedDirectorySnapshot(
|
|
317
|
+
root,
|
|
318
|
+
directory,
|
|
319
|
+
label + ' directory',
|
|
320
|
+
directorySnapshot,
|
|
321
|
+
);
|
|
322
|
+
await fs.rm(temporary, { force: true });
|
|
323
|
+
} catch {
|
|
324
|
+
// Do not follow a replaced parent during cleanup.
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// 组装 statePath 失效错误:声明的字段值 + 实际解析出的绝对路径 + 跨命名空间探测 + 调整指引。
|
|
331
|
+
// 交叉探测的两个命名空间取值来自 state-schema.mjs 的运行时路径常量(单一来源,本模块不硬编码);
|
|
332
|
+
// 探测只读、只改善错误措辞,不参与路径解析,也不改变控制流(绝不回退到另一命名空间,fail-closed
|
|
333
|
+
// 语义不变)。探测基准 = statePath 所在目录的父目录(项目根布局下即项目根);该基准落在项目外时
|
|
334
|
+
// 回落到项目根,保证探测点不越出项目。探测失败一律按"未探测到"处理,不掩盖主错误。
|
|
335
|
+
export async function statePathMissingError(declared, target) {
|
|
336
|
+
const lines = [
|
|
337
|
+
'workflow-run statePath does not exist: ' + declared + ' (resolved to ' + target + ')',
|
|
338
|
+
];
|
|
339
|
+
const parent = path.resolve(path.dirname(target), '..');
|
|
340
|
+
const probeBase = workflowPathInside(runRoot, parent) ? parent : runRoot;
|
|
341
|
+
const declaredNormalized = declared.replaceAll('\\', '/');
|
|
342
|
+
const elsewhere = [];
|
|
343
|
+
for (const candidate of [LEGACY_RUNTIME_STATE_PATH, RUNTIME_STATE_PATH]) {
|
|
344
|
+
if (candidate === declaredNormalized) continue;
|
|
345
|
+
if (await fileExists(path.resolve(probeBase, ...candidate.split('/')))) elsewhere.push(candidate);
|
|
346
|
+
}
|
|
347
|
+
if (elsewhere.length > 0) {
|
|
348
|
+
lines.push(
|
|
349
|
+
'hint: a state file exists in the other runtime namespace (' + elsewhere.join(', ') +
|
|
350
|
+
') — the project may not be migrated yet; run the installer (prepare-env) to finish the migration, or correct the statePath field',
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
lines.push(
|
|
354
|
+
'fix: edit "state.statePath" in the workflow protocol JSON (' + protocolPath +
|
|
355
|
+
') — the value must be the state file path relative to the project root (current runtime namespace: ' +
|
|
356
|
+
RUNTIME_STATE_PATH + ')',
|
|
357
|
+
);
|
|
358
|
+
const error = new Error(lines.join('\n'));
|
|
359
|
+
error.code = 'ENOENT';
|
|
360
|
+
return error;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export async function statePath(protocol) {
|
|
364
|
+
const preferred = String(protocol.state?.statePath ?? '');
|
|
365
|
+
const target = resolveWorkflowRelativePath(
|
|
366
|
+
runRoot,
|
|
367
|
+
preferred,
|
|
368
|
+
'workflow-run statePath',
|
|
369
|
+
).target;
|
|
370
|
+
const inspection = await inspectWorkflowProtectedPath(
|
|
371
|
+
runRoot,
|
|
372
|
+
target,
|
|
373
|
+
'workflow-run statePath',
|
|
374
|
+
'file',
|
|
375
|
+
);
|
|
376
|
+
if (!inspection.exists) {
|
|
377
|
+
throw await statePathMissingError(preferred, target);
|
|
378
|
+
}
|
|
379
|
+
return target;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export async function readStateJson(file) {
|
|
383
|
+
// 容忍 UTF-8 BOM(外部写入可能带 BOM)
|
|
384
|
+
return JSON.parse(
|
|
385
|
+
(
|
|
386
|
+
await readWorkflowProtectedFile(
|
|
387
|
+
runRoot,
|
|
388
|
+
file,
|
|
389
|
+
'workflow-run state',
|
|
390
|
+
WORKFLOW_PROJECT_FILE_MAX_BYTES,
|
|
391
|
+
)
|
|
392
|
+
).toString('utf8').replace(/^/, ''),
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export async function writeJson(file, value) {
|
|
397
|
+
await writeWorkflowProtectedFile(
|
|
398
|
+
runRoot,
|
|
399
|
+
file,
|
|
400
|
+
'workflow-run state',
|
|
401
|
+
JSON.stringify(value, null, 2) + '\n',
|
|
402
|
+
WORKFLOW_PROJECT_FILE_MAX_BYTES,
|
|
403
|
+
);
|
|
404
|
+
}
|