memstead-cli 0.7.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
//! `memstead init` — bootstrap a filesystem mem in the current (or named) folder.
//!
//! filesystem-mem is the single-mem, history-free, filesystem-backed product
//! surface. After `memstead init` the folder contains:
//!
//! - `.memstead/config.json` — workspace shape (mem name, schema pin,
//!   empty deps list). Pinned via [`memstead_base::filesystem::config`].
//! - `.memstead/cache/` — empty placeholder for any engine-managed cache
//!   data the workspace acquires later (e.g. resolved schema bytes).
//! - `.memstead/memstead-io/` — empty placeholder for cross-mem dependency
//!   archives populated by `memstead link`.
//!
//! No `.gitignore` is written — filesystem-mem does not assume a surrounding
//! git repo, and writing one would surprise users who *do* track the
//! workspace under git themselves.
//!
//! Strict mode in non-empty folders: see plan trade-off "Adopt vs.
//! strict for `memstead init`". A non-empty target errors out cleanly so
//! the user explicitly clears or moves files before initialising —
//! never silently ingests unrelated `.md` files.

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

use clap::Args;
use memstead_base::filesystem::config::{
    FILESYSTEM_WORKSPACE_FORMAT, config_path, init_filesystem_mem, validate_mem_name,
};
use memstead_schema::SchemaRef;
use serde_json::json;

use crate::CliError;
use crate::output::{ExitKind, print_json, print_markdown};
use crate::setup::CliContext;

/// Recovery hint for the nested-workspace refusal. Every printed
/// alternative must exist and be able to succeed in the binary that
/// prints it: `memstead mem init` is the full (mem-repo) verb; the
/// lean binary has no `mem` subcommand group, so it points outside
/// the existing workspace instead.
#[cfg(feature = "mem-repo")]
const NESTED_WORKSPACE_HINT: &str = "If you meant to add a mem inside the existing \
     workspace, run `memstead mem init` instead; for a separate graph, initialise in a \
     folder outside the existing workspace.";
#[cfg(not(feature = "mem-repo"))]
const NESTED_WORKSPACE_HINT: &str = "Initialise in a folder outside the existing \
     workspace instead.";

/// `memstead init` arguments.
#[derive(Args, Debug)]
pub struct InitArgs {
    /// Target folder. Defaults to the current working directory.
    #[arg(value_name = "PATH")]
    pub path: Option<PathBuf>,

    /// Mem name. Slug-shaped: `^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$`.
    #[arg(long)]
    pub name: String,

    /// Schema pin in exact `<name>@<version>` form (e.g.
    /// `default@1.0.0`). Bare-name pins are rejected. filesystem-mem v1
    /// resolves against the engine's builtin schema set;
    /// registry-resolved schemas land in a follow-up.
    #[arg(long)]
    pub schema: String,
}

pub fn run(ctx: &CliContext, args: InitArgs) -> anyhow::Result<()> {
    let target = args
        .path
        .clone()
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

    let schema_pin: SchemaRef = args.schema.parse().map_err(|e: String| CliError {
        code: "INVALID_INPUT",
        message: format!("invalid --schema {value:?}: {e}", value = args.schema),
        kind: ExitKind::Validation,
        details: None,
    })?;

    // The mem name is path-derived and no longer round-trips through
    // `config.json`, so validate the slug shape here at the boundary.
    validate_mem_name(&args.name).map_err(|e| CliError {
        code: "INVALID_INPUT",
        message: format!("invalid --name: {e}"),
        kind: ExitKind::Validation,
        details: None,
    })?;

    // A pin that resolves to no built-in schema is loudly flagged, not
    // refused: a fresh workspace has no `.memstead/schemas/` yet, and
    // `memstead schema install` only works *inside* a workspace, so
    // init-with-pin followed by install is the designed (and, on the
    // lean build, the only) custom-schema flow. Without the warning the
    // command reports success and every later engine-booting command
    // dies on `SCHEMA_NOT_FOUND` with no hint how the workspace got
    // into that state. (`memstead mem init` / MCP `memstead_mem_create`
    // refuse instead — there the workspace already exists, so
    // install-before-pin is always possible.)
    let builtin = memstead_schema::builtins::load_builtin_schemas().map_err(|e| CliError {
        code: "SCHEMA_RESOLVER_INIT_FAILED",
        message: format!("load built-in schema catalogue: {e}"),
        kind: ExitKind::Generic,
        details: None,
    })?;
    let pin_unresolved =
        memstead_base::engine::resolve_builtin_schema_pin_pub(&schema_pin, &builtin).is_none();
    let unresolved_warning = pin_unresolved.then(|| unresolved_pin_warning(&schema_pin, &builtin));
    if let Some(w) = &unresolved_warning {
        eprintln!("memstead: WARNING [SCHEMA_NOT_FOUND]: {w}");
    }

    if target.exists() {
        if !target.is_dir() {
            return Err(CliError {
                code: "INVALID_INPUT",
                message: format!("target {} exists but is not a directory", target.display()),
                kind: ExitKind::Validation,
                details: None,
            }
            .into());
        }
        ensure_empty(&target)?;
    } else {
        std::fs::create_dir_all(&target).map_err(|e| CliError {
            code: crate::INTERNAL_CODE,
            message: format!(
                "failed to create target directory {}: {e}",
                target.display()
            ),
            kind: ExitKind::Generic,
            details: None,
        })?;
    }

    // Refuse when an ancestor directory already has a
    // `.memstead/workspace.toml` — never nest a fresh filesystem-mem
    // workspace inside an existing one (the outer's `mem list` would
    // miss the inner, the inner would miss the outer). The walk starts
    // at the target's parent (target itself is what we're initialising)
    // and stops at the filesystem root.
    if let Some(found_at) = find_ancestor_workspace(&target)? {
        return Err(CliError {
            code: crate::WORKSPACE_ALREADY_EXISTS_ABOVE_CODE,
            kind: ExitKind::Validation,
            message: format!(
                "an existing memstead workspace lives above {} at {}; \
                 `memstead init` refuses to nest workspaces. {}",
                target.display(),
                found_at.display(),
                NESTED_WORKSPACE_HINT,
            ),
            details: Some(serde_json::json!({
                "found_at": found_at.display().to_string(),
                "hint": NESTED_WORKSPACE_HINT,
            })),
        }
        .into());
    }

    // Write the seed structure (config + `.memstead/` subdirs + adapter
    // marker + one-folder-mount roster) through the engine's shared
    // initialiser, so the CLI and the in-process embedders (the macOS app's
    // bootstrap) produce a byte-identical filesystem mem from one place.
    init_filesystem_mem(&target, &args.name, &schema_pin).map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        message: format!("initialise filesystem mem: {e}"),
        kind: ExitKind::Generic,
        details: None,
    })?;

    // Folder-mem provenance notice: this storage class has no version
    // control, so say at creation what provenance means here. Shares
    // the engine's typed warning so the CLI and `memstead_mem_create`
    // read as one voice. A warning, never a refusal.
    let provenance_notice = memstead_base::ops::WarningHint::FolderMemProvenance {
        mem: args.name.clone(),
    };

    if ctx.json {
        let mut warnings = vec![json!({
            "code": provenance_notice.code(),
            "message": provenance_notice.message(),
        })];
        // Additive optional entry on the stable success shape — only
        // present when the pin is unresolved at init time.
        if let Some(w) = &unresolved_warning {
            warnings.push(json!({ "code": "SCHEMA_NOT_FOUND", "message": w }));
        }
        let mut payload = json!({
            "workspace_root": target.display().to_string(),
            "config_path": config_path(&target).display().to_string(),
            "name": args.name,
            "schema": schema_pin.as_display(),
            "format": FILESYSTEM_WORKSPACE_FORMAT,
        });
        payload["warnings"] = json!(warnings);
        return print_json(&payload);
    }

    let mut lines = vec![
        format!("# Initialised filesystem mem `{}`", args.name),
        String::new(),
        format!("- Workspace root: `{}`", target.display()),
        format!("- Config:         `{}`", config_path(&target).display()),
        format!("- Schema pin:     `{}`", schema_pin.as_display()),
        String::new(),
        "Next steps:".to_string(),
    ];
    if unresolved_warning.is_some() {
        lines.push(format!(
            "- **Install the pinned schema first**: `memstead schema install <package-dir>` \
             (run inside this workspace) — `{}` resolves to no built-in schema, and every \
             engine-booting command fails with `SCHEMA_NOT_FOUND` until the package is installed.",
            schema_pin.as_display()
        ));
    }
    lines.extend([
        "- Drop `.md` entities into the workspace root.".to_string(),
        "- `memstead link <scope/name>` to add a cross-mem dependency.".to_string(),
        "- `memstead publish` to push the mem to the registry.".to_string(),
        String::new(),
        format!(
            "> [{}] {}",
            provenance_notice.code(),
            provenance_notice.message()
        ),
    ]);
    print_markdown(&lines.join("\n"));
    Ok(())
}

/// The loud-warning text for a schema pin that resolves to no built-in
/// schema at init time. Names the pin, the recovery command, and the
/// available built-ins, so the follow-up (`memstead schema install`) is
/// discoverable from the warning alone.
fn unresolved_pin_warning(
    pin: &SchemaRef,
    builtin: &[std::sync::Arc<memstead_schema::Schema>],
) -> String {
    let available: Vec<String> = builtin
        .iter()
        .map(|s| {
            let (name, version) = s.id();
            format!("{name}@{version}")
        })
        .collect();
    format!(
        "--schema {pin} resolves to no built-in schema (built-ins: {avail}). \
         The workspace is initialised, but every engine-booting command fails with \
         SCHEMA_NOT_FOUND until the package is installed: run \
         `memstead schema install <package-dir>` inside the new workspace.",
        pin = pin.as_display(),
        avail = available.join(", "),
    )
}

/// Walk parent directories looking for `.memstead/workspace.toml`.
/// Returns the absolute path of the first match, or `None` if no
/// ancestor carries the marker. Stops at the filesystem root. Symlinks are
/// not dereferenced — `ancestors()` operates on the resolved
/// `canonicalize`d path, which traverses symlinks once at the
/// boundary and then stays on the resolved filesystem.
/// Shared with `memstead quickstart`, which enforces the same
/// no-nested-workspaces rule.
pub(crate) fn find_ancestor_workspace(target: &Path) -> anyhow::Result<Option<PathBuf>> {
    let abs = std::fs::canonicalize(target).map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        kind: ExitKind::Generic,
        message: format!("canonicalize {}: {e}", target.display()),
        details: None,
    })?;
    // Skip `abs` itself — the target is what we're initialising; we
    // only care about ancestors. `ancestors()` yields `abs` first,
    // then each parent.
    for ancestor in abs.ancestors().skip(1) {
        if memstead_base::is_workspace_root(ancestor) {
            return Ok(Some(
                ancestor
                    .join(memstead_base::WORKSPACE_STORE_DIR)
                    .join("workspace.toml"),
            ));
        }
    }
    Ok(None)
}

/// Strict-mode emptiness check. The folder is "empty" when it contains
/// no entries at all — a `.git/` from a parent repo (the user's outer
/// project) is fine because that lives outside `target`. A pre-existing
/// `.memstead/`, any `.md` file, or any other content forces the user to
/// resolve the conflict before init proceeds.
fn ensure_empty(target: &Path) -> anyhow::Result<()> {
    let mut iter = std::fs::read_dir(target).map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        message: format!("read target {}: {e}", target.display()),
        kind: ExitKind::Generic,
        details: None,
    })?;
    if let Some(entry) = iter.next().transpose().map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        message: format!("read target {}: {e}", target.display()),
        kind: ExitKind::Generic,
        details: None,
    })? {
        let found = entry.file_name().to_string_lossy().to_string();
        return Err(CliError {
            code: crate::TARGET_NOT_EMPTY_CODE,
            message: format!(
                "target {} is not empty (found `{}`); \
                 memstead init refuses to ingest existing content — clear or move files first, \
                 or pick a fresh folder",
                target.display(),
                found,
            ),
            kind: ExitKind::Validation,
            details: Some(serde_json::json!({
                "path": target.display().to_string(),
                "found": [found],
            })),
        }
        .into());
    }
    Ok(())
}

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

    fn run_init(target: &Path, name: &str, schema: &str) -> anyhow::Result<()> {
        let ctx = CliContext {
            json: false,
            quiet: false,
            role: Default::default(),
        };
        run(
            &ctx,
            InitArgs {
                path: Some(target.to_path_buf()),
                name: name.to_string(),
                schema: schema.to_string(),
            },
        )
    }

    #[test]
    fn init_creates_config_and_subdirs_in_empty_folder() {
        // Identity is path-derived: the mem lives in a folder named after it.
        let tmp = TempDir::new().unwrap();
        let root = tmp.path().join("demo");
        run_init(&root, "demo", "default@1.0.0").unwrap();

        let cfg = read_workspace_config(&root).unwrap();
        assert_eq!(cfg.name, "demo"); // filled from the basename, not config.json
        assert_eq!(cfg.schema.as_display(), "default@1.0.0");
        assert!(cfg.deps.is_empty());

        // The persisted config carries no `name` (path-derived; the schema
        // validator tombstones a stray one).
        let raw: serde_json::Value =
            serde_json::from_slice(&std::fs::read(config_path(&root)).unwrap()).unwrap();
        assert!(
            raw.get("name").is_none(),
            "config.json must not persist `name`"
        );

        assert!(root.join(".memstead").join("cache").is_dir());
        assert!(root.join(".memstead").join("memstead-io").is_dir());
        // No .gitignore is written.
        assert!(!root.join(".gitignore").exists());
    }

    #[test]
    fn init_creates_target_when_missing() {
        let tmp = TempDir::new().unwrap();
        let target = tmp.path().join("nested-fresh");
        run_init(&target, "demo", "default@1.0.0").unwrap();
        assert!(target.join(".memstead").join("config.json").is_file());
    }

    #[test]
    fn init_rejects_non_empty_folder() {
        let tmp = TempDir::new().unwrap();
        std::fs::write(tmp.path().join("preexisting.md"), b"# pre").unwrap();
        let err = run_init(tmp.path(), "demo", "default@1.0.0").unwrap_err();
        assert!(
            err.to_string().contains("not empty"),
            "expected 'not empty' rejection, got: {err}"
        );
    }

    #[test]
    fn init_rejects_invalid_schema_pin() {
        let tmp = TempDir::new().unwrap();
        // Range syntax is rejected upstream by SchemaRef's FromStr.
        let err = run_init(tmp.path(), "demo", "default@^1.0.0").unwrap_err();
        assert!(
            err.to_string().contains("invalid --schema"),
            "expected schema rejection, got: {err}"
        );
    }

    #[test]
    fn init_rejects_invalid_name() {
        // The name is path-derived and no longer round-trips through
        // `config.json`, so the slug shape is enforced at the CLI boundary:
        // an invalid `--name` is rejected up front rather than on a later read.
        let tmp = TempDir::new().unwrap();
        let err = run_init(tmp.path(), "Demo Bad", "default@1.0.0").unwrap_err();
        assert!(
            err.to_string().contains("invalid --name"),
            "expected --name rejection, got: {err}"
        );
    }

    /// A well-formed pin that resolves to no built-in schema still
    /// initialises (init-then-`schema install` is the designed — and on
    /// the lean build the only — custom-schema flow), but never
    /// silently: the run emits the `SCHEMA_NOT_FOUND` warning whose
    /// text names the recovery command.
    #[test]
    fn init_succeeds_but_warns_on_unresolvable_schema_pin() {
        let tmp = TempDir::new().unwrap();
        let target = tmp.path().join("demo");
        run_init(&target, "demo", "agent-program@0.1.0").unwrap();
        // The workspace exists and carries the pin verbatim — the
        // follow-up `memstead schema install` completes the flow.
        let cfg = read_workspace_config(&target).unwrap();
        assert_eq!(cfg.schema.as_display(), "agent-program@0.1.0");
    }

    /// The warning text carries everything needed to recover: the pin,
    /// the `schema install` command, and the built-in alternatives.
    #[test]
    fn unresolved_pin_warning_names_pin_recovery_and_builtins() {
        let builtin = memstead_schema::builtins::load_builtin_schemas().unwrap();
        let pin: SchemaRef = "agent-program@0.1.0".parse().unwrap();
        assert!(
            memstead_base::engine::resolve_builtin_schema_pin_pub(&pin, &builtin).is_none(),
            "test premise: agent-program is not a built-in"
        );
        let w = unresolved_pin_warning(&pin, &builtin);
        assert!(w.contains("agent-program@0.1.0"), "got: {w}");
        assert!(w.contains("memstead schema install"), "got: {w}");
        assert!(w.contains("default@1.0.0"), "got: {w}");
        assert!(w.contains("SCHEMA_NOT_FOUND"), "got: {w}");
    }

    /// Every built-in schema is pinnable at init — the refusal above
    /// only fires for pins outside the built-in catalogue.
    #[test]
    fn init_accepts_every_builtin_schema_pin() {
        let builtin = memstead_schema::builtins::load_builtin_schemas().unwrap();
        assert!(!builtin.is_empty());
        for schema in builtin {
            let (name, version) = schema.id();
            let tmp = TempDir::new().unwrap();
            let target = tmp.path().join("demo");
            run_init(&target, "demo", &format!("{name}@{version}"))
                .unwrap_or_else(|e| panic!("built-in pin {name}@{version} refused: {e}"));
        }
    }

    #[test]
    fn init_rejects_bare_name_schema_pin() {
        let tmp = TempDir::new().unwrap();
        let err = run_init(tmp.path(), "demo", "default").unwrap_err();
        assert!(
            err.to_string().contains("invalid --schema"),
            "expected bare-name pin rejection, got: {err}"
        );
    }

    /// A fresh `memstead init` in a subdirectory of an existing workspace
    /// refuses with the typed `WORKSPACE_ALREADY_EXISTS_ABOVE`
    /// envelope rather than silently nesting a new workspace inside
    /// the existing one.
    #[test]
    fn init_refuses_nested_workspace_under_existing_one() {
        let tmp = TempDir::new().unwrap();
        // Seed an outer workspace at tmp.
        std::fs::create_dir_all(tmp.path().join(".memstead")).unwrap();
        std::fs::write(
            tmp.path().join(".memstead").join("workspace.toml"),
            "format = \"memstead-git-branch-2\"\n",
        )
        .unwrap();

        // Attempt a nested init under a sibling subdir.
        let inner = tmp.path().join("inner-mem");
        std::fs::create_dir_all(&inner).unwrap();
        let err = run_init(&inner, "inner", "default@1.0.0").unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("nest workspaces") || msg.contains("memstead mem init"),
            "expected nested-workspace refusal hint, got: {msg}"
        );
    }

    /// A fresh init in a clean directory (no ancestor workspace)
    /// still succeeds.
    #[test]
    fn init_succeeds_when_no_ancestor_workspace() {
        let tmp = TempDir::new().unwrap();
        let target = tmp.path().join("clean");
        std::fs::create_dir_all(&target).unwrap();
        run_init(&target, "demo", "default@1.0.0").unwrap();
        assert!(target.join(".memstead").join("workspace.toml").is_file());
    }
}