diff --git a/README.md b/README.md index bffb4f6d..93abbb1b 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ origin annotate @ review --pass -m "Looks good" # with details: -m, o origin annotate @ review --withdraw -m "Spoke too soon" # take your statement of a kind back origin annotate --on flaky --neutral -m "Timeout on CI" # a statement about a statement origin statements [] # the statements about a revision, and those about them -origin promote [] # carry results over from revisions with the same inputs +origin promote [] # carry results over from the target's revisions with the same inputs origin gc # drop expired payloads (policy.toml) and pack objects origin land [] [--onto ] # land a change and the ones below it diff --git a/apps/origin-src/benches/annotations.rs b/apps/origin-src/benches/annotations.rs index 3329e234..8bd6458e 100644 --- a/apps/origin-src/benches/annotations.rs +++ b/apps/origin-src/benches/annotations.rs @@ -8,8 +8,9 @@ use divan::Bencher; use divan::counter::ItemsCount; use origin_platform::Native; use origin_shared::{ - Base, ChangeId, Digest, EncodedObject, EntryKind, EntryName, Inputs, Kind, ObjectId, Principal, - Record, RecordId, RepoPath, Revision, RevisionId, Statement, Tree, TreeEntry, TreeId, Verdict, + AcceptanceBody, Base, ChangeId, Digest, EncodedObject, EntryKind, EntryName, Inputs, Kind, + NodeKey, Principal, Record, RecordId, RepoPath, Revision, RevisionId, Statement, TargetName, + Tree, TreeEntry, TreeId, Verdict, }; use origin_src::annotations::Annotations; use origin_src::fixture::FixtureTree; @@ -128,6 +129,19 @@ fn snapshot(version: usize, docs: usize) -> Vec { /// sources match one of them. fn promotion_store() -> (tempfile::TempDir, Store, RevisionId) { let (dir, store) = store(); + // Every revision is on `main`, so results promote between them. + let main = AcceptanceBody { + target: TargetName::MAIN.id(), + seq: 0, + prev: None, + state: Tree::default().encode().id().into(), + landed: Vec::new(), + evidence: Vec::new(), + policy: None, + } + .sign(&NodeKey::from_bytes(&[1; 32])) + .encode(); + store.write_all(std::slice::from_ref(&main)).unwrap(); let revision = |store: &Store, change: usize, version: usize, docs: usize| { let objects = snapshot(version, docs); let root = TreeId::from(objects.last().unwrap().id()); @@ -137,7 +151,7 @@ fn promotion_store() -> (tempfile::TempDir, Store, RevisionId) { let encoded = Revision { change: ChangeId::from_bytes(id), predecessors: Vec::new(), - base: Base::Acceptance(ObjectId::from_bytes([0; 32]).into()), + base: Base::Acceptance(main.id().into()), deps: Vec::new(), snapshot: root, description: EncodedObject::blob(b"").id().into(), diff --git a/apps/origin-src/src/annotations/mod.rs b/apps/origin-src/src/annotations/mod.rs index b87e2343..d3712466 100644 --- a/apps/origin-src/src/annotations/mod.rs +++ b/apps/origin-src/src/annotations/mod.rs @@ -415,7 +415,8 @@ mod tests { use std::thread; use origin_shared::{ - AttachmentId, Base, Digest, Inputs, ObjectId, RepoPath, Revision, RevisionId, Verdict, + AcceptanceBody, AttachmentId, Base, Digest, Inputs, NodeKey, RepoPath, Revision, + RevisionId, TargetName, Verdict, }; use super::*; @@ -432,7 +433,7 @@ mod tests { name.parse().unwrap() } - /// Writes a revision of `change` with `files`, evolving from + /// Writes a revision of `change` on `main` with `files`, evolving from /// `predecessors`. pub(super) fn revision( store: &Store, @@ -440,17 +441,40 @@ mod tests { predecessors: &[RevisionId], files: &[(&str, &str)], ) -> RevisionId { + revision_on(store, &TargetName::MAIN, change, predecessors, files) + } + + /// Writes a revision of `change` on `target`'s first acceptance with + /// `files`, evolving from `predecessors`. + pub(super) fn revision_on( + store: &Store, + target: &TargetName, + change: u8, + predecessors: &[RevisionId], + files: &[(&str, &str)], + ) -> RevisionId { + let acceptance = AcceptanceBody { + target: target.id(), + seq: 0, + prev: None, + state: build(store, &[]), + landed: Vec::new(), + evidence: Vec::new(), + policy: None, + } + .sign(&NodeKey::from_bytes(&[1; 32])) + .encode(); let encoded = Revision { change: ChangeId::from_bytes([change; 16]), predecessors: predecessors.to_vec(), - base: Base::Acceptance(ObjectId::from_bytes([0; 32]).into()), + base: Base::Acceptance(acceptance.id().into()), deps: Vec::new(), snapshot: build(store, files), description: AttachmentId::from(EncodedObject::attachment(b"").id()), author: principal("user:alice"), } .encode(); - store.write_all(std::slice::from_ref(&encoded)).unwrap(); + store.write_all(&[acceptance, encoded.clone()]).unwrap(); encoded.id().into() } diff --git a/apps/origin-src/src/annotations/promotion.rs b/apps/origin-src/src/annotations/promotion.rs index 91be5765..25a9ae9c 100644 --- a/apps/origin-src/src/annotations/promotion.rs +++ b/apps/origin-src/src/annotations/promotion.rs @@ -3,8 +3,9 @@ //! //! A promotion claims what its original does, so it counts only once //! checked against it: [`Store::claim`] checks it, and only that makes a -//! [`Claim`]. Whatever reads results, such as the landing queue, reads -//! claims, through [`Annotations::claims`]. +//! [`Claim`]. Results carry over within a target, never from one target +//! to another, whose checks may differ. Whatever reads results, such as +//! the landing queue, reads claims, through [`Annotations::claims`]. //! //! Promotion runs on demand, never as a side effect of reading: `origin //! promote`, and the landing queue when it checks evidence, call @@ -18,7 +19,9 @@ use std::collections::HashSet; use origin_platform::Platform; use origin_shared::api::Annotation; -use origin_shared::{Principal, Record, RecordId, Statement, StatementId}; +use origin_shared::{ + Acceptance, Base, Principal, Record, RecordId, Statement, StatementId, TargetId, +}; use super::Annotations; use crate::store::{Store, StoreError}; @@ -158,12 +161,13 @@ impl Annotations<'_, P> { /// issue them for a caller who `sees` some records, without writing /// them, sorted by kind: for each /// [promotion group](Statement::promotion_group), the latest statement - /// about another record the caller sees with the same input digest as - /// the subject, unless its issuer superseded it. So a promotion never - /// copies a result, and its payload, that the caller can't read. Groups whose kind and issuer the - /// subject already has a result of, directly or promoted, are skipped: - /// a result that ran there wins. A statement has no snapshot, so - /// nothing is promoted to one. + /// about another record the caller sees, on the same target, with the + /// same input digest as the subject, unless its issuer superseded it. + /// So a promotion never copies a result, and its payload, that the + /// caller can't read. Groups whose kind and issuer the subject already + /// has a result of, directly or promoted, are skipped: a result that ran + /// there wins. A snapshot or a statement is on no target, so nothing is + /// promoted to one. /// /// # Errors /// @@ -175,7 +179,10 @@ impl Annotations<'_, P> { issuer: &Principal, mut sees: impl FnMut(RecordId) -> Result, ) -> Result, StoreError> { - let Some(snapshot) = self.store.snapshot_of(subject)? else { + let (Some(snapshot), Some(target)) = ( + self.store.snapshot_of(subject)?, + self.store.target_of(subject)?, + ) else { return Ok(Vec::new()); }; self.compute_digests()?; @@ -205,7 +212,10 @@ impl Annotations<'_, P> { continue; } let statement = self.store.get(id)?; - if self.superseded_by(id, &statement)?.is_none() && sees(statement.subject)? { + if self.superseded_by(id, &statement)?.is_none() + && self.store.target_of(statement.subject)? == Some(target) + && sees(statement.subject)? + { promotions.push(Statement { subject, issuer: issuer.clone(), @@ -254,11 +264,11 @@ impl Store

{ /// Checks the claim `statement`, `id`, makes. A statement that promotes /// none makes its own. A promotion must name an original the store has /// that is [promotable](Statement::is_promotable), match its kind, - /// verdict, inputs and payload, and be about a record whose input - /// digest equals the original's subject's: then it makes the - /// original's claim, or else the claim is invalid, which is the inner - /// error. Whether anyone superseded either is left to the reader, see - /// [`Annotations::claims`]. + /// verdict, inputs and payload, and be about a record on the same + /// target (see [`Store::target_of`]) whose input digest equals the + /// original's subject's: then it makes the original's claim, or else the + /// claim is invalid, which is the inner error. Whether anyone superseded + /// either is left to the reader, see [`Annotations::claims`]. /// /// # Errors /// @@ -294,6 +304,10 @@ impl Store

{ if let Some(&(field, _)) = differs.iter().find(|(_, differs)| *differs) { return Ok(Err(InvalidPromotion::Differs(field))); } + let target = self.target_of(statement.subject)?; + if target.is_none() || target != self.target_of(original.subject)? { + return Ok(Err(InvalidPromotion::OtherTarget)); + } let (Some(here), Some(there), Some(inputs)) = ( self.snapshot_of(statement.subject)?, self.snapshot_of(original.subject)?, @@ -311,6 +325,44 @@ impl Store

{ original: Some((original_id, original)), })) } + + /// The target `record` is on, for promotion: an acceptance's, or for a + /// revision, that of the acceptance it's based on, through the + /// revisions below it. A snapshot, a statement, or a record whose base + /// this store lacks, is on none. + /// + /// # Errors + /// + /// Fails if a record cannot be read. + pub fn target_of(&self, record: RecordId) -> Result, StoreError> { + let base = match record { + RecordId::Acceptance(id) => Base::Acceptance(id), + // The target a revision based on it would be on. + RecordId::Revision(id) => Base::Revision(id), + RecordId::Snapshot(_) | RecordId::Statement(_) => return Ok(None), + }; + Ok(self + .base_acceptance(base)? + .map(|acceptance| acceptance.body().target)) + } + + /// The acceptance `base` leads to, through the revisions below it, or + /// `None` if the store lacks a record on the way. + fn base_acceptance(&self, mut base: Base) -> Result, StoreError> { + let missing = |error| match error { + StoreError::NotFound(_) => Ok(None), + error => Err(error), + }; + loop { + base = match base { + Base::Acceptance(id) => return self.get(id).map(Some).or_else(missing), + Base::Revision(below) => match self.get(below) { + Ok(below) => below.base, + Err(error) => return missing(error), + }, + }; + } + } } /// Why a promotion makes no claim. @@ -332,17 +384,53 @@ pub enum InvalidPromotion { /// Its subject's input digest differs from the original's subject's. #[error("its subject's inputs differ from its original's")] InputsDiffer, + /// Its subject isn't on the target its original's subject is on. + #[error("its subject isn't on the target its original's is on")] + OtherTarget, } #[cfg(test)] mod tests { - use origin_shared::{EncodedObject, Kind, Verdict}; + use origin_shared::{EncodedObject, Kind, TargetName, Verdict}; use super::*; use crate::annotations::AnnotateError; - use crate::annotations::tests::{FILES, check, issue, principal, promote, revision, store}; + use crate::annotations::tests::{ + FILES, check, issue, principal, promote, revision, revision_on, store, + }; use crate::index::Index; + #[test] + fn results_promote_within_their_target_only() { + let (_dir, store) = store(); + let feature = TargetName::new("feature").unwrap(); + let result = check( + revision_on(&store, &feature, 1, &[], FILES), + "lint", + Verdict::Pass, + "node:ci", + Some((&["src"], false)), + ); + let original = issue(&store, &result); + + // The other target's checks may differ: nothing carries over. + let on_main = revision(&store, 2, &[], FILES); + assert_eq!(promote(&store, on_main).len(), 0); + let forged = Statement { + subject: RecordId::Revision(on_main), + issuer: principal("node:queue"), + promoted_from: Some(original), + ..result + }; + let id = forged.encode().id().into(); + assert!(matches!( + store.claim(id, forged).unwrap(), + Err(InvalidPromotion::OtherTarget) + )); + let on_feature = revision_on(&store, &feature, 3, &[], FILES); + assert_eq!(promote(&store, on_feature).len(), 1); + } + #[test] fn promotes_results_whose_inputs_are_unchanged() { let (_dir, store) = store(); diff --git a/apps/origin-src/src/cli.rs b/apps/origin-src/src/cli.rs index 6ee40c3e..d90e2091 100644 --- a/apps/origin-src/src/cli.rs +++ b/apps/origin-src/src/cli.rs @@ -198,8 +198,8 @@ enum WorkspaceCommand { #[arg(default_value = "@")] rev: String, }, - /// Carry results over to a revision from the records with the same - /// input digest. + /// Carry results over to a revision from the records on the same target + /// with the same input digest. Promote { /// The revision: `@`, a change for its head, or a revision. #[arg(default_value = "@")] @@ -345,7 +345,7 @@ struct AnnotateArgs { )] withdraw: bool, /// The files and directories the claim depended on, which lets it be - /// promoted to revisions where they're the same. + /// promoted to revisions on the same target where they're the same. #[arg(long, num_args = 1..)] inputs: Vec, /// The check's command and configuration: text, or the hex digest of diff --git a/apps/origin-src/src/queue/mod.rs b/apps/origin-src/src/queue/mod.rs index e45cc434..b1f9cf11 100644 --- a/apps/origin-src/src/queue/mod.rs +++ b/apps/origin-src/src/queue/mod.rs @@ -23,9 +23,9 @@ //! they may not change. On `policy`, the policy is the acceptance //! before's, never the one landing's ([`Policy::governing`]). Each of //! the target's requirements needs a [claim](crate::annotations::Claim) -//! about the revision that lands: results with the same input digest -//! are promoted to it first, and a valid promotion claims what its -//! original does, as its original's issuer. +//! about the revision that lands: results about the target's revisions +//! with the same input digest are promoted to it first, and a valid +//! promotion claims what its original does, as its original's issuer. //! 4. **Missing evidence** leaves the landing waiting, until the statements //! are issued and the landing is retried. //! 5. **Otherwise** this node signs an acceptance naming what landed, the @@ -33,11 +33,13 @@ //! //! Landing another target works the same way: its tip is merged from the //! newest acceptances both targets' histories share, and the requirements -//! apply to the merged snapshot. A merge that changes nothing lands -//! nothing if either tip is in the other's history: the target has the -//! source's already, or the source has nothing more. Otherwise it lands, -//! with no evidence then, so the target's history records the source's tip -//! and later merges start from it. +//! apply to the merge, a revision this node makes on the target's tip, as +//! for a rebase: so results about the same merge on another target, which +//! ran under checks that may differ, never count here. A merge that +//! changes nothing lands nothing if either tip is in the other's history: +//! the target has the source's already, or the source has nothing more. +//! Otherwise it lands, with no evidence then, so the target's history +//! records the source's tip and later merges start from it. //! //! The queue of each target lives in `queue//`: a `lock` held //! while landing, so landings on one target never race, and a file for each @@ -55,8 +57,9 @@ use origin_platform::{DurableWrite, LockMode, LockedFile, Native, Platform}; use origin_shared::access::{Level, PathAccess}; use origin_shared::landing::{Blocked, Landing, Pending, Proposal}; use origin_shared::{ - AcceptanceBody, AcceptanceId, ChangeId, EncodedObject, Head, NodeKey, Principal, PrincipalName, - Record, RecordId, RepoPath, RevisionId, Source, TargetId, TargetName, Tree, TreeId, + AcceptanceBody, AcceptanceId, Base, ChangeId, Digest, EncodedObject, Head, NodeKey, Principal, + PrincipalName, Record, RecordId, RepoPath, Revision, RevisionId, Source, TargetId, TargetName, + Tree, TreeId, }; use crate::annotations::{AnnotateError, Annotations}; @@ -225,14 +228,36 @@ impl<'a, P: Platform> Queue<'a, P> { } // Even a merge that changes nothing lands, so that the tip is in // the target's history, and later merges start from it. + // + // Checked as a revision on `onto`'s tip, as a rebase is, so results + // about it count on `onto` alone (see `Store::target_of`): the same + // merge on another target is checked with that target's checks. The + // same tips make the same revision, which a retry checks again. + let onto_tip = session.tip.acceptance; + let digest = Digest::of(&[*onto_tip.as_bytes(), *tip.acceptance.as_bytes()].concat()); + let mut change = [0; ChangeId::LEN]; + change.copy_from_slice(&digest.as_bytes()[..ChangeId::LEN]); + let description = EncodedObject::attachment(b""); + let merged = Revision { + change: ChangeId::from_bytes(change), + predecessors: Vec::new(), + base: Base::Acceptance(onto_tip), + deps: Vec::new(), + snapshot: merge.tree, + description: description.id().into(), + author: self.principal(), + } + .encode(); + let mut trees = merge.trees; + trees.push(description); session.submit(Candidate { kept_as: proposal.clone(), proposal, source: Source::Target(tip.acceptance), - subject: RecordId::Snapshot(merge.tree), + subject: RecordId::Revision(merged.id().into()), state: merge.tree, - trees: merge.trees, - revision: None, + trees, + revision: Some(merged), }) } diff --git a/apps/origin-src/src/queue/session.rs b/apps/origin-src/src/queue/session.rs index 09d96ba5..2eb1e015 100644 --- a/apps/origin-src/src/queue/session.rs +++ b/apps/origin-src/src/queue/session.rs @@ -53,9 +53,10 @@ pub(super) struct Candidate { pub(super) subject: RecordId, /// The target's snapshot once it lands. pub(super) state: TreeId, - /// The trees the merge made, to write first. + /// The objects the merge made, to write first. pub(super) trees: Vec, - /// The rebased revision, to write after the trees, if it is one. + /// The revision checked, to write after the trees, if new: a rebase, + /// or a target landing's merge. pub(super) revision: Option, } diff --git a/apps/origin-src/src/queue/tests/evidence.rs b/apps/origin-src/src/queue/tests/evidence.rs index 006d0635..cbec373f 100644 --- a/apps/origin-src/src/queue/tests/evidence.rs +++ b/apps/origin-src/src/queue/tests/evidence.rs @@ -60,6 +60,55 @@ fn missing_evidence_waits_and_promoted_results_count() { assert_eq!(promoted.issuer, "node:queue".parse().unwrap()); } +#[test] +fn results_from_another_target_never_count() { + let require = + "require = [{ kind = \"ci.test\", verdict = \"pass\", issuer = \"group:runners\" }]\n"; + let repo = Repo::new(&format!( + "[groups]\nrunners = [\"node:ci\"]\n[targets.main]\nauthority = \"node:queue\"\n\ + {require}[targets.release]\nauthority = \"node:queue\"\n{require}" + )); + let (main, overhaul) = (TargetName::MAIN, TargetName::new("overhaul").unwrap()); + let release = TargetName::new("release").unwrap(); + let queue = repo.queue(); + queue.create_target(&overhaul, &main).unwrap(); + queue.create_target(&release, &main).unwrap(); + let on_overhaul = Base::Acceptance(repo.tip(&overhaul).acceptance); + let tested = repo.revision(2, &[], on_overhaul, &[("src/lib.rs", "fn a() {}")]); + let change = ChangeId::from_bytes([2; 16]); + queue + .land_change(&mut repo.annotations(), change, &overhaul) + .unwrap(); + let issue = |statement: &Statement| { + Annotations::open(&repo.store) + .unwrap() + .issue(statement, None) + .unwrap(); + }; + issue(&ci_test(tested, "node:ci")); + let waits = |landing: &Landing| matches!(landing, Landing::Pending(pending) if matches!(pending.blocked, Blocked::Evidence(_))); + + // Landing the overhaul on `release` makes the same merge as on + // `main`. The result about it ran under `release`'s checks. + let Landing::Pending(on_release) = queue + .land_target(&mut repo.annotations(), &overhaul, &release) + .unwrap() + else { + panic!("the overhaul's own result doesn't count on release") + }; + issue(&Statement { + subject: on_release.subject, + ..ci_test(tested, "node:ci") + }); + + // Results ran under `overhaul`'s and `release`'s checks, which may + // differ from `main`'s: landing the overhaul waits for `main`'s. + let landing = queue + .land_target(&mut repo.annotations(), &overhaul, &main) + .unwrap(); + assert!(waits(&landing), "{landing:?}"); +} + #[test] fn promotes_only_what_the_submitter_sees() { let repo = Repo::new( diff --git a/apps/origin-src/src/service.rs b/apps/origin-src/src/service.rs index 46a7a2da..833763a9 100644 --- a/apps/origin-src/src/service.rs +++ b/apps/origin-src/src/service.rs @@ -366,7 +366,8 @@ impl OriginService { RecordId::Acceptance(id) => { self.store.get(id).map(|acceptance| acceptance.body().state) } - RecordId::Statement(_) | RecordId::Snapshot(_) => { + // Never a bare tree, whose paths would be read as others. + RecordId::Snapshot(_) | RecordId::Statement(_) => { return Err(ServiceError::NotASnapshot(record)); } }; diff --git a/apps/origin-src/src/workspace/annotate.rs b/apps/origin-src/src/workspace/annotate.rs index a65f7e9c..a2ca687d 100644 --- a/apps/origin-src/src/workspace/annotate.rs +++ b/apps/origin-src/src/workspace/annotate.rs @@ -323,8 +323,8 @@ impl Workspace { } /// `origin promote []`: carries promotable results over to a - /// revision, the working copy by default, from the records with the - /// same input digest, as statements this node issues. + /// revision, the working copy by default, from the records on the same + /// target with the same input digest, as statements this node issues. /// /// # Errors /// diff --git a/crates/origin-shared/src/landing.rs b/crates/origin-shared/src/landing.rs index a23a82d0..f5cd8be6 100644 --- a/crates/origin-shared/src/landing.rs +++ b/crates/origin-shared/src/landing.rs @@ -42,8 +42,8 @@ pub struct Pending { /// What lands. pub proposal: Proposal, /// What the evidence must be about: the revision that would land, or - /// the merged snapshot of a target. A conflict is about the revision, - /// or the acceptance, that didn't merge. + /// for a target, the revision of the merge on the tip it lands on. A + /// conflict is about the revision, or the acceptance, that didn't merge. pub subject: RecordId, /// Why it waits. pub blocked: Blocked, diff --git a/crates/origin-shared/src/object/statement.rs b/crates/origin-shared/src/object/statement.rs index 8b2b352f..221200f2 100644 --- a/crates/origin-shared/src/object/statement.rs +++ b/crates/origin-shared/src/object/statement.rs @@ -15,7 +15,8 @@ use crate::{ /// /// Statements are permanent. An issuer revises one by issuing another that /// supersedes it. A promoted statement carries another one over to a record -/// with the same input digest, pointing at it instead of copying its issuer. +/// on the same target with the same input digest, pointing at it instead of +/// copying its issuer. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, utoipa::ToSchema)] pub struct Statement { /// The record the claim is about. diff --git a/docs/openapi.json b/docs/openapi.json index 5b2b9587..1e3d3823 100644 --- a/docs/openapi.json +++ b/docs/openapi.json @@ -1972,7 +1972,7 @@ }, "subject": { "$ref": "#/components/schemas/RecordId", - "description": "What the evidence must be about: the revision that would land, or\nthe merged snapshot of a target. A conflict is about the revision,\nor the acceptance, that didn't merge." + "description": "What the evidence must be about: the revision that would land, or\nfor a target, the revision of the merge on the tip it lands on. A\nconflict is about the revision, or the acceptance, that didn't merge." } } }, @@ -2270,7 +2270,7 @@ }, "Statement": { "type": "object", - "description": "An attributable claim about another record: a check result, a review, a\nconflict, an abandonment.\n\nStatements are permanent. An issuer revises one by issuing another that\nsupersedes it. A promoted statement carries another one over to a record\nwith the same input digest, pointing at it instead of copying its issuer.", + "description": "An attributable claim about another record: a check result, a review, a\nconflict, an abandonment.\n\nStatements are permanent. An issuer revises one by issuing another that\nsupersedes it. A promoted statement carries another one over to a record\non the same target with the same input digest, pointing at it instead of\ncopying its issuer.", "required": [ "subject", "kind",