memstead-cli 0.8.0

Command-line interface for Memstead — query and mutate typed entity graphs from the shell. Default build produces the full `memstead` binary (multi-mem, git-backed); `--no-default-features` builds the lean folder-only surface.
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
//! Engine setup from global CLI flags. Produces an `Engine`
//! synchronously (no tokio) for the CLI to call into directly.
//!
//! Post-rebuild there is one workspace marker: `.memstead/workspace.toml`
//! at the workspace root. The `mem-repo` Cargo feature decides
//! which engine factory consumes it — full routes through
//! [`memstead_git_branch::workspace_store::engine_from_workspace_root`]
//! (git-branch backends plus folder + archive), lean routes through
//! [`memstead_base::Engine::from_workspace_root`] (folder + archive
//! only).
//!
//! [`CliEngine`] wraps either flavour; subcommands match-dispatch on
//! it. The `WorkspaceShape` variant is retained so the lean build
//! can still surface an actionable "this is the lean binary, your
//! workspace has git-branch mounts" error when the operator points a
//! lean binary at a full workspace — the shape tag is derived from
//! `mem-repo/.git` co-existing with the marker rather than the
//! marker itself.

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

#[cfg(feature = "mem-repo")]
use anyhow::Context;

use memstead_base::Engine as BaseEngine;
use memstead_base::vcs::ClientId;
#[cfg(feature = "mem-repo")]
use memstead_base::vcs::{Actor, CommitContext};
#[cfg(feature = "mem-repo")]
use memstead_git_branch::workspace_store::engine_from_workspace_root;

use crate::CliError;
use crate::output::ExitKind;

/// Structured-code constant for the missing-workspace exit envelope.
/// Surfaced on both `--json` output (under the `code` key in
/// `details`) and as the `Display` body of the underlying `CliError`.
/// Scripts and agents branch on this stable token; the human prose
/// (which mentions the recovery command) is the message and can be
/// adjusted without breaking the contract.
pub const WORKSPACE_NOT_INITIALISED_CODE: &str = "WORKSPACE_NOT_INITIALISED";

/// Recovery command suggested when no `.memstead/workspace.toml` is
/// reachable from cwd. `memstead mem-repo init` in the full build (this
/// binary speaks mem-repo); `memstead init` in the lean build. The
/// structured `hint.recovery_command` field carries this token
/// verbatim so an agent can re-exec it.
#[cfg(feature = "mem-repo")]
pub const WORKSPACE_RECOVERY_COMMAND: &str = "memstead mem-repo init";
#[cfg(not(feature = "mem-repo"))]
pub const WORKSPACE_RECOVERY_COMMAND: &str = "memstead init";

/// Build the typed `WORKSPACE_NOT_INITIALISED` exit envelope. Goes
/// through `CliError` so the top-level `main` downcast lifts the
/// `code` + `hint` fields into the JSON output.
pub fn workspace_not_initialised_error(message: &str) -> CliError {
    CliError {
        kind: ExitKind::Generic,
        code: WORKSPACE_NOT_INITIALISED_CODE,
        message: message.to_string(),
        details: Some(serde_json::json!({
            "hint": { "recovery_command": WORKSPACE_RECOVERY_COMMAND },
        })),
    }
}

/// Lift a [`memstead_base::BootError`] into the typed CLI envelope.
/// The boot seam previously flattened these through `anyhow`, so the
/// `main` downcast missed them and every boot failure surfaced as
/// `code: INTERNAL` with no next step (plenum 2026-08-06/07, expertise
/// 2026-08-07). The typed material lives on
/// [`memstead_base::BootError::code`]; this function only wraps it in
/// the CLI's exit shape. The message is
/// [`memstead_base::BootError::surface_message`] verbatim — identical
/// on the MCP server's boot diagnostics for the same broken workspace.
pub fn boot_error_to_cli(workspace_root: &Path, e: memstead_base::BootError) -> CliError {
    let details = e.details();
    let details = match &details {
        serde_json::Value::Object(map) if map.is_empty() => None,
        _ => Some(details),
    };
    CliError {
        kind: ExitKind::Generic,
        code: e.code(),
        message: e.surface_message(workspace_root),
        details,
    }
}

/// Global CLI state: shared flags + a lazily-initialized `Engine`.
pub struct CliContext {
    pub json: bool,
    /// User asked for quiet stderr (`--quiet`). The CLI runs the
    /// engine in-process and never installs a `tracing_subscriber`,
    /// so the flag is informational.
    pub quiet: bool,
    /// The invocation-level declared role (`--role`, agent-trust
    /// plan 13), already validated at parse time. Stamped onto every
    /// engine this context constructs so mutations record it.
    pub role: memstead_base::vcs::Role,
}

/// Workspace flavour resolved from cwd. Subcommands dispatch on this
/// to pick the right engine accessor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkspaceShape {
    /// Mem-repo workspace — multi-mem, git-backed.
    /// The `.memstead/workspace.toml` root also carries `mem-repo/.git/`.
    MemRepo,
    /// Filesystem-mem workspace — single-mem, history-free.
    /// The `.memstead/workspace.toml` root has no `mem-repo/.git/`.
    Filesystem,
}

/// Render a string as one POSIX shell word. Bare when every character
/// is safe unquoted; otherwise single-quoted, with embedded `'` closed
/// and re-opened the POSIX way (`'\''`).
///
/// A leading `-` forces quoting even though `-` is otherwise safe: an
/// argument that starts with a dash is read as an option by whatever
/// receives it. (Quoting alone does not save `cd`, which parses its
/// argument after the shell strips quotes — callers printing a `cd`
/// emit `cd --`.)
///
/// Lives here rather than beside its first caller because every message
/// that interpolates a filesystem path into a command the reader is
/// expected to run needs it, and the one that did not — the shape
/// disclosure's other-shape command — was unrunnable for anyone whose
/// binary path contained a space.
pub fn shell_quote(value: &str) -> String {
    let safe = |c: char| c.is_ascii_alphanumeric() || "._-/@:+,=".contains(c);
    if !value.is_empty() && !value.starts_with('-') && value.chars().all(safe) {
        return value.to_string();
    }
    format!("'{}'", value.replace('\'', r"'\''"))
}

/// The running binary, resolved and shell-quoted — the form to
/// interpolate into any command a message tells the reader to run.
fn memstead_word() -> String {
    shell_quote(&memstead_program())
}

/// The `UNSUPPORTED_WORKSPACE_SHAPE` refusal, in one place because both
/// mem-repo-only gates mint it and they must not drift. Names the
/// recovering command and the verbs that do work here, both resolved to
/// this binary — the refusal is read by someone who is about to type
/// what it says.
///
/// Both gates that mint it are mem-repo-only, so the lean build never
/// reaches this refusal (it has no mem-repo-only subcommand to refuse).
#[cfg(feature = "mem-repo")]
fn unsupported_workspace_shape_message() -> String {
    let m = memstead_word();
    format!(
        "this subcommand is mem-repo-only and not yet supported on filesystem-mem workspaces — \
         bootstrap one with `{m} mem-repo init` in a fresh folder, or use `{m} status` / \
         `{m} list` / `{m} search` / `{m} entity` / `{m} health` / \
         `{m} create|update|delete|relate|rename` here instead."
    )
}

/// Resolve the running `memstead` binary to something the reader can
/// actually type. Bare `memstead` when that name on `PATH` resolves to
/// this very binary; otherwise the path we were invoked as.
///
/// A reader who ran `./target/debug/memstead`, or an unpacked download,
/// or a binary under a versioned directory, has no `memstead` on
/// `PATH` — and every printed command naming a bare `memstead` fails
/// for them with `command not found`. Every message that tells someone
/// to run this binary goes through here.
pub fn memstead_program() -> String {
    let Ok(exe) = std::env::current_exe() else {
        return "memstead".to_string();
    };
    let canonical_exe = exe.canonicalize().unwrap_or_else(|_| exe.clone());
    if let Some(paths) = std::env::var_os("PATH") {
        for dir in std::env::split_paths(&paths) {
            let candidate = dir.join("memstead");
            if candidate.is_file() && candidate.canonicalize().is_ok_and(|c| c == canonical_exe) {
                return "memstead".to_string();
            }
        }
    }
    exe.display().to_string()
}

/// The command that produces the *other* shape than the one a
/// disclosure is describing. Feature-gated because every command a
/// message names must exist in the binary that prints it: the lean
/// build has no `mem-repo` subcommand group, so it points at the full
/// build rather than at a verb it would reject. The program name is
/// resolved rather than hardcoded, for the same reason the verify
/// commands resolve it — this is an instruction, not a mention.
#[cfg(feature = "mem-repo")]
fn mem_repo_init_hint() -> String {
    format!("`{} mem-repo init` in a fresh folder", memstead_word())
}
#[cfg(not(feature = "mem-repo"))]
fn mem_repo_init_hint() -> String {
    "the full build of memstead (this lean build has no `mem-repo` subcommand), then \
     `memstead mem-repo init` in a fresh folder"
        .to_string()
}

/// What a filesystem-mem workspace cannot do — stated with the same
/// feature gate as the hint above, and for the same reason. The full
/// build names `memstead install`, which exists there and refuses by
/// shape; the lean build has no `install` subcommand at all, so naming
/// it would send the reader to a verb that does not parse. The lean
/// wording states the limit without borrowing a command it lacks.
#[cfg(feature = "mem-repo")]
const FILESYSTEM_CANNOT: &str = "**It cannot install mems from the registry.** `memstead install \
     <scope>/<name>` (and the other mem-repo-only subcommands) refuse here with \
     `UNSUPPORTED_WORKSPACE_SHAPE`.";
#[cfg(not(feature = "mem-repo"))]
const FILESYSTEM_CANNOT: &str = "**It cannot install mems from the registry, and holds exactly \
     one mem.** The subcommands that do either are mem-repo-only, and this lean build does not \
     carry them at all.";

impl WorkspaceShape {
    /// Resolve the shape of an existing workspace root. Routes through
    /// the engine's shared probe so the CLI, the refusals, and the MCP
    /// boot line can never disagree about the same directory.
    pub fn at(workspace_root: &Path) -> Self {
        if memstead_base::is_mem_repo_shaped(workspace_root) {
            WorkspaceShape::MemRepo
        } else {
            WorkspaceShape::Filesystem
        }
    }

    /// The one spelling of this shape, shared with the engine.
    pub fn label(self) -> &'static str {
        match self {
            WorkspaceShape::MemRepo => "mem-repo",
            WorkspaceShape::Filesystem => "filesystem-mem",
        }
    }
}

/// The three-part disclosure a workspace-creating command owes its
/// caller: which shape was just made, one concrete thing that shape
/// cannot do, and the exact command that produces the other one.
///
/// Held as parts rather than pre-rendered prose because both receipts
/// carry it: the markdown block a human reads, and the `--json`
/// envelope an agent reads. A label alone on the machine surface would
/// name the fork without disclosing it, which is the failure this whole
/// disclosure exists to end — so both renderings come from one value.
pub struct ShapeDisclosure {
    /// The shape just created.
    pub shape: WorkspaceShape,
    /// One sentence on what this shape is.
    pub summary: &'static str,
    /// One concrete thing this shape cannot do, in markdown.
    pub cannot: &'static str,
    /// The shape a caller would get instead.
    pub other_shape: WorkspaceShape,
    /// The exact command producing [`Self::other_shape`], in markdown.
    pub other_shape_command: String,
}

/// The disclosure for a shape.
///
/// `quickstart`, `init`, and `mem-repo init` all print this — the
/// disclosure is symmetric, not a warning bolted onto one branch. It
/// belongs in the creating command's own receipt because that is the
/// moment the fork is decided and the output the newcomer is already
/// reading; a sentence elsewhere (the `install --help` clause)
/// demonstrably arrives after the workspace exists.
pub fn shape_disclosure(shape: WorkspaceShape) -> ShapeDisclosure {
    match shape {
        WorkspaceShape::Filesystem => ShapeDisclosure {
            shape,
            summary: "One mem, plain `.md` files in this folder, no git history — nothing \
                      else to set up.",
            cannot: FILESYSTEM_CANNOT,
            other_shape: WorkspaceShape::MemRepo,
            other_shape_command: format!(
                "**The other shape** — mem-repo: many mems, git-backed, registry-capable — \
                 comes from {hint}. Switching later means starting a second \
                 workspace, so decide now if you intend to install mems.",
                hint = mem_repo_init_hint(),
            ),
        },
        WorkspaceShape::MemRepo => ShapeDisclosure {
            shape,
            summary: "Many mems on git branches, full history — every subcommand works here, \
                      including `memstead install <scope>/<name>`.",
            cannot: "**It costs a git repository.** The mems live in `mem-repo/.git/` and \
                     every mutation is a commit — not a folder of files you can hand-edit.",
            other_shape: WorkspaceShape::Filesystem,
            other_shape_command: format!(
                "**The other shape** — filesystem-mem: one mem, plain `.md` files, no git — \
                 comes from `{} quickstart` in a fresh folder.",
                memstead_word(),
            ),
        },
    }
}

impl ShapeDisclosure {
    /// The markdown block for a human-facing receipt.
    pub fn lines(&self) -> Vec<String> {
        vec![
            format!("## Workspace shape: {}", self.shape.label()),
            String::new(),
            self.summary.to_string(),
            String::new(),
            format!("- {}", self.cannot),
            format!("- {}", self.other_shape_command),
        ]
    }

    /// The same three parts for a `--json` receipt. The agent surface
    /// gets the limit and the recovering command, not just the label.
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "shape": self.shape.label(),
            "summary": self.summary,
            "cannot": self.cannot,
            "other_shape": self.other_shape.label(),
            "other_shape_command": self.other_shape_command,
        })
    }
}

/// Convenience for callers that only render markdown.
pub fn shape_disclosure_lines(shape: WorkspaceShape) -> Vec<String> {
    shape_disclosure(shape).lines()
}

/// Engine instance + the workspace flavour it serves. Subcommands
/// match on the variant to call the right engine API; the read-side
/// store accessor (`engine.store()`) lives on both flavours so simple
/// read commands can share most of their bodies.
///
/// The `MemRepo` variant is only present under the `mem-repo`
/// feature. In the lean build (`--no-default-features`) the enum
/// collapses to a single `Filesystem` arm — every subcommand's
/// dispatch elides the missing arm via `cfg`.
pub enum CliEngine {
    #[cfg(feature = "mem-repo")]
    MemRepo(BaseEngine),
    /// Filesystem-mem flavour, served by the unified [`memstead_base::Engine`].
    Filesystem(BaseEngine),
}

impl CliEngine {
    /// The unified base engine behind whichever flavour booted. Both
    /// variants wrap [`BaseEngine`]; commands that treat the flavours
    /// identically destructure here instead of carrying a per-site
    /// match (which, in the lean build's single-variant enum, is the
    /// `infallible_destructuring_match` shape the isolated lean clippy
    /// leg flags).
    pub fn base(&self) -> &BaseEngine {
        #[cfg(feature = "mem-repo")]
        {
            match self {
                CliEngine::MemRepo(e) => e,
                CliEngine::Filesystem(e) => e,
            }
        }
        #[cfg(not(feature = "mem-repo"))]
        {
            let CliEngine::Filesystem(e) = self;
            e
        }
    }

    /// Mutable twin of [`Self::base`].
    pub fn base_mut(&mut self) -> &mut BaseEngine {
        #[cfg(feature = "mem-repo")]
        {
            match self {
                CliEngine::MemRepo(e) => e,
                CliEngine::Filesystem(e) => e,
            }
        }
        #[cfg(not(feature = "mem-repo"))]
        {
            let CliEngine::Filesystem(e) = self;
            e
        }
    }

    /// Owning twin of [`Self::base`].
    pub fn into_base(self) -> BaseEngine {
        #[cfg(feature = "mem-repo")]
        {
            match self {
                CliEngine::MemRepo(e) => e,
                CliEngine::Filesystem(e) => e,
            }
        }
        #[cfg(not(feature = "mem-repo"))]
        {
            let CliEngine::Filesystem(e) = self;
            e
        }
    }
}

impl CliContext {
    /// Resolve the workspace flavour by walking up from cwd. Returns
    /// `None` when no `.memstead/workspace.toml` is found in any ancestor.
    ///
    /// Post-rebuild the marker is shape-neutral — the same
    /// `.memstead/workspace.toml` carries both folder-only workspaces and
    /// mem-repo workspaces. The flavour tag comes from whether the
    /// workspace root also carries `mem-repo/.git/` (mem-repo
    /// flavour) or not (folder-only flavour). The lean CLI uses this
    /// distinction to surface "this is the lean binary" when the
    /// operator points it at a workspace with git-branch mounts.
    pub fn workspace_shape(&self) -> Option<(WorkspaceShape, PathBuf)> {
        let cwd = std::env::current_dir().ok()?;
        let root = find_workspace_root(&cwd)?;
        Some((WorkspaceShape::at(&root), root))
    }

    /// Build a [`CliEngine`] from the current cwd. The workspace
    /// marker `.memstead/workspace.toml` resolves either flavour; the
    /// presence of `mem-repo/.git/` switches the engine factory.
    ///
    /// On the lean build (`--no-default-features`) the mem-repo
    /// branch surfaces a clear "not built into this binary" error so
    /// a user pointing the lean build at a mem-repo workspace
    /// gets an actionable signal rather than a confusing "no
    /// workspace" bail.
    pub fn cli_engine(&self) -> anyhow::Result<CliEngine> {
        match self.workspace_shape() {
            Some((_, root)) => self.cli_engine_at(&root),
            None => Err(workspace_not_initialised_error(
                "No workspace found. Run from a directory containing `.memstead/workspace.toml` (run `memstead init` for a folder-mount workspace, or `memstead mem-repo init` for a mem-repo workspace).",
            )
            .into()),
        }
    }

    /// Build a [`CliEngine`] rooted at an explicit workspace directory,
    /// skipping the cwd walk-up. The flavour is still derived from
    /// whether `<root>/mem-repo/.git/` is present, so callers that
    /// already know the root (e.g. `memstead publish --workspace`) get
    /// the same factory selection as [`Self::cli_engine`]. The split
    /// also gives subcommands a chdir-free, unit-testable engine seam.
    pub fn cli_engine_at(&self, root: &Path) -> anyhow::Result<CliEngine> {
        if memstead_base::is_mem_repo_shaped(root) {
            #[cfg(feature = "mem-repo")]
            {
                let mut engine =
                    engine_from_workspace_root(root).map_err(|e| boot_error_to_cli(root, e))?;
                engine.set_role(self.role);
                return Ok(CliEngine::MemRepo(engine));
            }
            #[cfg(not(feature = "mem-repo"))]
            {
                return Err(CliError {
                    kind: ExitKind::Generic,
                    code: "UNSUPPORTED_WORKSPACE_SHAPE",
                    message:
                        "this is the lean build of memstead (folder-mount only); the workspace is mem-repo-shaped (`mem-repo/.git/` present). Install the full build (`cargo build --features mem-repo`) or run from a workspace whose mounts are all folder-backed."
                            .to_string(),
                    details: None,
                }
                .into());
            }
        }
        let mut engine =
            BaseEngine::from_workspace_root(root).map_err(|e| boot_error_to_cli(root, e))?;
        engine.set_role(self.role);
        Ok(CliEngine::Filesystem(engine))
    }

    /// Build the unified [`memstead_base::Engine`] for a mem-repo-shaped
    /// workspace. Delegates to `engine_from_workspace_root` which
    /// handles layout detection, mount enumeration, schema resolution,
    /// and readMems hydration in one pass.
    ///
    /// Only compiled into the full build — the lean build never sees a
    /// mem-repo workspace because `cli_engine()` rejects it before
    /// reaching here.
    #[cfg(feature = "mem-repo")]
    pub fn engine(&self) -> anyhow::Result<BaseEngine> {
        let cwd = std::env::current_dir().context("Could not determine current directory")?;

        let Some(root) = find_workspace_root(&cwd) else {
            return Err(workspace_not_initialised_error(
                "No workspace found. Run from a directory containing `.memstead/workspace.toml` (run `memstead mem-repo init` to bootstrap).",
            )
            .into());
        };

        // Subcommands routed through `engine()` (rather than
        // `cli_engine()`) require mem-repo shape — they read /
        // write commit-shaped artefacts (`workspace dump` snapshots,
        // `batch-update` commit envelopes) that have no analogue on a
        // folder-mount-only workspace. Surface the mem-repo-only
        // tag here so callers print an actionable message instead of
        // booting into a foldery engine and erroring later.
        if !memstead_base::is_mem_repo_shaped(&root) {
            return Err(CliError {
                kind: ExitKind::Generic,
                code: "UNSUPPORTED_WORKSPACE_SHAPE",
                message: unsupported_workspace_shape_message(),
                details: None,
            }
            .into());
        }

        let mut engine =
            engine_from_workspace_root(&root).map_err(|e| boot_error_to_cli(&root, e))?;
        engine.set_role(self.role);
        Ok(engine)
    }
}

/// Walk upward from `start` looking for the first ancestor that
/// contains `.memstead/workspace.toml` (the post-rebuild workspace
/// marker). Returns the first ancestor directory carrying the marker,
/// or `None` if the walk reaches filesystem root without finding one.
///
/// Both files and directories are accepted as `start`. A plain file's
/// parent is used as the first candidate; for a directory, the
/// directory itself is the first candidate.
///
/// Deeper-marker semantics: because the walk is upward and stops at
/// the first match, an inner workspace nested inside an outer one
/// resolves to the inner.
///
/// Mirrors `memstead-mcp/src/main.rs::find_workspace_root` and the
/// per-command walkers in `memstead-cli/src/commands/link.rs` /
/// `memstead-cli/src/commands/publish.rs`. Keep the resolution rules in
/// sync if any of these change.
pub fn find_workspace_root(start: &Path) -> Option<PathBuf> {
    let mut cursor: PathBuf = if start.is_dir() {
        start.to_path_buf()
    } else {
        start.parent()?.to_path_buf()
    };
    loop {
        if memstead_base::is_workspace_root(&cursor) {
            return Some(cursor);
        }
        let parent = cursor.parent()?;
        if parent == cursor {
            return None;
        }
        cursor = parent.to_path_buf();
    }
}

/// Compatibility alias for `find_workspace_root` — kept so existing
/// CLI subcommands (export, changes, …) that historically routed
/// through the lean-flavour walker continue to compile. Both walkers
/// now find the same marker; the alias is intentional for
/// call-site clarity (`find_workspace_root` reads as the canonical
/// surface; `find_filesystem_workspace_root` documents the
/// folder-mount-only intent of its caller).
pub fn find_filesystem_workspace_root(start: &Path) -> Option<PathBuf> {
    find_workspace_root(start)
}

/// Provenance bundle for every CLI-initiated mutation. `Actor::Cli` +
/// `memstead-cli@<CARGO_PKG_VERSION>`. The `Tool:` trailer stays `None`: CLI
/// subcommands aren't MCP tools and the commit subject (`memstead: create …`)
/// already carries the action verb — a second taxonomy would drift.
///
/// Only used by mem-repo write paths today; filesystem-mem write
/// paths assemble their own provenance directly. The function therefore
/// only compiles when `mem-repo` is enabled.
#[cfg(feature = "mem-repo")]
pub fn cli_ctx() -> CommitContext<'static> {
    cli_ctx_with_note(None)
}

/// The `memstead-cli@<version>` client identity stamped into the commit
/// body's `Client:` provenance trailer. Shared by every CLI mutation
/// path so the trailer is uniform across `create` / `update` / `relate`
/// / `rename`. Un-gated (unlike [`cli_ctx_with_note`]) because the
/// `relate` path passes the client to `relate_entity` directly rather
/// than through a `CommitContext`, and that path compiles on both
/// flavours.
pub fn cli_client_id() -> ClientId {
    ClientId {
        name: "memstead-cli".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
    }
}

/// Provenance bundle carrying an optional agent-authored `--note`.
/// The note rides into the same payload slot the MCP `note` parameter
/// uses; the engine's `require_notes` policy gate fires `NOTE_MISSING`
/// symmetrically across both surfaces.
#[cfg(feature = "mem-repo")]
pub fn cli_ctx_with_note(note: Option<String>) -> CommitContext<'static> {
    CommitContext {
        actor: Actor::Cli,
        client: Some(cli_client_id()),
        tool: None,
        note,
        role: Default::default(),
        logical_operation_id: None,
        entity_ids: None,
    }
}

/// Build the unified [`memstead_base::Engine`] for a mem-repo-shaped
/// workspace. Delegates to `engine_from_workspace_root` which
/// handles layout detection, mount enumeration, schema resolution,
/// and readMems hydration in one pass.
///
/// Subcommands routed through this helper require mem-repo shape —
/// they read / write commit-shaped artefacts (`workspace dump`
/// snapshots, `batch-update` commit envelopes) that have no analogue
/// on a folder-mount-only workspace.
#[cfg(feature = "mem-repo")]
pub fn full_engine(_ctx: &CliContext) -> anyhow::Result<BaseEngine> {
    // Typed, not INTERNAL: an unreadable or deleted working directory
    // is an environment condition the caller can act on (`cd` somewhere
    // that exists), and no leaf of a user-triggerable command may
    // collapse into the generic sentinel.
    let cwd = std::env::current_dir().map_err(|e| {
        CliError::new(
            ExitKind::Generic,
            "INTERNAL_IO_ERROR",
            format!("could not determine the current directory ({e}) — run from a directory that exists and is readable"),
        )
    })?;

    let Some(root) = find_workspace_root(&cwd) else {
        return Err(workspace_not_initialised_error(
            "No workspace found. Run from a directory containing `.memstead/workspace.toml` (run `memstead mem-repo init` to bootstrap).",
        )
        .into());
    };

    if !memstead_base::is_mem_repo_shaped(&root) {
        return Err(CliError {
            code: "UNSUPPORTED_WORKSPACE_SHAPE",
            kind: ExitKind::Generic,
            message: unsupported_workspace_shape_message(),
            details: None,
        }
        .into());
    }

    let mut engine = engine_from_workspace_root(&root).map_err(|e| boot_error_to_cli(&root, e))?;
    engine.set_role(_ctx.role);
    Ok(engine)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn touch_marker(ws: &std::path::Path) {
        std::fs::create_dir_all(ws.join(".memstead")).unwrap();
        std::fs::write(ws.join(".memstead").join("workspace.toml"), "").unwrap();
    }

    #[test]
    fn find_workspace_root_walks_up_to_marker() {
        let tmp = TempDir::new().unwrap();
        let ws = tmp.path().join("ws");
        let nested = ws.join("a").join("b").join("specs");
        std::fs::create_dir_all(&nested).unwrap();
        touch_marker(&ws);
        let found =
            find_workspace_root(&nested).expect("walk should find .memstead/workspace.toml");
        assert_eq!(found.canonicalize().unwrap(), ws.canonicalize().unwrap());
    }

    #[test]
    fn find_workspace_root_returns_none_when_absent() {
        let tmp = TempDir::new().unwrap();
        let nested = tmp.path().join("a").join("b");
        std::fs::create_dir_all(&nested).unwrap();
        assert!(find_workspace_root(&nested).is_none());
    }

    #[test]
    fn find_workspace_root_stops_at_containing_dir() {
        let tmp = TempDir::new().unwrap();
        let ws = tmp.path().join("ws");
        std::fs::create_dir_all(&ws).unwrap();
        touch_marker(&ws);
        let found = find_workspace_root(&ws).expect("ws itself carries .memstead/workspace.toml");
        assert_eq!(found, ws);
    }

    #[test]
    fn find_workspace_root_accepts_file_start() {
        let tmp = TempDir::new().unwrap();
        let ws = tmp.path().join("ws");
        std::fs::create_dir_all(&ws).unwrap();
        touch_marker(&ws);
        let file = ws.join("some-file.md");
        std::fs::write(&file, "").unwrap();
        let found = find_workspace_root(&file).expect("file start should resolve to its dir");
        assert_eq!(found, ws);
    }

    #[test]
    fn find_workspace_root_deeper_marker_wins() {
        // Outer and inner each carry `.memstead/workspace.toml`. The walk
        // starts deep inside the inner dir and must resolve to the
        // inner — deeper marker wins because the upward walk stops at
        // the first match.
        let tmp = TempDir::new().unwrap();
        let outer = tmp.path().join("outer");
        let inner = outer.join("inner");
        let deep = inner.join("a").join("b");
        std::fs::create_dir_all(&deep).unwrap();
        touch_marker(&outer);
        touch_marker(&inner);
        let found = find_workspace_root(&deep).expect("walk should find the inner marker");
        assert_eq!(found.canonicalize().unwrap(), inner.canonicalize().unwrap());
    }
}