memstead-cli 0.1.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
#![cfg(feature = "mem-repo")]
// `memstead workspace ...` config commands ship only in the full build.

//! Integration tests for the `memstead workspace` write-side subcommand
//! family.
//!
//! Full flavour only — lean CLIs don't expose `memstead workspace`. Each
//! test seeds a fresh workspace (the minimum-viable `workspace.toml`
//! that `memstead mem-repo init` materialises), runs one or more
//! subcommands, and asserts on the resulting TOML and the engine's
//! parse of it.

use std::fs;
use std::path::Path;

use assert_cmd::Command;
use tempfile::TempDir;

const DEFAULT_BODY: &str =
    "format = \"memstead-git-branch-2\"\n\n[persistence_adapter]\nname = \"file-two-layer\"\n";

fn memstead() -> Command {
    Command::cargo_bin("memstead").expect("memstead binary must be built by cargo")
}

fn seed_workspace() -> TempDir {
    let tmp = TempDir::new().unwrap();
    let memstead_dir = tmp.path().join(".memstead");
    fs::create_dir_all(&memstead_dir).unwrap();
    fs::write(memstead_dir.join("workspace.toml"), DEFAULT_BODY).unwrap();
    fs::create_dir_all(tmp.path().join("mem-repo").join(".git")).unwrap();
    tmp
}

fn read_toml(workspace_root: &Path) -> String {
    fs::read_to_string(workspace_root.join(".memstead").join("workspace.toml")).unwrap()
}

#[test]
fn allow_create_writes_rule_then_re_derives_through_cli() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
        ])
        .assert()
        .success();

    let body = read_toml(ws.path());
    assert!(body.contains("[[mem_management.create]]"), "got:\n{body}");
    assert!(body.contains("pattern = \"exec-*\""), "got:\n{body}");
    assert!(
        body.contains("schemas = [\"default@1.0.0\"]"),
        "got:\n{body}"
    );
}

#[test]
fn allow_create_supports_before_flag() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "z-*",
            "--schema",
            "default@1.0.0",
        ])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "a-*",
            "--schema",
            "default@1.0.0",
            "--before",
            "z-*",
        ])
        .assert()
        .success();

    let body = read_toml(ws.path());
    let a_idx = body.find("pattern = \"a-*\"").expect("a-* missing");
    let z_idx = body.find("pattern = \"z-*\"").expect("z-* missing");
    assert!(a_idx < z_idx, "a-* must precede z-*; got:\n{body}");
}

#[test]
fn allow_create_supports_cross_links_flag() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
            "--cross-link",
            "engine,plugin",
        ])
        .assert()
        .success();

    let body = read_toml(ws.path());
    assert!(body.contains("default_cross_links"), "got:\n{body}");
    assert!(body.contains("\"engine\""), "got:\n{body}");
    assert!(body.contains("\"plugin\""), "got:\n{body}");
}

#[test]
fn revoke_create_removes_rule() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-create", "exec-*", "--schema", "default"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "revoke-create", "exec-*"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(!body.contains("pattern = \"exec-*\""), "got:\n{body}");
}

#[test]
fn allow_delete_then_revoke_delete_roundtrip() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-delete", "exec-*"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(body.contains("[[mem_management.delete]]"), "got:\n{body}");
    assert!(body.contains("pattern = \"exec-*\""), "got:\n{body}");
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "revoke-delete", "exec-*"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(!body.contains("pattern = \"exec-*\""), "got:\n{body}");
}

#[test]
fn grant_cross_link_named_and_wildcard() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "plugin", "engine"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "specs", "*"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(body.contains("[cross_mem_links]"), "got:\n{body}");
    assert!(body.contains("plugin = [\"engine\"]"), "got:\n{body}");
    assert!(body.contains("specs = \"*\""), "got:\n{body}");
}

#[test]
fn revoke_cross_link_drops_key_when_empty() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "plugin", "engine"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "revoke-cross-link", "plugin", "engine"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(!body.contains("plugin = "), "got:\n{body}");
}

#[test]
fn set_mutations_require_notes_toggles() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "set-mutations", "--require-notes", "true"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(body.contains("[mutations]"), "got:\n{body}");
    assert!(body.contains("require_notes = true"), "got:\n{body}");
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "set-mutations", "--require-notes", "false"])
        .assert()
        .success();
    let body = read_toml(ws.path());
    assert!(body.contains("require_notes = false"), "got:\n{body}");
}

/// Duplicate `allow-create`
/// is idempotent. The second invocation exits 0 (script-friendly) but
/// emits a `RULE_ALREADY_PRESENT` notice so the operator sees that
/// the file wasn't touched.
///
/// In markdown-default mode
/// the warning rides on stdout under `## Warnings` to match every
/// other CLI mutation's render shape; in `--json` mode the warning
/// stays on stderr to keep the envelope shape unchanged. Both shapes
/// surface the typed code so an operator can recover from either
/// channel.
#[test]
fn duplicate_pattern_is_idempotent_with_warning() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-create", "exec-*", "--schema", "default"])
        .assert()
        .success();
    let assertion = memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-create", "exec-*", "--schema", "default"])
        .assert()
        .success();
    let stdout = std::str::from_utf8(&assertion.get_output().stdout).unwrap();
    assert!(
        stdout.contains("## Warnings"),
        "markdown-default mode must surface a `## Warnings` block; got stdout:\n{stdout}",
    );
    assert!(
        stdout.contains("RULE_ALREADY_PRESENT"),
        "second allow-create must emit RULE_ALREADY_PRESENT under `## Warnings`; got stdout:\n{stdout}",
    );

    // `--json` mode keeps the warning on stderr so the envelope shape
    // (`{action, detail}`) stays untouched.
    let json_assertion = memstead()
        .current_dir(ws.path())
        .args([
            "--json",
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default",
        ])
        .assert()
        .success();
    let json_stderr = std::str::from_utf8(&json_assertion.get_output().stderr).unwrap();
    assert!(
        json_stderr.contains("RULE_ALREADY_PRESENT"),
        "--json mode must keep the warning on stderr; got stderr:\n{json_stderr}",
    );
}

#[test]
fn missing_workspace_returns_workspace_not_initialised_code() {
    let tmp = TempDir::new().unwrap();
    let output = memstead()
        .current_dir(tmp.path())
        .args([
            "--json",
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default",
        ])
        .assert()
        .failure()
        .get_output()
        // Under `--json` the error envelope rides stdout.
        .stdout
        .clone();
    let body = std::str::from_utf8(&output).unwrap();
    let parsed: serde_json::Value = serde_json::from_str(body.trim()).expect("JSON envelope");
    assert_eq!(parsed["code"], "WORKSPACE_NOT_INITIALISED");
}

/// `memstead workspace show` renders the active workspace
/// configuration in markdown by default; `--json` emits a structured
/// document covering mem_management, cross_mem_links, mutations,
/// and plugin sections.
#[test]
fn workspace_show_renders_markdown_by_default() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
        ])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "plugin", "engine"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "set-mutations", "--require-notes", "true"])
        .assert()
        .success();

    let output = memstead()
        .current_dir(ws.path())
        .args(["workspace", "show"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let body = String::from_utf8(output).unwrap();
    assert!(body.contains("# Workspace configuration"), "got:\n{body}");
    assert!(body.contains("Mem management"), "got:\n{body}");
    assert!(body.contains("`exec-*`"), "got:\n{body}");
    assert!(body.contains("Cross-mem links"), "got:\n{body}");
    assert!(body.contains("`plugin`"), "got:\n{body}");
    assert!(body.contains("Mutations"), "got:\n{body}");
    assert!(body.contains("`require_notes`: `true`"), "got:\n{body}");
}

#[test]
fn workspace_show_json_includes_all_sections() {
    let ws = seed_workspace();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
            "--cross-link",
            "engine",
        ])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-delete", "exec-*"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "plugin", "engine"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "specs", "*"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "set-mutations", "--require-notes", "false"])
        .assert()
        .success();

    let output = memstead()
        .current_dir(ws.path())
        .args(["--json", "workspace", "show"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let parsed: serde_json::Value = serde_json::from_slice(&output).expect("valid JSON");
    assert!(parsed["workspace_root"].is_string());

    let create = &parsed["mem_management"]["create"];
    assert!(create.is_array());
    assert_eq!(create[0]["pattern"], "exec-*");
    assert_eq!(create[0]["schemas"][0], "default@1.0.0");
    assert_eq!(create[0]["default_cross_links"][0], "engine");

    let delete = &parsed["mem_management"]["delete"];
    assert_eq!(delete[0]["pattern"], "exec-*");

    let links = &parsed["cross_mem_links"];
    assert_eq!(links["plugin"][0], "engine");
    assert_eq!(links["specs"], "*");

    assert_eq!(parsed["mutations"]["require_notes"], false);
}

#[test]
fn workspace_show_empty_workspace_reports_unset_sections() {
    let ws = seed_workspace();
    let output = memstead()
        .current_dir(ws.path())
        .args(["workspace", "show"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let body = String::from_utf8(output).unwrap();
    assert!(
        body.contains("no agent-driven mem creation allowed"),
        "got:\n{body}"
    );
    assert!(body.contains("default-deny"), "got:\n{body}");
    assert!(body.contains("`require_notes`: (unset"), "got:\n{body}");
}

/// The engine-semantically-owned sections of the workspace config can
/// be re-derived structurally end-to-end via the CLI:
///
/// > The engine-semantically-owned sections of `memstead/.memstead/workspace.toml`
/// > (`[[mem_management.*]]`, `[cross_mem_links]`, `[mutations]`,
/// > `schemas_dir`, `format`, `[persistence_adapter]`) can be re-derived
/// > structurally end-to-end by running the CLI commands in sequence
/// > against a fresh init.
///
/// This test stitches the seven commands together and re-derives the
/// shape that `memstead/.memstead/workspace.toml` would take, then loads the
/// result through `FileWorkspaceStore` to confirm the engine accepts
/// it. `[plugin.*]` is intentionally out of scope.
#[test]
fn end_to_end_rederive_loads_through_engine() {
    use memstead_base::{FileWorkspaceStore, WorkspaceStoreAdapter};
    let ws = seed_workspace();

    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "planning/plan-*",
            "--schema",
            "planning@0.1.0",
        ])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-delete", "planning/plan-*"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
        ])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "allow-delete", "exec-*"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "plugin", "engine"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "macos", "engine"])
        .assert()
        .success();
    memstead()
        .current_dir(ws.path())
        .args(["workspace", "set-mutations", "--require-notes", "true"])
        .assert()
        .success();

    let workspace = FileWorkspaceStore::new()
        .load(ws.path())
        .expect("re-derived workspace.toml must parse through the engine loader");
    assert_eq!(workspace.settings.mem_create_rules.len(), 2);
    assert_eq!(workspace.settings.mem_delete_rules.len(), 2);
    assert!(
        workspace
            .settings
            .mem_create_rules
            .iter()
            .any(|r| r.pattern == "planning/plan-*")
    );
    assert!(workspace.settings.cross_mem_links.contains_key("plugin"));
    assert!(workspace.settings.cross_mem_links.contains_key("macos"));
    assert_eq!(workspace.settings.mutations.require_notes, Some(true));
}

/// Markdown-default mode
/// renders a heading + bullet block matching every other CLI
/// mutation's shape (no more `workspace: <action> — <JSON>`
/// single-line shape). The pattern, schemas, position, and cross-link
/// defaults all surface as bullets.
#[test]
fn allow_create_markdown_default_renders_block_not_inline_json() {
    let ws = seed_workspace();
    let assertion = memstead()
        .current_dir(ws.path())
        .args([
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
        ])
        .assert()
        .success();
    let stdout = std::str::from_utf8(&assertion.get_output().stdout).unwrap();
    assert!(
        !stdout.contains("workspace: allow-create —"),
        "pre-fix single-line `workspace: action — JSON` shape must be gone; got:\n{stdout}",
    );
    assert!(
        stdout.contains("# Workspace allow-create rule `exec-*`"),
        "heading must name the action and the pattern; got:\n{stdout}",
    );
    assert!(
        stdout.contains("- Pattern: `exec-*`"),
        "pattern bullet must surface; got:\n{stdout}",
    );
    assert!(
        stdout.contains("- Schemas: [default@1.0.0]"),
        "schemas bullet must surface; got:\n{stdout}",
    );
    assert!(
        stdout.contains("- Position: appended"),
        "position bullet must surface; got:\n{stdout}",
    );
}

/// `grant-cross-link` follows the
/// same markdown-block shape across all seven subcommands.
#[test]
fn grant_cross_link_markdown_default_renders_block() {
    let ws = seed_workspace();
    let assertion = memstead()
        .current_dir(ws.path())
        .args(["workspace", "grant-cross-link", "test", "other"])
        .assert()
        .success();
    let stdout = std::str::from_utf8(&assertion.get_output().stdout).unwrap();
    assert!(
        !stdout.contains("workspace: grant-cross-link —"),
        "pre-fix shape must be gone; got:\n{stdout}",
    );
    assert!(
        stdout.contains("# Workspace grant-cross-link `test` → `other`"),
        "heading must name the grant direction; got:\n{stdout}",
    );
    assert!(
        stdout.contains("- From: `test`") && stdout.contains("- To: `other`"),
        "from/to bullets must surface; got:\n{stdout}",
    );
}

/// The `--json` envelope is unchanged: the `{action, detail}` shape every
/// script consumes today survives.
#[test]
fn allow_create_json_envelope_unchanged_post_fix() {
    let ws = seed_workspace();
    let assertion = memstead()
        .current_dir(ws.path())
        .args([
            "--json",
            "workspace",
            "allow-create",
            "exec-*",
            "--schema",
            "default@1.0.0",
        ])
        .assert()
        .success();
    let stdout = std::str::from_utf8(&assertion.get_output().stdout).unwrap();
    let parsed: serde_json::Value =
        serde_json::from_str(stdout.trim()).expect("--json must emit valid JSON");
    assert_eq!(parsed["action"], "allow-create");
    assert_eq!(parsed["detail"]["pattern"], "exec-*");
    assert_eq!(parsed["detail"]["schemas"][0], "default@1.0.0");
}