use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use vitrify_core::audit;
use vitrify_core::cascade::{Owner, Ownership};
use vitrify_core::graph::{self as unit_graph, Node};
use vitrify_core::journal::{self, Event};
use vitrify_core::model::{self, Instance, Shape, UtcTime};
use vitrify_core::writeset::{Violation, WriteSet};
use vitrify_core::{CoreError, ledger, trace, yamlish};
use super::mainline::{LaneLock, Mainline, append_event};
use super::progress::GateBoard;
use crate::cmd::{self, claim};
use crate::gate::{self, Ran, ShRunner};
use crate::git::Git;
use crate::incremental::blame::{self, Judged, PristineCheckout, ShellRunner};
use crate::incremental::dirty::{self, Cascade, DiffCache, Mode, Snapshot, SpecFiles};
use crate::incremental::{recheck, rows};
use crate::layout;
use crate::out::diag;
use crate::signals;
pub struct IntegrateRequest {
pub unit: String,
pub token_seq: i64,
pub builder_holder: String,
pub integrator_id: String,
}
#[derive(Debug, Clone)]
pub enum Outcome {
Verified {
merge_commit: String,
verify_commit: String,
},
RejectedFencing { reason: String },
SkippedLeaseExpired,
RejectedWriteSet { paths: Vec<String>, detail: String },
RejectedNotBuilt { reason: String },
RejectedStaleInputs { keys: Vec<String> },
RejectedGate { command: String, output: String },
OwnershipConflict { paths: Vec<String> },
LostRace,
Interrupted,
}
#[derive(Debug, Clone, Default)]
pub struct SddCommands {
pub bootstrap: Option<String>,
pub build: Option<String>,
pub test: Option<String>,
pub lint: Option<String>,
}
pub fn sdd_commands(core_text: &str) -> SddCommands {
let c = crate::incremental::rows::Commands::parse(core_text);
SddCommands {
bootstrap: c.bootstrap,
build: c.build,
test: c.test,
lint: c.lint,
}
}
pub fn run_gate_command(cmd: &str, cwd: &Path) -> (bool, String) {
let ran = gate::run_command(cmd, cwd);
(ran.ok(), ran.output)
}
#[derive(Debug, Clone, Default)]
pub struct GateMemo {
pub suite: Option<(String, bool)>,
}
#[derive(Debug, Clone)]
pub struct HeadCheck {
pub head: String,
pub findings: Vec<audit::Finding>,
pub broken: Option<(String, String)>,
pub suite: Option<(String, bool, String)>,
pub interrupted: bool,
}
struct Merged {
i: usize,
branch: String,
merge_sha: String,
}
enum Gate {
Green {
rechecked: Vec<String>,
footprint: Option<Footprint>,
},
Red {
command: String,
output: String,
},
Interrupted,
}
enum Suite {
Fine(Option<bool>),
Regressed {
command: String,
output: String,
},
Interrupted,
}
enum Final {
Published,
Redo(Vec<usize>),
Red {
command: String,
output: String,
},
Interrupted,
}
struct IdleOnDrop<'a>(&'a GateBoard);
impl Drop for IdleOnDrop<'_> {
fn drop(&mut self) {
self.0.idle();
}
}
const PUBLISH_TRIES: usize = 12;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RottenRow {
pub unit: String,
pub row_number: usize,
pub row: String,
pub base: String,
pub output: String,
}
pub struct Integrator {
pub ml: Mainline,
pub gate_jobs: usize,
pub board: GateBoard,
found: Mutex<Vec<RottenRow>>,
rotten_sink: Option<Box<dyn Fn(Vec<RottenRow>) + Send>>,
held: Mutex<BTreeSet<String>>,
diffs: Mutex<DiffCache>,
}
impl Integrator {
pub fn new(ml: Mainline, gate_jobs: usize) -> Integrator {
Integrator {
ml,
gate_jobs: gate_jobs.max(1),
board: GateBoard::off(),
found: Mutex::new(Vec::new()),
held: Mutex::new(BTreeSet::new()),
diffs: Mutex::new(DiffCache::default()),
rotten_sink: None,
}
}
pub fn with_rotten_sink(self, sink: impl Fn(Vec<RottenRow>) + Send + 'static) -> Integrator {
Integrator {
rotten_sink: Some(Box::new(sink)),
..self
}
}
fn report_rotten(&self, rows: Vec<RottenRow>) {
if rows.is_empty() {
return;
}
for r in &rows {
self.hold_rotten(&r.unit);
}
match &self.rotten_sink {
Some(sink) => sink(rows),
None => self
.found
.lock()
.unwrap_or_else(|e| e.into_inner())
.extend(rows),
}
}
pub fn take_rotten(&self) -> Vec<RottenRow> {
std::mem::take(&mut *self.found.lock().unwrap_or_else(|e| e.into_inner()))
}
pub fn hold_rotten(&self, unit: &str) {
self.held
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(unit.to_string());
}
pub fn forget_rotten(&self, unit: &str) {
self.held
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(unit);
}
fn held_rotten(&self) -> BTreeSet<String> {
self.held.lock().unwrap_or_else(|e| e.into_inner()).clone()
}
fn base_checkout(&self) -> PristineCheckout {
PristineCheckout::new(
self.ml.primary.clone(),
self.ml.layout.base_checkout(&self.ml.slug),
)
}
pub fn with_board(self, board: GateBoard) -> Integrator {
Integrator { board, ..self }
}
fn checkout(&self) -> PathBuf {
self.ml.checkout()
}
fn lock_checkout(&self) -> Result<Option<LaneLock>, CoreError> {
self.ml.lock_checkout(&|| {
self.board.step("lock");
diag(
"note: another vitrify process is integrating this SDD โ waiting for its integration checkout",
);
})
}
pub fn integrate(
&self,
req: &IntegrateRequest,
memo: &mut GateMemo,
before_publish: Option<&dyn Fn()>,
) -> Result<Outcome, CoreError> {
let mut out = self.integrate_batch(std::slice::from_ref(req), memo, before_publish)?;
Ok(out.pop().unwrap_or(Outcome::LostRace))
}
pub fn integrate_batch(
&self,
reqs: &[IntegrateRequest],
memo: &mut GateMemo,
before_publish: Option<&dyn Fn()>,
) -> Result<Vec<Outcome>, CoreError> {
let _idle = IdleOnDrop(&self.board);
self.board
.batch(reqs.iter().map(|r| r.unit.clone()).collect());
let mut out: Vec<Option<Outcome>> = vec![None; reqs.len()];
match self.lock_checkout()? {
Some(_held) => {
let all: Vec<usize> = (0..reqs.len()).collect();
self.run_batch(&all, reqs, &mut out, memo, before_publish)?;
}
None => out.fill(Some(Outcome::Interrupted)),
}
Ok(out
.into_iter()
.map(|o| o.unwrap_or(Outcome::LostRace))
.collect())
}
fn run_batch(
&self,
idx: &[usize],
reqs: &[IntegrateRequest],
out: &mut [Option<Outcome>],
memo: &mut GateMemo,
before_publish: Option<&dyn Fn()>,
) -> Result<(), CoreError> {
let mlwt = self.checkout();
let ml = self.ml.primary.at(&mlwt);
let mut pending: Vec<usize> = idx.to_vec();
let units = |which: &[usize]| -> Vec<String> {
which.iter().map(|&i| reqs[i].unit.clone()).collect()
};
let interrupt = |out: &mut [Option<Outcome>], which: &[usize]| {
for &i in which {
if out[i].is_none() {
out[i] = Some(Outcome::Interrupted);
}
}
};
for _attempt in 0..3 {
if signals::interrupted() {
interrupt(out, &pending);
return Ok(());
}
self.board.batch(units(&pending));
self.board.step("sync");
self.ml.sync_checkout(&mlwt)?;
let at_head = self.ml.sdd_in(&mlwt)?;
pending.retain(|&i| {
let lapsed = at_head.unit(&reqs[i].unit).is_some_and(|u| {
u.meta.get_int("claim_seq") == reqs[i].token_seq
&& u.meta.get_str_or("status", "Draft") == "Claimed"
&& !UtcTime::parse(u.meta.get_str_or("claim_expires", ""))
.is_some_and(|e| e > UtcTime::now())
});
if lapsed {
out[i] = Some(Outcome::SkippedLeaseExpired);
}
!lapsed
});
if pending.is_empty() {
return Ok(());
}
match self.ml.heal(true, true) {
Ok(_) => {}
Err(e) if e.exit_code() == 5 => diag(&format!("note: {e}")),
Err(e) => return Err(e),
}
let base = self.ml.sync_checkout(&mlwt)?;
self.board.batch(units(&pending));
self.board.step("merge");
let merged = self.assemble(&ml, &mlwt, &base, &pending, reqs, out)?;
if merged.is_empty() {
return Ok(());
}
let members: Vec<usize> = merged.iter().map(|m| m.i).collect();
self.board.batch(units(&members));
let red = |this: &Integrator,
out: &mut [Option<Outcome>],
memo: &mut GateMemo,
command: String,
output: String|
-> Result<(), CoreError> {
this.ml.pristine(&mlwt, &base)?;
if members.len() > 1 {
diag(&format!(
"note: gate red for a batch of {} ({command}); integrating them one at a time",
members.len()
));
for &i in &members {
this.run_batch(&[i], reqs, out, memo, before_publish)?;
}
} else {
out[members[0]] = Some(Outcome::RejectedGate { command, output });
}
Ok(())
};
let (rechecked, footprint) = match self.gate(&mlwt, &base, &merged, reqs)? {
Gate::Green {
rechecked,
footprint,
} => (rechecked, footprint),
Gate::Red { command, output } => return red(self, out, memo, command, output),
Gate::Interrupted => {
let _ = self.ml.pristine(&mlwt, &base);
interrupt(out, &members);
return Ok(());
}
};
let mut rebuilt = false;
let suite = match self.suite(&mlwt, &base, memo, &mut rebuilt)? {
Suite::Fine(result) => result,
Suite::Regressed { command, output } => {
return red(self, out, memo, command, output);
}
Suite::Interrupted => {
let _ = self.ml.pristine(&mlwt, &base);
interrupt(out, &members);
return Ok(());
}
};
match self.finalize(
&ml,
&mlwt,
&base,
merged,
rebuilt,
suite,
&rechecked,
footprint.as_ref(),
reqs,
out,
memo,
before_publish,
)? {
Final::Published => return Ok(()),
Final::Red { command, output } => return red(self, out, memo, command, output),
Final::Interrupted => {
let _ = self.ml.pristine(&mlwt, &base);
interrupt(out, &members);
return Ok(());
}
Final::Redo(remaining) => {
if remaining.is_empty() {
return Ok(());
}
diag(&format!(
"note: {} changed in substance during the gate; integrating again from the new head",
self.ml.branch
));
pending = remaining;
}
}
}
for i in pending {
if out[i].is_none() {
out[i] = Some(Outcome::LostRace);
}
}
Ok(())
}
fn assemble(
&self,
ml: &Git,
mlwt: &Path,
base: &str,
which: &[usize],
reqs: &[IntegrateRequest],
out: &mut [Option<Outcome>],
) -> Result<Vec<Merged>, CoreError> {
let inst = self.ml.sdd_in(mlwt)?;
let slug = layout::sdd_slug(&inst);
let mut candidates: Vec<(usize, String)> = Vec::new();
for &i in which {
let req = &reqs[i];
let branch = layout::unit_branch(&slug, &req.unit, req.token_seq);
let Some(unit) = inst.unit(&req.unit) else {
out[i] = Some(Outcome::RejectedFencing {
reason: format!("unit {} not found at the mainline head", req.unit),
});
continue;
};
let current_seq = unit.meta.get_int("claim_seq");
if req.token_seq != current_seq {
out[i] = Some(Outcome::RejectedFencing {
reason: format!(
"superseded token: branch carries c{}, current claim_seq is {}",
req.token_seq, current_seq
),
});
continue;
}
match unit.meta.get_str_or("status", "Draft") {
"Built" => {}
"Claimed" => {
let expires = UtcTime::parse(unit.meta.get_str_or("claim_expires", ""));
if !expires.is_some_and(|e| e > UtcTime::now()) {
out[i] = Some(Outcome::SkippedLeaseExpired);
continue;
}
}
other => {
out[i] = Some(Outcome::RejectedFencing {
reason: format!("unit {} is {other}, not Built/Claimed", req.unit),
});
continue;
}
}
if !ml.branch_exists(&branch).map_err(CoreError::from)? {
out[i] = Some(Outcome::RejectedFencing {
reason: format!("work branch {branch} does not exist"),
});
continue;
}
let violations = write_set_violations(ml, &inst, unit, mlwt, base, &branch)?;
if !violations.is_empty() {
out[i] = Some(Outcome::RejectedWriteSet {
paths: violations.iter().map(|v| v.path().to_string()).collect(),
detail: violations
.iter()
.map(|v| v.describe(&req.unit))
.collect::<Vec<_>>()
.join("\n"),
});
continue;
}
if let Some(outcome) = stamped_for_current_inputs(ml, &inst, unit, mlwt, &branch)? {
out[i] = Some(outcome);
continue;
}
candidates.push((i, branch));
}
let mut merged: Vec<Merged> = Vec::new();
for (i, branch) in candidates {
let req = &reqs[i];
let before = ml.rev_parse("HEAD").map_err(CoreError::from)?;
if let Err(e) = ml.merge_no_ff(
&branch,
&format!("sdd: integrate {} (c{})", req.unit, req.token_seq),
) {
let conflicted = ml.conflicted_paths();
ml.merge_abort().map_err(CoreError::from)?;
ml.reset_hard(&before).map_err(CoreError::from)?;
if conflicted.is_empty() {
return Err(e.into());
}
out[i] = Some(Outcome::OwnershipConflict { paths: conflicted });
continue;
}
let merged_inst = self.ml.sdd_in(mlwt)?;
let rejected = match merged_inst.unit(&req.unit) {
None => Some(Outcome::RejectedFencing {
reason: format!("unit {} missing after merge", req.unit),
}),
Some(unit) => built_from_current_inputs(&merged_inst, unit)?,
};
if let Some(outcome) = rejected {
ml.reset_hard(&before).map_err(CoreError::from)?;
out[i] = Some(outcome);
continue;
}
merged.push(Merged {
i,
branch,
merge_sha: ml.rev_parse("HEAD").map_err(CoreError::from)?,
});
}
Ok(merged)
}
fn gate(
&self,
mlwt: &Path,
base: &str,
merged: &[Merged],
reqs: &[IntegrateRequest],
) -> Result<Gate, CoreError> {
let inst = self.ml.sdd_in(mlwt)?;
let cmds = sdd_commands(&model::read_text(&inst.file)?);
let verdict = |command: &str, ran: Ran| {
if ran.interrupted || signals::interrupted() {
Gate::Interrupted
} else {
Gate::Red {
command: command.to_string(),
output: ran.output,
}
}
};
for (step, cmd) in [("bootstrap", &cmds.bootstrap), ("build", &cmds.build)] {
let Some(cmd) = cmd else { continue };
self.board.step(step);
let ran = gate::run_command_watched(cmd, mlwt, &self.board);
if !ran.ok() {
return Ok(verdict(cmd, ran));
}
}
let held = self.held_rotten();
let slug = unit_graph::slug_of(&inst).to_string();
let batch: Vec<String> = merged.iter().map(|m| reqs[m.i].unit.clone()).collect();
let mut tables = rows::tables(&inst)?;
for t in &mut tables {
if held.contains(&t.unit) {
t.verified = false;
}
}
let (dirty, snapshot) = self.read_dirty(mlwt, &slug);
let selection = rows::for_gate(&tables, &batch, &dirty, cmds.test.as_deref());
let footprint = snapshot
.and_then(|snap| Footprint::of(&snap, &slug, &selection.units, cmds.test.is_some()));
let rows = selection.rows.clone();
let own: BTreeSet<String> = tables
.iter()
.filter(|t| batch.contains(&t.unit))
.flat_map(|t| t.rows.iter().cloned())
.collect();
let verified: BTreeMap<Node, Vec<String>> = tables
.iter()
.filter(|t| selection.units.contains(&t.unit) && !batch.contains(&t.unit))
.map(|t| (Node::new(slug.clone(), t.unit.clone()), t.rows.clone()))
.collect();
self.board.step("rows");
self.board.rows(rows.len());
let results = gate::run_rows_watched(&rows, mlwt, self.gate_jobs, false, &self.board);
let failed: Vec<(&String, Ran)> = rows
.iter()
.zip(results)
.filter_map(|(cmd, ran)| ran.filter(|r| !r.ok()).map(|r| (cmd, r)))
.collect();
if let Some((cmd, ran)) = failed.iter().find(|(_, r)| r.interrupted) {
return Ok(verdict(cmd, ran.clone()));
}
if let Some((cmd, ran)) = failed.iter().find(|(cmd, _)| own.contains(*cmd)) {
return Ok(verdict(cmd, ran.clone()));
}
if !failed.is_empty() {
let commands: Vec<String> = failed.iter().map(|(c, _)| (*c).clone()).collect();
let setup: Vec<String> = [&cmds.bootstrap, &cmds.build]
.into_iter()
.flatten()
.cloned()
.collect();
self.board.step("rows-base");
let judged = blame::judge(
&commands,
&verified,
base,
&setup,
&self.base_checkout(),
&ShellRunner,
)?;
let Judged::Done { verdict: v, .. } = judged else {
return Ok(Gate::Interrupted);
};
if let Some(why) = &v.base_unusable {
diag(&format!(
"note: failing rows could not be judged on the base ({why}); the batch is blamed for them"
));
}
if let Some((cmd, ran)) = failed.iter().find(|(c, _)| v.broken.contains(*c)) {
return Ok(verdict(cmd, ran.clone()));
}
let mut found: Vec<RottenRow> = Vec::new();
for (node, unit_rows) in &v.rotten {
let Some(table) = verified.get(node) else {
continue;
};
for row in unit_rows {
let output = failed
.iter()
.find(|(c, _)| *c == row)
.map(|(_, r)| r.output.clone())
.unwrap_or_default();
found.push(RottenRow {
unit: node.unit.clone(),
row_number: table.iter().position(|c| c == row).map_or(0, |i| i + 1),
row: row.clone(),
base: base.to_string(),
output,
});
}
}
self.report_rotten(found);
}
if signals::interrupted() {
return Ok(Gate::Interrupted);
}
if let Some(cmd) = &cmds.lint {
self.board.step("lint");
let ran = gate::run_command_watched(cmd, mlwt, &self.board);
if !ran.ok() {
return Ok(verdict(cmd, ran));
}
}
let held = self.held_rotten();
Ok(Gate::Green {
rechecked: selection
.units
.into_iter()
.filter(|u| !batch.contains(u) && !held.contains(u))
.collect(),
footprint,
})
}
fn dirty_units(&self, checkout: &Path, slug: &str) -> Vec<String> {
self.read_dirty(checkout, slug).0
}
fn read_dirty(&self, checkout: &Path, slug: &str) -> (Vec<String>, Option<Snapshot>) {
let git = self.ml.primary.at(checkout);
let everything = || match self.ml.sdd_in(checkout) {
Ok(inst) => inst
.units
.iter()
.filter(|u| u.meta.get_str_or("status", "Draft") == "Verified")
.map(|u| u.unit.clone())
.collect(),
Err(_) => Vec::new(),
};
let snapshot = match Snapshot::load_resolvable(&git, checkout, "HEAD") {
Ok((snap, _)) => snap,
Err(e) => {
diag(&format!(
"note: which units a change reaches could not be told ({e}); every Verified unit is checked"
));
return (everything(), None);
}
};
let cached = snapshot.dirty_with(
&git,
Mode::Changes,
&mut self.diffs.lock().unwrap_or_else(|e| e.into_inner()),
);
let units = match cached {
Ok(Cascade::Resolved(set)) => set.of_sdd(slug).map(|d| d.node.unit.clone()).collect(),
Ok(Cascade::Cycles(_)) => everything(),
Err(e) => {
diag(&format!(
"note: which units a change reaches could not be told ({e}); every Verified unit is checked"
));
everything()
}
};
(units, Some(snapshot))
}
fn suite(
&self,
mlwt: &Path,
base: &str,
memo: &mut GateMemo,
rebuilt: &mut bool,
) -> Result<Suite, CoreError> {
let inst = self.ml.sdd_in(mlwt)?;
let cmds = sdd_commands(&model::read_text(&inst.file)?);
let Some(test) = cmds.test.clone() else {
return Ok(Suite::Fine(None));
};
self.board.step("test");
let after = gate::run_command_watched(&test, mlwt, &self.board);
if after.interrupted || signals::interrupted() {
return Ok(Suite::Interrupted);
}
if after.ok() {
return Ok(Suite::Fine(Some(true)));
}
let before = match self.suite_known(memo, base)? {
Some(passed) => passed,
None => {
self.board.step("test-base");
self.ml.pristine(mlwt, base)?;
*rebuilt = true;
let mut passed = true;
for cmd in [&cmds.bootstrap, &cmds.build, &Some(test.clone())]
.into_iter()
.flatten()
{
let ran = gate::run_command_watched(cmd, mlwt, &self.board);
if ran.interrupted || signals::interrupted() {
return Ok(Suite::Interrupted);
}
if !ran.ok() {
passed = false;
break;
}
}
memo.suite = Some((base.to_string(), passed));
passed
}
};
if before {
Ok(Suite::Regressed {
command: test,
output: format!(
"{}\n(the ยง5 Test command passed before this merge and fails after it)",
after.output
),
})
} else {
diag(&format!(
"note: the ยง5 Test command already fails at the {} head โ integrations are not judged by it until it passes: {test}",
self.ml.branch
));
Ok(Suite::Fine(Some(false)))
}
}
fn suite_known(&self, memo: &GateMemo, commit: &str) -> Result<Option<bool>, CoreError> {
match &memo.suite {
Some((at, passed)) if benign(&self.ml, at, commit)? => Ok(Some(*passed)),
_ => Ok(None),
}
}
#[allow(clippy::too_many_arguments)]
fn finalize(
&self,
ml: &Git,
mlwt: &Path,
gated_base: &str,
mut merged: Vec<Merged>,
mut rebuild: bool,
suite: Option<bool>,
rechecked: &[String],
footprint: Option<&Footprint>,
reqs: &[IntegrateRequest],
out: &mut [Option<Outcome>],
memo: &mut GateMemo,
before_publish: Option<&dyn Fn()>,
) -> Result<Final, CoreError> {
let mut base = gated_base.to_string();
let mut heals_left = 2;
for _ in 0..PUBLISH_TRIES {
self.board.step("publish");
if signals::interrupted() {
return Ok(Final::Interrupted);
}
let head = self.ml.head()?;
if head != base {
let untouched = self.benign(gated_base, &head)?
|| footprint.is_some_and(|f| f.untouched(&self.ml.primary, gated_base, &head));
if !untouched {
return Ok(Final::Redo(merged.iter().map(|m| m.i).collect()));
}
rebuild = true;
}
if rebuild {
self.ml.pristine(mlwt, &head)?;
let before: Vec<usize> = merged.iter().map(|m| m.i).collect();
merged = self.assemble(ml, mlwt, &head, &before, reqs, out)?;
if merged.len() != before.len() {
self.ml.pristine(mlwt, &head)?;
return Ok(Final::Redo(merged.iter().map(|m| m.i).collect()));
}
base = head.clone();
}
ml.reset_hard("HEAD").map_err(CoreError::from)?;
ml.clean_fdq().map_err(CoreError::from)?;
let inst = self.ml.sdd_in(mlwt)?;
for m in &merged {
let req = &reqs[m.i];
let Some(unit) = inst.unit(&req.unit) else {
continue;
};
stamp_verified(&inst, unit, short(&m.merge_sha))?;
let branch_sha = ml.rev_parse(&m.branch).map_err(CoreError::from)?;
for (event, actor, commit) in [
("built", &req.builder_holder, short(&branch_sha)),
("verified", &req.integrator_id, short(&m.merge_sha)),
] {
append_event(
&inst,
Event {
event: event.to_string(),
unit: Some(req.unit.clone()),
actor: Some(actor.clone()),
claim_seq: Some(req.token_seq),
commit: Some(commit.to_string()),
..Default::default()
},
)?;
}
}
let gated = short(&ml.rev_parse("HEAD").map_err(CoreError::from)?).to_string();
let held = self.held_rotten();
let stamp = model::render_stamp(
&UtcTime::now().format_date(),
inst.meta.get_str_or("version", "0.0.0"),
&gated,
);
for uid in rechecked.iter().filter(|u| !held.contains(*u)) {
let Some(unit) = inst.unit(uid) else {
continue;
};
if unit.meta.get_str_or("status", "Draft") != "Verified" {
continue;
}
model::rewrite_unit_meta(&inst.handle(), unit, |inner| {
yamlish::set_scalar_in_place(inner, "verified", &stamp)
})?;
append_event(
&inst,
Event {
event: "verified".to_string(),
unit: Some(uid.clone()),
actor: Some(journal::RECHECK_ACTOR.to_string()),
commit: Some(gated.clone()),
..Default::default()
},
)?;
}
let stamped = self.ml.sdd_in(mlwt)?;
ledger::regen(&stamped, &UtcTime::now().format_date())?;
let (findings, _) = audit::run(
&stamped,
mlwt,
&cmd::audit_options(
audit::Options {
skip_rotten: true,
..Default::default()
},
mlwt,
&stamped,
),
&ShRunner,
UtcTime::now(),
)?;
let batch: Vec<&str> = merged.iter().map(|m| reqs[m.i].unit.as_str()).collect();
let elsewhere = |f: &audit::Finding| {
matches!(f.check, 5 | 7) && !batch.contains(&f.subject.as_str())
};
if !findings.is_empty() && findings.iter().all(elsewhere) {
if heals_left == 0 {
self.ml.pristine(mlwt, &base)?;
return Ok(Final::Redo(merged.iter().map(|m| m.i).collect()));
}
heals_left -= 1;
match self.ml.heal(true, true) {
Ok(_) => {}
Err(e) if e.exit_code() == 5 => diag(&format!("note: {e}")),
Err(e) => return Err(e),
}
rebuild = true;
continue;
}
if !findings.is_empty() {
self.ml.pristine(mlwt, &base)?;
return Ok(Final::Red {
command: "vitrify audit --skip-rotten --journal".to_string(),
output: findings
.iter()
.map(|f| format!("[check {}] {}: {}", f.check, f.subject, f.cause))
.collect::<Vec<_>>()
.join("\n"),
});
}
append_event(
&stamped,
Event {
event: "audit".to_string(),
actor: Some("audit".to_string()),
findings: Some(0),
suite: (suite == Some(true)).then(|| journal::SUITE_PASS.to_string()),
commit: Some(gated.clone()),
..Default::default()
},
)?;
ml.add_all().map_err(CoreError::from)?;
let label = merged
.iter()
.map(|m| format!("{} (c{})", reqs[m.i].unit, reqs[m.i].token_seq))
.collect::<Vec<_>>()
.join(", ");
ml.commit(&format!("sdd: verify {label}"))
.map_err(CoreError::from)?;
let verify_sha = ml.rev_parse("HEAD").map_err(CoreError::from)?;
if let Some(hook) = before_publish {
hook();
}
if signals::interrupted() {
return Ok(Final::Interrupted);
}
let turn = claim::tx_lock();
if let Some(landed) = self.ml.publish(&verify_sha, &base)? {
self.ml.follow(&landed)?;
drop(turn);
if let Some(passed) = suite {
memo.suite = Some((verify_sha.clone(), passed));
}
let slug = layout::sdd_slug(&stamped);
for m in &merged {
let req = &reqs[m.i];
let wt = self
.ml
.layout
.unit_worktree(&slug, &req.unit, req.token_seq);
if wt.exists() {
let _ = self.ml.primary.worktree_remove(&wt);
}
self.ml
.primary
.delete_branch(&m.branch)
.map_err(CoreError::from)?;
out[m.i] = Some(Outcome::Verified {
merge_commit: m.merge_sha.clone(),
verify_commit: verify_sha.clone(),
});
}
return Ok(Final::Published);
}
drop(turn);
diag(&format!(
"note: lost publish race integrating {label} on {}; redoing from sync",
self.ml.branch
));
rebuild = true;
}
Ok(Final::Redo(merged.iter().map(|m| m.i).collect()))
}
pub fn benign(&self, old: &str, new: &str) -> Result<bool, CoreError> {
benign(&self.ml, old, new)
}
pub fn recheck(&self, units: &[String]) -> Result<Rechecked, CoreError> {
let mlwt = self.checkout();
let _idle = IdleOnDrop(&self.board);
self.board.batch(units.to_vec());
let mut out = Rechecked::default();
let Some(_held) = self.lock_checkout()? else {
out.interrupted = true;
return Ok(out);
};
self.board.step("recheck");
let head = self.ml.sync_checkout(&mlwt)?;
let report = recheck::run(
&self.ml.primary.at(&mlwt),
&head,
&self.ml.sdd_rel,
units,
&PoolRunner(self.gate_jobs),
)?;
out.commit = report.commit.clone();
if report.interrupted || signals::interrupted() {
out.interrupted = true;
return Ok(out);
}
if let Some(b) = &report.broken {
out.broken = Some((b.command.clone(), b.output.clone()));
return Ok(out);
}
out.suite_ran = report.suite.is_some();
let inst = self.ml.sdd_in(&mlwt)?;
self.report_rotten(rotten_of(&inst, &report)?);
out.rotten = self.take_rotten();
let edits = report.edits();
out.passed = edits.verified.clone();
out.recorded = record_recheck(&self.ml, &edits);
Ok(out)
}
pub fn check_head(
&self,
memo: &mut GateMemo,
want_suite: bool,
) -> Result<HeadCheck, CoreError> {
let mlwt = self.checkout();
let _idle = IdleOnDrop(&self.board);
self.board.batch(Vec::new());
let Some(_held) = self.lock_checkout()? else {
return Ok(HeadCheck {
head: String::new(),
findings: Vec::new(),
broken: None,
suite: None,
interrupted: true,
});
};
self.board.step("head-check");
let head = self.ml.sync_checkout(&mlwt)?;
let inst = self.ml.sdd_in(&mlwt)?;
let cmds = sdd_commands(&model::read_text(&inst.file)?);
let mut check = HeadCheck {
head: head.clone(),
findings: Vec::new(),
broken: None,
suite: None,
interrupted: false,
};
let slug = unit_graph::slug_of(&inst).to_string();
let held = self.held_rotten();
let dirty: Vec<String> = self
.dirty_units(&mlwt, &slug)
.into_iter()
.filter(|u| !held.contains(u))
.collect();
let tables = rows::tables(&inst)?;
let selection = rows::for_preflight(&tables, &dirty);
let suite_known = match self.suite_known(memo, &head)? {
Some(passed) => Some(passed),
None => self.suite_recorded(&mlwt, &slug).then_some(true),
};
let run_suite = want_suite && cmds.test.is_some() && suite_known.is_none();
if !selection.rows.is_empty() || run_suite {
for cmd in [&cmds.bootstrap, &cmds.build].into_iter().flatten() {
let ran = gate::run_command_watched(cmd, &mlwt, &self.board);
if ran.interrupted || signals::interrupted() {
check.interrupted = true;
return Ok(check);
}
if !ran.ok() {
check.broken = Some((cmd.clone(), ran.output));
return Ok(check);
}
}
}
let (mut findings, _) = audit::run(
&inst,
&mlwt,
&cmd::audit_options(
audit::Options {
skip_rotten: true,
..Default::default()
},
&mlwt,
&inst,
),
&ShRunner,
UtcTime::now(),
)?;
if !selection.rows.is_empty() {
self.board.step("rows");
self.board.rows(selection.rows.len());
let results =
gate::run_rows_watched(&selection.rows, &mlwt, self.gate_jobs, false, &self.board);
if signals::interrupted() {
check.interrupted = true;
return Ok(check);
}
for t in tables.iter().filter(|t| selection.units.contains(&t.unit)) {
for (row, ran) in selection.rows.iter().zip(&results) {
if t.rows.contains(row)
&& let Some(ran) = ran
&& !ran.ok()
{
findings.push(audit::Finding {
check: 4,
subject: t.unit.clone(),
cause: format!("Verification row fails: `{row}` (exit {})", ran.code),
remediation: format!(
"run `{row}` in a checkout of the head and fix what it reports"
),
});
}
}
}
}
check.findings = findings;
if want_suite && let Some(test) = &cmds.test {
check.suite = Some(match suite_known {
Some(passed) => (test.clone(), passed, String::new()),
None => {
self.board.step("test");
let ran = gate::run_command_watched(test, &mlwt, &self.board);
if ran.interrupted || signals::interrupted() {
check.interrupted = true;
return Ok(check);
}
memo.suite = Some((head.clone(), ran.ok()));
(test.clone(), ran.ok(), ran.output)
}
});
}
Ok(check)
}
fn suite_recorded(&self, checkout: &Path, slug: &str) -> bool {
let git = self.ml.primary.at(checkout);
match Snapshot::load_resolvable(&git, checkout, "HEAD").and_then(|(s, _)| {
s.dirty_with(
&git,
Mode::Changes,
&mut self.diffs.lock().unwrap_or_else(|e| e.into_inner()),
)
}) {
Ok(Cascade::Resolved(set)) => set
.suite(slug)
.is_some_and(|s| s.anchor.is_some() && s.dirty.is_none()),
_ => false,
}
}
}
#[derive(Debug, Clone)]
pub struct Footprint {
slug: String,
checked: BTreeSet<Node>,
suite: bool,
ownership: Ownership,
spec: SpecFiles,
}
impl Footprint {
fn of(snap: &Snapshot, slug: &str, units: &[String], suite: bool) -> Option<Footprint> {
let resolution = snap.resolved.as_ref().ok()?;
let mut checked: BTreeSet<Node> = BTreeSet::new();
for unit in units {
let node = Node::new(slug, unit.as_str());
checked.extend(resolution.closure(&node).into_iter().cloned());
checked.insert(node);
}
Some(Footprint {
slug: slug.to_string(),
checked,
suite,
ownership: snap.ownership.clone(),
spec: snap.spec.clone(),
})
}
fn reads(&self, path: &str) -> bool {
match self.ownership.classify(path) {
Owner::Spec(s) => s == self.slug,
Owner::Unit(n) => self.checked.contains(&n) || (self.suite && n.slug == self.slug),
Owner::Unmanaged(s) => s == self.slug,
Owner::Outside => self.ownership.takes_outside(&self.slug, path),
}
}
pub fn untouched(&self, git: &Git, old: &str, new: &str) -> bool {
if old == new {
return true;
}
if !git.is_ancestor(old, new).unwrap_or(false) {
return false;
}
match dirty::substantive_changes(git, old, new, &self.spec) {
Ok(paths) => !paths.iter().any(|p| self.reads(p)),
Err(_) => false,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Rechecked {
pub commit: String,
pub passed: Vec<String>,
pub recorded: bool,
pub rotten: Vec<RottenRow>,
pub broken: Option<(String, String)>,
pub suite_ran: bool,
pub interrupted: bool,
}
pub(crate) struct PoolRunner(pub usize);
impl recheck::Runner for PoolRunner {
fn run(&self, cmd: &str, cwd: &Path) -> recheck::CommandResult {
recheck::ShRunner.run(cmd, cwd)
}
fn run_rows(&self, rows: &[String], cwd: &Path) -> Vec<recheck::CommandResult> {
gate::run_rows(rows, cwd, self.0, false)
.into_iter()
.map(|ran| match ran {
Some(ran) => recheck::CommandResult {
passed: ran.ok(),
interrupted: ran.interrupted,
output: ran.output,
},
None => recheck::CommandResult {
passed: false,
interrupted: true,
output: String::new(),
},
})
.collect()
}
}
pub(crate) fn rotten_of(
inst: &Instance,
report: &recheck::Report,
) -> Result<Vec<RottenRow>, CoreError> {
let tables = rows::tables(inst)?;
let mut out = Vec::new();
for u in report.units.iter().filter(|u| !u.passed) {
let table = tables
.iter()
.find(|t| t.unit == u.unit)
.map(|t| t.rows.as_slice())
.unwrap_or_default();
for f in &u.failures {
out.push(RottenRow {
unit: u.unit.clone(),
row_number: table
.iter()
.position(|r| r == &f.command)
.map_or(0, |i| i + 1),
row: f.command.clone(),
base: report.commit.clone(),
output: f.output.clone(),
});
}
}
Ok(out)
}
pub(crate) fn record_recheck(ml: &Mainline, edits: &recheck::Edits) -> bool {
if edits.is_empty() {
return false;
}
let message = if edits.verified.is_empty() {
format!("sdd: re-check the suite at {}", edits.commit)
} else {
format!(
"sdd: re-check {} at {}",
edits.verified.join(", "),
edits.commit
)
};
let published = ml.publish_tx(&message, &|tx| {
let inst = ml.sdd_in(tx)?;
let findings = recheck::apply(&inst, tx, edits)?;
if !findings.is_empty() {
diag(&format!(
"note: the re-check was not recorded โ the audit found {} finding(s) after it",
findings.len()
));
return Ok(false);
}
Ok(true)
});
match published {
Ok(commit) => commit.is_some(),
Err(e) => {
diag(&format!("note: the re-check was not recorded: {e}"));
false
}
}
}
fn spec_files(ml: &Mainline) -> SpecFiles {
let (mut sdds, _) = cmd::loaded(&ml.primary.dir, None);
if let Ok(own) = ml.sdd_in(&ml.primary.dir) {
sdds.push(own);
}
SpecFiles::of(&ml.primary.dir, &sdds)
}
pub fn benign(ml: &Mainline, old: &str, new: &str) -> Result<bool, CoreError> {
if old == new {
return Ok(true);
}
Ok(substantive(ml, old, new)?.is_some_and(|paths| paths.is_empty()))
}
fn substantive(ml: &Mainline, old: &str, new: &str) -> Result<Option<Vec<String>>, CoreError> {
type Memo = Vec<((String, String), Option<Vec<String>>)>;
static SEEN: std::sync::Mutex<Memo> = std::sync::Mutex::new(Vec::new());
let key = (old.to_string(), new.to_string());
if let Some((_, paths)) = SEEN
.lock()
.unwrap_or_else(|e| e.into_inner())
.iter()
.find(|(k, _)| *k == key)
{
return Ok(paths.clone());
}
let paths = if ml.primary.is_ancestor(old, new).map_err(CoreError::from)? {
Some(dirty::substantive_changes(
&ml.primary,
old,
new,
&spec_files(ml),
)?)
} else {
None
};
let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
seen.push((key, paths.clone()));
if seen.len() > 64 {
seen.remove(0);
}
Ok(paths)
}
pub fn spec_moved(ml: &Mainline, old: &str, new: &str) -> Result<bool, CoreError> {
if old == new {
return Ok(false);
}
let root = cmd::sdd_arg(&ml.sdd_rel);
let under = |p: &str| root == "." || p == root || p.starts_with(&format!("{root}/"));
Ok(match substantive(ml, old, new)? {
Some(paths) => paths.iter().any(|p| under(p)),
None => true,
})
}
fn short(sha: &str) -> &str {
&sha[..7.min(sha.len())]
}
fn unit_meta_in(shape: Shape, text: &str, uid: &str) -> Option<model::Meta> {
match shape {
Shape::Bundle => model::front_matter(text),
Shape::Single => {
let (start, end) = model::find_unit_span(text, uid)?;
let region = &text[start..end];
let (_, _, inner_start, inner_end) = model::find_meta_block_span(region)?;
Some(model::Meta(yamlish::parse(®ion[inner_start..inner_end])))
}
}
}
fn stamped_for_current_inputs(
ml: &Git,
inst: &Instance,
unit: &model::Unit,
root: &Path,
branch: &str,
) -> Result<Option<Outcome>, CoreError> {
let spec_file = WriteSet::of(inst, unit, root)?.spec_file;
let stamped = ml
.show_text(branch, &spec_file)
.map_err(CoreError::from)?
.and_then(|text| unit_meta_in(inst.shape, &text, &unit.unit))
.unwrap_or_default();
judge_stamp(inst, unit, &stamped)
}
fn built_from_current_inputs(
merged: &Instance,
unit: &model::Unit,
) -> Result<Option<Outcome>, CoreError> {
judge_stamp(merged, unit, &unit.meta)
}
fn judge_stamp(
inst: &Instance,
unit: &model::Unit,
stamped: &model::Meta,
) -> Result<Option<Outcome>, CoreError> {
let merged = inst;
let status = stamped.get_str_or("status", "Draft");
let recorded = stamped.get_map("trace").cloned().unwrap_or_default();
if status != "Built" || recorded.is_empty() {
return Ok(Some(Outcome::RejectedNotBuilt {
reason: format!(
"the work does not stamp {} Built (status {status}{}) โ `vitrify trace record --stamp built` is the handoff",
unit.unit,
if recorded.is_empty() {
", no trace"
} else {
""
}
),
}));
}
let (current, problems) = trace::compute(merged, unit)?;
if !problems.is_empty() {
return Ok(Some(Outcome::RejectedStaleInputs {
keys: problems.iter().map(|p| p.to_string()).collect(),
}));
}
let mismatches = trace::verify(&recorded, ¤t);
if mismatches.is_empty() {
Ok(None)
} else {
Ok(Some(Outcome::RejectedStaleInputs {
keys: mismatches.into_iter().map(|m| m.key).collect(),
}))
}
}
fn write_set_violations(
ml: &Git,
inst: &Instance,
unit: &model::Unit,
root: &Path,
base: &str,
branch: &str,
) -> Result<Vec<Violation>, CoreError> {
let ws = WriteSet::of(inst, unit, root)?;
let fork = ml.merge_base(base, branch).map_err(CoreError::from)?;
let changed = ml.changed_paths(&fork, branch).map_err(CoreError::from)?;
let spec_file = ws.spec_file.clone();
let versions = || {
(
ml.show_text(&fork, &spec_file).ok().flatten(),
ml.show_text(branch, &spec_file).ok().flatten(),
)
};
Ok(ws.violations(&changed, versions))
}
fn stamp_verified(inst: &Instance, unit: &model::Unit, commit: &str) -> Result<(), CoreError> {
let (trace_map, problems) = trace::compute(inst, unit)?;
if !problems.is_empty() {
return Err(CoreError::refusal(
4,
problems
.iter()
.map(|p| format!("error: {p}"))
.collect::<Vec<_>>()
.join("\n"),
));
}
let stamp = model::render_stamp(
&UtcTime::now().format_date(),
inst.meta.get_str_or("version", "0.0.0"),
commit,
);
model::rewrite_unit_meta(&inst.handle(), unit, |inner| {
let b = yamlish::set_scalar_in_place(inner, "status", "Verified");
let b = yamlish::set_scalar_in_place(&b, "verified", &stamp);
let b = yamlish::set_scalar_in_place(&b, "claimed_by", "โ");
let b = yamlish::set_scalar_in_place(&b, "claim_expires", "โ");
yamlish::set_map_in_place(&b, "trace", &trace_map)
})?;
Ok(())
}