mx 0.1.132

A Swiss army knife for Claude Code and multi-agent toolkits
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
//! Centralized path resolution for mx CLI
//!
//! All paths the application needs derive from `mx_home()`. The base directory
//! is determined once per process (via `OnceLock`) using this priority:
//!
//! 1. `MX_HOME` environment variable (explicit override)
//! 2. Fallback: `~/.mx/`
//!
//! Subsystem-specific overrides (`MX_CODEX_PATH`, `MX_SURREAL_ROOT`, etc.)
//! continue to work -- they take precedence over the derived path when set.
//!
//! Per-file env-var overrides (`MX_KV_SCHEMA`, `MX_KV_DATA`) survive too --
//! but they are resolved at the call site, not here, because they may include
//! `{agent}` placeholders.

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

static MX_HOME: OnceLock<PathBuf> = OnceLock::new();

/// Pure resolution logic for MX_HOME. Takes the env var value as a parameter
/// so callers (especially tests) don't need to touch process state.
fn resolve_mx_home_with(env_val: Option<&str>) -> PathBuf {
    if let Some(val) = env_val
        && !val.is_empty()
    {
        return PathBuf::from(val);
    }
    dirs::home_dir()
        .expect("Could not determine home directory")
        .join(".mx")
}

/// Resolve the MX_HOME base directory.
///
/// Priority: `MX_HOME` env var > `~/.mx/`
/// Result is cached for the lifetime of the process.
pub fn mx_home() -> &'static PathBuf {
    MX_HOME.get_or_init(|| resolve_mx_home_with(std::env::var("MX_HOME").ok().as_deref()))
}

/// Pure detection of the legacy `MX_MEMORY_PATH` env var.
///
/// Returns true when a non-empty value is present, signalling that a startup
/// note should be emitted to alert the user that the variable was renamed.
///
/// TODO(memory-path-rename-note): remove this detection after one release cycle.
pub(crate) fn legacy_memory_path_set(env_val: Option<&str>) -> bool {
    env_val.map(|v| !v.is_empty()).unwrap_or(false)
}

/// Emit a one-line stderr note when the deprecated `MX_MEMORY_PATH`
/// environment variable is set, telling the user it was renamed to
/// `MX_SURREAL_ROOT`.
///
/// This is the only startup note: the previous "Using default $MX_HOME"
/// message was removed because it fired on every invocation for users who
/// hadn't customized anything (i.e. most users) and wasn't part of the
/// decision-9 spec. A future verbose-path-debugging mode would belong on
/// its own ticket.
///
/// TODO(memory-path-rename-note): remove this detection+warning after one
/// release cycle.
pub fn emit_legacy_memory_path_note() {
    if legacy_memory_path_set(std::env::var("MX_MEMORY_PATH").ok().as_deref()) {
        eprintln!(
            "note: `MX_MEMORY_PATH` is no longer used. \
             It was renamed to `MX_SURREAL_ROOT`. Update your environment."
        );
    }
}

// ---------------------------------------------------------------------------
// Derived paths -- every path the codebase needs lives here
// ---------------------------------------------------------------------------

/// Swap directory: `$MX_HOME/swap/`
pub fn swap_dir() -> PathBuf {
    mx_home().join("swap")
}

/// Sync cache directory for a specific repo: `$MX_HOME/cache/sync/<repo-slug>/`
pub fn sync_cache_dir(repo: &str) -> PathBuf {
    let repo_slug = repo.replace('/', "-");
    mx_home().join("cache").join("sync").join(repo_slug)
}

/// Artifacts directory: `$MX_HOME/artifacts/`
pub fn artifacts_dir() -> PathBuf {
    mx_home().join("artifacts")
}

// ---------------------------------------------------------------------------
// kv (decision 1)
// ---------------------------------------------------------------------------

/// `$MX_HOME/kv/schema/`
pub fn kv_schema_dir() -> PathBuf {
    mx_home().join("kv").join("schema")
}

/// `$MX_HOME/kv/data/`
pub fn kv_data_dir() -> PathBuf {
    mx_home().join("kv").join("data")
}

/// `$MX_HOME/kv/schema/{agent}.toml`
pub fn kv_schema_path(agent: &str) -> PathBuf {
    kv_schema_dir().join(format!("{}.toml", agent))
}

/// `$MX_HOME/kv/data/{agent}.json`
pub fn kv_data_path(agent: &str) -> PathBuf {
    kv_data_dir().join(format!("{}.json", agent))
}

/// Legacy `~/.crewu/kv/{agent}.schema.toml` -- soft fallback only.
///
/// TODO(kv-path-migration): remove after one release cycle.
pub fn legacy_crewu_kv_schema_path(agent: &str) -> Option<PathBuf> {
    dirs::home_dir().map(|h| {
        h.join(".crewu")
            .join("kv")
            .join(format!("{}.schema.toml", agent))
    })
}

/// Legacy `~/.crewu/kv/{agent}.data.json` -- soft fallback only.
///
/// TODO(kv-path-migration): remove after one release cycle.
pub fn legacy_crewu_kv_data_path(agent: &str) -> Option<PathBuf> {
    dirs::home_dir().map(|h| {
        h.join(".crewu")
            .join("kv")
            .join(format!("{}.data.json", agent))
    })
}

// ---------------------------------------------------------------------------
// state / tensor schemas (decision 5)
// ---------------------------------------------------------------------------

/// `$MX_HOME/state/schemas/`
pub fn state_schemas_dir() -> PathBuf {
    mx_home().join("state").join("schemas")
}

/// `$MX_HOME/state/schemas/{id}.yaml`
pub fn tensor_schema_path(id: &str) -> PathBuf {
    state_schemas_dir().join(format!("{}.yaml", id))
}

// ---------------------------------------------------------------------------
// memory (decisions 6, 7, 9)
// ---------------------------------------------------------------------------

fn surreal_root_with(env_val: Option<&str>, home: &Path) -> PathBuf {
    if let Some(path) = env_val
        && !path.is_empty()
    {
        return PathBuf::from(path);
    }
    home.join("memory").join("surreal")
}

/// SurrealDB store root.
///
/// Override: `MX_SURREAL_ROOT` env var.
/// Default: `$MX_HOME/memory/surreal/`
pub fn surreal_root() -> PathBuf {
    surreal_root_with(std::env::var("MX_SURREAL_ROOT").ok().as_deref(), mx_home())
}

/// `$MX_HOME/memory/seed/agents/`
pub fn memory_seed_agents_dir() -> PathBuf {
    mx_home().join("memory").join("seed").join("agents")
}

/// `$MX_HOME/memory/seed/knowledge/`
pub fn memory_seed_knowledge_dir() -> PathBuf {
    mx_home().join("memory").join("seed").join("knowledge")
}

/// Pure resolution of the legacy agents directory layout. Takes `home`
/// explicitly so tests don't need to touch process state -- mirrors the
/// `surreal_root_with` / `fastembed_cache_dir_with` pattern.
fn legacy_agents_dir_with(home: &Path) -> PathBuf {
    home.join("agents")
}

/// Legacy `$MX_HOME/agents/` -- soft fallback only.
///
/// TODO(memory-seed-agents-migration): remove after one release cycle.
pub fn legacy_agents_dir() -> PathBuf {
    legacy_agents_dir_with(mx_home())
}

/// Legacy `$MX_HOME/memory/index.jsonl` -- soft fallback only.
///
/// TODO(memory-seed-knowledge-migration): remove after one release cycle.
pub fn legacy_memory_index_jsonl() -> PathBuf {
    mx_home().join("memory").join("index.jsonl")
}

// ---------------------------------------------------------------------------
// FastEmbed cache (decision 4)
// ---------------------------------------------------------------------------

fn fastembed_cache_dir_with(
    isolate_env: Option<&str>,
    cache_dir_fn: impl FnOnce() -> Option<PathBuf>,
    home: &Path,
) -> PathBuf {
    if isolate_env.map(|v| !v.is_empty()).unwrap_or(false) {
        return home.join("memory").join("embed");
    }
    cache_dir_fn()
        .map(|d| d.join("fastembed"))
        .unwrap_or_else(|| PathBuf::from(".fastembed_cache"))
}

/// FastEmbed model cache directory.
///
/// - Default: `$XDG_CACHE_HOME/fastembed/` (shared across tools)
/// - If `MX_ISOLATE_FASTEMBED` is set: `$MX_HOME/memory/embed/`
pub fn fastembed_cache_dir() -> PathBuf {
    fastembed_cache_dir_with(
        std::env::var("MX_ISOLATE_FASTEMBED").ok().as_deref(),
        dirs::cache_dir,
        mx_home(),
    )
}

// ---------------------------------------------------------------------------
// Codex (existing)
// ---------------------------------------------------------------------------

/// Pure resolution logic for codex directory. Takes the `MX_CODEX_PATH` env
/// var value as a parameter so callers (especially tests) don't need to touch
/// process state.
fn codex_dir_with(env_val: Option<&str>, home: &Path) -> PathBuf {
    if let Some(path) = env_val
        && !path.is_empty()
    {
        return PathBuf::from(path);
    }
    home.join("codex")
}

/// Codex directory (session archives).
///
/// Override: `MX_CODEX_PATH` env var.
/// Default: `$MX_HOME/codex/`
pub fn codex_dir() -> PathBuf {
    codex_dir_with(std::env::var("MX_CODEX_PATH").ok().as_deref(), mx_home())
}

// ---------------------------------------------------------------------------
// External Claude data (read-only ingest sources, owned by another tool)
// ---------------------------------------------------------------------------

/// `~/.claude/projects/` -- read-only by convention.
pub fn claude_projects_dir() -> PathBuf {
    dirs::home_dir()
        .expect("Could not determine home directory")
        .join(".claude")
        .join("projects")
}

/// `~/.claude.json` -- read-only by convention.
pub fn claude_config_path() -> PathBuf {
    dirs::home_dir()
        .expect("Could not determine home directory")
        .join(".claude.json")
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // Tests call the `_with` variants directly with explicit parameters,
    // avoiding any env-var mutation and running safely in parallel.

    #[test]
    fn mx_home_default_when_unset() {
        let result = resolve_mx_home_with(None);
        let expected = dirs::home_dir().unwrap().join(".mx");
        assert_eq!(result, expected);
    }

    #[test]
    fn mx_home_respects_env_var() {
        let result = resolve_mx_home_with(Some("/tmp/test-mx-home"));
        assert_eq!(result, PathBuf::from("/tmp/test-mx-home"));
    }

    #[test]
    fn mx_home_empty_env_is_default() {
        let result = resolve_mx_home_with(Some(""));
        let expected = dirs::home_dir().unwrap().join(".mx");
        assert_eq!(result, expected);
    }

    #[test]
    fn derived_dirs_under_mx_home() {
        // Test real derived-path functions against the cached mx_home().
        // Each function should return a path rooted under mx_home().
        let home = mx_home();

        let swap = swap_dir();
        assert!(swap.starts_with(home), "swap_dir not under mx_home");
        assert_eq!(swap.file_name().unwrap(), "swap");

        let artifacts = artifacts_dir();
        assert!(
            artifacts.starts_with(home),
            "artifacts_dir not under mx_home"
        );
        assert_eq!(artifacts.file_name().unwrap(), "artifacts");

        // codex_dir without override should also be under mx_home
        let codex = codex_dir_with(None, home);
        assert!(codex.starts_with(home), "codex_dir not under mx_home");
        assert_eq!(codex.file_name().unwrap(), "codex");

        let sync = sync_cache_dir("owner/repo");
        assert!(sync.starts_with(home), "sync_cache_dir not under mx_home");
    }

    #[test]
    fn codex_dir_respects_override() {
        let home = mx_home().clone();
        let result = codex_dir_with(Some("/custom/codex"), &home);
        assert_eq!(result, PathBuf::from("/custom/codex"));
    }

    #[test]
    fn codex_dir_empty_override_is_default() {
        let home = mx_home().clone();
        let result = codex_dir_with(Some(""), &home);
        assert_eq!(result, home.join("codex"));
    }

    #[test]
    fn codex_dir_none_override_is_default() {
        let home = mx_home().clone();
        let result = codex_dir_with(None, &home);
        assert_eq!(result, home.join("codex"));
    }

    #[test]
    fn swap_dir_is_under_mx_home() {
        let swap = swap_dir();
        assert!(swap.starts_with(mx_home()));
    }

    #[test]
    fn sync_cache_dir_slugifies_repo() {
        let dir = sync_cache_dir("owner/repo");
        // Should contain "owner-repo" not "owner/repo"
        assert!(dir.to_string_lossy().contains("owner-repo"));
        assert!(dir.starts_with(mx_home()));
    }

    // ---------------------------------------------------------------------
    // kv helpers
    // ---------------------------------------------------------------------

    #[test]
    fn kv_helpers_under_mx_home() {
        let home = mx_home();
        assert!(kv_schema_dir().starts_with(home));
        assert!(kv_data_dir().starts_with(home));
        assert!(kv_schema_path("smith").ends_with("kv/schema/smith.toml"));
        assert!(kv_data_path("smith").ends_with("kv/data/smith.json"));
    }

    // ---------------------------------------------------------------------
    // state / tensor schema helpers
    // ---------------------------------------------------------------------

    #[test]
    fn tensor_schema_path_layout() {
        let p = tensor_schema_path("crewu");
        assert!(p.ends_with("state/schemas/crewu.yaml"));
        assert!(p.starts_with(mx_home()));
    }

    // ---------------------------------------------------------------------
    // memory / surreal helpers
    // ---------------------------------------------------------------------

    #[test]
    fn surreal_root_default() {
        let home = mx_home().clone();
        let r = surreal_root_with(None, &home);
        assert_eq!(r, home.join("memory").join("surreal"));
    }

    #[test]
    fn surreal_root_respects_override() {
        let home = mx_home().clone();
        let r = surreal_root_with(Some("/custom/surreal"), &home);
        assert_eq!(r, PathBuf::from("/custom/surreal"));
    }

    #[test]
    fn surreal_root_empty_override_is_default() {
        let home = mx_home().clone();
        let r = surreal_root_with(Some(""), &home);
        assert_eq!(r, home.join("memory").join("surreal"));
    }

    #[test]
    fn memory_seed_dirs_under_mx_home() {
        let home = mx_home();
        assert!(memory_seed_agents_dir().starts_with(home));
        assert!(memory_seed_agents_dir().ends_with("memory/seed/agents"));
        assert!(memory_seed_knowledge_dir().starts_with(home));
        assert!(memory_seed_knowledge_dir().ends_with("memory/seed/knowledge"));
    }

    #[test]
    fn legacy_agents_dir_with_uses_supplied_home() {
        let home = PathBuf::from("/tmp/some-test-home");
        let r = legacy_agents_dir_with(&home);
        assert_eq!(r, home.join("agents"));
    }

    #[test]
    fn legacy_agents_dir_public_wrapper_matches_seam() {
        // The public wrapper should produce the same path as calling the
        // _with helper against the cached mx_home().
        assert_eq!(legacy_agents_dir(), legacy_agents_dir_with(mx_home()));
    }

    // ---------------------------------------------------------------------
    // fastembed cache
    // ---------------------------------------------------------------------

    #[test]
    fn fastembed_cache_default_uses_xdg() {
        let home = mx_home().clone();
        let xdg = PathBuf::from("/xdg/cache");
        let r = fastembed_cache_dir_with(None, || Some(xdg.clone()), &home);
        assert_eq!(r, xdg.join("fastembed"));
    }

    #[test]
    fn fastembed_cache_isolate_uses_mx_home() {
        let home = mx_home().clone();
        let xdg = PathBuf::from("/xdg/cache");
        let r = fastembed_cache_dir_with(Some("1"), || Some(xdg.clone()), &home);
        assert_eq!(r, home.join("memory").join("embed"));
    }

    #[test]
    fn fastembed_cache_isolate_empty_is_default() {
        let home = mx_home().clone();
        let xdg = PathBuf::from("/xdg/cache");
        let r = fastembed_cache_dir_with(Some(""), || Some(xdg.clone()), &home);
        assert_eq!(r, xdg.join("fastembed"));
    }

    #[test]
    fn fastembed_cache_no_xdg_falls_back() {
        let home = mx_home().clone();
        let r = fastembed_cache_dir_with(None, || None, &home);
        assert_eq!(r, PathBuf::from(".fastembed_cache"));
    }

    // ---------------------------------------------------------------------
    // claude external paths
    // ---------------------------------------------------------------------

    #[test]
    fn claude_paths_under_home() {
        let home = dirs::home_dir().unwrap();
        assert_eq!(claude_projects_dir(), home.join(".claude").join("projects"));
        assert_eq!(claude_config_path(), home.join(".claude.json"));
    }

    // -----------------------------------------------------------------
    // legacy_memory_path_set -- decision 9 (MX_MEMORY_PATH rename)
    // -----------------------------------------------------------------

    #[test]
    fn legacy_memory_path_unset_returns_false() {
        assert!(!legacy_memory_path_set(None));
    }

    #[test]
    fn legacy_memory_path_empty_returns_false() {
        assert!(!legacy_memory_path_set(Some("")));
    }

    #[test]
    fn legacy_memory_path_set_returns_true() {
        assert!(legacy_memory_path_set(Some("/anything")));
    }
}