@blazediff/agent 0.2.1 → 0.4.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/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { Page } from 'playwright';
2
1
  import { BoundingBox, InterpretResult } from '@blazediff/core-native';
2
+ import { Page, Browser, BrowserContext } from 'playwright';
3
3
 
4
4
  type VerdictLabel = "regression-likely" | "intentional-likely" | "noise-likely" | "ambiguous";
5
5
  type VerdictAction = "investigate" | "rewrite-if-intended" | "ignore-or-rewrite";
@@ -10,7 +10,84 @@ interface Verdict {
10
10
  action: VerdictAction;
11
11
  }
12
12
 
13
+ interface JudgeInput {
14
+ entry: ManifestEntry;
15
+ baselinePath: string;
16
+ actualPath: string;
17
+ diffPath?: string;
18
+ regions?: RegionSummary[];
19
+ diffPercentage?: number;
20
+ severity?: string;
21
+ heuristicVerdict: Verdict;
22
+ /**
23
+ * Fired the moment the judge starts real work on this test, *after* any
24
+ * internal queueing (e.g. waiting on a single-instance ML model). The CLI
25
+ * uses this to print the "judging X" line at the correct pipeline moment
26
+ * rather than when the call was merely scheduled.
27
+ */
28
+ onJudgingStart?: () => void;
29
+ }
30
+ type JudgmentRequestRegion = RegionSummary;
31
+ type JudgeFailureReason = "model-load" | "read" | "internal";
32
+ type JudgeOutput = {
33
+ kind: "judged";
34
+ verdict: Verdict;
35
+ rationale?: string;
36
+ confidence?: number;
37
+ } | {
38
+ kind: "deferred";
39
+ requestPath: string;
40
+ } | {
41
+ /**
42
+ * The judge could not produce a verdict (model load failed, crop read
43
+ * threw, …). `fallback` is the deterministic heuristic verdict the
44
+ * caller should adopt; `error` is the originating error for diagnostics.
45
+ */
46
+ kind: "failed";
47
+ reason: JudgeFailureReason;
48
+ error: Error;
49
+ fallback: Verdict;
50
+ };
51
+ interface Judge {
52
+ readonly name: string;
53
+ judge(input: JudgeInput, cwd: string): Promise<JudgeOutput>;
54
+ /**
55
+ * Optional one-time load step (e.g. streaming model weights). Run once after
56
+ * capture and before judging so the cost surfaces as its own phase instead of
57
+ * stalling the first test silently.
58
+ */
59
+ warmup?(): Promise<void>;
60
+ }
61
+ type JudgeBackend = "none" | "host" | "local";
62
+
13
63
  declare const STABILITY_HOOKS_VERSION = 1;
64
+ /**
65
+ * Context handed to a harness. `params` is intentionally generic — a harness
66
+ * author types it to whatever the harness needs (e.g. `{ persona: string }`
67
+ * for a login harness). `screenshot(name)` emits a named sub-screenshot that
68
+ * becomes its own baseline entry (`<entryId>__<name>`).
69
+ */
70
+ interface HarnessContext<P = Record<string, unknown>> {
71
+ page: Page;
72
+ browser: Browser;
73
+ context: BrowserContext;
74
+ params: P;
75
+ screenshot(name: string): Promise<void>;
76
+ }
77
+ /**
78
+ * A pluggable script run before/around a screenshot. `setup` harnesses run
79
+ * before navigation (establish session, e.g. login); `interact` harnesses run
80
+ * after the base screenshot and may drive the page + emit named screenshots.
81
+ */
82
+ interface Harness<P = Record<string, unknown>> {
83
+ phase?: "setup" | "interact";
84
+ run(ctx: HarnessContext<P>): Promise<void>;
85
+ }
86
+ /** Per-entry reference to a harness file under `.blazediff/harnesses/<name>.js`. */
87
+ interface HarnessRef {
88
+ name: string;
89
+ params?: Record<string, unknown>;
90
+ }
14
91
  interface RegionSummary {
15
92
  bbox: BoundingBox;
16
93
  pixelCount: number;
@@ -31,10 +108,13 @@ interface ManifestEntry {
31
108
  id: string;
32
109
  url: string;
33
110
  viewport: Viewport;
34
- auth: null | string;
111
+ harnesses?: HarnessRef[];
35
112
  waitFor: WaitFor[];
36
113
  mask: string[];
37
114
  fullPage?: boolean;
115
+ /** Set on sub-entries produced by a harness `screenshot(name)` call. */
116
+ parent?: string;
117
+ derived?: boolean;
38
118
  baselinePath: string;
39
119
  captureHash: string;
40
120
  createdBy: "agent" | "human";
@@ -52,16 +132,13 @@ interface DevServerConfig {
52
132
  cwd?: string;
53
133
  readyTimeoutMs?: number;
54
134
  }
55
- interface AgentAuthConfig {
56
- harness: string;
57
- loginUrl: string;
58
- }
59
135
  interface AgentConfig {
60
136
  devServer: DevServerConfig | null;
61
137
  framework?: string;
62
138
  packageManager?: "npm" | "pnpm" | "yarn" | "bun";
63
139
  baseUrl?: string;
64
- auth?: AgentAuthConfig;
140
+ /** default judge backend for `check`, set by `onboard --stack` */
141
+ judge?: JudgeBackend;
65
142
  }
66
143
  interface CaptureOptions {
67
144
  id: string;
@@ -93,23 +170,36 @@ interface CheckResult {
93
170
  diffPercentage?: number;
94
171
  severity?: string;
95
172
  regions?: RegionSummary[];
173
+ /** Natural pixel dimensions of the compared image (for region overlay scaling). */
174
+ width?: number;
175
+ height?: number;
96
176
  verdict?: Verdict;
97
177
  diffPath?: string;
98
178
  actualPath?: string;
99
179
  baselinePath?: string;
100
180
  message?: string;
181
+ /** Human decision recorded by `review` (re-baselined or confirmed regression). */
182
+ review?: "approved" | "rejected";
101
183
  }
102
184
 
185
+ interface SubCapture {
186
+ name: string;
187
+ outputPath: string;
188
+ bytes: number;
189
+ }
103
190
  interface CaptureResult {
104
191
  id: string;
105
192
  outputPath: string;
106
193
  mode: "baseline" | "actual";
107
194
  bytes: number;
195
+ subCaptures?: SubCapture[];
108
196
  }
109
- interface CaptureAuth {
110
- hook: (page: Page) => Promise<void>;
197
+ /** A harness paired with the params to run it with, resolved by the caller. */
198
+ interface ResolvedHarness {
199
+ harness: Harness;
200
+ params: Record<string, unknown>;
111
201
  }
112
- declare function captureScreenshot(baseUrl: string, opts: CaptureOptions, cwd?: string, auth?: CaptureAuth): Promise<CaptureResult>;
202
+ declare function captureScreenshot(baseUrl: string, opts: CaptureOptions, cwd?: string, harnesses?: ResolvedHarness[]): Promise<CaptureResult>;
113
203
 
114
204
  interface BrowsersInstallOptions {
115
205
  check?: boolean;
@@ -128,7 +218,7 @@ interface CaptureRouteInput {
128
218
  viewport?: Viewport;
129
219
  waitFor?: WaitFor[];
130
220
  fullPage?: boolean;
131
- auth?: null | string;
221
+ harnesses?: (string | HarnessRef)[];
132
222
  mode?: "baseline" | "actual";
133
223
  }
134
224
  interface RunCapturesOptions {
@@ -146,6 +236,7 @@ interface CaptureRouteResult {
146
236
  ok: boolean;
147
237
  outputPath?: string;
148
238
  bytes?: number;
239
+ subCaptures?: string[];
149
240
  error?: string;
150
241
  }
151
242
  interface RunCapturesReport {
@@ -188,31 +279,14 @@ interface DiscoverOptions {
188
279
  }
189
280
  declare function discover(opts: DiscoverOptions): Promise<DiscoveredRoute[]>;
190
281
 
191
- interface JudgeInput {
192
- entry: ManifestEntry;
193
- baselinePath: string;
194
- actualPath: string;
195
- diffPath?: string;
196
- regions?: RegionSummary[];
197
- diffPercentage?: number;
198
- severity?: string;
199
- heuristicVerdict: Verdict;
200
- }
201
- type JudgmentRequestRegion = RegionSummary;
202
- type JudgeOutput = {
203
- kind: "judged";
204
- verdict: Verdict;
205
- rationale?: string;
206
- confidence?: number;
207
- } | {
208
- kind: "deferred";
209
- requestPath: string;
210
- };
211
- interface Judge {
212
- readonly name: string;
213
- judge(input: JudgeInput, cwd: string): Promise<JudgeOutput>;
214
- }
215
- type JudgeBackend = "none" | "host";
282
+ /**
283
+ * Load env files (`.blazediff/.env[.local]` then `.env[.local]`) into
284
+ * `process.env` so harnesses can read secrets (e.g. `BLAZEDIFF_AUTH_*`) without
285
+ * the user exporting them by hand. Real environment variables always win, and
286
+ * `.blazediff/` files take precedence over the project-root ones. Returns the
287
+ * files actually applied.
288
+ */
289
+ declare function loadEnvFiles(cwd?: string): string[];
216
290
 
217
291
  interface ApplyJudgmentsResult {
218
292
  report: CheckReport;
@@ -225,7 +299,7 @@ interface ApplyJudgmentsOptions {
225
299
  onEvent?: (event: RunEvent) => void;
226
300
  junitPath?: string;
227
301
  }
228
- declare function applyJudgments(opts?: ApplyJudgmentsOptions | string): Promise<ApplyJudgmentsResult>;
302
+ declare function applyJudgments(opts?: ApplyJudgmentsOptions): Promise<ApplyJudgmentsResult>;
229
303
 
230
304
  interface JudgmentRequest {
231
305
  id: string;
@@ -267,8 +341,15 @@ interface JudgmentInterrupt {
267
341
 
268
342
  type ResumeMap = Record<string, Verdict>;
269
343
  type RunEvent = {
344
+ type: "captured";
345
+ entryId: string;
346
+ } | {
270
347
  type: "result";
271
348
  result: CheckResult;
349
+ } | {
350
+ type: "judging";
351
+ entryId: string;
352
+ url: string;
272
353
  } | {
273
354
  type: "interrupt";
274
355
  interrupt: JudgmentInterrupt;
@@ -301,6 +382,28 @@ interface ResumeOptions {
301
382
  declare function resumeGraph(opts: ResumeOptions): Promise<CheckReport>;
302
383
  declare function runCheck(opts: CheckOptions): Promise<CheckReport>;
303
384
 
385
+ declare class HarnessError extends Error {
386
+ readonly cause?: unknown | undefined;
387
+ constructor(message: string, cause?: unknown | undefined);
388
+ }
389
+ /**
390
+ * Resolve a harness reference to an absolute file path. A bare `name` resolves
391
+ * to `.blazediff/harnesses/<name>.js` (preferring `.mjs` if present); an
392
+ * explicit relative/absolute path is honored as-is.
393
+ */
394
+ declare function resolveHarnessFile(name: string, cwd: string): string;
395
+ /**
396
+ * Load a harness module. Harnesses are ESM `.js`/`.mjs` files that
397
+ * default-export a {@link Harness}. The module is cached by absolute path.
398
+ */
399
+ declare function loadHarness(harnessFile: string): Promise<Harness>;
400
+ /**
401
+ * Safety check for login harnesses: confirms the page left the login URL and no
402
+ * visible password input remains. Exported so generated auth harnesses can call
403
+ * it without the framework special-casing auth.
404
+ */
405
+ declare function assertLeftLoginPage(page: Page, loginUrl: string): Promise<void>;
406
+
304
407
  declare function loadManifest(cwd?: string): Promise<Manifest | null>;
305
408
  declare function saveManifest(manifest: Manifest, cwd?: string): Promise<void>;
306
409
 
@@ -308,14 +411,81 @@ declare const paths: (cwd?: string) => {
308
411
  root: string;
309
412
  config: string;
310
413
  manifest: string;
414
+ harnesses: string;
311
415
  baselines: string;
312
416
  actual: string;
313
417
  judgments: string;
314
418
  checkpoints: string;
315
- summary: string;
419
+ report: string;
316
420
  gitignore: string;
317
421
  serverLog: string;
318
422
  serverPid: string;
319
423
  };
320
424
 
321
- export { type AgentAuthConfig, type AgentConfig, type ApplyJudgmentsResult, type BrowsersInstallOptions, type BrowsersInstallResult, type CaptureOptions, type CaptureRouteInput, type CaptureRouteResult, type CheckOptions, type CheckReport, type CheckResult, type DevServerConfig, type DiffOptions, type DiffOutcome, type DiscoveredRoute, type Judge, type JudgeBackend, type JudgeInput, type JudgeOutput, type JudgmentRequest, type JudgmentRequestRegion, type Manifest, type ManifestEntry, type RegionSummary, type ResumeMap, type ResumeOptions, type RunCapturesOptions, type RunCapturesReport, type RunEvent, type RunOptions, STABILITY_HOOKS_VERSION, type Verdict, type VerdictAction, type VerdictLabel, type Viewport, type WaitFor, type WaitForSelector, applyJudgments, captureScreenshot, configHash, diffEntry, discover, installBrowsers, loadConfig, loadManifest, paths, resolveBaseUrl, resolveJudge, resumeGraph, runCaptures, runCheck, runGraph, saveConfig, saveManifest, threadIdFor };
425
+ /** Persist the machine-readable check report consumed by `review`. */
426
+ declare function writeReport(report: CheckReport, cwd?: string): Promise<string>;
427
+ /** Read `.blazediff/report.json`, or null when absent/unparseable. */
428
+ declare function readReport(cwd?: string): Promise<CheckReport | null>;
429
+
430
+ type ReviewStatus = "unreviewed" | "approved" | "rejected";
431
+ /** Design's CSS tint classes; mapped from the agent's VerdictLabel. */
432
+ type ReviewClass = "intentional-likely" | "regression" | "layout-shift";
433
+ interface ReviewRegion {
434
+ id: number;
435
+ bbox: {
436
+ x: number;
437
+ y: number;
438
+ w: number;
439
+ h: number;
440
+ };
441
+ kind: string;
442
+ pixels: number;
443
+ change: number;
444
+ }
445
+ interface ReviewEntry {
446
+ id: string;
447
+ name: string;
448
+ url: string;
449
+ width: number;
450
+ height: number;
451
+ diff: number;
452
+ classification: ReviewClass;
453
+ severity: string;
454
+ action: string;
455
+ summary: string;
456
+ changes: string[];
457
+ regions: ReviewRegion[];
458
+ status: ReviewStatus;
459
+ reviewedAt?: string;
460
+ reviewedBy?: string;
461
+ }
462
+ interface ReviewRunMeta {
463
+ name: string;
464
+ baseline: string;
465
+ candidate: string;
466
+ startedAt: string;
467
+ threshold: number;
468
+ total: number;
469
+ failed: number;
470
+ passed: number;
471
+ }
472
+ interface ReviewPayload {
473
+ meta: ReviewRunMeta;
474
+ entries: ReviewEntry[];
475
+ }
476
+
477
+ /**
478
+ * Approve = accept the reviewed candidate: promote the existing
479
+ * `actual/<id>.png` to the baseline (no re-screenshot) and drop the now-stale
480
+ * actual/diff/judgment artifacts. The report entry flips to a passing,
481
+ * approved state so it stays green in CI and lingers in the UI's "Done" tab.
482
+ */
483
+ declare function approveEntry(id: string, cwd?: string): Promise<ReviewEntry | null>;
484
+ /**
485
+ * Reject = confirm a regression. Write a `regression-likely` verdict.json so
486
+ * `check --apply-judgments` keeps the entry failing, and record the decision
487
+ * in the report so the UI reflects it on reload.
488
+ */
489
+ declare function rejectEntry(id: string, cwd?: string): Promise<ReviewEntry | null>;
490
+
491
+ export { type AgentConfig, type ApplyJudgmentsResult, type BrowsersInstallOptions, type BrowsersInstallResult, type CaptureOptions, type CaptureRouteInput, type CaptureRouteResult, type CheckOptions, type CheckReport, type CheckResult, type DevServerConfig, type DiffOptions, type DiffOutcome, type DiscoveredRoute, type Harness, type HarnessContext, HarnessError, type HarnessRef, type Judge, type JudgeBackend, type JudgeInput, type JudgeOutput, type JudgmentRequest, type JudgmentRequestRegion, type Manifest, type ManifestEntry, type RegionSummary, type ResumeMap, type ResumeOptions, type ReviewEntry, type ReviewPayload, type ReviewRegion, type ReviewRunMeta, type ReviewStatus, type RunCapturesOptions, type RunCapturesReport, type RunEvent, type RunOptions, STABILITY_HOOKS_VERSION, type Verdict, type VerdictAction, type VerdictLabel, type Viewport, type WaitFor, type WaitForSelector, applyJudgments, approveEntry, assertLeftLoginPage, captureScreenshot, configHash, diffEntry, discover, installBrowsers, loadConfig, loadEnvFiles, loadHarness, loadManifest, paths, readReport, rejectEntry, resolveBaseUrl, resolveHarnessFile, resolveJudge, resumeGraph, runCaptures, runCheck, runGraph, saveConfig, saveManifest, threadIdFor, writeReport };