diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index e866a620..282b8c76 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -5,7 +5,7 @@ "plugins": [ { "name": "visualci", - "version": "0.1.26", + "version": "0.1.27", "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/", diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index bb45b61f..12db3312 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "visualci", - "version": "0.1.26", + "version": "0.1.27", "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/", diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.json index 411eef99..dd1ada1c 100644 --- a/.cursor-plugin/plugin.json +++ b/.cursor-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "visualci", "displayName": "VisualCI", - "version": "0.1.26", + "version": "0.1.27", "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" diff --git a/gemini-extension.json b/gemini-extension.json index 28400472..aac4b8dd 100644 --- a/gemini-extension.json +++ b/gemini-extension.json @@ -1,6 +1,6 @@ { "name": "visualci", - "version": "0.1.26", + "version": "0.1.27", "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.", "mcpServers": { "visualci": { diff --git a/server/frontend/content/docs/agents.md b/server/frontend/content/docs/agents.md index 56a59c45..eda4473a 100644 --- a/server/frontend/content/docs/agents.md +++ b/server/frontend/content/docs/agents.md @@ -43,7 +43,7 @@ claude mcp add --transport http visualci https://mcp.visualci.com/mcp \ 4. The agent checks each change against the code and the task: - **Intended:** it calls `review` with `approve`. The approved snapshots become the baseline, and the check turns green. No new commit or CI run is needed. - **A bug:** it fixes the code and pushes again. The next build is compared with the same baseline. -5. `snapshot_history` shows one snapshot across recent builds, which helps find when a page started changing. +5. `snapshot_history` shows one snapshot across recent builds, which helps find when a page started changing. `get_build` marks a change `flaky: true` when its content flipped back and forth over the branch's recent builds: often a test that isn't deterministic rather than a real change (see [Deterministic screenshots](/docs/screenshots/deterministic/)). 6. Before asking for a merge, `get_pr_changeset` (the project and the pull request's number, or a branch) lists everything the pull request adds, changes or removes across all of its builds, against the baseline it forked from, with where review stands for each. A change approved in an earlier build still shows there, so nothing slips through; nothing should be pending or unexpected. The web app shows the same at `///pulls/`. On GitHub, VisualCI keeps one comment on each pull request up to date: each suite's newest build, its counts and review state, before and after images of the changed snapshots, and what was removed. To show images in a comment of your own, use the `embed_url` links `get_snapshot` gives for each page: they work for a week. diff --git a/server/frontend/content/docs/screenshots/deterministic.md b/server/frontend/content/docs/screenshots/deterministic.md index 77119c68..7b3dd779 100644 --- a/server/frontend/content/docs/screenshots/deterministic.md +++ b/server/frontend/content/docs/screenshots/deterministic.md @@ -42,6 +42,10 @@ diff <(cd run1 && find . -name '*.png' | sort | xargs sha256sum) \ Run it in CI's image, since that's where the baselines come from. +VisualCI points at likely suspects too: a change whose content flipped back and forth over its branch's recent builds +(A, B, A, B) is marked **flaky?** on the build page and `flaky: true` in `get_build`. It's only a hint: nothing is +ignored or approved because of it. + ## Fonts Text is most of a screenshot, and fonts are the most common difference between machines. diff --git a/server/frontend/src/api/types.ts b/server/frontend/src/api/types.ts index 6524ce44..ea7d449b 100644 --- a/server/frontend/src/api/types.ts +++ b/server/frontend/src/api/types.ts @@ -298,6 +298,8 @@ export type SnapshotRow = { /** The same new version was already approved on another branch. A hint: it approves nothing here. */ approved_elsewhere: ApprovedElsewhere | null thumbnail_url: string | null + /** Its content flipped back and forth over the branch's recent builds: a hint only. */ + flaky: boolean } export type BuildDetail = BuildSummary & { diff --git a/server/frontend/src/pages/BuildPage.tsx b/server/frontend/src/pages/BuildPage.tsx index 9b67cee1..1199932e 100644 --- a/server/frontend/src/pages/BuildPage.tsx +++ b/server/frontend/src/pages/BuildPage.tsx @@ -484,6 +484,11 @@ function SnapshotItem({ + {s.flaky && ( + + flaky? + + )} {reviewable(build, s) && ( diff --git a/server/src/controllers/ui.rs b/server/src/controllers/ui.rs index c2a0d459..9db033e2 100644 --- a/server/src/controllers/ui.rs +++ b/server/src/controllers/ui.rs @@ -750,6 +750,8 @@ struct SnapshotRow { approved_elsewhere: Option, /// A thumbnail: the first changed page (else the first page). thumbnail_url: Option, + /// A change whose content flipped back and forth over the branch's recent builds (`flaky_in`): a hint only. + flaky: bool, } #[debug_handler] @@ -774,6 +776,7 @@ async fn show_build( let mut elsewhere = crate::models::baselines::approved_elsewhere(&ctx.db, build.id).await?; let ids: Vec = list.iter().filter_map(|s| s.baseline_id).collect(); let notes = crate::models::baselines::local_notes(&ctx.db, &ids).await?; + let flaky = snapshots::Model::flaky_in(&ctx.db, &build).await?; for s in list { let pages = snapshot_pages::Entity::find() .filter(snapshot_pages::Column::SnapshotId.eq(s.id)) @@ -801,6 +804,7 @@ async fn show_build( (d.iter().filter(|l| l.tag == "+").count(), d.iter().filter(|l| l.tag == "-").count()) }) }); + let is_flaky = s.is_change() && flaky.contains(&(s.name.clone(), s.platform.clone())); rows.push(SnapshotRow { format: s.text_format().map(|f| f.as_str().to_string()), lines_added: text_stats.map(|t| t.0), @@ -821,6 +825,7 @@ async fn show_build( change: sizes.get(&s.id).copied(), approved_elsewhere: elsewhere.remove(&s.id), thumbnail_url: thumb, + flaky: is_flaky, }); } let mut summary = serde_json::to_value(BuildSummary::for_viewer(&build, member)).unwrap_or_default(); diff --git a/server/src/mcp.rs b/server/src/mcp.rs index e5813bcf..97ffc1a0 100644 --- a/server/src/mcp.rs +++ b/server/src/mcp.rs @@ -1433,7 +1433,7 @@ impl VisualCi { } #[tool( - description = "A build's status and its snapshots that need attention, 30 at a time (offset and limit page through; longer lists also come grouped by state and name prefix) (changed, added, removed, failed), with how many pages changed in each, the largest change first (order: name to sort by name), and approved_elsewhere when the same new version was already approved on another branch (a hint: it approves nothing). Unchanged snapshots are only counted unless include_unchanged is set. `config` is the visualci.yml the build was sent (null when it had none: the project's settings applied).", + description = "A build's status and its snapshots that need attention, 30 at a time (offset and limit page through; longer lists also come grouped by state and name prefix) (changed, added, removed, failed), with how many pages changed in each, the largest change first (order: name to sort by name), and approved_elsewhere when the same new version was already approved on another branch (a hint: it approves nothing). Unchanged snapshots are only counted unless include_unchanged is set. `config` is the visualci.yml the build was sent (null when it had none: the project's settings applied). `flaky: true` marks a change whose content flipped back and forth over the branch's recent builds: often something that differs between runs (the time, an animation, a late font) rather than a real change, so check it before approving, and make the test deterministic.", annotations(title = "Get build", read_only_hint = true, idempotent_hint = true, open_world_hint = false) )] async fn get_build( @@ -1474,6 +1474,7 @@ impl VisualCi { *groups.entry((s.state.clone(), prefix)).or_default() += 1; } } + let flaky = snapshots::Model::flaky_in(&self.ctx.db, &build).await.map_err(internal)?; let mut items = Vec::new(); for s in listed.into_iter().skip(offset).take(limit) { let attention = s.is_change() || s.state == "failed"; @@ -1501,6 +1502,10 @@ impl VisualCi { "changed_pages": pages.map(|p| p.iter().filter(|p| p.state == "changed").count()), "moved_lines": detail.as_ref().map_or(0, |d| d.moved.len()), }); + if s.is_change() && flaky.contains(&(s.name.clone(), s.platform.clone())) { + // Its content flipped back and forth over the branch's recent builds: a hint, not a verdict. + item["flaky"] = json!(true); + } if let Some(format) = s.text_format() { item["format"] = json!(format.as_str()); if let Some(text) = detail.as_ref().and_then(|d| d.text.as_ref()) { diff --git a/server/src/models/snapshots.rs b/server/src/models/snapshots.rs index 023c5487..d2e6a7fe 100644 --- a/server/src/models/snapshots.rs +++ b/server/src/models/snapshots.rs @@ -529,6 +529,87 @@ impl Model { } Ok(ids) } + + /// The snapshots of this build that look flaky, by name and platform: over the newest builds of its suite on its + /// branch, up to this one ([`FLAKY_WINDOW`]), the snapshot's content changed at least three times and came back + /// to a version it had before (A, B, A, B). Commits on one branch rarely flip a page back and forth, so this + /// usually means something that differs from run to run: the time, an animation, a font that loads late. Only a + /// hint for whoever reviews: nothing is ignored or approved because of it. + /// + /// # Errors + /// On a database error. + pub async fn flaky_in( + db: &DatabaseConnection, + build: &builds::Model, + ) -> ModelResult> { + use super::_entities::builds as b; + let recent: Vec = b::Entity::find() + .select_only() + .column(b::Column::Id) + .filter(b::Column::ProjectId.eq(build.project_id)) + .filter(b::Column::Name.eq(&build.name)) + .filter(b::Column::Branch.eq(&build.branch)) + .filter(b::Column::Id.lte(build.id)) + .order_by_desc(b::Column::Id) + .limit(FLAKY_WINDOW) + .into_tuple() + .all(db) + .await?; + // Three changes need at least four builds. + if recent.len() < 4 { + return Ok(std::collections::HashSet::new()); + } + let rows: Vec<(i64, String, String, String)> = Entity::find() + .select_only() + .columns([snapshots::Column::BuildId, snapshots::Column::Name, snapshots::Column::Platform]) + .column(snapshots::Column::SourceFingerprint) + .filter(snapshots::Column::BuildId.is_in(recent)) + .filter(snapshots::Column::SourceFingerprint.ne("")) + .order_by_asc(snapshots::Column::BuildId) + .into_tuple() + .all(db) + .await?; + let mut history: std::collections::HashMap<(String, String), Vec> = std::collections::HashMap::new(); + for (_, name, platform, fingerprint) in rows { + history.entry((name, platform)).or_default().push(fingerprint); + } + Ok(history.into_iter().filter(|(_, versions)| flips(versions)).map(|(key, _)| key).collect()) + } +} + +/// How many of a branch's newest builds the flaky hint looks at. +pub const FLAKY_WINDOW: u64 = 8; + +/// Whether a run of versions, oldest first, changed at least three times and came back to an earlier version. +fn flips(versions: &[String]) -> bool { + let changes = versions.windows(2).filter(|w| w[0] != w[1]).count(); + let returned = + versions.iter().enumerate().skip(1).any(|(i, v)| versions[i - 1] != *v && versions[..i - 1].contains(v)); + changes >= 3 && returned +} + +#[cfg(test)] +mod flaky_tests { + use super::flips; + + fn run(s: &str) -> Vec { + s.chars().map(String::from).collect() + } + + #[test] + fn flipping_back_and_forth_is_flaky() { + assert!(flips(&run("ABAB"))); + assert!(flips(&run("AABBAAB"))); + assert!(flips(&run("ABCA"))); + } + + #[test] + fn steady_changes_and_one_revert_are_not() { + assert!(!flips(&run("AAAA"))); + assert!(!flips(&run("ABCD")), "changed every build but never came back"); + assert!(!flips(&run("ABA")), "one revert is two changes"); + assert!(!flips(&run("AABBA")), "a change and its revert"); + } } /// Conditions on a baseline row `bl` for exact-match passes: the same suite as the snapshot's build ($5), a CI diff --git a/server/tests/requests/comments.rs b/server/tests/requests/comments.rs index 6baf7eb6..160c5a4b 100644 --- a/server/tests/requests/comments.rs +++ b/server/tests/requests/comments.rs @@ -1,5 +1,6 @@ //! Comment threads: an open thread blocks approving its snapshot until it's resolved (rejecting still works), and -//! threads follow the branch to its newer builds. Also reviewing only a build's added, or only its changed, snapshots. +//! threads follow the branch to its newer builds. Also reviewing only a build's added, or only its changed, snapshots, +//! and the flaky hint, for a snapshot that flips back and forth. //! //! These need the S3-compatible blob store (MinIO on the dev box). Where it can't be reached (CI has none), they print //! why and pass without running. @@ -333,6 +334,33 @@ async fn a_blob_is_confirmed_with_its_real_size() { .await; } +/// A snapshot whose image flips back and forth over a branch's builds is flagged flaky; one that changes steadily, +/// or flips on another branch, isn't. +#[tokio::test] +#[serial] +async fn a_snapshot_that_flips_back_and_forth_is_flagged_flaky() { + request::(|_request, ctx| async move { + let Some(w) = world(&ctx).await else { return }; + w.build("A", "main", None, &[], &[("x", png(RED)), ("y", png(RED))]).await; + let flips = [RED, BLUE, RED, BLUE]; + let steady = [GREEN, BLUE, PURPLE, png_color(10)]; + let mut last = None; + for (i, (f, s)) in flips.iter().zip(steady.iter()).enumerate() { + let commit = format!("F{i}"); + last = Some(w.build(&commit, "feat", Some(5), &["A"], &[("x", png(*f)), ("y", png(*s))]).await); + } + let last = last.unwrap(); + let flaky = snapshots::Model::flaky_in(w.db, &last).await.unwrap(); + assert!(flaky.contains(&("x".to_string(), String::new())), "x went red, blue, red, blue: {flaky:?}"); + assert!(!flaky.contains(&("y".to_string(), String::new())), "y changed every build but never came back"); + + // main has only one build: nothing to go on. + let main = w.build("B", "main", None, &["A"], &[("x", png(BLUE)), ("y", png(RED))]).await; + assert!(snapshots::Model::flaky_in(w.db, &main).await.unwrap().is_empty()); + }) + .await; +} + /// A build that runs its organization out of credits while it processes stops there: it fails with the /// out-of-credits reason, and the renders it queued that nothing else needs are dropped, instead of drawing every page /// of a large build on credit the organization doesn't have. @@ -416,3 +444,7 @@ async fn a_review_can_take_only_the_added_or_only_the_changed_snapshots() { }) .await; } + +fn png_color(v: u8) -> [u8; 3] { + [v, v, v] +} diff --git a/skills/visualci-deterministic/SKILL.md b/skills/visualci-deterministic/SKILL.md index 8d737eb0..398ddd82 100644 --- a/skills/visualci-deterministic/SKILL.md +++ b/skills/visualci-deterministic/SKILL.md @@ -47,6 +47,10 @@ diff <(cd run1 && find . -name '*.png' | sort | xargs sha256sum) \ Run it in CI's image, since that's where the baselines come from. +VisualCI points at likely suspects too: a change whose content flipped back and forth over its branch's recent builds +(A, B, A, B) is marked **flaky?** on the build page and `flaky: true` in `get_build`. It's only a hint: nothing is +ignored or approved because of it. + ## Fonts Text is most of a screenshot, and fonts are the most common difference between machines. diff --git a/skills/visualci-review/SKILL.md b/skills/visualci-review/SKILL.md index e411a8f0..0d00b8da 100644 --- a/skills/visualci-review/SKILL.md +++ b/skills/visualci-review/SKILL.md @@ -48,7 +48,7 @@ claude mcp add --transport http visualci https://mcp.visualci.com/mcp \ 4. The agent checks each change against the code and the task: - **Intended:** it calls `review` with `approve`. The approved snapshots become the baseline, and the check turns green. No new commit or CI run is needed. - **A bug:** it fixes the code and pushes again. The next build is compared with the same baseline. -5. `snapshot_history` shows one snapshot across recent builds, which helps find when a page started changing. +5. `snapshot_history` shows one snapshot across recent builds, which helps find when a page started changing. `get_build` marks a change `flaky: true` when its content flipped back and forth over the branch's recent builds: often a test that isn't deterministic rather than a real change (see [Deterministic screenshots](https://visualci.com/docs/screenshots/deterministic.md)). 6. Before asking for a merge, `get_pr_changeset` (the project and the pull request's number, or a branch) lists everything the pull request adds, changes or removes across all of its builds, against the baseline it forked from, with where review stands for each. A change approved in an earlier build still shows there, so nothing slips through; nothing should be pending or unexpected. The web app shows the same at `///pulls/`. On GitHub, VisualCI keeps one comment on each pull request up to date: each suite's newest build, its counts and review state, before and after images of the changed snapshots, and what was removed. To show images in a comment of your own, use the `embed_url` links `get_snapshot` gives for each page: they work for a week.