frankensearch-storage 0.2.1

FrankenSQLite-backed metadata and embedding job storage for frankensearch
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
//! Index metadata persistence for tracking built indices, build history,
//! and staleness signals.
//!
//! Provides the storage layer for bd-3w1.11: every index build is recorded
//! with its embedder, dimension, timing, and optional FEC sidecar info.
//! Build history is retained (last 100 per index) for staleness detection
//! and KL drift analysis.

use std::io;
use std::time::{SystemTime, UNIX_EPOCH};

use frankensearch_core::{SearchError, SearchResult};
use fsqlite::{AsyncConnection, Row};
use fsqlite_types::value::SqliteValue;
use serde::{Deserialize, Serialize};

use crate::connection::{Storage, map_storage_error, retry_transient_storage};

/// What triggered an index build.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BuildTrigger {
    /// First-time build on startup.
    Initial,
    /// Scheduled periodic rebuild.
    Scheduled,
    /// Content changes detected (new/modified documents).
    ContentChanged,
    /// Explicit user or API request.
    Manual,
    /// Embedder model or config changed.
    ConfigChanged,
    /// Repair pipeline triggered a rebuild.
    Repair,
}

impl BuildTrigger {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Initial => "initial",
            Self::Scheduled => "scheduled",
            Self::ContentChanged => "content_changed",
            Self::Manual => "manual",
            Self::ConfigChanged => "config_changed",
            Self::Repair => "repair",
        }
    }

    fn from_str(s: &str) -> Option<Self> {
        match s {
            "initial" => Some(Self::Initial),
            "scheduled" => Some(Self::Scheduled),
            "content_changed" => Some(Self::ContentChanged),
            "manual" => Some(Self::Manual),
            "config_changed" => Some(Self::ConfigChanged),
            "repair" => Some(Self::Repair),
            _ => None,
        }
    }
}

/// Why an index is considered stale.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StalenessReason {
    /// Documents have been modified since last build.
    ContentChanged,
    /// New documents were added since last build.
    NewDocuments,
    /// Index has never been built.
    NeverBuilt,
    /// Embedder revision changed since last build.
    EmbedderChanged,
    /// Index age exceeded the configured maximum.
    AgeExceeded,
    /// Storage schema version differs from the index build schema version.
    SchemaChanged,
}

/// Result of a staleness check for a given index.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StalenessCheck {
    pub index_name: String,
    pub is_stale: bool,
    pub reasons: Vec<StalenessReason>,
    /// Number of documents modified since last build.
    pub docs_modified: i64,
    /// Number of new documents since last build.
    pub docs_added: i64,
    /// Timestamp of the last build (None if never built).
    pub last_built_at: Option<i64>,
}

/// Metadata about a single index.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IndexMetadata {
    pub index_name: String,
    pub index_type: String,
    pub embedder_id: String,
    pub embedder_revision: Option<String>,
    pub dimension: i64,
    pub record_count: i64,
    pub file_path: Option<String>,
    pub file_size_bytes: Option<i64>,
    pub file_hash: Option<String>,
    pub schema_version: Option<i64>,
    pub built_at: Option<i64>,
    pub build_duration_ms: Option<i64>,
    pub source_doc_count: i64,
    pub config_json: Option<String>,
    pub fec_path: Option<String>,
    pub fec_size_bytes: Option<i64>,
    pub last_verified_at: Option<i64>,
    pub last_repair_at: Option<i64>,
    pub repair_count: i64,
    pub mean_norm: Option<f64>,
    pub variance: Option<f64>,
}

/// A single entry in the build history log.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IndexBuildRecord {
    pub build_id: i64,
    pub index_name: String,
    pub built_at: i64,
    pub build_duration_ms: i64,
    pub record_count: i64,
    pub source_doc_count: i64,
    pub trigger: BuildTrigger,
    pub config_json: Option<String>,
    pub notes: Option<String>,
    pub mean_norm: Option<f64>,
    pub variance: Option<f64>,
}

/// Parameters for recording an index build.
#[derive(Debug, Clone)]
pub struct RecordBuildParams {
    pub index_name: String,
    pub index_type: String,
    pub embedder_id: String,
    pub embedder_revision: Option<String>,
    pub dimension: i64,
    pub record_count: i64,
    pub source_doc_count: i64,
    pub build_duration_ms: i64,
    pub trigger: BuildTrigger,
    pub file_path: Option<String>,
    pub file_size_bytes: Option<i64>,
    pub file_hash: Option<String>,
    pub schema_version: Option<i64>,
    pub config_json: Option<String>,
    pub fec_path: Option<String>,
    pub fec_size_bytes: Option<i64>,
    pub notes: Option<String>,
    pub mean_norm: Option<f64>,
    pub variance: Option<f64>,
}

// ─── Storage methods ────────────────────────────────────────────────────────

impl Storage {
    /// Record a completed index build. Upserts the `index_metadata` row and
    /// appends to `index_build_history`. Retains at most 100 history entries
    /// per index (oldest are pruned).
    pub fn record_index_build(&self, params: &RecordBuildParams) -> SearchResult<()> {
        ensure_non_empty(&params.index_name, "index_name")?;
        ensure_non_empty(&params.index_type, "index_type")?;
        ensure_non_empty(&params.embedder_id, "embedder_id")?;

        let built_at = unix_timestamp_ms()?;

        self.transaction(|conn| {
            upsert_index_metadata(conn, params, built_at)?;
            insert_build_history(conn, params, built_at)?;
            prune_build_history(conn, &params.index_name, 100)?;
            Ok(())
        })?;

        tracing::debug!(
            target: "frankensearch.storage",
            op = "record_index_build",
            index_name = %params.index_name,
            index_type = %params.index_type,
            embedder_id = %params.embedder_id,
            record_count = params.record_count,
            trigger = params.trigger.as_str(),
            "index build recorded"
        );

        Ok(())
    }

    /// Fetch metadata for a specific index by name.
    pub fn get_index_metadata(&self, index_name: &str) -> SearchResult<Option<IndexMetadata>> {
        ensure_non_empty(index_name, "index_name")?;

        let params = [SqliteValue::Text(index_name.to_owned().into())];
        let rows = retry_transient_storage(
            || {
                self.connection()
                    .query_with_params_sync(
                        "SELECT index_name, index_type, embedder_id, embedder_revision, \
            dimension, record_count, file_path, file_size_bytes, file_hash, \
            schema_version, built_at, build_duration_ms, source_doc_count, \
            config_json, fec_path, fec_size_bytes, last_verified_at, \
            last_repair_at, repair_count, mean_norm, variance \
         FROM index_metadata WHERE index_name = ?1 LIMIT 1;",
                        &params,
                    )
                    .map_err(map_storage_error)
            },
            "index metadata fetch",
        )?;

        let Some(row) = rows.first() else {
            return Ok(None);
        };

        Ok(Some(parse_index_metadata(row)?))
    }

    /// List all known index metadata entries.
    pub fn list_index_metadata(&self) -> SearchResult<Vec<IndexMetadata>> {
        let rows = retry_transient_storage(
            || {
                self.connection()
                    .query_sync(
                        "SELECT index_name, index_type, embedder_id, embedder_revision, \
            dimension, record_count, file_path, file_size_bytes, file_hash, \
            schema_version, built_at, build_duration_ms, source_doc_count, \
            config_json, fec_path, fec_size_bytes, last_verified_at, \
            last_repair_at, repair_count, mean_norm, variance \
         FROM index_metadata ORDER BY index_name;",
                    )
                    .map_err(map_storage_error)
            },
            "index metadata list",
        )?;

        let mut result = Vec::with_capacity(rows.len());
        for row in &rows {
            result.push(parse_index_metadata(row)?);
        }
        Ok(result)
    }

    /// Check whether an index is stale by comparing document timestamps
    /// and embedder revision against the stored metadata.
    pub fn check_index_staleness(
        &self,
        index_name: &str,
        current_embedder_revision: Option<&str>,
    ) -> SearchResult<StalenessCheck> {
        ensure_non_empty(index_name, "index_name")?;

        let meta = self.get_index_metadata(index_name)?;

        let Some(meta) = meta else {
            return Ok(StalenessCheck {
                index_name: index_name.to_owned(),
                is_stale: true,
                reasons: vec![StalenessReason::NeverBuilt],
                docs_modified: 0,
                docs_added: 0,
                last_built_at: None,
            });
        };

        let mut reasons = Vec::new();
        let built_at = meta.built_at.unwrap_or(0);

        // Check for modified documents since last build.
        let current_docs = count_total_documents(self.connection())?;
        let docs_removed = if current_docs < meta.source_doc_count {
            meta.source_doc_count - current_docs
        } else {
            0
        };
        let docs_modified =
            count_modified_since(self.connection(), built_at)?.saturating_add(docs_removed);
        if docs_modified > 0 {
            reasons.push(StalenessReason::ContentChanged);
        }

        // Check for new documents since last build.
        let docs_added = count_added_since(self.connection(), built_at)?;
        if docs_added > 0 {
            reasons.push(StalenessReason::NewDocuments);
        }

        // Check embedder revision change.
        if let Some(current_rev) = current_embedder_revision {
            match &meta.embedder_revision {
                Some(stored_rev) if stored_rev != current_rev => {
                    reasons.push(StalenessReason::EmbedderChanged);
                }
                None => {
                    // No stored revision implies unknown — treat as changed.
                    reasons.push(StalenessReason::EmbedderChanged);
                }
                _ => {}
            }
        }

        Ok(StalenessCheck {
            index_name: index_name.to_owned(),
            is_stale: !reasons.is_empty(),
            reasons,
            docs_modified,
            docs_added,
            last_built_at: meta.built_at,
        })
    }

    /// Record a successful integrity verification of an index file.
    pub fn record_verification(&self, index_name: &str) -> SearchResult<bool> {
        ensure_non_empty(index_name, "index_name")?;

        let now = unix_timestamp_ms()?;
        let params = [
            SqliteValue::Integer(now),
            SqliteValue::Text(index_name.to_owned().into()),
        ];

        let affected = retry_transient_storage(
            || {
                self.connection()
                    .execute_with_params_sync(
                        "UPDATE index_metadata SET last_verified_at = ?1 WHERE index_name = ?2;",
                        &params,
                    )
                    .map_err(map_storage_error)
            },
            "index verification stamp",
        )?;

        tracing::debug!(
            target: "frankensearch.storage",
            op = "record_verification",
            index_name,
            updated = affected > 0,
            "index verification recorded"
        );

        Ok(affected > 0)
    }

    /// Record a successful repair of an index file.
    pub fn record_repair(&self, index_name: &str) -> SearchResult<bool> {
        ensure_non_empty(index_name, "index_name")?;

        let now = unix_timestamp_ms()?;
        let params = [
            SqliteValue::Integer(now),
            SqliteValue::Text(index_name.to_owned().into()),
        ];

        let affected = retry_transient_storage(
            || {
                self.connection()
                    .execute_with_params_sync(
                        "UPDATE index_metadata SET last_repair_at = ?1, \
            repair_count = repair_count + 1 \
         WHERE index_name = ?2;",
                        &params,
                    )
                    .map_err(map_storage_error)
            },
            "index repair stamp",
        )?;

        tracing::debug!(
            target: "frankensearch.storage",
            op = "record_repair",
            index_name,
            updated = affected > 0,
            "index repair recorded"
        );

        Ok(affected > 0)
    }

    /// Fetch the build history for an index, most recent first.
    pub fn get_build_history(
        &self,
        index_name: &str,
        limit: usize,
    ) -> SearchResult<Vec<IndexBuildRecord>> {
        ensure_non_empty(index_name, "index_name")?;
        if limit == 0 {
            return Ok(Vec::new());
        }

        // NOTE: Uses format!() with sql_escape_single_quoted instead of
        // query_with_params because FrankenSQLite's query_with_params
        // currently returns at most one row for multi-row result sets.
        let escaped_index = sql_escape_single_quoted(index_name);
        let sql = format!(
            "SELECT build_id, index_name, built_at, build_duration_ms, \
                record_count, source_doc_count, \"trigger\", config_json, notes, \
                mean_norm, variance \
             FROM index_build_history \
             WHERE index_name = '{escaped_index}' \
             ORDER BY built_at DESC, build_id DESC;"
        );
        let rows = retry_transient_storage(
            || {
                self.connection()
                    .query_sync(&sql)
                    .map_err(map_storage_error)
            },
            "index build history",
        )?;

        let mut history = Vec::with_capacity(rows.len());
        for row in &rows {
            history.push(parse_build_record(row)?);
        }
        if history.len() > limit {
            history.truncate(limit);
        }

        tracing::debug!(
            target: "frankensearch.storage",
            op = "get_build_history",
            index_name,
            limit,
            count = history.len(),
            "build history query completed"
        );

        Ok(history)
    }

    /// Delete metadata and build history for an index.
    pub fn delete_index_metadata(&self, index_name: &str) -> SearchResult<bool> {
        ensure_non_empty(index_name, "index_name")?;

        let (history_deleted, metadata_deleted) = self.transaction(|conn| {
            let params = [SqliteValue::Text(index_name.to_owned().into())];
            let history_deleted = conn
                .execute_with_params_sync(
                    "DELETE FROM index_build_history WHERE index_name = ?1;",
                    &params,
                )
                .map_err(map_storage_error)?;
            let metadata_deleted = conn
                .execute_with_params_sync(
                    "DELETE FROM index_metadata WHERE index_name = ?1;",
                    &params,
                )
                .map_err(map_storage_error)?;
            Ok((history_deleted, metadata_deleted))
        })?;
        let deleted_any = history_deleted > 0 || metadata_deleted > 0;

        tracing::debug!(
            target: "frankensearch.storage",
            op = "delete_index_metadata",
            index_name,
            history_deleted,
            metadata_deleted,
            deleted = deleted_any,
            "index metadata deleted"
        );

        Ok(deleted_any)
    }
}

// ─── Internal helpers ───────────────────────────────────────────────────────

fn upsert_index_metadata(
    conn: &AsyncConnection,
    params: &RecordBuildParams,
    built_at: i64,
) -> SearchResult<()> {
    let update_params = [
        SqliteValue::Text(params.index_type.clone().into()),
        SqliteValue::Text(params.embedder_id.clone().into()),
        sqlite_text_opt(params.embedder_revision.as_deref()),
        SqliteValue::Integer(params.dimension),
        SqliteValue::Integer(params.record_count),
        sqlite_text_opt(params.file_path.as_deref()),
        sqlite_i64_opt(params.file_size_bytes),
        sqlite_text_opt(params.file_hash.as_deref()),
        sqlite_i64_opt(params.schema_version),
        SqliteValue::Integer(built_at),
        SqliteValue::Integer(params.build_duration_ms),
        SqliteValue::Integer(params.source_doc_count),
        sqlite_text_opt(params.config_json.as_deref()),
        sqlite_text_opt(params.fec_path.as_deref()),
        sqlite_i64_opt(params.fec_size_bytes),
        sqlite_f64_opt(params.mean_norm),
        sqlite_f64_opt(params.variance),
        SqliteValue::Text(params.index_name.clone().into()),
    ];

    let updated = conn
        .execute_with_params_sync(
            "UPDATE index_metadata SET \
        index_type = ?1, \
        embedder_id = ?2, \
        embedder_revision = ?3, \
        dimension = ?4, \
        record_count = ?5, \
        file_path = ?6, \
        file_size_bytes = ?7, \
        file_hash = ?8, \
        schema_version = ?9, \
        built_at = ?10, \
        build_duration_ms = ?11, \
        source_doc_count = ?12, \
        config_json = ?13, \
        fec_path = ?14, \
        fec_size_bytes = ?15, \
        mean_norm = ?16, \
        variance = ?17 \
     WHERE index_name = ?18;",
            &update_params,
        )
        .map_err(map_storage_error)?;
    if updated > 0 {
        return Ok(());
    }

    let insert_params = [
        SqliteValue::Text(params.index_name.clone().into()),
        SqliteValue::Text(params.index_type.clone().into()),
        SqliteValue::Text(params.embedder_id.clone().into()),
        sqlite_text_opt(params.embedder_revision.as_deref()),
        SqliteValue::Integer(params.dimension),
        SqliteValue::Integer(params.record_count),
        sqlite_text_opt(params.file_path.as_deref()),
        sqlite_i64_opt(params.file_size_bytes),
        sqlite_text_opt(params.file_hash.as_deref()),
        sqlite_i64_opt(params.schema_version),
        SqliteValue::Integer(built_at),
        SqliteValue::Integer(params.build_duration_ms),
        SqliteValue::Integer(params.source_doc_count),
        sqlite_text_opt(params.config_json.as_deref()),
        sqlite_text_opt(params.fec_path.as_deref()),
        sqlite_i64_opt(params.fec_size_bytes),
        SqliteValue::Null,
        SqliteValue::Null,
        SqliteValue::Integer(0),
        sqlite_f64_opt(params.mean_norm),
        sqlite_f64_opt(params.variance),
    ];

    conn.execute_with_params_sync(
        "INSERT INTO index_metadata (\
        index_name, index_type, embedder_id, embedder_revision, \
        dimension, record_count, file_path, file_size_bytes, file_hash, \
        schema_version, built_at, build_duration_ms, source_doc_count, \
        config_json, fec_path, fec_size_bytes, last_verified_at, \
        last_repair_at, repair_count, mean_norm, variance\
     ) VALUES (\
        ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, \
        ?15, ?16, ?17, ?18, ?19, ?20, ?21\
     );",
        &insert_params,
    )
    .map_err(map_storage_error)?;

    Ok(())
}

fn insert_build_history(
    conn: &AsyncConnection,
    params: &RecordBuildParams,
    built_at: i64,
) -> SearchResult<()> {
    let sql_params = [
        SqliteValue::Text(params.index_name.clone().into()),
        SqliteValue::Integer(built_at),
        SqliteValue::Integer(params.build_duration_ms),
        SqliteValue::Integer(params.record_count),
        SqliteValue::Integer(params.source_doc_count),
        SqliteValue::Text(params.trigger.as_str().to_owned().into()),
        sqlite_text_opt(params.config_json.as_deref()),
        sqlite_text_opt(params.notes.as_deref()),
        sqlite_f64_opt(params.mean_norm),
        sqlite_f64_opt(params.variance),
    ];

    conn.execute_with_params_sync(
        "INSERT INTO index_build_history (\
        index_name, built_at, build_duration_ms, record_count, \
        source_doc_count, \"trigger\", config_json, notes, mean_norm, variance\
     ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10);",
        &sql_params,
    )
    .map_err(map_storage_error)?;

    Ok(())
}

fn prune_build_history(conn: &AsyncConnection, index_name: &str, keep: i64) -> SearchResult<()> {
    let escaped_index = sql_escape_single_quoted(index_name);

    if keep <= 0 {
        conn.execute_sync(&format!(
            "DELETE FROM index_build_history WHERE index_name = '{escaped_index}';"
        ))
        .map_err(map_storage_error)?;
        return Ok(());
    }
    let keep = usize::try_from(keep).map_err(|error| SearchError::SubsystemError {
        subsystem: "storage",
        source: Box::new(io::Error::other(format!(
            "invalid keep value for prune_build_history: {error}"
        ))),
    })?;

    // NOTE: Uses format!() because query_with_params returns at most one
    // row for multi-row result sets (FrankenSQLite limitation).
    let rows = conn
        .query_sync(&format!(
            "SELECT build_id FROM index_build_history \
         WHERE index_name = '{escaped_index}' \
         ORDER BY build_id DESC;"
        ))
        .map_err(map_storage_error)?;
    if rows.len() <= keep {
        return Ok(());
    }

    let cutoff_row = &rows[keep - 1];
    let cutoff = row_i64(cutoff_row, 0, "index_build_history.build_id")?;

    conn.execute_sync(&format!(
        "DELETE FROM index_build_history \
         WHERE index_name = '{escaped_index}' \
           AND build_id < {cutoff};"
    ))
    .map_err(map_storage_error)?;
    Ok(())
}

fn count_modified_since(conn: &AsyncConnection, since: i64) -> SearchResult<i64> {
    let params = [SqliteValue::Integer(since), SqliteValue::Integer(since)];
    let rows = retry_transient_storage(
        || {
            conn.query_with_params_sync(
                "SELECT COUNT(*) FROM documents WHERE updated_at > ?1 AND created_at <= ?2;",
                &params,
            )
            .map_err(map_storage_error)
        },
        "index staleness modified count",
    )?;
    let Some(row) = rows.first() else {
        return Ok(0);
    };
    row_i64(row, 0, "count_modified")
}

fn count_added_since(conn: &AsyncConnection, since: i64) -> SearchResult<i64> {
    let params = [SqliteValue::Integer(since)];
    let rows = retry_transient_storage(
        || {
            conn.query_with_params_sync(
                "SELECT COUNT(*) FROM documents WHERE created_at > ?1;",
                &params,
            )
            .map_err(map_storage_error)
        },
        "index staleness added count",
    )?;
    let Some(row) = rows.first() else {
        return Ok(0);
    };
    row_i64(row, 0, "count_added")
}

fn count_total_documents(conn: &AsyncConnection) -> SearchResult<i64> {
    let rows = retry_transient_storage(
        || {
            conn.query_sync("SELECT COUNT(*) FROM documents;")
                .map_err(map_storage_error)
        },
        "index staleness document count",
    )?;
    let row = rows.first().ok_or_else(|| SearchError::SubsystemError {
        subsystem: "storage",
        source: Box::new(std::io::Error::other(
            "documents count query returned no row",
        )),
    })?;
    row_i64(row, 0, "documents.count")
}

fn parse_index_metadata(row: &Row) -> SearchResult<IndexMetadata> {
    Ok(IndexMetadata {
        index_name: row_text(row, 0, "index_metadata.index_name")?.to_owned(),
        index_type: row_text(row, 1, "index_metadata.index_type")?.to_owned(),
        embedder_id: row_text(row, 2, "index_metadata.embedder_id")?.to_owned(),
        embedder_revision: row_optional_text(row, 3)?,
        dimension: row_i64(row, 4, "index_metadata.dimension")?,
        record_count: row_i64(row, 5, "index_metadata.record_count")?,
        file_path: row_optional_text(row, 6)?,
        file_size_bytes: row_optional_i64(row, 7)?,
        file_hash: row_optional_text(row, 8)?,
        schema_version: row_optional_i64(row, 9)?,
        built_at: row_optional_i64(row, 10)?,
        build_duration_ms: row_optional_i64(row, 11)?,
        source_doc_count: row_i64(row, 12, "index_metadata.source_doc_count")?,
        config_json: row_optional_text(row, 13)?,
        fec_path: row_optional_text(row, 14)?,
        fec_size_bytes: row_optional_i64(row, 15)?,
        last_verified_at: row_optional_i64(row, 16)?,
        last_repair_at: row_optional_i64(row, 17)?,
        repair_count: row_i64(row, 18, "index_metadata.repair_count")?,
        mean_norm: row_optional_f64(row, 19)?,
        variance: row_optional_f64(row, 20)?,
    })
}

fn parse_build_record(row: &Row) -> SearchResult<IndexBuildRecord> {
    let trigger_str = row_text(row, 6, "index_build_history.trigger")?;
    let trigger =
        BuildTrigger::from_str(trigger_str).ok_or_else(|| SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!(
                "unknown build trigger: {trigger_str}"
            ))),
        })?;

    Ok(IndexBuildRecord {
        build_id: row_i64(row, 0, "index_build_history.build_id")?,
        index_name: row_text(row, 1, "index_build_history.index_name")?.to_owned(),
        built_at: row_i64(row, 2, "index_build_history.built_at")?,
        build_duration_ms: row_i64(row, 3, "index_build_history.build_duration_ms")?,
        record_count: row_i64(row, 4, "index_build_history.record_count")?,
        source_doc_count: row_i64(row, 5, "index_build_history.source_doc_count")?,
        trigger,
        config_json: row_optional_text(row, 7)?,
        notes: row_optional_text(row, 8)?,
        mean_norm: row_optional_f64(row, 9)?,
        variance: row_optional_f64(row, 10)?,
    })
}

// ─── Row extraction helpers ─────────────────────────────────────────────────

fn row_text<'a>(row: &'a Row, index: usize, field: &str) -> SearchResult<&'a str> {
    match row.get(index) {
        Some(SqliteValue::Text(value)) => Ok(value),
        Some(other) => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!(
                "unexpected type for {field}: {other:?}"
            ))),
        }),
        None => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!("missing column for {field}"))),
        }),
    }
}

fn row_i64(row: &Row, index: usize, field: &str) -> SearchResult<i64> {
    match row.get(index) {
        Some(SqliteValue::Integer(value)) => Ok(*value),
        Some(other) => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!(
                "unexpected type for {field}: {other:?}"
            ))),
        }),
        None => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!("missing column for {field}"))),
        }),
    }
}

fn row_optional_text(row: &Row, index: usize) -> SearchResult<Option<String>> {
    match row.get(index) {
        Some(SqliteValue::Text(value)) => Ok(Some(value.to_string())),
        Some(SqliteValue::Null) | None => Ok(None),
        Some(other) => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!(
                "unexpected optional text type: {other:?}"
            ))),
        }),
    }
}

fn row_optional_i64(row: &Row, index: usize) -> SearchResult<Option<i64>> {
    match row.get(index) {
        Some(SqliteValue::Integer(value)) => Ok(Some(*value)),
        Some(SqliteValue::Null) | None => Ok(None),
        Some(other) => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!(
                "unexpected optional i64 type: {other:?}"
            ))),
        }),
    }
}

fn row_optional_f64(row: &Row, index: usize) -> SearchResult<Option<f64>> {
    match row.get(index) {
        Some(SqliteValue::Float(value)) => Ok(Some(*value)),
        Some(SqliteValue::Null) | None => Ok(None),
        // Integer 0 is sometimes stored for "no value" — coerce.
        #[allow(clippy::cast_precision_loss)]
        Some(SqliteValue::Integer(value)) => Ok(Some(*value as f64)),
        Some(other) => Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!(
                "unexpected optional f64 type: {other:?}"
            ))),
        }),
    }
}

fn sqlite_text_opt(value: Option<&str>) -> SqliteValue {
    value.map_or(SqliteValue::Null, |v| {
        SqliteValue::Text(v.to_owned().into())
    })
}

fn sqlite_i64_opt(value: Option<i64>) -> SqliteValue {
    value.map_or(SqliteValue::Null, SqliteValue::Integer)
}

fn sqlite_f64_opt(value: Option<f64>) -> SqliteValue {
    value.map_or(SqliteValue::Null, SqliteValue::Float)
}

/// Escape single quotes for SQL string literals.
///
/// Used as a workaround for multi-row SELECT queries where
/// `query_with_params` currently returns at most one row (`FrankenSQLite`
/// limitation).  Single-row queries and DML (INSERT/UPDATE/DELETE) use
/// parameterized queries instead.
fn sql_escape_single_quoted(value: &str) -> String {
    value.replace('\'', "''")
}

fn ensure_non_empty(value: &str, field: &'static str) -> SearchResult<()> {
    if value.trim().is_empty() {
        return Err(SearchError::SubsystemError {
            subsystem: "storage",
            source: Box::new(io::Error::other(format!("{field} must not be empty"))),
        });
    }
    Ok(())
}

fn unix_timestamp_ms() -> SearchResult<i64> {
    let duration = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_err(map_storage_error)?;
    i64::try_from(duration.as_millis()).map_err(|error| SearchError::SubsystemError {
        subsystem: "storage",
        source: Box::new(io::Error::other(format!(
            "unix timestamp overflow: {error}"
        ))),
    })
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use crate::connection::Storage;
    use crate::document::DocumentRecord;

    use super::*;

    fn sample_build_params(name: &str) -> RecordBuildParams {
        RecordBuildParams {
            index_name: name.to_owned(),
            index_type: "fsvi".to_owned(),
            embedder_id: "potion-128m".to_owned(),
            embedder_revision: Some("v1.0".to_owned()),
            dimension: 256,
            record_count: 100,
            source_doc_count: 0,
            build_duration_ms: 500,
            trigger: BuildTrigger::Initial,
            file_path: Some("/tmp/index.fsvi".to_owned()),
            file_size_bytes: Some(102_400),
            file_hash: Some("abc123".to_owned()),
            schema_version: Some(1),
            config_json: Some(r#"{"tier":"fast"}"#.to_owned()),
            fec_path: None,
            fec_size_bytes: None,
            notes: None,
            mean_norm: Some(1.0),
            variance: Some(0.05),
        }
    }

    fn insert_test_document(storage: &Storage, doc_id: &str, created_at: i64, updated_at: i64) {
        let doc = DocumentRecord::new(
            doc_id,
            "test content",
            [0x42; 32],
            64,
            created_at,
            updated_at,
        );
        storage.upsert_document(&doc).expect("doc insert");
    }

    #[test]
    fn record_and_get_round_trip() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("fast-index");

        storage.record_index_build(&params).expect("record build");

        let meta = storage
            .get_index_metadata("fast-index")
            .expect("get metadata")
            .expect("should exist");

        assert_eq!(meta.index_name, "fast-index");
        assert_eq!(meta.index_type, "fsvi");
        assert_eq!(meta.embedder_id, "potion-128m");
        assert_eq!(meta.embedder_revision.as_deref(), Some("v1.0"));
        assert_eq!(meta.dimension, 256);
        assert_eq!(meta.record_count, 100);
        assert_eq!(meta.source_doc_count, 0);
        assert_eq!(meta.file_path.as_deref(), Some("/tmp/index.fsvi"));
        assert_eq!(meta.file_size_bytes, Some(102_400));
        assert_eq!(meta.mean_norm, Some(1.0));
        assert_eq!(meta.variance, Some(0.05));
        assert!(meta.built_at.is_some());
        assert_eq!(meta.repair_count, 0);
    }

    #[test]
    fn get_nonexistent_returns_none() {
        let storage = Storage::open_in_memory().expect("storage");
        let result = storage
            .get_index_metadata("nonexistent")
            .expect("get should succeed");
        assert!(result.is_none());
    }

    #[test]
    fn upsert_updates_existing_metadata() {
        let storage = Storage::open_in_memory().expect("storage");

        let params_v1 = sample_build_params("idx");
        storage.record_index_build(&params_v1).expect("first build");

        let mut params_v2 = sample_build_params("idx");
        params_v2.record_count = 200;
        params_v2.embedder_revision = Some("v2.0".to_owned());
        params_v2.trigger = BuildTrigger::ContentChanged;
        storage
            .record_index_build(&params_v2)
            .expect("second build");

        let meta = storage
            .get_index_metadata("idx")
            .expect("get")
            .expect("exists");
        assert_eq!(meta.record_count, 200);
        assert_eq!(meta.embedder_revision.as_deref(), Some("v2.0"));
    }

    #[test]
    fn build_history_is_appended() {
        let storage = Storage::open_in_memory().expect("storage");

        for i in 0..3 {
            let mut params = sample_build_params("idx");
            params.record_count = i64::from(i) * 50;
            params.trigger = if i == 0 {
                BuildTrigger::Initial
            } else {
                BuildTrigger::ContentChanged
            };
            storage.record_index_build(&params).expect("build");
        }

        let history = storage.get_build_history("idx", 10).expect("history query");
        assert_eq!(history.len(), 3);
        // Most recent first.
        assert!(history[0].built_at >= history[1].built_at);
    }

    #[test]
    fn build_history_respects_limit() {
        let storage = Storage::open_in_memory().expect("storage");

        for _ in 0..5 {
            let params = sample_build_params("idx");
            storage.record_index_build(&params).expect("build");
        }

        let history = storage.get_build_history("idx", 2).expect("history");
        assert_eq!(history.len(), 2);
    }

    #[test]
    fn build_history_zero_limit_returns_empty() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        let history = storage.get_build_history("idx", 0).expect("history");
        assert!(history.is_empty());
    }

    #[test]
    fn prune_keeps_at_most_100_entries() {
        let storage = Storage::open_in_memory().expect("storage");

        for _ in 0..110 {
            let params = sample_build_params("idx");
            storage.record_index_build(&params).expect("build");
        }

        let history = storage.get_build_history("idx", 200).expect("history");
        assert_eq!(history.len(), 100);
    }

    #[test]
    fn record_verification_updates_timestamp() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        let updated = storage.record_verification("idx").expect("verify");
        assert!(updated);

        let meta = storage
            .get_index_metadata("idx")
            .expect("get")
            .expect("exists");
        assert!(meta.last_verified_at.is_some());
    }

    #[test]
    fn record_verification_missing_index_returns_false() {
        let storage = Storage::open_in_memory().expect("storage");
        let updated = storage.record_verification("missing").expect("verify");
        assert!(!updated);
    }

    #[test]
    fn record_repair_increments_count() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        storage.record_repair("idx").expect("repair 1");
        storage.record_repair("idx").expect("repair 2");

        let meta = storage
            .get_index_metadata("idx")
            .expect("get")
            .expect("exists");
        assert_eq!(meta.repair_count, 2);
        assert!(meta.last_repair_at.is_some());
    }

    #[test]
    fn staleness_never_built() {
        let storage = Storage::open_in_memory().expect("storage");
        let check = storage
            .check_index_staleness("missing", None)
            .expect("staleness check");

        assert!(check.is_stale);
        assert_eq!(check.reasons, vec![StalenessReason::NeverBuilt]);
        assert!(check.last_built_at.is_none());
    }

    #[test]
    fn staleness_fresh_index_is_not_stale() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        let check = storage
            .check_index_staleness("idx", Some("v1.0"))
            .expect("staleness check");

        assert!(!check.is_stale);
        assert!(check.reasons.is_empty());
        assert_eq!(check.docs_modified, 0);
        assert_eq!(check.docs_added, 0);
    }

    #[test]
    fn staleness_detects_new_documents() {
        let storage = Storage::open_in_memory().expect("storage");

        // Build the index first.
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        let meta = storage
            .get_index_metadata("idx")
            .expect("get")
            .expect("exists");
        let built_at = meta.built_at.unwrap();

        // Add a document after the build.
        insert_test_document(&storage, "new-doc", built_at + 1000, built_at + 1000);

        let check = storage
            .check_index_staleness("idx", Some("v1.0"))
            .expect("staleness check");

        assert!(check.is_stale);
        assert!(check.reasons.contains(&StalenessReason::NewDocuments));
        assert!(check.docs_added > 0);
    }

    #[test]
    fn staleness_detects_modified_documents() {
        let storage = Storage::open_in_memory().expect("storage");

        // Insert a document before the build.
        insert_test_document(&storage, "old-doc", 1_000_000, 1_000_000);

        // Build the index.
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        let meta = storage
            .get_index_metadata("idx")
            .expect("get")
            .expect("exists");
        let built_at = meta.built_at.unwrap();

        // Modify the document (update updated_at to after build time).
        let mut updated_doc = DocumentRecord::new(
            "old-doc",
            "updated content",
            [0x99; 32],
            64,
            1_000_000,
            built_at + 1000,
        );
        updated_doc.source_path = None;
        storage.upsert_document(&updated_doc).expect("update doc");

        let check = storage
            .check_index_staleness("idx", Some("v1.0"))
            .expect("staleness check");

        assert!(check.is_stale);
        assert!(check.reasons.contains(&StalenessReason::ContentChanged));
        assert!(check.docs_modified > 0);
    }

    #[test]
    fn staleness_detects_embedder_change() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        let check = storage
            .check_index_staleness("idx", Some("v2.0"))
            .expect("staleness check");

        assert!(check.is_stale);
        assert!(check.reasons.contains(&StalenessReason::EmbedderChanged));
    }

    #[test]
    fn staleness_detects_missing_documents_from_build_snapshot() {
        let storage = Storage::open_in_memory().expect("storage");

        insert_test_document(&storage, "doc-1", 1_000_000, 1_000_000);

        let mut params = sample_build_params("idx");
        params.source_doc_count = 2;
        storage.record_index_build(&params).expect("build");

        let check = storage
            .check_index_staleness("idx", Some("v1.0"))
            .expect("staleness check");

        assert!(check.is_stale);
        assert!(check.reasons.contains(&StalenessReason::ContentChanged));
        assert!(check.docs_modified >= 1);
    }

    #[test]
    fn staleness_embedder_none_stored_marks_changed() {
        let storage = Storage::open_in_memory().expect("storage");
        let mut params = sample_build_params("idx");
        params.embedder_revision = None;
        storage.record_index_build(&params).expect("build");

        let check = storage
            .check_index_staleness("idx", Some("v1.0"))
            .expect("staleness check");

        assert!(check.is_stale);
        assert!(check.reasons.contains(&StalenessReason::EmbedderChanged));
    }

    #[test]
    fn delete_index_metadata_cascades_history() {
        let storage = Storage::open_in_memory().expect("storage");
        let params = sample_build_params("idx");
        storage.record_index_build(&params).expect("build");

        assert!(
            storage
                .delete_index_metadata("idx")
                .expect("delete should succeed")
        );
        assert!(
            storage
                .get_index_metadata("idx")
                .expect("get should succeed")
                .is_none()
        );
        assert!(
            storage
                .get_build_history("idx", 10)
                .expect("history should succeed")
                .is_empty()
        );
    }

    #[test]
    fn delete_nonexistent_returns_false() {
        let storage = Storage::open_in_memory().expect("storage");
        let deleted = storage
            .delete_index_metadata("nonexistent")
            .expect("delete");
        assert!(!deleted);
    }

    #[test]
    fn list_index_metadata_returns_all() {
        let storage = Storage::open_in_memory().expect("storage");

        let fast = sample_build_params("fast-index");
        let quality = {
            let mut p = sample_build_params("quality-index");
            p.embedder_id = "minilm-l6-v2".to_owned();
            p.dimension = 384;
            p
        };

        storage.record_index_build(&fast).expect("fast build");
        storage.record_index_build(&quality).expect("quality build");

        let all = storage.list_index_metadata().expect("list");
        assert_eq!(all.len(), 2);
        let names: Vec<&str> = all.iter().map(|m| m.index_name.as_str()).collect();
        assert!(names.contains(&"fast-index"));
        assert!(names.contains(&"quality-index"));
    }

    #[test]
    fn build_trigger_round_trip() {
        let triggers = [
            BuildTrigger::Initial,
            BuildTrigger::Scheduled,
            BuildTrigger::ContentChanged,
            BuildTrigger::Manual,
            BuildTrigger::ConfigChanged,
            BuildTrigger::Repair,
        ];
        for trigger in triggers {
            let s = trigger.as_str();
            let parsed = BuildTrigger::from_str(s);
            assert_eq!(parsed, Some(trigger), "round-trip failed for {s}");
        }
    }

    #[test]
    fn empty_index_name_is_rejected() {
        let storage = Storage::open_in_memory().expect("storage");
        let mut params = sample_build_params("");
        params.index_name = String::new();

        let err = storage
            .record_index_build(&params)
            .expect_err("empty name should fail");
        assert!(err.to_string().contains("must not be empty"));
    }
}