rust-memex 0.6.5

Operator CLI + MCP server: canonical corpus second: semantic index second to aicx
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
use anyhow::Result;
use serde::Serialize;
use std::sync::Arc;
use tokio::sync::Mutex;

pub use rust_memex::contracts::stats::{DatabaseStats, NamespaceStats, StorageMetrics};
pub use rust_memex::contracts::timeline::{TimeRange, TimelineEntry, TimelineFilter};
use rust_memex::{
    BM25Config, BM25Index, EmbeddingClient, EmbeddingConfig, HealthChecker, HybridConfig,
    HybridSearcher, RAGPipeline, SearchMode, SearchOptions, SliceLayer, StorageManager,
    diagnostics::{
        self, TimelineBucket, TimelineQuery, namespace_stats as collect_namespace_stats,
    },
    inspect_cross_store_recovery,
};

fn bm25_path_from_db(db_path: &str) -> String {
    let expanded = shellexpand::tilde(db_path).to_string();
    std::path::Path::new(&expanded)
        .parent()
        .map(|parent| parent.join("bm25").to_string_lossy().to_string())
        .unwrap_or_else(|| BM25Config::default().index_path)
}

/// Run overview command - quick stats and health check
pub async fn run_overview(
    namespace: Option<String>,
    json_output: bool,
    db_path: String,
) -> Result<()> {
    let storage = StorageManager::new_lance_only(&db_path).await?;
    let storage = Arc::new(storage);

    // Use a zero embedding to get all documents
    let all_docs = storage.all_documents(namespace.as_deref(), 100000).await?;

    if all_docs.is_empty() {
        if json_output {
            println!(
                "{}",
                serde_json::to_string_pretty(&serde_json::json!({
                    "status": "empty",
                    "message": "No documents found",
                    "namespace": namespace,
                    "db_path": db_path
                }))?
            );
        } else {
            eprintln!("\n-> Overview for {}\n", storage.lance_path());
            if let Some(ns) = &namespace {
                eprintln!("No documents found in namespace '{}'", ns);
            } else {
                eprintln!("Database is empty. Use 'rust-memex index' to add documents.");
            }
        }
        return Ok(());
    }

    let stats_list = collect_namespace_stats(storage.as_ref(), namespace.as_deref()).await?;

    if json_output {
        let json = serde_json::json!({
            "db_path": db_path,
            "total_chunks": all_docs.len(),
            "namespace_count": stats_list.len(),
            "namespaces": stats_list
        });
        println!("{}", serde_json::to_string_pretty(&json)?);
    } else {
        eprintln!("\n=== RMCP-MEMEX OVERVIEW ===\n");
        eprintln!("Database: {}", db_path);
        eprintln!("Total chunks: {}", all_docs.len());
        eprintln!("Namespaces: {}\n", stats_list.len());

        for stats in &stats_list {
            eprintln!("--- {} ---", stats.name);
            eprintln!("  Chunks: {}", stats.total_chunks);

            // Layer breakdown
            if !stats.layer_counts.is_empty() {
                let layer_str: Vec<String> = stats
                    .layer_counts
                    .iter()
                    .map(|(k, v)| format!("{}:{}", k, v))
                    .collect();
                eprintln!("  Layers: {}", layer_str.join(", "));
            }

            // Top keywords
            if !stats.top_keywords.is_empty() {
                let kw_str: Vec<String> = stats
                    .top_keywords
                    .iter()
                    .take(5)
                    .map(|(k, v)| format!("{}({})", k, v))
                    .collect();
                eprintln!("  Top topics: {}", kw_str.join(", "));
            }

            // Date range
            if let (Some(earliest), Some(latest)) = (&stats.earliest_indexed, &stats.latest_indexed)
            {
                if earliest != latest {
                    eprintln!("  Date range: {} -> {}", earliest, latest);
                } else {
                    eprintln!("  Date: {}", earliest);
                }
            }

            // Timestamps warning
            if !stats.has_timestamps {
                eprintln!("  [!] No timestamp metadata found");
            }

            eprintln!();
        }

        eprintln!("Tip: Use 'rust-memex search -n <namespace> -q <query>' to search");
        eprintln!("     Use 'rust-memex dive -n <namespace> -q <query>' for deep exploration");
    }

    Ok(())
}

/// Health check result for JSON output
#[derive(Debug, Clone, Serialize)]
pub struct HealthReport {
    pub database: DatabaseHealth,
    pub embedder: Option<EmbedderHealth>,
    pub namespaces: Vec<NamespaceHealth>,
    pub cross_store: Option<CrossStoreHealth>,
    pub recommendations: Vec<String>,
    pub overall_status: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct DatabaseHealth {
    pub path: String,
    pub status: String,
    pub row_count: usize,
    pub version_count: usize,
    pub size_estimate_mb: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct EmbedderHealth {
    pub provider: Option<String>,
    pub status: String,
    pub dimension: Option<usize>,
    pub dimension_match: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct NamespaceHealth {
    pub name: String,
    pub chunk_count: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct CrossStoreHealth {
    pub recovery_dir: String,
    pub status: String,
    pub pending_batches: usize,
    pub divergent_batches: usize,
    pub rolled_back_batches: usize,
    pub stale_batches: usize,
    pub missing_bm25_documents: usize,
    pub missing_lance_documents: usize,
}

/// Run health check command
pub async fn run_health(
    db_path: String,
    embedding_config: &EmbeddingConfig,
    config_path_display: Option<String>,
    quick: bool,
    json_output: bool,
) -> Result<()> {
    let mut recommendations = Vec::new();
    let mut overall_ok = true;

    // 1. Database health
    let db_health = match StorageManager::new_lance_only(&db_path).await {
        Ok(storage) => {
            let stats = storage.stats().await.unwrap_or(rust_memex::TableStats {
                row_count: 0,
                version_count: 0,
                table_name: "memories".to_string(),
                db_path: db_path.clone(),
            });

            // Estimate size: ~4KB per row (embedding + metadata)
            let size_mb = (stats.row_count as f64 * 4.0) / 1024.0;

            // Check version count threshold
            if stats.version_count > 50 {
                recommendations.push(format!(
                    "Run 'rust-memex optimize' ({} versions accumulated)",
                    stats.version_count
                ));
            }

            DatabaseHealth {
                path: db_path.clone(),
                status: "OK".to_string(),
                row_count: stats.row_count,
                version_count: stats.version_count,
                size_estimate_mb: size_mb,
            }
        }
        Err(e) => {
            overall_ok = false;
            recommendations.push(format!("Database error: {}", e));
            DatabaseHealth {
                path: db_path.clone(),
                status: format!("ERROR: {}", e),
                row_count: 0,
                version_count: 0,
                size_estimate_mb: 0.0,
            }
        }
    };

    // 2. Embedder health (skip if --quick)
    let embedder_health = if quick {
        None
    } else {
        let checker = HealthChecker::new();
        let result = checker.run_all(embedding_config, &db_path).await;

        let provider = result.connected_provider.clone();
        let dimension = result.verified_dimension;
        let dim_ok = dimension
            .map(|d| d == embedding_config.required_dimension)
            .unwrap_or(false);

        let status = if result.all_passed() {
            "OK".to_string()
        } else {
            overall_ok = false;
            let failures: Vec<_> = result
                .items
                .iter()
                .filter(|i| i.status.is_fail())
                .map(|i| i.name.clone())
                .collect();
            if provider.is_none() {
                recommendations.push(
                    "Embedder unreachable - check if embedding server is running".to_string(),
                );
            }
            format!("FAILED: {}", failures.join(", "))
        };

        Some(EmbedderHealth {
            provider,
            status,
            dimension,
            dimension_match: dim_ok,
        })
    };

    // 3. Namespace stats
    let namespaces = if let Ok(storage) = StorageManager::new_lance_only(&db_path).await {
        storage
            .list_namespaces()
            .await
            .unwrap_or_default()
            .into_iter()
            .map(|(name, count)| NamespaceHealth {
                name,
                chunk_count: count,
            })
            .collect()
    } else {
        vec![]
    };

    let cross_store = match StorageManager::new_lance_only(&db_path).await {
        Ok(storage) => match BM25Index::new(&BM25Config::default().with_read_only(true)) {
            Ok(bm25) => {
                let recovery = inspect_cross_store_recovery(&storage, &bm25, None).await?;
                let status = if recovery.pending_batches == 0 {
                    "OK".to_string()
                } else if recovery.divergent_batches > 0 {
                    overall_ok = false;
                    recommendations.push(
                        "Run 'rust-memex repair-writes --execute' to reconcile Lance/BM25 divergence"
                            .to_string(),
                    );
                    "DIVERGENT".to_string()
                } else if recovery.rolled_back_batches > 0 || recovery.stale_batches > 0 {
                    overall_ok = false;
                    recommendations.push(
                        "Run 'rust-memex repair-writes --execute' to clear rolled-back/stale recovery ledgers"
                            .to_string(),
                    );
                    "RECOVERY_PENDING".to_string()
                } else {
                    recommendations.push(
                        "Run 'rust-memex repair-writes --execute' to clear completed recovery ledgers"
                            .to_string(),
                    );
                    "LEDGER_PENDING".to_string()
                };

                Some(CrossStoreHealth {
                    recovery_dir: recovery.recovery_dir,
                    status,
                    pending_batches: recovery.pending_batches,
                    divergent_batches: recovery.divergent_batches,
                    rolled_back_batches: recovery.rolled_back_batches,
                    stale_batches: recovery.stale_batches,
                    missing_bm25_documents: recovery.documents_missing_bm25,
                    missing_lance_documents: recovery.documents_missing_lance,
                })
            }
            Err(error) => {
                overall_ok = false;
                recommendations.push(format!("BM25 recovery inspection unavailable: {}", error));
                Some(CrossStoreHealth {
                    recovery_dir: storage.cross_store_recovery_dir().display().to_string(),
                    status: format!("ERROR: {error}"),
                    pending_batches: 0,
                    divergent_batches: 0,
                    rolled_back_batches: 0,
                    stale_batches: 0,
                    missing_bm25_documents: 0,
                    missing_lance_documents: 0,
                })
            }
        },
        Err(_) => None,
    };

    // Build report
    let overall_status = if overall_ok && recommendations.is_empty() {
        "HEALTHY".to_string()
    } else if overall_ok {
        "OK (with recommendations)".to_string()
    } else {
        "UNHEALTHY".to_string()
    };

    let report = HealthReport {
        database: db_health,
        embedder: embedder_health,
        namespaces,
        cross_store,
        recommendations: recommendations.clone(),
        overall_status: overall_status.clone(),
    };

    // Output
    if json_output {
        println!("{}", serde_json::to_string_pretty(&report)?);
    } else {
        // Config info
        if let Some(ref path) = config_path_display {
            eprintln!("Config: {}", path);
        }
        eprintln!();

        // Database section
        eprintln!("Database: {}", report.database.path);
        eprintln!("  Status:   {}", report.database.status);
        eprintln!("  Rows:     {}", report.database.row_count);
        eprintln!("  Versions: {}", report.database.version_count);
        eprintln!(
            "  Size:     ~{:.1} MB (estimate)",
            report.database.size_estimate_mb
        );
        eprintln!();

        // Embedder section
        if let Some(ref emb) = report.embedder {
            eprintln!("Embedder:");
            eprintln!("  Status:    {}", emb.status);
            if let Some(ref provider) = emb.provider {
                eprintln!("  Provider:  {}", provider);
            }
            if let Some(dim) = emb.dimension {
                let check = if emb.dimension_match {
                    "[OK]"
                } else {
                    "[MISMATCH]"
                };
                eprintln!("  Dimension: {} {}", dim, check);
            }
            eprintln!();
        } else if quick {
            eprintln!("Embedder: (skipped with --quick)");
            eprintln!();
        }

        // Namespaces section
        if !report.namespaces.is_empty() {
            eprintln!("Namespaces:");
            for ns in &report.namespaces {
                eprintln!("  {}: {} chunks", ns.name, ns.chunk_count);
            }
            eprintln!();
        }

        if let Some(ref cross_store) = report.cross_store {
            eprintln!("Cross-store recovery:");
            eprintln!("  Status:           {}", cross_store.status);
            eprintln!("  Recovery dir:     {}", cross_store.recovery_dir);
            eprintln!("  Pending batches:  {}", cross_store.pending_batches);
            eprintln!("  Divergent batches: {}", cross_store.divergent_batches);
            eprintln!("  Rolled back:      {}", cross_store.rolled_back_batches);
            eprintln!("  Stale ledgers:    {}", cross_store.stale_batches);
            eprintln!(
                "  Missing BM25 docs: {}",
                cross_store.missing_bm25_documents
            );
            eprintln!(
                "  Missing Lance docs: {}",
                cross_store.missing_lance_documents
            );
            eprintln!();
        }

        // Recommendations
        if report.recommendations.is_empty() {
            eprintln!("Status: {} - No action needed", overall_status);
        } else {
            eprintln!("Recommendations:");
            for rec in &report.recommendations {
                eprintln!("  - {}", rec);
            }
            eprintln!();
            eprintln!("Status: {}", overall_status);
        }
    }

    Ok(())
}

/// Recall result for JSON output
#[derive(Debug, Clone, Serialize)]
pub struct RecallReport {
    pub query: String,
    pub summary: String,
    pub sources: Vec<RecallSource>,
    pub related: Vec<RecallRelated>,
    pub total_chunks: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct RecallSource {
    pub namespace: String,
    pub source: Option<String>,
    pub date: Option<String>,
    pub preview: String,
    pub score: f32,
}

#[derive(Debug, Clone, Serialize)]
pub struct RecallRelated {
    pub title: String,
    pub date: Option<String>,
    pub namespace: String,
}

/// Run recall command - synthesized search results
pub async fn run_recall(
    query: String,
    namespace_filter: Option<String>,
    limit: usize,
    json_output: bool,
    db_path: String,
    embedding_config: &EmbeddingConfig,
) -> Result<()> {
    use std::collections::HashMap;

    let storage = Arc::new(StorageManager::new_lance_only(&db_path).await?);
    storage.ensure_collection().await?;

    let embedding_client = Arc::new(Mutex::new(EmbeddingClient::new(embedding_config).await?));
    let hybrid = HybridSearcher::new(
        storage.clone(),
        HybridConfig {
            mode: SearchMode::Hybrid,
            bm25: BM25Config {
                index_path: bm25_path_from_db(&db_path),
                read_only: true,
                ..Default::default()
            },
            ..Default::default()
        },
    )
    .await?;
    let query_embedding = embedding_client.lock().await.embed(&query).await?;

    // Get namespaces to search
    let namespaces: Vec<String> = if let Some(ref ns) = namespace_filter {
        vec![ns.clone()]
    } else {
        storage
            .list_namespaces()
            .await?
            .into_iter()
            .map(|(name, _)| name)
            .collect()
    };

    if namespaces.is_empty() {
        if json_output {
            println!(
                "{}",
                serde_json::json!({
                    "error": "No namespaces found",
                    "query": query,
                })
            );
        } else {
            eprintln!("No namespaces found in database");
        }
        return Ok(());
    }

    // Recall is a human-facing truth surface, not a token-minimization primitive.
    // Search all onion layers and let hybrid lexical/vector ranking decide what is
    // actually responsive to the query; outer-only recall often returns metadata
    // stubs while the useful content lives in middle/core slices.
    let mut all_results: Vec<(String, rust_memex::SearchResult)> = Vec::new();

    for ns in &namespaces {
        let results = hybrid
            .search(
                &query,
                query_embedding.clone(),
                Some(ns),
                limit,
                SearchOptions::deep(),
            )
            .await?;

        for r in results {
            all_results.push((
                ns.clone(),
                rust_memex::SearchResult {
                    id: r.id,
                    namespace: r.namespace,
                    text: r.document,
                    score: r.combined_score,
                    metadata: r.metadata,
                    layer: r.layer,
                    parent_id: r.parent_id,
                    children_ids: r.children_ids,
                    keywords: r.keywords,
                },
            ));
        }
    }

    // Sort by score (best first)
    all_results.sort_by(|a, b| {
        b.1.score
            .partial_cmp(&a.1.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Take top results
    let top_results: Vec<_> = all_results.into_iter().take(limit).collect();

    if top_results.is_empty() {
        if json_output {
            println!(
                "{}",
                serde_json::json!({
                    "query": query,
                    "summary": "No relevant memories found",
                    "sources": [],
                    "related": [],
                    "total_chunks": 0,
                })
            );
        } else {
            eprintln!("No relevant memories found for: \"{}\"", query);
        }
        return Ok(());
    }

    // Build summary from top outer slices
    let mut summary_parts: Vec<String> = Vec::new();
    let mut sources: Vec<RecallSource> = Vec::new();
    let mut seen_sources: HashMap<String, bool> = HashMap::new();

    for (ns, result) in &top_results {
        // Extract source file
        let source = result
            .metadata
            .get("source")
            .and_then(|v| v.as_str())
            .or_else(|| result.metadata.get("file_path").and_then(|v| v.as_str()))
            .map(|s| {
                std::path::Path::new(s)
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or(s)
                    .to_string()
            });

        let date = result
            .metadata
            .get("indexed_at")
            .and_then(|v| v.as_str())
            .or_else(|| result.metadata.get("timestamp").and_then(|v| v.as_str()))
            .map(|s| s.split('T').next().unwrap_or(s).to_string());

        // Add to summary (first few, avoiding duplicates)
        let source_key = format!("{}-{:?}", ns, source);
        if !seen_sources.contains_key(&source_key) && summary_parts.len() < 5 {
            // Use the text directly as it's already a summary (outer layer)
            let text = result.text.trim();
            if !text.is_empty() && text.len() > 20 {
                summary_parts.push(text.to_string());
            }
            seen_sources.insert(source_key, true);
        }

        // Build preview
        let preview: String = result.text.chars().take(150).collect();
        let preview = if result.text.len() > 150 {
            format!("{}...", preview.trim())
        } else {
            preview.trim().to_string()
        };

        sources.push(RecallSource {
            namespace: ns.clone(),
            source,
            date,
            preview,
            score: result.score,
        });
    }

    // Build related items (unique sources/dates)
    let mut related: Vec<RecallRelated> = Vec::new();
    let mut seen_related: HashMap<String, bool> = HashMap::new();

    for source in &sources {
        let key = format!("{:?}-{:?}", source.source, source.date);
        if let std::collections::hash_map::Entry::Vacant(e) = seen_related.entry(key) {
            related.push(RecallRelated {
                title: source
                    .source
                    .clone()
                    .unwrap_or_else(|| "Unknown".to_string()),
                date: source.date.clone(),
                namespace: source.namespace.clone(),
            });
            e.insert(true);
        }
    }

    // Combine summary parts
    let summary = if summary_parts.is_empty() {
        "Found relevant memories but no clear summary available.".to_string()
    } else {
        summary_parts.join("\n\n")
    };

    let report = RecallReport {
        query: query.clone(),
        summary,
        sources,
        related,
        total_chunks: top_results.len(),
    };

    // Output
    if json_output {
        println!("{}", serde_json::to_string_pretty(&report)?);
    } else {
        eprintln!("Recall: \"{}\"", query);
        eprintln!();

        eprintln!("Based on {} relevant memories:", report.total_chunks);
        eprintln!();

        // Print summary
        for part in report.summary.lines().take(20) {
            eprintln!("  {}", part);
        }
        eprintln!();

        // Print related discussions
        if !report.related.is_empty() {
            eprintln!("Related discussions:");
            for r in report.related.iter().take(5) {
                let date_str = r.date.as_deref().unwrap_or("unknown date");
                eprintln!("  - \"{}\" ({}) [{}]", r.title, date_str, r.namespace);
            }
            if report.related.len() > 5 {
                eprintln!("  ... and {} more", report.related.len() - 5);
            }
            eprintln!();
        }

        // Namespace breakdown
        let mut ns_counts: HashMap<String, usize> = HashMap::new();
        for s in &report.sources {
            *ns_counts.entry(s.namespace.clone()).or_default() += 1;
        }
        let ns_summary: Vec<_> = ns_counts
            .iter()
            .map(|(k, v)| format!("{} from {}", v, k))
            .collect();
        eprintln!("Sources: {}", ns_summary.join(", "));
    }

    Ok(())
}

/// Run timeline command - show when content was indexed
pub async fn run_timeline(
    db_path: String,
    namespace_filter: Option<String>,
    since: Option<String>,
    show_gaps_only: bool,
    json_output: bool,
) -> Result<()> {
    let storage = StorageManager::new_lance_only(&db_path).await?;
    let report = diagnostics::timeline_report(
        &storage,
        &TimelineQuery {
            namespace: namespace_filter.clone(),
            since,
            until: None,
            bucket: TimelineBucket::Day,
        },
    )
    .await?;

    if report.namespaces.is_empty() {
        if json_output {
            println!(
                "{}",
                serde_json::json!({
                    "error": "No namespaces found",
                    "namespaces": [],
                    "entries": [],
                })
            );
        } else {
            eprintln!("No namespaces found in database");
        }
        return Ok(());
    }

    // Output
    if json_output {
        println!("{}", serde_json::to_string_pretty(&report)?);
    } else if show_gaps_only {
        // Only show gaps
        if report.gaps.is_empty() {
            eprintln!("No gaps found in timeline");
        } else {
            eprintln!("Timeline Gaps:");
            for gap in &report.gaps {
                eprintln!("  - {}", gap);
            }
        }
    } else {
        // Full timeline grouped by month
        eprintln!("Timeline: {} namespace(s)", report.namespaces.len());
        if let Some(ref ns) = namespace_filter {
            eprintln!("  Namespace: {}", ns);
        }
        eprintln!();

        // Group by year-month
        let mut by_month: std::collections::BTreeMap<String, Vec<(String, String, usize)>> =
            std::collections::BTreeMap::new();
        for entry in &report.entries {
            let month = if entry.date.len() >= 7 {
                entry.date[..7].to_string()
            } else {
                entry.date.clone()
            };

            by_month.entry(month).or_default().push((
                entry.date.clone(),
                format!(
                    "[{}] {} ({})",
                    entry.namespace,
                    entry
                        .source
                        .clone()
                        .unwrap_or_else(|| "unknown".to_string()),
                    entry.chunk_count
                ),
                entry.chunk_count,
            ));
        }

        for (month, entries) in by_month {
            eprintln!("{}:", month);
            for (date, desc, _) in entries.iter().take(10) {
                eprintln!("  {}: {}", date, desc);
            }
            if entries.len() > 10 {
                eprintln!("  ... and {} more entries", entries.len() - 10);
            }
            eprintln!();
        }

        // Coverage summary
        eprintln!("Coverage:");
        if let (Some(e), Some(l)) = (&report.coverage.earliest, &report.coverage.latest) {
            eprintln!("  Period: {} to {}", e, l);
        }
        eprintln!(
            "  Days with data: {} / {} total",
            report.coverage.days_with_data, report.coverage.total_days
        );

        if !report.gaps.is_empty() {
            eprintln!();
            eprintln!("Gaps ({}):", report.gaps.len());
            for gap in report.gaps.iter().take(5) {
                eprintln!("  - {}", gap);
            }
            if report.gaps.len() > 5 {
                eprintln!("  ... and {} more gaps", report.gaps.len() - 5);
            }
        }
    }

    Ok(())
}

/// Run dive command - deep exploration with all onion layers
pub async fn run_dive(
    namespace: String,
    query: String,
    limit: usize,
    verbose: bool,
    json_output: bool,
    db_path: String,
    embedding_config: &EmbeddingConfig,
) -> Result<()> {
    let embedding_client = Arc::new(Mutex::new(EmbeddingClient::new(embedding_config).await?));
    let storage = Arc::new(StorageManager::new_lance_only(&db_path).await?);
    let rag = RAGPipeline::new(embedding_client, storage).await?;

    // Search each layer separately
    let layers = [
        (Some(SliceLayer::Outer), "OUTER"),
        (Some(SliceLayer::Middle), "MIDDLE"),
        (Some(SliceLayer::Inner), "INNER"),
        (Some(SliceLayer::Core), "CORE"),
    ];

    let mut all_results: Vec<serde_json::Value> = Vec::new();

    if !json_output {
        eprintln!("\n=== DEEP DIVE: \"{}\" in [{}] ===\n", query, namespace);
    }

    for (layer_filter, layer_name) in &layers {
        let results = rag
            .memory_search_with_layer(&namespace, &query, limit, *layer_filter)
            .await?;

        if json_output {
            let layer_results: Vec<serde_json::Value> = results
                .iter()
                .map(|r| {
                    let mut obj = serde_json::json!({
                        "id": r.id,
                        "score": r.score,
                        "keywords": r.keywords,
                        "layer": r.layer.map(|l| l.name()),
                        "can_expand": r.can_expand(),
                        "parent_id": r.parent_id,
                    });
                    if verbose {
                        obj["text"] = serde_json::json!(r.text);
                        obj["metadata"] = r.metadata.clone();
                        obj["children_ids"] = serde_json::json!(r.children_ids);
                    } else {
                        // Truncated preview
                        let preview: String = r.text.chars().take(200).collect();
                        obj["preview"] = serde_json::json!(preview);
                    }
                    obj
                })
                .collect();

            all_results.push(serde_json::json!({
                "layer": layer_name,
                "count": results.len(),
                "results": layer_results
            }));
        } else {
            eprintln!("--- {} LAYER ({} results) ---", layer_name, results.len());

            if results.is_empty() {
                eprintln!("  (no results)\n");
                continue;
            }

            for (i, result) in results.iter().enumerate() {
                eprintln!("  {}. [score: {:.3}] {}", i + 1, result.score, result.id);

                // Keywords
                if !result.keywords.is_empty() {
                    eprintln!("     Keywords: {}", result.keywords.join(", "));
                }

                // Text preview or full text
                if verbose {
                    eprintln!("     ---");
                    // Indent each line of text
                    for line in result.text.lines().take(20) {
                        eprintln!("     {}", line);
                    }
                    if result.text.lines().count() > 20 {
                        eprintln!("     ... ({} more lines)", result.text.lines().count() - 20);
                    }
                    eprintln!("     ---");

                    // Full metadata
                    if !result.metadata.is_null() && result.metadata != serde_json::json!({}) {
                        eprintln!("     Metadata: {}", result.metadata);
                    }
                } else {
                    // Short preview
                    let preview: String = result
                        .text
                        .chars()
                        .take(100)
                        .collect::<String>()
                        .replace('\n', " ");
                    let ellipsis = if result.text.len() > 100 { "..." } else { "" };
                    eprintln!("     \"{}{}\"", preview, ellipsis);
                }

                // Hierarchy info
                if result.can_expand() {
                    eprintln!("     [expandable: {} children]", result.children_ids.len());
                }
                if result.parent_id.is_some() {
                    eprintln!("     [has parent: can drill up]");
                }

                eprintln!();
            }
        }
    }

    if json_output {
        let output = serde_json::json!({
            "query": query,
            "namespace": namespace,
            "limit_per_layer": limit,
            "verbose": verbose,
            "layers": all_results
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        eprintln!("=== END DIVE ===\n");
        eprintln!(
            "Tip: Use 'rust-memex expand -n {} -i <id>' to expand a result",
            namespace
        );
    }

    Ok(())
}

/// Run garbage collection
pub async fn run_gc(
    config: rust_memex::GcConfig,
    db_path: String,
    json_output: bool,
) -> Result<()> {
    let storage = StorageManager::new_lance_only(&db_path).await?;

    let mode_str = if config.dry_run { "DRY RUN" } else { "EXECUTE" };
    let ns_str = config.namespace.as_deref().unwrap_or("all namespaces");

    if !json_output {
        eprintln!("\n=== GARBAGE COLLECTION ({}) ===\n", mode_str);
        eprintln!("Database: {}", db_path);
        eprintln!("Scope: {}", ns_str);
        eprintln!();

        if config.remove_orphans {
            eprintln!("- Checking for orphan embeddings...");
        }
        if config.remove_empty {
            eprintln!("- Checking for empty namespaces...");
        }
        if let Some(ref dur) = config.older_than {
            let days = dur.num_days();
            eprintln!("- Checking for documents older than {} days...", days);
        }
        eprintln!();
    }

    // Run GC
    let stats = storage.garbage_collect(&config).await?;

    if json_output {
        let output = serde_json::json!({
            "mode": if config.dry_run { "dry_run" } else { "execute" },
            "db_path": db_path,
            "namespace": config.namespace,
            "orphans": {
                "found": stats.orphans_found,
                "removed": stats.orphans_removed
            },
            "empty_namespaces": {
                "found": stats.empty_namespaces_found,
                "removed": stats.empty_namespaces_removed,
                "names": stats.empty_namespace_names
            },
            "old_documents": {
                "found": stats.old_docs_found,
                "removed": stats.old_docs_removed,
                "affected_namespaces": stats.affected_namespaces
            },
            "bytes_freed": stats.bytes_freed,
            "has_issues": stats.has_issues(),
            "has_deletions": stats.has_deletions()
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        // Human-readable output
        eprintln!("=== RESULTS ===\n");

        // Orphans
        if config.remove_orphans {
            if stats.orphans_found > 0 {
                eprintln!("Orphan embeddings:");
                eprintln!("  Found:   {}", stats.orphans_found);
                if config.dry_run {
                    eprintln!("  Action:  Would remove {} orphans", stats.orphans_found);
                } else {
                    eprintln!("  Removed: {}", stats.orphans_removed);
                }
            } else {
                eprintln!("Orphan embeddings: None found");
            }
            eprintln!();
        }

        // Empty namespaces
        if config.remove_empty {
            if stats.empty_namespaces_found > 0 {
                eprintln!("Empty namespaces:");
                eprintln!("  Found: {}", stats.empty_namespaces_found);
                for ns in &stats.empty_namespace_names {
                    eprintln!("    - {}", ns);
                }
            } else {
                eprintln!("Empty namespaces: None found");
            }
            eprintln!();
        }

        // Old documents
        if config.older_than.is_some() {
            if stats.old_docs_found > 0 {
                eprintln!("Old documents:");
                eprintln!("  Found:   {}", stats.old_docs_found);
                if config.dry_run {
                    eprintln!("  Action:  Would remove {} documents", stats.old_docs_found);
                } else {
                    eprintln!("  Removed: {}", stats.old_docs_removed);
                }
                if !stats.affected_namespaces.is_empty() {
                    eprintln!("  Affected namespaces:");
                    for ns in &stats.affected_namespaces {
                        eprintln!("    - {}", ns);
                    }
                }
            } else {
                eprintln!("Old documents: None found (no documents with parseable timestamps)");
            }
            eprintln!();
        }

        // Summary
        eprintln!("=== SUMMARY ===\n");
        if !stats.has_issues() {
            eprintln!("No issues found. Database is clean.");
        } else if config.dry_run {
            eprintln!("Issues found. Run with --execute to apply changes.");
            eprintln!();
            eprintln!("Example:");
            let mut cmd = "rust-memex gc".to_string();
            if config.remove_orphans {
                cmd.push_str(" --remove-orphans");
            }
            if config.remove_empty {
                cmd.push_str(" --remove-empty");
            }
            if let Some(ref dur) = config.older_than {
                cmd.push_str(&format!(" --older-than {}d", dur.num_days()));
            }
            cmd.push_str(" --execute");
            eprintln!("  {}", cmd);
        } else if stats.has_deletions() {
            eprintln!("Cleanup complete!");
            let total_removed = stats.orphans_removed + stats.old_docs_removed;
            eprintln!("  Total items removed: {}", total_removed);
            if let Some(bytes) = stats.bytes_freed {
                eprintln!("  Space freed: {} bytes", bytes);
            }
            eprintln!();
            eprintln!("Tip: Run 'rust-memex optimize' to compact the database and reclaim space.");
        }
    }

    Ok(())
}