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.55",
"version": "0.1.56",
"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.55",
"version": "0.1.56",
"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.55",
"version": "0.1.56",
"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
15 changes: 15 additions & 0 deletions app/database/migrations/2026_12_06_000000_covered_builds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! A CI build finalized with no snapshots, on a commit an earlier build of its suite already compared (a rerun of only
//! the failed jobs: sc-9044), follows that build: `covered_by_build_id` names it, and the empty build shows its result.

use laravel::db::AnyTransaction;

pub async fn migrate(tx: &mut AnyTransaction<'_>) -> laravel::Result<()> {
tx.query(
"ALTER TABLE builds ADD COLUMN covered_by_build_id bigint REFERENCES builds(id) ON DELETE SET NULL; \
CREATE INDEX builds_covered_by_idx ON builds USING btree (covered_by_build_id) \
WHERE covered_by_build_id IS NOT NULL",
)
.unprepared()
.await?;
Ok(())
}
3 changes: 2 additions & 1 deletion app/src/domain/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub async fn create(project: &Project, params: &CreateParams, creator: Option<i6
group_depth: None,
manifest_same_as_build_id: None,
empty_reason: None,
covered_by_build_id: None,
})
.await?;
Ok((build, true))
Expand Down Expand Up @@ -484,7 +485,7 @@ pub async fn reopen_branch(project_id: i64, branch: &str) -> laravel::Result<usi
warnings = COALESCE((SELECT jsonb_agg(w) FROM jsonb_array_elements(warnings) w \
WHERE w NOT IN (to_jsonb({pull_request}::TEXT), to_jsonb({deleted}::TEXT))), '[]'::jsonb) \
WHERE id = (SELECT id FROM builds WHERE project_id = {project_id} AND branch = {branch} \
AND review_state = 'closed' ORDER BY id DESC LIMIT 1) \
AND review_state = 'closed' AND covered_by_build_id IS NULL ORDER BY id DESC LIMIT 1) \
RETURNING id"
)
.scalar()
Expand Down
1 change: 1 addition & 0 deletions app/src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod pages;
pub mod processing;
pub mod projects;
pub mod renders;
pub mod reruns;
pub mod retention;
pub mod reviews;
pub mod snapshots;
Expand Down
5 changes: 5 additions & 0 deletions app/src/domain/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,11 @@ pub async fn process(build: Build, store: &BlobStore, renderer_version: &str) ->
let id = build.id;
// The "visualci.yml" item is worked out again below.
db::query!("DELETE FROM snapshots WHERE build_id = {id} AND platform = 'visualci'").delete().await?;
// A CI build sent nothing, on a commit an earlier build of its suite compared (a rerun of only the failed jobs):
// it shows that build's result rather than failing as empty.
if let Some(done) = Box::pin(super::reruns::follow(&build)).await? {
return Ok(done);
}
let mut snapshots: Vec<Snapshot> = Snapshot::query()
.filter(Snapshot::build_id.eq(id))
.order_by(Snapshot::name.asc())
Expand Down
192 changes: 192 additions & 0 deletions app/src/domain/reruns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
//! Reruns of only the failed jobs (sc-9044). CircleCI's "rerun from failed" starts a new workflow, and GitHub Actions'
//! "re-run failed jobs" a new run attempt, in which only the failed jobs run again. A CI that keys its builds on the
//! workflow or the attempt creates a new build for every suite, and a suite whose jobs passed the first time sends it
//! nothing. Such a build mustn't fail with "No snapshots were sent": an earlier build of its suite already compared this
//! commit. It follows that build instead (`covered_by_build_id`): it concludes with its conclusion and review state, its
//! check shows that build's result, and a later review of that build reaches it.
//!
//! A build follows only when it's a CI build that was sent no snapshots (`--allow-empty` or not), and an earlier CI build
//! of the same project, suite (`name`), environment, commit and branch has been finalized and wasn't itself empty. While
//! that build is still processing, the empty one waits, processing, and concludes when it does.

use laravel::db;
use laravel::model::update;

use super::builds::EMPTY_BUILD;
use crate::models::Build;

/// The warning on a build that follows an earlier one.
#[must_use]
pub fn covered_warning(number: i32) -> String {
format!(
"No snapshots were sent in this run; build #{number} already covers this commit (a rerun of failed jobs?), so \
this build shows its result."
)
}

/// How many snapshots the build was sent itself (not carried forward, not counted as removed, not the visualci.yml
/// item).
async fn sent(build_id: i64) -> laravel::Result<i64> {
let id = build_id;
Ok(db::query!(
"SELECT COUNT(*) FROM snapshots WHERE build_id = {id} AND NOT carried_forward AND state <> 'removed' \
AND platform <> 'visualci'"
)
.scalar()
.await?
.unwrap_or(0))
}

/// The earlier build this empty CI build would follow: the newest earlier CI build of the same project, suite,
/// environment, commit and branch that was finalized (processing or completed), wasn't empty itself, and follows no
/// build itself. The branch counts: the same commit on another branch compares with other baselines and has its own
/// review rules, so its result isn't this build's.
///
/// # Errors
/// On a database error.
pub async fn covering(build: &Build) -> laravel::Result<Option<Build>> {
if build.mode != "ci" {
return Ok(None);
}
let (id, project_id, name, environment, sha, branch) = (
build.id,
build.project_id,
build.name.as_str(),
build.environment.as_str(),
build.commit_sha.as_str(),
build.branch.as_str(),
);
let found: Vec<Build> = db::query!(
"SELECT * FROM builds WHERE project_id = {project_id} AND name = {name} AND environment = {environment} \
AND commit_sha = {sha} AND branch = {branch} AND mode = 'ci' AND id < {id} \
AND covered_by_build_id IS NULL AND state IN ('processing', 'completed') \
AND error IS DISTINCT FROM {EMPTY_BUILD} \
ORDER BY id DESC LIMIT 1"
)
.select_as()
.await?;
Ok(found.into_iter().next())
}

/// Processing's first step for a CI build: when it was sent no snapshots and an earlier build covers its commit, it
/// follows that build. `None`: it doesn't, so it's processed as usual. `Some(false)`: the earlier build is still
/// processing, and this one waits for it. `Some(true)`: it concluded with the earlier build's result.
///
/// # Errors
/// On a database error.
pub async fn follow(build: &Build) -> laravel::Result<Option<bool>> {
if build.mode != "ci" {
return Ok(None);
}
let earlier = match build.covered_by_build_id {
Some(id) => Build::find(id).await?,
None if sent(build.id).await? == 0 => covering(build).await?,
None => None,
};
let Some(earlier) = earlier else { return Ok(None) };
if !matches!(earlier.state.as_str(), "completed" | "failed") {
if build.covered_by_build_id.is_none() {
let mut build = build.clone();
let warnings = with_warning(&build, earlier.number);
update!(build { covered_by_build_id: Some(earlier.id), warnings }).await?;
}
return Ok(Some(false));
}
mirror(build.id, &earlier).await?;
Ok(Some(true))
}

fn with_warning(build: &Build, number: i32) -> serde_json::Value {
let mut warnings: Vec<String> = serde_json::from_value(build.warnings.clone()).unwrap_or_default();
let warning = covered_warning(number);
if !warnings.contains(&warning) {
warnings.push(warning);
}
serde_json::json!(warnings)
}

/// Give the build the earlier build's result: completed, with its conclusion, review state and superseding build (a
/// failed earlier build fails it). Under the build's advisory lock; only a build still processing, or one that already
/// follows `earlier`. Returns whether anything changed.
///
/// # Errors
/// On a database error.
pub async fn mirror(build_id: i64, earlier: &Build) -> laravel::Result<bool> {
Box::pin(db::transaction(async |_| {
super::processing::lock(build_id).await?;
let mut build = Build::find_or_fail(build_id).await?;
let follows = build.state == "processing" || build.covered_by_build_id == Some(earlier.id);
if !follows || build.mode != "ci" {
return Ok(false);
}
let failed = earlier.state == "failed" || earlier.conclusion.as_deref() == Some("failed");
let conclusion = if failed { Some("failed".to_string()) } else { earlier.conclusion.clone() };
let review_state = if failed { "none".to_string() } else { earlier.review_state.clone() };
let error = failed.then(|| match &earlier.error {
Some(e) => format!("Build #{} failed: {e}", earlier.number),
None => format!("Build #{} failed: see that build", earlier.number),
});
let superseded_by_build_id = earlier.superseded_by_build_id;
let covered_by_build_id = Some(earlier.id);
let warnings = with_warning(&build, earlier.number);
let unchanged = build.state == "completed"
&& build.conclusion == conclusion
&& build.review_state == review_state
&& build.error == error
&& build.superseded_by_build_id == superseded_by_build_id
&& build.covered_by_build_id == covered_by_build_id
&& build.warnings == warnings;
if unchanged {
return Ok(false);
}
let completed_at = build.completed_at.or_else(|| Some(laravel::now().timestamp()));
update!(build {
state: "completed",
conclusion,
review_state,
error,
superseded_by_build_id,
covered_by_build_id,
warnings,
completed_at
})
.await?;
Ok(true)
}))
.await
}

/// A build changed (it concluded, was reviewed, superseded, closed or reopened): the builds that follow it take its
/// result, and their checks, comments and pages follow (`BuildChanged`).
///
/// # Errors
/// On a database or queue error.
pub async fn update_followers(build_id: i64) -> laravel::Result<usize> {
let id = build_id;
let followers: Vec<i64> =
db::query!("SELECT id FROM builds WHERE covered_by_build_id = {id} ORDER BY id").select_as().await?;
if followers.is_empty() {
return Ok(0);
}
let Some(earlier) = Build::find(id).await? else { return Ok(0) };
if !matches!(earlier.state.as_str(), "completed" | "failed") {
return Ok(0);
}
let mut changed = 0;
for follower in followers {
if mirror(follower, &earlier).await? {
changed += 1;
crate::events::BuildChanged { build_id: follower }.dispatch().await?;
}
}
Ok(changed)
}

/// Why a build that follows another can't be reviewed itself.
#[must_use]
pub fn not_reviewable(number: i32) -> String {
format!(
"This build was sent no snapshots: it shows the result of build #{number}, which covers the same commit. Review \
build #{number} instead."
)
}
4 changes: 4 additions & 0 deletions app/src/domain/reviews.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ pub async fn review(
if build.state != "completed" {
return Ok(Err("only completed builds can be reviewed".into()));
}
if let Some(id) = build.covered_by_build_id {
let number = Build::find(id).await?.map(|b| b.number).unwrap_or_default();
return Ok(Err(super::reruns::not_reviewable(number)));
}
let verdict = match action {
"approve" => "approved",
"reject" => "rejected",
Expand Down
24 changes: 23 additions & 1 deletion app/src/forge/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,33 @@ pub fn check_state(build: &Build, project: &Project, superseded_by: Option<i32>)
}
}

/// The whole check for a CI build: its state, the counts, who reviewed it, what changed, and how to review it.
/// The whole check for a CI build: its state, the counts, who reviewed it, what changed, and how to review it. A build
/// that follows an earlier one (it was sent no snapshots in a rerun of only the failed jobs: `domain::reruns`) shows
/// that build's check, so the suite's newest check on the commit is that build's result, not "No snapshots were sent".
///
/// # Errors
/// On a database error.
pub async fn check_for(build: &Build, project: &Project, org: &Organization) -> laravel::Result<Check> {
let earlier = match build.covered_by_build_id {
Some(id) => Build::find(id).await?,
None => None,
};
let shown = earlier.as_ref().unwrap_or(build);
let mut check = Box::pin(own_check(shown, project, org)).await?;
if let Some(earlier) = &earlier {
check.title = format!("{} (build #{})", check.title, earlier.number).chars().take(120).collect();
check.summary = format!(
"No snapshots were sent in build #{} (a rerun of failed jobs?). Build #{} already covers this commit, so \
this check shows its result.\n\n{}",
build.number, earlier.number, check.summary
);
}
check.name = check_name(&build.name);
check.head_sha.clone_from(&build.commit_sha);
Ok(check)
}

async fn own_check(build: &Build, project: &Project, org: &Organization) -> laravel::Result<Check> {
let superseded_by = match build.superseded_by_build_id {
Some(id) => Build::find(id).await?.map(|b| b.number),
None => None,
Expand Down
2 changes: 1 addition & 1 deletion app/src/forge/pr_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ pub async fn suites(project: &Project, org: &Organization, pr_number: i32) -> la
let project_id = project.id;
let all: Vec<Build> = db::query!(
"SELECT * FROM builds WHERE project_id = {project_id} AND pr_number = {pr_number} AND mode = 'ci' \
ORDER BY id DESC"
AND covered_by_build_id IS NULL ORDER BY id DESC"
)
.select_as()
.await?;
Expand Down
7 changes: 7 additions & 0 deletions app/src/listeners/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub async fn tell_the_live_streams(event: Event<crate::events::BuildChanged>) ->
Ok(())
}

/// A build changed: the empty builds that follow it (reruns of only the failed jobs, `domain::reruns`) take its result.
#[listener]
pub async fn update_the_builds_that_follow_it(event: Event<crate::events::BuildChanged>) -> laravel::Result<()> {
crate::domain::reruns::update_followers(event.build_id).await?;
Ok(())
}

/// A default-branch CI build changed: Cloudflare's copies of the gallery URLs it touches are purged in the background,
/// when purges are configured.
#[listener]
Expand Down
3 changes: 3 additions & 0 deletions app/src/models/builds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ pub struct Build {
pub group_depth: Option<i32>,
pub manifest_same_as_build_id: Option<i64>,
pub empty_reason: Option<String>,
/// The earlier build of the same suite and commit whose result this one shows: it was finalized with no
/// snapshots (a rerun of only the failed jobs), see `domain::reruns`.
pub covered_by_build_id: Option<i64>,
}
Loading
Loading