exocortex-client 0.4.0

The Exocortex MCP client: local cache, WAL offline capture, the session-wrapup tool, and the reconnecting SSE subscriber.
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
//! BR-PRD acceptance (docs/prd/backup-restore-prd.md): the WAL as a
//! portable file — round trip, idempotent import, fingerprint gate,
//! empty round trip, state preservation.

use std::io::Write;
use std::process::{Child, Command, Stdio};

mod support;

struct Client {
    child: Child,
    responses: support::BoundedLineReader,
}

impl Client {
    /// A one-shot mode (--export/--import): run to completion.
    fn run_oneshot(
        dir: &std::path::Path,
        flag: &str,
        file: &std::path::Path,
    ) -> std::process::Output {
        Command::new(env!("CARGO_BIN_EXE_exocortex-mcp-client"))
            .args([
                "--org",
                "backup",
                "--user",
                "tester",
                "--data-dir",
                dir.to_str().unwrap(),
                flag,
                file.to_str().unwrap(),
            ])
            .output()
            .expect("run one-shot mode")
    }

    fn spawn_serving(dir: &std::path::Path) -> Self {
        let mut cmd = Command::new(env!("CARGO_BIN_EXE_exocortex-mcp-client"));
        cmd.args([
            "--org",
            "backup",
            "--user",
            "tester",
            "--data-dir",
            dir.to_str().unwrap(),
        ]);
        let mut child = cmd
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .expect("spawn exocortex-mcp-client");
        let responses = support::BoundedLineReader::new(child.stdout.take().expect("stdout"));
        Self { child, responses }
    }

    fn send_all(&mut self, msgs: &[serde_json::Value]) {
        let stdin = self.child.stdin.as_mut().expect("stdin");
        for m in msgs {
            writeln!(stdin, "{m}").unwrap();
        }
        stdin.flush().unwrap();
    }

    fn read_line(&mut self) -> serde_json::Value {
        self.responses.read_json(&mut self.child)
    }
}

impl Drop for Client {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

fn tempdir() -> std::path::PathBuf {
    let dir = std::env::temp_dir().join(format!(
        "exocortex-backup-{}-{}",
        std::process::id(),
        uuid::Uuid::new_v4().simple()
    ));
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn init_msgs() -> Vec<serde_json::Value> {
    vec![
        serde_json::json!({
            "jsonrpc": "2.0", "id": 1, "method": "initialize",
            "params": {
                "protocolVersion": "2024-11-05",
                "capabilities": {},
                "clientInfo": { "name": "backup-test", "version": "0" }
            }
        }),
        serde_json::json!({
            "jsonrpc": "2.0", "method": "notifications/initialized", "params": {}
        }),
    ]
}

fn search_msg(id: i64, query: &str) -> serde_json::Value {
    serde_json::json!({
        "jsonrpc": "2.0", "id": id, "method": "tools/call",
        "params": {
            "name": "exocortex.search_memories",
            "arguments": { "query": query, "limit": 50 }
        }
    })
}

fn hits_of(payload: &serde_json::Value) -> Vec<(String, String)> {
    let text = payload["result"]["content"][0]["text"]
        .as_str()
        .unwrap_or("");
    let inner: serde_json::Value = serde_json::from_str(text).expect("payload JSON");
    inner["memories"]
        .as_array()
        .cloned()
        .unwrap_or_default()
        .into_iter()
        .map(|m| {
            (
                m["id"].as_str().unwrap_or("").to_string(),
                m["title"].as_str().unwrap_or("").to_string(),
            )
        })
        .collect()
}

fn end_session_msg(id: i64) -> serde_json::Value {
    serde_json::json!({
        "jsonrpc": "2.0", "id": id, "method": "tools/call",
        "params": {
            "name": "exocortex.end_session",
            "arguments": {
                "session_id": "s-backup",
                "project_id": "proj",
                "memories": [
                    { "draft_key": "p", "memory_type": "Problem", "title": "Yak shave in release script", "content": "release script shaves yaks", "visibility": "org", "tags": ["release"] },
                    { "draft_key": "f", "memory_type": "Fix", "title": "Pin the toolchain instead", "content": "pin rust-toolchain", "visibility": "org" }
                ],
                "edges": [
                    { "from_draft_key": "f", "to_draft_key": "p", "kind": "Fixes", "strength": 0.9 }
                ]
            }
        }
    })
}

/// AC1: write, export, WIPE the data-dir, import, restart — every
/// memory searchable, every id byte-identical to pre-export.
#[test]
fn round_trip_preserves_ids_and_reads() {
    let dir = tempdir();
    let file = dir.join("memories.json");

    // Phase 1: write one batch (problem + fix + edge), capture ids.
    let ids = {
        let mut c = Client::spawn_serving(&dir);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        msgs.push(search_msg(82, "yak"));
        msgs.push(search_msg(83, "toolchain"));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some(), "write ok");
        let h1 = hits_of(&c.read_line());
        let h2 = hits_of(&c.read_line());
        assert_eq!(h1.len(), 1);
        assert_eq!(h2.len(), 1);
        vec![h1[0].0.clone(), h2[0].0.clone()]
    };

    // Phase 2: export, wipe, import.
    let out = Client::run_oneshot(&dir, "--export", &file);
    assert!(
        out.status.success(),
        "export ok: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    std::fs::remove_dir_all(dir.join("wal")).expect("wipe wal");
    let out = Client::run_oneshot(&dir, "--import", &file);
    assert!(
        out.status.success(),
        "import ok: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Phase 3: same ids, same reads, on the restored WAL.
    let mut c = Client::spawn_serving(&dir);
    let mut msgs = init_msgs();
    msgs.push(search_msg(90, "yak"));
    msgs.push(search_msg(91, "toolchain"));
    c.send_all(&msgs);
    let _init = c.read_line();
    let h1 = hits_of(&c.read_line());
    let h2 = hits_of(&c.read_line());
    assert_eq!(h1.len(), 1, "yak write restored: {h1:?}");
    assert_eq!(h2.len(), 1, "toolchain write restored: {h2:?}");
    assert_eq!(h1[0].0, ids[0], "id byte-identical across wipe+restore");
    assert_eq!(h2[0].0, ids[1], "id byte-identical across wipe+restore");
}

/// AC2: importing the same backup twice is a no-op on the served graph.
#[test]
fn import_is_idempotent() {
    let dir = tempdir();
    let file = dir.join("memories.json");
    {
        let mut c = Client::spawn_serving(&dir);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some());
    }
    assert!(Client::run_oneshot(&dir, "--export", &file)
        .status
        .success());
    // Import the same file twice into the SAME dir (entries already there).
    assert!(Client::run_oneshot(&dir, "--import", &file)
        .status
        .success());
    assert!(Client::run_oneshot(&dir, "--import", &file)
        .status
        .success());
    let mut c = Client::spawn_serving(&dir);
    let mut msgs = init_msgs();
    msgs.push(search_msg(90, "yak"));
    c.send_all(&msgs);
    let _init = c.read_line();
    let hits = hits_of(&c.read_line());
    assert_eq!(hits.len(), 1, "no duplicates after double import: {hits:?}");
}

/// AC3: a fingerprint-mismatched backup aborts before anything is
/// written; the target WAL is untouched.
#[test]
fn fingerprint_mismatch_aborts_cleanly() {
    let src = tempdir();
    let dst = tempdir();
    let file = src.join("memories.json");
    {
        let mut c = Client::spawn_serving(&src);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some());
    }
    assert!(Client::run_oneshot(&src, "--export", &file)
        .status
        .success());

    // Tamper with the ontology: a foreign summary (OC-PRD D2) on the
    // modern path, and — in a second leg — a legacy-shaped document
    // whose v1-scheme fingerprint no longer matches.
    let original = std::fs::read_to_string(&file).unwrap();
    let mut doc: serde_json::Value = serde_json::from_str(&original).unwrap();
    doc["ontology_summary"]["memory_types"][0] = serde_json::json!("NotARealType");
    std::fs::write(&file, serde_json::to_string(&doc).unwrap()).unwrap();
    let legacy_file = src.join("legacy.json");
    let mut legacy: serde_json::Value = serde_json::from_str(&original).unwrap();
    legacy["ontology_summary"].take();
    legacy["ontology_fingerprint"] = serde_json::json!("0".repeat(64));
    std::fs::write(&legacy_file, serde_json::to_string(&legacy).unwrap()).unwrap();

    // Seed the destination with one real write, so "untouched" is
    // observable.
    {
        let mut c = Client::spawn_serving(&dst);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some());
    }
    let out = Client::run_oneshot(&dst, "--import", &file);
    assert!(!out.status.success(), "mismatched import must fail");
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(stderr.contains("ontology"), "reason surfaces: {stderr}");
    let out = Client::run_oneshot(&dst, "--import", &legacy_file);
    assert!(
        !out.status.success(),
        "legacy-shaped mismatched import must fail"
    );

    let mut c = Client::spawn_serving(&dst);
    let mut msgs = init_msgs();
    msgs.push(search_msg(90, "yak"));
    c.send_all(&msgs);
    let _init = c.read_line();
    let hits = hits_of(&c.read_line());
    assert_eq!(hits.len(), 1, "target WAL untouched (its own write only)");
}

/// OC-PRD S6 (docs/prd/ontology-compatibility-prd.md): restores stop
/// expiring. A backup written under `{dev-v1}` restores into a binary
/// running `{dev-v1 + appended type}` (every draft revalidated
/// against the current rulebook); an id-shifting ontology still
/// refuses, which per-draft revalidation alone cannot detect.
#[test]
fn superset_ontology_restores_subset_backup() {
    let src = tempdir();
    let file = src.join("memories.json");
    {
        let mut c = Client::spawn_serving(&src);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some());
    }
    assert!(Client::run_oneshot(&src, "--export", &file)
        .status
        .success());

    // The runtime pin is the COMPOSED set (dev-v1 + mortgage both link
    // into the client binary), so the superset grows the LAST pack —
    // appending past mortgage shifts nothing the pin references.
    let grown_dev = exocortex_pack_dev_v1::pack_def();
    let mut grown_mortgage = exocortex_pack_mortgage_v1::pack_def();
    grown_mortgage.memory_type_names.push("FutureThing".into());
    let grown = exocortex_kernel::Ontology::from_packs(vec![grown_dev, grown_mortgage]).unwrap();

    let dst = tempdir();
    let wal = exocortex_client::wal::Wal::open(&dst.join("wal")).unwrap();
    let report = exocortex_client::backup::import(&wal, &grown, &file)
        .expect("subset backup restores into the superset binary");
    assert_eq!(report.imported, 1);

    // Insertion shifts positional ids: the backup's summary is no
    // longer a subset, even though every name still resolves.
    let mut shifted = exocortex_pack_dev_v1::pack_def();
    shifted.memory_type_names.insert(0, "AlphaHazard".into());
    let shifted = exocortex_kernel::Ontology::from_packs(vec![shifted]).unwrap();
    let err = exocortex_client::backup::import(&wal, &shifted, &file).unwrap_err();
    assert!(err.to_string().contains("superset"), "{err}");
}

/// AC4: an empty WAL exports a valid empty backup; importing it is a
/// no-op success.
#[test]
fn empty_round_trip() {
    let dir = tempdir();
    let file = dir.join("memories.json");
    let out = Client::run_oneshot(&dir, "--export", &file);
    assert!(out.status.success());
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(stdout.contains("0 entries"), "summary says empty: {stdout}");
    let out = Client::run_oneshot(&dir, "--import", &file);
    assert!(out.status.success(), "empty import is a no-op success");
}

/// AC5: entry states ride verbatim — `Synced` before export is
/// `Synced` after import and cannot re-drain.
#[test]
fn states_ride_verbatim() {
    let dir = tempdir();
    let file = dir.join("memories.json");
    {
        let mut c = Client::spawn_serving(&dir);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some());
    }
    // Settle the entry directly, then export.
    {
        let wal = exocortex_client::wal::Wal::open(&dir.join("wal")).unwrap();
        let states = wal.states_for_test().unwrap();
        assert_eq!(states.len(), 1);
        wal.mark_synced(states[0].0, 777).unwrap();
        drop(wal);
    }
    assert!(Client::run_oneshot(&dir, "--export", &file)
        .status
        .success());
    std::fs::remove_dir_all(dir.join("wal")).expect("wipe wal");
    assert!(Client::run_oneshot(&dir, "--import", &file)
        .status
        .success());
    let wal = exocortex_client::wal::Wal::open(&dir.join("wal")).unwrap();
    let states = wal.states_for_test().unwrap();
    assert_eq!(
        states.len(),
        1,
        "one entry after wipe+import, no duplicates"
    );
    match states[0].1 {
        exocortex_client::wal::WalState::Synced { backend_lsn } => {
            assert_eq!(
                backend_lsn, 777,
                "Synced rides verbatim — will not re-drain"
            );
        }
        other => panic!("expected Synced, got {other:?}"),
    }
}

/// R6-R20: oversized files are rejected from metadata before allocation or
/// parsing, and the pre-existing WAL remains byte-for-byte usable.
#[test]
fn oversized_backup_is_rejected_without_partial_import() {
    let dir = tempdir();
    let file = dir.join("oversized.json");
    {
        let mut c = Client::spawn_serving(&dir);
        let mut msgs = init_msgs();
        msgs.push(end_session_msg(80));
        c.send_all(&msgs);
        let _init = c.read_line();
        assert!(c.read_line().get("result").is_some());
    }
    let oversized = std::fs::File::create(&file).unwrap();
    oversized
        .set_len(exocortex_client::backup::MAX_BACKUP_BYTES + 1)
        .unwrap();
    drop(oversized);

    let out = Client::run_oneshot(&dir, "--import", &file);
    assert!(!out.status.success());
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("maximum supported size"),
        "resource rejection must be explicit: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let mut c = Client::spawn_serving(&dir);
    let mut msgs = init_msgs();
    msgs.push(search_msg(90, "yak"));
    c.send_all(&msgs);
    let _init = c.read_line();
    assert_eq!(hits_of(&c.read_line()).len(), 1, "existing WAL is intact");
}