Skip to main content

khive_pack_session/mirror/
parse.rs

1//! Parsers for CLI transcripts and provider conversation exports.
2//!
3//! Every function here is deterministic and side-effect-free so the unit tests
4//! can run without any runtime or DB setup.
5
6use std::collections::{HashMap, HashSet};
7
8use chrono::DateTime;
9use khive_runtime::secret_gate;
10use serde_json::{Map, Value};
11
12fn mask_session_mirror(text: &str) -> std::borrow::Cow<'_, str> {
13    secret_gate::mask_for_redaction_surface(secret_gate::RedactionSurface::SessionMirror, text)
14}
15
16/// Bound the masker's own input window (see `secret_gate::mask_bounded`)
17/// before capping display text to `output_cap_chars`, rather than masking
18/// the full, unbounded text: cost stays proportional to the shared window
19/// regardless of input length, and a token straddling the window is dropped
20/// whole rather than echoed unmasked.
21fn mask_session_mirror_bounded(text: &str, output_cap_chars: usize) -> String {
22    secret_gate::mask_bounded(
23        secret_gate::RedactionSurface::SessionMirror,
24        text,
25        secret_gate::MASK_WINDOW_CHARS,
26        output_cap_chars,
27    )
28    .text
29}
30
31/// A single parsed event, source-agnostic.
32#[derive(Debug, Clone, PartialEq)]
33pub struct ParsedEvent {
34    /// Event UUID — the primary key for idempotency.
35    ///
36    /// For Claude Code events this is the top-level `uuid` field.
37    /// For Codex events (which carry no per-message uuid) this is synthesised
38    /// as `"{session_id}:{abs_byte_offset}"`.
39    /// For ChatGPT export events this is the mapping node's `message.id`.
40    /// For Claude.ai export events this is `chat_messages[].uuid`.
41    pub uuid: String,
42    /// Session UUID.
43    pub session_id: String,
44    /// Parent event UUID if present.
45    pub parent_uuid: Option<String>,
46    /// Whether this event is on a sidechain.
47    pub is_sidechain: bool,
48    /// Normalized provider role when present.
49    pub role: Option<String>,
50    /// Source event or content type.
51    pub msg_type: String,
52    /// Extracted display text, secrets masked; `None` for non-message events.
53    pub text: Option<String>,
54    /// Original line or serialized export message/node with secrets masked.
55    pub raw: String,
56    /// Source timestamp as microseconds since the Unix epoch; 0 if absent or unparseable.
57    pub created_at_micros: i64,
58    /// `cwd` if present, secrets masked.
59    pub cwd: Option<String>,
60    /// `gitBranch` (CC) or `payload.git.branch` (Codex) if present, secrets masked.
61    pub git_branch: Option<String>,
62    /// `slug` if present (CC: project slug; ChatGPT/Claude.ai export:
63    /// conversation title; Codex files carry no slug concept).
64    pub slug: Option<String>,
65}
66
67/// Session metadata parsed independently of retained message events.
68///
69/// Whole-file exports can contain valid conversations with no displayable
70/// messages. Keeping this record separate from [`ParsedEvent`] lets the
71/// ingester create the required `sessions` row without inventing a synthetic
72/// `session_messages` row.
73#[derive(Debug, Clone, PartialEq)]
74pub(crate) struct ParsedSession {
75    /// Provider conversation/session identifier.
76    pub(crate) session_id: String,
77    /// Source timestamp as microseconds since the Unix epoch; 0 if absent or unparseable.
78    pub(crate) created_at_micros: i64,
79    /// `cwd` if present.
80    pub(crate) cwd: Option<String>,
81    /// Source git branch if present.
82    pub(crate) git_branch: Option<String>,
83    /// Provider conversation title or project slug if present.
84    pub(crate) slug: Option<String>,
85}
86
87/// Parsed whole-file export payload used by the ingest layer.
88#[derive(Debug, Clone, PartialEq)]
89pub(crate) struct ParsedExport {
90    pub(crate) sessions: Vec<ParsedSession>,
91    pub(crate) events: Vec<ParsedEvent>,
92}
93
94/// Parse one Claude Code JSONL line.
95///
96/// Returns `None` for:
97/// - blank or whitespace-only lines
98/// - lines that are not valid JSON objects
99/// - lines that lack a top-level `uuid` field
100/// - lines that lack a top-level `sessionId` field
101///
102/// The returned `raw` and `text` fields have secrets masked.
103pub fn parse_cc_line(line: &str) -> Option<ParsedEvent> {
104    let trimmed = line.trim();
105    if trimmed.is_empty() {
106        return None;
107    }
108
109    let obj: Value = serde_json::from_str(trimmed).ok()?;
110    let map = obj.as_object()?;
111
112    // Both uuid and sessionId are required for idempotency and routing.
113    let uuid = map.get("uuid")?.as_str()?.to_string();
114    if uuid.is_empty() {
115        return None;
116    }
117    let session_id = map.get("sessionId")?.as_str()?.to_string();
118    if session_id.is_empty() {
119        return None;
120    }
121
122    let parent_uuid = map
123        .get("parentUuid")
124        .and_then(|v| v.as_str())
125        .filter(|s| !s.is_empty())
126        .map(str::to_string);
127
128    let is_sidechain = map
129        .get("isSidechain")
130        .and_then(|v| v.as_bool())
131        .unwrap_or(false);
132
133    let msg_type = map
134        .get("type")
135        .and_then(|v| v.as_str())
136        .unwrap_or("unknown")
137        .to_string();
138
139    let cwd = map
140        .get("cwd")
141        .and_then(|v| v.as_str())
142        .map(|s| mask_session_mirror(s).into_owned());
143
144    let git_branch = map
145        .get("gitBranch")
146        .and_then(|v| v.as_str())
147        .map(|s| mask_session_mirror(s).into_owned());
148
149    let slug = map
150        .get("slug")
151        .and_then(|v| v.as_str())
152        .map(|s| mask_session_mirror(s).into_owned());
153
154    let created_at_micros = map
155        .get("timestamp")
156        .and_then(|v| v.as_str())
157        .and_then(|ts| DateTime::parse_from_rfc3339(ts).ok())
158        .map(|dt| dt.timestamp_micros())
159        .unwrap_or(0);
160
161    // Extract role and text from message when present.
162    let (role, text) = match map.get("message").and_then(|m| m.as_object()) {
163        None => (None, None),
164        Some(msg) => {
165            let role = msg.get("role").and_then(|v| v.as_str()).map(str::to_string);
166            let text = extract_text(msg.get("content"));
167            (role, text)
168        }
169    };
170
171    // Apply masking to the raw line and the extracted text, reusing the
172    // canonical write-time secret detector (khive-runtime) — never a second,
173    // weaker masker.
174    let raw = mask_session_mirror(trimmed).into_owned();
175    let text = text.map(|t| mask_session_mirror(&t).into_owned());
176
177    Some(ParsedEvent {
178        uuid,
179        session_id,
180        parent_uuid,
181        is_sidechain,
182        role,
183        msg_type,
184        text,
185        raw,
186        created_at_micros,
187        cwd,
188        git_branch,
189        slug,
190    })
191}
192
193/// Parse one Codex CLI JSONL line.
194///
195/// `session_id` must be derived from the filename before calling this function
196/// (e.g. from `rollout-<timestamp>-<uuid>.jsonl`).  `abs_byte_offset` is the
197/// file byte offset of the **start** of this line; it is embedded in the
198/// synthesised event UUID so that `INSERT OR IGNORE` on `session_messages.id`
199/// is idempotent across re-tails of an append-only file.
200///
201/// Returns `None` for:
202/// - blank or whitespace-only lines
203/// - lines that are not valid JSON objects
204/// - lines whose top-level `type` is `"event_msg"` — these are duplicate
205///   event-stream representations of messages and must not be double-stored
206/// - any other line type that carries no useful message content
207///
208/// The returned `raw` and `text` fields have secrets masked.
209pub fn parse_codex_line(line: &str, session_id: &str, abs_byte_offset: u64) -> Option<ParsedEvent> {
210    let trimmed = line.trim();
211    if trimmed.is_empty() {
212        return None;
213    }
214
215    let obj: Value = serde_json::from_str(trimmed).ok()?;
216    let map = obj.as_object()?;
217
218    let line_type = map.get("type")?.as_str()?;
219
220    // event_msg lines are duplicate event-stream representations — skip them.
221    if line_type == "event_msg" {
222        return None;
223    }
224
225    let created_at_micros = map
226        .get("timestamp")
227        .and_then(|v| v.as_str())
228        .and_then(|ts| DateTime::parse_from_rfc3339(ts).ok())
229        .map(|dt| dt.timestamp_micros())
230        .unwrap_or(0);
231
232    match line_type {
233        "session_meta" => {
234            // session_meta carries cwd and git metadata; the session UUID in
235            // payload.id should match the filename-derived session_id, but we
236            // do NOT use it as the event id — use the synthesised offset key so
237            // the message row is unique and idempotent.
238            let payload = map.get("payload").and_then(|v| v.as_object());
239
240            let cwd = payload
241                .and_then(|p| p.get("cwd"))
242                .and_then(|v| v.as_str())
243                .map(|s| mask_session_mirror(s).into_owned());
244
245            let git_branch = payload
246                .and_then(|p| p.get("git"))
247                .and_then(|g| g.as_object())
248                .and_then(|g| g.get("branch"))
249                .and_then(|v| v.as_str())
250                .map(|s| mask_session_mirror(s).into_owned());
251
252            let uuid = format!("{session_id}:{abs_byte_offset}");
253            let raw = mask_session_mirror(trimmed).into_owned();
254
255            Some(ParsedEvent {
256                uuid,
257                session_id: session_id.to_string(),
258                parent_uuid: None,
259                is_sidechain: false,
260                role: None,
261                msg_type: "session_meta".to_string(),
262                text: None,
263                raw,
264                created_at_micros,
265                cwd,
266                git_branch,
267                slug: None,
268            })
269        }
270        "response_item" => {
271            let payload = map.get("payload").and_then(|v| v.as_object())?;
272
273            // Only ingest message items; skip tool_call, completion, etc.
274            if payload.get("type").and_then(|v| v.as_str()) != Some("message") {
275                return None;
276            }
277
278            let role = payload
279                .get("role")
280                .and_then(|v| v.as_str())
281                .map(str::to_string);
282
283            let text = extract_text(payload.get("content"));
284            let text = text.map(|t| mask_session_mirror_bounded(&t, 500));
285
286            let uuid = format!("{session_id}:{abs_byte_offset}");
287            let raw = mask_session_mirror(trimmed).into_owned();
288
289            Some(ParsedEvent {
290                uuid,
291                session_id: session_id.to_string(),
292                parent_uuid: None,
293                is_sidechain: false,
294                role,
295                msg_type: "response_item".to_string(),
296                text,
297                raw,
298                created_at_micros,
299                cwd: None,
300                git_branch: None,
301                slug: None,
302            })
303        }
304        // Unknown line types are silently skipped.
305        _ => None,
306    }
307}
308
309/// Parse a ChatGPT data-export `conversations.json` file: parses the whole
310/// file at once (unlike the line-at-a-time `parse_cc_line`/`parse_codex_line`)
311/// and returns every message-bearing event across every conversation, in
312/// deterministic DFS preorder per conversation.
313///
314/// Returns `None` when `content` is not valid JSON or the top level is not a
315/// JSON array — the caller treats that as a per-file error so the mirror
316/// cursor does not advance. A malformed *conversation* inside an otherwise-
317/// valid array is skipped individually, not the whole file. The returned
318/// `raw` and `text` fields have secrets masked, exactly like
319/// `parse_cc_line`/`parse_codex_line`. See
320/// `crates/khive-pack-session/docs/api/mirror-parse.md` for the DFS/sidechain
321/// algorithm detail.
322pub fn parse_chatgpt_export(content: &str) -> Option<Vec<ParsedEvent>> {
323    let value: Value = serde_json::from_str(content).ok()?;
324    let conversations = value.as_array()?;
325
326    // Both providers use the filename `conversations.json`. If operators point
327    // both roots at the same directory, rejecting an unmistakable Claude.ai
328    // file here prevents the ChatGPT ingester from consuming its cursor first.
329    if conversations.iter().any(is_claude_ai_conversation) {
330        return None;
331    }
332
333    let mut events = Vec::new();
334    for conv in conversations {
335        parse_conversation(conv, &mut events);
336    }
337    Some(events)
338}
339
340/// Parse a claude.ai data-export `conversations.json` file.
341///
342/// Claude.ai exports are a top-level JSON array whose conversation objects
343/// carry `uuid`, `name`, and `chat_messages`. Message UUIDs and conversation
344/// UUIDs are preserved as the idempotency and provider-session anchors. When
345/// `current_leaf_message_uuid` is present, messages outside its parent chain
346/// are retained with `is_sidechain = true`; older flat exports without that
347/// field keep every message on the main path.
348///
349/// Returns `None` when `content` is not valid JSON or the top level is not an
350/// array. Malformed conversation objects inside a valid array are skipped.
351pub fn parse_claude_ai_export(content: &str) -> Option<Vec<ParsedEvent>> {
352    parse_claude_ai_export_with_sessions(content).map(|parsed| parsed.events)
353}
354
355/// Parse a claude.ai export while retaining conversation metadata even when a
356/// conversation has no displayable messages.
357pub(crate) fn parse_claude_ai_export_with_sessions(content: &str) -> Option<ParsedExport> {
358    let value: Value = serde_json::from_str(content).ok()?;
359    let conversations = value.as_array()?;
360
361    // Symmetric source-shape check for accidentally overlapping export roots.
362    // Empty and wholly malformed arrays remain valid so per-conversation
363    // isolation keeps its existing semantics.
364    if conversations.iter().any(is_chatgpt_conversation) {
365        return None;
366    }
367
368    let mut sessions = Vec::new();
369    let mut events = Vec::new();
370    for conversation in conversations {
371        if let Some(session) = parse_claude_ai_conversation(conversation, &mut events) {
372            sessions.push(session);
373        }
374    }
375    Some(ParsedExport { sessions, events })
376}
377
378fn is_chatgpt_conversation(conversation: &Value) -> bool {
379    conversation.as_object().is_some_and(|conversation| {
380        conversation
381            .get("id")
382            .and_then(Value::as_str)
383            .is_some_and(|id| !id.is_empty())
384            && conversation.get("mapping").is_some_and(Value::is_object)
385    })
386}
387
388fn is_claude_ai_conversation(conversation: &Value) -> bool {
389    conversation.as_object().is_some_and(|conversation| {
390        conversation
391            .get("uuid")
392            .and_then(Value::as_str)
393            .is_some_and(|id| !id.is_empty())
394            && conversation
395                .get("chat_messages")
396                .is_some_and(Value::is_array)
397    })
398}
399
400fn parse_claude_ai_conversation(
401    conversation: &Value,
402    out: &mut Vec<ParsedEvent>,
403) -> Option<ParsedSession> {
404    let conversation = conversation.as_object()?;
405    let session_id = conversation
406        .get("uuid")
407        .and_then(Value::as_str)
408        .filter(|id| !id.is_empty())?;
409    let messages = conversation
410        .get("chat_messages")
411        .and_then(Value::as_array)?;
412
413    let slug = conversation
414        .get("name")
415        .and_then(Value::as_str)
416        .filter(|name| !name.is_empty())
417        .or_else(|| {
418            conversation
419                .get("summary")
420                .and_then(Value::as_str)
421                .filter(|summary| !summary.is_empty())
422        })
423        .map(|title| mask_session_mirror(title).into_owned());
424    let conversation_created_at_micros = conversation
425        .get("created_at")
426        .and_then(parse_rfc3339_micros)
427        .or_else(|| {
428            conversation
429                .get("updated_at")
430                .and_then(parse_rfc3339_micros)
431        })
432        .unwrap_or(0);
433
434    let mut parent_by_id: HashMap<String, Option<String>> = HashMap::new();
435    for message in messages {
436        let Some(message) = message.as_object() else {
437            continue;
438        };
439        let Some(uuid) = message
440            .get("uuid")
441            .and_then(Value::as_str)
442            .filter(|id| !id.is_empty())
443        else {
444            continue;
445        };
446        parent_by_id
447            .entry(uuid.to_string())
448            .or_insert_with(|| claude_ai_parent_uuid(message));
449    }
450
451    let current_path = claude_ai_current_path(conversation, &parent_by_id);
452    let mut order: Vec<usize> = (0..messages.len()).collect();
453    order.sort_by_key(|&position| {
454        let source_index = messages[position]
455            .as_object()
456            .and_then(|message| message.get("index"))
457            .and_then(Value::as_i64)
458            .unwrap_or(position as i64);
459        (source_index, position)
460    });
461
462    let mut emitted = HashSet::new();
463    let start = out.len();
464    for position in order {
465        let Some(message) = messages[position].as_object() else {
466            continue;
467        };
468        let Some(uuid) = message
469            .get("uuid")
470            .and_then(Value::as_str)
471            .filter(|id| !id.is_empty())
472        else {
473            continue;
474        };
475        if !emitted.insert(uuid.to_string()) {
476            continue;
477        }
478        if let Some(event) = build_claude_ai_event(
479            message,
480            session_id,
481            slug.as_deref(),
482            conversation_created_at_micros,
483            &current_path,
484        ) {
485            out.push(event);
486        }
487    }
488
489    let stored_ids: HashSet<String> = out[start..]
490        .iter()
491        .map(|event| event.uuid.clone())
492        .collect();
493    for event in &mut out[start..] {
494        if event
495            .parent_uuid
496            .as_ref()
497            .is_some_and(|parent| !stored_ids.contains(parent))
498        {
499            event.parent_uuid = None;
500        }
501    }
502
503    Some(ParsedSession {
504        session_id: session_id.to_string(),
505        created_at_micros: conversation_created_at_micros,
506        cwd: None,
507        git_branch: None,
508        slug,
509    })
510}
511
512fn claude_ai_current_path(
513    conversation: &Map<String, Value>,
514    parent_by_id: &HashMap<String, Option<String>>,
515) -> HashSet<String> {
516    let Some(current_leaf) = conversation
517        .get("current_leaf_message_uuid")
518        .and_then(Value::as_str)
519        .filter(|id| parent_by_id.contains_key(*id))
520    else {
521        return parent_by_id.keys().cloned().collect();
522    };
523
524    let mut path = HashSet::new();
525    let mut cursor = Some(current_leaf.to_string());
526    while let Some(message_id) = cursor {
527        if !path.insert(message_id.clone()) {
528            break;
529        }
530        cursor = parent_by_id.get(&message_id).cloned().flatten();
531    }
532    path
533}
534
535fn build_claude_ai_event(
536    message: &Map<String, Value>,
537    session_id: &str,
538    slug: Option<&str>,
539    conversation_created_at_micros: i64,
540    current_path: &HashSet<String>,
541) -> Option<ParsedEvent> {
542    let uuid = message
543        .get("uuid")
544        .and_then(Value::as_str)
545        .filter(|id| !id.is_empty())?
546        .to_string();
547
548    let role = message
549        .get("sender")
550        .or_else(|| message.get("role"))
551        .and_then(Value::as_str)
552        .filter(|sender| !sender.is_empty())
553        .map(|sender| match sender {
554            "human" | "user" => "user".to_string(),
555            other => other.to_string(),
556        });
557    let text = extract_claude_ai_text(message)?;
558    if text.trim().is_empty() {
559        return None;
560    }
561
562    let created_at_micros = message
563        .get("created_at")
564        .and_then(parse_rfc3339_micros)
565        .or_else(|| message.get("updated_at").and_then(parse_rfc3339_micros))
566        .unwrap_or(conversation_created_at_micros);
567    let parent_uuid = claude_ai_parent_uuid(message);
568    let raw_json = serde_json::to_string(message).unwrap_or_default();
569
570    Some(ParsedEvent {
571        uuid: uuid.clone(),
572        session_id: session_id.to_string(),
573        parent_uuid,
574        is_sidechain: !current_path.contains(&uuid),
575        role,
576        msg_type: "message".to_string(),
577        text: Some(mask_session_mirror(&text).into_owned()),
578        raw: mask_session_mirror(&raw_json).into_owned(),
579        created_at_micros,
580        cwd: None,
581        git_branch: None,
582        slug: slug.map(str::to_string),
583    })
584}
585
586fn claude_ai_parent_uuid(message: &Map<String, Value>) -> Option<String> {
587    message
588        .get("parent_message_uuid")
589        .and_then(Value::as_str)
590        .filter(|parent| !parent.is_empty() && *parent != "00000000-0000-4000-8000-000000000000")
591        .map(str::to_string)
592}
593
594fn extract_claude_ai_text(message: &Map<String, Value>) -> Option<String> {
595    let top_level_text = message
596        .get("text")
597        .and_then(Value::as_str)
598        .filter(|text| !text.trim().is_empty());
599    let mut parts = Vec::new();
600
601    match message.get("content") {
602        Some(Value::Array(blocks)) => {
603            for part in blocks
604                .iter()
605                .filter_map(extract_block)
606                .filter(|part| !part.trim().is_empty())
607            {
608                parts.push(part);
609            }
610        }
611        Some(Value::String(text)) if !text.trim().is_empty() => parts.push(text.clone()),
612        Some(_) | None => {}
613    }
614
615    if let Some(text) = top_level_text {
616        if !parts.iter().any(|part| part == text) {
617            parts.push(text.to_string());
618        }
619    }
620
621    (!parts.is_empty()).then(|| parts.join("\n"))
622}
623
624fn parse_rfc3339_micros(value: &Value) -> Option<i64> {
625    value
626        .as_str()
627        .and_then(|timestamp| DateTime::parse_from_rfc3339(timestamp).ok())
628        .map(|timestamp| timestamp.timestamp_micros())
629}
630
631/// Extract a display-friendly text string from a message `content` value
632/// (string form or structured-block array form).
633fn extract_text(content: Option<&Value>) -> Option<String> {
634    match content? {
635        Value::String(s) => Some(s.clone()),
636        Value::Array(blocks) => {
637            let parts: Vec<String> = blocks.iter().filter_map(extract_block).collect();
638            if parts.is_empty() {
639                None
640            } else {
641                Some(parts.join("\n"))
642            }
643        }
644        _ => None,
645    }
646}
647
648/// Extract a display string from a single content block (`"text"` /
649/// `"input_text"` / `"output_text"` / `"voice_note"` / `"tool_use"` /
650/// `"tool_result"`). See
651/// `crates/khive-pack-session/docs/api/mirror-parse.md#extract_text--extract_block-claude-code-codex-and-claudeai-blocks`.
652fn extract_block(block: &Value) -> Option<String> {
653    let map = block.as_object()?;
654    match map.get("type")?.as_str()? {
655        // Claude Code text block and Codex user/assistant text blocks all carry
656        // their display text in a "text" field — same extraction logic.
657        "text" | "input_text" | "output_text" | "voice_note" => {
658            map.get("text").and_then(|v| v.as_str()).map(str::to_string)
659        }
660        "tool_use" => {
661            let name = map
662                .get("name")
663                .and_then(|v| v.as_str())
664                .unwrap_or("unknown");
665            let input = map.get("input").cloned().unwrap_or(Value::Null);
666            let input_json = serde_json::to_string(&input).unwrap_or_default();
667            let input_str = mask_session_mirror_bounded(&input_json, 500);
668            Some(format!("[tool_use: {name}] {input_str}"))
669        }
670        "tool_result" => {
671            let content_val = map.get("content").cloned().unwrap_or(Value::Null);
672            let content_str = match &content_val {
673                Value::String(s) => s.clone(),
674                other => serde_json::to_string(other).unwrap_or_default(),
675            };
676            Some(format!(
677                "[tool_result] {}",
678                mask_session_mirror_bounded(&content_str, 500)
679            ))
680        }
681        _ => None,
682    }
683}
684
685/// Context threaded through one conversation's DFS walk. See
686/// `crates/khive-pack-session/docs/api/mirror-parse.md#convcontext`.
687struct ConvContext<'a> {
688    mapping: &'a Map<String, Value>,
689    current_path: &'a HashSet<String>,
690    session_id: &'a str,
691    conv_created_at_micros: i64,
692    slug: Option<&'a str>,
693}
694
695/// Parse one ChatGPT export conversation object (skips the whole
696/// conversation on a missing/empty `id` or missing `mapping`), appending its
697/// message-bearing nodes to `out` in deterministic DFS preorder. See
698/// `crates/khive-pack-session/docs/api/mirror-parse.md#parse_conversation`.
699fn parse_conversation(conv: &Value, out: &mut Vec<ParsedEvent>) {
700    let Some(conv_obj) = conv.as_object() else {
701        return;
702    };
703    let Some(session_id) = conv_obj
704        .get("id")
705        .and_then(|v| v.as_str())
706        .filter(|s| !s.is_empty())
707    else {
708        return;
709    };
710    let Some(mapping) = conv_obj.get("mapping").and_then(|v| v.as_object()) else {
711        return;
712    };
713
714    let slug = conv_obj
715        .get("title")
716        .and_then(|v| v.as_str())
717        .filter(|s| !s.is_empty())
718        .map(|title| mask_session_mirror(title).into_owned());
719
720    let conv_created_at_micros = conv_obj
721        .get("create_time")
722        .and_then(|v| v.as_f64())
723        .map(|secs| (secs * 1_000_000.0) as i64)
724        .unwrap_or(0);
725
726    // current-path set: walk current_node -> parent -> ... -> root; off-path
727    // nodes are flagged is_sidechain (see docs guide).
728    let mut current_path: HashSet<String> = HashSet::new();
729    if let Some(current_node) = conv_obj.get("current_node").and_then(|v| v.as_str()) {
730        let mut cursor = Some(current_node.to_string());
731        while let Some(node_id) = cursor {
732            if !current_path.insert(node_id.clone()) {
733                break; // cycle guard against malformed mapping data
734            }
735            cursor = mapping
736                .get(&node_id)
737                .and_then(|n| n.as_object())
738                .and_then(|n| n.get("parent"))
739                .and_then(|v| v.as_str())
740                .map(str::to_string);
741        }
742    }
743
744    let root_id = mapping.iter().find_map(|(id, node)| {
745        let node_obj = node.as_object()?;
746        let parent_is_null = node_obj.get("parent").map(|v| v.is_null()).unwrap_or(true);
747        parent_is_null.then(|| id.clone())
748    });
749    let Some(root_id) = root_id else {
750        return;
751    };
752
753    let ctx = ConvContext {
754        mapping,
755        current_path: &current_path,
756        session_id,
757        conv_created_at_micros,
758        slug: slug.as_deref(),
759    };
760
761    // Deterministic DFS preorder, explicit stack (not recursion — see docs guide).
762    let mut stack: Vec<String> = vec![root_id];
763    let mut visited: HashSet<String> = HashSet::new();
764
765    while let Some(node_id) = stack.pop() {
766        if !visited.insert(node_id.clone()) {
767            continue; // cycle guard
768        }
769        let Some(node) = mapping.get(&node_id).and_then(|n| n.as_object()) else {
770            continue;
771        };
772
773        if let Some(message) = node.get("message").filter(|m| !m.is_null()) {
774            if let Some(message_obj) = message.as_object() {
775                if let Some(ev) = build_chatgpt_event(&node_id, node, message_obj, &ctx) {
776                    out.push(ev);
777                }
778            }
779        }
780
781        if let Some(children) = node.get("children").and_then(|c| c.as_array()) {
782            // Push in reverse so the first child in the array is popped (and
783            // thus visited) first — preorder must follow children order.
784            for child in children.iter().rev() {
785                if let Some(child_id) = child.as_str() {
786                    stack.push(child_id.to_string());
787                }
788            }
789        }
790    }
791}
792
793/// Build a `ParsedEvent` for a single message-bearing mapping node; `None`
794/// on a missing `id` or empty/whitespace-only extracted text (ChatGPT
795/// scaffolding nodes). See
796/// `crates/khive-pack-session/docs/api/mirror-parse.md#build_chatgpt_event`.
797fn build_chatgpt_event(
798    node_id: &str,
799    node: &Map<String, Value>,
800    message: &Map<String, Value>,
801    ctx: &ConvContext,
802) -> Option<ParsedEvent> {
803    let uuid = message
804        .get("id")
805        .and_then(|v| v.as_str())
806        .filter(|s| !s.is_empty())?
807        .to_string();
808
809    let role = message
810        .get("author")
811        .and_then(|a| a.as_object())
812        .and_then(|a| a.get("role"))
813        .and_then(|v| v.as_str())
814        .map(str::to_string);
815
816    let content = message.get("content").and_then(|c| c.as_object());
817    let content_type = content
818        .and_then(|c| c.get("content_type"))
819        .and_then(|v| v.as_str())
820        .unwrap_or("unknown")
821        .to_string();
822
823    let text = extract_chatgpt_text(&content_type, content)?;
824    if text.trim().is_empty() {
825        return None;
826    }
827
828    let created_at_micros = message
829        .get("create_time")
830        .and_then(|v| v.as_f64())
831        .map(|secs| (secs * 1_000_000.0) as i64)
832        .unwrap_or(ctx.conv_created_at_micros);
833
834    // parent_uuid is Some only when the parent node itself carries a message
835    // (provenance linkage — see docs guide).
836    let parent_uuid = node
837        .get("parent")
838        .and_then(|v| v.as_str())
839        .filter(|pid| {
840            ctx.mapping
841                .get(*pid)
842                .and_then(|p| p.as_object())
843                .and_then(|p| p.get("message"))
844                .map(|m| !m.is_null())
845                .unwrap_or(false)
846        })
847        .map(str::to_string);
848
849    let is_sidechain = !ctx.current_path.contains(node_id);
850
851    let raw_json = serde_json::to_string(node).unwrap_or_default();
852    let raw = mask_session_mirror(&raw_json).into_owned();
853    let text = mask_session_mirror(&text).into_owned();
854
855    Some(ParsedEvent {
856        uuid,
857        session_id: ctx.session_id.to_string(),
858        parent_uuid,
859        is_sidechain,
860        role,
861        msg_type: content_type,
862        text: Some(text),
863        raw,
864        created_at_micros,
865        cwd: None,
866        git_branch: None,
867        slug: ctx.slug.map(str::to_string),
868    })
869}
870
871/// Extract display text from a ChatGPT message `content` object per its
872/// `content_type`. See
873/// `crates/khive-pack-session/docs/api/mirror-parse.md#extract_chatgpt_text`.
874fn extract_chatgpt_text(
875    content_type: &str,
876    content: Option<&Map<String, Value>>,
877) -> Option<String> {
878    let content = content?;
879
880    if content_type == "text" {
881        let parts = content.get("parts")?.as_array()?;
882        let joined: Vec<String> = parts
883            .iter()
884            .filter_map(|p| p.as_str().map(str::to_string))
885            .collect();
886        return if joined.is_empty() {
887            None
888        } else {
889            Some(joined.join("\n"))
890        };
891    }
892
893    if let Some(text) = content.get("text").and_then(|v| v.as_str()) {
894        return Some(text.to_string());
895    }
896
897    let parts = content.get("parts")?.as_array()?;
898    let joined: Vec<String> = parts
899        .iter()
900        .filter_map(|p| p.as_str().map(str::to_string))
901        .collect();
902    if joined.is_empty() {
903        None
904    } else {
905        Some(joined.join("\n"))
906    }
907}
908
909// ── unit tests ────────────────────────────────────────────────────────────────
910
911#[cfg(test)]
912mod tests {
913    use super::*;
914
915    fn anthropic_api_key_fixture() -> String {
916        format!("sk-ant-api03-{}AA", "A".repeat(93))
917    }
918
919    fn github_fine_grained_pat_fixture() -> String {
920        format!("github_pat_{}", "A".repeat(82))
921    }
922
923    // Helper: build a minimal CC event JSON string.
924    fn make_line(uuid: &str, session_id: &str, type_: &str, extra: &str) -> String {
925        format!(
926            r#"{{"uuid":"{uuid}","sessionId":"{session_id}","type":"{type_}","timestamp":"2026-06-29T10:00:00Z"{extra}}}"#,
927        )
928    }
929
930    #[test]
931    fn test_blank_line_returns_none() {
932        assert!(parse_cc_line("").is_none());
933        assert!(parse_cc_line("   ").is_none());
934    }
935
936    #[test]
937    fn test_no_uuid_returns_none() {
938        // pr-link style: no uuid
939        let line = r#"{"type":"pr-link","sessionId":"sess-1","url":"https://github.com/foo"}"#;
940        assert!(parse_cc_line(line).is_none());
941    }
942
943    #[test]
944    fn test_no_session_id_returns_none() {
945        let line = r#"{"uuid":"aaaa-bbbb","type":"user"}"#;
946        assert!(parse_cc_line(line).is_none());
947    }
948
949    #[test]
950    fn test_user_text_line() {
951        let line = make_line(
952            "aaaa-bbbb",
953            "sess-1111",
954            "user",
955            r#","message":{"role":"user","content":"Hello world"},"cwd":"/proj","gitBranch":"main","slug":"my-proj""#,
956        );
957        let ev = parse_cc_line(&line).expect("should parse");
958        assert_eq!(ev.uuid, "aaaa-bbbb");
959        assert_eq!(ev.session_id, "sess-1111");
960        assert_eq!(ev.role.as_deref(), Some("user"));
961        assert_eq!(ev.msg_type, "user");
962        assert_eq!(ev.text.as_deref(), Some("Hello world"));
963        assert_eq!(ev.cwd.as_deref(), Some("/proj"));
964        assert_eq!(ev.git_branch.as_deref(), Some("main"));
965        assert_eq!(ev.slug.as_deref(), Some("my-proj"));
966        assert!(ev.created_at_micros > 0);
967        assert!(!ev.is_sidechain);
968    }
969
970    #[test]
971    fn test_cc_line_masks_secret_bearing_slug() {
972        let secret = format!("{}{}", "AKIA", "FAKEKEY1234567890");
973        let line = make_line(
974            "aaaa-bbbb",
975            "sess-1111",
976            "user",
977            &format!(
978                r#","message":{{"role":"user","content":"Hello world"}},"slug":"prod creds {secret}""#
979            ),
980        );
981        let ev = parse_cc_line(&line).expect("should parse");
982        let slug = ev.slug.as_deref().expect("slug");
983        assert!(!slug.contains(&secret));
984        assert!(slug.contains("***MASKED***"));
985    }
986
987    #[test]
988    fn test_cc_line_masks_secret_bearing_cwd_and_git_branch() {
989        let marker = "CwdCredMarkerXYZ789";
990        let cwd = format!("/repo?token=postgres://svc:{marker}q@internal-host.example.test/db");
991        let branch = format!("feature/postgres://svc:{marker}q@internal-host.example.test/db");
992        let line = make_line(
993            "aaaa-cwd1",
994            "sess-1111",
995            "user",
996            &format!(
997                r#","message":{{"role":"user","content":"Hello world"}},"cwd":"{cwd}","gitBranch":"{branch}""#
998            ),
999        );
1000        let ev = parse_cc_line(&line).expect("should parse");
1001        let stored_cwd = ev.cwd.as_deref().expect("cwd");
1002        let stored_branch = ev.git_branch.as_deref().expect("git_branch");
1003        assert!(!stored_cwd.contains(marker), "cwd: {stored_cwd}");
1004        assert!(stored_cwd.contains("***MASKED***"), "cwd: {stored_cwd}");
1005        assert!(
1006            !stored_branch.contains(marker),
1007            "git_branch: {stored_branch}"
1008        );
1009        assert!(
1010            stored_branch.contains("***MASKED***"),
1011            "git_branch: {stored_branch}"
1012        );
1013    }
1014
1015    #[test]
1016    fn test_assistant_with_text_and_tool_use_blocks() {
1017        let line = r#"{"uuid":"cccc-dddd","sessionId":"sess-1111","type":"assistant","timestamp":"2026-06-29T10:01:00Z","message":{"role":"assistant","content":[{"type":"text","text":"I'll run a search."},{"type":"tool_use","name":"bash","input":{"command":"ls"}}]}}"#
1018            .to_string();
1019        let ev = parse_cc_line(&line).expect("should parse");
1020        assert_eq!(ev.role.as_deref(), Some("assistant"));
1021        let text = ev.text.expect("text should be present");
1022        assert!(text.contains("I'll run a search."), "text: {text}");
1023        assert!(text.contains("[tool_use: bash]"), "text: {text}");
1024        assert!(text.contains("command"), "text: {text}");
1025    }
1026
1027    #[test]
1028    fn test_tool_use_input_credential_masked_before_truncation() {
1029        // The credential's terminating `@` lands past the 500-character
1030        // display cap applied to a tool_use input block. A masker that only
1031        // sees the already-truncated prefix can never recognize the span, so
1032        // the password marker survives untouched in the stored `text`.
1033        let marker = "ToolUseCredMarkerXYZ789";
1034        let padding = "z".repeat(600);
1035        let password = format!("{marker}{padding}");
1036        let url = format!("postgres://svc:{password}@internal-host.example.com/db");
1037        let input = serde_json::json!({ "url": url });
1038        let serialized_input = serde_json::to_string(&input).unwrap();
1039        let at_offset = serialized_input
1040            .find('@')
1041            .expect("fixture must contain '@'");
1042        assert!(at_offset > 500);
1043
1044        let content = serde_json::json!([
1045            { "type": "tool_use", "name": "bash", "input": input }
1046        ]);
1047        let message = serde_json::json!({
1048            "uuid": "tool-use-cred",
1049            "sessionId": "sess-cred",
1050            "type": "assistant",
1051            "timestamp": "2026-06-29T10:01:00Z",
1052            "message": { "role": "assistant", "content": content }
1053        });
1054        let line = message.to_string();
1055
1056        let ev = parse_cc_line(&line).expect("should parse");
1057        let text = ev.text.expect("text should be present");
1058        assert!(
1059            !text.contains(marker),
1060            "no fragment of the credential may survive masking: {text}"
1061        );
1062        assert!(
1063            text.contains("***MASKED***"),
1064            "MASKED marker must appear in text: {text}"
1065        );
1066    }
1067
1068    #[test]
1069    fn test_tool_result_content_credential_masked_before_truncation() {
1070        // Same shape as the tool_use case above, but for a tool_result
1071        // content string whose credential terminator crosses the 500-char cut.
1072        let marker = "ToolResultCredMarkerXYZ789";
1073        let padding = "z".repeat(600);
1074        let password = format!("{marker}{padding}");
1075        let url = format!("postgres://svc:{password}@internal-host.example.com/db");
1076        let at_offset = url.find('@').expect("fixture must contain '@'");
1077        assert!(at_offset > 500);
1078
1079        let content = serde_json::json!([
1080            { "type": "tool_result", "content": url }
1081        ]);
1082        let message = serde_json::json!({
1083            "uuid": "tool-result-cred",
1084            "sessionId": "sess-cred",
1085            "type": "user",
1086            "timestamp": "2026-06-29T10:02:00Z",
1087            "message": { "role": "user", "content": content }
1088        });
1089        let line = message.to_string();
1090
1091        let ev = parse_cc_line(&line).expect("should parse");
1092        let text = ev.text.expect("text should be present");
1093        assert!(
1094            !text.contains(marker),
1095            "no fragment of the credential may survive masking: {text}"
1096        );
1097        assert!(
1098            text.contains("***MASKED***"),
1099            "MASKED marker must appear in text: {text}"
1100        );
1101    }
1102
1103    #[test]
1104    fn test_tool_result_block() {
1105        let line = r#"{"uuid":"eeee-ffff","sessionId":"sess-1111","type":"user","timestamp":"2026-06-29T10:02:00Z","message":{"role":"user","content":[{"type":"tool_result","content":"file1.rs\nfile2.rs"}]}}"#
1106            .to_string();
1107        let ev = parse_cc_line(&line).expect("should parse");
1108        let text = ev.text.expect("text should be present");
1109        assert!(text.contains("[tool_result]"), "text: {text}");
1110        assert!(text.contains("file1.rs"), "text: {text}");
1111    }
1112
1113    #[test]
1114    fn test_attachment_line_no_message() {
1115        // uuid present, sessionId present, but no message -> role/text None
1116        let line = r#"{"uuid":"gggg-hhhh","sessionId":"sess-1111","type":"attachment","timestamp":"2026-06-29T10:02:00Z","filename":"file.txt"}"#
1117            .to_string();
1118        let ev = parse_cc_line(&line).expect("should parse");
1119        assert_eq!(ev.msg_type, "attachment");
1120        assert!(ev.role.is_none());
1121        assert!(ev.text.is_none());
1122    }
1123
1124    #[test]
1125    fn test_secret_masking_in_text_and_raw() {
1126        let secret = anthropic_api_key_fixture();
1127        let line = format!(
1128            r#"{{"uuid":"iiii-jjjj","sessionId":"sess-1111","type":"user","timestamp":"2026-06-29T10:03:00Z","message":{{"role":"user","content":"my key is {secret}"}}}}"#
1129        );
1130        let ev = parse_cc_line(&line).expect("should parse");
1131
1132        let text = ev.text.expect("text should be present");
1133        assert!(
1134            !text.contains(secret.as_str()),
1135            "secret must not appear in text: {text}"
1136        );
1137        assert!(
1138            text.contains("***MASKED***"),
1139            "MASKED marker must appear in text: {text}"
1140        );
1141
1142        assert!(
1143            !ev.raw.contains(secret.as_str()),
1144            "secret must not appear in raw: {}",
1145            ev.raw
1146        );
1147        assert!(
1148            ev.raw.contains("***MASKED***"),
1149            "MASKED marker must appear in raw: {}",
1150            ev.raw
1151        );
1152    }
1153
1154    #[test]
1155    fn test_github_pat_masked() {
1156        let secret = github_fine_grained_pat_fixture();
1157        let line = format!(
1158            r#"{{"uuid":"kkkk-llll","sessionId":"sess-2","type":"user","timestamp":"2026-06-29T10:04:00Z","message":{{"role":"user","content":"token={secret}"}}}}"#
1159        );
1160        let ev = parse_cc_line(&line).unwrap();
1161        assert!(!ev.raw.contains(secret.as_str()));
1162        assert!(ev.raw.contains("***MASKED***"));
1163    }
1164
1165    #[test]
1166    fn test_timestamp_to_micros() {
1167        let line = make_line(
1168            "ts-test",
1169            "sess-ts",
1170            "system",
1171            r#","timestamp":"2026-06-29T17:56:01.123Z""#,
1172        );
1173        let ev = parse_cc_line(&line).unwrap();
1174        // 2026-06-29T17:56:01.123Z in micros should be a large positive number
1175        assert!(ev.created_at_micros > 0, "created_at_micros should be > 0");
1176    }
1177
1178    #[test]
1179    fn test_sidechain_flag() {
1180        let line = make_line("side-uuid", "sess-side", "user", r#","isSidechain":true"#);
1181        let ev = parse_cc_line(&line).unwrap();
1182        assert!(ev.is_sidechain);
1183    }
1184
1185    // ── parse_codex_line tests ─────────────────────────────────────────────────
1186
1187    const CDX_SID: &str = "cdx-session-0001-0001-0001-000000000001";
1188
1189    /// Build a Codex user message line using the real `input_text` block shape.
1190    fn codex_user_msg(text: &str) -> String {
1191        format!(
1192            r#"{{"type":"response_item","timestamp":"2026-06-30T09:00:00Z","payload":{{"type":"message","role":"user","content":[{{"type":"input_text","text":"{text}"}}]}}}}"#
1193        )
1194    }
1195
1196    /// Build a Codex assistant message line using the real `output_text` block shape.
1197    fn codex_asst_msg(text: &str) -> String {
1198        format!(
1199            r#"{{"type":"response_item","timestamp":"2026-06-30T09:00:00Z","payload":{{"type":"message","role":"assistant","content":[{{"type":"output_text","text":"{text}"}}]}}}}"#
1200        )
1201    }
1202
1203    /// Build a Codex session_meta line.
1204    fn codex_meta(cwd: &str, branch: &str) -> String {
1205        format!(
1206            r#"{{"type":"session_meta","timestamp":"2026-06-30T09:00:00Z","payload":{{"id":"{CDX_SID}","cwd":"{cwd}","git":{{"branch":"{branch}","commit_hash":"abc123","repository_url":"https://github.com/example/repo"}}}}}}"#
1207        )
1208    }
1209
1210    /// Build a Codex response_item with a tool_use block (no text block).
1211    fn codex_tool_use_msg() -> String {
1212        r#"{"type":"response_item","timestamp":"2026-06-30T09:01:00Z","payload":{"type":"message","role":"assistant","content":[{"type":"tool_use","name":"bash","input":{"command":"cargo test"}}]}}"#.to_string()
1213    }
1214
1215    /// Build a Codex event_msg line (duplicate; must be skipped).
1216    fn codex_event_msg() -> String {
1217        r#"{"type":"event_msg","timestamp":"2026-06-30T09:00:00Z","payload":{"type":"user_message","content":"duplicate"}}"#.to_string()
1218    }
1219
1220    #[test]
1221    fn test_codex_blank_returns_none() {
1222        assert!(parse_codex_line("", CDX_SID, 0).is_none());
1223        assert!(parse_codex_line("   ", CDX_SID, 0).is_none());
1224    }
1225
1226    #[test]
1227    fn test_codex_event_msg_skipped() {
1228        let line = codex_event_msg();
1229        assert!(
1230            parse_codex_line(&line, CDX_SID, 42).is_none(),
1231            "event_msg must be skipped"
1232        );
1233    }
1234
1235    #[test]
1236    fn test_codex_unknown_type_skipped() {
1237        let line = r#"{"type":"some_other_event","timestamp":"2026-06-30T09:00:00Z","payload":{}}"#;
1238        assert!(parse_codex_line(line, CDX_SID, 0).is_none());
1239    }
1240
1241    #[test]
1242    fn test_codex_session_meta_produces_event() {
1243        let line = codex_meta("/workspace/proj", "main");
1244        let ev = parse_codex_line(&line, CDX_SID, 0).expect("session_meta should parse");
1245        assert_eq!(ev.session_id, CDX_SID);
1246        assert_eq!(ev.msg_type, "session_meta");
1247        assert_eq!(ev.cwd.as_deref(), Some("/workspace/proj"));
1248        assert_eq!(ev.git_branch.as_deref(), Some("main"));
1249        assert!(ev.role.is_none());
1250        assert!(ev.text.is_none());
1251        // Synthetic uuid: "{session_id}:{offset}".
1252        assert_eq!(ev.uuid, format!("{CDX_SID}:0"));
1253        assert!(ev.created_at_micros > 0);
1254    }
1255
1256    #[test]
1257    fn test_codex_session_meta_masks_secret_bearing_cwd_and_branch() {
1258        let marker = "CodexCwdCredMarkerXYZ789";
1259        let cwd = format!("/repo?token=postgres://svc:{marker}q@internal-host.example.test/db");
1260        let branch = format!("feature/postgres://svc:{marker}q@internal-host.example.test/db");
1261        let line = codex_meta(&cwd, &branch);
1262        let ev = parse_codex_line(&line, CDX_SID, 0).expect("session_meta should parse");
1263        let stored_cwd = ev.cwd.as_deref().expect("cwd");
1264        let stored_branch = ev.git_branch.as_deref().expect("git_branch");
1265        assert!(!stored_cwd.contains(marker), "cwd: {stored_cwd}");
1266        assert!(stored_cwd.contains("***MASKED***"), "cwd: {stored_cwd}");
1267        assert!(
1268            !stored_branch.contains(marker),
1269            "git_branch: {stored_branch}"
1270        );
1271        assert!(
1272            stored_branch.contains("***MASKED***"),
1273            "git_branch: {stored_branch}"
1274        );
1275    }
1276
1277    #[test]
1278    fn test_codex_user_message_input_text_block() {
1279        // Regression: real Codex user messages use `input_text` blocks.
1280        let line = codex_user_msg("Hello Codex");
1281        let ev = parse_codex_line(&line, CDX_SID, 128).expect("user message should parse");
1282        assert_eq!(ev.session_id, CDX_SID);
1283        assert_eq!(ev.msg_type, "response_item");
1284        assert_eq!(ev.role.as_deref(), Some("user"));
1285        // text must NOT be None — this was the NULL bug.
1286        let text = ev.text.expect("text must be non-NULL for input_text block");
1287        assert_eq!(text, "Hello Codex");
1288        assert_eq!(ev.uuid, format!("{CDX_SID}:128"));
1289    }
1290
1291    #[test]
1292    fn test_codex_assistant_message_output_text_block() {
1293        // Regression: real Codex assistant messages use `output_text` blocks.
1294        let line = codex_asst_msg("Hello from assistant");
1295        let ev = parse_codex_line(&line, CDX_SID, 256).expect("assistant message should parse");
1296        assert_eq!(ev.role.as_deref(), Some("assistant"));
1297        // text must NOT be None.
1298        let text = ev
1299            .text
1300            .expect("text must be non-NULL for output_text block");
1301        assert_eq!(text, "Hello from assistant");
1302        assert_eq!(ev.uuid, format!("{CDX_SID}:256"));
1303    }
1304
1305    #[test]
1306    fn test_codex_tool_use_block_extracted() {
1307        let line = codex_tool_use_msg();
1308        let ev = parse_codex_line(&line, CDX_SID, 512).expect("tool_use message should parse");
1309        assert_eq!(ev.role.as_deref(), Some("assistant"));
1310        let text = ev.text.expect("text must be present");
1311        assert!(text.contains("[tool_use: bash]"), "text: {text}");
1312        assert!(text.contains("cargo test"), "text: {text}");
1313    }
1314
1315    #[test]
1316    fn test_codex_text_truncated_at_500_chars() {
1317        // input_text block with 600-char body — must be truncated.
1318        let long_text = "x".repeat(600);
1319        let line = codex_user_msg(&long_text);
1320        let ev = parse_codex_line(&line, CDX_SID, 0).expect("should parse");
1321        let text = ev.text.expect("text must be present");
1322        // char count must be ≤ 501 (500 + the '…' ellipsis char).
1323        assert!(
1324            text.chars().count() <= 501,
1325            "text must be truncated: len={}",
1326            text.chars().count()
1327        );
1328        assert!(text.ends_with('…'), "truncated text must end with ellipsis");
1329    }
1330
1331    #[test]
1332    fn test_codex_secret_masked_in_text_and_raw() {
1333        // input_text block carrying a secret — masking must apply to both text and raw.
1334        let secret = anthropic_api_key_fixture();
1335        let line = codex_user_msg(&secret);
1336        let ev = parse_codex_line(&line, CDX_SID, 0).expect("should parse");
1337        let text = ev.text.expect("text present");
1338        assert!(
1339            !text.contains(secret.as_str()),
1340            "secret must not appear in text"
1341        );
1342        assert!(
1343            text.contains("***MASKED***"),
1344            "MASKED marker must appear in text"
1345        );
1346        assert!(
1347            !ev.raw.contains(secret.as_str()),
1348            "secret must not appear in raw"
1349        );
1350        assert!(
1351            ev.raw.contains("***MASKED***"),
1352            "MASKED marker must appear in raw"
1353        );
1354    }
1355
1356    #[test]
1357    fn test_codex_synthetic_uuid_stable_across_calls() {
1358        // The same line at the same offset must produce the same uuid regardless
1359        // of how many times it is called (deterministic, no random component).
1360        let line = codex_user_msg("consistency");
1361        let ev1 = parse_codex_line(&line, CDX_SID, 999).unwrap();
1362        let ev2 = parse_codex_line(&line, CDX_SID, 999).unwrap();
1363        assert_eq!(ev1.uuid, ev2.uuid);
1364        assert_eq!(ev1.uuid, format!("{CDX_SID}:999"));
1365    }
1366
1367    #[test]
1368    fn test_codex_response_item_non_message_payload_skipped() {
1369        // A response_item where payload.type != "message" must be skipped.
1370        let line = r#"{"type":"response_item","timestamp":"2026-06-30T09:00:00Z","payload":{"type":"tool_call","name":"some_tool"}}"#;
1371        assert!(parse_codex_line(line, CDX_SID, 0).is_none());
1372    }
1373
1374    #[test]
1375    fn test_cc_text_block_still_works() {
1376        // Regression guard: adding input_text/output_text must not break the
1377        // existing CC "text" block handling.
1378        let line = r#"{"uuid":"cc-t1","sessionId":"cc-sess","type":"assistant","timestamp":"2026-06-30T09:00:00Z","message":{"role":"assistant","content":[{"type":"text","text":"CC still works"}]}}"#;
1379        let ev = parse_cc_line(line).expect("CC text block must parse");
1380        assert_eq!(ev.text.as_deref(), Some("CC still works"));
1381    }
1382
1383    // ── parse_claude_ai_export: direct unit tests ───────────────────────────
1384
1385    #[test]
1386    fn test_claude_ai_export_preserves_ids_order_roles_and_title() {
1387        let export = serde_json::json!([{
1388            "uuid": "claude-conv-1",
1389            "name": "Claude Web Conversation",
1390            "created_at": "2026-07-31T10:00:00Z",
1391            "current_leaf_message_uuid": "claude-msg-2",
1392            "chat_messages": [
1393                {
1394                    "uuid": "claude-msg-2",
1395                    "sender": "assistant",
1396                    "index": 1,
1397                    "parent_message_uuid": "claude-msg-1",
1398                    "created_at": "2026-07-31T10:00:02Z",
1399                    "content": [
1400                        {"type": "thinking", "thinking": "not display text"},
1401                        {"type": "text", "text": "Hello from Claude"}
1402                    ]
1403                },
1404                {
1405                    "uuid": "claude-msg-1",
1406                    "sender": "human",
1407                    "index": 0,
1408                    "parent_message_uuid": "00000000-0000-4000-8000-000000000000",
1409                    "created_at": "2026-07-31T10:00:01Z",
1410                    "content": [{"type": "text", "text": "Hello"}]
1411                }
1412            ]
1413        }]);
1414
1415        let events = parse_claude_ai_export(&export.to_string()).expect("valid export");
1416        assert_eq!(events.len(), 2);
1417        assert_eq!(events[0].uuid, "claude-msg-1");
1418        assert_eq!(events[0].session_id, "claude-conv-1");
1419        assert_eq!(events[0].role.as_deref(), Some("user"));
1420        assert_eq!(events[0].text.as_deref(), Some("Hello"));
1421        assert_eq!(events[0].parent_uuid, None);
1422        assert_eq!(events[0].slug.as_deref(), Some("Claude Web Conversation"));
1423        assert!(!events[0].is_sidechain);
1424
1425        assert_eq!(events[1].uuid, "claude-msg-2");
1426        assert_eq!(events[1].role.as_deref(), Some("assistant"));
1427        assert_eq!(events[1].text.as_deref(), Some("Hello from Claude"));
1428        assert_eq!(events[1].parent_uuid.as_deref(), Some("claude-msg-1"));
1429        assert!(!events[1].is_sidechain);
1430        assert!(events[1].created_at_micros > events[0].created_at_micros);
1431    }
1432
1433    #[test]
1434    fn test_claude_ai_export_preserves_branches_and_marks_inactive_path() {
1435        let export = serde_json::json!([{
1436            "uuid": "claude-conv-branch",
1437            "name": "Branching Claude Conversation",
1438            "current_leaf_message_uuid": "claude-main",
1439            "chat_messages": [
1440                {
1441                    "uuid": "claude-user",
1442                    "sender": "human",
1443                    "index": 0,
1444                    "parent_message_uuid": null,
1445                    "content": [{"type": "text", "text": "Question"}]
1446                },
1447                {
1448                    "uuid": "claude-main",
1449                    "sender": "assistant",
1450                    "index": 1,
1451                    "parent_message_uuid": "claude-user",
1452                    "content": [{"type": "text", "text": "Current answer"}]
1453                },
1454                {
1455                    "uuid": "claude-alt",
1456                    "sender": "assistant",
1457                    "index": 2,
1458                    "parent_message_uuid": "claude-user",
1459                    "content": [{"type": "text", "text": "Alternate answer"}]
1460                }
1461            ]
1462        }]);
1463
1464        let events = parse_claude_ai_export(&export.to_string()).expect("valid branch export");
1465        assert_eq!(events.len(), 3, "alternate branches must be retained");
1466        assert!(!events[0].is_sidechain);
1467        assert!(!events[1].is_sidechain);
1468        assert!(events[2].is_sidechain);
1469        assert_eq!(events[2].uuid, "claude-alt");
1470        assert_eq!(events[2].parent_uuid.as_deref(), Some("claude-user"));
1471    }
1472
1473    #[test]
1474    fn test_claude_ai_export_keeps_blocks_and_distinct_top_level_answer() {
1475        let export = serde_json::json!([{
1476            "uuid": "claude-conv-agent",
1477            "chat_messages": [
1478                {
1479                    "uuid": "claude-agent-turn",
1480                    "sender": "assistant",
1481                    "text": "Final answer",
1482                    "content": [
1483                        {"type": "tool_use", "name": "bash_tool", "input": {"command": "pwd"}},
1484                        {"type": "tool_result", "content": [{"type": "text", "text": "/tmp"}]}
1485                    ]
1486                },
1487                {
1488                    "uuid": "claude-deduplicated-text",
1489                    "sender": "assistant",
1490                    "text": "Already present",
1491                    "content": [{"type": "text", "text": "Already present"}]
1492                }
1493            ]
1494        }]);
1495
1496        let events = parse_claude_ai_export(&export.to_string()).expect("valid agent export");
1497        assert_eq!(events.len(), 2);
1498        let agent_text = events[0].text.as_deref().expect("agent turn text");
1499        assert!(agent_text.contains("[tool_use: bash_tool]"));
1500        assert!(agent_text.contains("[tool_result]"));
1501        assert!(agent_text.ends_with("Final answer"));
1502        assert_eq!(events[1].text.as_deref(), Some("Already present"));
1503    }
1504
1505    #[test]
1506    fn test_claude_ai_export_accepts_legacy_role_and_voice_note_text() {
1507        let export = serde_json::json!([{
1508            "uuid": "claude-conv-voice",
1509            "chat_messages": [{
1510                "uuid": "claude-voice-turn",
1511                "role": "human",
1512                "content": [{"type": "voice_note", "title": "Memo", "text": "Spoken prompt"}]
1513            }]
1514        }]);
1515
1516        let events = parse_claude_ai_export(&export.to_string()).expect("valid legacy export");
1517        assert_eq!(events.len(), 1);
1518        assert_eq!(events[0].role.as_deref(), Some("user"));
1519        assert_eq!(events[0].text.as_deref(), Some("Spoken prompt"));
1520    }
1521
1522    #[test]
1523    fn test_claude_ai_export_legacy_flat_text_is_masked() {
1524        let secret = format!("{}{}", "AKIA", "FAKEKEY1234567890");
1525        let export = serde_json::json!([{
1526            "uuid": "claude-conv-flat",
1527            "summary": "Legacy export",
1528            "chat_messages": [
1529                {
1530                    "uuid": "claude-flat-user",
1531                    "sender": "human",
1532                    "text": format!("key={secret}"),
1533                    "created_at": "2026-07-31T10:00:00Z"
1534                },
1535                {
1536                    "uuid": "claude-flat-assistant",
1537                    "sender": "assistant",
1538                    "text": "Legacy assistant text",
1539                    "content": [{"type": "thinking", "thinking": "not visible"}],
1540                    "created_at": "2026-07-31T10:00:01Z"
1541                }
1542            ]
1543        }]);
1544
1545        let events = parse_claude_ai_export(&export.to_string()).expect("legacy export");
1546        assert_eq!(events.len(), 2);
1547        assert!(events.iter().all(|event| !event.is_sidechain));
1548        assert_eq!(events[0].slug.as_deref(), Some("Legacy export"));
1549        assert!(!events[0].text.as_deref().unwrap().contains(&secret));
1550        assert!(events[0].text.as_deref().unwrap().contains("***MASKED***"));
1551        assert!(!events[0].raw.contains(&secret));
1552        assert_eq!(events[1].text.as_deref(), Some("Legacy assistant text"));
1553    }
1554
1555    #[test]
1556    fn test_claude_ai_export_masks_secret_bearing_title_and_summary() {
1557        let secret = format!("{}{}", "AKIA", "FAKEKEY1234567890");
1558        let export = serde_json::json!([
1559            {
1560                "uuid": "claude-conv-secret-name",
1561                "name": format!("prod creds {secret}"),
1562                "chat_messages": [{
1563                    "uuid": "claude-secret-name-user",
1564                    "sender": "human",
1565                    "text": "hello",
1566                    "created_at": "2026-07-31T10:00:00Z"
1567                }]
1568            },
1569            {
1570                "uuid": "claude-conv-secret-summary",
1571                "summary": format!("key={secret}"),
1572                "chat_messages": []
1573            }
1574        ]);
1575
1576        let parsed =
1577            parse_claude_ai_export_with_sessions(&export.to_string()).expect("secret-title export");
1578        assert_eq!(parsed.sessions.len(), 2);
1579        for session in &parsed.sessions {
1580            let slug = session.slug.as_deref().expect("session slug");
1581            assert!(!slug.contains(&secret));
1582            assert!(slug.contains("***MASKED***"));
1583        }
1584        assert_eq!(parsed.events.len(), 1);
1585        let event_slug = parsed.events[0].slug.as_deref().expect("event slug");
1586        assert!(!event_slug.contains(&secret));
1587        assert!(event_slug.contains("***MASKED***"));
1588    }
1589
1590    #[test]
1591    fn test_claude_ai_export_rejects_bad_top_level_and_skips_bad_conversations() {
1592        assert!(parse_claude_ai_export("not json").is_none());
1593        assert!(parse_claude_ai_export(r#"{"uuid":"not-an-array"}"#).is_none());
1594
1595        let export = serde_json::json!([
1596            {"uuid": "missing-messages"},
1597            {
1598                "uuid": "claude-valid",
1599                "chat_messages": [{
1600                    "uuid": "claude-valid-message",
1601                    "sender": "human",
1602                    "text": "kept"
1603                }]
1604            }
1605        ]);
1606        let events = parse_claude_ai_export(&export.to_string()).expect("valid array");
1607        assert_eq!(events.len(), 1);
1608        assert_eq!(events[0].session_id, "claude-valid");
1609    }
1610
1611    #[test]
1612    fn test_provider_export_parsers_reject_the_other_source_shape() {
1613        let claude_ai = serde_json::json!([{
1614            "uuid": "claude-conversation",
1615            "chat_messages": []
1616        }]);
1617        assert!(parse_chatgpt_export(&claude_ai.to_string()).is_none());
1618
1619        let chatgpt = serde_json::json!([{
1620            "id": "chatgpt-conversation",
1621            "mapping": {}
1622        }]);
1623        assert!(parse_claude_ai_export(&chatgpt.to_string()).is_none());
1624
1625        let mixed = serde_json::json!([
1626            {"uuid": "claude-conversation", "chat_messages": []},
1627            {"id": "chatgpt-conversation", "mapping": {}}
1628        ]);
1629        assert!(parse_chatgpt_export(&mixed.to_string()).is_none());
1630        assert!(parse_claude_ai_export(&mixed.to_string()).is_none());
1631
1632        assert_eq!(parse_chatgpt_export("[]"), Some(Vec::new()));
1633        assert_eq!(parse_claude_ai_export("[]"), Some(Vec::new()));
1634    }
1635
1636    // ── parse_chatgpt_export: direct unit tests ─────────────────────────────
1637    //
1638    // Unlike the end-to-end mirror_chatgpt_export_file tests in ingest.rs
1639    // (which go through file I/O + SQL), these exercise the pure JSON-tree
1640    // parsing seam directly with no runtime or DB setup.
1641
1642    #[test]
1643    fn test_chatgpt_export_minimal_valid_export() {
1644        let export = serde_json::json!([{
1645            "id": "conv-min",
1646            "title": "Minimal Export",
1647            "current_node": "msg-assistant",
1648            "create_time": 1_700_000_000.0,
1649            "mapping": {
1650                "root": {
1651                    "id": "root",
1652                    "message": null,
1653                    "parent": null,
1654                    "children": ["msg-user"]
1655                },
1656                "msg-user": {
1657                    "id": "msg-user",
1658                    "parent": "root",
1659                    "children": ["msg-assistant"],
1660                    "message": {
1661                        "id": "msg-user",
1662                        "author": {"role": "user"},
1663                        "content": {"content_type": "text", "parts": ["Hello"]}
1664                    }
1665                },
1666                "msg-assistant": {
1667                    "id": "msg-assistant",
1668                    "parent": "msg-user",
1669                    "children": [],
1670                    "message": {
1671                        "id": "msg-assistant",
1672                        "author": {"role": "assistant"},
1673                        "content": {"content_type": "text", "parts": ["Hi there"]}
1674                    }
1675                }
1676            }
1677        }]);
1678        let content = serde_json::to_string(&export).unwrap();
1679
1680        let events = parse_chatgpt_export(&content).expect("valid export must parse");
1681        assert_eq!(events.len(), 2, "root has no message, 2 nodes carry one");
1682
1683        assert_eq!(events[0].uuid, "msg-user");
1684        assert_eq!(events[0].session_id, "conv-min");
1685        assert_eq!(events[0].role.as_deref(), Some("user"));
1686        assert_eq!(events[0].text.as_deref(), Some("Hello"));
1687        assert_eq!(events[0].parent_uuid, None, "root carries no message");
1688        assert!(!events[0].is_sidechain);
1689        assert_eq!(events[0].slug.as_deref(), Some("Minimal Export"));
1690
1691        assert_eq!(events[1].uuid, "msg-assistant");
1692        assert_eq!(events[1].role.as_deref(), Some("assistant"));
1693        assert_eq!(events[1].text.as_deref(), Some("Hi there"));
1694        assert_eq!(events[1].parent_uuid.as_deref(), Some("msg-user"));
1695        assert!(!events[1].is_sidechain);
1696    }
1697
1698    #[test]
1699    fn test_chatgpt_export_masks_secret_bearing_title() {
1700        let secret = format!("{}{}", "AKIA", "FAKEKEY1234567890");
1701        let export = serde_json::json!([{
1702            "id": "conv-secret-title",
1703            "title": format!("prod creds {secret}"),
1704            "current_node": "msg-user",
1705            "create_time": 1_700_000_000.0,
1706            "mapping": {
1707                "root": {
1708                    "id": "root",
1709                    "message": null,
1710                    "parent": null,
1711                    "children": ["msg-user"]
1712                },
1713                "msg-user": {
1714                    "id": "msg-user",
1715                    "parent": "root",
1716                    "children": [],
1717                    "message": {
1718                        "id": "msg-user",
1719                        "author": {"role": "user"},
1720                        "content": {"content_type": "text", "parts": ["Hello"]}
1721                    }
1722                }
1723            }
1724        }]);
1725        let content = serde_json::to_string(&export).unwrap();
1726
1727        let events = parse_chatgpt_export(&content).expect("valid export must parse");
1728        assert_eq!(events.len(), 1);
1729        let slug = events[0].slug.as_deref().expect("event slug");
1730        assert!(!slug.contains(&secret));
1731        assert!(slug.contains("***MASKED***"));
1732    }
1733
1734    #[test]
1735    fn test_chatgpt_export_multi_branch_tree_dfs_order_and_sidechain() {
1736        // root -> user -> {main (current path), alt (regenerated/abandoned)}
1737        let export = serde_json::json!([{
1738            "id": "conv-branch",
1739            "title": "Branching Conversation",
1740            "current_node": "msg-main",
1741            "mapping": {
1742                "root": {
1743                    "id": "root",
1744                    "message": null,
1745                    "parent": null,
1746                    "children": ["msg-user"]
1747                },
1748                "msg-user": {
1749                    "id": "msg-user",
1750                    "parent": "root",
1751                    "children": ["msg-main", "msg-alt"],
1752                    "message": {
1753                        "id": "msg-user",
1754                        "author": {"role": "user"},
1755                        "content": {"content_type": "text", "parts": ["Question"]}
1756                    }
1757                },
1758                "msg-main": {
1759                    "id": "msg-main",
1760                    "parent": "msg-user",
1761                    "children": [],
1762                    "message": {
1763                        "id": "msg-main",
1764                        "author": {"role": "assistant"},
1765                        "content": {"content_type": "text", "parts": ["Main answer"]}
1766                    }
1767                },
1768                "msg-alt": {
1769                    "id": "msg-alt",
1770                    "parent": "msg-user",
1771                    "children": [],
1772                    "message": {
1773                        "id": "msg-alt",
1774                        "author": {"role": "assistant"},
1775                        "content": {"content_type": "text", "parts": ["Alternate answer"]}
1776                    }
1777                }
1778            }
1779        }]);
1780        let content = serde_json::to_string(&export).unwrap();
1781
1782        let events = parse_chatgpt_export(&content).expect("branch export must parse");
1783        assert_eq!(events.len(), 3);
1784
1785        // DFS preorder must follow the mapping's `children` array order, not
1786        // JSON object key order: user, then main, then alt.
1787        let uuids: Vec<&str> = events.iter().map(|e| e.uuid.as_str()).collect();
1788        assert_eq!(uuids, vec!["msg-user", "msg-main", "msg-alt"]);
1789
1790        let by_uuid = |id: &str| events.iter().find(|e| e.uuid == id).unwrap();
1791        assert!(!by_uuid("msg-user").is_sidechain);
1792        assert!(
1793            !by_uuid("msg-main").is_sidechain,
1794            "current_node's own path must not be flagged"
1795        );
1796        assert!(
1797            by_uuid("msg-alt").is_sidechain,
1798            "branch off current_node path must be flagged sidechain"
1799        );
1800        assert_eq!(by_uuid("msg-alt").text.as_deref(), Some("Alternate answer"));
1801    }
1802
1803    #[test]
1804    fn test_chatgpt_export_malformed_inputs() {
1805        // Top level not valid JSON at all -> None (caller must not advance cursor).
1806        assert!(parse_chatgpt_export("not json").is_none());
1807
1808        // Valid JSON but not an array -> None.
1809        assert!(parse_chatgpt_export(r#"{"oops": "not an array"}"#).is_none());
1810
1811        // Valid array, but individual malformed conversations (missing
1812        // mapping, missing id, non-object entry) must be skipped
1813        // individually rather than sinking the whole file.
1814        let export = serde_json::json!([
1815            {"id": "no-mapping"},
1816            {"mapping": {}},
1817            "not-an-object",
1818            {
1819                "id": "conv-good",
1820                "current_node": "msg-good",
1821                "mapping": {
1822                    "root": {"id": "root", "message": null, "parent": null, "children": ["msg-good"]},
1823                    "msg-good": {
1824                        "id": "msg-good",
1825                        "parent": "root",
1826                        "children": [],
1827                        "message": {
1828                            "id": "msg-good",
1829                            "author": {"role": "user"},
1830                            "content": {"content_type": "text", "parts": ["Still works"]}
1831                        }
1832                    }
1833                }
1834            }
1835        ]);
1836        let content = serde_json::to_string(&export).unwrap();
1837
1838        let events = parse_chatgpt_export(&content)
1839            .expect("array with some malformed conversations still parses");
1840        assert_eq!(
1841            events.len(),
1842            1,
1843            "malformed conversations skipped, valid one still yields its event"
1844        );
1845        assert_eq!(events[0].uuid, "msg-good");
1846        assert_eq!(events[0].session_id, "conv-good");
1847    }
1848}