semantic-memory 0.6.0

Hybrid semantic search with SQLite, FTS5, and HNSW — built for AI agents
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
//! Vector codec profile and artifact boundary.
//!
//! Codecs are derived acceleration/transport helpers only. SQLite memory rows
//! and raw f32 embeddings remain authoritative and every artifact must carry a
//! profile digest that can be checked before decoding.

use crate::{db, quantize, MemoryError};
use serde::{Deserialize, Serialize};
use stack_ids::{ContentDigest, DigestBuilder};

const PROFILE_SCHEMA_V1: &str = "vector_codec_profile_v1";
const ARTIFACT_SCHEMA_V1: &str = "vector_artifact_v1";

fn b3_digest(bytes: &[u8]) -> String {
    format!("blake3:{}", ContentDigest::compute(bytes).hex())
}

fn b3_json_digest<T: Serialize>(domain: &str, value: &T) -> String {
    let mut builder = DigestBuilder::new();
    builder.update_str(domain).separator();
    match builder.update_json(value) {
        Ok(_) => format!("blake3:{}", builder.finalize().hex()),
        Err(_) => b3_digest(format!("{domain}:digest-fallback").as_bytes()),
    }
}

fn dim_u32(dim: usize) -> Result<u32, MemoryError> {
    u32::try_from(dim).map_err(|_| MemoryError::InvalidConfig {
        field: "embedding.dimensions",
        reason: format!("dimension {dim} does not fit vector codec profile u32"),
    })
}

fn dim_usize(dim: u32) -> usize {
    dim as usize
}

/// Stable identity for a vector codec configuration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VectorCodecProfileV1 {
    /// Stable schema marker for this profile format.
    pub schema_version: String,
    /// Codec implementation identifier.
    pub codec: String,
    /// Vector dimensionality. Persisted as fixed-width `u32`, never `usize`.
    pub dim: u32,
    /// Effective encoded bits per scalar, when applicable.
    pub bits: u8,
    /// Number of projection vectors, reserved for future derived codecs.
    pub projections: Option<u32>,
    /// Codec seed, reserved for randomized derived codecs.
    pub seed: Option<u64>,
    /// Codec wire/algorithm version.
    pub codec_version: String,
    /// Declared scoring semantics for decoded/reference comparison.
    pub scoring_semantics: String,
    /// Declared vector normalization contract.
    pub normalization: String,
}

impl VectorCodecProfileV1 {
    /// Build the reference raw f32 profile.
    pub fn raw_f32(dim: usize) -> Result<Self, MemoryError> {
        Ok(Self {
            schema_version: PROFILE_SCHEMA_V1.into(),
            codec: "raw_f32".into(),
            dim: dim_u32(dim)?,
            bits: 32,
            projections: None,
            seed: None,
            codec_version: "1".into(),
            scoring_semantics: "cosine_on_decoded_f32".into(),
            normalization: "caller_supplied".into(),
        })
    }

    /// Build the existing per-vector scalar quantization profile.
    pub fn sq8(dim: usize) -> Result<Self, MemoryError> {
        Ok(Self {
            schema_version: PROFILE_SCHEMA_V1.into(),
            codec: "sq8".into(),
            dim: dim_u32(dim)?,
            bits: 8,
            projections: None,
            seed: None,
            codec_version: "1".into(),
            scoring_semantics: "cosine_on_dequantized_f32".into(),
            normalization: "caller_supplied".into(),
        })
    }

    /// Build a TurboQuant profile.
    #[cfg(feature = "turbo-quant-codec")]
    pub fn turbo_quant(
        dim: usize,
        bits: u8,
        projections: usize,
        seed: u64,
    ) -> Result<Self, MemoryError> {
        Ok(Self {
            schema_version: PROFILE_SCHEMA_V1.into(),
            codec: "turbo_quant".into(),
            dim: dim_u32(dim)?,
            bits,
            projections: Some(dim_u32(projections)?),
            seed: Some(seed),
            codec_version: "turbo-quant:0.2.0-alpha.1".into(),
            scoring_semantics: "inner_product_estimate".into(),
            normalization: "caller_supplied".into(),
        })
    }

    /// Build a FibQuant profile.
    #[cfg(feature = "fib-quant-codec")]
    pub fn fib_quant(dim: usize) -> Result<Self, MemoryError> {
        Ok(Self {
            schema_version: PROFILE_SCHEMA_V1.into(),
            codec: "fib_quant".into(),
            dim: dim_u32(dim)?,
            bits: 8,
            projections: None,
            seed: None,
            codec_version: "fib-quant:0.1.0-beta.3".into(),
            scoring_semantics: "inner_product_estimate".into(),
            normalization: "caller_supplied".into(),
        })
    }

    /// Stable digest over the explicit profile fields.
    pub fn digest(&self) -> String {
        b3_json_digest(PROFILE_SCHEMA_V1, self)
    }
}

/// Persistable encoded vector plus the profile identity required to decode it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VectorArtifactV1 {
    /// Stable schema marker for this artifact format.
    pub schema_version: String,
    /// Full codec profile used to produce `encoded`.
    pub profile: VectorCodecProfileV1,
    /// Digest of `profile`; checked before decode.
    pub profile_digest: String,
    /// Digest of `encoded`; checked before decode when present.
    #[serde(default)]
    pub artifact_digest: String,
    /// Codec-owned encoded bytes.
    pub encoded: Vec<u8>,
}

impl VectorArtifactV1 {
    /// Construct a new artifact and stamp the profile digest.
    pub fn new(profile: VectorCodecProfileV1, encoded: Vec<u8>) -> Self {
        let profile_digest = profile.digest();
        let artifact_digest = b3_digest(&encoded);
        Self {
            schema_version: ARTIFACT_SCHEMA_V1.into(),
            profile,
            profile_digest,
            artifact_digest,
            encoded,
        }
    }

    /// Stable digest over encoded artifact bytes.
    pub fn encoded_digest(&self) -> String {
        b3_digest(&self.encoded)
    }
}

/// INT-001: Object-safe vector codec boundary for derived vector artifacts.
///
/// This trait is the semantic-memory consumer-facing boundary. It should
/// eventually be replaced by direct consumption of the canonical
/// `quant_codec_core::VectorCodec` trait. Until then, implementations of
/// this trait should also implement the canonical trait where practical.
pub trait VectorCodec: Send + Sync {
    /// Codec profile identity.
    fn profile(&self) -> &VectorCodecProfileV1;

    /// INT-001: Codec capabilities — what operations this codec supports.
    fn capabilities(&self) -> CodecCapabilityInfo {
        CodecCapabilityInfo::default()
    }

    /// Encode a raw f32 vector into a byte artifact.
    fn encode(&self, vector: &[f32]) -> Result<VectorArtifactV1, MemoryError>;

    /// Decode an artifact back to f32 for reference scoring or differential tests.
    fn decode(&self, artifact: &VectorArtifactV1) -> Result<Vec<f32>, MemoryError>;
}

/// INT-001: Codec capability information for semantic-memory consumers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CodecCapabilityInfo {
    /// Codec can estimate inner product from compressed form.
    pub can_score_inner_product: bool,
    /// Codec is lossless (raw f32 representation).
    pub is_lossless: bool,
}

fn validate_artifact_profile(
    expected: &VectorCodecProfileV1,
    artifact: &VectorArtifactV1,
) -> Result<(), MemoryError> {
    let artifact_profile_digest = artifact.profile.digest();
    if artifact.profile_digest != artifact_profile_digest {
        return Err(MemoryError::VectorCodecProfileMismatch {
            expected_digest: artifact_profile_digest,
            actual_digest: artifact.profile_digest.clone(),
        });
    }

    let expected_digest = expected.digest();
    if artifact.profile_digest != expected_digest {
        return Err(MemoryError::VectorCodecProfileMismatch {
            expected_digest,
            actual_digest: artifact.profile_digest.clone(),
        });
    }

    let encoded_digest = artifact.encoded_digest();
    if !artifact.artifact_digest.is_empty() && artifact.artifact_digest != encoded_digest {
        return Err(MemoryError::CorruptData {
            table: "vector_artifacts",
            row_id: artifact.profile_digest.clone(),
            detail: format!(
                "encoded artifact digest mismatch: expected {}, got {}",
                artifact.artifact_digest, encoded_digest
            ),
        });
    }

    Ok(())
}

/// Reference codec that stores raw little-endian f32 bytes.
#[derive(Debug, Clone)]
pub struct RawF32Codec {
    profile: VectorCodecProfileV1,
}

impl RawF32Codec {
    /// Create a raw f32 codec for `dim` dimensions.
    pub fn new(dim: usize) -> Result<Self, MemoryError> {
        Ok(Self {
            profile: VectorCodecProfileV1::raw_f32(dim)?,
        })
    }
}

impl VectorCodec for RawF32Codec {
    fn profile(&self) -> &VectorCodecProfileV1 {
        &self.profile
    }

    fn capabilities(&self) -> CodecCapabilityInfo {
        CodecCapabilityInfo {
            can_score_inner_product: false,
            is_lossless: true,
        }
    }

    fn encode(&self, vector: &[f32]) -> Result<VectorArtifactV1, MemoryError> {
        db::validate_embedding(vector, dim_usize(self.profile.dim))?;
        Ok(VectorArtifactV1::new(
            self.profile.clone(),
            db::encode_f32_le(vector),
        ))
    }

    fn decode(&self, artifact: &VectorArtifactV1) -> Result<Vec<f32>, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        db::decode_f32_le(&artifact.encoded, dim_usize(self.profile.dim))
    }
}

/// Codec wrapper around the existing per-vector SQ8 quantization path.
#[derive(Debug, Clone)]
pub struct Sq8Codec {
    profile: VectorCodecProfileV1,
}

impl Sq8Codec {
    /// Create an SQ8 codec for `dim` dimensions.
    pub fn new(dim: usize) -> Result<Self, MemoryError> {
        Ok(Self {
            profile: VectorCodecProfileV1::sq8(dim)?,
        })
    }
}

impl VectorCodec for Sq8Codec {
    fn profile(&self) -> &VectorCodecProfileV1 {
        &self.profile
    }

    fn capabilities(&self) -> CodecCapabilityInfo {
        CodecCapabilityInfo {
            can_score_inner_product: false,
            is_lossless: false,
        }
    }

    fn encode(&self, vector: &[f32]) -> Result<VectorArtifactV1, MemoryError> {
        db::validate_embedding(vector, dim_usize(self.profile.dim))?;
        let quantized = quantize::Quantizer::new(dim_usize(self.profile.dim)).quantize(vector)?;
        Ok(VectorArtifactV1::new(
            self.profile.clone(),
            quantize::pack_quantized(&quantized),
        ))
    }

    fn decode(&self, artifact: &VectorArtifactV1) -> Result<Vec<f32>, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        let quantized = quantize::unpack_quantized(&artifact.encoded, dim_usize(self.profile.dim))?;
        let decoded = quantize::Quantizer::new(dim_usize(self.profile.dim)).dequantize(&quantized);
        db::validate_embedding(&decoded, dim_usize(self.profile.dim))?;
        Ok(decoded)
    }
}

#[cfg(feature = "per-dim-codec")]
#[derive(Debug, Clone)]
pub struct PerDimCodec {
    profile: VectorCodecProfileV1,
    scorer: compressed_scorer::PerDimScorer,
}

#[cfg(feature = "per-dim-codec")]
impl PerDimCodec {
    pub fn new(dim: usize, bits: u8) -> Result<Self, MemoryError> {
        let scorer = compressed_scorer::PerDimScorer::new(dim, bits as u32)
            .map_err(|e| MemoryError::QuantizationError(format!("per-dim: {e}")))?;
        Ok(Self {
            profile: VectorCodecProfileV1 {
                schema_version: PROFILE_SCHEMA_V1.into(),
                codec: "per_dim".into(),
                dim: dim_u32(dim)?,
                bits,
                projections: None,
                seed: None,
                codec_version: "compressed-scorer:0.1.0".into(),
                scoring_semantics: "inner_product_estimate".into(),
                normalization: "caller_supplied".into(),
            },
            scorer,
        })
    }
    pub fn profile(&self) -> &VectorCodecProfileV1 {
        &self.profile
    }
    pub fn config_json(&self) -> Result<String, MemoryError> {
        serde_json::to_string(
            &serde_json::json!({"dim": self.profile.dim, "bits": self.profile.bits}),
        )
        .map_err(|e| MemoryError::Other(format!("serialize per-dim config: {e}")))
    }

    pub fn prepare_query(
        &self,
        query: &[f32],
    ) -> Result<compressed_scorer::PerDimPrepared, MemoryError> {
        use compressed_scorer::CompressedScorer;
        self.scorer
            .prepare_query(query)
            .map_err(|e| MemoryError::QuantizationError(format!("per-dim: {e}")))
    }
    pub fn score_inner_product_prepared(
        &self,
        artifact: &VectorArtifactV1,
        prepared: &compressed_scorer::PerDimPrepared,
    ) -> Result<f32, MemoryError> {
        use compressed_scorer::CompressedScorer;
        validate_artifact_profile(&self.profile, artifact)?;
        let c = self.decode_compressed(artifact)?;
        self.scorer
            .score_prepared(prepared, &c)
            .map_err(|e| MemoryError::QuantizationError(format!("per-dim: {e}")))
    }
    fn decode_compressed(
        &self,
        artifact: &VectorArtifactV1,
    ) -> Result<compressed_scorer::PerDimCompressed, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        if artifact.encoded.len() < 4 {
            return Err(MemoryError::CorruptData {
                table: "vector_artifacts",
                row_id: artifact.profile_digest.clone(),
                detail: "per-dim artifact too short".into(),
            });
        }
        Ok(compressed_scorer::PerDimCompressed {
            norm: f32::from_le_bytes(artifact.encoded[..4].try_into().unwrap()),
            codes: artifact.encoded[4..].to_vec(),
        })
    }
}

#[cfg(feature = "per-dim-codec")]
impl VectorCodec for PerDimCodec {
    fn profile(&self) -> &VectorCodecProfileV1 {
        &self.profile
    }
    fn capabilities(&self) -> CodecCapabilityInfo {
        CodecCapabilityInfo {
            can_score_inner_product: true,
            is_lossless: false,
        }
    }
    fn encode(&self, vector: &[f32]) -> Result<VectorArtifactV1, MemoryError> {
        use compressed_scorer::CompressedScorer;
        db::validate_embedding(vector, dim_usize(self.profile.dim))?;
        let c = self
            .scorer
            .compress(vector)
            .map_err(|e| MemoryError::QuantizationError(format!("per-dim: {e}")))?;
        let mut bytes = c.norm.to_le_bytes().to_vec();
        bytes.extend_from_slice(&c.codes);
        Ok(VectorArtifactV1::new(self.profile.clone(), bytes))
    }
    fn decode(&self, artifact: &VectorArtifactV1) -> Result<Vec<f32>, MemoryError> {
        use compressed_scorer::CompressedScorer;
        self.scorer
            .decode(&self.decode_compressed(artifact)?)
            .map_err(|e| MemoryError::QuantizationError(format!("per-dim: {e}")))
    }
}

#[cfg(feature = "turbo-quant-codec")]
fn map_turbo_error(err: turbo_quant::TurboQuantError) -> MemoryError {
    MemoryError::QuantizationError(format!("turbo-quant: {err}"))
}

/// Optional TurboQuant codec backend.
#[cfg(feature = "turbo-quant-codec")]
#[derive(Debug, Clone)]
pub struct TurboQuantCodec {
    profile: VectorCodecProfileV1,
    quantizer: turbo_quant::TurboQuantizer,
}

#[cfg(feature = "turbo-quant-codec")]
impl TurboQuantCodec {
    /// Create a TurboQuant codec profile.
    pub fn new(dim: usize, bits: u8, projections: usize, seed: u64) -> Result<Self, MemoryError> {
        let quantizer = turbo_quant::TurboQuantizer::new(dim, bits, projections, seed)
            .map_err(map_turbo_error)?;
        Ok(Self {
            profile: VectorCodecProfileV1::turbo_quant(dim, bits, projections, seed)?,
            quantizer,
        })
    }

    fn decode_code(
        &self,
        artifact: &VectorArtifactV1,
    ) -> Result<turbo_quant::TurboCode, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        self.quantizer
            .decode_code_from_bytes(&artifact.encoded)
            .map_err(map_turbo_error)
    }

    /// Estimate inner product between a raw query vector and a TurboQuant artifact.
    pub fn score_inner_product(
        &self,
        artifact: &VectorArtifactV1,
        query: &[f32],
    ) -> Result<f32, MemoryError> {
        db::validate_embedding(query, dim_usize(self.profile.dim))?;
        validate_artifact_profile(&self.profile, artifact)?;
        self.quantizer
            .score_inner_product_from_bytes(&artifact.encoded, query)
            .map_err(map_turbo_error)
    }

    /// Prepare a query once for scoring many TurboQuant artifacts.
    pub fn prepare_query(
        &self,
        query: &[f32],
    ) -> Result<turbo_quant::TurboProjectedQuery, MemoryError> {
        db::validate_embedding(query, dim_usize(self.profile.dim))?;
        self.quantizer.prepare_query(query).map_err(map_turbo_error)
    }

    /// Estimate inner product using a pre-projected query.
    pub fn score_inner_product_prepared(
        &self,
        artifact: &VectorArtifactV1,
        prepared: &turbo_quant::TurboProjectedQuery,
    ) -> Result<f32, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        let code = self.decode_code(artifact)?;
        self.quantizer
            .inner_product_estimate_prepared(&code, prepared)
            .map_err(map_turbo_error)
    }

    /// Estimate squared L2 distance between a raw query vector and a TurboQuant artifact.
    pub fn score_l2(&self, artifact: &VectorArtifactV1, query: &[f32]) -> Result<f32, MemoryError> {
        db::validate_embedding(query, dim_usize(self.profile.dim))?;
        validate_artifact_profile(&self.profile, artifact)?;
        let code = self.decode_code(artifact)?;
        self.quantizer
            .l2_distance_estimate(&code, query)
            .map_err(map_turbo_error)
    }
}

#[cfg(feature = "turbo-quant-codec")]
impl VectorCodec for TurboQuantCodec {
    fn profile(&self) -> &VectorCodecProfileV1 {
        &self.profile
    }

    fn capabilities(&self) -> CodecCapabilityInfo {
        CodecCapabilityInfo {
            can_score_inner_product: true,
            is_lossless: false,
        }
    }

    fn encode(&self, vector: &[f32]) -> Result<VectorArtifactV1, MemoryError> {
        db::validate_embedding(vector, dim_usize(self.profile.dim))?;
        let encoded = self
            .quantizer
            .encode_to_bytes(vector)
            .map_err(map_turbo_error)?;
        Ok(VectorArtifactV1::new(self.profile.clone(), encoded))
    }

    fn decode(&self, artifact: &VectorArtifactV1) -> Result<Vec<f32>, MemoryError> {
        let code = self.decode_code(artifact)?;
        let decoded = self
            .quantizer
            .decode_approximate(&code)
            .map_err(map_turbo_error)?;
        db::validate_embedding(&decoded, dim_usize(self.profile.dim))?;
        Ok(decoded)
    }
}

/// A query prepared for repeated FibQuant scoring.
#[cfg(feature = "fib-quant-codec")]
#[derive(Debug, Clone)]
pub struct FibQuantPreparedQuery {
    vector: Vec<f32>,
}

/// Optional FibQuant codec backend.
#[cfg(feature = "fib-quant-codec")]
#[derive(Debug, Clone)]
pub struct FibQuantCodec {
    profile: VectorCodecProfileV1,
    quantizer: fib_quant::FibQuantizer,
}

#[cfg(feature = "fib-quant-codec")]
fn map_fib_error(err: fib_quant::FibQuantError) -> MemoryError {
    MemoryError::QuantizationError(format!("fib-quant: {err}"))
}

#[cfg(feature = "fib-quant-codec")]
impl FibQuantCodec {
    pub fn codebook(&self) -> &fib_quant::FibCodebookV1 {
        self.quantizer.codebook()
    }

    pub fn gram_scorer(&self) -> Result<crate::scoring::fib_scorer::FibGramScorer, MemoryError> {
        crate::scoring::fib_scorer::FibGramScorer::from_quantizer(&self.quantizer)
    }

    pub fn encode_code(&self, vector: &[f32]) -> Result<fib_quant::FibCodeV1, MemoryError> {
        db::validate_embedding(vector, dim_usize(self.profile.dim))?;
        self.quantizer.encode(vector).map_err(map_fib_error)
    }

    pub fn code_from_artifact(
        &self,
        artifact: &VectorArtifactV1,
    ) -> Result<fib_quant::FibCodeV1, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        serde_json::from_slice(&artifact.encoded)
            .map_err(|e| MemoryError::QuantizationError(format!("fib-quant decode: {e}")))
    }

    pub fn from_codebook(codebook: fib_quant::FibCodebookV1) -> Result<Self, MemoryError> {
        let quantizer = fib_quant::FibQuantizer::from_codebook(codebook).map_err(map_fib_error)?;
        let dim = quantizer.profile().ambient_dim as usize;
        Ok(Self {
            profile: VectorCodecProfileV1::fib_quant(dim)?,
            quantizer,
        })
    }

    pub fn new(
        dim: usize,
        block_count: usize,
        gram_table_size: usize,
    ) -> Result<Self, MemoryError> {
        let fib_profile =
            fib_quant::FibQuantProfileV1::paper_default(dim, block_count, gram_table_size, 0)
                .map_err(map_fib_error)?;
        let quantizer = fib_quant::FibQuantizer::new(fib_profile).map_err(map_fib_error)?;
        Ok(Self {
            profile: VectorCodecProfileV1::fib_quant(dim)?,
            quantizer,
        })
    }

    pub fn prepare_query(&self, query: &[f32]) -> Result<FibQuantPreparedQuery, MemoryError> {
        db::validate_embedding(query, dim_usize(self.profile.dim))?;
        Ok(FibQuantPreparedQuery {
            vector: query.to_vec(),
        })
    }

    pub fn score_inner_product_prepared(
        &self,
        artifact: &VectorArtifactV1,
        prepared: &FibQuantPreparedQuery,
    ) -> Result<f32, MemoryError> {
        let decoded = self.decode(artifact)?;
        let score = fib_quant::metrics::cosine_similarity(&prepared.vector, &decoded)
            .map_err(map_fib_error)?;
        Ok(score as f32)
    }

    pub fn score_inner_product(
        &self,
        artifact: &VectorArtifactV1,
        query: &[f32],
    ) -> Result<f32, MemoryError> {
        let prepared = self.prepare_query(query)?;
        self.score_inner_product_prepared(artifact, &prepared)
    }
}

#[cfg(feature = "fib-quant-codec")]
impl VectorCodec for FibQuantCodec {
    fn profile(&self) -> &VectorCodecProfileV1 {
        &self.profile
    }
    fn capabilities(&self) -> CodecCapabilityInfo {
        CodecCapabilityInfo {
            can_score_inner_product: true,
            is_lossless: false,
        }
    }

    fn encode(&self, vector: &[f32]) -> Result<VectorArtifactV1, MemoryError> {
        db::validate_embedding(vector, dim_usize(self.profile.dim))?;
        let code = self.encode_code(vector)?;
        let encoded = serde_json::to_vec(&code)
            .map_err(|e| MemoryError::QuantizationError(format!("fib-quant encode: {e}")))?;
        Ok(VectorArtifactV1::new(self.profile.clone(), encoded))
    }

    fn decode(&self, artifact: &VectorArtifactV1) -> Result<Vec<f32>, MemoryError> {
        validate_artifact_profile(&self.profile, artifact)?;
        let code = self.code_from_artifact(artifact)?;
        let decoded = self.quantizer.decode(&code).map_err(map_fib_error)?;
        db::validate_embedding(&decoded, dim_usize(self.profile.dim))?;
        Ok(decoded)
    }
}