codex-recall 0.1.3

Local search and recall for Codex session JSONL archives
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
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
use crate::commands::date::resolve_date_window;
use crate::config::default_db_path;
use crate::memory::MemoryKind;
use crate::output::{compact_whitespace, preview};
use crate::store::{
    encode_delta_cursor, DeltaItem, MatchStrategy, MemoryEvidence, MemoryObject, MemoryResult,
    MemorySearchOptions, RecentSession, ResourceRecord, SearchOptions, Store,
};
use anyhow::{bail, Result};
use clap::{Args, ValueEnum};
use serde::Deserialize;
use serde_json::json;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum MemoryKindArg {
    Decision,
    Task,
    Fact,
    OpenQuestion,
    Blocker,
}

impl MemoryKindArg {
    fn memory_kind(self) -> MemoryKind {
        match self {
            Self::Decision => MemoryKind::Decision,
            Self::Task => MemoryKind::Task,
            Self::Fact => MemoryKind::Fact,
            Self::OpenQuestion => MemoryKind::OpenQuestion,
            Self::Blocker => MemoryKind::Blocker,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum ResourceKindArg {
    Session,
    Memory,
    All,
}

#[derive(Debug, Clone, Args)]
pub struct MemoriesArgs {
    #[arg(help = "Optional query over extracted memory objects")]
    pub query: Option<String>,
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
    #[arg(long, default_value_t = 20, help = "Maximum memory objects to print")]
    pub limit: usize,
    #[arg(long, help = "Restrict memories to a repo name")]
    pub repo: Option<String>,
    #[arg(long, help = "Restrict memories to a cwd substring")]
    pub cwd: Option<String>,
    #[arg(long, help = "Restrict by age, for example 7d, today, or 2026-04-01")]
    pub since: Option<String>,
    #[arg(long, help = "Restrict to memories seen at or after this date/time")]
    pub from: Option<String>,
    #[arg(long, help = "Restrict to memories seen before this date/time")]
    pub until: Option<String>,
    #[arg(long, help = "Restrict to one local calendar day, YYYY-MM-DD")]
    pub day: Option<String>,
    #[arg(
        long = "kind",
        value_enum,
        value_name = "KIND",
        help = "Restrict memories by kind; repeatable"
    )]
    pub kinds: Vec<MemoryKindArg>,
    #[arg(long, help = "Include retrieval trace fields in JSON output")]
    pub trace: bool,
    #[arg(long, help = "Emit machine-readable JSON")]
    pub json: bool,
}

#[derive(Debug, Clone, Args)]
pub struct MemoryShowArgs {
    #[arg(help = "Memory object id")]
    pub memory_id: String,
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
    #[arg(long, default_value_t = 20, help = "Maximum evidence rows to print")]
    pub limit: usize,
    #[arg(long, help = "Emit machine-readable JSON")]
    pub json: bool,
}

#[derive(Debug, Clone, Args)]
pub struct DeltaArgs {
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
    #[arg(long, help = "Opaque cursor returned by a previous delta call")]
    pub cursor: Option<String>,
    #[arg(long, default_value_t = 50, help = "Maximum changed items to return")]
    pub limit: usize,
    #[arg(long, help = "Restrict changed items to a repo name")]
    pub repo: Option<String>,
    #[arg(long, help = "Emit machine-readable JSON")]
    pub json: bool,
}

#[derive(Debug, Clone, Args)]
pub struct RelatedArgs {
    #[arg(help = "Session id/session key or memory id")]
    pub reference: String,
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
    #[arg(
        long,
        default_value_t = 10,
        help = "Maximum related sessions and memories"
    )]
    pub limit: usize,
    #[arg(long, help = "Emit machine-readable JSON")]
    pub json: bool,
}

#[derive(Debug, Clone, Args)]
pub struct EvalArgs {
    #[arg(help = "Path to a JSON eval fixture")]
    pub fixture: PathBuf,
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
    #[arg(long, help = "Emit machine-readable JSON")]
    pub json: bool,
}

#[derive(Debug, Clone, Args)]
pub struct ResourcesArgs {
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
    #[arg(
        long,
        value_enum,
        default_value = "all",
        help = "Resource type to list"
    )]
    pub kind: ResourceKindArg,
    #[arg(long, default_value_t = 20, help = "Maximum resources to print")]
    pub limit: usize,
    #[arg(long, help = "Emit machine-readable JSON")]
    pub json: bool,
}

#[derive(Debug, Clone, Args)]
pub struct ReadResourceArgs {
    #[arg(help = "Resource URI to read")]
    pub uri: String,
    #[arg(long, help = "SQLite index path")]
    pub db: Option<PathBuf>,
}

#[derive(Debug, Deserialize)]
struct EvalFixture {
    cases: Vec<EvalCase>,
}

#[derive(Debug, Deserialize)]
struct EvalCase {
    name: String,
    command: String,
    #[serde(default)]
    query: Option<String>,
    #[serde(default)]
    cursor: Option<String>,
    #[serde(default)]
    limit: Option<usize>,
    expected: EvalExpected,
}

#[derive(Debug, Default, Deserialize)]
struct EvalExpected {
    session_id: Option<String>,
    top_memory_kind: Option<String>,
    top_memory_summary_contains: Option<String>,
    contains_session_id: Option<String>,
    contains_memory_kind: Option<String>,
    next_cursor_present: Option<bool>,
    kind: Option<String>,
    summary_contains: Option<String>,
}

pub fn run_memories(args: MemoriesArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    let (since, from, until) = resolve_date_window(args.since, args.from, args.until, args.day)?;
    let options = MemorySearchOptions {
        query: args.query.clone(),
        limit: args.limit,
        repo: args.repo,
        cwd: args.cwd,
        since,
        from,
        until,
        kinds: args.kinds.iter().map(|kind| kind.memory_kind()).collect(),
    };
    let (match_strategy, memories) = store.memory_results_with_trace(options)?;
    if args.json {
        print_memories_json(&memories, match_strategy, args.trace)?;
        return Ok(());
    }

    if memories.is_empty() {
        println!("no memories");
        return Ok(());
    }

    for (index, memory) in memories.iter().enumerate() {
        println!(
            "{}. {}  {}",
            index + 1,
            memory.object.id,
            memory.object.kind.as_str()
        );
        println!("   summary: {}", memory.object.summary);
        println!("   evidence: {}", memory.object.evidence_count);
        println!("   last_seen: {}", memory.object.last_seen_at);
    }
    Ok(())
}

pub fn run_memory_show(args: MemoryShowArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    let Some(memory) = store.memory_by_id(&args.memory_id)? else {
        println!("no memory {}", args.memory_id);
        return Ok(());
    };
    let evidence = store.memory_evidence(&args.memory_id, args.limit)?;
    if args.json {
        print_memory_json(&memory, &evidence)?;
        return Ok(());
    }

    println!("{}  {}", memory.id, memory.kind.as_str());
    println!("{}", memory.summary);
    for item in evidence {
        println!(
            "- {}  {}:{}",
            item.session_id,
            item.source_file_path.display(),
            item.source_line_number
        );
        println!("  {}", preview(&item.evidence_text, 160));
    }
    Ok(())
}

pub fn run_delta(args: DeltaArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    let items = store.delta_items(args.cursor.as_deref(), args.limit, args.repo.as_deref())?;
    let next_cursor = items.last().map(encode_delta_cursor);
    if args.json {
        let items = items.iter().map(delta_item_json).collect::<Vec<_>>();
        let value = json!({
            "object": "delta_page",
            "cursor": args.cursor,
            "count": items.len(),
            "next_cursor": next_cursor,
            "items": items,
        });
        println!("{}", serde_json::to_string_pretty(&value)?);
        return Ok(());
    }

    if items.is_empty() {
        println!("no changed items");
        return Ok(());
    }

    for item in &items {
        match item {
            DeltaItem::Session {
                session_key,
                session_id,
                updated_at,
                ..
            } => println!("session  {session_key}  {session_id}  {updated_at}"),
            DeltaItem::Memory { object, .. } => {
                println!(
                    "memory   {}  {}  {}",
                    object.id,
                    object.kind.as_str(),
                    object.updated_at
                )
            }
            DeltaItem::Deleted {
                object_type,
                object_id,
                updated_at,
                ..
            } => println!("deleted  {object_type}  {object_id}  {updated_at}"),
        }
    }
    if let Some(cursor) = next_cursor {
        println!("next_cursor: {cursor}");
    }
    Ok(())
}

pub fn run_related(args: RelatedArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    if let Some(memory) = store.memory_by_id(&args.reference)? {
        let sessions = store.related_sessions_for_memory(&args.reference, args.limit)?;
        let memories = store.cooccurring_memories(&args.reference, args.limit)?;
        if args.json {
            let value = json!({
                "object": "related_context",
                "reference": memory_json_value(&memory, &[], false, MatchStrategy::AllTerms),
                "sessions": sessions.iter().map(session_json_value).collect::<Vec<_>>(),
                "memories": memories.iter().map(|item| memory_result_json(item, false, MatchStrategy::AllTerms)).collect::<Vec<_>>(),
            });
            println!("{}", serde_json::to_string_pretty(&value)?);
            return Ok(());
        }
        println!("related memory {}", args.reference);
        for session in sessions {
            println!("- session {} {}", session.session_key, session.session_id);
        }
        return Ok(());
    }

    let matches = store.resolve_session_reference(&args.reference)?;
    if matches.is_empty() {
        println!("no related context for {}", args.reference);
        return Ok(());
    }
    if matches.len() > 1 {
        let choices = matches
            .iter()
            .map(|session| {
                format!(
                    "  {}  {}",
                    session.session_key,
                    session.source_file_path.display()
                )
            })
            .collect::<Vec<_>>()
            .join("\n");
        bail!(
            "multiple indexed sessions match `{}`; use one session_key:\n{choices}",
            args.reference
        );
    }
    let session = &matches[0];
    let sessions = store.related_sessions_for_session(&session.session_key, args.limit)?;
    let memories = store.related_memories_for_session(&session.session_key, args.limit)?;
    if args.json {
        let value = json!({
            "object": "related_context",
            "reference": {
                "object": "session",
                "session_key": session.session_key,
                "session_id": session.session_id,
                "repo": session.repo,
                "cwd": session.cwd,
                "resource_uri": format!("codex-recall://session/{}", session.session_key),
            },
            "sessions": sessions.iter().map(session_json_value).collect::<Vec<_>>(),
            "memories": memories.iter().map(|item| memory_result_json(item, false, MatchStrategy::AllTerms)).collect::<Vec<_>>(),
        });
        println!("{}", serde_json::to_string_pretty(&value)?);
        return Ok(());
    }

    println!("related session {}", session.session_key);
    for related in sessions {
        println!("- session {} {}", related.session_key, related.session_id);
    }
    Ok(())
}

pub fn run_eval(args: EvalArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    let fixture: EvalFixture = serde_json::from_slice(&fs::read(&args.fixture)?)?;
    let mut passed = 0_u64;
    let mut failed = 0_u64;
    let mut cases_json = Vec::new();

    for case in fixture.cases {
        let (status, details) = match case.command.as_str() {
            "search" => {
                let query = case
                    .query
                    .as_deref()
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .ok_or_else(|| {
                        anyhow::anyhow!("eval search case `{}` is missing `query`", case.name)
                    })?;
                let (_, results) = store.search_with_trace(SearchOptions::new(
                    query.to_owned(),
                    case.limit.unwrap_or(1),
                ))?;
                let actual = results.first().map(|result| result.session_id.clone());
                if actual == case.expected.session_id {
                    passed += 1;
                    ("pass", actual.unwrap_or_default())
                } else {
                    failed += 1;
                    ("fail", actual.unwrap_or_else(|| "no result".to_owned()))
                }
            }
            "memories" => {
                let query = case
                    .query
                    .as_deref()
                    .map(str::trim)
                    .filter(|value| !value.is_empty())
                    .ok_or_else(|| {
                        anyhow::anyhow!("eval memories case `{}` is missing `query`", case.name)
                    })?;
                let (_, results) = store.memory_results_with_trace(MemorySearchOptions {
                    query: Some(query.to_owned()),
                    limit: case.limit.unwrap_or(1),
                    ..MemorySearchOptions::default()
                })?;
                let actual = results.first();
                let kind_ok = case
                    .expected
                    .top_memory_kind
                    .as_deref()
                    .or(case.expected.kind.as_deref())
                    .map(|kind| actual.map(|item| item.object.kind.as_str()) == Some(kind))
                    .unwrap_or(true);
                let summary_ok = case
                    .expected
                    .top_memory_summary_contains
                    .as_deref()
                    .or(case.expected.summary_contains.as_deref())
                    .map(|fragment| {
                        actual
                            .map(|item| item.object.summary.contains(fragment))
                            .unwrap_or(false)
                    })
                    .unwrap_or(true);
                if kind_ok && summary_ok {
                    passed += 1;
                    (
                        "pass",
                        actual
                            .map(|item| item.object.id.clone())
                            .unwrap_or_default(),
                    )
                } else {
                    failed += 1;
                    (
                        "fail",
                        actual
                            .map(|item| item.object.summary.clone())
                            .unwrap_or_else(|| "no result".to_owned()),
                    )
                }
            }
            "delta" => {
                let items =
                    store.delta_items(case.cursor.as_deref(), case.limit.unwrap_or(20), None)?;
                let next_cursor = items.last().map(encode_delta_cursor);
                let session_ok = case
                    .expected
                    .contains_session_id
                    .as_deref()
                    .map(|session_id| {
                        items.iter().any(|item| {
                            matches!(
                                item,
                                DeltaItem::Session { session_id: item_session_id, .. }
                                    if item_session_id == session_id
                            )
                        })
                    })
                    .unwrap_or(true);
                let memory_ok = case
                    .expected
                    .contains_memory_kind
                    .as_deref()
                    .map(|kind| {
                        items.iter().any(|item| {
                            matches!(
                                item,
                                DeltaItem::Memory { object, .. }
                                    if object.kind.as_str() == kind
                            )
                        })
                    })
                    .unwrap_or(true);
                let cursor_ok = case
                    .expected
                    .next_cursor_present
                    .map(|expected| next_cursor.is_some() == expected)
                    .unwrap_or(true);

                if session_ok && memory_ok && cursor_ok {
                    passed += 1;
                    (
                        "pass",
                        next_cursor.unwrap_or_else(|| format!("{} items", items.len())),
                    )
                } else {
                    failed += 1;
                    (
                        "fail",
                        format!(
                            "items={} next_cursor={}",
                            items.len(),
                            next_cursor.unwrap_or_default()
                        ),
                    )
                }
            }
            other => {
                failed += 1;
                ("fail", format!("unsupported command `{other}`"))
            }
        };

        cases_json.push(json!({
            "name": case.name,
            "command": case.command,
            "status": status,
            "details": details,
        }));
    }

    if args.json {
        let value = json!({
            "object": "eval_result",
            "passed": passed,
            "failed": failed,
            "cases": cases_json,
        });
        println!("{}", serde_json::to_string_pretty(&value)?);
        return Ok(());
    }

    println!("passed: {passed}");
    println!("failed: {failed}");
    Ok(())
}

pub fn run_resources(args: ResourcesArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    let mut resources = Vec::new();
    match args.kind {
        ResourceKindArg::Session => resources.extend(store.session_resources(args.limit)?),
        ResourceKindArg::Memory => resources.extend(store.memory_resources(args.limit)?),
        ResourceKindArg::All => {
            resources.extend(store.memory_resources(args.limit)?);
            resources.extend(store.session_resources(args.limit)?);
            resources.sort_by(|left, right| {
                right
                    .updated_at
                    .cmp(&left.updated_at)
                    .then_with(|| left.uri.cmp(&right.uri))
            });
            resources.truncate(args.limit);
        }
    }

    if args.json {
        let value = json!({
            "object": "resource_list",
            "count": resources.len(),
            "resources": resources.iter().map(resource_json_value).collect::<Vec<_>>(),
        });
        println!("{}", serde_json::to_string_pretty(&value)?);
        return Ok(());
    }

    for resource in &resources {
        println!("{}  {}", resource.object_type, resource.uri);
    }
    Ok(())
}

pub fn run_read_resource(args: ReadResourceArgs) -> Result<()> {
    let db_path = args.db.unwrap_or(default_db_path()?);
    let store = Store::open_readonly(&db_path)?;
    if let Some(memory_id) = args.uri.strip_prefix("codex-recall://memory/") {
        let Some(memory) = store.memory_by_id(memory_id)? else {
            bail!("unknown memory resource `{}`", args.uri);
        };
        let evidence = store.memory_evidence(memory_id, 50)?;
        let value = memory_json_value(&memory, &evidence, false, MatchStrategy::AllTerms);
        println!("{}", serde_json::to_string_pretty(&value)?);
        return Ok(());
    }
    if let Some(session_key) = args.uri.strip_prefix("codex-recall://session/") {
        let matches = store.resolve_session_reference(session_key)?;
        if matches.is_empty() {
            bail!("unknown session resource `{}`", args.uri);
        }
        let session = &matches[0];
        let events = store.session_events(session_key, 120)?;
        let value = json!({
            "object": "session",
            "session_key": session.session_key,
            "session_id": session.session_id,
            "repo": session.repo,
            "cwd": session.cwd,
            "resource_uri": args.uri,
            "events": events.iter().map(|event| json!({
                "kind": event.kind.as_str(),
                "text": event.text,
                "cwd": event.cwd,
                "source_file_path": event.source_file_path,
                "source_line_number": event.source_line_number,
                "source_timestamp": event.source_timestamp,
            })).collect::<Vec<_>>(),
        });
        println!("{}", serde_json::to_string_pretty(&value)?);
        return Ok(());
    }

    bail!("unsupported resource uri `{}`", args.uri)
}

fn print_memories_json(
    memories: &[MemoryResult],
    match_strategy: MatchStrategy,
    include_trace: bool,
) -> Result<()> {
    let value = json!({
        "object": "list",
        "type": "memory",
        "count": memories.len(),
        "match_strategy": match_strategy.as_str(),
        "results": memories
            .iter()
            .map(|memory| memory_result_json(memory, include_trace, match_strategy))
            .collect::<Vec<_>>(),
    });
    println!("{}", serde_json::to_string_pretty(&value)?);
    Ok(())
}

fn print_memory_json(memory: &MemoryObject, evidence: &[MemoryEvidence]) -> Result<()> {
    let value = memory_json_value(memory, evidence, false, MatchStrategy::AllTerms);
    println!("{}", serde_json::to_string_pretty(&value)?);
    Ok(())
}

fn memory_json_value(
    memory: &MemoryObject,
    evidence: &[MemoryEvidence],
    include_trace: bool,
    match_strategy: MatchStrategy,
) -> serde_json::Value {
    let mut value = json!({
        "object": "memory",
        "id": memory.id,
        "kind": memory.kind.as_str(),
        "summary": memory.summary,
        "normalized_text": memory.normalized_text,
        "first_seen_at": memory.first_seen_at,
        "last_seen_at": memory.last_seen_at,
        "created_at": memory.created_at,
        "updated_at": memory.updated_at,
        "evidence_count": memory.evidence_count,
        "resource_uri": format!("codex-recall://memory/{}", memory.id),
        "evidence": evidence.iter().map(evidence_json_value).collect::<Vec<_>>(),
    });
    if include_trace {
        value["trace"] = json!({
            "match_strategy": match_strategy.as_str(),
            "evidence_count": memory.evidence_count,
        });
    }
    value
}

fn memory_result_json(
    memory: &MemoryResult,
    include_trace: bool,
    match_strategy: MatchStrategy,
) -> serde_json::Value {
    let mut value = json!({
        "object": "memory",
        "id": memory.object.id,
        "kind": memory.object.kind.as_str(),
        "summary": memory.object.summary,
        "normalized_text": memory.object.normalized_text,
        "first_seen_at": memory.object.first_seen_at,
        "last_seen_at": memory.object.last_seen_at,
        "created_at": memory.object.created_at,
        "updated_at": memory.object.updated_at,
        "evidence_count": memory.object.evidence_count,
        "repos": memory.repos,
        "session_keys": memory.session_keys,
        "resource_uri": format!("codex-recall://memory/{}", memory.object.id),
    });
    if include_trace {
        value["trace"] = json!({
            "match_strategy": match_strategy.as_str(),
            "evidence_count": memory.object.evidence_count,
            "repo_count": value["repos"].as_array().map(|repos| repos.len()).unwrap_or_default(),
            "session_count": value["session_keys"].as_array().map(|sessions| sessions.len()).unwrap_or_default(),
        });
    }
    value
}

fn evidence_json_value(evidence: &MemoryEvidence) -> serde_json::Value {
    json!({
        "session_key": evidence.session_key,
        "session_id": evidence.session_id,
        "repo": evidence.repo,
        "cwd": evidence.cwd,
        "session_timestamp": evidence.session_timestamp,
        "source_file_path": evidence.source_file_path,
        "source_line_number": evidence.source_line_number,
        "source_timestamp": evidence.source_timestamp,
        "event_kind": evidence.event_kind.as_str(),
        "evidence_text": compact_whitespace(&evidence.evidence_text),
        "resource_uri": format!("codex-recall://session/{}", evidence.session_key),
    })
}

fn delta_item_json(item: &DeltaItem) -> serde_json::Value {
    match item {
        DeltaItem::Session {
            change_id,
            action,
            session_key,
            session_id,
            repo,
            cwd,
            session_timestamp,
            updated_at,
        } => json!({
            "object": "session",
            "change_id": change_id,
            "change_kind": item.change_kind(),
            "action": action,
            "session_key": session_key,
            "session_id": session_id,
            "repo": repo,
            "cwd": cwd,
            "session_timestamp": session_timestamp,
            "updated_at": updated_at,
            "resource_uri": format!("codex-recall://session/{}", session_key),
        }),
        DeltaItem::Memory {
            change_id,
            action,
            object,
            repos,
            session_keys,
        } => json!({
            "object": "memory",
            "change_id": change_id,
            "change_kind": item.change_kind(),
            "action": action,
            "id": object.id,
            "kind": object.kind.as_str(),
            "summary": object.summary,
            "updated_at": object.updated_at,
            "evidence_count": object.evidence_count,
            "repos": repos,
            "session_keys": session_keys,
            "resource_uri": format!("codex-recall://memory/{}", object.id),
        }),
        DeltaItem::Deleted {
            change_id,
            object_type,
            object_id,
            action,
            updated_at,
        } => json!({
            "object": "deleted",
            "change_id": change_id,
            "change_kind": item.change_kind(),
            "action": action,
            "object_type": object_type,
            "id": object_id,
            "updated_at": updated_at,
        }),
    }
}

fn resource_json_value(resource: &ResourceRecord) -> serde_json::Value {
    json!({
        "uri": resource.uri,
        "name": resource.name,
        "description": resource.description,
        "mime_type": resource.mime_type,
        "object_type": resource.object_type,
        "updated_at": resource.updated_at,
    })
}

fn session_json_value(session: &RecentSession) -> serde_json::Value {
    json!({
        "object": "session",
        "session_key": session.session_key,
        "session_id": session.session_id,
        "repo": session.repo,
        "cwd": session.cwd,
        "session_timestamp": session.session_timestamp,
        "resource_uri": format!("codex-recall://session/{}", session.session_key),
    })
}