memstead-cli 0.2.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
//! `memstead link <scope/name>` — fetch a published mem from the
//! registry, cache it locally, and record the dependency in the filesystem
//! workspace config.
//!
//! Re-fetch is the refresh: invoking `memstead link <same-ref>` again
//! re-downloads the archive and overwrites the cached file. The
//! workspace-config dep entry is idempotent — `WorkspaceConfig::add_dep`
//! deduplicates on `==`, so repeated invocations do not accumulate
//! duplicate entries.
//!
//! Cache layout: `<workspace_root>/.memstead/memstead-io/<scope>/<name>.mem`.
//! The Tier 3 wiki-link resolver consumes the cached archive at this
//! exact path (criterion 7 of the plan, gated on filesystem engine
//! context).

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

use clap::Args;
use memstead_base::filesystem::config::{DepRef, read_workspace_config, write_workspace_config};
use serde_json::json;

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

/// `memstead link` arguments.
#[derive(Args, Debug)]
pub struct LinkArgs {
    /// Cross-mem dependency in `scope/name` form (no `@` prefix —
    /// that is the `memstead install` shape). Tier 3 wiki-links use the
    /// same form, so the input here matches what users will type
    /// inside `[[scope/name:slug]]`.
    #[arg(value_name = "SCOPE/NAME")]
    pub dep: String,

    /// Override the registry URL. Falls back to `MEMSTEAD_REGISTRY` then
    /// the default `https://memstead.io`.
    #[arg(long, value_name = "URL")]
    pub registry: Option<String>,

    /// Override the workspace root (the folder containing
    /// `.memstead/config.json`). When omitted, the command walks up from
    /// the current working directory.
    #[arg(long, value_name = "PATH")]
    pub workspace: Option<PathBuf>,
}

pub fn run(ctx: &CliContext, args: LinkArgs) -> anyhow::Result<()> {
    let dep: DepRef = args.dep.parse().map_err(|e: String| CliError {
        code: "INVALID_INPUT",
        message: format!(
            "invalid dependency reference {value:?}: {e} (expected 'scope/name')",
            value = args.dep
        ),
        kind: ExitKind::Validation,
        details: None,
    })?;

    let workspace_root = match args.workspace.clone() {
        Some(p) => {
            let canon = p.canonicalize().unwrap_or(p);
            if !memstead_base::is_workspace_root(&canon) {
                return Err(CliError {
                    code: "WORKSPACE_NOT_INITIALISED",
                    message: format!(
                        "no workspace at {} (missing .memstead/workspace.toml)",
                        canon.display()
                    ),
                    kind: ExitKind::NotFound,
                    details: None,
                }
                .into());
            }
            canon
        }
        None => find_filesystem_workspace_root()?,
    };

    let mut config = read_workspace_config(&workspace_root).map_err(|e| CliError {
        code: "WORKSPACE_CONFIG_READ_FAILED",
        message: format!("read workspace config: {e}"),
        kind: ExitKind::Generic,
        details: None,
    })?;

    let cache_dir = workspace_root
        .join(memstead_base::WORKSPACE_STORE_DIR)
        .join("memstead-io")
        .join(&dep.scope);
    std::fs::create_dir_all(&cache_dir).map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        message: format!("create cache dir {}: {e}", cache_dir.display()),
        kind: ExitKind::Generic,
        details: None,
    })?;
    let cache_path = cache_dir.join(format!(
        "{}.{}",
        dep.name,
        memstead_schema::ARCHIVE_EXTENSION
    ));

    let base = registry::registry_base(args.registry.as_deref());
    let client = registry::build_http()?;
    let bytes = registry::download_mem(&client, &base, &dep.scope, &dep.name, &cache_path)
        .map_err(|e| {
            let (msg, kind, code): (String, ExitKind, &'static str) = match e {
                DownloadError::NotFound => (
                    format!(
                        "registry has no mem {}/{} — check the spelling or `memstead publish` it first",
                        dep.scope, dep.name
                    ),
                    ExitKind::NotFound,
                    "REGISTRY_NOT_FOUND",
                ),
                DownloadError::Gone => (
                    format!(
                        "mem {}/{} has been unpublished from the registry",
                        dep.scope, dep.name
                    ),
                    ExitKind::NotFound,
                    "GONE",
                ),
                other => (
                    format!("download from registry: {other}"),
                    ExitKind::Generic,
                    "REGISTRY_ERROR",
                ),
            };
            CliError {
                code,
                message: msg,
                kind,
                details: None,
            }
        })?;

    let added = config.add_dep(dep.clone());
    write_workspace_config(&workspace_root, &config).map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        message: format!("update workspace config: {e}"),
        kind: ExitKind::Generic,
        details: None,
    })?;

    if ctx.json {
        let payload = json!({
            "scope": dep.scope,
            "name": dep.name,
            "cached_at": cache_path.display().to_string(),
            "bytes": bytes,
            "registry": base,
            "newly_recorded": added,
            "deps_total": config.deps.len(),
        });
        return print_json(&payload);
    }

    let action = if added { "Linked" } else { "Re-fetched" };
    let lines = [
        format!("# {} `{}`", action, dep.as_display()),
        String::new(),
        format!("- Cached:   `{}`", cache_path.display()),
        format!("- Bytes:    {bytes}"),
        format!("- Registry: {base}"),
        format!("- Total deps in this workspace: {}", config.deps.len()),
    ];
    print_markdown(&lines.join("\n"));
    Ok(())
}

/// Walk upward from `cwd` looking for the first ancestor that
/// carries `.memstead/workspace.toml` — the post-rebuild workspace
/// marker. Mirrors the resolver in `memstead-cli/src/setup.rs` and the
/// MCP binary's walker; keep them in sync.
fn find_filesystem_workspace_root() -> Result<PathBuf, CliError> {
    let cwd = std::env::current_dir().map_err(|e| CliError {
        code: crate::INTERNAL_CODE,
        message: format!("read cwd: {e}"),
        kind: ExitKind::Generic,
        details: None,
    })?;

    let mut current: &Path = &cwd;
    loop {
        if memstead_base::is_workspace_root(current) {
            return Ok(current.to_path_buf());
        }
        match current.parent() {
            Some(p) => current = p,
            None => {
                return Err(CliError {
                    code: "WORKSPACE_NOT_INITIALISED",
                    message: format!(
                        "no workspace found from {} or any ancestor (missing \
                         .memstead/workspace.toml) — run `memstead init` first or \
                         pass --workspace <path>",
                        cwd.display()
                    ),
                    kind: ExitKind::NotFound,
                    details: None,
                });
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use memstead_base::filesystem::config::{FILESYSTEM_WORKSPACE_FORMAT, WorkspaceConfig};
    use memstead_schema::SchemaRef;
    use tempfile::TempDir;

    fn write_minimal_workspace(tmp: &TempDir) {
        // Lay down the post-rebuild marker `.memstead/workspace.toml` so
        // the link command's walk-up resolves. The legacy
        // `.memstead/config.json` still holds the `deps` list and lands
        // alongside via `write_workspace_config`.
        let memstead_dir = tmp.path().join(".memstead");
        std::fs::create_dir_all(&memstead_dir).unwrap();
        std::fs::write(
            memstead_dir.join("workspace.toml"),
            "format = \"memstead-git-branch-2\"\n\n[persistence_adapter]\nname = \"file-two-layer\"\n",
        )
        .unwrap();
        let pin: SchemaRef = "default@1.0.0".parse().unwrap();
        let cfg = WorkspaceConfig::new("demo", pin);
        write_workspace_config(tmp.path(), &cfg).unwrap();
    }

    /// Spin up a tiny axum server that serves a single fixture archive
    /// at `/api/mem/<scope>/<name>.mem`. Returns the bound base
    /// URL (e.g. `http://127.0.0.1:54321`) and a `JoinHandle` the
    /// caller drops to shut the server down.
    async fn spawn_fixture_registry(
        scope: &'static str,
        name: &'static str,
        body: Vec<u8>,
    ) -> (String, tokio::task::JoinHandle<()>) {
        use axum::{Router, extract::Path as AxumPath, http::StatusCode, routing::get};
        use std::sync::Arc;

        let body = Arc::new(body);
        let app: Router = Router::new().route(
            "/api/mem/{scope_at}/{name_memstead}",
            get({
                let body = body.clone();
                move |AxumPath((scope_at, name_memstead)): AxumPath<(String, String)>| {
                    let body = body.clone();
                    async move {
                        let want_scope = scope.to_string();
                        let want_name = format!("{name}.mem");
                        if scope_at == want_scope && name_memstead == want_name {
                            (StatusCode::OK, (*body).clone())
                        } else {
                            (StatusCode::NOT_FOUND, vec![])
                        }
                    }
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let handle = tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });
        (format!("http://{addr}"), handle)
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn link_downloads_archive_and_records_dep() {
        let tmp = TempDir::new().unwrap();
        write_minimal_workspace(&tmp);

        let archive_bytes = b"fake-memstead-archive-bytes".to_vec();
        let (base, handle) =
            spawn_fixture_registry("anthropic", "core", archive_bytes.clone()).await;

        // Run on a blocking thread because `registry::download_mem`
        // is `reqwest::blocking`.
        let workspace = tmp.path().to_path_buf();
        let base_clone = base.clone();
        let result = tokio::task::spawn_blocking(move || {
            let ctx = CliContext {
                json: false,
                quiet: false,
            };
            run(
                &ctx,
                LinkArgs {
                    dep: "anthropic/core".to_string(),
                    registry: Some(base_clone),
                    workspace: Some(workspace),
                },
            )
        })
        .await
        .unwrap();
        handle.abort();
        result.unwrap();

        // Cached archive lands at the expected path with the expected bytes.
        let cache = tmp
            .path()
            .join(".memstead")
            .join("memstead-io")
            .join("anthropic")
            .join("core.mem");
        assert!(
            cache.is_file(),
            "cached archive must exist at {}",
            cache.display()
        );
        assert_eq!(std::fs::read(&cache).unwrap(), archive_bytes);

        // Workspace config records the dep.
        let cfg = read_workspace_config(tmp.path()).unwrap();
        assert_eq!(cfg.format, FILESYSTEM_WORKSPACE_FORMAT);
        assert_eq!(cfg.deps.len(), 1);
        assert_eq!(cfg.deps[0].as_display(), "anthropic/core");
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn link_is_idempotent_on_repeat() {
        let tmp = TempDir::new().unwrap();
        write_minimal_workspace(&tmp);

        let archive_bytes = b"v1".to_vec();
        let (base, handle) =
            spawn_fixture_registry("anthropic", "core", archive_bytes.clone()).await;

        let workspace = tmp.path().to_path_buf();
        let base_clone = base.clone();
        for _ in 0..2 {
            let workspace = workspace.clone();
            let base_clone = base_clone.clone();
            tokio::task::spawn_blocking(move || {
                let ctx = CliContext {
                    json: false,
                    quiet: false,
                };
                run(
                    &ctx,
                    LinkArgs {
                        dep: "anthropic/core".to_string(),
                        registry: Some(base_clone),
                        workspace: Some(workspace),
                    },
                )
                .unwrap();
            })
            .await
            .unwrap();
        }
        handle.abort();

        let cfg = read_workspace_config(tmp.path()).unwrap();
        assert_eq!(
            cfg.deps.len(),
            1,
            "repeated link must not duplicate the dep entry"
        );
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn link_404_is_typed_and_actionable() {
        // Server only knows about `anthropic/core`; we ask for a
        // different name and expect a `NotFound` exit code.
        let tmp = TempDir::new().unwrap();
        write_minimal_workspace(&tmp);

        let (base, handle) = spawn_fixture_registry("anthropic", "core", b"".to_vec()).await;
        let workspace = tmp.path().to_path_buf();
        let base_clone = base.clone();
        let err = tokio::task::spawn_blocking(move || {
            let ctx = CliContext {
                json: false,
                quiet: false,
            };
            run(
                &ctx,
                LinkArgs {
                    dep: "anthropic/missing".to_string(),
                    registry: Some(base_clone),
                    workspace: Some(workspace),
                },
            )
            .unwrap_err()
        })
        .await
        .unwrap();
        handle.abort();
        let msg = err.to_string();
        assert!(
            msg.contains("registry has no mem"),
            "expected actionable 404 message, got: {msg}"
        );
    }

    #[test]
    fn link_rejects_invalid_dep_ref() {
        let tmp = TempDir::new().unwrap();
        write_minimal_workspace(&tmp);
        let ctx = CliContext {
            json: false,
            quiet: false,
        };
        let err = run(
            &ctx,
            LinkArgs {
                dep: "not-a-scope-name".to_string(),
                registry: None,
                workspace: Some(tmp.path().to_path_buf()),
            },
        )
        .unwrap_err();
        assert!(err.to_string().contains("invalid dependency reference"));
    }

    #[test]
    fn link_rejects_missing_workspace() {
        let tmp = TempDir::new().unwrap();
        // No .memstead/workspace.toml under tmp.
        let ctx = CliContext {
            json: false,
            quiet: false,
        };
        let err = run(
            &ctx,
            LinkArgs {
                dep: "anthropic/core".to_string(),
                registry: None,
                workspace: Some(tmp.path().to_path_buf()),
            },
        )
        .unwrap_err();
        assert!(err.to_string().contains("no workspace"));
    }
}