cob: Keep only the root's applied descendants in a history

Loading a collaborative object kept every entry reachable from its
refs, also entries that do not descend from the root. They were never
applied, but they were tips, so the next honest update used them as
parents. **Any user could thus make a delegate's change to one object
also apply to another**, or publish an entry with parents as the root
of a new object.

To address this, evaluation now follows three rules:
1. The root has no COB parents, or the object does not load.
2. Only entries that descend from the root are kept; every other entry is
removed together with the entries built on it.
3. An entry is applied only when every one of its COB parents was
applied. After evaluation, only applied entries remain, so new entries
build on the applied tips only.

A COB parent is a git parent other than the identity revision named by
`Rad-Resource` and the code commits named by `Rad-Related`. Honest
clients write a root without COB parents and build every entry on the
object's own tips, so honest histories evaluate as before.

Co-authored-by: Adrian Duke <adrian.duke@gmail.com>
This commit is contained in:
Yorgos Saslis
2026-11-06 18:13:51 +01:00
committed by Lorenz Leutgeb
co-authored by Adrian Duke
parent 4724dd4987
commit df62b66d9d
2 changed files with 234 additions and 3 deletions
+19 -3
View File
@@ -19,6 +19,8 @@ pub enum EvaluateError {
Signature(EntryId),
#[error("root entry '{0}' missing from graph")]
MissingRoot(EntryId),
#[error("root entry '{0}' has parents")]
RootHasParents(EntryId),
}
/// The graph of changes for a particular collaborative object
@@ -111,17 +113,27 @@ impl ChangeGraph {
if !root.valid_signatures() {
return Err(EvaluateError::Signature(root.id));
}
if !root.dependencies.is_empty() {
return Err(EvaluateError::RootHasParents(root.id));
}
// Evaluate the root separately, since we can't have a COB without a valid root.
// Then, traverse the graph starting from the root's dependents.
// Then, traverse the graph starting from the root's dependents, building the
// `applied` DAG directly so only applied descendants of `root` form the history.
let mut object =
T::init(&root.value, store).map_err(|e| EvaluateError::Init(Box::new(e)))?;
let children = Vec::from_iter(root.dependents.iter().cloned());
let manifest = root.manifest.clone();
let mut applied = Dag::root(root.id, root.value.clone());
let root = root.id;
self.graph.prune_by(
&children,
|_, entry, siblings| {
|id, entry, siblings| {
// Every COB parent must have been applied before the entry.
if !entry.dependencies.iter().all(|p| applied.contains(p)) {
log::debug!("Entry {} has a parent that was not applied.", entry.id);
return ControlFlow::Break(());
}
// Check the entry signatures are valid.
if !entry.valid_signatures() {
log::debug!("Signature verification failed for entry {}.", entry.id);
@@ -132,6 +144,10 @@ impl ChangeGraph {
log::debug!("Application of entry {} returned error: {err}", entry.id);
return ControlFlow::Break(());
}
applied.node(*id, entry.value.clone());
for parent in &entry.dependencies {
applied.dependency(*id, *parent);
}
ControlFlow::Continue(())
},
Self::chronological,
@@ -140,7 +156,7 @@ impl ChangeGraph {
Ok(CollaborativeObject {
manifest,
object,
history: History::new(root, self.graph),
history: History::new(root, applied),
id: self.object_id,
})
}
+215
View File
@@ -936,12 +936,14 @@ mod test {
use pretty_assertions::assert_eq;
use super::*;
use crate::cob::change::{Storage as _, Template};
use crate::cob::{ActorId, Reaction, store::CobWithType};
use crate::crypto::{Signer as _, SigningKey};
use crate::git::Oid;
use crate::issue::cache::Issues as _;
use crate::test::arbitrary;
use crate::{assert_matches, test};
use nonempty::NonEmpty;
#[test]
fn concurrency() {
@@ -1470,6 +1472,219 @@ mod test {
assert_eq!(issue.state(), &State::Open);
}
/// Point `signer`'s ref for `issue` at `entry`.
fn write_cob_ref(
repo: &storage::git::Repository,
signer: &SigningKey,
issue: &IssueId,
entry: Oid,
) {
let name =
crate::git::refs::storage::cob_with_namespace(signer.public_key(), &TYPENAME, issue);
repo.backend
.reference(name.as_str(), entry.into(), true, "test")
.unwrap();
}
/// Store a signed issue entry with the given COB parents, without moving any ref.
fn store_entry(
repo: &storage::git::Repository,
signer: &SigningKey,
parents: Vec<Oid>,
action: Action,
) -> Oid {
let resource = repo.identity_head().unwrap();
let template = Template {
type_name: TYPENAME.clone(),
tips: parents,
message: String::from("test"),
embeds: vec![],
contents: NonEmpty::new(store::encoding::encode(action).unwrap()),
};
repo.store(Some(resource), vec![], signer, template)
.unwrap()
.id
}
/// Write a plain Git commit that is not a COB entry.
fn plain_commit(repo: &storage::git::Repository) -> Oid {
let tree = repo.backend.treebuilder(None).unwrap().write().unwrap();
let tree = repo.backend.find_tree(tree).unwrap();
let sig = crate::git::raw::Signature::now("test", "test@example.com").unwrap();
repo.backend
.commit(None, &sig, &sig, "not a COB entry", &tree, &[])
.unwrap()
.into()
}
fn close() -> Action {
Action::Lifecycle {
state: State::Closed {
reason: CloseReason::Other,
},
}
}
#[test]
fn foreign_entries_are_not_part_of_the_object() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
let b = issues
.create(cob::Title::new("B").unwrap(), "B", &[], &[], [])
.unwrap()
.id;
let mallory = SigningKey::mock(!1);
// Mallory points her ref for A at the tip of B.
write_cob_ref(&repo, &mallory, &a, *b);
let object = cob::get::<Issue, _>(&*repo, &TYPENAME, &a)
.unwrap()
.unwrap();
assert_eq!(object.history.len(), 1);
assert_eq!(object.history.tips(), BTreeSet::from([*a]));
}
#[test]
fn honest_update_does_not_build_on_a_foreign_tip() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
let b = issues
.create(cob::Title::new("B").unwrap(), "B", &[], &[], [])
.unwrap()
.id;
let mallory = SigningKey::mock(!1);
// Mallory points her ref for A at the tip of B, then Maya closes A.
write_cob_ref(&repo, &mallory, &a, *b);
let closed = issues
.get_mut(&a)
.unwrap()
.lifecycle(State::Closed {
reason: CloseReason::Other,
})
.unwrap();
let entry = repo.load(closed).unwrap();
assert_eq!(entry.parents, vec![*a]);
}
#[test]
fn entry_with_an_unapplied_parent_is_not_applied() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
let b = issues
.create(cob::Title::new("B").unwrap(), "B", &[], &[], [])
.unwrap()
.id;
let mallory = SigningKey::mock(!1);
// Maya's close of A, written with the tip of B as an extra parent.
let closed = store_entry(&repo, &node.signer, vec![*a, *b], close());
// Mallory points her ref for B at it.
write_cob_ref(&repo, &mallory, &b, closed);
let issue = issues.get(&b).unwrap().unwrap();
assert_eq!(issue.state(), &State::Open);
}
#[test]
fn entry_with_parents_cannot_be_an_object_root() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
let mallory = SigningKey::mock(!1);
// Maya's comment on A, which Mallory then publishes as the root of a new issue.
let comment = store_entry(
&repo,
&node.signer,
vec![*a],
Action::Comment {
body: String::from("Hello"),
reply_to: None,
embeds: vec![],
},
);
let ghost = IssueId::from(comment);
write_cob_ref(&repo, &mallory, &ghost, comment);
assert!(!matches!(issues.get(&ghost), Ok(Some(_))));
let listed = issues
.list()
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(listed.len(), 1);
}
#[test]
fn disconnected_invalid_entry_is_not_adopted_by_an_update() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
let mallory = SigningKey::mock(!1);
// Mallory's unauthorized close of A, with no COB parents, under her own ref for A.
let invalid = store_entry(&repo, &mallory, vec![], close());
write_cob_ref(&repo, &mallory, &a, invalid);
let reply = issues
.get_mut(&a)
.unwrap()
.comment("Reply", *a, vec![])
.unwrap();
let entry = repo.load(reply).unwrap();
assert_eq!(entry.parents, vec![*a]);
assert_eq!(issues.get(&a).unwrap().unwrap().state(), &State::Open);
}
#[test]
fn entry_with_only_an_unloadable_parent_is_not_adopted_by_an_update() {
let test::setup::NodeWithRepo { node, repo, .. } = test::setup::NodeWithRepo::default();
let mut issues = Cache::no_cache(&*repo, &node.signer).unwrap();
let a = issues
.create(cob::Title::new("A").unwrap(), "A", &[], &[], [])
.unwrap()
.id;
let mallory = SigningKey::mock(!1);
// Mallory's entry whose only parent is not a COB entry, under her own ref for A.
let orphan = store_entry(&repo, &mallory, vec![plain_commit(&repo)], close());
write_cob_ref(&repo, &mallory, &a, orphan);
let reply = issues
.get_mut(&a)
.unwrap()
.comment("Reply", *a, vec![])
.unwrap();
let entry = repo.load(reply).unwrap();
assert_eq!(entry.parents, vec![*a]);
}
/// Point `mallory`'s ref for `issue` at a blob instead of a commit.
fn write_non_commit_cob_ref(repo: &storage::git::Repository, issue: &IssueId) {
let mallory = SigningKey::mock(!1);