semantic-memory 0.5.1

Local-first hybrid semantic search (SQLite + FTS5 + usearch 2.25) with bitemporal truth and typed receipts
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
use crate::error::MemoryError;
use crate::types::{
    ProjectionClaimVersion, ProjectionEntityAlias, ProjectionEpisode, ProjectionEvidenceRef,
    ProjectionQuery, ProjectionRelationVersion,
};
use stack_ids::{
    ClaimId, ClaimVersionId, EntityId, EnvelopeId, EpisodeId, RelationVersionId, ScopeKey,
};

fn projection_scan_limit(query: &ProjectionQuery) -> usize {
    let base = query.limit.max(1);
    base.saturating_mul(8).min(256).max(base)
}

fn query_terms(text_query: Option<&str>) -> Vec<String> {
    text_query
        .unwrap_or("")
        .split_whitespace()
        .map(|term| term.trim().to_lowercase())
        .filter(|term| !term.is_empty())
        .collect()
}

fn optional_search_token(value: Option<&str>) -> &str {
    value.unwrap_or("")
}

fn text_score(terms: &[String], searchable: &str) -> usize {
    if terms.is_empty() {
        return 1;
    }
    let haystack = searchable.to_lowercase();
    terms
        .iter()
        .filter(|term| haystack.contains(term.as_str()))
        .count()
}

fn decode_scope_key(
    namespace: String,
    domain: Option<String>,
    workspace_id: Option<String>,
    repo_id: Option<String>,
) -> ScopeKey {
    ScopeKey {
        namespace,
        domain,
        workspace_id,
        repo_id,
    }
}

fn decode_json_value(
    table: &'static str,
    row_id: &str,
    column: &'static str,
    raw: &str,
) -> Result<serde_json::Value, MemoryError> {
    serde_json::from_str(raw).map_err(|err| MemoryError::CorruptData {
        table,
        row_id: row_id.to_string(),
        detail: format!("invalid {column} JSON: {err}"),
    })
}

fn decode_optional_json_value(
    table: &'static str,
    row_id: &str,
    column: &'static str,
    raw: Option<String>,
) -> Result<Option<serde_json::Value>, MemoryError> {
    raw.map(|value| decode_json_value(table, row_id, column, &value))
        .transpose()
}

fn decode_string_vec(
    table: &'static str,
    row_id: &str,
    column: &'static str,
    raw: &str,
) -> Result<Vec<String>, MemoryError> {
    serde_json::from_str(raw).map_err(|err| MemoryError::CorruptData {
        table,
        row_id: row_id.to_string(),
        detail: format!("invalid {column} JSON: {err}"),
    })
}

/// Query imported claim versions with full scope and temporal filters.
pub(crate) fn query_claim_versions(
    conn: &rusqlite::Connection,
    query: &ProjectionQuery,
) -> Result<Vec<ProjectionClaimVersion>, MemoryError> {
    if query.limit == 0 {
        return Ok(Vec::new());
    }

    let scan_limit = projection_scan_limit(query);
    let terms = query_terms(query.text_query.as_deref());
    let mut stmt = conn.prepare(
        "SELECT cv.claim_version_id, cv.claim_id, cv.claim_state, cv.projection_family,
                cv.subject_entity_id, cv.predicate, cv.object_anchor,
                cv.scope_namespace, cv.scope_domain, cv.scope_workspace_id, cv.scope_repo_id,
                cv.valid_from, cv.valid_to, cv.recorded_at, cv.preferred_open,
                cv.source_envelope_id, cv.source_authority, cv.trace_id,
                cv.freshness, cv.contradiction_status, cv.supersedes_claim_version_id,
                cv.content, cv.confidence, cv.metadata,
                pil.source_exported_at, pil.transformed_at
         FROM claim_versions cv
         LEFT JOIN projection_import_log pil
           ON pil.source_envelope_id = cv.source_envelope_id
          AND pil.imported_at = cv.recorded_at
         WHERE cv.scope_namespace = ?1
           AND ((?2 IS NULL AND cv.scope_domain IS NULL) OR cv.scope_domain = ?2)
           AND ((?3 IS NULL AND cv.scope_workspace_id IS NULL) OR cv.scope_workspace_id = ?3)
           AND ((?4 IS NULL AND cv.scope_repo_id IS NULL) OR cv.scope_repo_id = ?4)
           AND (?5 IS NULL OR cv.subject_entity_id = ?5)
           AND (?6 IS NULL OR cv.claim_state = ?6)
           AND (?7 IS NULL OR cv.claim_id = ?7)
           AND (?8 IS NULL OR cv.claim_version_id = ?8)
           AND (?9 IS NULL OR cv.recorded_at <= ?9)
           AND (?10 IS NULL OR ((cv.valid_from IS NULL OR cv.valid_from <= ?10)
                            AND (cv.valid_to IS NULL OR cv.valid_to > ?10)))
         ORDER BY cv.preferred_open DESC, cv.recorded_at DESC
         LIMIT ?11",
    )?;
    let mut rows = stmt.query(rusqlite::params![
        query.scope.namespace.as_str(),
        query.scope.domain.as_deref(),
        query.scope.workspace_id.as_deref(),
        query.scope.repo_id.as_deref(),
        query.subject_entity_id.as_ref().map(|id| id.as_str()),
        query.claim_state.as_deref(),
        query.claim_id.as_ref().map(|id| id.as_str()),
        query.claim_version_id.as_ref().map(|id| id.as_str()),
        query.recorded_at_or_before.as_deref(),
        query.valid_at.as_deref(),
        scan_limit as i64,
    ])?;

    let mut results = Vec::new();
    while let Some(row) = rows.next()? {
        let claim_version_id = row.get::<_, String>(0)?;
        let claim_id = row.get::<_, String>(1)?;
        let subject_entity_id = row.get::<_, String>(4)?;
        let object_anchor_raw = row.get::<_, String>(6)?;
        let searchable = format!(
            "{} {} {} {} {}",
            row.get::<_, String>(21)?,
            row.get::<_, String>(5)?,
            subject_entity_id,
            object_anchor_raw,
            claim_id
        );
        let score = text_score(&terms, &searchable);
        if !terms.is_empty() && score == 0 {
            continue;
        }

        results.push((
            score,
            ProjectionClaimVersion {
                claim_version_id: ClaimVersionId::new(claim_version_id.clone()),
                claim_id: ClaimId::new(claim_id.clone()),
                claim_state: row.get(2)?,
                projection_family: row.get(3)?,
                subject_entity_id: EntityId::new(subject_entity_id.clone()),
                predicate: row.get(5)?,
                object_anchor: decode_json_value(
                    "claim_versions",
                    &claim_version_id,
                    "object_anchor",
                    &object_anchor_raw,
                )?,
                scope_key: decode_scope_key(row.get(7)?, row.get(8)?, row.get(9)?, row.get(10)?),
                valid_from: row.get(11)?,
                valid_to: row.get(12)?,
                recorded_at: row.get(13)?,
                preferred_open: row.get::<_, i32>(14)? != 0,
                source_envelope_id: EnvelopeId::new(row.get::<_, String>(15)?),
                source_authority: row.get(16)?,
                trace_id: row.get(17)?,
                freshness: row.get(18)?,
                contradiction_status: row.get(19)?,
                supersedes_claim_version_id: row
                    .get::<_, Option<String>>(20)?
                    .map(ClaimVersionId::new),
                content: row.get(21)?,
                confidence: row.get(22)?,
                metadata: decode_optional_json_value(
                    "claim_versions",
                    &claim_version_id,
                    "metadata",
                    row.get(23)?,
                )?,
                source_exported_at: row.get(24)?,
                transformed_at: row.get(25)?,
            },
        ));
    }

    if !terms.is_empty() {
        results.sort_by(|a, b| {
            b.0.cmp(&a.0)
                .then_with(|| b.1.preferred_open.cmp(&a.1.preferred_open))
                .then_with(|| b.1.recorded_at.cmp(&a.1.recorded_at))
        });
    }

    Ok(results
        .into_iter()
        .map(|(_, item)| item)
        .take(query.limit)
        .collect())
}

/// Query imported relation versions with full scope and temporal filters.
pub(crate) fn query_relation_versions(
    conn: &rusqlite::Connection,
    query: &ProjectionQuery,
) -> Result<Vec<ProjectionRelationVersion>, MemoryError> {
    if query.limit == 0 {
        return Ok(Vec::new());
    }

    let scan_limit = projection_scan_limit(query);
    let terms = query_terms(query.text_query.as_deref());
    let mut stmt = conn.prepare(
        "SELECT rv.relation_version_id, rv.subject_entity_id, rv.predicate, rv.object_anchor,
                rv.scope_namespace, rv.scope_domain, rv.scope_workspace_id, rv.scope_repo_id,
                rv.claim_id, rv.source_episode_id, rv.valid_from, rv.valid_to,
                rv.recorded_at, rv.preferred_open, rv.supersedes_relation_version_id,
                rv.contradiction_status, rv.source_confidence, rv.projection_family,
                rv.source_envelope_id, rv.source_authority, rv.trace_id,
                rv.freshness, rv.metadata,
                pil.source_exported_at, pil.transformed_at
         FROM relation_versions rv
         LEFT JOIN projection_import_log pil
           ON pil.source_envelope_id = rv.source_envelope_id
          AND pil.imported_at = rv.recorded_at
         WHERE rv.scope_namespace = ?1
           AND ((?2 IS NULL AND rv.scope_domain IS NULL) OR rv.scope_domain = ?2)
           AND ((?3 IS NULL AND rv.scope_workspace_id IS NULL) OR rv.scope_workspace_id = ?3)
           AND ((?4 IS NULL AND rv.scope_repo_id IS NULL) OR rv.scope_repo_id = ?4)
           AND (?5 IS NULL OR rv.subject_entity_id = ?5)
           AND (?6 IS NULL OR rv.recorded_at <= ?6)
           AND (?7 IS NULL OR ((rv.valid_from IS NULL OR rv.valid_from <= ?7)
                           AND (rv.valid_to IS NULL OR rv.valid_to > ?7)))
         ORDER BY rv.preferred_open DESC, rv.recorded_at DESC
         LIMIT ?8",
    )?;
    let mut rows = stmt.query(rusqlite::params![
        query.scope.namespace.as_str(),
        query.scope.domain.as_deref(),
        query.scope.workspace_id.as_deref(),
        query.scope.repo_id.as_deref(),
        query.subject_entity_id.as_ref().map(|id| id.as_str()),
        query.recorded_at_or_before.as_deref(),
        query.valid_at.as_deref(),
        scan_limit as i64,
    ])?;

    let mut results = Vec::new();
    while let Some(row) = rows.next()? {
        let relation_version_id = row.get::<_, String>(0)?;
        let subject_entity_id = row.get::<_, String>(1)?;
        let object_anchor_raw = row.get::<_, String>(3)?;
        let claim_id = row.get::<_, Option<String>>(8)?;
        let source_episode_id = row.get::<_, Option<String>>(9)?;
        let searchable = format!(
            "{} {} {} {} {}",
            row.get::<_, String>(2)?,
            subject_entity_id,
            object_anchor_raw,
            optional_search_token(claim_id.as_deref()),
            optional_search_token(source_episode_id.as_deref())
        );
        let score = text_score(&terms, &searchable);
        if !terms.is_empty() && score == 0 {
            continue;
        }

        results.push((
            score,
            ProjectionRelationVersion {
                relation_version_id: RelationVersionId::new(relation_version_id.clone()),
                subject_entity_id: EntityId::new(subject_entity_id),
                predicate: row.get(2)?,
                object_anchor: decode_json_value(
                    "relation_versions",
                    &relation_version_id,
                    "object_anchor",
                    &object_anchor_raw,
                )?,
                scope_key: decode_scope_key(row.get(4)?, row.get(5)?, row.get(6)?, row.get(7)?),
                claim_id: claim_id.map(ClaimId::new),
                source_episode_id: source_episode_id.map(EpisodeId::new),
                valid_from: row.get(10)?,
                valid_to: row.get(11)?,
                recorded_at: row.get(12)?,
                preferred_open: row.get::<_, i32>(13)? != 0,
                supersedes_relation_version_id: row
                    .get::<_, Option<String>>(14)?
                    .map(RelationVersionId::new),
                contradiction_status: row.get(15)?,
                source_confidence: row.get(16)?,
                projection_family: row.get(17)?,
                source_envelope_id: EnvelopeId::new(row.get::<_, String>(18)?),
                source_authority: row.get(19)?,
                trace_id: row.get(20)?,
                freshness: row.get(21)?,
                metadata: decode_optional_json_value(
                    "relation_versions",
                    &relation_version_id,
                    "metadata",
                    row.get(22)?,
                )?,
                source_exported_at: row.get(23)?,
                transformed_at: row.get(24)?,
            },
        ));
    }

    if !terms.is_empty() {
        results.sort_by(|a, b| {
            b.0.cmp(&a.0)
                .then_with(|| b.1.preferred_open.cmp(&a.1.preferred_open))
                .then_with(|| b.1.recorded_at.cmp(&a.1.recorded_at))
        });
    }

    Ok(results
        .into_iter()
        .map(|(_, item)| item)
        .take(query.limit)
        .collect())
}

/// Query imported episode rows with full scope and recorded-time filters.
pub(crate) fn query_episode_rows(
    conn: &rusqlite::Connection,
    query: &ProjectionQuery,
) -> Result<Vec<ProjectionEpisode>, MemoryError> {
    if query.limit == 0 {
        return Ok(Vec::new());
    }

    let scan_limit = projection_scan_limit(query);
    let terms = query_terms(query.text_query.as_deref());
    let mut stmt = conn.prepare(
        "SELECT el.episode_id, el.document_id, el.cause_ids, el.effect_type, el.outcome,
                el.confidence, el.experiment_id, el.source_envelope_id, el.source_authority,
                el.trace_id, el.recorded_at, el.metadata,
                pil.scope_namespace, pil.scope_domain, pil.scope_workspace_id, pil.scope_repo_id,
                pil.source_exported_at, pil.transformed_at
         FROM episode_links el
         JOIN projection_import_log pil
           ON pil.source_envelope_id = el.source_envelope_id
          AND pil.imported_at = el.recorded_at
         WHERE pil.scope_namespace = ?1
           AND ((?2 IS NULL AND pil.scope_domain IS NULL) OR pil.scope_domain = ?2)
           AND ((?3 IS NULL AND pil.scope_workspace_id IS NULL) OR pil.scope_workspace_id = ?3)
           AND ((?4 IS NULL AND pil.scope_repo_id IS NULL) OR pil.scope_repo_id = ?4)
           AND (?5 IS NULL OR el.recorded_at <= ?5)
         ORDER BY el.recorded_at DESC
         LIMIT ?6",
    )?;
    let mut rows = stmt.query(rusqlite::params![
        query.scope.namespace.as_str(),
        query.scope.domain.as_deref(),
        query.scope.workspace_id.as_deref(),
        query.scope.repo_id.as_deref(),
        query.recorded_at_or_before.as_deref(),
        scan_limit as i64,
    ])?;

    let mut results = Vec::new();
    while let Some(row) = rows.next()? {
        let episode_id = row.get::<_, String>(0)?;
        let cause_ids_raw = row.get::<_, String>(2)?;
        let searchable = format!(
            "{} {} {} {}",
            row.get::<_, String>(1)?,
            row.get::<_, String>(3)?,
            row.get::<_, String>(4)?,
            cause_ids_raw
        );
        let score = text_score(&terms, &searchable);
        if !terms.is_empty() && score == 0 {
            continue;
        }

        results.push((
            score,
            ProjectionEpisode {
                episode_id: EpisodeId::new(episode_id.clone()),
                document_id: row.get(1)?,
                cause_ids: decode_string_vec(
                    "episode_links",
                    &episode_id,
                    "cause_ids",
                    &cause_ids_raw,
                )?,
                effect_type: row.get(3)?,
                outcome: row.get(4)?,
                confidence: row.get(5)?,
                experiment_id: row.get(6)?,
                scope_key: decode_scope_key(row.get(12)?, row.get(13)?, row.get(14)?, row.get(15)?),
                source_envelope_id: EnvelopeId::new(row.get::<_, String>(7)?),
                source_authority: row.get(8)?,
                trace_id: row.get(9)?,
                recorded_at: row.get(10)?,
                metadata: decode_optional_json_value(
                    "episode_links",
                    &episode_id,
                    "metadata",
                    row.get(11)?,
                )?,
                source_exported_at: row.get(16)?,
                transformed_at: row.get(17)?,
            },
        ));
    }

    if !terms.is_empty() {
        results.sort_by(|a, b| {
            b.0.cmp(&a.0)
                .then_with(|| b.1.recorded_at.cmp(&a.1.recorded_at))
        });
    }

    Ok(results
        .into_iter()
        .map(|(_, item)| item)
        .take(query.limit)
        .collect())
}

/// Query imported alias rows with full scope and recorded-time filters.
pub(crate) fn query_entity_aliases(
    conn: &rusqlite::Connection,
    query: &ProjectionQuery,
) -> Result<Vec<ProjectionEntityAlias>, MemoryError> {
    if query.limit == 0 {
        return Ok(Vec::new());
    }

    let scan_limit = projection_scan_limit(query);
    let terms = query_terms(query.text_query.as_deref());
    let mut stmt = conn.prepare(
        "SELECT ea.canonical_entity_id, ea.alias_text, ea.alias_source, ea.match_evidence,
                ea.confidence, ea.merge_decision, ea.scope_namespace, ea.scope_domain,
                ea.scope_workspace_id, ea.scope_repo_id, ea.review_state,
                ea.is_human_confirmed, ea.is_human_confirmed_final,
                ea.superseded_by_entity_id, ea.split_from_entity_id,
                ea.source_envelope_id, ea.recorded_at,
                pil.source_exported_at, pil.transformed_at
         FROM entity_aliases ea
         LEFT JOIN projection_import_log pil
           ON pil.source_envelope_id = ea.source_envelope_id
          AND pil.imported_at = ea.recorded_at
         WHERE ea.scope_namespace = ?1
           AND ((?2 IS NULL AND ea.scope_domain IS NULL) OR ea.scope_domain = ?2)
           AND ((?3 IS NULL AND ea.scope_workspace_id IS NULL) OR ea.scope_workspace_id = ?3)
           AND ((?4 IS NULL AND ea.scope_repo_id IS NULL) OR ea.scope_repo_id = ?4)
           AND (?5 IS NULL OR ea.canonical_entity_id = ?5)
           AND (?6 IS NULL OR ea.recorded_at <= ?6)
         ORDER BY ea.is_human_confirmed_final DESC, ea.recorded_at DESC
         LIMIT ?7",
    )?;
    let mut rows = stmt.query(rusqlite::params![
        query.scope.namespace.as_str(),
        query.scope.domain.as_deref(),
        query.scope.workspace_id.as_deref(),
        query.scope.repo_id.as_deref(),
        query.canonical_entity_id.as_ref().map(|id| id.as_str()),
        query.recorded_at_or_before.as_deref(),
        scan_limit as i64,
    ])?;

    let mut results = Vec::new();
    while let Some(row) = rows.next()? {
        let canonical_entity_id = row.get::<_, String>(0)?;
        let searchable = format!(
            "{} {} {}",
            row.get::<_, String>(1)?,
            canonical_entity_id,
            row.get::<_, String>(2)?
        );
        let score = text_score(&terms, &searchable);
        if !terms.is_empty() && score == 0 {
            continue;
        }

        results.push((
            score,
            ProjectionEntityAlias {
                canonical_entity_id: EntityId::new(canonical_entity_id.clone()),
                alias_text: row.get(1)?,
                alias_source: row.get(2)?,
                match_evidence: decode_optional_json_value(
                    "entity_aliases",
                    &canonical_entity_id,
                    "match_evidence",
                    row.get(3)?,
                )?,
                confidence: row.get(4)?,
                merge_decision: row.get(5)?,
                scope_key: decode_scope_key(row.get(6)?, row.get(7)?, row.get(8)?, row.get(9)?),
                review_state: row.get(10)?,
                is_human_confirmed: row.get::<_, i32>(11)? != 0,
                is_human_confirmed_final: row.get::<_, i32>(12)? != 0,
                superseded_by_entity_id: row.get::<_, Option<String>>(13)?.map(EntityId::new),
                split_from_entity_id: row.get::<_, Option<String>>(14)?.map(EntityId::new),
                source_envelope_id: EnvelopeId::new(row.get::<_, String>(15)?),
                recorded_at: row.get(16)?,
                source_exported_at: row.get(17)?,
                transformed_at: row.get(18)?,
            },
        ));
    }

    if !terms.is_empty() {
        results.sort_by(|a, b| {
            b.0.cmp(&a.0)
                .then_with(|| {
                    b.1.is_human_confirmed_final
                        .cmp(&a.1.is_human_confirmed_final)
                })
                .then_with(|| b.1.recorded_at.cmp(&a.1.recorded_at))
        });
    }

    Ok(results
        .into_iter()
        .map(|(_, item)| item)
        .take(query.limit)
        .collect())
}

/// Query imported evidence-reference rows with full scope and recorded-time filters.
pub(crate) fn query_evidence_refs(
    conn: &rusqlite::Connection,
    query: &ProjectionQuery,
) -> Result<Vec<ProjectionEvidenceRef>, MemoryError> {
    if query.limit == 0 {
        return Ok(Vec::new());
    }

    let scan_limit = projection_scan_limit(query);
    let terms = query_terms(query.text_query.as_deref());
    let mut stmt = conn.prepare(
        "SELECT er.claim_id, er.claim_version_id, er.fetch_handle, er.source_authority,
                er.source_envelope_id, er.recorded_at, er.metadata,
                pil.scope_namespace, pil.scope_domain, pil.scope_workspace_id, pil.scope_repo_id,
                pil.source_exported_at, pil.transformed_at
         FROM evidence_refs er
         JOIN projection_import_log pil
           ON pil.source_envelope_id = er.source_envelope_id
          AND pil.imported_at = er.recorded_at
         WHERE pil.scope_namespace = ?1
           AND ((?2 IS NULL AND pil.scope_domain IS NULL) OR pil.scope_domain = ?2)
           AND ((?3 IS NULL AND pil.scope_workspace_id IS NULL) OR pil.scope_workspace_id = ?3)
           AND ((?4 IS NULL AND pil.scope_repo_id IS NULL) OR pil.scope_repo_id = ?4)
           AND (?5 IS NULL OR er.claim_id = ?5)
           AND (?6 IS NULL OR er.claim_version_id = ?6)
           AND (?7 IS NULL OR er.recorded_at <= ?7)
         ORDER BY er.recorded_at DESC
         LIMIT ?8",
    )?;
    let mut rows = stmt.query(rusqlite::params![
        query.scope.namespace.as_str(),
        query.scope.domain.as_deref(),
        query.scope.workspace_id.as_deref(),
        query.scope.repo_id.as_deref(),
        query.claim_id.as_ref().map(|id| id.as_str()),
        query.claim_version_id.as_ref().map(|id| id.as_str()),
        query.recorded_at_or_before.as_deref(),
        scan_limit as i64,
    ])?;

    let mut results = Vec::new();
    while let Some(row) = rows.next()? {
        let claim_id = row.get::<_, String>(0)?;
        let claim_version_id = row.get::<_, Option<String>>(1)?;
        let fetch_handle = row.get::<_, String>(2)?;
        let searchable = format!(
            "{} {} {}",
            claim_id,
            optional_search_token(claim_version_id.as_deref()),
            fetch_handle
        );
        let score = text_score(&terms, &searchable);
        if !terms.is_empty() && score == 0 {
            continue;
        }

        results.push((
            score,
            ProjectionEvidenceRef {
                claim_id: ClaimId::new(claim_id),
                claim_version_id: claim_version_id.map(ClaimVersionId::new),
                fetch_handle,
                source_authority: row.get(3)?,
                source_envelope_id: EnvelopeId::new(row.get::<_, String>(4)?),
                scope_key: decode_scope_key(row.get(7)?, row.get(8)?, row.get(9)?, row.get(10)?),
                recorded_at: row.get(5)?,
                metadata: decode_optional_json_value(
                    "evidence_refs",
                    &row.get::<_, String>(4)?,
                    "metadata",
                    row.get(6)?,
                )?,
                source_exported_at: row.get(11)?,
                transformed_at: row.get(12)?,
            },
        ));
    }

    if !terms.is_empty() {
        results.sort_by(|a, b| {
            b.0.cmp(&a.0)
                .then_with(|| b.1.recorded_at.cmp(&a.1.recorded_at))
        });
    }

    Ok(results
        .into_iter()
        .map(|(_, item)| item)
        .take(query.limit)
        .collect())
}

/// Query projection import log entries.
pub(crate) fn query_projection_import_log(
    conn: &rusqlite::Connection,
    scope_namespace: Option<&str>,
    limit: usize,
) -> Result<Vec<ProjectionImportLogRow>, MemoryError> {
    let sql = if let Some(ns) = scope_namespace {
        let mut stmt = conn.prepare(
            "SELECT batch_id, source_envelope_id, schema_version, export_schema_version,
                    content_digest, source_authority, scope_namespace, scope_domain,
                    scope_workspace_id, scope_repo_id, trace_id, record_count,
                    claim_count, relation_count, episode_count, alias_count, evidence_count,
                    status, source_exported_at, transformed_at, imported_at,
                    source_run_id, comparability_snapshot_version, direct_write, failure_reason,
                    evidence_bundle_id, evidence_bundle_json, episode_bundle_id, episode_bundle_json,
                    execution_context_json, kernel_payload_json
             FROM projection_import_log
             WHERE scope_namespace = ?1
             ORDER BY imported_at DESC LIMIT ?2",
        )?;
        let rows = stmt
            .query_map(rusqlite::params![ns, limit as i64], map_import_log_row)?
            .collect::<Result<Vec<_>, _>>()?;
        return Ok(rows);
    } else {
        "SELECT batch_id, source_envelope_id, schema_version, export_schema_version,
                content_digest, source_authority, scope_namespace, scope_domain,
                scope_workspace_id, scope_repo_id, trace_id, record_count,
                claim_count, relation_count, episode_count, alias_count, evidence_count,
                status, source_exported_at, transformed_at, imported_at,
                source_run_id, comparability_snapshot_version, direct_write, failure_reason,
                evidence_bundle_id, evidence_bundle_json, episode_bundle_id, episode_bundle_json,
                execution_context_json, kernel_payload_json
         FROM projection_import_log
         ORDER BY imported_at DESC LIMIT ?1"
    };
    let mut stmt = conn.prepare(sql)?;
    let rows = stmt
        .query_map(rusqlite::params![limit as i64], map_import_log_row)?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(rows)
}

pub(crate) fn latest_rebuildable_kernel_projection_import(
    conn: &rusqlite::Connection,
    scope: &ScopeKey,
) -> Result<Option<ProjectionImportLogRow>, MemoryError> {
    let mut stmt = conn.prepare(
        "SELECT batch_id, source_envelope_id, schema_version, export_schema_version,
                content_digest, source_authority, scope_namespace, scope_domain,
                scope_workspace_id, scope_repo_id, trace_id, record_count,
                claim_count, relation_count, episode_count, alias_count, evidence_count,
                status, source_exported_at, transformed_at, imported_at,
                source_run_id, comparability_snapshot_version, direct_write, failure_reason,
                evidence_bundle_id, evidence_bundle_json, episode_bundle_id, episode_bundle_json,
                execution_context_json, kernel_payload_json
         FROM projection_import_log
         WHERE scope_namespace = ?1
           AND ((?2 IS NULL AND scope_domain IS NULL) OR scope_domain = ?2)
           AND ((?3 IS NULL AND scope_workspace_id IS NULL) OR scope_workspace_id = ?3)
           AND ((?4 IS NULL AND scope_repo_id IS NULL) OR scope_repo_id = ?4)
           AND status = 'complete'
           AND kernel_payload_json IS NOT NULL
         ORDER BY imported_at DESC
         LIMIT 1",
    )?;
    let mut rows = stmt.query(rusqlite::params![
        scope.namespace.as_str(),
        scope.domain.as_deref(),
        scope.workspace_id.as_deref(),
        scope.repo_id.as_deref(),
    ])?;
    match rows.next()? {
        Some(row) => Ok(Some(map_import_log_row(row)?)),
        None => Ok(None),
    }
}

pub(crate) fn query_projection_import_failures(
    conn: &rusqlite::Connection,
    scope_namespace: Option<&str>,
    limit: usize,
) -> Result<Vec<ProjectionImportFailureRow>, MemoryError> {
    let sql = if let Some(ns) = scope_namespace {
        let mut stmt = conn.prepare(
            "SELECT failure_id, source_envelope_id, schema_version, export_schema_version,
                    content_digest, source_authority, scope_namespace, scope_domain,
                    scope_workspace_id, scope_repo_id, trace_id, record_count,
                    error_kind, error_message, source_exported_at, transformed_at, failed_at,
                    source_run_id, comparability_snapshot_version, direct_write,
                    evidence_bundle_id, evidence_bundle_json, episode_bundle_id, episode_bundle_json,
                    execution_context_json, kernel_payload_json
             FROM projection_import_failures
             WHERE scope_namespace = ?1
             ORDER BY failed_at DESC LIMIT ?2",
        )?;
        let rows = stmt
            .query_map(rusqlite::params![ns, limit as i64], map_import_failure_row)?
            .collect::<Result<Vec<_>, _>>()?;
        return Ok(rows);
    } else {
        "SELECT failure_id, source_envelope_id, schema_version, export_schema_version,
                content_digest, source_authority, scope_namespace, scope_domain,
                scope_workspace_id, scope_repo_id, trace_id, record_count,
                error_kind, error_message, source_exported_at, transformed_at, failed_at,
                source_run_id, comparability_snapshot_version, direct_write,
                evidence_bundle_id, evidence_bundle_json, episode_bundle_id, episode_bundle_json,
                execution_context_json, kernel_payload_json
         FROM projection_import_failures
         ORDER BY failed_at DESC LIMIT ?1"
    };
    let mut stmt = conn.prepare(sql)?;
    let rows = stmt
        .query_map(rusqlite::params![limit as i64], map_import_failure_row)?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(rows)
}

fn map_import_log_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<ProjectionImportLogRow> {
    Ok(ProjectionImportLogRow {
        batch_id: row.get(0)?,
        source_envelope_id: row.get(1)?,
        schema_version: row.get(2)?,
        export_schema_version: row.get(3)?,
        content_digest: row.get(4)?,
        source_authority: row.get(5)?,
        scope_namespace: row.get(6)?,
        scope_domain: row.get(7)?,
        scope_workspace_id: row.get(8)?,
        scope_repo_id: row.get(9)?,
        trace_id: row.get(10)?,
        record_count: row.get::<_, i64>(11)? as usize,
        claim_count: row.get::<_, i64>(12)? as usize,
        relation_count: row.get::<_, i64>(13)? as usize,
        episode_count: row.get::<_, i64>(14)? as usize,
        alias_count: row.get::<_, i64>(15)? as usize,
        evidence_count: row.get::<_, i64>(16)? as usize,
        status: row.get(17)?,
        source_exported_at: row.get(18)?,
        transformed_at: row.get(19)?,
        imported_at: row.get(20)?,
        source_run_id: row.get(21)?,
        comparability_snapshot_version: row.get(22)?,
        direct_write: row.get::<_, i64>(23)? != 0,
        failure_reason: row.get(24)?,
        evidence_bundle_id: row.get(25)?,
        evidence_bundle_json: row.get(26)?,
        episode_bundle_id: row.get(27)?,
        episode_bundle_json: row.get(28)?,
        execution_context_json: row.get(29)?,
        kernel_payload_json: row.get(30)?,
    })
}

fn map_import_failure_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<ProjectionImportFailureRow> {
    Ok(ProjectionImportFailureRow {
        failure_id: row.get(0)?,
        source_envelope_id: row.get(1)?,
        schema_version: row.get(2)?,
        export_schema_version: row.get(3)?,
        content_digest: row.get(4)?,
        source_authority: row.get(5)?,
        scope_namespace: row.get(6)?,
        scope_domain: row.get(7)?,
        scope_workspace_id: row.get(8)?,
        scope_repo_id: row.get(9)?,
        trace_id: row.get(10)?,
        record_count: row.get::<_, i64>(11)? as usize,
        error_kind: row.get(12)?,
        error_message: row.get(13)?,
        source_exported_at: row.get(14)?,
        transformed_at: row.get(15)?,
        failed_at: row.get(16)?,
        source_run_id: row.get(17)?,
        comparability_snapshot_version: row.get(18)?,
        direct_write: row.get::<_, i64>(19)? != 0,
        evidence_bundle_id: row.get(20)?,
        evidence_bundle_json: row.get(21)?,
        episode_bundle_id: row.get(22)?,
        episode_bundle_json: row.get(23)?,
        execution_context_json: row.get(24)?,
        kernel_payload_json: row.get(25)?,
    })
}

/// Flat row struct for inserting claim versions.
pub(crate) struct ClaimVersionRow {
    pub claim_version_id: String,
    pub claim_id: String,
    pub claim_state: String,
    pub projection_family: String,
    pub subject_entity_id: String,
    pub predicate: String,
    pub object_anchor: String,
    pub scope_namespace: String,
    pub scope_domain: Option<String>,
    pub scope_workspace_id: Option<String>,
    pub scope_repo_id: Option<String>,
    pub valid_from: Option<String>,
    pub valid_to: Option<String>,
    pub recorded_at: String,
    pub preferred_open: bool,
    pub source_envelope_id: String,
    pub source_authority: String,
    pub trace_id: Option<String>,
    pub freshness: String,
    pub contradiction_status: String,
    pub supersedes_claim_version_id: Option<String>,
    pub content: String,
    pub confidence: f32,
    pub content_digest: Option<String>,
    pub metadata: Option<String>,
}

/// Flat row struct for inserting relation versions.
pub(crate) struct RelationVersionRow {
    pub relation_version_id: String,
    pub subject_entity_id: String,
    pub predicate: String,
    pub object_anchor: String,
    pub scope_namespace: String,
    pub scope_domain: Option<String>,
    pub scope_workspace_id: Option<String>,
    pub scope_repo_id: Option<String>,
    pub claim_id: Option<String>,
    pub source_episode_id: Option<String>,
    pub valid_from: Option<String>,
    pub valid_to: Option<String>,
    pub recorded_at: String,
    pub preferred_open: bool,
    pub supersedes_relation_version_id: Option<String>,
    pub contradiction_status: String,
    pub source_confidence: f32,
    pub projection_family: String,
    pub source_envelope_id: String,
    pub source_authority: String,
    pub trace_id: Option<String>,
    pub freshness: String,
    pub metadata: Option<String>,
}

/// Flat row struct for inserting entity aliases.
pub(crate) struct EntityAliasRow {
    pub canonical_entity_id: String,
    pub alias_text: String,
    pub alias_source: String,
    pub match_evidence: Option<String>,
    pub confidence: f32,
    pub merge_decision: String,
    pub scope_namespace: String,
    pub scope_domain: Option<String>,
    pub scope_workspace_id: Option<String>,
    pub scope_repo_id: Option<String>,
    pub review_state: String,
    pub is_human_confirmed: bool,
    pub is_human_confirmed_final: bool,
    pub superseded_by_entity_id: Option<String>,
    pub split_from_entity_id: Option<String>,
    pub source_envelope_id: String,
    pub recorded_at: String,
}

/// Flat row struct for inserting evidence refs.
pub(crate) struct EvidenceRefRow {
    pub claim_id: String,
    pub claim_version_id: Option<String>,
    pub fetch_handle: String,
    pub source_authority: String,
    pub source_envelope_id: String,
    pub recorded_at: String,
    pub metadata: Option<String>,
}

/// Flat row struct for episode links.
pub(crate) struct EpisodeLinkRow {
    pub episode_id: String,
    pub document_id: String,
    pub cause_ids: String,
    pub effect_type: String,
    pub outcome: String,
    pub confidence: f32,
    pub experiment_id: Option<String>,
    pub source_envelope_id: String,
    pub source_authority: String,
    pub trace_id: Option<String>,
    pub recorded_at: String,
    pub metadata: Option<String>,
}

/// Flat row struct for projection import log.
#[derive(Clone)]
pub(crate) struct ProjectionImportLogRow {
    pub batch_id: String,
    pub source_envelope_id: String,
    pub schema_version: String,
    pub export_schema_version: Option<String>,
    pub content_digest: String,
    pub source_authority: String,
    pub scope_namespace: String,
    pub scope_domain: Option<String>,
    pub scope_workspace_id: Option<String>,
    pub scope_repo_id: Option<String>,
    pub trace_id: Option<String>,
    pub record_count: usize,
    pub claim_count: usize,
    pub relation_count: usize,
    pub episode_count: usize,
    pub alias_count: usize,
    pub evidence_count: usize,
    pub status: String,
    pub source_exported_at: Option<String>,
    pub transformed_at: Option<String>,
    pub imported_at: String,
    pub source_run_id: Option<String>,
    pub comparability_snapshot_version: Option<String>,
    pub direct_write: bool,
    pub failure_reason: Option<String>,
    pub evidence_bundle_id: Option<String>,
    pub evidence_bundle_json: Option<String>,
    pub episode_bundle_id: Option<String>,
    pub episode_bundle_json: Option<String>,
    pub execution_context_json: Option<String>,
    pub kernel_payload_json: Option<String>,
}

/// Durable failed projection import receipt.
pub(crate) struct ProjectionImportFailureRow {
    pub failure_id: String,
    pub source_envelope_id: String,
    pub schema_version: String,
    pub export_schema_version: Option<String>,
    pub content_digest: String,
    pub source_authority: String,
    pub scope_namespace: String,
    pub scope_domain: Option<String>,
    pub scope_workspace_id: Option<String>,
    pub scope_repo_id: Option<String>,
    pub trace_id: Option<String>,
    pub record_count: usize,
    pub error_kind: String,
    pub error_message: String,
    pub source_exported_at: Option<String>,
    pub transformed_at: Option<String>,
    pub failed_at: String,
    pub source_run_id: Option<String>,
    pub comparability_snapshot_version: Option<String>,
    pub direct_write: bool,
    pub evidence_bundle_id: Option<String>,
    pub evidence_bundle_json: Option<String>,
    pub episode_bundle_id: Option<String>,
    pub episode_bundle_json: Option<String>,
    pub execution_context_json: Option<String>,
    pub kernel_payload_json: Option<String>,
}