mere-kernel 0.0.3

Portable identity, authority, and mutation kernel for the Mere browser. Renamed from graphshell-core.
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
// 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

//! Importing one graph into another as edits: what a codicil import writes
//! into a session's journal.
//!
//! Reservoir plan V2, ยง7 item 27. An import used to rebuild the graph from the
//! union of two snapshots and swap it in, which a journal cannot record. The
//! same union is written here as ordinary edits:
//! - a node the live graph lacks arrives whole, through the edits undo uses to
//!   bring back a removed node, and with its images;
//! - a node both graphs hold keeps its own fields, and the incoming facets
//!   overwrite its own;
//! - the incoming relations join the live ones between the same two nodes, as
//!   a snapshot load restores a union.
//!
//! The rebuild also gave every node the default-valued facets its empty legacy
//! columns import as. These edits give none: the incoming graph carries its
//! codicil's facet store whole, as a session loads its own. No codicil carries
//! column data, since canonical saves have written the columns empty since
//! before the first codicil schema.

use std::collections::BTreeSet;

use euclid::default::Point2D;
use uuid::Uuid;

use super::Graph;
use super::capture::CapturedDelta;
use super::revert::{Revert, edges_between, node_state};
use crate::persistence::PersistedEdge;

/// The edits that import `incoming` into `live`. `incoming` is a codicil's
/// graph with the codicil's facet store laid in whole, not over what its
/// snapshot's legacy columns import.
/// This compatibility path stops on resources until resource-aware import lands.
pub fn import_edits(live: &Graph, incoming: &Graph) -> Vec<CapturedDelta> {
    assert!(
        incoming.resources.node_count() == 0
            && incoming.resources.edge_count() == 0
            && incoming.shown_resources.is_empty(),
        "surface-only import cannot preserve resource records"
    );
    let mut edits = Vec::new();
    for (_, node) in incoming.nodes() {
        let (id, node_id) = (node.id, node.id.to_string());
        if live.get_node_by_id(id).is_none() {
            let mut made = Revert::default();
            made.recreate(
                id,
                &node_state(incoming, id).expect("an incoming node has a state"),
            );
            edits.extend(made.edits);
            edits.extend(node.images.iter().map(|(role, image)| {
                CapturedDelta::ReplaySetNodeImageById {
                    node_id: node_id.clone(),
                    role: *role,
                    image: *image,
                }
            }));
            continue;
        }
        let Some(facets) = incoming.facets().facets_of(&id) else {
            continue;
        };
        for (facet, value) in facets.iter() {
            if live.facets().get(&id, facet) != Some(value) {
                edits.push(CapturedDelta::ReplaySetNodeFacetById {
                    node_id: node_id.clone(),
                    facet: facet.as_str().to_string(),
                    value_json: value.to_string(),
                });
            }
        }
    }

    let id_of = |key| incoming.get_node(key).map(|node| node.id);
    let pairs: BTreeSet<(Uuid, Uuid)> = incoming
        .relations()
        .filter_map(|relation| Some((id_of(relation.from)?, id_of(relation.to)?)))
        .collect();
    for (from, to) in pairs {
        let held = edges_between(live, from, to);
        let joined = joined(from, to, &held, &edges_between(incoming, from, to));
        if joined != held {
            edits.push(CapturedDelta::ReplaySetEdgesByIds {
                from_id: from.to_string(),
                to_id: to.to_string(),
                edges: joined,
            });
        }
    }
    edits
}

/// The relations from `from` to `to` once `incoming`'s are restored after
/// `held`, worked out on a scratch pair of nodes.
fn joined(
    from: Uuid,
    to: Uuid,
    held: &[PersistedEdge],
    incoming: &[PersistedEdge],
) -> Vec<PersistedEdge> {
    let mut scratch = Graph::new();
    let origin = Point2D::new(0.0, 0.0);
    let from_key = scratch.add_node_with_id(from, format!("urn:uuid:{from}"), origin);
    let to_key = if to == from {
        from_key
    } else {
        scratch.add_node_with_id(to, format!("urn:uuid:{to}"), origin)
    };
    for edge in held.iter().chain(incoming) {
        scratch.restore_persisted_edge(from_key, to_key, edge);
    }
    scratch.persisted_edges_between(from_key, to_key)
}

#[cfg(test)]
mod tests {
    use std::collections::{BTreeMap, HashSet};

    use serde_json::Value;

    use chartulary::AcceptAll;

    use super::*;
    use crate::graph::revert::NodeState;
    use crate::graph::revert::tests::{add, apply_all, facet, id, relate, tag, title};
    use crate::graph::{EdgeAssertion, NavigationTrigger, NodeFacetStore, SemanticSubKind};
    use crate::persistence::GraphSnapshot;
    use crate::types::{ImageRef, ImageRole};

    #[test]
    fn surface_import_stops_before_losing_resources_with_legacy_positive_control() {
        let live = Graph::new();
        let mut legacy = Graph::new();
        legacy.add_node_with_id(
            id(1),
            "https://surface.test/".into(),
            Point2D::new(0.0, 0.0),
        );
        let edits = import_edits(&live, &legacy);
        assert!(!edits.is_empty());
        let mut accepted = live.clone();
        apply_all(&mut accepted, &edits);
        assert_eq!(accepted.node_count(), 1);
        assert!(accepted.get_node_by_id(id(1)).is_some());
        let mut snapshot = legacy.to_snapshot();
        let iri = "https://resource.test/page";
        snapshot
            .resources
            .push(crate::persistence::PersistedResourceRecord {
                canonical_iri: iri.into(),
                facets: vec![],
            });
        let incoming = Graph::try_from_snapshot(&snapshot).unwrap();
        assert!(
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                import_edits(&live, &incoming)
            }))
            .is_err()
        );
        assert_eq!(live.node_count(), 0);
        assert!(live.resource_nodes().next().is_none());
        assert_eq!(incoming.node_count(), 1);
        assert!(incoming.resource(chartulary::resource_id(iri)).is_some());
    }

    type Codicil = (GraphSnapshot, NodeFacetStore);

    fn touch(n: u128, at: u64) -> CapturedDelta {
        CapturedDelta::ReplayTouchNodeLastVisitedById {
            node_id: id(n).to_string(),
            timestamp_ms: at,
        }
    }

    fn supports(from: u128, to: u128) -> CapturedDelta {
        CapturedDelta::ReplayAssertRelationByIds {
            from_id: id(from).to_string(),
            to_id: id(to).to_string(),
            assertion: EdgeAssertion::Semantic {
                sub_kind: SemanticSubKind::Supports,
                label: None,
            },
        }
    }

    fn traverse(from: u128, to: u128, at: u64) -> CapturedDelta {
        CapturedDelta::ReplayAppendTraversalByIds {
            from_id: id(from).to_string(),
            to_id: id(to).to_string(),
            trigger: NavigationTrigger::LinkClick,
            timestamp_ms: at,
        }
    }

    fn at_url(n: u128, url: &str) -> CapturedDelta {
        CapturedDelta::ReplayAddNodeWithIdIfMissing {
            id: id(n).to_string(),
            url: url.to_string(),
            position: [0.0, 0.0],
        }
    }

    /// `members` cut from `source` as Graphshell's export cuts a codicil.
    fn codicil(source: &Graph, members: &[u128]) -> Codicil {
        let members: HashSet<Uuid> = members.iter().map(|n| id(*n)).collect();
        let ids: HashSet<String> = members.iter().map(Uuid::to_string).collect();
        let mut snapshot = source.to_snapshot();
        snapshot.nodes.retain(|node| ids.contains(&node.node_id));
        snapshot
            .edges
            .retain(|edge| ids.contains(&edge.from_node_id) && ids.contains(&edge.to_node_id));
        snapshot.import_records.clear();
        snapshot.fields.clear();
        snapshot.couplings.clear();
        snapshot.navigation = Default::default();
        let mut facets = NodeFacetStore::new();
        for (node, node_facets) in source.facets().iter() {
            if !members.contains(node) {
                continue;
            }
            for (facet, value) in node_facets.iter() {
                facets
                    .set(*node, facet.clone(), value.clone(), &AcceptAll)
                    .expect("AcceptAll admits every value");
            }
        }
        (snapshot, facets)
    }

    /// The import as Graphshell ran it before sessions: the union of the two
    /// snapshots, rebuilt, with the live facets and then the codicil's laid over.
    fn rebuilt(live: &Graph, (snapshot, facets): &Codicil) -> Graph {
        let current = live.facets().clone();
        let mut merged = live.to_snapshot();
        let mut known: HashSet<String> = merged
            .nodes
            .iter()
            .map(|node| node.node_id.clone())
            .collect();
        for node in &snapshot.nodes {
            if known.insert(node.node_id.clone()) {
                merged.nodes.push(node.clone());
            }
        }
        merged.edges.extend(snapshot.edges.iter().cloned());
        merged.timestamp_secs = snapshot.timestamp_secs;
        let mut graph = Graph::from_snapshot(&merged);
        graph.overlay_facets(current).expect("live facets install");
        graph
            .overlay_facets(facets.clone())
            .expect("codicil facets install");
        graph
    }

    /// The same import as edits.
    fn edited(live: &Graph, (snapshot, facets): &Codicil) -> (Graph, Vec<CapturedDelta>) {
        let mut incoming = Graph::from_snapshot(snapshot);
        *incoming.facets_mut() = facets.clone();
        let edits = import_edits(live, &incoming);
        let mut graph = live.clone();
        apply_all(&mut graph, &edits);
        (graph, edits)
    }

    type Nodes = BTreeMap<Uuid, (NodeState, Vec<(ImageRole, ImageRef)>)>;
    type Relations = BTreeMap<(Uuid, Uuid), Vec<PersistedEdge>>;

    /// Bucket partition is a compatibility storage detail: an imported claim
    /// may coalesce with a live pair bucket while a rebuilt legacy snapshot
    /// still has parallel buckets. Compare the exact assertion multiset per
    /// directed pair, along with the exposed family set and traversal rows.
    fn relation_contents(
        relations: &Relations,
    ) -> BTreeMap<
        (Uuid, Uuid),
        (
            Vec<crate::persistence::PersistedSemanticStatement>,
            Vec<String>,
            Vec<String>,
        ),
    > {
        relations
            .iter()
            .map(|(pair, edges)| {
                let mut assertions: Vec<_> = edges
                    .iter()
                    .flat_map(|edge| edge.retained_assertions())
                    .cloned()
                    .collect();
                assertions.sort_by(|left, right| left.statement_id.cmp(&right.statement_id));
                let families: BTreeSet<_> = edges
                    .iter()
                    .flat_map(|edge| &edge.families)
                    .map(|family| format!("{family:?}"))
                    .collect();
                let mut traversals: Vec<_> = edges
                    .iter()
                    .filter_map(|edge| edge.traversal.as_ref())
                    .map(|traversal| serde_json::to_string(traversal).unwrap())
                    .collect();
                traversals.sort();
                (
                    *pair,
                    (assertions, families.into_iter().collect(), traversals),
                )
            })
            .collect()
    }

    /// Every node's state and images, every relation by pair, and the
    /// relations as the graph reports them, derived ones included.
    fn whole(graph: &Graph) -> (Nodes, Relations, BTreeSet<String>) {
        let nodes: Nodes = graph
            .nodes()
            .map(|(_, node)| {
                let images = node.images.iter().map(|(role, image)| (*role, *image));
                let state = node_state(graph, node.id).expect("a listed node has a state");
                (node.id, (state, images.collect()))
            })
            .collect();
        let mut relations = Relations::new();
        for from in nodes.keys() {
            for to in nodes.keys() {
                let edges = edges_between(graph, *from, *to);
                if !edges.is_empty() {
                    relations.insert((*from, *to), edges);
                }
            }
        }
        let id_of = |key| graph.get_node(key).map(|node| node.id);
        let listed = graph
            .relations()
            .map(|relation| {
                let ends = (id_of(relation.from), id_of(relation.to));
                format!("{ends:?} {:?}", relation.kind)
            })
            .collect();
        (nodes, relations, listed)
    }

    fn facet_keys(facets: &NodeFacetStore, node: &Uuid) -> BTreeSet<String> {
        facets
            .facets_of(node)
            .map(|facets| {
                facets
                    .iter()
                    .map(|(id, _)| id.as_str().to_string())
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Whether a facet value says nothing: what an empty legacy column imports as.
    fn empty(value: &Value) -> bool {
        match value {
            Value::Null => true,
            Value::Bool(value) => !value,
            Value::Array(values) => values.is_empty(),
            Value::Object(fields) => fields.values().all(empty),
            _ => false,
        }
    }

    #[test]
    fn an_import_as_edits_matches_the_rebuilt_union() {
        let mut live = Graph::new();
        apply_all(
            &mut live,
            &[
                add(1),
                title(1, "Live one"),
                facet(1, "custom.a", "1"),
                add(2),
                relate(1, 2),
                touch(1, 1_700_000_000_000),
                touch(2, 1_700_000_001_000),
            ],
        );
        let image = ImageRef::new([7; 32], 16, 16);
        let mut source = Graph::new();
        apply_all(
            &mut source,
            &[
                add(1),
                title(1, "Source one"),
                facet(1, "custom.a", "2"),
                facet(1, "custom.b", "true"),
                add(2),
                at_url(3, "https://site.test/a"),
                title(3, "Three"),
                tag(3, "kept"),
                facet(3, "custom.c", r#"{"x":1}"#),
                at_url(4, "https://site.test/a/b"),
                supports(1, 2),
                relate(3, 4),
                traverse(3, 1, 1_700_000_002_000),
                touch(1, 1_700_000_003_000),
                touch(2, 1_700_000_004_000),
                touch(3, 1_700_000_005_000),
                touch(4, 1_700_000_006_000),
            ],
        );
        apply_all(
            &mut source,
            &[CapturedDelta::ReplaySetNodeImageById {
                node_id: id(3).to_string(),
                role: ImageRole::Favicon,
                image,
            }],
        );
        let codicil = codicil(&source, &[1, 2, 3, 4]);

        let (imported, edits) = edited(&live, &codicil);
        let (mut want_nodes, want_relations, want_listed) = whole(&rebuilt(&live, &codicil));
        // The rebuild's one extra: facets neither graph held, all of them empty.
        let mut artifacts = BTreeSet::new();
        for (node, (state, _)) in &mut want_nodes {
            let mut held = facet_keys(live.facets(), node);
            held.extend(facet_keys(&codicil.1, node));
            state.facets.retain(|facet, value| {
                let artifact = !held.contains(facet);
                if artifact {
                    assert!(empty(value), "{facet} = {value} is not an empty column");
                    artifacts.insert(*node);
                }
                !artifact
            });
        }
        assert!(
            artifacts.contains(&id(1)) && artifacts.contains(&id(3)),
            "the rebuild added empty facets to a live node and an imported one: {artifacts:?}"
        );
        let (nodes, relations, listed) = whole(&imported);

        assert_eq!(nodes, want_nodes);
        assert_eq!(
            relations.keys().collect::<Vec<_>>(),
            want_relations.keys().collect::<Vec<_>>()
        );
        assert_eq!(
            relation_contents(&relations),
            relation_contents(&want_relations)
        );
        assert_eq!(listed, want_listed);

        // What the edits did, as a positive control on the comparison.
        let (one, _) = &nodes[&id(1)];
        assert_eq!(
            one.title, "Live one",
            "a node both hold keeps its own fields"
        );
        assert_eq!(one.facets.get("custom.a"), Some(&serde_json::json!(2)));
        assert_eq!(one.facets.get("custom.b"), Some(&serde_json::json!(true)));
        assert_eq!(nodes[&id(3)].1, [(ImageRole::Favicon, image)]);
        assert_eq!(
            relations[&(id(1), id(2))]
                .iter()
                .flat_map(|edge| edge.retained_assertions())
                .count(),
            2,
            "the incoming relation joins the live one"
        );
        assert!(
            edits.iter().all(|edit| edit.replay().into_write().is_ok()),
            "every edit has a replay form, so a session can journal it"
        );
    }

    #[test]
    fn importing_what_the_graph_already_holds_writes_nothing() {
        let mut live = Graph::new();
        apply_all(
            &mut live,
            &[add(1), facet(1, "custom.a", "1"), add(2), relate(1, 2)],
        );
        let (_, edits) = edited(&live, &codicil(&live, &[1, 2]));
        assert!(edits.is_empty(), "{edits:?}");
    }
}