graphforge-storage 0.5.2

GraphForge StorageProvider trait and Parquet backend
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
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
//! Coordinated, crash-safe publication for text and vector search artifacts.
//!
//! A per-key advisory file lock serializes builders across threads and
//! processes. Builds occur in a unique sibling directory, are synchronized,
//! renamed into an immutable version directory, then made visible by atomically
//! replacing a small `current.json` pointer. The prior pointer remains readable
//! until that final replace.

use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use super::search_manifest::{
    SearchArtifactError, SearchArtifactKey, SearchIndexKind, SearchManifest, SearchSourceSnapshot,
};

const CURRENT_FILE: &str = "current.json";
const MANIFEST_FILE: &str = "manifest.json";
const VERSIONS_DIR: &str = "versions";
const BUILD_PREFIX: &str = ".build-";
const VERSION_PREFIX: &str = "version-";
const MAX_CURRENT_BYTES: usize = 4096;

/// Bounds for lock waits and reopen cleanup.
#[derive(Clone, Copy, Debug)]
pub struct SearchCoordinationLimits {
    /// Maximum time to wait for another builder of the same key.
    pub lock_timeout: Duration,
    /// Cooperative checkpoint interval while waiting.
    pub lock_poll_interval: Duration,
    /// Maximum filesystem entries inspected during one cleanup pass.
    pub cleanup_entries: usize,
}

impl Default for SearchCoordinationLimits {
    fn default() -> Self {
        Self {
            lock_timeout: Duration::from_secs(30),
            lock_poll_interval: Duration::from_millis(25),
            cleanup_entries: 10_000,
        }
    }
}

/// Whether a coordinated call may reuse an exactly matching fresh artifact.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SearchPublicationMode {
    /// Reuse a verified fresh artifact; otherwise build and publish.
    ReuseFresh,
    /// Always build and atomically replace the active pointer.
    Replace,
}

/// Immutable publication parameters supplied by a concrete backend.
#[derive(Clone, Copy, Debug)]
pub struct SearchPublicationPlan<'a> {
    /// Normalized artifact identity.
    pub key: &'a SearchArtifactKey,
    /// Pinned backend version.
    pub backend_version: &'a str,
    /// Pinned scoring/tokenization/vector contract version.
    pub contract_version: &'a str,
    /// Required vector dimension; absent for text.
    pub dimension: Option<u32>,
    /// Fresh reuse or forced replacement.
    pub mode: SearchPublicationMode,
}

/// A verified currently published search artifact.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PublishedSearchArtifact {
    /// Immutable version directory containing backend files and manifest.
    pub path: PathBuf,
    /// Parsed completed manifest.
    pub manifest: SearchManifest,
}

/// Result of one coordinated build request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SearchPublicationOutcome {
    /// Another request or an earlier run already published an exact match.
    Reused(PublishedSearchArtifact),
    /// This request published a new immutable version.
    Published {
        /// Newly active artifact.
        artifact: PublishedSearchArtifact,
        /// One for the normal path, two after a concurrent mutation retry.
        attempts: u8,
    },
}

/// Decision returned by an atomic primary-data update builder.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SearchUpdateBuild {
    /// The current immutable publication already contains the requested data.
    ReuseCurrent,
    /// The builder wrote a complete replacement into the supplied directory.
    Publish,
}

/// Coordinate freshness reuse, a bounded mutation retry, and atomic
/// publication under the artifact-key writer lock.
///
/// `snapshot` must return both the current committed search generation and the
/// canonical fingerprint of the relevant graph snapshot. `validate_current`
/// validates backend files before a fresh manifest can be reused; a text
/// backend reports rebuildable corruption as
/// [`SearchArtifactError::CorruptDerivedIndex`]. `build` writes only inside the
/// supplied temporary directory. `checkpoint` enforces cooperative
/// cancellation and backend time/resource limits.
///
/// # Errors
/// Distinguishes cancellation, lock failure, corrupt primary vectors,
/// resource exhaustion, build failure, I/O, and repeated concurrent mutation.
pub fn coordinate_search_publication<S, V, B, C>(
    project_dir: &Path,
    plan: SearchPublicationPlan<'_>,
    limits: SearchCoordinationLimits,
    mut snapshot: S,
    mut validate_current: V,
    mut build: B,
    mut checkpoint: C,
) -> Result<SearchPublicationOutcome, SearchArtifactError>
where
    S: FnMut() -> Result<SearchSourceSnapshot, SearchArtifactError>,
    V: FnMut(&PublishedSearchArtifact) -> Result<(), SearchArtifactError>,
    B: FnMut(&Path, &SearchSourceSnapshot) -> Result<(), SearchArtifactError>,
    C: FnMut() -> Result<(), SearchArtifactError>,
{
    let root = plan.key.artifact_root(project_dir);
    std::fs::create_dir_all(&root).map_err(|source| io("create artifact root", &root, source))?;
    let _writer = SearchWriterLock::acquire(&root, limits, &mut checkpoint)?;
    checkpoint()?;

    let initial = snapshot()?;
    match current_search_artifact(project_dir, plan.key) {
        Ok(Some(artifact)) if plan.mode == SearchPublicationMode::ReuseFresh => {
            match artifact.manifest.verify_fresh(
                plan.key,
                plan.backend_version,
                plan.contract_version,
                plan.dimension,
                &initial,
            ) {
                Ok(()) => match validate_current(&artifact) {
                    Ok(()) => return Ok(SearchPublicationOutcome::Reused(artifact)),
                    Err(error)
                        if plan.key.kind() == SearchIndexKind::Text
                            && rebuildable_metadata(&error) => {}
                    Err(error) if plan.key.kind() == SearchIndexKind::Vector => {
                        return Err(primary_vector_error(root, error));
                    }
                    Err(error) => return Err(error),
                },
                Err(SearchArtifactError::Stale { .. })
                    if plan.key.kind() == SearchIndexKind::Text => {}
                Err(error) => return Err(error),
            }
        }
        Ok(Some(_) | None) => {}
        Err(error) if plan.key.kind() == SearchIndexKind::Text && rebuildable_metadata(&error) => {}
        Err(error) if plan.key.kind() == SearchIndexKind::Vector => {
            return Err(primary_vector_error(root, error));
        }
        Err(error) => return Err(error),
    }

    let mut before = initial;
    for attempt in 1_u8..=2 {
        checkpoint()?;
        let publication = PendingPublication::new(&root)?;
        build(publication.path(), &before)?;
        checkpoint()?;
        let after = snapshot()?;
        if before != after {
            if attempt == 2 {
                return Err(SearchArtifactError::ConcurrentMutation);
            }
            before = after;
            continue;
        }
        let manifest = SearchManifest::for_key(
            plan.key,
            plan.backend_version,
            plan.contract_version,
            plan.dimension,
            &before,
            true,
        )?;
        let artifact = publication.publish(&manifest)?;
        return Ok(SearchPublicationOutcome::Published {
            artifact,
            attempts: attempt,
        });
    }
    unreachable!("the bounded publication loop returns on both terminal paths")
}

/// Coordinate an atomic update that may inspect and reuse the current artifact.
///
/// This is the primary-data counterpart to [`coordinate_search_publication`].
/// The per-key writer lock is held while `build` sees the current immutable
/// publication.  Returning [`SearchUpdateBuild::ReuseCurrent`] makes an
/// idempotent update a no-op; returning [`SearchUpdateBuild::Publish`] uses the
/// same synchronized immutable-directory and atomic-pointer protocol as a
/// derived build.  Both decisions recheck the graph snapshot, with one bounded
/// retry, so membership validation cannot race a supported graph mutation.
///
/// `build` must write only inside its supplied temporary directory.  It also
/// receives the cooperative checkpoint callback so backend work cannot publish
/// after cancellation.
///
/// # Errors
/// Distinguishes missing/corrupt primary data, cancellation, lock failure,
/// resource exhaustion, build failure, I/O, and repeated concurrent mutation.
pub fn coordinate_search_update<S, B, C>(
    project_dir: &Path,
    plan: SearchPublicationPlan<'_>,
    limits: SearchCoordinationLimits,
    mut snapshot: S,
    mut build: B,
    mut checkpoint: C,
) -> Result<SearchPublicationOutcome, SearchArtifactError>
where
    S: FnMut() -> Result<SearchSourceSnapshot, SearchArtifactError>,
    B: FnMut(
        Option<&PublishedSearchArtifact>,
        &Path,
        &SearchSourceSnapshot,
        &mut C,
    ) -> Result<SearchUpdateBuild, SearchArtifactError>,
    C: FnMut() -> Result<(), SearchArtifactError>,
{
    if plan.mode != SearchPublicationMode::Replace {
        return Err(SearchArtifactError::Build(
            "atomic search updates require replacement mode".to_owned(),
        ));
    }
    let root = plan.key.artifact_root(project_dir);
    std::fs::create_dir_all(&root).map_err(|source| io("create artifact root", &root, source))?;
    let _writer = SearchWriterLock::acquire(&root, limits, &mut checkpoint)?;
    checkpoint()?;
    let current = match current_search_artifact(project_dir, plan.key) {
        Ok(current) => current,
        Err(error) if plan.key.kind() == SearchIndexKind::Vector => {
            return Err(primary_vector_error(root, error));
        }
        Err(error) => return Err(error),
    };

    let mut before = snapshot()?;
    for attempt in 1_u8..=2 {
        checkpoint()?;
        let publication = PendingPublication::new(&root)?;
        let decision = build(
            current.as_ref(),
            publication.path(),
            &before,
            &mut checkpoint,
        )?;
        checkpoint()?;
        let after = snapshot()?;
        if before != after {
            if attempt == 2 {
                return Err(SearchArtifactError::ConcurrentMutation);
            }
            before = after;
            continue;
        }
        match decision {
            SearchUpdateBuild::ReuseCurrent => {
                return current
                    .clone()
                    .map(SearchPublicationOutcome::Reused)
                    .ok_or_else(|| {
                        SearchArtifactError::Build(
                            "update requested reuse without a current artifact".to_owned(),
                        )
                    });
            }
            SearchUpdateBuild::Publish => {
                let manifest = SearchManifest::for_key(
                    plan.key,
                    plan.backend_version,
                    plan.contract_version,
                    plan.dimension,
                    &before,
                    true,
                )?;
                let artifact = publication.publish(&manifest)?;
                return Ok(SearchPublicationOutcome::Published {
                    artifact,
                    attempts: attempt,
                });
            }
        }
    }
    unreachable!("the bounded update loop returns on both terminal paths")
}

/// Resolve and parse the currently published immutable version.
///
/// Missing `current.json` is `Ok(None)`. A torn, incompatible, incomplete, or
/// traversal-like pointer/manifest is a structured error and is never served.
///
/// # Errors
/// Returns a manifest, resource, or filesystem error.
pub fn current_search_artifact(
    project_dir: &Path,
    key: &SearchArtifactKey,
) -> Result<Option<PublishedSearchArtifact>, SearchArtifactError> {
    let root = key.artifact_root(project_dir);
    let pointer = root.join(CURRENT_FILE);
    let bytes = match std::fs::read(&pointer) {
        Ok(bytes) => bytes,
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(source) => return Err(io("read current pointer", &pointer, source)),
    };
    if bytes.len() > MAX_CURRENT_BYTES {
        return Err(SearchArtifactError::ResourceExhausted {
            resource: "current_pointer_bytes",
            limit: MAX_CURRENT_BYTES as u64,
        });
    }
    let value: serde_json::Value =
        serde_json::from_slice(&bytes).map_err(|error| SearchArtifactError::CorruptManifest {
            path: pointer.clone(),
            reason: error.to_string(),
        })?;
    let version = value
        .as_object()
        .and_then(|object| object.get("version"))
        .and_then(serde_json::Value::as_str)
        .filter(|name| valid_owned_name(name, VERSION_PREFIX))
        .ok_or_else(|| SearchArtifactError::CorruptManifest {
            path: pointer,
            reason: "expected a safe version pointer".to_owned(),
        })?;
    let artifact_dir = root.join(VERSIONS_DIR).join(version);
    let manifest_path = artifact_dir.join(MANIFEST_FILE);
    let manifest_bytes = match std::fs::read(&manifest_path) {
        Ok(bytes) => bytes,
        Err(source) if source.kind() == std::io::ErrorKind::NotFound => {
            return Err(SearchArtifactError::Missing {
                path: manifest_path,
            });
        }
        Err(source) => return Err(io("read published manifest", &manifest_path, source)),
    };
    let manifest = SearchManifest::from_json(&manifest_path, &manifest_bytes)?;
    if !manifest.completed {
        return Err(SearchArtifactError::CorruptManifest {
            path: manifest_path,
            reason: "published manifest is not completed".to_owned(),
        });
    }
    Ok(Some(PublishedSearchArtifact {
        path: artifact_dir,
        manifest,
    }))
}

/// Remove abandoned GraphForge search build directories and pointer temp files
/// after reopen.
///
/// Only the known `indexes/search/` and `embeddings/` trees are traversed.
/// Only exact GraphForge-owned names are removed; symlinks and unrecognized
/// user files are preserved. The entire bounded scan completes before any
/// deletion starts.
///
/// # Errors
/// Returns an I/O or cleanup-entry resource error. Exceeding the bound removes
/// nothing.
pub fn cleanup_abandoned_search_builds(
    project_dir: &Path,
    max_entries: usize,
) -> Result<usize, SearchArtifactError> {
    let roots = [
        project_dir.join("indexes").join("search"),
        project_dir.join("embeddings"),
    ];
    let mut stack = roots.to_vec();
    let mut inspected = 0_usize;
    let mut remove = Vec::new();
    while let Some(directory) = stack.pop() {
        let entries = match std::fs::read_dir(&directory) {
            Ok(entries) => entries,
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => continue,
            Err(source) => return Err(io("scan abandoned builds", &directory, source)),
        };
        for entry in entries {
            inspected = inspected
                .checked_add(1)
                .ok_or(SearchArtifactError::ResourceExhausted {
                    resource: "cleanup_entries",
                    limit: max_entries as u64,
                })?;
            if inspected > max_entries {
                return Err(SearchArtifactError::ResourceExhausted {
                    resource: "cleanup_entries",
                    limit: max_entries as u64,
                });
            }
            let entry = entry.map_err(|source| io("read cleanup entry", &directory, source))?;
            let file_type = entry
                .file_type()
                .map_err(|source| io("read cleanup file type", &entry.path(), source))?;
            let name = entry.file_name();
            let name = name.to_string_lossy();
            if file_type.is_dir() && valid_owned_name(&name, BUILD_PREFIX) {
                remove.push((entry.path(), true));
            } else if file_type.is_dir() && !file_type.is_symlink() {
                stack.push(entry.path());
            } else if file_type.is_file() && valid_pointer_temp(&name) {
                remove.push((entry.path(), false));
            }
        }
    }

    remove.sort_unstable_by(|left, right| right.0.cmp(&left.0));
    for (path, directory) in &remove {
        if *directory {
            std::fs::remove_dir_all(path)
                .map_err(|source| io("remove abandoned build", path, source))?;
        } else {
            std::fs::remove_file(path)
                .map_err(|source| io("remove abandoned pointer", path, source))?;
        }
    }
    Ok(remove.len())
}

struct SearchWriterLock {
    file: File,
}

impl SearchWriterLock {
    fn acquire<C>(
        root: &Path,
        limits: SearchCoordinationLimits,
        checkpoint: &mut C,
    ) -> Result<Self, SearchArtifactError>
    where
        C: FnMut() -> Result<(), SearchArtifactError>,
    {
        let path = root.join(".writer.lock");
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(&path)
            .map_err(|source| SearchArtifactError::Lock {
                path: path.clone(),
                reason: source.to_string(),
            })?;
        let started = Instant::now();
        loop {
            match file.try_lock() {
                Ok(()) => return Ok(Self { file }),
                Err(std::fs::TryLockError::WouldBlock) => {
                    checkpoint()?;
                    if started.elapsed() >= limits.lock_timeout {
                        return Err(SearchArtifactError::Lock {
                            path,
                            reason: format!(
                                "timed out after {} ms",
                                limits.lock_timeout.as_millis()
                            ),
                        });
                    }
                    std::thread::sleep(limits.lock_poll_interval);
                }
                Err(std::fs::TryLockError::Error(source)) => {
                    return Err(SearchArtifactError::Lock {
                        path,
                        reason: source.to_string(),
                    });
                }
            }
        }
    }
}

impl Drop for SearchWriterLock {
    fn drop(&mut self) {
        let _ = self.file.unlock();
    }
}

struct PendingPublication {
    root: PathBuf,
    temp: tempfile::TempDir,
}

impl PendingPublication {
    fn new(root: &Path) -> Result<Self, SearchArtifactError> {
        let temp = tempfile::Builder::new()
            .prefix(BUILD_PREFIX)
            .tempdir_in(root)
            .map_err(|source| io("create build directory", root, source))?;
        Ok(Self {
            root: root.to_path_buf(),
            temp,
        })
    }

    fn path(&self) -> &Path {
        self.temp.path()
    }

    fn publish(
        self,
        manifest: &SearchManifest,
    ) -> Result<PublishedSearchArtifact, SearchArtifactError> {
        let manifest_path = self.temp.path().join(MANIFEST_FILE);
        let manifest_bytes = manifest.to_canonical_json()?;
        write_synced_file(&manifest_path, &manifest_bytes)?;
        sync_tree(self.temp.path())?;

        let token = self
            .temp
            .path()
            .file_name()
            .and_then(|name| name.to_str())
            .and_then(|name| name.strip_prefix(BUILD_PREFIX))
            .filter(|token| {
                !token.is_empty() && token.bytes().all(|byte| byte.is_ascii_alphanumeric())
            })
            .ok_or_else(|| {
                SearchArtifactError::Build("temporary build name is invalid".to_owned())
            })?;
        let version_name = format!("{VERSION_PREFIX}{token}");
        let versions = self.root.join(VERSIONS_DIR);
        std::fs::create_dir_all(&versions)
            .map_err(|source| io("create versions directory", &versions, source))?;
        let version_path = versions.join(&version_name);
        let temp_path = self.temp.keep();
        if let Err(source) = std::fs::rename(&temp_path, &version_path) {
            let _ = std::fs::remove_dir_all(&temp_path);
            return Err(io("publish immutable version", &version_path, source));
        }
        sync_directory(&versions)?;

        let pointer = serde_json::to_vec(&serde_json::json!({ "version": version_name }))
            .map_err(|error| SearchArtifactError::Build(error.to_string()))?;
        persist_synced_pointer(&self.root.join(CURRENT_FILE), &pointer)?;
        sync_directory(&self.root)?;
        Ok(PublishedSearchArtifact {
            path: version_path,
            manifest: manifest.clone(),
        })
    }
}

fn rebuildable_metadata(error: &SearchArtifactError) -> bool {
    matches!(
        error,
        SearchArtifactError::Missing { .. }
            | SearchArtifactError::CorruptManifest { .. }
            | SearchArtifactError::CorruptDerivedIndex { .. }
            | SearchArtifactError::IncompatibleManifest { .. }
            | SearchArtifactError::Stale { .. }
            | SearchArtifactError::ResourceExhausted {
                resource: "manifest_bytes" | "current_pointer_bytes",
                ..
            }
    )
}

fn primary_vector_error(root: PathBuf, error: SearchArtifactError) -> SearchArtifactError {
    match error {
        error @ SearchArtifactError::CorruptPrimaryVectors { .. } => error,
        error => SearchArtifactError::CorruptPrimaryVectors {
            path: root,
            reason: error.to_string(),
        },
    }
}

fn write_synced_file(path: &Path, bytes: &[u8]) -> Result<(), SearchArtifactError> {
    let mut file = OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .open(path)
        .map_err(|source| io("create publication file", path, source))?;
    file.write_all(bytes)
        .map_err(|source| io("write publication file", path, source))?;
    file.sync_all()
        .map_err(|source| io("sync publication file", path, source))
}

fn persist_synced_pointer(path: &Path, bytes: &[u8]) -> Result<(), SearchArtifactError> {
    let parent = path
        .parent()
        .ok_or_else(|| SearchArtifactError::Build("current pointer has no parent".to_owned()))?;
    let mut temp = tempfile::Builder::new()
        .prefix("current.json.")
        .suffix(".tmp")
        .tempfile_in(parent)
        .map_err(|source| io("create current pointer temp", path, source))?;
    temp.write_all(bytes)
        .map_err(|source| io("write current pointer temp", path, source))?;
    temp.as_file()
        .sync_all()
        .map_err(|source| io("sync current pointer temp", path, source))?;
    temp.persist(path)
        .map_err(|error| io("publish current pointer", path, error.error))?;
    Ok(())
}

fn sync_tree(root: &Path) -> Result<(), SearchArtifactError> {
    let mut directories = vec![root.to_path_buf()];
    let mut files = Vec::new();
    let mut cursor = 0;
    while cursor < directories.len() {
        let directory = directories[cursor].clone();
        cursor += 1;
        let entries = std::fs::read_dir(&directory)
            .map_err(|source| io("scan build for sync", &directory, source))?;
        for entry in entries {
            let entry = entry.map_err(|source| io("read build entry", &directory, source))?;
            let file_type = entry
                .file_type()
                .map_err(|source| io("read build file type", &entry.path(), source))?;
            if file_type.is_symlink() {
                return Err(SearchArtifactError::Build(format!(
                    "search build must not contain symlink {}",
                    entry.path().display()
                )));
            }
            if file_type.is_dir() {
                directories.push(entry.path());
            } else if file_type.is_file() {
                files.push(entry.path());
            }
        }
    }
    files.sort_unstable();
    for path in files {
        sync_file(&path)?;
    }
    directories.sort_unstable_by_key(|path| std::cmp::Reverse(path.components().count()));
    for directory in directories {
        sync_directory(&directory)?;
    }
    Ok(())
}

fn sync_file(path: &Path) -> Result<(), SearchArtifactError> {
    OpenOptions::new()
        .read(true)
        .write(true)
        .open(path)
        .and_then(|file| file.sync_all())
        .map_err(|source| io("sync build file", path, source))
}

#[cfg(unix)]
fn sync_directory(path: &Path) -> Result<(), SearchArtifactError> {
    File::open(path)
        .and_then(|file| file.sync_all())
        .map_err(|source| io("sync directory", path, source))
}

#[cfg(not(unix))]
fn sync_directory(_path: &Path) -> Result<(), SearchArtifactError> {
    Ok(())
}

fn valid_owned_name(name: &str, prefix: &str) -> bool {
    name.strip_prefix(prefix).is_some_and(|token| {
        !token.is_empty() && token.bytes().all(|byte| byte.is_ascii_alphanumeric())
    })
}

fn valid_pointer_temp(name: &str) -> bool {
    name.strip_prefix("current.json.")
        .and_then(|name| name.strip_suffix(".tmp"))
        .is_some_and(|token| {
            !token.is_empty() && token.bytes().all(|byte| byte.is_ascii_alphanumeric())
        })
}

fn io(operation: &'static str, path: &Path, source: std::io::Error) -> SearchArtifactError {
    SearchArtifactError::Io {
        operation,
        path: path.to_path_buf(),
        source,
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::{Arc, Barrier};

    use tempfile::TempDir;

    use super::*;

    fn key() -> SearchArtifactKey {
        SearchArtifactKey::text("Person", ["name"]).unwrap()
    }

    fn snapshot(generation: u64) -> SearchSourceSnapshot {
        SearchSourceSnapshot {
            generation,
            fingerprint: format!("gf-fnv1a256:{generation:064x}"),
        }
    }

    fn plan<'a>(
        key: &'a SearchArtifactKey,
        mode: SearchPublicationMode,
    ) -> SearchPublicationPlan<'a> {
        SearchPublicationPlan {
            key,
            backend_version: "tantivy-0.25",
            contract_version: "graphforge_text_v1",
            dimension: None,
            mode,
        }
    }

    #[test]
    fn publish_is_invisible_until_current_pointer_swap() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let root = key.artifact_root(dir.path());
        std::fs::create_dir_all(&root).unwrap();
        let pending = PendingPublication::new(&root).unwrap();
        std::fs::write(pending.path().join("index"), b"complete").unwrap();
        assert!(current_search_artifact(dir.path(), &key).unwrap().is_none());

        let manifest = SearchManifest::for_key(
            &key,
            "tantivy-0.25",
            "graphforge_text_v1",
            None,
            &snapshot(1),
            true,
        )
        .unwrap();
        let published = pending.publish(&manifest).unwrap();
        assert_eq!(
            current_search_artifact(dir.path(), &key).unwrap(),
            Some(published)
        );
    }

    #[test]
    fn sync_tree_flushes_regular_files_without_mutating_contents() {
        let dir = TempDir::new().unwrap();
        let nested = dir.path().join("nested");
        std::fs::create_dir(&nested).unwrap();
        let first = dir.path().join("first");
        let second = nested.join("second");
        std::fs::write(&first, b"one").unwrap();
        std::fs::write(&second, b"two").unwrap();

        sync_tree(dir.path()).unwrap();

        assert_eq!(std::fs::read(first).unwrap(), b"one");
        assert_eq!(std::fs::read(second).unwrap(), b"two");
    }

    #[test]
    fn forced_rebuild_atomically_replaces_pointer_and_keeps_old_version() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let first = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |path, _| {
                std::fs::write(path.join("index"), b"first")
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || Ok(()),
        )
        .unwrap();
        let first_path = match first {
            SearchPublicationOutcome::Published { artifact, .. } => artifact.path,
            SearchPublicationOutcome::Reused(_) => panic!("forced build reused"),
        };
        let second = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |path, _| {
                std::fs::write(path.join("index"), b"second")
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || Ok(()),
        )
        .unwrap();
        let second_path = match second {
            SearchPublicationOutcome::Published { artifact, .. } => artifact.path,
            SearchPublicationOutcome::Reused(_) => panic!("forced build reused"),
        };
        assert_ne!(first_path, second_path);
        assert!(
            first_path.exists(),
            "old readers retain an immutable version"
        );
        assert_eq!(
            current_search_artifact(dir.path(), &key)
                .unwrap()
                .unwrap()
                .path,
            second_path
        );
    }

    #[test]
    fn failed_replacement_keeps_the_previous_publication() {
        let dir = TempDir::new().unwrap();
        let key = key();
        coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |path, _| {
                std::fs::write(path.join("index"), b"committed")
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || Ok(()),
        )
        .unwrap();
        let previous = current_search_artifact(dir.path(), &key).unwrap().unwrap();

        let error = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |path, _| {
                std::fs::write(path.join("index"), b"partial").unwrap();
                Err(SearchArtifactError::Build("injected failure".to_owned()))
            },
            || Ok(()),
        )
        .unwrap_err();
        assert!(matches!(error, SearchArtifactError::Build(_)));
        assert_eq!(
            current_search_artifact(dir.path(), &key).unwrap().unwrap(),
            previous
        );
        assert_eq!(
            std::fs::read(previous.path.join("index")).unwrap(),
            b"committed"
        );
    }

    #[test]
    fn fresh_lazy_request_reuses_without_running_builder() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let builds = AtomicUsize::new(0);
        let run = |mode| {
            coordinate_search_publication(
                dir.path(),
                plan(&key, mode),
                SearchCoordinationLimits::default(),
                || Ok(snapshot(1)),
                |_| Ok(()),
                |path, _| {
                    builds.fetch_add(1, Ordering::SeqCst);
                    std::fs::write(path.join("index"), b"data")
                        .map_err(|error| SearchArtifactError::Build(error.to_string()))
                },
                || Ok(()),
            )
        };
        assert!(matches!(
            run(SearchPublicationMode::ReuseFresh).unwrap(),
            SearchPublicationOutcome::Published { .. }
        ));
        assert!(matches!(
            run(SearchPublicationMode::ReuseFresh).unwrap(),
            SearchPublicationOutcome::Reused(_)
        ));
        assert_eq!(builds.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn corrupt_derived_backend_is_rebuilt_before_reuse() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let builds = AtomicUsize::new(0);
        let run = || {
            coordinate_search_publication(
                dir.path(),
                plan(&key, SearchPublicationMode::ReuseFresh),
                SearchCoordinationLimits::default(),
                || Ok(snapshot(1)),
                |artifact| {
                    let path = artifact.path.join("index");
                    let bytes = std::fs::read(&path).map_err(|error| {
                        SearchArtifactError::CorruptDerivedIndex {
                            path: path.clone(),
                            reason: error.to_string(),
                        }
                    })?;
                    if bytes == b"valid" {
                        Ok(())
                    } else {
                        Err(SearchArtifactError::CorruptDerivedIndex {
                            path,
                            reason: "backend validation failed".to_owned(),
                        })
                    }
                },
                |path, _| {
                    builds.fetch_add(1, Ordering::SeqCst);
                    std::fs::write(path.join("index"), b"valid")
                        .map_err(|error| SearchArtifactError::Build(error.to_string()))
                },
                || Ok(()),
            )
        };
        assert!(matches!(
            run().unwrap(),
            SearchPublicationOutcome::Published { .. }
        ));
        let current = current_search_artifact(dir.path(), &key).unwrap().unwrap();
        std::fs::write(current.path.join("index"), b"corrupt").unwrap();
        assert!(matches!(
            run().unwrap(),
            SearchPublicationOutcome::Published { .. }
        ));
        assert_eq!(builds.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn mutation_retries_once_and_second_mutation_fails_closed() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let reads = AtomicUsize::new(0);
        let outcome = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || {
                let call = reads.fetch_add(1, Ordering::SeqCst);
                Ok(snapshot(u64::from(call >= 1)))
            },
            |_| Ok(()),
            |path, source| {
                std::fs::write(path.join("source"), source.generation.to_string())
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || Ok(()),
        )
        .unwrap();
        assert!(matches!(
            outcome,
            SearchPublicationOutcome::Published { attempts: 2, .. }
        ));

        let reads = AtomicUsize::new(0);
        let error = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(reads.fetch_add(1, Ordering::SeqCst) as u64)),
            |_| Ok(()),
            |path, _| {
                std::fs::write(path.join("index"), b"data")
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || Ok(()),
        )
        .unwrap_err();
        assert!(matches!(error, SearchArtifactError::ConcurrentMutation));
    }

    #[test]
    fn atomic_update_requires_replace_and_cannot_reuse_missing_publication() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let error = coordinate_search_update(
            dir.path(),
            plan(&key, SearchPublicationMode::ReuseFresh),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_, _, _, _| Ok(SearchUpdateBuild::Publish),
            || Ok(()),
        )
        .unwrap_err();
        assert!(
            matches!(error, SearchArtifactError::Build(reason) if reason.contains("replacement mode"))
        );

        let error = coordinate_search_update(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |current, _, _, _| {
                assert!(current.is_none());
                Ok(SearchUpdateBuild::ReuseCurrent)
            },
            || Ok(()),
        )
        .unwrap_err();
        assert!(
            matches!(error, SearchArtifactError::Build(reason) if reason.contains("without a current artifact"))
        );
        assert!(current_search_artifact(dir.path(), &key).unwrap().is_none());
    }

    #[test]
    fn same_key_requests_serialize_and_share_one_lazy_build() {
        let dir = Arc::new(TempDir::new().unwrap());
        let key = Arc::new(key());
        let builds = Arc::new(AtomicUsize::new(0));
        let barrier = Arc::new(Barrier::new(2));
        let mut handles = Vec::new();
        for _ in 0..2 {
            let dir = Arc::clone(&dir);
            let key = Arc::clone(&key);
            let builds = Arc::clone(&builds);
            let barrier = Arc::clone(&barrier);
            handles.push(std::thread::spawn(move || {
                barrier.wait();
                coordinate_search_publication(
                    dir.path(),
                    plan(&key, SearchPublicationMode::ReuseFresh),
                    SearchCoordinationLimits::default(),
                    || Ok(snapshot(1)),
                    |_| Ok(()),
                    |path, _| {
                        builds.fetch_add(1, Ordering::SeqCst);
                        std::thread::sleep(Duration::from_millis(75));
                        std::fs::write(path.join("index"), b"data")
                            .map_err(|error| SearchArtifactError::Build(error.to_string()))
                    },
                    || Ok(()),
                )
            }));
        }
        let outcomes = handles
            .into_iter()
            .map(|handle| handle.join().unwrap().unwrap())
            .collect::<Vec<_>>();
        assert_eq!(builds.load(Ordering::SeqCst), 1);
        assert!(
            outcomes
                .iter()
                .any(|outcome| matches!(outcome, SearchPublicationOutcome::Reused(_)))
        );
    }

    #[test]
    fn cancellation_while_waiting_does_not_publish() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let root = key.artifact_root(dir.path());
        std::fs::create_dir_all(&root).unwrap();
        let lock =
            SearchWriterLock::acquire(&root, SearchCoordinationLimits::default(), &mut || Ok(()))
                .unwrap();
        let cancelled = AtomicBool::new(false);
        let limits = SearchCoordinationLimits {
            lock_timeout: Duration::from_secs(1),
            lock_poll_interval: Duration::from_millis(1),
            ..SearchCoordinationLimits::default()
        };
        let result = SearchWriterLock::acquire(&root, limits, &mut || {
            if cancelled.swap(true, Ordering::SeqCst) {
                Err(SearchArtifactError::Cancelled)
            } else {
                Ok(())
            }
        });
        drop(lock);
        assert!(matches!(result, Err(SearchArtifactError::Cancelled)));
        assert!(current_search_artifact(dir.path(), &key).unwrap().is_none());
    }

    #[test]
    fn cancellation_after_build_does_not_publish_partial_output() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let checkpoints = AtomicUsize::new(0);
        let error = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::Replace),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |path, _| {
                std::fs::write(path.join("index"), b"partial")
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || {
                if checkpoints.fetch_add(1, Ordering::SeqCst) >= 2 {
                    Err(SearchArtifactError::Cancelled)
                } else {
                    Ok(())
                }
            },
        )
        .unwrap_err();
        assert!(matches!(error, SearchArtifactError::Cancelled));
        assert!(current_search_artifact(dir.path(), &key).unwrap().is_none());
    }

    #[test]
    fn cleanup_is_bounded_scoped_and_preserves_unknown_files() {
        let dir = TempDir::new().unwrap();
        let search = dir.path().join("indexes/search/text/key");
        let embeddings = dir.path().join("embeddings/space/key");
        let notes = dir.path().join("notes");
        for path in [&search, &embeddings, &notes] {
            std::fs::create_dir_all(path).unwrap();
        }
        let stale = [
            search.join(".build-Abc123"),
            embeddings.join(".build-Xyz789"),
        ];
        for path in &stale {
            std::fs::create_dir_all(path).unwrap();
            std::fs::write(path.join("partial"), b"x").unwrap();
        }
        let pointer_temp = search.join("current.json.Qwe456.tmp");
        std::fs::write(&pointer_temp, b"x").unwrap();
        let preserved = [
            search.join(".build-bad-name"),
            embeddings.join("vectors.parquet"),
            notes.join(".build-Abc123"),
        ];
        for path in &preserved {
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent).unwrap();
            }
            std::fs::write(path, b"keep").unwrap();
        }

        assert!(matches!(
            cleanup_abandoned_search_builds(dir.path(), 1),
            Err(SearchArtifactError::ResourceExhausted {
                resource: "cleanup_entries",
                ..
            })
        ));
        assert!(stale.iter().all(|path| path.exists()));
        assert_eq!(cleanup_abandoned_search_builds(dir.path(), 100).unwrap(), 3);
        assert!(stale.iter().all(|path| !path.exists()));
        assert!(!pointer_temp.exists());
        assert!(preserved.iter().all(|path| path.exists()));
    }

    #[test]
    fn missing_published_text_manifest_is_rebuilt() {
        let dir = TempDir::new().unwrap();
        let key = key();
        let root = key.artifact_root(dir.path());
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join(CURRENT_FILE), br#"{"version":"version-Abc123"}"#).unwrap();
        let builds = AtomicUsize::new(0);
        let outcome = coordinate_search_publication(
            dir.path(),
            plan(&key, SearchPublicationMode::ReuseFresh),
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |path, _| {
                builds.fetch_add(1, Ordering::SeqCst);
                std::fs::write(path.join("index"), b"rebuilt")
                    .map_err(|error| SearchArtifactError::Build(error.to_string()))
            },
            || Ok(()),
        )
        .unwrap();
        assert!(matches!(
            outcome,
            SearchPublicationOutcome::Published { attempts: 1, .. }
        ));
        assert_eq!(builds.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn corrupt_and_incompatible_text_manifests_are_rebuilt() {
        for incompatible in [false, true] {
            let dir = TempDir::new().unwrap();
            let key = key();
            let builds = AtomicUsize::new(0);
            let run = || {
                coordinate_search_publication(
                    dir.path(),
                    plan(&key, SearchPublicationMode::ReuseFresh),
                    SearchCoordinationLimits::default(),
                    || Ok(snapshot(1)),
                    |_| Ok(()),
                    |path, _| {
                        builds.fetch_add(1, Ordering::SeqCst);
                        std::fs::write(path.join("index"), b"complete")
                            .map_err(|error| SearchArtifactError::Build(error.to_string()))
                    },
                    || Ok(()),
                )
            };
            run().unwrap();
            let current = current_search_artifact(dir.path(), &key).unwrap().unwrap();
            let manifest_path = current.path.join(MANIFEST_FILE);
            if incompatible {
                let mut manifest: serde_json::Value =
                    serde_json::from_slice(&std::fs::read(&manifest_path).unwrap()).unwrap();
                manifest["manifest_version"] = serde_json::Value::from(99);
                std::fs::write(&manifest_path, serde_json::to_vec(&manifest).unwrap()).unwrap();
            } else {
                std::fs::write(&manifest_path, b"corrupt").unwrap();
            }

            assert!(matches!(
                run().unwrap(),
                SearchPublicationOutcome::Published { attempts: 1, .. }
            ));
            assert_eq!(builds.load(Ordering::SeqCst), 2);
        }
    }

    #[test]
    fn corrupt_vector_metadata_is_not_discarded() {
        let dir = TempDir::new().unwrap();
        let key = SearchArtifactKey::vector("Person", "semantic").unwrap();
        let root = key.artifact_root(dir.path());
        std::fs::create_dir_all(&root).unwrap();
        std::fs::write(root.join(CURRENT_FILE), b"corrupt").unwrap();
        let error = coordinate_search_publication(
            dir.path(),
            SearchPublicationPlan {
                key: &key,
                backend_version: "exact-cosine-v1",
                contract_version: "vector-v1",
                dimension: Some(3),
                mode: SearchPublicationMode::Replace,
            },
            SearchCoordinationLimits::default(),
            || Ok(snapshot(1)),
            |_| Ok(()),
            |_, _| Ok(()),
            || Ok(()),
        )
        .unwrap_err();
        assert!(matches!(
            error,
            SearchArtifactError::CorruptPrimaryVectors { .. }
        ));
        assert_eq!(std::fs::read(root.join(CURRENT_FILE)).unwrap(), b"corrupt");
    }

    #[test]
    fn current_pointer_malformed_state_matrix_is_exact_and_non_mutating() {
        let key = key();
        let cases: Vec<Vec<u8>> = vec![
            b"not-json".to_vec(),
            br#"{}"#.to_vec(),
            br#"{"version":"../escape"}"#.to_vec(),
            br#"{"version":"version-bad/slash"}"#.to_vec(),
            vec![b'x'; MAX_CURRENT_BYTES + 1],
        ];
        for bytes in cases {
            let dir = TempDir::new().unwrap();
            let root = key.artifact_root(dir.path());
            std::fs::create_dir_all(&root).unwrap();
            let pointer = root.join(CURRENT_FILE);
            std::fs::write(&pointer, &bytes).unwrap();
            let result = current_search_artifact(dir.path(), &key);
            assert!(matches!(
                result,
                Err(SearchArtifactError::CorruptManifest { .. })
                    | Err(SearchArtifactError::ResourceExhausted {
                        resource: "current_pointer_bytes",
                        ..
                    })
            ));
            assert_eq!(std::fs::read(&pointer).unwrap(), bytes);
            assert!(!root.join(VERSIONS_DIR).exists());
        }
    }

    #[test]
    fn rebuildability_primary_wrapping_and_owned_name_matrices_are_total() {
        let path = PathBuf::from("artifact");
        let rebuildable = [
            SearchArtifactError::Missing { path: path.clone() },
            SearchArtifactError::CorruptManifest {
                path: path.clone(),
                reason: "bad".into(),
            },
            SearchArtifactError::CorruptDerivedIndex {
                path: path.clone(),
                reason: "bad".into(),
            },
            SearchArtifactError::IncompatibleManifest {
                path: path.clone(),
                found: 2,
                supported: 1,
            },
            SearchArtifactError::Stale {
                reason: "old".into(),
            },
            SearchArtifactError::ResourceExhausted {
                resource: "manifest_bytes",
                limit: 1,
            },
            SearchArtifactError::ResourceExhausted {
                resource: "current_pointer_bytes",
                limit: 1,
            },
        ];
        for error in &rebuildable {
            assert!(rebuildable_metadata(error), "{error}");
        }
        for error in [
            SearchArtifactError::Cancelled,
            SearchArtifactError::ConcurrentMutation,
            SearchArtifactError::ResourceExhausted {
                resource: "other",
                limit: 1,
            },
            SearchArtifactError::Build("bad".into()),
        ] {
            assert!(!rebuildable_metadata(&error), "{error}");
        }

        let primary = SearchArtifactError::CorruptPrimaryVectors {
            path: path.clone(),
            reason: "primary".into(),
        };
        assert!(matches!(
            primary_vector_error(path.clone(), primary),
            SearchArtifactError::CorruptPrimaryVectors { reason, .. } if reason == "primary"
        ));
        assert!(matches!(
            primary_vector_error(path.clone(), SearchArtifactError::Cancelled),
            SearchArtifactError::CorruptPrimaryVectors { path: actual, reason }
                if actual == path && reason.contains("cancelled")
        ));

        for valid in ["build-A1", "version-z9"] {
            let prefix = if valid.starts_with("build") {
                "build-"
            } else {
                "version-"
            };
            assert!(valid_owned_name(valid, prefix));
        }
        for invalid in ["build-", "build-a/b", "build-a_b", "other-a"] {
            assert!(!valid_owned_name(invalid, "build-"));
        }
        for (name, expected) in [
            ("current.json.A1.tmp", true),
            ("current.json..tmp", false),
            ("current.json.a_b.tmp", false),
            ("current.json.a", false),
        ] {
            assert_eq!(valid_pointer_temp(name), expected);
        }
    }

    #[cfg(unix)]
    #[test]
    fn syncing_build_with_symlink_fails_without_following_or_mutating_target() {
        use std::os::unix::fs::symlink;

        let build = TempDir::new().unwrap();
        let external = TempDir::new().unwrap();
        let target = external.path().join("secret");
        std::fs::write(&target, b"caller bytes").unwrap();
        let link = build.path().join("linked");
        symlink(&target, &link).unwrap();
        assert!(matches!(
            sync_tree(build.path()),
            Err(SearchArtifactError::Build(_))
        ));
        assert_eq!(std::fs::read(&target).unwrap(), b"caller bytes");
        assert!(link.symlink_metadata().unwrap().file_type().is_symlink());
    }

    #[test]
    fn wave10_writer_timeout_and_cleanup_bounds_are_fail_closed() {
        let project = TempDir::new().unwrap();
        let root = key().artifact_root(project.path());
        std::fs::create_dir_all(&root).unwrap();
        let first =
            SearchWriterLock::acquire(&root, SearchCoordinationLimits::default(), &mut || Ok(()))
                .unwrap();
        let zero_wait = SearchCoordinationLimits {
            lock_timeout: Duration::ZERO,
            lock_poll_interval: Duration::ZERO,
            ..SearchCoordinationLimits::default()
        };
        assert!(matches!(
            SearchWriterLock::acquire(&root, zero_wait, &mut || Ok(())),
            Err(SearchArtifactError::Lock { .. })
        ));
        drop(first);

        let cleanup = project.path().join("indexes/search/owned");
        std::fs::create_dir_all(&cleanup).unwrap();
        std::fs::write(cleanup.join("caller"), b"preserve").unwrap();
        assert!(matches!(
            cleanup_abandoned_search_builds(project.path(), 0),
            Err(SearchArtifactError::ResourceExhausted {
                resource: "cleanup_entries",
                ..
            })
        ));
        assert_eq!(std::fs::read(cleanup.join("caller")).unwrap(), b"preserve");
    }
}