stowken 0.7.0

Compressed storage and retrieval of LLM token sequences
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
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
//! `Stowken` — the main orchestrator.
//!
//! Coordinates segmentation, dedup, compression, storage, and indexing.

use std::collections::HashMap;
use std::sync::Arc;

use thiserror::Error;
use tokio::task;
use tracing::{debug, info, instrument};

use crate::{
    compression::{CompressionPipeline, FrameVersion},
    dedup::exact,
    dict_registry::{DictError, DictId, DictInfo, DictRegistry},
    index::metadata::{BatchSegmentOp, MetadataError, MetadataIndex},
    near_dedup::{self, MinHashSignature},
    segmenter::{Segmenter, SegmenterConfig, SegmenterError},
    storage::backend::{StorageBackend, StorageError},
    substring_registry::{
        hash_window, SubstringError, SubstringInfo, SubstringRegistry, MIN_LENGTH as SUBSTR_MIN_LEN,
    },
    types::{
        AnalyticsQuery, Conversation, ConversationManifest, ConversationText, ConversationTurn,
        ExportConfig, ExportStats, NearDuplicateCluster, NearDuplicateVariant,
        RetrievedConversation, RetrievedSegment, SegmentRef, SegmentType, SegmentTypeStats,
        StoreResult, StoredSegment, SystemPromptInfo, Token, TokenUsageStats, TokenizerAdapter,
        StowkenConfig,
    },
};

use crate::types::SegmentHash;

/// Internal: output of a successful near-dedup probe.
struct NearDedupOutcome {
    canonical_hash: SegmentHash,
    delta_frame: Vec<u8>,
}

/// Substring discovery on a sample of segments. For each MIN_LENGTH-token
/// window, count distinct segments observed it (the same window appearing
/// twice in one segment counts once). Promote windows that occurred in
/// `min_occurrences` or more distinct segments.
///
/// Greedy left-to-right per segment so we don't double-count overlapping
/// matches, and so the resulting promotions favour the highest-frequency
/// canonical position. Emits each promoted window as a `MIN_LENGTH`-token
/// substring; v0.5.x will extend promising windows outward.
///
/// Sync function (called from `tokio::task::spawn_blocking`) so it doesn't
/// hold a runtime thread during the linear scan.
fn discover_substrings(
    samples: &[Vec<Token>],
    min_occurrences: u32,
    registry: &SubstringRegistry,
) -> Result<Vec<SubstringInfo>, StowkenError> {
    use std::collections::HashMap;

    // First pass: count distinct segments per window hash.
    // hash → (occurrences, prototype_tokens)
    let mut counts: HashMap<u64, (u32, Vec<Token>)> = HashMap::new();
    for sample in samples {
        if sample.len() < SUBSTR_MIN_LEN {
            continue;
        }
        // Per-segment dedup: a window seen multiple times in one segment
        // shouldn't inflate the count.
        let mut seen_this_segment: std::collections::HashSet<u64> =
            std::collections::HashSet::new();
        for start in 0..=sample.len() - SUBSTR_MIN_LEN {
            let window = &sample[start..start + SUBSTR_MIN_LEN];
            let h = hash_window(window);
            if !seen_this_segment.insert(h) {
                continue;
            }
            let entry = counts
                .entry(h)
                .or_insert_with(|| (0, window.to_vec()));
            entry.0 += 1;
        }
    }

    // Second pass: promote windows clearing the threshold. Skip ones that
    // are already in the registry (e.g. from a previous train pass) — the
    // registry's `register` would error, but we'd rather just no-op.
    let mut promoted: Vec<SubstringInfo> = Vec::new();
    let mut sorted: Vec<(u64, u32, Vec<Token>)> = counts
        .into_iter()
        .filter(|(_, (count, _))| *count >= min_occurrences)
        .map(|(h, (count, tokens))| (h, count, tokens))
        .collect();
    // Highest-occurrence first so the most valuable substrings get
    // registered first (and survive if we ever cap the registry).
    sorted.sort_by_key(|t| std::cmp::Reverse(t.1));

    for (_h, count, tokens) in sorted {
        match registry.register(tokens, count) {
            Ok(info) => promoted.push(info),
            // A duplicate window registration shouldn't happen unless two
            // distinct token sequences hash-collide — extremely rare with
            // a 64-bit FNV-style hash on a real corpus, but safe to skip.
            Err(SubstringError::TooShort(_)) => continue,
            Err(SubstringError::Serialization(_)) => continue,
            Err(other) => return Err(StowkenError::Substring(other)),
        }
    }

    Ok(promoted)
}

/// Errors from vault operations.
#[derive(Debug, Error)]
pub enum StowkenError {
    #[error("storage error: {0}")]
    Storage(#[from] StorageError),
    #[error("metadata index error: {0}")]
    Metadata(#[from] MetadataError),
    #[error("segmentation error: {0}")]
    Segmentation(#[from] SegmenterError),
    #[error("compression error: {0}")]
    Compression(String),
    #[error("dictionary error: {0}")]
    Dict(#[from] DictError),
    #[error("substring registry error: {0}")]
    Substring(#[from] SubstringError),
    #[error("not enough segments to train a dictionary ({0} < {1} required)")]
    InsufficientTrainingSamples(usize, usize),
    #[error("export error: {0}")]
    Export(String),
    #[error("conversation not found: {0}")]
    NotFound(String),
    #[error("internal error: {0}")]
    Internal(String),
    #[cfg(feature = "semantic-search")]
    #[error("embedding error: {0}")]
    Embedding(String),
    #[cfg(feature = "semantic-search")]
    #[error("summarization error: {0}")]
    Summarization(String),
}

/// `Stowken<B>` — the main entry point for all storage and retrieval operations.
///
/// `B` is the storage backend (e.g., `MemoryBackend`, `FilesystemBackend`).
pub struct Stowken<B: StorageBackend> {
    backend: Arc<B>,
    compressor: CompressionPipeline,
    index: MetadataIndex,
    /// Registry of zstd compression dictionaries. In-memory for `Stowken::new`;
    /// filesystem-backed (under `<dirname(db_path)>/dictionaries/`) for `Stowken::open`.
    dict_registry: Arc<DictRegistry>,
    /// Registry of token-level substrings for v0.5 substring dedup.
    /// In-memory for `Stowken::new`, file-backed under
    /// `<dirname(db_path)>/substrings/` for `Stowken::open`.
    substring_registry: Arc<SubstringRegistry>,
    segmenter_config: SegmenterConfig,
    tokenizer: Option<Arc<dyn TokenizerAdapter>>,
    /// Cached from StowkenConfig — threshold for near-dedup. None disables.
    near_dedup_threshold: Option<f64>,
    #[cfg(feature = "semantic-search")]
    embedding_adapter:
        Arc<std::sync::RwLock<Option<Arc<dyn crate::types::EmbeddingAdapter>>>>,
    #[cfg(feature = "semantic-search")]
    summary_strategy: Arc<std::sync::RwLock<crate::types::SummaryStrategy>>,
    #[cfg(feature = "semantic-search")]
    embed_on_store: Arc<std::sync::atomic::AtomicBool>,
}

impl<B: StorageBackend + 'static> Stowken<B> {
    /// Create a new vault with an in-memory metadata index and an in-memory
    /// dictionary registry. Dictionaries trained on this vault are NOT
    /// persisted — use `open` for that.
    pub async fn new(backend: B, config: StowkenConfig) -> Result<Self, StowkenError> {
        let index = MetadataIndex::open_in_memory()?;
        let dict_registry = Arc::new(DictRegistry::in_memory());
        let substring_registry = Arc::new(SubstringRegistry::in_memory());
        let compressor = CompressionPipeline::new(config.enable_compression, 3)
            .with_registry(Arc::clone(&dict_registry))
            .with_substring_registry(Arc::clone(&substring_registry));
        let near_dedup_threshold = config.near_dedup_threshold;
        Ok(Self {
            backend: Arc::new(backend),
            compressor,
            index,
            dict_registry,
            substring_registry,
            segmenter_config: SegmenterConfig::default(),
            tokenizer: None,
            near_dedup_threshold,
            #[cfg(feature = "semantic-search")]
            embedding_adapter: Arc::new(std::sync::RwLock::new(None)),
            #[cfg(feature = "semantic-search")]
            summary_strategy: Arc::new(std::sync::RwLock::new(
                crate::types::SummaryStrategy::default(),
            )),
            #[cfg(feature = "semantic-search")]
            embed_on_store: Arc::new(std::sync::atomic::AtomicBool::new(true)),
        })
    }

    /// Open a vault with a persistent metadata database at `db_path`. The
    /// dictionary registry is rooted at `<dirname(db_path)>/dictionaries/`.
    pub async fn open(backend: B, config: StowkenConfig, db_path: &str) -> Result<Self, StowkenError> {
        let index = MetadataIndex::open(db_path)?;

        let parent = std::path::Path::new(db_path)
            .parent()
            .map(|p| p.to_path_buf())
            .unwrap_or_else(|| std::path::PathBuf::from("."));
        let dict_registry = Arc::new(DictRegistry::open(parent.join("dictionaries"))?);
        let substring_registry =
            Arc::new(SubstringRegistry::open(parent.join("substrings"))?);

        let compressor = CompressionPipeline::new(config.enable_compression, 3)
            .with_registry(Arc::clone(&dict_registry))
            .with_substring_registry(Arc::clone(&substring_registry));
        let near_dedup_threshold = config.near_dedup_threshold;
        Ok(Self {
            backend: Arc::new(backend),
            compressor,
            index,
            dict_registry,
            substring_registry,
            segmenter_config: SegmenterConfig::default(),
            tokenizer: None,
            near_dedup_threshold,
            #[cfg(feature = "semantic-search")]
            embedding_adapter: Arc::new(std::sync::RwLock::new(None)),
            #[cfg(feature = "semantic-search")]
            summary_strategy: Arc::new(std::sync::RwLock::new(
                crate::types::SummaryStrategy::default(),
            )),
            #[cfg(feature = "semantic-search")]
            embed_on_store: Arc::new(std::sync::atomic::AtomicBool::new(true)),
        })
    }

    /// Set a tokenizer adapter for text → token conversion.
    pub fn set_tokenizer(&mut self, tokenizer: impl TokenizerAdapter + 'static) {
        self.tokenizer = Some(Arc::new(tokenizer));
    }

    /// Set the segmenter configuration (e.g., context delimiter).
    pub fn set_segmenter_config(&mut self, cfg: SegmenterConfig) {
        self.segmenter_config = cfg;
    }

    // ── Semantic search configuration ─────────────────────────────────────

    #[cfg(feature = "semantic-search")]
    /// Set the embedding adapter used by `semantic_search`, embed-on-store,
    /// and `embed_all`. Takes `&self` (interior mutability via `RwLock`).
    pub fn set_embedding_adapter(
        &self,
        adapter: impl crate::types::EmbeddingAdapter + 'static,
    ) {
        *self.embedding_adapter.write().unwrap() = Some(Arc::new(adapter));
    }

    #[cfg(feature = "semantic-search")]
    /// Switch the conversation summary strategy (concat-truncate or LLM-generated).
    pub fn set_summary_strategy(&self, strategy: crate::types::SummaryStrategy) {
        *self.summary_strategy.write().unwrap() = strategy;
    }

    #[cfg(feature = "semantic-search")]
    /// Sugar: install an LLM summarizer. Equivalent to
    /// `set_summary_strategy(SummaryStrategy::LlmGenerated(Arc::new(s)))`.
    pub fn set_summarizer(&self, summarizer: impl crate::types::SummarizerAdapter + 'static) {
        self.set_summary_strategy(crate::types::SummaryStrategy::LlmGenerated(Arc::new(
            summarizer,
        )));
    }

    #[cfg(feature = "semantic-search")]
    /// Sugar: use concat-and-truncate summaries with the given character limit.
    pub fn use_concat_summary(&self, max_chars: usize) {
        self.set_summary_strategy(crate::types::SummaryStrategy::ConcatTruncate { max_chars });
    }

    #[cfg(feature = "semantic-search")]
    /// Toggle whether `store()` automatically embeds new segments and a
    /// conversation summary. Default: true (when an adapter is configured).
    pub fn set_embed_on_store(&self, enabled: bool) {
        self.embed_on_store
            .store(enabled, std::sync::atomic::Ordering::Relaxed);
    }

    // ── Internal accessors used by the `semantic` module ──────────────────
    //
    // These exist purely so `crate::semantic` can read private fields
    // without exposing them through the public API. Each is a one-liner.

    #[cfg(feature = "semantic-search")]
    pub(crate) fn embedding_adapter_internal(
        &self,
    ) -> Option<Arc<dyn crate::types::EmbeddingAdapter>> {
        self.embedding_adapter.read().unwrap().clone()
    }
    #[cfg(feature = "semantic-search")]
    pub(crate) fn summary_strategy_internal(&self) -> crate::types::SummaryStrategy {
        self.summary_strategy.read().unwrap().clone()
    }
    #[cfg(feature = "semantic-search")]
    pub(crate) fn embed_on_store_internal(&self) -> bool {
        self.embed_on_store
            .load(std::sync::atomic::Ordering::Relaxed)
    }
    #[cfg(feature = "semantic-search")]
    pub(crate) fn index_internal(&self) -> MetadataIndex {
        self.index.clone()
    }
    #[cfg(feature = "semantic-search")]
    pub(crate) fn tokenizer_internal(&self) -> Option<Arc<dyn TokenizerAdapter>> {
        self.tokenizer.clone()
    }
    pub(crate) fn default_tokenizer_name_internal(&self) -> String {  // used by retrieve_batch + semantic
        self.tokenizer
            .as_ref()
            .map(|t| t.name().to_owned())
            .unwrap_or_else(|| "cl100k_base".to_owned())
    }

    /// Detokenize a token slice using the vault's tokenizer or a built-in
    /// fallback for the given tokenizer name.
    pub(crate) fn detokenize_segment_tokens_internal(
        &self,
        tokens: &[Token],
        tokenizer_name: &str,
    ) -> String {
        if let Some(t) = &self.tokenizer {
            t.detokenize(tokens)
        } else if let Some(t) = crate::tokenizer::get_tokenizer(tokenizer_name) {
            t.detokenize(tokens)
        } else {
            tokens.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" ")
        }
    }
    #[cfg(feature = "semantic-search")]
    pub(crate) async fn backend_get_segment_internal(
        &self,
        hash: &SegmentHash,
    ) -> Result<StoredSegment, StowkenError> {
        Ok(self.backend.get_segment(hash).await?)
    }

    // ── Core Operations ───────────────────────────────────────────────────

    /// Store a conversation. Returns the conversation ID and storage metrics.
    #[instrument(skip(self, conversation), fields(model = %conversation.model))]
    pub async fn store(&self, conversation: Conversation) -> Result<StoreResult, StowkenError> {
        let id = conversation
            .id
            .clone()
            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

        let segmenter = self.make_segmenter(&conversation.tokenizer);
        let segments = segmenter.segment(&conversation)?;

        let mut segment_refs: Vec<SegmentRef> = Vec::with_capacity(segments.len());
        let mut index_ops: Vec<BatchSegmentOp> = Vec::with_capacity(segments.len());
        let mut new_segments: u64 = 0;
        let mut deduped_segments: u64 = 0;
        let mut bytes_saved: u64 = 0;
        let mut total_compressed: u64 = 0;
        let mut total_raw: u64 = 0;

        // Captured up-front so the per-segment loop can branch without
        // an additional async boundary; cheap copy.
        let near_dedup_threshold = self.near_dedup_threshold;

        for (position, segment) in segments.iter().enumerate() {
            let hash = exact::hash_segment(&segment.tokens);

            if self.backend.has_segment(&hash).await? {
                // Exact dedup hit — most efficient path.
                self.backend.increment_ref(&hash).await?;
                index_ops.push(BatchSegmentOp::IncrementRef(hash.clone()));
                deduped_segments += 1;
                bytes_saved += (segment.tokens.len() as u64) * 4;
            } else if let Some(threshold) = near_dedup_threshold {
                // Near-dedup probe. If a matching canonical exists, store
                // the variant as a 0x04 delta frame.
                match self
                    .try_near_dedup(&hash, &segment.tokens, threshold)
                    .await?
                {
                    Some(NearDedupOutcome { canonical_hash, delta_frame }) => {
                        let raw_size = (segment.tokens.len() as u32) * 4;
                        let compressed_size = delta_frame.len() as u32;
                        total_compressed += compressed_size as u64;
                        total_raw += raw_size as u64;

                        let stored = StoredSegment {
                            hash: hash.clone(),
                            segment_type: segment.segment_type.clone(),
                            tokenizer: conversation.tokenizer.clone(),
                            token_count: segment.tokens.len() as u32,
                            compressed_data: delta_frame,
                            raw_size,
                            compressed_size,
                            ref_count: 1,
                            created_at: chrono::Utc::now(),
                        };
                        self.backend.put_segment(&stored).await?;
                        // The variant's existence keeps the canonical alive
                        // — bump the canonical's ref so GC doesn't collect
                        // it out from under the delta.
                        self.backend.increment_ref(&canonical_hash).await?;
                        index_ops.push(BatchSegmentOp::Upsert(stored));
                        index_ops.push(BatchSegmentOp::IncrementRef(canonical_hash));
                        new_segments += 1;

                        // Index this variant in the LSH tables too — variants
                        // can themselves be canonicals for future variants.
                        self.upsert_lsh_entry(&hash, &segment.tokens).await?;
                    }
                    None => {
                        self.store_new_segment(
                            &hash,
                            segment,
                            &conversation.tokenizer,
                            &mut index_ops,
                            &mut total_compressed,
                            &mut total_raw,
                        )
                        .await?;
                        new_segments += 1;
                        // Add to LSH so future segments can find this one.
                        self.upsert_lsh_entry(&hash, &segment.tokens).await?;
                    }
                }
            } else {
                self.store_new_segment(
                    &hash,
                    segment,
                    &conversation.tokenizer,
                    &mut index_ops,
                    &mut total_compressed,
                    &mut total_raw,
                )
                .await?;
                new_segments += 1;
            }

            segment_refs.push(SegmentRef {
                segment_type: segment.segment_type.clone(),
                hash,
                token_count: segment.tokens.len() as u32,
                position: position as u32,
            });
        }

        let total_tokens: u64 = segment_refs.iter().map(|s| s.token_count as u64).sum();
        let manifest = ConversationManifest {
            schema_version: crate::types::MANIFEST_SCHEMA_VERSION,
            id: id.clone(),
            application: conversation.application.clone(),
            model: conversation.model.clone(),
            tokenizer: conversation.tokenizer.clone(),
            total_tokens,
            segments: segment_refs,
            created_at: chrono::Utc::now(),
            metadata: conversation.metadata.clone(),
        };

        self.backend.put_manifest(&manifest).await?;

        // Snapshot new-segment hashes for the embed-on-store hook (below)
        // before `index_ops` is consumed by the SQLite batch writer.
        #[cfg(feature = "semantic-search")]
        let new_segment_hashes = crate::semantic::collect_new_hashes(&index_ops);

        let index = self.index.clone();
        let manifest_clone = manifest.clone();
        task::spawn_blocking(move || index.store_conversation_batch(&index_ops, &manifest_clone))
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))??;

        // Embed-on-store: when an `EmbeddingAdapter` is configured and
        // `embed_on_store` is true (default), embed both the new segments
        // and a per-conversation summary. Idempotent — already-embedded
        // segments are skipped.
        #[cfg(feature = "semantic-search")]
        if let Err(e) = self
            .run_embed_on_store(new_segment_hashes, &id, &conversation.tokenizer)
            .await
        {
            // Don't fail the store on embedding errors — the conversation
            // is already persisted. Log and continue. Callers needing
            // exact-once semantics should set_embed_on_store(false) and
            // run embed_all() explicitly.
            tracing::warn!(id = %id, error = %e, "embed-on-store failed; segment is stored, embedding will be retried by embed_all");
        }

        let compression_ratio = if total_raw == 0 {
            1.0
        } else {
            total_compressed as f64 / total_raw as f64
        };

        let total_segments = new_segments + deduped_segments;
        info!(id, total_segments, new_segments, deduped_segments, "stored conversation");

        Ok(StoreResult {
            id,
            total_segments,
            new_segments,
            deduped_segments,
            bytes_saved,
            compression_ratio,
        })
    }

    /// Retrieve a full conversation with all segments decompressed.
    #[instrument(skip(self))]
    pub async fn retrieve(&self, id: &str) -> Result<RetrievedConversation, StowkenError> {
        let manifest = self.backend.get_manifest(id).await.map_err(|e| match e {
            StorageError::ConversationNotFound(_) => StowkenError::NotFound(id.to_owned()),
            other => StowkenError::Storage(other),
        })?;

        if manifest.schema_version > crate::types::MANIFEST_SCHEMA_VERSION {
            return Err(StowkenError::Internal(format!(
                "manifest {id} schema version {} is newer than supported {}",
                manifest.schema_version,
                crate::types::MANIFEST_SCHEMA_VERSION
            )));
        }

        let mut retrieved_segments = Vec::with_capacity(manifest.segments.len());
        for seg_ref in &manifest.segments {
            let stored = self.backend.get_segment(&seg_ref.hash).await?;
            let tokens = self.decompress_segment_async(&stored.compressed_data).await?;
            retrieved_segments.push(RetrievedSegment {
                segment_type: seg_ref.segment_type.clone(),
                hash: seg_ref.hash.clone(),
                tokens,
                token_count: seg_ref.token_count,
                position: seg_ref.position,
            });
        }

        debug!(id, segments = retrieved_segments.len(), "retrieved conversation");
        Ok(RetrievedConversation {
            manifest,
            segments: retrieved_segments,
        })
    }

    /// Retrieve multiple conversations by ID and return them as flat,
    /// human-readable `ConversationText` objects — tokens detokenized and
    /// joined with role markers. Designed to close the search → read loop:
    /// pass the `conversation_id` values from `semantic_search` hits directly.
    ///
    /// IDs that are not found are silently skipped; check the returned length
    /// if you need to detect missing conversations.
    pub async fn retrieve_batch(
        &self,
        ids: &[&str],
    ) -> Result<Vec<ConversationText>, StowkenError> {
        let tokenizer_name = self.default_tokenizer_name_internal();
        let mut results = Vec::with_capacity(ids.len());
        for id in ids {
            let conv = match self.retrieve(id).await {
                Ok(c) => c,
                Err(StowkenError::NotFound(_)) => continue,
                Err(e) => return Err(e),
            };
            let role_for = |seg_type: &SegmentType| -> &'static str {
                match seg_type {
                    SegmentType::SystemPrompt  => "system",
                    SegmentType::UserTurn      => "user",
                    SegmentType::AssistantTurn => "assistant",
                    SegmentType::ToolCall      => "tool_call",
                    SegmentType::ToolResult    => "tool_result",
                    SegmentType::Context       => "context",
                    SegmentType::Continuation  => "continuation",
                }
            };
            let mut turns: Vec<ConversationTurn> = Vec::new();
            for seg in &conv.segments {
                let text = self.detokenize_segment_tokens_internal(&seg.tokens, &tokenizer_name);
                let role = role_for(&seg.segment_type).to_string();
                // Merge consecutive continuation segments into the preceding turn.
                if seg.segment_type == SegmentType::Continuation {
                    if let Some(last) = turns.last_mut() {
                        last.text.push(' ');
                        last.text.push_str(&text);
                        continue;
                    }
                }
                turns.push(ConversationTurn { role, text });
            }
            let full_text = turns
                .iter()
                .map(|t| format!("[{}] {}", t.role, t.text))
                .collect::<Vec<_>>()
                .join("\n");
            results.push(ConversationText {
                conversation_id: conv.manifest.id.clone(),
                model: conv.manifest.model.clone(),
                application: conv.manifest.application.clone(),
                text: full_text,
                turns,
                created_at: conv.manifest.created_at,
            });
        }
        Ok(results)
    }

    /// Retrieve a filtered subset of segments from a conversation.
    pub async fn retrieve_segments(
        &self,
        id: &str,
        segment_type: Option<SegmentType>,
    ) -> Result<Vec<RetrievedSegment>, StowkenError> {
        let conv = self.retrieve(id).await?;
        Ok(conv
            .segments
            .into_iter()
            .filter(|s| segment_type.as_ref().is_none_or(|t| &s.segment_type == t))
            .collect())
    }

    /// Retrieve a single segment by its content hash.
    pub async fn retrieve_segment_by_hash(
        &self,
        hash: &SegmentHash,
    ) -> Result<RetrievedSegment, StowkenError> {
        let stored = self.backend.get_segment(hash).await?;
        let tokens = self.decompress_segment_async(&stored.compressed_data).await?;
        Ok(RetrievedSegment {
            segment_type: stored.segment_type,
            hash: hash.clone(),
            token_count: stored.token_count,
            tokens,
            position: 0,
        })
    }

    /// Delete a conversation. Decrements segment ref-counts but does not GC immediately.
    #[instrument(skip(self))]
    pub async fn delete(&self, id: &str) -> Result<(), StowkenError> {
        let manifest = self.backend.get_manifest(id).await.map_err(|e| match e {
            StorageError::ConversationNotFound(_) => StowkenError::NotFound(id.to_owned()),
            other => StowkenError::Storage(other),
        })?;

        for seg_ref in &manifest.segments {
            self.backend.decrement_ref(&seg_ref.hash).await?;
        }
        self.backend.delete_manifest(id).await?;

        let index = self.index.clone();
        let id_owned = id.to_owned();
        task::spawn_blocking(move || index.remove_conversation(&id_owned))
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))??;

        info!(id, "deleted conversation");
        Ok(())
    }

    // ── Analytics ─────────────────────────────────────────────────────────

    /// Get overall storage statistics.
    pub async fn stats(&self) -> Result<TokenUsageStats, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.get_stats())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    /// Get per-segment-type statistics.
    pub async fn segment_stats(&self) -> Result<Vec<SegmentTypeStats>, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.get_segment_type_stats())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    /// Run a filtered analytics query.
    pub async fn query(
        &self,
        query: AnalyticsQuery,
    ) -> Result<Vec<HashMap<String, serde_json::Value>>, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.query_analytics(&query))
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    /// List all unique system prompts.
    pub async fn list_system_prompts(&self) -> Result<Vec<SystemPromptInfo>, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.list_unique_system_prompts())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    /// Find conversations that contain a specific segment.
    pub async fn find_by_segment(&self, hash: &SegmentHash) -> Result<Vec<String>, StowkenError> {
        let index = self.index.clone();
        let h = hash.clone();
        task::spawn_blocking(move || index.find_conversations_by_segment(&h))
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    /// Bytes a corpus would occupy if stored as one raw copy per unique
    /// segment (no compression). Used to separate dedup savings from zstd
    /// savings in benchmarks.
    pub async fn dedup_only_bytes(&self) -> Result<u64, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.dedup_only_bytes())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    /// Find segments referenced at least `min_refs` times — the high-traffic
    /// dedup candidates worth examining for prompt refactoring.
    pub async fn find_duplicates(
        &self,
        min_refs: u64,
        limit: u64,
    ) -> Result<Vec<crate::types::DuplicateSegment>, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.find_duplicates(min_refs, limit))
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    // ── Export ────────────────────────────────────────────────────────────

    /// Export training data pairs (user/assistant turns) in the requested format.
    pub async fn export_training_data(
        &self,
        config: ExportConfig,
        writer: &mut dyn std::io::Write,
    ) -> Result<ExportStats, StowkenError> {
        crate::export::training::export_jsonl(self, &config, writer)
            .await
            .map_err(|e| StowkenError::Export(e.to_string()))
    }

    // ── Maintenance ───────────────────────────────────────────────────────

    /// Run garbage collection on orphaned segments (ref_count == 0).
    pub async fn gc(&self) -> Result<u64, StowkenError> {
        let deleted = self.backend.garbage_collect().await?;
        info!(deleted, "garbage collected segments");
        Ok(deleted)
    }

    /// List the segment hashes currently eligible for GC, without deleting.
    pub async fn gc_candidates(&self) -> Result<Vec<SegmentHash>, StowkenError> {
        Ok(self.backend.list_garbage().await?)
    }

    // ── Near-duplicate analytics ──────────────────────────────────────────

    /// Cluster heavily-referenced segments together with their nearest
    /// duplicates. For each segment with `ref_count >= min_refs`, scans
    /// the LSH index for candidates and returns those whose Jaccard
    /// similarity to the canonical exceeds `threshold`.
    ///
    /// Useful for prompt-drift audits: surfaces "you have 47 system
    /// prompts that are 90%+ similar to this one — should they be one?".
    /// Read-only; does not change storage.
    pub async fn find_near_duplicates(
        &self,
        threshold: f64,
        min_refs: u64,
        max_clusters: u64,
    ) -> Result<Vec<NearDuplicateCluster>, StowkenError> {
        let candidates = self.find_duplicates(min_refs, max_clusters).await?;
        let mut clusters = Vec::new();

        for canonical in candidates {
            // Need the canonical's tokens to compute a signature. Avoid
            // doing this inside the SQL critical path.
            let canonical_stored = self.backend.get_segment(&canonical.hash).await?;
            let canonical_tokens = self
                .decompress_segment_async(&canonical_stored.compressed_data)
                .await?;
            let sig = MinHashSignature::compute(&canonical_tokens);
            let band_hashes: Vec<u64> =
                (0..near_dedup::BANDS).map(|b| sig.band_hash(b)).collect();

            let index = self.index.clone();
            let bh = band_hashes.clone();
            let cand_hashes = task::spawn_blocking(move || {
                index.find_near_dedup_candidates(&bh)
            })
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))??;

            let mut variants = Vec::new();
            for c in cand_hashes {
                if c == canonical.hash {
                    continue;
                }
                let index = self.index.clone();
                let h = c.clone();
                let sig_bytes_opt =
                    task::spawn_blocking(move || index.get_near_dedup_signature(&h))
                        .await
                        .map_err(|e| StowkenError::Internal(e.to_string()))??;
                let other_sig = match sig_bytes_opt {
                    Some(b) => MinHashSignature::from_bytes(&b)
                        .map_err(|e| StowkenError::Internal(format!("LSH signature corrupt: {e}")))?,
                    None => continue,
                };
                let similarity = sig.jaccard(&other_sig);
                if similarity >= threshold {
                    let stored = self.backend.get_segment(&c).await?;
                    variants.push(NearDuplicateVariant {
                        hash: c,
                        similarity,
                        token_count: stored.token_count,
                        ref_count: stored.ref_count,
                    });
                }
            }

            if !variants.is_empty() {
                variants.sort_by(|a, b| b.similarity.partial_cmp(&a.similarity).unwrap_or(std::cmp::Ordering::Equal));
                clusters.push(NearDuplicateCluster {
                    canonical: canonical.hash,
                    canonical_token_count: canonical.token_count,
                    canonical_ref_count: canonical.ref_count,
                    variants,
                });
            }
        }

        Ok(clusters)
    }

    /// Number of segments tracked in the LSH index. Useful for sanity
    /// checks and post-reindex confirmation.
    pub async fn near_dedup_index_size(&self) -> Result<u64, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.near_dedup_size())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }

    // ── Substring management (v0.5) ────────────────────────────────────────

    /// Retroactively rewrite pre-train segments to use the substring
    /// registry. Walks every unique segment in the index, decompresses
    /// it, tries `0x05` encoding, and replaces the stored bytes if the
    /// substring frame is smaller. Skips `0x04` (delta) and `0x05`
    /// (already-substring) frames.
    ///
    /// Atomic per-segment via the backend's `replace_segment_data`. A
    /// crash mid-compaction leaves each segment in either old or new
    /// form — both decompressible. The metadata index's
    /// `compressed_size` is updated after each replace, so stats
    /// remain consistent.
    pub async fn compact_substrings(&self) -> Result<crate::types::SubstringCompactStats, StowkenError> {
        if self.substring_registry.is_empty() {
            return Ok(crate::types::SubstringCompactStats {
                segments_examined: 0,
                segments_rewritten: 0,
                bytes_saved: 0,
                segments_skipped: 0,
            });
        }

        let hashes = self.list_all_segment_hashes().await?;

        let mut examined = 0u64;
        let mut rewritten = 0u64;
        let mut skipped = 0u64;
        let mut bytes_saved: u64 = 0;

        for hash in hashes {
            examined += 1;
            let stored = match self.backend.get_segment(&hash).await {
                Ok(s) => s,
                Err(_) => {
                    skipped += 1;
                    continue;
                }
            };

            // Skip 0x04 (delta) and 0x05 (already substring-encoded)
            // frames. 0x04 frames hold edit scripts against another
            // canonical, not native tokens — re-encoding them as 0x05
            // would be conceptually wrong. 0x05 frames are already in
            // their final form.
            let first_byte = match stored.compressed_data.first() {
                Some(&b) => b,
                None => {
                    skipped += 1;
                    continue;
                }
            };
            if first_byte == FrameVersion::Delta as u8
                || first_byte == FrameVersion::Substring as u8
            {
                skipped += 1;
                continue;
            }

            // Decompress to native tokens and try the substring path.
            let tokens = self
                .decompress_segment_async(&stored.compressed_data)
                .await?;
            let baseline_len = stored.compressed_data.len();
            let new_frame = match self
                .compressor
                .try_compress_substring_frame(&tokens, baseline_len)
                .map_err(|e| StowkenError::Compression(e.to_string()))?
            {
                Some(frame) => frame,
                None => continue, // not a win — leave the segment alone
            };

            let saved = (baseline_len - new_frame.len()) as u64;
            let new_size = new_frame.len() as u32;
            self.backend.replace_segment_data(&hash, new_frame).await?;

            // Keep the metadata index in sync with the on-disk size.
            let index = self.index.clone();
            let h = hash.clone();
            task::spawn_blocking(move || index.update_segment_compressed_size(&h, new_size))
                .await
                .map_err(|e| StowkenError::Internal(e.to_string()))??;

            rewritten += 1;
            bytes_saved += saved;
        }

        info!(
            examined,
            rewritten,
            skipped,
            bytes_saved,
            "compacted segments to use substring registry"
        );
        Ok(crate::types::SubstringCompactStats {
            segments_examined: examined,
            segments_rewritten: rewritten,
            bytes_saved,
            segments_skipped: skipped,
        })
    }

    /// Drop substrings from the registry that are no longer referenced
    /// by any `0x05` frame in the backend. Walks every segment, decodes
    /// any `0x05` frame to collect referenced substring ids, then
    /// retains only those in the registry.
    pub async fn gc_substrings(&self) -> Result<crate::types::SubstringGcStats, StowkenError> {
        let before = self.substring_registry.len() as u64;
        if before == 0 {
            return Ok(crate::types::SubstringGcStats {
                registry_size_before: 0,
                registry_size_after: 0,
                substrings_dropped: 0,
            });
        }

        // Collect every substring id referenced by a 0x05 frame.
        let hashes = self.list_all_segment_hashes().await?;
        let mut referenced: std::collections::HashSet<crate::substring_registry::SubstringId> =
            std::collections::HashSet::new();
        for hash in hashes {
            let stored = match self.backend.get_segment(&hash).await {
                Ok(s) => s,
                Err(_) => continue,
            };
            if stored.compressed_data.first() != Some(&(FrameVersion::Substring as u8)) {
                continue;
            }
            // Body starts at byte 1 (skip version tag).
            let ops = match crate::compression::decode_substring_frame(&stored.compressed_data[1..]) {
                Ok(o) => o,
                Err(_) => continue,
            };
            for op in ops {
                if let crate::compression::SubstringOp::Ref(id) = op {
                    referenced.insert(id);
                }
            }
        }

        let dropped = self.substring_registry.retain(&referenced)?;
        let after = self.substring_registry.len() as u64;

        info!(
            before,
            after,
            dropped,
            "garbage-collected substring registry"
        );
        Ok(crate::types::SubstringGcStats {
            registry_size_before: before,
            registry_size_after: after,
            substrings_dropped: dropped,
        })
    }

    /// Helper: every distinct segment hash known to the metadata index.
    async fn list_all_segment_hashes(&self) -> Result<Vec<SegmentHash>, StowkenError> {
        let index = self.index.clone();
        task::spawn_blocking(move || index.all_segment_hashes())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))?
            .map_err(Into::into)
    }


    /// Result of a `train_substrings` pass.
    pub fn substring_registry(&self) -> &Arc<SubstringRegistry> {
        &self.substring_registry
    }

    /// All known substrings, in registration order.
    pub fn list_substrings(&self) -> Vec<SubstringInfo> {
        self.substring_registry.list()
    }

    /// Discover candidate substrings in `sample_count` recent segments,
    /// filter by occurrence threshold, and register the survivors. Returns
    /// the list of newly-registered substrings.
    ///
    /// Discovery is a fixed-window scan: every `MIN_LENGTH`-token window
    /// across the sample is hashed; windows occurring in at least
    /// `min_occurrences` distinct segments are promoted as substrings.
    ///
    /// New segments stored after this call automatically use the
    /// registered substrings if the resulting `0x05` frame is smaller
    /// than the baseline.
    pub async fn train_substrings(
        &self,
        sample_count: usize,
        min_occurrences: u32,
    ) -> Result<Vec<SubstringInfo>, StowkenError> {
        let samples = self.collect_substring_samples(sample_count).await?;
        if samples.is_empty() {
            return Ok(Vec::new());
        }

        // For each MIN_LENGTH-token window, count distinct segments
        // observed it. We dedup within a single segment (a window seen
        // 5x in one segment counts as 1 occurrence) by collecting per-
        // segment hashes into a set first.
        let registry = Arc::clone(&self.substring_registry);
        let report = task::spawn_blocking(move || {
            discover_substrings(&samples, min_occurrences, &registry)
        })
        .await
        .map_err(|e| StowkenError::Internal(e.to_string()))??;

        info!(
            promoted = report.len(),
            "trained substring registry"
        );
        Ok(report)
    }

    /// Pull at most `count` recent segments (decompressed) for substring
    /// discovery. Skips delta segments — their tokens are derivable but
    /// they're already a near-dedup variant, so promoting substrings from
    /// them would double-count.
    async fn collect_substring_samples(&self, count: usize) -> Result<Vec<Vec<Token>>, StowkenError> {
        let query = AnalyticsQuery::default();
        let ids = self.backend.list_conversations(&query, count as u64, 0).await?;

        let mut samples: Vec<Vec<Token>> = Vec::with_capacity(count);
        for id in &ids {
            if samples.len() >= count {
                break;
            }
            let manifest = match self.backend.get_manifest(id).await {
                Ok(m) => m,
                Err(_) => continue,
            };
            for seg_ref in &manifest.segments {
                if samples.len() >= count {
                    break;
                }
                let stored = match self.backend.get_segment(&seg_ref.hash).await {
                    Ok(s) => s,
                    Err(_) => continue,
                };
                if stored.compressed_data.first() == Some(&(FrameVersion::Delta as u8)) {
                    continue;
                }
                let tokens = match self.decompress_segment_async(&stored.compressed_data).await {
                    Ok(t) => t,
                    Err(_) => continue,
                };
                if tokens.len() >= SUBSTR_MIN_LEN {
                    samples.push(tokens);
                }
            }
        }
        Ok(samples)
    }

    // ── Dictionary management ──────────────────────────────────────────────

    /// Minimum samples required to train a dictionary. The zstd builder
    /// itself wants at least a handful; below this we'd produce a dict that
    /// generalises poorly and isn't worth the frame overhead.
    pub const MIN_TRAINING_SAMPLES: usize = 10;

    /// Train a new zstd dictionary from `sample_count` segments pulled from
    /// the backend (most-recent conversations first), register it, and return
    /// the new dict's metadata. The dict is NOT activated — call
    /// `activate_dictionary` to have new compressions use it.
    ///
    /// Use `train_and_activate_dictionary` for the common case.
    pub async fn train_dictionary(&self, sample_count: usize) -> Result<DictInfo, StowkenError> {
        let samples = self.collect_training_samples(sample_count).await?;
        if samples.len() < Self::MIN_TRAINING_SAMPLES {
            return Err(StowkenError::InsufficientTrainingSamples(
                samples.len(),
                Self::MIN_TRAINING_SAMPLES,
            ));
        }

        // 110 KiB is the zstd-recommended dict size for token-shaped data.
        let dict_bytes = task::spawn_blocking(move || {
            let refs: Vec<&[u8]> = samples.iter().map(Vec::as_slice).collect();
            zstd::dict::from_samples(&refs, 112_640)
        })
        .await
        .map_err(|e| StowkenError::Internal(e.to_string()))?
        .map_err(|e| StowkenError::Compression(format!("dict training: {e}")))?;

        let actual_samples = sample_count as u32;
        let info = self.dict_registry.register(dict_bytes, actual_samples)?;
        info!(dict_id = info.id, samples = actual_samples, "trained dictionary");
        Ok(info)
    }

    /// Activate `dict_id` so new compressions use it. Old frames remain
    /// readable as long as their dictionary stays in the registry.
    pub async fn activate_dictionary(&self, dict_id: DictId) -> Result<(), StowkenError> {
        self.dict_registry.activate(dict_id)?;
        info!(dict_id, "activated dictionary");
        Ok(())
    }

    /// Convenience: train a dictionary and immediately activate it.
    pub async fn train_and_activate_dictionary(
        &self,
        sample_count: usize,
    ) -> Result<DictInfo, StowkenError> {
        let info = self.train_dictionary(sample_count).await?;
        self.activate_dictionary(info.id).await?;
        Ok(DictInfo { is_active: true, ..info })
    }

    /// All known dictionaries, sorted by creation time. Each entry's
    /// `is_active` flag indicates which is the writer.
    pub fn list_dictionaries(&self) -> Vec<DictInfo> {
        self.dict_registry.list()
    }

    /// Read access to the dictionary registry — exposed for advanced use
    /// (e.g. the CLI inspector). Most callers should use the methods above.
    pub fn dict_registry(&self) -> &Arc<DictRegistry> {
        &self.dict_registry
    }

    /// Pull at most `count` varint-encoded segment payloads from the backend
    /// for use as dictionary training samples. We decompress each segment
    /// and re-encode just the varint stream — the zstd dict-builder works
    /// best on the data that will *actually* be compressed in production,
    /// which is the varint output (not the zstd output, not the raw tokens).
    async fn collect_training_samples(&self, count: usize) -> Result<Vec<Vec<u8>>, StowkenError> {
        let query = AnalyticsQuery::default();
        let ids = self.backend.list_conversations(&query, count as u64, 0).await?;

        let mut samples: Vec<Vec<u8>> = Vec::with_capacity(count);
        for id in &ids {
            if samples.len() >= count {
                break;
            }
            let manifest = match self.backend.get_manifest(id).await {
                Ok(m) => m,
                Err(_) => continue,
            };
            for seg_ref in &manifest.segments {
                if samples.len() >= count {
                    break;
                }
                let stored = match self.backend.get_segment(&seg_ref.hash).await {
                    Ok(s) => s,
                    Err(_) => continue,
                };
                let tokens = match self.decompress_data(&stored.compressed_data) {
                    Ok(t) => t,
                    Err(_) => continue,
                };
                samples.push(crate::compression::varint::encode_tokens(&tokens));
            }
        }
        Ok(samples)
    }

    /// Rebuild the SQLite metadata index from scratch by walking every
    /// manifest in the backend. Manifests are the source of truth; the
    /// index is a derived cache.
    ///
    /// Use this after a corrupted or deleted `metadata.db`, after restoring
    /// a vault from a backup that didn't include the index, or to reconcile
    /// the index when you suspect drift.
    ///
    /// Does not touch backend ref-counts. Reindex makes the SQL state match
    /// what the manifests actually reference; running `gc` afterwards will
    /// clean up any orphaned segments in the backend whose ref-counts
    /// drifted independently.
    pub async fn reindex(&self) -> Result<crate::types::ReindexStats, StowkenError> {
        use std::collections::HashSet;

        // Wipe the existing index in one transaction.
        let index = self.index.clone();
        task::spawn_blocking(move || index.clear())
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))??;

        // Replay every manifest through the same batch path that store() uses.
        let query = AnalyticsQuery::default();
        let ids = self
            .backend
            .list_conversations(&query, u64::MAX, 0)
            .await?;

        let mut seen: HashSet<SegmentHash> = HashSet::new();
        let mut conversations_indexed = 0u64;
        let mut unique_segments_indexed = 0u64;
        let mut segments_missing = 0u64;
        // Track all unique non-delta segments so we can replay the LSH index
        // pass after the SQL-state pass. Done in two phases because the
        // batch-insert path commits per-conversation and we don't want LSH
        // upserts interleaved with that.
        let mut lsh_targets: Vec<SegmentHash> = Vec::new();

        for id in &ids {
            let manifest = match self.backend.get_manifest(id).await {
                Ok(m) => m,
                Err(_) => continue,
            };

            let mut index_ops: Vec<BatchSegmentOp> =
                Vec::with_capacity(manifest.segments.len());

            for seg_ref in &manifest.segments {
                if seen.insert(seg_ref.hash.clone()) {
                    // First time seeing this hash this run — read its meta
                    // from the backend so the segments_meta row gets the
                    // correct compressed_size, raw_size, etc.
                    match self.backend.get_segment(&seg_ref.hash).await {
                        Ok(stored) => {
                            // Schedule for LSH reindex if this is a real
                            // (non-delta) frame — only canonicals get
                            // signatures.
                            let is_delta = stored.compressed_data.first()
                                == Some(&(crate::compression::FrameVersion::Delta as u8));
                            if !is_delta {
                                lsh_targets.push(seg_ref.hash.clone());
                            }
                            // Force ref_count=1 in the BatchSegmentOp::Upsert
                            // payload; subsequent IncrementRef ops will bump
                            // it as we encounter further references.
                            let mut s = stored;
                            s.ref_count = 1;
                            index_ops.push(BatchSegmentOp::Upsert(s));
                            unique_segments_indexed += 1;
                        }
                        Err(_) => {
                            segments_missing += 1;
                            continue;
                        }
                    }
                } else {
                    index_ops.push(BatchSegmentOp::IncrementRef(seg_ref.hash.clone()));
                }
            }

            let index = self.index.clone();
            let manifest_clone = manifest.clone();
            task::spawn_blocking(move || {
                index.store_conversation_batch(&index_ops, &manifest_clone)
            })
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))??;

            conversations_indexed += 1;
        }

        // Phase 2: rebuild the LSH index for non-delta segments. Done
        // unconditionally so the index is correct even when near-dedup
        // is currently disabled — re-enabling later won't require another
        // reindex.
        for hash in &lsh_targets {
            let stored = match self.backend.get_segment(hash).await {
                Ok(s) => s,
                Err(_) => continue,
            };
            let tokens = match self.decompress_segment_async(&stored.compressed_data).await {
                Ok(t) => t,
                Err(_) => continue,
            };
            self.upsert_lsh_entry(hash, &tokens).await?;
        }

        info!(
            conversations = conversations_indexed,
            segments = unique_segments_indexed,
            missing = segments_missing,
            lsh = lsh_targets.len(),
            "reindexed metadata from manifests"
        );

        Ok(crate::types::ReindexStats {
            conversations_indexed,
            unique_segments_indexed,
            segments_missing,
        })
    }

    // ── Helpers ───────────────────────────────────────────────────────────

    /// Compress `segment` and store it as a fresh blob (no near-dedup).
    /// Tracking counters are updated in place; the BatchSegmentOp is pushed
    /// into `index_ops`. Caller increments `new_segments`.
    async fn store_new_segment(
        &self,
        hash: &SegmentHash,
        segment: &crate::types::Segment,
        tokenizer: &str,
        index_ops: &mut Vec<BatchSegmentOp>,
        total_compressed: &mut u64,
        total_raw: &mut u64,
    ) -> Result<(), StowkenError> {
        // Always start with the baseline frame (0x02 / 0x03 / 0x01).
        let baseline = self.compress_tokens(&segment.tokens)?;
        // Try the substring-frame path; keep it only if the result beats
        // the baseline.
        let compressed = match self
            .compressor
            .try_compress_substring_frame(&segment.tokens, baseline.len())
            .map_err(|e| StowkenError::Compression(e.to_string()))?
        {
            Some(substring_frame) => substring_frame,
            None => baseline,
        };

        let raw_size = (segment.tokens.len() as u32) * 4;
        let compressed_size = compressed.len() as u32;
        *total_compressed += compressed_size as u64;
        *total_raw += raw_size as u64;

        let stored = StoredSegment {
            hash: hash.clone(),
            segment_type: segment.segment_type.clone(),
            tokenizer: tokenizer.to_owned(),
            token_count: segment.tokens.len() as u32,
            compressed_data: compressed,
            raw_size,
            compressed_size,
            ref_count: 1,
            created_at: chrono::Utc::now(),
        };
        self.backend.put_segment(&stored).await?;
        index_ops.push(BatchSegmentOp::Upsert(stored));
        Ok(())
    }

    /// Probe the LSH index for a near-duplicate canonical and, if found,
    /// build a 0x04 delta frame for `tokens`. Returns `None` if no
    /// candidate clears the threshold.
    async fn try_near_dedup(
        &self,
        variant_hash: &SegmentHash,
        tokens: &[Token],
        threshold: f64,
    ) -> Result<Option<NearDedupOutcome>, StowkenError> {
        let signature = MinHashSignature::compute(tokens);
        let band_hashes: Vec<u64> =
            (0..near_dedup::BANDS).map(|b| signature.band_hash(b)).collect();

        // Pull LSH candidates from SQLite.
        let index = self.index.clone();
        let band_hashes_clone = band_hashes.clone();
        let candidates = task::spawn_blocking(move || {
            index.find_near_dedup_candidates(&band_hashes_clone)
        })
        .await
        .map_err(|e| StowkenError::Internal(e.to_string()))??;

        // Fetch all candidate signatures in one query, then score in memory.
        let to_score: Vec<SegmentHash> = candidates
            .into_iter()
            .filter(|h| h != variant_hash)
            .collect();

        let mut best: Option<(SegmentHash, f64)> = None;
        if !to_score.is_empty() {
            let index = self.index.clone();
            let to_score_clone = to_score.clone();
            let sig_map = task::spawn_blocking(move || {
                index.get_near_dedup_signatures_batch(&to_score_clone)
            })
            .await
            .map_err(|e| StowkenError::Internal(e.to_string()))??;

            for candidate_hash in to_score {
                let Some(bytes) = sig_map.get(&candidate_hash) else { continue };
                let candidate_sig = MinHashSignature::from_bytes(bytes)
                    .map_err(|e| StowkenError::Internal(format!("LSH signature corrupt: {e}")))?;
                let sim = signature.jaccard(&candidate_sig);
                if sim >= threshold && best.as_ref().is_none_or(|(_, prev)| sim > *prev) {
                    best = Some((candidate_hash, sim));
                }
            }
        }

        let Some((canonical_hash, _sim)) = best else { return Ok(None) };

        // Materialise the canonical's tokens to compute a delta against.
        // Bypass our own decompress path's 0x04 handler — we only delta
        // against true canonicals, never against existing deltas.
        let canonical_stored = self.backend.get_segment(&canonical_hash).await?;
        if canonical_stored.compressed_data.first() == Some(&(FrameVersion::Delta as u8)) {
            // The "canonical" we matched is itself a delta. Skip — don't
            // chain deltas. (Future v0.4.x optimization: hop through the
            // chain to find the root canonical.)
            return Ok(None);
        }
        let canonical_tokens = self.compressor.decompress(&canonical_stored.compressed_data)
            .map_err(|e| StowkenError::Compression(e.to_string()))?;

        let ops = near_dedup::compute_delta(&canonical_tokens, tokens);
        let frame = self.compressor.compress_delta(&canonical_hash, &ops)
            .map_err(|e| StowkenError::Compression(e.to_string()))?;

        // Only accept the delta if it's actually smaller than a fresh full
        // compression. Otherwise we'd be paying for the cross-segment ref
        // (extra ref bump, can't GC canonical) for negative gain.
        let full_size = self.compress_tokens(tokens)?.len();
        if frame.len() >= full_size {
            return Ok(None);
        }

        Ok(Some(NearDedupOutcome { canonical_hash, delta_frame: frame }))
    }

    /// Persist a segment's signature + LSH band hashes so future incoming
    /// segments can find this one as a candidate.
    async fn upsert_lsh_entry(
        &self,
        hash: &SegmentHash,
        tokens: &[Token],
    ) -> Result<(), StowkenError> {
        let signature = MinHashSignature::compute(tokens);
        let sig_bytes = signature.to_bytes();
        let band_hashes: Vec<u64> =
            (0..near_dedup::BANDS).map(|b| signature.band_hash(b)).collect();

        let index = self.index.clone();
        let h = hash.clone();
        task::spawn_blocking(move || {
            index.upsert_near_dedup_entry(&h, &sig_bytes, &band_hashes)
        })
        .await
        .map_err(|e| StowkenError::Internal(e.to_string()))??;
        Ok(())
    }

    fn make_segmenter(&self, tokenizer_name: &str) -> Segmenter {
        let seg = Segmenter::new(self.segmenter_config.clone());
        if let Some(ref adapter) = self.tokenizer {
            seg.with_tokenizer(Arc::clone(adapter))
        } else if let Some(adapter) = crate::tokenizer::get_tokenizer(tokenizer_name) {
            seg.with_tokenizer(Arc::from(adapter))
        } else {
            seg
        }
    }

    /// Compress a token sequence using the active pipeline.
    pub fn compress_tokens(&self, tokens: &[Token]) -> Result<Vec<u8>, StowkenError> {
        self.compressor
            .compress(tokens)
            .map_err(|e| StowkenError::Compression(e.to_string()))
    }

    /// Decompress stored bytes back to a token sequence using the active
    /// pipeline. Synchronous: cannot resolve `0x04` (delta) frames since
    /// resolving the canonical requires backend I/O. Use
    /// `decompress_segment_async` for retrieval paths.
    pub fn decompress_data(&self, data: &[u8]) -> Result<Vec<Token>, StowkenError> {
        self.compressor
            .decompress(data)
            .map_err(|e| StowkenError::Compression(e.to_string()))
    }

    /// Decompress any frame, including `0x04` deltas. For deltas, the
    /// canonical is fetched from the backend and the delta applied.
    /// Used by `retrieve` and the LSH reindex path.
    ///
    /// Box the future because decoding a delta against a canonical that is
    /// itself a delta would otherwise create an infinitely-sized async
    /// state machine. (We currently refuse to chain deltas, but the
    /// boxing keeps the code-path safe even if that policy ever changes.)
    pub fn decompress_segment_async<'a>(
        &'a self,
        data: &'a [u8],
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Vec<Token>, StowkenError>> + Send + 'a>>
    {
        Box::pin(async move {
            let canonical_hash = self
                .compressor
                .frame_canonical_hash(data)
                .map_err(|e| StowkenError::Compression(e.to_string()))?;

            match canonical_hash {
                None => self.decompress_data(data),
                Some(canonical_hash) => {
                    let canonical_stored = self.backend.get_segment(&canonical_hash).await?;
                    let canonical_tokens = self
                        .decompress_segment_async(&canonical_stored.compressed_data)
                        .await?;

                    // Frame body = 1 byte version + 32 byte hash + delta ops.
                    let delta_bytes = &data[33..];
                    let ops = near_dedup::decode_delta(delta_bytes)
                        .map_err(|e| StowkenError::Compression(e.to_string()))?;
                    near_dedup::apply_delta(&canonical_tokens, &ops)
                        .map_err(|e| StowkenError::Compression(e.to_string()))
                }
            }
        })
    }

    /// Read access to the storage backend.
    pub fn backend(&self) -> &B {
        &self.backend
    }

    /// Read access to the metadata index.
    pub fn index(&self) -> &MetadataIndex {
        &self.index
    }
}