uwright 0.0.0-stage → 0.1.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/README.md +112 -2
- package/dist/browser-install.js +23 -0
- package/dist/cli-args.js +12 -0
- package/dist/cli.js +219 -0
- package/dist/diff.js +62 -0
- package/dist/driver.js +187 -0
- package/dist/e2e-lock.js +439 -0
- package/dist/e2e-preflight.js +130 -0
- package/dist/e2e-runner.js +621 -0
- package/dist/e2e-status.js +30 -0
- package/dist/lock.js +295 -0
- package/dist/mcp.js +65 -0
- package/dist/model.js +121 -0
- package/dist/navigation-action.js +43 -0
- package/dist/run-viewer.js +50 -0
- package/dist/runner.js +447 -0
- package/dist/secret-action.js +99 -0
- package/dist/tap-action.js +79 -0
- package/dist/templates/run-viewer.html +188 -0
- package/dist/templates/watch.html +538 -0
- package/dist/watch.js +412 -0
- package/dist/workspace.js +129 -0
- package/package.json +57 -4
package/dist/e2e-lock.js
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
3
|
+
import { mkdir, mkdtemp, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { hostname } from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { FlowError } from './model.js';
|
|
9
|
+
import { navigationSchema } from './navigation-action.js';
|
|
10
|
+
import { literalFillSchema, secretFillSchema } from './secret-action.js';
|
|
11
|
+
import { tapSchema } from './tap-action.js';
|
|
12
|
+
const entryName = /^(?:[a-f0-9]{64}|plan)\.json$/;
|
|
13
|
+
export const compiledSectionSchema = z.object({
|
|
14
|
+
calls: z
|
|
15
|
+
.array(z.object({
|
|
16
|
+
step: z.number().int().positive(),
|
|
17
|
+
skill: z.string(),
|
|
18
|
+
inputs: z
|
|
19
|
+
.array(z.object({ name: z.string(), type: z.enum(['literal', 'secret']), value: z.string() }))
|
|
20
|
+
.default([]),
|
|
21
|
+
}))
|
|
22
|
+
.default([]),
|
|
23
|
+
checks: z.array(z.number().int().positive()).default([]),
|
|
24
|
+
});
|
|
25
|
+
const policySchema = z.object({
|
|
26
|
+
initialPath: z.string(),
|
|
27
|
+
actionTimeoutMs: z.number().positive(),
|
|
28
|
+
checkTimeoutMs: z.number().positive(),
|
|
29
|
+
checkVision: z.literal(false),
|
|
30
|
+
teardownFailure: z.literal('report'),
|
|
31
|
+
});
|
|
32
|
+
const defaultPolicy = {
|
|
33
|
+
initialPath: '/',
|
|
34
|
+
actionTimeoutMs: 120000,
|
|
35
|
+
checkTimeoutMs: 60000,
|
|
36
|
+
checkVision: false,
|
|
37
|
+
teardownFailure: 'report',
|
|
38
|
+
};
|
|
39
|
+
const engineSchema = z.object({ core: z.string(), web: z.string(), playwright: z.string(), protocol: z.string() });
|
|
40
|
+
const require = createRequire(import.meta.url);
|
|
41
|
+
function packageVersion(entry, name) {
|
|
42
|
+
let directory = path.dirname(require.resolve(entry));
|
|
43
|
+
for (;;) {
|
|
44
|
+
try {
|
|
45
|
+
const parsed = z
|
|
46
|
+
.object({ name: z.string(), version: z.string() })
|
|
47
|
+
.safeParse(JSON.parse(readFileSync(path.join(directory, 'package.json'), 'utf8')));
|
|
48
|
+
if (parsed.success && parsed.data.name === name)
|
|
49
|
+
return parsed.data.version;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error.code !== 'ENOENT')
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
const parent = path.dirname(directory);
|
|
56
|
+
if (parent === directory)
|
|
57
|
+
throw new FlowError('CONFIG_ERROR', `Cannot find package version for ${name}`);
|
|
58
|
+
directory = parent;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function engineIdentity() {
|
|
62
|
+
return {
|
|
63
|
+
core: packageVersion('@e2e-dev/core/agent', '@e2e-dev/core'),
|
|
64
|
+
web: packageVersion('@e2e-dev/web/engine', '@e2e-dev/web'),
|
|
65
|
+
playwright: packageVersion('playwright', 'playwright'),
|
|
66
|
+
protocol: 'uwright-e2e-v3',
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const actionSchema = z
|
|
70
|
+
.object({
|
|
71
|
+
recordings: z.array(z.string().regex(/^[a-f0-9]{64}\.json$/)).max(1),
|
|
72
|
+
observedActions: z.number().int().nonnegative(),
|
|
73
|
+
instruction: z.string().optional(),
|
|
74
|
+
secretFill: secretFillSchema.optional(),
|
|
75
|
+
literalFill: literalFillSchema.optional(),
|
|
76
|
+
tap: tapSchema.optional(),
|
|
77
|
+
navigation: navigationSchema.optional(),
|
|
78
|
+
stateCheck: z.string().optional(),
|
|
79
|
+
})
|
|
80
|
+
.refine((action) => Number(action.recordings.length > 0) +
|
|
81
|
+
Number(!!action.secretFill) +
|
|
82
|
+
Number(!!action.literalFill) +
|
|
83
|
+
Number(!!action.tap) +
|
|
84
|
+
Number(!!action.navigation) +
|
|
85
|
+
Number(!!action.stateCheck) <=
|
|
86
|
+
1, 'An action must have only one replay method');
|
|
87
|
+
const planSchema = z.object({
|
|
88
|
+
version: z.literal(1),
|
|
89
|
+
fingerprint: z.string(),
|
|
90
|
+
flow: z.string(),
|
|
91
|
+
dependencies: z.array(z.object({ source: z.string(), hash: z.string() })),
|
|
92
|
+
config: z.object({ target: z.unknown(), execution: z.unknown(), ai: z.unknown() }),
|
|
93
|
+
policy: policySchema.default(defaultPolicy),
|
|
94
|
+
engine: engineSchema.optional(),
|
|
95
|
+
documents: z.record(z.string(), z.record(z.string(), z.array(z.object({ text: z.string(), line: z.number() })))),
|
|
96
|
+
bindings: z.record(z.string(), compiledSectionSchema),
|
|
97
|
+
assertions: z.record(z.string(), z.string()).default({}),
|
|
98
|
+
actions: z.record(z.string(), actionSchema),
|
|
99
|
+
});
|
|
100
|
+
const digest = (value) => createHash('sha256').update(value).digest('hex');
|
|
101
|
+
const compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
102
|
+
export function invocationSuffix(parentId) {
|
|
103
|
+
return parentId ? `@${digest(parentId).slice(0, 16)}` : '';
|
|
104
|
+
}
|
|
105
|
+
export function newE2EPlan(ws, flow) {
|
|
106
|
+
const docs = [flow, ...ws.skills, ...ws.contexts].sort((a, b) => compare(a.path, b.path));
|
|
107
|
+
const relative = (doc) => path.relative(ws.root, doc.path).split(path.sep).join('/');
|
|
108
|
+
const dependencies = docs.map((doc) => ({ source: relative(doc), hash: digest(doc.source) }));
|
|
109
|
+
const assetDirs = new Set([
|
|
110
|
+
path.resolve(ws.root, ws.config.flowsDir, 'assets'),
|
|
111
|
+
...docs.map((doc) => path.join(path.dirname(doc.path), 'assets')),
|
|
112
|
+
]);
|
|
113
|
+
for (const directory of assetDirs) {
|
|
114
|
+
let names;
|
|
115
|
+
try {
|
|
116
|
+
names = readdirSync(directory);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (error.code === 'ENOENT')
|
|
120
|
+
continue;
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
for (const name of names.sort()) {
|
|
124
|
+
const file = path.join(directory, name);
|
|
125
|
+
if (statSync(file).isFile())
|
|
126
|
+
dependencies.push({
|
|
127
|
+
source: path.relative(ws.root, file).split(path.sep).join('/'),
|
|
128
|
+
hash: digest(readFileSync(file)),
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
dependencies.sort((a, b) => compare(a.source, b.source));
|
|
133
|
+
const config = { target: ws.config.target, execution: ws.config.execution, ai: ws.config.ai };
|
|
134
|
+
const engine = engineIdentity(), policy = { ...defaultPolicy };
|
|
135
|
+
return {
|
|
136
|
+
version: 1,
|
|
137
|
+
fingerprint: digest(JSON.stringify({ dependencies, config, engine, policy })),
|
|
138
|
+
flow: relative(flow),
|
|
139
|
+
dependencies,
|
|
140
|
+
config,
|
|
141
|
+
engine,
|
|
142
|
+
policy,
|
|
143
|
+
documents: Object.fromEntries(docs.map((doc) => [relative(doc), doc.sections])),
|
|
144
|
+
bindings: {},
|
|
145
|
+
assertions: {},
|
|
146
|
+
actions: {},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function entries(directory) {
|
|
150
|
+
let names;
|
|
151
|
+
try {
|
|
152
|
+
names = await readdir(directory);
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
if (error.code === 'ENOENT')
|
|
156
|
+
return new Map();
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
return new Map(await Promise.all(names
|
|
160
|
+
.filter((name) => entryName.test(name))
|
|
161
|
+
.map(async (name) => [name, await readFile(path.join(directory, name), 'utf8')])));
|
|
162
|
+
}
|
|
163
|
+
const ownerSchema = z.object({ pid: z.number().int().positive(), host: z.string() });
|
|
164
|
+
const journalSchema = z.object({
|
|
165
|
+
version: z.literal(1),
|
|
166
|
+
changes: z.array(z.object({ name: z.string().regex(entryName), before: z.string().nullable(), after: z.string().nullable() })),
|
|
167
|
+
});
|
|
168
|
+
async function optional(file) {
|
|
169
|
+
try {
|
|
170
|
+
return await readFile(file, 'utf8');
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
if (error.code === 'ENOENT')
|
|
174
|
+
return null;
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async function acquire(directory) {
|
|
179
|
+
await mkdir(directory, { recursive: true });
|
|
180
|
+
const mutex = path.join(directory, '.write-lock');
|
|
181
|
+
try {
|
|
182
|
+
await mkdir(mutex);
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
if (error.code === 'EEXIST')
|
|
186
|
+
throw new FlowError('LOCK_BUSY', `Lock is being updated: ${directory}`);
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
try {
|
|
190
|
+
await writeFile(path.join(mutex, 'owner.json'), JSON.stringify({ pid: process.pid, host: hostname() }), {
|
|
191
|
+
flag: 'wx',
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
await rm(mutex, { recursive: true, force: true });
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
return async () => {
|
|
199
|
+
await rm(mutex, { recursive: true, force: true });
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async function assertNoTransaction(directory) {
|
|
203
|
+
try {
|
|
204
|
+
await readdir(path.join(directory, '.transaction'));
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
if (error.code === 'ENOENT')
|
|
208
|
+
return;
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
throw new FlowError('LOCK_RECOVERY_REQUIRED', `Interrupted lock update in ${directory}; run uwright lock recover`);
|
|
212
|
+
}
|
|
213
|
+
async function applyJournal(directory, journal) {
|
|
214
|
+
for (const change of journal.changes) {
|
|
215
|
+
const current = await optional(path.join(directory, change.name));
|
|
216
|
+
if (current !== change.before && current !== change.after)
|
|
217
|
+
throw new FlowError('LOCK_RECOVERY_CONFLICT', `Lock changed outside the transaction: ${change.name}`);
|
|
218
|
+
}
|
|
219
|
+
for (const change of journal.changes) {
|
|
220
|
+
const file = path.join(directory, change.name);
|
|
221
|
+
if ((await optional(file)) === change.after)
|
|
222
|
+
continue;
|
|
223
|
+
if (change.after === null) {
|
|
224
|
+
await rm(file, { force: true });
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
const temp = `${file}.tmp-${randomUUID()}`;
|
|
228
|
+
try {
|
|
229
|
+
await writeFile(temp, change.after, { flag: 'wx', mode: 0o600 });
|
|
230
|
+
await rename(temp, file);
|
|
231
|
+
}
|
|
232
|
+
finally {
|
|
233
|
+
await rm(temp, { force: true });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
async function snapshot(directory) {
|
|
238
|
+
const release = await acquire(directory);
|
|
239
|
+
try {
|
|
240
|
+
await assertNoTransaction(directory);
|
|
241
|
+
return await entries(directory);
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
await release();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
export async function readE2ELockFiles(directory) {
|
|
248
|
+
return snapshot(directory);
|
|
249
|
+
}
|
|
250
|
+
export function e2eLockDir(ws, flow) {
|
|
251
|
+
const relative = path.relative(path.resolve(ws.root, ws.config.flowsDir), flow.path);
|
|
252
|
+
if (!relative || relative.startsWith('..' + path.sep) || path.isAbsolute(relative))
|
|
253
|
+
throw new FlowError('LOCK_INVALID', 'Flow is outside the flows directory');
|
|
254
|
+
return path.resolve(ws.root, ws.config.lockDir, 'e2e', relative + '.recordings');
|
|
255
|
+
}
|
|
256
|
+
export async function stageE2ELock(ws, flow, secrets) {
|
|
257
|
+
const destination = e2eLockDir(ws, flow);
|
|
258
|
+
const base = await snapshot(destination);
|
|
259
|
+
const scratch = path.resolve(ws.root, ws.config.artifactsDir, 'e2e-staging');
|
|
260
|
+
await mkdir(scratch, { recursive: true });
|
|
261
|
+
const directory = await mkdtemp(path.join(scratch, 'run-'));
|
|
262
|
+
for (const [name, value] of base)
|
|
263
|
+
await writeFile(path.join(directory, name), value, { mode: 0o600 });
|
|
264
|
+
return {
|
|
265
|
+
directory,
|
|
266
|
+
async loadPlan(expected, strict = false) {
|
|
267
|
+
const raw = base.get('plan.json');
|
|
268
|
+
if (!raw) {
|
|
269
|
+
if (strict)
|
|
270
|
+
throw new FlowError('LOCK_MISSING_IN_STRICT_MODE', `No execution plan for ${expected.flow}`, 3);
|
|
271
|
+
for (const name of base.keys())
|
|
272
|
+
await rm(path.join(directory, name));
|
|
273
|
+
return expected;
|
|
274
|
+
}
|
|
275
|
+
let plan;
|
|
276
|
+
try {
|
|
277
|
+
plan = planSchema.parse(JSON.parse(raw));
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
throw new FlowError('LOCK_INVALID', 'Invalid e2e execution plan');
|
|
281
|
+
}
|
|
282
|
+
if (plan.fingerprint === expected.fingerprint) {
|
|
283
|
+
const identity = (p) => JSON.stringify({
|
|
284
|
+
flow: p.flow,
|
|
285
|
+
dependencies: p.dependencies,
|
|
286
|
+
config: p.config,
|
|
287
|
+
policy: p.policy,
|
|
288
|
+
engine: p.engine,
|
|
289
|
+
documents: p.documents,
|
|
290
|
+
});
|
|
291
|
+
if (identity(plan) !== identity(expected))
|
|
292
|
+
throw new FlowError('LOCK_INVALID', `Execution plan metadata does not match its fingerprint for ${expected.flow}`, 3);
|
|
293
|
+
return plan;
|
|
294
|
+
}
|
|
295
|
+
if (strict)
|
|
296
|
+
throw new FlowError('LOCK_DRIFT_DETECTED', `Execution plan is stale for ${expected.flow}`, 3);
|
|
297
|
+
for (const name of base.keys())
|
|
298
|
+
await rm(path.join(directory, name));
|
|
299
|
+
return expected;
|
|
300
|
+
},
|
|
301
|
+
async savePlan(plan, explored = new Set()) {
|
|
302
|
+
const files = await entries(directory);
|
|
303
|
+
for (const [key, action] of Object.entries(plan.actions)) {
|
|
304
|
+
action.recordings = action.recordings.filter((name) => {
|
|
305
|
+
const value = files.get(name);
|
|
306
|
+
if (!value)
|
|
307
|
+
return false;
|
|
308
|
+
if (explored.has(key) && base.get(name) === value)
|
|
309
|
+
return false;
|
|
310
|
+
try {
|
|
311
|
+
return JSON.parse(value)?.payload?.recordedFor?.testId === `core:${key}`;
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
if (action.recordings.length) {
|
|
318
|
+
delete action.secretFill;
|
|
319
|
+
delete action.literalFill;
|
|
320
|
+
delete action.tap;
|
|
321
|
+
delete action.navigation;
|
|
322
|
+
delete action.stateCheck;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
await writeFile(path.join(directory, 'plan.json'), JSON.stringify(planSchema.parse(plan), null, 2) + '\n', {
|
|
326
|
+
mode: 0o600,
|
|
327
|
+
});
|
|
328
|
+
},
|
|
329
|
+
async publish() {
|
|
330
|
+
const next = await entries(directory);
|
|
331
|
+
const changed = [...next].filter(([name, value]) => base.get(name) !== value);
|
|
332
|
+
const deleted = [...base.keys()].filter((name) => !next.has(name));
|
|
333
|
+
if (!changed.length && !deleted.length)
|
|
334
|
+
return 0;
|
|
335
|
+
for (const [, value] of changed)
|
|
336
|
+
if (secrets.some((secret) => secret && value.includes(secret)))
|
|
337
|
+
throw new FlowError('SECRET_IN_LOCK', 'A recording contains a declared secret; no recordings were published');
|
|
338
|
+
const release = await acquire(destination);
|
|
339
|
+
try {
|
|
340
|
+
await assertNoTransaction(destination);
|
|
341
|
+
const current = await entries(destination);
|
|
342
|
+
if (current.size !== base.size || [...base].some(([name, value]) => current.get(name) !== value))
|
|
343
|
+
throw new FlowError('LOCK_CONFLICT', 'Another run changed this flow’s recordings; rerun to use the latest lock');
|
|
344
|
+
const changes = [
|
|
345
|
+
...changed.map(([name, value]) => ({ name, before: base.get(name) ?? null, after: value })),
|
|
346
|
+
...deleted.map((name) => ({ name, before: base.get(name) ?? null, after: null })),
|
|
347
|
+
].sort((a, b) => compare(a.name, b.name));
|
|
348
|
+
const transaction = path.join(destination, '.transaction');
|
|
349
|
+
await mkdir(transaction);
|
|
350
|
+
await writeFile(path.join(transaction, 'journal.tmp'), JSON.stringify({ version: 1, changes }), {
|
|
351
|
+
flag: 'wx',
|
|
352
|
+
mode: 0o600,
|
|
353
|
+
});
|
|
354
|
+
await rename(path.join(transaction, 'journal.tmp'), path.join(transaction, 'journal.json'));
|
|
355
|
+
await applyJournal(destination, { version: 1, changes });
|
|
356
|
+
await rm(transaction, { recursive: true, force: true });
|
|
357
|
+
}
|
|
358
|
+
finally {
|
|
359
|
+
await release();
|
|
360
|
+
}
|
|
361
|
+
return changed.length + deleted.length;
|
|
362
|
+
},
|
|
363
|
+
async close() {
|
|
364
|
+
await rm(directory, { recursive: true, force: true });
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
export async function recoverE2ELocks(ws) {
|
|
369
|
+
const root = path.resolve(ws.root, ws.config.lockDir, 'e2e');
|
|
370
|
+
async function visit(directory) {
|
|
371
|
+
let children;
|
|
372
|
+
try {
|
|
373
|
+
children = await readdir(directory, { withFileTypes: true });
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
if (error.code === 'ENOENT')
|
|
377
|
+
return;
|
|
378
|
+
throw error;
|
|
379
|
+
}
|
|
380
|
+
if (!directory.endsWith('.recordings')) {
|
|
381
|
+
for (const child of children)
|
|
382
|
+
if (child.isDirectory() && !child.name.startsWith('.'))
|
|
383
|
+
await visit(path.join(directory, child.name));
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
const mutex = path.join(directory, '.write-lock');
|
|
387
|
+
const rawOwner = await optional(path.join(mutex, 'owner.json'));
|
|
388
|
+
if (rawOwner !== null) {
|
|
389
|
+
let owner;
|
|
390
|
+
try {
|
|
391
|
+
owner = ownerSchema.parse(JSON.parse(rawOwner));
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
throw new FlowError('LOCK_RECOVERY_REQUIRED', `Invalid lock owner in ${directory}`);
|
|
395
|
+
}
|
|
396
|
+
if (owner.host !== hostname())
|
|
397
|
+
throw new FlowError('LOCK_BUSY', `Lock owner is on another host: ${directory}`);
|
|
398
|
+
try {
|
|
399
|
+
process.kill(owner.pid, 0);
|
|
400
|
+
throw new FlowError('LOCK_BUSY', `Process ${owner.pid} still holds ${directory}`);
|
|
401
|
+
}
|
|
402
|
+
catch (error) {
|
|
403
|
+
if (error instanceof FlowError)
|
|
404
|
+
throw error;
|
|
405
|
+
if (error.code !== 'ESRCH')
|
|
406
|
+
throw error;
|
|
407
|
+
}
|
|
408
|
+
await rm(mutex, { recursive: true, force: true });
|
|
409
|
+
}
|
|
410
|
+
else if (children.some((child) => child.name === '.write-lock')) {
|
|
411
|
+
throw new FlowError('LOCK_BUSY', `Lock has no owner information: ${directory}`);
|
|
412
|
+
}
|
|
413
|
+
const release = await acquire(directory);
|
|
414
|
+
try {
|
|
415
|
+
const transaction = path.join(directory, '.transaction');
|
|
416
|
+
const raw = await optional(path.join(transaction, 'journal.json'));
|
|
417
|
+
if (raw !== null) {
|
|
418
|
+
let journal;
|
|
419
|
+
try {
|
|
420
|
+
journal = journalSchema.parse(JSON.parse(raw));
|
|
421
|
+
if (new Set(journal.changes.map((change) => change.name)).size !== journal.changes.length)
|
|
422
|
+
throw new Error('Duplicate journal entries');
|
|
423
|
+
for (const change of journal.changes)
|
|
424
|
+
if (change.name === 'plan.json' && change.after !== null)
|
|
425
|
+
planSchema.parse(JSON.parse(change.after));
|
|
426
|
+
}
|
|
427
|
+
catch {
|
|
428
|
+
throw new FlowError('LOCK_INVALID', `Invalid recovery journal in ${directory}`);
|
|
429
|
+
}
|
|
430
|
+
await applyJournal(directory, journal);
|
|
431
|
+
}
|
|
432
|
+
await rm(transaction, { recursive: true, force: true });
|
|
433
|
+
}
|
|
434
|
+
finally {
|
|
435
|
+
await release();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
await visit(root);
|
|
439
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { readTraceEntry } from '@e2e-dev/core/lib/cache/trace-read';
|
|
4
|
+
import { invocationSuffix } from './e2e-lock.js';
|
|
5
|
+
import { FlowError } from './model.js';
|
|
6
|
+
import { contextsFor } from './workspace.js';
|
|
7
|
+
const recordingName = /^[a-f0-9]{64}\.json$/;
|
|
8
|
+
export async function preflightE2E(ws, flow, plan, directory, checkSecrets = true) {
|
|
9
|
+
const declaredSecrets = new Set([flow, ...ws.skills, ...ws.contexts].flatMap((doc) => doc.frontmatter.secrets));
|
|
10
|
+
async function document(doc, stack, parentId, inheritedSecrets = new Map()) {
|
|
11
|
+
const source = path.relative(ws.root, doc.path).split(path.sep).join('/');
|
|
12
|
+
if (stack.includes(source))
|
|
13
|
+
throw new FlowError('SKILL_CIRCULAR_DEPENDENCY', `Skill cycle: ${[...stack, source].join(' -> ')}`);
|
|
14
|
+
const scopedSecrets = new Map(inheritedSecrets);
|
|
15
|
+
for (const declared of [...contextsFor(ws, doc), doc])
|
|
16
|
+
for (const key of declared.frontmatter.secrets) {
|
|
17
|
+
if (checkSecrets && !process.env[key])
|
|
18
|
+
throw new FlowError('CONFIG_SECRET_MISSING', `Missing environment variable ${key}`);
|
|
19
|
+
if (!scopedSecrets.has(key))
|
|
20
|
+
scopedSecrets.set(key, key);
|
|
21
|
+
}
|
|
22
|
+
const invocation = invocationSuffix(parentId);
|
|
23
|
+
for (const section of ['setup', 'steps', 'teardown']) {
|
|
24
|
+
const steps = doc.sections[section] ?? [];
|
|
25
|
+
if (!steps.length)
|
|
26
|
+
continue;
|
|
27
|
+
const bindingKey = `${source}:${section}${invocation}`;
|
|
28
|
+
const binding = plan.bindings[bindingKey];
|
|
29
|
+
if (!binding)
|
|
30
|
+
throw new FlowError('LOCK_INCOMPLETE', `Missing compiled section ${bindingKey}`, 3);
|
|
31
|
+
const calls = new Map(binding.calls.map((call) => [call.step, call]));
|
|
32
|
+
const checks = new Set(binding.checks);
|
|
33
|
+
if (calls.size !== binding.calls.length ||
|
|
34
|
+
checks.size !== binding.checks.length ||
|
|
35
|
+
[...calls.keys(), ...checks].some((index) => index > steps.length) ||
|
|
36
|
+
[...checks].some((index) => calls.has(index)))
|
|
37
|
+
throw new FlowError('LOCK_INVALID', `Invalid compiled section ${bindingKey}`, 3);
|
|
38
|
+
for (const [index, item] of steps.entries()) {
|
|
39
|
+
const ordinal = index + 1;
|
|
40
|
+
const key = `${source}:${section}:${ordinal}${invocation}`;
|
|
41
|
+
const call = calls.get(ordinal);
|
|
42
|
+
if (checks.has(ordinal)) {
|
|
43
|
+
if (!plan.assertions[key])
|
|
44
|
+
throw new FlowError('LOCK_INCOMPLETE', `Missing assertion ${key}`, 3);
|
|
45
|
+
}
|
|
46
|
+
else if (call) {
|
|
47
|
+
const skill = ws.skills.find((candidate) => candidate.frontmatter.name === call.skill);
|
|
48
|
+
if (!skill)
|
|
49
|
+
throw new FlowError('SKILL_NOT_FOUND', `Unknown skill ${call.skill}`);
|
|
50
|
+
const inputNames = new Set();
|
|
51
|
+
const childSecrets = new Map(scopedSecrets);
|
|
52
|
+
for (const input of skill.frontmatter.inputs)
|
|
53
|
+
if (input.default !== undefined)
|
|
54
|
+
childSecrets.delete(input.name);
|
|
55
|
+
for (const input of call.inputs) {
|
|
56
|
+
if (inputNames.has(input.name) || !skill.frontmatter.inputs.some((def) => def.name === input.name))
|
|
57
|
+
throw new FlowError('LOCK_INVALID', `Invalid input ${input.name} for ${call.skill}`, 3);
|
|
58
|
+
inputNames.add(input.name);
|
|
59
|
+
if (input.type === 'secret' && !declaredSecrets.has(input.value))
|
|
60
|
+
throw new FlowError('LOCK_INVALID', `Undeclared secret ${input.value}`, 3);
|
|
61
|
+
if (input.type === 'secret' && checkSecrets && !process.env[input.value])
|
|
62
|
+
throw new FlowError('CONFIG_SECRET_MISSING', `Missing environment variable ${input.value}`);
|
|
63
|
+
if (input.type === 'secret')
|
|
64
|
+
childSecrets.set(input.name, input.value);
|
|
65
|
+
else
|
|
66
|
+
childSecrets.delete(input.name);
|
|
67
|
+
}
|
|
68
|
+
for (const input of skill.frontmatter.inputs)
|
|
69
|
+
if (input.required && input.default === undefined && !inputNames.has(input.name))
|
|
70
|
+
throw new FlowError('LOCK_INVALID', `Missing input ${input.name} for ${call.skill}`, 3);
|
|
71
|
+
const id = `${parentId ? `${parentId}>` : ''}${source}:${item.line}`;
|
|
72
|
+
await document(skill, [...stack, source], id, childSecrets);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const action = plan.actions[key];
|
|
76
|
+
if (!action?.instruction)
|
|
77
|
+
throw new FlowError('LOCK_INCOMPLETE', `Missing action ${key}`, 3);
|
|
78
|
+
const methods = Number(action.recordings.length > 0) +
|
|
79
|
+
Number(!!action.secretFill) +
|
|
80
|
+
Number(!!action.literalFill) +
|
|
81
|
+
Number(!!action.tap) +
|
|
82
|
+
Number(!!action.navigation) +
|
|
83
|
+
Number(!!action.stateCheck);
|
|
84
|
+
if (!methods)
|
|
85
|
+
throw new FlowError('LOCK_INCOMPLETE', `No recording for ${key}`, 3);
|
|
86
|
+
if (action.recordings.length > 1)
|
|
87
|
+
throw new FlowError('LOCK_INVALID', `Ambiguous action recordings for ${key}`, 3);
|
|
88
|
+
if (methods > 1)
|
|
89
|
+
throw new FlowError('LOCK_INVALID', `Multiple action recordings for ${key}`, 3);
|
|
90
|
+
if (action.secretFill && !declaredSecrets.has(action.secretFill.secret))
|
|
91
|
+
throw new FlowError('LOCK_INVALID', `Undeclared secret ${action.secretFill.secret}`, 3);
|
|
92
|
+
if (action.secretFill && checkSecrets && !process.env[action.secretFill.secret])
|
|
93
|
+
throw new FlowError('CONFIG_SECRET_MISSING', `Missing environment variable ${action.secretFill.secret}`);
|
|
94
|
+
if (action.secretFill && !action.secretFill.parameter)
|
|
95
|
+
throw new FlowError('LOCK_INCOMPLETE', `Missing secret parameter binding for ${key}`, 3);
|
|
96
|
+
if (action.secretFill?.parameter &&
|
|
97
|
+
scopedSecrets.get(action.secretFill.parameter) !== action.secretFill.secret)
|
|
98
|
+
throw new FlowError('LOCK_INVALID', `Secret parameter binding changed for ${key}`, 3);
|
|
99
|
+
if (action.navigation &&
|
|
100
|
+
new URL(action.navigation.path, ws.config.target.baseUrl).origin !==
|
|
101
|
+
new URL(ws.config.target.baseUrl).origin)
|
|
102
|
+
throw new FlowError('LOCK_INVALID', `Navigation leaves the configured origin for ${key}`, 3);
|
|
103
|
+
for (const name of action.recordings) {
|
|
104
|
+
if (!recordingName.test(name))
|
|
105
|
+
throw new FlowError('LOCK_INVALID', `Invalid recording reference in ${key}`, 3);
|
|
106
|
+
let data;
|
|
107
|
+
try {
|
|
108
|
+
data = JSON.parse(await readFile(path.join(directory, name), 'utf8'));
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
throw new FlowError('LOCK_INCOMPLETE', `Missing or invalid recording ${name} for ${key}`, 3);
|
|
112
|
+
}
|
|
113
|
+
const entry = readTraceEntry(data);
|
|
114
|
+
if (!entry || entry.payload.recordedFor?.testId !== `core:${key}`)
|
|
115
|
+
throw new FlowError('LOCK_INVALID', `Invalid recording or action identity for ${name}`, 3);
|
|
116
|
+
if (entry.payload.truncated || entry.payload.actions.some((action) => action.name === 'tool'))
|
|
117
|
+
throw new FlowError('LOCK_INCOMPLETE', `Recording ${name} cannot replay without the model`, 3);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const section = doc === flow ? 'expected outcome' : 'postconditions';
|
|
123
|
+
for (const item of doc.sections[section] ?? []) {
|
|
124
|
+
const key = `${source}:${section}:${item.line}${invocation}`;
|
|
125
|
+
if (!plan.assertions[key])
|
|
126
|
+
throw new FlowError('LOCK_INCOMPLETE', `Missing assertion ${key}`, 3);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
await document(flow, []);
|
|
130
|
+
}
|