1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
use rusqlite::params;
use std::collections::HashMap;
use crate::error::Result;
use crate::types::Stats;
use super::{now, YantrikDB};
/// Ceiling on the bounded global ingest queue (`oplog` rows with `applied=0`).
///
/// Single source of truth as of v0.10 Item 4a.6a. This was previously declared
/// three times as a function-local `const` (`log_op_pending`,
/// `log_op_pending_for_reembed_queue`, and now the shared admission check) — a
/// value that gates every foreground write should not be able to drift between
/// its copies.
pub(crate) const MAX_PENDING_OPS: i64 = 10_000;
/// Ops drained per pass by [`YantrikDB::drain_materializer_backlog`].
///
/// The conn lock is released between passes, so this is the granularity at
/// which a foreground drain yields to concurrent writers and to the
/// background materializer pool. Matches the pool's own `DRAIN_BATCH_SIZE`
/// order of magnitude; larger would hold the lock longer for no gain.
const THINK_DRAIN_BATCH: usize = 64;
/// Hard ceiling on ops a single `think()` will drain.
///
/// Deliberately below [`MAX_PENDING_OPS`] (10_000): a foreground call must
/// stay bounded even when the queue is at its admission ceiling. See
/// [`YantrikDB::drain_materializer_backlog`] for what is given up at the
/// boundary and why a bounded drain beats an unbounded one.
const THINK_DRAIN_BUDGET: usize = 4096;
/// `extractor_version` written by the bound extractor (occurrence-local
/// binding with evidence spans; `graph::extract_relations_bound`).
pub(crate) const BOUND_EXTRACTOR_VERSION: &str = "2.0";
/// Refusal rows kept per memory.
const REFUSAL_LEDGER_CAP_PER_MEMORY: usize = 8;
/// The claims table stores spans as `INTEGER`; a span past `i32::MAX`
/// (never on a memory this engine accepts) is stored as none.
fn span_columns(span: Option<(usize, usize)>) -> (Option<i32>, Option<i32>) {
match span {
Some((s, e)) => (i32::try_from(s).ok(), i32::try_from(e).ok()),
None => (None, None),
}
}
impl YantrikDB {
/// Get engine statistics. Optionally filter memory counts by namespace.
pub fn stats(&self, namespace: Option<&str>) -> Result<Stats> {
let conn = self.conn.lock();
let ns_filter = namespace
.map(|ns| format!(" AND namespace = '{}'", ns.replace('\'', "''")))
.unwrap_or_default();
let active = conn.query_row(
&format!(
"SELECT COUNT(*) FROM memories WHERE consolidation_status = 'active'{}",
ns_filter
),
[],
|row| row.get(0),
)?;
let consolidated = conn.query_row(
&format!(
"SELECT COUNT(*) FROM memories WHERE consolidation_status = 'consolidated'{}",
ns_filter
),
[],
|row| row.get(0),
)?;
let tombstoned = conn.query_row(
&format!(
"SELECT COUNT(*) FROM memories WHERE consolidation_status = 'tombstoned'{}",
ns_filter
),
[],
|row| row.get(0),
)?;
let archived = conn.query_row(
&format!(
"SELECT COUNT(*) FROM memories WHERE storage_tier = 'cold'{}",
ns_filter
),
[],
|row| row.get(0),
)?;
let edges = conn.query_row(
"SELECT COUNT(*) FROM edges WHERE tombstoned = 0",
[],
|row| row.get(0),
)?;
let entities = conn.query_row("SELECT COUNT(*) FROM entities", [], |row| row.get(0))?;
let chunk_vectors: u64 = conn
.query_row("SELECT COUNT(*) FROM memory_chunks", [], |row| {
row.get::<_, i64>(0)
})
.map(|n| n.max(0) as u64)
.unwrap_or(0);
let operations = conn.query_row("SELECT COUNT(*) FROM oplog", [], |row| row.get(0))?;
let open_conflicts = conn.query_row(
"SELECT COUNT(*) FROM conflicts WHERE status = 'open'",
[],
|row| row.get(0),
)?;
let resolved_conflicts = conn.query_row(
"SELECT COUNT(*) FROM conflicts WHERE status IN ('resolved', 'dismissed')",
[],
|row| row.get(0),
)?;
let pending_triggers = conn.query_row(
"SELECT COUNT(*) FROM trigger_log WHERE status = 'pending'",
[],
|row| row.get(0),
)?;
let active_patterns = conn.query_row(
"SELECT COUNT(*) FROM patterns WHERE status = 'active'",
[],
|row| row.get(0),
)?;
// v0.10 Item 1: how many records are currently superseded
// (selected active inbound supersedes edge).
let superseded_records = conn.query_row(
"SELECT COUNT(DISTINCT target_rid) FROM record_links \
WHERE link_type = 'supersedes' \
AND status = 'active' AND selection_state = 'selected'",
[],
|row| row.get(0),
)?;
// C5b pollution census: apostrophe-bearing entities (phantom
// possessives + contraction entities minted by the pre-C5a
// tokenizer) and how many the migration has aliased to their
// canonicals. before/after on these two IS the migration's
// success metric. Best-effort on old schemas.
let apostrophe_entities: u64 = conn
.query_row(
"SELECT COUNT(*) FROM entities WHERE name LIKE '%''%'",
[],
|row| row.get::<_, i64>(0),
)
.map(|n| n.max(0) as u64)
.unwrap_or(0);
let possessive_aliases: u64 = conn
.query_row(
"SELECT COUNT(*) FROM entity_aliases \
WHERE source = 'possessive_migration_v1'",
[],
|row| row.get::<_, i64>(0),
)
.map(|n| n.max(0) as u64)
.unwrap_or(0);
let provenance_verified_records = conn.query_row(
&format!(
"SELECT COUNT(*) FROM memories \
WHERE consolidation_status != 'tombstoned' \
AND json_valid(metadata) \
AND json_extract(metadata, '$.provenance_verified') = 1{}",
ns_filter
),
[],
|row| row.get(0),
)?;
let unverified_user_source_records = conn.query_row(
&format!(
"SELECT COUNT(*) FROM memories \
WHERE consolidation_status != 'tombstoned' \
AND source = 'user' \
AND COALESCE(\
CASE WHEN json_valid(metadata) \
THEN json_extract(metadata, '$.provenance_verified') END,\
0\
) != 1{}",
ns_filter
),
[],
|row| row.get(0),
)?;
let grouped_counts =
|value_sql: &str, extra_filter: &str| -> Result<HashMap<String, i64>> {
let sql = format!(
"SELECT {value_sql}, COUNT(*) FROM memories \
WHERE consolidation_status != 'tombstoned' \
{extra_filter}{ns_filter} GROUP BY 1"
);
let mut stmt = conn.prepare(&sql)?;
let rows = stmt.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
})?;
let mut counts = HashMap::new();
for row in rows {
let (value, count) = row?;
counts.insert(value, count);
}
Ok(counts)
};
let provenance_source_counts =
grouped_counts("COALESCE(NULLIF(source, ''), 'unknown')", "")?;
let provenance_method_counts = grouped_counts(
"COALESCE(NULLIF(CASE WHEN json_valid(metadata) \
THEN CAST(json_extract(metadata, '$.provenance_method') AS TEXT) END, ''), \
'unmarked')",
"",
)?;
let unverified_source_counts = grouped_counts(
"COALESCE(NULLIF(source, ''), 'unknown')",
"AND COALESCE(CASE WHEN json_valid(metadata) \
THEN json_extract(metadata, '$.provenance_verified') END, 0) != 1 ",
)?;
let recall_candidate_cap_bound_by_namespace_since_boot: HashMap<String, u64> = self
.recall_candidate_cap_bound_since_boot
.lock()
.iter()
.filter(|(key, _)| namespace.map_or(true, |ns| key.as_str() == ns))
.map(|(key, count)| (key.clone(), *count))
.collect();
let recall_candidate_cap_bound_since_boot =
recall_candidate_cap_bound_by_namespace_since_boot
.values()
.copied()
.sum();
let synthesis_fanout_cap = Self::synthesis_fanout_cap_from_conn(&conn)?;
let synthesis_namespace_filter = namespace
.map(|ns| format!(" AND d.namespace = '{}'", ns.replace('\'', "''")))
.unwrap_or_default();
let (
synthesis_fanout_current_high_water,
synthesis_fanout_sources_at_cap,
synthesis_fanout_sources_over_cap,
): (i64, i64, i64) = conn.query_row(
&format!(
"WITH fanout AS ( \
SELECT d.source_rid, COUNT(DISTINCT d.synthesis_rid) AS n \
FROM synthesis_dependencies d \
JOIN memories s ON s.rid = d.synthesis_rid \
WHERE s.synthesis_state = 'verified' \
AND s.consolidation_status = 'active'{} \
GROUP BY d.source_rid \
) \
SELECT COALESCE(MAX(n), 0), \
COALESCE(SUM(CASE WHEN n = ?1 THEN 1 ELSE 0 END), 0), \
COALESCE(SUM(CASE WHEN n > ?1 THEN 1 ELSE 0 END), 0) \
FROM fanout",
synthesis_namespace_filter
),
rusqlite::params![synthesis_fanout_cap as i64],
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
)?;
drop(conn);
Ok(Stats {
active_memories: active,
consolidated_memories: consolidated,
tombstoned_memories: tombstoned,
archived_memories: archived,
edges,
entities,
operations,
open_conflicts,
resolved_conflicts,
pending_triggers,
active_patterns,
scoring_cache_entries: self.scoring_cache.read().len(),
vec_index_entries: self.search_state.load().vec_index.len(),
graph_index_entities: self.graph_index.read().entity_count(),
graph_index_edges: self.graph_index.read().edge_count(),
status_read_policy: if self.status_read_policy() {
"exclude_superseded".to_string()
} else {
"legacy".to_string()
},
superseded_records,
superseded_served_since_boot: self
.superseded_served_since_boot
.load(std::sync::atomic::Ordering::Relaxed),
recall_candidate_cap: super::recall::MAX_OVERSAMPLED_RECALL_CANDIDATES,
recall_candidate_cap_namespace_capacity:
super::recall::MAX_TRACKED_RECALL_LIMIT_NAMESPACES,
recall_candidate_cap_bound_since_boot,
recall_candidate_cap_bound_by_namespace_since_boot,
recall_candidate_cap_namespace_stats_truncated_since_boot: self
.recall_candidate_cap_namespace_stats_truncated_since_boot
.load(std::sync::atomic::Ordering::Relaxed),
synthesis_fanout_cap,
synthesis_fanout_refused_since_boot: self
.synthesis_fanout_refused_since_boot
.load(std::sync::atomic::Ordering::Relaxed),
synthesis_fanout_current_high_water: synthesis_fanout_current_high_water.max(0)
as usize,
synthesis_fanout_sources_at_cap,
synthesis_fanout_sources_over_cap,
provenance_gate_mode: self.provenance_gate_mode().as_str().to_string(),
provenance_flagged_since_boot: self
.provenance_flagged_since_boot
.load(std::sync::atomic::Ordering::Relaxed),
claim_chain_gate_mode: self.claim_chain_gate_mode().as_str().to_string(),
claim_chain_gate_suppressed_since_boot: self
.claim_chain_gate_suppressed_since_boot
.lock()
.iter()
.map(|(k, v)| (k.clone(), *v))
.collect(),
foreign_sqlite_mode: self.foreign_sqlite.mode().as_str().to_string(),
foreign_sqlite_supported: self.foreign_sqlite.supported(),
foreign_sqlite_active: self.foreign_sqlite.active(),
foreign_sqlite_tainted: self.foreign_sqlite.tainted(),
foreign_sqlite_detected_since_boot: self.foreign_sqlite.detected_since_boot(),
foreign_sqlite_refused_since_boot: self.foreign_sqlite.refused_since_boot(),
foreign_commits_detected_since_boot: self
.foreign_sqlite
.foreign_commits_detected_since_boot(),
integrity_check_pending: self.foreign_sqlite.integrity_check_pending(),
integrity_checks_since_boot: self.foreign_sqlite.integrity_checks_since_boot(),
last_integrity_check: self.foreign_sqlite.last_integrity().unwrap_or_default(),
provenance_verified_records,
unverified_user_source_records,
provenance_source_counts,
provenance_method_counts,
unverified_source_counts,
embedder_window_chars: self.embedder_window(),
embedder_truncated_writes: self.embedder_truncated_write_count(),
embedder_chunked_writes: self.embedder_chunked_write_count(),
chunk_vectors,
apostrophe_entities,
possessive_aliases,
})
}
/// Append an operation to the oplog with HLC and optional embedding hash.
///
/// **Issue #41 brainstorm-2 §1 / brainstorm-4 §6.** Stamps the
/// v27 `applied_generation` column with the active SearchState
/// generation. The post-swap materializer (Layer 5) uses this
/// column to discriminate ops already applied under generation G
/// (skip them — they're durably indexed) from queued-during-
/// reembed ops (`applied_generation IS NULL` — need re-encode
/// under the new embedder). Sync writers call this AFTER their
/// `vec_index.append` so the generation read here is the same
/// generation the index entry was written against (the
/// `SyncWriteGuard` held by the caller prevents reembed from
/// completing its swap while we read).
pub fn log_op(
&self,
op_type: &str,
target_rid: Option<&str>,
payload: &serde_json::Value,
emb_hash: Option<&[u8]>,
) -> Result<String> {
let op_id = crate::id::new_id();
let hlc_ts = self.tick_hlc();
let hlc_bytes = hlc_ts.to_bytes().to_vec();
// 0.13.2: sealed on encrypted databases (see encode_oplog_payload).
let payload_str = self.encode_oplog_payload(&serde_json::to_string(payload)?)?;
let applied_generation: i64 = self.search_state.load().generation as i64;
let conn = self.conn.lock();
conn.execute(
"INSERT INTO oplog (op_id, op_type, timestamp, target_rid, payload, \
actor_id, hlc, embedding_hash, origin_actor, applied, applied_generation) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 1, ?10)",
params![
op_id,
op_type,
now(),
target_rid,
payload_str,
self.actor_id,
hlc_bytes,
emb_hash,
self.actor_id,
applied_generation,
],
)?;
Ok(op_id)
}
/// In-transaction sibling of [`Self::log_op`] (v0.10 Item 4a.6a).
///
/// Writes an `applied=1` oplog row using the CALLER'S transaction instead of
/// re-locking `self.conn`. This is the primitive that lets a write path
/// commit its row and its oplog provenance ATOMICALLY. `log_op` cannot do
/// that: it re-acquires `self.conn` (see [`Self::log_op`]), and
/// `parking_lot::Mutex` is not reentrant, so calling it while holding a conn
/// guard deadlocks.
///
/// Generalized from `insert_correct_op_in_tx`, which `correct()` has used
/// since Item 3 — the shape is proven, this just parameterizes `op_type`.
///
/// `applied_generation` is passed IN rather than read from `search_state`
/// here: the caller holds a `SyncWriteGuard` pinning the generation for its
/// whole critical section, and must stamp the same generation its vector
/// entry was written against.
#[allow(clippy::too_many_arguments)]
pub(crate) fn log_op_in_tx(
&self,
// &Connection, not &Transaction: a `Transaction` derefs to it, so tx
// callers are unchanged, while SAVEPOINT-guarded callers (record_batch,
// 4a.6d-2b) have no `Transaction` to offer — same generalization
// `advance_importance_stats_in_tx` made in 4a.6b.
tx: &rusqlite::Connection,
op_type: &str,
target_rid: Option<&str>,
payload: &serde_json::Value,
emb_hash: Option<&[u8]>,
embedding: Option<&[u8]>,
applied_generation: i64,
// 4a.6c: an idempotency claim binds to the authoritative op's id as its
// recovery evidence, and the claim must be the FIRST statement of the
// transaction (the v37 partial unique index on memories would otherwise
// fire before the claim resolves a dup). So the keyed path mints the
// op_id BEFORE the tx and passes it here; None keeps minting inline.
preminted_op_id: Option<&str>,
) -> Result<String> {
let hlc_bytes = self.tick_hlc().to_bytes().to_vec();
self.log_op_at_hlc_in_tx(
tx,
op_type,
target_rid,
payload,
emb_hash,
embedding,
applied_generation,
preminted_op_id,
&hlc_bytes,
)
}
/// Variant used when a materialized row must carry the exact HLC of its
/// authoritative oplog operation in the same transaction.
#[allow(clippy::too_many_arguments)]
pub(crate) fn log_op_at_hlc_in_tx(
&self,
tx: &rusqlite::Connection,
op_type: &str,
target_rid: Option<&str>,
payload: &serde_json::Value,
emb_hash: Option<&[u8]>,
embedding: Option<&[u8]>,
applied_generation: i64,
preminted_op_id: Option<&str>,
hlc_bytes: &[u8],
) -> Result<String> {
let op_id = match preminted_op_id {
Some(id) => id.to_string(),
None => crate::id::new_id(),
};
// 0.13.2: sealed on encrypted databases (see encode_oplog_payload).
let payload_str = self.encode_oplog_payload(&serde_json::to_string(payload)?)?;
tx.execute(
"INSERT INTO oplog \
(op_id, op_type, timestamp, target_rid, payload, actor_id, hlc, \
embedding_hash, origin_actor, applied, applied_generation, embedding) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 1, ?10, ?11)",
params![
op_id,
op_type,
now(),
target_rid,
payload_str,
self.actor_id,
hlc_bytes,
emb_hash,
self.actor_id,
applied_generation,
embedding,
],
)?;
Ok(op_id)
}
/// In-transaction sibling of [`Self::log_op_pending`] (v0.10 Item 4a.6a).
///
/// Writes an `applied=0` oplog row in the caller's transaction.
///
/// **Deliberately does NOT touch `pending_op_count`.** That split is
/// load-bearing, not laziness. The counter may only be incremented AFTER the
/// enclosing transaction commits: increment it here and a later rollback
/// leaves the row gone but the counter raised, with nothing to bring it back
/// down — [`Self::mark_op_applied`] only decrements rows it actually
/// transitions, and there is no row. The drift is monotonic, and at
/// `MAX_PENDING_OPS` of net drift the admission check wedges EVERY foreground
/// write into `Backpressure` forever, with zero pending ops in SQL. That is
/// the v0.7.1 counter-leak class. The caller owns the increment, and must run
/// it only on the committed path (in `record()` the `ReservationGuard` owns
/// it, so a post-commit unwind cannot skip it).
///
/// Uses a plain `INSERT`, NOT `INSERT OR IGNORE` — and that difference is
/// deliberate (sol, 4a.6a review finding 4).
///
/// This function mints a FRESH `op_id`, so "ignored" could only ever mean an
/// id collision or some other constraint — and swallowing it would let the
/// record transaction commit and `record()` return `Ok` while the
/// post-materialization enqueue silently did not happen. That write's entity
/// materialization would then be owed to nobody, forever, which is the exact
/// failure moving the enqueue into the transaction was meant to prevent. A
/// plain INSERT turns that into the error it is, rolling the whole write back.
///
/// (I originally copied `OR IGNORE` from [`Self::log_op_pending`] without
/// checking whether its rationale applied here. It did not — and this doc
/// then spent a release asserting that `log_op_pending` needed `OR IGNORE`
/// "because it has cluster-replay callers that re-use an existing `op_id`".
/// That was false: it mints its own id and no caller can supply one, so the
/// ignore there could never fire for the stated reason and only ever hid real
/// violations. #83 made it a plain INSERT too. The rationale I copied was
/// never real anywhere — sol #83 r2 caught this paragraph still teaching it.)
///
/// Returns the op_id. Deliberately does NOT touch `pending_op_count`: the
/// counter may only move AFTER the enclosing transaction commits — increment
/// it here and a rollback leaves the row gone but the counter raised, with
/// nothing to bring it back down ([`Self::mark_op_applied`] only decrements
/// rows it actually transitions, and there is no row). The drift is
/// monotonic, and at `MAX_PENDING_OPS` of net drift the admission check wedges
/// EVERY foreground write into `Backpressure` forever with zero pending ops in
/// SQL. That is the v0.7.1 counter-leak class.
pub(crate) fn log_op_pending_in_tx(
&self,
// &Connection, not &Transaction — same generalization as
// `log_op_in_tx` (4a.6d-2b): Transaction derefs, so tx callers are
// unchanged, and SAVEPOINT-guarded callers (record_with_rid,
// 4a.6d-3) have no Transaction to offer.
tx: &rusqlite::Connection,
op_type: &str,
target_rid: Option<&str>,
payload: &serde_json::Value,
emb_hash: Option<&[u8]>,
embedding: Option<&[u8]>,
) -> Result<String> {
let op_id = crate::id::new_id();
let hlc_bytes = self.tick_hlc().to_bytes().to_vec();
// 0.13.2: sealed on encrypted databases (see encode_oplog_payload).
let payload_str = self.encode_oplog_payload(&serde_json::to_string(payload)?)?;
tx.execute(
"INSERT INTO oplog \
(op_id, op_type, timestamp, target_rid, payload, actor_id, hlc, \
embedding_hash, origin_actor, applied, embedding) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 0, ?10)",
params![
op_id,
op_type,
now(),
target_rid,
payload_str,
self.actor_id,
hlc_bytes,
emb_hash,
self.actor_id,
embedding,
],
)?;
Ok(op_id)
}
/// The `MAX_PENDING_OPS` admission check (v0.10 Item 4a.6a).
///
/// **Must be called while holding the conn lock** to be authoritative. It was
/// originally an unlocked pre-lock read, which sol's 4a.6a review finding 1
/// showed is a TOCTOU: at 9,999 pending, N writers all read "under the limit",
/// then serialize under `conn` and each commit an enqueue, pushing the queue
/// past the ceiling. Under the lock the read and the commit that acts on it
/// are serialized, so the bound actually holds.
///
/// [`Self::check_pending_backpressure_fast`] is the unlocked variant, useful
/// only as an early reject before doing expensive work.
pub(crate) fn check_pending_backpressure_locked(&self) -> Result<()> {
self.check_pending_backpressure_fast()
}
/// Unlocked, advisory admission check — an early reject only. NOT
/// authoritative: see [`Self::check_pending_backpressure_locked`].
pub(crate) fn check_pending_backpressure_fast(&self) -> Result<()> {
use std::sync::atomic::Ordering;
let pending = self.pending_op_count.load(Ordering::Relaxed);
if pending >= MAX_PENDING_OPS {
return Err(crate::error::YantrikDbError::Backpressure {
pending,
max: MAX_PENDING_OPS,
retry_after_ms: 100,
});
}
Ok(())
}
// NOTE (4a.6d-2b): `log_record_ops_batch` — the post-commit batched
// "record"-op writer introduced by #79's fix — was deleted here. Its one
// caller, `record_batch`, now commits each item's op INSIDE its savepoint
// via `log_op_in_tx` under a preminted op id (the id an idempotency claim
// binds to), which is what closed #94's Err-after-commit for that path.
/// **Decoupled write path RFC, Phase 1.**
///
/// Append a *pending* operation to the oplog (applied=0) carrying the
/// full embedding bytes. Background materializer workers will later drain
/// these and apply them to the in-memory indexes (memories table,
/// vec_index, graph_index, scoring_cache), flipping `applied` to 1.
///
/// This is the "WAL append" step from the RFC's freeway diagram. Foreground
/// `record()` does not call this yet — Phase 4 of the RFC flips that. For
/// Phase 1 (this version), the API is exposed for tests and for Phase 3
/// worker scaffolding.
///
/// **NOT idempotent on op_id, and cannot be.** This rustdoc used to claim
/// "if the same op_id is appended twice (e.g. via crash-restart replay), the
/// second INSERT is silently skipped" — describing a capability the signature
/// does not offer: there is no `op_id` parameter, so a caller CANNOT supply
/// one. Every call mints a fresh id (below), so the `OR IGNORE` that claim
/// justified could never fire for the reason given, and instead silently
/// swallowed real constraint violations — returning `Ok(op_id)` for a row
/// that was never written. This now uses a plain INSERT so a failure to
/// enqueue is an error, not a fiction. (The genuine replay case is
/// replication apply, which really does receive a remote op_id and inserts it
/// itself.)
pub fn log_op_pending(
&self,
op_type: &str,
target_rid: Option<&str>,
payload: &serde_json::Value,
emb_hash: Option<&[u8]>,
embedding: Option<&[u8]>,
) -> Result<String> {
use std::sync::atomic::Ordering;
let op_id = crate::id::new_id();
let hlc_ts = self.tick_hlc();
let hlc_bytes = hlc_ts.to_bytes().to_vec();
// 0.13.2: sealed on encrypted databases (see encode_oplog_payload).
let payload_str = self.encode_oplog_payload(&serde_json::to_string(payload)?)?;
// **v0.7.1 perf hotfix.** Backpressure check is now an atomic
// load against `pending_op_count` instead of `SELECT COUNT(*) FROM
// oplog WHERE applied = 0`. The previous SQL pattern dominated
// foreground latency when v0.7.0 wired log_op_pending into the
// record() hot path — every write paid a Mutex<Connection> acquire
// + index scan + drop just to check the bound. Cached counter
// is maintained by `log_op_pending` (fetch_add on insert) and
// `mark_op_applied` (fetch_sub on apply-win).
// Advisory fast reject (unlocked). NOT authoritative on its own.
self.check_pending_backpressure_fast()?;
let conn = self.conn.lock();
// THE authoritative check (sol 4a.6a r2 finding 1). The unlocked load
// above is a TOCTOU: at MAX_PENDING_OPS-1, N producers all read "under the
// limit", then serialize here and each insert, overshooting the ceiling.
// Re-reading under the SAME lock that guards the INSERT serializes the
// read with the write it authorizes, so the bound actually holds. 4a.6a
// first fixed only record()'s copy of this race and left this one — the
// instance, not the class.
self.check_pending_backpressure_locked()?;
conn.execute(
"INSERT INTO oplog \
(op_id, op_type, timestamp, target_rid, payload, \
actor_id, hlc, embedding_hash, origin_actor, applied, embedding) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, 0, ?10)",
params![
op_id,
op_type,
now(),
target_rid,
payload_str,
self.actor_id,
hlc_bytes,
emb_hash,
self.actor_id,
embedding,
],
)?;
// **v0.7.1**: maintain the cached counter.
// Unconditional: a plain INSERT that returned Ok inserted exactly one
// row — anything else is an Err above. The old `changes() > 0` guard
// existed to cope with `OR IGNORE` no-ops that could not happen for the
// documented reason (no caller can supply an op_id), and it turned a
// swallowed constraint violation into a silently-correct-looking count.
//
// Scope of that claim (sol #83 finding 3): it is about THIS statement,
// not about durability. `conn()` is public, so a caller that opens its
// own SAVEPOINT around this and rolls back leaves the row gone and this
// counter high — pre-existing, and not something `changes()` ever
// caught either. The counter is a cache over `applied = 0` and re-seeds
// on restart; the in-transaction sibling (`log_op_pending_in_tx`)
// deliberately leaves it alone precisely because only its committer
// knows the outcome.
self.pending_op_count.fetch_add(1, Ordering::Relaxed);
Ok(op_id)
}
/// **Decoupled write path RFC, Phase 1.**
///
/// Count of pending oplog entries (applied=0). Used by tests and by the
/// background materializer to decide whether to wake up.
///
/// **v0.7.1 hotfix:** returns the cached `pending_op_count` atomic
/// instead of running `SELECT COUNT(*)`. The atomic is maintained by
/// `log_op_pending` (`fetch_add` on insert) and `mark_op_applied`
/// (`fetch_sub` on apply-win) so it's always coherent with the SQL
/// state. Tests that mutate oplog by hand (rare, only in test
/// helpers) can still use the SQL form via `count_pending_ops_sql`.
pub fn count_pending_ops(&self) -> Result<i64> {
use std::sync::atomic::Ordering;
Ok(self.pending_op_count.load(Ordering::Relaxed))
}
/// SQL-backed count for tests / debug. Same shape as v0.7.0's
/// `count_pending_ops` but routed through `read_conn`. Kept as a
/// reconciliation oracle for the cached counter; in production paths
/// use `count_pending_ops`.
#[doc(hidden)]
pub fn count_pending_ops_sql(&self) -> Result<i64> {
let conn = self.read_conn();
let count: i64 =
conn.query_row("SELECT COUNT(*) FROM oplog WHERE applied = 0", [], |row| {
row.get(0)
})?;
Ok(count)
}
/// **Decoupled write path RFC, Phase 1.**
///
/// Mark a pending oplog entry as materialized. Called by the background
/// worker after it has applied the op to the in-memory indexes.
///
/// **Returns** `Ok(true)` iff this caller transitioned the row from
/// `applied=0` to `applied=1`. `Ok(false)` means another worker
/// already applied it (race on shared oplog, normal under N workers).
///
/// **This is a completion ACKNOWLEDGEMENT, not a pre-work claim.** The
/// distinction is load-bearing and was previously documented backwards
/// ("the work is idempotent so double-execution is safe; this filter just
/// decides which worker gets to claim the apply count"). Callers do the work
/// FIRST and only then race on `WHERE applied = 0`, so the filter does not
/// prevent duplicate execution — N workers draining the same pending op all
/// perform the work, and it merely picks which one gets to record it.
/// Retries after an error re-execute it too.
///
/// So `apply_pending_ops_once` is exactly-once in its BOOKKEEPING and
/// at-least-once in its EFFECTS. Every op handler must therefore be
/// genuinely idempotent on its own; that is a real obligation, not a
/// property this function confers. It was violated by the mention-count
/// bump in `apply_materialize_record_post` (see the note there).
/// Making this a true pre-work lease is v0.10 Item 4a.6.
pub fn mark_op_applied(&self, op_id: &str) -> Result<bool> {
use std::sync::atomic::Ordering;
let conn = self.conn.lock();
let changed = conn.execute(
"UPDATE oplog SET applied = 1 WHERE op_id = ?1 AND applied = 0",
params![op_id],
)?;
let won = changed > 0;
// **v0.7.1**: decrement the cached counter only when this caller
// actually transitioned the row. Mirrors log_op_pending's
// increment-only-on-real-insert pattern; keeps the atomic
// coherent with SQL applied-state under N concurrent workers.
if won {
self.pending_op_count.fetch_sub(1, Ordering::Relaxed);
}
Ok(won)
}
/// **Decoupled write path RFC, Phase 3 scaffolding.**
///
/// Drain up to `limit` pending oplog entries (applied=0) and apply each
/// to the engine's in-memory indexes. Returns the number of ops actually
/// applied this pass. Idempotent on re-entry — already-applied ops are
/// skipped via the `applied = 0` filter.
///
/// This is the worker's main-loop body as a sync function. Phase 3.5
/// will wrap it in a thread spawn + condvar wake + Drop-based shutdown.
/// Phase 4 will switch foreground `record()` to call `log_op_pending()`
/// instead of materializing inline, at which point this drain becomes
/// the production write-completion path.
///
/// Op-type dispatch in Phase 3 is intentionally a stub: each op type
/// has a placeholder materializer that just marks the op applied. Phase 4
/// fills in the actual application logic (memories INSERT, vec_index
/// update, graph_index update, scoring_cache insert) — and at that point
/// foreground record() can stop doing it inline.
/// THE materializer drain query — one definition, shared by the drain
/// itself and by the test that pins its query plan (#113).
///
/// It is a const rather than an inline literal specifically so the
/// plan-assertion test cannot pin a private copy: a test that asserted
/// against its own duplicated SQL would keep passing while this query
/// drifted back into a full scan. The index it depends on is
/// `idx_oplog_pending_ordered` — partial on `(hlc, op_id)`, i.e. on the
/// SORT KEYS, because a partial index on the filter column alone cannot
/// serve the ORDER BY and SQLite will silently prefer a full history
/// scan via `idx_oplog_hlc`. Changing the ORDER BY here without changing
/// that index re-introduces the idle-CPU defect.
pub(crate) const PENDING_OPS_QUERY: &'static str =
"SELECT op_id, op_type, payload, embedding_model FROM oplog \
WHERE applied = 0 \
ORDER BY hlc, op_id \
LIMIT ?1";
pub fn apply_pending_ops_once(&self, limit: usize) -> Result<usize> {
// **Issue #41 Layer 5.** Pull `embedding_model` alongside the
// standard tuple so we can dispatch queued-during-reembed
// writes through the re-encode path. embedding_model IS NOT
// NULL is the v27 signature for record_queued ops (sync
// record() leaves it NULL).
let pending: Vec<(String, String, String, Option<String>)> = {
let conn = self.read_conn();
let mut stmt = conn.prepare(Self::PENDING_OPS_QUERY)?;
let rows = stmt.query_map(params![limit as i64], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
row.get::<_, Option<String>>(3)?,
))
})?;
rows.collect::<std::result::Result<Vec<_>, _>>()?
};
// 0.13.2: payloads are sealed on encrypted databases. Decode HERE,
// at the single point every apply_* dispatch flows through, so the
// materializers keep taking plaintext JSON and no future op type
// can be added that forgets to unseal (the copy-a-pattern class).
let pending: Vec<(String, String, String, Option<String>)> = pending
.into_iter()
.map(|(op_id, op_type, payload, model)| {
self.decode_oplog_payload(&payload)
.map(|p| (op_id, op_type, p, model))
})
.collect::<Result<Vec<_>>>()?;
let mut applied = 0usize;
for (op_id, op_type, payload, embedding_model) in &pending {
match op_type.as_str() {
// **Phase 4.3 — saga task 3.** This is the only op_type
// whose dispatch is *real* materialization work (the
// unbounded entity/relation loops that used to be on the
// foreground request path). Foreground enqueues; worker
// applies. See docs/phase_4_3_design.md for the contract.
crate::engine::op_types::OP_MATERIALIZE_RECORD_POST => {
match self.apply_materialize_record_post(payload) {
Ok(()) => {
// Only count this apply if THIS worker won
// the race to flip applied=0 -> applied=1.
// Other workers may have done duplicate work
// (idempotent), but exactly one gets the count.
if self.mark_op_applied(op_id)? {
applied += 1;
}
}
Err(e) => {
// Don't mark applied — leave pending for retry
// on next tick. Workers race-safe via the
// applied=0 filter, so a transient failure
// doesn't lose the op.
tracing::warn!(
target: "yantrikdb::ingest::materialize",
op_id = %op_id,
op_type = %op_type,
error = %e,
"post-record materialization failed; leaving pending for retry"
);
}
}
}
// **Phase 4.3 Commit C — saga task 19.** Cluster-mode
// sibling of OP_MATERIALIZE_RECORD_POST. Same race-safety
// semantics; the difference is the dispatch logic uses
// caller-supplied entity list with no extraction.
crate::engine::op_types::OP_MATERIALIZE_RECORD_WITH_RID_POST => {
match self.apply_materialize_record_with_rid_post(payload) {
Ok(()) => {
if self.mark_op_applied(op_id)? {
applied += 1;
}
}
Err(e) => {
tracing::warn!(
target: "yantrikdb::ingest::materialize",
op_id = %op_id,
op_type = %op_type,
error = %e,
"post-record-with-rid materialization failed; leaving pending for retry"
);
}
}
}
// **Issue #41 Layer 5 — Queue-mode record drain.** The
// signature embedding_model IS NOT NULL identifies an
// op that was queued by `record_queued` during a
// reembed cutover. It carries TEXT (not an embedding).
// After the swap completes, the materializer drains
// these ops by re-encoding the text under the active
// generation's embedder + applying directly into the
// memories table + new vec_index at the new gen.
//
// While reembed is still in flight (meta.reembed_state
// set), defer: leave applied=0 so the next tick after
// completion picks it up. This matches brainstorm-2 §5
// "materializer fully PAUSED during reembed" (we pause
// only this op class — other op types still drain).
"record" if embedding_model.is_some() => {
if self.reembed_status().is_some() {
tracing::trace!(
target: "yantrikdb::ingest::materialize",
op_id = %op_id,
"Layer 5: reembed in flight; deferring queued record"
);
// No mark_op_applied — leave for next tick.
continue;
}
match self.apply_queued_reembed_record(payload) {
Ok(()) => {
if self.mark_op_applied(op_id)? {
applied += 1;
}
}
Err(e) => {
tracing::warn!(
target: "yantrikdb::ingest::materialize",
op_id = %op_id,
op_type = %op_type,
error = %e,
"Layer 5 queued reembed record apply failed; leaving pending for retry"
);
}
}
}
"record" | "forget" | "relate" | "correct" | "consolidate" => {
tracing::trace!(
target: "yantrikdb::ingest::materialize",
op_id = %op_id,
op_type = %op_type,
"phase 3 stub: marking pending op as applied without inline materialization"
);
if self.mark_op_applied(op_id)? {
applied += 1;
}
}
other => {
tracing::warn!(
target: "yantrikdb::ingest::materialize",
op_id = %op_id,
op_type = %other,
"unknown op_type in pending oplog — skipping"
);
}
}
}
Ok(applied)
}
/// Drain the materializer backlog to a fixed point, bounded.
///
/// **Why this exists (#95).** Foreground `record()` no longer extracts
/// entities or claims inline — it enqueues `OP_MATERIALIZE_RECORD_POST`
/// and returns (`engine/record.rs`). The extraction that actually
/// populates `entities`, `memory_entities` and `claims` runs in
/// [`Self::apply_materialize_record_post`], reached only from
/// [`Self::apply_pending_ops_once`] — which, before this, was called
/// only by the background materializer pool on its 100 ms idle timer
/// ([`crate::engine::materializer`]) and had no binding surface at all.
///
/// So every `record(); record(); think()` sequence RACED that timer and
/// usually lost: the conflict scans read `claims`, `claims` was still
/// empty, and `think()` reported `conflicts_found: 0`. Adding a `sleep`
/// before `think()` — changing nothing else — flipped the same input to
/// `1`. The same race is why `search_entities` and
/// `backfill_memory_entities` returned 0 for freshly-written memories:
/// the ops were still sitting in the oplog at `applied = 0`.
///
/// **Bounded, deliberately.** `think()` is a foreground call with a
/// latency budget; it must not become "block until an arbitrarily large
/// backlog clears". Two limits:
///
/// - each pass drains at most [`THINK_DRAIN_BATCH`] ops, so the conn
/// lock is released between passes and concurrent writers make
/// progress;
/// - the call applies at most [`THINK_DRAIN_BUDGET`] ops in total.
///
/// The budget is the honest tradeoff and worth stating plainly: the
/// drain query is `ORDER BY hlc, op_id` (oldest first), so on a store
/// whose backlog exceeds the budget, `think()` clears the *oldest*
/// ops and the just-written ones may still be pending. Determinism is
/// therefore guaranteed for the case that matters — an interactive
/// caller whose backlog is its own handful of writes — and degrades to
/// today's behaviour under a backlog larger than 4096 ops, rather than
/// degrading into an unbounded stall. A hang would be strictly worse
/// than the bug being fixed.
///
/// **Terminates.** Each pass either applies at least one op or returns
/// zero and breaks. Ops that a materializer refuses (an apply error, or
/// a record deferred mid-reembed) stay `applied = 0` and are simply not
/// counted, so a queue of nothing-but-deferred ops yields `0` on the
/// first pass and exits — it cannot spin.
///
/// **Cannot deadlock.** `apply_pending_ops_once` and the materializers
/// it dispatches to acquire [`Self::conn`] themselves, and that mutex
/// is not reentrant. Every caller must therefore hold NO connection
/// guard across this call. `think()` satisfies that by calling it as
/// its first statement, before any phase takes a guard.
///
/// **Safe against the background pool.** Concurrent foreground and
/// worker drains are the design `apply_pending_ops_once` already
/// documents: the materializers are idempotent, and `mark_op_applied`
/// awards the count to exactly one racer. A pass that loses every race
/// returns `0` while the effects it applied are nonetheless in place,
/// which is why breaking on zero is correct rather than premature.
///
/// Returns the number of ops this call was credited with applying.
pub(crate) fn drain_materializer_backlog(&self) -> Result<usize> {
let mut total = 0usize;
while total < THINK_DRAIN_BUDGET {
let batch = THINK_DRAIN_BATCH.min(THINK_DRAIN_BUDGET - total);
let applied = self.apply_pending_ops_once(batch)?;
if applied == 0 {
break;
}
total += applied;
}
Ok(total)
}
/// **Phase 4.3 — apply a queued `materialize_record_post` op.**
///
/// Mirrors the post-INSERT entity/relation extraction loop that used to
/// live on the foreground `record()` path. Now runs on the materializer
/// thread so the foreground caller is not blocked on the unbounded
/// loop count (5-15 entities + 0-3 relations per typical record).
///
/// Idempotent: every SQL operation here is `INSERT OR IGNORE` on a
/// natural key (entity name, memory_entities pair, edge tuple). A
/// double-apply across worker restarts produces identical state.
///
/// Payload shape (see `docs/phase_4_3_design.md`):
///
/// ```json
/// {
/// "rid": "01HX...",
/// "text": "<plaintext OR engine-encrypted>",
/// "namespace": "default",
/// "ts_secs": 1715184000.0,
/// "domain": "general",
/// "source": "user"
/// }
/// ```
/// **Issue #41 Layer 5 — apply a queued-during-reembed record.**
///
/// `record_queued` writes oplog rows with applied=0,
/// embedding_model=<old_name>, and a payload carrying the
/// memory's TEXT (not pre-encoded embedding). After reembed
/// completes, this materializer drain re-encodes the text under
/// the ACTIVE SearchState's embedder (the new one) and applies
/// the row to memories + vec_index at the new generation.
///
/// Idempotent: INSERT OR IGNORE on rid + the
/// search_state-snapshot-once pattern guarantees a re-apply
/// (from worker race or restart) produces identical state.
///
/// Caller (apply_pending_ops_once) has already verified the op
/// type is "record" AND embedding_model IS NOT NULL AND no
/// reembed is in flight.
fn apply_queued_reembed_record(&self, payload_json: &str) -> Result<()> {
use crate::serde_helpers::serialize_f32;
let payload: serde_json::Value = serde_json::from_str(payload_json).map_err(|e| {
crate::error::YantrikDbError::InvalidInput(format!(
"Layer 5 record drain: payload parse failed: {e}"
))
})?;
let rid = payload.get("rid").and_then(|v| v.as_str()).ok_or_else(|| {
crate::error::YantrikDbError::InvalidInput("Layer 5 record drain: missing rid".into())
})?;
let memory_type = payload
.get("type")
.and_then(|v| v.as_str())
.unwrap_or("episodic");
let text = payload
.get("text")
.and_then(|v| v.as_str())
.ok_or_else(|| {
crate::error::YantrikDbError::InvalidInput(
"Layer 5 record drain: missing text".into(),
)
})?;
let importance = payload
.get("importance")
.and_then(|v| v.as_f64())
.unwrap_or(0.5);
let valence = payload
.get("valence")
.and_then(|v| v.as_f64())
.unwrap_or(0.0);
let half_life = payload
.get("half_life")
.and_then(|v| v.as_f64())
.unwrap_or(604800.0);
let metadata = payload
.get("metadata")
.cloned()
.unwrap_or_else(|| serde_json::json!({}));
let ts = payload
.get("created_at")
.and_then(|v| v.as_f64())
.unwrap_or_else(super::now);
let namespace = payload
.get("namespace")
.and_then(|v| v.as_str())
.unwrap_or("default");
let certainty = payload
.get("certainty")
.and_then(|v| v.as_f64())
.unwrap_or(0.8);
let domain = payload
.get("domain")
.and_then(|v| v.as_str())
.unwrap_or("general");
let source = payload
.get("source")
.and_then(|v| v.as_str())
.unwrap_or("user");
let emotional_state = payload
.get("emotional_state")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// 4a.6c: the v37 idempotency columns, carried by keyed queued writes.
// Absent/null (every pre-4a.6c op, every keyless write) stores NULL —
// identical to the pre-4a.6c row shape.
let idempotency_key = payload
.get("idempotency_key")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let origin_actor = payload
.get("origin_actor")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
// Snapshot the active SearchState. The embedder + generation
// here are the post-swap ones (caller verified no reembed in
// flight; the in-memory state is durable).
let state = self.search_state.load_full();
let embedder = state
.embedder
.as_ref()
.ok_or_else(|| {
crate::error::YantrikDbError::Inference(
"Layer 5 record drain: active SearchState has no embedder (cannot re-encode)"
.into(),
)
})?
.clone();
let new_emb = embedder.embed(text).map_err(|e| {
crate::error::YantrikDbError::Inference(format!(
"Layer 5 record drain: embedder failed on rid {rid:?}: {e}"
))
})?;
if new_emb.len() != state.dim() {
return Err(crate::error::YantrikDbError::Inference(format!(
"Layer 5 record drain: embedder returned len {} but SearchState dim {}",
new_emb.len(),
state.dim(),
)));
}
let emb_blob = serialize_f32(&new_emb);
let stored_emb = self.encrypt_embedding(&emb_blob)?;
// The payload's text/metadata are already in engine-stored
// form (record_queued passed them through). Don't double-
// encrypt; just hand back as stored.
let stored_text = text.to_string();
let stored_meta = serde_json::to_string(&metadata)?;
// v48 (#149): event-time columns from the SAME value `stored_meta`
// is serialized from. On an encrypted store the queued payload's
// metadata is the stored (ciphertext-string) form; bounds are then
// None, matching a blob that exposes no extractable event time.
let (event_time_min, event_time_max) = crate::base::datetext::event_time_bounds(&metadata);
// v50: source_turn from the same value `stored_meta` is serialized
// from. On an encrypted store that value is ciphertext (a JSON
// string), the extractor sees no keys, and the column stays NULL —
// which is exactly why the completeness marker must NOT be restored
// in that case: this write may have persisted an (encrypted) turn
// the column does not carry, so the trigger's '0' legitimately
// stands and `maintain_source_turn_backfill` owes the row a
// decrypt-and-stamp. Only a plaintext-visible metadata object was
// stamped faithfully and may preserve the marker.
let source_turn = crate::engine::thread::extract_source_turn(&metadata);
let metadata_is_plaintext = metadata.is_object();
let embedding_generation: i64 = state.generation as i64;
// INSERT OR IGNORE on rid for idempotency. If a prior worker
// already inserted this row (race + retry), the OR IGNORE
// makes this a no-op AND mark_op_applied lets one worker win
// the count.
{
let conn = self.conn();
let marker_prior = if metadata_is_plaintext {
Some(crate::engine::thread::marker_snapshot(&conn)?)
} else {
None
};
conn.execute(
"INSERT OR IGNORE INTO memories \
(rid, type, text, embedding, created_at, updated_at, importance, \
half_life, last_access, valence, metadata, namespace, \
certainty, domain, source, emotional_state, embedding_generation, \
idempotency_key, origin_actor, event_time_min, event_time_max, \
source_turn) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, \
?18, ?19, ?20, ?21, ?22)",
params![
rid,
memory_type,
stored_text,
stored_emb,
ts,
ts,
importance,
half_life,
ts,
valence,
stored_meta,
namespace,
certainty,
domain,
source,
emotional_state,
embedding_generation,
idempotency_key,
origin_actor,
// v48 (#149) event time.
event_time_min,
event_time_max,
// v50 source turn.
source_turn,
],
)?;
if let Some(prior) = marker_prior {
crate::engine::thread::marker_restore(&conn, &prior)?;
}
}
// Chunked embeddings: the drain is a re-encode of TEXT under the
// post-swap embedder, so it chunks exactly like record_text — a
// queued long record must not silently lose its tail just
// because it arrived during a reembed cutover. Embedded from the
// same string the head embed used, under the same snapshot.
let chunk_vecs: Vec<(usize, Vec<f32>)> = match self.chunk_plan(text) {
Some(ranges) => {
let mut cv = Vec::with_capacity(ranges.len());
for (i, (a, b)) in ranges.iter().enumerate() {
let v = embedder.embed(&text[*a..*b]).map_err(|e| {
crate::error::YantrikDbError::Inference(format!(
"Layer 5 record drain: embedder failed on rid {rid:?} chunk {}: {e}",
i + 1
))
})?;
crate::validate::validate_embedding("record_drain#chunk", &v, state.dim())?;
cv.push((i + 1, v));
}
cv
}
None => Vec::new(),
};
if !chunk_vecs.is_empty() {
let conn = self.conn();
for (idx, v) in &chunk_vecs {
let blob = self.encrypt_embedding(&serialize_f32(v))?;
// OR REPLACE: a retried drain overwrites its own prior
// partial work rather than erroring on the PK.
conn.execute(
"INSERT OR REPLACE INTO memory_chunks (rid, chunk_idx, embedding) \
VALUES (?1, ?2, ?3)",
params![rid, *idx as i64, blob],
)?;
}
}
// Append into the active vec_index. Idempotent: DeltaIndex
// de-dupes on rid+seq.
let seq = self
.vec_seq
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+ 1;
state.vec_index.append(rid.to_string(), new_emb, seq)?;
// Windows after the parent, same seq (a chunk hit collapses to
// the parent, which must be findable first).
for (idx, v) in &chunk_vecs {
let key = crate::vector::chunk::chunk_key(rid, *idx);
state.vec_index.append(key, v.clone(), seq)?;
}
if !chunk_vecs.is_empty() {
self.note_chunked_write();
}
// Bump visible_seq for RYW. Layer 6 will refine the
// generation-aware semantics; for now bump under the
// current generation's covers_through_seq logic.
self.bump_visible_seq(namespace, seq);
// The two obligations every other write surface discharges and this
// drain silently didn't (found in the 2026-08-15 surface-family
// audit):
//
// 1. SCORING CACHE. Every recall lane begins `let Some(row) =
// cache.get(rid)` and the cache loads from SQL only at open — so
// a record written during a reembed cutover was durable, oplogged
// and vector-indexed, yet INVISIBLE to every recall until the
// engine restarted.
self.cache_insert(
rid.to_string(),
crate::types::ScoringRow {
created_at: ts,
importance,
half_life,
last_access: ts,
access_count: 0,
valence,
consolidation_status: "active".to_string(),
synthesis_state: None,
synthesis_axis: None,
synthesis_granularity: None,
memory_type: memory_type.to_string(),
namespace: namespace.to_string(),
certainty,
domain: domain.to_string(),
source: source.to_string(),
emotional_state: emotional_state.map(|s| s.to_string()),
},
);
// 2. MATERIALIZE POST-OP. record() enqueues entity extraction /
// claims ingestion for every write; the drain never did, so
// cutover-written records permanently got no entities, no graph
// links, no claims. Not the record_with_rid determinism contract
// — this is an ORIGIN write.
let post_payload = serde_json::json!({
"rid": rid,
"text": stored_text,
"namespace": namespace,
"ts_secs": ts,
"domain": domain,
"source": source,
});
self.log_op_pending(
crate::engine::op_types::OP_MATERIALIZE_RECORD_POST,
Some(rid),
&post_payload,
None,
None,
)?;
Ok(())
}
fn apply_materialize_record_post(&self, payload_json: &str) -> Result<()> {
let payload: serde_json::Value = serde_json::from_str(payload_json).map_err(|e| {
crate::error::YantrikDbError::InvalidInput(format!(
"materialize_record_post: payload parse failed: {e}"
))
})?;
let rid = payload.get("rid").and_then(|v| v.as_str()).ok_or_else(|| {
crate::error::YantrikDbError::InvalidInput(
"materialize_record_post: missing rid".into(),
)
})?;
let text_stored = payload
.get("text")
.and_then(|v| v.as_str())
.ok_or_else(|| {
crate::error::YantrikDbError::InvalidInput(
"materialize_record_post: missing text".into(),
)
})?;
let namespace = payload
.get("namespace")
.and_then(|v| v.as_str())
.unwrap_or("default");
let ts_secs = payload
.get("ts_secs")
.and_then(|v| v.as_f64())
.unwrap_or_else(super::now);
let domain = payload
.get("domain")
.and_then(|v| v.as_str())
.unwrap_or("general");
let source = payload
.get("source")
.and_then(|v| v.as_str())
.unwrap_or("user");
// Decrypt the text field if engine encrypted at rest. The payload
// stored the engine-encrypted form, so the worker decrypts before
// running the heuristic extractor on plaintext.
let text_owned: String = self.decrypt_text(text_stored)?;
let text = text_owned.as_str();
let text_tokens = crate::graph::tokenize(text);
// The store learns how it writes each token BEFORE judging this
// memory's candidates, so a word the store has been writing in
// lowercase is already a word when it shows up capitalized at a
// sentence start here.
{
let conn = self.conn();
self.record_token_case_observations(&conn, text)?;
}
let heuristic_entities = self.extract_entities_for(text);
// Loop A: seed heuristic entities.
//
// The mention bump is gated on `memory_entities` so replaying this op is
// a no-op for the counter. It previously ran unconditionally under a
// comment calling the statement "idempotent" — the INSERT does not FAIL
// on conflict, but `mention_count = mention_count + 1` is not idempotent
// at all, and this op is genuinely executed more than once:
//
// - `mark_op_applied` is a completion ACKNOWLEDGEMENT, not a pre-work
// claim. Workers do the work and only THEN race on
// `UPDATE ... WHERE applied = 0`, so N workers draining the same
// pending op all run this loop; the filter merely picks who counts it.
// - on error the op stays pending and is retried, re-running the loop.
//
// Each duplicate inflated every heuristic entity's mention_count, which
// feeds the IDF term in `patterns.rs` (`total_entities / (1 + mention_count)`),
// so the drift silently skewed salience scoring.
//
// `memory_entities` is PK'd on (memory_rid, entity_name) and carries no
// FK, so `INSERT OR IGNORE` + `changes()` is an exact, durable answer to
// "is this the first time this memory's mention of this entity has been
// recorded?" — which is precisely what the counter is supposed to count.
// It is per-entity, so a run that died midway through the loop still
// replays correctly: entities already counted are skipped, the rest are
// counted once.
if !heuristic_entities.is_empty() {
let conn = self.conn();
for entity in &heuristic_entities {
let entity_type = crate::graph::classify_entity_type(entity);
// Claim the mention first; Loop B's INSERT OR IGNORE below is a
// no-op for anything already claimed here.
let first_mention = conn.execute(
"INSERT OR IGNORE INTO memory_entities \
(memory_rid, entity_name, entity_name_norm) VALUES (?1, ?2, ?3)",
params![
rid,
entity,
crate::engine::thread::normalize_entity_name(entity)
],
)? > 0;
// Self-heal AFTER the first-mention read: the repair is a
// separate UPDATE precisely so it can never count as a
// mention (see repair_entity_norm).
crate::engine::thread::repair_entity_norm(&conn, rid, entity)?;
let inc: i64 = if first_mention { 1 } else { 0 };
conn.execute(
"INSERT INTO entities (name, entity_type, first_seen, last_seen, mention_count) \
VALUES (?1, ?2, ?3, ?3, ?4) \
ON CONFLICT(name) DO UPDATE SET \
last_seen = ?3, \
mention_count = mention_count + ?4, \
entity_type = CASE \
WHEN entity_type = 'unknown' AND ?2 != 'unknown' THEN ?2 \
ELSE entity_type END",
params![entity, entity_type, ts_secs, inc],
)?;
}
}
// Compose candidate set: heuristic + already-known entities.
let mut candidates: std::collections::HashSet<String> =
heuristic_entities.iter().cloned().collect();
for known in self.graph_index.read().all_entity_names() {
if crate::graph::entity_matches_text(&known, &text_tokens) {
candidates.insert(known);
}
}
if !candidates.is_empty() {
// Loop B: memory_entities INSERT OR IGNORE.
{
let conn = self.conn();
for entity in &candidates {
conn.execute(
"INSERT OR IGNORE INTO memory_entities \
(memory_rid, entity_name, entity_name_norm) VALUES (?1, ?2, ?3)",
params![
rid,
entity,
crate::engine::thread::normalize_entity_name(entity)
],
)?;
crate::engine::thread::repair_entity_norm(&conn, rid, entity)?;
}
}
// graph_index in-memory update (idempotent — add_entity/link dedupe).
let mut gi = self.graph_index.write();
for entity in &candidates {
let entity_type = crate::graph::classify_entity_type(entity);
gi.add_entity(entity, entity_type);
gi.link_memory(rid, entity);
}
}
// Loops C+D+E live in `ingest_extracted_claims` so the one-time
// re-extraction heal (`reextract_claims`) runs the SAME code the
// materializer runs — one definition, two callers.
let heuristic_vec: Vec<String> = heuristic_entities.iter().cloned().collect();
self.ingest_extracted_claims(rid, text, namespace, &heuristic_vec);
// Audit telemetry — same shape as the foreground path emitted before.
let features = crate::graph::analyze_text_features(text, &heuristic_vec);
tracing::info!(
target: "yantrikdb::audit::extraction",
namespace = %namespace,
memory_rid = %rid,
domain = %domain,
source = %source,
extractor_version = "heuristic_v1",
char_length = features.char_length,
sentence_count = features.sentence_count,
entity_count = features.entity_count,
entities_matched_in_graph = candidates.len().saturating_sub(heuristic_entities.len()),
negation_cue_count = features.negation_cue_count,
temporal_cue_count = features.temporal_cue_count,
modality_cue_count = features.modality_cue_count,
has_compound_markers = features.has_compound_markers,
likely_assertion = features.likely_assertion,
"extraction audit (materialized post-record)"
);
Ok(())
}
/// Rewrite the refusal ledger rows for one memory: what the bound
/// extractor saw and would not bind, capped so a log-shaped memory
/// cannot flood the table. Best-effort: a ledger failure never fails
/// extraction.
pub(crate) fn record_extraction_refusals(
&self,
rid: &str,
namespace: &str,
refusals: &[crate::graph::ExtractionRefusal],
) {
let conn = self.conn();
if conn
.execute(
"DELETE FROM extraction_refusals WHERE memory_rid = ?1",
params![rid],
)
.is_err()
{
return; // pre-v54 store (an old pack): no ledger
}
let ts = now();
for r in refusals.iter().take(REFUSAL_LEDGER_CAP_PER_MEMORY) {
let _ = conn.execute(
"INSERT OR REPLACE INTO extraction_refusals (memory_rid, namespace, rel_type, \
trigger, reason, left_token, right_token, at, extractor_version, created_at) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
params![
rid,
namespace,
r.rel_type,
r.trigger,
r.reason,
r.left,
r.right,
r.at as i64,
BOUND_EXTRACTOR_VERSION,
ts
],
);
}
}
/// Relation extraction + claim ingestion for one memory (the
/// materializer's Loops C+D+E): built-in patterns as `heuristic_v1`,
/// active learned templates as `learned_v1`, never minting a fact any
/// extractor already recorded. Returns the number of claims written.
/// Shared by `apply_materialize_record_post` and `reextract_claims`.
pub(crate) fn ingest_extracted_claims(
&self,
rid: &str,
text: &str,
namespace: &str,
heuristic_vec: &[String],
) -> usize {
let mut written = 0usize;
// Extracted claims inherit the record's temporal tag too: a memory
// about 2024 yields claims valid from 2024, not from today.
let event_min = self.memory_event_time_min(rid);
// Value objects (`0.19.0`, `1985`) are never entities but must stay
// available as relation OBJECTS, or `runs`/`born_in` claims vanish
// with the entity junk they used to ride on (issue #213).
let mut candidates: Vec<String> = heuristic_vec.to_vec();
for v in crate::graph::extract_value_candidates(text) {
if !candidates.contains(&v) {
candidates.push(v);
}
}
let extraction = crate::graph::extract_relations_bound(text, &candidates);
self.record_extraction_refusals(rid, namespace, &extraction.refusals);
for rel in &extraction.relations {
// A value can be an object, never a subject: `2026 -leads-> X`
// anchors nothing at read time and is refused there anyway.
// And only a few relations can take a value as an object.
if crate::graph::is_rejected_entity_name(&rel.src)
|| !crate::graph::relation_admits_value_object(&rel.rel_type, &rel.dst)
{
continue;
}
let already_exists = {
let conn = self.conn();
conn.query_row(
"SELECT COUNT(*) FROM edges WHERE src = ?1 AND rel_type = ?2 AND dst = ?3 \
AND namespace = ?4 AND extractor = 'heuristic_v1' AND tombstoned = 0",
params![rel.src, rel.rel_type, rel.dst, namespace],
|row| row.get::<_, i64>(0),
)
.unwrap_or(0)
> 0
};
if already_exists {
continue;
}
let (span_start, span_end) = span_columns(rel.span);
written += usize::from(
self.ingest_claim_grounded(
&rel.src,
&rel.rel_type,
&rel.dst,
namespace,
rel.polarity,
&rel.modality,
event_min,
None,
"heuristic_v1",
Some(BOUND_EXTRACTOR_VERSION),
&rel.confidence_band,
Some(rid),
span_start,
span_end,
1.0,
crate::engine::claims_lane::GROUNDING_EXTRACTOR_BOUND,
)
.is_ok(),
);
}
// Loop E: self-mined templates (see engine::graph_ops, "Self-mined
// relation templates"). Active templates for this namespace are
// applied to the same entity windows; a fact any extractor already
// recorded is not minted twice.
let templates = {
let conn = self.conn();
Self::active_relation_templates(&conn, namespace)
};
if !templates.is_empty() {
let learned = crate::graph::extract_learned_relations(text, &candidates, &templates);
for rel in &learned {
if crate::graph::is_rejected_entity_name(&rel.src)
|| !crate::graph::relation_admits_value_object(&rel.rel_type, &rel.dst)
{
continue;
}
let already_exists = {
let conn = self.conn();
conn.query_row(
"SELECT COUNT(*) FROM edges WHERE src = ?1 AND rel_type = ?2 AND dst = ?3 \
AND namespace = ?4 AND tombstoned = 0",
params![rel.src, rel.rel_type, rel.dst, namespace],
|row| row.get::<_, i64>(0),
)
.unwrap_or(0)
> 0
};
if already_exists {
continue;
}
let (span_start, span_end) = span_columns(rel.span);
written += usize::from(
self.ingest_claim_grounded(
&rel.src,
&rel.rel_type,
&rel.dst,
namespace,
rel.polarity,
&rel.modality,
event_min,
None,
crate::engine::graph_ops::LEARNED_CLAIM_EXTRACTOR,
Some(BOUND_EXTRACTOR_VERSION),
&rel.confidence_band,
Some(rid),
span_start,
span_end,
1.0,
crate::engine::claims_lane::GROUNDING_EXTRACTOR_BOUND,
)
.is_ok(),
);
}
}
written
}
/// **Phase 4.3 Commit C — apply a queued `materialize_record_with_rid_post` op.**
///
/// Cluster-mode sibling of [`Self::apply_materialize_record_post`]. Runs the
/// post-INSERT entity / memory_entities / graph_index updates that used
/// to live inside the foreground SAVEPOINT block of `record_with_rid()`.
///
/// **Why it differs from the heuristic path.** `record_with_rid` is the
/// cluster determinism primitive — the leader's apply emits a payload
/// containing the explicit `extracted_entities` slice; followers must
/// converge to byte-identical SQL state by replaying that same slice.
/// Running heuristic extraction on the materializer would risk
/// divergence (extractor versions or text edge cases differing across
/// nodes). So this dispatch arm uses the caller's entity list verbatim,
/// no `extract_heuristic_entities` / `extract_heuristic_relations`
/// calls.
///
/// `was_new_row` payload field controls whether `entities.mention_count`
/// gets bumped on insert. Mirrors the original SQL conditional:
///
/// `mention_count = CASE WHEN ?was_new THEN mention_count + 1 ELSE mention_count END`
///
/// This preserves the contract that replayed-but-not-newly-inserted
/// memories don't double-count entity mentions.
///
/// Idempotent on every loop step (INSERT OR IGNORE, ON CONFLICT DO UPDATE
/// with mention_count guarded by `was_new_row`).
fn apply_materialize_record_with_rid_post(&self, payload_json: &str) -> Result<()> {
let payload: serde_json::Value = serde_json::from_str(payload_json).map_err(|e| {
crate::error::YantrikDbError::InvalidInput(format!(
"materialize_record_with_rid_post: payload parse failed: {e}"
))
})?;
let rid = payload.get("rid").and_then(|v| v.as_str()).ok_or_else(|| {
crate::error::YantrikDbError::InvalidInput(
"materialize_record_with_rid_post: missing rid".into(),
)
})?;
let _namespace = payload
.get("namespace")
.and_then(|v| v.as_str())
.unwrap_or("default");
let ts_secs = payload
.get("ts_secs")
.and_then(|v| v.as_f64())
.unwrap_or_else(super::now);
let was_new_row = payload
.get("was_new_row")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let entities: Vec<String> = payload
.get("extracted_entities")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|x| x.as_str().map(|s| s.to_string()))
.collect()
})
.unwrap_or_default();
if entities.is_empty() {
// Nothing to materialize — early return is the cheap idempotent path.
return Ok(());
}
// Loop A: entities INSERT (mirrors the inline savepoint block exactly).
{
let conn = self.conn();
for entity in &entities {
let entity_type = crate::graph::classify_entity_type(entity);
conn.execute(
"INSERT INTO entities (name, entity_type, first_seen, last_seen, mention_count) \
VALUES (?1, ?2, ?3, ?3, 1) \
ON CONFLICT(name) DO UPDATE SET \
last_seen = ?3, \
mention_count = CASE WHEN ?4 THEN mention_count + 1 ELSE mention_count END, \
entity_type = CASE \
WHEN entity_type = 'unknown' AND ?2 != 'unknown' THEN ?2 \
ELSE entity_type END",
params![entity, entity_type, ts_secs, was_new_row],
)?;
conn.execute(
"INSERT OR IGNORE INTO memory_entities \
(memory_rid, entity_name, entity_name_norm) VALUES (?1, ?2, ?3)",
params![
rid,
entity,
crate::engine::thread::normalize_entity_name(entity)
],
)?;
crate::engine::thread::repair_entity_norm(&conn, rid, entity)?;
}
}
// graph_index in-memory update (idempotent — add_entity/link_memory dedupe).
{
let mut gi = self.graph_index.write();
for entity in &entities {
let entity_type = crate::graph::classify_entity_type(entity);
gi.add_entity(entity, entity_type);
gi.link_memory(rid, entity);
}
}
Ok(())
}
/// **Phase 6 RYW** — allocate or accept a seq for a write primitive.
///
/// Single-node mode: callers pass `None` and the engine allocates a
/// fresh seq via `vec_seq.fetch_add` (1-indexed via `+ 1`).
///
/// Cluster mode (RFC 010, design lock 2026-05-07): the applier passes
/// `Some(commit_log_index)` so the seq IS the openraft commit-log
/// index — leader and followers thereby agree on a single global
/// monotonic stream. The engine ratchets `vec_seq` up to at least the
/// supplied value (via `fetch_max`) so any future single-node writes
/// against the same engine never produce seqs that collide with the
/// cluster-supplied stream.
///
/// Returns the seq the caller should use to tag the delta entry, the
/// oplog row, and the visible_seq bump.
pub(crate) fn assign_seq(&self, requested: Option<u64>) -> u64 {
use std::sync::atomic::Ordering;
match requested {
Some(n) => {
self.vec_seq.fetch_max(n, Ordering::Relaxed);
n
}
None => self.vec_seq.fetch_add(1, Ordering::Relaxed) + 1,
}
}
/// **Phase 6 RYW** — bump the visible_seq high-water mark for a
/// namespace. Called by record/record_with_rid and siblings after the
/// write has been materialized into the in-memory delta. Idempotent:
/// only advances the watermark via `fetch_max`; same-or-lower seqs
/// are no-ops.
///
/// Wakes any threads in ``recall_with_seq`` waiting on this namespace
/// via the paired condvar.
pub(crate) fn bump_visible_seq(&self, namespace: &str, seq: u64) {
use std::sync::atomic::Ordering;
// Fast path: namespace already present — single fetch_max, no
// hashmap mutation.
if let Some(entry) = self.visible_seq.get(namespace) {
entry.fetch_max(seq, Ordering::Release);
} else {
// First write for this namespace: insert. The DashMap entry
// API gives us insert-or-existing semantics atomically per
// shard. If two threads race to insert the same namespace
// for the first time, one wins and the other's fetch_max
// converges anyway.
self.visible_seq
.entry(namespace.to_string())
.or_insert_with(|| std::sync::atomic::AtomicU64::new(0))
.fetch_max(seq, Ordering::Release);
}
self.visible_seq_cv.notify_all();
}
/// **Phase 6 RYW** — current visible_seq high-water mark for a namespace.
/// Returns 0 for namespaces that have never been bumped.
///
/// Lock-free in steady state: a DashMap shard read + an atomic load.
pub fn visible_seq_for(&self, namespace: &str) -> u64 {
use std::sync::atomic::Ordering;
self.visible_seq
.get(namespace)
.map(|e| e.load(Ordering::Acquire))
.unwrap_or(0)
}
/// **Phase 6 RYW** — wait until visible_seq[namespace] >= min_seq or
/// the timeout expires. Returns ``Ok(())`` on watermark reached;
/// ``Err(Error::RyWaitTimeout)`` on timeout.
///
/// Callers requesting strict read-your-writes pass a seq from a prior
/// write to gate a subsequent recall. Default ``recall()`` does not
/// call this — the delta is always visible by virtue of being scanned
/// during search; this primitive is only needed when the caller wants
/// to wait through a compaction-in-progress window or a cluster
/// follower-apply-lag window.
pub fn wait_for_visible_seq(
&self,
namespace: &str,
min_seq: u64,
timeout: std::time::Duration,
) -> Result<()> {
let deadline = std::time::Instant::now() + timeout;
loop {
let current = self.visible_seq_for(namespace);
if current >= min_seq {
return Ok(());
}
let now = std::time::Instant::now();
if now >= deadline {
return Err(crate::error::YantrikDbError::RyWaitTimeout {
namespace: namespace.to_string(),
requested_seq: min_seq,
observed_seq: current,
waited_ms: timeout.as_millis() as u64,
});
}
let remaining = deadline - now;
// The sentinel mutex is a no-data lock pair for the Condvar.
// Critical race-avoidance pattern: re-check the watermark AFTER
// acquiring the mutex but BEFORE waiting, because the writer
// may have bumped + notified between our outer check and here.
let mut guard = self.visible_seq_wait_mu.lock();
let recheck = self.visible_seq_for(namespace);
if recheck >= min_seq {
return Ok(());
}
let result = self.visible_seq_cv.wait_for(&mut guard, remaining);
drop(guard);
if result.timed_out() {
let final_current = self.visible_seq_for(namespace);
if final_current >= min_seq {
return Ok(());
}
return Err(crate::error::YantrikDbError::RyWaitTimeout {
namespace: namespace.to_string(),
requested_seq: min_seq,
observed_seq: final_current,
waited_ms: timeout.as_millis() as u64,
});
}
// Spurious wakeup or notify_all — re-check the watermark.
}
}
}
#[cfg(test)]
mod pending_ops_tests {
use super::*;
use crate::YantrikDB;
fn open_test_db() -> YantrikDB {
// Use :memory: so tests don't touch disk and migrations are fresh.
YantrikDB::new(":memory:", 64).expect("open test db")
}
/// **#113 — the idle-CPU defect, pinned at the query plan.**
///
/// A field user burned ~55% of a 32-core machine while IDLE because every
/// 100ms materializer poll scanned the ENTIRE oplog: the drain query's
/// `ORDER BY hlc, op_id` could not be served by a partial index on the
/// filter column, so SQLite chose `idx_oplog_hlc`, walked all history
/// filtering row-by-row, and added a temp B-tree — and with nothing
/// pending, no LIMIT short-circuit ever fired. Cost grew with history
/// depth, times 16 workers, times N engines.
///
/// Asserting the PLAN, not a latency budget: the plan is deterministic
/// and the regression is structural (a re-ordered ORDER BY, a dropped
/// index, a "harmless" index rename all reintroduce it), whereas a timing
/// assertion would be flaky on CI and would only fail once a corpus grew
/// large enough to hurt — i.e. in a user's process, not here.
///
/// It runs against [`YantrikDB::PENDING_OPS_QUERY`], the same const the
/// drain executes. That is deliberate: pinning a copy of the SQL would
/// pass forever while the real query drifted.
#[test]
fn pending_ops_query_uses_the_partial_index_and_never_scans_history() {
let db = open_test_db();
let conn = db.conn();
// The index must exist on a FRESH database — the original
// `idx_oplog_pending` was added in a migration and never in
// SCHEMA_SQL, so every new install since v24 silently had no pending
// index at all.
let idx_exists: i64 = conn
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' \
AND name = 'idx_oplog_pending_ordered'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(
idx_exists, 1,
"fresh databases must carry idx_oplog_pending_ordered (it must live in \
SCHEMA_SQL, not only in a migration)"
);
let plan: Vec<String> = {
let sql = format!("EXPLAIN QUERY PLAN {}", YantrikDB::PENDING_OPS_QUERY);
let mut stmt = conn.prepare(&sql).unwrap();
let rows = stmt
.query_map(params![64_i64], |r| r.get::<_, String>(3))
.unwrap();
rows.collect::<std::result::Result<Vec<_>, _>>().unwrap()
};
let joined = plan.join(" | ");
assert!(
joined.contains("idx_oplog_pending_ordered"),
"the drain must use the partial pending index; plan was: {joined}"
);
// The two signatures of the defect, asserted separately so a failure
// says WHICH half regressed.
assert!(
!joined.contains("idx_oplog_hlc"),
"the drain fell back to the full-history hlc index — this is the #113 \
idle-scan defect; plan was: {joined}"
);
assert!(
!joined.to_uppercase().contains("TEMP B-TREE"),
"the drain is sorting in a temp B-tree, so the index no longer serves \
the ORDER BY; plan was: {joined}"
);
}
fn fake_embedding(seed: f32, dim: usize) -> Vec<u8> {
let raw: Vec<f32> = (0..dim).map(|i| (seed + i as f32) * 0.1).collect();
let norm: f32 = raw.iter().map(|x| x * x).sum::<f32>().sqrt().max(1e-9);
let normalized: Vec<f32> = raw.iter().map(|x| x / norm).collect();
crate::serde_helpers::serialize_f32(&normalized)
}
#[test]
fn pending_op_round_trip() {
let db = open_test_db();
assert_eq!(
db.count_pending_ops().unwrap(),
0,
"fresh db has no pending"
);
let payload = serde_json::json!({
"rid": "test_rid_1",
"type": "episodic",
"text": "first pending op",
});
let emb_bytes = fake_embedding(1.0, 64);
let op_id = db
.log_op_pending(
"record",
Some("test_rid_1"),
&payload,
None,
Some(&emb_bytes),
)
.expect("log_op_pending");
assert_eq!(
db.count_pending_ops().unwrap(),
1,
"one pending op after append"
);
db.mark_op_applied(&op_id).expect("mark applied");
assert_eq!(db.count_pending_ops().unwrap(), 0, "no pending after mark");
}
/// NOT a typo: this asserts the OPPOSITE of idempotency, which is the real
/// contract. It was named `pending_op_idempotent_on_double_append` — a lie
/// that agreed with the rustdoc `log_op_pending` used to carry, and which
/// justified the `INSERT OR IGNORE` this test's own body disproves.
#[test]
fn pending_op_double_append_writes_two_distinct_rows() {
let db = open_test_db();
let payload = serde_json::json!({"rid": "rid_idem"});
let emb_bytes = fake_embedding(2.0, 64);
let op_id_a = db
.log_op_pending("record", Some("rid_idem"), &payload, None, Some(&emb_bytes))
.unwrap();
let op_id_b = db
.log_op_pending("record", Some("rid_idem"), &payload, None, Some(&emb_bytes))
.unwrap();
// Two distinct op_ids generated (uuid7), but each is a separate row.
assert_ne!(op_id_a, op_id_b, "each call generates a distinct op_id");
assert_eq!(db.count_pending_ops().unwrap(), 2);
}
#[test]
fn pending_op_persists_embedding_blob() {
let db = open_test_db();
let emb_bytes = fake_embedding(3.0, 64);
let op_id = db
.log_op_pending(
"record",
Some("rid_emb"),
&serde_json::json!({}),
None,
Some(&emb_bytes),
)
.unwrap();
let conn = db.read_conn();
let stored: Option<Vec<u8>> = conn
.query_row(
"SELECT embedding FROM oplog WHERE op_id = ?1",
params![op_id],
|row| row.get(0),
)
.unwrap();
assert_eq!(
stored.as_deref(),
Some(emb_bytes.as_slice()),
"embedding bytes round-trip exactly"
);
}
#[test]
fn mark_op_applied_idempotent() {
let db = open_test_db();
let op_id = db
.log_op_pending("record", None, &serde_json::json!({}), None, None)
.unwrap();
db.mark_op_applied(&op_id).unwrap();
// Calling again on already-applied op is a no-op.
db.mark_op_applied(&op_id).unwrap();
assert_eq!(db.count_pending_ops().unwrap(), 0);
}
#[test]
fn count_pending_ignores_applied_ops() {
let db = open_test_db();
// Old log_op writes applied=1 directly.
db.log_op("record", Some("rid_old"), &serde_json::json!({}), None)
.unwrap();
assert_eq!(
db.count_pending_ops().unwrap(),
0,
"log_op (applied=1) is not pending"
);
// log_op_pending writes applied=0.
db.log_op_pending(
"record",
Some("rid_new"),
&serde_json::json!({}),
None,
None,
)
.unwrap();
assert_eq!(db.count_pending_ops().unwrap(), 1);
}
#[test]
fn backpressure_engages_at_max_pending() {
// Saturate the queue with 10_000 pending ops, then verify the
// 10_001st returns Error::Backpressure with sane fields.
let db = open_test_db();
for i in 0..10_000 {
db.log_op_pending(
"record",
Some(&format!("rid_{i}")),
&serde_json::json!({}),
None,
None,
)
.expect("first 10k succeed");
}
assert_eq!(db.count_pending_ops().unwrap(), 10_000);
let err = db
.log_op_pending(
"record",
Some("rid_overflow"),
&serde_json::json!({}),
None,
None,
)
.expect_err("11k must fail with backpressure");
match err {
crate::error::YantrikDbError::Backpressure {
pending,
max,
retry_after_ms,
} => {
assert_eq!(max, 10_000);
assert_eq!(pending, 10_000);
assert!(retry_after_ms > 0, "retry hint must be non-zero");
}
other => panic!("expected Backpressure, got {other:?}"),
}
// After draining one, the next push must succeed (proves backpressure
// is reactive, not sticky). v0.7.1: drain via the public
// `mark_op_applied` API so the cached `pending_op_count` atomic
// stays coherent. Bypassing it with raw SQL (the v0.7.0 shape)
// wouldn't decrement the counter and would falsely keep
// backpressure engaged — the new test path verifies the
// atomic counter contract end-to-end.
let one_op_id: String = {
let conn = db.read_conn();
conn.query_row(
"SELECT op_id FROM oplog WHERE applied = 0 LIMIT 1",
[],
|row| row.get(0),
)
.unwrap()
};
let was_unset = db.mark_op_applied(&one_op_id).unwrap();
assert!(was_unset, "mark_op_applied should win the transition");
db.log_op_pending(
"record",
Some("rid_after_drain"),
&serde_json::json!({}),
None,
None,
)
.expect("succeeds after one drained");
}
#[test]
fn apply_pending_drains_then_marks() {
let db = open_test_db();
// Seed 3 pending ops of various types.
for (op_type, target) in [
("record", "rid_1"),
("forget", "rid_2"),
("relate", "rid_3"),
] {
db.log_op_pending(op_type, Some(target), &serde_json::json!({}), None, None)
.unwrap();
}
assert_eq!(db.count_pending_ops().unwrap(), 3);
let applied = db.apply_pending_ops_once(10).unwrap();
assert_eq!(applied, 3, "all 3 pending ops drained in one pass");
assert_eq!(db.count_pending_ops().unwrap(), 0);
}
#[test]
fn apply_pending_respects_limit() {
let db = open_test_db();
for i in 0..5 {
db.log_op_pending(
"record",
Some(&format!("rid_{i}")),
&serde_json::json!({}),
None,
None,
)
.unwrap();
}
let applied = db.apply_pending_ops_once(2).unwrap();
assert_eq!(applied, 2, "only 2 of 5 drained when limit=2");
assert_eq!(db.count_pending_ops().unwrap(), 3);
// Subsequent drain picks up the rest.
let applied2 = db.apply_pending_ops_once(10).unwrap();
assert_eq!(applied2, 3);
assert_eq!(db.count_pending_ops().unwrap(), 0);
}
#[test]
fn apply_pending_idempotent_when_empty() {
let db = open_test_db();
// No pending ops — drain returns 0 cleanly.
assert_eq!(db.apply_pending_ops_once(100).unwrap(), 0);
assert_eq!(db.apply_pending_ops_once(100).unwrap(), 0);
}
#[test]
fn apply_pending_skips_unknown_op_type() {
let db = open_test_db();
// v0.7.1: enqueue via log_op_pending with a synthetic op_type so
// the cached `pending_op_count` atomic increments. v0.7.0 used
// direct SQL INSERT here, but that bypasses the counter and
// mismatches the public-API count_pending_ops contract — fixed
// alongside the perf hotfix.
db.log_op_pending(
"made_up_op",
Some("synth_unknown"),
&serde_json::json!({}),
None,
None,
)
.unwrap();
assert_eq!(db.count_pending_ops().unwrap(), 1);
// Drain doesn't apply unknown op types — they stay pending so a
// future runtime that knows the op type can drain them.
let applied = db.apply_pending_ops_once(10).unwrap();
assert_eq!(applied, 0);
assert_eq!(
db.count_pending_ops().unwrap(),
1,
"unknown op_type stays pending"
);
}
#[test]
fn schema_v25_columns_present() {
// Open a fresh DB so the canonical SCHEMA_SQL runs and creates
// memories with the v25 columns. Then verify column metadata.
let db = open_test_db();
let conn = db.read_conn();
let mut stmt = conn.prepare("PRAGMA table_info(memories)").unwrap();
let cols: Vec<String> = stmt
.query_map([], |row| row.get::<_, String>(1))
.unwrap()
.filter_map(|r| r.ok())
.collect();
assert!(
cols.contains(&"tombstone_reason".to_string()),
"tombstone_reason missing — schema v25 not applied"
);
assert!(
cols.contains(&"created_at_unix_micros".to_string()),
"created_at_unix_micros missing — schema v25 not applied"
);
assert!(
cols.contains(&"embedding_model".to_string()),
"embedding_model missing — schema v25 not applied"
);
}
#[test]
fn schema_version_meta_at_current() {
// Locks the meta-stamp to the SCHEMA_VERSION constant so a future
// bump (e.g. v26 → v27 from RFC 026's next phase) automatically
// moves this test forward without a manual literal edit.
// Previously hard-coded "25"; v26 (issue #29) made the brittleness
// obvious so the renaming + constant reference go together.
let db = open_test_db();
let conn = db.read_conn();
let v: String = conn
.query_row(
"SELECT value FROM meta WHERE key = 'schema_version'",
[],
|row| row.get(0),
)
.unwrap();
assert_eq!(v, crate::schema::SCHEMA_VERSION.to_string());
}
// ── Perf regression tests (added v0.7.1 after the SELECT COUNT incident) ──
//
// The v0.7.0 → v0.7.1 hotfix story (yantrikdb-server msg b951a2de):
// log_op_pending was running `SELECT COUNT(*) FROM oplog WHERE applied=0`
// on every foreground call. At 8 writers that's 16 conn acquisitions/sec
// just for the backpressure check. v0.7.1 replaced it with an
// AtomicI64 cached counter. These tests structurally pin both
// properties (atomic stays coherent with SQL truth + backpressure check
// is O(1) under load) so a future regression of the same class is
// caught BEFORE it ships, not by yantrikdb-server's homelab bench.
/// Atomic counter must never drift from SQL truth across mixed
/// log_op_pending / mark_op_applied / apply_pending_ops_once
/// operations. If this drifts, the backpressure check is wrong AND
/// `count_pending_ops()` lies to the materializer. Drift = silent
/// data loss possibility.
#[test]
fn pending_op_count_atomic_matches_sql_after_workload() {
let db = open_test_db();
// Phase 1: pure inserts.
for i in 0..50 {
db.log_op_pending(
"record",
Some(&format!("rid_w1_{i}")),
&serde_json::json!({}),
None,
None,
)
.unwrap();
assert_eq!(
db.count_pending_ops().unwrap(),
db.count_pending_ops_sql().unwrap(),
"drift after insert #{i}"
);
}
// Phase 2: mixed inserts + applies via apply_pending_ops_once
// (which exercises the full mark_op_applied → atomic.fetch_sub path).
let drained = db.apply_pending_ops_once(20).unwrap();
assert!(drained > 0, "drained at least one");
assert_eq!(
db.count_pending_ops().unwrap(),
db.count_pending_ops_sql().unwrap(),
"drift after apply_pending_ops_once"
);
// Phase 3: more inserts after partial drain.
for i in 0..30 {
db.log_op_pending(
"record",
Some(&format!("rid_w3_{i}")),
&serde_json::json!({}),
None,
None,
)
.unwrap();
}
assert_eq!(
db.count_pending_ops().unwrap(),
db.count_pending_ops_sql().unwrap(),
"drift after second-wave inserts"
);
// Phase 4: drain all the rest.
loop {
let n = db.apply_pending_ops_once(100).unwrap();
if n == 0 {
break;
}
}
assert_eq!(db.count_pending_ops().unwrap(), 0);
assert_eq!(db.count_pending_ops_sql().unwrap(), 0);
// Phase 5: idempotent re-mark must not double-decrement.
let extra_op_id = db
.log_op_pending(
"record",
Some("rid_extra"),
&serde_json::json!({}),
None,
None,
)
.unwrap();
let first_mark = db.mark_op_applied(&extra_op_id).unwrap();
let second_mark = db.mark_op_applied(&extra_op_id).unwrap();
assert!(first_mark, "first mark wins the transition");
assert!(!second_mark, "second mark is a no-op (idempotent)");
assert_eq!(
db.count_pending_ops().unwrap(),
0,
"double-mark must NOT push counter negative"
);
}
/// **Regression guard for the v0.7.0 → v0.7.1 hotfix.** Before the
/// fix, `log_op_pending` did a SELECT COUNT(*) WHERE applied=0 per
/// call. That made the foreground hot path scale O(pending_count)
/// despite the partial index. The fix made it a single atomic load.
///
/// This test pins the property: insert N pending ops, then time the
/// next log_op_pending call. Even with 5000 pending ops in the
/// oplog, the call should complete in <10ms (the actual production
/// number is ~1µs; the threshold is ~10000× looser to absorb CI
/// noise without false positives). If a regression re-introduces
/// SELECT COUNT, the call time grows with the count and the
/// assertion fires.
#[test]
fn log_op_pending_is_o1_under_pending_load() {
use std::time::Instant;
let db = open_test_db();
// Seed 5000 pending ops. Each insert is a real conn acquire +
// INSERT — this is the setup cost, not the test of interest.
for i in 0..5000 {
db.log_op_pending(
"record",
Some(&format!("rid_seed_{i}")),
&serde_json::json!({}),
None,
None,
)
.expect("seed insert");
}
assert_eq!(db.count_pending_ops().unwrap(), 5000);
// Time the next call. Pre-v0.7.1 this scanned 5000 oplog rows
// via the partial index AND did a separate read_conn acquire.
// Post-fix it's an atomic load. We give 10ms headroom for CI
// noise; the actual cost should be sub-millisecond.
let t0 = Instant::now();
let _id = db
.log_op_pending(
"record",
Some("rid_under_load"),
&serde_json::json!({}),
None,
None,
)
.expect("call under load");
let elapsed = t0.elapsed();
assert!(
elapsed.as_millis() < 10,
"log_op_pending should be O(1); 5001st call took {elapsed:?} \
(regression: probably SELECT COUNT re-introduced)"
);
}
/// Atomic counter must remain non-negative and stable when many
/// concurrent workers race on mark_op_applied. The applied=0 filter
/// in the SQL guarantees exactly-once SQL transition; the
/// `if won { fetch_sub }` guard inside mark_op_applied must
/// preserve that into the atomic.
#[test]
fn pending_op_count_under_concurrent_mark() {
use std::sync::Arc;
use std::thread;
let db = Arc::new(open_test_db());
for i in 0..100 {
db.log_op_pending(
"record",
Some(&format!("rid_conc_{i}")),
&serde_json::json!({}),
None,
None,
)
.unwrap();
}
assert_eq!(db.count_pending_ops().unwrap(), 100);
// Snapshot all op_ids so each worker can race to mark them.
let op_ids: Vec<String> = {
let conn = db.read_conn();
let mut stmt = conn
.prepare("SELECT op_id FROM oplog WHERE applied = 0")
.unwrap();
stmt.query_map([], |row| row.get::<_, String>(0))
.unwrap()
.filter_map(|r| r.ok())
.collect()
};
// 4 workers each try to mark every op. The applied=0 filter +
// bool return + `if won` guard means each op is decremented
// exactly once across all workers.
let mut handles = Vec::new();
for _ in 0..4 {
let db_c = Arc::clone(&db);
let ids_c = op_ids.clone();
handles.push(thread::spawn(move || {
for id in ids_c {
let _ = db_c.mark_op_applied(&id);
}
}));
}
for h in handles {
h.join().unwrap();
}
assert_eq!(
db.count_pending_ops().unwrap(),
0,
"concurrent racing on mark_op_applied must converge atomic to 0"
);
assert_eq!(
db.count_pending_ops_sql().unwrap(),
0,
"and SQL truth must agree"
);
}
#[test]
fn schema_v25_indexes_present() {
let db = open_test_db();
let conn = db.read_conn();
let mut stmt = conn
.prepare("SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='memories'")
.unwrap();
let names: Vec<String> = stmt
.query_map([], |row| row.get::<_, String>(0))
.unwrap()
.filter_map(|r| r.ok())
.collect();
assert!(
names.iter().any(|n| n == "idx_memories_created_at_micros"),
"idx_memories_created_at_micros missing"
);
assert!(
names.iter().any(|n| n == "idx_memories_embedding_model"),
"idx_memories_embedding_model missing"
);
}
// ── Phase 4.3 (saga task 3): materialize_record_post dispatch arm ──
//
// These tests exercise the worker-side materialization path WITHOUT
// changing foreground behavior. They enqueue an op directly via
// log_op_pending and drain via apply_pending_ops_once, asserting the
// resulting SQL/graph state matches what the foreground inline loop
// would have produced. Commit B will flip foreground to enqueue;
// these tests act as the contract pin so the flip is provably safe.
fn enqueue_post_record(db: &YantrikDB, rid: &str, text: &str, namespace: &str) -> String {
// First INSERT a stub memories row so memory_entities + claims
// FK references are valid. Foreground (Commit B) will INSERT the
// memories row before enqueuing; tests mirror that ordering.
let conn = db.conn();
let stored_text = db.encrypt_text(text).unwrap();
let ts = super::super::now();
conn.execute(
"INSERT INTO memories \
(rid, type, text, embedding, created_at, updated_at, importance, \
half_life, last_access, valence, metadata, namespace, \
certainty, domain, source, emotional_state) \
VALUES (?1, 'episodic', ?2, NULL, ?3, ?3, 0.5, 604800.0, ?3, 0.0, '{}', ?4, 0.8, 'general', 'user', NULL)",
params![rid, stored_text, ts, namespace],
).unwrap();
drop(conn);
let payload = serde_json::json!({
"rid": rid,
"text": stored_text,
"namespace": namespace,
"ts_secs": ts,
"domain": "general",
"source": "user",
});
db.log_op_pending(
crate::engine::op_types::OP_MATERIALIZE_RECORD_POST,
Some(rid),
&payload,
None,
None,
)
.expect("log_op_pending")
}
#[test]
fn materialize_record_post_inserts_entities() {
let db = open_test_db();
let _op_id = enqueue_post_record(&db, "r1", "Alice met Acme yesterday", "default");
assert_eq!(db.count_pending_ops().unwrap(), 1);
let n = db.apply_pending_ops_once(10).unwrap();
assert_eq!(n, 1, "one op drained");
assert_eq!(db.count_pending_ops().unwrap(), 0);
// Entities table should now contain Alice and Acme.
let conn = db.read_conn();
let alice: i64 = conn
.query_row(
"SELECT COUNT(*) FROM entities WHERE name = 'Alice'",
[],
|r| r.get(0),
)
.unwrap();
let acme: i64 = conn
.query_row(
"SELECT COUNT(*) FROM entities WHERE name = 'Acme'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(alice, 1, "Alice seeded by heuristic");
assert_eq!(acme, 1, "Acme seeded by heuristic");
}
#[test]
fn materialize_record_post_inserts_memory_entities() {
let db = open_test_db();
let _op_id = enqueue_post_record(&db, "r2", "Bob works at Beta Corp", "default");
let _ = db.apply_pending_ops_once(10).unwrap();
let conn = db.read_conn();
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM memory_entities WHERE memory_rid = 'r2'",
[],
|r| r.get(0),
)
.unwrap();
assert!(
count >= 2,
"memory_entities has at least 2 rows for Bob+Beta Corp; got {count}"
);
}
#[test]
fn materialize_record_post_idempotent_on_double_drain() {
// Drain twice — second drain must be a no-op (op already marked
// applied by first drain). entities mention_count must stay at 1.
let db = open_test_db();
let _op_id = enqueue_post_record(&db, "r3", "Charlie went to Delta", "default");
let n1 = db.apply_pending_ops_once(10).unwrap();
let n2 = db.apply_pending_ops_once(10).unwrap();
assert_eq!(n1, 1, "first drain applies");
assert_eq!(n2, 0, "second drain finds nothing pending");
let conn = db.read_conn();
let mc: i64 = conn
.query_row(
"SELECT mention_count FROM entities WHERE name = 'Charlie'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(mc, 1, "mention_count not double-bumped");
}
#[test]
fn materialize_record_post_updates_graph_index() {
// graph_index in-memory state must reflect the worker's apply
// — this is what makes recall-by-entity find the new memory.
let db = open_test_db();
let _op_id = enqueue_post_record(&db, "r4", "Eve climbed Everest", "default");
let _ = db.apply_pending_ops_once(10).unwrap();
let gi = db.graph_index.read();
let names = gi.all_entity_names();
assert!(names.iter().any(|n| n == "Eve"), "Eve in graph_index");
assert!(
names.iter().any(|n| n == "Everest"),
"Everest in graph_index"
);
}
#[test]
fn materialize_record_post_concurrent_workers_no_double_apply() {
// 4 worker threads + 20 ops → exactly-once semantics on the
// applied=0 filter. Same race-safety as the existing materializer
// tests, but exercising the new dispatch path.
use std::sync::Arc;
use std::thread;
let db = Arc::new(open_test_db());
for i in 0..20 {
let _ = enqueue_post_record(
&db,
&format!("rcc_{i}"),
&format!("Person{i} met Place{i}"),
"default",
);
}
assert_eq!(db.count_pending_ops().unwrap(), 20);
let mut handles = Vec::new();
for _ in 0..4 {
let db_c = Arc::clone(&db);
handles.push(thread::spawn(move || {
let mut total = 0;
while db_c.count_pending_ops().unwrap() > 0 {
total += db_c.apply_pending_ops_once(50).unwrap();
if total >= 20 {
break;
}
}
total
}));
}
let totals: Vec<usize> = handles.into_iter().map(|h| h.join().unwrap()).collect();
assert_eq!(
totals.iter().sum::<usize>(),
20,
"exactly 20 applies across all workers, no double-counting; got {totals:?}"
);
assert_eq!(db.count_pending_ops().unwrap(), 0);
// entities should have 20 distinct Person* rows, 20 distinct Place* rows.
let conn = db.read_conn();
let person_count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM entities WHERE name LIKE 'Person%'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(person_count, 20);
}
#[test]
fn materialize_record_post_invalid_payload_leaves_op_pending() {
// Malformed payload → worker logs warning, leaves op pending for
// retry. Must not advance applied flag (otherwise we'd silently
// lose data on a transient parse failure).
let db = open_test_db();
let bad_payload = serde_json::json!({"not_a_rid": "oops"});
let _ = db
.log_op_pending(
crate::engine::op_types::OP_MATERIALIZE_RECORD_POST,
Some("r_bad"),
&bad_payload,
None,
None,
)
.unwrap();
let n = db.apply_pending_ops_once(10).unwrap();
assert_eq!(n, 0, "malformed op not applied");
assert_eq!(
db.count_pending_ops().unwrap(),
1,
"still pending for retry"
);
}
}