memvid-cli 2.0.140

Command-line interface for Memvid v2 - AI memory with crash-safe, single-file storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! CLI commands for time-travel replay session management.
//!
//! These commands enable recording and managing agent sessions for
//! debugging and testing purposes.

use anyhow::Result;
use clap::{Args, Subcommand};
use colored::Colorize;
#[cfg(feature = "replay")]
use memvid_ask_model::run_model_inference;
#[cfg(feature = "replay")]
use memvid_core::replay::ActionType;
use memvid_core::Memvid;

/// Session management commands for time-travel replay
#[derive(Args, Debug)]
pub struct SessionArgs {
    #[command(subcommand)]
    pub command: SessionCommand,
}

#[derive(Subcommand, Debug)]
pub enum SessionCommand {
    /// Start recording a new session
    Start(SessionStartArgs),
    /// End the current recording session
    End(SessionEndArgs),
    /// List all recorded sessions
    List(SessionListArgs),
    /// View details of a specific session
    View(SessionViewArgs),
    /// Create a checkpoint in the current session
    Checkpoint(SessionCheckpointArgs),
    /// Delete a recorded session
    Delete(SessionDeleteArgs),
    /// Save sessions to the memory file
    Save(SessionSaveArgs),
    /// Load sessions from the memory file
    Load(SessionLoadArgs),
    /// Replay a recorded session and verify consistency
    Replay(SessionReplayArgs),
    /// Compare two recorded sessions
    Compare(SessionCompareArgs),
}

#[derive(Args, Debug)]
pub struct SessionStartArgs {
    /// Path to the memory file
    pub file: String,

    /// Optional name for the session
    #[arg(short, long)]
    pub name: Option<String>,

    /// Auto-checkpoint interval (number of actions between checkpoints, 0 = disabled)
    #[arg(long, default_value = "0")]
    pub auto_checkpoint: u64,
}

#[derive(Args, Debug)]
pub struct SessionEndArgs {
    /// Path to the memory file
    pub file: String,
}

#[derive(Args, Debug)]
pub struct SessionListArgs {
    /// Path to the memory file
    pub file: String,

    /// Output format (text, json)
    #[arg(short, long, default_value = "text")]
    pub format: String,
}

#[derive(Args, Debug)]
pub struct SessionViewArgs {
    /// Path to the memory file
    pub file: String,

    /// Session ID to view
    #[arg(short, long)]
    pub session: String,

    /// Output format (text, json)
    #[arg(short, long, default_value = "text")]
    pub format: String,
}

#[derive(Args, Debug)]
pub struct SessionCheckpointArgs {
    /// Path to the memory file
    pub file: String,
}

#[derive(Args, Debug)]
pub struct SessionDeleteArgs {
    /// Path to the memory file
    pub file: String,

    /// Session ID to delete
    #[arg(short, long)]
    pub session: String,
}

#[derive(Args, Debug)]
pub struct SessionSaveArgs {
    /// Path to the memory file
    pub file: String,
}

#[derive(Args, Debug)]
pub struct SessionLoadArgs {
    /// Path to the memory file
    pub file: String,
}

#[derive(Args, Debug)]
pub struct SessionReplayArgs {
    /// Path to the memory file
    pub file: String,

    /// Session ID to replay
    #[arg(short, long)]
    pub session: String,

    /// Start replay from a specific checkpoint
    #[arg(long)]
    pub from_checkpoint: Option<u64>,

    /// Number of results to retrieve during replay (default: same as original)
    /// Use higher values to discover documents that were missed due to low top-k
    #[arg(short = 'k', long)]
    pub top_k: Option<usize>,

    /// Use adaptive retrieval that adjusts top-k based on score distribution
    #[arg(long)]
    pub adaptive: bool,

    /// Minimum relevancy score for adaptive retrieval (0.0-1.0)
    #[arg(long, default_value = "0.5")]
    pub min_relevancy: f32,

    /// Skip put/update/delete actions
    #[arg(long)]
    pub skip_mutations: bool,

    /// Skip find/search actions
    #[arg(long)]
    pub skip_finds: bool,

    /// Skip ask actions (LLM calls)
    #[arg(long)]
    pub skip_asks: bool,

    /// Stop on first mismatch
    #[arg(long)]
    pub stop_on_mismatch: bool,

    /// AUDIT MODE: Use frozen retrieval for ASK actions
    /// Replays use recorded frame IDs instead of re-executing search
    #[arg(long)]
    pub audit: bool,

    /// Override model for audit replay (format: "provider:model")
    /// When set, prepares for LLM re-execution with frozen context
    #[arg(long)]
    pub use_model: Option<String>,

    /// Generate diff report comparing original vs new answers
    #[arg(long)]
    pub diff: bool,

    /// Output format (text, json)
    #[arg(short, long, default_value = "text")]
    pub format: String,

    /// Launch the Time Machine web UI for visual replay
    #[cfg(feature = "web")]
    #[arg(long)]
    pub web: bool,

    /// Port for the Time Machine web server (default: 4242)
    #[cfg(feature = "web")]
    #[arg(long, default_value = "4242")]
    pub port: u16,

    /// Don't auto-open browser when starting web UI
    #[cfg(feature = "web")]
    #[arg(long)]
    pub no_open: bool,
}

#[derive(Args, Debug)]
pub struct SessionCompareArgs {
    /// Path to the memory file
    pub file: String,

    /// First session ID
    #[arg(short = 'a', long)]
    pub session_a: String,

    /// Second session ID
    #[arg(short = 'b', long)]
    pub session_b: String,

    /// Output format (text, json)
    #[arg(short, long, default_value = "text")]
    pub format: String,
}

/// Handle session subcommands
#[cfg(feature = "replay")]
pub fn handle_session(args: SessionArgs) -> Result<()> {
    match args.command {
        SessionCommand::Start(args) => handle_session_start(args),
        SessionCommand::End(args) => handle_session_end(args),
        SessionCommand::List(args) => handle_session_list(args),
        SessionCommand::View(args) => handle_session_view(args),
        SessionCommand::Checkpoint(args) => handle_session_checkpoint(args),
        SessionCommand::Delete(args) => handle_session_delete(args),
        SessionCommand::Save(args) => handle_session_save(args),
        SessionCommand::Load(args) => handle_session_load(args),
        SessionCommand::Replay(args) => handle_session_replay(args),
        SessionCommand::Compare(args) => handle_session_compare(args),
    }
}

#[cfg(not(feature = "replay"))]
pub fn handle_session(_args: SessionArgs) -> Result<()> {
    bail!("Session recording requires the 'replay' feature. Rebuild with --features replay")
}

#[cfg(feature = "replay")]
fn handle_session_start(args: SessionStartArgs) -> Result<()> {
    use memvid_core::replay::ReplayConfig;

    let mut mem = Memvid::open(&args.file)?;

    // Check if there's already an active session
    if mem.load_active_session()? {
        let session_id = mem.active_session_id().unwrap();
        anyhow::bail!(
            "Session {} is already active. End it first with `session end`.",
            session_id
        );
    }

    let config = ReplayConfig {
        auto_checkpoint_interval: args.auto_checkpoint,
        ..Default::default()
    };

    let session_id = mem.start_session(args.name, Some(config))?;

    // Save the active session to a sidecar file so it persists across commands
    mem.save_active_session()?;

    println!("Started session: {}", session_id);
    println!("Recording is now active. Run operations like put, find, ask.");
    println!("End the session with: memvid session end {}", args.file);

    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_end(args: SessionEndArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    // Load the active session from the sidecar file
    if !mem.load_active_session()? {
        anyhow::bail!("No active session found. Start one with `session start`.");
    }

    let session = mem.end_session()?;
    println!("Ended session: {}", session.session_id);
    println!("  Actions recorded: {}", session.actions.len());
    println!("  Checkpoints: {}", session.checkpoints.len());
    println!("  Duration: {}s", session.duration_secs());

    // First commit to finalize any pending Tantivy/index data
    // This ensures data_end is accurate before saving replay segment
    mem.commit()?;

    // Now save the replay sessions after all other data is finalized
    mem.save_replay_sessions()?;

    // Commit again to persist the replay manifest in TOC
    mem.commit()?;

    // Remove the sidecar file
    mem.clear_active_session_file()?;

    println!("Session saved to file.");
    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_list(args: SessionListArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    // Check for active session
    let has_active = mem.load_active_session()?;

    // Load completed sessions from file
    mem.load_replay_sessions()?;

    let sessions = mem.list_sessions();

    if !has_active && sessions.is_empty() {
        println!("No recorded sessions.");
        return Ok(());
    }

    if args.format == "json" {
        let json = serde_json::to_string_pretty(&sessions)?;
        println!("{}", json);
    } else {
        // Show active session first if present
        if has_active {
            if let Some(session_id) = mem.active_session_id() {
                println!("Active session (recording):");
                println!("  {} - currently recording", session_id);
                println!();
            }
        }

        if !sessions.is_empty() {
            println!("Completed sessions ({}):", sessions.len());
            for summary in sessions {
                let name = summary.name.as_deref().unwrap_or("(unnamed)");
                let status = if summary.ended_secs.is_some() {
                    "completed"
                } else {
                    "recording"
                };
                println!(
                    "  {} - {} [{}, {} actions, {} checkpoints]",
                    summary.session_id,
                    name,
                    status,
                    summary.action_count,
                    summary.checkpoint_count
                );
            }
        }
    }

    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_view(args: SessionViewArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    // Load sessions from file
    mem.load_replay_sessions()?;

    let session_id: uuid::Uuid = args.session.parse()?;
    let session = mem
        .get_session(session_id)
        .ok_or_else(|| anyhow::anyhow!("Session {} not found", session_id))?;

    if args.format == "json" {
        let json = serde_json::to_string_pretty(&session)?;
        println!("{}", json);
    } else {
        let name = session.name.as_deref().unwrap_or("(unnamed)");
        println!("Session: {} - {}", session.session_id, name);
        println!("  Created: {}", session.created_secs);
        if let Some(ended) = session.ended_secs {
            println!("  Ended: {}", ended);
            println!("  Duration: {}s", session.duration_secs());
        } else {
            println!("  Status: recording");
        }
        println!("  Actions: {}", session.actions.len());
        println!("  Checkpoints: {}", session.checkpoints.len());

        if !session.actions.is_empty() {
            println!("\nActions:");
            for (i, action) in session.actions.iter().enumerate().take(20) {
                println!(
                    "  [{}] {} - {:?}",
                    i,
                    action.action_type.name(),
                    action.action_type
                );
            }
            if session.actions.len() > 20 {
                println!("  ... and {} more", session.actions.len() - 20);
            }
        }
    }

    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_checkpoint(args: SessionCheckpointArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    let checkpoint_id = mem.create_checkpoint()?;
    println!("Created checkpoint: {}", checkpoint_id);

    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_delete(args: SessionDeleteArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    // Load sessions from file
    mem.load_replay_sessions()?;

    let session_id: uuid::Uuid = args.session.parse()?;
    mem.delete_session(session_id)?;

    // Save updated sessions
    mem.save_replay_sessions()?;
    mem.commit()?;

    println!("Deleted session: {}", session_id);
    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_save(args: SessionSaveArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    // Load any existing sessions from file first
    mem.load_replay_sessions()?;

    // Check if there are any sessions to save
    let session_count = mem.list_sessions().len();
    if session_count == 0 {
        println!(
            "No sessions to save. Sessions are automatically saved when you run `session end`."
        );
        return Ok(());
    }

    // Re-save all sessions (useful if you added sessions via API or need to re-persist)
    mem.save_replay_sessions()?;
    mem.commit()?;

    println!("Saved {} session(s) to file.", session_count);
    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_load(args: SessionLoadArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    mem.load_replay_sessions()?;

    let sessions = mem.list_sessions();
    println!("Loaded {} sessions from file.", sessions.len());

    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_replay(args: SessionReplayArgs) -> Result<()> {
    // Check if web UI is requested
    #[cfg(feature = "web")]
    if args.web {
        use crate::commands::session_web::start_web_server;
        use std::path::PathBuf;

        let rt = tokio::runtime::Runtime::new()?;
        return rt.block_on(start_web_server(
            args.session.clone(),
            PathBuf::from(&args.file),
            args.port,
            !args.no_open,
        ));
    }

    use memvid_core::replay::{ReplayEngine, ReplayExecutionConfig};

    // Use open_read_only to match how find command opens files
    // This ensures lex_enabled is properly set from has_lex_index()
    let mut mem = Memvid::open_read_only(&args.file)?;

    // Load sessions from file
    mem.load_replay_sessions()?;

    let session_id: uuid::Uuid = args.session.parse()?;
    let session = mem
        .get_session(session_id)
        .ok_or_else(|| anyhow::anyhow!("Session {} not found", session_id))?
        .clone();

    let config = ReplayExecutionConfig {
        skip_puts: args.skip_mutations,
        skip_finds: args.skip_finds,
        skip_asks: args.skip_asks,
        stop_on_mismatch: args.stop_on_mismatch,
        verbose: true,
        top_k: args.top_k,
        adaptive: args.adaptive,
        min_relevancy: args.min_relevancy,
        audit_mode: args.audit,
        use_model: args.use_model.clone(),
        generate_diff: args.diff,
    };

    let mut engine = ReplayEngine::new(&mut mem, config);
    let result = engine.replay_session_from(&session, args.from_checkpoint)?;

    if args.format == "json" {
        let json = serde_json::to_string_pretty(&result)?;
        println!("{}", json);
    } else {
        println!();
        println!("{}", "".repeat(60).dimmed());
        println!(
            "{} {}",
            "Replaying session:".bold(),
            session_id.to_string().cyan()
        );
        println!("{} {}", "File:".dimmed(), args.file);

        // Show mode based on flags
        let mode_str = if args.audit {
            "Audit (frozen retrieval)".green().to_string()
        } else {
            "Debug (showing recorded actions)".white().to_string()
        };
        println!("{} {}", "Mode:".dimmed(), mode_str);

        // Show model override if set
        if let Some(ref model) = args.use_model {
            println!("{} {}", "Model Override:".dimmed(), model.yellow());
        }

        println!("{}", "".repeat(60).dimmed());
        println!();

        // Show all actions with their details
        let total = result.action_results.len();
        for (action_idx, action_result) in result.action_results.iter().enumerate() {
            let seq = action_result.sequence;
            let action_type = &action_result.action_type;
            let diff = action_result.diff.as_deref().unwrap_or("");

            // Skip "skipped" actions in display unless they're ASK
            if diff == "skipped" && action_type != "ASK" {
                continue;
            }

            // Format based on action type
            if action_type == "ASK" {
                // Rich output for ASK actions
                let status = if action_result.matched {
                    "".green()
                } else {
                    "".red()
                };
                println!(
                    "{} Step {}/{} {} {}",
                    status,
                    (seq + 1).to_string().yellow(),
                    total,
                    "ask".cyan().bold(),
                    ""
                );
                // Print multiline diff with proper indentation
                for line in diff.lines() {
                    println!("         {}", line);
                }

                // If audit mode with use_model, re-execute LLM with frozen context
                if args.audit && args.use_model.is_some() {
                    if let Some(original_action) = session.actions.get(action_idx) {
                        if let ActionType::Ask { query, .. } = &original_action.action_type {
                            let frame_ids = &original_action.affected_frames;
                            if !frame_ids.is_empty() {
                                // Build context from frozen frames
                                let mut context_parts = Vec::new();
                                for (idx, frame_id) in frame_ids.iter().enumerate() {
                                    if let Ok(content) = mem.frame_text_by_id(*frame_id) {
                                        // Truncate to reasonable size
                                        let snippet = if content.len() > 1000 {
                                            format!("{}...", &content[..1000])
                                        } else {
                                            content
                                        };
                                        context_parts.push(format!("[{}] {}", idx + 1, snippet));
                                    }
                                }
                                let frozen_context = context_parts.join("\n\n");

                                // Call LLM with override model
                                let model_name = args.use_model.as_ref().unwrap();
                                match run_model_inference(
                                    model_name,
                                    query,
                                    &frozen_context,
                                    &[], // No hits needed
                                    None,
                                    None,
                                    None,
                                ) {
                                    Ok(inference) => {
                                        let new_answer = &inference.answer.answer;
                                        let original_answer = &original_action.output_preview;

                                        // Show the new answer
                                        println!(
                                            "         {} \"{}\"",
                                            "New Answer:".green().bold(),
                                            if new_answer.len() > 150 {
                                                format!("{}...", &new_answer[..150])
                                            } else {
                                                new_answer.clone()
                                            }
                                        );

                                        // Show diff if requested
                                        if args.diff {
                                            let same = new_answer.trim() == original_answer.trim();
                                            if same {
                                                println!(
                                                    "         {} {}",
                                                    "Diff:".dimmed(),
                                                    "IDENTICAL".green()
                                                );
                                            } else {
                                                println!(
                                                    "         {} {}",
                                                    "Diff:".dimmed(),
                                                    "CHANGED".yellow()
                                                );
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        println!("         {} {}", "LLM Error:".red(), e);
                                    }
                                }
                            }
                        }
                    }
                }
                println!();
            } else if action_type == "FIND" && diff.contains("DISCOVERY:")
                || diff.contains("FILTER:")
            {
                // Rich output for FIND with discoveries
                let colored_diff = if diff.starts_with("FILTER:") {
                    diff.replace("FILTER:", &"FILTER:".red().bold().to_string())
                        .replace("would be MISSED", &"would be MISSED".red().to_string())
                } else if diff.starts_with("DISCOVERY:") {
                    diff.replace("DISCOVERY:", &"DISCOVERY:".green().bold().to_string())
                        .replace("[NEW]", &"[NEW]".green().to_string())
                } else {
                    diff.to_string()
                };
                println!(
                    "  Step {}/{} {} - {}",
                    (seq + 1).to_string().yellow(),
                    total,
                    action_type.cyan(),
                    colored_diff
                );
            } else if !diff.is_empty() && diff != "skipped" {
                // Standard output for other actions with diffs
                let status = if action_result.matched { "" } else { "" };
                println!(
                    "  {} Step {}/{} {} - {}",
                    if action_result.matched {
                        status.green()
                    } else {
                        status.red()
                    },
                    (seq + 1).to_string().yellow(),
                    total,
                    action_type.cyan(),
                    diff.dimmed()
                );
            }
        }

        println!("{}", "".repeat(60).dimmed());
        println!();
        println!("{}", "Summary:".bold());
        println!(
            "  Total actions: {}",
            result.total_actions.to_string().white()
        );
        println!("  Matched: {}", result.matched_actions.to_string().green());
        println!(
            "  Mismatched: {}",
            result.mismatched_actions.to_string().red()
        );
        println!("  Skipped: {}", result.skipped_actions.to_string().yellow());
        println!(
            "  Match rate: {}",
            format!("{:.1}%", result.match_rate()).cyan()
        );
        println!("  Duration: {}ms", result.total_duration_ms);

        if let Some(cp) = result.from_checkpoint {
            println!("  Started from checkpoint: {}", cp);
        }

        println!();
        if result.is_success() {
            println!("{}", "✓ Replay completed successfully".green().bold());
        } else {
            println!("{}", "✗ Replay found mismatches".red().bold());
        }
    }

    Ok(())
}

#[cfg(feature = "replay")]
fn handle_session_compare(args: SessionCompareArgs) -> Result<()> {
    use memvid_core::replay::ReplayEngine;

    let mut mem = Memvid::open(&args.file)?;

    // Load sessions from file
    mem.load_replay_sessions()?;

    let session_a_id: uuid::Uuid = args.session_a.parse()?;
    let session_b_id: uuid::Uuid = args.session_b.parse()?;

    let session_a = mem
        .get_session(session_a_id)
        .ok_or_else(|| anyhow::anyhow!("Session {} not found", session_a_id))?
        .clone();

    let session_b = mem
        .get_session(session_b_id)
        .ok_or_else(|| anyhow::anyhow!("Session {} not found", session_b_id))?
        .clone();

    let comparison = ReplayEngine::compare_sessions(&session_a, &session_b);

    if args.format == "json" {
        let json = serde_json::to_string_pretty(&comparison)?;
        println!("{}", json);
    } else {
        println!("Session Comparison:");
        println!("  Session A: {}", session_a_id);
        println!("  Session B: {}", session_b_id);
        println!();

        if comparison.is_identical() {
            println!("✓ Sessions are identical");
            println!("  Matching actions: {}", comparison.matching_actions);
        } else {
            println!("✗ Sessions differ:");
            println!("  Matching actions: {}", comparison.matching_actions);

            if !comparison.actions_only_in_a.is_empty() {
                println!("  Actions only in A: {:?}", comparison.actions_only_in_a);
            }

            if !comparison.actions_only_in_b.is_empty() {
                println!("  Actions only in B: {:?}", comparison.actions_only_in_b);
            }

            if !comparison.differing_actions.is_empty() {
                println!("  Differing actions:");
                for diff in &comparison.differing_actions {
                    println!(
                        "    [{}] {} vs {} - {}",
                        diff.sequence, diff.action_type_a, diff.action_type_b, diff.description
                    );
                }
            }
        }
    }

    Ok(())
}