Skip to content

fix(diff): Count extra comp pixels when compare image is larger than base#170

Merged
dmtrKovalenko merged 1 commit into
dmtrKovalenko:mainfrom
NicoHinderling:04-09-fix_diff_count_extra_comp_pixels_when_compare_image_is_larger_than_base
Apr 16, 2026
Merged

fix(diff): Count extra comp pixels when compare image is larger than base#170
dmtrKovalenko merged 1 commit into
dmtrKovalenko:mainfrom
NicoHinderling:04-09-fix_diff_count_extra_comp_pixels_when_compare_image_is_larger_than_base

Conversation

@NicoHinderling
Copy link
Copy Markdown
Contributor

Summary

  • compareDifferentLayouts now counts extra comp columns (per row) and extra comp rows when the compare image is larger than the base — previously these pixels were silently ignored, causing false match: true for images where comp is wider or taller
  • Use bounding box denominator max(base.width, comp.width) × max(base.height, comp.height) for diffPercentage so it stays ≤100%
  • Add server test for different-dimension image detection
  • Update existing layout diff test expectations to reflect the corrected pixel counts

Root cause

compareDifferentLayouts had asymmetric handling: it counted extra pixels when base > comp but completely ignored extra pixels when comp > base. For example, comparing a 200×200 base against a 300×200 compare image would ignore the 100 extra columns entirely, producing diff_count == 0.

This bug affects both CLI and server mode. The race condition from #169 (thread-safe allocator) is also theoretically present in CLI, but since the process exits immediately after one comparison, the overlapping memory never gets a chance to cause observable damage. In server mode, the corrupted allocations compound across requests. This dimension bug, however, is fully deterministic — it reproduces 100% of the time in both modes for any image pair where the compare image is larger than the base.

Why the diffPercentage denominator also changes

Counting extra comp pixels in diff_count without changing the denominator produces >100% percentages (e.g. 400% for a 4×4 base vs 8×8 compare). This would break downstream consumers that assume percentage is bounded at 100%.

For example, Argos (a major consumer of odiff) computes score = diffPercentage / 100 and stores it with a database constraint of maximum: 1. A >100% percentage would fail validation and crash. Argos itself avoids this by resizing images to matching dimensions before diffing, but other consumers may not.

The tradeoff: changing the denominator from base.width × base.height to max(base.width, comp.width) × max(base.height, comp.height) (the bounding box) means the percentage changes for different-dimension image pairs. For same-dimension images (the vast majority of real-world usage), the denominator is identical and behavior is unchanged.

Isolated test results (server mode, 100 runs × 11 pairs = 1100 comparisons)

Configuration False matches Rate
No fixes (baseline) 235/1100 21.4%
Dimension fix only (this PR without #169) 136/300 45.3%
ThreadSafeAllocator only (#169 without this PR) 100/1100 9.1%
Both fixes (#169 + this PR) 0/1100 0%

The 100/1100 remaining with ThreadSafeAllocator alone are all from width_change.png (200×200 vs 300×200) — the exact bug this PR fixes. The dimension fix alone does not help with the race condition (still 45.3% on a smaller test set).

Depends on

This PR depends on #169 (thread-safe allocator fix) and should be reviewed/merged after it.

Test plan

  • zig build test-all passes (updated expectations for layout diff tests)
  • CLI correctly reports 20000 different pixels for 200×200 vs 300×200 pair
  • diffPercentage stays ≤100% — uses bounding box denominator
  • Same-dimension images are completely unaffected (denominator unchanged)
  • Consider whether the bounding box denominator is the right semantic choice for the project

@NicoHinderling NicoHinderling force-pushed the 04-09-fix_diff_count_extra_comp_pixels_when_compare_image_is_larger_than_base branch from e9bcf0e to 40ad40b Compare April 10, 2026 20:00
@NicoHinderling NicoHinderling force-pushed the 04-09-fix_diff_count_extra_comp_pixels_when_compare_image_is_larger_than_base branch from 40ad40b to df7dd20 Compare April 10, 2026 21:57
@NicoHinderling
Copy link
Copy Markdown
Contributor Author

Updated the test to use a new white8x4.png fixture (8×4 white image) paired against the existing white4x4.png (4×4 white). The overlap region is identical, so the only differences are the 16 extra comp pixels — this test fails without the fix (match: true) and passes with it (diffCount: 16).

@NicoHinderling
Copy link
Copy Markdown
Contributor Author

NicoHinderling commented Apr 13, 2026

(I can work around this in the short term but it involves me only running in CLI mode until this is merged, but for sure hope we can move back to server mode if this is merged!)

NicoHinderling added a commit to getsentry/sentry that referenced this pull request Apr 13, 2026
…ch bug

odiff server mode has a stdin buffer reuse bug that causes
non-deterministic false match=True results. Switch to CLI mode
(one subprocess per comparison) until the upstream fix lands.

All server-mode code is commented out for easy revert once
dmtrKovalenko/odiff#170 is released
in odiff-bin.

Also derives changed pixel count from the diff mask histogram
instead of parsing odiff stdout, which is more reliable.
NicoHinderling added a commit to getsentry/sentry that referenced this pull request Apr 13, 2026
…ch bug (#112829)

Switch snapshot image comparison from odiff server mode to CLI mode
(one subprocess per comparison) to work around a non-deterministic
false match bug caused by stdin buffer reuse in server mode.

The upstream fix is at dmtrKovalenko/odiff#170.
All server-mode code is commented out (not deleted) for easy revert
once the fix is released in odiff-bin.

Also changes pixel counting from parsing odiff stdout (which varied
across versions) to counting non-zero pixels from the diff mask via
PIL histogram — more reliable and version-independent.
@dmtrKovalenko dmtrKovalenko merged commit 27fd14e into dmtrKovalenko:main Apr 16, 2026
11 checks passed
NicoHinderling added a commit to getsentry/sentry that referenced this pull request Apr 17, 2026
odiff-bin v4.3.8 ships the upstream fix for the server-mode stdin buffer
reuse bug (dmtrKovalenko/odiff#170), so we can
drop the CLI-mode fallback introduced in #112829 and go back to using
server mode.

Also:
- Pin odiff-bin to exact 4.3.8 (no caret) in package.json and CI
- Point _find_odiff_binary() at node_modules/odiff-bin/bin/odiff.exe;
  v4.3.8's post-install only chmods this path, while raw_binaries/ ship
  without the execute bit
- Extract _fetch_batch_images helper in tasks.py to avoid per-iteration
  closure rebinding inside the batch loop

Co-Authored-By: Claude <noreply@anthropic.com>
NicoHinderling added a commit to getsentry/sentry that referenced this pull request Apr 20, 2026
)

Reverts the odiff CLI-mode workaround (#112829) now that odiff-bin
v4.3.8 ships the upstream fix for the server-mode stdin buffer reuse bug
([dmtrKovalenko/odiff#170](dmtrKovalenko/odiff#170)).
Snapshot comparison goes back to using server mode — one persistent
subprocess per batch instead of spawning one per comparison.

### Dependency pin

`odiff-bin` is pinned to exact `4.3.8` (no caret) in `package.json`,
`pnpm-lock.yaml`, and the CI workflow. Exact pin keeps dev / prod / CI
in lockstep with minimal machinery.

### Bonus fix — binary resolution

`_find_odiff_binary()` now points at
`node_modules/odiff-bin/bin/odiff.exe` instead of
`raw_binaries/odiff-<platform>`. v4.3.8's `post_install.js` only sets
the execute bit on `bin/odiff.exe`; the raw binaries ship without `+x`,
which was causing `PermissionError` on fresh installs. Despite the
`.exe` suffix, this is the package's cross-platform entrypoint on all
OSes.

### Minor cleanup

Extracted `_fetch_batch_images` helper in `tasks.py` so the per-batch
closure / lock / cache setup lives in one place instead of being rebuilt
inside every loop iteration.

Co-authored-by: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants