incipit 0.2.3

Workspace identity vocabulary for Mere: the stable ids of a graph and a session, and view-local curation (subgraph specifications and folds), and, behind the `arrangement` feature, forme: the workbench arrangement authority. No dependency on the graph itself.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Copyright 2026 Mark Alan Boykin
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0

//! Subgraph binding reconciliation.
//!
//! When a linked subgraph's graph-truth membership changes, the tree must
//! detect the delta, propose reconciliation to the host, and apply the
//! chosen outcome. This module implements the delta computation and
//! outcome application described in
//! `subgraph_projection_binding_spec.md ยง7`.

use std::collections::HashSet;

use crate::forme::MemberId;
use crate::forme::subgraph::{
    SubgraphBinding, SubgraphId, SubgraphMemberDelta, SubgraphSpec, ReconciliationChoice,
    ReconciliationProposal,
};
use crate::forme::member::Lifecycle;
use crate::forme::nav::TreeIntent;
use crate::forme::tree::GraphTree;

/// Compute the roster delta between a linked subgraph's expected members
/// (from graph truth) and the tree's current members for that subgraph.
///
/// `graph_truth_members` is the authoritative member set as derived from
/// the graph using the subgraph's edge projection spec.
pub fn compute_roster_delta<N: MemberId>(
    tree: &GraphTree<N>,
    subgraph_id: SubgraphId,
    graph_truth_members: &[N],
) -> SubgraphMemberDelta<N> {
    let truth_set: HashSet<&N> = graph_truth_members.iter().collect();
    let tree_members: Vec<N> = tree
        .subgraph_members(subgraph_id)
        .into_iter()
        .cloned()
        .collect();
    let tree_set: HashSet<&N> = tree_members.iter().collect();

    let added: Vec<N> = graph_truth_members
        .iter()
        .filter(|m| !tree_set.contains(m))
        .cloned()
        .collect();

    let removed: Vec<N> = tree_members
        .iter()
        .filter(|m| !truth_set.contains(m))
        .cloned()
        .collect();

    SubgraphMemberDelta {
        added,
        removed,
        rebased_seeds: Vec::new(),
    }
}

/// Build a reconciliation proposal for a linked subgraph that has drifted
/// from graph truth.
///
/// Returns `None` if the delta is empty (no reconciliation needed).
pub fn propose_reconciliation<N: MemberId>(
    tree: &GraphTree<N>,
    subgraph_id: SubgraphId,
    graph_truth_members: &[N],
    reason: impl Into<String>,
) -> Option<ReconciliationProposal<N>> {
    let delta = compute_roster_delta(tree, subgraph_id, graph_truth_members);
    if delta.is_empty() {
        return None;
    }
    Some(ReconciliationProposal {
        subgraph_id,
        delta,
        reason: reason.into(),
    })
}

/// Apply a reconciliation choice to the tree.
///
/// Returns intents for the host to act on (activation requests for new
/// members, dismissal requests for removed members, etc.).
pub fn apply_reconciliation<N: MemberId>(
    tree: &mut GraphTree<N>,
    proposal: &ReconciliationProposal<N>,
    choice: ReconciliationChoice,
) -> Vec<TreeIntent<N>> {
    let gid = proposal.subgraph_id;
    let mut intents = Vec::new();

    match choice {
        ReconciliationChoice::ApplyKeepLinked => {
            // Add new members to the tree with subgraph membership.
            for member in &proposal.delta.added {
                if tree.get(member).is_none() {
                    tree.apply(crate::forme::nav::NavAction::Attach {
                        member: member.clone(),
                        provenance: crate::forme::member::Provenance::Derived {
                            connection: None,
                            derivation: format!("subgraph-reconciliation:{}", gid),
                        },
                    });
                }
                // Tag with subgraph membership.
                if let Some(entry) = tree.get_mut(member) {
                    if !entry.subgraph_membership.contains(&gid) {
                        entry.subgraph_membership.push(gid);
                    }
                }
                intents.push(TreeIntent::MemberAttached(member.clone()));
            }

            // Remove members no longer in graph truth.
            for member in &proposal.delta.removed {
                if let Some(entry) = tree.get_mut(member) {
                    entry.subgraph_membership.retain(|id| *id != gid);
                }
                // If the member has no remaining subgraph memberships and is Cold,
                // detach it entirely.
                let should_detach = tree
                    .get(member)
                    .map(|e| e.subgraph_membership.is_empty() && e.lifecycle == Lifecycle::Cold)
                    .unwrap_or(false);
                if should_detach {
                    tree.apply(crate::forme::nav::NavAction::Detach {
                        member: member.clone(),
                        recursive: false,
                    });
                    intents.push(TreeIntent::MemberDetached(member.clone()));
                }
            }

            intents.push(TreeIntent::ReconciliationNeeded {
                subgraph: gid,
                reason: format!("applied: {}", proposal.reason),
            });
        },

        ReconciliationChoice::KeepAsUnlinkedSession => {
            // Convert binding to UnlinkedSession.
            if let Some(subgraph) = tree.subgraphs_mut().iter_mut().find(|g| g.id == gid) {
                subgraph.binding = SubgraphBinding::UnlinkedSession;
            }
        },

        ReconciliationChoice::SaveAsNewFork { ref reason } => {
            // Branch: preserve current roster, change binding to Branched.
            if let Some(subgraph) = tree.subgraphs_mut().iter_mut().find(|g| g.id == gid) {
                let parent_spec = match &subgraph.binding {
                    SubgraphBinding::Linked { spec } => spec.clone(),
                    _ => SubgraphSpec {
                        kind: subgraph
                            .kind
                            .clone()
                            .unwrap_or(crate::forme::subgraph::SubgraphKind::Session),
                        anchors: Vec::new(),
                        primary_anchor: None,
                        selectors: Vec::new(),
                    },
                };
                subgraph.binding = SubgraphBinding::Branched {
                    parent_spec,
                    reason: reason.clone(),
                };
            }
        },

        ReconciliationChoice::Cancel => {
            // No-op: discard the proposal, tree unchanged.
        },
    }

    intents
}

/// Detect whether a manual operation (user-initiated attach/detach) on a
/// member that belongs to a linked subgraph should trigger a fork.
///
/// Returns the subgraph ID and a fork reason if the operation diverges
/// from the linked spec.
pub fn detect_fork_on_manual_override<N: MemberId>(
    tree: &GraphTree<N>,
    member: &N,
    operation: &str,
) -> Option<(SubgraphId, String)> {
    let entry = tree.get(member)?;
    for &gid in &entry.subgraph_membership {
        let subgraph = tree.subgraphs().iter().find(|g| g.id == gid)?;
        if matches!(subgraph.binding, SubgraphBinding::Linked { .. }) {
            return Some((
                gid,
                format!(
                    "manual {} on member {:?} diverges from linked subgraph {}",
                    operation, member, gid
                ),
            ));
        }
    }
    None
}

/// Transition a linked subgraph to Branched state.
///
/// Called when `detect_fork_on_manual_override` fires and the host
/// decides (or auto-policy decides) that the override should fork.
pub fn apply_fork<N: MemberId>(tree: &mut GraphTree<N>, subgraph_id: SubgraphId, reason: String) {
    if let Some(subgraph) = tree
        .subgraphs_mut()
        .iter_mut()
        .find(|g| g.id == subgraph_id)
    {
        let parent_spec = match &subgraph.binding {
            SubgraphBinding::Linked { spec } => spec.clone(),
            _ => return, // Not linked โ€” nothing to fork.
        };
        subgraph.binding = SubgraphBinding::Branched {
            parent_spec,
            reason,
        };
    }
}

// ---------------------------------------------------------------------------
// Containment lens derivation
// ---------------------------------------------------------------------------

/// Input for containment lens derivation: origin/domain groupings
/// supplied by the host from ContainmentRelation edges.
///
/// The forme crate doesn't know about edge types, so the host
/// extracts containment relationships and passes them as group lists.
#[derive(Clone, Debug)]
pub struct ContainmentGroup<N: MemberId> {
    /// Label for this containment group (e.g. domain name, folder path).
    pub label: String,
    /// Members that belong to this group.
    pub members: Vec<N>,
}

/// Derive a containment-oriented topology for tree members.
///
/// Given containment groups, returns a list of (parent, children) pairs
/// that the host can apply as topology via `NavAction::Reparent`.
///
/// Members not in any group are collected under a synthetic "ungrouped"
/// parent (the first member of the ungrouped set).
pub fn derive_containment_topology<N: MemberId>(
    tree: &GraphTree<N>,
    groups: &[ContainmentGroup<N>],
) -> Vec<(N, Vec<N>)> {
    let all_members: HashSet<N> = tree.members().map(|(k, _)| k.clone()).collect();
    let mut grouped: HashSet<N> = HashSet::new();
    let mut result: Vec<(N, Vec<N>)> = Vec::new();

    for group in groups {
        // The first member in the group becomes the parent.
        let valid_members: Vec<N> = group
            .members
            .iter()
            .filter(|m| all_members.contains(m))
            .cloned()
            .collect();
        if valid_members.len() < 2 {
            // Single-member or empty groups don't need hierarchy.
            for m in &valid_members {
                grouped.insert(m.clone());
            }
            continue;
        }
        let parent = valid_members[0].clone();
        let children: Vec<N> = valid_members[1..].to_vec();
        for m in &valid_members {
            grouped.insert(m.clone());
        }
        result.push((parent, children));
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::forme::subgraph::{SubgraphKind, SubgraphRef};
    use crate::forme::member::Provenance;
    use crate::forme::nav::NavAction;

    fn make_tree_with_linked_subgraph(members: &[u64], subgraph_members: &[u64]) -> GraphTree<u64> {
        let mut tree = GraphTree::new(
            crate::forme::layout::LayoutMode::TreeStyleTabs,
            crate::forme::lens::ProjectionLens::Traversal,
        );
        for &m in members {
            tree.apply(NavAction::Attach {
                member: m,
                provenance: Provenance::Manual {
                    source: None,
                    context: None,
                },
            });
        }

        let spec = SubgraphSpec {
            kind: SubgraphKind::Session,
            anchors: Vec::new(),
            primary_anchor: None,
            selectors: Vec::new(),
        };
        let subgraph = SubgraphRef {
            id: 0,
            anchors: Vec::new(),
            primary_anchor: None,
            binding: SubgraphBinding::Linked { spec },
            kind: Some(SubgraphKind::Session),
        };
        tree.add_subgraph(subgraph);

        for &m in subgraph_members {
            if let Some(entry) = tree.get_mut(&m) {
                entry.subgraph_membership.push(0);
            }
        }
        tree
    }

    #[test]
    fn empty_delta_when_synchronized() {
        let tree = make_tree_with_linked_subgraph(&[1, 2, 3], &[1, 2, 3]);
        let delta = compute_roster_delta(&tree, 0, &[1, 2, 3]);
        assert!(delta.is_empty());
    }

    #[test]
    fn delta_detects_additions() {
        let tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let delta = compute_roster_delta(&tree, 0, &[1, 2, 3]);
        assert_eq!(delta.added, vec![3]);
        assert!(delta.removed.is_empty());
    }

    #[test]
    fn delta_detects_removals() {
        let tree = make_tree_with_linked_subgraph(&[1, 2, 3], &[1, 2, 3]);
        let delta = compute_roster_delta(&tree, 0, &[1, 2]);
        assert!(delta.added.is_empty());
        assert_eq!(delta.removed, vec![3]);
    }

    #[test]
    fn delta_detects_both() {
        let tree = make_tree_with_linked_subgraph(&[1, 2, 3], &[1, 2, 3]);
        let delta = compute_roster_delta(&tree, 0, &[1, 2, 4]);
        assert_eq!(delta.added, vec![4]);
        assert_eq!(delta.removed, vec![3]);
    }

    #[test]
    fn propose_returns_none_when_synchronized() {
        let tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        assert!(propose_reconciliation(&tree, 0, &[1, 2], "test").is_none());
    }

    #[test]
    fn propose_returns_some_when_diverged() {
        let tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let proposal = propose_reconciliation(&tree, 0, &[1, 2, 3], "graph change");
        assert!(proposal.is_some());
        assert_eq!(proposal.unwrap().delta.added, vec![3]);
    }

    #[test]
    fn apply_keep_linked_adds_members() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let proposal = propose_reconciliation(&tree, 0, &[1, 2, 3], "test").unwrap();
        let intents =
            apply_reconciliation(&mut tree, &proposal, ReconciliationChoice::ApplyKeepLinked);

        // Member 3 should now exist in the tree.
        assert!(tree.get(&3).is_some());
        // And should be tagged with subgraph 0.
        assert!(tree.get(&3).unwrap().subgraph_membership.contains(&0));
        assert!(!intents.is_empty());
    }

    #[test]
    fn apply_keep_linked_removes_cold_unaffiliated_members() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2, 3], &[1, 2, 3]);
        // Make member 3 Cold so it can be auto-detached.
        tree.apply(NavAction::SetLifecycle(3, Lifecycle::Cold));
        let proposal = propose_reconciliation(&tree, 0, &[1, 2], "test").unwrap();
        apply_reconciliation(&mut tree, &proposal, ReconciliationChoice::ApplyKeepLinked);

        // Member 3 should be detached (no remaining subgraph membership + Cold).
        assert!(tree.get(&3).is_none());
    }

    #[test]
    fn apply_keep_unlinked_converts_binding() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let proposal = propose_reconciliation(&tree, 0, &[1, 2, 3], "test").unwrap();
        apply_reconciliation(
            &mut tree,
            &proposal,
            ReconciliationChoice::KeepAsUnlinkedSession,
        );

        let binding = &tree.subgraphs()[0].binding;
        assert!(matches!(binding, SubgraphBinding::UnlinkedSession));
    }

    #[test]
    fn apply_fork_preserves_parent_spec() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let proposal = propose_reconciliation(&tree, 0, &[1, 2, 3], "test").unwrap();
        apply_reconciliation(
            &mut tree,
            &proposal,
            ReconciliationChoice::SaveAsNewFork {
                reason: "user override".to_string(),
            },
        );

        let binding = &tree.subgraphs()[0].binding;
        match binding {
            SubgraphBinding::Branched {
                parent_spec,
                reason,
            } => {
                assert_eq!(parent_spec.kind, SubgraphKind::Session);
                assert_eq!(reason, "user override");
            },
            _ => panic!("expected Branched binding"),
        }
    }

    #[test]
    fn cancel_leaves_tree_unchanged() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let proposal = propose_reconciliation(&tree, 0, &[1, 2, 3], "test").unwrap();
        apply_reconciliation(&mut tree, &proposal, ReconciliationChoice::Cancel);

        // No member 3 should exist.
        assert!(tree.get(&3).is_none());
        // Binding should still be Linked.
        assert!(matches!(
            tree.subgraphs()[0].binding,
            SubgraphBinding::Linked { .. }
        ));
    }

    #[test]
    fn detect_fork_finds_linked_subgraph() {
        let tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        let result = detect_fork_on_manual_override(&tree, &1, "dismiss");
        assert!(result.is_some());
        let (gid, _reason) = result.unwrap();
        assert_eq!(gid, 0);
    }

    #[test]
    fn detect_fork_ignores_unlinked() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        // Convert to unlinked.
        tree.subgraphs_mut()[0].binding = SubgraphBinding::UnlinkedSession;
        let result = detect_fork_on_manual_override(&tree, &1, "dismiss");
        assert!(result.is_none());
    }

    #[test]
    fn apply_fork_transitions_to_forked() {
        let mut tree = make_tree_with_linked_subgraph(&[1, 2], &[1, 2]);
        apply_fork(&mut tree, 0, "manual override".to_string());
        assert!(matches!(
            tree.subgraphs()[0].binding,
            SubgraphBinding::Branched { .. }
        ));
    }

    #[test]
    fn containment_topology_groups_members() {
        let mut tree = GraphTree::new(
            crate::forme::layout::LayoutMode::TreeStyleTabs,
            crate::forme::lens::ProjectionLens::Traversal,
        );
        for i in 1..=6u64 {
            tree.apply(NavAction::Attach {
                member: i,
                provenance: Provenance::Manual {
                    source: None,
                    context: None,
                },
            });
        }

        let groups = vec![
            ContainmentGroup {
                label: "example.com".to_string(),
                members: vec![1, 2, 3],
            },
            ContainmentGroup {
                label: "other.org".to_string(),
                members: vec![4, 5],
            },
        ];

        let topology = derive_containment_topology(&tree, &groups);
        assert_eq!(topology.len(), 2);
        assert_eq!(topology[0].0, 1); // parent of first group
        assert_eq!(topology[0].1, vec![2, 3]); // children
        assert_eq!(topology[1].0, 4); // parent of second group
        assert_eq!(topology[1].1, vec![5]); // children
    }
}