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
#![cfg(feature = "mem-repo")]
//! The `open_questions` health axis (agent-trust plan 11): a composed
//! per-mem worklist of what the holding does not know — stubs,
//! recheck/unresolvable anchors, unsatisfied constraints, dangling
//! links, and a paired process mem's entries with negative findings
//! under the distinct already-searched heading. Composition only:
//! every count is asserted AGAINST the per-signal axis in the same
//! response, never against fixture constants — the no-disagreement
//! guarantee taken literally.

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

use assert_cmd::Command;
use serde_json::Value;
use tempfile::TempDir;

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

fn run_ok(root: &Path, args: &[&str]) -> Vec<u8> {
    memstead()
        .current_dir(root)
        .env("MEMSTEAD_OPERATOR_MODE", "1")
        .args(args)
        .assert()
        .success()
        .get_output()
        .stdout
        .clone()
}

fn json(bytes: &[u8]) -> Value {
    serde_json::from_slice(bytes).unwrap_or_else(|e| {
        panic!(
            "--json output must parse: {e}\n--- stdout ---\n{}\n--- end ---",
            String::from_utf8_lossy(bytes)
        )
    })
}

/// A schema package declaring a warn-tier `requires_when` constraint,
/// for the unsatisfied-constraint signal. No exemplar — also the
/// third-party-shape control.
fn write_constraint_schema(dir: &Path) {
    fs::create_dir_all(dir.join("types")).unwrap();
    fs::write(
        dir.join("schema.yaml"),
        r#"name: qcheck
version: 0.1.0
description: constraint fixture
when_to_use: tests
types:
  - item
relationships:
  mode: strict
  definitions:
    - name: PART_OF
      description: hier
      default_weight: 3.0
    - name: _default
      description: fallback
      default_weight: 1.0
community:
  resolution: 1.0
  seed: 42
"#,
    )
    .unwrap();
    fs::write(
        dir.join("types").join("item.yaml"),
        r#"name: item
description: t
when_to_use: tests
sections:
  - key: body
    heading: Body
    required: true
    search_weight: 10.0
    catch_all: true
    write_rules: []
metadata_fields:
  - key: status
    description: state
    field_type: string
    enum_values: [open, checked]
  - key: checked_by
    description: who checked
    field_type: string
title_weight: 100.0
text_fields:
  - body
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
  - title
  - body
health_required_fields:
  - body
staleness_threshold_days: 90
constraints:
  - kind: requires_when
    field: checked_by
    when_field: status
    when_value: checked
write_rules: []
"#,
    )
    .unwrap();
}

/// Build the five-signal + process-pairing workspace:
/// - `hold` (default schema): an anchored entity spanning all four
///   anchor states, and a body wiki-link to an absent target (one
///   stub + one dangling link).
/// - `qmem` (qcheck schema): one entity violating the requires_when
///   constraint.
/// - binding `hold/proc` (destination `hold`) with a MOUNTED process
///   mem `proc` (ingest schema) holding one coverage_gap and one
///   negative_finding; binding `hold/ghostproc` with NO mounted mem.
fn build_workspace(root: &Path) {
    run_ok(root, &["mem-repo", "init", "."]);
    run_ok(root, &["mem", "init", "hold", "--no-gitignore"]);

    // Anchored entity across the four states.
    run_ok(
        root,
        &[
            "create",
            "--mem",
            "hold",
            "--title",
            "Holder",
            "--type",
            "spec",
            "--section",
            "identity=Anchored fixture entity.",
            "--section",
            "purpose=Verification states.",
        ],
    );
    for (name, content) in [
        ("src-a.txt", "alpha"),
        ("src-b.txt", "beta"),
        ("src-c.txt", "gamma"),
        ("src-d.txt", "delta"),
    ] {
        fs::write(root.join(name), content).unwrap();
    }
    let h = |content: &str| memstead_base::anchor::prepared_content_hash(content.as_bytes());
    let anchors = [
        format!(
            r#"{{"artifact":"src-a.txt","grain":"file","class":"anchored","hash":"{}","hash_stability":"stable"}}"#,
            h("alpha")
        ),
        format!(
            r#"{{"artifact":"src-b.txt","grain":"file","class":"anchored","hash":"{}","hash_stability":"stable"}}"#,
            h("beta")
        ),
        format!(
            r#"{{"artifact":"src-c.txt","grain":"file","class":"anchored","hash":"{}","hash_stability":"unstable"}}"#,
            h("gamma")
        ),
        format!(
            r#"{{"artifact":"src-d.txt","grain":"file","class":"anchored","hash":"{}","hash_stability":"stable"}}"#,
            h("delta")
        ),
    ];
    let mut args: Vec<String> = vec![
        "update".into(),
        "hold--holder".into(),
        "--auto-hash".into(),
        "--append".into(),
        "purpose= Anchored. See [[hold--ghost-target]].".into(),
    ];
    for a in &anchors {
        args.push("--anchor".into());
        args.push(a.clone());
    }
    let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
    run_ok(root, &arg_refs);
    fs::write(root.join("src-b.txt"), "beta CHANGED").unwrap();
    fs::write(root.join("src-c.txt"), "gamma CHANGED").unwrap();
    fs::remove_file(root.join("src-d.txt")).unwrap();

    // Constraint-violating entity in its own mem.
    let pkg = root.join("qcheck-pkg");
    write_constraint_schema(&pkg);
    run_ok(root, &["schema", "install", pkg.to_str().unwrap()]);
    run_ok(
        root,
        &[
            "mem",
            "init",
            "qmem",
            "--schema",
            "qcheck@0.1.0",
            "--no-gitignore",
        ],
    );
    run_ok(
        root,
        &[
            "create",
            "--mem",
            "qmem",
            "--title",
            "Checked Without Checker",
            "--type",
            "item",
            "--section",
            "body=Violates requires_when.",
            "--metadata",
            "status=checked",
        ],
    );

    // Process pairing: binding hold/proc + mounted process mem `proc`.
    run_ok(
        root,
        &[
            "projection",
            "init",
            "--mem",
            "hold",
            "--source",
            "./src-a.txt",
            "--medium-type",
            "filesystem",
            "--name",
            "proc",
        ],
    );
    run_ok(
        root,
        &[
            "mem",
            "init",
            "proc",
            "--schema",
            "ingest@0.5.0",
            "--no-gitignore",
        ],
    );
    run_ok(
        root,
        &[
            "create",
            "--mem",
            "proc",
            "--title",
            "Uncovered corner",
            "--type",
            "coverage_gap",
            "--section",
            "area=A corner the destination lacks.",
            "--section",
            "evidence=src-a.txt paragraph two.",
        ],
    );
    run_ok(
        root,
        &[
            "create",
            "--mem",
            "proc",
            "--title",
            "No licensing note anywhere",
            "--type",
            "negative_finding",
            "--section",
            "sought=A licensing statement in the source.",
            "--section",
            "search_path=Full read of src-a.txt; grep for license.",
            "--section",
            "finding=Nothing — the source carries no licensing note.",
        ],
    );
    // Second binding with NO mounted process mem.
    run_ok(
        root,
        &[
            "projection",
            "init",
            "--mem",
            "hold",
            "--source",
            "./src-b.txt",
            "--medium-type",
            "filesystem",
            "--name",
            "ghostproc",
        ],
    );
}

#[test]
fn axis_composes_all_signals_and_matches_per_signal_axes() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();
    build_workspace(root);

    let out = json(&run_ok(
        root,
        &[
            "--json",
            "health",
            "--include",
            "open_questions,stubs,anchors,constraints,dangling_links",
        ],
    ));
    let axis = &out["open_questions"];
    assert_eq!(axis["_item_cap"], 20, "cap is stated: {axis}");

    // --- hold: stubs — count equals the stubs axis' hold entries.
    let hold = &axis["hold"];
    let stubs_axis_hold = out["stubs"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|s| s["id"].as_str().unwrap().starts_with("hold--"))
        .count() as u64;
    assert!(stubs_axis_hold >= 1, "fixture seeds a stub: {out}");
    assert_eq!(
        hold["stubs"]["count"].as_u64().unwrap(),
        stubs_axis_hold,
        "no-disagreement: stubs — {axis}"
    );
    assert_eq!(hold["stubs"]["items"][0]["kind"], "stub");

    // --- hold: anchors — counts equal the anchors axis' states.
    let anchors_hold = &out["anchors"]["hold"];
    assert_eq!(
        hold["anchors_recheck"]["count"], anchors_hold["recheck"],
        "no-disagreement: recheck — {axis} vs {anchors_hold}"
    );
    assert_eq!(
        hold["anchors_unresolvable"]["count"], anchors_hold["unresolvable"],
        "no-disagreement: unresolvable"
    );
    assert!(hold["anchors_recheck"]["count"].as_u64().unwrap() >= 1);
    assert!(hold["anchors_unresolvable"]["count"].as_u64().unwrap() >= 1);
    assert_eq!(
        hold["anchors_recheck"]["items"][0]["kind"],
        "anchor_recheck"
    );
    assert!(
        hold["anchors_recheck"]["items"][0]["id"]
            .as_str()
            .unwrap()
            .starts_with("hold--"),
        "items carry the hanging id"
    );

    // --- hold: dangling links — count equals the dangling_links axis.
    let dangling_axis_hold = out["dangling_links"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|d| d["from"].as_str().unwrap().starts_with("hold--"))
        .count() as u64;
    assert!(dangling_axis_hold >= 1, "fixture seeds a dangling link");
    assert_eq!(
        hold["dangling_links"]["count"].as_u64().unwrap(),
        dangling_axis_hold,
        "no-disagreement: dangling links"
    );

    // --- qmem: unsatisfied constraints — count equals the
    // constraints axis' qmem entries.
    let qmem = &axis["qmem"];
    let constraints_axis_qmem = out["constraints"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|c| c["mem"] == "qmem")
        .count() as u64;
    assert!(
        constraints_axis_qmem >= 1,
        "fixture seeds a violation: {out}"
    );
    assert_eq!(
        qmem["unsatisfied_constraints"]["count"].as_u64().unwrap(),
        constraints_axis_qmem,
        "no-disagreement: constraints"
    );
    assert_eq!(
        qmem["unsatisfied_constraints"]["items"][0]["kind"],
        "unsatisfied_constraint"
    );

    // --- process pairing (criterion 2): the mounted process mem's
    // entries appear, negative finding under the DISTINCT heading;
    // the unmounted binding is stated unresolvable — never an error.
    let process = hold["process"].as_array().expect("process section");
    let proc = process
        .iter()
        .find(|p| p["binding"] == "proc")
        .expect("mounted binding present");
    assert_eq!(proc["resolvable"], true);
    let open = serde_json::to_string(&proc["open_entries"]).unwrap();
    assert!(
        open.contains("coverage_gap") && open.contains("uncovered-corner"),
        "coverage gap under open work: {open}"
    );
    assert!(
        !open.contains("negative_finding"),
        "negative findings must NOT be in the todo pile: {open}"
    );
    let searched = serde_json::to_string(&proc["already_searched"]).unwrap();
    assert!(
        searched.contains("negative_finding") && searched.contains("no-licensing-note"),
        "negative finding under already_searched: {searched}"
    );
    let ghost = process
        .iter()
        .find(|p| p["binding"] == "ghostproc")
        .expect("unmounted binding stated");
    assert_eq!(ghost["resolvable"], false, "{ghost}");
    assert!(ghost.get("open_entries").is_none());

    // --- a hole-free mem reports an empty axis entry, not an error.
    let proc_entry = &axis["proc"];
    assert_eq!(proc_entry["stubs"]["count"], 0);
    assert_eq!(proc_entry["dangling_links"]["count"], 0);

    // --- without the include: no axis key at all.
    let plain = json(&run_ok(root, &["--json", "health"]));
    assert!(
        plain.get("open_questions").is_none(),
        "health without the include must not carry the axis"
    );
}

/// The item cap triggers with an explicit remainder count — silent
/// truncation is the anti-pattern.
#[test]
fn item_cap_truncates_with_explicit_remainder() {
    let tmp = TempDir::new().unwrap();
    let root = tmp.path();
    run_ok(root, &["mem-repo", "init", "."]);
    run_ok(root, &["mem", "init", "hold", "--no-gitignore"]);

    // One entity whose body wiki-links 25 absent targets → 25 stubs.
    let links: String = (0..25)
        .map(|i| format!("[[hold--ghost-{i}]]"))
        .collect::<Vec<_>>()
        .join(" ");
    run_ok(
        root,
        &[
            "create",
            "--mem",
            "hold",
            "--title",
            "Linker",
            "--type",
            "spec",
            "--section",
            &format!("identity=Links: {links}"),
            "--section",
            "purpose=Cap fixture.",
        ],
    );

    let out = json(&run_ok(
        root,
        &["--json", "health", "--include", "open_questions"],
    ));
    let stubs = &out["open_questions"]["hold"]["stubs"];
    assert_eq!(stubs["count"], 25, "{out}");
    assert_eq!(stubs["items"].as_array().unwrap().len(), 20, "capped at 20");
    assert_eq!(stubs["more"], 5, "explicit remainder");
}