memfork 0.3.0

Git for agent memory, shared by every AI tool on your machine: fork, merge, rewind and hand off work between agents over MCP.
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
//! The memory graph: entries and agents as nodes, the relations the engine
//! already knows as edges, and a layout that is a pure function of the
//! store.
//!
//! Nothing here is inferred. A fact names its source files; a decision
//! cites a fact by key; a lesson names the task it came from and any
//! decision it mentions by key; a task names what it depends on; a handoff's
//! `next` list names tasks; a briefing carried entries and went to an agent;
//! an agent holds a task and wrote entries. That is the whole list.
//!
//! Layout is decided here, not on the page, so the same store gives the same
//! picture on every machine and a test can say so without a browser. Columns
//! run left to right in a fixed order. Within a column a node's height is a
//! hash of its id, so adding a node never moves another; a column with few
//! nodes is spaced evenly instead, in key order, which is the same picture
//! everywhere too.

use std::collections::{BTreeMap, BTreeSet};

use memfork_core::{Db, Entry, WRITTEN_BY};
use serde_json::{json, Value as Json};

use crate::events::Connected;
use crate::shared::Shared;

/// The columns, left to right.
pub const COLUMNS: [&str; 7] = [
    "agents",
    "briefings",
    "decisions ยท handoffs",
    "lessons",
    "plan",
    "facts",
    "files",
];

/// A column holding this many nodes or fewer spaces them evenly, in id
/// order. Above it, every node's height is a hash of its id.
pub const FEW: usize = 12;

/// The height of a column, in the units a node's `y` is given in.
pub const HEIGHT: u32 = 65_535;

/// Longest label drawn on the canvas.
const MAX_LABEL: usize = 60;

/// A board with more tasks than this has handoffs matched to tasks by id
/// only, not by title: matching every title against every handoff is the
/// one thing here that grows as the product of two counts.
pub const MAX_TITLE_MATCHED: usize = 2000;

/// The kinds of node, in the order their counts are reported.
pub const KINDS: [&str; 9] = [
    "agent", "brief", "decision", "handoff", "lesson", "task", "fact", "entry", "file",
];

/// Which column a kind lives in.
pub fn column(kind: &str) -> u8 {
    match kind {
        "agent" => 0,
        "brief" => 1,
        "decision" | "handoff" => 2,
        "lesson" => 3,
        "task" => 4,
        "file" => 6,
        _ => 5,
    }
}

/// One node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node {
    /// The entry's key, `agent:<name>`, `file:<path>` or `brief:<order>`.
    pub id: String,
    /// One of [`KINDS`].
    pub kind: &'static str,
    /// What is drawn beside it.
    pub label: String,
    /// The branch sequence number it has existed since; zero when unknown.
    pub seq: u64,
    /// A word for its state: `stale` for a fact, `open`, `claimed` or `done`
    /// for a task, `connected` or `away` for an agent, `waiting` or `picked
    /// up` for a handoff.
    pub state: Option<&'static str>,
    /// Who wrote it, as a person knows the client.
    pub who: Option<String>,
}

/// One relation.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Edge {
    /// Index into the graph's nodes.
    pub from: usize,
    /// Index into the graph's nodes.
    pub to: usize,
    /// `source of`, `cited by`, `about`, `for`, `depends on`, `next`, `in`,
    /// `served to`, `holds` or `wrote`.
    pub kind: &'static str,
}

/// The graph, laid out.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Graph {
    /// The branch's sequence number the graph was built from.
    pub seq: u64,
    /// The past point it shows, if it is not the present.
    pub at: Option<u64>,
    /// Nodes, in a fixed order: by column, then by id.
    pub nodes: Vec<Node>,
    /// Each node's column and height.
    pub positions: Vec<(u8, u16)>,
    /// Edges, sorted, without duplicates.
    pub edges: Vec<Edge>,
}

/// The 64-bit FNV-1a hash of a string: small, deterministic, and enough to
/// scatter ids over a column.
pub fn fnv1a64(text: &str) -> u64 {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for byte in text.bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x0100_0000_01b3);
    }
    hash
}

/// Where every node goes: the column its kind lives in, and a height that
/// depends on nothing but the ids in that column.
pub fn layout(nodes: &[Node]) -> Vec<(u8, u16)> {
    let mut per_column: BTreeMap<u8, Vec<usize>> = BTreeMap::new();
    for (i, node) in nodes.iter().enumerate() {
        per_column.entry(column(node.kind)).or_default().push(i);
    }
    let mut out = vec![(0u8, 0u16); nodes.len()];
    for (col, members) in per_column {
        let mut ordered = members.clone();
        ordered.sort_by(|a, b| nodes[*a].id.cmp(&nodes[*b].id));
        let n = ordered.len();
        for (rank, i) in ordered.into_iter().enumerate() {
            let y = if n <= FEW {
                // Evenly spaced, centred in the column: rank + 1/2 out of n.
                ((2 * rank as u64 + 1) * u64::from(HEIGHT) / (2 * n as u64)) as u16
            } else {
                (fnv1a64(&nodes[i].id) >> 48) as u16
            };
            out[i] = (col, y);
        }
    }
    out
}

/// Cut a label to what fits beside a node.
fn label(text: &str) -> String {
    let text = text.trim().lines().next().unwrap_or_default().trim();
    let mut out: String = text.chars().take(MAX_LABEL).collect();
    if text.chars().count() > MAX_LABEL {
        out.push('โ€ฆ');
    }
    out
}

/// The text of an entry, whatever it holds.
fn text_of(entry: &Entry) -> String {
    String::from_utf8_lossy(&entry.value).into_owned()
}

/// The key family and the rest of a key under `ns`.
fn split<'a>(key: &'a str, prefix: &str) -> Option<(&'a str, &'a str)> {
    key.strip_prefix(prefix)?.split_once(':')
}

/// The keys of `family` that `text` refers to: written in full, or as
/// `<family>:<rest>`. Found by scanning for the family's name, so the cost
/// is the text's length and not the number of keys.
fn references(text: &str, family: &str, ns_prefix: &str, known: &BTreeSet<String>) -> Vec<String> {
    let marker = format!("{family}:");
    let mut found = Vec::new();
    let mut from = 0;
    while let Some(at) = text[from..].find(&marker) {
        let start = from + at + marker.len();
        let rest: String = text[start..]
            .chars()
            .take_while(|c| c.is_alphanumeric() || matches!(c, '.' | '_' | '-' | ':'))
            .collect();
        let rest = rest.trim_end_matches(':');
        if !rest.is_empty() {
            let key = format!("{ns_prefix}{marker}{rest}");
            if known.contains(&key) && !found.contains(&key) {
                found.push(key);
            }
        }
        from = start;
    }
    found
}

/// Build the graph of `ns` on `branch`, now or as it was at `at`.
pub fn build(
    db: &Db,
    branch: &str,
    ns: &str,
    at: Option<u64>,
    side: &Shared,
    connected: &[Connected],
) -> Result<Graph, memfork_core::Error> {
    let view = match at {
        Some(seq) => db.at(branch, seq)?,
        None => db.read(branch)?,
    };
    let prefix = format!("{ns}{}", crate::namespace::SEPARATOR);
    let entries = view.list(&prefix, None);
    let present = at.is_none();

    let mut nodes: Vec<Node> = Vec::with_capacity(entries.len());
    let mut index: BTreeMap<String, usize> = BTreeMap::new();
    let mut edges: Vec<(String, String, &'static str)> = Vec::new();
    let mut agents: BTreeMap<String, u64> = BTreeMap::new();

    let add = |nodes: &mut Vec<Node>, index: &mut BTreeMap<String, usize>, node: Node| {
        if let Some(&i) = index.get(&node.id) {
            if node.seq < nodes[i].seq {
                nodes[i].seq = node.seq;
            }
            return i;
        }
        index.insert(node.id.clone(), nodes.len());
        nodes.push(node);
        nodes.len() - 1
    };

    let stale: BTreeSet<String> = side.sidecar.stale_facts(ns).into_iter().collect();
    let known: BTreeSet<String> = entries.iter().map(|(k, _)| k.clone()).collect();
    let task_prefix = crate::namespace::prefix(ns, "task");
    // The tasks a handoff's next list can name, once: by id, as a word of
    // the item, and by title, as a substring. Titles are matched only while
    // the board is small enough for that to be cheap.
    let tasks: Vec<(String, String, String)> = entries
        .iter()
        .filter(|(k, _)| k.starts_with(&task_prefix))
        .map(|(k, e)| {
            let title = serde_json::from_slice::<Json>(&e.value)
                .ok()
                .and_then(|t| t["title"].as_str().map(str::to_lowercase))
                .unwrap_or_default();
            (k.clone(), k[task_prefix.len()..].to_lowercase(), title)
        })
        .collect();
    let task_by_id: BTreeMap<&str, &str> = tasks
        .iter()
        .map(|(key, id, _)| (id.as_str(), key.as_str()))
        .collect();
    let match_titles = tasks.len() <= MAX_TITLE_MATCHED;

    // Pass one: every entry is a node.
    for (key, entry) in &entries {
        let Some((family, rest)) = split(key, &prefix) else {
            continue;
        };
        let text = text_of(entry);
        let parsed: Json = serde_json::from_str(&text).unwrap_or(Json::Null);
        let who = entry
            .meta
            .get(WRITTEN_BY)
            .map(|w| crate::clients::display_for_writer(w));
        let is_fact = crate::facts::sources_of(&entry.meta).is_some();
        let (kind, label, state): (&'static str, String, Option<&'static str>) = match family {
            "decision" => ("decision", label(rest), None),
            "handoff" => (
                "handoff",
                format!("handoff {}", rest.trim_start_matches('0')),
                None,
            ),
            "lesson" => (
                "lesson",
                label(parsed["lesson"].as_str().unwrap_or(&text)),
                None,
            ),
            "task" => {
                let holder = present.then(|| side.board.holder(key)).flatten();
                let status = parsed["status"].as_str().unwrap_or("open");
                let state = if status == "done" {
                    "done"
                } else if holder.is_some() {
                    "claimed"
                } else {
                    "open"
                };
                if let Some((client, _)) = holder {
                    let agent = format!("agent:{}", crate::clients::display_for_writer(&client));
                    edges.push((agent, key.clone(), "holds"));
                }
                (
                    "task",
                    label(parsed["title"].as_str().unwrap_or(rest)),
                    Some(state),
                )
            }
            _ if is_fact => ("fact", label(rest), stale.contains(key).then_some("stale")),
            _ => ("entry", label(rest), None),
        };
        let seq = entry.created_seq;
        if let Some(w) = &who {
            let id = format!("agent:{w}");
            let first = agents.entry(id.clone()).or_insert(seq);
            *first = (*first).min(seq);
            edges.push((id, key.clone(), "wrote"));
        }
        add(
            &mut nodes,
            &mut index,
            Node {
                id: key.clone(),
                kind,
                label,
                seq,
                state,
                who,
            },
        );
    }

    // Pass two: what the entries say about each other.
    for (key, entry) in &entries {
        let Some((family, _)) = split(key, &prefix) else {
            continue;
        };
        let text = text_of(entry);
        let parsed: Json = serde_json::from_str(&text).unwrap_or(Json::Null);
        let seq = entry.created_seq;
        if let Some(sources) = crate::facts::sources_of(&entry.meta) {
            for path in sources {
                let id = format!("file:{path}");
                add(
                    &mut nodes,
                    &mut index,
                    Node {
                        id: id.clone(),
                        kind: "file",
                        label: path.clone(),
                        seq,
                        state: None,
                        who: None,
                    },
                );
                edges.push((id, key.clone(), "source of"));
            }
        }
        match family {
            "decision" => {
                let meta: String = entry.meta.values().cloned().collect::<Vec<_>>().join(" ");
                for fact in references(&format!("{text} {meta}"), "fact", &prefix, &known) {
                    edges.push((fact, key.clone(), "cited by"));
                }
            }
            "lesson" => {
                for decision in references(&text, "decision", &prefix, &known) {
                    edges.push((key.clone(), decision, "about"));
                }
                if let Some(task) = parsed["task"].as_str() {
                    let task_key = crate::board::task_key(ns, task);
                    if known.contains(&task_key) {
                        edges.push((key.clone(), task_key, "for"));
                    }
                }
            }
            "task" => {
                for dep in parsed["depends_on"].as_array().into_iter().flatten() {
                    if let Some(dep) = dep.as_str() {
                        let dep_key = crate::board::task_key(ns, dep);
                        if known.contains(&dep_key) {
                            edges.push((dep_key, key.clone(), "depends on"));
                        }
                    }
                }
            }
            "handoff" => {
                let next: Vec<String> = parsed["next"]
                    .as_array()
                    .into_iter()
                    .flatten()
                    .filter_map(Json::as_str)
                    .map(str::to_lowercase)
                    .collect();
                for item in &next {
                    for word in item
                        .split(|c: char| !(c.is_alphanumeric() || matches!(c, '.' | '_' | '-')))
                        .filter(|w| !w.is_empty())
                    {
                        if let Some(task_key) = task_by_id.get(word) {
                            edges.push((key.clone(), (*task_key).to_owned(), "next"));
                        }
                    }
                    if match_titles {
                        for (task_key, _, title) in &tasks {
                            if !title.is_empty() && item.contains(title.as_str()) {
                                edges.push((key.clone(), task_key.clone(), "next"));
                            }
                        }
                    }
                }
            }
            _ => {}
        }
    }

    // Briefings served, from beside the store.
    for brief in side.sidecar.briefings(ns) {
        if brief.branch != branch || at.is_some_and(|seq| brief.seq > seq) {
            continue;
        }
        let id = format!("brief:{:012}", brief.order);
        let to = format!("agent:{}", crate::clients::display_for_writer(&brief.to));
        agents.entry(to.clone()).or_insert(brief.seq);
        add(
            &mut nodes,
            &mut index,
            Node {
                id: id.clone(),
                kind: "brief",
                label: format!(
                    "briefing ยท {} B ยท to {}",
                    brief.bytes,
                    crate::clients::display_for_writer(&brief.to)
                ),
                seq: brief.seq,
                state: None,
                who: Some("MemFork".to_owned()),
            },
        );
        for key in &brief.keys {
            if known.contains(key) {
                edges.push((key.clone(), id.clone(), "in"));
            }
        }
        edges.push((id, to, "served to"));
    }

    // Agents: everyone who wrote, held or was served, and everyone here now.
    if present {
        for c in connected {
            let id = format!("agent:{}", crate::clients::display_for_writer(&c.client));
            agents.entry(id).or_insert(0);
        }
    }
    let here: BTreeSet<String> = connected
        .iter()
        .map(|c| format!("agent:{}", crate::clients::display_for_writer(&c.client)))
        .collect();
    for (id, seq) in agents {
        let name = id.trim_start_matches("agent:").to_owned();
        let state = if present && here.contains(&id) {
            "connected"
        } else {
            "away"
        };
        add(
            &mut nodes,
            &mut index,
            Node {
                id,
                kind: "agent",
                label: name,
                seq,
                state: Some(state),
                who: None,
            },
        );
    }
    for (from, key, kind) in &edges {
        if kind == &"holds" || kind == &"wrote" {
            if let Some(&i) = index.get(from) {
                if let Some(&j) = index.get(key) {
                    nodes[i].seq = nodes[i].seq.min(nodes[j].seq);
                }
            }
        }
    }

    // A fixed order: by column, then by id; the same store, the same list.
    let mut order: Vec<usize> = (0..nodes.len()).collect();
    order.sort_by(|a, b| {
        column(nodes[*a].kind)
            .cmp(&column(nodes[*b].kind))
            .then_with(|| nodes[*a].id.cmp(&nodes[*b].id))
    });
    let mut renumber = vec![0usize; nodes.len()];
    for (new, old) in order.iter().enumerate() {
        renumber[*old] = new;
    }
    let nodes: Vec<Node> = order.iter().map(|i| nodes[*i].clone()).collect();
    let mut resolved: Vec<Edge> = edges
        .iter()
        .filter_map(|(from, to, kind)| {
            Some(Edge {
                from: renumber[*index.get(from)?],
                to: renumber[*index.get(to)?],
                kind,
            })
        })
        .collect();
    resolved.sort();
    resolved.dedup();
    let positions = layout(&nodes);

    Ok(Graph {
        seq: view.seq(),
        at,
        nodes,
        positions,
        edges: resolved,
    })
}

/// The graph as the page receives it: arrays rather than objects, since a
/// large store has a hundred thousand nodes and every byte is parsed. A
/// node is id, kind, label, column, y, seq, state and writer, with the kind
/// and the writer as indexes into the kinds and agents lists, and the label
/// null when it is the id without its project, which the page derives.
pub fn to_json(graph: &Graph) -> Json {
    let mut counts: BTreeMap<&str, usize> = KINDS.iter().map(|k| (*k, 0)).collect();
    for node in &graph.nodes {
        *counts.entry(node.kind).or_insert(0) += 1;
    }
    let mut agents: Vec<&str> = Vec::new();
    let mut agent_index: BTreeMap<&str, usize> = BTreeMap::new();
    let nodes: Vec<Json> = graph
        .nodes
        .iter()
        .zip(&graph.positions)
        .map(|(n, (col, y))| {
            let kind = KINDS.iter().position(|k| *k == n.kind).unwrap_or(7);
            let who = n.who.as_deref().map(|w| {
                *agent_index.entry(w).or_insert_with(|| {
                    agents.push(w);
                    agents.len() - 1
                })
            });
            let derived = n.id.split_once(':').is_some_and(|(_, rest)| {
                rest.split_once(':')
                    .is_some_and(|(_, tail)| tail == n.label)
            });
            json!([
                n.id,
                kind,
                if derived { Json::Null } else { json!(n.label) },
                col,
                y,
                n.seq,
                n.state,
                who,
            ])
        })
        .collect();
    json!({
        "seq": graph.seq,
        "at": graph.at,
        "columns": COLUMNS,
        "kinds": KINDS,
        "agents": agents,
        "few": FEW,
        "height": HEIGHT,
        "nodes": nodes,
        "edges": graph.edges.iter().map(|e| json!([e.from, e.to, e.kind])).collect::<Vec<_>>(),
        "counts": counts,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use memfork_core::Value;

    fn shop() -> Db {
        let db = Db::new();
        let put = |key: &str, value: Value| {
            db.put("main", key, value).unwrap();
        };
        put(
            "shop:fact:auth-entry",
            Value::new("auth lives in src/auth, entry login.rs")
                .with_meta(
                    crate::facts::SOURCES_META,
                    crate::facts::sources_meta(&["src/auth/login.rs".to_owned()]),
                )
                .with_meta(WRITTEN_BY, "claude-code"),
        );
        put(
            "shop:decision:payments",
            Value::new("hosted checkout, see fact:auth-entry").with_meta(WRITTEN_BY, "claude-code"),
        );
        put(
            "shop:task:schema",
            Value::new(r#"{"title":"design the order schema","status":"done"}"#),
        );
        put(
            "shop:task:checkout",
            Value::new(r#"{"title":"checkout page","status":"open","depends_on":["schema"]}"#),
        );
        put(
            "shop:lesson:00000001",
            Value::new(r#"{"lesson":"cards break decision:payments","branch":"try-refunds"}"#)
                .with_meta(WRITTEN_BY, "codex-mcp-client"),
        );
        put(
            "shop:handoff:00000001",
            Value::new(r#"{"summary":"checkout works","next":["do the checkout page"]}"#)
                .with_meta(WRITTEN_BY, "claude-code"),
        );
        db
    }

    fn edge_kinds(graph: &Graph) -> Vec<(String, String, &'static str)> {
        graph
            .edges
            .iter()
            .map(|e| {
                (
                    graph.nodes[e.from].id.clone(),
                    graph.nodes[e.to].id.clone(),
                    e.kind,
                )
            })
            .collect()
    }

    #[test]
    fn the_relations_are_the_ones_the_engine_knows() {
        let db = shop();
        let side = Shared::in_memory();
        let graph = build(&db, "main", "shop", None, &side, &[]).unwrap();
        let edges = edge_kinds(&graph);
        let has =
            |a: &str, b: &str, k: &str| edges.iter().any(|(x, y, z)| x == a && y == b && *z == k);
        assert!(has(
            "file:src/auth/login.rs",
            "shop:fact:auth-entry",
            "source of"
        ));
        assert!(has(
            "shop:fact:auth-entry",
            "shop:decision:payments",
            "cited by"
        ));
        assert!(has(
            "shop:lesson:00000001",
            "shop:decision:payments",
            "about"
        ));
        assert!(has("shop:task:schema", "shop:task:checkout", "depends on"));
        assert!(has("shop:handoff:00000001", "shop:task:checkout", "next"));
        assert!(has("agent:Claude Code", "shop:decision:payments", "wrote"));
        assert!(has("agent:Codex CLI", "shop:lesson:00000001", "wrote"));
        let kinds: Vec<&str> = graph.nodes.iter().map(|n| n.kind).collect();
        assert!(kinds.contains(&"agent") && kinds.contains(&"file") && kinds.contains(&"task"));
        // Columns are in order along the node list.
        let cols: Vec<u8> = graph.positions.iter().map(|(c, _)| *c).collect();
        assert!(cols.windows(2).all(|w| w[0] <= w[1]));
    }

    #[test]
    fn the_same_store_gives_the_same_graph() {
        let side = Shared::in_memory();
        let a = build(&shop(), "main", "shop", None, &side, &[]).unwrap();
        let b = build(&shop(), "main", "shop", None, &side, &[]).unwrap();
        assert_eq!(a, b);
        assert_eq!(to_json(&a), to_json(&b));
    }

    #[test]
    fn few_nodes_are_spaced_evenly_and_many_are_hashed_and_stable() {
        let mut nodes: Vec<Node> = (0..3)
            .map(|i| Node {
                id: format!("shop:decision:d{i}"),
                kind: "decision",
                label: String::new(),
                seq: 0,
                state: None,
                who: None,
            })
            .collect();
        let few = layout(&nodes);
        assert_eq!(
            few.iter().map(|(_, y)| *y).collect::<Vec<_>>(),
            vec![10922, 32767, 54612]
        );

        for i in 3..40 {
            nodes.push(Node {
                id: format!("shop:decision:d{i}"),
                kind: "decision",
                label: String::new(),
                seq: 0,
                state: None,
                who: None,
            });
        }
        let many = layout(&nodes);
        nodes.push(Node {
            id: "shop:decision:one-more".to_owned(),
            kind: "decision",
            label: String::new(),
            seq: 0,
            state: None,
            who: None,
        });
        let more = layout(&nodes);
        assert_eq!(
            &more[..many.len()],
            &many[..],
            "adding a node moved another"
        );
        assert_eq!(
            more[many.len()].1,
            (fnv1a64("shop:decision:one-more") >> 48) as u16
        );
        assert_eq!(fnv1a64(""), 0xcbf2_9ce4_8422_2325);
        assert_eq!(fnv1a64("a"), 0xaf63_dc4c_8601_ec8c);
    }

    #[test]
    fn a_past_view_shows_what_was_there_then() {
        let db = shop();
        let side = Shared::in_memory();
        let now = build(&db, "main", "shop", None, &side, &[]).unwrap();
        let then = build(&db, "main", "shop", Some(2), &side, &[]).unwrap();
        assert!(then.nodes.len() < now.nodes.len());
        assert_eq!(then.at, Some(2));
        assert!(then.nodes.iter().all(|n| n.seq <= 2));
    }

    #[test]
    fn references_are_found_by_key_or_shorthand() {
        let known: BTreeSet<String> = ["shop:fact:a-b", "shop:fact:c"]
            .iter()
            .map(|s| (*s).to_owned())
            .collect();
        let found = references(
            "see shop:fact:a-b and fact:c, not fact:zzz.",
            "fact",
            "shop:",
            &known,
        );
        assert_eq!(
            found,
            vec!["shop:fact:a-b".to_owned(), "shop:fact:c".to_owned()]
        );
    }
}