@docsxai/engine 0.2.0 → 0.3.0-rc.1
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 +26 -6
- package/dist/address-class.d.ts +14 -0
- package/dist/address-class.js +151 -0
- package/dist/auth/api-login.js +6 -2
- package/dist/backend-client-contracts.d.ts +2 -0
- package/dist/calibrate.js +5 -0
- package/dist/cli-commands-authoring.js +23 -4
- package/dist/cli-commands-backend.js +2 -2
- package/dist/cli-commands-docpack.d.ts +5 -0
- package/dist/cli-commands-docpack.js +70 -24
- package/dist/cli-commands-pack.d.ts +2 -0
- package/dist/cli-commands-pack.js +69 -0
- package/dist/cli-commands-session.js +92 -88
- package/dist/cli-retired.d.ts +6 -0
- package/dist/cli-retired.js +12 -0
- package/dist/cli-usage.d.ts +1 -1
- package/dist/cli-usage.js +53 -5
- package/dist/cli-verify.d.ts +14 -0
- package/dist/cli-verify.js +54 -0
- package/dist/cli.js +14 -1
- package/dist/deprecation.d.ts +19 -0
- package/dist/deprecation.js +32 -0
- package/dist/diagnose.d.ts +25 -0
- package/dist/diagnose.js +69 -3
- package/dist/diff-compute.js +1 -0
- package/dist/doc-pack-io.d.ts +14 -0
- package/dist/doc-pack-io.js +121 -14
- package/dist/doc-pack.d.ts +2165 -181
- package/dist/doc-pack.js +152 -42
- package/dist/doctor-checks.d.ts +1 -1
- package/dist/doctor-checks.js +29 -5
- package/dist/driver-budget.d.ts +6 -0
- package/dist/driver-budget.js +15 -0
- package/dist/egress-guard.d.ts +58 -0
- package/dist/egress-guard.js +136 -0
- package/dist/environment-spec.d.ts +67 -0
- package/dist/environment-spec.js +38 -0
- package/dist/export/playwright-test.js +42 -10
- package/dist/flow-file.d.ts +7 -1
- package/dist/flow-file.js +21 -1
- package/dist/flow-halt.d.ts +16 -0
- package/dist/flow-halt.js +76 -0
- package/dist/flow-hidden.d.ts +9 -0
- package/dist/flow-hidden.js +20 -0
- package/dist/flow-lint.js +91 -1
- package/dist/flow-matrix-lint.d.ts +10 -0
- package/dist/flow-matrix-lint.js +40 -0
- package/dist/flow-matrix.d.ts +44 -0
- package/dist/flow-matrix.js +238 -0
- package/dist/flow-name-rules.d.ts +15 -0
- package/dist/flow-name-rules.js +39 -0
- package/dist/flow-runtime.d.ts +67 -21
- package/dist/flow-runtime.js +72 -110
- package/dist/flow-success.d.ts +3 -0
- package/dist/flow-success.js +37 -0
- package/dist/flow-tree.d.ts +2 -0
- package/dist/flow-tree.js +17 -5
- package/dist/flow-wait.d.ts +3 -0
- package/dist/flow-wait.js +22 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/matrix-spec.d.ts +145 -0
- package/dist/matrix-spec.js +120 -0
- package/dist/obstacles.d.ts +33 -0
- package/dist/obstacles.js +72 -0
- package/dist/page-hide.d.ts +10 -0
- package/dist/page-hide.js +45 -0
- package/dist/page-nearby-boxes.d.ts +9 -0
- package/dist/page-nearby-boxes.js +92 -0
- package/dist/page-settle.d.ts +39 -0
- package/dist/page-settle.js +142 -0
- package/dist/playwright-driver.d.ts +30 -8
- package/dist/playwright-driver.js +88 -18
- package/dist/run-flows.d.ts +40 -0
- package/dist/run-flows.js +129 -0
- package/dist/upload-path.d.ts +15 -0
- package/dist/upload-path.js +63 -0
- package/dist/verify-compare.d.ts +43 -0
- package/dist/verify-compare.js +237 -0
- package/dist/verify-determinism.d.ts +16 -0
- package/dist/verify-determinism.js +88 -0
- package/dist/verify-report.d.ts +53 -0
- package/dist/verify-report.js +141 -0
- package/dist/verify-tree.d.ts +27 -0
- package/dist/verify-tree.js +95 -0
- package/dist/viewer-bin.d.ts +6 -0
- package/dist/viewer-bin.js +24 -0
- package/dist/workspace.d.ts +8 -0
- package/package.json +7 -6
|
@@ -11,7 +11,13 @@ import { existsSync, promises as fs } from "node:fs";
|
|
|
11
11
|
import * as path from "node:path";
|
|
12
12
|
import { chromium, } from "playwright-core";
|
|
13
13
|
import { VIEWPORT_PRESETS, } from "./doc-pack.js";
|
|
14
|
+
import { egressContextOptions, installEgressGuard, resolveEgressGuard, } from "./egress-guard.js";
|
|
15
|
+
import { budget, withTimeout } from "./driver-budget.js";
|
|
16
|
+
import { HIDDEN_ATTR, markHidden, unmarkHidden } from "./page-hide.js";
|
|
17
|
+
import { collectNearbyBoxes } from "./page-nearby-boxes.js";
|
|
18
|
+
import { awaitSettled, runSettle } from "./page-settle.js";
|
|
14
19
|
import { applyRedactions } from "./redact.js";
|
|
20
|
+
import { resolveUploadPath } from "./upload-path.js";
|
|
15
21
|
import { resolveWorkspacePathReal } from "./workspace.js";
|
|
16
22
|
/**
|
|
17
23
|
* The cached Chromium binary path, or `undefined` when `playwright-core` has none installed.
|
|
@@ -50,9 +56,11 @@ async function installClock(page, env) {
|
|
|
50
56
|
}
|
|
51
57
|
/** Launch Chromium (or, with `connectOverCdp`, attach to a running one) and return a {@link PlaywrightSession}. */
|
|
52
58
|
export async function launchPlaywrightSession(opts = {}) {
|
|
59
|
+
const guard = resolveEgressGuard(opts.egressGuard);
|
|
53
60
|
if (opts.connectOverCdp) {
|
|
54
61
|
const browser = await chromium.connectOverCDP(opts.connectOverCdp);
|
|
55
62
|
const context = browser.contexts()[0] ?? (await browser.newContext());
|
|
63
|
+
await installEgressGuard(context, guard);
|
|
56
64
|
const pages = context.pages();
|
|
57
65
|
const page = pages.find((p) => /^https?:/.test(p.url())) ?? pages[0] ?? (await context.newPage());
|
|
58
66
|
// The attached context is externally owned — context-level environment fields can't apply.
|
|
@@ -65,7 +73,7 @@ export async function launchPlaywrightSession(opts = {}) {
|
|
|
65
73
|
].join(", ")}\n`);
|
|
66
74
|
}
|
|
67
75
|
await installClock(page, opts.environment);
|
|
68
|
-
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".");
|
|
76
|
+
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".", opts.outputRoot);
|
|
69
77
|
return {
|
|
70
78
|
browser,
|
|
71
79
|
context,
|
|
@@ -87,10 +95,12 @@ export async function launchPlaywrightSession(opts = {}) {
|
|
|
87
95
|
...(opts.ignoreHTTPSErrors ? { ignoreHTTPSErrors: true } : {}),
|
|
88
96
|
...(opts.environment ? environmentContextOptions(opts.environment) : {}),
|
|
89
97
|
...(opts.contextOptions ?? {}),
|
|
98
|
+
...egressContextOptions(guard),
|
|
90
99
|
});
|
|
100
|
+
await installEgressGuard(context, guard);
|
|
91
101
|
const page = await context.newPage();
|
|
92
102
|
await installClock(page, opts.environment);
|
|
93
|
-
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".");
|
|
103
|
+
const driver = new PlaywrightDriver(page, opts.docPackRoot ?? ".", opts.outputRoot);
|
|
94
104
|
return {
|
|
95
105
|
browser,
|
|
96
106
|
context,
|
|
@@ -106,33 +116,73 @@ export async function launchPlaywrightSession(opts = {}) {
|
|
|
106
116
|
export class PlaywrightDriver {
|
|
107
117
|
page;
|
|
108
118
|
docPackRoot;
|
|
109
|
-
|
|
119
|
+
outputRoot;
|
|
120
|
+
constructor(page, docPackRoot = ".", outputRoot) {
|
|
110
121
|
this.page = page;
|
|
111
122
|
this.docPackRoot = docPackRoot;
|
|
123
|
+
this.outputRoot = outputRoot ?? docPackRoot;
|
|
112
124
|
}
|
|
113
125
|
goto(url) {
|
|
114
126
|
return this.page.goto(url).then(() => undefined);
|
|
115
127
|
}
|
|
116
|
-
click(selector) {
|
|
117
|
-
return this.page.click(selector);
|
|
128
|
+
click(selector, timeoutMs) {
|
|
129
|
+
return this.page.click(selector, budget(timeoutMs));
|
|
118
130
|
}
|
|
119
|
-
fill(selector, value) {
|
|
120
|
-
return this.page.fill(selector, value);
|
|
131
|
+
fill(selector, value, timeoutMs) {
|
|
132
|
+
return this.page.fill(selector, value, budget(timeoutMs));
|
|
121
133
|
}
|
|
122
|
-
upload(selector, filePath) {
|
|
123
|
-
|
|
134
|
+
async upload(selector, filePath, timeoutMs) {
|
|
135
|
+
const file = await resolveUploadPath(this.docPackRoot, filePath);
|
|
136
|
+
return this.page.setInputFiles(selector, file, budget(timeoutMs));
|
|
124
137
|
}
|
|
125
|
-
press(selector, key) {
|
|
126
|
-
return selector
|
|
138
|
+
press(selector, key, timeoutMs) {
|
|
139
|
+
return selector
|
|
140
|
+
? this.page.press(selector, key, budget(timeoutMs))
|
|
141
|
+
: this.page.keyboard.press(key);
|
|
127
142
|
}
|
|
128
|
-
hover(selector) {
|
|
129
|
-
return this.page.hover(selector);
|
|
143
|
+
hover(selector, timeoutMs) {
|
|
144
|
+
return this.page.hover(selector, budget(timeoutMs));
|
|
130
145
|
}
|
|
131
|
-
selectOption(selector, value) {
|
|
132
|
-
return this.page.selectOption(selector, value).then(() => undefined);
|
|
146
|
+
selectOption(selector, value, timeoutMs) {
|
|
147
|
+
return this.page.selectOption(selector, value, budget(timeoutMs)).then(() => undefined);
|
|
133
148
|
}
|
|
134
|
-
setChecked(selector, checked) {
|
|
135
|
-
return this.page.setChecked(selector, checked);
|
|
149
|
+
setChecked(selector, checked, timeoutMs) {
|
|
150
|
+
return this.page.setChecked(selector, checked, budget(timeoutMs));
|
|
151
|
+
}
|
|
152
|
+
/** Selectors a `hide` step registered; re-applied before every screenshot (see {@link syncHidden}). */
|
|
153
|
+
hidden = new Set();
|
|
154
|
+
async hideElements(selector, timeoutMs) {
|
|
155
|
+
// Wait for the element to exist, not to be visible: it may already be hidden by the page.
|
|
156
|
+
await this.page
|
|
157
|
+
.locator(selector)
|
|
158
|
+
.first()
|
|
159
|
+
.waitFor({ state: "attached", ...budget(timeoutMs) });
|
|
160
|
+
this.hidden.add(selector);
|
|
161
|
+
try {
|
|
162
|
+
await this.syncHidden();
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
this.hidden.delete(selector); // a rule the page can't take must not poison every later screenshot
|
|
166
|
+
throw e;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async showElements(selector) {
|
|
170
|
+
if (selector === null)
|
|
171
|
+
this.hidden.clear();
|
|
172
|
+
else
|
|
173
|
+
this.hidden.delete(selector);
|
|
174
|
+
await this.syncHidden();
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Re-mark every element a registered selector matches right now. Navigation drops the marks and
|
|
178
|
+
* a framework re-render can replace the elements, so the registry (not the DOM) is the source of
|
|
179
|
+
* truth and the marks are rebuilt from it. Unmark first so a `show` takes effect.
|
|
180
|
+
*/
|
|
181
|
+
async syncHidden() {
|
|
182
|
+
await this.page.locator(`[${HIDDEN_ATTR}]`).evaluateAll(unmarkHidden, HIDDEN_ATTR);
|
|
183
|
+
for (const selector of this.hidden) {
|
|
184
|
+
await this.page.locator(selector).evaluateAll(markHidden, HIDDEN_ATTR);
|
|
185
|
+
}
|
|
136
186
|
}
|
|
137
187
|
waitForNetworkIdle() {
|
|
138
188
|
return this.page.waitForLoadState("networkidle");
|
|
@@ -161,6 +211,9 @@ export class PlaywrightDriver {
|
|
|
161
211
|
await this.page.waitForTimeout(100);
|
|
162
212
|
}
|
|
163
213
|
}
|
|
214
|
+
async waitForSettled(timeoutMs) {
|
|
215
|
+
await runSettle((args) => this.page.evaluate(awaitSettled, args), timeoutMs);
|
|
216
|
+
}
|
|
164
217
|
waitForSelector(selector, timeoutMs) {
|
|
165
218
|
return this.page
|
|
166
219
|
.waitForSelector(selector, timeoutMs ? { timeout: timeoutMs } : {})
|
|
@@ -256,15 +309,32 @@ export class PlaywrightDriver {
|
|
|
256
309
|
})
|
|
257
310
|
.catch(() => null);
|
|
258
311
|
}
|
|
312
|
+
async nearbyBoxes(selector, radius, timeoutMs) {
|
|
313
|
+
const loc = this.page.locator(selector).first();
|
|
314
|
+
try {
|
|
315
|
+
await loc.waitFor({
|
|
316
|
+
state: "visible",
|
|
317
|
+
...(timeoutMs !== undefined ? { timeout: timeoutMs } : {}),
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
// `evaluate` has no timeout of its own; a hung page must not stall the run.
|
|
324
|
+
return withTimeout(loc.evaluate(collectNearbyBoxes, radius), timeoutMs);
|
|
325
|
+
}
|
|
259
326
|
async screenshot(relPath, redactions = []) {
|
|
260
327
|
// relPath segments carry flow names + step ids from the flow-file — containment-checked
|
|
261
328
|
// (symlink-aware) against the doc-pack root before writing.
|
|
262
|
-
const abs = await resolveWorkspacePathReal(this.
|
|
329
|
+
const abs = await resolveWorkspacePathReal(this.outputRoot, relPath);
|
|
263
330
|
await fs.mkdir(path.dirname(abs), { recursive: true });
|
|
264
331
|
// `animations: "disabled"` fast-forwards finite CSS animations/transitions to their end state
|
|
265
332
|
// and cancels infinite ones, so an element transitioning in (opacity/transform) is captured
|
|
266
333
|
// fully settled instead of mid-fade. `caret: "hide"` keeps a blinking text caret out of shots.
|
|
267
334
|
const shotOptions = { animations: "disabled", caret: "hide" };
|
|
335
|
+
// Elements a `hide` step registered are re-marked first: a navigation or re-render since may have dropped the marks.
|
|
336
|
+
if (this.hidden.size > 0)
|
|
337
|
+
await this.syncHidden();
|
|
268
338
|
if (redactions.length === 0) {
|
|
269
339
|
await this.page.screenshot({ path: abs, ...shotOptions });
|
|
270
340
|
return;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type StorageState } from "./auth.js";
|
|
2
|
+
import { type FlowFile } from "./doc-pack.js";
|
|
3
|
+
import { type FlowVariant } from "./flow-matrix.js";
|
|
4
|
+
export interface RunFlowsOptions {
|
|
5
|
+
/** The workspace: flows, auth and `upload` sources resolve against it. */
|
|
6
|
+
projectDir: string;
|
|
7
|
+
/** Where `docs/<flow>/…` is written. The workspace itself for a plain `run`. */
|
|
8
|
+
outputRoot: string;
|
|
9
|
+
/** Flows to run. A flow with a `matrix` runs once per variant (see `expandFlow`). */
|
|
10
|
+
flows: FlowFile[];
|
|
11
|
+
/** Run only the matrix variant with this id; other variants and flows without a matrix are skipped. */
|
|
12
|
+
variant?: string | undefined;
|
|
13
|
+
storageState?: StorageState | undefined;
|
|
14
|
+
baseURL?: string | undefined;
|
|
15
|
+
headed: boolean;
|
|
16
|
+
ignoreHTTPSErrors: boolean;
|
|
17
|
+
cdpEndpoint?: string | undefined;
|
|
18
|
+
stopAfter?: string | undefined;
|
|
19
|
+
startFrom?: string | undefined;
|
|
20
|
+
pause: boolean;
|
|
21
|
+
/** `annotations.obstacles` from the workspace config. */
|
|
22
|
+
obstacles: boolean;
|
|
23
|
+
concurrency: number;
|
|
24
|
+
/** Progress lines (`run: …`). stdout for `run`; stderr in verify mode so stdout stays the report. */
|
|
25
|
+
progress: (line: string) => void;
|
|
26
|
+
}
|
|
27
|
+
/** `<flow>` for a flow without a matrix, `<flow>/<variant>` for a variant (the name `render` and `burn` use). */
|
|
28
|
+
export declare function unitLabel(unit: FlowVariant): string;
|
|
29
|
+
/** A unit that did not finish. `flow` is the unit label; `step` is set when the runtime halted on a step. */
|
|
30
|
+
export interface FlowFailure {
|
|
31
|
+
flow: string;
|
|
32
|
+
step: string | null;
|
|
33
|
+
message: string;
|
|
34
|
+
}
|
|
35
|
+
export interface RunFlowsResult {
|
|
36
|
+
okCount: number;
|
|
37
|
+
failures: FlowFailure[];
|
|
38
|
+
}
|
|
39
|
+
/** Execute `opts.flows`, up to `opts.concurrency` at a time, each in its own browser session. */
|
|
40
|
+
export declare function runFlowsInSessions(opts: RunFlowsOptions): Promise<RunFlowsResult>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// The flow-execution loop behind `docsxai run`: one Playwright session per unit (a flow, or one
|
|
2
|
+
// matrix variant of a flow), a worker pool for `--concurrency`, and the write of each unit's
|
|
3
|
+
// annotations.json under an output root (`docs/<flow>/`, or `docs/<flow>/<variant>/` for a matrix).
|
|
4
|
+
// Split out of the CLI handler so `run --verify-determinism` drives the identical code path against
|
|
5
|
+
// an isolated output root instead of the workspace.
|
|
6
|
+
import { promises as fs } from "node:fs";
|
|
7
|
+
import { FlowExecutionError } from "./flow-halt.js";
|
|
8
|
+
import { expandFlows } from "./flow-matrix.js";
|
|
9
|
+
import { runFlow } from "./flow-runtime.js";
|
|
10
|
+
import { launchPlaywrightSession } from "./playwright-driver.js";
|
|
11
|
+
import { resolveWorkspacePath, resolveWorkspacePathReal } from "./workspace.js";
|
|
12
|
+
/** `<flow>` for a flow without a matrix, `<flow>/<variant>` for a variant (the name `render` and `burn` use). */
|
|
13
|
+
export function unitLabel(unit) {
|
|
14
|
+
return unit.id ? `${unit.flow.name}/${unit.id}` : unit.flow.name;
|
|
15
|
+
}
|
|
16
|
+
/** The units `opts` selects: every flow expanded, narrowed to `opts.variant` when it is set. */
|
|
17
|
+
function selectUnits(opts) {
|
|
18
|
+
const units = expandFlows(opts.flows);
|
|
19
|
+
return opts.variant ? units.filter((u) => u.id === opts.variant) : units;
|
|
20
|
+
}
|
|
21
|
+
/** Merge a `--start-from` partial run into the annotations file already on disk, by step id. */
|
|
22
|
+
async function mergeAnnotations(annotationsPath, fresh) {
|
|
23
|
+
try {
|
|
24
|
+
const existing = JSON.parse(await fs.readFile(annotationsPath, "utf8"));
|
|
25
|
+
const newStepIds = new Set(fresh.annotations.map((a) => a.step));
|
|
26
|
+
const merged = [
|
|
27
|
+
...existing.annotations.filter((a) => !newStepIds.has(a.step)),
|
|
28
|
+
...fresh.annotations,
|
|
29
|
+
];
|
|
30
|
+
return { ...fresh, annotations: merged };
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// No existing file (or unreadable) — just write what we have.
|
|
34
|
+
return fresh;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Execute `opts.flows`, up to `opts.concurrency` at a time, each in its own browser session. */
|
|
38
|
+
export async function runFlowsInSessions(opts) {
|
|
39
|
+
const { projectDir, outputRoot, startFrom, stopAfter, pause } = opts;
|
|
40
|
+
const tag = opts.concurrency > 1 ? (name) => `run [${name}]: ` : () => "run: ";
|
|
41
|
+
const say = (line) => opts.progress(line);
|
|
42
|
+
const failures = [];
|
|
43
|
+
async function runOne(unit) {
|
|
44
|
+
const { flow } = unit;
|
|
45
|
+
const name = unitLabel(unit);
|
|
46
|
+
let session;
|
|
47
|
+
try {
|
|
48
|
+
// When attaching to an existing Chrome via --cdp, the operator owns its auth state — don't
|
|
49
|
+
// load cached `storageState` over it (would replace cookies). When launching fresh, do.
|
|
50
|
+
session = await launchPlaywrightSession({
|
|
51
|
+
baseURL: opts.baseURL,
|
|
52
|
+
headed: opts.headed,
|
|
53
|
+
ignoreHTTPSErrors: opts.ignoreHTTPSErrors,
|
|
54
|
+
...(opts.cdpEndpoint
|
|
55
|
+
? { connectOverCdp: opts.cdpEndpoint }
|
|
56
|
+
: { storageState: opts.storageState }),
|
|
57
|
+
...(flow.environment ? { environment: flow.environment } : {}),
|
|
58
|
+
docPackRoot: projectDir,
|
|
59
|
+
outputRoot,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
const msg = e.message;
|
|
64
|
+
const reason = /Executable doesn't exist|browserType\.launch|playwright install/i.test(msg)
|
|
65
|
+
? "no Chromium binary found. Install one: npx playwright-core install chromium (source checkout: pnpm -C packages/engine exec playwright-core install chromium)"
|
|
66
|
+
: `failed to launch browser: ${msg}`;
|
|
67
|
+
process.stderr.write(`${tag(name)}${reason}\n`);
|
|
68
|
+
failures.push({ flow: name, step: null, message: reason });
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const result = await runFlow(flow, session.driver, {
|
|
73
|
+
resolveLocator: (n) => flow.locators[n],
|
|
74
|
+
...(unit.info ? { variant: unit.info } : {}),
|
|
75
|
+
...(stopAfter ? { stopAfter } : {}),
|
|
76
|
+
...(startFrom ? { startFrom } : {}),
|
|
77
|
+
...(opts.obstacles ? { obstacles: true } : {}),
|
|
78
|
+
});
|
|
79
|
+
const docSegments = ["docs", flow.name, ...(unit.id ? [unit.id] : [])];
|
|
80
|
+
await fs.mkdir(resolveWorkspacePath(outputRoot, ...docSegments), { recursive: true });
|
|
81
|
+
// Flow names and variant ids come from the flow-files — resolve the write target symlink-aware.
|
|
82
|
+
const annotationsPath = await resolveWorkspacePathReal(outputRoot, ...docSegments, "annotations.json");
|
|
83
|
+
// With `startFrom`, only the post-startFrom steps emit annotations — merge them into the
|
|
84
|
+
// existing file (if any) by step id so the prior steps' annotations stay in place. Same
|
|
85
|
+
// story for screenshots (they live as separate PNGs and are simply not re-captured).
|
|
86
|
+
const toWrite = startFrom
|
|
87
|
+
? await mergeAnnotations(annotationsPath, result.annotations)
|
|
88
|
+
: result.annotations;
|
|
89
|
+
await fs.writeFile(annotationsPath, JSON.stringify(toWrite, null, 2) + "\n", "utf8");
|
|
90
|
+
say(`${tag(name)}${name} — ${result.steps.length} step(s) executed, ${result.annotations.annotations.length} annotation(s) ${startFrom ? "merged" : "written"}\n`);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
const message = e.message;
|
|
95
|
+
process.stderr.write(`${tag(name)}${message}\n`);
|
|
96
|
+
failures.push({
|
|
97
|
+
flow: name,
|
|
98
|
+
step: e instanceof FlowExecutionError ? e.stepId : null,
|
|
99
|
+
message,
|
|
100
|
+
});
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
if (pause) {
|
|
105
|
+
say("run: --pause — browser is open at the last step run; close it to exit.\n");
|
|
106
|
+
await new Promise((resolve) => session.browser.on("disconnected", () => resolve()));
|
|
107
|
+
}
|
|
108
|
+
await session.close();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const units = selectUnits(opts);
|
|
112
|
+
let idx = 0;
|
|
113
|
+
let okCount = 0;
|
|
114
|
+
async function worker() {
|
|
115
|
+
while (idx < units.length) {
|
|
116
|
+
const unit = units[idx++];
|
|
117
|
+
if (await runOne(unit))
|
|
118
|
+
okCount++;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const workers = Math.min(opts.concurrency, units.length);
|
|
122
|
+
const noun = units.some((u) => u.id) ? "flow variants" : "flows";
|
|
123
|
+
if (workers > 1)
|
|
124
|
+
say(`run: running ${units.length} ${noun} with ${workers} parallel worker(s)…\n`);
|
|
125
|
+
await Promise.all(Array.from({ length: workers }, () => worker()));
|
|
126
|
+
// Workers finish in completion order; sort so the failure list is stable across runs.
|
|
127
|
+
failures.sort((a, b) => (a.flow < b.flow ? -1 : a.flow > b.flow ? 1 : 0));
|
|
128
|
+
return { okCount, failures };
|
|
129
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** An `upload` value that reaches outside the workspace or does not name a regular file. */
|
|
2
|
+
export declare class UploadPathError extends Error {
|
|
3
|
+
constructor(value: string, reason: string);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Why `value` cannot be an upload path on its spelling alone, or null: empty, with a NUL byte,
|
|
7
|
+
* absolute (a leading slash or backslash, or a drive letter), with a `..` segment, with a segment
|
|
8
|
+
* that starts with a dot (`.auth/` holds captured sessions, `.docsxai.json` the workspace config) or
|
|
9
|
+
* naming `webhook-job.json`. The backend
|
|
10
|
+
* keeps a copy of this rule for flow files it stores, and a test in `packages/docsxai` holds the
|
|
11
|
+
* two to the same answers.
|
|
12
|
+
*/
|
|
13
|
+
export declare function uploadPathProblem(value: string): string | null;
|
|
14
|
+
/** The real path of the file `value` names under `root`; throws {@link UploadPathError} otherwise. */
|
|
15
|
+
export declare function resolveUploadPath(root: string, value: string): Promise<string>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Where an `upload` step may read from. The step hands a file to the page it is driving, so a
|
|
2
|
+
// value that reaches outside the workspace (`/proc/self/environ`, a key under the home directory)
|
|
3
|
+
// would put that file in front of the app's own script. The value must be a path relative to the
|
|
4
|
+
// workspace root, with no `..` segment, that names a regular file whose real path (symlinks
|
|
5
|
+
// followed) is still under the root.
|
|
6
|
+
import { promises as fs } from "node:fs";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import { resolveWorkspacePathReal, WorkspacePathEscapeError } from "./workspace.js";
|
|
9
|
+
/** An `upload` value that reaches outside the workspace or does not name a regular file. */
|
|
10
|
+
export class UploadPathError extends Error {
|
|
11
|
+
constructor(value, reason) {
|
|
12
|
+
super(`upload path ${JSON.stringify(value)} ${reason}`);
|
|
13
|
+
this.name = "UploadPathError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Why `value` cannot be an upload path on its spelling alone, or null: empty, with a NUL byte,
|
|
18
|
+
* absolute (a leading slash or backslash, or a drive letter), with a `..` segment, with a segment
|
|
19
|
+
* that starts with a dot (`.auth/` holds captured sessions, `.docsxai.json` the workspace config) or
|
|
20
|
+
* naming `webhook-job.json`. The backend
|
|
21
|
+
* keeps a copy of this rule for flow files it stores, and a test in `packages/docsxai` holds the
|
|
22
|
+
* two to the same answers.
|
|
23
|
+
*/
|
|
24
|
+
export function uploadPathProblem(value) {
|
|
25
|
+
if (value === "")
|
|
26
|
+
return "is empty";
|
|
27
|
+
if (value.includes("\0"))
|
|
28
|
+
return "contains a NUL byte";
|
|
29
|
+
if (/^(?:[\\/]|[A-Za-z]:)/.test(value))
|
|
30
|
+
return "is an absolute path";
|
|
31
|
+
const segments = value.split(/[\\/]+/);
|
|
32
|
+
if (segments.includes(".."))
|
|
33
|
+
return "has a .. segment";
|
|
34
|
+
if (segments.some((s) => s.startsWith(".") && s !== "."))
|
|
35
|
+
return "has a hidden segment";
|
|
36
|
+
if (segments.some((s) => s.toLowerCase() === "webhook-job.json"))
|
|
37
|
+
return "is a reserved file";
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
/** The real path of the file `value` names under `root`; throws {@link UploadPathError} otherwise. */
|
|
41
|
+
export async function resolveUploadPath(root, value) {
|
|
42
|
+
const spelling = uploadPathProblem(value);
|
|
43
|
+
if (spelling)
|
|
44
|
+
throw new UploadPathError(value, spelling);
|
|
45
|
+
let real;
|
|
46
|
+
try {
|
|
47
|
+
real = await fs.realpath(await resolveWorkspacePathReal(root, value));
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
if (e instanceof WorkspacePathEscapeError) {
|
|
51
|
+
throw new UploadPathError(value, "is outside the workspace");
|
|
52
|
+
}
|
|
53
|
+
throw new UploadPathError(value, "does not name a readable file under the workspace");
|
|
54
|
+
}
|
|
55
|
+
// A symlink under an innocent name must not lead to a hidden or reserved file either.
|
|
56
|
+
const target = path.relative(await fs.realpath(root), real);
|
|
57
|
+
const reserved = uploadPathProblem(target);
|
|
58
|
+
if (reserved)
|
|
59
|
+
throw new UploadPathError(value, `leads to a path that ${reserved}`);
|
|
60
|
+
if (!(await fs.stat(real)).isFile())
|
|
61
|
+
throw new UploadPathError(value, "is not a regular file");
|
|
62
|
+
return real;
|
|
63
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type DimensionChange, type DriftRegion } from "./diff.js";
|
|
2
|
+
export type DifferenceKind = "missing" | "extra" | "json" | "png" | "text" | "bytes";
|
|
3
|
+
export interface ArtefactDifference {
|
|
4
|
+
/** Root-relative, `/`-separated. */
|
|
5
|
+
path: string;
|
|
6
|
+
/** The run that disagreed with run 1 (2 and up). */
|
|
7
|
+
run: number;
|
|
8
|
+
kind: DifferenceKind;
|
|
9
|
+
/** One line a person can act on. */
|
|
10
|
+
hint: string;
|
|
11
|
+
size?: {
|
|
12
|
+
a: number;
|
|
13
|
+
b: number;
|
|
14
|
+
};
|
|
15
|
+
/** `json`: where the two documents first differ, e.g. `annotations[0].bounding_box.x`. */
|
|
16
|
+
json_path?: string;
|
|
17
|
+
/** `png`: bounding box of the changed pixels. */
|
|
18
|
+
region?: DriftRegion;
|
|
19
|
+
changed_pixel_count?: number;
|
|
20
|
+
dimension_change?: DimensionChange;
|
|
21
|
+
/** `text`: 1-based line of the first difference. */
|
|
22
|
+
line?: number;
|
|
23
|
+
}
|
|
24
|
+
export type ContentDifference = Omit<ArtefactDifference, "path" | "run">;
|
|
25
|
+
/** First difference between two parsed JSON values, in a stable order: array indices ascending, object keys ordinal. */
|
|
26
|
+
export declare function firstJsonDifference(a: unknown, b: unknown, at?: string): {
|
|
27
|
+
path: string;
|
|
28
|
+
a: unknown;
|
|
29
|
+
b: unknown;
|
|
30
|
+
} | null;
|
|
31
|
+
/** Compare one artefact's two byte strings. `null` when they are identical. */
|
|
32
|
+
export declare function compareArtefactBytes(artefactPath: string, a: Buffer, b: Buffer): ContentDifference | null;
|
|
33
|
+
/** Artefacts above this size are compared as raw bytes in chunks and never decoded or held whole. */
|
|
34
|
+
export declare const MAX_COMPARE_BYTES: number;
|
|
35
|
+
export interface TreeComparison {
|
|
36
|
+
/** Artefacts in run 1, the side every other run is compared against. */
|
|
37
|
+
compared: number;
|
|
38
|
+
differences: ArtefactDifference[];
|
|
39
|
+
}
|
|
40
|
+
/** Compare run root `a` (run 1) with run root `b` (run `run`). Differences come out in ordinal path order. */
|
|
41
|
+
export declare function compareRunRoots(aRoot: string, bRoot: string, run: number, maxBytes?: number): Promise<TreeComparison>;
|
|
42
|
+
/** Compare run 1 against every other run. Differences are ordered by path, then by run. */
|
|
43
|
+
export declare function compareRuns(roots: string[]): Promise<TreeComparison>;
|