Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"plugins": [
{
"name": "visualci",
"version": "0.1.50",
"version": "0.1.51",
"description": "Visual regression and snapshot review for PDFs, screenshots, JSON, HTML and text: VisualCI's MCP server and its skills for each CI provider and test tool.",
"author": { "name": "DocSpring", "url": "https://visualci.com" },
"homepage": "https://visualci.com/docs/skills/",
Expand Down
2 changes: 1 addition & 1 deletion .claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "visualci",
"version": "0.1.50",
"version": "0.1.51",
"description": "Visual regression and snapshot review for PDFs, screenshots, JSON, HTML and text: VisualCI's MCP server and its skills for each CI provider and test tool.",
"author": { "name": "DocSpring", "url": "https://visualci.com" },
"homepage": "https://visualci.com/docs/skills/",
Expand Down
2 changes: 1 addition & 1 deletion .cursor-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "visualci",
"displayName": "VisualCI",
"version": "0.1.50",
"version": "0.1.51",
"description": "Visual regression and snapshot review for PDFs, screenshots, JSON, HTML and text: VisualCI's MCP server and its skills for each CI provider and test tool.",
"author": {
"name": "DocSpring"
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "3"
members = ["crates/*", "app"]

[workspace.package]
version = "0.2.21"
version = "0.2.22"
edition = "2024"
license = "Apache-2.0"
repository = "https://github.com/DocSpring/visualci"
Expand Down
15 changes: 15 additions & 0 deletions app/database/migrations/2026_12_04_000000_masked_page_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! A page whose images differ only inside an ignore rule's masked areas (`region`, `rects`; sc-9018) has the state
//! `masked`: like `within_threshold`, it doesn't make its snapshot changed.

use laravel::db::AnyTransaction;

pub async fn migrate(tx: &mut AnyTransaction<'_>) -> laravel::Result<()> {
tx.query(
"ALTER TABLE snapshot_pages DROP CONSTRAINT snapshot_pages_state_check; \
ALTER TABLE snapshot_pages ADD CONSTRAINT snapshot_pages_state_check CHECK (state = ANY (ARRAY['unchanged', \
'changed', 'ignored', 'variant', 'within_threshold', 'masked']))",
)
.unprepared()
.await?;
Ok(())
}
26 changes: 21 additions & 5 deletions app/src/domain/build_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use globset::GlobMatcher;
use serde::Serialize;
use visualci_core::config::{self, Config, ScopedIgnoreRule, Threshold};
use visualci_core::config::{self, Config, ImageMask, ScopedIgnoreRule, Threshold};
use visualci_core::snapshot_text::{CompiledRules, IgnoreRules};

use crate::models::{Build, Project, Snapshot};
Expand Down Expand Up @@ -81,12 +81,15 @@ impl<'a> BuildSettings<'a> {
(self.source, self.rules.iter().map(|(_, r)| r.clone()).collect())
}

/// Every ignore rule that applies to a snapshot, with its source.
/// Every text ignore rule that applies to a snapshot, with its source.
pub fn applied_rules(&self, snapshot: &Snapshot) -> Vec<AppliedRule> {
let mut out: Vec<AppliedRule> = self
.rules
.iter()
.filter(|(m, _)| m.as_ref().is_some_and(|m| m.is_match(&snapshot.name)))
.filter(|(m, r)| {
m.as_ref().is_some_and(|m| m.is_match(&snapshot.name))
&& !(r.json_paths.is_empty() && r.text_patterns.is_empty())
})
.map(|(_, r)| AppliedRule {
source: self.source,
snapshots: Some(r.snapshots.clone()),
Expand Down Expand Up @@ -123,6 +126,17 @@ impl<'a> BuildSettings<'a> {
out
}

/// The areas of a snapshot's images left out of its diffs: the `region` and `rects` of every rule in force whose
/// glob matches its name. Like text rules, they're the build's own (its file's, else the project's), read when the
/// build is compared or looked at.
pub fn image_mask(&self, snapshot: &Snapshot) -> ImageMask {
let mut out = ImageMask::default();
for (_, r) in self.rules.iter().filter(|(m, _)| m.as_ref().is_some_and(|m| m.is_match(&snapshot.name))) {
out.add(r);
}
out
}

/// The file's threshold for a snapshot name: the longest matching glob's.
fn file_threshold(&self, name: &str) -> Option<f64> {
self.thresholds.iter().filter(|(m, _, _)| m.is_match(name)).max_by_key(|(_, g, _)| g.len()).map(|(_, _, t)| *t)
Expand Down Expand Up @@ -200,8 +214,8 @@ impl<'a> BuildSettings<'a> {
})
}

/// Whether these settings could hide a change the base build's settings would show: an ignore rule (glob and path
/// or pattern) the base didn't have, or a higher threshold for some glob.
/// Whether these settings could hide a change the base build's settings would show: an ignore rule (glob and path,
/// pattern, region or rectangle) the base didn't have, or a higher threshold for some glob.
pub fn hides_more_than(&self, base: &BuildSettings<'_>) -> bool {
let pairs = |s: &BuildSettings<'_>| -> std::collections::HashSet<(String, String)> {
s.rules
Expand All @@ -211,6 +225,8 @@ impl<'a> BuildSettings<'a> {
.iter()
.map(|p| (r.snapshots.clone(), format!("json:{p}")))
.chain(r.text_patterns.iter().map(|p| (r.snapshots.clone(), format!("text:{p}"))))
.chain(r.region.iter().map(|e| (r.snapshots.clone(), format!("region:{e:?}"))))
.chain(r.rects.iter().map(|x| (r.snapshots.clone(), format!("rect:{x:?}"))))
.collect::<Vec<_>>()
})
.collect()
Expand Down
12 changes: 8 additions & 4 deletions app/src/domain/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub async fn compare(
);

let options = settings.diff_options(snapshot);
let mask = settings.image_mask(snapshot);
let (mut changed, mut ignored) = (false, false);
let mut matched_environment: Option<String> = None;
for (position, op) in ops.iter().enumerate() {
Expand All @@ -165,7 +166,7 @@ pub async fn compare(
}
PageOp::Changed { base: b, head: h } => {
let (bp, hp) = (&base_pages[b], &head_pages[h]);
let (state, diff_id, matched) = page_state(snapshot, store, project, h, bp, hp, options).await?;
let (state, diff_id, matched) = page_state(snapshot, store, project, h, bp, hp, options, &mask).await?;
if matched.is_some() {
matched_environment = matched;
}
Expand Down Expand Up @@ -200,7 +201,9 @@ pub async fn compare(
}

/// The state of a page whose fingerprint changed: identical pixels, an accepted variant, an image approved before,
/// within the threshold, an ignored change, or a change. With the diff, and the other environment it matched.
/// within the threshold, different only inside the masked areas, an ignored change, or a change. With the diff, and the
/// other environment it matched.
#[allow(clippy::too_many_arguments)]
async fn page_state(
snapshot: &Snapshot,
store: &BlobStore,
Expand All @@ -209,6 +212,7 @@ async fn page_state(
base: &RenderPage,
head: &RenderPage,
options: visualci_core::diff::DiffOptions,
mask: &visualci_core::config::ImageMask,
) -> laravel::Result<(&'static str, Option<i64>, Option<String>)> {
let (project_id, build_id) = (project.id, snapshot.build_id);
let (name, platform) = (snapshot.name.as_str(), snapshot.platform.as_str());
Expand Down Expand Up @@ -276,9 +280,9 @@ async fn page_state(
let other = (env != snapshot.environment).then_some(env);
return Ok(("variant", None, other));
}
let diff = super::diffs::find_or_compute(store, &base.image, &head.image, options).await?;
let diff = super::diffs::find_or_compute(store, &base.image, &head.image, options, mask).await?;
if !super::diffs::is_change(&diff) {
return Ok(("within_threshold", Some(diff.id), None));
return Ok((super::diffs::unchanged_state(&diff), Some(diff.id), None));
}
if let Some(fingerprint) = &diff.fingerprint {
let is_ignored = IgnoredChange::query()
Expand Down
9 changes: 7 additions & 2 deletions app/src/domain/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub async fn review_detail_against(
store: &BlobStore,
earlier: &Snapshot,
options: visualci_core::diff::DiffOptions,
mask: &visualci_core::config::ImageMask,
context: usize,
) -> laravel::Result<ReviewDetail> {
let heads = async |s: &Snapshot| -> laravel::Result<Vec<Page>> {
Expand Down Expand Up @@ -161,8 +162,12 @@ pub async fn review_detail_against(
let (bp, hp) = (&old[b], &new[h]);
match (&bp.head_image, &hp.head_image) {
(Some(bi), Some(hi)) => {
let diff = super::diffs::find_or_compute(store, bi, hi, options).await?;
let state = if super::diffs::is_change(&diff) { "changed" } else { "within_threshold" };
let diff = super::diffs::find_or_compute(store, bi, hi, options, mask).await?;
let state = if super::diffs::is_change(&diff) {
"changed"
} else {
super::diffs::unchanged_state(&diff)
};
row(position, "changed", state, Some(bp), Some(hp), Some(diff.id))
}
_ => {
Expand Down
31 changes: 26 additions & 5 deletions app/src/domain/diffs.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
//! Image diffs, cached by (base image, head image, options): the same pair is never diffed twice.
//! Image diffs, cached by (base image, head image, options and mask): the same pair is never diffed twice.

use std::io::Cursor;

use laravel::db;
use laravel::model::update;
use visualci_core::config::ImageMask;
use visualci_core::diff::{DiffOptions, DiffResult};

use crate::models::Diff;
use crate::storage::BlobStore;

/// The cache key: the algorithm, the options, and a hash of the mask when there is one.
#[must_use]
pub fn options_key(options: &DiffOptions) -> String {
format!("{}/t{}/aa{}", visualci_core::diff::ALGORITHM, options.threshold, u8::from(options.ignore_anti_aliasing))
pub fn options_key(options: &DiffOptions, mask: &ImageMask) -> String {
let key = format!(
"{}/t{}/aa{}",
visualci_core::diff::ALGORITHM,
options.threshold,
u8::from(options.ignore_anti_aliasing)
);
match mask.key() {
Some(m) => format!("{key}/m{m}"),
None => key,
}
}

#[must_use]
Expand All @@ -24,6 +35,13 @@ pub fn is_change(diff: &Diff) -> bool {
diff.size_changed || diff.changed_pixels > 0
}

/// A page's state when its diff isn't a change: `masked` when it differs only inside the masked areas, else
/// `within_threshold`.
#[must_use]
pub fn unchanged_state(diff: &Diff) -> &'static str {
if result(diff).is_some_and(|r| r.masked_pixels > 0) { "masked" } else { "within_threshold" }
}

async fn find(base_image: &str, head_image: &str, key: &str) -> laravel::Result<Option<Diff>> {
Diff::query()
.filter(Diff::base_image.eq(base_image) & Diff::head_image.eq(head_image) & Diff::options_key.eq(key))
Expand Down Expand Up @@ -53,8 +71,9 @@ pub async fn find_or_compute(
base_image: &str,
head_image: &str,
options: DiffOptions,
mask: &ImageMask,
) -> laravel::Result<Diff> {
let key = options_key(&options);
let key = options_key(&options, mask);
let stale = match find(base_image, head_image, &key).await? {
Some(diff) if diff.overlay_blob.is_some() || result(&diff).is_none_or(|r| r.is_identical()) => {
mark_used(diff.id).await?;
Expand All @@ -63,10 +82,12 @@ pub async fn find_or_compute(
other => other,
};
let (base_bytes, head_bytes) = (store.get(base_image).await?, store.get(head_image).await?);
let mask = mask.clone();
let (outcome, overlay_png) = laravel::task::spawn_blocking(move || -> Result<_, String> {
let base = visualci_core::image_hash::decode(&base_bytes).map_err(|e| e.to_string())?.to_rgba8();
let head = visualci_core::image_hash::decode(&head_bytes).map_err(|e| e.to_string())?.to_rgba8();
let (outcome, overlay) = visualci_core::diff::diff(&base, &head, options);
let masked = mask.resolve(base.width().max(head.width()), base.height().max(head.height()));
let (outcome, overlay) = visualci_core::diff::diff_masked(&base, &head, options, &masked);
let overlay_png = if outcome.is_identical() {
None
} else {
Expand Down
5 changes: 3 additions & 2 deletions app/src/domain/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//!
//! The archive holds `<name>[@<platform>][/page-<n>]/{base,head,diff,regions}.<ext>` (`regions` is the head cropped to
//! the changed regions) and `manifest.json`, which lists every snapshot with its state, pages, changed fraction,
//! regions, files and URL. Snapshots are read through `effective_snapshots`, so a build that reads a group from an
//! earlier build downloads that group's images too.
//! regions, masked areas, files and URL. Snapshots are read through `effective_snapshots`, so a build that reads a
//! group from an earlier build downloads that group's images too.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -437,6 +437,7 @@ async fn write_archive(
"state": p.state,
"changed_fraction": result.as_ref().map(|r| r.changed_fraction),
"regions": result.as_ref().map(|r| &r.regions),
"masked": result.as_ref().map(|r| &r.masked),
"files": written,
}));
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/domain/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ pub async fn change_settings(project: Project, p: SettingsChange) -> laravel::Re
snapshots: r.snapshots.trim().to_string(),
json_paths: clean(r.json_paths),
text_patterns: clean(r.text_patterns),
region: r.region,
rects: r.rects,
})
.collect();
if let Err(e) = visualci_core::config::validate_ignore_rules(&rules) {
Expand Down
18 changes: 17 additions & 1 deletion app/src/forge/pr_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub struct Change {
pub kind: String,
pub before: Option<String>,
pub after: Option<String>,
/// How many areas of the shown page an ignore rule masked: left out of its diff, and named in the row, since the
/// images are shown as they are.
pub masked: usize,
}

/// One suite's newest build for the pull request.
Expand Down Expand Up @@ -132,7 +135,12 @@ pub fn render(suites: &[Suite], request: &str) -> String {
} else {
(img(&c.before), img(&c.after))
};
out.push(format!("| `{}` ({}) | {before} | {after} |", c.name.replace('|', "\\|"), c.state));
let masked = match c.masked {
0 => String::new(),
1 => ", 1 masked area not compared".to_string(),
n => format!(", {n} masked areas not compared"),
};
out.push(format!("| `{}` ({}{masked}) | {before} | {after} |", c.name.replace('|', "\\|"), c.state));
}
if s.more_changes > 0 {
out.push(format!("| and {} more in [the build]({}) | | |", s.more_changes, s.url));
Expand Down Expand Up @@ -232,12 +240,20 @@ pub async fn suites(project: &Project, org: &Organization, pr_number: i32) -> la
.select_as()
.await?;
let page = pages.iter().find(|p| p.state == "changed").or(pages.first());
let masked = match page.and_then(|p| p.diff_id) {
Some(id) => crate::models::Diff::find(id)
.await?
.and_then(|d| crate::domain::diffs::result(&d))
.map_or(0, |r| r.masked.len()),
None => 0,
};
shown.push(Change {
name: s.name.clone(),
state: s.state.clone(),
kind: s.kind.clone(),
before: embed(&store, page.and_then(|p| p.base_image.as_ref())).await?,
after: embed(&store, page.and_then(|p| p.head_image.as_ref())).await?,
masked,
});
}
let removed: Vec<String> = list.iter().filter(|s| s.state == "removed").map(|s| s.name.clone()).collect();
Expand Down
3 changes: 2 additions & 1 deletion app/src/http/ui/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ pub async fn show_snapshot(
let shown = match against {
Some((_, earlier)) => {
let options = settings.diff_options(&snapshot);
detail::review_detail_against(&snapshot, &store, earlier, options, context).await?
let mask = settings.image_mask(&snapshot);
detail::review_detail_against(&snapshot, &store, earlier, options, &mask, context).await?
}
None => detail::review_detail(&snapshot, &store, context).await?,
};
Expand Down
Loading
Loading