Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <id> flaky --neutral -m "Timeout on CI" # a statement about a statement
origin statements [<rev>] # the statements about a revision, and those about them
origin promote [<rev>] # carry results over from revisions with the same inputs
origin promote [<rev>] # carry results over from the target's revisions with the same inputs
origin gc # drop expired payloads (policy.toml) and pack objects

origin land [<change>] [--onto <target>] # land a change and the ones below it
Expand Down
20 changes: 17 additions & 3 deletions apps/origin-src/benches/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,6 +129,19 @@ fn snapshot(version: usize, docs: usize) -> Vec<EncodedObject> {
/// 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());
Expand All @@ -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(),
Expand Down
32 changes: 28 additions & 4 deletions apps/origin-src/src/annotations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -432,25 +433,48 @@ 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,
change: u8,
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()
}

Expand Down
124 changes: 106 additions & 18 deletions apps/origin-src/src/annotations/promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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};
Expand Down Expand Up @@ -158,12 +161,13 @@ impl<P: Platform> 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
///
Expand All @@ -175,7 +179,10 @@ impl<P: Platform> Annotations<'_, P> {
issuer: &Principal,
mut sees: impl FnMut(RecordId) -> Result<bool, StoreError>,
) -> Result<Vec<Statement>, 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()?;
Expand Down Expand Up @@ -205,7 +212,10 @@ impl<P: Platform> 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(),
Expand Down Expand Up @@ -254,11 +264,11 @@ impl<P: Platform> Store<P> {
/// 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
///
Expand Down Expand Up @@ -294,6 +304,10 @@ impl<P: Platform> Store<P> {
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)?,
Expand All @@ -311,6 +325,44 @@ impl<P: Platform> Store<P> {
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<Option<TargetId>, 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<Option<Acceptance>, 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.
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions apps/origin-src/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "@")]
Expand Down Expand Up @@ -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<RepoPath>,
/// The check's command and configuration: text, or the hex digest of
Expand Down
Loading