brainwires-cognition 0.8.0

Unified intelligence layer — knowledge graphs, adaptive prompting, RAG, spectral math, and code analysis for the Brainwires Agent Framework
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
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
use super::RagClient;
use crate::rag::embedding::EmbeddingProvider;
use crate::rag::indexer::{CodeChunk, FileWalker};
use crate::rag::types::{ChunkMetadata, IndexResponse};
use anyhow::{Context, Result};
use rayon::prelude::*;
use rmcp::{Peer, RoleServer, model::ProgressNotificationParam, model::ProgressToken};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Instant;
use tokio_util::sync::CancellationToken;

/// Helper macro to check for cancellation and return early if cancelled
macro_rules! check_cancelled {
    ($cancel_token:expr) => {
        if $cancel_token.is_cancelled() {
            tracing::info!("Indexing operation cancelled");
            anyhow::bail!("Indexing was cancelled");
        }
    };
}

/// Result of embedding generation with cancellation support
struct EmbeddingResult {
    embeddings: Vec<Vec<f32>>,
    successful_chunks: Vec<CodeChunk>,
    errors: Vec<String>,
}

/// Generate embeddings for chunks with frequent cancellation checks
///
/// This function processes chunks in small batches and checks for cancellation
/// between each batch, allowing for faster response to cancellation requests.
async fn generate_embeddings_with_cancellation(
    client: &RagClient,
    chunks: &[CodeChunk],
    cancel_token: &CancellationToken,
    peer: &Option<Peer<RoleServer>>,
    progress_token: &Option<ProgressToken>,
    progress_start: f64,
    progress_end: f64,
) -> Result<EmbeddingResult> {
    let batch_size = client.config.embedding.batch_size;
    let timeout_secs = client.config.embedding.timeout_secs;
    let check_interval = if client.config.embedding.cancellation_check_interval > 0 {
        client.config.embedding.cancellation_check_interval
    } else {
        batch_size // Fall back to batch size if interval is 0
    };

    let mut all_embeddings = Vec::with_capacity(chunks.len());
    let mut successful_chunks = Vec::with_capacity(chunks.len());
    let mut errors = Vec::new();

    let total_batches = chunks.len().div_ceil(batch_size);
    let mut chunks_processed = 0;

    for (batch_idx, chunk_batch) in chunks.chunks(batch_size).enumerate() {
        // Check for cancellation at start of each batch
        if cancel_token.is_cancelled() {
            tracing::info!(
                "Embedding generation cancelled after {} chunks",
                chunks_processed
            );
            anyhow::bail!("Indexing was cancelled");
        }

        // Process batch in smaller sub-batches for more frequent cancellation checks
        let mut batch_embeddings = Vec::new();
        let mut batch_successful_chunks = Vec::new();

        for sub_batch in chunk_batch.chunks(check_interval) {
            // Check cancellation before each sub-batch
            if cancel_token.is_cancelled() {
                tracing::info!(
                    "Embedding generation cancelled during batch {} after {} chunks",
                    batch_idx,
                    chunks_processed
                );
                anyhow::bail!("Indexing was cancelled");
            }

            let texts: Vec<String> = sub_batch.iter().map(|c| c.content.clone()).collect();

            // Generate embeddings with timeout protection
            let provider = client.embedding_provider.clone();
            let embed_future = tokio::task::spawn_blocking(move || provider.embed_batch(texts));

            match tokio::time::timeout(std::time::Duration::from_secs(timeout_secs), embed_future)
                .await
            {
                Ok(Ok(Ok(embeddings))) => {
                    batch_embeddings.extend(embeddings);
                    batch_successful_chunks.extend(sub_batch.iter().cloned());
                    chunks_processed += sub_batch.len();
                }
                Ok(Ok(Err(e))) => {
                    errors.push(format!(
                        "Failed to generate embeddings for sub-batch: {}",
                        e
                    ));
                    // Continue with next sub-batch
                }
                Ok(Err(e)) => {
                    errors.push(format!("Embedding task panicked: {}", e));
                    // Continue with next sub-batch
                }
                Err(_) => {
                    errors.push(format!(
                        "Embedding generation timed out after {} seconds",
                        timeout_secs
                    ));
                    // Continue with next sub-batch
                }
            }
        }

        // Add batch results to overall results
        all_embeddings.extend(batch_embeddings);
        successful_chunks.extend(batch_successful_chunks);

        // Send progress during embedding
        if let (Some(peer), Some(token)) = (peer, progress_token) {
            let progress = progress_start
                + ((batch_idx + 1) as f64 / total_batches as f64) * (progress_end - progress_start);
            let _ = peer
                .notify_progress(ProgressNotificationParam {
                    progress_token: token.clone(),
                    progress,
                    total: Some(100.0),
                    message: Some(format!(
                        "Generating embeddings... {}/{} batches ({} chunks)",
                        batch_idx + 1,
                        total_batches,
                        chunks_processed
                    )),
                })
                .await;
        }
    }

    Ok(EmbeddingResult {
        embeddings: all_embeddings,
        successful_chunks,
        errors,
    })
}

/// Index a complete codebase
#[allow(clippy::too_many_arguments)]
pub async fn do_index(
    client: &RagClient,
    path: String,
    project: Option<String>,
    include_patterns: Vec<String>,
    exclude_patterns: Vec<String>,
    max_file_size: usize,
    peer: Option<Peer<RoleServer>>,
    progress_token: Option<ProgressToken>,
    cancel_token: CancellationToken,
) -> Result<IndexResponse> {
    let start = Instant::now();
    let mut errors = Vec::new();

    // Send initial progress
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 0.0,
                total: Some(100.0),
                message: Some("Starting file walk...".into()),
            })
            .await;
    }

    // Walk the directory (on a blocking thread since it's CPU-intensive)
    // Create a cancellation flag for the blocking file walker
    let cancelled_flag = Arc::new(AtomicBool::new(false));
    let cancelled_flag_clone = cancelled_flag.clone();
    let cancel_token_clone = cancel_token.clone();

    // Spawn a task to set the flag when cancellation is requested
    let _cancel_watcher = tokio::spawn(async move {
        cancel_token_clone.cancelled().await;
        cancelled_flag_clone.store(true, Ordering::Relaxed);
        tracing::debug!("Cancellation flag set for file walker");
    });

    let walker = FileWalker::new(&path, max_file_size)
        .with_project(project.clone())
        .with_patterns(include_patterns.clone(), exclude_patterns.clone())
        .with_cancellation_flag(cancelled_flag);

    let files = tokio::task::spawn_blocking(move || walker.walk())
        .await
        .context("Failed to spawn file walker task")?
        .context("Failed to walk directory")?;
    let files_indexed = files.len();

    // Check for cancellation after file walk
    check_cancelled!(cancel_token);

    // Send progress after file walk
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 20.0,
                total: Some(100.0),
                message: Some(format!("Found {} files, chunking...", files_indexed)),
            })
            .await;
    }

    // Chunk all files in parallel for better performance
    let chunker = client.chunker.clone();
    let all_chunks: Vec<_> = files
        .par_iter()
        .flat_map(|file| chunker.chunk_file(file))
        .collect();

    let chunks_created = all_chunks.len();

    // Send progress after chunking
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 40.0,
                total: Some(100.0),
                message: Some(format!(
                    "Created {} chunks, generating embeddings...",
                    chunks_created
                )),
            })
            .await;
    }

    if all_chunks.is_empty() {
        return Ok(IndexResponse {
            mode: crate::rag::types::IndexingMode::Full,
            files_indexed: 0,
            chunks_created: 0,
            embeddings_generated: 0,
            duration_ms: start.elapsed().as_millis() as u64,
            errors: vec!["No code chunks found to index".to_string()],
            files_updated: 0,
            files_removed: 0,
        });
    }

    // Generate embeddings with frequent cancellation checks
    // Progress range: 40% to 80%
    let embed_result = generate_embeddings_with_cancellation(
        client,
        &all_chunks,
        &cancel_token,
        &peer,
        &progress_token,
        40.0,
        80.0,
    )
    .await?;

    let all_embeddings = embed_result.embeddings;
    let successful_chunks = embed_result.successful_chunks;
    errors.extend(embed_result.errors);

    let embeddings_generated = all_embeddings.len();

    // Send progress before storing
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 85.0,
                total: Some(100.0),
                message: Some(format!(
                    "Storing {} embeddings in database...",
                    embeddings_generated
                )),
            })
            .await;
    }

    // Store in vector database (pass normalized root path for per-project BM25)
    // Use successful_chunks to ensure metadata/contents match embeddings count
    let metadata: Vec<ChunkMetadata> = successful_chunks
        .iter()
        .map(|c| c.metadata.clone())
        .collect();
    let contents: Vec<String> = successful_chunks
        .iter()
        .map(|c| c.content.clone())
        .collect();

    // Sanity check: ensure all arrays have the same length to prevent RecordBatch errors
    debug_assert_eq!(
        all_embeddings.len(),
        metadata.len(),
        "Embeddings and metadata count mismatch"
    );
    debug_assert_eq!(
        all_embeddings.len(),
        contents.len(),
        "Embeddings and contents count mismatch"
    );

    // Check for cancellation before storing
    check_cancelled!(cancel_token);

    if !all_embeddings.is_empty() {
        client
            .vector_db
            .store_embeddings(all_embeddings, metadata, contents, &path)
            .await
            .context("Failed to store embeddings")?;
    }

    // Send progress before saving cache
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 95.0,
                total: Some(100.0),
                message: Some("Saving cache...".into()),
            })
            .await;
    }

    // Save file hashes to persistent cache
    let file_hashes: HashMap<String, String> = files
        .iter()
        .map(|f| (f.relative_path.clone(), f.hash.clone()))
        .collect();

    let mut cache = client.hash_cache.write().await;
    cache.update_root(path.clone(), file_hashes);

    // Persist to disk
    if let Err(e) = cache.save(&client.cache_path) {
        tracing::warn!("Failed to save hash cache: {}", e);
    }
    drop(cache);

    // Send progress before flush
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 98.0,
                total: Some(100.0),
                message: Some("Flushing index to disk...".into()),
            })
            .await;
    }

    // Flush the index to disk
    client
        .vector_db
        .flush()
        .await
        .map_err(|e| anyhow::anyhow!("Failed to flush index to disk: {}", e))?;

    // Clear dirty flag early — embeddings + cache are persisted, so the index
    // is consistent even if the process is killed after this point.
    {
        let mut cache = client.hash_cache.write().await;
        cache.clear_dirty(&path);
        if let Err(e) = cache.save(&client.cache_path) {
            tracing::warn!("Failed to clear dirty flag after full index: {}", e);
        }
        tracing::debug!("Cleared dirty flag early (full index) for: {}", path);
    }

    // Send final completion progress
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 100.0,
                total: Some(100.0),
                message: Some("Indexing complete!".into()),
            })
            .await;
    }

    Ok(IndexResponse {
        mode: crate::rag::types::IndexingMode::Full,
        files_indexed,
        chunks_created,
        embeddings_generated,
        duration_ms: start.elapsed().as_millis() as u64,
        errors,
        files_updated: 0,
        files_removed: 0,
    })
}

/// Perform incremental update (only changed files)
#[allow(clippy::too_many_arguments)]
pub async fn do_incremental_update(
    client: &RagClient,
    path: String,
    project: Option<String>,
    include_patterns: Vec<String>,
    exclude_patterns: Vec<String>,
    max_file_size: usize,
    peer: Option<Peer<RoleServer>>,
    progress_token: Option<ProgressToken>,
    cancel_token: CancellationToken,
) -> Result<IndexResponse> {
    let start = Instant::now();

    // Send initial progress
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 0.0,
                total: Some(100.0),
                message: Some("Checking for changes...".into()),
            })
            .await;
    }

    // Get existing file hashes from persistent cache
    let cache = client.hash_cache.read().await;
    let existing_hashes = cache.get_root(&path).cloned().unwrap_or_default();
    drop(cache);

    // Send progress after reading cache
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 10.0,
                total: Some(100.0),
                message: Some(format!(
                    "Found {} cached files, scanning directory...",
                    existing_hashes.len()
                )),
            })
            .await;
    }

    // Walk directory to find current files (on a blocking thread)
    // Create a cancellation flag for the blocking file walker
    let cancelled_flag = Arc::new(AtomicBool::new(false));
    let cancelled_flag_clone = cancelled_flag.clone();
    let cancel_token_clone = cancel_token.clone();

    // Spawn a task to set the flag when cancellation is requested
    let _cancel_watcher = tokio::spawn(async move {
        cancel_token_clone.cancelled().await;
        cancelled_flag_clone.store(true, Ordering::Relaxed);
        tracing::debug!("Cancellation flag set for file walker");
    });

    let walker = FileWalker::new(&path, max_file_size)
        .with_project(project.clone())
        .with_patterns(include_patterns.clone(), exclude_patterns.clone())
        .with_cancellation_flag(cancelled_flag);

    let current_files = tokio::task::spawn_blocking(move || walker.walk())
        .await
        .context("Failed to spawn file walker task")?
        .context("Failed to walk directory")?;

    // Check for cancellation after file walk
    check_cancelled!(cancel_token);

    let mut files_added = 0;
    let mut files_updated = 0;
    let mut files_removed = 0;
    let mut chunks_modified = 0;

    // Send progress after file walk
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 30.0,
                total: Some(100.0),
                message: Some(format!(
                    "Found {} files, comparing with cache...",
                    current_files.len()
                )),
            })
            .await;
    }

    // Find new and modified files
    let mut new_hashes = HashMap::with_capacity(current_files.len());
    let mut files_to_index = Vec::with_capacity(current_files.len());

    for file in current_files {
        new_hashes.insert(file.relative_path.clone(), file.hash.clone());

        match existing_hashes.get(&file.relative_path) {
            None => {
                // New file
                files_added += 1;
                files_to_index.push(file);
            }
            Some(old_hash) if old_hash != &file.hash => {
                // Modified file - delete old embeddings first
                if let Err(e) = client.vector_db.delete_by_file(&file.relative_path).await {
                    tracing::warn!("Failed to delete old embeddings: {}", e);
                }
                files_updated += 1;
                files_to_index.push(file);
            }
            _ => {
                // Unchanged file, skip
            }
        }
    }

    // Find removed files
    for old_file in existing_hashes.keys() {
        if !new_hashes.contains_key(old_file) {
            files_removed += 1;
            if let Err(e) = client.vector_db.delete_by_file(old_file).await {
                tracing::warn!("Failed to delete embeddings for removed file: {}", e);
            }
        }
    }

    // Send progress after identifying changes
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 50.0,
                total: Some(100.0),
                message: Some(format!(
                    "Processing {} changed files...",
                    files_to_index.len()
                )),
            })
            .await;
    }

    // Index new/modified files
    let (embeddings_generated, embed_errors) = if !files_to_index.is_empty() {
        // Chunk files in parallel for better performance
        let chunker = client.chunker.clone();
        let all_chunks: Vec<_> = files_to_index
            .par_iter()
            .flat_map(|file| chunker.chunk_file(file))
            .collect();

        chunks_modified = all_chunks.len();

        // Send progress after chunking
        if let (Some(peer), Some(token)) = (&peer, &progress_token) {
            let _ = peer
                .notify_progress(ProgressNotificationParam {
                    progress_token: token.clone(),
                    progress: 60.0,
                    total: Some(100.0),
                    message: Some(format!(
                        "Created {} chunks, generating embeddings...",
                        chunks_modified
                    )),
                })
                .await;
        }

        // Generate embeddings with frequent cancellation checks
        // Progress range: 60% to 85%
        let embed_result = generate_embeddings_with_cancellation(
            client,
            &all_chunks,
            &cancel_token,
            &peer,
            &progress_token,
            60.0,
            85.0,
        )
        .await?;

        let all_embeddings = embed_result.embeddings;
        let successful_chunks = embed_result.successful_chunks;

        // Send progress before storing
        if let (Some(peer), Some(token)) = (&peer, &progress_token) {
            let _ = peer
                .notify_progress(ProgressNotificationParam {
                    progress_token: token.clone(),
                    progress: 90.0,
                    total: Some(100.0),
                    message: Some(format!("Storing {} embeddings...", all_embeddings.len())),
                })
                .await;
        }

        // Check for cancellation before storing
        check_cancelled!(cancel_token);

        // Store all embeddings (pass normalized root path for per-project BM25)
        // Use successful_chunks to ensure metadata/contents match embeddings count
        let metadata: Vec<ChunkMetadata> = successful_chunks
            .iter()
            .map(|c| c.metadata.clone())
            .collect();
        let contents: Vec<String> = successful_chunks
            .iter()
            .map(|c| c.content.clone())
            .collect();

        if !all_embeddings.is_empty() {
            client
                .vector_db
                .store_embeddings(all_embeddings.clone(), metadata, contents, &path)
                .await
                .context("Failed to store embeddings")?;
        }

        (all_embeddings.len(), embed_result.errors)
    } else {
        (0, vec![])
    };

    // Collect any embedding errors (logged but not fatal)
    for err in embed_errors {
        tracing::warn!("Embedding error during incremental update: {}", err);
    }

    // Send progress before saving cache
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 95.0,
                total: Some(100.0),
                message: Some("Saving cache...".into()),
            })
            .await;
    }

    // Update persistent cache
    let mut cache = client.hash_cache.write().await;
    cache.update_root(path.clone(), new_hashes);

    // Persist to disk
    if let Err(e) = cache.save(&client.cache_path) {
        tracing::warn!("Failed to save hash cache: {}", e);
    }
    drop(cache);

    // Send progress before flush
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 98.0,
                total: Some(100.0),
                message: Some("Flushing index to disk...".into()),
            })
            .await;
    }

    // Flush the vector database to disk
    client
        .vector_db
        .flush()
        .await
        .context("Failed to flush index to disk")?;

    // Clear dirty flag early — embeddings + cache are persisted, so the index
    // is consistent even if the process is killed after this point.
    {
        let mut cache = client.hash_cache.write().await;
        cache.clear_dirty(&path);
        if let Err(e) = cache.save(&client.cache_path) {
            tracing::warn!("Failed to clear dirty flag after incremental update: {}", e);
        }
        tracing::debug!("Cleared dirty flag early (incremental) for: {}", path);
    }

    // Send final completion progress
    if let (Some(peer), Some(token)) = (&peer, &progress_token) {
        let _ = peer
            .notify_progress(ProgressNotificationParam {
                progress_token: token.clone(),
                progress: 100.0,
                total: Some(100.0),
                message: Some("Incremental update complete!".into()),
            })
            .await;
    }

    Ok(IndexResponse {
        mode: crate::rag::types::IndexingMode::Incremental,
        files_indexed: files_added,
        chunks_created: chunks_modified,
        embeddings_generated,
        duration_ms: start.elapsed().as_millis() as u64,
        errors: vec![],
        files_updated,
        files_removed,
    })
}

/// Smart index that automatically chooses between full and incremental based on existing cache
#[allow(clippy::too_many_arguments)]
pub async fn do_index_smart(
    client: &RagClient,
    path: String,
    project: Option<String>,
    include_patterns: Vec<String>,
    exclude_patterns: Vec<String>,
    max_file_size: usize,
    peer: Option<Peer<RoleServer>>,
    progress_token: Option<ProgressToken>,
    cancel_token: CancellationToken,
) -> Result<IndexResponse> {
    use super::IndexLockResult;

    // Try to acquire indexing lock
    let lock_result = client.try_acquire_index_lock(&path).await?;

    match lock_result {
        IndexLockResult::WaitForResult(mut receiver) => {
            // Another task in THIS PROCESS is indexing, wait for its result via broadcast
            tracing::info!(
                "Waiting for existing indexing operation in this process to complete for: {}",
                path
            );

            // Send progress notification if we have a peer
            if let (Some(peer), Some(token)) = (&peer, &progress_token) {
                let _ = peer
                    .notify_progress(ProgressNotificationParam {
                        progress_token: token.clone(),
                        progress: 0.0,
                        total: Some(100.0),
                        message: Some(
                            "Waiting for existing indexing operation to complete...".into(),
                        ),
                    })
                    .await;
            }

            // Wait for the result from the other operation
            match receiver.recv().await {
                Ok(result) => {
                    tracing::info!("Received result from existing indexing operation");
                    Ok(result)
                }
                Err(e) => {
                    // The sender was dropped without sending a result (error case)
                    Err(anyhow::anyhow!(
                        "Indexing operation failed or was cancelled: {}",
                        e
                    ))
                }
            }
        }
        IndexLockResult::WaitForFilesystemLock(normalized_path) => {
            // Another PROCESS is indexing this path, wait for the filesystem lock
            tracing::info!(
                "Another process is indexing {} - waiting for filesystem lock to be released",
                normalized_path
            );

            // Send progress notification if we have a peer
            if let (Some(peer), Some(token)) = (&peer, &progress_token) {
                let _ = peer
                    .notify_progress(ProgressNotificationParam {
                        progress_token: token.clone(),
                        progress: 0.0,
                        total: Some(100.0),
                        message: Some("Waiting for another process to finish indexing...".into()),
                    })
                    .await;
            }

            // Block until we can acquire the filesystem lock (with 30 min timeout)
            // This happens when the other process finishes indexing
            use super::FsLockGuard;
            use std::time::Duration;

            let path_for_lock = normalized_path.clone();
            let fs_lock_result = tokio::task::spawn_blocking(move || {
                FsLockGuard::acquire_blocking(&path_for_lock, Duration::from_secs(30 * 60))
            })
            .await
            .context("Filesystem lock blocking task panicked")??;

            match fs_lock_result {
                Some(_lock) => {
                    // We acquired the lock! The other process finished.
                    // The database should be up-to-date from their indexing.
                    // We'll do an incremental check to be safe (will be fast if nothing changed)
                    tracing::info!(
                        "Other process finished indexing {} - performing incremental check",
                        normalized_path
                    );

                    // Drop the lock immediately - we don't need it for incremental check
                    // since we're not modifying the database
                    drop(_lock);

                    // Return a response indicating we waited and the index should be current
                    // The caller can do an incremental check if they want to verify
                    Ok(IndexResponse {
                        mode: crate::rag::types::IndexingMode::Incremental,
                        files_indexed: 0,
                        chunks_created: 0,
                        embeddings_generated: 0,
                        duration_ms: 0,
                        errors: vec![],
                        files_updated: 0,
                        files_removed: 0,
                    })
                }
                None => {
                    // Timeout waiting for the lock - the other process took too long
                    Err(anyhow::anyhow!(
                        "Timeout waiting for another process to finish indexing {} (30 minutes)",
                        normalized_path
                    ))
                }
            }
        }
        IndexLockResult::Acquired(lock) => {
            // We acquired the lock, perform the actual indexing
            let result = do_index_smart_inner(
                client,
                path.clone(),
                project,
                include_patterns,
                exclude_patterns,
                max_file_size,
                peer,
                progress_token,
                cancel_token,
            )
            .await;

            // Broadcast the result to any waiters (even on error, so they don't hang)
            match &result {
                Ok(response) => {
                    lock.broadcast_result(response);
                }
                Err(e) => {
                    // On error, broadcast an error response so waiters don't hang
                    tracing::error!("Indexing failed for {}: {}", path, e);
                    let error_response = IndexResponse {
                        mode: crate::rag::types::IndexingMode::Full,
                        files_indexed: 0,
                        chunks_created: 0,
                        embeddings_generated: 0,
                        duration_ms: 0,
                        errors: vec![format!("Indexing failed: {}", e)],
                        files_updated: 0,
                        files_removed: 0,
                    };
                    lock.broadcast_result(&error_response);
                }
            }

            // Release the lock synchronously to avoid race conditions
            // This ensures the lock is removed from the map before we return
            lock.release().await;

            result
        }
    }
}

/// Default stale dirty flag timeout: 2 hours
/// If a dirty flag is older than this, it's likely from a crashed/cancelled process
const STALE_DIRTY_FLAG_TIMEOUT_SECS: u64 = 2 * 60 * 60;

/// Result of dirty flag validation
#[derive(Debug)]
enum DirtyFlagValidation {
    /// The dirty flag is valid - index is truly corrupted
    TrulyCorrupted { reason: String },
    /// The dirty flag is stale and can be safely cleared
    StaleFlag { age_secs: u64 },
    /// The index appears to be complete despite the dirty flag
    IndexAppearsComplete {
        cached_files: usize,
        indexed_files: usize,
    },
}

/// Validate whether a dirty flag represents actual corruption or is stale
async fn validate_dirty_flag(
    client: &RagClient,
    normalized_path: &str,
) -> Result<DirtyFlagValidation> {
    // Read cache and extract the information we need, then drop the lock
    let (dirty_info_data, cached_files_count) = {
        let cache = client.hash_cache.read().await;
        let dirty_info = cache.get_dirty_info(normalized_path).cloned();
        let cached_files_count = cache
            .get_root(normalized_path)
            .map(|h| h.len())
            .unwrap_or(0);
        (dirty_info, cached_files_count)
    };

    // Check if dirty flag is stale (older than timeout)
    if let Some(ref info) = dirty_info_data {
        let age = info.age_secs();
        if info.is_stale(STALE_DIRTY_FLAG_TIMEOUT_SECS) {
            return Ok(DirtyFlagValidation::StaleFlag { age_secs: age });
        }
    }

    // Check if the vector database has embeddings for this path
    let indexed_count = client
        .vector_db
        .count_by_root_path(normalized_path)
        .await
        .unwrap_or(0);

    // If we have cached file hashes but no embeddings, index is truly corrupted
    if cached_files_count > 0 && indexed_count == 0 {
        return Ok(DirtyFlagValidation::TrulyCorrupted {
            reason: format!(
                "Cache has {} files but vector DB has 0 embeddings",
                cached_files_count
            ),
        });
    }

    // If we have no cached files and no embeddings, the dirty flag was set
    // before any work was done - safe to clear and start fresh
    if cached_files_count == 0 && indexed_count == 0 {
        return Ok(DirtyFlagValidation::StaleFlag {
            age_secs: dirty_info_data.as_ref().map(|i| i.age_secs()).unwrap_or(0),
        });
    }

    // If we have both cached files and embeddings, compare the counts
    // This is a rough check - if they're close, the index is likely complete
    let indexed_files = client
        .vector_db
        .get_indexed_files(normalized_path)
        .await
        .unwrap_or_default();
    let indexed_files_count = indexed_files.len();

    // If the indexed file count is close to or exceeds cached file count,
    // the index is likely complete (some files may have multiple chunks)
    if indexed_files_count > 0 && indexed_files_count >= cached_files_count * 8 / 10 {
        // At least 80% of files are indexed
        return Ok(DirtyFlagValidation::IndexAppearsComplete {
            cached_files: cached_files_count,
            indexed_files: indexed_files_count,
        });
    }

    // Otherwise, the index is likely incomplete
    Ok(DirtyFlagValidation::TrulyCorrupted {
        reason: format!(
            "Cached {} files but only {} files indexed ({}%)",
            cached_files_count,
            indexed_files_count,
            if cached_files_count > 0 {
                indexed_files_count * 100 / cached_files_count
            } else {
                0
            }
        ),
    })
}

/// Inner implementation of smart indexing (called when we have the lock)
#[allow(clippy::too_many_arguments)]
async fn do_index_smart_inner(
    client: &RagClient,
    path: String,
    project: Option<String>,
    include_patterns: Vec<String>,
    exclude_patterns: Vec<String>,
    max_file_size: usize,
    peer: Option<Peer<RoleServer>>,
    progress_token: Option<ProgressToken>,
    cancel_token: CancellationToken,
) -> Result<IndexResponse> {
    // Normalize path to canonical form for consistent cache lookups
    let normalized_path = RagClient::normalize_path(&path)?;

    // Check if index is dirty (previous indexing was interrupted)
    let is_dirty = {
        let cache = client.hash_cache.read().await;
        cache.is_dirty(&normalized_path)
    };

    // Handle dirty index with validation
    let mut force_full_reindex = false;
    if is_dirty {
        tracing::info!(
            "Index for '{}' is marked as dirty. Validating dirty flag...",
            normalized_path
        );

        // Validate the dirty flag to determine if it's truly corrupted
        let validation = validate_dirty_flag(client, &normalized_path).await?;

        match validation {
            DirtyFlagValidation::TrulyCorrupted { reason } => {
                tracing::warn!(
                    "Index for '{}' is truly corrupted: {}. Clearing and performing full reindex.",
                    normalized_path,
                    reason
                );

                // Send progress notification about dirty state
                if let (Some(peer), Some(token)) = (&peer, &progress_token) {
                    let _ = peer
                        .notify_progress(ProgressNotificationParam {
                            progress_token: token.clone(),
                            progress: 0.0,
                            total: Some(100.0),
                            message: Some(format!(
                                "Corrupted index detected ({}), clearing...",
                                reason
                            )),
                        })
                        .await;
                }

                // Clear any existing embeddings for this path
                if let Err(e) = clear_path_data(client, &normalized_path).await {
                    tracing::error!(
                        "Failed to clear corrupted index data for '{}': {}",
                        normalized_path,
                        e
                    );
                }

                // Clear the cache entry
                let mut cache = client.hash_cache.write().await;
                cache.remove_root(&normalized_path);
                if let Err(e) = cache.save(&client.cache_path) {
                    tracing::warn!("Failed to save cache after clearing dirty state: {}", e);
                }
                drop(cache);

                force_full_reindex = true;
            }
            DirtyFlagValidation::StaleFlag { age_secs } => {
                tracing::info!(
                    "Dirty flag for '{}' is stale (age: {} seconds). Clearing flag and proceeding with incremental update.",
                    normalized_path,
                    age_secs
                );

                // Send progress notification
                if let (Some(peer), Some(token)) = (&peer, &progress_token) {
                    let _ = peer
                        .notify_progress(ProgressNotificationParam {
                            progress_token: token.clone(),
                            progress: 0.0,
                            total: Some(100.0),
                            message: Some(format!(
                                "Stale dirty flag detected (age: {}s), clearing...",
                                age_secs
                            )),
                        })
                        .await;
                }

                // Just clear the dirty flag, don't remove the cache
                let mut cache = client.hash_cache.write().await;
                cache.clear_dirty(&normalized_path);
                if let Err(e) = cache.save(&client.cache_path) {
                    tracing::warn!(
                        "Failed to save cache after clearing stale dirty flag: {}",
                        e
                    );
                }
                drop(cache);
                // Proceed with incremental update
            }
            DirtyFlagValidation::IndexAppearsComplete {
                cached_files,
                indexed_files,
            } => {
                tracing::info!(
                    "Index for '{}' appears complete despite dirty flag ({} cached files, {} indexed files). Clearing flag and proceeding with incremental update.",
                    normalized_path,
                    cached_files,
                    indexed_files
                );

                // Send progress notification
                if let (Some(peer), Some(token)) = (&peer, &progress_token) {
                    let _ = peer
                        .notify_progress(ProgressNotificationParam {
                            progress_token: token.clone(),
                            progress: 0.0,
                            total: Some(100.0),
                            message: Some(
                                "Index appears complete, clearing stale dirty flag...".into(),
                            ),
                        })
                        .await;
                }

                // Clear the dirty flag
                let mut cache = client.hash_cache.write().await;
                cache.clear_dirty(&normalized_path);
                if let Err(e) = cache.save(&client.cache_path) {
                    tracing::warn!("Failed to save cache after clearing dirty flag: {}", e);
                }
                drop(cache);
                // Proceed with incremental update
            }
        }
    }

    // Mark the index as dirty BEFORE starting (persisted immediately)
    // This ensures that if we crash/are killed, the next run knows the index is corrupted
    {
        let mut cache = client.hash_cache.write().await;
        cache.mark_dirty(&normalized_path);
        if let Err(e) = cache.save(&client.cache_path) {
            tracing::error!("Failed to save dirty flag: {}", e);
            // This is critical - if we can't persist the dirty flag, we shouldn't proceed
            anyhow::bail!("Failed to mark index as dirty before indexing: {}", e);
        }
        tracing::debug!("Marked index as dirty for: {}", normalized_path);
    }

    // Re-check has_existing_index after potential cleanup
    let cache = client.hash_cache.read().await;
    let has_existing_index = cache.get_root(&normalized_path).is_some();
    drop(cache);

    // Perform the actual indexing
    let result = if has_existing_index && !force_full_reindex {
        tracing::info!(
            "Existing index found for '{}' (normalized: '{}'), performing incremental update",
            path,
            normalized_path
        );
        do_incremental_update(
            client,
            normalized_path.clone(),
            project,
            include_patterns,
            exclude_patterns,
            max_file_size,
            peer,
            progress_token,
            cancel_token,
        )
        .await
    } else {
        tracing::info!(
            "No existing index found for '{}' (normalized: '{}') or force_full_reindex={}, performing full index",
            path,
            normalized_path,
            force_full_reindex
        );
        do_index(
            client,
            normalized_path.clone(),
            project,
            include_patterns,
            exclude_patterns,
            max_file_size,
            peer,
            progress_token,
            cancel_token,
        )
        .await
    };

    // Clear the dirty flag ONLY on successful completion
    // On error/cancellation, the dirty flag remains set
    match &result {
        Ok(_) => {
            let mut cache = client.hash_cache.write().await;
            cache.clear_dirty(&normalized_path);
            if let Err(e) = cache.save(&client.cache_path) {
                tracing::warn!(
                    "Failed to clear dirty flag after successful indexing: {}",
                    e
                );
                // Don't fail the whole operation for this
            }
            tracing::debug!("Cleared dirty flag for: {}", normalized_path);
        }
        Err(e) => {
            tracing::warn!(
                "Indexing failed or was cancelled for '{}', dirty flag remains set: {}",
                normalized_path,
                e
            );
            // Dirty flag intentionally left set - next indexing will do full reindex
        }
    }

    result
}

/// Clear all indexed data for a specific path
async fn clear_path_data(client: &RagClient, normalized_path: &str) -> Result<()> {
    // Get all file paths that were indexed for this root
    let cache = client.hash_cache.read().await;
    let file_paths: Vec<String> = cache
        .get_root(normalized_path)
        .map(|hashes| hashes.keys().cloned().collect())
        .unwrap_or_default();
    drop(cache);

    // Delete embeddings for each file
    for file_path in file_paths {
        if let Err(e) = client.vector_db.delete_by_file(&file_path).await {
            tracing::warn!(
                "Failed to delete embeddings for file '{}': {}",
                file_path,
                e
            );
        }
    }

    tracing::info!("Cleared indexed data for path: {}", normalized_path);
    Ok(())
}

#[cfg(test)]
mod tests;