zeph-memory 0.22.0

Semantic memory with SQLite and Qdrant for Zeph agent
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Sealed [`IngestSourceAdapter`] trait and its MVP implementations (spec-067 ยง2.3).
//!
//! Adapters are pure (no I/O) โ€” they receive raw text (or pre-parsed JSONL) and produce
//! [`super::IngestDocument`] values. All I/O (disk reads, network) happens at the binary
//! layer before the adapter is invoked.
//!
//! External-agent adapters ([`ClaudeCodeJsonl`], [`CodexJsonl`]) are **strict and
//! version-pinned**: any record whose structure deviates from the pinned schema causes an
//! `Err` return rather than silent best-effort parsing. This is spec-067 Phase 3 requirement
//! D1 โ€” unknown schema versions must fail loud.

use serde::Deserialize;
use zeph_llm::provider::Message;

use crate::MemoryError;
use crate::graph::ingest::document::IngestSourceKind;
use crate::graph::types::{GraphOrigin, GraphProvenance};

use super::document::IngestDocument;
use super::report::ImportBatchId;

mod sealed {
    pub trait Sealed {}
}

/// Build one [`IngestDocument`] per non-empty `texts[i]`, pairing it with `ids[i]` and the
/// `context_window` preceding texts.
///
/// Shared by every [`IngestSourceAdapter`] impl in this module โ€” they differ only in how
/// `source_uri` is formatted and which [`GraphOrigin`] tags the provenance.
fn build_ingest_documents(
    texts: &[String],
    ids: &[String],
    context_window: usize,
    batch_id: &ImportBatchId,
    origin: GraphOrigin,
    source_uri: impl Fn(&str) -> String,
) -> Vec<IngestDocument> {
    let mut docs = Vec::with_capacity(texts.len());
    for (i, (content, id)) in texts.iter().zip(ids.iter()).enumerate() {
        if content.trim().is_empty() {
            continue;
        }
        let start = i.saturating_sub(context_window);
        let ctx: Vec<String> = texts[start..i].to_vec();
        let provenance = GraphProvenance {
            origin,
            import_batch_id: batch_id.as_str().to_owned(),
            source_uri: Some(source_uri(id)),
        };
        docs.push(IngestDocument::new(content.clone(), ctx, provenance));
    }
    docs
}

/// Adapter that converts raw source material into [`IngestDocument`] values.
///
/// The trait is sealed โ€” only implementations in `zeph-memory` are permitted.
/// This preserves the valid-by-construction invariants of [`IngestDocument`]:
/// non-empty content, non-empty source URI, and a hash computed from content.
///
/// # Design
///
/// Adapters are pure: no async, no I/O, no external crate dependencies beyond
/// `zeph-llm` (which `zeph-memory` already depends on). The binary layer reads
/// raw bytes off disk and passes the string in via [`IngestSourceAdapter::parse`].
pub trait IngestSourceAdapter: sealed::Sealed + Send + Sync {
    /// Parse `raw` source material into a list of [`IngestDocument`] values.
    ///
    /// `batch_id` is embedded in every document's provenance so that all documents
    /// produced in one `ingest_documents` call share the same rollback key.
    ///
    /// # Contract
    ///
    /// Implementations SHOULD skip malformed individual records (e.g. a single
    /// unparseable JSONL line) and continue parsing the rest of the input โ€” partial
    /// parse errors are not fatal.  `Err` is returned only for an irrecoverable
    /// format error where no documents could be produced from `raw` at all.
    ///
    /// # Errors
    ///
    /// Returns [`MemoryError::Ingest`] only for irrecoverable format errors where
    /// no documents could be produced from `raw`. Malformed individual records are
    /// skipped with a warning and do not cause an `Err` return.
    fn parse(
        &self,
        raw: &str,
        batch_id: &ImportBatchId,
    ) -> Result<Vec<IngestDocument>, MemoryError>;
}

/// A single entry in a subagent JSONL transcript.
///
/// The binary (`zeph-subagent`) writes transcripts as one JSON object per line.
/// Each entry wraps a [`Message`] produced by the LLM or the agent.
///
/// Only `message` is used by the adapter โ€” `seq` and `timestamp` are available
/// for diagnostic purposes but are not stored in the graph.
#[derive(Debug, Deserialize)]
pub struct TranscriptEntry {
    /// Zero-based sequence number within the session.
    pub seq: u64,
    /// ISO-8601 timestamp (opaque string, not parsed).
    pub timestamp: Option<String>,
    /// The LLM or agent message for this turn.
    pub message: Message,
}

/// Adapter for subagent JSONL transcripts.
///
/// Each line in the transcript is a [`TranscriptEntry`]. The adapter produces one
/// [`IngestDocument`] per entry whose flat text content is non-empty. Context for
/// each entry is the plain text of the `context_window` preceding entries.
///
/// # Examples
///
/// ```no_run
/// use zeph_memory::graph::ingest::{SubagentJsonl, IngestSourceAdapter, ImportBatchId};
///
/// let raw = r#"{"seq":0,"timestamp":"2026-01-01T00:00:00Z","message":{"role":"user","content":"hello","parts":[]}}"#;
/// let adapter = SubagentJsonl::new("task-42");
/// let batch = ImportBatchId::new();
/// let docs = adapter.parse(raw, &batch).unwrap();
/// assert!(!docs.is_empty());
/// ```
pub struct SubagentJsonl {
    task_id: String,
    /// Number of preceding messages used as extraction context.
    context_window: usize,
}

impl SubagentJsonl {
    /// Creates a new `SubagentJsonl` adapter for the given task ID.
    ///
    /// `task_id` is embedded in every document's `source_uri` as
    /// `"subagent:<task_id>#<seq>"`.
    ///
    /// The `context_window` defaults to 3 preceding messages.
    #[must_use]
    pub fn new(task_id: impl Into<String>) -> Self {
        Self {
            task_id: task_id.into(),
            context_window: 3,
        }
    }

    /// Overrides the context window size.
    #[must_use]
    pub fn with_context_window(mut self, n: usize) -> Self {
        self.context_window = n;
        self
    }
}

impl sealed::Sealed for SubagentJsonl {}

impl IngestSourceAdapter for SubagentJsonl {
    /// Parse a JSONL transcript string.
    ///
    /// Lines that fail to parse are skipped with a warning; entries whose flat
    /// text content is empty are also skipped (nothing to extract).
    fn parse(
        &self,
        raw: &str,
        batch_id: &ImportBatchId,
    ) -> Result<Vec<IngestDocument>, MemoryError> {
        let entries: Vec<TranscriptEntry> = raw
            .lines()
            .filter(|l| !l.trim().is_empty())
            .filter_map(|line| match serde_json::from_str::<TranscriptEntry>(line) {
                Ok(e) => Some(e),
                Err(err) => {
                    tracing::warn!("SubagentJsonl: skipping malformed line: {err}");
                    None
                }
            })
            .collect();

        let texts: Vec<String> = entries
            .iter()
            .map(|e| e.message.to_llm_content().to_owned())
            .collect();
        let ids: Vec<String> = entries.iter().map(|e| e.seq.to_string()).collect();

        Ok(build_ingest_documents(
            &texts,
            &ids,
            self.context_window,
            batch_id,
            IngestSourceKind::SubagentTranscript.graph_origin(),
            |id| format!("subagent:{}#{id}", self.task_id),
        ))
    }
}

// โ”€โ”€ Claude Code JSONL adapter โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

/// Strict raw-record type for one line in a Claude Code `.jsonl` session file.
///
/// Only `type == "user" | "assistant"` lines are accepted; all other types
/// (mode, permission-mode, file-history-snapshot, โ€ฆ) are silently skipped.
/// A user/assistant line that lacks `message.role` is a schema-version error.
#[derive(Debug, Deserialize)]
struct ClaudeCodeRecord {
    #[serde(rename = "type")]
    record_type: String,
    #[serde(default)]
    uuid: Option<String>,
    message: Option<ClaudeCodeMessage>,
}

/// `message` field inside a Claude Code user/assistant record.
#[derive(Debug, Deserialize)]
struct ClaudeCodeMessage {
    role: String,
    content: ClaudeCodeContent,
}

/// Content in a Claude Code message: either a plain string or an array of blocks.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum ClaudeCodeContent {
    Text(String),
    Blocks(Vec<ClaudeCodeBlock>),
}

/// One block inside a Claude Code content array.
#[derive(Debug, Deserialize)]
struct ClaudeCodeBlock {
    #[serde(rename = "type")]
    block_type: String,
    /// Present in `text` and `thinking` blocks.
    #[serde(default)]
    text: Option<String>,
    /// Present in `tool_use` blocks.
    #[serde(default)]
    name: Option<String>,
}

impl ClaudeCodeContent {
    /// Flatten all extractable parts into a single string.
    ///
    /// Extracts `text` and `thinking` blocks verbatim.
    /// For `tool_use` blocks, appends a short `<tool: name>` marker so that
    /// tool-heavy assistant turns are not silently dropped.
    fn to_text(&self) -> String {
        match self {
            Self::Text(s) => s.clone(),
            Self::Blocks(blocks) => blocks
                .iter()
                .filter_map(|b| match b.block_type.as_str() {
                    "text" | "thinking" => b.text.as_deref().map(str::to_owned),
                    "tool_use" => Some(format!(
                        "<tool: {}>",
                        b.name.as_deref().unwrap_or("unknown")
                    )),
                    _ => None,
                })
                .collect::<Vec<_>>()
                .join("\n"),
        }
    }
}

/// Adapter for Claude Code session `.jsonl` files (spec-067 ยง9 Phase 3).
///
/// **Strict and version-pinned**: only `"type": "user"` and `"type": "assistant"` records
/// are processed. All other record types are silently skipped. A user/assistant record
/// that lacks the expected `message` structure causes an `Err` โ€” no silent mis-parsing.
///
/// Files are expected to be scoped to the current project by the calling binary layer
/// (path enforcement via `~/.claude/projects/<project-slug>/`). This adapter performs
/// no I/O and no path checks; it receives raw text from the binary layer.
///
/// # Examples
///
/// ```no_run
/// use zeph_memory::graph::ingest::{ClaudeCodeJsonl, IngestSourceAdapter, ImportBatchId};
///
/// let raw = r#"{"type":"user","uuid":"abc","message":{"role":"user","content":"hello"}}"#;
/// let adapter = ClaudeCodeJsonl::new("session-42");
/// let batch = ImportBatchId::new();
/// let docs = adapter.parse(raw, &batch).unwrap();
/// assert!(!docs.is_empty());
/// ```
pub struct ClaudeCodeJsonl {
    session_id: String,
    context_window: usize,
}

impl ClaudeCodeJsonl {
    /// Creates a new `ClaudeCodeJsonl` adapter for the given session ID.
    ///
    /// `session_id` is embedded in every document's `source_uri` as
    /// `"claude-code:<session_id>#<uuid>"`.
    ///
    /// The `context_window` defaults to 3 preceding messages.
    #[must_use]
    pub fn new(session_id: impl Into<String>) -> Self {
        Self {
            session_id: session_id.into(),
            context_window: 3,
        }
    }

    /// Overrides the context window size.
    #[must_use]
    pub fn with_context_window(mut self, n: usize) -> Self {
        self.context_window = n;
        self
    }
}

impl sealed::Sealed for ClaudeCodeJsonl {}

impl IngestSourceAdapter for ClaudeCodeJsonl {
    /// Parse a Claude Code `.jsonl` session file.
    ///
    /// Skips non-user/assistant records silently. A `user` or `assistant` record
    /// that lacks the expected `message` structure is a schema error and causes `Err`.
    fn parse(
        &self,
        raw: &str,
        batch_id: &ImportBatchId,
    ) -> Result<Vec<IngestDocument>, MemoryError> {
        let mut texts: Vec<String> = Vec::new();
        let mut uuids: Vec<String> = Vec::new();

        for (lineno, line) in raw.lines().enumerate() {
            if line.trim().is_empty() {
                continue;
            }

            let record: ClaudeCodeRecord = serde_json::from_str(line).map_err(|e| {
                MemoryError::Ingest(format!(
                    "ClaudeCodeJsonl: line {lineno} JSON parse error: {e}"
                ))
            })?;

            // Skip non-conversation record types silently.
            if record.record_type != "user" && record.record_type != "assistant" {
                continue;
            }

            // A user/assistant record MUST have a valid `message` โ€” fail loud.
            let msg = record.message.ok_or_else(|| {
                MemoryError::Ingest(format!(
                    "ClaudeCodeJsonl: line {lineno} has type={:?} but missing `message` field \
                     โ€” unexpected schema version",
                    record.record_type
                ))
            })?;

            // role must be "user" or "assistant"
            if msg.role != "user" && msg.role != "assistant" {
                return Err(MemoryError::Ingest(format!(
                    "ClaudeCodeJsonl: line {lineno} unexpected message.role={:?}",
                    msg.role
                )));
            }

            texts.push(msg.content.to_text());
            uuids.push(record.uuid.unwrap_or_else(|| format!("line{lineno}")));
        }

        Ok(build_ingest_documents(
            &texts,
            &uuids,
            self.context_window,
            batch_id,
            IngestSourceKind::ExternalAgent.graph_origin(),
            |uuid| format!("claude-code:{}#{uuid}", self.session_id),
        ))
    }
}

// โ”€โ”€ Codex JSONL adapter โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

/// Strict raw-record type for one line in an `OpenAI` Codex CLI `.jsonl` session file.
///
/// Only `type == "response_item"` records with `payload.type == "message"` and
/// `payload.role` in `["user", "assistant"]` are accepted. The `session_meta` record
/// at the start of each file is used by the binary layer for project-scope enforcement
/// but is skipped by this adapter. All other record types are silently skipped.
#[derive(Debug, Deserialize)]
struct CodexRecord {
    #[serde(rename = "type")]
    record_type: String,
    #[serde(default)]
    payload: Option<CodexPayload>,
}

/// `payload` field in a Codex record.
#[derive(Debug, Deserialize)]
struct CodexPayload {
    #[serde(rename = "type")]
    payload_type: Option<String>,
    role: Option<String>,
    content: Option<Vec<CodexContentBlock>>,
    id: Option<String>,
}

/// One content block inside a Codex message.
#[derive(Debug, Deserialize)]
struct CodexContentBlock {
    #[serde(rename = "type")]
    block_type: String,
    text: Option<String>,
}

/// Adapter for `OpenAI` Codex CLI session `.jsonl` files (spec-067 ยง9 Phase 3).
///
/// **Strict and version-pinned**: only `"type": "response_item"` records with
/// `payload.type == "message"` and `payload.role` in `["user", "assistant"]` are
/// processed. Records with `role == "developer"` (system instructions) are skipped.
/// Unknown structure in a `response_item` causes an `Err`.
///
/// Project-scope enforcement (only files whose `session_meta.payload.cwd` matches the
/// current project root) is performed by the binary layer, not this adapter.
///
/// # Examples
///
/// ```no_run
/// use zeph_memory::graph::ingest::{CodexJsonl, IngestSourceAdapter, ImportBatchId};
///
/// let raw = concat!(
///     r#"{"type":"session_meta","payload":{"id":"s1","cwd":"/project"}}"#, "\n",
///     r#"{"type":"response_item","payload":{"type":"message","role":"user","id":"i1","content":[{"type":"input_text","text":"hello"}]}}"#
/// );
/// let adapter = CodexJsonl::new("session-1");
/// let batch = ImportBatchId::new();
/// let docs = adapter.parse(raw, &batch).unwrap();
/// assert_eq!(docs.len(), 1);
/// ```
pub struct CodexJsonl {
    session_id: String,
    context_window: usize,
}

impl CodexJsonl {
    /// Creates a new `CodexJsonl` adapter for the given session ID.
    ///
    /// `session_id` is embedded in every document's `source_uri` as
    /// `"codex:<session_id>#<item_id>"`.
    #[must_use]
    pub fn new(session_id: impl Into<String>) -> Self {
        Self {
            session_id: session_id.into(),
            context_window: 3,
        }
    }

    /// Overrides the context window size.
    #[must_use]
    pub fn with_context_window(mut self, n: usize) -> Self {
        self.context_window = n;
        self
    }
}

impl sealed::Sealed for CodexJsonl {}

impl IngestSourceAdapter for CodexJsonl {
    /// Parse a Codex CLI `.jsonl` session file.
    ///
    /// Skips `session_meta`, `developer`-role messages, and all other non-message
    /// record types silently. A `response_item` record whose `payload` is missing or
    /// structurally invalid causes `Err`.
    fn parse(
        &self,
        raw: &str,
        batch_id: &ImportBatchId,
    ) -> Result<Vec<IngestDocument>, MemoryError> {
        let mut texts: Vec<String> = Vec::new();
        let mut item_ids: Vec<String> = Vec::new();

        for (lineno, line) in raw.lines().enumerate() {
            if line.trim().is_empty() {
                continue;
            }

            let record: CodexRecord = serde_json::from_str(line).map_err(|e| {
                MemoryError::Ingest(format!("CodexJsonl: line {lineno} JSON parse error: {e}"))
            })?;

            // Skip session_meta and all other non-response_item records.
            if record.record_type != "response_item" {
                continue;
            }

            // response_item MUST have a payload โ€” fail loud.
            let payload = record.payload.ok_or_else(|| {
                MemoryError::Ingest(format!(
                    "CodexJsonl: line {lineno} has type=response_item but missing `payload` \
                     โ€” unexpected schema version"
                ))
            })?;

            // Must be a message payload.
            let payload_type = payload.payload_type.as_deref().unwrap_or("");
            if payload_type != "message" {
                continue; // tool_call, tool_output, etc. โ€” skip silently
            }

            let role = payload.role.as_deref().unwrap_or("");
            match role {
                "user" | "assistant" => {}
                "developer" => continue, // system-level instructions โ€” skip
                _ => {
                    return Err(MemoryError::Ingest(format!(
                        "CodexJsonl: line {lineno} unexpected payload.role={role:?}"
                    )));
                }
            }

            // Extract text from input_text (user turns) and output_text (assistant turns).
            let content_text: String = payload
                .content
                .as_deref()
                .unwrap_or_default()
                .iter()
                .filter(|b| b.block_type == "input_text" || b.block_type == "output_text")
                .filter_map(|b| b.text.as_deref())
                .collect::<Vec<_>>()
                .join("\n");

            let item_id = payload.id.unwrap_or_else(|| format!("line{lineno}"));

            texts.push(content_text);
            item_ids.push(item_id);
        }

        Ok(build_ingest_documents(
            &texts,
            &item_ids,
            self.context_window,
            batch_id,
            IngestSourceKind::ExternalAgent.graph_origin(),
            |item_id| format!("codex:{}#{item_id}", self.session_id),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::types::GraphOrigin;

    fn make_jsonl(entries: &[(u64, &str)]) -> String {
        entries
            .iter()
            .map(|(seq, text)| {
                format!(
                    r#"{{"seq":{seq},"timestamp":null,"message":{{"role":"user","content":{text:?},"parts":[]}}}}"#,
                )
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn parse_single_entry() {
        let raw = make_jsonl(&[(0, "Rust is a systems language")]);
        let adapter = SubagentJsonl::new("task-1");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "Rust is a systems language");
        assert_eq!(docs[0].source_uri(), "subagent:task-1#0");
        assert!(!docs[0].content_hash().is_empty());
    }

    #[test]
    fn parse_skips_empty_content() {
        let raw = make_jsonl(&[(0, ""), (1, "non-empty content")]);
        let adapter = SubagentJsonl::new("task-2");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "non-empty content");
    }

    #[test]
    fn parse_builds_context_window() {
        let raw = make_jsonl(&[
            (0, "first message"),
            (1, "second message"),
            (2, "third message"),
            (3, "fourth message"),
        ]);
        let adapter = SubagentJsonl::new("task-3").with_context_window(2);
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 4);
        assert!(docs[0].context().is_empty());
        assert_eq!(docs[1].context().len(), 1);
        assert_eq!(docs[2].context().len(), 2);
        assert_eq!(docs[3].context().len(), 2); // capped at window size
    }

    #[test]
    fn parse_skips_malformed_lines() {
        let raw = "not json\n".to_owned() + &make_jsonl(&[(0, "valid message")]);
        let adapter = SubagentJsonl::new("task-4");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
    }

    #[test]
    fn parse_embeds_batch_id_in_provenance() {
        let raw = make_jsonl(&[(0, "content")]);
        let adapter = SubagentJsonl::new("task-5");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs[0].provenance().import_batch_id, batch.as_str());
    }

    #[test]
    fn parse_empty_raw_returns_empty_vec() {
        let adapter = SubagentJsonl::new("task-6");
        let batch = ImportBatchId::new();
        let docs = adapter.parse("", &batch).unwrap();
        assert!(docs.is_empty());
    }

    // โ”€โ”€ ClaudeCodeJsonl tests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    fn make_claude_code_user(uuid: &str, text: &str) -> String {
        format!(
            r#"{{"type":"user","uuid":"{uuid}","message":{{"role":"user","content":{text:?}}}}}"#
        )
    }

    fn make_claude_code_assistant(uuid: &str, text: &str) -> String {
        format!(
            r#"{{"type":"assistant","uuid":"{uuid}","message":{{"role":"assistant","content":[{{"type":"text","text":{text:?}}}]}}}}"#
        )
    }

    fn make_claude_code_non_conversation(record_type: &str) -> String {
        format!(r#"{{"type":"{record_type}","sessionId":"s1"}}"#)
    }

    #[test]
    fn claude_code_parses_user_message() {
        let raw = make_claude_code_user("uuid-1", "Hello from user");
        let adapter = ClaudeCodeJsonl::new("sess-1");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "Hello from user");
        assert_eq!(docs[0].source_uri(), "claude-code:sess-1#uuid-1");
        assert_eq!(docs[0].provenance().origin, GraphOrigin::ExternalAgent);
    }

    #[test]
    fn claude_code_parses_assistant_array_content() {
        let raw = make_claude_code_assistant("uuid-2", "Hello from assistant");
        let adapter = ClaudeCodeJsonl::new("sess-2");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "Hello from assistant");
    }

    #[test]
    fn claude_code_skips_non_conversation_records() {
        let raw = [
            make_claude_code_non_conversation("mode"),
            make_claude_code_non_conversation("permission-mode"),
            make_claude_code_non_conversation("file-history-snapshot"),
            make_claude_code_user("uuid-3", "actual content"),
        ]
        .join("\n");
        let adapter = ClaudeCodeJsonl::new("sess-3");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "actual content");
    }

    #[test]
    fn claude_code_errors_on_missing_message_in_user_record() {
        let raw = r#"{"type":"user","uuid":"u1"}"#; // no "message" field
        let adapter = ClaudeCodeJsonl::new("sess-err");
        let batch = ImportBatchId::new();
        assert!(adapter.parse(raw, &batch).is_err());
    }

    #[test]
    fn claude_code_skips_empty_content() {
        let raw = [
            make_claude_code_user("u1", ""),
            make_claude_code_user("u2", "non-empty"),
        ]
        .join("\n");
        let adapter = ClaudeCodeJsonl::new("sess-4");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "non-empty");
    }

    #[test]
    fn claude_code_tags_external_agent_origin() {
        let raw = make_claude_code_user("u1", "some content");
        let adapter = ClaudeCodeJsonl::new("sess-5");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs[0].provenance().origin, GraphOrigin::ExternalAgent);
    }

    #[test]
    fn claude_code_embeds_batch_id_in_provenance() {
        let raw = make_claude_code_user("u1", "content");
        let adapter = ClaudeCodeJsonl::new("sess-6");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs[0].provenance().import_batch_id, batch.as_str());
    }

    #[test]
    fn claude_code_context_window() {
        let raw = [
            make_claude_code_user("u1", "first"),
            make_claude_code_user("u2", "second"),
            make_claude_code_user("u3", "third"),
            make_claude_code_user("u4", "fourth"),
        ]
        .join("\n");
        let adapter = ClaudeCodeJsonl::new("sess-7").with_context_window(2);
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 4);
        assert!(docs[0].context().is_empty());
        assert_eq!(docs[1].context().len(), 1);
        assert_eq!(docs[2].context().len(), 2);
        assert_eq!(docs[3].context().len(), 2); // capped at window
    }

    #[test]
    fn claude_code_thinking_block_extracted() {
        // Assistant record with only a thinking block โ€” must not be dropped.
        let raw = r#"{"type":"assistant","uuid":"u1","message":{"role":"assistant","content":[{"type":"thinking","text":"inner reasoning"}]}}"#;
        let adapter = ClaudeCodeJsonl::new("sess-think");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "inner reasoning");
    }

    #[test]
    fn claude_code_tool_use_block_produces_marker() {
        let raw = r#"{"type":"assistant","uuid":"u1","message":{"role":"assistant","content":[{"type":"tool_use","name":"shell","text":null}]}}"#;
        let adapter = ClaudeCodeJsonl::new("sess-tool");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert!(docs[0].content().contains("<tool: shell>"));
    }

    #[test]
    fn claude_code_empty_raw() {
        let adapter = ClaudeCodeJsonl::new("sess-empty");
        let batch = ImportBatchId::new();
        let docs = adapter.parse("", &batch).unwrap();
        assert!(docs.is_empty());
    }

    // โ”€โ”€ CodexJsonl tests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

    fn make_codex_session_meta(cwd: &str) -> String {
        format!(
            r#"{{"type":"session_meta","payload":{{"id":"s1","cwd":"{cwd}","originator":"codex_cli_rs","cli_version":"1.0.0"}}}}"#
        )
    }

    fn make_codex_response_item(role: &str, text: &str, id: &str) -> String {
        format!(
            r#"{{"type":"response_item","payload":{{"type":"message","role":"{role}","id":"{id}","content":[{{"type":"input_text","text":{text:?}}}]}}}}"#
        )
    }

    fn make_codex_assistant(text: &str, id: &str) -> String {
        format!(
            r#"{{"type":"response_item","payload":{{"type":"message","role":"assistant","id":"{id}","content":[{{"type":"output_text","text":{text:?}}}]}}}}"#
        )
    }

    #[test]
    fn codex_parses_user_message() {
        let raw = [
            make_codex_session_meta("/project"),
            make_codex_response_item("user", "Hello", "item-1"),
        ]
        .join("\n");
        let adapter = CodexJsonl::new("sess-c1");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "Hello");
        assert_eq!(docs[0].source_uri(), "codex:sess-c1#item-1");
        assert_eq!(docs[0].provenance().origin, GraphOrigin::ExternalAgent);
    }

    #[test]
    fn codex_skips_developer_role() {
        let raw = [
            make_codex_session_meta("/project"),
            make_codex_response_item("developer", "System instructions", "sys-1"),
            make_codex_response_item("user", "User message", "u-1"),
        ]
        .join("\n");
        let adapter = CodexJsonl::new("sess-c2");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "User message");
    }

    #[test]
    fn codex_errors_on_missing_payload_in_response_item() {
        let raw = r#"{"type":"response_item"}"#; // no payload
        let adapter = CodexJsonl::new("sess-c-err");
        let batch = ImportBatchId::new();
        assert!(adapter.parse(raw, &batch).is_err());
    }

    #[test]
    fn codex_tags_external_agent_origin() {
        let raw = make_codex_response_item("assistant", "answer", "a-1");
        let adapter = CodexJsonl::new("sess-c3");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs[0].provenance().origin, GraphOrigin::ExternalAgent);
    }

    #[test]
    fn codex_extracts_output_text_from_assistant_turns() {
        // Real Codex assistant turns use output_text, not input_text.
        let raw = [
            make_codex_session_meta("/project"),
            make_codex_assistant("assistant answer", "a-1"),
        ]
        .join("\n");
        let adapter = CodexJsonl::new("sess-c4");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "assistant answer");
    }

    #[test]
    fn codex_embeds_batch_id_in_provenance() {
        let raw = make_codex_response_item("user", "hello", "u-1");
        let adapter = CodexJsonl::new("sess-c5");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs[0].provenance().import_batch_id, batch.as_str());
    }

    #[test]
    fn codex_context_window() {
        let raw = [
            make_codex_response_item("user", "msg1", "i1"),
            make_codex_response_item("user", "msg2", "i2"),
            make_codex_response_item("user", "msg3", "i3"),
            make_codex_response_item("user", "msg4", "i4"),
        ]
        .join("\n");
        let adapter = CodexJsonl::new("sess-c6").with_context_window(2);
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 4);
        assert!(docs[0].context().is_empty());
        assert_eq!(docs[1].context().len(), 1);
        assert_eq!(docs[2].context().len(), 2);
        assert_eq!(docs[3].context().len(), 2);
    }

    #[test]
    fn codex_skips_empty_content() {
        let raw = [
            make_codex_response_item("user", "", "empty-1"),
            make_codex_response_item("user", "non-empty", "u-2"),
        ]
        .join("\n");
        let adapter = CodexJsonl::new("sess-c7");
        let batch = ImportBatchId::new();
        let docs = adapter.parse(&raw, &batch).unwrap();
        assert_eq!(docs.len(), 1);
        assert_eq!(docs[0].content(), "non-empty");
    }
}