engram-memory 0.6.0

Core binary and shared infrastructure for Engram memory system
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
use std::path::PathBuf;
use std::sync::Mutex;

use tempfile::tempdir;

use engram_core::init_handler;

// Serializes tests that mutate process-global env vars (ENGRAM_MCP_SERVER_PATH,
// ENGRAM_BIN, HOME). Documented as acceptable minor risk in plan.
static ENV_LOCK: Mutex<()> = Mutex::new(());

#[test]
fn init_creates_config_file() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let result = init_handler::execute_with_dirs(project.path(), home.path());
    assert!(result.is_ok(), "execute should succeed: {result:?}");

    let config_path = home.path().join(".engram").join("engram.toml");
    assert!(
        config_path.exists(),
        "home config file should exist at {config_path:?}"
    );
    let content = std::fs::read_to_string(&config_path).expect("should read config file");
    // [database] block intentionally absent — database.path is resolved from
    // per-project layout and the field is optional; see ADR
    // 2026-05-13-config-optional-database-and-socket-fallbacks.
    assert!(content.contains("[embedding]"));
    assert!(content.contains("[llm]"));
    assert!(content.contains("[server]"));
    assert!(content.contains("[hnsw]"));
    assert!(content.contains("[consolidation]"));
}

#[test]
fn init_creates_database() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let result = init_handler::execute_with_dirs(project.path(), home.path());
    assert!(result.is_ok(), "execute should succeed: {result:?}");
    let database_path = project.path().join(".engram").join("engram.db");
    assert!(
        database_path.exists(),
        "project database should exist at {database_path:?}"
    );
}

#[test]
fn init_socket_path_too_long() {
    // Path length validation is a pure string check; physical directories need not exist.
    let home = tempdir().expect("home");
    let root = tempdir().expect("project root");
    let mut deep: PathBuf = root.path().to_path_buf();
    // 30 segments of 10 chars + separators guarantees the socket path exceeds 104 bytes.
    let segment = "abcdefghij";
    for _ in 0..30 {
        deep.push(segment);
    }

    let result = init_handler::execute_with_dirs(&deep, home.path());
    let error = result.expect_err("should fail due to socket path length");
    let message = error.to_string();
    assert!(
        message.contains("[6012]") && message.to_lowercase().contains("socket"),
        "expected socket length error, got: {message}"
    );
}

#[test]
fn gitignore_idempotent_empty() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");
    let gitignore_path = project.path().join(".gitignore");
    let content = std::fs::read_to_string(&gitignore_path).expect("gitignore exists");
    assert!(content.contains(".engram/"), "marker missing: {content:?}");
}

#[test]
fn gitignore_append_when_missing_marker() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let gitignore_path = project.path().join(".gitignore");
    std::fs::write(&gitignore_path, "node_modules\n").expect("seed gitignore");

    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let content = std::fs::read_to_string(&gitignore_path).expect("gitignore exists");
    assert!(
        content.contains("node_modules"),
        "original content retained"
    );
    assert!(content.contains(".engram/"), "marker appended");
}

#[test]
fn gitignore_noop_when_marker_present() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let gitignore_path = project.path().join(".gitignore");
    let original = "node_modules\n.engram/\nother\n";
    std::fs::write(&gitignore_path, original).expect("seed gitignore");

    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let content = std::fs::read_to_string(&gitignore_path).expect("gitignore exists");
    assert_eq!(content, original, "gitignore must not be modified");
}

#[test]
fn mcp_json_with_node_path() {
    let _lock = ENV_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let script = tempdir().expect("script tempdir");
    let script_path = script.path().join("index.js");
    std::fs::write(&script_path, b"// stub").expect("write stub script");
    let bin_tempdir = tempdir().expect("bin tempdir");
    let bin_path = bin_tempdir.path().join("engram");
    std::fs::write(&bin_path, b"#!/bin/sh\n").expect("stub bin");

    let original_bin = std::env::var("ENGRAM_BIN").ok();
    // SAFETY: serialized via ENV_LOCK; restored before returning.
    unsafe {
        std::env::set_var("ENGRAM_BIN", &bin_path);
        std::env::set_var("ENGRAM_MCP_SERVER_PATH", &script_path);
    }
    let result = init_handler::execute_with_dirs(project.path(), home.path());
    unsafe {
        std::env::remove_var("ENGRAM_MCP_SERVER_PATH");
        match original_bin {
            Some(value) => std::env::set_var("ENGRAM_BIN", value),
            None => std::env::remove_var("ENGRAM_BIN"),
        }
    }
    assert!(result.is_ok(), "init should succeed: {result:?}");

    let mcp_path = project.path().join(".mcp.json");
    let content = std::fs::read_to_string(&mcp_path).expect("mcp json");
    let parsed: serde_json::Value = serde_json::from_str(&content).expect("valid json");
    let command = parsed["mcpServers"]["engram"]["command"]
        .as_str()
        .expect("command is string");
    assert_eq!(command, "node");
    let args = parsed["mcpServers"]["engram"]["args"]
        .as_array()
        .expect("args array");
    assert_eq!(args.len(), 1);
    let canonical = script_path
        .canonicalize()
        .map(|path| path.to_string_lossy().to_string())
        .unwrap_or_else(|_| script_path.to_string_lossy().to_string());
    let arg_value = args[0].as_str().expect("arg string").to_string();
    assert!(
        arg_value == canonical || arg_value == script_path.to_string_lossy(),
        "arg={arg_value}, canonical={canonical}"
    );
    let env_block = parsed["mcpServers"]["engram"]["env"]
        .as_object()
        .expect("env block must be present when ENGRAM_BIN is resolved");
    let engram_bin_value = env_block
        .get("ENGRAM_BIN")
        .and_then(|value| value.as_str())
        .expect("ENGRAM_BIN must be a string");
    let canonical_bin = bin_path
        .canonicalize()
        .map(|path| path.to_string_lossy().to_string())
        .unwrap_or_else(|_| bin_path.to_string_lossy().to_string());
    assert_eq!(
        engram_bin_value, canonical_bin,
        "ENGRAM_BIN env must match the resolved absolute binary path"
    );
}

#[test]
fn mcp_json_fallback_to_npx() {
    let _lock = ENV_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let isolated = tempdir().expect("isolated bin");
    let bin_path = isolated.path().join("engram");
    std::fs::write(&bin_path, b"#!/bin/sh\n").expect("stub bin");

    let original_bin = std::env::var("ENGRAM_BIN").ok();
    // SAFETY: serialized via ENV_LOCK; restored before returning.
    unsafe {
        std::env::remove_var("ENGRAM_MCP_SERVER_PATH");
        std::env::set_var("ENGRAM_BIN", &bin_path);
    }
    let result = init_handler::execute_with_dirs(project.path(), home.path());
    unsafe {
        match original_bin {
            Some(value) => std::env::set_var("ENGRAM_BIN", value),
            None => std::env::remove_var("ENGRAM_BIN"),
        }
    }
    assert!(result.is_ok(), "init should succeed: {result:?}");

    let mcp_path = project.path().join(".mcp.json");
    let content = std::fs::read_to_string(&mcp_path).expect("mcp json");
    let parsed: serde_json::Value = serde_json::from_str(&content).expect("valid json");
    let command = parsed["mcpServers"]["engram"]["command"]
        .as_str()
        .expect("command is string");
    assert_eq!(command, "npx");
    let args = parsed["mcpServers"]["engram"]["args"]
        .as_array()
        .expect("args array");
    assert_eq!(args.len(), 1);
    assert_eq!(args[0].as_str().unwrap(), "@engramm/engram-mcp-server");
    let env_block = parsed["mcpServers"]["engram"]["env"]
        .as_object()
        .expect("env block must be present when ENGRAM_BIN is set");
    let engram_bin_value = env_block
        .get("ENGRAM_BIN")
        .and_then(|value| value.as_str())
        .expect("ENGRAM_BIN must be a string");
    let canonical_bin = bin_path
        .canonicalize()
        .map(|path| path.to_string_lossy().to_string())
        .unwrap_or_else(|_| bin_path.to_string_lossy().to_string());
    assert_eq!(
        engram_bin_value, canonical_bin,
        "ENGRAM_BIN env must match the resolved absolute binary path"
    );
}

#[test]
fn init_execute_fails_when_home_not_set() {
    let _lock = ENV_LOCK
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());

    // The public `execute()` wrapper must surface a clear error when HOME is missing,
    // so the underlying invariant (resolve cwd + HOME, then delegate) is verified.
    let original_home = std::env::var("HOME").ok();
    // SAFETY: serialized via ENV_LOCK; restored before returning.
    unsafe {
        std::env::remove_var("HOME");
    }
    let result = init_handler::execute();
    unsafe {
        if let Some(value) = original_home {
            std::env::set_var("HOME", value);
        }
    }

    let error = result.expect_err("execute() must fail without HOME");
    let message = error.to_string();
    assert!(
        message.contains("[6012]") && message.contains("HOME"),
        "expected HOME-related init failure, got: {message}"
    );
}

#[test]
fn gitignore_appends_after_missing_trailing_newline() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let gitignore_path = project.path().join(".gitignore");
    std::fs::write(&gitignore_path, "*.log").expect("seed gitignore without trailing newline");

    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let content = std::fs::read_to_string(&gitignore_path).expect("gitignore exists");
    assert_eq!(
        content, "*.log\n.engram/\n",
        "marker should follow an inserted newline, existing content preserved"
    );
}

#[test]
fn gitignore_empty_file() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let gitignore_path = project.path().join(".gitignore");
    std::fs::write(&gitignore_path, "").expect("seed empty gitignore");

    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let content = std::fs::read_to_string(&gitignore_path).expect("gitignore exists");
    assert!(
        content.contains(".engram/"),
        "marker must be present in seeded empty file: {content:?}"
    );
}

// Socket path layout: <project_dir>/.engram/engram.sock — exactly 20 bytes
// for the relative tail (including the leading separator). The UNIX limit
// is 104 bytes inclusive; `validate_socket_path` rejects at length >= 104.
const SOCKET_RELATIVE_TAIL_BYTES: usize = "/.engram/engram.sock".len();

fn build_project_dir_of_length(target_len: usize) -> (tempfile::TempDir, PathBuf) {
    let root = tempdir().expect("project root tempdir");
    let root_len = root.path().as_os_str().len();
    assert!(
        target_len > root_len,
        "tempdir path ({root_len} bytes) already longer than target {target_len}"
    );
    // A single separator already sits between root and the padding segment.
    let remaining = target_len - root_len - 1;
    let padding: String = "a".repeat(remaining);
    let deep = root.path().join(&padding);
    std::fs::create_dir_all(&deep).expect("create padded project dir");
    assert_eq!(
        deep.as_os_str().len(),
        target_len,
        "project dir length must equal target"
    );
    (root, deep)
}

#[test]
fn init_socket_path_boundary_103_bytes() {
    // project_dir length such that socket path = 103 bytes (allowed).
    let target = 103 - SOCKET_RELATIVE_TAIL_BYTES;
    let (_root, project_dir) = build_project_dir_of_length(target);
    let home = tempdir().expect("home");

    let result = init_handler::execute_with_dirs(&project_dir, home.path());
    assert!(
        result.is_ok(),
        "socket path of 103 bytes must succeed: {result:?}"
    );
}

#[test]
fn init_socket_path_boundary_104_bytes() {
    // project_dir length such that socket path = 104 bytes (rejected).
    let target = 104 - SOCKET_RELATIVE_TAIL_BYTES;
    let (_root, project_dir) = build_project_dir_of_length(target);
    let home = tempdir().expect("home");

    let result = init_handler::execute_with_dirs(&project_dir, home.path());
    let error = result.expect_err("socket path of 104 bytes must fail");
    let message = error.to_string();
    assert!(
        message.contains("104"),
        "error message must cite the 104-byte UNIX limit, got: {message}"
    );
    assert!(
        message.contains("[6012]") && message.to_lowercase().contains("socket"),
        "expected socket length error, got: {message}"
    );
}

#[test]
fn init_writes_project_local_config() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let project_config = project.path().join(".engram").join("engram.toml");
    assert!(
        project_config.exists(),
        "project-local config should exist at {project_config:?}"
    );
    let content = std::fs::read_to_string(&project_config).expect("read project config");
    assert!(
        content.contains("[embedding]"),
        "template should mention the embedding section: {content:?}"
    );
}

#[test]
fn init_project_local_config_has_no_api_key() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let project_config = project.path().join(".engram").join("engram.toml");
    let content = std::fs::read_to_string(&project_config).expect("read project config");
    assert!(
        !content.contains("api_key"),
        "project-local template must never reference api_key: {content:?}"
    );
    // Dead runtime-ignored fields must not appear either.
    assert!(
        !content.contains("database.path") && !content.contains("socket_path"),
        "project-local template must not reference dead fallback fields: {content:?}"
    );
}

#[test]
fn init_does_not_overwrite_existing_project_local_config() {
    let project = tempdir().expect("project");
    let home = tempdir().expect("home");
    let engram_dir = project.path().join(".engram");
    std::fs::create_dir_all(&engram_dir).expect("create .engram");
    let project_config = engram_dir.join("engram.toml");
    let preexisting = "[embedding]\nhyde_threshold = 99\n";
    std::fs::write(&project_config, preexisting).expect("seed project config");

    init_handler::execute_with_dirs(project.path(), home.path()).expect("init");

    let content = std::fs::read_to_string(&project_config).expect("read project config");
    assert_eq!(
        content, preexisting,
        "existing project-local config must not be overwritten"
    );
}