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
//! What the page shows beside the graph: the headline, the panels, the
//! footer, one entry in full, a ranked search, and the keys two branches
//! disagree on. All of it read from the store and the side structure, none
//! of it changing either.

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

use hyper::StatusCode;
use memfork_core::{Entry, Op, WRITTEN_BY};
use serde_json::{json, Value as Json};

use super::{namespaces, Context, Failure, Query};
use crate::namespace;

/// Most hits a search returns.
pub const MAX_HITS: usize = 50;

/// Most commits walked for one entry's history.
pub const MAX_HISTORY_COMMITS: usize = 10_000;

/// Most lines of history returned for one entry.
pub const MAX_HISTORY: usize = 50;

/// Most keys a branch comparison lists.
pub const MAX_DIFF: usize = 500;

/// Longest value shown in a panel row.
const ROW_CHARS: usize = 300;

/// Longest value shown in the side sheet.
const SHEET_CHARS: usize = 20_000;

fn clip(text: &str, max: usize) -> String {
    let mut out: String = text.chars().take(max).collect();
    if text.chars().count() > max {
        out.push('…');
    }
    out
}

fn text_of(entry: &Entry) -> String {
    String::from_utf8_lossy(&entry.value).into_owned()
}

fn who(entry: &Entry) -> Option<String> {
    entry
        .meta
        .get(WRITTEN_BY)
        .map(|w| crate::clients::display_for_writer(w))
}

fn display(name: &str) -> String {
    crate::clients::display_for_writer(name)
}

/// The branch a query names, or the default one; refused if it does not
/// exist.
pub fn branch_of(context: &Context, query: &Query) -> Result<String, Failure> {
    let branch = query
        .get("branch")
        .unwrap_or_else(|| context.db.default_branch())
        .to_owned();
    if !context.db.has_branch(&branch) {
        return Err((
            StatusCode::NOT_FOUND,
            format!("there is no branch `{branch}`"),
        ));
    }
    Ok(branch)
}

/// The project a query names, or the one to show first: what a connected
/// client is working in, else the first project on the branch, else the
/// fallback.
pub fn namespace_of(context: &Context, query: &Query, branch: &str) -> Result<String, Failure> {
    if let Some(ns) = query.get("ns") {
        namespace::validate(ns).map_err(|why| (StatusCode::BAD_REQUEST, format!("`ns`: {why}")))?;
        return Ok(ns.to_owned());
    }
    if let Some(connected) = context.events.connected().first() {
        return Ok(connected.namespace.clone());
    }
    Ok(namespaces(&context.db, branch)?
        .into_iter()
        .next()
        .unwrap_or_else(|| namespace::FALLBACK.to_owned()))
}

/// The headline, the panels and the footer for a project on a branch.
pub fn summary(context: &Context, query: &Query) -> Result<Json, Failure> {
    let branch = branch_of(context, query)?;
    let ns = namespace_of(context, query, &branch)?;
    let db = &context.db;
    let side = &context.side;
    let view = db
        .read(&branch)
        .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let prefix = format!("{ns}{}", namespace::SEPARATOR);
    let entries = view.list(&prefix, None);
    let connected = context.events.connected();

    // The headline: real counts, from the side file.
    let stats = side.sidecar.stats(Some(&ns));
    let total = &stats["total"];
    let count = |name: &str| total[name].as_u64().unwrap_or(0);
    let counts = json!({
        "briefings": count("briefings"),
        "handoffs_picked_up": count("handoffs_picked_up"),
        "dead_ends_not_repeated": count("lessons_served"),
        "stale_facts_caught": count("facts_stale"),
        "claim_conflicts_avoided": count("claim_conflicts"),
        "briefing_bytes": count("briefing_bytes"),
        "memory_bytes": count("memory_bytes"),
    });
    let twice = count("lessons_served")
        + count("facts_stale")
        + count("claim_conflicts")
        + count("handoffs_picked_up");

    let briefings = side.sidecar.briefings(&ns);
    let served_count = |key: &str| {
        briefings
            .iter()
            .filter(|b| b.keys.iter().any(|k| k == key))
            .count()
    };

    // Handoffs: who left each, who picked it up, and what they took next.
    let handoff_prefix = namespace::prefix(&ns, "handoff");
    let task_prefix = namespace::prefix(&ns, "task");
    let mut handoffs: Vec<Json> = entries
        .iter()
        .filter(|(k, _)| k.starts_with(&handoff_prefix))
        .map(|(key, entry)| {
            let parsed: Json = serde_json::from_slice(&entry.value).unwrap_or(Json::Null);
            let by = who(entry);
            let pickup = briefings
                .iter()
                .filter(|b| b.handoff.as_deref() == Some(key.as_str()))
                .find(|b| Some(display(&b.to)) != by);
            let picked = pickup.map(|b| {
                let to = display(&b.to);
                // What that client took on after the briefing: a task it holds
                // now, or one it finished since.
                let next: Vec<String> = entries
                    .iter()
                    .filter(|(k, _)| k.starts_with(&task_prefix))
                    .filter(|(k, e)| {
                        let held = side.board.holder(k).is_some_and(|(c, _)| display(&c) == to);
                        let task: Json = serde_json::from_slice(&e.value).unwrap_or(Json::Null);
                        let done_by = task["done_by"].as_str().map(display);
                        held || (done_by.as_deref() == Some(to.as_str())
                            && e.last_access_seq > b.seq)
                    })
                    .map(|(k, e)| {
                        let task: Json = serde_json::from_slice(&e.value).unwrap_or(Json::Null);
                        task["title"]
                            .as_str()
                            .map_or_else(|| k[task_prefix.len()..].to_owned(), str::to_owned)
                    })
                    .collect();
                json!({
                    "to": to,
                    "bytes": b.bytes,
                    "approx_tokens": b.bytes.div_ceil(4),
                    "seq": b.seq,
                    "next": next,
                })
            });
            json!({
                "key": key,
                "number": key[handoff_prefix.len()..].trim_start_matches('0'),
                "by": by,
                "seq": entry.created_seq,
                "summary": clip(parsed["summary"].as_str().unwrap_or(&text_of(entry)), ROW_CHARS),
                "next": parsed["next"],
                "blockers": parsed["blockers"],
                "questions": parsed["questions"],
                "picked_up": picked,
            })
        })
        .collect();
    handoffs.reverse();

    // Coordination: the board as it stands.
    let tasks = side
        .board
        .list(db, &branch, &ns, "all")
        .map(|l| l["tasks"].clone())
        .unwrap_or_else(|_| json!([]));

    // Freshness: every fact, with what the last check found.
    let stale: BTreeSet<String> = side.sidecar.stale_facts(&ns).into_iter().collect();
    let fresh: BTreeSet<String> = side.sidecar.fresh_facts(&ns).into_iter().collect();
    let fact_state = |key: &str| {
        if stale.contains(key) {
            "stale"
        } else if fresh.contains(key) {
            "fresh"
        } else {
            "unverified"
        }
    };
    let facts: Vec<Json> = entries
        .iter()
        .filter_map(|(key, entry)| {
            let sources = crate::facts::sources_of(&entry.meta)?;
            Some(json!({
                "key": key,
                "value": clip(&text_of(entry), ROW_CHARS),
                "sources": sources,
                "state": fact_state(key),
                "by": who(entry),
                "seq": entry.created_seq,
            }))
        })
        .collect();

    // Dead ends: lessons, where they came from, how often they were served.
    let lesson_prefix = namespace::prefix(&ns, "lesson");
    let mut lessons: Vec<Json> = entries
        .iter()
        .filter(|(k, _)| k.starts_with(&lesson_prefix))
        .map(|(key, entry)| {
            let mut view = crate::lessons::view(key, entry);
            view["by"] = json!(who(entry));
            view["served"] = json!(served_count(key));
            view["seq"] = json!(entry.created_seq);
            view
        })
        .collect();
    lessons.reverse();

    // Briefings served, newest first.
    let mut served: Vec<Json> = briefings
        .iter()
        .filter(|b| b.branch == branch)
        .map(|b| {
            let mut families: BTreeMap<&str, usize> = BTreeMap::new();
            for key in &b.keys {
                let family = key
                    .strip_prefix(&prefix)
                    .and_then(|rest| rest.split_once(':'))
                    .map_or("entry", |(f, _)| f);
                *families.entry(family).or_insert(0) += 1;
            }
            json!({
                "order": b.order,
                "to": display(&b.to),
                "bytes": b.bytes,
                "approx_tokens": b.bytes.div_ceil(4),
                "seq": b.seq,
                "carried": families,
                "keys": b.keys,
                "omitted": b.omitted,
                "since_commits": b.since_commits,
                "task": b.task,
            })
        })
        .collect();
    served.reverse();

    // The footer.
    let store_bytes: usize = view
        .list("", None)
        .iter()
        .map(|(k, e)| k.len() + e.value.len())
        .sum();
    let log = db.log(&branch, None).unwrap_or_default();
    let horizon = log.last().map_or(0, |c| c.seq);
    let policy = match crate::policy::current() {
        Ok(p) => p.summary(),
        Err(e) => format!("unreadable: {}", e.why),
    };
    let branches: Vec<String> = db.branches().into_iter().map(|b| b.name).collect();

    // Autopilot: what it did, and what it left for a person.
    let autopilot = crate::autopilot::engine::Context {
        db: db.clone(),
        shared: Arc::clone(side),
        events: Some(Arc::clone(&context.events)),
    }
    .status_of(&ns);

    Ok(json!({
        "version": crate::VERSION,
        "page_build": super::page_build(),
        "port": context.port,
        "read_only": true,
        "namespace": ns,
        "branch": branch,
        "seq": view.seq(),
        "entries": entries.len(),
        "branches": branches,
        "namespaces": namespaces(db, &branch)?,
        "clients": connected,
        "policy": policy,
        "headline": { "twice": twice, "counts": counts },
        "handoffs": handoffs,
        "tasks": tasks,
        "facts": facts,
        "lessons": lessons,
        "briefings": served,
        "autopilot": autopilot,
        "footer": {
            "store_bytes": store_bytes,
            "commits_retained": db.commit_count(),
            "history_from_seq": horizon,
            "branch_entries": view.len(),
        },
    }))
}

/// What needs a look: contradictions, near-duplicates, stale facts,
/// maintenance suggestions and claims whose holder is gone. Flagged, never
/// fixed. Its own route, fetched after the first paint: scanning a large
/// project for duplicates is the one slow thing here, and the headline and
/// the panels need not wait for it.
pub fn attention(context: &Context, query: &Query) -> Result<Json, Failure> {
    let branch = branch_of(context, query)?;
    let ns = namespace_of(context, query, &branch)?;
    let db = &context.db;
    let side = &context.side;
    let view = db
        .read(&branch)
        .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let prefix = format!("{ns}{}", namespace::SEPARATOR);
    let entries = view.list(&prefix, None);
    let task_prefix = namespace::prefix(&ns, "task");
    let here: BTreeSet<String> = context
        .events
        .connected()
        .iter()
        .map(|c| display(&c.client))
        .collect();
    let stale: BTreeSet<String> = side.sidecar.stale_facts(&ns).into_iter().collect();
    // Attention: flagged, never fixed.
    let mut attention: Vec<Json> = Vec::new();
    for flag in crate::flags::scan(db, &branch, &ns).unwrap_or_default() {
        let (title, detail) = match flag["kind"].as_str().unwrap_or("") {
            "conflicting_decisions" => (
                format!(
                    "Two decisions on {}",
                    short(&ns, flag["key"].as_str().unwrap_or(""))
                ),
                flag["branches"]
                    .as_array()
                    .into_iter()
                    .flatten()
                    .map(|b| {
                        format!(
                            "{} on {}",
                            b["by"]
                                .as_str()
                                .map_or_else(|| "someone".to_owned(), display),
                            b["branch"].as_str().unwrap_or("?")
                        )
                    })
                    .collect::<Vec<_>>()
                    .join(", "),
            ),
            "duplicate" => (
                "Near-duplicate keys".to_owned(),
                flag["keys"]
                    .as_array()
                    .into_iter()
                    .flatten()
                    .filter_map(Json::as_str)
                    .collect::<Vec<_>>()
                    .join(" and "),
            ),
            "facts_disagree" => (
                "Facts from the same files disagree".to_owned(),
                flag["keys"]
                    .as_array()
                    .into_iter()
                    .flatten()
                    .filter_map(Json::as_str)
                    .collect::<Vec<_>>()
                    .join(" and "),
            ),
            other => (
                other.to_owned(),
                flag["look"].as_str().unwrap_or("").to_owned(),
            ),
        };
        attention.push(json!({
            "kind": flag["kind"],
            "title": title,
            "detail": detail,
            "keys": flag["keys"].as_array().cloned().or_else(|| flag["key"].as_str().map(|k| vec![json!(k)])),
        }));
    }
    for key in &stale {
        if entries.iter().any(|(k, _)| k == key) {
            attention.push(json!({
                "kind": "stale_fact",
                "title": "Fact went stale",
                "detail": format!("{} · re-check before trusting", short(&ns, key)),
                "keys": [key],
            }));
        }
    }
    for (trigger, key) in side.sidecar.fired_all(&ns) {
        attention.push(json!({
            "kind": "maintenance",
            "title": "Memory could use tidying",
            "detail": format!("{} · task {} is on the board", trigger.replace('_', " "), short(&ns, &key)),
            "keys": [key],
        }));
    }
    for (key, _) in entries.iter().filter(|(k, _)| k.starts_with(&task_prefix)) {
        if let Some((client, left)) = side.board.holder(key) {
            let holder = display(&client);
            if !here.contains(&holder) {
                attention.push(json!({
                    "kind": "stuck_claim",
                    "title": "A claim whose holder is not connected",
                    "detail": format!("{} held by {holder} · lease ends in {left} s", short(&ns, key)),
                    "keys": [key],
                }));
            }
        }
    }

    // Autopilot: a fork it kept for a person, and memory whose git branch is
    // gone. Listed with the way out; never done by MemFork.
    let autopilot = crate::autopilot::engine::Context {
        db: db.clone(),
        shared: Arc::clone(side),
        events: Some(Arc::clone(&context.events)),
    }
    .status_of(&ns);
    for fork in autopilot["kept_forks"].as_array().into_iter().flatten() {
        let name = fork.as_str().unwrap_or("?");
        attention.push(json!({
            "kind": "autopilot_fork_kept",
            "title": "A fork autopilot kept",
            "detail": format!("{name} · no check judged it: merge it, or discard it with a lesson"),
            "keys": [],
        }));
    }
    for orphan in autopilot["orphans"].as_array().into_iter().flatten() {
        let name = orphan["branch"].as_str().unwrap_or("?");
        attention.push(json!({
            "kind": "orphan_branch",
            "title": "Memory branch with no git branch",
            "detail": format!(
                "{name} · {} · {}, then {}",
                orphan["why"].as_str().unwrap_or(""),
                orphan["merge_then_discard"][0].as_str().unwrap_or(""),
                orphan["merge_then_discard"][1].as_str().unwrap_or("")
            ),
            "keys": [],
        }));
    }

    Ok(json!({
        "namespace": ns,
        "branch": branch,
        "attention": attention,
    }))
}

/// A key without its project, for a sentence.
fn short(ns: &str, key: &str) -> String {
    key.strip_prefix(&format!("{ns}{}", namespace::SEPARATOR))
        .unwrap_or(key)
        .to_owned()
}

/// One entry in full: its value, its metadata, its sources and what the last
/// check found for each, and its history across commits.
pub fn entry(context: &Context, query: &Query) -> Result<Json, Failure> {
    let branch = branch_of(context, query)?;
    let key = query
        .get("key")
        .ok_or_else(|| (StatusCode::BAD_REQUEST, "`key` is needed".to_owned()))?;
    let at = query.number("at")?;
    let db = &context.db;
    let view = match at {
        Some(seq) => db.at(&branch, seq),
        None => db.read(&branch),
    }
    .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let Some(entry) = view.get(key) else {
        return Err((
            StatusCode::NOT_FOUND,
            format!("there is no `{key}` on {branch}"),
        ));
    };
    let ns = key.split_once(':').map_or("", |(ns, _)| ns);
    let stale: BTreeSet<String> = context.side.sidecar.stale_facts(ns).into_iter().collect();
    let fresh: BTreeSet<String> = context.side.sidecar.fresh_facts(ns).into_iter().collect();
    let state = if stale.contains(key) {
        "stale"
    } else if fresh.contains(key) {
        "fresh"
    } else {
        "unverified"
    };
    let sources = crate::facts::sources_of(&entry.meta).map(|paths| {
        paths
            .into_iter()
            .map(|p| json!({ "path": p, "state": state }))
            .collect::<Vec<_>>()
    });

    // History: every commit on the branch that touched this key, newest
    // first, within the retention window.
    let mut history = Vec::new();
    let mut cursor = db.head(&branch).ok();
    let mut walked = 0;
    while let Some(id) = cursor {
        if walked >= MAX_HISTORY_COMMITS || history.len() >= MAX_HISTORY {
            break;
        }
        let Ok(commit) = db.commit(id) else { break };
        walked += 1;
        for op in &commit.ops {
            if op.key() != key {
                continue;
            }
            let (what, by) = match op {
                Op::Put { value, .. } => {
                    ("written", value.meta.get(WRITTEN_BY).map(|w| display(w)))
                }
                Op::Delete { .. } => ("deleted", None),
                Op::Evict { .. } => ("evicted to stay in budget", None),
            };
            history.push(json!({
                "seq": commit.seq,
                "commit": commit.id.to_hex(),
                "what": what,
                "by": by,
                "message": commit.message,
            }));
        }
        cursor = commit.first_parent();
    }

    let meta: BTreeMap<&String, &String> = entry
        .meta
        .iter()
        .filter(|(k, _)| k.as_str() != crate::facts::SOURCES_META)
        .collect();
    Ok(json!({
        "key": key,
        "branch": branch,
        "at": at,
        "value": clip(&text_of(&entry), SHEET_CHARS),
        "bytes": entry.value.len(),
        "by": who(&entry),
        "importance": entry.importance,
        "created_seq": entry.created_seq,
        "last_seq": entry.last_access_seq,
        "ttl_commits": entry.ttl_commits,
        "meta": meta,
        "sources": sources,
        "history": history,
        "history_truncated": history.len() >= MAX_HISTORY,
    }))
}

/// The engine's ranked text search over a project on a branch.
pub fn search(context: &Context, query: &Query) -> Result<Json, Failure> {
    let branch = branch_of(context, query)?;
    let ns = namespace_of(context, query, &branch)?;
    let q = query.get("q").unwrap_or_default();
    let view = context
        .db
        .read(&branch)
        .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let entries = view.list(&format!("{ns}{}", namespace::SEPARATOR), None);
    let texts: Vec<(&str, String)> = entries
        .iter()
        .map(|(k, e)| (k.as_str(), text_of(e)))
        .collect();
    let docs: Vec<crate::find::Doc<'_>> = texts
        .iter()
        .map(|(k, t)| crate::find::Doc { key: k, text: t })
        .collect();
    let hits: Vec<Json> = if crate::find::words(q).is_empty() {
        Vec::new()
    } else {
        crate::find::rank(&docs, q, MAX_HITS)
            .into_iter()
            .map(|h| {
                let text = texts
                    .iter()
                    .find(|(k, _)| *k == h.key)
                    .map_or("", |(_, t)| t.as_str());
                json!({
                    "key": h.key,
                    "score": h.score,
                    "snippet": crate::find::snippet(text, h.first_match),
                })
            })
            .collect()
    };
    Ok(json!({
        "namespace": ns,
        "branch": branch,
        "query": q,
        "hits": hits,
        "limit": MAX_HITS,
    }))
}

/// The keys that differ between two branches: what a merge would face.
pub fn diff(context: &Context, query: &Query) -> Result<Json, Failure> {
    let a = query
        .get("a")
        .ok_or_else(|| (StatusCode::BAD_REQUEST, "`a` is needed".to_owned()))?;
    let b = query
        .get("b")
        .ok_or_else(|| (StatusCode::BAD_REQUEST, "`b` is needed".to_owned()))?;
    for name in [a, b] {
        if !context.db.has_branch(name) {
            return Err((
                StatusCode::NOT_FOUND,
                format!("there is no branch `{name}`"),
            ));
        }
    }
    let ns = query
        .get("ns")
        .map(|ns| format!("{ns}{}", namespace::SEPARATOR));
    let changes = context
        .db
        .diff(a, b)
        .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let va = context
        .db
        .read(a)
        .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let vb = context
        .db
        .read(b)
        .map_err(|e| (StatusCode::NOT_FOUND, e.to_string()))?;
    let wanted: Vec<_> = changes
        .iter()
        .filter(|c| ns.as_ref().is_none_or(|p| c.key.starts_with(p)))
        .collect();
    let total = wanted.len();
    let listed: Vec<Json> = wanted
        .iter()
        .take(MAX_DIFF)
        .map(|c| {
            let value = |v: Option<std::sync::Arc<Entry>>| v.map(|e| clip(&text_of(&e), ROW_CHARS));
            json!({
                "key": c.key,
                "kind": match c.kind {
                    memfork_core::ChangeKind::Added => "added",
                    memfork_core::ChangeKind::Removed => "removed",
                    memfork_core::ChangeKind::Modified => "modified",
                },
                "a": value(va.get(&c.key)),
                "b": value(vb.get(&c.key)),
            })
        })
        .collect();
    Ok(json!({
        "a": a,
        "b": b,
        "namespace": query.get("ns"),
        "count": total,
        "changes": listed,
        "omitted": total.saturating_sub(MAX_DIFF),
    }))
}