@blazediff/agent 0.2.1 → 0.3.0
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 +36 -12
- package/SKILL.md +56 -10
- package/dist/cli.js +598 -397
- package/dist/index.d.mts +79 -12
- package/dist/index.d.ts +79 -12
- package/dist/index.js +429 -191
- package/dist/index.mjs +407 -193
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readFile, mkdir, writeFile, access, readdir, stat, rm, rename } from 'fs/promises';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { availableParallelism, cpus } from 'os';
|
|
4
|
+
import { existsSync, readFileSync } from 'fs';
|
|
5
|
+
import { pathToFileURL } from 'url';
|
|
4
6
|
import { chromium } from 'playwright';
|
|
5
7
|
import { spawn } from 'child_process';
|
|
6
|
-
import { existsSync } from 'fs';
|
|
7
8
|
import { createRequire } from 'module';
|
|
8
|
-
import { pathToFileURL } from 'url';
|
|
9
9
|
import { createHash } from 'crypto';
|
|
10
10
|
import { compare } from '@blazediff/core-native';
|
|
11
|
+
import * as util from 'util';
|
|
11
12
|
import { Annotation, Command, StateGraph, START, Send, END, interrupt } from '@langchain/langgraph';
|
|
12
13
|
import sharp from 'sharp';
|
|
13
14
|
import { BaseCheckpointSaver, TASKS, maxChannelVersion, getCheckpointId, copyCheckpoint, WRITES_IDX_MAP } from '@langchain/langgraph-checkpoint';
|
|
@@ -34,6 +35,8 @@ var paths = (cwd = process.cwd()) => {
|
|
|
34
35
|
root,
|
|
35
36
|
config: path.join(root, "config.json"),
|
|
36
37
|
manifest: path.join(root, "manifest.json"),
|
|
38
|
+
harnesses: path.join(root, "harnesses"),
|
|
39
|
+
authHarness: path.join(root, "harnesses", "auth.js"),
|
|
37
40
|
baselines: path.join(root, "baselines"),
|
|
38
41
|
actual: path.join(root, "actual"),
|
|
39
42
|
judgments: path.join(root, "judgments"),
|
|
@@ -44,6 +47,90 @@ var paths = (cwd = process.cwd()) => {
|
|
|
44
47
|
serverPid: path.join(root, "dev-server.pid")
|
|
45
48
|
};
|
|
46
49
|
};
|
|
50
|
+
|
|
51
|
+
// src/harness/loader.ts
|
|
52
|
+
var HarnessError = class extends Error {
|
|
53
|
+
constructor(message, cause) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.cause = cause;
|
|
56
|
+
this.name = "HarnessError";
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var moduleCache = /* @__PURE__ */ new Map();
|
|
60
|
+
function resolveHarnessFile(name, cwd) {
|
|
61
|
+
if (name.includes("/") || name.includes("\\") || path.isAbsolute(name)) {
|
|
62
|
+
return path.isAbsolute(name) ? name : path.resolve(cwd, name);
|
|
63
|
+
}
|
|
64
|
+
const dir = paths(cwd).harnesses;
|
|
65
|
+
const mjs = path.join(dir, `${name}.mjs`);
|
|
66
|
+
if (existsSync(mjs)) return mjs;
|
|
67
|
+
return path.join(dir, `${name}.js`);
|
|
68
|
+
}
|
|
69
|
+
async function loadHarness(harnessFile) {
|
|
70
|
+
const absolute = path.isAbsolute(harnessFile) ? harnessFile : path.resolve(process.cwd(), harnessFile);
|
|
71
|
+
if (!existsSync(absolute)) {
|
|
72
|
+
throw new HarnessError(
|
|
73
|
+
`harness not found at ${absolute}. Author one under .blazediff/harnesses/ (or run \`blazediff-agent auth init\` for a login harness).`
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
const cached = moduleCache.get(absolute);
|
|
77
|
+
if (cached) return cached;
|
|
78
|
+
const url = pathToFileURL(absolute).href;
|
|
79
|
+
const loading = (async () => {
|
|
80
|
+
let mod;
|
|
81
|
+
try {
|
|
82
|
+
mod = await import(url);
|
|
83
|
+
} catch (err) {
|
|
84
|
+
throw new HarnessError(
|
|
85
|
+
`failed to load harness at ${absolute}: ${err.message}`,
|
|
86
|
+
err
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const harness = mod.default;
|
|
90
|
+
if (!harness || typeof harness.run !== "function") {
|
|
91
|
+
if (typeof mod.login === "function") {
|
|
92
|
+
throw new HarnessError(
|
|
93
|
+
`legacy auth harness detected at ${absolute} (exports \`login\`). Re-run \`blazediff-agent auth init\` to regenerate it as a harness.`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
throw new HarnessError(
|
|
97
|
+
`harness at ${absolute} must default-export an object with a \`run(ctx)\` function`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return { phase: harness.phase ?? "interact", run: harness.run };
|
|
101
|
+
})();
|
|
102
|
+
moduleCache.set(absolute, loading);
|
|
103
|
+
return loading;
|
|
104
|
+
}
|
|
105
|
+
function loginPathOf(loginUrl) {
|
|
106
|
+
try {
|
|
107
|
+
return new URL(loginUrl, "http://placeholder").pathname;
|
|
108
|
+
} catch {
|
|
109
|
+
return loginUrl;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function assertLeftLoginPage(page, loginUrl) {
|
|
113
|
+
const loginPath = loginPathOf(loginUrl);
|
|
114
|
+
let currentPath;
|
|
115
|
+
try {
|
|
116
|
+
currentPath = new URL(page.url()).pathname;
|
|
117
|
+
} catch {
|
|
118
|
+
currentPath = page.url();
|
|
119
|
+
}
|
|
120
|
+
if (loginPath && currentPath === loginPath) {
|
|
121
|
+
throw new HarnessError(
|
|
122
|
+
`login harness returned but page is still on the login URL (${currentPath}). Check that the harness submits the form and waits for redirect.`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
await page.locator('input[type="password"]').first().waitFor({ state: "detached", timeout: 2e3 }).catch(() => {
|
|
126
|
+
});
|
|
127
|
+
const passwordInputs = await page.locator('input[type="password"]:visible').count().catch(() => 0);
|
|
128
|
+
if (passwordInputs > 0) {
|
|
129
|
+
throw new HarnessError(
|
|
130
|
+
`login harness returned but a visible password input is still on the page. The login likely did not complete (wrong credentials, captcha, or selectors out of date).`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
47
134
|
var FROZEN_NOW = Date.UTC(2025, 0, 1, 0, 0, 0);
|
|
48
135
|
var STABILITY_CSS = `
|
|
49
136
|
*,*::before,*::after{
|
|
@@ -204,6 +291,11 @@ var DEFAULT_MASK_SELECTOR = `[${DEFAULT_MASK_ATTR}]`;
|
|
|
204
291
|
async function applyMaskOverlays(page, masks) {
|
|
205
292
|
const selectors = [DEFAULT_MASK_SELECTOR, ...masks];
|
|
206
293
|
await page.evaluate((selectors2) => {
|
|
294
|
+
for (const stale of Array.from(
|
|
295
|
+
document.querySelectorAll("[data-blazediff-mask]")
|
|
296
|
+
)) {
|
|
297
|
+
stale.remove();
|
|
298
|
+
}
|
|
207
299
|
for (const sel of selectors2) {
|
|
208
300
|
for (const el of Array.from(
|
|
209
301
|
document.querySelectorAll(sel)
|
|
@@ -226,39 +318,90 @@ async function applyMaskOverlays(page, masks) {
|
|
|
226
318
|
}
|
|
227
319
|
|
|
228
320
|
// src/browser/capture.ts
|
|
229
|
-
|
|
321
|
+
var SCREENSHOT_NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/i;
|
|
322
|
+
async function shoot(page, outputPath, fullPage) {
|
|
323
|
+
await mkdir(path.dirname(outputPath), { recursive: true });
|
|
324
|
+
const buffer = await page.screenshot({
|
|
325
|
+
path: outputPath,
|
|
326
|
+
type: "png",
|
|
327
|
+
fullPage,
|
|
328
|
+
animations: "disabled",
|
|
329
|
+
caret: "hide",
|
|
330
|
+
scale: "device"
|
|
331
|
+
});
|
|
332
|
+
return buffer.length;
|
|
333
|
+
}
|
|
334
|
+
async function stabilize(page, waitFor, masks) {
|
|
335
|
+
await waitForStability(page, waitFor);
|
|
336
|
+
await applyMaskOverlays(page, masks);
|
|
337
|
+
await page.evaluate(
|
|
338
|
+
() => new Promise((r) => requestAnimationFrame(() => r()))
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
async function captureScreenshot(baseUrl, opts, cwd = process.cwd(), harnesses) {
|
|
230
342
|
const viewport = opts.viewport ?? DEFAULT_VIEWPORT;
|
|
231
343
|
const waitFor = opts.waitFor ?? DEFAULT_WAIT_FOR;
|
|
232
344
|
const masks = opts.mask ?? [];
|
|
233
345
|
const fullPage = opts.fullPage ?? DEFAULT_FULL_PAGE;
|
|
346
|
+
const setup = (harnesses ?? []).filter(
|
|
347
|
+
(h) => (h.harness.phase ?? "interact") === "setup"
|
|
348
|
+
);
|
|
349
|
+
const interact = (harnesses ?? []).filter(
|
|
350
|
+
(h) => (h.harness.phase ?? "interact") !== "setup"
|
|
351
|
+
);
|
|
234
352
|
const handle = await acquireStableContext(viewport, {
|
|
235
|
-
pool: !
|
|
353
|
+
pool: !(harnesses && harnesses.length > 0),
|
|
236
354
|
baseURL: baseUrl || void 0
|
|
237
355
|
});
|
|
356
|
+
const context = handle.context;
|
|
357
|
+
const browser = await getBrowser();
|
|
238
358
|
const page = await openStablePage(handle);
|
|
239
359
|
try {
|
|
240
|
-
|
|
241
|
-
|
|
360
|
+
const rejectScreenshot = async () => {
|
|
361
|
+
throw new HarnessError("setup harness must not call screenshot()");
|
|
362
|
+
};
|
|
363
|
+
for (const { harness, params } of setup) {
|
|
364
|
+
await harness.run({
|
|
365
|
+
page,
|
|
366
|
+
browser,
|
|
367
|
+
context,
|
|
368
|
+
params,
|
|
369
|
+
screenshot: rejectScreenshot
|
|
370
|
+
});
|
|
242
371
|
}
|
|
243
372
|
const url = new URL(opts.url, baseUrl).toString();
|
|
244
373
|
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 3e4 });
|
|
245
|
-
await
|
|
246
|
-
await applyMaskOverlays(page, masks);
|
|
247
|
-
await page.evaluate(
|
|
248
|
-
() => new Promise((r) => requestAnimationFrame(() => r()))
|
|
249
|
-
);
|
|
374
|
+
await stabilize(page, waitFor, masks);
|
|
250
375
|
const outputDir = opts.mode === "baseline" ? paths(cwd).baselines : paths(cwd).actual;
|
|
251
|
-
await mkdir(outputDir, { recursive: true });
|
|
252
376
|
const outputPath = path.join(outputDir, `${opts.id}.png`);
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
377
|
+
const bytes = await shoot(page, outputPath, fullPage);
|
|
378
|
+
const subCaptures = [];
|
|
379
|
+
const used = /* @__PURE__ */ new Set();
|
|
380
|
+
const screenshot = async (name) => {
|
|
381
|
+
if (!SCREENSHOT_NAME_RE.test(name)) {
|
|
382
|
+
throw new HarnessError(
|
|
383
|
+
`invalid screenshot name "${name}": use alphanumeric/kebab-case (no "__", spaces, or slashes)`
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
if (used.has(name)) {
|
|
387
|
+
throw new HarnessError(`duplicate screenshot name "${name}"`);
|
|
388
|
+
}
|
|
389
|
+
used.add(name);
|
|
390
|
+
await stabilize(page, waitFor, masks);
|
|
391
|
+
const subPath = path.join(outputDir, `${opts.id}__${name}.png`);
|
|
392
|
+
const subBytes = await shoot(page, subPath, fullPage);
|
|
393
|
+
subCaptures.push({ name, outputPath: subPath, bytes: subBytes });
|
|
394
|
+
};
|
|
395
|
+
for (const { harness, params } of interact) {
|
|
396
|
+
await harness.run({ page, browser, context, params, screenshot });
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
id: opts.id,
|
|
400
|
+
outputPath,
|
|
401
|
+
mode: opts.mode,
|
|
402
|
+
bytes,
|
|
403
|
+
subCaptures: subCaptures.length > 0 ? subCaptures : void 0
|
|
404
|
+
};
|
|
262
405
|
} finally {
|
|
263
406
|
await page.close().catch(() => {
|
|
264
407
|
});
|
|
@@ -312,112 +455,6 @@ async function installBrowsers(opts = {}) {
|
|
|
312
455
|
const executablePath = await readExecutablePath();
|
|
313
456
|
return { installed: Boolean(executablePath), executablePath, cliPath };
|
|
314
457
|
}
|
|
315
|
-
|
|
316
|
-
// src/auth/env.ts
|
|
317
|
-
var LEGACY_REQUIRED = "required";
|
|
318
|
-
var DEFAULT_PERSONA = "default";
|
|
319
|
-
function canonicalPersona(auth) {
|
|
320
|
-
return auth === LEGACY_REQUIRED ? DEFAULT_PERSONA : auth;
|
|
321
|
-
}
|
|
322
|
-
var AuthHarnessError = class extends Error {
|
|
323
|
-
constructor(message, cause) {
|
|
324
|
-
super(message);
|
|
325
|
-
this.cause = cause;
|
|
326
|
-
this.name = "AuthHarnessError";
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
var moduleCache = /* @__PURE__ */ new Map();
|
|
330
|
-
async function loadAuthHarness(harnessFile) {
|
|
331
|
-
const absolute = path.isAbsolute(harnessFile) ? harnessFile : path.resolve(process.cwd(), harnessFile);
|
|
332
|
-
if (!existsSync(absolute)) {
|
|
333
|
-
throw new AuthHarnessError(
|
|
334
|
-
`auth harness not found at ${absolute}. Run \`blazediff-agent auth init\` to create one.`
|
|
335
|
-
);
|
|
336
|
-
}
|
|
337
|
-
const cached = moduleCache.get(absolute);
|
|
338
|
-
if (cached) return cached;
|
|
339
|
-
const url = pathToFileURL(absolute).href;
|
|
340
|
-
const loading = (async () => {
|
|
341
|
-
try {
|
|
342
|
-
const mod = await import(url);
|
|
343
|
-
if (typeof mod.login !== "function") {
|
|
344
|
-
throw new AuthHarnessError(
|
|
345
|
-
`auth harness at ${absolute} does not export a \`login\` function`
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
return { login: mod.login };
|
|
349
|
-
} catch (err) {
|
|
350
|
-
if (err instanceof AuthHarnessError) throw err;
|
|
351
|
-
throw new AuthHarnessError(
|
|
352
|
-
`failed to load auth harness at ${absolute}: ${err.message}`,
|
|
353
|
-
err
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
})();
|
|
357
|
-
moduleCache.set(absolute, loading);
|
|
358
|
-
return loading;
|
|
359
|
-
}
|
|
360
|
-
function loginPathOf(loginUrl) {
|
|
361
|
-
try {
|
|
362
|
-
return new URL(loginUrl, "http://placeholder").pathname;
|
|
363
|
-
} catch {
|
|
364
|
-
return loginUrl;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
async function verifyPostLogin(page, loginUrl) {
|
|
368
|
-
const loginPath = loginPathOf(loginUrl);
|
|
369
|
-
let currentPath;
|
|
370
|
-
try {
|
|
371
|
-
currentPath = new URL(page.url()).pathname;
|
|
372
|
-
} catch {
|
|
373
|
-
currentPath = page.url();
|
|
374
|
-
}
|
|
375
|
-
if (loginPath && currentPath === loginPath) {
|
|
376
|
-
throw new AuthHarnessError(
|
|
377
|
-
`auth harness returned but page is still on the login URL (${currentPath}). Check that the harness submits the form and waits for redirect.`
|
|
378
|
-
);
|
|
379
|
-
}
|
|
380
|
-
await page.locator('input[type="password"]').first().waitFor({ state: "detached", timeout: 2e3 }).catch(() => {
|
|
381
|
-
});
|
|
382
|
-
const passwordInputs = await page.locator('input[type="password"]:visible').count().catch(() => 0);
|
|
383
|
-
if (passwordInputs > 0) {
|
|
384
|
-
throw new AuthHarnessError(
|
|
385
|
-
`auth harness returned but a visible password input is still on the page. The login likely did not complete (wrong credentials, captcha, or selectors out of date).`
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// src/auth/hook.ts
|
|
391
|
-
var AuthConfigMissingError = class extends Error {
|
|
392
|
-
constructor(message) {
|
|
393
|
-
super(message);
|
|
394
|
-
this.name = "AuthConfigMissingError";
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
function buildAuthHook(persona, authConfig, cwd) {
|
|
398
|
-
if (!authConfig) {
|
|
399
|
-
throw new AuthConfigMissingError(
|
|
400
|
-
"auth required but .blazediff/config.json has no `auth` block. Run `blazediff-agent auth init`."
|
|
401
|
-
);
|
|
402
|
-
}
|
|
403
|
-
const harnessFile = path.isAbsolute(authConfig.harness) ? authConfig.harness : path.join(cwd, authConfig.harness);
|
|
404
|
-
const canonical = canonicalPersona(persona);
|
|
405
|
-
const loginUrl = authConfig.loginUrl;
|
|
406
|
-
return {
|
|
407
|
-
hook: async (page) => {
|
|
408
|
-
const mod = await loadAuthHarness(harnessFile);
|
|
409
|
-
try {
|
|
410
|
-
await mod.login(page, canonical);
|
|
411
|
-
} catch (err) {
|
|
412
|
-
throw new AuthHarnessError(
|
|
413
|
-
`auth harness threw for persona "${canonical}": ${err.message}`,
|
|
414
|
-
err
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
await verifyPostLogin(page, loginUrl);
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
458
|
async function loadConfig(cwd = process.cwd()) {
|
|
422
459
|
const file = paths(cwd).config;
|
|
423
460
|
if (!existsSync(file)) return null;
|
|
@@ -467,6 +504,16 @@ var Semaphore = class {
|
|
|
467
504
|
var STABILITY_HOOKS_VERSION = 1;
|
|
468
505
|
|
|
469
506
|
// src/manifest.ts
|
|
507
|
+
function serializeHarnesses(harnesses) {
|
|
508
|
+
return JSON.stringify(
|
|
509
|
+
harnesses.map((h) => ({
|
|
510
|
+
name: h.name,
|
|
511
|
+
params: h.params ? Object.fromEntries(
|
|
512
|
+
Object.entries(h.params).sort(([a], [b]) => a.localeCompare(b))
|
|
513
|
+
) : void 0
|
|
514
|
+
}))
|
|
515
|
+
);
|
|
516
|
+
}
|
|
470
517
|
async function loadManifest(cwd = process.cwd()) {
|
|
471
518
|
const file = paths(cwd).manifest;
|
|
472
519
|
if (!existsSync(file)) return null;
|
|
@@ -492,8 +539,9 @@ function hashMaterial(input) {
|
|
|
492
539
|
viewport: input.viewport,
|
|
493
540
|
mask: [...input.mask].sort(),
|
|
494
541
|
waitFor: input.waitFor,
|
|
495
|
-
|
|
542
|
+
harnesses: serializeHarnesses(input.harnesses),
|
|
496
543
|
fullPage: input.fullPage,
|
|
544
|
+
subName: input.subName ?? null,
|
|
497
545
|
hooks: STABILITY_HOOKS_VERSION
|
|
498
546
|
};
|
|
499
547
|
return `sha256:${createHash("sha256").update(JSON.stringify(material)).digest("hex")}`;
|
|
@@ -502,24 +550,27 @@ function makeEntry(input) {
|
|
|
502
550
|
const viewport = input.viewport ?? DEFAULT_VIEWPORT;
|
|
503
551
|
const waitFor = input.waitFor ?? DEFAULT_WAIT_FOR;
|
|
504
552
|
const mask = input.mask ?? [];
|
|
505
|
-
const
|
|
553
|
+
const harnesses = input.harnesses ?? [];
|
|
506
554
|
const fullPage = input.fullPage ?? DEFAULT_FULL_PAGE;
|
|
507
555
|
return {
|
|
508
556
|
id: input.id,
|
|
509
557
|
url: input.url,
|
|
510
558
|
viewport,
|
|
511
|
-
|
|
559
|
+
harnesses: harnesses.length > 0 ? harnesses : void 0,
|
|
512
560
|
waitFor,
|
|
513
561
|
mask,
|
|
514
562
|
fullPage,
|
|
563
|
+
parent: input.parent,
|
|
564
|
+
derived: input.derived || void 0,
|
|
515
565
|
baselinePath: path.posix.join(".blazediff", "baselines", `${input.id}.png`),
|
|
516
566
|
captureHash: hashMaterial({
|
|
517
567
|
url: input.url,
|
|
518
568
|
viewport,
|
|
519
569
|
mask,
|
|
520
570
|
waitFor,
|
|
521
|
-
|
|
522
|
-
fullPage
|
|
571
|
+
harnesses,
|
|
572
|
+
fullPage,
|
|
573
|
+
subName: input.subName
|
|
523
574
|
}),
|
|
524
575
|
createdBy: input.createdBy ?? "agent",
|
|
525
576
|
createdAt: (/* @__PURE__ */ new Date()).toISOString().slice(0, 10)
|
|
@@ -532,19 +583,40 @@ function addOrReplaceEntry(manifest, entry) {
|
|
|
532
583
|
].sort((a, b) => a.id.localeCompare(b.id));
|
|
533
584
|
return { ...manifest, entries };
|
|
534
585
|
}
|
|
586
|
+
function isDerived(entry) {
|
|
587
|
+
return entry.derived === true || entry.parent != null && entry.id.includes("__");
|
|
588
|
+
}
|
|
589
|
+
function subNameOf(entry) {
|
|
590
|
+
if (!isDerived(entry)) return void 0;
|
|
591
|
+
const idx = entry.id.indexOf("__");
|
|
592
|
+
return idx === -1 ? void 0 : entry.id.slice(idx + 2);
|
|
593
|
+
}
|
|
535
594
|
function isEntryStale(entry) {
|
|
536
595
|
const recomputed = hashMaterial({
|
|
537
596
|
url: entry.url,
|
|
538
597
|
viewport: entry.viewport,
|
|
539
598
|
mask: entry.mask,
|
|
540
599
|
waitFor: entry.waitFor,
|
|
541
|
-
|
|
542
|
-
fullPage: entry.fullPage ?? DEFAULT_FULL_PAGE
|
|
600
|
+
harnesses: entry.harnesses ?? [],
|
|
601
|
+
fullPage: entry.fullPage ?? DEFAULT_FULL_PAGE,
|
|
602
|
+
subName: subNameOf(entry)
|
|
543
603
|
});
|
|
544
604
|
return recomputed !== entry.captureHash;
|
|
545
605
|
}
|
|
546
606
|
|
|
547
607
|
// src/captures.ts
|
|
608
|
+
function normalizeHarnessRefs(refs) {
|
|
609
|
+
if (!refs) return [];
|
|
610
|
+
return refs.map((ref) => typeof ref === "string" ? { name: ref } : ref);
|
|
611
|
+
}
|
|
612
|
+
async function resolveHarnesses(refs, cwd) {
|
|
613
|
+
return Promise.all(
|
|
614
|
+
refs.map(async (ref) => ({
|
|
615
|
+
harness: await loadHarness(resolveHarnessFile(ref.name, cwd)),
|
|
616
|
+
params: ref.params ?? {}
|
|
617
|
+
}))
|
|
618
|
+
);
|
|
619
|
+
}
|
|
548
620
|
function validateRoute(route, i) {
|
|
549
621
|
if (!route || typeof route !== "object")
|
|
550
622
|
return `route[${i}] must be an object`;
|
|
@@ -590,7 +662,8 @@ async function runCaptures(opts) {
|
|
|
590
662
|
(r, i) => semaphore.run(async () => {
|
|
591
663
|
const mode = r.mode ?? defaultMode;
|
|
592
664
|
try {
|
|
593
|
-
const
|
|
665
|
+
const refs = normalizeHarnessRefs(r.harnesses);
|
|
666
|
+
const harnesses = await resolveHarnesses(refs, cwd);
|
|
594
667
|
const shot = await captureScreenshot(
|
|
595
668
|
opts.baseUrl,
|
|
596
669
|
{
|
|
@@ -603,7 +676,7 @@ async function runCaptures(opts) {
|
|
|
603
676
|
mode
|
|
604
677
|
},
|
|
605
678
|
cwd,
|
|
606
|
-
|
|
679
|
+
harnesses
|
|
607
680
|
);
|
|
608
681
|
slot[i] = {
|
|
609
682
|
id: r.id,
|
|
@@ -611,22 +684,40 @@ async function runCaptures(opts) {
|
|
|
611
684
|
mode,
|
|
612
685
|
ok: true,
|
|
613
686
|
outputPath: shot.outputPath,
|
|
614
|
-
bytes: shot.bytes
|
|
687
|
+
bytes: shot.bytes,
|
|
688
|
+
subCaptures: shot.subCaptures?.map((s) => s.name)
|
|
615
689
|
};
|
|
616
690
|
if (mode === "baseline" && manifest) {
|
|
691
|
+
const shared = {
|
|
692
|
+
url: r.url,
|
|
693
|
+
viewport: r.viewport,
|
|
694
|
+
mask: r.mask,
|
|
695
|
+
waitFor: r.waitFor,
|
|
696
|
+
fullPage: r.fullPage,
|
|
697
|
+
harnesses: refs.length > 0 ? refs : void 0
|
|
698
|
+
};
|
|
699
|
+
manifest = {
|
|
700
|
+
...manifest,
|
|
701
|
+
entries: manifest.entries.filter((e) => e.parent !== r.id)
|
|
702
|
+
};
|
|
617
703
|
manifest = addOrReplaceEntry(
|
|
618
704
|
manifest,
|
|
619
|
-
makeEntry({
|
|
620
|
-
id: r.id,
|
|
621
|
-
url: r.url,
|
|
622
|
-
viewport: r.viewport,
|
|
623
|
-
mask: r.mask,
|
|
624
|
-
waitFor: r.waitFor,
|
|
625
|
-
fullPage: r.fullPage,
|
|
626
|
-
auth: r.auth ?? null
|
|
627
|
-
})
|
|
705
|
+
makeEntry({ id: r.id, ...shared })
|
|
628
706
|
);
|
|
629
707
|
manifestUpdates += 1;
|
|
708
|
+
for (const sub of shot.subCaptures ?? []) {
|
|
709
|
+
manifest = addOrReplaceEntry(
|
|
710
|
+
manifest,
|
|
711
|
+
makeEntry({
|
|
712
|
+
id: `${r.id}__${sub.name}`,
|
|
713
|
+
...shared,
|
|
714
|
+
parent: r.id,
|
|
715
|
+
derived: true,
|
|
716
|
+
subName: sub.name
|
|
717
|
+
})
|
|
718
|
+
);
|
|
719
|
+
manifestUpdates += 1;
|
|
720
|
+
}
|
|
630
721
|
}
|
|
631
722
|
} catch (err) {
|
|
632
723
|
slot[i] = {
|
|
@@ -878,6 +969,55 @@ async function discover(opts) {
|
|
|
878
969
|
}
|
|
879
970
|
return Array.from(merged.values()).sort((a, b) => a.url.localeCompare(b.url));
|
|
880
971
|
}
|
|
972
|
+
function envFiles(cwd) {
|
|
973
|
+
const root = paths(cwd).root;
|
|
974
|
+
return [
|
|
975
|
+
path.join(root, ".env.local"),
|
|
976
|
+
path.join(root, ".env"),
|
|
977
|
+
path.join(cwd, ".env.local"),
|
|
978
|
+
path.join(cwd, ".env")
|
|
979
|
+
];
|
|
980
|
+
}
|
|
981
|
+
function parse(content) {
|
|
982
|
+
if (typeof util.parseEnv === "function") {
|
|
983
|
+
return util.parseEnv(content);
|
|
984
|
+
}
|
|
985
|
+
const out = {};
|
|
986
|
+
for (const raw of content.split("\n")) {
|
|
987
|
+
const line = raw.trim();
|
|
988
|
+
if (!line || line.startsWith("#")) continue;
|
|
989
|
+
const eq = line.indexOf("=");
|
|
990
|
+
if (eq === -1) continue;
|
|
991
|
+
const key = line.slice(0, eq).trim();
|
|
992
|
+
let value = line.slice(eq + 1).trim();
|
|
993
|
+
if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
|
|
994
|
+
value = value.slice(1, -1);
|
|
995
|
+
}
|
|
996
|
+
if (key) out[key] = value;
|
|
997
|
+
}
|
|
998
|
+
return out;
|
|
999
|
+
}
|
|
1000
|
+
function loadEnvFiles(cwd = process.cwd()) {
|
|
1001
|
+
const loaded = [];
|
|
1002
|
+
for (const file of envFiles(cwd)) {
|
|
1003
|
+
if (!existsSync(file)) continue;
|
|
1004
|
+
let parsed;
|
|
1005
|
+
try {
|
|
1006
|
+
parsed = parse(readFileSync(file, "utf8"));
|
|
1007
|
+
} catch {
|
|
1008
|
+
continue;
|
|
1009
|
+
}
|
|
1010
|
+
let applied = false;
|
|
1011
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
1012
|
+
if (process.env[key] === void 0) {
|
|
1013
|
+
process.env[key] = value;
|
|
1014
|
+
applied = true;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
if (applied) loaded.push(file);
|
|
1018
|
+
}
|
|
1019
|
+
return loaded;
|
|
1020
|
+
}
|
|
881
1021
|
var ENTRIES = [
|
|
882
1022
|
"actual/",
|
|
883
1023
|
"judgments/",
|
|
@@ -885,10 +1025,12 @@ var ENTRIES = [
|
|
|
885
1025
|
"summary.md",
|
|
886
1026
|
"dev-server.log",
|
|
887
1027
|
"dev-server.pid",
|
|
1028
|
+
".env",
|
|
1029
|
+
".env.local",
|
|
888
1030
|
"*.tmp"
|
|
889
1031
|
];
|
|
890
1032
|
var STALE_ENTRIES = /* @__PURE__ */ new Set(["diffs/", "pending-judgments/", "report.json"]);
|
|
891
|
-
var HEADER = "# blazediff: generated artifacts (committed: config.json, manifest.json, baselines/)\n";
|
|
1033
|
+
var HEADER = "# blazediff: generated artifacts (committed: config.json, manifest.json, baselines/, harnesses/)\n";
|
|
892
1034
|
async function ensureGitignore(cwd) {
|
|
893
1035
|
const file = paths(cwd).gitignore;
|
|
894
1036
|
await mkdir(path.dirname(file), { recursive: true });
|
|
@@ -1856,13 +1998,8 @@ function failResult(entry, outcome, actualPath, baselinePath, verdict) {
|
|
|
1856
1998
|
}
|
|
1857
1999
|
|
|
1858
2000
|
// src/graph/nodes/capture.ts
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
if (legacyAuthHinted) return;
|
|
1862
|
-
legacyAuthHinted = true;
|
|
1863
|
-
process.stderr.write(
|
|
1864
|
-
'blazediff: manifest entries with auth: "required" are treated as persona "default" \u2014 please migrate.\n'
|
|
1865
|
-
);
|
|
2001
|
+
function asCaptured(entry, output) {
|
|
2002
|
+
return { entry, output };
|
|
1866
2003
|
}
|
|
1867
2004
|
function makeCaptureNode(semaphore) {
|
|
1868
2005
|
return async function captureNode(state) {
|
|
@@ -1871,27 +2008,39 @@ function makeCaptureNode(semaphore) {
|
|
|
1871
2008
|
if (!entry || !options) {
|
|
1872
2009
|
throw new Error("captureNode: entry or options missing");
|
|
1873
2010
|
}
|
|
2011
|
+
const children = state.children;
|
|
1874
2012
|
if (isEntryStale(entry)) {
|
|
1875
2013
|
return {
|
|
1876
|
-
|
|
2014
|
+
captured: [
|
|
2015
|
+
asCaptured(entry, {
|
|
2016
|
+
id: entry.id,
|
|
2017
|
+
skipResult: staleResult(entry)
|
|
2018
|
+
}),
|
|
2019
|
+
...children.map(
|
|
2020
|
+
(c) => asCaptured(c, { id: c.id, skipResult: staleResult(c) })
|
|
2021
|
+
)
|
|
2022
|
+
]
|
|
1877
2023
|
};
|
|
1878
2024
|
}
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
2025
|
+
const failAll = (message) => ({
|
|
2026
|
+
captured: [
|
|
2027
|
+
asCaptured(entry, {
|
|
2028
|
+
id: entry.id,
|
|
2029
|
+
skipResult: errorResult(entry, message)
|
|
2030
|
+
}),
|
|
2031
|
+
...children.map(
|
|
2032
|
+
(c) => asCaptured(c, { id: c.id, skipResult: errorResult(c, message) })
|
|
2033
|
+
)
|
|
2034
|
+
]
|
|
2035
|
+
});
|
|
2036
|
+
let harnesses;
|
|
2037
|
+
try {
|
|
2038
|
+
harnesses = await resolveHarnesses(
|
|
2039
|
+
normalizeHarnessRefs(entry.harnesses),
|
|
2040
|
+
options.cwd
|
|
2041
|
+
);
|
|
2042
|
+
} catch (err) {
|
|
2043
|
+
return failAll(`harness: ${err.message}`);
|
|
1895
2044
|
}
|
|
1896
2045
|
const baselinePath = path.join(options.baselinesDir, `${entry.id}.png`);
|
|
1897
2046
|
try {
|
|
@@ -1908,24 +2057,50 @@ function makeCaptureNode(semaphore) {
|
|
|
1908
2057
|
mode: "actual"
|
|
1909
2058
|
},
|
|
1910
2059
|
options.cwd,
|
|
1911
|
-
|
|
2060
|
+
harnesses
|
|
1912
2061
|
)
|
|
1913
2062
|
);
|
|
1914
|
-
|
|
1915
|
-
|
|
2063
|
+
const items = [
|
|
2064
|
+
asCaptured(entry, {
|
|
1916
2065
|
id: entry.id,
|
|
1917
2066
|
captureOutputPath: capture.outputPath,
|
|
1918
2067
|
baselinePath
|
|
2068
|
+
})
|
|
2069
|
+
];
|
|
2070
|
+
const subByName = new Map(
|
|
2071
|
+
(capture.subCaptures ?? []).map((s) => [s.name, s])
|
|
2072
|
+
);
|
|
2073
|
+
for (const child of children) {
|
|
2074
|
+
const name = subNameOf(child);
|
|
2075
|
+
const sub = name ? subByName.get(name) : void 0;
|
|
2076
|
+
const childBaseline = path.join(
|
|
2077
|
+
options.baselinesDir,
|
|
2078
|
+
`${child.id}.png`
|
|
2079
|
+
);
|
|
2080
|
+
if (sub) {
|
|
2081
|
+
items.push(
|
|
2082
|
+
asCaptured(child, {
|
|
2083
|
+
id: child.id,
|
|
2084
|
+
captureOutputPath: sub.outputPath,
|
|
2085
|
+
baselinePath: childBaseline
|
|
2086
|
+
})
|
|
2087
|
+
);
|
|
2088
|
+
} else {
|
|
2089
|
+
items.push(
|
|
2090
|
+
asCaptured(child, {
|
|
2091
|
+
id: child.id,
|
|
2092
|
+
skipResult: errorResult(
|
|
2093
|
+
child,
|
|
2094
|
+
`harness did not produce screenshot "${name ?? child.id}"`
|
|
2095
|
+
)
|
|
2096
|
+
})
|
|
2097
|
+
);
|
|
1919
2098
|
}
|
|
1920
|
-
}
|
|
2099
|
+
}
|
|
2100
|
+
return { captured: items };
|
|
1921
2101
|
} catch (err) {
|
|
1922
|
-
if (err instanceof
|
|
1923
|
-
return {
|
|
1924
|
-
captureOutput: {
|
|
1925
|
-
id: entry.id,
|
|
1926
|
-
skipResult: errorResult(entry, `auth: ${err.message}`)
|
|
1927
|
-
}
|
|
1928
|
-
};
|
|
2102
|
+
if (err instanceof HarnessError) {
|
|
2103
|
+
return failAll(`harness: ${err.message}`);
|
|
1929
2104
|
}
|
|
1930
2105
|
throw err;
|
|
1931
2106
|
}
|
|
@@ -2228,6 +2403,25 @@ var resultsChannel = Annotation({
|
|
|
2228
2403
|
reducer: (acc, next) => [...acc, ...next],
|
|
2229
2404
|
default: () => []
|
|
2230
2405
|
});
|
|
2406
|
+
var capturedChannel = Annotation({
|
|
2407
|
+
reducer: (acc, next) => [...acc, ...next],
|
|
2408
|
+
default: () => []
|
|
2409
|
+
});
|
|
2410
|
+
var CaptureState = Annotation.Root({
|
|
2411
|
+
entry: Annotation({
|
|
2412
|
+
reducer: (_, next) => next,
|
|
2413
|
+
default: () => void 0
|
|
2414
|
+
}),
|
|
2415
|
+
children: Annotation({
|
|
2416
|
+
reducer: (_, next) => next,
|
|
2417
|
+
default: () => []
|
|
2418
|
+
}),
|
|
2419
|
+
options: Annotation({
|
|
2420
|
+
reducer: (acc, next) => next ?? acc,
|
|
2421
|
+
default: () => void 0
|
|
2422
|
+
}),
|
|
2423
|
+
captured: capturedChannel
|
|
2424
|
+
});
|
|
2231
2425
|
var BranchState = Annotation.Root({
|
|
2232
2426
|
entry: Annotation({
|
|
2233
2427
|
reducer: (_, next) => next,
|
|
@@ -2256,6 +2450,7 @@ var GraphState = Annotation.Root({
|
|
|
2256
2450
|
reducer: (acc, next) => next ?? acc,
|
|
2257
2451
|
default: () => []
|
|
2258
2452
|
}),
|
|
2453
|
+
captured: capturedChannel,
|
|
2259
2454
|
results: resultsChannel,
|
|
2260
2455
|
manifest: Annotation({
|
|
2261
2456
|
reducer: (acc, next) => next ?? acc,
|
|
@@ -2276,15 +2471,36 @@ function cpuParallelism() {
|
|
|
2276
2471
|
function threadIdFor(cwd) {
|
|
2277
2472
|
return createHash("sha1").update(paths(cwd).manifest).digest("hex").slice(0, 16);
|
|
2278
2473
|
}
|
|
2279
|
-
function
|
|
2280
|
-
return new StateGraph(
|
|
2474
|
+
function buildCaptureGraph(captureSemaphore) {
|
|
2475
|
+
return new StateGraph(CaptureState).addNode("capture", makeCaptureNode(captureSemaphore)).addEdge(START, "capture").addEdge("capture", END).compile();
|
|
2476
|
+
}
|
|
2477
|
+
function buildBranchGraph(diffSemaphore) {
|
|
2478
|
+
return new StateGraph(BranchState).addNode("diff", makeDiffNode(diffSemaphore)).addNode("judge", judgeNode).addEdge(START, "diff").addEdge("diff", "judge").addEdge("judge", END).compile();
|
|
2479
|
+
}
|
|
2480
|
+
function dispatchNode() {
|
|
2481
|
+
return {};
|
|
2281
2482
|
}
|
|
2282
2483
|
function buildGraph(captureSemaphore, diffSemaphore, checkpointer) {
|
|
2283
|
-
const
|
|
2284
|
-
|
|
2484
|
+
const captureBranch = buildCaptureGraph(captureSemaphore);
|
|
2485
|
+
const branch = buildBranchGraph(diffSemaphore);
|
|
2486
|
+
return new StateGraph(GraphState).addNode("load", loadNode).addNode("capture", captureBranch).addNode("dispatch", dispatchNode).addNode("branch", branch).addEdge(START, "load").addConditionalEdges(
|
|
2285
2487
|
"load",
|
|
2286
|
-
(state) => state.entries.map(
|
|
2287
|
-
(entry) => new Send("
|
|
2488
|
+
(state) => state.entries.filter((entry) => !isDerived(entry)).map(
|
|
2489
|
+
(entry) => new Send("capture", {
|
|
2490
|
+
entry,
|
|
2491
|
+
children: state.entries.filter((e) => e.parent === entry.id),
|
|
2492
|
+
options: state.options
|
|
2493
|
+
})
|
|
2494
|
+
),
|
|
2495
|
+
["capture"]
|
|
2496
|
+
).addEdge("capture", "dispatch").addConditionalEdges(
|
|
2497
|
+
"dispatch",
|
|
2498
|
+
(state) => state.captured.map(
|
|
2499
|
+
(c) => new Send("branch", {
|
|
2500
|
+
entry: c.entry,
|
|
2501
|
+
captureOutput: c.output,
|
|
2502
|
+
options: state.options
|
|
2503
|
+
})
|
|
2288
2504
|
),
|
|
2289
2505
|
["branch"]
|
|
2290
2506
|
).addEdge("branch", END).compile({ checkpointer });
|
|
@@ -2367,7 +2583,6 @@ async function runGraph(opts) {
|
|
|
2367
2583
|
if (!opts.resume) {
|
|
2368
2584
|
await checkpointer.deleteThread(threadId);
|
|
2369
2585
|
}
|
|
2370
|
-
const config = await loadConfig(cwd);
|
|
2371
2586
|
const input = opts.resume ? new Command({ resume: opts.resume }) : {
|
|
2372
2587
|
options: {
|
|
2373
2588
|
baseUrl: opts.baseUrl ?? "",
|
|
@@ -2376,8 +2591,7 @@ async function runGraph(opts) {
|
|
|
2376
2591
|
concurrency,
|
|
2377
2592
|
emitDiffPng: opts.emitDiffPng ?? true,
|
|
2378
2593
|
judge: opts.judge ?? "none",
|
|
2379
|
-
baselinesDir
|
|
2380
|
-
auth: config?.auth
|
|
2594
|
+
baselinesDir
|
|
2381
2595
|
}
|
|
2382
2596
|
};
|
|
2383
2597
|
let collect;
|
|
@@ -2409,4 +2623,4 @@ function runCheck(opts) {
|
|
|
2409
2623
|
return runGraph(opts);
|
|
2410
2624
|
}
|
|
2411
2625
|
|
|
2412
|
-
export { STABILITY_HOOKS_VERSION, applyJudgments, captureScreenshot, configHash, diffEntry, discover, installBrowsers, loadConfig, loadManifest, paths, resolveBaseUrl, resolveJudge, resumeGraph, runCaptures, runCheck, runGraph, saveConfig, saveManifest, threadIdFor };
|
|
2626
|
+
export { HarnessError, STABILITY_HOOKS_VERSION, applyJudgments, assertLeftLoginPage, captureScreenshot, configHash, diffEntry, discover, installBrowsers, loadConfig, loadEnvFiles, loadHarness, loadManifest, paths, resolveBaseUrl, resolveHarnessFile, resolveJudge, resumeGraph, runCaptures, runCheck, runGraph, saveConfig, saveManifest, threadIdFor };
|