memstead-git-branch 0.3.0

Mem-repo engine for Memstead — multi-mem, git-backed typed entity graphs. Internal library surface consumed by the memstead binaries — pre-1.0, experimental, no API stability promise.
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
//! Top-level test-support helpers used across the workspace's
//! integration tests.
//!
//! `Engine::init_with_settings` fail-fasts when the workspace lacks
//! `<workspace_root>/mem-repo/.git/`. Tests that need the engine to
//! boot use [`init_real_mem_repo`] to lay down a real `mem-repo-git`
//! carrying `main` (operator docs) plus `__MEMSTEAD` (with
//! `mems/<name>/config.json` blobs) plus an empty-tree initial commit
//! on each requested mem branch.
//!
//! # Visibility
//!
//! The module is compiled in for the crate's own `cfg(test)` builds and
//! for downstream consumers that opt into the `test-support` Cargo
//! feature in their `[dev-dependencies]` — that is how `memstead-cli`,
//! `memstead-mcp`, and `memstead-swift` reach in.
//!
//! # Negative-case coverage
//!
//! The fail-fast itself is exercised by
//! `tests::engine_init_against_workspace_without_mem_repo_fails` in
//! `memstead-git-branch/src/lib.rs`. That test deliberately avoids calling this
//! helper so the missing-dir error path stays covered.

use std::path::{Path, PathBuf};
use std::sync::Mutex;

use memstead_base::{
    FileWorkspaceStore, Mount, MountCapability, MountLifecycle, MountStorage, Workspace,
    WorkspaceStoreAdapter,
};

/// Write `.memstead/workspace.toml` + `.memstead/state/mounts.json` carrying
/// one git-branch mount per `(mem, branch)` pair so the new-layout
/// loader (`Engine::from_workspace_root` / `engine_from_workspace_root`)
/// discovers the workspace. `branch` defaults to `mem` when omitted.
/// `mems` carries `(mem_name, branch, schema_pin_str)` per mount.
/// Reading the schema pin off the just-seeded gitdir would work too,
/// but callers already know it — passing it through keeps the helper
/// independent of the read path (and side-steps `read_config_at_gitdir`'s
/// dependency on the gitdir already being committed).
fn seed_new_layout(workspace_root: &Path, gitdir: &Path, mems: &[(&str, &str, &str)]) {
    let memstead = workspace_root.join(".memstead");
    std::fs::create_dir_all(&memstead).unwrap();
    let toml_path = memstead.join("workspace.toml");
    if !toml_path.is_file() {
        // schemas_dir is a top-level key — emit it BEFORE the
        // [persistence_adapter] section so TOML's greedy section
        // parsing doesn't fold it into that table.
        let mut head = String::from("format = \"memstead-git-branch-2\"\n");
        head.push_str("\n[persistence_adapter]\nname = \"file-two-layer\"\n");
        std::fs::write(&toml_path, head).unwrap();
    }
    let mounts: Vec<Mount> = mems
        .iter()
        .map(|(mem, branch, schema)| {
            let pin: memstead_schema::SchemaRef = schema
                .parse()
                .unwrap_or_else(|e| panic!("test seed: invalid schema pin {schema:?}: {e}"));
            Mount {
                mem: mem.to_string(),
                schema: Some(pin),
                storage: MountStorage::GitBranch {
                    gitdir: gitdir.to_path_buf(),
                    branch: branch.to_string(),
                },
                capability: MountCapability::Write,
                lifecycle: MountLifecycle::Eager,
                cross_linkable: true,
                migration_target: None,
            }
        })
        .collect();
    let workspace = Workspace {
        mounts,
        settings: Default::default(),
    };
    FileWorkspaceStore::new()
        .save_state(workspace_root, &workspace)
        .unwrap();
}

/// Initialise `<workspace_root>/mem-repo/.git/` as a real bare repo
/// carrying `main` (operator-facing docs, empty in tests) plus
/// `__SYSTEM` (with `<name>/config.json` blobs for every mem) plus an
/// empty-tree initial commit on each per-mem branch
/// (`refs/heads/<name>`).
///
/// `mems` is `&[(name, schema_name)]`. Each entry produces:
/// - one `<name>/config.json` blob on `__SYSTEM` of the form
///   `{"schema": "<schema>"}`, and
/// - one branch `refs/heads/<name>` with an empty-tree initial commit.
///
/// Mirrors the K-shape registry layout so `mem_repo_config::read_config`
/// and `mem_init_from_branch` both work against the result.
///
/// Idempotent: running against an existing bare repo with the same
/// shape is a no-op (the gix repo open succeeds, the per-branch ref
/// exists). Running against an existing repo with a *different* shape
/// will append commits — tests should pass a fresh `TempDir` per run.
///
/// Panics on any gix or fs failure, mirroring the `.unwrap()` pattern
/// the rest of the integration tests use.
pub fn init_real_mem_repo(workspace_root: &Path, mems: &[(&str, &str)]) -> PathBuf {
    // Process-wide lock so two parallel test threads racing on the
    // same workspace_root (e.g. multiple `PathBuf::from(".")` callers)
    // do not interleave between the `gix::open` probe and the
    // subsequent `gix::init_bare`.
    static INIT_LOCK: Mutex<()> = Mutex::new(());

    let gitdir = workspace_root.join("mem-repo").join(".git");
    std::fs::create_dir_all(&gitdir).unwrap_or_else(|e| {
        panic!(
            "failed to create test mem-repo at {}: {e}",
            gitdir.display()
        )
    });

    let _guard = INIT_LOCK.lock().unwrap_or_else(|e| e.into_inner());

    let repo = match gix::open(&gitdir) {
        Ok(r) => r,
        Err(_) => gix::init_bare(&gitdir).unwrap_or_else(|e| {
            panic!(
                "failed to initialise bare mem-repo-git at {}: {e}",
                gitdir.display()
            )
        }),
    };

    // Skip the seed if `__SYSTEM` already exists — idempotent re-runs.
    if matches!(repo.try_find_reference("refs/heads/__SYSTEM"), Ok(Some(_))) {
        return workspace_root.to_path_buf();
    }

    let actor = gix::actor::Signature {
        name: "test".into(),
        email: "test@example.com".into(),
        time: gix::date::Time {
            seconds: 0,
            offset: 0,
        },
    };

    // Seed `main` with empty tree (operator-facing docs branch — content
    // not used by engine).
    {
        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        repo.commit_as(
            actor_ref,
            actor_ref,
            "refs/heads/main",
            "seed main",
            repo.empty_tree().id().detach(),
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();
    }

    // Seed `__SYSTEM` with one `<name>/config.json` blob per mem.
    let mut system_editor = repo.empty_tree().edit().unwrap();
    for (name, schema) in mems {
        let config = format!(r#"{{"schema": "{schema}"}}"#);
        let blob = repo.write_blob(config.as_bytes()).unwrap().detach();
        system_editor
            .upsert(
                format!("{name}/config.json"),
                gix::objs::tree::EntryKind::Blob,
                blob,
            )
            .unwrap();
    }
    let system_tree = system_editor.write().unwrap().detach();
    let mut buf = gix::date::parse::TimeBuf::default();
    let actor_ref = actor.to_ref(&mut buf);
    repo.commit_as(
        actor_ref,
        actor_ref,
        "refs/heads/__SYSTEM",
        "seed __SYSTEM",
        system_tree,
        Vec::<gix::ObjectId>::new(),
    )
    .unwrap();

    let empty_tree = repo.empty_tree().id().detach();
    for (name, _) in mems {
        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        let ref_name = format!("refs/heads/{name}");
        repo.commit_as(
            actor_ref,
            actor_ref,
            ref_name.as_str(),
            format!("seed {name}"),
            empty_tree,
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();
    }

    // Project __SYSTEM onto __MEMSTEAD so post-s140 reads land.
    crate::storage_memstead::migrate_to_memstead_ref(&gitdir).unwrap();

    seed_new_layout(
        workspace_root,
        &gitdir,
        &mems
            .iter()
            .map(|(n, schema)| (*n, *n, *schema))
            .collect::<Vec<_>>(),
    );

    workspace_root.to_path_buf()
}

/// Auto-detect every disk-shaped mem directory directly under
/// `workspace_root` (any subdir carrying `.memstead/config.json`), then
/// seed `<workspace_root>/mem-repo/.git/` from those dirs. Returns a
/// default `WorkspaceSettings`.
pub fn auto_seeded_settings(workspace_root: &Path) -> memstead_base::WorkspaceSettings {
    auto_seed_with_settings(workspace_root, memstead_base::WorkspaceSettings::default())
}

/// Like [`auto_seeded_settings`] but preserves caller-supplied
/// `WorkspaceSettings` overrides (e.g. `mem_create_rules`,
/// `schemas_dir`).
pub fn auto_seed_with_settings(
    workspace_root: &Path,
    settings: memstead_base::WorkspaceSettings,
) -> memstead_base::WorkspaceSettings {
    let mut mems: Vec<(std::path::PathBuf, String)> = Vec::new();
    if let Ok(rd) = std::fs::read_dir(workspace_root) {
        for entry in rd.flatten() {
            let p = entry.path();
            if !p.is_dir() {
                continue;
            }
            // Skip the mem-repo gitdir itself.
            if p.file_name().and_then(|s| s.to_str()) == Some("mem-repo") {
                continue;
            }
            let cfg_path = p.join(".memstead").join("config.json");
            if !cfg_path.is_file() {
                continue;
            }
            // Configs no longer carry an in-config `name` field. The
            // mem leaf identifier is the disk-path basename.
            if let Some(name) = p.file_name().and_then(|s| s.to_str()) {
                mems.push((p.clone(), name.to_string()));
            }
        }
    }
    mems.sort_by(|a, b| a.1.cmp(&b.1));
    let refs: Vec<(&Path, &str)> = mems
        .iter()
        .map(|(p, n)| (p.as_path(), n.as_str()))
        .collect();
    init_real_mem_repo_from_disk(workspace_root, &refs);
    settings
}

/// Re-seed `<workspace_root>/mem-repo/.git/` from disk after the disk
/// shape has changed. Deletes the existing gitdir and re-runs
/// [`init_real_mem_repo_from_disk`].
///
/// Useful for tests that mutate disk mem content via `fs::write`
/// between `init` and `engine.reload()` — without re-seeding, the
/// dispatcher's GitTree read path keeps returning the stale tree
/// state. Mirrors disk-fall-through behaviour where reload re-reads
/// the live disk content.
pub fn reseed_mem_repo_from_disk(workspace_root: &Path, mems: &[(&Path, &str)]) -> PathBuf {
    let gitdir = workspace_root.join("mem-repo").join(".git");
    let _ = std::fs::remove_dir_all(&gitdir);
    init_real_mem_repo_from_disk(workspace_root, mems)
}

/// Migration helper: bootstrap `<workspace_root>/mem-repo/.git/` from
/// the layout of disk-shaped mem directories already on disk.
///
/// For each `(mem_dir, name)` entry, reads the disk-shape mem at
/// `<workspace_root>/<mem_dir>/`:
/// - `<mem_dir>/.memstead/config.json` (if present) → `__SYSTEM:<name>/config.json`
/// - every `*.md` file under `<mem_dir>/` (recursive) → branch tree
///
/// Use this from tests that pre-populate disk mems via `fs::write`
/// before initialising the engine. The helper preserves the entity
/// content so dispatcher GitTree reads return the same data the disk
/// path used to surface.
///
/// `name` is the mem name (matches `MemInit.name`). It is used
/// for the branch ref and the `<name>/config.json` blob on `__SYSTEM`.
pub fn init_real_mem_repo_from_disk(workspace_root: &Path, mems: &[(&Path, &str)]) -> PathBuf {
    let triples: Vec<(&Path, &str, &str)> = mems
        .iter()
        .map(|(dir, name)| (*dir, *name, *name))
        .collect();
    init_real_mem_repo_from_disk_with_paths(workspace_root, &triples)
}

/// Hierarchical-layout variant of [`init_real_mem_repo_from_disk`].
///
/// `mems` is `&[(mem_dir, leaf, full_path)]`. The branch ref is
/// sealed at `refs/heads/<full_path>` and the `__SYSTEM` config blob
/// lands at `<full_path>/config.json`, but the engine still references
/// the mem by its `leaf` name (matches `MemInit.name`). Pass
/// `full_path = leaf` to get flat-layout behaviour identical to
/// [`init_real_mem_repo_from_disk`].
///
/// Use this from tests that need to exercise the production-shape
/// hierarchical workspace (e.g. `memstead/engine` branch with leaf
/// `engine`). Without a hierarchical fixture the per-mem drift
/// detection helper that maps leaf → branch ref looks identical to
/// the flat case and will not catch ref-format regressions.
pub fn init_real_mem_repo_from_disk_with_paths(
    workspace_root: &Path,
    mems: &[(&Path, &str, &str)],
) -> PathBuf {
    static INIT_LOCK: Mutex<()> = Mutex::new(());

    let gitdir = workspace_root.join("mem-repo").join(".git");
    std::fs::create_dir_all(&gitdir).unwrap_or_else(|e| {
        panic!(
            "failed to create test mem-repo at {}: {e}",
            gitdir.display()
        )
    });

    let _guard = INIT_LOCK.lock().unwrap_or_else(|e| e.into_inner());

    let repo = match gix::open(&gitdir) {
        Ok(r) => r,
        Err(_) => gix::init_bare(&gitdir).unwrap_or_else(|e| {
            panic!(
                "failed to initialise bare mem-repo-git at {}: {e}",
                gitdir.display()
            )
        }),
    };

    if matches!(repo.try_find_reference("refs/heads/__SYSTEM"), Ok(Some(_))) {
        return workspace_root.to_path_buf();
    }

    let actor = gix::actor::Signature {
        name: "test".into(),
        email: "test@example.com".into(),
        time: gix::date::Time {
            seconds: 0,
            offset: 0,
        },
    };

    // main: empty (operator-facing docs branch — content not used by engine).
    {
        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        repo.commit_as(
            actor_ref,
            actor_ref,
            "refs/heads/main",
            "seed main",
            repo.empty_tree().id().detach(),
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();
    }

    // __SYSTEM: read each mem_dir's `.memstead/config.json` (or synthesise
    // a default) and seed it as `<full_path>/config.json` so the
    // resolver maps `<leaf>` → `<full_path>` correctly.
    let mut system_editor = repo.empty_tree().edit().unwrap();
    for (mem_dir, _leaf, full_path) in mems {
        let cfg_path = mem_dir.join(".memstead").join("config.json");
        let cfg_bytes = match std::fs::read(&cfg_path) {
            Ok(b) => b,
            Err(_) => r#"{"schema": "default@1.0.0"}"#.to_string().into_bytes(),
        };
        let blob = repo.write_blob(&cfg_bytes).unwrap().detach();
        system_editor
            .upsert(
                format!("{full_path}/config.json"),
                gix::objs::tree::EntryKind::Blob,
                blob,
            )
            .unwrap();
    }
    let system_tree = system_editor.write().unwrap().detach();
    let mut buf = gix::date::parse::TimeBuf::default();
    let actor_ref = actor.to_ref(&mut buf);
    repo.commit_as(
        actor_ref,
        actor_ref,
        "refs/heads/__SYSTEM",
        "seed __SYSTEM",
        system_tree,
        Vec::<gix::ObjectId>::new(),
    )
    .unwrap();

    // Per-mem branches: walk each mem_dir for `*.md` files and
    // upsert them into the branch tree at their relative path. Branch
    // ref is `refs/heads/<full_path>` (flat == hierarchical when
    // `full_path == leaf`).
    let empty_tree = repo.empty_tree().id().detach();
    for (mem_dir, _leaf, full_path) in mems {
        let mut entities: Vec<(String, Vec<u8>)> = Vec::new();
        if mem_dir.is_dir() {
            collect_md_entities(mem_dir, mem_dir, &mut entities);
        }
        let tree_id = if entities.is_empty() {
            empty_tree
        } else {
            let mut editor = repo.empty_tree().edit().unwrap();
            for (rel, bytes) in &entities {
                let blob = repo.write_blob(bytes).unwrap().detach();
                editor
                    .upsert(rel.clone(), gix::objs::tree::EntryKind::Blob, blob)
                    .unwrap();
            }
            editor.write().unwrap().detach()
        };

        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        let ref_name = format!("refs/heads/{full_path}");
        repo.commit_as(
            actor_ref,
            actor_ref,
            ref_name.as_str(),
            format!("seed {full_path}"),
            tree_id,
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();
    }

    // Project the just-written __SCHEMAS + __SYSTEM content onto the
    // unified `__MEMSTEAD` ref. The migrator is idempotent — re-runs are
    // no-ops — and is the canonical projection logic, so test
    // fixtures stay in lockstep with what `engine_from_workspace_root`
    // produces at boot (s136). Without this, tests that bypass the
    // boot path (e.g., direct `Engine::from_mounts` calls) would have
    // `__MEMSTEAD` missing and the schema reader's legacy `__SCHEMAS`
    // fallback (retired in s139) would start failing. Per-fixture
    // failures collapse via `unwrap()` — fixture setup is hard-fail.
    crate::storage_memstead::migrate_to_memstead_ref(&gitdir).unwrap();

    // For each mem, lift the schema pin from `<mem_dir>/.memstead/config.json`
    // (the same source the just-written `__SYSTEM` blob used). When the
    // file is missing or the field is absent we fall back to the
    // sentinel "default" pin — matches the pre-deletion `LegacyProStore`
    // behaviour and keeps the test surface stable.
    let owned: Vec<(String, String, String)> = mems
        .iter()
        .map(|(mem_dir, leaf, full)| {
            let pin = std::fs::read_to_string(mem_dir.join(".memstead").join("config.json"))
                .ok()
                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
                .and_then(|v| {
                    v.get("schema")
                        .and_then(|s| s.as_str())
                        .map(|s| s.to_string())
                })
                .unwrap_or_else(|| "default@1.0.0".to_string());
            ((*leaf).to_string(), (*full).to_string(), pin)
        })
        .collect();
    let refs: Vec<(&str, &str, &str)> = owned
        .iter()
        .map(|(a, b, c)| (a.as_str(), b.as_str(), c.as_str()))
        .collect();
    seed_new_layout(workspace_root, &gitdir, &refs);

    workspace_root.to_path_buf()
}

fn collect_md_entities(root: &Path, current: &Path, out: &mut Vec<(String, Vec<u8>)>) {
    let Ok(rd) = std::fs::read_dir(current) else {
        return;
    };
    for entry in rd.flatten() {
        let path = entry.path();
        // Skip the `.memstead/` and any `.git/` subtrees inside the mem.
        let name = path.file_name().and_then(|s| s.to_str()).unwrap_or("");
        if name.starts_with('.') {
            continue;
        }
        if path.is_dir() {
            collect_md_entities(root, &path, out);
        } else if path.extension().and_then(|s| s.to_str()) == Some("md")
            && let Ok(rel) = path.strip_prefix(root)
            && let Ok(bytes) = std::fs::read(&path)
        {
            out.push((rel.to_string_lossy().into_owned(), bytes));
        }
    }
}

/// `(name, schema, [(path, content)])` seed tuple consumed by
/// [`init_real_mem_repo_with_entities`].
pub type MemSeed<'a> = (&'a str, &'a str, &'a [(&'a str, &'a str)]);

/// Initialise `<workspace_root>/mem-repo/.git/` as a real bare repo
/// like [`init_real_mem_repo`], but additionally writes the supplied
/// entity blobs into each mem's branch tree. `mems_with_entities`
/// is `&[(name, schema, &[(path, content)])]` — the path/content tuples
/// land at the corresponding paths on the per-mem branch's tree.
///
/// Use this when the test asserts on entity content surfaced through
/// the dispatcher's GitTree read path. For the simpler "engine boots,
/// no entities" case use [`init_real_mem_repo`].
pub fn init_real_mem_repo_with_entities(
    workspace_root: &Path,
    mems_with_entities: &[MemSeed<'_>],
) -> PathBuf {
    static INIT_LOCK: Mutex<()> = Mutex::new(());

    let gitdir = workspace_root.join("mem-repo").join(".git");
    std::fs::create_dir_all(&gitdir).unwrap_or_else(|e| {
        panic!(
            "failed to create test mem-repo at {}: {e}",
            gitdir.display()
        )
    });

    let _guard = INIT_LOCK.lock().unwrap_or_else(|e| e.into_inner());

    let repo = match gix::open(&gitdir) {
        Ok(r) => r,
        Err(_) => gix::init_bare(&gitdir).unwrap_or_else(|e| {
            panic!(
                "failed to initialise bare mem-repo-git at {}: {e}",
                gitdir.display()
            )
        }),
    };

    if matches!(repo.try_find_reference("refs/heads/__SYSTEM"), Ok(Some(_))) {
        return workspace_root.to_path_buf();
    }

    let actor = gix::actor::Signature {
        name: "test".into(),
        email: "test@example.com".into(),
        time: gix::date::Time {
            seconds: 0,
            offset: 0,
        },
    };

    // main: empty (operator-facing docs branch).
    {
        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        repo.commit_as(
            actor_ref,
            actor_ref,
            "refs/heads/main",
            "seed main",
            repo.empty_tree().id().detach(),
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();
    }

    // __SYSTEM: per-mem `<name>/config.json` blobs.
    let mut system_editor = repo.empty_tree().edit().unwrap();
    for (name, schema, _) in mems_with_entities {
        let config = format!(r#"{{"schema": "{schema}"}}"#);
        let blob = repo.write_blob(config.as_bytes()).unwrap().detach();
        system_editor
            .upsert(
                format!("{name}/config.json"),
                gix::objs::tree::EntryKind::Blob,
                blob,
            )
            .unwrap();
    }
    let system_tree = system_editor.write().unwrap().detach();
    let mut buf = gix::date::parse::TimeBuf::default();
    let actor_ref = actor.to_ref(&mut buf);
    repo.commit_as(
        actor_ref,
        actor_ref,
        "refs/heads/__SYSTEM",
        "seed __SYSTEM",
        system_tree,
        Vec::<gix::ObjectId>::new(),
    )
    .unwrap();

    let empty_tree = repo.empty_tree().id().detach();
    for (name, _, entities) in mems_with_entities {
        let tree_id = if entities.is_empty() {
            empty_tree
        } else {
            let mut editor = repo.empty_tree().edit().unwrap();
            for (path, content) in *entities {
                let blob = repo.write_blob(content.as_bytes()).unwrap().detach();
                editor
                    .upsert((*path).to_string(), gix::objs::tree::EntryKind::Blob, blob)
                    .unwrap();
            }
            editor.write().unwrap().detach()
        };

        let mut buf = gix::date::parse::TimeBuf::default();
        let actor_ref = actor.to_ref(&mut buf);
        let ref_name = format!("refs/heads/{name}");
        repo.commit_as(
            actor_ref,
            actor_ref,
            ref_name.as_str(),
            format!("seed {name}"),
            tree_id,
            Vec::<gix::ObjectId>::new(),
        )
        .unwrap();
    }

    // Project __SYSTEM onto __MEMSTEAD so post-s140 reads land.
    crate::storage_memstead::migrate_to_memstead_ref(&gitdir).unwrap();

    seed_new_layout(
        workspace_root,
        &gitdir,
        &mems_with_entities
            .iter()
            .map(|(name, schema, _)| (*name, *name, *schema))
            .collect::<Vec<_>>(),
    );

    workspace_root.to_path_buf()
}