diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index e14be583..ea36c27d 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -5,7 +5,7 @@ "plugins": [ { "name": "visualci", - "version": "0.1.43", + "version": "0.1.44", "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 dda1b1b7..8688f7b3 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "visualci", - "version": "0.1.43", + "version": "0.1.44", "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 e3a95810..1420245c 100644 --- a/.cursor-plugin/plugin.json +++ b/.cursor-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "visualci", "displayName": "VisualCI", - "version": "0.1.43", + "version": "0.1.44", "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/Cargo.lock b/Cargo.lock index dc7b8161..7656b01a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5206,7 +5206,7 @@ checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] name = "visualci-cli" -version = "0.2.15" +version = "0.2.16" dependencies = [ "anyhow", "base64 0.22.1", @@ -5223,7 +5223,7 @@ dependencies = [ [[package]] name = "visualci-core" -version = "0.2.15" +version = "0.2.16" dependencies = [ "globset", "hex", @@ -5240,7 +5240,7 @@ dependencies = [ [[package]] name = "visualci-renderer" -version = "0.2.15" +version = "0.2.16" dependencies = [ "anyhow", "image", @@ -5255,7 +5255,7 @@ dependencies = [ [[package]] name = "visualci-wasm" -version = "0.2.15" +version = "0.2.16" dependencies = [ "image", "serde", @@ -5265,7 +5265,7 @@ dependencies = [ [[package]] name = "visualci_server" -version = "0.2.15" +version = "0.2.16" dependencies = [ "aes-gcm", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 47a47e25..87637380 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/*", "server", "server/migration"] [workspace.package] -version = "0.2.15" +version = "0.2.16" edition = "2024" license = "Apache-2.0" repository = "https://github.com/DocSpring/visualci" diff --git a/crates/visualci-cli/src/client.rs b/crates/visualci-cli/src/client.rs index 9aeb917f..c84cd560 100644 --- a/crates/visualci-cli/src/client.rs +++ b/crates/visualci-cli/src/client.rs @@ -163,9 +163,19 @@ impl Client { } /// Finalize a shard (or the whole build). `allow_empty`: the build may have no snapshots on purpose; otherwise - /// one that was sent none fails. - pub fn finalize(&self, id: &str, shard: Option, allow_empty: bool) -> Result { - let body = json!({ "shard": shard, "allow_empty": allow_empty }); + /// one that was sent none fails. `empty_reason` says why (and implies `allow_empty`). + pub fn finalize( + &self, + id: &str, + shard: Option, + allow_empty: bool, + empty_reason: Option<&str>, + ) -> Result { + let mut body = json!({ "shard": shard, "allow_empty": allow_empty }); + // Only when there's one: a server from before empty_reason refuses a field it doesn't know + if let Some(reason) = empty_reason { + body["empty_reason"] = json!(reason); + } Ok(serde_json::from_value(self.post(&format!("/api/v1/builds/{id}/finalize"), &body)?)?) } diff --git a/crates/visualci-cli/src/main.rs b/crates/visualci-cli/src/main.rs index f19e1341..d75c9e8f 100644 --- a/crates/visualci-cli/src/main.rs +++ b/crates/visualci-cli/src/main.rs @@ -259,6 +259,10 @@ enum BuildCommand { /// sent none fails. #[arg(long)] allow_empty: bool, + /// Why the build has no snapshots, like "inputs unchanged": implies --allow-empty, and the build's summary + /// (its check and its page) shows it with the build its baseline came from. + #[arg(long)] + empty_reason: Option, #[command(flatten)] connection: Connection, }, @@ -495,10 +499,18 @@ fn run() -> Result<()> { } println!("{}", build.id); } - Command::Build(BuildCommand::Finalize { build, shard, wait, timeout, allow_empty, connection }) => { + Command::Build(BuildCommand::Finalize { + build, + shard, + wait, + timeout, + allow_empty, + empty_reason, + connection, + }) => { let client = Client::from_env(connection.api_url, connection.token, connection.project)?; let id = build_id(build)?; - let mut build = client.finalize(&id, shard, allow_empty)?; + let mut build = client.finalize(&id, shard, allow_empty, empty_reason.as_deref())?; if wait { build = client.wait(&id, Duration::from_secs(timeout))?; } @@ -696,7 +708,7 @@ fn run() -> Result<()> { } } // Finalized whatever happened to the tests: a run that sent nothing (it failed before any snapshot) too. - let mut build = client.finalize(&build.id, None, true)?; + let mut build = client.finalize(&build.id, None, true, None)?; if !no_wait { build = client.wait(&build.id, Duration::from_secs(timeout))?; } @@ -714,7 +726,7 @@ fn run() -> Result<()> { } send_snapshots(&client, &build.id, &snapshots)?; eprintln!("waiting for build #{}", build.number); - client.finalize(&build.id, None, false)?; + client.finalize(&build.id, None, false, None)?; let build = client.wait(&build.id, Duration::from_secs(timeout))?; print_build(&client, &build)?; if build.blocked_message().is_some() { diff --git a/gemini-extension.json b/gemini-extension.json index e09f73da..d4167084 100644 --- a/gemini-extension.json +++ b/gemini-extension.json @@ -1,6 +1,6 @@ { "name": "visualci", - "version": "0.1.43", + "version": "0.1.44", "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/ci/circleci.md b/server/frontend/content/docs/ci/circleci.md index fa294361..911a83b8 100644 --- a/server/frontend/content/docs/ci/circleci.md +++ b/server/frontend/content/docs/ci/circleci.md @@ -45,6 +45,8 @@ The build is compared once every shard has finalized. The [Ruby helpers](/docs/s If some containers can be skipped (for example, tests split by changed files), add `--subset` when creating the build, so snapshots that weren't sent aren't counted as removed. +When a pipeline skips every job that sends a build's snapshots (a cached job whose inputs didn't change, or a docs-only push), the job that finalizes it still runs, and a build that was sent nothing fails. Finalize that build with `--allow-empty` instead: its snapshots are carried forward from the nearest ancestor, its check passes, and the check and the build's page read "No snapshots this run: baseline from #N". Only do this for jobs the pipeline really skipped: a job that ran and sent nothing should still fail. + Snapshot names must be unique across the build's jobs and containers. Sending the same file under a name again is fine (a job that's run again does it), but two containers sending different content under one name is refused with a 409 that names the snapshot: give each test's snapshot a name of its own. ## Orb diff --git a/server/frontend/content/docs/reference/api.md b/server/frontend/content/docs/reference/api.md index da2772d7..04723e1f 100644 --- a/server/frontend/content/docs/reference/api.md +++ b/server/frontend/content/docs/reference/api.md @@ -57,7 +57,7 @@ A name (with its platform) is one snapshot per build. Registering it again with ## Finalize -`POST /api/v1/builds/{id}/finalize`, with an empty body or `{"shard": 0}`. VisualCI compares the build once every shard has finalized. The response is the build. A build that was sent no snapshots fails; send `"allow_empty": true` when it's empty on purpose. +`POST /api/v1/builds/{id}/finalize`, with an empty body or `{"shard": 0}`. VisualCI compares the build once every shard has finalized. The response is the build. A build that was sent no snapshots fails; send `"allow_empty": true` when it's empty on purpose, or `"empty_reason": "inputs unchanged"`, which implies it and puts the reason in the build's summary with the build its baseline came from. ## Read a build diff --git a/server/migration/src/lib.rs b/server/migration/src/lib.rs index b06595e5..fc91132a 100644 --- a/server/migration/src/lib.rs +++ b/server/migration/src/lib.rs @@ -47,6 +47,7 @@ mod m20261202_000040_closed_builds; mod m20261202_000041_build_creator; mod m20261202_000042_build_groups; mod m20261203_000046_waitlist; +mod m20261203_000047_empty_reason; pub struct Migrator; @@ -100,6 +101,7 @@ impl MigratorTrait for Migrator { Box::new(m20261202_000041_build_creator::Migration), Box::new(m20261202_000042_build_groups::Migration), Box::new(m20261203_000046_waitlist::Migration), + Box::new(m20261203_000047_empty_reason::Migration), // inject-above (do not remove this comment) ] } diff --git a/server/migration/src/m20261203_000047_empty_reason.rs b/server/migration/src/m20261203_000047_empty_reason.rs new file mode 100644 index 00000000..f9b7670d --- /dev/null +++ b/server/migration/src/m20261203_000047_empty_reason.rs @@ -0,0 +1,24 @@ +//! Why a build was finished with no snapshots of its own on purpose (`builds.empty_reason`, "inputs unchanged" for a +//! CI run that skipped a suite's jobs), shown in its summary with the build its baseline came from. +use sea_orm_migration::prelude::*; + +pub struct Migration; + +impl MigrationName for Migration { + fn name(&self) -> &str { + "m20261203_000047_empty_reason" + } +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> { + m.get_connection().execute_unprepared("ALTER TABLE builds ADD COLUMN empty_reason TEXT;").await?; + Ok(()) + } + + async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> { + m.get_connection().execute_unprepared("ALTER TABLE builds DROP COLUMN empty_reason;").await?; + Ok(()) + } +} diff --git a/server/src/controllers/api.rs b/server/src/controllers/api.rs index 73861f71..8dba9b57 100644 --- a/server/src/controllers/api.rs +++ b/server/src/controllers/api.rs @@ -311,6 +311,9 @@ struct FinalizeParams { /// The build may have no snapshots on purpose; otherwise one that was sent none fails. #[serde(default)] allow_empty: bool, + /// Why it has none ("inputs unchanged"): implies allow_empty, and shows in the build's summary. + #[serde(default)] + empty_reason: Option, } #[debug_handler] @@ -332,7 +335,12 @@ async fn finalize( }; let shard = params.shard; let was_receiving = build.state == "receiving"; - let build = build.finalize_shard(&ctx.db, shard, params.allow_empty).await.map_err(model_error)?; + let build = match params.empty_reason.as_deref() { + Some(reason) => build.allow_empty_because(&ctx.db, reason).await.map_err(model_error)?, + None => build, + }; + let allow_empty = params.allow_empty || params.empty_reason.is_some(); + let build = build.finalize_shard(&ctx.db, shard, allow_empty).await.map_err(model_error)?; let build = if was_receiving { build.stop_if_out_of_credits(&ctx).await? } else { build }; if was_receiving && build.state == "processing" { ProcessBuildWorker::perform_later(&ctx, ProcessBuildWorkerArgs { build_id: build.id }).await?; diff --git a/server/src/mcp.rs b/server/src/mcp.rs index ba11153f..190365db 100644 --- a/server/src/mcp.rs +++ b/server/src/mcp.rs @@ -471,6 +471,9 @@ pub struct FinalizeArgs { pub shard: Option, /// True when the build may have no snapshots on purpose. Otherwise a build that was sent none fails. pub allow_empty: Option, + /// Why it has none, like "inputs unchanged" (the CI run skipped the tests): implies allow_empty, and shows in the + /// build's summary with the build its baseline came from. + pub empty_reason: Option, } // Standard Support level 1 (standardsupport.dev): the schema names every field and its choices, with no field @@ -2400,7 +2403,7 @@ impl VisualCi { } #[tool( - description = "Finish a build (or one shard of it): VisualCI renders and compares what was uploaded. Then poll `get_build` until its state is completed. A build that was sent no snapshots fails, unless allow_empty is true.", + description = "Finish a build (or one shard of it): VisualCI renders and compares what was uploaded. Then poll `get_build` until its state is completed. A build that was sent no snapshots fails, unless allow_empty is true or empty_reason says why it has none (\"inputs unchanged\").", annotations( title = "Finalize build", read_only_hint = false, @@ -2421,7 +2424,15 @@ impl VisualCi { return Ok(tool_error(format!("no build {} in this project", args.build))); }; let was_receiving = build.state == "receiving"; - let build = match build.finalize_shard(&self.ctx.db, args.shard, args.allow_empty.unwrap_or(false)).await { + let build = match args.empty_reason.as_deref() { + Some(reason) => match build.allow_empty_because(&self.ctx.db, reason).await { + Ok(b) => b, + Err(e) => return Ok(tool_error(e.to_string())), + }, + None => build, + }; + let allow_empty = args.allow_empty.unwrap_or(false) || args.empty_reason.is_some(); + let build = match build.finalize_shard(&self.ctx.db, args.shard, allow_empty).await { Ok(b) => b, Err(e) => return Ok(tool_error(e.to_string())), }; diff --git a/server/src/models/_entities/builds.rs b/server/src/models/_entities/builds.rs index 366874fa..3b2f3b90 100644 --- a/server/src/models/_entities/builds.rs +++ b/server/src/models/_entities/builds.rs @@ -65,6 +65,7 @@ pub struct Model { pub manifest_same_as_build_id: Option, /// The person whose token made the build, if one did (not a project token). pub created_by_user_id: Option, + pub empty_reason: Option, pub finalized_at: Option, pub completed_at: Option, pub created_at: DateTimeWithTimeZone, diff --git a/server/src/models/builds.rs b/server/src/models/builds.rs index 7566b9b5..8667ac48 100644 --- a/server/src/models/builds.rs +++ b/server/src/models/builds.rs @@ -93,6 +93,9 @@ impl Default for Via { } } +/// The longest reason a build may give for having no snapshots on purpose. +pub const EMPTY_REASON_MAX: usize = 200; + /// Why a build that was sent no snapshots failed. pub const EMPTY_BUILD: &str = "No snapshots were sent, so nothing was compared. Check the paths the upload was given \ (or visualci.yml's uploads), or finalize with --allow-empty when an empty build is on \ @@ -387,6 +390,27 @@ impl Model { Ok(out) } + /// Let the build finish with no snapshots of its own, and say why ("inputs unchanged": the CI run skipped the + /// suite's jobs). Its summary shows the reason and the build its baseline came from. Only while it's receiving. + /// + /// # Errors + /// `Message` for a reason that's empty or longer than [`EMPTY_REASON_MAX`] characters. + pub async fn allow_empty_because(self, db: &DatabaseConnection, reason: &str) -> ModelResult { + let reason = reason.trim(); + if reason.is_empty() || reason.chars().count() > EMPTY_REASON_MAX { + return Err(ModelError::Message(format!( + "empty_reason must be 1 to {EMPTY_REASON_MAX} characters, like \"inputs unchanged\"" + ))); + } + if self.state != "receiving" { + return Ok(self); + } + let mut active = self.into_active_model(); + active.allow_empty = Set(true); + active.empty_reason = Set(Some(reason.to_string())); + Ok(active.update(db).await?) + } + /// Mark a shard finished. Returns the build, now `processing` when every shard has finished. /// /// # Errors diff --git a/server/src/summary.rs b/server/src/summary.rs index 3e1dec79..d6ea9f12 100644 --- a/server/src/summary.rs +++ b/server/src/summary.rs @@ -24,6 +24,12 @@ pub async fn summary_line(db: &DatabaseConnection, build: &builds::Model) -> Res } let all = snapshots::Model::for_build(db, build.id).await?; let mut parts = Vec::new(); + // Finished with nothing of its own on purpose (a CI run that skipped the suite's jobs): say so, and where its + // baseline came from, rather than leave "0 unchanged" to read like something broke + let sent = all.iter().any(|s| !s.carried_forward && s.state != "removed" && s.platform != CONFIG_PLATFORM); + if build.allow_empty && !sent && build.conclusion.as_deref() != Some("failed") { + parts.push(nothing_sent(db, build).await?); + } if all.iter().any(|s| s.platform == CONFIG_PLATFORM && s.is_change()) { parts.push("visualci.yml could hide changes".to_string()); } @@ -44,6 +50,17 @@ pub async fn summary_line(db: &DatabaseConnection, build: &builds::Model) -> Res Ok((!parts.is_empty()).then(|| parts.join("; "))) } +/// "No snapshots this run (inputs unchanged): baseline from #12" +async fn nothing_sent(db: &DatabaseConnection, build: &builds::Model) -> Result { + let reason = build.empty_reason.as_deref().map(|r| format!(" ({r})")).unwrap_or_default(); + let base = match build.base_build_id { + Some(id) => builds::Entity::find_by_id(id).one(db).await?.map(|b| b.number), + None => None, + }; + let baseline = base.map_or_else(|| "no baseline yet".to_string(), |n| format!("baseline from #{n}")); + Ok(format!("No snapshots this run{reason}: {baseline}")) +} + fn display_name(s: &snapshots::Model) -> String { if s.platform.is_empty() { s.name.clone() } else { format!("{} ({})", s.name, s.platform) } } diff --git a/server/tests/requests/api.rs b/server/tests/requests/api.rs index 4ee78d99..0beef5a9 100644 --- a/server/tests/requests/api.rs +++ b/server/tests/requests/api.rs @@ -121,6 +121,39 @@ async fn the_api_reads_numbers_sent_as_strings() { .await; } +/// A finalize that says why the build has no snapshots ("inputs unchanged") lets it be empty and keeps the reason +/// for its summary (sc-8866). +#[tokio::test] +#[serial] +async fn finalize_takes_a_reason_for_having_no_snapshots() { + request::(|request, ctx| async move { + let auth = format!("Bearer {}", project_token(&ctx).await); + let mut params = build_params(None); + params["subset"] = serde_json::json!(true); + let r = request.post("/api/v1/builds").add_header("authorization", auth.clone()).json(¶ms).await; + let id = r.json::()["id"].as_str().unwrap().to_string(); + let r = request + .post(&format!("/api/v1/builds/{id}/finalize")) + .add_header("authorization", auth) + .json(&serde_json::json!({ "empty_reason": "inputs unchanged" })) + .await; + assert_eq!(r.status_code(), 200, "{}", r.text()); + let saved = { + use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; + let pid: uuid::Uuid = id.parse().unwrap(); + visualci_server::models::_entities::builds::Entity::find() + .filter(visualci_server::models::_entities::builds::Column::Pid.eq(pid)) + .one(&ctx.db) + .await + .unwrap() + .unwrap() + }; + assert!(saved.allow_empty); + assert_eq!(saved.empty_reason.as_deref(), Some("inputs unchanged")); + }) + .await; +} + #[tokio::test] #[serial] async fn shards_with_the_same_nonce_share_a_build() { diff --git a/server/tests/requests/baselines.rs b/server/tests/requests/baselines.rs index 761ade6f..f8f629d3 100644 --- a/server/tests/requests/baselines.rs +++ b/server/tests/requests/baselines.rs @@ -504,6 +504,32 @@ async fn a_build_sent_no_snapshots_fails_unless_it_may_be_empty() { .await; } +// A CI run that skipped a suite's jobs (their inputs didn't change) finishes its build empty on purpose, and says why +#[tokio::test] +#[serial] +async fn a_build_empty_on_purpose_says_why_and_where_its_baseline_came_from() { + request::(|_request, ctx| async move { + let Some(w) = world(&ctx).await else { return }; + let a = w.build("A", "main", &[], "ci", &[("x", File::Image(RED))]).await; + let params: builds::CreateParams = serde_json::from_value(serde_json::json!({ + "commit": "B", "branch": "main", "parent_commits": ["A"], "subset": true, + })) + .unwrap(); + let build = builds::Model::create(w.db, &w.project, ¶ms).await.unwrap(); + assert!(build.clone().allow_empty_because(w.db, " ").await.is_err(), "a reason says something"); + let build = build.allow_empty_because(w.db, "inputs unchanged").await.unwrap(); + let build = build.finalize_shard(w.db, None, true).await.unwrap(); + let id = build.id; + assert!(build.process(w.db, &w.store, "test").await.unwrap()); + let b = builds::Model::find_by_id(w.db, id).await.unwrap(); + assert_eq!(b.conclusion.as_deref(), Some("unchanged"), "{:?}", b.error); + assert_eq!(w.states(&b).await, st(&[("x", "unchanged")]), "carried from A"); + let line = visualci_server::summary::summary_line(w.db, &b).await.unwrap().unwrap(); + assert_eq!(line, format!("No snapshots this run (inputs unchanged): baseline from #{}", a.number)); + }) + .await; +} + #[tokio::test] #[serial] async fn approving_a_build_supersedes_the_older_pending_builds_it_covers() { diff --git a/skills/visualci-circleci/SKILL.md b/skills/visualci-circleci/SKILL.md index 7b6b8119..048eee83 100644 --- a/skills/visualci-circleci/SKILL.md +++ b/skills/visualci-circleci/SKILL.md @@ -50,6 +50,8 @@ The build is compared once every shard has finalized. The [Ruby helpers](https:/ If some containers can be skipped (for example, tests split by changed files), add `--subset` when creating the build, so snapshots that weren't sent aren't counted as removed. +When a pipeline skips every job that sends a build's snapshots (a cached job whose inputs didn't change, or a docs-only push), the job that finalizes it still runs, and a build that was sent nothing fails. Finalize that build with `--allow-empty` instead: its snapshots are carried forward from the nearest ancestor, its check passes, and the check and the build's page read "No snapshots this run: baseline from #N". Only do this for jobs the pipeline really skipped: a job that ran and sent nothing should still fail. + Snapshot names must be unique across the build's jobs and containers. Sending the same file under a name again is fine (a job that's run again does it), but two containers sending different content under one name is refused with a 409 that names the snapshot: give each test's snapshot a name of its own. ## Orb