Skip to content
Merged
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
356 changes: 344 additions & 12 deletions pkgs/id/src/boards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,39 +923,118 @@ 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.
pub fn board_object_set(&self, board: &ArtifactHash) -> BTreeSet<ArtifactHash> {
let mut set: BTreeSet<ArtifactHash> = 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<ArtifactHash> = set.iter().cloned().collect();
while let Some(hash) = frontier.pop() {
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<ArtifactHash> {
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<ArtifactHash> = 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(),
Comment on lines +993 to +1034

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Include dependencies of objects cited in the source frontier

When a valid fork record cites a source-board post without also listing its thread in source_frontier, this adds the post to the export but omits the thread: the initial seed contains only fork-board objects, and the dependency traversal has no case for Post. In a fresh replica, validate_links rejects the post because its thread is missing, then rejects the fork record because the post is missing, so replicate_board_from stalls instead of completing. The same problem affects frontier threads with forum/rules references and objects requiring moderator grants. Expand the closure to include the acceptance dependencies of cited source objects, not just the frontier hashes themselves.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 18cb2a8 (branch rebased onto current main first; the original commit is now 5dbd708).

board_object_set's closure now follows the acceptance dependencies of every object kind — the exact references validate_links requires to be present — instead of only Board/Theme/BoardFork references: Post pulls its board, thread, parent post, attachments, and embed target; Thread pulls its forum and rules references; Supersede/Tombstone/BoardListing pull their targets and superseded records; RoleAttestation pulls its supersedes chain; and the moderator-gated kinds (Rules, Forum, ModerationAction) additionally pull the RoleAttestation grant that gives their author moderator authority on that board.

Regression test fork_frontier_post_pulls_its_acceptance_dependencies: a fork record citing only a source post in its frontier now exports the post, its thread, the thread's forum and rules, and the moderator grant, and replicate_board_from into a fresh graph completes with an identical fork-board set. Verified on the laptop: cargo test --lib boards:: — 32 passed, 0 failed.

— queue task 13400, lane code-u

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());
}
}
Expand Down Expand Up @@ -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);
Expand Down
Loading