memstead-base 0.24.0

Engine internals for Memstead — store, parser, validators, mem lifecycle, workspace policy. Published as a dependency of the memstead-cli and memstead-mcp products, not as a supported library — pre-1.0, 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
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
//! `memstead mem fork`: a new git-branch mem whose branch starts at a
//! source mem's commit (a recorded ancestor), whose config is the
//! source's with the origin written into it, and which carries the
//! source's outgoing cross-link grants so its copied edges read
//! conformant from the first read.
//!
//! Two forms, one pipeline. **Local**: the source is a mounted
//! git-branch mem of the workspace's mem-repo; the fork's branch is a
//! ref created at the source's tip (or at a given sha the source branch
//! reaches). **Remote** (`remote: Some`): the source branch and the
//! remote's `__MEMSTEAD` ref are fetched into remote-tracking refs, the
//! source's config is read from that tracking ref (the local
//! `__MEMSTEAD` is never moved), the fetched tree is validated against
//! the source's schema pin exactly as `pull` validates before it moves a
//! pointer, and the fork's branch is created at the fetched commit.
//! **Adopt** (`remote: Some`, `adopt: true`): the fork branch already
//! exists on the remote, made elsewhere from a mem this workspace
//! mounts (by the engine on another machine and pushed, or by a server
//! that accepts proposal branches); the branch of the fork's own name
//! is fetched, its ancestor is the nearest commit it shares with the
//! local source branch, the fetched tree is validated against the local
//! source's pin, the fork's config is derived from the local source
//! (the remote needs no `__MEMSTEAD`), and the branch is created at the
//! fetched tip with no commit of its own: an adopted tree is the
//! proposer's, and the fork writes nothing on it. Its `base` is the
//! engine's fork commit when the branch carries one above the ancestor,
//! absent otherwise (the brief then reads the fork against its
//! ancestor, as for a fork made before fork commits existed).
//!
//! The fork is an engine act end to end: the branch through the
//! engine's git layer ([`crate::GitBranchOps`]), the fork commit through
//! the fork's own backend, the config through the backend's config
//! writer on the `__MEMSTEAD` ref, the grants through the workspace
//! policy writer the grant verb uses, the mount through
//! [`crate::Engine::register_writable_mem`]. Every refusal is typed and
//! lands nothing; a failure after the branch exists rolls the branch
//! (the fork commit with it) and the config back through the residue
//! prune, and the policy line through the deletion scrub (both
//! idempotent).
//!
//! **The fork commit.** A copied tree still names the source: the
//! anchors sidecar keys its rows by `<source>--<slug>`, the derivations
//! sidecar keys its baselines the same way, and a body link the source
//! qualified with its own name (`[[<source>--slug]]`, `[[<source>:slug]]`)
//! would parse in the fork as a cross-mem link into the source. So the
//! fork's first act on its branch, before the mount exists, is one
//! commit that moves every such id and link from the source's name to
//! its own ([`retarget_fork_tree`]): the rows keep their hashes, spans
//! and observations, a link naming another mem stays, a code span is
//! never touched, and a tree with nothing to move gets the commit all
//! the same (an empty retarget is still the base). Its sha is recorded
//! as `forkedFrom.base`; the ancestor on the source stays `sha`. The
//! workspace check ledger is not consulted: a fork starts unchecked,
//! because a fork's entity is not the same entity as its source's.

use std::path::Path;

use memstead_schema::workspace_config::CrossLinkValue;

use crate::FullEngineError;
use crate::backend::BackendError;
use crate::engine::GitBranchOps;
use crate::ops::WarningHint;
use crate::vcs::CommitContext;
use crate::workspace_config_edit::{
    CrossLinkTarget, grant_cross_link, scrub_policy_for_deleted_mem,
};

use super::lifecycle::{
    NOTE_MAX_LEN, ResidueProbe, admit_by_create_rules, classify_invalid_mem_name,
    residue_probe_for_workspace, sibling_name_suggestion,
};

/// Parameters for [`fork_mem`]. Mirrors `memstead mem fork
/// <source>[@<sha>] <new-name> [--remote <name>]`.
#[derive(Debug, Clone)]
pub struct MemForkParams {
    /// The source mem's name. Local form: a mounted git-branch mem of
    /// the workspace's mem-repo. Remote form: the name of the branch
    /// (`refs/heads/<source>`) and of the config blob
    /// (`mems/<source>/config.json`) on the remote.
    pub source: String,
    /// The commit the fork starts at; `None` means the source branch's
    /// tip. Anything `git rev-parse` resolves (a full or abbreviated
    /// sha), and it must be reachable from the source branch.
    pub sha: Option<String>,
    /// The new mem's name: the full hierarchical identifier, under the
    /// same grammar and create rules as any created mem.
    pub name: String,
    /// The mem-repo remote to fetch the source from; `None` is the
    /// local form.
    pub remote: Option<String>,
    /// Adopt form: the branch `refs/heads/<name>` already exists on
    /// `remote` as a fork of the locally mounted `source`; fetch it and
    /// mount it as that fork. Requires `remote`; excludes `sha`.
    pub adopt: bool,
    /// Agent-authored provenance note (≤[`NOTE_MAX_LEN`] chars).
    pub note: Option<String>,
    /// Operator posture: skips the `[[mem_management.create]]`
    /// allowlist and its schema gate, as `create_mem` does. Never a
    /// wire-shape input from agents.
    pub operator_mode: bool,
    /// Caller category for the commits' provenance trailers.
    pub actor: crate::vcs::Actor,
    /// Client identity paired with `actor`.
    pub client: Option<crate::vcs::ClientId>,
}

/// Response of [`fork_mem`]: what was created, where it came from,
/// and what it inherited.
#[derive(Debug, Clone)]
pub struct MemForkResponse {
    /// The new mem's name.
    pub name: String,
    /// The origin as written into the fork's config: source mem, the
    /// 40-hex sha the branch starts at, the remote when one was used,
    /// and the fork commit's sha as `base`.
    pub forked_from: memstead_schema::ForkedFrom,
    /// The schema pin the fork carries: the source's, copied.
    pub schema_ref: memstead_schema::SchemaRef,
    /// The fork's branch, `refs/heads/<name>`.
    pub branch_ref: String,
    /// The source's outgoing cross-link grants the fork now carries
    /// under its own name (local form; a source with no grants, and
    /// every remote fork, inherits none).
    pub inherited_grants: Option<CrossLinkValue>,
    /// Non-fatal findings (`NOTE_MISSING` under `require_notes`).
    pub warnings: Vec<WarningHint>,
}

/// Create a mem as a fork of another git-branch mem at a recorded
/// ancestor. See the module documentation for the two forms and the
/// atomicity contract.
///
/// Refusals, all before the first write: `INVALID_MEM_NAME`,
/// `INVALID_INPUT` (no mem-repo workspace; a folder, archive or
/// in-memory source; a source config without a schema), `UNKNOWN_MEM`
/// (local source not mounted), `UNKNOWN_REMOTE`, `UNKNOWN_REF` (a
/// source branch the remote lacks, a sha that does not resolve or is
/// not on the source branch), `SCHEMA_NOT_FOUND` (the source's pin
/// does not resolve in this workspace; the message names `memstead
/// schema install`), `MEM_PATH_NOT_ALLOWED`, `MEM_SCHEMA_NOT_ALLOWED`,
/// `MEM_NAME_COLLISION`, `MEM_NAME_REF_CONFLICT`,
/// `MEM_STORAGE_RESIDUE_DETECTED`, and for the remote and adopt forms
/// `SCHEMA_VIOLATION_IN_FETCH` over the fetched tree. The adopt form
/// also refuses `INVALID_INPUT` (no remote, or a `sha` beside it) and
/// `UNKNOWN_REF` (the remote lacks the fork branch, or the branch
/// shares no history with the local source).
pub fn fork_mem(
    engine: &mut crate::Engine,
    params: MemForkParams,
) -> Result<MemForkResponse, FullEngineError> {
    // ---- Step 0: input validation ----
    if let Some(note) = params.note.as_deref()
        && note.chars().count() > NOTE_MAX_LEN
    {
        return Err(crate::EngineError::InvalidInput(format!(
            "note exceeds {NOTE_MAX_LEN} characters"
        ))
        .into());
    }
    if let Some(reason) = classify_invalid_mem_name(&params.name) {
        return Err(FullEngineError::InvalidMemName {
            name: params.name.clone(),
            reason,
        });
    }
    if crate::entity::id::validate_mem_name_grammar(&params.name).is_err() {
        return Err(FullEngineError::InvalidMemName {
            name: params.name.clone(),
            reason: "invalid_char",
        });
    }
    // The source name composes a ref and a tree path in both forms:
    // it obeys the mem-name grammar too, and a bad one is the caller's
    // input, never a git error.
    if crate::entity::id::validate_mem_name_grammar(&params.source).is_err() {
        return Err(crate::EngineError::InvalidInput(format!(
            "source {:?} is not a mem name (grammar `[a-z0-9-]+(/[a-z0-9-]+)*`)",
            params.source
        ))
        .into());
    }
    if params.source == params.name {
        return Err(crate::EngineError::InvalidInput(format!(
            "a fork needs its own name: {:?} is the source",
            params.name
        ))
        .into());
    }
    if params.adopt {
        if params.remote.is_none() {
            return Err(crate::EngineError::InvalidInput(
                "adopt names a fork branch on a remote: pass the remote it lives on".to_string(),
            )
            .into());
        }
        if params.sha.is_some() {
            return Err(crate::EngineError::InvalidInput(format!(
                "an adopted fork starts at the remote branch's tip: `{}@<sha>` and adopt exclude \
                 each other",
                params.source
            ))
            .into());
        }
    }

    // ---- Step 0b: workspace shape ----
    // A fork is a branch at an ancestor: it needs the mem-repo gitdir
    // and the git layer. A folder-only workspace has neither.
    let root = engine
        .workspace_root()
        .map(Path::to_path_buf)
        .ok_or_else(|| {
            crate::EngineError::InvalidInput(
                "mem fork requires a workspace root to locate mem-repo/.git/".to_string(),
            )
        })?;
    let gitdir = root.join("mem-repo").join(".git");
    if !gitdir.is_dir() {
        return Err(crate::EngineError::InvalidInput(format!(
            "mem fork requires a mem-repo workspace ({} not found): a fork is a branch at \
             an ancestor, and a folder-only workspace has no branch to fork",
            gitdir.display()
        ))
        .into());
    }
    let gitdir = gitdir.canonicalize().unwrap_or(gitdir);
    let ops = engine.git_branch_ops().ok_or_else(|| {
        crate::EngineError::InvalidInput(
            "mem fork requires the git-branch ops bundle (full boot only)".to_string(),
        )
    })?;
    let ctx = engine.commit_context(
        Some("memstead_mem_fork"),
        params.actor,
        params.client.clone(),
        params.note.clone(),
    );

    // ---- Step 1: the source's config bytes and the ref the fork starts from ----
    // Local and remote forms: the ref is the source's tip. Adopt: the
    // config is the local source's, the ref is the fetched fork branch,
    // and the local source's ref rides along for the ancestor.
    let (config_bytes, tip_ref, adopted_source_ref) = match params.remote.as_deref() {
        None => {
            let (bytes, tip_ref) = resolve_local_source(engine, &gitdir, &params.source)?;
            (bytes, tip_ref, None)
        }
        Some(remote) if params.adopt => {
            let (bytes, source_ref) = resolve_local_source(engine, &gitdir, &params.source)?;
            let tracking = fetch_adopted_fork(&ops, &gitdir, remote, &params.source, &params.name)?;
            (bytes, tracking, Some(source_ref))
        }
        Some(remote) => {
            let (bytes, tip_ref) = resolve_remote_source(&ops, &gitdir, remote, &params.source)?;
            (bytes, tip_ref, None)
        }
    };
    let source_value: serde_json::Value = serde_json::from_slice(&config_bytes).map_err(|e| {
        crate::EngineError::InvalidInput(format!(
            "source {:?}: its config is not JSON: {e}",
            params.source
        ))
    })?;
    let source_config = memstead_schema::parse_mem_config(&source_value).map_err(|e| {
        crate::EngineError::InvalidInput(format!(
            "source {:?}: its config does not parse: {e}",
            params.source
        ))
    })?;

    // ---- Step 2: the commit the fork starts at ----
    let tip = (ops.resolve_ref)(&gitdir, &tip_ref)
        .map_err(crate::EngineError::Backend)?
        .ok_or_else(|| crate::EngineError::UnknownRef(tip_ref.clone()))?;
    let sha = match params.sha.as_deref() {
        None => tip.clone(),
        Some(raw) => {
            // `^{commit}`: the object must exist and peel to a commit.
            // A bare `rev-parse --verify` accepts any 40-hex string
            // whether or not the store holds it, and the ancestry
            // check would then fail raw.
            let full = (ops.resolve_ref)(&gitdir, &format!("{raw}^{{commit}}"))
                .map_err(crate::EngineError::Backend)?
                .ok_or_else(|| {
                    crate::EngineError::UnknownRef(format!(
                        "{raw} is not a commit in the mem-repo (source branch {tip_ref})"
                    ))
                })?;
            let on_branch = (ops.is_ancestor)(&gitdir, &full, &tip).map_err(|e| {
                crate::EngineError::UnknownRef(format!(
                    "{raw} could not be placed against {tip_ref}: {e}"
                ))
            })?;
            if !on_branch {
                return Err(crate::EngineError::UnknownRef(format!(
                    "{raw} is not on {tip_ref} (tip {tip}): a fork starts at a commit the \
                     source branch reaches"
                ))
                .into());
            }
            full
        }
    };
    // The ancestor on the source: the commit the fork starts at in the
    // local and remote forms; for an adopted branch, the nearest commit
    // it shares with the local source branch.
    let ancestor = match &adopted_source_ref {
        None => sha.clone(),
        Some(source_ref) => {
            let source_tip = (ops.resolve_ref)(&gitdir, source_ref)
                .map_err(crate::EngineError::Backend)?
                .ok_or_else(|| crate::EngineError::UnknownRef(source_ref.clone()))?;
            (ops.merge_base)(&gitdir, &sha, &source_tip)
                .map_err(crate::EngineError::Backend)?
                .ok_or_else(|| {
                    crate::EngineError::UnknownRef(format!(
                        "remote branch {} at {sha} shares no history with the local source {:?} \
                         at {source_tip}: it is not a fork of this mem",
                        tip_ref, params.source
                    ))
                })?
        }
    };

    // ---- Step 3: the schema pin, copied and resolved, never re-pinned ----
    let pin = source_config.schema.clone().ok_or_else(|| {
        crate::EngineError::InvalidInput(format!(
            "source {:?}: its config declares no schema pin",
            params.source
        ))
    })?;
    let mut catalogue: Vec<std::sync::Arc<memstead_schema::Schema>> =
        engine.workspace_schemas().to_vec();
    catalogue.extend_from_slice(engine.builtin_schemas());
    let resolved_schema = crate::engine::SchemaResolver::new(&catalogue)
        .resolve(&pin)
        .map_err(|sources| {
            // The pin is copied and never re-pinned, so the remedy is
            // always the install, never `mem set-schema` (which would
            // name a mem that does not exist yet): a package of the
            // name in the tree when there is one, the placeholder
            // otherwise.
            let hint = crate::engine::error::probe_authoring_package(&root, &pin.name)
                .unwrap_or_else(|| {
                    crate::engine::error::SCHEMA_INSTALL_PACKAGE_UNKNOWN.to_string()
                });
            crate::EngineError::SchemaNotFound {
                mem: params.name.clone(),
                pin: pin.to_string(),
                sources,
                install_hint: Some(hint),
            }
        })?;
    let canonical_schema_ref = memstead_schema::SchemaRef::new(
        resolved_schema.manifest.name.clone(),
        resolved_schema.version.clone(),
    );

    // ---- Step 4: the create rules, as for any created mem ----
    let canonical_location = root.join(&params.name);
    if !params.operator_mode {
        admit_by_create_rules(
            engine,
            &params.name,
            &canonical_location,
            &canonical_schema_ref,
            &catalogue,
        )?;
    }

    // ---- Step 5: collisions, all before any write ----
    if let Some(existing) = engine.mem_router().origin_for_mem(&params.name) {
        return Err(crate::EngineError::MemNameCollision {
            name: params.name,
            source_origin: existing.render_source(),
        }
        .into());
    }
    if engine
        .mem_router()
        .archive_path_for_mem(&params.name)
        .is_some()
    {
        return Err(crate::EngineError::MemNameCollision {
            name: params.name,
            source_origin: "attached read mem".to_string(),
        }
        .into());
    }
    let conflicting = (ops.branch_namespace_conflicts)(&gitdir, &params.name)
        .map_err(|e| crate::EngineError::Mem(format!("ref namespace probe: {e}")))?;
    if !conflicting.is_empty() {
        let suggestion = sibling_name_suggestion(&params.name, &conflicting);
        return Err(FullEngineError::MemNameRefConflict {
            branch_ref: format!("refs/heads/{}", params.name),
            name: params.name,
            conflicting_branches: conflicting,
            suggestion,
        });
    }
    // Residue at the name (an unregistered mem's branch and config, a
    // crashed create) refuses outright: a fork offers no reattach and
    // no overwrite, because the branch it would create is the lineage
    // claim and must not adopt or destroy another one.
    if let ResidueProbe::Present {
        branch_ref,
        config_blob,
        ..
    } = residue_probe_for_workspace(
        engine,
        Some(&root),
        &params.name,
        &params.name,
        &canonical_schema_ref,
    ) {
        return Err(FullEngineError::MemStorageResidueDetected {
            branch_ref,
            config_blob,
            entity_count: 0,
        });
    }

    // ---- Step 6: remote form: the fetched tree passes the schema before anything lands ----
    // The same check `pull` runs before it moves a pointer, against the
    // schema the source's pin resolved to. The local form skips it:
    // the source's tree is already this engine's validated state.
    if params.remote.is_some() {
        crate::Engine::validate_ref_with_schema(
            &ops,
            &gitdir,
            &params.name,
            &sha,
            resolved_schema.as_ref(),
        )?;
    }

    // ---- Step 7: the fork's config, short of its origin ----
    // The source's config with the source's own cursors left behind:
    // sync state and the review mark are the source's maintenance
    // bookkeeping, the tombstone is not the fork's, and the name is
    // path-derived. The origin is written in once the fork commit
    // exists, because it records that commit's sha.
    let mut config = source_config;
    config.name = None;
    config.sync_state.clear();
    config.review_mark = None;
    config.unregistered_at = None;

    // ---- Step 8: the writes, each rolled back on the next one's failure ----
    let branch_ref = format!("refs/heads/{}", params.name);
    (ops.create_branch_at)(&gitdir, &params.name, &sha)
        .map_err(|e| crate::EngineError::Mem(format!("fork branch: {e}")))?;
    let mount = crate::workspace::Mount {
        mem: params.name.clone(),
        schema: Some(canonical_schema_ref.clone()),
        storage: crate::workspace::MountStorage::GitBranch {
            gitdir: gitdir.clone(),
            branch: branch_ref.clone(),
        },
        capability: crate::workspace::MountCapability::Write,
        lifecycle: crate::workspace::MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let factory = engine.backend_factory();
    let backend = match factory(&mount) {
        Ok(b) => b,
        Err(e) => {
            roll_back(
                &ops,
                &gitdir,
                &root,
                &params.name,
                &ctx,
                "backend instantiate",
            );
            return Err(crate::EngineError::Mem(format!("instantiate backend: {e}")).into());
        }
    };
    // The fork commit: sidecar ids and self-links move to the fork's
    // name in one commit on the fork's branch, right above the
    // ancestor. The branch rollback covers it (the prune drops the ref,
    // and the commit with it). An adopted branch gets none: its tree is
    // the proposer's and the fork writes nothing on it; its base is the
    // fork commit the branch already carries, when it carries one.
    let base = if params.adopt {
        match adopted_fork_base(
            &ops,
            &gitdir,
            &branch_ref,
            &params.name,
            &params.source,
            &ancestor,
        ) {
            Ok(base) => base,
            Err(e) => {
                roll_back(
                    &ops,
                    &gitdir,
                    &root,
                    &params.name,
                    &ctx,
                    "fork commit lookup",
                );
                return Err(e.into());
            }
        }
    } else {
        match retarget_fork_tree(backend.as_ref(), &params.source, &params.name, &sha, &ctx) {
            Ok(base) => Some(base),
            Err(e) => {
                roll_back(&ops, &gitdir, &root, &params.name, &ctx, "fork commit");
                return Err(e.into());
            }
        }
    };
    let forked_from = memstead_schema::ForkedFrom {
        mem: params.source.clone(),
        sha: ancestor.clone(),
        remote: params.remote.clone(),
        base,
    };
    config.forked_from = Some(forked_from.clone());
    let config_bytes = match serde_json::to_vec_pretty(&config) {
        Ok(mut bytes) => {
            bytes.push(b'\n');
            bytes
        }
        Err(e) => {
            roll_back(&ops, &gitdir, &root, &params.name, &ctx, "config serialize");
            return Err(crate::EngineError::InvalidInput(format!(
                "could not serialize the fork's config: {e}"
            ))
            .into());
        }
    };
    if let Err(e) = backend.write_mem_config(&config_bytes, &ctx) {
        roll_back(&ops, &gitdir, &root, &params.name, &ctx, "config write");
        return Err(crate::EngineError::Mem(format!("write mem config: {e}")).into());
    }

    // The source's outgoing grants, under the fork's name, through the
    // policy writer the grant verb uses. Local form only: a remote's
    // policy is not this workspace's, and the fork gets what any
    // created mem gets there (the create rule's `default_cross_links`,
    // synthesised at link time).
    let inherited_grants: Option<CrossLinkValue> = if params.remote.is_none() {
        match engine.settings().cross_mem_links.get(&params.source) {
            Some(CrossLinkValue::List(targets)) if targets.is_empty() => None,
            other => other.cloned(),
        }
    } else {
        None
    };
    if let Some(value) = &inherited_grants
        && let Err(e) = write_inherited_grants(engine, &root, &params.name, value)
    {
        roll_back(
            &ops,
            &gitdir,
            &root,
            &params.name,
            &ctx,
            "grant inheritance",
        );
        return Err(crate::EngineError::Mem(format!("inherit cross-link grants: {e}")).into());
    }

    let origin = crate::MemOrigin::RuntimeCreated {
        at: std::time::SystemTime::now(),
        by_tool: "memstead_mem_fork",
    };
    if let Err(e) = engine.register_writable_mem(mount, backend, origin) {
        roll_back(
            &ops,
            &gitdir,
            &root,
            &params.name,
            &ctx,
            "mount registration",
        );
        return Err(e.into());
    }
    if let Some(value) = &inherited_grants {
        // The writer changed the file; the running engine's policy
        // follows it, so the fork's edges read conformant in this
        // process too, not only after the next boot.
        let mut settings = engine.settings().clone();
        settings
            .cross_mem_links
            .insert(params.name.clone(), value.clone());
        engine.set_settings(settings);
    }
    engine.persist_state()?;

    let warnings: Vec<WarningHint> = engine
        .note_missing_warning("fork_mem", params.note.as_deref())
        .into_iter()
        .collect();
    Ok(MemForkResponse {
        name: params.name,
        forked_from,
        schema_ref: canonical_schema_ref,
        branch_ref,
        inherited_grants,
        warnings,
    })
}

/// The fork commit: move every id and link that names `source` to
/// `fork`, in the anchors sidecar (entity keys, an entity-grain row's
/// `artifact`, every `derived_from` entry naming the source mem), the
/// derivations sidecar (source keys and same-mem targets) and the entity bodies
/// (mem-qualified self-links, through the export retargeting rule:
/// `[[<source>--slug]]` and `[[<source>:slug]]`, labels kept, code
/// spans and links naming another mem untouched), then commit once on
/// the fork's branch with the caller's provenance. Rows keep their
/// hashes, spans and observations; a body with nothing to move keeps
/// its bytes. The commit is made even when nothing moved, so every
/// fork has a base of its own above the ancestor. Returns the commit's
/// sha.
fn retarget_fork_tree(
    backend: &dyn crate::backend::MemBackend,
    source: &str,
    fork: &str,
    ancestor: &str,
    ctx: &CommitContext<'_>,
) -> Result<String, crate::EngineError> {
    // The anchors sidecar: entity keys.
    if let Some(bytes) = backend.read_anchors_sidecar()? {
        let mut sidecar = crate::anchor::AnchorSidecar::from_bytes(&bytes).map_err(|e| {
            crate::EngineError::Mem(format!(
                "fork {fork:?}: the source's anchors sidecar does not parse: {e}"
            ))
        })?;
        let mut moved = false;
        // A row that names a same-mem entity as its artifact (entity
        // grain) or among its inputs would otherwise resolve against
        // the source's entity from the fork, and read unobserved where
        // the source is not mounted.
        for row in sidecar.entities.values_mut().flatten() {
            if row.grain == crate::anchor::AnchorGrain::Entity
                && let Some(to) = retarget_entity_id(&row.artifact, source, fork)
            {
                row.artifact = to;
                moved = true;
            }
            for input in &mut row.derived_from {
                if let Some(to) = retarget_entity_id(input, source, fork) {
                    *input = to;
                    moved = true;
                }
            }
        }
        let keys: Vec<String> = sidecar.entities.keys().cloned().collect();
        for key in keys {
            if let Some(to) = retarget_entity_id(&key, source, fork) {
                sidecar.rename(&key, &to);
                moved = true;
            }
        }
        if moved {
            backend.write_anchors_sidecar(&sidecar.to_bytes())?;
        }
    }

    // The derivations sidecar: source keys and same-mem targets.
    let derivations_path = Path::new(crate::derivation::DERIVATION_SIDECAR_PATH);
    if let Some(bytes) = backend.read_entity(derivations_path)? {
        let sidecar = crate::derivation::DerivationSidecar::from_bytes(&bytes).map_err(|e| {
            crate::EngineError::Mem(format!(
                "fork {fork:?}: the source's derivations sidecar does not parse: {e}"
            ))
        })?;
        let mut moved = false;
        let mut retargeted = crate::derivation::DerivationSidecar {
            version: sidecar.version,
            baselines: Default::default(),
        };
        for (key, baselines) in sidecar.baselines {
            let key = match retarget_entity_id(&key, source, fork) {
                Some(to) => {
                    moved = true;
                    to
                }
                None => key,
            };
            let baselines = baselines
                .into_iter()
                .map(|mut b| {
                    if let Some(to) = retarget_entity_id(&b.target, source, fork) {
                        b.target = to;
                        moved = true;
                    }
                    b
                })
                .collect();
            retargeted.baselines.insert(key, baselines);
        }
        if moved {
            backend.write_entity(derivations_path, &retargeted.to_bytes())?;
        }
    }

    // The entity bodies: mem-qualified self-links.
    for rel_path in backend.list_entities()? {
        let Some(bytes) = backend.read_entity(&rel_path)? else {
            continue;
        };
        let retargeted = crate::ops::export::retarget_mem_links(bytes.clone(), source, fork);
        if retargeted != bytes {
            backend.write_entity(&rel_path, &retargeted)?;
        }
    }

    // The source's proposal record is the target branch's: a fork
    // carries none (the brief reads the target's; a merge writes the
    // target's). Dropping an absent member is a no-op.
    backend.delete_entity(std::path::Path::new(
        crate::ops::proposal::PROPOSAL_RECORD_PATH,
    ))?;

    let short = &ancestor[..ancestor.len().min(12)];
    let subject = format!("memstead: fork mem {fork} from {source}@{short}");
    Ok(backend.commit(&subject, ctx)?)
}

/// `<source>--<slug>` as `<fork>--<slug>`; `None` for an id qualified
/// by any other mem (a cross-mem target, a foreign row), which the fork
/// leaves as it found it.
fn retarget_entity_id(id: &str, source: &str, fork: &str) -> Option<String> {
    let slug = id.strip_prefix(source)?.strip_prefix("--")?;
    (!slug.is_empty()).then(|| format!("{fork}--{slug}"))
}

/// Local form: the source is a mounted git-branch mem of this
/// mem-repo. Returns its config bytes (read through its own backend,
/// the freshest state) and the ref its tip is read from.
fn resolve_local_source(
    engine: &crate::Engine,
    gitdir: &Path,
    source: &str,
) -> Result<(Vec<u8>, String), FullEngineError> {
    let mount = engine
        .mount(source)
        .cloned()
        .ok_or_else(|| engine.unknown_mem_error(source))?;
    let branch = match &mount.storage {
        crate::workspace::MountStorage::GitBranch {
            gitdir: source_gitdir,
            branch,
        } => {
            let source_gitdir = source_gitdir
                .canonicalize()
                .unwrap_or(source_gitdir.clone());
            if source_gitdir != gitdir {
                return Err(crate::EngineError::InvalidInput(format!(
                    "source {source:?} lives in {} and not in the workspace mem-repo {}: a fork \
                     is a branch of the same mem-repo",
                    source_gitdir.display(),
                    gitdir.display()
                ))
                .into());
            }
            crate::workspace::branch_full_ref(branch)
        }
        crate::workspace::MountStorage::Folder { .. } => {
            return Err(crate::EngineError::InvalidInput(format!(
                "source {source:?} is a folder mem: a fork needs a git-branch source, because \
                 the branch is the lineage"
            ))
            .into());
        }
        crate::workspace::MountStorage::Archive { .. } => {
            return Err(crate::EngineError::InvalidInput(format!(
                "source {source:?} is a sealed archive mount: a fork needs a git-branch source, \
                 because the branch is the lineage"
            ))
            .into());
        }
        crate::workspace::MountStorage::InMemory => {
            return Err(crate::EngineError::InvalidInput(format!(
                "source {source:?} is an in-memory mem: a fork needs a git-branch source, \
                 because the branch is the lineage"
            ))
            .into());
        }
    };
    let factory = engine.backend_factory();
    let backend = factory(&mount)
        .map_err(|e| crate::EngineError::Mem(format!("source backend instantiate: {e}")))?;
    let bytes = backend
        .read_mem_config()
        .map_err(crate::EngineError::Backend)?
        .ok_or_else(|| {
            crate::EngineError::Mem(format!(
                "source {source:?}: no config on __MEMSTEAD:mems/{source}/config.json"
            ))
        })?;
    Ok((bytes, branch))
}

/// Remote form: the remote's branch roster first (typed
/// `UNKNOWN_REMOTE`, and a branch the remote lacks refuses before a
/// fetch), then one fetch of the source branch and the remote's
/// `__MEMSTEAD` into remote-tracking refs. The local `__MEMSTEAD` is
/// never moved. Returns the source's config bytes from the tracking
/// ref and the tracking ref of the source branch.
fn resolve_remote_source(
    ops: &GitBranchOps,
    gitdir: &Path,
    remote: &str,
    source: &str,
) -> Result<(Vec<u8>, String), FullEngineError> {
    let heads = (ops.ls_remote)(gitdir, remote).map_err(lift_remote_marker)?;
    let source_ref = format!("refs/heads/{source}");
    if !heads.iter().any(|(name, _)| name == &source_ref) {
        return Err(crate::EngineError::UnknownRef(format!(
            "remote {remote:?} has no branch {source_ref}: the source mem {source:?} is not on \
             that remote"
        ))
        .into());
    }
    if !heads
        .iter()
        .any(|(name, _)| name == "refs/heads/__MEMSTEAD")
    {
        return Err(crate::EngineError::InvalidInput(format!(
            "remote {remote:?} carries no __MEMSTEAD ref: it is not a mem-repo, so there is no \
             config to fork from it; a fork branch of a mem this workspace mounts that already \
             lives on the remote is adopted instead (`memstead mem fork <source> <name> --remote \
             {remote} --adopt`), which derives the config from the local source"
        ))
        .into());
    }
    let tracking_source = format!("refs/remotes/{remote}/{source}");
    let tracking_registry = format!("refs/remotes/{remote}/__MEMSTEAD");
    (ops.fetch)(
        gitdir,
        remote,
        &[
            format!("+{source_ref}:{tracking_source}"),
            format!("+refs/heads/__MEMSTEAD:{tracking_registry}"),
        ],
    )
    .map_err(lift_remote_marker)?;
    let bytes = (ops.read_config_at_ref)(gitdir, &tracking_registry, source)
        .map_err(crate::EngineError::Backend)?
        .ok_or_else(|| {
            crate::EngineError::InvalidInput(format!(
                "remote {remote:?}: its __MEMSTEAD carries no config for {source:?} \
                 (mems/{source}/config.json)"
            ))
        })?;
    Ok((bytes, tracking_source))
}

/// Adopt form: the fork branch of the fork's own name must be on the
/// remote (typed `UNKNOWN_REMOTE`; a branch the remote lacks refuses
/// before a fetch, naming the source a fresh fork would be made from),
/// then one fetch of that branch into its remote-tracking ref. Neither
/// the remote's nor the local `__MEMSTEAD` is touched. Returns the
/// tracking ref of the fork branch.
fn fetch_adopted_fork(
    ops: &GitBranchOps,
    gitdir: &Path,
    remote: &str,
    source: &str,
    name: &str,
) -> Result<String, FullEngineError> {
    let heads = (ops.ls_remote)(gitdir, remote).map_err(lift_remote_marker)?;
    let fork_ref = format!("refs/heads/{name}");
    if !heads.iter().any(|(head, _)| head == &fork_ref) {
        return Err(crate::EngineError::UnknownRef(format!(
            "remote {remote:?} has no branch {fork_ref}: nothing to adopt as {name:?} (a fork \
             that does not exist yet is made with `memstead mem fork {source} {name}`)"
        ))
        .into());
    }
    let tracking = format!("refs/remotes/{remote}/{name}");
    (ops.fetch)(gitdir, remote, &[format!("+{fork_ref}:{tracking}")])
        .map_err(lift_remote_marker)?;
    Ok(tracking)
}

/// The base of an adopted fork: the engine's fork commit on the branch
/// above `ancestor`, when the branch carries one (made by `mem fork`
/// elsewhere under the same name and source: subject `memstead: fork
/// mem <name> from <source>@…`, the oldest such commit when several),
/// `None` otherwise.
fn adopted_fork_base(
    ops: &GitBranchOps,
    gitdir: &Path,
    branch_ref: &str,
    name: &str,
    source: &str,
    ancestor: &str,
) -> Result<Option<String>, crate::EngineError> {
    let changes = (ops.changes_since)(
        gitdir,
        branch_ref,
        name,
        ancestor,
        crate::ops::RENAME_SIMILARITY_DEFAULT,
    )
    .map_err(crate::EngineError::Backend)?;
    let prefix = format!("memstead: fork mem {name} from {source}@");
    Ok(changes
        .notes
        .iter()
        .filter(|n| n.tool_verb.as_deref() == Some("fork") && n.subject.starts_with(&prefix))
        .map(|n| n.sha.clone())
        .next_back())
}

/// The transport's in-band remote marker, lifted to the typed code the
/// other transport verbs use.
fn lift_remote_marker(e: BackendError) -> FullEngineError {
    match e {
        BackendError::Other(msg) if msg.starts_with("UNKNOWN_REMOTE:") => {
            crate::EngineError::UnknownRemote(
                msg.trim_start_matches("UNKNOWN_REMOTE:").trim().to_string(),
            )
            .into()
        }
        other => crate::EngineError::Backend(other).into(),
    }
}

/// The source's grant value, written under the fork's name through the
/// same writer `workspace grant-cross-link` uses: the wildcard as one
/// grant, a list as one grant per target.
fn write_inherited_grants(
    engine: &crate::Engine,
    root: &Path,
    name: &str,
    value: &CrossLinkValue,
) -> Result<(), crate::workspace_config_edit::WorkspaceEditError> {
    let known: Vec<String> = engine.mem_names().into_iter().map(str::to_string).collect();
    match value {
        CrossLinkValue::Wildcard => {
            grant_cross_link(root, name, &CrossLinkTarget::Wildcard, &known)?;
        }
        CrossLinkValue::List(targets) => {
            for target in targets {
                grant_cross_link(root, name, &CrossLinkTarget::Named(target.clone()), &known)?;
            }
        }
    }
    Ok(())
}

/// Undo a partial fork: the branch and the config blob through the
/// residue prune, the policy line through the deletion scrub. Both are
/// idempotent, so a stage that never wrote is not an error. A failing
/// rollback is logged with the manual remedy; the caller still returns
/// the original refusal.
fn roll_back(
    ops: &GitBranchOps,
    gitdir: &Path,
    root: &Path,
    name: &str,
    ctx: &CommitContext<'_>,
    stage: &str,
) {
    if let Err(e) = (ops.prune_residue)(gitdir, name, ctx) {
        tracing::warn!(
            mem = %name,
            stage,
            error = %e,
            "fork_mem: {stage} failed and the branch rollback failed too; \
             `memstead mem delete <name> --operator-mode` removes the leftover"
        );
    }
    if let Err(e) = scrub_policy_for_deleted_mem(root, name) {
        tracing::warn!(
            mem = %name,
            stage,
            error = %e,
            "fork_mem: {stage} failed and the policy rollback failed too; \
             `memstead workspace revoke-cross-link <name> <target>` removes the leftover"
        );
    }
}

#[cfg(test)]
mod tests {
    use super::retarget_entity_id;

    /// The id rule the two sidecars share: exactly the source's own
    /// ids move, by exact qualifier match; hierarchical names included.
    #[test]
    fn retarget_entity_id_moves_the_sources_ids_and_nothing_else() {
        assert_eq!(
            retarget_entity_id("specs--alpha", "specs", "specs-fork").as_deref(),
            Some("specs-fork--alpha")
        );
        assert_eq!(
            retarget_entity_id(
                "library/sample--claim-one",
                "library/sample",
                "proposals/sample-001"
            )
            .as_deref(),
            Some("proposals/sample-001--claim-one")
        );
        // A slug with dashes keeps them.
        assert_eq!(
            retarget_entity_id("specs--a--b", "specs", "f").as_deref(),
            Some("f--a--b")
        );
        // Another mem's id, a mem whose name merely starts with the
        // source's, a bare slug, an empty slug: untouched.
        assert_eq!(retarget_entity_id("plans--roadmap", "specs", "f"), None);
        assert_eq!(retarget_entity_id("specs-old--alpha", "specs", "f"), None);
        assert_eq!(retarget_entity_id("alpha", "specs", "f"), None);
        assert_eq!(retarget_entity_id("specs--", "specs", "f"), None);
    }
}