Skip to content
Merged
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
1 change: 1 addition & 0 deletions .github/test-suite-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@
"pkgs/id/crates/id-core/tests/screen_parity.rs",
"pkgs/id/crates/id-core/tests/signer_bound_space.rs",
"pkgs/id/crates/id-core/tests/stream_lifecycle_model.rs",
"pkgs/id/crates/id-core/tests/carried_comment_projection.rs",
"pkgs/id/crates/id-core/tests/social_graph_traversal.rs",
"pkgs/id/crates/id-core/tests/succeeds_dag.rs",
"pkgs/id/crates/id-core/tests/swarm_bench.rs",
Expand Down
123 changes: 123 additions & 0 deletions pkgs/id/crates/id-core/src/graph/comment_projection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//! Carried ancestor comments for the V3 social projection (slice
//! A-GRAPH-9129-02, #10269; design part A §8, statements S5.44,
//! S5.45).
//!
//! Comments attach to the exact version they were authored against;
//! they never change identity, target or bytes when the content moves
//! on. This projection carries eligible ancestor comments forward onto
//! a selected descendant: each appears once, with its immutable
//! original target kept as its origin — preserved transitively, so a
//! comment authored on v1 still names v1 when it is shown on v3 — and
//! an old-version marker whenever that target is not the selected
//! version itself.
//!
//! Access, moderation and deletion filters still apply to carried
//! comments, and the projection contains only comments in the
//! selected version's own ancestry: sibling branches never leak.

use std::collections::{HashMap, HashSet, VecDeque};

use super::social::{SocialObject, SocialObjectKind};
use super::term::ObjectRef;
use super::version::{ref_order_key, VersionDag};

/// Projection-side filters over comment identities.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CommentFilters {
/// Comments hidden by moderation in this projection.
pub hidden: HashSet<ObjectRef>,
/// Comments deleted in this projection.
pub deleted: HashSet<ObjectRef>,
}

/// One comment in a carried-forward view.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CarriedComment {
/// The comment's immutable identity.
pub comment_id: ObjectRef,
/// The version the comment was authored against; never rewritten
/// by the carry.
pub original_target: ObjectRef,
/// The comment's origin, preserved transitively: always the
/// original target, however many versions the carry spans.
pub origin_version: ObjectRef,
/// True when the comment was authored against an older version
/// than the selected one.
pub old_version: bool,
/// `succeeds` steps from the selected version back to the
/// comment's original target (0 for comments on the selected
/// version itself).
pub distance: usize,
}

/// The default descendant view of `selected`: its own comments plus
/// every eligible ancestor comment carried forward.
///
/// Eligibility is decided against the selected version's ancestry in
/// `dag` (breadth-first over the reverse `succeeds` index, which also
/// fixes each target's shortest distance). A comment qualifies when
/// it is a [`SocialObjectKind::Comment`] whose parent is the selected
/// version or one of its ancestors, when neither filter names it, and
/// when the `authorized` access predicate accepts it. Duplicate
/// listings of one comment ID yield one entry. The result is ordered
/// by distance, then by the deterministic reference order, so the
/// selected version's own comments come first and nearer ancestors
/// before farther ones.
#[must_use]
pub fn carried_comments(
dag: &VersionDag,
comments: &[SocialObject],
selected: &ObjectRef,
filters: &CommentFilters,
authorized: impl Fn(&SocialObject) -> bool,
) -> Vec<CarriedComment> {
let mut distance: HashMap<ObjectRef, usize> = HashMap::new();
let mut queue: VecDeque<ObjectRef> = VecDeque::new();
distance.insert(*selected, 0);
queue.push_back(*selected);
while let Some(at) = queue.pop_front() {
let steps = distance[&at];
for predecessor in dag.predecessors_of(&at) {
if !distance.contains_key(&predecessor) {
distance.insert(predecessor, steps + 1);
queue.push_back(predecessor);
}
}
}

let mut seen: HashSet<ObjectRef> = HashSet::new();
let mut out: Vec<CarriedComment> = Vec::new();
for comment in comments {
if comment.kind != SocialObjectKind::Comment {
continue;
}
let Some(target) = comment.parent_ref else {
continue;
};
let Some(&steps) = distance.get(&target) else {
continue;
};
if filters.hidden.contains(&comment.object_id)
|| filters.deleted.contains(&comment.object_id)
{
continue;
}
if !authorized(comment) {
continue;
}
if !seen.insert(comment.object_id) {
continue;
}
out.push(CarriedComment {
comment_id: comment.object_id,
original_target: target,
origin_version: target,
old_version: steps > 0,
distance: steps,
});
}
out.sort_by(|a, b| {
(a.distance, ref_order_key(&a.comment_id)).cmp(&(b.distance, ref_order_key(&b.comment_id)))
});
out
}
1 change: 1 addition & 0 deletions pkgs/id/crates/id-core/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

pub mod append_list;
pub mod attribute;
pub mod comment_projection;
pub mod cross_signing;
pub mod cursor;
pub mod delegation;
Expand Down
253 changes: 253 additions & 0 deletions pkgs/id/crates/id-core/tests/carried_comment_projection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
//! Carried ancestor comments (slice A-GRAPH-9129-02, issue #10269;
//! design part A §8, statements S5.44, S5.45).
//!
//! Oracle: **model**. Fixed fixtures pin the transitive origin, the
//! old-version marker, shortest-path distance over diamond ancestry,
//! sibling-branch isolation and the access/moderation/deletion
//! filters; a naive layered-expansion model over the same DAG checks
//! the full result on a mixed fixture.

#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::collections::HashSet;

use id_core::graph::comment_projection::{carried_comments, CommentFilters};
use id_core::graph::social::{SocialObject, SocialObjectKind};
use id_core::graph::term::{ObjectRef, Term};
use id_core::graph::version::{SucceedsEdge, Version, VersionDag};
use id_core::id_shape::{ContentId, HashProfile};
use id_core::signed_envelope::EnvelopeId;

fn oid(n: u8) -> ObjectRef {
ObjectRef::Envelope(EnvelopeId::from_content_id(ContentId::new(
HashProfile::RawBlake3_256,
[n; 32],
)))
}

fn dag(versions: &[u8], edges: &[(u8, u8)]) -> VersionDag {
let mut dag = VersionDag::new();
for n in versions {
dag.add_version(Version {
id: oid(*n),
logical_object: Term::typed(7, b"doc".to_vec()),
schema: 1,
})
.unwrap();
}
for (from, to) in edges {
dag.admit(SucceedsEdge::new(oid(*from), oid(*to))).unwrap();
}
dag
}

fn comment(id: u8, target: Option<u8>) -> SocialObject {
SocialObject {
object_id: oid(id),
kind: SocialObjectKind::Comment,
author: "alice".to_owned(),
created_claim: u64::from(id),
parent_ref: target.map(oid),
content_ref: oid(100 + id),
schema_version: 1,
succeeds_ref: None,
}
}

fn no_filters() -> CommentFilters {
CommentFilters::default()
}

fn summary(
dag: &VersionDag,
comments: &[SocialObject],
selected: u8,
filters: &CommentFilters,
) -> Vec<(u8, u8, bool, usize)> {
carried_comments(dag, comments, &oid(selected), filters, |_| true)
.iter()
.map(|carried| {
let digest = |reference: ObjectRef| {
let ObjectRef::Envelope(id) = reference else {
panic!("fixture refs are envelopes");
};
id.content_id().digest()[0]
};
(
digest(carried.comment_id),
digest(carried.origin_version),
carried.old_version,
carried.distance,
)
})
.collect()
}

#[test]
fn chain_carry_keeps_transitive_origin_and_marks_old_versions() {
let dag = dag(&[1, 2, 3], &[(1, 2), (2, 3)]);
let comments = vec![
comment(11, Some(1)),
comment(12, Some(2)),
comment(13, Some(3)),
];
// The v1 comment shown on v3 still names v1 as its origin, two
// steps back; only the comment on v3 itself is unmarked.
assert_eq!(
summary(&dag, &comments, 3, &no_filters()),
vec![(13, 3, false, 0), (12, 2, true, 1), (11, 1, true, 2)]
);
}

#[test]
fn sibling_branches_do_not_leak() {
let dag = dag(&[1, 2, 3], &[(1, 2), (1, 3)]);
let comments = vec![comment(11, Some(1)), comment(13, Some(3))];
assert_eq!(
summary(&dag, &comments, 2, &no_filters()),
vec![(11, 1, true, 1)]
);
assert_eq!(
summary(&dag, &comments, 3, &no_filters()),
vec![(13, 3, false, 0), (11, 1, true, 1)]
);
}

#[test]
fn diamond_ancestry_carries_once_at_shortest_distance() {
let dag = dag(&[1, 2, 3, 4], &[(1, 2), (1, 3), (2, 4), (3, 4)]);
// The same comment listed twice still appears once.
let comments = vec![comment(11, Some(1)), comment(11, Some(1))];
assert_eq!(
summary(&dag, &comments, 4, &no_filters()),
vec![(11, 1, true, 2)]
);
}

#[test]
fn access_moderation_and_deletion_filters_still_apply() {
let dag = dag(&[1, 2], &[(1, 2)]);
let mut stranger = comment(14, Some(1));
stranger.author = "mallory".to_owned();
let comments = vec![
comment(11, Some(1)),
comment(12, Some(1)),
comment(13, Some(1)),
stranger,
comment(15, Some(2)),
comment(16, Some(1)),
];
let filters = CommentFilters {
hidden: HashSet::from([oid(12)]),
deleted: HashSet::from([oid(13)]),
};
let got = carried_comments(&dag, &comments, &oid(2), &filters, |object| {
object.author != "mallory"
});
let ids: Vec<u8> = got
.iter()
.map(|carried| {
let ObjectRef::Envelope(id) = carried.comment_id else {
panic!("fixture refs are envelopes");
};
id.content_id().digest()[0]
})
.collect();
assert_eq!(ids, vec![15, 11, 16]);
}

#[test]
fn unrelated_and_malformed_comments_are_ignored() {
let dag = dag(&[1, 2], &[(1, 2)]);
let mut topic = comment(21, Some(1));
topic.kind = SocialObjectKind::Topic;
let comments = vec![
topic,
comment(22, None),
comment(23, Some(9)),
comment(24, Some(1)),
];
assert_eq!(
summary(&dag, &comments, 2, &no_filters()),
vec![(24, 1, true, 1)]
);
}

/// Naive model: eligible targets are the selected version plus
/// `VersionDag::ancestors`; distance is found by layered frontier
/// expansion from the selected version.
fn model_summary(
dag: &VersionDag,
comments: &[SocialObject],
selected: u8,
) -> Vec<(u8, u8, bool, usize)> {
let mut frontier = vec![oid(selected)];
let mut distance: std::collections::HashMap<ObjectRef, usize> =
frontier.iter().map(|reference| (*reference, 0)).collect();
let mut steps = 0;
loop {
steps += 1;
let mut next = Vec::new();
for at in &frontier {
for predecessor in dag.predecessors_of(at) {
if !distance.contains_key(&predecessor) {
distance.insert(predecessor, steps);
next.push(predecessor);
}
}
}
if next.is_empty() {
break;
}
frontier = next;
}
let mut seen = HashSet::new();
let mut out = Vec::new();
for object in comments {
if object.kind != SocialObjectKind::Comment {
continue;
}
let Some(target) = object.parent_ref else {
continue;
};
let Some(&steps) = distance.get(&target) else {
continue;
};
if !seen.insert(object.object_id) {
continue;
}
let digest = |reference: ObjectRef| {
let ObjectRef::Envelope(id) = reference else {
panic!("fixture refs are envelopes");
};
id.content_id().digest()[0]
};
out.push((digest(object.object_id), digest(target), steps > 0, steps));
}
out.sort_by_key(|(id, _, _, steps)| (*steps, *id));
out
}

#[test]
fn mixed_fixture_matches_naive_model() {
let dag = dag(
&[1, 2, 3, 4, 5, 6],
&[(1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (3, 6)],
);
let comments = vec![
comment(11, Some(1)),
comment(12, Some(2)),
comment(13, Some(3)),
comment(14, Some(4)),
comment(15, Some(5)),
comment(16, Some(6)),
comment(17, Some(3)),
];
for selected in [4, 5, 6] {
assert_eq!(
summary(&dag, &comments, selected, &no_filters()),
model_summary(&dag, &comments, selected),
"selected version {selected}"
);
}
}