@cappa/core 0.16.0 → 0.18.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.cjs CHANGED
@@ -213,6 +213,61 @@ var PNG = class PNG {
213
213
  const sourcePath = typeof source === "string" ? source : void 0;
214
214
  return new PNG(info.width, info.height, data, sourcePath, metadata);
215
215
  }
216
+ /**
217
+ * A copy of this image on a `width` × `height` canvas, anchored top-left,
218
+ * with the added area filled with `fill` (RGBA, transparent by default).
219
+ *
220
+ * This is how two screenshots of different sizes are given a shared
221
+ * coordinate space: a component that grew keeps its pixels where they were.
222
+ */
223
+ padTo(width, height, fill = [
224
+ 0,
225
+ 0,
226
+ 0,
227
+ 0
228
+ ]) {
229
+ if (width < this.width_ || height < this.height_) throw new Error(`Cannot pad a ${this.width_}x${this.height_} image to a smaller ${width}x${height} canvas`);
230
+ if (width === this.width_ && height === this.height_) return new PNG(width, height, Buffer.from(this.data_));
231
+ const data = Buffer.alloc(width * height * 4);
232
+ const pixel = Buffer.from(fill);
233
+ for (let offset = 0; offset < data.length; offset += 4) pixel.copy(data, offset);
234
+ const rowBytes = this.width_ * 4;
235
+ for (let y = 0; y < this.height_; y++) this.data_.copy(data, y * width * 4, y * rowBytes, (y + 1) * rowBytes);
236
+ return new PNG(width, height, data);
237
+ }
238
+ /**
239
+ * The most common colour along the right column and the bottom row — the
240
+ * edges a top-left anchored {@link padTo} extends. For a screenshot that is
241
+ * its background, which is what the area it never covered should read as.
242
+ */
243
+ edgeColor() {
244
+ const counts = /* @__PURE__ */ new Map();
245
+ let best = 0;
246
+ let bestCount = 0;
247
+ const count = (x, y) => {
248
+ const value = this.data_.readUInt32BE((y * this.width_ + x) * 4);
249
+ const next = (counts.get(value) ?? 0) + 1;
250
+ counts.set(value, next);
251
+ if (next > bestCount) {
252
+ best = value;
253
+ bestCount = next;
254
+ }
255
+ };
256
+ if (this.width_ === 0 || this.height_ === 0) return [
257
+ 0,
258
+ 0,
259
+ 0,
260
+ 0
261
+ ];
262
+ for (let y = 0; y < this.height_; y++) count(this.width_ - 1, y);
263
+ for (let x = 0; x < this.width_ - 1; x++) count(x, this.height_ - 1);
264
+ return [
265
+ best >>> 24 & 255,
266
+ best >>> 16 & 255,
267
+ best >>> 8 & 255,
268
+ best & 255
269
+ ];
270
+ }
216
271
  get width() {
217
272
  return this.width_;
218
273
  }
@@ -270,21 +325,28 @@ const compare = (image1, image2, diff, width, height, options) => {
270
325
  * @returns Comparison result
271
326
  */
272
327
  async function compareImagesGMSD(image1, image2, withDiff = false, options = {}) {
273
- const [png1, png2] = await Promise.all([loadPNG(image1), loadPNG(image2)]);
274
- const { width, height } = png1;
275
- if (width !== png2.width || height !== png2.height) return {
276
- gmsd: Infinity,
277
- passed: false
278
- };
328
+ const [loaded1, loaded2] = await Promise.all([loadPNG(image1), loadPNG(image2)]);
329
+ const differentSizes = loaded1.width !== loaded2.width || loaded1.height !== loaded2.height;
330
+ const width = Math.max(loaded1.width, loaded2.width);
331
+ const height = Math.max(loaded1.height, loaded2.height);
332
+ const png1 = differentSizes ? loaded1.padTo(width, height, loaded1.edgeColor()) : loaded1;
333
+ const png2 = differentSizes ? loaded2.padTo(width, height, loaded2.edgeColor()) : loaded2;
279
334
  const diff = withDiff ? PNG.create(width, height) : void 0;
280
335
  try {
281
336
  const gmsd = compare(png1.data, png2.data, diff?.data, width, height, {
282
337
  downsample: options.downsample ?? 0,
283
338
  c: options.c ?? 170
284
339
  });
340
+ const diffBuffer = diff ? await annotateDiffImage(diff, options).toBuffer() : void 0;
341
+ if (differentSizes) return {
342
+ gmsd,
343
+ diffBuffer,
344
+ passed: false,
345
+ differentSizes: true
346
+ };
285
347
  return {
286
348
  gmsd,
287
- diffBuffer: diff ? await annotateDiffImage(diff, options).toBuffer() : void 0,
349
+ diffBuffer,
288
350
  passed: gmsd <= (options.threshold ?? .1)
289
351
  };
290
352
  } catch (error) {
@@ -321,6 +383,46 @@ async function imagesMatchGMSD(image1, image2, options = {}) {
321
383
  return (await compareImagesGMSD(image1, image2, false, options)).passed;
322
384
  }
323
385
  //#endregion
386
+ //#region src/features/png/canvas.ts
387
+ /**
388
+ * Write copies of two images, padded to a shared canvas, into `dir`.
389
+ *
390
+ * Both are anchored top-left and padded up to the larger width and the larger
391
+ * height, so every pixel keeps its original coordinates and a region found on
392
+ * the canvas points at the same spot in either image.
393
+ *
394
+ * Each image is padded with its own background — the most common colour on
395
+ * the edges being extended — rather than with transparency. Area an image
396
+ * never covered then reads as empty rather than as content: a list that grew
397
+ * by one item diffs as that item (an addition), not as a whole strip that
398
+ * changed, and a component that only gained whitespace diffs as nothing at
399
+ * all, leaving the size change itself as the finding.
400
+ */
401
+ async function writeCommonCanvas(expected, actual, dir) {
402
+ const [expectedPng, actualPng] = await Promise.all([PNG.load(expected), PNG.load(actual)]);
403
+ const width = Math.max(expectedPng.width, actualPng.width);
404
+ const height = Math.max(expectedPng.height, actualPng.height);
405
+ const expectedPath = node_path.default.join(dir, "expected-canvas.png");
406
+ const actualPath = node_path.default.join(dir, "actual-canvas.png");
407
+ await Promise.all([expectedPng.padTo(width, height, expectedPng.edgeColor()).save(expectedPath), actualPng.padTo(width, height, actualPng.edgeColor()).save(actualPath)]);
408
+ return {
409
+ expectedPath,
410
+ actualPath,
411
+ width,
412
+ height,
413
+ dimensions: {
414
+ expected: {
415
+ width: expectedPng.width,
416
+ height: expectedPng.height
417
+ },
418
+ actual: {
419
+ width: actualPng.width,
420
+ height: actualPng.height
421
+ }
422
+ }
423
+ };
424
+ }
425
+ //#endregion
324
426
  //#region src/compare/pixel.ts
325
427
  async function ensureTempRoot(state) {
326
428
  if (!state.tempRoot) state.tempRoot = await node_fs_promises.default.mkdtemp(node_path.default.join(node_os.default.tmpdir(), "cappa-pxl-"));
@@ -384,13 +486,7 @@ async function compareImages(image1, image2, withDiff = false, options = {}) {
384
486
  }
385
487
  if (nativeResult.match === false) {
386
488
  if (nativeResult.reason === "file-not-exists") throw new Error(`File not found: ${nativeResult.file}`);
387
- if (nativeResult.reason === "layout-diff") return {
388
- numDiffPixels: 0,
389
- totalPixels: 0,
390
- percentDifference: 0,
391
- passed: false,
392
- differentSizes: true
393
- };
489
+ if (nativeResult.reason === "layout-diff") return await compareOnCommonCanvas(p1, p2, withDiff, options, state);
394
490
  }
395
491
  const { width, height } = await readIhdrDimensions(p1);
396
492
  const totalPixels = width * height;
@@ -402,23 +498,10 @@ async function compareImages(image1, image2, withDiff = false, options = {}) {
402
498
  passed: isPassed(0, 0, options),
403
499
  differentSizes: false
404
500
  };
405
- let diffBuffer;
406
- if (withDiff && diffPath) {
407
- const rawDiff = await node_fs_promises.default.readFile(diffPath);
408
- const diffMetadata = { "cappa.diff.algorithm": "pixel" };
409
- for (const [key, value] of Object.entries(options)) if (value !== void 0) diffMetadata[`cappa.diff.${key}`] = String(value);
410
- diffBuffer = injectTextMetadata(rawDiff, diffMetadata);
411
- }
501
+ const diffBuffer = withDiff && diffPath ? await readDiffImage(diffPath, options) : void 0;
412
502
  const numDiffPixels = nativeResult.diffCount;
413
503
  const percentDifference = nativeResult.diffPercentage;
414
- let interpretation;
415
- if (wantInterpret && !nativeResult.match) try {
416
- interpretation = await (0, _blazediff_interpret_native.interpret)(p1, p2, void 0, {
417
- source: "pixel",
418
- threshold: options.threshold,
419
- antialiasing: options.includeAA ?? false
420
- });
421
- } catch {}
504
+ const interpretation = wantInterpret ? await interpretBestEffort(p1, p2, options) : void 0;
422
505
  return {
423
506
  numDiffPixels,
424
507
  totalPixels,
@@ -435,6 +518,107 @@ async function compareImages(image1, image2, withDiff = false, options = {}) {
435
518
  }).catch(() => {});
436
519
  }
437
520
  }
521
+ async function readDiffImage(diffPath, options) {
522
+ let rawDiff;
523
+ try {
524
+ rawDiff = await node_fs_promises.default.readFile(diffPath);
525
+ } catch {
526
+ return;
527
+ }
528
+ return injectTextMetadata(rawDiff, diffMetadataFor(options));
529
+ }
530
+ function diffMetadataFor(options) {
531
+ const diffMetadata = { "cappa.diff.algorithm": "pixel" };
532
+ for (const [key, value] of Object.entries(options)) if (value !== void 0) diffMetadata[`cappa.diff.${key}`] = String(value);
533
+ return diffMetadata;
534
+ }
535
+ /**
536
+ * The diff for a size change whose padded images match: only background was
537
+ * added or removed. blazediff writes nothing for a match, so this draws one in
538
+ * the same spirit — the image faded out, with the area only one of the two
539
+ * images covers tinted — rather than leave the reviewer a placeholder.
540
+ */
541
+ async function renderSizeOnlyDiff(paddedActualPath, dimensions) {
542
+ const image = await PNG.load(paddedActualPath);
543
+ const sharedWidth = Math.min(dimensions.expected.width, dimensions.actual.width);
544
+ const sharedHeight = Math.min(dimensions.expected.height, dimensions.actual.height);
545
+ const data = image.data;
546
+ for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
547
+ const i = (y * image.width + x) * 4;
548
+ const faded = 255 + (.299 * (data[i] ?? 0) + .587 * (data[i + 1] ?? 0) + .114 * (data[i + 2] ?? 0) - 255) * .1 * (data[i + 3] ?? 0) / 255;
549
+ const outside = x >= sharedWidth || y >= sharedHeight;
550
+ data[i] = outside ? 255 : faded;
551
+ data[i + 1] = outside ? faded * .75 : faded;
552
+ data[i + 2] = outside ? faded * .75 : faded;
553
+ data[i + 3] = 255;
554
+ }
555
+ return image;
556
+ }
557
+ async function interpretBestEffort(expectedPath, actualPath, options) {
558
+ try {
559
+ return await (0, _blazediff_interpret_native.interpret)(expectedPath, actualPath, void 0, {
560
+ source: "pixel",
561
+ threshold: options.threshold,
562
+ antialiasing: options.includeAA ?? false
563
+ });
564
+ } catch {
565
+ return;
566
+ }
567
+ }
568
+ /**
569
+ * Compare two images of different sizes on a shared canvas.
570
+ *
571
+ * blazediff refuses a size mismatch outright (`layout-diff`), which used to
572
+ * leave a changed screenshot with no pixel count, no diff image and no
573
+ * regions — exactly the screenshots where something grew or shrank and a
574
+ * reviewer most needs to see where. Padding both to the larger width and
575
+ * height keeps every pixel at its original coordinates, so the diff, the
576
+ * interpretation's region boxes and the review UI all line up with the
577
+ * top-left of either image.
578
+ */
579
+ async function compareOnCommonCanvas(expectedPath, actualPath, withDiff, options, state) {
580
+ const root = await ensureTempRoot(state);
581
+ const canvas = await writeCommonCanvas(expectedPath, actualPath, root);
582
+ const { dimensions, width, height } = canvas;
583
+ const paddedExpected = canvas.expectedPath;
584
+ const paddedActual = canvas.actualPath;
585
+ const diffPath = withDiff ? node_path.default.join(root, "diff-canvas.png") : void 0;
586
+ let nativeResult;
587
+ try {
588
+ nativeResult = await (0, _blazediff_core_native.compare)(paddedExpected, paddedActual, diffPath, toNativeOptions(options));
589
+ } catch (error) {
590
+ const message = error.message || "Unknown error";
591
+ (0, _cappa_logger.getLogger)().error(message);
592
+ return {
593
+ numDiffPixels: 0,
594
+ totalPixels: 0,
595
+ percentDifference: 0,
596
+ passed: false,
597
+ differentSizes: true,
598
+ dimensions,
599
+ error: message
600
+ };
601
+ }
602
+ const matched = nativeResult.match;
603
+ const numDiffPixels = "diffCount" in nativeResult ? nativeResult.diffCount : 0;
604
+ const percentDifference = "diffPercentage" in nativeResult ? nativeResult.diffPercentage : 0;
605
+ let diffBuffer;
606
+ if (withDiff && diffPath) {
607
+ if (matched) diffBuffer = injectTextMetadata(await (await renderSizeOnlyDiff(canvas.actualPath, dimensions)).toBuffer(), diffMetadataFor(options));
608
+ else diffBuffer = await readDiffImage(diffPath, options);
609
+ }
610
+ const interpretation = !matched && options.interpret && withDiff ? await interpretBestEffort(paddedExpected, paddedActual, options) : void 0;
611
+ return {
612
+ numDiffPixels,
613
+ totalPixels: width * height,
614
+ percentDifference,
615
+ diffBuffer,
616
+ passed: false,
617
+ differentSizes: true,
618
+ dimensions,
619
+ interpretation
620
+ };
621
+ }
438
622
  /**
439
623
  * Quick comparison function that returns only pass/fail
440
624
  * @param image1 - Baseline (expected) image (file path or Buffer)
@@ -525,6 +709,247 @@ const attachConsoleLogging = (page, setting, logger) => {
525
709
  });
526
710
  };
527
711
  //#endregion
712
+ //#region src/crop.ts
713
+ const DEFAULT_CROP_PADDING = 8;
714
+ /** The crop to apply, or `undefined` for none. */
715
+ const resolveCrop = (crop) => {
716
+ if (!crop) return void 0;
717
+ const options = crop === true ? {} : crop;
718
+ return {
719
+ root: options.root ?? "body",
720
+ padding: Math.max(0, options.padding ?? 8)
721
+ };
722
+ };
723
+ /**
724
+ * Runs in the page: the union of the boxes of everything `root` paints, in
725
+ * document coordinates.
726
+ *
727
+ * This is the box of the *ink*, not of the elements: a full-width wrapper
728
+ * `div` with nothing of its own to draw does not count, the text and the
729
+ * button inside it do. An element counts when it draws something itself — a
730
+ * background, a border, an outline, a shadow — or is replaced content (an
731
+ * image, an SVG, a canvas, a form control); text counts by the boxes of its
732
+ * line fragments. Descendants of an element that clips its overflow are
733
+ * clipped to it, and nothing inside a hidden or fully transparent subtree is
734
+ * counted.
735
+ *
736
+ * Portals need no special case: anything a component renders elsewhere in
737
+ * `body` — a popover, a dialog and its backdrop — is measured like the rest.
738
+ *
739
+ * Kept free of syntax a bundler might rewrite with a helper (spread, optional
740
+ * catch binding): Playwright ships this function to the browser as source.
741
+ */
742
+ function measurePaintedBox(args) {
743
+ const doc = document;
744
+ const win = window;
745
+ const scrollX = win.scrollX;
746
+ const scrollY = win.scrollY;
747
+ const scrolling = doc.scrollingElement || doc.documentElement;
748
+ const result = {
749
+ box: null,
750
+ scrollX,
751
+ scrollY,
752
+ documentWidth: Math.max(scrolling.scrollWidth, win.innerWidth),
753
+ documentHeight: Math.max(scrolling.scrollHeight, win.innerHeight)
754
+ };
755
+ const root = doc.querySelector(args.root);
756
+ if (!root) return result;
757
+ const NO_CLIP = {
758
+ l: -Infinity,
759
+ t: -Infinity,
760
+ r: Infinity,
761
+ b: Infinity
762
+ };
763
+ let l = Infinity;
764
+ let t = Infinity;
765
+ let r = -Infinity;
766
+ let b = -Infinity;
767
+ const add = (rect, grow, clip) => {
768
+ if (rect.width <= 0 || rect.height <= 0) return;
769
+ const left = Math.max(rect.left - grow, clip.l);
770
+ const top = Math.max(rect.top - grow, clip.t);
771
+ const right = Math.min(rect.right + grow, clip.r);
772
+ const bottom = Math.min(rect.bottom + grow, clip.b);
773
+ if (right <= left || bottom <= top) return;
774
+ if (left < l) l = left;
775
+ if (top < t) t = top;
776
+ if (right > r) r = right;
777
+ if (bottom > b) b = bottom;
778
+ };
779
+ const visibleColor = (color) => {
780
+ if (!color || color === "transparent") return false;
781
+ const slash = color.match(/\/\s*([\d.]+)(%?)\s*\)$/);
782
+ if (slash) return parseFloat(slash[1] || "0") > 0;
783
+ const rgba = color.match(/^rgba\((?:[^,]+,){3}\s*([\d.]+)(%?)\s*\)$/);
784
+ if (rgba) return parseFloat(rgba[1] || "0") > 0;
785
+ return true;
786
+ };
787
+ const shadowExtent = (value) => {
788
+ if (!value || value === "none") return 0;
789
+ let stripped = value;
790
+ let previous = "";
791
+ while (previous !== stripped) {
792
+ previous = stripped;
793
+ stripped = stripped.replace(/[a-z-]+\([^()]*\)/gi, " ");
794
+ }
795
+ let extent = 0;
796
+ const shadows = stripped.split(",");
797
+ for (let i = 0; i < shadows.length; i++) {
798
+ const shadow = shadows[i] || "";
799
+ if (/\binset\b/.test(shadow)) continue;
800
+ const lengths = (shadow.match(/-?\d*\.?\d+px/g) || []).map(parseFloat);
801
+ const reach = Math.max(Math.abs(lengths[0] || 0), Math.abs(lengths[1] || 0)) + Math.max(0, lengths[2] || 0) + (lengths[3] || 0);
802
+ if (reach > extent) extent = reach;
803
+ }
804
+ return extent;
805
+ };
806
+ const REPLACED = {
807
+ IMG: true,
808
+ SVG: true,
809
+ CANVAS: true,
810
+ VIDEO: true,
811
+ IFRAME: true,
812
+ EMBED: true,
813
+ OBJECT: true,
814
+ INPUT: true,
815
+ TEXTAREA: true,
816
+ SELECT: true,
817
+ PROGRESS: true,
818
+ METER: true,
819
+ HR: true
820
+ };
821
+ const SKIPPED = {
822
+ SCRIPT: true,
823
+ STYLE: true,
824
+ LINK: true,
825
+ META: true,
826
+ TEMPLATE: true,
827
+ NOSCRIPT: true
828
+ };
829
+ const range = doc.createRange();
830
+ const paintsSelf = (style) => {
831
+ if (visibleColor(style.backgroundColor)) return true;
832
+ if (style.backgroundImage && style.backgroundImage !== "none") return true;
833
+ const sides = [
834
+ "Top",
835
+ "Right",
836
+ "Bottom",
837
+ "Left"
838
+ ];
839
+ for (let i = 0; i < sides.length; i++) {
840
+ const side = sides[i];
841
+ const width = parseFloat(style.getPropertyValue(`border-${side?.toLowerCase()}-width`));
842
+ const kind = style.getPropertyValue(`border-${side?.toLowerCase()}-style`);
843
+ const color = style.getPropertyValue(`border-${side?.toLowerCase()}-color`);
844
+ if (width > 0 && kind !== "none" && kind !== "hidden" && visibleColor(color)) return true;
845
+ }
846
+ return false;
847
+ };
848
+ const walk = (node, clip, visible, isRoot) => {
849
+ if (node.nodeType === 3) {
850
+ if (!visible || !(node.textContent || "").trim()) return;
851
+ range.selectNodeContents(node);
852
+ const rects = range.getClientRects();
853
+ for (let i = 0; i < rects.length; i++) {
854
+ const rect = rects[i];
855
+ if (rect) add(rect, 0, clip);
856
+ }
857
+ return;
858
+ }
859
+ if (node.nodeType !== 1) return;
860
+ const el = node;
861
+ if (SKIPPED[el.tagName.toUpperCase()]) return;
862
+ const style = win.getComputedStyle(el);
863
+ if (style.display === "none") return;
864
+ if (parseFloat(style.opacity) === 0) return;
865
+ const selfVisible = style.visibility !== "hidden" && style.visibility !== "collapse";
866
+ const rect = el.getBoundingClientRect();
867
+ if (!isRoot && selfVisible) {
868
+ const tag = el.tagName.toUpperCase();
869
+ if (REPLACED[tag]) add(rect, 0, clip);
870
+ else if (paintsSelf(style)) add(rect, 0, clip);
871
+ const shadow = shadowExtent(style.boxShadow);
872
+ if (shadow > 0) add(rect, shadow, clip);
873
+ if (style.outlineStyle !== "none") {
874
+ const outline = (parseFloat(style.outlineWidth) || 0) + (parseFloat(style.outlineOffset) || 0);
875
+ if (outline > 0) add(rect, outline, clip);
876
+ }
877
+ if (REPLACED[tag]) return;
878
+ }
879
+ let childClip = clip;
880
+ const clipsX = style.overflowX !== "visible";
881
+ const clipsY = style.overflowY !== "visible";
882
+ if (!isRoot && (clipsX || clipsY)) childClip = {
883
+ l: clipsX ? Math.max(clip.l, rect.left) : clip.l,
884
+ t: clipsY ? Math.max(clip.t, rect.top) : clip.t,
885
+ r: clipsX ? Math.min(clip.r, rect.right) : clip.r,
886
+ b: clipsY ? Math.min(clip.b, rect.bottom) : clip.b
887
+ };
888
+ const shadowRoot = el.shadowRoot;
889
+ if (shadowRoot) {
890
+ const shadowChildren = shadowRoot.childNodes;
891
+ for (let i = 0; i < shadowChildren.length; i++) {
892
+ const child = shadowChildren[i];
893
+ if (child) walk(child, childClip, selfVisible, false);
894
+ }
895
+ }
896
+ const children = el.childNodes;
897
+ for (let i = 0; i < children.length; i++) {
898
+ const child = children[i];
899
+ if (child) walk(child, childClip, selfVisible, false);
900
+ }
901
+ };
902
+ walk(root, NO_CLIP, true, true);
903
+ range.detach();
904
+ if (r <= l || b <= t) return result;
905
+ result.box = {
906
+ x: l + scrollX,
907
+ y: t + scrollY,
908
+ width: r - l,
909
+ height: b - t
910
+ };
911
+ return result;
912
+ }
913
+ /**
914
+ * The clip for a content-cropped screenshot, or `undefined` when there is
915
+ * nothing to crop to (nothing painted, or the root is missing) — the caller
916
+ * then takes the uncropped screenshot rather than fail.
917
+ *
918
+ * Coordinates follow Playwright's `clip`: relative to the document with
919
+ * `fullPage`, to the viewport without it. Edges are rounded outwards to whole
920
+ * pixels so the crop never cuts into anti-aliased edges, and the padding is
921
+ * clamped to the document so it never asks for pixels that do not exist.
922
+ */
923
+ async function measureContentClip(page, crop, fullPage) {
924
+ const measured = await page.evaluate(measurePaintedBox, { root: crop.root });
925
+ if (!measured.box) return void 0;
926
+ const { box } = measured;
927
+ let left = Math.max(0, Math.floor(box.x - crop.padding));
928
+ let top = Math.max(0, Math.floor(box.y - crop.padding));
929
+ let right = Math.min(measured.documentWidth, Math.ceil(box.x + box.width + crop.padding));
930
+ let bottom = Math.min(measured.documentHeight, Math.ceil(box.y + box.height + crop.padding));
931
+ if (!fullPage) {
932
+ const viewport = page.viewportSize();
933
+ const scrollX = Math.floor(measured.scrollX);
934
+ const scrollY = Math.floor(measured.scrollY);
935
+ left = Math.max(0, left - scrollX);
936
+ top = Math.max(0, top - scrollY);
937
+ right -= scrollX;
938
+ bottom -= scrollY;
939
+ if (viewport) {
940
+ right = Math.min(right, viewport.width);
941
+ bottom = Math.min(bottom, viewport.height);
942
+ }
943
+ }
944
+ if (right <= left || bottom <= top) return void 0;
945
+ return {
946
+ x: left,
947
+ y: top,
948
+ width: right - left,
949
+ height: bottom - top
950
+ };
951
+ }
952
+ //#endregion
528
953
  //#region src/mapWithConcurrency.ts
529
954
  /**
530
955
  * Run async work over `items` with at most `concurrency` tasks in flight.
@@ -1597,6 +2022,8 @@ var ScreenshotTool = class {
1597
2022
  headless;
1598
2023
  viewport;
1599
2024
  fullPage;
2025
+ /** Default content crop; a screenshot's own `crop` overrides it. */
2026
+ crop;
1600
2027
  outputDir;
1601
2028
  browser = null;
1602
2029
  context = null;
@@ -1630,6 +2057,7 @@ var ScreenshotTool = class {
1630
2057
  height: 1080
1631
2058
  };
1632
2059
  this.fullPage = options.fullPage ?? true;
2060
+ this.crop = options.crop ?? false;
1633
2061
  this.outputDir = options.outputDir || "./screenshots";
1634
2062
  this.diff = this.resolveDiffOptions(options.diff);
1635
2063
  this.concurrency = Math.max(1, options.concurrency || 1);
@@ -1808,9 +2236,35 @@ var ScreenshotTool = class {
1808
2236
  try {
1809
2237
  const filepath = this.filesystem.getActualFilePath(filename);
1810
2238
  await this.filesystem.ensureParentDir(filepath);
2239
+ await this.shoot(page, options, filepath);
2240
+ this.logger.success(`Screenshot saved: ${filepath}`);
2241
+ return filepath;
2242
+ } catch (error) {
2243
+ this.logger.error(`Error taking screenshot of ${page.url()}:`, error.message);
2244
+ throw error;
2245
+ }
2246
+ }
2247
+ /**
2248
+ * Take one screenshot with `options`, under its viewport and after its
2249
+ * delay, cropped to the page's content when `crop` asks for it.
2250
+ *
2251
+ * The crop is measured right before the shot, after the delay, so it sees
2252
+ * the same render the screenshot does — and every retry measures afresh.
2253
+ */
2254
+ async shoot(page, options, filepath) {
2255
+ const fullPage = options.fullPage ?? this.fullPage;
2256
+ const crop = resolveCrop(options.crop ?? this.crop);
2257
+ return this.withViewport(page, options.viewport, async () => {
2258
+ if (options.delay) {
2259
+ this.logger.debug(`Waiting ${options.delay}ms before taking screenshot`);
2260
+ await page.waitForTimeout(options.delay);
2261
+ }
2262
+ const clip = crop ? await measureContentClip(page, crop, fullPage) : void 0;
2263
+ if (crop && !clip) this.logger.debug(`Nothing painted inside "${crop.root}" to crop to; taking the uncropped screenshot`);
1811
2264
  const screenshotOptions = {
1812
2265
  path: filepath,
1813
- fullPage: options.fullPage ?? this.fullPage,
2266
+ fullPage,
2267
+ clip,
1814
2268
  type: "png",
1815
2269
  timeout: 6e4,
1816
2270
  mask: options.mask,
@@ -1819,19 +2273,8 @@ var ScreenshotTool = class {
1819
2273
  animations: "disabled",
1820
2274
  caret: "hide"
1821
2275
  };
1822
- await this.withViewport(page, options.viewport, async () => {
1823
- if (options.delay) {
1824
- this.logger.debug(`Waiting ${options.delay}ms before taking screenshot`);
1825
- await page.waitForTimeout(options.delay);
1826
- }
1827
- await page.screenshot(screenshotOptions);
1828
- });
1829
- this.logger.success(`Screenshot saved: ${filepath}`);
1830
- return filepath;
1831
- } catch (error) {
1832
- this.logger.error(`Error taking screenshot of ${page.url()}:`, error.message);
1833
- throw error;
1834
- }
2276
+ return page.screenshot(screenshotOptions);
2277
+ });
1835
2278
  }
1836
2279
  /**
1837
2280
  * Sets the viewport size and executes the action, ensuring the original viewport is restored
@@ -1855,23 +2298,7 @@ var ScreenshotTool = class {
1855
2298
  if (!this.browser) throw new Error("Browser not initialized");
1856
2299
  if (options.skip) throw new Error("Screenshot skipped");
1857
2300
  try {
1858
- const screenshotOptions = {
1859
- fullPage: options.fullPage ?? this.fullPage,
1860
- type: "png",
1861
- timeout: 6e4,
1862
- mask: options.mask,
1863
- omitBackground: options.omitBackground,
1864
- scale: "css",
1865
- animations: "disabled",
1866
- caret: "hide"
1867
- };
1868
- return await this.withViewport(page, options.viewport, async () => {
1869
- if (options.delay) {
1870
- this.logger.debug(`Waiting ${options.delay}ms before taking screenshot`);
1871
- await page.waitForTimeout(options.delay);
1872
- }
1873
- return page.screenshot(screenshotOptions);
1874
- });
2301
+ return await this.shoot(page, options);
1875
2302
  } catch (error) {
1876
2303
  this.logger.error(`Error taking screenshot of ${page.url()}:`, error.message);
1877
2304
  throw error;
@@ -1883,6 +2310,7 @@ var ScreenshotTool = class {
1883
2310
  const filepath = this.filesystem.getActualFilePath(filename);
1884
2311
  const screenshotSettings = {
1885
2312
  fullPage: options.fullPage ?? this.fullPage,
2313
+ crop: options.crop,
1886
2314
  mask: options.mask,
1887
2315
  omitBackground: options.omitBackground,
1888
2316
  delay: options.delay,
@@ -1909,11 +2337,13 @@ var ScreenshotTool = class {
1909
2337
  }
1910
2338
  if (retryScreenshot.comparisonResult !== null && typeof retryScreenshot.comparisonResult === "object" && "differentSizes" in retryScreenshot.comparisonResult && retryScreenshot.comparisonResult.differentSizes) {
1911
2339
  this.logger.warn(`Screenshot has different sizes than reference image`);
1912
- const diffFilename = options.diffImageFilename || filename;
1913
- diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
1914
- const diffBuffer = await createDiffSizePngImage(200, 200);
1915
- await this.filesystem.writeDiffFile(diffFilename, diffBuffer);
1916
- this.logger.debug(`Diff Size image saved: ${diffImagePath}`);
2340
+ if (!diffImagePath) {
2341
+ const diffFilename = options.diffImageFilename || filename;
2342
+ diffImagePath = this.filesystem.getDiffFilePath(diffFilename);
2343
+ const diffBuffer = await createDiffSizePngImage(200, 200);
2344
+ await this.filesystem.writeDiffFile(diffFilename, diffBuffer);
2345
+ this.logger.debug(`Diff Size image saved: ${diffImagePath}`);
2346
+ }
1917
2347
  }
1918
2348
  return {
1919
2349
  screenshotPath: filepath,
@@ -1948,7 +2378,7 @@ var ScreenshotTool = class {
1948
2378
  passed: true
1949
2379
  };
1950
2380
  else {
1951
- const summary = "numDiffPixels" in comparisonResult ? comparisonResult.differentSizes ? "different image sizes" : `${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)` : `gmsd score ${comparisonResult.gmsd.toFixed(4)}`;
2381
+ const summary = "numDiffPixels" in comparisonResult ? `${comparisonResult.differentSizes ? "different image sizes, " : ""}${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)` : `gmsd score ${comparisonResult.gmsd.toFixed(4)}`;
1952
2382
  this.logger.warn(`Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \nComparison result: ${summary}`);
1953
2383
  }
1954
2384
  } catch (error) {
@@ -2203,27 +2633,23 @@ var ScreenshotTool = class {
2203
2633
  */
2204
2634
  const AGENT_DIRNAME = ".cappa-agent";
2205
2635
  /** Bumped only if `request.json` or `report.json` change incompatibly. */
2206
- const AGENT_REPORT_VERSION = 1;
2636
+ const AGENT_REPORT_VERSION = 2;
2207
2637
  const agentDir = (outputDir) => node_path.default.resolve(outputDir, AGENT_DIRNAME);
2208
2638
  const judgmentsRoot = (outputDir) => node_path.default.join(agentDir(outputDir), "judgments");
2209
2639
  const agentReportPath = (outputDir) => node_path.default.join(agentDir(outputDir), "report.json");
2210
2640
  //#endregion
2211
2641
  //#region src/agent/verdict.ts
2212
2642
  /**
2213
- * What a reviewer — heuristic or agent — believes a changed screenshot is.
2643
+ * What a judge believes a changed screenshot is, after looking at it.
2214
2644
  *
2215
- * `ambiguous` is only ever produced by the heuristic: it is the signal that
2216
- * the change has to be looked at, and an agent's verdict must resolve it into
2217
- * one of the other three.
2645
+ * Only a judge labels a change. Cappa's diff threshold already absorbs
2646
+ * sub-pixel rendering differences, so a screenshot that reaches `changed`
2647
+ * differs beyond the tolerance the project configured — and classifying it
2648
+ * from diff signals alone (change types, severity, pixel counts) proved to
2649
+ * mislabel exactly the changes that matter: a font-size change reads as a
2650
+ * harmless shift plus recolour. Every changed screenshot is looked at instead.
2218
2651
  */
2219
2652
  const VERDICT_LABELS = [
2220
- "regression-likely",
2221
- "intentional-likely",
2222
- "noise-likely",
2223
- "ambiguous"
2224
- ];
2225
- /** The labels a judge may hand back — everything but `ambiguous`. */
2226
- const JUDGED_LABELS = [
2227
2653
  "regression-likely",
2228
2654
  "intentional-likely",
2229
2655
  "noise-likely"
@@ -2231,103 +2657,43 @@ const JUDGED_LABELS = [
2231
2657
  const ACTION_BY_LABEL = {
2232
2658
  "regression-likely": "investigate",
2233
2659
  "intentional-likely": "approve-if-intended",
2234
- "noise-likely": "mask-or-approve",
2235
- ambiguous: "investigate"
2660
+ "noise-likely": "mask-or-approve"
2236
2661
  };
2237
2662
  const isVerdictLabel = (value) => typeof value === "string" && VERDICT_LABELS.includes(value);
2238
- const isJudgedLabel = (value) => typeof value === "string" && JUDGED_LABELS.includes(value);
2239
2663
  /**
2240
2664
  * Change types are normalised to kebab-case before they are compared.
2241
2665
  *
2242
2666
  * `@blazediff/interpret-native` reports `color-change` today but earlier
2243
- * releases reported `ColorChange`; normalising keeps the heuristic working
2244
- * across a diff-engine upgrade instead of silently degrading every screenshot
2245
- * to `ambiguous`.
2667
+ * releases reported `ColorChange`; normalising keeps the capture report's
2668
+ * labels working across a diff-engine upgrade.
2246
2669
  */
2247
2670
  const normalizeChangeType = (changeType) => changeType.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
2248
- const normalizeSeverity = (severity) => (severity ?? "").toLowerCase();
2249
- /** Structural: something appeared, vanished or was replaced. */
2250
- const REGRESSIVE_TYPES = /* @__PURE__ */ new Set([
2251
- "content-change",
2252
- "addition",
2253
- "deletion"
2254
- ]);
2255
- /** Restyled or moved, with structure intact. */
2256
- const RESTYLE_TYPES = /* @__PURE__ */ new Set(["color-change", "shift"]);
2257
- const NOISE_TYPES = /* @__PURE__ */ new Set(["rendering-noise"]);
2258
- const ELEVATED_SEVERITY = /* @__PURE__ */ new Set(["medium", "high"]);
2259
- /** Below this share of the image, a low-severity change is not perceptible. */
2260
- const SUB_PERCEPTUAL_PERCENT = .01;
2261
- /** Mean region confidence above which a pure recolour is trusted. */
2262
- const RECOLOR_CONFIDENCE = .7;
2263
2671
  const formatPercent = (percent) => {
2264
2672
  if (percent === void 0) return "?%";
2265
2673
  return percent >= .01 ? `${percent.toFixed(2)}%` : `${percent.toFixed(3)}%`;
2266
2674
  };
2267
- const countByType = (regions) => {
2268
- const counts = /* @__PURE__ */ new Map();
2269
- for (const region of regions) {
2270
- const type = normalizeChangeType(region.changeType);
2271
- counts.set(type, (counts.get(type) ?? 0) + 1);
2272
- }
2273
- return counts;
2274
- };
2275
- const formatBreakdown = (counts) => [...counts].sort((a, b) => b[1] - a[1]).map(([type, count]) => `${count} ${type}`).join(", ");
2276
2675
  const largestRegion = (regions) => regions.reduce((best, region) => !best || region.pixelCount > best.pixelCount ? region : best, void 0);
2277
- const meanConfidence = (regions) => regions.length === 0 ? 0 : regions.reduce((sum, region) => sum + (region.confidence ?? 0), 0) / regions.length;
2278
2676
  /**
2279
- * A one-line description of a change, shaped so that the same kind of change
2280
- * on different screenshots produces the same headline — which is what lets an
2281
- * agent spot one shared cause (a footer timestamp, a theme toggle) across
2282
- * dozens of screenshots without opening any of them.
2677
+ * A one-line description of where and how much a screenshot changed, shaped
2678
+ * so that the same change on different screenshots produces the same text —
2679
+ * which is what lets a judge spot one shared cause (a footer timestamp, a
2680
+ * theme toggle) across dozens of screenshots and judge them together.
2681
+ *
2682
+ * Deliberately geometry only: no change types, no severity. Those are guesses
2683
+ * from pixel signals, and a judge primed with "noise" or "shift" looks less
2684
+ * carefully at what is in fact a regression.
2283
2685
  */
2284
- const buildHeadline = (input) => {
2285
- if (input.differentSizes) return "image dimensions changed";
2286
- const interpretation = input.interpretation;
2287
- const percent = input.percentDifference ?? interpretation?.diffPercentage;
2288
- if (!interpretation || interpretation.regions.length === 0) return `${(input.numDiffPixels ?? interpretation?.diffCount)?.toLocaleString("en-US") ?? "?"} px (${formatPercent(percent)}) - no region analysis`;
2289
- const regions = interpretation.regions;
2290
- const counts = countByType(regions);
2686
+ const describeChange = (input) => {
2687
+ const regions = input.interpretation?.regions ?? [];
2688
+ if (input.differentSizes) return regions.length > 0 ? `dimensions changed; ${describeChange({
2689
+ ...input,
2690
+ differentSizes: false
2691
+ })}` : "image dimensions changed";
2692
+ const percent = input.percentDifference ?? input.interpretation?.diffPercentage;
2693
+ if (regions.length === 0) return `${(input.numDiffPixels ?? input.interpretation?.diffCount)?.toLocaleString("en-US") ?? "?"} px (${formatPercent(percent)}) - no regions located`;
2291
2694
  const position = largestRegion(regions)?.position;
2292
2695
  const at = position ? ` @ ${position}` : "";
2293
- const severity = normalizeSeverity(interpretation.severity) || "?";
2294
- if (regions.length === 1) return `1 ${[...counts.keys()][0]}${at} (${formatPercent(percent)}, ${severity})`;
2295
- return `${regions.length} regions: ${formatBreakdown(counts)}${at} (${formatPercent(percent)}, ${severity})`;
2296
- };
2297
- const verdict = (label, headline, rationale) => ({
2298
- label,
2299
- headline,
2300
- rationale,
2301
- action: ACTION_BY_LABEL[label]
2302
- });
2303
- /**
2304
- * Classify a changed screenshot from its diff interpretation alone.
2305
- *
2306
- * Deliberately conservative: only the cases the signals settle are labelled,
2307
- * and everything else is `ambiguous` so that it reaches a judge who can look
2308
- * at the pixels. A wrong `noise-likely` hides a regression; a wrong
2309
- * `ambiguous` costs one look.
2310
- */
2311
- const deriveVerdict = (input) => {
2312
- const headline = buildHeadline(input);
2313
- if (input.differentSizes) return verdict("ambiguous", headline, ["expected and actual have different dimensions — content likely grew or shrank", "could be an intended content change or a broken layout"]);
2314
- const interpretation = input.interpretation;
2315
- if (!interpretation || interpretation.regions.length === 0) return verdict("ambiguous", headline, ["pixels differ but no change regions were found"]);
2316
- const regions = interpretation.regions;
2317
- const types = regions.map((region) => normalizeChangeType(region.changeType));
2318
- const severity = normalizeSeverity(interpretation.severity);
2319
- const percent = input.percentDifference ?? interpretation.diffPercentage;
2320
- const counts = countByType(regions);
2321
- if (types.every((type) => NOISE_TYPES.has(type))) return verdict("noise-likely", headline, ["every region is classified as rendering noise"]);
2322
- if (percent < .01 && severity === "low") return verdict("noise-likely", headline, [`less than ${SUB_PERCEPTUAL_PERCENT}% of the image differs (${formatPercent(percent)}) at low severity`, "sub-perceptual change"]);
2323
- if (types.some((type) => REGRESSIVE_TYPES.has(type)) && ELEVATED_SEVERITY.has(severity)) {
2324
- const structural = [...counts].filter(([type]) => REGRESSIVE_TYPES.has(type)).map(([type, count]) => `${count} ${type}`).join(", ");
2325
- return verdict("regression-likely", headline, [`${severity} severity with structural changes (${structural})`, "content or layout changed, not just styling"]);
2326
- }
2327
- const allRecolor = types.every((type) => type === "color-change");
2328
- if (allRecolor && meanConfidence(regions) > RECOLOR_CONFIDENCE) return verdict("intentional-likely", headline, [`${regions.length} confident color-change ${regions.length === 1 ? "region" : "regions"}`, "edge structure preserved — looks like a theming or palette change"]);
2329
- if (!allRecolor && types.every((type) => RESTYLE_TYPES.has(type))) return verdict("intentional-likely", headline, ["every region is a shift or color change — content moved or was restyled, structure preserved"]);
2330
- return verdict("ambiguous", headline, [`mix of change types (${formatBreakdown(counts)}) at ${severity || "unknown"} severity`, `${formatPercent(percent)} of the image differs`]);
2696
+ return `${`${regions.length} ${regions.length === 1 ? "region" : "regions"}`}${at} (${formatPercent(percent)})`;
2331
2697
  };
2332
2698
  //#endregion
2333
2699
  //#region src/agent/reviewIndex.ts
@@ -2345,11 +2711,11 @@ const readAgentReviewIndex = async (outputDir) => {
2345
2711
  return;
2346
2712
  }
2347
2713
  const createdAt = typeof raw?.createdAt === "string" ? Date.parse(raw.createdAt) : NaN;
2348
- if (raw?.version !== 1 || Number.isNaN(createdAt) || !Array.isArray(raw.entries)) return;
2714
+ if (raw?.version !== 2 || Number.isNaN(createdAt) || !Array.isArray(raw.entries)) return;
2349
2715
  const reviews = /* @__PURE__ */ new Map();
2350
2716
  for (const entry of raw.entries) {
2351
2717
  if (typeof entry !== "object" || entry === null) continue;
2352
- const { name, category, verdict, pending } = entry;
2718
+ const { name, category, verdict } = entry;
2353
2719
  if (typeof name !== "string" || category !== "changed") continue;
2354
2720
  if (typeof verdict !== "object" || verdict === null) continue;
2355
2721
  const v = verdict;
@@ -2359,9 +2725,9 @@ const readAgentReviewIndex = async (outputDir) => {
2359
2725
  headline: v.headline,
2360
2726
  rationale: Array.isArray(v.rationale) ? v.rationale.filter((line) => typeof line === "string") : [],
2361
2727
  action: v.action,
2362
- source: v.source === "agent" ? "agent" : "heuristic",
2728
+ source: "agent",
2363
2729
  ...typeof v.confidence === "number" ? { confidence: v.confidence } : {},
2364
- pending: pending === true,
2730
+ pending: false,
2365
2731
  reviewedAt: new Date(createdAt).toISOString()
2366
2732
  });
2367
2733
  }
@@ -2370,9 +2736,15 @@ const readAgentReviewIndex = async (outputDir) => {
2370
2736
  reviews
2371
2737
  };
2372
2738
  };
2739
+ /**
2740
+ * A file's modification time at the report's precision: `createdAt` is an ISO
2741
+ * string, so whole milliseconds, while `mtimeMs` carries a fraction. Compared
2742
+ * unrounded, an image written in the same millisecond the check started would
2743
+ * read as newer than the check and drop a verdict made about it.
2744
+ */
2373
2745
  const modifiedAt = async (file) => {
2374
2746
  try {
2375
- return (await node_fs_promises.default.stat(file)).mtimeMs;
2747
+ return Math.floor((await node_fs_promises.default.stat(file)).mtimeMs);
2376
2748
  } catch {
2377
2749
  return Number.POSITIVE_INFINITY;
2378
2750
  }
@@ -3621,6 +3993,7 @@ var LocalEngine = class {
3621
3993
  concurrency: this.options.concurrency,
3622
3994
  logConsoleEvents: this.options.logConsoleEvents,
3623
3995
  fullPage: this.options.fullPage ?? true,
3996
+ crop: this.options.crop ?? false,
3624
3997
  viewport: this.options.viewport ?? {
3625
3998
  width: 1920,
3626
3999
  height: 1080
@@ -4173,6 +4546,9 @@ const renderOverview = async (options) => {
4173
4546
  * Agent review: turn the screenshots on disk into something a coding agent
4174
4547
  * can judge cheaply, and fold its judgments back in.
4175
4548
  *
4549
+ * Every changed screenshot gets a judgment request. Nothing is labelled from
4550
+ * diff signals alone — see `VERDICT_LABELS` for why.
4551
+ *
4176
4552
  * Nothing here captures. It reads `actual/`, `expected/` and `diff/` as the
4177
4553
  * last `cappa capture` left them, so it is safe to run repeatedly — a re-run
4178
4554
  * is how an agent's verdicts are applied, and a re-capture invalidates them
@@ -4187,7 +4563,7 @@ const renderOverview = async (options) => {
4187
4563
  * judgments/<name>/request.json what to judge and where the evidence is
4188
4564
  * judgments/<name>/locator.png thumbnail with every region outlined
4189
4565
  * judgments/<name>/regions.png [expected | actual] crop per region
4190
- * judgments/<name>/overview.png instead of the two above when sizes differ
4566
+ * judgments/<name>/overview.png both images whole, when their sizes differ
4191
4567
  * judgments/<name>/verdict.json written by the judge
4192
4568
  * ```
4193
4569
  */
@@ -4200,11 +4576,6 @@ const JUDGMENT_FILES = [
4200
4576
  "regions.png",
4201
4577
  "overview.png"
4202
4578
  ];
4203
- const JUDGE_MODES = [
4204
- "ambiguous",
4205
- "all",
4206
- "none"
4207
- ];
4208
4579
  /**
4209
4580
  * The directory holding a screenshot's judgment.
4210
4581
  *
@@ -4257,7 +4628,7 @@ const parseAgentVerdict = (raw) => {
4257
4628
  const outer = raw;
4258
4629
  const inner = typeof outer.verdict === "object" && outer.verdict !== null ? outer.verdict : outer;
4259
4630
  const label = inner.label;
4260
- if (!isJudgedLabel(label)) return {
4631
+ if (!isVerdictLabel(label)) return {
4261
4632
  ok: false,
4262
4633
  error: `label must be one of regression-likely, intentional-likely, noise-likely (got ${JSON.stringify(label)})`
4263
4634
  };
@@ -4286,6 +4657,18 @@ const readSize = async (file) => {
4286
4657
  height
4287
4658
  };
4288
4659
  };
4660
+ /**
4661
+ * Whether the diff on disk can be outlined in the compared coordinate space.
4662
+ *
4663
+ * Same-size screenshots always qualify. For a size change, only a diff of the
4664
+ * padded canvas does — one left by an older capture (a fixed-size
4665
+ * placeholder) would be stretched over the regions and misplace them.
4666
+ */
4667
+ const diffMatchesCanvas = async (diffPath, comparedActual, differentSizes) => {
4668
+ if (!differentSizes) return true;
4669
+ const [diff, canvas] = await Promise.all([readSize(diffPath), readSize(comparedActual)]);
4670
+ return diff.width === canvas.width && diff.height === canvas.height;
4671
+ };
4289
4672
  const toAgentRegions = (regions) => [...regions].sort((a, b) => b.pixelCount - a.pixelCount).map((region, index) => ({
4290
4673
  index: index + 1,
4291
4674
  bbox: {
@@ -4297,9 +4680,7 @@ const toAgentRegions = (regions) => [...regions].sort((a, b) => b.pixelCount - a
4297
4680
  pixelCount: region.pixelCount,
4298
4681
  percentage: region.percentage,
4299
4682
  position: region.position,
4300
- shape: region.shape,
4301
- changeType: normalizeChangeType(region.changeType),
4302
- confidence: region.confidence
4683
+ shape: region.shape
4303
4684
  }));
4304
4685
  /**
4305
4686
  * The interpretation a capture already persisted, or a fresh one.
@@ -4322,8 +4703,10 @@ const interpretationFor = async (screenshot, expectedPath, actualPath, diff) =>
4322
4703
  }
4323
4704
  };
4324
4705
  const JUDGE_INSTRUCTIONS = [
4706
+ "Every changed screenshot differs beyond the project's diff threshold, so sub-pixel rendering is already filtered out: look at each change before labelling it.",
4325
4707
  "Read locator.png and regions.png together. locator.png outlines every region (numbered) on a thumbnail; regions.png stacks one [expected | actual] row per region, in `regions` order.",
4326
- "Open overview.png when it exists instead — the image dimensions changed, so no regions could be located.",
4708
+ "overview.png exists only when the image dimensions changed: it shows both images whole, side by side. Regions are then located on a canvas padded to the larger size, anchored top-left — in a crop, area that only one image covers is empty.",
4709
+ "Compare sizes, not just content: text that is larger, smaller, bolder or reflowed, and spacing, alignment or wrapping that moved, are regressions unless a code change explains them.",
4327
4710
  "Only open paths.expected / paths.actual / paths.diff (full size) when a change clearly continues outside its crop.",
4328
4711
  "Write verdict.json next to this file, or run `cappa agent judge <name> --label <label> --headline <text>`."
4329
4712
  ];
@@ -4343,12 +4726,45 @@ const analyseChanged = async (screenshot, options, createdAt) => {
4343
4726
  readSize(actualPath),
4344
4727
  exists(diffPath)
4345
4728
  ]);
4346
- const differentSizes = expectedSize.width !== actualSize.width || expectedSize.height !== actualSize.height;
4347
- const interpretation = differentSizes ? void 0 : await interpretationFor(screenshot, expectedPath, actualPath, options.diff);
4729
+ const canvasDir = expectedSize.width !== actualSize.width || expectedSize.height !== actualSize.height ? await node_fs_promises.default.mkdtemp(node_path.default.join(node_os.default.tmpdir(), "cappa-agent-canvas-")) : void 0;
4730
+ try {
4731
+ const canvas = canvasDir ? await writeCommonCanvas(expectedPath, actualPath, canvasDir) : void 0;
4732
+ return await analyseOnCanvas({
4733
+ screenshot,
4734
+ options,
4735
+ createdAt,
4736
+ paths: {
4737
+ expectedPath,
4738
+ actualPath,
4739
+ diffPath,
4740
+ diffExists
4741
+ },
4742
+ sizes: {
4743
+ expectedSize,
4744
+ actualSize
4745
+ },
4746
+ canvas: canvas ? {
4747
+ expectedPath: canvas.expectedPath,
4748
+ actualPath: canvas.actualPath
4749
+ } : void 0
4750
+ });
4751
+ } finally {
4752
+ if (canvasDir) await node_fs_promises.default.rm(canvasDir, {
4753
+ recursive: true,
4754
+ force: true
4755
+ });
4756
+ }
4757
+ };
4758
+ const analyseOnCanvas = async ({ screenshot, options, createdAt, paths: { expectedPath, actualPath, diffPath, diffExists }, sizes: { expectedSize, actualSize }, canvas }) => {
4759
+ const { outputDir } = options;
4760
+ const differentSizes = canvas !== void 0;
4761
+ const comparedExpected = canvas?.expectedPath ?? expectedPath;
4762
+ const comparedActual = canvas?.actualPath ?? actualPath;
4763
+ const interpretation = await interpretationFor(screenshot, comparedExpected, comparedActual, options.diff);
4348
4764
  const percentDifference = screenshot.diffMeta?.percentDifference ?? interpretation?.diffPercentage;
4349
4765
  const numDiffPixels = screenshot.diffMeta?.numDiffPixels ?? interpretation?.diffCount;
4350
4766
  const regions = toAgentRegions(interpretation?.regions ?? []);
4351
- const heuristic = deriveVerdict({
4767
+ const change = describeChange({
4352
4768
  percentDifference,
4353
4769
  numDiffPixels,
4354
4770
  interpretation,
@@ -4372,20 +4788,13 @@ const analyseChanged = async (screenshot, options, createdAt) => {
4372
4788
  expected: expectedSize,
4373
4789
  actual: actualSize
4374
4790
  },
4375
- severity: interpretation?.severity?.toLowerCase(),
4376
- summary: interpretation?.summary,
4791
+ change,
4377
4792
  regions,
4378
- heuristic,
4379
- verdict: {
4380
- ...heuristic,
4381
- source: "heuristic"
4382
- },
4383
- pending: false
4793
+ pending: true
4384
4794
  };
4385
4795
  const dir = judgmentDir(outputDir, screenshot.name);
4386
4796
  const requestPath = node_path.default.join(dir, REQUEST_FILE);
4387
4797
  const verdictPath = node_path.default.join(dir, VERDICT_FILE);
4388
- if (!(options.judge === "all" || options.judge === "ambiguous" && heuristic.label === "ambiguous" || await exists(requestPath))) return entry;
4389
4798
  const fingerprint = await fingerprintImages(expectedPath, actualPath);
4390
4799
  const previous = await readJson(requestPath);
4391
4800
  if (previous && previous.fingerprint !== fingerprint) await clearJudgmentFiles(dir);
@@ -4400,11 +4809,11 @@ const analyseChanged = async (screenshot, options, createdAt) => {
4400
4809
  actualPath,
4401
4810
  outDir: dir
4402
4811
  });
4403
- else {
4812
+ if (!differentSizes || regions.length > 0) {
4404
4813
  const tiles = await renderJudgmentTiles({
4405
- expectedPath,
4406
- actualPath,
4407
- diffPath: diffExists ? diffPath : void 0,
4814
+ expectedPath: comparedExpected,
4815
+ actualPath: comparedActual,
4816
+ diffPath: diffExists && await diffMatchesCanvas(diffPath, comparedActual, differentSizes) ? diffPath : void 0,
4408
4817
  regions: regions.map((region) => region.bbox),
4409
4818
  outDir: dir,
4410
4819
  maxRegions: options.maxTileRegions
@@ -4414,7 +4823,7 @@ const analyseChanged = async (screenshot, options, createdAt) => {
4414
4823
  }
4415
4824
  entry.judgment = judgment;
4416
4825
  await writeJson(requestPath, {
4417
- version: 1,
4826
+ version: 2,
4418
4827
  name: entry.name,
4419
4828
  taskId: entry.taskId,
4420
4829
  plugin: entry.plugin,
@@ -4422,12 +4831,10 @@ const analyseChanged = async (screenshot, options, createdAt) => {
4422
4831
  createdAt,
4423
4832
  instructions: JUDGE_INSTRUCTIONS,
4424
4833
  verdictSchema: VERDICT_SCHEMA,
4425
- heuristic,
4834
+ change,
4426
4835
  percentDifference,
4427
4836
  numDiffPixels,
4428
4837
  dimensions: entry.dimensions,
4429
- severity: entry.severity,
4430
- summary: entry.summary,
4431
4838
  regions,
4432
4839
  paths: {
4433
4840
  locator: judgment.locator,
@@ -4439,22 +4846,18 @@ const analyseChanged = async (screenshot, options, createdAt) => {
4439
4846
  verdict: verdictPath
4440
4847
  }
4441
4848
  });
4442
- if (!await exists(verdictPath)) {
4443
- entry.pending = true;
4444
- return entry;
4445
- }
4849
+ if (!await exists(verdictPath)) return entry;
4446
4850
  const parsed = parseAgentVerdict(await readJson(verdictPath));
4447
4851
  if (!parsed.ok) {
4448
- entry.pending = true;
4449
4852
  entry.verdictError = parsed.error;
4450
4853
  return entry;
4451
4854
  }
4855
+ entry.pending = false;
4452
4856
  entry.verdict = {
4453
4857
  label: parsed.label,
4454
4858
  headline: parsed.headline,
4455
4859
  rationale: parsed.rationale,
4456
4860
  action: ACTION_BY_LABEL[parsed.label],
4457
- source: "agent",
4458
4861
  ...parsed.confidence !== void 0 ? { confidence: parsed.confidence } : {}
4459
4862
  };
4460
4863
  return entry;
@@ -4498,13 +4901,12 @@ const pruneJudgments = async (outputDir, keep) => {
4498
4901
  }
4499
4902
  };
4500
4903
  /**
4501
- * Analyse every non-passing screenshot, write judgment requests for the ones
4502
- * that need a judge, apply any verdicts already written, and persist the
4503
- * full report to `<outputDir>/.cappa-agent/report.json`.
4904
+ * Analyse every non-passing screenshot, write a judgment request for every
4905
+ * changed one, apply any verdicts already written, and persist the full
4906
+ * report to `<outputDir>/.cappa-agent/report.json`.
4504
4907
  */
4505
4908
  const runAgentCheck = async (options) => {
4506
4909
  const outputDir = node_path.default.resolve(options.outputDir);
4507
- const judge = options.judge ?? "ambiguous";
4508
4910
  const createdAt = (options.now?.() ?? /* @__PURE__ */ new Date()).toISOString();
4509
4911
  const screenshots = await collectScreenshots(outputDir);
4510
4912
  await node_fs_promises.default.mkdir(agentDir(outputDir), { recursive: true });
@@ -4512,8 +4914,7 @@ const runAgentCheck = async (options) => {
4512
4914
  const changed = screenshots.filter((s) => s.category === "changed");
4513
4915
  const changedEntries = await mapWithConcurrency(options.concurrency ?? 4, changed, (screenshot) => analyseChanged(screenshot, {
4514
4916
  ...options,
4515
- outputDir,
4516
- judge
4917
+ outputDir
4517
4918
  }, createdAt));
4518
4919
  await pruneJudgments(outputDir, new Set(changedEntries.flatMap((entry) => entry.judgment ? [entry.judgment.dir] : [])));
4519
4920
  const otherEntries = screenshots.flatMap((s) => {
@@ -4538,11 +4939,10 @@ const runAgentCheck = async (options) => {
4538
4939
  const entries = [...changedEntries, ...otherEntries];
4539
4940
  const count = (category) => screenshots.filter((s) => s.category === category).length;
4540
4941
  const report = {
4541
- version: 1,
4942
+ version: 2,
4542
4943
  createdAt,
4543
4944
  outputDir,
4544
4945
  reportPath: agentReportPath(outputDir),
4545
- judge,
4546
4946
  totals: {
4547
4947
  total: screenshots.length,
4548
4948
  passed: count("passed"),
@@ -4559,7 +4959,7 @@ const runAgentCheck = async (options) => {
4559
4959
  /** Read the report the last {@link runAgentCheck} wrote, if any. */
4560
4960
  const readAgentReport = async (outputDir) => {
4561
4961
  const raw = await readJson(agentReportPath(outputDir));
4562
- return raw?.version === 1 ? raw : void 0;
4962
+ return raw?.version === 2 ? raw : void 0;
4563
4963
  };
4564
4964
  /**
4565
4965
  * The report as an agent should first see it: one line of state per
@@ -4572,11 +4972,11 @@ const toSlimAgentReport = (report) => ({
4572
4972
  results: report.entries.map((entry) => ({
4573
4973
  name: entry.name,
4574
4974
  category: entry.category,
4975
+ ...entry.change ? { change: entry.change } : {},
4575
4976
  ...entry.verdict ? { verdict: {
4576
4977
  label: entry.verdict.label,
4577
4978
  headline: entry.verdict.headline,
4578
- action: entry.verdict.action,
4579
- source: entry.verdict.source
4979
+ action: entry.verdict.action
4580
4980
  } } : {},
4581
4981
  ...entry.pending ? { pending: true } : {},
4582
4982
  ...entry.pending && entry.judgment ? { judgment: entry.judgment.dir } : {},
@@ -4589,7 +4989,7 @@ const toSlimAgentReport = (report) => ({
4589
4989
  */
4590
4990
  const writeAgentVerdict = async (outputDir, name, input) => {
4591
4991
  const dir = judgmentDir(outputDir, name);
4592
- if (!await exists(node_path.default.join(dir, REQUEST_FILE))) throw new Error(`No judgment request for "${name}" — run \`cappa agent check\` first (use --judge all to request one for any changed screenshot).`);
4992
+ if (!await exists(node_path.default.join(dir, REQUEST_FILE))) throw new Error(`No judgment request for "${name}" — run \`cappa agent check\` first; only changed screenshots get one.`);
4593
4993
  const parsed = parseAgentVerdict(input);
4594
4994
  if (!parsed.ok) throw new Error(parsed.error);
4595
4995
  const verdictPath = node_path.default.join(dir, VERDICT_FILE);
@@ -4640,6 +5040,7 @@ exports.AGENT_DIRNAME = AGENT_DIRNAME;
4640
5040
  exports.AGENT_REPORT_VERSION = AGENT_REPORT_VERSION;
4641
5041
  exports.CONSOLE_LOG_LEVELS = CONSOLE_LOG_LEVELS;
4642
5042
  exports.CaptureRunner = CaptureRunner;
5043
+ exports.DEFAULT_CROP_PADDING = DEFAULT_CROP_PADDING;
4643
5044
  exports.DEFAULT_DEBOUNCE_MS = DEFAULT_DEBOUNCE_MS;
4644
5045
  exports.DEFAULT_IGNORED_DIRS = DEFAULT_IGNORED_DIRS;
4645
5046
  exports.DEFAULT_KILL_GRACE_MS = DEFAULT_KILL_GRACE_MS;
@@ -4647,8 +5048,6 @@ exports.DEFAULT_MAX_TASKS = DEFAULT_MAX_TASKS;
4647
5048
  exports.DEFAULT_WEB_SERVER_TIMEOUT_MS = DEFAULT_WEB_SERVER_TIMEOUT_MS;
4648
5049
  exports.ENGINE_ERROR_CODES = ENGINE_ERROR_CODES;
4649
5050
  exports.FsScreenshotStore = FsScreenshotStore;
4650
- exports.JUDGED_LABELS = JUDGED_LABELS;
4651
- exports.JUDGE_MODES = JUDGE_MODES;
4652
5051
  exports.LocalEngine = LocalEngine;
4653
5052
  exports.MANIFEST_FILENAME = MANIFEST_FILENAME;
4654
5053
  exports.MANIFEST_VERSION = MANIFEST_VERSION;
@@ -4656,7 +5055,6 @@ exports.NO_WEB_SERVER_HINT = NO_WEB_SERVER_HINT;
4656
5055
  exports.OutputTail = OutputTail;
4657
5056
  exports.RunInProgressError = RunInProgressError;
4658
5057
  exports.RunStore = RunStore;
4659
- exports.SUB_PERCEPTUAL_PERCENT = SUB_PERCEPTUAL_PERCENT;
4660
5058
  exports.ScreenshotFileSystem = ScreenshotFileSystem;
4661
5059
  exports.ScreenshotTool = ScreenshotTool;
4662
5060
  exports.UnknownTargetsError = UnknownTargetsError;
@@ -4669,14 +5067,13 @@ exports.WebServerPool = WebServerPool;
4669
5067
  exports.agentDir = agentDir;
4670
5068
  exports.agentReportPath = agentReportPath;
4671
5069
  exports.attachConsoleLogging = attachConsoleLogging;
4672
- exports.buildHeadline = buildHeadline;
4673
5070
  exports.clearJudgment = clearJudgment;
4674
5071
  exports.collectScreenshots = collectScreenshots;
4675
5072
  exports.compareImages = compareImages;
4676
5073
  exports.compareImagesGMSD = compareImagesGMSD;
4677
5074
  exports.contextPadding = contextPadding;
4678
5075
  exports.defineConfig = defineConfig;
4679
- exports.deriveVerdict = deriveVerdict;
5076
+ exports.describeChange = describeChange;
4680
5077
  exports.didScreenshotFail = didScreenshotFail;
4681
5078
  exports.filterTasks = filterTasks;
4682
5079
  exports.findRefusedConnection = findRefusedConnection;
@@ -4685,7 +5082,6 @@ exports.getDeletedScreenshots = getDeletedScreenshots;
4685
5082
  exports.groupScreenshots = groupScreenshots;
4686
5083
  exports.imagesMatch = imagesMatch;
4687
5084
  exports.imagesMatchGMSD = imagesMatchGMSD;
4688
- exports.isJudgedLabel = isJudgedLabel;
4689
5085
  exports.isRunInProgressError = isRunInProgressError;
4690
5086
  exports.isUnknownTargetsError = isUnknownTargetsError;
4691
5087
  exports.isUrlReady = isUrlReady;
@@ -4693,6 +5089,7 @@ exports.isVerdictLabel = isVerdictLabel;
4693
5089
  exports.isWatchInProgressError = isWatchInProgressError;
4694
5090
  exports.judgmentDir = judgmentDir;
4695
5091
  exports.mapWithConcurrency = mapWithConcurrency;
5092
+ exports.measureContentClip = measureContentClip;
4696
5093
  exports.mergeCaptureManifest = mergeCaptureManifest;
4697
5094
  exports.normalizeChangeType = normalizeChangeType;
4698
5095
  exports.padAndClamp = padAndClamp;
@@ -4703,6 +5100,7 @@ exports.readDiffMeta = readDiffMeta;
4703
5100
  exports.recordCaptureOrigins = recordCaptureOrigins;
4704
5101
  exports.renderJudgmentTiles = renderJudgmentTiles;
4705
5102
  exports.renderOverview = renderOverview;
5103
+ exports.resolveCrop = resolveCrop;
4706
5104
  exports.resolveWebServerConfig = resolveWebServerConfig;
4707
5105
  exports.runAgentCheck = runAgentCheck;
4708
5106
  exports.sameServer = sameServer;