symforge 10.8.0

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
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
//! Single-file admission + publication seam (relocated from `watcher`,
//! task #24) — the ONE canonical path a single repository file takes into the
//! live index: hard-scope/source/gitignore exclusion checks, the same
//! metadata-first scout and admission classification bulk load runs (including
//! secret-content demotion), content-hash-gated parsing, and
//! generation-fenced publication.
//!
//! Compiled under BOTH the server and embed features: the watcher, freshen,
//! and reconciliation paths consume it via `crate::watcher`'s re-export, and
//! the embed facade exposes [`update_file_from_disk`] / [`remove_file`] so an
//! embedder's per-file reindex goes through the identical seam instead of a
//! hand-rolled parse-and-poke (which would bypass admission entirely).

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

use tracing::{debug, trace, warn};

use crate::domain::{
    FileClassification, FileDisposition, LanguageId, MetadataOnlyReason, ScoutDecision,
};
use crate::hash;
use crate::live_index::store::{IndexedFile, PublicationFence, PublishedGeneration, SharedIndex};
use crate::parsing;

/// Result of a single re-index attempt for one file.
#[derive(Debug, PartialEq, Eq)]
pub enum ReindexResult {
    /// Content hash matched existing entry — tree-sitter parse was skipped.
    HashSkip,
    /// File was re-parsed and the index was updated.
    Reindexed,
    /// File classified as Tier 2/3 by the admission gate — NOT parsed/inserted.
    /// Any prior Tier-1 entry was removed and a skip record recorded. The index
    /// remains free of this path's symbols.
    Skipped,
    /// ENOENT observed by `read_and_index`; caller decides whether to retry or treat as confirmed-absent.
    NotFound,
    /// File was not found (ENOENT) — it has been removed from the index.
    Removed,
    /// File could not be read for a reason other than ENOENT.
    ReadError(String),
}

/// Crate-internal result of a single-file publication attempt.
///
/// `ReindexResult` is part of the semver-public embed facade and downstream
/// callers can exhaustively match its six variants. Publication contention is
/// therefore carried on this private result instead of widening that public
/// enum in a patch release.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum ReindexOutcome {
    HashSkip,
    Reindexed,
    Skipped,
    NotFound,
    Removed,
    ReadError(String),
    /// The observed change did not reach a publication boundary. Callers must
    /// retry or refuse rather than claim the stale state was reconciled.
    PublicationRejected,
}

impl ReindexOutcome {
    fn into_public_compat(self) -> ReindexResult {
        match self {
            Self::HashSkip => ReindexResult::HashSkip,
            Self::Reindexed => ReindexResult::Reindexed,
            Self::Skipped => ReindexResult::Skipped,
            Self::NotFound => ReindexResult::NotFound,
            Self::Removed => ReindexResult::Removed,
            Self::ReadError(error) => ReindexResult::ReadError(error),
            // Before this patch the public update seam reported a lost
            // publication race as Skipped. Preserve that compatibility only at
            // the frozen embed boundary; in-crate callers retain the typed
            // rejection and must retry or refuse.
            Self::PublicationRejected => ReindexResult::Skipped,
        }
    }
}

pub(crate) struct ReindexReceipt {
    pub outcome: ReindexOutcome,
    /// Publication fence captured immediately before this attempt's
    /// filesystem observation.
    pub observed_at: PublicationFence,
    /// Exact immutable generation returned by the winning publication seam.
    pub published: Option<Arc<PublishedGeneration>>,
    pub snapshot_created: bool,
}

impl ReindexReceipt {
    fn observed(outcome: ReindexOutcome, observed_at: PublicationFence) -> Self {
        Self {
            outcome,
            observed_at,
            published: None,
            snapshot_created: false,
        }
    }

    fn published(
        outcome: ReindexOutcome,
        observed_at: PublicationFence,
        published: Arc<PublishedGeneration>,
    ) -> Self {
        Self {
            outcome,
            observed_at,
            published: Some(published),
            snapshot_created: false,
        }
    }
}

/// Content-hash-gated single-file re-index.
///
/// Reads the file, compares its hash against the existing index entry, and
/// skips the expensive tree-sitter parse when the hash matches.
///
/// # Lock discipline
/// The write lock is **never** held during the tree-sitter parse. The sequence is:
/// 1. Read file bytes (no lock)
/// 2. Acquire read lock → compare hash → drop read lock
/// 3. Parse (no lock)
/// 4. Acquire write lock → update_file → drop write lock
pub(crate) fn maybe_reindex<L>(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    language: L,
    expected_gen: u64,
) -> ReindexOutcome
where
    L: Into<Option<LanguageId>>,
{
    let language = language.into();
    let first =
        read_and_index_with_receipt(relative_path, abs_path, shared, language, expected_gen);
    if first.observed_at.project_generation != expected_gen {
        return ReindexOutcome::PublicationRejected;
    }
    let mut last_absence_fence = match first.outcome {
        ReindexOutcome::NotFound => first.observed_at,
        other => return other,
    };

    let delays_ms = [50u64, 200, 500];
    for delay_ms in delays_ms {
        std::thread::sleep(std::time::Duration::from_millis(delay_ms));
        let retry =
            read_and_index_with_receipt(relative_path, abs_path, shared, language, expected_gen);
        if retry.observed_at.project_generation != expected_gen {
            return ReindexOutcome::PublicationRejected;
        }
        match retry.outcome {
            ReindexOutcome::NotFound => last_absence_fence = retry.observed_at,
            other => return other,
        }
    }

    finalize_missing_file(
        shared,
        relative_path,
        abs_path,
        expected_gen,
        last_absence_fence,
    )
}

fn finalize_missing_file(
    shared: &SharedIndex,
    relative_path: &str,
    abs_path: &Path,
    expected_gen: u64,
    absence_fence: PublicationFence,
) -> ReindexOutcome {
    if absence_fence.project_generation != expected_gen {
        return ReindexOutcome::PublicationRejected;
    }
    if shared
        .remove_file_if_absent_at_publication_fence_with_receipt(
            relative_path,
            abs_path,
            absence_fence,
        )
        .is_some()
    {
        warn!("watcher: file not found after retries, removed from index: {relative_path}");
        ReindexOutcome::Removed
    } else {
        trace!(
            "watcher: file not found after retries, stale publication rejected remove: {relative_path}"
        );
        ReindexOutcome::PublicationRejected
    }
}

/// Recover the project root by walking up from the absolute event path once per
/// component of the relative path. Both come from the same watcher event (or
/// freshen-on-read call), so the suffix relationship holds by construction;
/// `None` only if the relative path is deeper than the absolute one.
fn project_root_from_paths(abs_path: &Path, relative_path: &str) -> Option<PathBuf> {
    let depth = Path::new(relative_path).components().count();
    abs_path.ancestors().nth(depth).map(|p| p.to_path_buf())
}

fn catalog_terminal_disposition(decision: &ScoutDecision) -> Option<FileDisposition> {
    match decision {
        ScoutDecision::HardSkip { reason } => Some(FileDisposition::HardSkip { reason: *reason }),
        ScoutDecision::MetadataOnly { reason } => Some(FileDisposition::MetadataOnly {
            reason: reason.clone(),
        }),
        ScoutDecision::Ingest { .. } | ScoutDecision::Unavailable { .. } => None,
    }
}

pub(crate) fn read_and_index<L>(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    language: L,
    expected_gen: u64,
) -> ReindexOutcome
where
    L: Into<Option<LanguageId>>,
{
    read_and_index_with_receipt(relative_path, abs_path, shared, language, expected_gen).outcome
}

fn read_and_index_with_receipt<L>(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    language: L,
    expected_gen: u64,
) -> ReindexReceipt
where
    L: Into<Option<LanguageId>>,
{
    read_and_index_with_stable_read_receipt(
        relative_path,
        abs_path,
        shared,
        language,
        expected_gen,
        crate::live_index::store::stable_read_file,
    )
}

/// Run one canonical repository-relative path through the same metadata-first
/// admission and publication seam used by watcher events and reconciliation.
pub(crate) fn admit_and_index_single_path(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    expected_gen: u64,
) -> ReindexOutcome {
    admit_and_index_single_path_with_receipt(relative_path, abs_path, shared, expected_gen).outcome
}

pub(crate) fn admit_and_index_single_path_with_receipt(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    expected_gen: u64,
) -> ReindexReceipt {
    read_and_index_with_stable_read_receipt(
        relative_path,
        abs_path,
        shared,
        None,
        expected_gen,
        crate::live_index::store::stable_read_file,
    )
}

#[cfg(test)]
pub(crate) fn read_and_index_with_stable_read<L, R>(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    language: L,
    expected_gen: u64,
    stable_read: R,
) -> ReindexOutcome
where
    L: Into<Option<LanguageId>>,
    R: FnMut(&Path, &crate::domain::FileStamp) -> crate::live_index::store::StableReadOutcome,
{
    read_and_index_with_stable_read_receipt(
        relative_path,
        abs_path,
        shared,
        language,
        expected_gen,
        stable_read,
    )
    .outcome
}

fn read_and_index_with_stable_read_receipt<L, R>(
    relative_path: &str,
    abs_path: &Path,
    shared: &SharedIndex,
    language: L,
    expected_gen: u64,
    mut stable_read: R,
) -> ReindexReceipt
where
    L: Into<Option<LanguageId>>,
    R: FnMut(&Path, &crate::domain::FileStamp) -> crate::live_index::store::StableReadOutcome,
{
    let language = language.into();
    let mut base = shared.published_generation();
    let mut observed_at = PublicationFence::from_published(&base);
    // Keep single-file watcher/freshen paths symmetric with the bulk walk.
    // Both universal name exclusions and placement-derived subtree exclusions
    // run before any metadata/content read from VCS or runtime-state internals.
    let relative = Path::new(relative_path);
    if crate::discovery::path_is_hard_scope_excluded(relative)
        || shared.is_source_excluded(relative)
        || shared.read().is_path_gitignored(relative_path)
    {
        if observed_at.project_generation != expected_gen {
            return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
        }
        return match shared
            .remove_file_at_publication_fence_with_receipt(relative_path, observed_at)
        {
            Some(published) => {
                debug!("watcher: source-scope eviction {relative_path}");
                ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published)
            }
            None => {
                trace!("watcher: source-scope eviction publication rejected {relative_path}");
                ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at)
            }
        };
    }

    const MAX_PUBLICATION_ATTEMPTS: usize = 4;
    for attempt in 1..=MAX_PUBLICATION_ATTEMPTS {
        base = shared.published_generation();
        observed_at = PublicationFence::from_published(&base);
        // The store mutation CAS protects the health/live index base, whose
        // generation is intentionally distinct from the full publication
        // bundle generation. Bridge- or authority-only publishes can advance
        // the latter while retaining this same live-index base.
        let expected_index_state_generation = base.health.generation;

        // Run the same metadata-first scout as cold load before any whole-file read.
        let mut scouted = match crate::discovery::scout_single_path(relative_path, abs_path) {
            Ok(scouted) => scouted,
            Err(error) => {
                if matches!(
                    std::fs::metadata(abs_path),
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound
                ) {
                    return ReindexReceipt::observed(ReindexOutcome::NotFound, observed_at);
                }
                warn!("watcher: failed to scout {relative_path}: {error}");
                return ReindexReceipt::observed(
                    ReindexOutcome::ReadError(error.to_string()),
                    observed_at,
                );
            }
        };
        let mtime_secs = scouted
            .stamp
            .modified_hint
            .and_then(|time| time.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|duration| duration.as_secs())
            .unwrap_or(0);

        if let Some(disposition) = catalog_terminal_disposition(&scouted.decision) {
            if let Some(published) = shared.publish_terminal_disposition_at_generation(
                relative_path,
                scouted,
                disposition,
                expected_gen,
                expected_index_state_generation,
            ) {
                debug!("watcher: metadata-terminal admission {relative_path}");
                return ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published);
            }
            if shared.current_project_generation() == expected_gen
                && shared.published_state().generation != expected_index_state_generation
            {
                trace!(attempt, "watcher: retrying metadata-terminal publication");
                continue;
            }
            trace!("watcher: stale metadata-terminal admission rejected: {relative_path}");
            return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
        }
        if let ScoutDecision::Unavailable { stage, kind } = &scouted.decision {
            if *stage == crate::domain::AccessStage::Metadata
                && *kind == crate::domain::AccessErrorKind::NotFound
            {
                return ReindexReceipt::observed(ReindexOutcome::NotFound, observed_at);
            }
            let disposition = FileDisposition::Unreadable {
                stage: *stage,
                kind: *kind,
            };
            if let Some(published) = shared.publish_terminal_disposition_at_generation(
                relative_path,
                scouted,
                disposition,
                expected_gen,
                expected_index_state_generation,
            ) {
                return ReindexReceipt::published(
                    ReindexOutcome::ReadError("single-path scout unavailable".to_string()),
                    observed_at,
                    published,
                );
            }
            if shared.current_project_generation() == expected_gen
                && shared.published_state().generation != expected_index_state_generation
            {
                trace!(attempt, "watcher: retrying unavailable publication");
                continue;
            }
            return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
        }

        // Generated-output placement is metadata policy too; evaluate it before
        // loading content, matching the cold-walk ordering.
        if let Some(root) = project_root_from_paths(abs_path, relative_path)
            && crate::discovery::is_untracked_generated_output_path(&root, relative_path)
        {
            let decision = ScoutDecision::MetadataOnly {
                reason: MetadataOnlyReason::GeneratedOrVendor,
            };
            let disposition = catalog_terminal_disposition(&decision)
                .expect("metadata-only decision must project to a terminal disposition");
            scouted.decision = decision;
            if let Some(published) = shared.publish_terminal_disposition_at_generation(
                relative_path,
                scouted,
                disposition,
                expected_gen,
                expected_index_state_generation,
            ) {
                return ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published);
            }
            if shared.current_project_generation() == expected_gen
                && shared.published_state().generation != expected_index_state_generation
            {
                trace!(attempt, "watcher: retrying generated-output publication");
                continue;
            }
            return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
        }

        let targets = match &scouted.decision {
            ScoutDecision::Ingest { targets } => *targets,
            _ => unreachable!("terminal scout decisions return before content ingestion"),
        };

        let language = language.or(scouted.language).unwrap_or(LanguageId::Text);

        let bytes = match stable_read(abs_path, &scouted.stamp) {
            crate::live_index::store::StableReadOutcome::Accepted { bytes, .. } => bytes,
            crate::live_index::store::StableReadOutcome::HardSkip { reason } => {
                let decision = ScoutDecision::HardSkip { reason };
                let disposition = catalog_terminal_disposition(&decision)
                    .expect("hard-skip decision must project to a terminal disposition");
                scouted.decision = decision;
                if let Some(published) = shared.publish_terminal_disposition_at_generation(
                    relative_path,
                    scouted,
                    disposition,
                    expected_gen,
                    expected_index_state_generation,
                ) {
                    return ReindexReceipt::published(
                        ReindexOutcome::Skipped,
                        observed_at,
                        published,
                    );
                }
                if shared.current_project_generation() == expected_gen
                    && shared.published_state().generation != expected_index_state_generation
                {
                    trace!(
                        attempt,
                        "watcher: retrying stable-read hard-skip publication"
                    );
                    continue;
                }
                return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
            }
            crate::live_index::store::StableReadOutcome::Unreadable { stage, kind } => {
                let publication = shared.publish_terminal_disposition_at_generation(
                    relative_path,
                    scouted,
                    FileDisposition::Unreadable { stage, kind },
                    expected_gen,
                    expected_index_state_generation,
                );
                if let Some(published) = publication {
                    return ReindexReceipt::published(
                        ReindexOutcome::ReadError("stable read unavailable".to_string()),
                        observed_at,
                        published,
                    );
                }
                if shared.current_project_generation() == expected_gen
                    && shared.published_state().generation != expected_index_state_generation
                {
                    trace!(
                        attempt,
                        "watcher: retrying stable-read unavailable publication"
                    );
                    continue;
                }
                return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
            }
            crate::live_index::store::StableReadOutcome::UnstableDuringRead => {
                let publication = shared.publish_terminal_disposition_at_generation(
                    relative_path,
                    scouted,
                    FileDisposition::UnstableDuringRead,
                    expected_gen,
                    expected_index_state_generation,
                );
                if let Some(published) = publication {
                    return ReindexReceipt::published(
                        ReindexOutcome::ReadError("file changed during stable read".to_string()),
                        observed_at,
                        published,
                    );
                }
                if shared.current_project_generation() == expected_gen
                    && shared.published_state().generation != expected_index_state_generation
                {
                    trace!(attempt, "watcher: retrying unstable-read publication");
                    continue;
                }
                return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
            }
        };

        // The watcher shares the exact stable-content admission boundary used
        // by cold load. Terminal outcomes are published before hashing or parsing,
        // and the owned byte buffer is discarded on every non-admitted path.
        if let crate::knowledge::StableContentAdmission::MetadataOnly(reason) =
            crate::knowledge::classify_stable_content(relative_path, targets, &bytes)
        {
            if let Some(published) = shared.publish_terminal_disposition_at_generation(
                relative_path,
                scouted,
                FileDisposition::MetadataOnly { reason },
                expected_gen,
                expected_index_state_generation,
            ) {
                return ReindexReceipt::published(ReindexOutcome::Skipped, observed_at, published);
            }
            if shared.current_project_generation() == expected_gen
                && shared.published_state().generation != expected_index_state_generation
            {
                trace!(attempt, "watcher: retrying content-policy publication");
                continue;
            }
            return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
        }

        // Compute the hash and compare without holding the publication writer lock.
        let new_hash = hash::digest_hex(&bytes);
        {
            let index = shared.read();
            if let Some(existing) = index.get_file(relative_path)
                && existing.content_hash == new_hash
            {
                drop(index);
                if let Some(published) = shared.publish_hash_skip_at_generation(
                    relative_path,
                    mtime_secs,
                    scouted,
                    targets,
                    expected_gen,
                    expected_index_state_generation,
                ) {
                    debug!("watcher: hash-skip {relative_path}");
                    return ReindexReceipt {
                        outcome: ReindexOutcome::HashSkip,
                        observed_at,
                        published: Some(published),
                        snapshot_created: false,
                    };
                }
                if shared.current_project_generation() == expected_gen
                    && shared.published_state().generation != expected_index_state_generation
                {
                    trace!(attempt, "watcher: retrying hash-skip publication");
                    continue;
                }
                return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
            }
        }

        // Parse outside the lock (Tier-1 only).
        let result = parsing::process_file_with_classification(
            relative_path,
            &bytes,
            language,
            FileClassification::for_indexed_path(relative_path, targets),
        );
        let indexed = IndexedFile::from_parse_result(result, bytes).with_mtime(mtime_secs);

        // Commit every observable lane under one source-base fence and one
        // publication boundary. If another update won while this build was
        // off-lock, retry from disk instead of overwriting that newer bundle.
        if let Some(publication) = shared.publish_indexed_file_at_generation(
            relative_path,
            indexed,
            scouted,
            targets,
            expected_gen,
            expected_index_state_generation,
        ) {
            debug!("watcher: re-indexed {relative_path}");
            return ReindexReceipt {
                outcome: ReindexOutcome::Reindexed,
                observed_at,
                published: Some(publication.published),
                snapshot_created: publication.snapshot_created,
            };
        }
        if shared.current_project_generation() == expected_gen
            && shared.published_state().generation != expected_index_state_generation
        {
            trace!(attempt, "watcher: retrying indexed-file publication");
            continue;
        }
        trace!("watcher: stale indexed-file publication rejected: {relative_path}");
        return ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at);
    }

    warn!(
        "watcher: aborting {relative_path} after {MAX_PUBLICATION_ATTEMPTS} concurrent publications"
    );
    ReindexReceipt::observed(ReindexOutcome::PublicationRejected, observed_at)
}

/// Re-index one repository file FROM DISK through the canonical single-file
/// admission seam (embed facade; task #24 / AAP ask 3).
///
/// `relative_path` is the repo-relative, forward-slash path (backslashes are
/// normalized). The file's current bytes are read from
/// `repo_root/relative_path` with the same stable-read, admission, hash-skip,
/// and fenced-publication behavior the watcher applies — an embedder needs no
/// parsing or store internals, and a file the admission gate demotes is
/// recorded as skipped, never silently parsed.
///
/// No retries, no sleeps: a missing file returns [`ReindexResult::NotFound`]
/// immediately (deadline-friendly for in-VM embedders); callers tracking a
/// deletion should follow up with [`remove_file`].
pub fn update_file_from_disk(
    shared: &SharedIndex,
    repo_root: &Path,
    relative_path: &str,
) -> ReindexResult {
    let relative = relative_path.replace('\\', "/");
    let abs_path = repo_root.join(&relative);
    let expected_gen = shared.current_project_generation();
    admit_and_index_single_path(&relative, &abs_path, shared, expected_gen).into_public_compat()
}

/// Remove one file from the live index (embed facade; task #24 / AAP ask 3).
///
/// Generation-fenced like the watcher's removals: returns `true` when the
/// removal was APPLIED under the current project generation (including the
/// no-op case where the path was not tracked), `false` only when a concurrent
/// retarget invalidated the generation fence.
pub fn remove_file(shared: &SharedIndex, relative_path: &str) -> bool {
    let relative = relative_path.replace('\\', "/");
    shared.remove_file_at_generation(&relative, shared.current_project_generation())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::live_index::LiveIndex;

    /// Task #24: the facade per-file entry points drive the SAME admission
    /// seam — index a new file, see its symbols, update it, remove it — with
    /// no parsing/store imports below the facade.
    #[test]
    fn facade_update_and_remove_round_trip() {
        let dir = tempfile::TempDir::new().expect("root");
        std::fs::write(
            dir.path().join("lib.rs"),
            "pub fn first() {}
",
        )
        .expect("seed");
        let shared = LiveIndex::load(dir.path()).expect("cold load");
        assert_eq!(shared.read().file_count(), 1);

        // New file appears through the seam.
        std::fs::write(
            dir.path().join("extra.rs"),
            "pub fn second() {}
",
        )
        .expect("new file");
        let outcome = update_file_from_disk(&shared, dir.path(), "extra.rs");
        assert!(
            matches!(outcome, ReindexResult::Reindexed),
            "new file must be admitted and parsed, got {outcome:?}"
        );
        assert_eq!(shared.read().file_count(), 2);

        // Unchanged content is hash-skipped, not re-parsed.
        let outcome = update_file_from_disk(&shared, dir.path(), "extra.rs");
        assert!(
            matches!(outcome, ReindexResult::HashSkip),
            "unchanged file must hash-skip, got {outcome:?}"
        );

        // Edited content re-parses.
        std::fs::write(
            dir.path().join("extra.rs"),
            "pub fn renamed() {}
",
        )
        .expect("edit");
        let outcome = update_file_from_disk(&shared, dir.path(), "extra.rs");
        assert!(
            matches!(outcome, ReindexResult::Reindexed),
            "edited file must re-parse, got {outcome:?}"
        );

        // Removal is fenced: applied under the current generation (a second
        // removal is an applied no-op, still fence-true).
        assert!(remove_file(&shared, "extra.rs"), "tracked file removes");
        assert_eq!(shared.read().file_count(), 1);
        assert!(
            remove_file(&shared, "extra.rs"),
            "no-op removal still passes the generation fence"
        );
        assert_eq!(shared.read().file_count(), 1);

        // Missing file reports NotFound without retries.
        let outcome = update_file_from_disk(&shared, dir.path(), "ghost.rs");
        assert!(
            matches!(outcome, ReindexResult::NotFound),
            "missing file must be NotFound, got {outcome:?}"
        );
    }

    #[test]
    fn reindex_receipt_remains_exact_after_later_publication() {
        let dir = tempfile::TempDir::new().expect("root");
        let path = dir.path().join("lib.rs");
        std::fs::write(&path, "pub fn first() {}\n").expect("seed");
        let shared = LiveIndex::load(dir.path()).expect("cold load");
        let expected_gen = shared.current_project_generation();

        std::fs::write(&path, "pub fn second() {}\n").expect("first edit");
        let second =
            admit_and_index_single_path_with_receipt("lib.rs", &path, &shared, expected_gen);
        assert_eq!(second.outcome, ReindexOutcome::Reindexed);
        assert!(second.snapshot_created);
        let second_published = second.published.expect("second publication receipt");
        let second_fence = PublicationFence::from_published(&second_published);

        let unchanged =
            admit_and_index_single_path_with_receipt("lib.rs", &path, &shared, expected_gen);
        assert_eq!(unchanged.outcome, ReindexOutcome::HashSkip);
        assert!(!unchanged.snapshot_created);
        let unchanged_published = unchanged.published.expect("hash-skip publication receipt");
        assert!(
            unchanged_published
                .live
                .get_file("lib.rs")
                .expect("hash-skip file")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "second")
        );

        std::fs::write(&path, "pub fn third() {}\n").expect("second edit");
        let third =
            admit_and_index_single_path_with_receipt("lib.rs", &path, &shared, expected_gen);
        assert_eq!(third.outcome, ReindexOutcome::Reindexed);
        let third_published = third.published.expect("third publication receipt");
        let third_fence = PublicationFence::from_published(&third_published);
        assert_ne!(second_fence, third_fence);

        let second_file = second_published
            .live
            .get_file("lib.rs")
            .expect("second file");
        assert!(
            second_file
                .symbols
                .iter()
                .any(|symbol| symbol.name == "second")
        );
        assert!(
            !second_file
                .symbols
                .iter()
                .any(|symbol| symbol.name == "third")
        );
        assert!(
            !unchanged_published
                .live
                .get_file("lib.rs")
                .expect("immutable hash-skip file")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "third")
        );
        assert!(
            third_published
                .live
                .get_file("lib.rs")
                .expect("third file")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "third")
        );

        assert!(
            shared
                .take_pre_update_snapshot_for_publication_at_generation(
                    "lib.rs",
                    expected_gen,
                    second_fence,
                )
                .is_none(),
            "the old receipt must not drain the later publication's snapshot"
        );
        let third_baseline = shared
            .take_pre_update_snapshot_for_publication_at_generation(
                "lib.rs",
                expected_gen,
                third_fence,
            )
            .expect("third publication owns the current snapshot");
        assert!(
            third_baseline
                .symbols
                .iter()
                .any(|symbol| symbol.name == "second")
        );
    }

    #[test]
    fn bridge_only_publication_does_not_invalidate_live_index_reindex_cas() {
        let dir = tempfile::TempDir::new().expect("root");
        let path = dir.path().join("lib.rs");
        std::fs::write(&path, "pub fn before() {}\n").expect("seed");
        let shared = LiveIndex::load(dir.path()).expect("cold load");
        let before = shared.published_generation();
        let health_generation = before.health.generation;

        let prepared = shared.prepare_bridge_rebuild();
        assert!(shared.publish_prepared_bridge(prepared));
        let bridge_only = shared.published_generation();
        assert!(
            bridge_only.publication_generation > before.publication_generation,
            "bridge publication must advance the full publication bundle"
        );
        assert_eq!(
            bridge_only.health.generation, health_generation,
            "bridge publication must retain the live-index health base"
        );

        std::fs::write(&path, "pub fn after() {}\n").expect("edit");
        let receipt = admit_and_index_single_path_with_receipt(
            "lib.rs",
            &path,
            &shared,
            shared.current_project_generation(),
        );
        assert_eq!(receipt.outcome, ReindexOutcome::Reindexed);
        assert!(
            receipt
                .published
                .expect("winning reindex publication")
                .live
                .get_file("lib.rs")
                .expect("reindexed file")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "after")
        );
    }

    #[test]
    fn stale_not_found_fence_preserves_recreated_file_and_reports_rejection() {
        let dir = tempfile::TempDir::new().expect("root");
        let path = dir.path().join("lib.rs");
        std::fs::write(&path, "pub fn first() {}\n").expect("seed");
        let shared = LiveIndex::load(dir.path()).expect("cold load");

        std::fs::remove_file(&path).expect("simulate missing observation");
        let absence_fence = shared.publication_fence();

        std::fs::write(&path, "pub fn recreated() {}\n").expect("recreate");
        assert_eq!(
            finalize_missing_file(
                &shared,
                "lib.rs",
                &path,
                shared.current_project_generation(),
                absence_fence,
            ),
            ReindexOutcome::PublicationRejected,
            "a rejected stale removal must not claim the file was removed"
        );
        assert!(
            shared
                .read()
                .get_file("lib.rs")
                .expect("last-valid indexed file remains")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "first")
        );
        assert!(
            std::fs::read_to_string(&path)
                .expect("recreated disk file")
                .contains("recreated")
        );
    }

    #[test]
    fn missing_file_finalization_ignores_unrelated_same_project_publication() {
        let dir = tempfile::TempDir::new().expect("root");
        let a_path = dir.path().join("a.rs");
        let b_path = dir.path().join("b.rs");
        std::fs::write(&a_path, "pub fn a_before() {}\n").expect("seed a");
        std::fs::write(&b_path, "pub fn b_before() {}\n").expect("seed b");
        let shared = LiveIndex::load(dir.path()).expect("cold load");
        let expected_gen = shared.current_project_generation();

        std::fs::remove_file(&a_path).expect("observe a missing");
        let absence_fence = shared.publication_fence();

        std::fs::write(&b_path, "pub fn b_after() {}\n").expect("edit b");
        let b_receipt =
            admit_and_index_single_path_with_receipt("b.rs", &b_path, &shared, expected_gen);
        assert_eq!(b_receipt.outcome, ReindexOutcome::Reindexed);
        let after_b = shared.publication_fence();
        assert_eq!(after_b.project_generation, absence_fence.project_generation);
        assert_ne!(
            after_b.publication_generation, absence_fence.publication_generation,
            "the regression requires an intervening unrelated publication"
        );

        assert_eq!(
            finalize_missing_file(&shared, "a.rs", &a_path, expected_gen, absence_fence,),
            ReindexOutcome::Removed,
            "an unrelated publication must not prevent convergence to disk absence"
        );
        let published = shared.published_generation();
        assert!(published.live.get_file("a.rs").is_none());
        assert!(
            published
                .live
                .get_file("b.rs")
                .expect("unrelated publication survives")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "b_after")
        );
    }

    #[test]
    fn stale_project_generation_cannot_remove_from_rebound_project() {
        let project_a = tempfile::TempDir::new().expect("project A");
        let project_b = tempfile::TempDir::new().expect("project B");
        std::fs::write(project_a.path().join("lib.rs"), "pub fn a() {}\n").unwrap();
        std::fs::write(project_b.path().join("lib.rs"), "pub fn b() {}\n").unwrap();
        let shared = LiveIndex::load(project_a.path()).expect("load A");
        let stale_gen = shared.current_project_generation();
        shared.reload(project_b.path()).expect("rebind to B");
        let rebound_fence = shared.publication_fence();

        assert_eq!(
            finalize_missing_file(
                &shared,
                "lib.rs",
                &project_a.path().join("lib.rs"),
                stale_gen,
                rebound_fence,
            ),
            ReindexOutcome::PublicationRejected
        );
        assert!(
            shared
                .read()
                .get_file("lib.rs")
                .expect("B file survives stale absence finalization")
                .symbols
                .iter()
                .any(|symbol| symbol.name == "b")
        );

        let publication_before = shared.publication_fence();
        let excluded = read_and_index_with_receipt(
            ".git/config",
            &project_b.path().join(".git/config"),
            &shared,
            None::<LanguageId>,
            stale_gen,
        );
        assert_eq!(excluded.outcome, ReindexOutcome::PublicationRejected);
        assert_eq!(shared.publication_fence(), publication_before);
        assert!(shared.read().get_file("lib.rs").is_some());
    }
}