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
//! Maintenance command handlers (verify, doctor, verify-single-file, nudge, process-queue).
//! These commands surface integrity checks, healing workflows, and lock inspection
//! for `.mv2` files without mutating unrelated state.

use std::path::{Path, PathBuf};

use anyhow::{anyhow, bail, Result};
use clap::{ArgAction, Args};
use memvid_core::{
    lockfile, DoctorActionDetail, DoctorActionKind, DoctorFindingCode, DoctorOptions,
    DoctorPhaseKind, DoctorReport, DoctorSeverity, DoctorStatus, Memvid, VerificationStatus,
};
use serde::Serialize;

use crate::config::CliConfig;

/// Arguments for the `nudge` subcommand
#[derive(Args)]
pub struct NudgeArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
}

/// Arguments for the `verify` subcommand
#[derive(Args)]
pub struct VerifyArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(long)]
    pub deep: bool,
    #[arg(long)]
    pub json: bool,
}

/// Arguments for the `doctor` subcommand
#[derive(Args)]
pub struct DoctorArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
    #[arg(long = "rebuild-time-index", action = ArgAction::SetTrue)]
    pub rebuild_time_index: bool,
    #[arg(long = "rebuild-lex-index", action = ArgAction::SetTrue)]
    pub rebuild_lex_index: bool,
    #[arg(long = "rebuild-vec-index", action = ArgAction::SetTrue)]
    pub rebuild_vec_index: bool,
    #[arg(long = "vacuum", action = ArgAction::SetTrue)]
    pub vacuum: bool,
    #[arg(long = "plan-only", action = ArgAction::SetTrue)]
    pub plan_only: bool,
    #[arg(long = "json", action = ArgAction::SetTrue)]
    pub json: bool,
}

/// Arguments for the `verify-single-file` subcommand
#[derive(Args)]
pub struct VerifySingleFileArgs {
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,
}

// ============================================================================
// Maintenance command handlers
// ============================================================================

pub fn handle_verify(_config: &CliConfig, args: VerifyArgs) -> Result<()> {
    let report = Memvid::verify(&args.file, args.deep)?;
    if args.json {
        println!("{}", serde_json::to_string_pretty(&report)?);
    } else {
        println!("Verification report for {}", args.file.display());
        for check in &report.checks {
            match &check.details {
                Some(details) => println!("- {}: {:?} ({details})", check.name, check.status),
                None => println!("- {}: {:?}", check.name, check.status),
            }
        }
        println!("Overall: {:?}", report.overall_status);
    }

    if report.overall_status == VerificationStatus::Failed {
        anyhow::bail!("verification failed");
    }
    Ok(())
}

pub fn handle_doctor(_config: &CliConfig, args: DoctorArgs) -> Result<()> {
    let options = DoctorOptions {
        rebuild_time_index: args.rebuild_time_index,
        rebuild_lex_index: args.rebuild_lex_index,
        rebuild_vec_index: args.rebuild_vec_index,
        vacuum: args.vacuum,
        dry_run: args.plan_only,
        quiet: false,
    };

    let report = Memvid::doctor(&args.file, options)?;

    if args.json {
        println!("{}", serde_json::to_string_pretty(&report)?);
    } else {
        print_doctor_report(&args.file, &report);
    }

    match report.status {
        DoctorStatus::Failed => anyhow::bail!("doctor failed; see findings for details"),
        DoctorStatus::Partial => {
            anyhow::bail!("doctor completed with partial repairs; rerun or restore from backup")
        }
        DoctorStatus::PlanOnly => {
            if report.plan.is_noop() && !args.json {
                println!(
                    "No repairs required for {} (plan-only run)",
                    args.file.display()
                );
            } else if !args.json {
                println!("Plan generated. Re-run without --plan-only to apply repairs.");
            }
            Ok(())
        }
        _ => Ok(()),
    }
}

fn print_doctor_report(path: &Path, report: &DoctorReport) {
    println!("Doctor status for {}: {:?}", path.display(), report.status);

    if !report.plan.findings.is_empty() {
        println!("Findings:");
        for finding in &report.plan.findings {
            let severity = format_severity(finding.severity);
            let code = format_finding_code(finding.code);
            match &finding.detail {
                Some(detail) => println!(
                    "  - [{}] {}: {} ({detail})",
                    severity, code, finding.message
                ),
                None => println!("  - [{}] {}: {}", severity, code, finding.message),
            }
        }
    }

    if report.plan.phases.is_empty() {
        println!("Planned phases: (none)");
    } else {
        println!("Planned phases:");
        for phase in &report.plan.phases {
            println!("  - {}", label_phase(phase.phase));
            for action in &phase.actions {
                let mut notes: Vec<String> = Vec::new();
                if action.required {
                    notes.push("required".into());
                }
                if !action.reasons.is_empty() {
                    let reasons: Vec<String> = action
                        .reasons
                        .iter()
                        .map(|code| format_finding_code(*code))
                        .collect();
                    notes.push(format!("reasons: {}", reasons.join(", ")));
                }
                if let Some(detail) = &action.detail {
                    notes.push(format_action_detail(detail));
                }
                if let Some(note) = &action.note {
                    notes.push(note.clone());
                }
                let suffix = if notes.is_empty() {
                    String::new()
                } else {
                    format!(" ({})", notes.join(" | "))
                };
                println!("    * {}{}", label_action(action.action), suffix);
            }
        }
    }

    if report.phases.is_empty() {
        println!("Execution: (skipped)");
    } else {
        println!("Execution:");
        for phase in &report.phases {
            println!("  - {}: {:?}", label_phase(phase.phase), phase.status);
            if let Some(duration) = phase.duration_ms {
                println!("      duration: {} ms", duration);
            }
            for action in &phase.actions {
                match &action.detail {
                    Some(detail) => println!(
                        "    * {}: {:?} ({detail})",
                        label_action(action.action),
                        action.status
                    ),
                    None => println!("    * {}: {:?}", label_action(action.action), action.status),
                }
            }
        }
    }

    if report.metrics.total_duration_ms > 0 {
        println!("Total duration: {} ms", report.metrics.total_duration_ms);
    }
}

fn format_severity(severity: DoctorSeverity) -> &'static str {
    match severity {
        DoctorSeverity::Info => "info",
        DoctorSeverity::Warning => "warning",
        DoctorSeverity::Error => "error",
    }
}

fn format_finding_code(code: DoctorFindingCode) -> String {
    serde_json::to_string(&code)
        .map(|value| value.trim_matches('"').replace('_', " "))
        .unwrap_or_else(|_| format!("{code:?}"))
}

fn label_phase(kind: DoctorPhaseKind) -> &'static str {
    match kind {
        DoctorPhaseKind::Probe => "probe",
        DoctorPhaseKind::HeaderHealing => "header healing",
        DoctorPhaseKind::WalReplay => "wal replay",
        DoctorPhaseKind::IndexRebuild => "index rebuild",
        DoctorPhaseKind::Vacuum => "vacuum",
        DoctorPhaseKind::Finalize => "finalize",
        DoctorPhaseKind::Verify => "verify",
    }
}

fn label_action(kind: DoctorActionKind) -> &'static str {
    match kind {
        DoctorActionKind::HealHeaderPointer => "heal header pointer",
        DoctorActionKind::HealTocChecksum => "heal toc checksum",
        DoctorActionKind::ReplayWal => "replay wal",
        DoctorActionKind::DiscardWal => "discard wal",
        DoctorActionKind::RebuildTimeIndex => "rebuild time index",
        DoctorActionKind::RebuildLexIndex => "rebuild lex index",
        DoctorActionKind::RebuildVecIndex => "rebuild vector index",
        DoctorActionKind::VacuumCompaction => "vacuum compaction",
        DoctorActionKind::RecomputeToc => "recompute toc",
        DoctorActionKind::UpdateHeader => "update header",
        DoctorActionKind::DeepVerify => "deep verify",
        DoctorActionKind::NoOp => "no-op",
    }
}

fn format_action_detail(detail: &DoctorActionDetail) -> String {
    match detail {
        DoctorActionDetail::HeaderPointer {
            target_footer_offset,
        } => {
            format!("target offset: {}", target_footer_offset)
        }
        DoctorActionDetail::TocChecksum { expected } => {
            let checksum: String = expected.iter().map(|b| format!("{:02x}", b)).collect();
            format!("expected checksum: {}", checksum)
        }
        DoctorActionDetail::WalReplay {
            from_sequence,
            to_sequence,
            pending_records,
        } => format!(
            "apply wal records {}{} ({} pending)",
            from_sequence, to_sequence, pending_records
        ),
        DoctorActionDetail::TimeIndex { expected_entries } => {
            format!("expected entries: {}", expected_entries)
        }
        DoctorActionDetail::LexIndex { expected_docs } => {
            format!("expected docs: {}", expected_docs)
        }
        DoctorActionDetail::VecIndex {
            expected_vectors,
            dimension,
        } => format!(
            "expected vectors: {}, dimension: {}",
            expected_vectors, dimension
        ),
        DoctorActionDetail::VacuumStats { active_frames } => {
            format!("active frames: {}", active_frames)
        }
    }
}

pub fn handle_verify_single_file(_config: &CliConfig, args: VerifySingleFileArgs) -> Result<()> {
    let offenders = find_auxiliary_files(&args.file)?;
    if offenders.is_empty() {
        println!(
            "\u{2713} Single file guarantee maintained ({})",
            args.file.display()
        );
        Ok(())
    } else {
        println!("Found auxiliary files:");
        for path in &offenders {
            println!("- {}", path.display());
        }
        anyhow::bail!("auxiliary files detected")
    }
}

pub fn handle_nudge(args: NudgeArgs) -> Result<()> {
    match lockfile::current_owner(&args.file)? {
        Some(owner) => {
            if let Some(pid) = owner.pid {
                #[cfg(unix)]
                {
                    let result = unsafe { libc::kill(pid as libc::pid_t, libc::SIGUSR1) };
                    if result == 0 {
                        println!("Sent SIGUSR1 to process {pid}");
                    } else {
                        return Err(std::io::Error::last_os_error().into());
                    }
                }
                #[cfg(not(unix))]
                {
                    println!(
                        "Active writer pid {pid}; nudging is not supported on this platform. Notify the process manually."
                    );
                }
            } else {
                bail!("Active writer does not expose a pid; cannot nudge");
            }
        }
        None => {
            println!("No active writer for {}", args.file.display());
        }
    }
    Ok(())
}

fn find_auxiliary_files(memory: &Path) -> Result<Vec<PathBuf>> {
    let parent = memory
        .parent()
        .map(Path::to_path_buf)
        .unwrap_or_else(|| PathBuf::from("."));
    let name = memory
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or_else(|| anyhow!("memory path must be a valid file name"))?;

    let mut offenders = Vec::new();
    let forbidden = ["-wal", "-shm", "-lock", "-journal"];
    for suffix in &forbidden {
        let candidate = parent.join(format!("{name}{suffix}"));
        if candidate.exists() {
            offenders.push(candidate);
        }
    }
    let hidden_forbidden = [".wal", ".shm", ".lock", ".journal"];
    for suffix in &hidden_forbidden {
        let candidate = parent.join(format!(".{name}{suffix}"));
        if candidate.exists() {
            offenders.push(candidate);
        }
    }
    Ok(offenders)
}

// ============================================================================
// Process Queue Command - Progressive Ingestion Enrichment
// ============================================================================

/// Arguments for the `process-queue` subcommand
#[derive(Args)]
pub struct ProcessQueueArgs {
    /// Path to the `.mv2` file
    #[arg(value_name = "FILE", value_parser = clap::value_parser!(PathBuf))]
    pub file: PathBuf,

    /// Only show queue status without processing
    #[arg(long)]
    pub status: bool,

    /// Output as JSON
    #[arg(long)]
    pub json: bool,
}

/// Result of processing the enrichment queue
#[derive(Debug, Serialize)]
pub struct ProcessQueueResult {
    /// Number of frames in queue before processing
    pub queue_before: usize,
    /// Number of frames processed
    pub frames_processed: usize,
    /// Number of frames remaining in queue
    pub queue_after: usize,
    /// Total active frames
    pub total_frames: usize,
    /// Frames fully enriched
    pub enriched_frames: usize,
    /// Frames that are searchable but not enriched
    pub searchable_only: usize,
}

/// Handle the `process-queue` command for progressive ingestion enrichment.
///
/// This command processes frames in the enrichment queue:
/// - Re-extracts full text for skim (time-limited) extractions
/// - Updates the Tantivy index with enriched content
/// - Marks frames as Enriched when complete
pub fn handle_process_queue(_config: &CliConfig, args: ProcessQueueArgs) -> Result<()> {
    let mut mem = Memvid::open(&args.file)?;

    // Get initial stats
    let initial_stats = mem.enrichment_stats();
    let queue_before = mem.enrichment_queue_len();

    if args.status {
        // Just show status
        if args.json {
            let result = ProcessQueueResult {
                queue_before,
                frames_processed: 0,
                queue_after: queue_before,
                total_frames: initial_stats.total_frames,
                enriched_frames: initial_stats.enriched_frames,
                searchable_only: initial_stats.searchable_only,
            };
            println!("{}", serde_json::to_string_pretty(&result)?);
        } else {
            println!("Enrichment Queue Status:");
            println!("  Pending: {} frames", queue_before);
            println!("  Total frames: {}", initial_stats.total_frames);
            println!("  Enriched: {}", initial_stats.enriched_frames);
            println!("  Searchable only: {}", initial_stats.searchable_only);

            if queue_before == 0 {
                println!("\n✓ No frames pending enrichment");
            } else {
                println!(
                    "\nRun without --status to process {} pending frames",
                    queue_before
                );
            }
        }
        return Ok(());
    }

    if queue_before == 0 {
        if args.json {
            let result = ProcessQueueResult {
                queue_before: 0,
                frames_processed: 0,
                queue_after: 0,
                total_frames: initial_stats.total_frames,
                enriched_frames: initial_stats.enriched_frames,
                searchable_only: initial_stats.searchable_only,
            };
            println!("{}", serde_json::to_string_pretty(&result)?);
        } else {
            println!("✓ No frames pending enrichment");
        }
        return Ok(());
    }

    // Process all pending enrichment tasks
    if !args.json {
        eprintln!("Processing {} pending frames...", queue_before);
    }

    let start = std::time::Instant::now();
    let frames_processed = mem.process_all_enrichment();
    let elapsed = start.elapsed();

    // Commit changes
    mem.commit()?;

    // Get final stats
    let final_stats = mem.enrichment_stats();
    let queue_after = mem.enrichment_queue_len();

    if args.json {
        let result = ProcessQueueResult {
            queue_before,
            frames_processed,
            queue_after,
            total_frames: final_stats.total_frames,
            enriched_frames: final_stats.enriched_frames,
            searchable_only: final_stats.searchable_only,
        };
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        println!("Enrichment complete:");
        println!("  Frames processed: {}", frames_processed);
        println!("  Time: {:.2}s", elapsed.as_secs_f64());
        println!(
            "  Throughput: {:.1} frames/sec",
            frames_processed as f64 / elapsed.as_secs_f64().max(0.001)
        );
        println!();
        println!("Status:");
        println!("  Total frames: {}", final_stats.total_frames);
        println!("  Enriched: {}", final_stats.enriched_frames);
        println!("  Searchable only: {}", final_stats.searchable_only);
        println!("  Queue remaining: {}", queue_after);
    }

    Ok(())
}