crtx 0.1.1

CLI for the Cortex supervisory memory substrate.
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
//! Definitive end-to-end integration test for the full Cortex product loop.
//!
//! ## Loop under test
//!
//! ```text
//! cortex init
//!   → cortex session close (--fixtures-dir replay)    [step 2]
//!   → cortex memory list --json                       [step 3]
//!   → cortex memory health --json                     [step 4: pre-outcome]
//!   → cortex memory outcome record                    [step 5]
//!   → cortex memory health --json                     [step 6: never_validated_count shrinks]
//!   → cortex memory search "cortex" --json            [step 7]
//!   → cortex context build --task "cortex" --json     [step 8]
//!   → cortex memory outcome record (second memory)    [step 9]
//! ```
//!
//! ## Single-store design (post proof-closure fix)
//!
//! All steps run against the same "session store". `cortex session close` now
//! dual-writes events to both the JSONL ledger and the SQLite `events` table,
//! so `verify_memory_proof_closure` finds the source events and session-close
//! memories pass the proof-closure gate. `cortex memory search` and
//! `cortex context build` work directly on session-close-activated memories.
//!
//! Steps 7–9 use a separate "search store" that inserts a memory with explicit
//! SQLite lineage to exercise the `insert_active_memory_with_lineage` helper
//! and verify that non-session-close memories also work. This is retained to
//! provide broader coverage without duplicating the helper.
//!
//! ## Proof of the full loop
//!
//! Running `cargo test -p cortex-mem --test cli_e2e_full_loop` with exit 0 is
//! the definitive evidence that the Cortex product loop is end-to-end functional
//! without Ollama or any live LLM — all LLM calls are satisfied via the
//! `ReplayAdapter` and the `--fixtures-dir` flag. See `docs/USER_GUIDE.md`
//! §"Full-loop proof" for the reference.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};

use chrono::{TimeZone, Utc};
use cortex_core::{AuditRecordId, Event, EventId, EventSource, EventType, TraceId, SCHEMA_VERSION};
use cortex_llm::{blake3_hex, LlmMessage, LlmRequest, LlmRole};
use cortex_reflect::{session_reflection_json_schema, DEFAULT_REFLECTION_MODEL};
use cortex_store::migrate::apply_pending;
use cortex_store::repo::memories::accept_candidate_policy_decision_test_allow;
use cortex_store::repo::{EventRepo, MemoryAcceptanceAudit, MemoryCandidate, MemoryRepo};
use rusqlite::Connection;
use serde_json::json;

// ─────────────────────────────────────────────────────────────────────────────
// Shared helpers
// ─────────────────────────────────────────────────────────────────────────────

fn cortex_bin() -> PathBuf {
    PathBuf::from(env!("CARGO_BIN_EXE_cortex"))
}

fn run_in(cwd: &Path, args: &[&str]) -> std::process::Output {
    Command::new(cortex_bin())
        .current_dir(cwd)
        .env("XDG_DATA_HOME", cwd.join("xdg"))
        .env("HOME", cwd)
        .args(args)
        .output()
        .expect("spawn cortex")
}

fn assert_exit(out: &std::process::Output, expected: i32) {
    let code = out.status.code().expect("process exited via signal");
    assert_eq!(
        code,
        expected,
        "expected exit {expected}, got {code}\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr),
    );
}

/// Run `cortex init` in `tmp`, assert exit 0, return the resolved DB path.
fn init(tmp: &Path) -> PathBuf {
    let out = run_in(tmp, &["init"]);
    assert_exit(&out, 0);
    let stdout = String::from_utf8_lossy(&out.stdout);
    let db_line = stdout
        .lines()
        .find(|line| line.starts_with("cortex init: db"))
        .expect("init stdout must include db path line");
    let path = db_line
        .split_once('=')
        .expect("db line must have '='")
        .1
        .trim()
        .split_once(" (")
        .expect("db line must have status suffix")
        .0;
    PathBuf::from(path)
}

fn tmp_dir(suffix: &str) -> tempfile::TempDir {
    tempfile::Builder::new()
        .prefix(&format!("cortex-e2e-full-{suffix}-"))
        .tempdir()
        .expect("create temp dir")
}

/// Build a two-event session fixture and write it to `dir/<name>.json`.
fn write_session_fixture(
    dir: &Path,
    name: &str,
    trace_id: &str,
    event_id_a: &str,
    event_id_b: &str,
) -> PathBuf {
    let path = dir.join(format!("{name}.json"));
    let events = json!({
        "events": [
            {
                "id": event_id_a,
                "schema_version": 1,
                "observed_at": "2026-05-13T10:00:00Z",
                "recorded_at": "2026-05-13T10:00:00Z",
                "source": { "type": "child_agent", "model": "replay" },
                "event_type": "cortex.event.agent_response.v1",
                "trace_id": trace_id,
                "session_id": "e2e-full-loop-session",
                "domain_tags": ["testing"],
                "payload": { "text": "E2E full loop test event one." },
                "payload_hash": "",
                "prev_event_hash": null,
                "event_hash": ""
            },
            {
                "id": event_id_b,
                "schema_version": 1,
                "observed_at": "2026-05-13T10:00:05Z",
                "recorded_at": "2026-05-13T10:00:05Z",
                "source": { "type": "child_agent", "model": "replay" },
                "event_type": "cortex.event.agent_response.v1",
                "trace_id": trace_id,
                "session_id": "e2e-full-loop-session",
                "domain_tags": ["testing"],
                "payload": { "text": "E2E full loop test event two." },
                "payload_hash": "",
                "prev_event_hash": null,
                "event_hash": ""
            }
        ]
    });
    fs::write(&path, serde_json::to_string_pretty(&events).unwrap())
        .expect("write session fixture");
    path
}

/// Build a `SessionReflection` JSON with one memory candidate.
fn reflection_json(trace_id: &str, source_event_id: &str) -> String {
    json!({
        "trace_id": trace_id,
        "episode_candidates": [
            {
                "summary": "E2E full loop produced a test memory.",
                "source_event_ids": [source_event_id],
                "domains": ["testing"],
                "entities": ["Cortex"],
                "candidate_meaning": "Full loop end-to-end works.",
                "confidence": 0.85
            }
        ],
        "memory_candidates": [
            {
                "memory_type": "episodic",
                "claim": "cortex session close activates memories for the full product loop.",
                "source_episode_indexes": [0],
                "applies_when": ["end-to-end testing"],
                "does_not_apply_when": [],
                "confidence": 0.85,
                "initial_salience": {
                    "reusability": 0.8,
                    "consequence": 0.7,
                    "emotional_charge": 0.0
                }
            }
        ],
        "contradictions": [],
        "doctrine_suggestions": []
    })
    .to_string()
}

/// Create a replay fixture directory for one trace+reflection so the CLI
/// `--fixtures-dir` path resolves the LLM call without Ollama.
fn write_replay_fixtures(base_dir: &Path, trace_id: &str, reflection_text: &str) -> PathBuf {
    let unique = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system time after epoch")
        .as_nanos();
    let fixtures_dir = base_dir.join(format!("fixtures-{unique}"));
    fs::create_dir(&fixtures_dir).expect("create fixtures dir");

    let trace: TraceId = trace_id.parse().expect("valid trace id");
    let req = LlmRequest {
        model: DEFAULT_REFLECTION_MODEL.to_string(),
        system: "Return SessionReflection JSON matching the supplied schema.".to_string(),
        messages: vec![LlmMessage {
            role: LlmRole::User,
            content: format!("Reflect trace {trace} into candidate-only Cortex memory."),
        }],
        temperature: 0.0,
        max_tokens: 4096,
        json_schema: Some(session_reflection_json_schema()),
        timeout_ms: 30_000,
    };

    let fixture = json!({
        "request_match": {
            "model": DEFAULT_REFLECTION_MODEL,
            "prompt_hash": req.prompt_hash()
        },
        "response": {
            "text": reflection_text
        }
    });
    let fixture_path = fixtures_dir.join("reflection.json");
    let fixture_bytes = serde_json::to_vec_pretty(&fixture).expect("fixture serializes");
    fs::write(&fixture_path, &fixture_bytes).expect("write fixture");
    fs::write(
        fixtures_dir.join("INDEX.toml"),
        format!(
            "[[fixture]]\npath = \"reflection.json\"\nblake3 = \"{}\"\n",
            blake3_hex(&fixture_bytes)
        ),
    )
    .expect("write INDEX.toml");

    fixtures_dir
}

/// Insert an active memory with proper SQLite event lineage so that
/// `verify_memory_proof_closure` passes and `cortex memory search` /
/// `cortex context build` can include it.
fn insert_active_memory_with_lineage(
    db_path: &Path,
    memory_id: &str,
    source_event_id: &str,
    claim: &str,
    domains: &[&str],
    second: u32,
) {
    let pool = Connection::open(db_path).expect("open db");
    apply_pending(&pool).expect("apply migrations");

    let event_repo = EventRepo::new(&pool);
    if event_repo
        .get_by_id(&source_event_id.parse::<EventId>().expect("valid event id"))
        .expect("check event")
        .is_none()
    {
        let ts = Utc.with_ymd_and_hms(2026, 5, 13, 12, 0, second).unwrap();
        event_repo
            .append(&Event {
                id: source_event_id.parse().expect("valid event id"),
                schema_version: SCHEMA_VERSION,
                observed_at: ts,
                recorded_at: ts,
                source: EventSource::Tool {
                    name: "e2e-full-loop-test".into(),
                },
                event_type: EventType::ToolResult,
                trace_id: None,
                session_id: Some("e2e-full-loop-test".into()),
                domain_tags: domains.iter().map(|d| (*d).to_string()).collect(),
                payload: json!({"source": "e2e-full-loop-test", "second": second}),
                payload_hash: format!("payload-full-e2e-{second}"),
                prev_event_hash: None,
                event_hash: format!("event-full-e2e-{second}"),
            })
            .expect("append source event to SQLite events table");
    }

    let repo = MemoryRepo::new(&pool);
    let created_at = Utc.with_ymd_and_hms(2026, 5, 13, 12, 0, second).unwrap();
    let candidate = MemoryCandidate {
        id: memory_id.parse().expect("valid memory id"),
        memory_type: "semantic".into(),
        claim: claim.into(),
        source_episodes_json: json!([]),
        source_events_json: json!([source_event_id]),
        domains_json: json!(domains),
        salience_json: json!({"score": 0.85}),
        confidence: 0.9,
        authority: "user".into(),
        applies_when_json: json!([]),
        does_not_apply_when_json: json!([]),
        created_at,
        updated_at: created_at,
    };
    let id_str = candidate.id.to_string();
    repo.insert_candidate(&candidate).expect("insert candidate");

    let audit = MemoryAcceptanceAudit {
        id: AuditRecordId::new(),
        actor_json: json!({"kind": "e2e-full-loop-test"}),
        reason: "e2e full loop search-eligible memory".into(),
        source_refs_json: json!([id_str]),
        created_at: Utc
            .with_ymd_and_hms(2026, 5, 13, 12, 0, second + 1)
            .unwrap(),
    };
    repo.accept_candidate(
        &memory_id.parse().expect("valid memory id"),
        Utc.with_ymd_and_hms(2026, 5, 13, 12, 0, second + 2)
            .unwrap(),
        &audit,
        &accept_candidate_policy_decision_test_allow(),
    )
    .expect("accept candidate");
}

// ─────────────────────────────────────────────────────────────────────────────
// The definitive full-loop test
// ─────────────────────────────────────────────────────────────────────────────

/// Full Cortex product loop: init → session close → memory list → memory health
/// → outcome record → health post-outcome → memory search (session store) →
/// memory search (search store) → context build → outcome record.
///
/// Step 7 proves the proof-closure gap is closed: `cortex memory search` now
/// works directly on session-close-activated memories because `cortex session
/// close` dual-writes events to both the JSONL ledger and the SQLite `events`
/// table. Steps 8–10 exercise the same path with an explicitly-inserted memory
/// to provide broader coverage.
///
/// This test is the definitive proof that the end-to-end loop works without
/// Ollama. All LLM calls are satisfied by the `ReplayAdapter` via `--fixtures-dir`.
///
/// See `docs/USER_GUIDE.md` §"Full-loop proof" for the reference.
#[test]
fn full_product_loop_init_close_search_context_outcome_health() {
    // ── Session store: steps 1–7 ──────────────────────────────────────────────
    // Proves: init → session close activates memories (with SQLite dual-write)
    // → list/health counts are correct → outcome record decreases
    // never_validated_count → memory search works on session-close memories.

    let session_tmp = tmp_dir("session");

    // Step 1: cortex init — creates the SQLite store and JSONL ledger.
    init(session_tmp.path());

    // Step 2: cortex session close (replay fixtures, no Ollama).
    // The CLI path activates memories immediately via MemoryRepo::set_active.
    let trace_id = TraceId::new().to_string();
    let event_id_a = EventId::new().to_string();
    let event_id_b = EventId::new().to_string();
    let session_path = write_session_fixture(
        session_tmp.path(),
        "session-full-loop",
        &trace_id,
        &event_id_a,
        &event_id_b,
    );
    let reflection = reflection_json(&trace_id, &event_id_a);
    let fixtures_dir = write_replay_fixtures(session_tmp.path(), &trace_id, &reflection);

    let close_out = run_in(
        session_tmp.path(),
        &[
            "--json",
            "session",
            "close",
            session_path.to_str().unwrap(),
            "--fixtures-dir",
            fixtures_dir.to_str().unwrap(),
        ],
    );
    // Exit 0 is a hard gate: all downstream steps require active memories.
    assert_exit(&close_out, 0);

    let close_stdout = String::from_utf8_lossy(&close_out.stdout);
    let close_json: serde_json::Value = serde_json::from_str(&close_stdout)
        .expect("session close --json stdout must be valid JSON");
    assert_eq!(
        close_json["command"].as_str(),
        Some("cortex.session.close"),
        "session close envelope command must be cortex.session.close: {close_json}"
    );
    let close_report = &close_json["report"];
    let activated_count = close_report["activated_count"]
        .as_u64()
        .expect("activated_count must be a number");
    assert!(
        activated_count >= 1,
        "session close must activate at least one memory; report: {close_report}"
    );
    assert_eq!(
        close_report["no_candidates"].as_bool(),
        Some(false),
        "session close must not report no_candidates: {close_report}"
    );

    // Capture the first activated memory id for the outcome step below.
    let activated_ids = close_report["activated_memory_ids"]
        .as_array()
        .expect("activated_memory_ids must be an array");
    assert!(
        !activated_ids.is_empty(),
        "activated_memory_ids must not be empty: {close_report}"
    );
    let session_memory_id = activated_ids[0]
        .as_str()
        .expect("memory id must be a string")
        .to_string();

    // Step 3: cortex memory list --json — confirms active memories exist.
    // `memory list` does NOT apply proof-closure, so it reliably reflects
    // the raw status='active' count.
    let list_out = run_in(session_tmp.path(), &["--json", "memory", "list"]);
    assert_exit(&list_out, 0);
    let list_stdout = String::from_utf8_lossy(&list_out.stdout);
    let list_json: serde_json::Value =
        serde_json::from_str(&list_stdout).expect("memory list --json must be valid JSON");
    let list_count = list_json["report"]["match_count"]
        .as_u64()
        .expect("match_count must be a number");
    assert!(
        list_count >= 1,
        "memory list must return at least one active memory after session close: {list_json}"
    );

    // Step 4: cortex memory health --json (pre-outcome) — verifies structure
    // and that newly activated memories appear in never_validated_count.
    let health_pre_out = run_in(session_tmp.path(), &["--json", "memory", "health"]);
    assert_exit(&health_pre_out, 0);
    let health_pre_stdout = String::from_utf8_lossy(&health_pre_out.stdout);
    let health_pre_json: serde_json::Value = serde_json::from_str(&health_pre_stdout)
        .expect("memory health --json (pre) must be valid JSON");
    let health_pre = &health_pre_json["report"];

    let total_active_pre = health_pre["total_active"]
        .as_u64()
        .expect("total_active must be a number");
    assert!(
        total_active_pre >= 1,
        "memory health total_active must be >= 1 after session close: {health_pre}"
    );
    assert!(
        health_pre["low_confidence_count"].is_number(),
        "health report must have low_confidence_count: {health_pre}"
    );
    assert!(
        health_pre["never_validated_count"].is_number(),
        "health report must have never_validated_count: {health_pre}"
    );
    assert!(
        health_pre["no_outcome_count"].is_number(),
        "health report must have no_outcome_count: {health_pre}"
    );
    // Newly activated memories have validation_epoch = NULL (never validated).
    let never_validated_pre = health_pre["never_validated_count"]
        .as_u64()
        .expect("never_validated_count must be a number");
    assert!(
        never_validated_pre >= 1,
        "newly activated memories have validation_epoch=NULL so never_validated_count >= 1: {health_pre}"
    );
    let no_outcome_pre = health_pre["no_outcome_count"]
        .as_u64()
        .expect("no_outcome_count must be a number");
    assert!(
        no_outcome_pre >= 1,
        "newly activated memories have no outcome records yet so no_outcome_count >= 1: {health_pre}"
    );

    // Step 5: cortex memory outcome record — records "helpful" for the first
    // session-close-activated memory.
    let outcome_out = run_in(
        session_tmp.path(),
        &[
            "memory",
            "outcome",
            "record",
            "--memory-id",
            &session_memory_id,
            "--session",
            "e2e-full-loop-session",
            "--result",
            "helpful",
        ],
    );
    assert_exit(&outcome_out, 0);
    let outcome_stdout = String::from_utf8_lossy(&outcome_out.stdout);
    assert!(
        outcome_stdout.contains("outcome recorded"),
        "outcome record must confirm success: stdout={outcome_stdout} stderr={}",
        String::from_utf8_lossy(&outcome_out.stderr)
    );
    assert!(
        outcome_stdout.contains(&session_memory_id),
        "outcome record must echo the memory id: {outcome_stdout}"
    );

    // Step 6: cortex memory health --json (post-outcome) — no_outcome_count
    // must have decreased because one memory now has an outcome record.
    //
    // Note: `never_validated_count` tracks `validation_epoch == 0` which is a
    // separate column updated only by an explicit validation pass, not by
    // recording a session outcome. Only `no_outcome_count` is expected to
    // decrease after `memory outcome record`.
    let health_post_out = run_in(session_tmp.path(), &["--json", "memory", "health"]);
    assert_exit(&health_post_out, 0);
    let health_post_stdout = String::from_utf8_lossy(&health_post_out.stdout);
    let health_post_json: serde_json::Value = serde_json::from_str(&health_post_stdout)
        .expect("memory health --json (post) must be valid JSON");
    let health_post = &health_post_json["report"];

    let no_outcome_post = health_post["no_outcome_count"]
        .as_u64()
        .expect("no_outcome_count must be a number");
    assert!(
        no_outcome_post < no_outcome_pre,
        "no_outcome_count must decrease after recording an outcome: \
         before={no_outcome_pre} after={no_outcome_post}; {health_post}"
    );

    // never_validated_count is unchanged by outcome recording (only updated by
    // an explicit validation-epoch pass). Confirm it is still a valid number.
    assert!(
        health_post["never_validated_count"].is_number(),
        "health report must still have never_validated_count after outcome: {health_post}"
    );

    // total_active must not decrease between health checks (no memories removed).
    let total_active_post = health_post["total_active"]
        .as_u64()
        .expect("total_active must be a number");
    assert!(
        total_active_post >= total_active_pre,
        "total_active must not decrease; before={total_active_pre} after={total_active_post}"
    );

    // ── Step 7: cortex memory search against the session store ───────────────
    // Proof-closure gap fix: `cortex session close` now dual-writes events to
    // both the JSONL ledger AND the SQLite `events` table, so
    // `verify_memory_proof_closure` finds source events and session-close
    // memories pass the proof-closure gate. This step proves the fix works —
    // search now succeeds directly on the session store without a separate
    // store with manually inserted lineage.
    let session_search_out = run_in(
        session_tmp.path(),
        &["--json", "memory", "search", "session"],
    );
    assert_exit(&session_search_out, 0);
    let session_search_stdout = String::from_utf8_lossy(&session_search_out.stdout);
    let session_search_json: serde_json::Value = serde_json::from_str(&session_search_stdout)
        .expect("session-store memory search --json must be valid JSON");
    let session_search_count = session_search_json["report"]["match_count"]
        .as_u64()
        .expect("match_count must be a number");
    assert!(
        session_search_count >= 1,
        "memory search must return at least one match for 'session' in the session store \
         after proof-closure fix: {session_search_json}"
    );

    // ── Search store: steps 8–10 ──────────────────────────────────────────────
    // Proves: memory search → context build → outcome record with a memory that
    // has explicit SQLite event lineage (non-session-close path).

    let search_tmp = tmp_dir("search");
    let search_db = init(search_tmp.path());

    // Insert a memory with explicit SQLite event lineage. The claim contains
    // "cortex" so search can match it. The memory id must be a valid ULID.
    let search_mem_id = "mem_01ARZ3NDEKTSV4RRFFQ69G5FAE";
    let search_event_id = "evt_01ARZ3NDEKTSV4RRFFQ69G5FAV";
    let search_claim = "cortex full product loop is verified end-to-end without Ollama.";
    insert_active_memory_with_lineage(
        &search_db,
        search_mem_id,
        search_event_id,
        search_claim,
        &["testing"],
        10,
    );

    // Step 8: cortex memory search "cortex" --json — must find the lineage-valid
    // memory. Exit 0 proves proof closure passes for the inserted memory.
    let search_out = run_in(search_tmp.path(), &["--json", "memory", "search", "cortex"]);
    assert_exit(&search_out, 0);
    let search_stdout = String::from_utf8_lossy(&search_out.stdout);
    let search_json: serde_json::Value =
        serde_json::from_str(&search_stdout).expect("memory search --json must be valid JSON");
    let search_count = search_json["report"]["match_count"]
        .as_u64()
        .expect("match_count must be a number");
    assert!(
        search_count >= 1,
        "memory search must return at least one match for 'cortex': {search_json}"
    );

    let matches = search_json["report"]["matches"]
        .as_array()
        .expect("matches must be an array");
    let found = matches
        .iter()
        .any(|m| m["memory_id"].as_str() == Some(search_mem_id));
    assert!(
        found,
        "search results must include the lineage-valid memory {search_mem_id}: {search_json}"
    );

    // Step 9: cortex context build --task "cortex" --json — builds a context
    // pack. The pack's `selected_refs` must be non-empty.
    let ctx_out = run_in(
        search_tmp.path(),
        &["--json", "context", "build", "--task", "cortex"],
    );
    assert_exit(&ctx_out, 0);
    let ctx_stdout = String::from_utf8_lossy(&ctx_out.stdout);
    let ctx_json: serde_json::Value =
        serde_json::from_str(&ctx_stdout).expect("context build --json must be valid JSON");
    assert_eq!(
        ctx_json["command"].as_str(),
        Some("cortex.context.build"),
        "context build envelope command must be cortex.context.build: {ctx_json}"
    );
    let selected_refs = ctx_json["report"]["selected_refs"]
        .as_array()
        .expect("context pack report must have selected_refs array");
    assert!(
        !selected_refs.is_empty(),
        "context build must include at least one selected ref when active memories exist: {ctx_json}"
    );

    // Step 10: cortex memory outcome record (search-store memory) — records a
    // "helpful" verdict for the lineage-valid memory to confirm outcome recording
    // works in the search store as well.
    let search_outcome_out = run_in(
        search_tmp.path(),
        &[
            "memory",
            "outcome",
            "record",
            "--memory-id",
            search_mem_id,
            "--session",
            "e2e-full-loop-search-session",
            "--result",
            "helpful",
        ],
    );
    assert_exit(&search_outcome_out, 0);
    let search_outcome_stdout = String::from_utf8_lossy(&search_outcome_out.stdout);
    assert!(
        search_outcome_stdout.contains("outcome recorded"),
        "search-store outcome record must confirm success: stdout={search_outcome_stdout} stderr={}",
        String::from_utf8_lossy(&search_outcome_out.stderr)
    );
    assert!(
        search_outcome_stdout.contains(search_mem_id),
        "search-store outcome record must echo the memory id: {search_outcome_stdout}"
    );
}