diff --git a/pkgs/id/src/boards.rs b/pkgs/id/src/boards.rs index f678244063..0a5c974d86 100644 --- a/pkgs/id/src/boards.rs +++ b/pkgs/id/src/boards.rs @@ -923,13 +923,18 @@ impl BoardGraph { } /// Every hash in one board's replication set: the board root plus - /// every object scoped to it, plus the dependency closure a fresh - /// graph needs before [`Self::validate_links`] will accept those - /// objects — the root's `theme_ref` theme and `forked_from` - /// ancestor root, recursively (an ancestor's own theme and fork - /// lineage, and a theme's fork lineage). Themes and ancestor roots - /// name no board (`board_ref` is `None`), so a scope-only filter - /// would export a set that can never be accepted elsewhere: + /// every object scoped to it, plus the fork provenance record when + /// this board is a fork (the [`BoardFork`] record names no board in + /// `board`, only `source_board`/`fork_board`, so scope alone would + /// miss it and fork provenance would never replicate), plus the + /// dependency closure a fresh graph needs before + /// [`Self::validate_links`] will accept those objects — the root's + /// `theme_ref` theme and `forked_from` ancestor root, recursively + /// (an ancestor's own theme and fork lineage, a theme's fork + /// lineage, and a fork record's source root, fork root, and cited + /// source frontier). Themes, ancestor roots, and fork records name + /// no board (`board_ref` is `None`), so a scope-only filter would + /// export a set that can never be accepted elsewhere: /// themed and forked boards would stall replication forever /// (Codex P1 on PR #4048). This set — not a server-side copy — is /// what hosts replicate and converge on. @@ -937,7 +942,11 @@ impl BoardGraph { let mut set: BTreeSet = self .objects .iter() - .filter(|(hash, signed)| *hash == board || signed.object.board_ref() == Some(board)) + .filter(|(hash, signed)| { + *hash == board + || signed.object.board_ref() == Some(board) + || matches!(&signed.object, BoardObject::BoardFork(fork) if &fork.fork_board == board) + }) .map(|(hash, _)| hash.clone()) .collect(); let mut frontier: Vec = set.iter().cloned().collect(); @@ -945,17 +954,87 @@ impl BoardGraph { let Some(signed) = self.objects.get(&hash) else { continue; }; - let dependencies: Vec<&ArtifactHash> = match &signed.object { + // The closure follows the acceptance dependencies of every + // object kind -- exactly the references validate_links + // requires to already be present -- not just roots, themes, + // and fork records. A frontier-cited source object (a post, + // say) is scoped to the *source* board, so the seed never + // contains its thread, that thread's forum/rules, its parent + // post, or its attachments; without traversing those, the + // export can never be accepted by a fresh graph and + // replication stalls (Codex P1 on PR #4545). Moderator-gated + // kinds (rules, forums, moderation actions) additionally + // depend on the grant that gives their author authority. + let author = signed.artifact.author.clone(); + let moderator_grants = |board: &ArtifactHash| -> Vec { + self.objects + .iter() + .filter_map(|(grant_hash, grant)| match &grant.object { + BoardObject::RoleAttestation(value) + if &value.board == board + && value.member == author + && value.role == BoardRole::Moderator + && value.granted => + { + Some(grant_hash.clone()) + } + _ => None, + }) + .collect() + }; + let dependencies: Vec = match &signed.object { BoardObject::Board(body) => body .forked_from .iter() .chain(body.theme_ref.iter()) + .cloned() + .collect(), + BoardObject::Theme(body) => body.forked_from.iter().cloned().collect(), + BoardObject::BoardFork(body) => body + .source_frontier + .iter() + .chain([&body.source_board, &body.fork_board]) + .cloned() + .collect(), + BoardObject::Rules(body) => std::iter::once(body.board.clone()) + .chain(moderator_grants(&body.board)) + .collect(), + BoardObject::Forum(body) => std::iter::once(body.board.clone()) + .chain(moderator_grants(&body.board)) + .collect(), + BoardObject::Thread(body) => std::iter::once(body.board.clone()) + .chain(body.forum_ref.iter().cloned()) + .chain(body.rules_ref.iter().cloned()) + .collect(), + BoardObject::Post(body) => std::iter::once(body.board.clone()) + .chain(std::iter::once(body.thread.clone())) + .chain(body.parent_post.iter().cloned()) + .chain(body.attachments.iter().cloned()) + .chain(body.embed_ref.iter().map(|embed| embed.object.clone())) + .collect(), + BoardObject::Attachment(body) => vec![body.board.clone()], + BoardObject::Membership(body) => vec![body.board.clone()], + BoardObject::RoleAttestation(body) => std::iter::once(body.board.clone()) + .chain(body.supersedes.iter().cloned()) + .collect(), + BoardObject::ModerationAction(body) => std::iter::once(body.board.clone()) + .chain(std::iter::once(body.target.clone())) + .chain(moderator_grants(&body.board)) + .collect(), + BoardObject::Supersede(body) => vec![ + body.board.clone(), + body.previous.clone(), + body.replacement.clone(), + ], + BoardObject::Tombstone(body) => { + vec![body.board.clone(), body.target.clone()] + } + BoardObject::BoardListing(body) => std::iter::once(body.board.clone()) + .chain(body.supersedes.iter().cloned()) .collect(), - BoardObject::Theme(body) => body.forked_from.iter().collect(), - _ => Vec::new(), }; for dependency in dependencies { - if self.objects.contains_key(dependency) && set.insert(dependency.clone()) { + if self.objects.contains_key(&dependency) && set.insert(dependency.clone()) { frontier.push(dependency.clone()); } } @@ -3135,6 +3214,259 @@ mod tests { ); } + #[test] + fn fork_record_is_in_fork_board_replication_set() { + // #4081: a BoardFork record names no `board`, so the scope-only + // seed left it in no board's replication set and fork provenance + // never replicated. The record belongs to the fork board's set + // (it is signed by the fork board's creator), and the closure + // pulls both roots plus the cited source frontier. + let owner = key(7); + let forker = key(8); + let mut source = BoardGraph::default(); + let ancestor = source.insert(board(&owner)).unwrap(); + let frontier_thread = source + .insert(thread(&owner, ancestor.clone(), "fork frontier")) + .unwrap(); + let fork_root = source + .insert( + SignedBoardObject::sign( + &forker, + 1, + "", + BoardObject::Board(Board { + board_id: "forked-board".to_owned(), + forked_from: Some(ancestor.clone()), + theme_ref: None, + }), + ) + .unwrap(), + ) + .unwrap(); + let fork_record = source + .insert( + SignedBoardObject::sign( + &forker, + 1, + "", + BoardObject::BoardFork(BoardFork { + source_board: ancestor.clone(), + fork_board: fork_root.clone(), + source_frontier: vec![ancestor.clone(), frontier_thread.clone()], + }), + ) + .unwrap(), + ) + .unwrap(); + + let fork_set = source.board_object_set(&fork_root); + assert!(fork_set.contains(&fork_record)); + assert!(fork_set.contains(&fork_root)); + assert!(fork_set.contains(&ancestor)); + assert!(fork_set.contains(&frontier_thread)); + // The provenance record travels with the fork board, not the + // source board: source replicas must not require an object + // their board's creator never signed. + assert!(!source.board_object_set(&ancestor).contains(&fork_record)); + } + + #[test] + fn fork_record_replicates_end_to_end_and_gates_completeness() { + // #4081: replicate_board_from into a fresh graph accepts and + // transfers the fork record, and a replica missing it reports + // Incomplete — never a silent Complete. + let owner = key(7); + let forker = key(8); + let ancestor_signed = board(&owner); + // Content hashes are deterministic over the signed artifact, so + // the thread can name the ancestor root before either is + // inserted. + let frontier_signed = thread(&owner, ancestor_signed.hash().unwrap(), "fork frontier"); + let mut source = BoardGraph::default(); + let ancestor = source.insert(ancestor_signed.clone()).unwrap(); + let frontier_thread = source.insert(frontier_signed.clone()).unwrap(); + let fork_root_signed = SignedBoardObject::sign( + &forker, + 1, + "", + BoardObject::Board(Board { + board_id: "forked-board".to_owned(), + forked_from: Some(ancestor.clone()), + theme_ref: None, + }), + ) + .unwrap(); + let fork_root = source.insert(fork_root_signed.clone()).unwrap(); + let fork_record_signed = SignedBoardObject::sign( + &forker, + 1, + "", + BoardObject::BoardFork(BoardFork { + source_board: ancestor.clone(), + fork_board: fork_root.clone(), + source_frontier: vec![ancestor.clone(), frontier_thread.clone()], + }), + ) + .unwrap(); + let fork_record = source.insert(fork_record_signed.clone()).unwrap(); + + let mut replica = BoardGraph::default(); + let report = replica.replicate_board_from(&source, &fork_root).unwrap(); + assert_eq!(report.status.state, ReplicaState::Complete); + assert!(report.transferred.contains(&fork_record)); + assert_eq!( + replica.board_object_set(&fork_root), + source.board_object_set(&fork_root) + ); + + let mut partial = BoardGraph::default(); + partial.insert(ancestor_signed).unwrap(); + partial.insert(frontier_signed).unwrap(); + partial.insert(fork_root_signed).unwrap(); + let status = partial.replica_status_against(&fork_root, &source); + assert_eq!(status.state, ReplicaState::Incomplete); + assert!(status.missing.contains(&fork_record)); + assert!(status.present_objects < status.expected_objects); + } + + #[test] + fn fork_frontier_post_pulls_its_acceptance_dependencies() { + // Codex P1 on PR #4545: when a fork record cites a source-board + // post in its frontier without also listing the post's thread, + // the closure must still pull the thread -- and the thread's + // forum and rules, and the moderator grant behind those -- or a + // fresh replica rejects the post, then the fork record, and + // replication stalls instead of completing. + let owner = key(7); + let moderator = key(11); + let moderator_hex = hex_encode(&moderator.verifying_key().to_bytes()); + let forker = key(8); + let mut source = BoardGraph::default(); + let root = source.insert(board(&owner)).unwrap(); + let grant = source + .insert( + SignedBoardObject::sign( + &owner, + 2, + "", + BoardObject::RoleAttestation(RoleAttestation { + board: root.clone(), + member: moderator_hex, + role: BoardRole::Moderator, + granted: true, + supersedes: None, + }), + ) + .unwrap(), + ) + .unwrap(); + let rules = source + .insert( + SignedBoardObject::sign( + &moderator, + 1, + "", + BoardObject::Rules(Rules { + board: root.clone(), + revision: 1, + text: "be kind".to_owned(), + }), + ) + .unwrap(), + ) + .unwrap(); + let forum = source + .insert( + SignedBoardObject::sign( + &moderator, + 2, + "", + BoardObject::Forum(Forum { + board: root.clone(), + name: "general".to_owned(), + }), + ) + .unwrap(), + ) + .unwrap(); + let thread = source + .insert( + SignedBoardObject::sign( + &owner, + 2, + "", + BoardObject::Thread(Thread { + board: root.clone(), + forum_ref: Some(forum.clone()), + rules_ref: Some(rules.clone()), + title: "cited thread".to_owned(), + }), + ) + .unwrap(), + ) + .unwrap(); + let cited_post = source + .insert(post( + &owner, + root.clone(), + thread.clone(), + "cited by the fork", + )) + .unwrap(); + let fork_root = source + .insert( + SignedBoardObject::sign( + &forker, + 1, + "", + BoardObject::Board(Board { + board_id: "forked-board".to_owned(), + forked_from: Some(root.clone()), + theme_ref: None, + }), + ) + .unwrap(), + ) + .unwrap(); + let fork_record = source + .insert( + SignedBoardObject::sign( + &forker, + 1, + "", + BoardObject::BoardFork(BoardFork { + source_board: root.clone(), + fork_board: fork_root.clone(), + source_frontier: vec![cited_post.clone()], + }), + ) + .unwrap(), + ) + .unwrap(); + + let fork_set = source.board_object_set(&fork_root); + for expected in [ + &fork_record, + &fork_root, + &root, + &cited_post, + &thread, + &forum, + &rules, + &grant, + ] { + assert!(fork_set.contains(expected), "missing {expected:?}"); + } + + let mut replica = BoardGraph::default(); + let report = replica.replicate_board_from(&source, &fork_root).unwrap(); + assert_eq!(report.status.state, ReplicaState::Complete); + assert_eq!( + replica.board_object_set(&fork_root), + source.board_object_set(&fork_root) + ); + } + #[test] fn stale_replica_is_explicit_not_silent() { let owner = key(7);