feat(graph): a hub stands for its implicit tag, no separate tag node beside it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dasemuandClaude Opus 5.5 committed 2026-10-26 04:11:27 +01:00
1 parent 58c3ee9611
commit ad62a7d258
4 files changed
+71 -27

No files matched your search

+12 -3
View File
@@ -285,7 +285,8 @@ pub(crate) fn build(g: &Graph, seq: i64) -> Result<GraphData> {
}
/// Tags are virtual nodes (`tag:<key>`, appended after the pages): every page carrying a tag,
/// listed or implied by a type, gets one `TAGGED` edge to it.
/// listed or implied by a type, gets one `TAGGED` edge to it. A tag named like a hub already
/// in the graph (a hub's implicit tag) uses the hub's node instead of a second one.
fn tagged(
g: &Graph,
index: &HashMap<String, u32>,
@@ -301,7 +302,12 @@ fn tagged(
.conn()
.prepare("SELECT tag_key, page_id FROM page_tags ORDER BY tag_key, page_id")?;
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
let mut tag_index: HashMap<String, u32> = HashMap::new();
let mut tag_index: HashMap<String, u32> = nodes
.iter()
.enumerate()
.filter(|(_, n)| n.is_hub)
.map(|(i, n)| (title_key(&n.title), i as u32))
.collect();
for row in rows {
let (key, page) = row?;
let Some(&s) = index.get(&page) else {
@@ -325,7 +331,10 @@ fn tagged(
});
nodes.len() as u32 - 1
});
b.add(s, t, edge_kind::TAGGED, 1.0);
// A hub's members already hang from it by a CONTAINS edge.
if !b.edges.contains_key(&(t, s, edge_kind::CONTAINS)) {
b.add(s, t, edge_kind::TAGGED, 1.0);
}
}
Ok(())
}
+41 -11
View File
@@ -364,6 +364,9 @@ fn legacy_candidates_and_purge() {
let (keep, _) = page_with(&g, "Keep", &["x"]);
page_with(&g, "contents", &["- x"]);
page_with(&g, "now", &[]);
page_with(&g, "Blank", &["", " "]);
let (typed, _) = page_with(&g, "Typed", &[]);
g.set_page_type(typed, Some("persona".into())).unwrap();
g.create_hub("later".into(), String::new()).unwrap();
{
let shared = g.shared();
@@ -389,6 +392,9 @@ fn legacy_candidates_and_purge() {
assert!(!by("Template").preselected);
assert_eq!(by("Template").reason, "type:: system");
assert!(c.iter().all(|x| x.title != "Keep"));
assert_eq!(by("Blank").reason, "Empty");
assert!(by("Blank").preselected, "only blank blocks");
assert!(c.iter().all(|x| x.title != "Typed"), "a type is content");
// A bad id rejects the call before anything is deleted.
assert!(g.purge_pages(vec![todo.clone(), "nope".into()]).is_err());
@@ -453,6 +459,32 @@ fn tags_are_virtual_graph_nodes_with_their_own_edge_kind() {
assert_eq!(g.pages_with_tag("IDEA".into()).unwrap().len(), 2);
}
#[test]
fn a_tag_named_like_a_hub_uses_the_hub_node() {
let (_d, g) = open();
let hub = g.create_hub("Proyectos".into(), String::new()).unwrap().id;
let (member, _) = page_with(&g, "Synapse", &["x"]);
let (loose, _) = page_with(&g, "Otro", &["x"]);
g.add_to_hub(member.clone(), hub.clone()).unwrap();
g.add_page_tag(member, "proyectos".into()).unwrap();
g.add_page_tag(loose, "Proyectos".into()).unwrap();
let data = g.graph_data().unwrap();
assert!(
data.nodes.iter().all(|n| !n.is_tag),
"no tag node beside the hub"
);
let title = |i: u32| data.nodes[i as usize].title.clone();
let tagged: Vec<(String, String)> = (0..data.kinds.len())
.filter(|&e| data.kinds[e] == edge_kind::TAGGED)
.map(|e| (title(data.sources[e]), title(data.targets[e])))
.collect();
assert_eq!(
tagged,
[("Otro".to_string(), "Proyectos".to_string())],
"members already hang from the hub; other pages tagged with it point at it"
);
}
#[test]
fn page_type_and_tag_renaming_round_trip_through_the_api() {
let (_d, g) = open();
@@ -475,7 +507,7 @@ fn page_type_and_tag_renaming_round_trip_through_the_api() {
}
#[test]
fn hub_members_point_at_one_virtual_tag_node_named_after_the_hub() {
fn a_hub_tag_and_a_same_name_type_need_no_tag_node_beside_the_hub() {
let (_d, g) = open();
let hub = g.create_hub("Anime".into(), String::new()).unwrap();
let (a, _) = page_with(&g, "Naruto", &["x"]);
@@ -484,16 +516,14 @@ fn hub_members_point_at_one_virtual_tag_node_named_after_the_hub() {
g.add_to_hub(b.clone(), hub.id.clone()).unwrap();
g.set_page_type(b.clone(), Some("anime".into())).unwrap();
let data = g.graph_data().unwrap();
let tags: Vec<_> = data.nodes.iter().filter(|n| n.is_tag).collect();
assert_eq!(tags.len(), 1, "hub tag and same-name type merge");
assert_eq!(tags[0].title, "Anime");
let target = data.nodes.iter().position(|n| n.is_tag).unwrap() as u32;
let mut from: Vec<String> = (0..data.kinds.len())
.filter(|&e| data.kinds[e] == edge_kind::TAGGED && data.targets[e] == target)
.map(|e| data.nodes[data.sources[e] as usize].title.clone())
.collect();
from.sort();
assert_eq!(from, ["Bleach", "Naruto"]);
assert!(
data.nodes.iter().all(|n| !n.is_tag),
"the hub stands for its tag"
);
assert!(
!data.kinds.contains(&edge_kind::TAGGED),
"members already hang from the hub"
);
let cat = g.tag_catalog().unwrap();
assert_eq!(
(cat[0].name.to_lowercase().as_str(), cat[0].count),