mere-kernel 0.0.1

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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
// Copyright 2026 Mark AB (markik)
// SPDX-License-Identifier: MIT OR Apache-2.0

//! The edit spine: mere's captured-delta stream over the substrate log.
//!
//! The graph's durable mutations already exist as a stream of [`CapturedDelta`]s
//! (stable-id, serializable), emitted through the capture hook as each live
//! [`GraphDelta`](super::apply::GraphDelta) applies (see `capture.rs`). This module
//! gives that stream its principled home: a [`codicil::Codicil`] of captured
//! deltas, the append-only log primitive shared across the Merely apps. The
//! materialized graph is the *replay* of the journal, and because live editing and
//! replay both funnel through `apply_graph_delta`, the two cannot diverge.
//!
//! This is the edit spine over the substrate. mere keeps its own rich edit
//! vocabulary — `CapturedDelta` carries content edits (title, tags, body,
//! navigation) that chartulary's topology-only `GraphEdit` deliberately cannot
//! express — and codicil supplies the ordered, forkable, persistable log beneath
//! it. Checkpointing reuses mere's existing `GraphSnapshot`: load a snapshot, then
//! [`replay_from`](GraphJournal::replay_from) the journal tail past its sequence,
//! mirroring `chartulary::GraphLog::load_checkpointed`.
//!
//! WASM-clean: codicil and muniment are the substrate's portable primitives (the
//! same ones the browser persists through), so this module compiles to
//! `wasm32-unknown-unknown` like the rest of `graph/`.

use std::sync::{Arc, Mutex};

use codicil::{Codicil, LogId, Provenance, Seq};
use muniment::{Backend, Codec, SlotStore, StoreError};

use rkyv::{Archive, Deserialize, Serialize};

use super::Graph;
use super::capture::{CapturedDelta, replay_captured_deltas, replay_captured_deltas_onto};
use super::source_time::{SourceExtent, SourceTime};

/// The author every trusted-UI edit records under. Denizen runs scope their
/// own author (the subject's hex) via [`GraphJournal::set_author`]; entries
/// migrated from pre-envelope logs carry `pre-gate` (chartulary's convention).
pub const USER_AUTHOR: &str = "user";

/// One journal entry: a captured delta in the attribution envelope — the
/// participant-gate plan's B1 adoption of chartulary's `Batch { author, edits }`
/// shape over mere's edit spine. WHO made a change rides the journal, so a
/// denizen's edits read back attributed and compensable.
#[derive(
    Debug, Clone, PartialEq, Archive, Serialize, Deserialize, serde::Serialize, serde::Deserialize,
)]
pub struct AttributedDelta {
    /// `user` for the trusted UI path, a denizen subject's hex for gated runs,
    /// `pre-gate` for entries migrated from bare logs.
    pub author: String,
    pub delta: CapturedDelta,
}

/// An append-only journal of a graph's captured edits: the codicil-backed edit
/// spine. The materialized [`Graph`] is the replay of this log; forking it forks
/// the graph's whole history with provenance, and it persists whole through one
/// muniment slot.
#[derive(Clone, Debug)]
pub struct GraphJournal {
    log: Codicil<AttributedDelta>,
    /// The author the next [`record`](Self::record) attributes — `user` by
    /// default; a host scopes a denizen run with [`set_author`](Self::set_author)
    /// and restores afterwards.
    author: String,
}

impl Default for GraphJournal {
    fn default() -> Self {
        Self {
            log: Codicil::default(),
            author: USER_AUTHOR.to_string(),
        }
    }
}

impl GraphJournal {
    /// A fresh, empty journal (author `user`).
    pub fn new() -> Self {
        Self::default()
    }

    /// A journal with a stable identity, so it can later be forked with
    /// provenance pointing back at it.
    pub fn with_id(id: LogId) -> Self {
        Self {
            log: Codicil::with_id(id),
            author: USER_AUTHOR.to_string(),
        }
    }

    /// Adopt an existing codicil of attributed deltas (e.g. one just loaded
    /// from a store or received from a peer).
    pub fn from_log(log: Codicil<AttributedDelta>) -> Self {
        Self {
            log,
            author: USER_AUTHOR.to_string(),
        }
    }

    /// The author subsequent [`record`](Self::record)s attribute.
    pub fn author(&self) -> &str {
        &self.author
    }

    /// Scope the recording author (a denizen run); the host restores `user`
    /// when the run ends.
    pub fn set_author(&mut self, author: impl Into<String>) {
        self.author = author.into();
    }

    /// Append one captured edit under the current author, returning the [`Seq`]
    /// it was stamped with. This is the write path: a live mutation's capture
    /// lands here (usually via [`journal_capture_hook`]).
    pub fn record(&mut self, delta: CapturedDelta) -> Seq {
        let author = self.author.clone();
        self.record_as(author, delta)
    }

    /// Append one captured edit under an explicit author (the gate path).
    pub fn record_as(&mut self, author: impl Into<String>, delta: CapturedDelta) -> Seq {
        self.log.append(AttributedDelta {
            author: author.into(),
            delta,
        })
    }

    /// The underlying codicil, for cursors, replication, and persistence.
    pub fn log(&self) -> &Codicil<AttributedDelta> {
        &self.log
    }

    /// Every attributed edit, oldest first.
    pub fn entries(&self) -> &[AttributedDelta] {
        self.log.entries()
    }

    /// The number of edits recorded.
    pub fn len(&self) -> usize {
        self.log.len()
    }

    /// Whether the journal holds no edits.
    pub fn is_empty(&self) -> bool {
        self.log.is_empty()
    }

    /// The [`Seq`] the next recorded edit will receive — a durable cursor a
    /// checkpoint can store to resume replay from.
    pub fn next_seq(&self) -> Seq {
        self.log.next_seq()
    }

    /// The earliest source-time cursor: the empty graph before the first
    /// captured delta. Journal source time is a **prefix cursor**, so this is
    /// intentionally `Seq(0)` even though no entry is addressed there yet.
    pub const fn earliest_cursor(&self) -> Seq {
        Seq(0)
    }

    /// The current live source-time cursor: the prefix immediately after the
    /// newest captured delta. Appending later deltas never changes the graph a
    /// previously returned cursor replays to.
    pub fn live_cursor(&self) -> Seq {
        self.next_seq()
    }

    /// Materialize graph truth at a journal prefix cursor without changing the
    /// journal or its current live graph. `Seq(n)` replays exactly the first
    /// `n` entries; `Seq(0)` is empty and [`live_cursor`](Self::live_cursor)
    /// replays every current entry. A cursor beyond the known prefix is stale
    /// and is refused rather than silently clamped to live.
    pub fn snapshot_at(&self, cursor: Seq) -> Option<Graph> {
        let end = cursor.index();
        (end <= self.log.len()).then(|| {
            replay_captured_deltas(
                self.log.entries()[..end]
                    .iter()
                    .map(|entry| entry.delta.clone()),
            )
        })
    }

    /// Stable, evenly distributed prefix cursors for a compact scrubber. This
    /// uses sequence order alone: journal history has no required wall-clock
    /// timestamp. `max_points == 1` yields only the current live cursor;
    /// larger requests always include both earliest and live when distinct.
    pub fn sequence_ticks(&self, max_points: usize) -> Vec<Seq> {
        let live = self.live_cursor();
        if max_points == 0 {
            return Vec::new();
        }
        if max_points == 1 || live.0 == 0 {
            return vec![live];
        }
        let points = max_points.min(live.0 as usize + 1);
        let denominator = (points - 1) as u64;
        (0..points)
            .map(|index| Seq((index as u64 * live.0) / denominator))
            .collect()
    }

    /// This journal's log identity, if any.
    pub fn id(&self) -> Option<&LogId> {
        self.log.id()
    }

    /// Where this journal forked from, if it is a fork.
    pub fn provenance(&self) -> Option<&Provenance> {
        self.log.provenance()
    }

    /// Rebuild the whole graph by replaying every edit from empty (attribution
    /// rides the journal, not the graph — replay strips the envelope).
    pub fn replay(&self) -> Graph {
        replay_captured_deltas(self.log.entries().iter().map(|e| e.delta.clone()))
    }

    /// Advance an already-materialized `graph` by the edits from `since` onward.
    /// The checkpoint-plus-tail path: restore a `GraphSnapshot`, then apply only
    /// the journal entries recorded after the snapshot's sequence. The incremental
    /// twin of [`replay`](Self::replay).
    pub fn replay_from(&self, since: Seq, graph: &mut Graph) {
        replay_captured_deltas_onto(graph, self.log.from(since).iter().map(|e| e.delta.clone()));
    }

    /// Fork this journal under a new identity: copies the whole edit history and
    /// records where it branched from, then diverges independently. The log-level
    /// mirror of a graph fork; the fork replays to an identical graph and then
    /// edits without touching the source.
    pub fn fork(&self, new_id: LogId) -> Self {
        Self {
            log: self.log.fork(new_id),
            author: self.author.clone(),
        }
    }

    /// Write the whole journal to the muniment slot at `key`.
    pub async fn save<B: Backend, C: Codec>(
        &self,
        slots: &SlotStore<B, C>,
        key: &str,
    ) -> Result<(), StoreError> {
        self.log.save(slots, key).await
    }

    /// Load a journal from the muniment slot at `key`, or an empty journal if the
    /// slot is absent.
    pub async fn load<B: Backend, C: Codec>(
        slots: &SlotStore<B, C>,
        key: &str,
    ) -> Result<Self, StoreError> {
        Ok(Self::from_log(Codicil::load(slots, key).await?))
    }

    /// Adopt a pre-envelope codicil of bare [`CapturedDelta`]s, attributing
    /// every entry `pre-gate` (chartulary's migration convention): the one-way
    /// load-time migration for logs recorded before attribution existed.
    pub fn migrate_bare_log(log: Codicil<CapturedDelta>) -> Self {
        let mut journal = GraphJournal::new();
        for delta in log.entries() {
            journal.record_as("pre-gate", delta.clone());
        }
        journal
    }
}

impl SourceTime for GraphJournal {
    type Cursor = Seq;
    type Snapshot = Graph;

    fn source_extent(&self) -> SourceExtent<Self::Cursor> {
        SourceExtent {
            earliest: self.earliest_cursor(),
            current: self.live_cursor(),
        }
    }

    fn source_ticks(&self, max_points: usize) -> Vec<Self::Cursor> {
        self.sequence_ticks(max_points)
    }

    fn source_snapshot(&self, cursor: &Self::Cursor) -> Option<Self::Snapshot> {
        self.snapshot_at(*cursor)
    }
}

/// A shared journal plus a capture hook that records every emitted
/// [`CapturedDelta`] into it. Install the hook with
/// [`set_captured_delta_hook`](super::set_captured_delta_hook) and keep the handle
/// to replay or persist the journal:
///
/// ```ignore
/// let (journal, hook) = journal_capture_hook();
/// kernel::graph::set_captured_delta_hook(Some(hook));
/// // ... drive live mutations through apply_graph_delta ...
/// let restored = journal.lock().unwrap().replay();
/// ```
///
/// The capture hook is one per-thread slot, so this replaces any previously
/// installed hook. It is offered as the host's codicil-backed persistence path.
pub fn journal_capture_hook() -> (
    Arc<Mutex<GraphJournal>>,
    Arc<dyn Fn(&CapturedDelta) + Send + Sync + 'static>,
) {
    let journal = Arc::new(Mutex::new(GraphJournal::new()));
    let sink = Arc::clone(&journal);
    let hook: Arc<dyn Fn(&CapturedDelta) + Send + Sync + 'static> = Arc::new(move |delta| {
        if let Ok(mut journal) = sink.lock() {
            journal.record(delta.clone());
        }
    });
    (journal, hook)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::apply::{GraphDelta, GraphDeltaResult, apply_graph_delta};
    use crate::graph::set_captured_delta_hook;
    use crate::graph::{EdgeAssertion, SemanticSubKind, SourceExtent, SourceTime};
    use euclid::default::Point2D;
    use uuid::Uuid;

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

    /// A key-independent view: sorted node ids and sorted (from, to, kind) triples.
    /// Two graphs are equal iff their fingerprints are, regardless of petgraph keys.
    fn fingerprint(graph: &Graph) -> (Vec<String>, Vec<(String, String, String)>) {
        let mut nodes: Vec<String> = graph.nodes().map(|(_, n)| n.id.to_string()).collect();
        nodes.sort();
        let mut edges: Vec<(String, String, String)> = graph
            .relations()
            .map(|rel| {
                let from = graph
                    .get_node(rel.from)
                    .map(|n| n.id.to_string())
                    .unwrap_or_default();
                let to = graph
                    .get_node(rel.to)
                    .map(|n| n.id.to_string())
                    .unwrap_or_default();
                (from, to, format!("{:?}", rel.kind))
            })
            .collect();
        edges.sort();
        (nodes, edges)
    }

    /// The envelope: entries carry their author; the default is `user`, a
    /// scoped author attributes a denizen run, and replay strips the envelope.
    #[test]
    fn entries_are_attributed_and_author_scoping_works() {
        let mut journal = GraphJournal::new();
        journal.record(add(1, "https://a.test/"));
        journal.set_author("aa11");
        journal.record(add(2, "https://b.test/"));
        journal.set_author(USER_AUTHOR);
        journal.record_as("gate", add(3, "https://c.test/"));

        let authors: Vec<&str> = journal
            .entries()
            .iter()
            .map(|e| e.author.as_str())
            .collect();
        assert_eq!(authors, ["user", "aa11", "gate"]);
        assert_eq!(journal.author(), USER_AUTHOR, "scoping restored");
        assert_eq!(
            journal.replay().node_count(),
            3,
            "replay strips the envelope"
        );
    }

    #[test]
    fn source_time_contract_selects_immutable_journal_prefixes() {
        let mut journal = GraphJournal::new();
        journal.record(add(1, "https://one.test/"));
        journal.record(add(2, "https://two.test/"));

        assert_eq!(
            journal.source_extent(),
            SourceExtent {
                earliest: Seq(0),
                current: Seq(2),
            }
        );
        assert_eq!(journal.source_ticks(3), [Seq(0), Seq(1), Seq(2)]);
        assert_eq!(
            journal
                .source_snapshot(&Seq(1))
                .expect("known source cursor")
                .node_count(),
            1
        );
        assert!(journal.source_snapshot(&Seq(3)).is_none());
        assert_eq!(
            journal.replay().node_count(),
            2,
            "scrubbing leaves live truth intact"
        );
    }

    /// A pre-envelope bare log migrates one-way with the `pre-gate` author.
    #[test]
    fn a_bare_log_migrates_as_pre_gate() {
        let mut bare = Codicil::<CapturedDelta>::default();
        bare.append(add(1, "https://a.test/"));
        bare.append(add(2, "https://b.test/"));
        let journal = GraphJournal::migrate_bare_log(bare);
        assert!(journal.entries().iter().all(|e| e.author == "pre-gate"));
        assert_eq!(journal.replay().node_count(), 2);
    }

    #[test]
    fn records_and_replays_reconstruct_the_graph() {
        let mut journal = GraphJournal::new();
        journal.record(add(1, "https://a.test/"));
        journal.record(add(2, "https://b.test/"));
        journal.record(CapturedDelta::ReplayAssertRelationByIds {
            from_id: Uuid::from_u128(1).to_string(),
            to_id: Uuid::from_u128(2).to_string(),
            assertion: EdgeAssertion::Semantic {
                sub_kind: SemanticSubKind::Cites,
                label: None,
                decay_progress: None,
            },
        });
        journal.record(CapturedDelta::ReplaySetNodeTitleById {
            node_id: Uuid::from_u128(1).to_string(),
            title: "Paper A".to_string(),
        });

        let graph = journal.replay();
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.relations().count(), 1, "the cites relation replayed");
        let (_, node) = graph
            .get_node_by_id(Uuid::from_u128(1))
            .expect("node a present");
        assert_eq!(node.title, "Paper A", "the content edit replayed too");
    }

    #[test]
    fn prefix_cursors_replay_historical_graphs_without_touching_live_history() {
        let mut journal = GraphJournal::new();
        journal.record(add(1, "https://a.test/"));
        journal.record(add(2, "https://b.test/"));
        journal.record(CapturedDelta::ReplayAssertRelationByIds {
            from_id: Uuid::from_u128(1).to_string(),
            to_id: Uuid::from_u128(2).to_string(),
            assertion: EdgeAssertion::Semantic {
                sub_kind: SemanticSubKind::Cites,
                label: None,
                decay_progress: None,
            },
        });

        assert_eq!(journal.earliest_cursor(), Seq(0));
        assert_eq!(journal.live_cursor(), Seq(3));
        assert_eq!(journal.snapshot_at(Seq(0)).unwrap().node_count(), 0);
        assert_eq!(journal.snapshot_at(Seq(1)).unwrap().node_count(), 1);
        let historical = journal.snapshot_at(Seq(2)).expect("known prefix");
        assert_eq!(historical.node_count(), 2);
        assert_eq!(historical.relations().count(), 0);
        assert_eq!(journal.snapshot_at(Seq(3)).unwrap().relations().count(), 1);
        assert!(
            journal.snapshot_at(Seq(4)).is_none(),
            "stale cursors refuse"
        );
        assert_eq!(
            journal.replay().relations().count(),
            1,
            "live history remains live"
        );
        assert_eq!(journal.sequence_ticks(3), vec![Seq(0), Seq(1), Seq(3)]);
    }

    #[test]
    fn fork_carries_history_then_diverges() {
        let mut source = GraphJournal::with_id(LogId::new("source"));
        source.record(add(1, "https://a.test/"));
        source.record(add(2, "https://b.test/"));

        let mut fork = source.fork(LogId::new("fork"));
        assert_eq!(
            fork.entries(),
            source.entries(),
            "the fork copies the history"
        );
        let provenance = fork.provenance().expect("a fork has provenance");
        assert_eq!(provenance.source, Some(LogId::new("source")));
        assert_eq!(provenance.at, Seq(source.len() as u64));

        // Diverging the fork leaves the source untouched.
        fork.record(add(3, "https://c.test/"));
        assert_eq!(source.replay().node_count(), 2, "source unchanged");
        assert_eq!(fork.replay().node_count(), 3);
    }

    #[test]
    fn journal_round_trips_through_a_muniment_slot() {
        use muniment::{JsonSlots, MemoryBackend};
        pollster::block_on(async {
            let slots = JsonSlots::new(MemoryBackend::new());

            let mut journal = GraphJournal::new();
            journal.record(add(1, "https://a.test/"));
            journal.record(add(2, "https://b.test/"));
            journal.save(&slots, "journal").await.unwrap();

            let reloaded = GraphJournal::load(&slots, "journal").await.unwrap();
            assert_eq!(reloaded.len(), 2, "the log survived the round trip");
            assert_eq!(
                fingerprint(&reloaded.replay()),
                fingerprint(&journal.replay()),
                "the reloaded journal replays to the same graph"
            );
        });
    }

    #[test]
    fn the_capture_hook_feeds_the_journal_and_replay_matches_live() {
        // The edit-spine invariant: a graph built by live mutation and one rebuilt
        // by replaying the journal those mutations produced are identical.
        let (journal, hook) = journal_capture_hook();
        set_captured_delta_hook(Some(hook));

        let mut live = Graph::new();
        let a = match apply_graph_delta(
            &mut live,
            GraphDelta::AddNode {
                id: Some(Uuid::from_u128(1)),
                url: "https://a.test/".to_string(),
                position: Point2D::new(0.0, 0.0),
            },
        ) {
            GraphDeltaResult::NodeAdded(key) => key,
            other => panic!("expected NodeAdded, got {other:?}"),
        };
        let b = match apply_graph_delta(
            &mut live,
            GraphDelta::AddNode {
                id: Some(Uuid::from_u128(2)),
                url: "https://b.test/".to_string(),
                position: Point2D::new(1.0, 0.0),
            },
        ) {
            GraphDeltaResult::NodeAdded(key) => key,
            other => panic!("expected NodeAdded, got {other:?}"),
        };
        apply_graph_delta(
            &mut live,
            GraphDelta::AssertRelation {
                from: a,
                to: b,
                assertion: EdgeAssertion::Semantic {
                    sub_kind: SemanticSubKind::Cites,
                    label: None,
                    decay_progress: None,
                },
            },
        );
        apply_graph_delta(
            &mut live,
            GraphDelta::SetNodeTitle {
                key: a,
                title: "Paper A".to_string(),
            },
        );

        set_captured_delta_hook(None);

        let replayed = journal.lock().unwrap().replay();
        assert_eq!(
            fingerprint(&replayed),
            fingerprint(&live),
            "replay of the captured journal reconstructs the live graph"
        );
        let (_, node) = replayed.get_node_by_id(Uuid::from_u128(1)).expect("node a");
        assert_eq!(node.title, "Paper A");
    }
}