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