aicx 0.9.2

Operator CLI + MCP server: canonical corpus first, optional semantic index second (Claude Code, Codex, Gemini)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#![allow(unused_imports)]
use crate::sources::*;
use serde::Deserialize;
use std::collections::HashMap;
use std::io::BufReader;

use crate::timeline::FrameKind;

/// Claude Code JSONL entry structure.
#[derive(Debug, Deserialize)]
struct ClaudeEntry {
    #[serde(rename = "type")]
    entry_type: String,
    #[serde(default)]
    message: Option<serde_json::Value>,
    #[serde(default)]
    timestamp: Option<String>,
    #[serde(rename = "sessionId", default)]
    session_id: Option<String>,
    #[serde(rename = "gitBranch", default)]
    git_branch: Option<String>,
    #[serde(default)]
    cwd: Option<String>,
    /// Harness-injected/meta marker. When `true`, the row is synthetic context
    /// (hook output, system reminder, injected card) rather than real
    /// conversation, so every block it produces is reclassified to
    /// `FrameKind::SystemNote`.
    #[serde(rename = "isMeta", default)]
    is_meta: Option<bool>,
}

#[derive(Debug)]
struct ClaudeRawEntry {
    entry: ClaudeEntry,
    line_number: usize,
}

fn frame_kind_from_claude_type(entry_type: &str) -> Option<FrameKind> {
    match entry_type {
        "user" => Some(FrameKind::UserMsg),
        "assistant" => Some(FrameKind::AgentReply),
        "thinking" => Some(FrameKind::InternalThought),
        "tool_use" | "tool_result" => Some(FrameKind::ToolCall),
        _ => None,
    }
}

fn extract_claude_classified_blocks(
    message: &Option<serde_json::Value>,
    fallback_role: &str,
) -> Vec<ClassifiedFrameBlock> {
    fn from_content(
        content: &serde_json::Value,
        role: &str,
        blocks: &mut Vec<ClassifiedFrameBlock>,
    ) {
        match content {
            serde_json::Value::String(text) => {
                if let Some(frame_kind) = frame_kind_from_role(role) {
                    push_classified_block(blocks, role, frame_kind, text.clone());
                }
            }
            serde_json::Value::Array(items) => {
                for item in items {
                    from_content(item, role, blocks);
                }
            }
            serde_json::Value::Object(block) => {
                let block_type = block.get("type").and_then(|value| value.as_str());
                match block_type {
                    Some("text") => {
                        if let Some(text) = block.get("text").and_then(|value| value.as_str())
                            && let Some(frame_kind) = frame_kind_from_role(role)
                        {
                            push_classified_block(blocks, role, frame_kind, text.to_string());
                        }
                    }
                    Some("thinking") => {
                        if let Some(thinking) = render_claude_thinking_block(block) {
                            push_classified_block(
                                blocks,
                                role_for_frame_kind(FrameKind::InternalThought),
                                FrameKind::InternalThought,
                                thinking,
                            );
                        }
                    }
                    Some("tool_use") | Some("tool_result") => {
                        push_classified_block(
                            blocks,
                            role_for_frame_kind(FrameKind::ToolCall),
                            FrameKind::ToolCall,
                            render_claude_tool_block(block),
                        );
                    }
                    _ => {
                        if let Some(content) = block.get("content") {
                            let nested_role = block
                                .get("role")
                                .and_then(|value| value.as_str())
                                .unwrap_or(role);
                            from_content(content, nested_role, blocks);
                        } else if block.get("thought").and_then(|value| value.as_bool())
                            == Some(true)
                        {
                            if let Some(thinking) = render_claude_thinking_block(block) {
                                push_classified_block(
                                    blocks,
                                    role_for_frame_kind(FrameKind::InternalThought),
                                    FrameKind::InternalThought,
                                    thinking,
                                );
                            }
                        } else if let Some(text) =
                            extract_text_from_json_value(&serde_json::Value::Object(block.clone()))
                            && let Some(frame_kind) = frame_kind_from_role(role)
                        {
                            push_classified_block(blocks, role, frame_kind, text);
                        }
                    }
                }
            }
            _ => {}
        }
    }

    let mut blocks = Vec::new();
    match message {
        Some(serde_json::Value::Object(object)) => {
            let role = object
                .get("role")
                .and_then(|value| value.as_str())
                .unwrap_or(fallback_role);
            if let Some(content) = object.get("content") {
                from_content(content, role, &mut blocks);
            } else {
                from_content(
                    &serde_json::Value::Object(object.clone()),
                    role,
                    &mut blocks,
                );
            }
        }
        Some(value) => from_content(value, fallback_role, &mut blocks),
        None => {}
    }

    blocks
}

fn extract_claude_line_entries(
    entry: ClaudeEntry,
    timestamp: DateTime<Utc>,
    timestamp_source: Option<&str>,
    session_id: &str,
    config: &ExtractionConfig,
    warnings: &mut Vec<ClaudeSessionWarning>,
) -> Vec<TimelineEntry> {
    if timestamp < config.cutoff || config.watermark.is_some_and(|wm| timestamp < wm) {
        return Vec::new();
    }

    let mut entries = Vec::new();
    // A harness-injected/meta row is synthetic context, not real conversation:
    // collapse every block it produces to SystemNote so the conversation
    // projection and the user-only/intent lane (both keyed on frame_kind) drop
    // it structurally, regardless of its surface role.
    let force_system = entry.is_meta == Some(true);
    let fallback_role = if entry.entry_type == "tool_use" || entry.entry_type == "tool_result" {
        role_for_frame_kind(FrameKind::ToolCall)
    } else {
        entry.entry_type.as_str()
    };

    let classified = extract_claude_classified_blocks(&entry.message, fallback_role);
    if !classified.is_empty() {
        for block in classified {
            let frame_kind = if force_system {
                FrameKind::SystemNote
            } else {
                block.frame_kind
            };
            let role = if force_system {
                role_for_frame_kind(FrameKind::SystemNote)
            } else {
                block.role.as_str()
            };
            if !should_keep_entry(Some(frame_kind), config) {
                continue;
            }
            entries.push(build_timeline_entry_with_content_warnings(
                timestamp,
                "claude",
                session_id,
                role,
                block.message,
                TimelineEntryMeta {
                    branch: entry.git_branch.clone(),
                    cwd: entry.cwd.clone(),
                    frame_kind: Some(frame_kind),
                    timestamp_source: timestamp_source.map(str::to_string),
                },
                warnings,
            ));
        }
        return entries;
    }

    let message = extract_message_text(&entry.message);
    if message.trim().is_empty() {
        return Vec::new();
    }

    let frame_kind = if force_system {
        Some(FrameKind::SystemNote)
    } else {
        frame_kind_from_claude_type(&entry.entry_type)
            .or_else(|| frame_kind_from_role(fallback_role))
    };
    if !should_keep_entry(frame_kind, config) {
        return Vec::new();
    }
    let role = if force_system {
        role_for_frame_kind(FrameKind::SystemNote)
    } else {
        fallback_role
    };

    entries.push(build_timeline_entry_with_content_warnings(
        timestamp,
        "claude",
        session_id,
        role,
        message,
        TimelineEntryMeta {
            branch: entry.git_branch,
            cwd: entry.cwd,
            frame_kind,
            timestamp_source: timestamp_source.map(str::to_string),
        },
        warnings,
    ));
    entries
}

fn select_claude_session_id<'a>(
    entries: impl IntoIterator<Item = &'a ClaudeEntry>,
    fallback: &str,
    warnings: &mut Vec<ClaudeSessionWarning>,
) -> String {
    let mut first: Option<String> = None;
    let mut ignored = Vec::new();
    for id in entries
        .into_iter()
        .filter_map(|entry| entry.session_id.as_deref())
        .map(str::trim)
        .filter(|id| !id.is_empty())
    {
        if let Some(existing) = &first {
            if existing != id && !ignored.iter().any(|seen| seen == id) {
                ignored.push(id.to_string());
            }
        } else {
            first = Some(id.to_string());
        }
    }

    match first {
        Some(first) => {
            if !ignored.is_empty() {
                warnings.push(ClaudeSessionWarning::SessionIdDrift {
                    first: first.clone(),
                    ignored,
                });
            }
            first
        }
        None => {
            warnings.push(ClaudeSessionWarning::MissingSessionId {
                fallback: fallback.to_string(),
            });
            fallback.to_string()
        }
    }
}

/// Extract timeline entries from Claude Code session files.
///
/// Reads `~/.claude/projects/<project_dir>/<uuid>.jsonl` files.
/// Uses filename stem (UUID) as session_id for consistency.
pub fn extract_claude(config: &ExtractionConfig) -> Result<Vec<TimelineEntry>> {
    let claude_dir = dirs::home_dir()
        .context("No home dir")?
        .join(".claude")
        .join("projects");

    if !claude_dir.exists() {
        return Ok(vec![]);
    }

    let mut entries: Vec<TimelineEntry> = Vec::new();

    for dir_entry in fs::read_dir(&claude_dir)? {
        let dir_entry = dir_entry?;
        let dir_name = dir_entry.file_name().to_string_lossy().to_string();

        let project_dir = dir_entry.path();
        if !project_dir.is_dir() {
            continue;
        }

        // Determine if directory name inherently matches the filter
        let dir_matches = claude_project_dir_matches_filter(&dir_name, &config.project_filter);

        for file_entry in fs::read_dir(&project_dir)? {
            let file_entry = file_entry?;
            let path = file_entry.path();

            if path.extension().is_some_and(|e| e == "jsonl") {
                let session_id = path
                    .file_stem()
                    .map(|s| s.to_string_lossy().to_string())
                    .unwrap_or_default();

                let session_entries = parse_claude_jsonl(&path, &session_id, config)?;

                // If directory name matched, keep all entries.
                // Otherwise, check if ANY entry in this session matches the project filter.
                let keep_session = dir_matches
                    || (!config.project_filter.is_empty()
                        && session_entries.iter().any(|entry| {
                            entry.cwd.as_deref().is_some_and(|c| {
                                project_filter_matches_path(c, &config.project_filter)
                            })
                        }));

                if keep_session {
                    entries.extend(session_entries);
                }
            }
        }
    }

    // Merge claude history.jsonl entries
    match extract_claude_history(config) {
        Ok(hist) => entries.extend(hist),
        Err(e) => eprintln!("Claude history extraction warning: {}", e),
    }

    entries.sort_by_key(|a| a.timestamp);
    Ok(entries)
}

/// Parse a single Claude JSONL file into timeline entries.
fn parse_claude_jsonl(
    path: &std::path::Path,
    session_id: &str,
    config: &ExtractionConfig,
) -> Result<Vec<TimelineEntry>> {
    let (entries, warnings) = parse_claude_jsonl_with_diagnostics(path, session_id, config)?;
    emit_claude_session_warnings(path, &warnings);
    Ok(entries)
}

pub(crate) fn parse_claude_jsonl_with_diagnostics(
    path: &std::path::Path,
    default_session_id: &str,
    config: &ExtractionConfig,
) -> Result<(Vec<TimelineEntry>, Vec<ClaudeSessionWarning>)> {
    let file = sanitize::open_file_validated(path)?;
    let file_mtime = file
        .metadata()
        .ok()
        .and_then(|metadata| metadata.modified().ok())
        .map(DateTime::<Utc>::from);
    let mut reader = BufReader::new(file);
    let mut raw_entries = Vec::new();
    let mut warnings = Vec::new();
    let mut oversized_count = 0usize;
    let mut oversized_samples = Vec::new();
    let mut unparsable_ts_count = 0usize;
    let mut unparsable_ts_samples = Vec::new();
    let mut fallback_ts_count = 0usize;
    let mut fallback_ts_samples = Vec::new();
    let mut line_number = 0usize;

    while let Some(limited) = sanitize::read_line_capped(&mut reader, MAX_LINE_BYTES)? {
        line_number += 1;
        if limited.exceeded {
            observe_oversized_line(&mut oversized_count, &mut oversized_samples, line_number);
            continue;
        }
        let line = limited.line;
        if line.trim().is_empty() {
            continue;
        }

        let entry: ClaudeEntry = match serde_json::from_str(&line) {
            Ok(e) => e,
            Err(_) => continue,
        };
        if !matches!(
            entry.entry_type.as_str(),
            "user" | "assistant" | "tool_use" | "tool_result"
        ) {
            continue;
        }
        raw_entries.push(ClaudeRawEntry { entry, line_number });
    }

    let effective_session_id = select_claude_session_id(
        raw_entries.iter().map(|raw| &raw.entry),
        default_session_id,
        &mut warnings,
    );
    let mut entries = Vec::new();
    let mut previous_timestamp = None;
    for raw in raw_entries {
        let (timestamp, timestamp_source) = match raw.entry.timestamp.as_deref() {
            Some(ts) => match parse_rfc3339_or_naive_utc(ts) {
                Ok(timestamp) => {
                    let timestamp = timestamp.with_timezone(&Utc);
                    previous_timestamp = Some(timestamp);
                    (timestamp, None)
                }
                Err(_) => {
                    unparsable_ts_count += 1;
                    push_unique_sample(&mut unparsable_ts_samples, ts.to_string(), 5);
                    continue;
                }
            },
            None => {
                let (timestamp, source) = if let Some(timestamp) = previous_timestamp {
                    (timestamp, "fallback_previous")
                } else if let Some(timestamp) = file_mtime {
                    (timestamp, "fallback_file_mtime")
                } else {
                    (Utc::now(), "fallback_now")
                };
                (timestamp, Some(source))
            }
        };

        let line_entries = extract_claude_line_entries(
            raw.entry,
            timestamp,
            timestamp_source,
            &effective_session_id,
            config,
            &mut warnings,
        );
        if !line_entries.is_empty()
            && let Some(source) = timestamp_source
        {
            fallback_ts_count += line_entries.len();
            if fallback_ts_samples.len() < 5 {
                fallback_ts_samples
                    .push(format!("line {}: <missing> -> {source}", raw.line_number));
            }
        }
        entries.extend(line_entries);
    }

    if oversized_count > 0 {
        warnings.push(ClaudeSessionWarning::OversizedLine {
            count: oversized_count,
            samples: oversized_samples,
        });
    }
    if fallback_ts_count > 0 {
        warnings.push(ClaudeSessionWarning::FallbackTimestamp {
            count: fallback_ts_count,
            samples: fallback_ts_samples,
        });
    }
    if unparsable_ts_count > 0 {
        warnings.push(ClaudeSessionWarning::UnparsableTimestamp {
            count: unparsable_ts_count,
            samples: unparsable_ts_samples,
        });
    }

    Ok((entries, warnings))
}

/// Extract timeline entries from a single Claude JSONL-like file by path.
///
/// This is intentionally a "direct file" extractor used by:
/// `aicx extract --format claude <path> -o <out.md>`
///
/// Unlike `extract_claude()`, this does not require the file to live under
/// `~/.claude/projects/**` nor to have a `.jsonl` extension (Claude task outputs
/// often end with `.output` but are still JSONL).
pub fn extract_claude_file(path: &Path, config: &ExtractionConfig) -> Result<Vec<TimelineEntry>> {
    let default_session_id = path
        .file_stem()
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "unknown".to_string());

    let (mut entries, warnings) =
        parse_claude_jsonl_with_diagnostics(path, &default_session_id, config)?;
    emit_claude_session_warnings(path, &warnings);

    entries.sort_by_key(|a| a.timestamp);
    Ok(entries)
}

#[derive(Debug, Deserialize)]
struct ClaudeHistoryEntry {
    display: String,
    timestamp: i64, // milliseconds epoch
    #[serde(default)]
    project: Option<String>,
    #[serde(rename = "sessionId", default)]
    session_id: Option<String>,
    /// Pasted text content keyed by paste ID. The `display` field shows
    /// "[Pasted text #N +X lines]" placeholder; actual content lives here.
    #[serde(rename = "pastedContents", default)]
    pasted_contents: HashMap<String, serde_json::Value>,
}

/// Extract timeline entries from `~/.claude/history.jsonl`.
///
/// Contains user prompts with `project` (=cwd), `display` (text), `timestamp` (ms epoch).
/// Skips slash commands (`/init`, `/status`, `/model`, etc.).
pub fn extract_claude_history(config: &ExtractionConfig) -> Result<Vec<TimelineEntry>> {
    let history_path = dirs::home_dir()
        .context("No home dir")?
        .join(".claude")
        .join("history.jsonl");

    if !history_path.exists() {
        return Ok(vec![]);
    }

    let file = sanitize::open_file_validated(&history_path)?;
    let mut reader = BufReader::new(file);
    let mut entries = Vec::new();
    let mut warnings = Vec::new();
    let mut oversized_count = 0usize;
    let mut oversized_samples = Vec::new();
    let mut invalid_epoch_count = 0usize;
    let mut invalid_epoch_samples = Vec::new();
    let mut line_number = 0usize;

    while let Some(limited) = sanitize::read_line_capped(&mut reader, MAX_LINE_BYTES)? {
        line_number += 1;
        if limited.exceeded {
            observe_oversized_line(&mut oversized_count, &mut oversized_samples, line_number);
            continue;
        }
        let line = limited.line;
        if line.trim().is_empty() {
            continue;
        }

        let entry: ClaudeHistoryEntry = match serde_json::from_str(&line) {
            Ok(e) => e,
            Err(_) => continue,
        };

        // Skip slash commands
        if entry.display.starts_with('/') {
            continue;
        }

        // Expand pastedContents into the message text
        let message = if entry.pasted_contents.is_empty() {
            entry.display.clone()
        } else {
            let mut text = entry.display.clone();
            // Sort by key to get deterministic order
            let mut paste_keys: Vec<&String> = entry.pasted_contents.keys().collect();
            paste_keys.sort();
            for key in paste_keys {
                if let Some(obj) = entry.pasted_contents[key].as_object()
                    && let Some(content) = obj.get("content").and_then(|v| v.as_str())
                {
                    text.push_str("\n\n");
                    text.push_str(content);
                }
            }
            text
        };

        if message.trim().is_empty() {
            continue;
        }

        // Project filter
        if !config.project_filter.is_empty() {
            let matches = entry
                .project
                .as_deref()
                .is_some_and(|p| project_filter_matches_path(p, &config.project_filter));
            if !matches {
                continue;
            }
        }

        let timestamp = match DateTime::<Utc>::from_timestamp_millis(entry.timestamp) {
            Some(ts) => ts,
            None => {
                invalid_epoch_count += 1;
                push_unique_sample(&mut invalid_epoch_samples, entry.timestamp.to_string(), 5);
                continue;
            }
        };

        if timestamp < config.cutoff {
            continue;
        }
        if config.watermark.is_some_and(|wm| timestamp < wm) {
            continue;
        }

        entries.push(build_timeline_entry_with_content_warnings(
            timestamp,
            "claude",
            entry.session_id.as_deref().unwrap_or("history"),
            "user",
            message,
            TimelineEntryMeta {
                branch: None,
                cwd: entry.project,
                frame_kind: Some(FrameKind::UserMsg),
                timestamp_source: None,
            },
            &mut warnings,
        ));
    }

    if oversized_count > 0 {
        warnings.push(ClaudeSessionWarning::OversizedLine {
            count: oversized_count,
            samples: oversized_samples,
        });
    }
    if invalid_epoch_count > 0 {
        warnings.push(ClaudeSessionWarning::InvalidEpochMillis {
            count: invalid_epoch_count,
            samples: invalid_epoch_samples,
        });
    }
    emit_claude_session_warnings(&history_path, &warnings);

    entries.sort_by_key(|a| a.timestamp);
    Ok(entries)
}