File size: 66,426 Bytes
a3afa1d
 
 
 
 
 
 
 
 
d2b84ea
 
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
 
a3afa1d
 
 
 
 
 
d2b84ea
 
a3afa1d
 
 
 
d2b84ea
a3afa1d
d2b84ea
 
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
 
 
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
 
 
 
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2b84ea
 
 
 
a3afa1d
 
d2b84ea
a3afa1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
"""Lossless, reproducible storage for :mod:`fastplms.embeddings`."""

from __future__ import annotations

import hashlib
import io
import json
import sqlite3
import struct
import numpy as np
import torch
from bisect import bisect_right
from collections.abc import Iterable, Iterator, Sequence
from pathlib import Path
from typing import Any, cast, overload
from uuid import uuid4
from torch import Tensor

from .types import (
    EmbeddingRecord,
    EmbeddingResult,
    LazyTensorReference,
)


_DTYPE_NAMES: dict[torch.dtype, str] = {
    torch.float16: "float16",
    torch.bfloat16: "bfloat16",
    torch.float32: "float32",
    torch.float64: "float64",
    torch.int64: "int64",
    torch.int32: "int32",
    torch.int16: "int16",
    torch.int8: "int8",
    torch.uint8: "uint8",
    torch.bool: "bool",
}
_NAME_DTYPES = {name: dtype for dtype, name in _DTYPE_NAMES.items()}
DEFAULT_SHARD_SIZE = 2 * 1024**3
_MAX_RECORDS_PER_DESCRIPTOR_SHARD = 1_024
_TENSOR_HASH_CHUNK_BYTES = 16 * 1024**2


def _jsonable(value: Any) -> Any:
    if isinstance(value, dict):
        return {str(key): _jsonable(item) for key, item in value.items()}
    if isinstance(value, (list, tuple)):
        return [_jsonable(item) for item in value]
    if isinstance(value, Path):
        return str(value)
    if isinstance(value, torch.dtype):
        return str(value).removeprefix("torch.")
    if isinstance(value, torch.device):
        return str(value)
    if value is None or isinstance(value, (str, int, float, bool)):
        return value
    return repr(value)


def _persistent_metadata(
    metadata: dict[str, Any],
    *,
    descriptor_index: str,
    record_count: int | None = None,
) -> dict[str, Any]:
    """Remove per-record copies from metadata and identify the authoritative index."""

    cleaned_value = _jsonable(metadata)
    if not isinstance(cleaned_value, dict):
        raise TypeError("Embedding metadata must serialize to a JSON object.")
    cleaned: dict[str, Any] = cleaned_value
    cleaned.pop("outputs", None)
    cleaned.pop("tensor_hashes", None)
    cleaned["descriptor_index"] = descriptor_index
    if record_count is not None:
        cleaned["record_count"] = record_count
    return cleaned


def _tensor_bytes(X: Tensor) -> bytes:
    """Return the exact contiguous byte representation of X."""

    # X: (...)
    X = X.detach().cpu().contiguous()  # (...)
    return X.view(torch.uint8).numpy().tobytes()


def _bounded_tensor_chunks(X: Tensor, max_bytes: int) -> Iterator[Tensor]:
    """Yield row-major CPU chunks without materializing one full byte string."""

    # X: (...)
    flattened = X.detach().to(device="cpu").reshape(-1)  # (n,)
    if flattened.numel() == 0:
        return
    chunk_elements = max(1, max_bytes // flattened.element_size())
    for start in range(0, flattened.numel(), chunk_elements):
        chunk = flattened[start : start + chunk_elements]  # (n_chunk,)
        if chunk.stride(0) != 1:
            chunk = chunk.clone(memory_format=torch.contiguous_format)  # (n_chunk,)
        yield chunk  # (n_chunk,)


def _tensor_hash_chunks(X: Tensor) -> Iterator[bytes]:
    for chunk in _bounded_tensor_chunks(X, _TENSOR_HASH_CHUNK_BYTES):
        yield chunk.view(torch.uint8).numpy().tobytes()


def tensor_sha256(X: Tensor) -> str:
    """Hash dtype, shape, and exact tensor bytes."""

    if not isinstance(X, Tensor):
        raise TypeError("X must be a tensor.")
    if X.dtype not in _DTYPE_NAMES:
        raise TypeError(f"Unsupported tensor dtype {X.dtype}.")
    if X.is_meta:
        raise ValueError("Cannot hash a meta tensor without storage.")
    if X.layout != torch.strided:
        raise TypeError("Only strided tensors can be hashed.")
    digest = hashlib.sha256()
    digest.update(_DTYPE_NAMES[X.dtype].encode())
    digest.update(json.dumps(tuple(X.shape)).encode())
    for chunk in _tensor_hash_chunks(X):
        digest.update(chunk)
    return digest.hexdigest()


def _encode_tensor(X: Tensor) -> tuple[str, str, bytes]:
    if X.dtype not in _DTYPE_NAMES:
        raise TypeError(f"Unsupported tensor dtype {X.dtype}.")
    shape = json.dumps(tuple(X.shape), separators=(",", ":"))
    return _DTYPE_NAMES[X.dtype], shape, _tensor_bytes(X)


def _decode_tensor(dtype_name: str, shape_json: str, data: bytes) -> Tensor:
    try:
        dtype = _NAME_DTYPES[dtype_name]
    except KeyError as error:
        raise ValueError(f"Unsupported stored dtype {dtype_name!r}.") from error
    shape = tuple(json.loads(shape_json))
    # uint8 is used only as a byte-level carrier, preserving BF16 bits exactly.
    byte_array = np.frombuffer(data, dtype=np.uint8).copy()  # (n_bytes,)
    X = torch.from_numpy(byte_array).view(dtype)  # (n_elements,)
    return X.reshape(shape).clone()  # shape


def _index_path(path: str | Path) -> Path:
    path = Path(path)
    if path.suffix == ".json":
        return path
    if path.suffix == ".safetensors":
        return path.with_suffix(".json")
    return path / "index.json"


def _run_manifest_path(path: str | Path) -> Path:
    path = Path(path)
    if path.name == "index.json":
        return path.with_name("run.json")
    if path.suffix == ".json":
        return path.with_name(f"{path.stem}.run.json")
    if path.suffix == ".safetensors":
        return path.with_suffix(".run.json")
    return path / "run.json"


def _resolve_index_child(root: Path, relative: str, *, label: str) -> Path:
    relative_path = Path(relative)
    candidate = (root / relative_path).resolve()
    if relative_path.is_absolute() or candidate.parent != root.resolve():
        raise ValueError(f"Safetensors {label} references a file outside its output directory.")
    return candidate


def _canonical_json_bytes(payload: dict[str, Any]) -> bytes:
    return (json.dumps(payload, indent=2, sort_keys=True) + "\n").encode("utf-8")


def _load_authoritative_index(
    path: str | Path,
) -> tuple[dict[str, Any], Path, dict[str, Any]]:
    """Load the index selected by the atomic run-manifest commit record."""

    stable_index_path = _index_path(path)
    run_manifest_path = _run_manifest_path(path)
    if not run_manifest_path.is_file():
        raise ValueError(f"Missing safetensors run manifest: {run_manifest_path}.")
    run_manifest = json.loads(run_manifest_path.read_text(encoding="utf-8"))
    if not isinstance(run_manifest, dict):
        raise ValueError("Safetensors run manifest must contain a JSON object.")
    if run_manifest.get("format") != "fastplms-embedding-run":
        raise ValueError(f"Not a FastPLMs embedding run manifest: {run_manifest_path}.")
    version = run_manifest.get("version")
    index_reference = run_manifest.get("index")
    if not isinstance(index_reference, dict):
        raise ValueError("Safetensors run manifest contains an invalid index reference.")
    if version == 1:
        snapshot = run_manifest.get("index_payload")
        if isinstance(snapshot, dict):
            payload = snapshot
            index_bytes = _canonical_json_bytes(payload)
        elif snapshot is None:
            index_bytes = stable_index_path.read_bytes()
            payload = json.loads(index_bytes.decode("utf-8"))
            if not isinstance(payload, dict):
                raise ValueError("Safetensors index must contain a JSON object.")
        else:
            raise ValueError("Safetensors run manifest contains an invalid index snapshot.")
        expected = {
            "file": stable_index_path.name,
            "sha256": hashlib.sha256(index_bytes).hexdigest(),
        }
        index_path = stable_index_path
    elif version == 2:
        relative = index_reference.get("file")
        if not isinstance(relative, str):
            raise ValueError("Safetensors run manifest index file is invalid.")
        index_path = _resolve_index_child(stable_index_path.parent, relative, label="run manifest")
        index_bytes = index_path.read_bytes()
        payload = json.loads(index_bytes.decode("utf-8"))
        if not isinstance(payload, dict):
            raise ValueError("Safetensors generation index must contain a JSON object.")
        if payload.get("version") != 2:
            raise ValueError("Safetensors v2 run manifest must reference a v2 generation index.")
        expected = {
            "file": relative,
            "sha256": hashlib.sha256(index_bytes).hexdigest(),
        }
    else:
        raise ValueError(f"Unsupported safetensors run manifest version {version!r}.")
    if index_reference != expected:
        raise ValueError("Safetensors run manifest does not match its index.")
    if payload.get("format") != "fastplms-embedding-safetensors":
        raise ValueError(f"Not a FastPLMs embedding index: {index_path}.")
    record_count = payload.get("record_count")
    if record_count is None:
        legacy_records = payload.get("records", ())
        if not isinstance(legacy_records, list):
            raise ValueError("Safetensors index contains invalid records.")
        record_count = len(legacy_records)
    if not isinstance(record_count, int) or isinstance(record_count, bool) or record_count < 0:
        raise ValueError("Safetensors record count must be a non-negative integer.")
    if run_manifest.get("record_count") != record_count:
        raise ValueError("Safetensors run manifest record count does not match its index.")
    metadata = payload.get("metadata", {})
    if not isinstance(metadata, dict):
        raise ValueError("Safetensors index metadata must contain a JSON object.")
    if metadata.get("record_count", record_count) != record_count:
        raise ValueError("Safetensors metadata record count does not match its index.")
    if version == 1 and run_manifest.get("metadata") != payload.get("metadata"):
        raise ValueError("Safetensors run manifest metadata does not match its index.")
    return payload, index_path, run_manifest


def safetensors_result_exists(path: str | Path) -> bool:
    """Return whether an authoritative committed safetensors run exists."""

    try:
        _load_authoritative_index(path)
    except (OSError, ValueError, json.JSONDecodeError):
        return False
    return True


def _load_safetensor(path: Path, key: str) -> Tensor:
    try:
        from safetensors import safe_open
    except ImportError as error:
        raise ImportError("Loading embeddings requires the 'safetensors' package.") from error
    with safe_open(path, framework="pt", device="cpu") as handle:
        return cast(Tensor, handle.get_tensor(key))


def _safetensors_shard_prefix(path: str | Path) -> str:
    requested_path = Path(path)
    if requested_path.suffix in {".json", ".safetensors"}:
        return f"{requested_path.stem}-embeddings"
    return "embeddings"


def _authoritative_index_payload(path: str | Path) -> dict[str, Any] | None:
    """Return the last atomically committed generation index when available."""

    try:
        payload, _, _ = _load_authoritative_index(path)
    except (OSError, ValueError, json.JSONDecodeError):
        return None
    return payload


def _referenced_shards(
    index_path: Path,
    payload: dict[str, Any] | None = None,
) -> set[Path]:
    if payload is None:
        payload = _authoritative_index_payload(index_path)
    if payload is None:
        return set()
    shards: set[Path] = set()
    for descriptor_shard in payload.get("descriptor_shards", ()):
        tensor_file = descriptor_shard.get("tensor_file")
        if isinstance(tensor_file, str):
            candidate = _resolve_index_child(
                index_path.parent, tensor_file, label="descriptor index"
            )
            shards.add(candidate)
    for item in payload.get("records", ()):
        relative = item.get("tensor", {}).get("file")
        if not isinstance(relative, str):
            continue
        candidate = (index_path.parent / relative).resolve()
        if candidate.parent == index_path.parent.resolve():
            shards.add(candidate)
    return shards


def _validate_tensor_descriptor(
    tensor: dict[str, Any],
) -> tuple[str, str, tuple[int, ...], str]:
    key = tensor.get("key")
    if not isinstance(key, str) or not key:
        raise ValueError("Safetensors descriptor tensor key is invalid.")
    dtype = tensor.get("dtype")
    if not isinstance(dtype, str) or dtype not in _NAME_DTYPES:
        raise ValueError("Safetensors descriptor tensor dtype is invalid.")
    raw_shape = tensor.get("shape")
    if not isinstance(raw_shape, (list, tuple)) or not all(
        isinstance(dimension, int) and not isinstance(dimension, bool) and dimension >= 0
        for dimension in raw_shape
    ):
        raise ValueError("Safetensors descriptor tensor shape is invalid.")
    sha256 = tensor.get("sha256")
    if (
        not isinstance(sha256, str)
        or len(sha256) != 64
        or sha256 != sha256.lower()
        or any(character not in "0123456789abcdef" for character in sha256)
    ):
        raise ValueError("Safetensors descriptor tensor SHA-256 is invalid.")
    return key, dtype, tuple(raw_shape), sha256


def _record_from_safetensors_descriptor(root: Path, item: dict[str, Any]) -> EmbeddingRecord:
    if not isinstance(item, dict):
        raise ValueError("Safetensors record descriptor must contain a JSON object.")
    record_id = item.get("id")
    sequence = item.get("sequence")
    if not isinstance(record_id, str) or not record_id:
        raise ValueError("Safetensors descriptor record ID is invalid.")
    if not isinstance(sequence, str) or not sequence:
        raise ValueError("Safetensors descriptor sequence is invalid.")
    tensor = item.get("tensor")
    if not isinstance(tensor, dict):
        raise ValueError("Safetensors descriptor is missing tensor metadata.")
    relative = tensor.get("file")
    if not isinstance(relative, str) or not relative:
        raise ValueError("Safetensors descriptor tensor file is invalid.")
    key, dtype, shape, sha256 = _validate_tensor_descriptor(tensor)
    tensor_path = _resolve_index_child(root, relative, label="descriptor")
    if not tensor_path.is_file():
        raise ValueError(f"Safetensors tensor shard is missing: {relative}.")

    def load_tensor() -> Tensor:
        return _load_safetensor(tensor_path, key)

    reference = LazyTensorReference(
        source=str(tensor_path),
        key=key,
        dtype=dtype,
        shape=shape,
        sha256=sha256,
        _loader=load_tensor,
    )
    return EmbeddingRecord(record_id, sequence, reference)


class _SafetensorsRecordSequence(Sequence[EmbeddingRecord]):
    """Lazy immutable view over bounded descriptor JSONL shards."""

    _fastplms_immutable_sequence = True

    def __init__(self, root: Path, descriptor_shards: Sequence[dict[str, Any]]) -> None:
        if not isinstance(descriptor_shards, (list, tuple)):
            raise ValueError("Safetensors generation index has invalid descriptor shards.")
        self.root = root
        self.shards = tuple(descriptor_shards)
        cumulative: list[int] = []
        total = 0
        for shard in self.shards:
            if not isinstance(shard, dict):
                raise ValueError("Safetensors descriptor shard entry is invalid.")
            relative = shard.get("file")
            declared_count = shard.get("count")
            if (
                not isinstance(declared_count, int)
                or isinstance(declared_count, bool)
                or declared_count < 0
            ):
                raise ValueError("Safetensors descriptor shard count is invalid.")
            declared_sha256 = shard.get("sha256")
            if not isinstance(declared_sha256, str) or len(declared_sha256) != 64:
                raise ValueError("Safetensors descriptor shard SHA-256 is invalid.")
            if not isinstance(relative, str):
                raise ValueError("Safetensors descriptor index file is invalid.")
            descriptor_path = _resolve_index_child(root, relative, label="index")
            tensor_file = shard.get("tensor_file")
            if not isinstance(tensor_file, str):
                raise ValueError("Safetensors descriptor tensor file is invalid.")
            tensor_path = _resolve_index_child(root, tensor_file, label="index")
            if not tensor_path.is_file():
                raise ValueError(f"Safetensors tensor shard is missing: {tensor_file}.")
            digest = hashlib.sha256()
            count = 0
            with descriptor_path.open("rb") as handle:
                for line in handle:
                    digest.update(line)
                    if line.strip():
                        item = json.loads(line)
                        if not isinstance(item, dict):
                            raise ValueError("Safetensors record descriptor must be a JSON object.")
                        item_tensor = item.get("tensor")
                        if not isinstance(item_tensor, dict):
                            raise ValueError("Safetensors descriptor is missing tensor metadata.")
                        item_tensor_file = item_tensor.get("file")
                        if not isinstance(item_tensor_file, str):
                            raise ValueError("Safetensors descriptor tensor file is invalid.")
                        _resolve_index_child(root, item_tensor_file, label="descriptor")
                        if item_tensor_file != tensor_file:
                            raise ValueError(
                                "Safetensors descriptor tensor file does not match its shard."
                            )
                        count += 1
                        _validate_tensor_descriptor(item_tensor)
            if digest.hexdigest() != declared_sha256 or count != declared_count:
                raise ValueError(
                    f"Safetensors descriptor shard failed integrity validation: {relative}."
                )
            total += count
            cumulative.append(total)
        self._cumulative = tuple(cumulative)
        self._count = total

    def __len__(self) -> int:
        return self._count

    def _iter_shard(self, shard_index: int) -> Iterator[EmbeddingRecord]:
        descriptor_path = _resolve_index_child(
            self.root, str(self.shards[shard_index]["file"]), label="index"
        )
        with descriptor_path.open("r", encoding="utf-8") as handle:
            for line in handle:
                if line.strip():
                    yield _record_from_safetensors_descriptor(self.root, json.loads(line))

    def __iter__(self) -> Iterator[EmbeddingRecord]:
        for shard_index in range(len(self.shards)):
            yield from self._iter_shard(shard_index)

    @overload
    def __getitem__(self, index: int, /) -> EmbeddingRecord: ...

    @overload
    def __getitem__(self, index: slice, /) -> Sequence[EmbeddingRecord]: ...

    def __getitem__(self, index: int | slice) -> EmbeddingRecord | Sequence[EmbeddingRecord]:
        if isinstance(index, slice):
            start, stop, step = index.indices(self._count)
            return [self[position] for position in range(start, stop, step)]
        position = index + self._count if index < 0 else index
        if position < 0 or position >= self._count:
            raise IndexError(index)
        shard_index = bisect_right(self._cumulative, position)
        previous = self._cumulative[shard_index - 1] if shard_index else 0
        local_position = position - previous
        for offset, record in enumerate(self._iter_shard(shard_index)):
            if offset == local_position:
                return record
        raise IndexError(index)


class SafetensorsStreamWriter:
    """Bounded-memory, resumable publisher with immutable retained generations."""

    def __init__(
        self,
        path: str | Path,
        metadata: dict[str, Any],
        *,
        shard_size: int = DEFAULT_SHARD_SIZE,
        existing: Iterable[EmbeddingRecord] = (),
        reuse_existing: bool = False,
        publish_initial: bool = True,
        publish_incremental: bool = True,
    ) -> None:
        try:
            from safetensors.torch import save_file
        except ImportError as error:
            raise ImportError("Saving embeddings requires the 'safetensors' package.") from error
        if shard_size <= 0:
            raise ValueError("shard_size must be positive.")

        self.path = Path(path)
        self.index_path = _index_path(path)
        self.run_manifest_path = _run_manifest_path(path)
        self.index_path.parent.mkdir(parents=True, exist_ok=True)
        self.metadata = _persistent_metadata(
            metadata,
            descriptor_index="safetensors-generation-index",
            record_count=0,
        )
        self.shard_size = shard_size
        self.publish_incremental = publish_incremental
        self._save_file = save_file
        authoritative_payload = _authoritative_index_payload(path)
        prefix = _safetensors_shard_prefix(path)
        # A random generation identity prevents a new writer from reusing a
        # previously published or interrupted generation name. Published files
        # are immutable and remain available to lazy readers until explicit GC.
        self._generation = uuid4().hex
        self._prefix = prefix
        self._shard_index = 0
        self._seed_index = 0
        self._commit_index = 0
        self._descriptor_shards: list[dict[str, Any]] = []
        self._record_count = 0
        self._current: dict[str, Tensor] = {}
        self._pending: list[tuple[EmbeddingRecord, str, str, tuple[int, ...], str]] = []
        self._current_size = 0
        if reuse_existing:
            if authoritative_payload is None:
                raise ValueError("Cannot resume without an authoritative safetensors index.")
            authoritative_metadata = authoritative_payload.get("metadata")
            if not isinstance(authoritative_metadata, dict) or authoritative_metadata.get(
                "run_fingerprint"
            ) != self.metadata.get("run_fingerprint"):
                raise ValueError("Cannot resume a safetensors run with a different fingerprint.")
            expected_prefix_length = (
                len(existing) if isinstance(existing, Sequence) else sum(1 for _ in existing)
            )
            if authoritative_payload.get("version") == 2:
                self._descriptor_shards = list(authoritative_payload.get("descriptor_shards", ()))
                self._record_count = int(authoritative_payload.get("record_count", 0))
            else:
                legacy_records = list(authoritative_payload.get("records", ()))
                self._record_count = len(legacy_records)
                if legacy_records:
                    self._descriptor_shards.extend(self._write_descriptor_seed(legacy_records))
            if expected_prefix_length != self._record_count:
                raise ValueError(
                    "The resumable safetensors prefix does not match the validated "
                    "embedding records."
                )

        if publish_initial:
            self._publish_metadata(complete=False)

    def _write_descriptor_file(
        self,
        name: str,
        descriptors: Sequence[dict[str, Any]],
        *,
        tensor_file: str,
    ) -> dict[str, Any]:
        temporary = self.index_path.parent / f".{name}.tmp"
        destination = self.index_path.parent / name
        if temporary.exists() or destination.exists():
            raise FileExistsError(
                f"Refusing to reuse immutable safetensors generation path {destination}."
            )
        digest = hashlib.sha256()
        with temporary.open("wb") as handle:
            for item in descriptors:
                encoded = (
                    json.dumps(item, sort_keys=True, separators=(",", ":")).encode("utf-8") + b"\n"
                )
                handle.write(encoded)
                digest.update(encoded)
        temporary.replace(destination)
        return {
            "file": name,
            "sha256": digest.hexdigest(),
            "count": len(descriptors),
            "tensor_file": tensor_file,
        }

    def _write_descriptor_seed(self, records: Sequence[dict[str, Any]]) -> list[dict[str, Any]]:
        groups: list[tuple[str, list[dict[str, Any]]]] = []
        for record in records:
            tensor_file = str(record["tensor"]["file"])
            if (
                not groups
                or groups[-1][0] != tensor_file
                or len(groups[-1][1]) == _MAX_RECORDS_PER_DESCRIPTOR_SHARD
            ):
                groups.append((tensor_file, []))
            groups[-1][1].append(record)
        descriptor_shards: list[dict[str, Any]] = []
        for tensor_file, descriptors in groups:
            self._seed_index += 1
            name = (
                f"{self._prefix}-records-run-{self._generation}-seed-{self._seed_index:05d}.jsonl"
            )
            descriptor_shards.append(
                self._write_descriptor_file(name, descriptors, tensor_file=tensor_file)
            )
        return descriptor_shards

    def _write_shard(self) -> None:
        if not self._current:
            return
        self._shard_index += 1
        name = f"{self._prefix}-run-{self._generation}-{self._shard_index:05d}.safetensors"
        temporary = self.index_path.parent / f".{name}.tmp"
        destination = self.index_path.parent / name
        if temporary.exists() or destination.exists():
            raise FileExistsError(
                f"Refusing to reuse immutable safetensors generation path {destination}."
            )
        self._save_file(self._current, temporary)
        temporary.replace(destination)
        descriptors: list[dict[str, Any]] = []
        for record, key, dtype_name, shape, digest in self._pending:
            descriptors.append(
                {
                    "id": record.id,
                    "sequence": record.sequence,
                    "tensor": {
                        "file": name,
                        "key": key,
                        "dtype": dtype_name,
                        "shape": list(shape),
                        "sha256": digest,
                    },
                }
            )
        descriptor_name = (
            f"{self._prefix}-records-run-{self._generation}-{self._shard_index:05d}.jsonl"
        )
        self._descriptor_shards.append(
            self._write_descriptor_file(descriptor_name, descriptors, tensor_file=name)
        )
        self._record_count += len(descriptors)
        self._current = {}
        self._pending = []
        self._current_size = 0

    def append(
        self,
        records: Iterable[EmbeddingRecord],
        *,
        publish: bool | None = None,
    ) -> None:
        """Persist records while retaining at most one shard of tensors."""

        for record in records:
            position = self._record_count + len(self._pending)
            tensor = record.load_tensor().detach().cpu().contiguous()  # (...)
            if tensor.dtype not in _DTYPE_NAMES:
                raise TypeError(f"Unsupported tensor dtype {tensor.dtype}.")
            nbytes = tensor.numel() * tensor.element_size()
            if nbytes > self.shard_size:
                raise ValueError(
                    f"Embedding {position} requires {nbytes} bytes and cannot fit in a "
                    f"{self.shard_size}-byte safetensors shard."
                )
            if self._current and (
                self._current_size + nbytes > self.shard_size
                or len(self._pending) == _MAX_RECORDS_PER_DESCRIPTOR_SHARD
            ):
                self._write_shard()
                if self.publish_incremental:
                    self._publish_metadata(complete=False)
                position = self._record_count
            key = f"embedding_{position:08d}"
            self._current[key] = tensor
            self._current_size += nbytes
            self._pending.append(
                (
                    record,
                    key,
                    _DTYPE_NAMES[tensor.dtype],
                    tuple(tensor.shape),
                    tensor_sha256(tensor),
                )
            )
        if publish:
            self.publish(complete=False)

    def _publish_metadata(
        self,
        *,
        complete: bool,
        metadata: dict[str, Any] | None = None,
    ) -> EmbeddingResult:
        """Atomically expose one self-consistent metadata generation."""

        if metadata is not None:
            self.metadata = _persistent_metadata(
                metadata,
                descriptor_index="safetensors-generation-index",
            )
        self.metadata["complete"] = complete
        self.metadata["record_count"] = self._record_count
        self._commit_index += 1
        payload = {
            "version": 2,
            "format": "fastplms-embedding-safetensors",
            "metadata": self.metadata,
            "record_count": self._record_count,
            "descriptor_shards": self._descriptor_shards,
        }
        generation_index_name = (
            f"{self._prefix}-index-run-{self._generation}-{self._commit_index:05d}.json"
        )
        generation_index_path = self.index_path.parent / generation_index_name
        temporary_generation_index = generation_index_path.with_name(
            f".{generation_index_path.name}.tmp"
        )
        if temporary_generation_index.exists() or generation_index_path.exists():
            raise FileExistsError(
                f"Refusing to reuse immutable safetensors generation index {generation_index_path}."
            )
        encoded_index = _canonical_json_bytes(payload)
        temporary_generation_index.write_bytes(encoded_index)
        temporary_generation_index.replace(generation_index_path)

        index_sha256 = hashlib.sha256(encoded_index).hexdigest()
        index_reference = {
            "file": generation_index_name,
            "sha256": index_sha256,
        }
        run_manifest = {
            "version": 2,
            "format": "fastplms-embedding-run",
            "index": index_reference,
            "record_count": self._record_count,
        }
        pointer_identity = f"{self._generation}-{self._commit_index:05d}"
        temporary_manifest = self.run_manifest_path.with_name(
            f".{self.run_manifest_path.name}.{pointer_identity}.tmp"
        )
        temporary_manifest.write_bytes(_canonical_json_bytes(run_manifest))
        temporary_manifest.replace(self.run_manifest_path)

        # ``index.json`` is a non-authoritative convenience pointer. The run
        # manifest is committed first, so interruption here cannot invalidate
        # the newly committed generation.
        stable_pointer = {
            "version": 2,
            "format": "fastplms-embedding-index-pointer",
            "index": index_reference,
        }
        temporary_index = self.index_path.with_name(
            f".{self.index_path.name}.{pointer_identity}.tmp"
        )
        temporary_index.write_bytes(_canonical_json_bytes(stable_pointer))
        temporary_index.replace(self.index_path)

        return load_safetensors_result(self.index_path)

    def publish(
        self,
        *,
        complete: bool,
        metadata: dict[str, Any] | None = None,
    ) -> EmbeddingResult:
        """Flush the current shard and atomically expose a consistent generation."""

        self._write_shard()
        return self._publish_metadata(complete=complete, metadata=metadata)


def save_safetensors_result(
    result: EmbeddingResult,
    path: str | Path,
    *,
    shard_size: int = DEFAULT_SHARD_SIZE,
) -> EmbeddingResult:
    """Write sharded safetensors without materializing the full result."""

    writer = SafetensorsStreamWriter(
        path,
        result.metadata,
        shard_size=shard_size,
        publish_initial=False,
        publish_incremental=False,
    )
    writer.append(result, publish=False)
    return writer.publish(complete=bool(result.metadata.get("complete", True)))


def load_safetensors_result(path: str | Path) -> EmbeddingResult:
    """Load an indexed safetensors result without loading tensor payloads."""

    payload, index_path, _ = _load_authoritative_index(path)
    if payload.get("version") == 2:
        lazy_records = _SafetensorsRecordSequence(
            index_path.parent, payload.get("descriptor_shards", ())
        )
        if len(lazy_records) != payload.get("record_count"):
            raise ValueError("Safetensors descriptor count does not match its generation index.")
        return EmbeddingResult(lazy_records, payload.get("metadata", {}))

    records: list[EmbeddingRecord] = []
    for item in payload["records"]:
        records.append(_record_from_safetensors_descriptor(index_path.parent, item))
    return EmbeddingResult(records, payload.get("metadata", {}))


def garbage_collect_safetensors_generations(
    path: str | Path,
    *,
    dry_run: bool = True,
    confirm_no_active_readers_or_writers: bool = False,
) -> tuple[Path, ...]:
    """Remove non-authoritative generations after an explicit exclusivity check.

    Safetensors results retain immutable historical generations because an
    already-open :class:`EmbeddingResult` resolves tensors through those exact
    descriptor and shard paths. Destructive collection is therefore safe only
    when the caller guarantees that no reader or writer for ``path`` remains
    active. ``dry_run=True`` is the default and returns the paths that would be
    removed without changing the output directory.
    """

    if not isinstance(dry_run, bool):
        raise TypeError("dry_run must be a bool.")
    if not isinstance(confirm_no_active_readers_or_writers, bool):
        raise TypeError("confirm_no_active_readers_or_writers must be a bool.")
    if not dry_run and not confirm_no_active_readers_or_writers:
        raise ValueError(
            "Destructive safetensors generation collection requires "
            "confirm_no_active_readers_or_writers=True."
        )

    # Validate the full descriptor graph before identifying anything as stale.
    load_safetensors_result(path)
    payload, authoritative_index_path, _ = _load_authoritative_index(path)
    stable_index_path = _index_path(path)
    run_manifest_path = _run_manifest_path(path)
    root = stable_index_path.parent
    prefix = _safetensors_shard_prefix(path)
    protected = {
        stable_index_path.resolve(),
        run_manifest_path.resolve(),
        authoritative_index_path.resolve(),
        *_referenced_shards(stable_index_path, payload),
    }
    for descriptor_shard in payload.get("descriptor_shards", ()):
        relative = descriptor_shard.get("file")
        if isinstance(relative, str):
            protected.add(_resolve_index_child(root, relative, label="index").resolve())

    candidates: set[Path] = set()
    for pattern in (
        f"{prefix}-run-*-*.safetensors",
        f"{prefix}-records-run-*.jsonl",
        f"{prefix}-index-run-*.json",
        f".{prefix}-*.tmp",
    ):
        candidates.update(root.glob(pattern))
    candidates.update(root.glob(f".{stable_index_path.name}.*.tmp"))
    candidates.update(root.glob(f".{run_manifest_path.name}.*.tmp"))

    stale = tuple(
        sorted(
            (candidate for candidate in candidates if candidate.resolve() not in protected),
            key=lambda candidate: candidate.name,
        )
    )
    if not dry_run:
        for candidate in stale:
            candidate.unlink(missing_ok=True)
    return stale


def _ensure_sqlite_schema(connection: sqlite3.Connection) -> None:
    connection.executescript(
        """
        PRAGMA foreign_keys = ON;
        CREATE TABLE IF NOT EXISTS runs (
            run_id TEXT PRIMARY KEY,
            metadata_json TEXT NOT NULL,
            created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
            published_order INTEGER
        );
        CREATE TABLE IF NOT EXISTS tensors (
            run_id TEXT NOT NULL,
            position INTEGER NOT NULL,
            dtype TEXT NOT NULL,
            shape_json TEXT NOT NULL,
            data BLOB NOT NULL,
            sha256 TEXT NOT NULL,
            PRIMARY KEY (run_id, position),
            FOREIGN KEY (run_id) REFERENCES runs(run_id) ON DELETE CASCADE
        );
        CREATE TABLE IF NOT EXISTS records (
            run_id TEXT NOT NULL,
            position INTEGER NOT NULL,
            record_id TEXT NOT NULL,
            sequence TEXT NOT NULL,
            PRIMARY KEY (run_id, position),
            FOREIGN KEY (run_id, position) REFERENCES tensors(run_id, position)
                ON DELETE CASCADE
        );
        """
    )
    run_columns = {str(row[1]) for row in connection.execute("PRAGMA table_info(runs)").fetchall()}
    if "published_order" not in run_columns:
        connection.execute("ALTER TABLE runs ADD COLUMN published_order INTEGER")
        # Databases created before staged publication exposed every stored run.
        # Preserve that view for historical runs containing committed records.
        connection.execute(
            "UPDATE runs SET published_order = rowid "
            "WHERE published_order IS NULL AND EXISTS ("
            "SELECT 1 FROM records WHERE records.run_id = runs.run_id)"
        )
    connection.execute(
        "CREATE INDEX IF NOT EXISTS runs_published_order_idx ON runs(published_order)"
    )
    if "published_order" not in run_columns:
        # Schema upgrades run before callers open their data transaction.
        # End the migration transaction explicitly so BEGIN IMMEDIATE below
        # remains valid on existing databases.
        connection.commit()


def save_sqlite_result(result: EmbeddingResult, path: str | Path) -> EmbeddingResult:
    """Transactionally store an ordered result in normalized SQLite tables."""

    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    run_id = str(result.metadata.get("run_fingerprint", ""))
    if not run_id:
        raise ValueError("SQLite results require metadata['run_fingerprint'].")
    metadata_json = json.dumps(
        _persistent_metadata(
            result.metadata,
            descriptor_index="sqlite-records",
            record_count=len(result),
        ),
        sort_keys=True,
    )
    with sqlite3.connect(path, timeout=30) as connection:
        _ensure_sqlite_schema(connection)
        connection.execute("PRAGMA journal_mode = WAL")
        connection.execute("BEGIN IMMEDIATE")
        connection.execute("DELETE FROM runs WHERE run_id = ?", (run_id,))
        connection.execute(
            "INSERT INTO runs(run_id, metadata_json, published_order) "
            "SELECT ?, ?, COALESCE(MAX(published_order), 0) + 1 FROM runs",
            (run_id, metadata_json),
        )
        for position, record in enumerate(result):
            X = record.load_tensor().detach().cpu().contiguous()  # (...)
            dtype_name, shape_json, data = _encode_tensor(X)
            digest = tensor_sha256(X)
            connection.execute(
                "INSERT INTO tensors VALUES (?, ?, ?, ?, ?, ?)",
                (run_id, position, dtype_name, shape_json, data, digest),
            )
            connection.execute(
                "INSERT INTO records VALUES (?, ?, ?, ?)",
                (run_id, position, record.id, record.sequence),
            )
        connection.commit()
    return load_sqlite_result(path, run_id=run_id)


def initialize_sqlite_run(
    path: str | Path,
    metadata: dict[str, Any],
    *,
    resume: bool,
) -> str:
    """Create a resumable SQLite run without buffering tensor results."""

    path = Path(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    run_id = str(metadata.get("run_fingerprint", ""))
    if not run_id:
        raise ValueError("SQLite runs require metadata['run_fingerprint'].")
    with sqlite3.connect(path, timeout=30) as connection:
        _ensure_sqlite_schema(connection)
        connection.execute("PRAGMA journal_mode = WAL")
        connection.execute("BEGIN IMMEDIATE")
        exists = connection.execute("SELECT 1 FROM runs WHERE run_id = ?", (run_id,)).fetchone()
        if exists and not resume:
            connection.execute("DELETE FROM runs WHERE run_id = ?", (run_id,))
            exists = None
        if exists is None:
            initial_metadata = _persistent_metadata(
                metadata,
                descriptor_index="sqlite-records",
                record_count=0,
            )
            connection.execute(
                "INSERT INTO runs(run_id, metadata_json) VALUES (?, ?)",
                (run_id, json.dumps(initial_metadata, sort_keys=True)),
            )
        connection.commit()
    return run_id


def append_sqlite_records(
    path: str | Path,
    run_id: str,
    start_position: int,
    records: list[EmbeddingRecord],
    *,
    replace_metadata: dict[str, Any] | None = None,
) -> None:
    """Commit one ordered embedding batch so an interrupted run can resume."""

    if not isinstance(run_id, str) or not run_id:
        raise ValueError("run_id must be a non-empty string.")
    if not isinstance(start_position, int) or isinstance(start_position, bool):
        raise TypeError("start_position must be a non-negative integer.")
    if start_position < 0:
        raise ValueError("start_position must be a non-negative integer.")
    if not isinstance(records, list) or not all(
        isinstance(record, EmbeddingRecord) for record in records
    ):
        raise TypeError("records must be a list of EmbeddingRecord values.")

    with sqlite3.connect(Path(path), timeout=30) as connection:
        _ensure_sqlite_schema(connection)
        connection.execute("PRAGMA journal_mode = WAL")
        connection.execute("BEGIN IMMEDIATE")
        if replace_metadata is not None:
            replacement_run_id = str(replace_metadata.get("run_fingerprint", ""))
            if replacement_run_id != run_id:
                raise ValueError("Replacement metadata must match the SQLite run ID.")
            initial_metadata = _persistent_metadata(
                replace_metadata,
                descriptor_index="sqlite-records",
                record_count=0,
            )
            connection.execute("DELETE FROM runs WHERE run_id = ?", (run_id,))
            connection.execute(
                "INSERT INTO runs(run_id, metadata_json) VALUES (?, ?)",
                (run_id, json.dumps(initial_metadata, sort_keys=True)),
            )
        if connection.execute("SELECT 1 FROM runs WHERE run_id = ?", (run_id,)).fetchone() is None:
            raise KeyError(f"Missing SQLite embedding run {run_id}.")
        current_count, minimum_position, maximum_position = connection.execute(
            "SELECT COUNT(*), MIN(position), MAX(position) FROM records WHERE run_id = ?",
            (run_id,),
        ).fetchone()
        if current_count and (minimum_position != 0 or maximum_position != current_count - 1):
            raise ValueError("SQLite embedding run has a non-contiguous record prefix.")
        if start_position != current_count:
            raise ValueError(
                f"start_position={start_position} does not match the contiguous "
                f"SQLite prefix length {current_count}."
            )
        for offset, record in enumerate(records):
            position = start_position + offset
            X = record.load_tensor().detach().cpu().contiguous()  # (...)
            dtype_name, shape_json, data = _encode_tensor(X)
            digest = tensor_sha256(X)
            connection.execute(
                "INSERT INTO tensors VALUES (?, ?, ?, ?, ?, ?)",
                (run_id, position, dtype_name, shape_json, data, digest),
            )
            connection.execute(
                "INSERT INTO records VALUES (?, ?, ?, ?)",
                (run_id, position, record.id, record.sequence),
            )
        row = connection.execute(
            "SELECT metadata_json FROM runs WHERE run_id = ?", (run_id,)
        ).fetchone()
        if row is None:
            raise KeyError(f"Missing SQLite embedding run {run_id}.")
        metadata = json.loads(row[0])
        if not isinstance(metadata, dict):
            raise ValueError("SQLite run metadata must contain a JSON object.")
        metadata["record_count"] = start_position + len(records)
        metadata["descriptor_index"] = "sqlite-records"
        connection.execute(
            "UPDATE runs SET metadata_json = ? WHERE run_id = ?",
            (json.dumps(metadata, sort_keys=True), run_id),
        )
        if records:
            connection.execute(
                "UPDATE runs SET published_order = ("
                "SELECT COALESCE(MAX(published_order), 0) + 1 FROM runs"
                ") WHERE run_id = ? AND published_order IS NULL",
                (run_id,),
            )
        connection.commit()


def update_sqlite_run_metadata(path: str | Path, run_id: str, metadata: dict[str, Any]) -> None:
    """Finalize reproducibility metadata after the last streamed batch."""

    with sqlite3.connect(Path(path), timeout=30) as connection:
        row = connection.execute(
            "SELECT COUNT(*) FROM records WHERE run_id = ?", (run_id,)
        ).fetchone()
        record_count = int(row[0]) if row is not None else 0
        cleaned_metadata = _persistent_metadata(
            metadata,
            descriptor_index="sqlite-records",
            record_count=record_count,
        )
        updated = connection.execute(
            "UPDATE runs SET metadata_json = ? WHERE run_id = ?",
            (json.dumps(cleaned_metadata, sort_keys=True), run_id),
        ).rowcount
        if updated != 1:
            raise KeyError(f"Missing SQLite embedding run {run_id}.")
        connection.commit()


def _connect_sqlite_read_only(path: Path) -> sqlite3.Connection:
    if not path.is_file():
        raise FileNotFoundError(path)
    return sqlite3.connect(f"{path.resolve().as_uri()}?mode=ro", uri=True, timeout=30)


def _validate_sqlite_result_schema(connection: sqlite3.Connection, path: Path) -> None:
    tables = {
        str(row[0])
        for row in connection.execute(
            "SELECT name FROM sqlite_master WHERE type = 'table'"
        ).fetchall()
    }
    required = {"runs", "records", "tensors"}
    if not required.issubset(tables):
        raise ValueError(
            f"Not a FastPLMs embedding SQLite database: {path}. "
            "Use convert_legacy_sqlite() for a legacy embeddings table."
        )


def _load_sqlite_tensor(path: Path, run_id: str, position: int) -> Tensor:
    with _connect_sqlite_read_only(path) as connection:
        row = connection.execute(
            "SELECT dtype, shape_json, data FROM tensors WHERE run_id = ? AND position = ?",
            (run_id, position),
        ).fetchone()
    if row is None:
        raise KeyError(f"Missing SQLite tensor {run_id}:{position}.")
    return _decode_tensor(*row)


def _validate_sqlite_descriptor_row(
    row: Sequence[Any],
) -> tuple[int, str, str, str, str, str]:
    if len(row) != 6:
        raise ValueError("SQLite embedding descriptor has an invalid column count.")
    position, record_id, sequence, dtype_name, shape_json, digest = row
    if not isinstance(position, int) or isinstance(position, bool) or position < 0:
        raise ValueError("SQLite embedding position is invalid.")
    if not isinstance(record_id, str) or not record_id:
        raise ValueError("SQLite embedding record ID is invalid.")
    if not isinstance(sequence, str) or not sequence:
        raise ValueError("SQLite embedding sequence is invalid.")
    if not isinstance(shape_json, str):
        raise ValueError("SQLite embedding tensor shape is invalid.")
    try:
        shape = json.loads(shape_json)
    except json.JSONDecodeError as error:
        raise ValueError("SQLite embedding tensor shape is invalid.") from error
    _validate_tensor_descriptor(
        {
            "key": f"embedding_{position}",
            "dtype": dtype_name,
            "shape": shape,
            "sha256": digest,
        }
    )
    return position, record_id, sequence, dtype_name, shape_json, digest


def _sqlite_record_from_row(path: Path, run_id: str, row: Sequence[Any]) -> EmbeddingRecord:
    position, record_id, sequence, dtype_name, shape_json, digest = _validate_sqlite_descriptor_row(
        row
    )

    def load_tensor() -> Tensor:
        return _load_sqlite_tensor(path, run_id, position)

    reference = LazyTensorReference(
        source=str(path),
        key=f"{run_id}:{position}",
        dtype=dtype_name,
        shape=tuple(json.loads(shape_json)),
        sha256=digest,
        _loader=load_tensor,
    )
    return EmbeddingRecord(record_id, sequence, reference)


class _SQLiteRecordSequence(Sequence[EmbeddingRecord]):
    """Lazy immutable descriptor view over one SQLite embedding run."""

    _fastplms_immutable_sequence = True

    def __init__(self, path: Path, run_id: str, count: int) -> None:
        self.path = path
        self.run_id = run_id
        self._count = count

    @staticmethod
    def _row_query() -> str:
        return (
            "SELECT r.position, r.record_id, r.sequence, t.dtype, t.shape_json, t.sha256 "
            "FROM records r JOIN tensors t USING (run_id, position) "
            "WHERE r.run_id = ?"
        )

    def __len__(self) -> int:
        return self._count

    def __iter__(self) -> Iterator[EmbeddingRecord]:
        with _connect_sqlite_read_only(self.path) as connection:
            cursor = connection.execute(f"{self._row_query()} ORDER BY r.position", (self.run_id,))
            while rows := cursor.fetchmany(1_024):
                for row in rows:
                    yield _sqlite_record_from_row(self.path, self.run_id, row)

    @overload
    def __getitem__(self, index: int, /) -> EmbeddingRecord: ...

    @overload
    def __getitem__(self, index: slice, /) -> Sequence[EmbeddingRecord]: ...

    def __getitem__(self, index: int | slice) -> EmbeddingRecord | Sequence[EmbeddingRecord]:
        if isinstance(index, slice):
            start, stop, step = index.indices(self._count)
            return [self[position] for position in range(start, stop, step)]
        position = index + self._count if index < 0 else index
        if position < 0 or position >= self._count:
            raise IndexError(index)
        with _connect_sqlite_read_only(self.path) as connection:
            row = connection.execute(
                f"{self._row_query()} AND r.position = ?",
                (self.run_id, position),
            ).fetchone()
        if row is None:
            raise IndexError(index)
        return _sqlite_record_from_row(self.path, self.run_id, row)


def load_sqlite_result(
    path: str | Path,
    *,
    run_id: str | None = None,
    positions: Iterable[int] | None = None,
    record_ids: Iterable[str] | None = None,
    sequences: Iterable[str] | None = None,
) -> EmbeddingResult:
    """Load one SQLite run read-only, optionally in explicit selector order.

    Exactly one selector may be supplied. Repeated selectors are retained. An
    ID or sequence selector that matches multiple stored rows returns those
    rows in their original order for every occurrence of that selector.
    """

    path = Path(path).resolve()
    supplied_selectors = sum(
        selector is not None for selector in (positions, record_ids, sequences)
    )
    if supplied_selectors > 1:
        raise ValueError("Choose at most one of positions, record_ids, or sequences.")
    normalized_positions = tuple(positions) if positions is not None else None
    normalized_ids = tuple(record_ids) if record_ids is not None else None
    normalized_sequences = tuple(sequences) if sequences is not None else None
    if normalized_positions is not None and not all(
        isinstance(position, int) and not isinstance(position, bool) and position >= 0
        for position in normalized_positions
    ):
        raise ValueError("positions must contain non-negative integers.")
    for name, values in (
        ("record_ids", normalized_ids),
        ("sequences", normalized_sequences),
    ):
        if values is not None and not all(isinstance(value, str) for value in values):
            raise TypeError(f"{name} must contain strings.")

    with _connect_sqlite_read_only(path) as connection:
        _validate_sqlite_result_schema(connection, path)
        if run_id is None:
            run_columns = {
                str(info[1]) for info in connection.execute("PRAGMA table_info(runs)").fetchall()
            }
            if "published_order" in run_columns:
                row = connection.execute(
                    "SELECT run_id, metadata_json FROM runs "
                    "WHERE published_order IS NOT NULL "
                    "ORDER BY published_order DESC, rowid DESC LIMIT 1"
                ).fetchone()
            else:
                row = connection.execute(
                    "SELECT run_id, metadata_json FROM runs "
                    "ORDER BY created_at DESC, rowid DESC LIMIT 1"
                ).fetchone()
        else:
            row = connection.execute(
                "SELECT run_id, metadata_json FROM runs WHERE run_id = ?", (run_id,)
            ).fetchone()
        if row is None:
            raise KeyError(f"No embedding run found in {path}.")
        selected_run, metadata_json = row
        metadata = json.loads(metadata_json)
        if not isinstance(metadata, dict):
            raise ValueError("SQLite run metadata must contain a JSON object.")
        row_prefix = (
            "SELECT r.position, r.record_id, r.sequence, t.dtype, t.shape_json, t.sha256 "
            "FROM records r JOIN tensors t USING (run_id, position) "
            "WHERE r.run_id = ?"
        )
        record_count, minimum_position, maximum_position = connection.execute(
            "SELECT COUNT(*), MIN(position), MAX(position) FROM records WHERE run_id = ?",
            (selected_run,),
        ).fetchone()
        (tensor_count,) = connection.execute(
            "SELECT COUNT(*) FROM tensors WHERE run_id = ?", (selected_run,)
        ).fetchone()
        (joined_count,) = connection.execute(
            "SELECT COUNT(*) FROM records r JOIN tensors t USING (run_id, position) "
            "WHERE r.run_id = ?",
            (selected_run,),
        ).fetchone()
        if (
            tensor_count != record_count
            or joined_count != record_count
            or (record_count and (minimum_position != 0 or maximum_position != record_count - 1))
        ):
            raise ValueError("SQLite embedding run has inconsistent or non-contiguous records.")
        metadata_count = metadata.get("record_count")
        if (
            not isinstance(metadata_count, int)
            or isinstance(metadata_count, bool)
            or metadata_count != record_count
        ):
            raise ValueError("SQLite metadata record count does not match stored records.")
        descriptor_cursor = connection.execute(f"{row_prefix} ORDER BY r.position", (selected_run,))
        while descriptor_rows := descriptor_cursor.fetchmany(1_024):
            for descriptor_row in descriptor_rows:
                _validate_sqlite_descriptor_row(descriptor_row)
        if supplied_selectors == 0:
            rows: list[tuple[Any, ...]] | None = None
        else:
            selector_values: tuple[Any, ...]
            selector_column: str
            if normalized_positions is not None:
                selector_values = normalized_positions
                selector_column = "r.position"
            elif normalized_ids is not None:
                selector_values = normalized_ids
                selector_column = "r.record_id"
            else:
                if normalized_sequences is None:
                    raise RuntimeError("Filtered SQLite retrieval resolved no selector values.")
                selector_values = normalized_sequences
                selector_column = "r.sequence"
            fetched: list[tuple[Any, ...]] = []
            unique_values = tuple(dict.fromkeys(selector_values))
            for start in range(0, len(unique_values), 900):
                chunk = unique_values[start : start + 900]
                placeholders = ",".join("?" for _ in chunk)
                fetched.extend(
                    connection.execute(
                        f"{row_prefix} AND {selector_column} IN ({placeholders}) "
                        "ORDER BY r.position",
                        (selected_run, *chunk),
                    ).fetchall()
                )
            value_index = (
                0 if normalized_positions is not None else (1 if normalized_ids is not None else 2)
            )
            matched: dict[Any, list[tuple[Any, ...]]] = {}
            for fetched_row in sorted(fetched, key=lambda item: int(item[0])):
                matched.setdefault(fetched_row[value_index], []).append(fetched_row)
            missing = [value for value in selector_values if value not in matched]
            if missing:
                raise KeyError(f"SQLite embedding selectors were not found: {missing!r}.")
            rows = [
                fetched_row for value in selector_values for fetched_row in matched.get(value, ())
            ]

    if rows is None:
        return EmbeddingResult(
            _SQLiteRecordSequence(path, selected_run, int(record_count)),
            metadata,
        )
    records = [_sqlite_record_from_row(path, selected_run, selected_row) for selected_row in rows]
    if supplied_selectors:
        metadata = dict(metadata)
        metadata["selection"] = {
            "kind": (
                "positions"
                if normalized_positions is not None
                else "record_ids"
                if normalized_ids is not None
                else "sequences"
            ),
            "count": len(rows),
            "duplicate_policy": "preserve-request-order",
        }
    return EmbeddingResult(records, metadata)


def load_legacy_pth(path: str | Path, *, allow_unsafe_pickle: bool = False) -> EmbeddingResult:
    """Import a legacy mapping-only ``.pth`` file after explicit opt-in."""

    if not allow_unsafe_pickle:
        raise ValueError(
            "Legacy .pth loading can execute pickle payloads. Pass "
            "allow_unsafe_pickle=True only for a trusted file."
        )
    payload = torch.load(Path(path), map_location="cpu", weights_only=False)
    if not isinstance(payload, dict):
        raise ValueError("A legacy .pth embedding file must contain a mapping.")
    records: list[EmbeddingRecord] = []
    for position, (sequence, X) in enumerate(payload.items()):
        # X: (...)
        if not isinstance(sequence, str) or not isinstance(X, Tensor):
            raise ValueError("Legacy embedding mappings must use str keys and Tensor values.")
        records.append(EmbeddingRecord(str(position), sequence, X.detach().cpu()))
    return EmbeddingResult(records, {"format": "legacy-pth", "unsafe_pickle": True})


_LEGACY_COMPACT_VERSION = 0x01
_LEGACY_CODE_DTYPES: dict[int, tuple[np.dtype[Any], torch.dtype]] = {
    0: (np.dtype(np.float16), torch.float16),
    # Legacy BF16 blobs stored FP16 payload bytes and converted back to BF16.
    1: (np.dtype(np.float16), torch.bfloat16),
    2: (np.dtype(np.float32), torch.float32),
}


def _decode_legacy_sqlite_blob(
    data: bytes,
    *,
    fallback_shape: tuple[int, ...] | None,
    allow_unsafe_pickle: bool,
) -> Tensor:
    if len(data) >= 6 and data[0] == _LEGACY_COMPACT_VERSION:
        dtype_code = int(data[1])
        if dtype_code not in _LEGACY_CODE_DTYPES:
            raise ValueError(f"Unsupported legacy compact dtype code {dtype_code}.")
        (ndim,) = struct.unpack_from("<i", data, 2)
        if ndim < 0 or ndim > 16 or len(data) < 6 + 4 * ndim:
            raise ValueError("Malformed legacy compact embedding header.")
        shape = tuple(int(value) for value in struct.unpack_from(f"<{ndim}i", data, 6))
        if any(size < 0 for size in shape):
            raise ValueError("Malformed negative legacy embedding dimension.")
        numpy_dtype, target_dtype = _LEGACY_CODE_DTYPES[dtype_code]
        offset = 6 + 4 * ndim
        expected = int(np.prod(shape, dtype=np.int64)) * numpy_dtype.itemsize
        if len(data) - offset != expected:
            raise ValueError("Legacy compact embedding payload length does not match shape.")
        array = (  # shape
            np.frombuffer(data, dtype=numpy_dtype, offset=offset).copy().reshape(shape)
        )
        return torch.from_numpy(array).to(dtype=target_dtype)  # shape

    try:
        loaded = torch.load(io.BytesIO(data), map_location="cpu", weights_only=True)
    except Exception as safe_error:
        if allow_unsafe_pickle:
            loaded = torch.load(io.BytesIO(data), map_location="cpu", weights_only=False)
        elif fallback_shape is None:
            raise ValueError(
                "Legacy embedding blob is neither compact nor safely loadable. "
                "Provide fallback_shape for raw FP32 bytes, or set "
                "allow_unsafe_pickle=True only for a trusted database."
            ) from safe_error
        else:
            expected = int(np.prod(fallback_shape, dtype=np.int64)) * 4
            if len(data) != expected:
                raise ValueError(
                    "Legacy raw FP32 payload length does not match fallback_shape."
                ) from safe_error
            array = np.frombuffer(data, dtype=np.float32).copy().reshape(  # fallback_shape
                fallback_shape
            )
            return torch.from_numpy(array)  # fallback_shape
    if not isinstance(loaded, Tensor):
        raise ValueError("Legacy serialized embedding payload must contain one tensor.")
    return loaded.detach().cpu()  # (...)


def convert_legacy_sqlite(
    source: str | Path,
    output: str | Path,
    *,
    fallback_shape: tuple[int, ...] | None = None,
    allow_unsafe_pickle: bool = False,
    metadata: dict[str, Any] | None = None,
) -> EmbeddingResult:
    """Convert the v0 ``embeddings(sequence, embedding)`` database safely.

    The source is opened read-only. Compact blobs and ``weights_only`` Torch
    tensors are accepted by default. Unsafe general pickle deserialization
    remains an explicit opt-in.
    """

    source_path = Path(source)
    output_path = Path(output)
    if source_path.resolve() == output_path.resolve():
        raise ValueError("Legacy SQLite conversion requires a different output path.")
    if fallback_shape is not None and (
        not fallback_shape or any(not isinstance(size, int) or size < 0 for size in fallback_shape)
    ):
        raise ValueError("fallback_shape must contain non-negative integer dimensions.")
    with _connect_sqlite_read_only(source_path) as connection:
        columns = {
            str(row[1]) for row in connection.execute("PRAGMA table_info(embeddings)").fetchall()
        }
        if not {"sequence", "embedding"}.issubset(columns):
            raise ValueError("Legacy SQLite database must contain embeddings(sequence, embedding).")
        rows = connection.execute(
            "SELECT sequence, embedding FROM embeddings ORDER BY rowid"
        ).fetchall()
    if not rows:
        raise ValueError("Legacy SQLite database contains no embeddings.")

    records: list[EmbeddingRecord] = []
    content_digest = hashlib.sha256()
    for position, (sequence, data) in enumerate(rows):
        if not isinstance(sequence, str) or not sequence:
            raise ValueError("Legacy embedding sequences must be non-empty strings.")
        if not isinstance(data, bytes):
            data = bytes(data)
        tensor = _decode_legacy_sqlite_blob(
            data,
            fallback_shape=fallback_shape,
            allow_unsafe_pickle=allow_unsafe_pickle,
        )
        tensor_digest = tensor_sha256(tensor)
        for value in (sequence.encode("utf-8"), tensor_digest.encode("ascii")):
            content_digest.update(len(value).to_bytes(8, "big"))
            content_digest.update(value)
        records.append(EmbeddingRecord(str(position), sequence, tensor))

    content_sha256 = content_digest.hexdigest()
    run_fingerprint = hashlib.sha256(
        f"fastplms-legacy-sqlite-v1:{content_sha256}".encode("ascii")
    ).hexdigest()
    converted_metadata: dict[str, Any] = {
        "format_version": 1,
        "run_fingerprint": run_fingerprint,
        "source_format": "legacy-fastplms-sqlite-v0",
        "source_content_sha256": content_sha256,
        "unsafe_pickle": allow_unsafe_pickle,
        "complete": True,
    }
    if metadata:
        converted_metadata["conversion_metadata"] = _jsonable(metadata)
    return save_sqlite_result(
        EmbeddingResult(records, converted_metadata),
        output_path,
    )


def save_result(
    result: EmbeddingResult,
    path: str | Path,
    *,
    format: str = "safetensors",
    shard_size: int = DEFAULT_SHARD_SIZE,
) -> EmbeddingResult:
    if format == "safetensors":
        return save_safetensors_result(result, path, shard_size=shard_size)
    if format == "sqlite":
        return save_sqlite_result(result, path)
    if format == "pth":
        raise ValueError("Writing pickle-based .pth embeddings is not supported.")
    raise ValueError("format must be 'safetensors' or 'sqlite'.")


def load_result(path: str | Path, *, format: str = "safetensors") -> EmbeddingResult:
    if format == "safetensors":
        return load_safetensors_result(path)
    if format == "sqlite":
        return load_sqlite_result(path)
    raise ValueError("format must be 'safetensors' or 'sqlite'.")


__all__ = [
    "DEFAULT_SHARD_SIZE",
    "SafetensorsStreamWriter",
    "append_sqlite_records",
    "convert_legacy_sqlite",
    "garbage_collect_safetensors_generations",
    "initialize_sqlite_run",
    "load_legacy_pth",
    "load_result",
    "load_safetensors_result",
    "load_sqlite_result",
    "safetensors_result_exists",
    "save_result",
    "save_safetensors_result",
    "save_sqlite_result",
    "tensor_sha256",
    "update_sqlite_run_metadata",
]