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
// SPDX-License-Identifier: Apache-2.0
//! `WriterActor`, `WriteCommand`, `WriteHandle` — single-writer actor on a
//! dedicated OS thread. See ADR-0003 §"Trait shapes" and §"Operational
//! invariants".
//!
//! ## Why a dedicated OS thread (not a tokio task)
//!
//! `rusqlite::Connection::execute` blocks the current thread. If the actor
//! ran on a tokio worker, every write would block one worker for the
//! duration of the SQL — under burst load the runtime would starve and
//! every other task (HTTP handlers, MCP handlers, the snapshot timer) would
//! stall. The dedicated thread isolates that blocking from the runtime.
//!
//! Inside `run()` we use `mpsc::Receiver::blocking_recv()`; from outside the
//! actor, `WriteHandle::send().await` is async, so callers don't block on
//! sends.
//!
//! ## Reply-before-drain (ADR-0003 §P8-E)
//!
//! `Remember` carries a oneshot reply channel. The reply is sent **after**
//! the SQL transaction commits and `hnsw.add` succeeds, but **before** the
//! `pending_index` row is drained. Callers see "Ok = durable AND searchable"
//! without waiting on cleanup. If the drain itself fails, the row replays
//! on next startup — same end state.
//!
//! ## What's stubbed in commit 1.2
//!
//! `handle_forget`, `handle_consolidate`, `handle_reembed`,
//! `handle_save_snapshot` return `Error::Other("not yet implemented (commit
//! 1.x)")`. They're plumbed through the dispatch so callers can wire the
//! API today; the bodies fill in as the relevant commits land.
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params, params_from_iter};
use solo_core::{Embedder, Embedding, Episode, Error, MemoryId, Result, Tier, VectorIndex};
use tokio::runtime::Handle;
use tokio::sync::{mpsc, oneshot};
use crate::backup::backup_from_connection;
use crate::key_material::KeyMaterial;
/// Default mpsc channel capacity. ADR-0003 §"Channel capacity": 1024 lets a
/// 1000-write consolidation burst land without backpressure-blocking the
/// regular write path.
pub const DEFAULT_CHANNEL_CAPACITY: usize = 1024;
/// Filter + flags for `WriteCommand::Consolidate`.
///
/// Implements `Deserialize` so HTTP / future MCP transports can
/// build it from a JSON request body without extra plumbing.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct ConsolidationScope {
/// If `Some(N)`, only consolidate episodes with `ts_ms` in the last
/// N days. `None` = walk all `tier='hot' AND status='active'` rows.
/// Bounded windows are typical for the daemon's nightly timer;
/// unbounded for a one-shot bulk run.
pub window_days: Option<i64>,
/// When `true`, run the merge + regen passes even if there are
/// zero unclustered episodes to feed to `cluster_episodes`. The
/// usual flow short-circuits on empty candidates because there's
/// no fresh work — but pre-existing clusters can drift across
/// runs and should occasionally coalesce regardless. `--force-merge`
/// on `solo consolidate` (or `force_merge: true` in the HTTP
/// JSON body) opts into that drift catch-up.
#[serde(default)]
pub force_merge: bool,
}
/// What `WriteCommand::Consolidate` returns to the caller. v0.2.0
/// covers the SWS-equivalent clustering pass; abstraction +
/// contradiction counts will populate in later commits.
///
/// `Serialize` so HTTP responses can ship it directly as JSON.
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct ConsolidationReport {
/// Distinct episodes the candidate query returned.
pub episodes_seen: usize,
/// Clusters that survived the size + threshold filter and were
/// persisted to the `clusters` table.
pub clusters_built: usize,
/// Sum of episode_ids across all built clusters (i.e. how many
/// `cluster_episodes` rows were inserted).
pub episodes_clustered: usize,
/// Number of clusters absorbed into a survivor by the centroid-
/// merge pass (`solo_steward::cluster::merge_clusters_by_centroid`).
/// Closes the cross-UTC-day-bucket case where conversations
/// straddling midnight produce two clusters with similar centroids.
/// 0 when no merges were possible. Counts losers, not survivors —
/// `clusters_built` reflects the post-merge count.
pub clusters_merged: usize,
/// Number of freshly-built clusters absorbed into pre-existing
/// DB clusters via
/// `solo_steward::cluster::absorb_into_existing` (cross-run
/// re-consolidation). The absorbed cluster never gets its own
/// `clusters` row; its episodes link under the existing
/// cluster_id, and the existing cluster's centroid + coherence
/// refresh. Counts new-side clusters; `clusters_built` reflects
/// the post-absorb count of brand-new clusters that survived to
/// be inserted.
pub clusters_absorbed: usize,
/// Number of pre-existing clusters absorbed into another
/// pre-existing cluster by the existing-vs-existing merge pass
/// (`solo_steward::cluster::plan_existing_merges`). Closes the
/// long-tail case where two clusters drift toward each other
/// over time via repeated absorbs and should now coalesce.
/// Counts losers, not survivors. Each loser's
/// `cluster_episodes` rows reassign to the survivor's
/// `cluster_id`, then the loser row is DELETEd (cascading via
/// the 0001 + 0002 FKs through `cluster_episodes` (already
/// empty after the UPDATE), `semantic_abstractions`, and
/// `triples`). Survivor's stale abstraction is then regenerated
/// by the same regen pass that handles cross-run absorb
/// modifications.
pub existing_clusters_merged: usize,
/// Number of pre-existing clusters whose stale
/// `semantic_abstractions` + linked `triples` were dropped and
/// regenerated as a follow-on to the cross-run absorb pass.
/// Equal to the count of distinct existing cluster_ids that
/// absorbed at least one new cluster, **as long as** the
/// regenerate-abstract LLM call succeeds; per-cluster failures
/// are logged + skipped (the cluster row + its absorbed episodes
/// stay; the abstraction row stays empty until the next run).
/// 0 when no LLM steward is wired or no absorptions happened.
pub abstractions_regenerated: usize,
/// Number of `semantic_abstractions` rows successfully persisted
/// (Y.3.3). 0 when the writer was spawned without a `Steward` —
/// the prod default until a real `LlmClient` ships.
pub abstractions_built: usize,
/// Number of `triples` rows persisted alongside the abstractions
/// (Y.3.3). Each abstraction can produce 0..N triples; the LLM
/// is asked to extract them but may legitimately return none.
pub triples_built: usize,
/// Reserved for Y.4. Always 0 in this commit.
pub contradictions_found: usize,
}
/// Filter + flags for `WriteCommand::Reembed`.
#[derive(Debug, Clone, Default)]
pub struct ReembedScope {
/// If `Some((name, version))`, only reembed memories whose existing
/// embedding row was produced by this embedder identity. If `None`,
/// every memory whose embedding's `embedder_id` differs from the
/// writer's current `embedder_id` is a candidate.
pub from: Option<(String, String)>,
/// Walk + count only; write nothing.
pub dry_run: bool,
/// After re-embedding each touched memory, DELETE the prior
/// `embeddings` rows for that memory whose `embedder_id` differs
/// from the current. Without this flag, stale rows are retained
/// for forensics or rollback.
pub gc: bool,
}
#[derive(Debug, Clone, Default)]
pub struct ReembedReport {
/// Distinct memory_ids that matched the candidate query.
pub rows_seen: usize,
/// Memories whose new embedding was successfully written.
pub rows_reembedded: usize,
/// Memories that hit an error during embed or insert.
pub rows_failed: usize,
/// Number of stale `embeddings` rows DELETED via `--gc`.
pub rows_gc_deleted: usize,
/// Mirrors the scope flag so callers can format output without
/// retaining the original scope.
pub dry_run: bool,
}
/// All write operations go through this enum. Each variant carries a
/// oneshot reply channel.
#[derive(Debug)]
pub enum WriteCommand {
Remember {
episode: Episode,
embedding: Embedding,
reply: oneshot::Sender<Result<MemoryId>>,
},
Forget {
memory_id: MemoryId,
reason: String,
reply: oneshot::Sender<Result<()>>,
},
Consolidate {
scope: ConsolidationScope,
reply: oneshot::Sender<Result<ConsolidationReport>>,
},
Reembed {
scope: ReembedScope,
reply: oneshot::Sender<Result<ReembedReport>>,
},
SaveSnapshot {
reply: oneshot::Sender<Result<()>>,
},
/// Online encrypted backup of the writer's source database to
/// `dest_path`. The destination is created with PRAGMA key bound to
/// the same raw key the writer holds, so the backup file restores
/// under the same passphrase + salt as the source.
///
/// Available only when the writer was spawned with a `KeyMaterial`
/// (the `spawn_full_with_key_and_optional_steward` variant). Other
/// spawn paths get a clear "not configured" error.
Backup {
dest_path: PathBuf,
reply: oneshot::Sender<Result<()>>,
},
}
/// Cheaply cloneable handle. Pass clones to every task that needs to write.
/// Dropping the *last* clone closes the channel and triggers actor shutdown.
#[derive(Clone, Debug)]
pub struct WriteHandle {
tx: mpsc::Sender<WriteCommand>,
}
impl WriteHandle {
pub async fn remember(
&self,
episode: Episode,
embedding: Embedding,
) -> Result<MemoryId> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(WriteCommand::Remember {
episode,
embedding,
reply: reply_tx,
})
.await
.map_err(|_| Error::storage("writer task gone (channel closed)"))?;
reply_rx
.await
.map_err(|_| Error::storage("writer dropped reply channel"))?
}
pub async fn forget(&self, memory_id: MemoryId, reason: String) -> Result<()> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(WriteCommand::Forget {
memory_id,
reason,
reply: reply_tx,
})
.await
.map_err(|_| Error::storage("writer task gone (channel closed)"))?;
reply_rx
.await
.map_err(|_| Error::storage("writer dropped reply channel"))?
}
pub async fn consolidate(&self, scope: ConsolidationScope) -> Result<ConsolidationReport> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(WriteCommand::Consolidate { scope, reply: reply_tx })
.await
.map_err(|_| Error::storage("writer task gone (channel closed)"))?;
reply_rx
.await
.map_err(|_| Error::storage("writer dropped reply channel"))?
}
pub async fn reembed(&self, scope: ReembedScope) -> Result<ReembedReport> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(WriteCommand::Reembed { scope, reply: reply_tx })
.await
.map_err(|_| Error::storage("writer task gone (channel closed)"))?;
reply_rx
.await
.map_err(|_| Error::storage("writer dropped reply channel"))?
}
/// Run an online encrypted backup of the writer's source database
/// to `dest_path`. Available only when the writer was spawned with
/// a `KeyMaterial` (see [`WriterActor::spawn_full_with_key_and_optional_steward`]).
pub async fn backup(&self, dest_path: PathBuf) -> Result<()> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(WriteCommand::Backup {
dest_path,
reply: reply_tx,
})
.await
.map_err(|_| Error::storage("writer task gone (channel closed)"))?;
reply_rx
.await
.map_err(|_| Error::storage("writer dropped reply channel"))?
}
pub async fn save_snapshot(&self) -> Result<()> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(WriteCommand::SaveSnapshot { reply: reply_tx })
.await
.map_err(|_| Error::storage("writer task gone (channel closed)"))?;
reply_rx
.await
.map_err(|_| Error::storage("writer dropped reply channel"))?
}
}
/// The writer actor.
pub struct WriterActor {
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
rx: mpsc::Receiver<WriteCommand>,
/// Directory for HNSW snapshot save. `None` means `SaveSnapshot` returns
/// an error (used in unit tests that don't exercise the snapshot path).
/// The daemon main (commit 1.5) sets this to the data dir.
snapshot_dir: Option<PathBuf>,
/// Resolved `embedders.embedder_id` for the active embedder. Set once
/// at startup (`solo_storage::startup::run` resolves via
/// `get_or_insert_embedder_id`) and cached for every `INSERT INTO
/// embeddings` row. `None` means the writer was spawned in a test
/// context that doesn't exercise the embeddings-table-write path —
/// `dispatch_remember` falls back to skipping the embeddings INSERT
/// and just writes pending_index.
embedder_id: Option<i64>,
/// The active embedder. Required by `handle_reembed` to regenerate
/// vectors for episodes whose embeddings were produced by a different
/// (older) embedder. The regular `Remember` path does NOT call this —
/// callers pass pre-computed `Embedding` objects in `WriteCommand::
/// Remember`. `None` here means `solo reembed` is not available on
/// this writer (unit tests, daemon paths that opt out).
embedder: Option<Arc<dyn Embedder>>,
/// Handle to the tokio runtime that constructed this actor. Captured
/// at `spawn_full_with_embedder` time so the dedicated writer thread
/// can `block_on` async embedder calls during reembed. `None` matches
/// `embedder == None`.
runtime_handle: Option<Handle>,
/// The Steward (clustering + LLM-driven abstraction). Required by
/// `handle_consolidate`'s abstraction step (Y.3.3); the cheap
/// clustering step (Y.2) always runs and uses
/// `StewardConfig::default()` even when the steward is `None`. So:
/// - `None` → `consolidate` runs cluster persistence only,
/// `abstractions_built` stays 0. This is the default for prod
/// today since no real `LlmClient` ships.
/// - `Some` → after cluster persistence, the actor walks each
/// cluster, calls `steward.abstract_cluster`, persists the
/// `SemanticAbstraction`. Failures per cluster are logged +
/// counted; clusters themselves are already-persisted ground
/// truth and never roll back.
steward: Option<Arc<solo_steward::Steward>>,
/// Raw SQLCipher key used to open the source connection. Required
/// by `handle_backup` so it can encrypt the destination connection
/// with the same key. `None` means the writer was spawned without
/// key material (test paths, the legacy spawn variants); `WriteCommand::
/// Backup` returns a clear "not configured" error in that case.
key: Option<KeyMaterial>,
}
/// What `WriterActor::spawn*` returns. The daemon needs the
/// `std::thread::JoinHandle<()>` so it can wait for the writer's
/// `shutdown()` (`PRAGMA wal_checkpoint(TRUNCATE)`, final HNSW save)
/// to complete after the last `WriteHandle` is dropped. Without this,
/// the OS reaps the writer thread when `main` returns, possibly
/// mid-checkpoint.
///
/// Tests that don't care about clean shutdown timing can simply drop
/// the JoinHandle along with the WriteHandle.
pub struct WriterSpawn {
pub handle: WriteHandle,
pub join: std::thread::JoinHandle<()>,
}
impl WriterSpawn {
/// Drop the WriteHandle (closing the mpsc) and block until the writer
/// thread finishes its `shutdown()` and exits. No timeout — production
/// supervisors decide when to force-kill via SIGKILL.
pub fn shutdown_blocking(self) {
drop(self.handle);
if let Err(panic) = self.join.join() {
tracing::error!(?panic, "solo-writer thread panicked during shutdown");
}
}
}
impl WriterActor {
pub fn spawn(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
) -> WriterSpawn {
Self::spawn_with_capacity(conn, hnsw, DEFAULT_CHANNEL_CAPACITY)
}
pub fn spawn_with_capacity(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
capacity: usize,
) -> WriterSpawn {
Self::spawn_internal(conn, hnsw, capacity, None, None, None, None, None, None)
}
/// Spawn with a snapshot directory wired up. The daemon main calls this
/// path so `WriteCommand::SaveSnapshot` can reach disk.
pub fn spawn_with_snapshot_dir(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
snapshot_dir: PathBuf,
) -> WriterSpawn {
Self::spawn_internal(
conn,
hnsw,
DEFAULT_CHANNEL_CAPACITY,
Some(snapshot_dir),
None,
None,
None,
None,
None,
)
}
/// Spawn with both snapshot dir + cached embedder_id. The daemon
/// main calls this so every `remember` also INSERTs into the
/// `embeddings` table for durability + future `solo reembed`.
pub fn spawn_full(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
snapshot_dir: PathBuf,
embedder_id: i64,
) -> WriterSpawn {
Self::spawn_internal(
conn,
hnsw,
DEFAULT_CHANNEL_CAPACITY,
Some(snapshot_dir),
Some(embedder_id),
None,
None,
None,
None,
)
}
/// Spawn with snapshot dir + embedder_id + the active embedder. Use
/// this from any path that may invoke `WriteCommand::Reembed` — i.e.
/// the `solo reembed` one-shot. Captures `Handle::current()` to bridge
/// the async embedder API onto the writer's blocking thread.
///
/// **Requires a multi-thread tokio runtime.** Panics if called outside
/// any runtime context. On a `current_thread` runtime it would NOT
/// panic, but `handle_reembed`'s `runtime.block_on(embedder.embed(...))`
/// from the writer thread would deadlock — the runtime's only worker
/// would be the test's outer thread, already blocked awaiting the
/// reembed reply. Production callers run inside `#[tokio::main]`
/// (multi-thread by default); tests use `rt_multi(N)` with N >= 1
/// worker independent of the test's calling thread.
pub fn spawn_full_with_embedder(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
snapshot_dir: PathBuf,
embedder_id: i64,
embedder: Arc<dyn Embedder>,
) -> WriterSpawn {
Self::spawn_full_with_embedder_and_optional_steward(
conn,
hnsw,
snapshot_dir,
embedder_id,
embedder,
None,
)
}
/// The full surface: snapshot + embedder + steward. Use this from
/// the `solo consolidate` one-shot path or from a prod daemon
/// that ships with a real `LlmClient` configured. The steward's
/// `Arc<dyn LlmClient>` powers `handle_consolidate`'s abstraction
/// step (Y.3.3); without a steward, consolidate runs the
/// clustering pass only.
pub fn spawn_full_with_embedder_and_optional_steward(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
snapshot_dir: PathBuf,
embedder_id: i64,
embedder: Arc<dyn Embedder>,
steward: Option<Arc<solo_steward::Steward>>,
) -> WriterSpawn {
let handle = Handle::current();
Self::spawn_internal(
conn,
hnsw,
DEFAULT_CHANNEL_CAPACITY,
Some(snapshot_dir),
Some(embedder_id),
Some(embedder),
Some(handle),
steward,
None,
)
}
/// Like [`Self::spawn_full_with_embedder_and_optional_steward`] but
/// also captures `key` so the writer can serve `WriteCommand::Backup`.
/// The daemon (and one-shot paths that want HTTP-side backup) use
/// this variant; pure-test spawn paths can use the no-key variant.
pub fn spawn_full_with_key_and_optional_steward(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
snapshot_dir: PathBuf,
embedder_id: i64,
embedder: Arc<dyn Embedder>,
steward: Option<Arc<solo_steward::Steward>>,
key: KeyMaterial,
) -> WriterSpawn {
let handle = Handle::current();
Self::spawn_internal(
conn,
hnsw,
DEFAULT_CHANNEL_CAPACITY,
Some(snapshot_dir),
Some(embedder_id),
Some(embedder),
Some(handle),
steward,
Some(key),
)
}
fn spawn_internal(
conn: Connection,
hnsw: Arc<dyn VectorIndex + Send + Sync>,
capacity: usize,
snapshot_dir: Option<PathBuf>,
embedder_id: Option<i64>,
embedder: Option<Arc<dyn Embedder>>,
runtime_handle: Option<Handle>,
steward: Option<Arc<solo_steward::Steward>>,
key: Option<KeyMaterial>,
) -> WriterSpawn {
let (tx, rx) = mpsc::channel(capacity);
let actor = Self {
conn,
hnsw,
rx,
snapshot_dir,
embedder_id,
embedder,
runtime_handle,
steward,
key,
};
let join = std::thread::Builder::new()
.name("solo-writer".into())
.spawn(move || actor.run())
.expect("spawn solo-writer thread");
WriterSpawn {
handle: WriteHandle { tx },
join,
}
}
fn run(mut self) {
while let Some(cmd) = self.rx.blocking_recv() {
self.dispatch(cmd);
}
self.shutdown();
}
fn dispatch(&mut self, cmd: WriteCommand) {
match cmd {
WriteCommand::Remember {
episode,
embedding,
reply,
} => self.dispatch_remember(episode, embedding, reply),
WriteCommand::Forget {
memory_id,
reason,
reply,
} => {
let _ = reply.send(self.handle_forget(memory_id, reason));
}
WriteCommand::Consolidate { scope, reply } => {
let _ = reply.send(self.handle_consolidate(scope));
}
WriteCommand::Reembed { scope, reply } => {
let _ = reply.send(self.handle_reembed(scope));
}
WriteCommand::SaveSnapshot { reply } => {
let _ = reply.send(self.handle_save_snapshot());
}
WriteCommand::Backup { dest_path, reply } => {
let _ = reply.send(self.handle_backup(&dest_path));
}
}
}
fn dispatch_remember(
&mut self,
episode: Episode,
embedding: Embedding,
reply: oneshot::Sender<Result<MemoryId>>,
) {
let memory_id = episode.memory_id;
let result = self.handle_remember_durable(episode, embedding);
let durable_ok = result.is_ok();
let _ = reply.send(result);
if durable_ok {
if let Err(e) = self.conn.execute(
"DELETE FROM pending_index WHERE memory_id = ?",
params![memory_id.to_string()],
) {
tracing::warn!(
error = %e,
%memory_id,
"pending_index drain failed; will replay on next startup"
);
}
}
}
fn handle_remember_durable(
&mut self,
episode: Episode,
embedding: Embedding,
) -> Result<MemoryId> {
embedding.validate()?;
let memory_id = episode.memory_id;
let tx = self
.conn
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|e| Error::storage(format!("BEGIN IMMEDIATE for remember: {e}")))?;
let now_ms = chrono::Utc::now().timestamp_millis();
let encoding_ctx = serde_json::to_string(&episode.encoding_context)
.map_err(|e| Error::storage(format!("serialize encoding_context: {e}")))?;
let provenance_json = match &episode.provenance {
Some(p) => Some(
serde_json::to_string(p)
.map_err(|e| Error::storage(format!("serialize provenance: {e}")))?,
),
None => None,
};
let tier_str = match episode.tier {
Tier::Hot => "hot",
Tier::Warm => "warm",
Tier::Cold => "cold",
};
tx.execute(
"INSERT INTO episodes (
memory_id, ts_ms, source_type, source_id, content,
encoding_context_json, provenance_json, confidence,
strength, salience, tier, created_at_ms, updated_at_ms
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
params![
memory_id.to_string(),
episode.ts_ms,
episode.source_type,
episode.source_id,
episode.content,
encoding_ctx,
provenance_json,
episode.confidence.0,
episode.strength,
episode.salience,
tier_str,
now_ms,
now_ms,
],
)
.map_err(|e| Error::storage(format!("INSERT episode: {e}")))?;
let rowid = tx.last_insert_rowid();
// Persist the embedding to the `embeddings` table when
// we have a cached embedder_id. Without this row, `solo
// reembed` (post-v0.1) wouldn't know what vector this episode
// had under the previous model, and HNSW rebuild from SQL
// (also post-v0.1) couldn't repopulate the graph.
//
// Skipped when embedder_id is None — only happens in unit-test
// setups that didn't run the embedder-registry resolution
// step. The pending_index INSERT below still happens, so
// recovery on restart still works for tests that exercise
// the replay path.
if let Some(eid) = self.embedder_id {
let dtype_str = match embedding.dtype {
solo_core::EmbeddingDtype::F32 => "f32",
solo_core::EmbeddingDtype::F16 => "f16",
solo_core::EmbeddingDtype::I8 => "i8",
solo_core::EmbeddingDtype::Binary => "binary",
};
tx.execute(
"INSERT INTO embeddings (
memory_id, embedder_id, dtype, dim, vector, created_at_ms
) VALUES (?, ?, ?, ?, ?, ?)",
params![
memory_id.to_string(),
eid,
dtype_str,
embedding.dim as i64,
&embedding.data[..],
now_ms,
],
)
.map_err(|e| Error::storage(format!("INSERT embeddings: {e}")))?;
}
tx.execute(
"INSERT INTO pending_index (
memory_id, embedding, embedding_dim, enqueued_at
) VALUES (?, ?, ?, ?)",
params![
memory_id.to_string(),
&embedding.data[..],
embedding.dim as i64,
now_ms,
],
)
.map_err(|e| Error::storage(format!("INSERT pending_index: {e}")))?;
tx.commit()
.map_err(|e| Error::storage(format!("COMMIT remember: {e}")))?;
let f32_slice = embedding.as_f32_slice().ok_or_else(|| {
Error::embedder("HNSW expects F32 embeddings; convert dtype upstream")
})?;
self.hnsw.add(rowid, f32_slice)?;
Ok(memory_id)
}
fn handle_forget(&mut self, memory_id: MemoryId, reason: String) -> Result<()> {
// Soft-delete: set status='forgotten' on the episode. Per ADR-0003
// Forget reply timing semantics, the HNSW vector is NOT removed
// from the underlying graph — recall paths exclude
// `status='forgotten'` rows by SQL filter, and the architecture
// preserves silent traces for forensics + consolidation.
//
// BUT: we DO mark the rowid in the in-memory tombstone set
// (`HnswIndex::tombstones`). Without this, `index.len()` keeps
// counting the forgotten vector and `detect_drift` spuriously
// warns at runtime. Post-restart `rebuild_tombstones_from_sql`
// does the same thing; runtime tombstoning matches that behaviour.
//
// The `reason` parameter is logged but not persisted. A future
// schema (memory_revisions or forget_log) can record it; v0.1
// surfaces it through tracing only.
let now_ms = chrono::Utc::now().timestamp_millis();
let id_str = memory_id.to_string();
// Look up the rowid first so we can tombstone the HNSW even on
// the already-forgotten / not-found paths. The query is cheap
// (UNIQUE index on memory_id).
let rowid: Option<i64> = self
.conn
.query_row(
"SELECT rowid FROM episodes WHERE memory_id = ?",
params![&id_str],
|r| r.get::<_, i64>(0),
)
.optional()
.map_err(|e| Error::storage(format!("lookup rowid for forget: {e}")))?;
let Some(rowid) = rowid else {
return Err(Error::not_found(format!(
"memory_id {memory_id} not found in episodes"
)));
};
let updated = self
.conn
.execute(
"UPDATE episodes
SET status = 'forgotten', updated_at_ms = ?
WHERE memory_id = ? AND status <> 'forgotten'",
params![now_ms, &id_str],
)
.map_err(|e| Error::storage(format!("UPDATE episodes for forget: {e}")))?;
// Tombstone the HNSW. Idempotent for already-tombstoned rowids
// (HashSet::insert just no-ops).
if let Err(e) = self.hnsw.remove(rowid) {
tracing::warn!(
error = %e,
%memory_id,
"hnsw.remove during forget failed (non-fatal; SQL filter still hides the row)"
);
}
if updated == 0 {
// Already forgotten → idempotent success.
tracing::debug!(%memory_id, "forget called on already-forgotten episode (idempotent)");
return Ok(());
}
tracing::info!(%memory_id, %reason, "episode soft-deleted (status=forgotten)");
Ok(())
}
fn handle_consolidate(
&mut self,
scope: ConsolidationScope,
) -> Result<ConsolidationReport> {
// v0.2.0 implements only the SWS-equivalent clustering pass.
// Abstraction + contradiction-detection (Y.3+) require the
// LLM client; both fields stay 0 in the report.
//
// Discipline mirrors `handle_reembed`: validate state, build
// candidates from SQL, run the pure-deterministic algorithm,
// persist the output in one transaction. Mid-run failures
// bubble up — there's nothing to "skip and continue" inside
// the storage step (we either commit all clusters or none).
let current_id = self.embedder_id.ok_or_else(|| {
Error::Other(
"consolidate: writer has no current embedder_id (use spawn_full_with_embedder)"
.into(),
)
})?;
// Optional time window. Computed BEFORE the SELECT so a slow
// `now_ms` clock doesn't drift candidate selection.
let now_ms = chrono::Utc::now().timestamp_millis();
let cutoff_ms: Option<i64> = scope.window_days.and_then(|days| {
const MS_PER_DAY: i64 = 86_400_000;
days.checked_mul(MS_PER_DAY).map(|w| now_ms - w)
});
// Build candidates: (Episode, Embedding) tuples for active+hot
// rows whose embedding row matches the current embedder. The
// shape mirrors what `solo_steward::cluster::cluster_episodes`
// expects, but we only populate the fields the algorithm reads
// (memory_id, ts_ms) plus those that round-trip cheaply via SQL
// (content). The defaulted fields (provenance, encoding_context,
// confidence/strength/salience, source_id) are not used by the
// SWS pass; Y.3's REM pass will need them and we'll widen this
// SELECT then.
// Idempotency: exclude memories that are already part of a
// cluster. Without this, re-running consolidate on the same
// data set creates duplicate clusters with different UUID v7
// cluster_ids — same shape but two rows per memory in
// `cluster_episodes`, plus wasted LLM `abstract_cluster` calls
// in Y.3. Trade-off: a memory can never be re-clustered once
// it's been placed; cluster-merging across consolidation
// windows is a v0.3 feature ("re-consolidation"). For v0.2.0,
// first-write wins.
let candidates: Vec<(Episode, Embedding)> = {
let (sql, params): (&str, Vec<rusqlite::types::Value>) = match cutoff_ms {
Some(cutoff) => (
"SELECT e.memory_id, e.ts_ms, e.source_type, e.content,
e.confidence, e.strength, e.salience,
em.dtype, em.dim, em.vector
FROM episodes e
JOIN embeddings em ON em.memory_id = e.memory_id
WHERE em.embedder_id = ?1
AND e.status = 'active'
AND e.tier = 'hot'
AND e.ts_ms >= ?2
AND e.memory_id NOT IN (SELECT memory_id FROM cluster_episodes)
ORDER BY e.ts_ms, e.rowid",
vec![current_id.into(), cutoff.into()],
),
None => (
"SELECT e.memory_id, e.ts_ms, e.source_type, e.content,
e.confidence, e.strength, e.salience,
em.dtype, em.dim, em.vector
FROM episodes e
JOIN embeddings em ON em.memory_id = e.memory_id
WHERE em.embedder_id = ?1
AND e.status = 'active'
AND e.tier = 'hot'
AND e.memory_id NOT IN (SELECT memory_id FROM cluster_episodes)
ORDER BY e.ts_ms, e.rowid",
vec![current_id.into()],
),
};
let mut stmt = self
.conn
.prepare(sql)
.map_err(|e| Error::storage(format!("prepare consolidate select: {e}")))?;
let rows = stmt
.query_map(params_from_iter(¶ms), |r| {
let memory_id: String = r.get(0)?;
let ts_ms: i64 = r.get(1)?;
let source_type: String = r.get(2)?;
let content: String = r.get(3)?;
let confidence_f: f32 = r.get(4)?;
let strength: f32 = r.get(5)?;
let salience: f32 = r.get(6)?;
let dtype_str: String = r.get(7)?;
let dim: i64 = r.get(8)?;
let vector: Vec<u8> = r.get(9)?;
Ok((
memory_id,
ts_ms,
source_type,
content,
confidence_f,
strength,
salience,
dtype_str,
dim,
vector,
))
})
.map_err(|e| Error::storage(format!("query_map consolidate: {e}")))?;
let mut out = Vec::new();
for row in rows {
let (memory_id, ts_ms, source_type, content, conf, strength, salience, dtype_str, dim, vector) =
row.map_err(|e| {
Error::storage(format!("consolidate row decode: {e}"))
})?;
let mid = MemoryId::from_str(&memory_id).map_err(|e| {
Error::storage(format!("parse memory_id `{memory_id}`: {e}"))
})?;
let dtype = match dtype_str.as_str() {
"f32" => solo_core::EmbeddingDtype::F32,
"f16" => solo_core::EmbeddingDtype::F16,
"i8" => solo_core::EmbeddingDtype::I8,
"binary" => solo_core::EmbeddingDtype::Binary,
other => {
return Err(Error::storage(format!(
"unknown embeddings.dtype value `{other}`"
)));
}
};
let embedding = Embedding {
dtype,
dim: dim as usize,
data: vector,
};
let confidence = solo_core::Confidence::new(conf).map_err(|e| {
Error::storage(format!("invalid confidence in episodes row: {e}"))
})?;
let episode = Episode {
memory_id: mid,
ts_ms,
source_type,
source_id: None, // not selected; Y.3 will widen
content,
encoding_context: solo_core::EncodingContext::default(),
provenance: None,
confidence,
strength,
salience,
tier: Tier::Hot,
};
out.push((episode, embedding));
}
out
};
let mut report = ConsolidationReport {
episodes_seen: candidates.len(),
..ConsolidationReport::default()
};
if candidates.is_empty() && !scope.force_merge {
tracing::info!(seen = 0, "consolidate: no candidates");
return Ok(report);
}
if candidates.is_empty() {
tracing::info!(
seen = 0,
"consolidate: no candidates, but force_merge set; falling through to merge + regen"
);
}
// Run the pure-deterministic clustering. Threshold + min-size
// come from `StewardConfig::from_env()` so the operator-facing
// `SOLO_CLUSTER_COSINE_THRESHOLD` takes effect uniformly across:
// - this clustering pass (new candidates),
// - the in-run `merge_clusters_by_centroid` call below,
// - the existing-vs-existing `plan_existing_merges` further
// down — all three read this same `config`.
//
// Daemon and `solo consolidate` validate the env var at startup
// (their `from_env()?` fails fast on a malformed value); reading
// again here is defensive — env vars typically don't change
// mid-process, but the cost is essentially zero and it keeps
// the writer free of a separate `cluster_config` field that
// would touch every `spawn_full_with_*` caller. The
// `count_existing_merge_candidates` doctor path takes the same
// resolved config as a parameter — see `merge_candidates`'s
// "Sync requirement" docstring for the SQL+threshold pair.
let config = solo_steward::StewardConfig::from_env()?;
let mut clusters = solo_steward::cluster::cluster_episodes(&candidates, &config)?;
// Re-consolidation pass: fold clusters whose centroids are
// above threshold into a single survivor. v0.3 MVP scope is
// **just-built only** — the merge sees only the clusters
// produced by this run, not pre-existing ones in the DB.
// That closes the cross-UTC-day-bucket case (conversations
// straddling midnight produce two same-themed clusters in
// the per-day bucketing). Cross-run merge requires fetching
// existing clusters + abstraction-regeneration plumbing —
// separate iteration.
let absorbed =
solo_steward::cluster::merge_clusters_by_centroid(&mut clusters, &config)?;
report.clusters_merged = absorbed;
if absorbed > 0 {
tracing::info!(
absorbed,
survivors = clusters.len(),
"consolidate: centroid merge collapsed cross-bucket clusters"
);
}
report.clusters_built = clusters.len();
report.episodes_clustered = clusters.iter().map(|c| c.episode_ids.len()).sum();
if clusters.is_empty() {
tracing::info!(
seen = report.episodes_seen,
"consolidate: no new clusters formed; falling through to merge + regen"
);
// No early-return: the existing-vs-existing merge pass
// and the regen pass downstream operate on pre-existing
// DB clusters and can fire even when this run produced
// no fresh clusters (e.g. drift catch-up). The absorb
// pass naturally no-ops on an empty `clusters` Vec.
}
// -------- Cross-run absorb pass --------
//
// For each freshly-built cluster, decide whether it should
// fold into a pre-existing DB cluster with a similar
// centroid (within `cluster_cosine_threshold`). If so:
// - the new cluster gets NO row in `clusters`;
// - its `cluster_episodes` rows link under the existing
// cluster's id;
// - the existing cluster's centroid + coherence refresh
// to the post-absorb weighted mean;
// - the existing cluster's `semantic_abstractions` row
// is INTENTIONALLY left in place — abstraction
// regeneration on absorb is a separate iteration
// (needs a triples → cluster FK or status-flag to
// avoid duplicate triple_id rows when re-extracting).
//
// The expected_dim for the existing-cluster fetch comes from
// the first candidate's embedding (every candidate uses the
// current embedder's dim, enforced by the SELECT above).
// expected_dim is normally read off the first candidate's
// embedding; with `force_merge` and an empty candidate set
// there's no candidate to read from, so fall back to the
// current embedder's row in the `embedders` table.
let expected_dim = if let Some(c) = candidates.first() {
c.1.dim
} else {
self.conn
.query_row(
"SELECT dim FROM embedders WHERE embedder_id = ?",
params![current_id],
|r| r.get::<_, i64>(0),
)
.map(|d| d as usize)
.map_err(|e| {
Error::storage(format!(
"consolidate force_merge: lookup dim for embedder_id {current_id}: {e}"
))
})?
};
let existing_summaries =
self.fetch_existing_cluster_summaries(cutoff_ms, expected_dim)?;
let absorb_plan = if existing_summaries.is_empty() {
solo_steward::cluster::AbsorbPlan::default()
} else {
solo_steward::cluster::absorb_into_existing(
&clusters,
&existing_summaries,
&config,
)?
};
report.clusters_absorbed = absorb_plan.absorptions.len();
if !absorb_plan.absorptions.is_empty() {
tracing::info!(
absorbed = absorb_plan.absorptions.len(),
existing_modified = absorb_plan.modified_existing_ids().len(),
"consolidate: cross-run absorb folded clusters into existing"
);
}
// Build a quick map: new_cluster_id → AbsorbedCluster, for
// O(1) lookup during the persistence loop.
let absorbed_by_new: std::collections::HashMap<
MemoryId,
&solo_steward::cluster::AbsorbedCluster,
> = absorb_plan
.absorptions
.iter()
.map(|a| (a.new_cluster_id, a))
.collect();
// `clusters_built` should reflect the count of brand-new
// clusters that actually got persisted (non-absorbed). Update
// now that we know the absorb count.
report.clusters_built = clusters.len() - absorb_plan.absorptions.len();
// Persist all clusters in ONE transaction. If any insert fails,
// rollback — partial state would leave dangling cluster_episodes
// referencing nonexistent clusters.
let txn = self
.conn
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|e| Error::storage(format!("BEGIN consolidate: {e}")))?;
for cluster in &clusters {
// Cross-run absorb: if this freshly-built cluster was
// matched to an existing DB cluster, link its episodes
// under that existing cluster_id and skip the INSERT
// into `clusters`. The existing cluster's centroid +
// coherence refresh happens after this loop in a single
// batched UPDATE step.
if let Some(absorbed) = absorbed_by_new.get(&cluster.cluster_id) {
let target_id_s = absorbed.existing_cluster_id.to_string();
for memid in &cluster.episode_ids {
txn.execute(
"INSERT INTO cluster_episodes (cluster_id, memory_id) VALUES (?, ?)",
params![target_id_s, memid.to_string()],
)
.map_err(|e| {
Error::storage(format!("INSERT cluster_episodes (absorbed): {e}"))
})?;
}
continue;
}
let centroid_dtype: Option<&'static str> = cluster.centroid.as_ref().map(|e| {
match e.dtype {
solo_core::EmbeddingDtype::F32 => "f32",
solo_core::EmbeddingDtype::F16 => "f16",
solo_core::EmbeddingDtype::I8 => "i8",
solo_core::EmbeddingDtype::Binary => "binary",
}
});
let centroid_dim: Option<i64> =
cluster.centroid.as_ref().map(|e| e.dim as i64);
let centroid_blob: Option<&[u8]> =
cluster.centroid.as_ref().map(|e| e.data.as_slice());
txn.execute(
"INSERT INTO clusters (cluster_id, centroid, centroid_dtype, centroid_dim, coherence, created_at_ms)
VALUES (?, ?, ?, ?, ?, ?)",
params![
cluster.cluster_id.to_string(),
centroid_blob,
centroid_dtype,
centroid_dim,
cluster.coherence as f64,
now_ms,
],
)
.map_err(|e| Error::storage(format!("INSERT cluster: {e}")))?;
for memid in &cluster.episode_ids {
txn.execute(
"INSERT INTO cluster_episodes (cluster_id, memory_id) VALUES (?, ?)",
params![cluster.cluster_id.to_string(), memid.to_string()],
)
.map_err(|e| Error::storage(format!("INSERT cluster_episodes: {e}")))?;
}
}
// After the new INSERTs, refresh each modified existing
// cluster's centroid + coherence to the post-absorb values.
// Order: deterministic (sorted by cluster_id) so multi-
// existing absorb runs commit in stable order.
let mut modified_existing: Vec<&solo_steward::cluster::AbsorbedCluster> =
absorb_plan.absorptions.iter().collect();
modified_existing.sort_by(|a, b| a.existing_cluster_id.cmp(&b.existing_cluster_id));
// Only the LAST absorption into a given existing cluster
// carries the final centroid + coherence (the working state
// accumulates step-wise inside `absorb_into_existing`).
// Deduplicate to keep the most recent per existing_cluster_id.
let mut last_per_existing: std::collections::HashMap<
MemoryId,
&solo_steward::cluster::AbsorbedCluster,
> = std::collections::HashMap::new();
for a in &absorb_plan.absorptions {
last_per_existing.insert(a.existing_cluster_id, a);
}
let mut existing_ids_sorted: Vec<MemoryId> =
last_per_existing.keys().copied().collect();
existing_ids_sorted.sort();
for existing_id in existing_ids_sorted {
let absorbed = last_per_existing[&existing_id];
txn.execute(
"UPDATE clusters
SET centroid = ?, centroid_dtype = ?, centroid_dim = ?, coherence = ?
WHERE cluster_id = ?",
params![
absorbed.merged_centroid.data.as_slice(),
"f32",
absorbed.merged_centroid.dim as i64,
absorbed.merged_coherence as f64,
existing_id.to_string(),
],
)
.map_err(|e| Error::storage(format!("UPDATE existing cluster centroid: {e}")))?;
}
txn.commit()
.map_err(|e| Error::storage(format!("COMMIT consolidate: {e}")))?;
// -------- Existing-vs-existing merge pass --------
//
// Independent of cross-run absorb: detects pre-existing
// clusters whose centroids have drifted close enough to
// coalesce. Runs AFTER absorb so post-absorb centroid
// updates feed in (an absorbed-into existing cluster might
// now be similar to another pre-existing cluster).
//
// For each MergeOp in the plan:
// 1. UPDATE cluster_episodes SET cluster_id = survivor
// WHERE cluster_id IN (loser_ids) — episodes move.
// 2. UPDATE clusters SET centroid + coherence on the
// survivor.
// 3. DELETE FROM clusters WHERE cluster_id IN (loser_ids)
// — cascades drop loser's `cluster_episodes` (now
// empty), `semantic_abstractions`, and `triples`.
//
// Survivor's own stale `semantic_abstractions` + `triples`
// are NOT dropped here — the regen pass below handles them
// identically to absorb-modified survivors.
//
// Skipped if no LLM steward is wired (the regen pass that
// would replace the dropped abstractions only runs with a
// steward; running merge without regen would leave
// survivors in a stale-abstraction state with no recovery
// until the next consolidate). Without a steward the
// existing v0.2-era posture (stale-but-readable
// abstractions) is preserved.
let merge_plan: solo_steward::cluster::MergePlan =
if self.steward.is_some() {
let existing_full =
self.fetch_existing_clusters_full(cutoff_ms, expected_dim)?;
if existing_full.len() < 2 {
solo_steward::cluster::MergePlan::default()
} else {
solo_steward::cluster::plan_existing_merges(
&existing_full,
&config,
)?
}
} else {
solo_steward::cluster::MergePlan::default()
};
report.existing_clusters_merged = merge_plan.absorbed();
if !merge_plan.merges.is_empty() {
tracing::info!(
merges = merge_plan.merges.len(),
absorbed = merge_plan.absorbed(),
"consolidate: existing-vs-existing merge applied"
);
let merge_txn = self
.conn
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|e| {
Error::storage(format!("BEGIN existing-merge txn: {e}"))
})?;
for op in &merge_plan.merges {
let survivor_str = op.survivor_id.to_string();
// 1. Move episodes from each loser to the survivor.
for loser_id in &op.loser_ids {
merge_txn
.execute(
"UPDATE cluster_episodes
SET cluster_id = ?1
WHERE cluster_id = ?2",
params![survivor_str, loser_id.to_string()],
)
.map_err(|e| {
Error::storage(format!(
"UPDATE cluster_episodes (existing-merge): {e}"
))
})?;
}
// 2. Refresh survivor's centroid + coherence.
merge_txn
.execute(
"UPDATE clusters
SET centroid = ?, centroid_dtype = ?, centroid_dim = ?, coherence = ?
WHERE cluster_id = ?",
params![
op.merged_centroid.data.as_slice(),
"f32",
op.merged_centroid.dim as i64,
op.merged_coherence as f64,
survivor_str,
],
)
.map_err(|e| {
Error::storage(format!(
"UPDATE clusters (existing-merge): {e}"
))
})?;
// 3. DELETE losers — cascades clean their
// `cluster_episodes` (already empty),
// `semantic_abstractions`, and `triples`.
for loser_id in &op.loser_ids {
merge_txn
.execute(
"DELETE FROM clusters WHERE cluster_id = ?",
params![loser_id.to_string()],
)
.map_err(|e| {
Error::storage(format!(
"DELETE clusters (existing-merge): {e}"
))
})?;
}
}
merge_txn
.commit()
.map_err(|e| Error::storage(format!("COMMIT existing-merge: {e}")))?;
}
// -------- Y.3.3: REM-equivalent abstraction step --------
//
// For each cluster we just persisted, ask the LLM (via the
// Steward) to produce a SemanticAbstraction, then persist it
// to `semantic_abstractions`. Per-cluster failures are
// logged + counted; clusters themselves are already-
// persisted ground truth and never roll back. The cluster
// table stays consistent even if half of the abstraction
// calls fail (network / model glitch).
//
// Skipped entirely when `self.steward` is None, which is
// the prod default until a real LlmClient ships. The cheap
// clustering pass above always runs.
if let (Some(steward), Some(rt)) = (&self.steward, &self.runtime_handle) {
// Build a quick lookup so we can hand each cluster only
// its member episodes.
let by_id: std::collections::HashMap<MemoryId, &Episode> = candidates
.iter()
.map(|(ep, _)| (ep.memory_id, ep))
.collect();
// Y.4.2: accumulate every triple that successfully
// persists during the abstraction loop so the
// contradiction sweep below can pair them against the
// existing triples table.
let mut new_triples: Vec<solo_core::Triple> = Vec::new();
for cluster in &clusters {
// Cross-run absorb: clusters folded into an existing
// DB cluster have NO row in the `clusters` table —
// their cluster_id never landed. Abstracting them
// would produce a `semantic_abstractions` INSERT
// that FK-violates against the missing parent row.
// Skip. The existing cluster's abstraction stays
// as-is (stale w.r.t. the absorbed episodes); this
// is the documented v0.3 tradeoff. Re-abstraction
// on absorb is a planned follow-up requiring
// triples → cluster FK plumbing to avoid duplicate
// facts.
if absorbed_by_new.contains_key(&cluster.cluster_id) {
continue;
}
// Resolve the cluster's episode_ids → owned Episode
// copies (abstract_cluster takes a slice).
let mut member_eps: Vec<Episode> =
Vec::with_capacity(cluster.episode_ids.len());
let mut missing = false;
for memid in &cluster.episode_ids {
match by_id.get(memid) {
Some(ep) => member_eps.push((*ep).clone()),
None => {
tracing::warn!(
cluster_id = %cluster.cluster_id,
missing_memory_id = %memid,
"abstract step: cluster references episode not in candidate set"
);
missing = true;
break;
}
}
}
if missing {
continue;
}
// Drive the async LLM call from the writer thread via
// the captured runtime handle (same pattern as reembed).
let abs_res = rt.block_on(steward.abstract_cluster(cluster, &member_eps));
let abstraction = match abs_res {
Ok(a) => a,
Err(e) => {
tracing::warn!(
cluster_id = %cluster.cluster_id,
error = %e,
"abstract_cluster failed; cluster persisted, abstraction skipped"
);
continue;
}
};
// Persist the abstraction. One transaction per cluster
// — independent atomicity from the cluster batch.
let prov_json = match serde_json::to_string(&abstraction.provenance) {
Ok(s) => s,
Err(e) => {
tracing::warn!(
cluster_id = %cluster.cluster_id,
error = %e,
"serialize provenance for abstraction failed; skipping"
);
continue;
}
};
let abs_txn = match self
.conn
.transaction_with_behavior(TransactionBehavior::Immediate)
{
Ok(t) => t,
Err(e) => {
tracing::warn!(
cluster_id = %cluster.cluster_id,
error = %e,
"BEGIN abstraction txn failed; skipping"
);
continue;
}
};
let abs_insert_res = abs_txn.execute(
"INSERT INTO semantic_abstractions
(abstraction_id, cluster_id, content, provenance_json,
confidence, created_at_ms)
VALUES (?, ?, ?, ?, ?, ?)",
params![
abstraction.abstraction_id.to_string(),
abstraction.cluster_id.to_string(),
abstraction.content,
prov_json,
abstraction.confidence.0,
now_ms,
],
);
// Persist the triples the LLM extracted alongside the
// abstraction. Same atomicity as the abstraction row —
// a partial state where the abstraction lands but its
// triples don't would leak claims with no provenance
// back-link.
let triple_insert_res = abs_insert_res.and_then(|_| {
for triple in &abstraction.triples {
let tprov = serde_json::to_string(&triple.provenance)
.unwrap_or_else(|_| "{}".to_string());
let object_kind_str = match triple.object_kind {
solo_core::TripleObjectKind::Entity => "entity",
solo_core::TripleObjectKind::Literal => "literal",
};
abs_txn.execute(
"INSERT INTO triples
(triple_id, subject_id, predicate, object_id,
object_kind, valid_from_ms, valid_to_ms,
confidence, provenance_json,
created_at_ms, updated_at_ms, cluster_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
params![
triple.triple_id.to_string(),
triple.subject_id,
triple.predicate,
triple.object_id,
object_kind_str,
triple.valid_from_ms,
triple.valid_to_ms,
triple.confidence.0,
tprov,
now_ms,
now_ms,
cluster.cluster_id.to_string(),
],
)?;
}
Ok(())
});
match triple_insert_res.and_then(|_| abs_txn.commit()) {
Ok(()) => {
report.abstractions_built += 1;
report.triples_built += abstraction.triples.len();
// Track these for the contradiction sweep
// below; only the committed-cluster triples
// join the candidate set.
new_triples.extend(abstraction.triples);
}
Err(e) => {
tracing::warn!(
cluster_id = %cluster.cluster_id,
error = %e,
"INSERT semantic_abstractions / triples failed; skipping"
);
}
}
}
// -------- Re-abstraction regen pass --------
//
// For each pre-existing cluster whose stale abstraction
// was invalidated by this run's absorb pass OR this
// run's existing-vs-existing merge pass, drop the
// existing `semantic_abstractions` row + its linked
// `triples` (FK ON DELETE CASCADE on cluster_id makes
// this a single DELETE chain — see migration 0002),
// then regenerate by feeding the cluster's full episode
// set back through `steward.abstract_cluster`.
//
// Failure modes:
// - LLM call fails → log + skip; the cluster ends up
// without an abstraction until a subsequent run
// retries.
// - DELETE/INSERT fails → log + skip with the txn
// rolled back; abstraction stays whatever the
// pre-DELETE state was (intact stale row).
//
// Sources:
// - Absorb-modified: from `absorb_plan.absorptions`;
// centroid + coherence come from the last
// absorption per existing_cluster_id.
// - Merge-modified: from `merge_plan.merges`;
// centroid + coherence come from `merged_centroid`
// + `merged_coherence` on the MergeOp.
// - When a cluster appears in BOTH (absorb fired,
// then merge consumed the result), the **merge**
// entry wins because it captures the FINAL post-
// merge state. Absorb's centroid is intermediate.
//
// One transaction per cluster — independent atomicity.
let last_per_existing_for_regen: std::collections::HashMap<
MemoryId,
&solo_steward::cluster::AbsorbedCluster,
> = absorb_plan
.absorptions
.iter()
.map(|a| (a.existing_cluster_id, a))
.collect();
let merge_lookup: std::collections::HashMap<
MemoryId,
&solo_steward::cluster::MergeOp,
> = merge_plan
.merges
.iter()
.map(|m| (m.survivor_id, m))
.collect();
// Build the union of regen targets in deterministic
// (sorted) order so the LLM sees the same prompt
// sequence across re-runs.
let mut regen_targets: std::collections::BTreeSet<MemoryId> =
std::collections::BTreeSet::new();
regen_targets.extend(absorb_plan.modified_existing_ids());
regen_targets.extend(merge_lookup.keys().copied());
for existing_id in regen_targets {
// Pick the source with the post-everything state.
// Merge wins over absorb when both apply.
let (regen_centroid, regen_coherence) =
if let Some(op) = merge_lookup.get(&existing_id) {
(op.merged_centroid.clone(), op.merged_coherence)
} else if let Some(absorbed) =
last_per_existing_for_regen.get(&existing_id)
{
(absorbed.merged_centroid.clone(), absorbed.merged_coherence)
} else {
continue; // shouldn't happen — target sourced from one of the two maps.
};
let episodes = match self.fetch_episodes_for_cluster(&existing_id) {
Ok(eps) => eps,
Err(e) => {
tracing::warn!(
cluster_id = %existing_id,
error = %e,
"regen: fetch_episodes_for_cluster failed; skipping"
);
continue;
}
};
if episodes.is_empty() {
continue;
}
let cluster_shell = solo_core::Cluster {
cluster_id: existing_id,
episode_ids: episodes.iter().map(|e| e.memory_id).collect(),
centroid: Some(regen_centroid),
coherence: regen_coherence,
};
let abs_res = rt.block_on(steward.abstract_cluster(&cluster_shell, &episodes));
let new_abstraction = match abs_res {
Ok(a) => a,
Err(e) => {
tracing::warn!(
cluster_id = %existing_id,
error = %e,
"regen: abstract_cluster failed; abstraction stays absent until next run"
);
continue;
}
};
let prov_json = match serde_json::to_string(&new_abstraction.provenance) {
Ok(s) => s,
Err(e) => {
tracing::warn!(
cluster_id = %existing_id,
error = %e,
"regen: serialize provenance failed; skipping"
);
continue;
}
};
let regen_txn = match self
.conn
.transaction_with_behavior(TransactionBehavior::Immediate)
{
Ok(t) => t,
Err(e) => {
tracing::warn!(
cluster_id = %existing_id,
error = %e,
"regen: BEGIN txn failed; skipping"
);
continue;
}
};
let regen_writes: rusqlite::Result<()> = (|| {
regen_txn.execute(
"DELETE FROM semantic_abstractions WHERE cluster_id = ?",
params![existing_id.to_string()],
)?;
regen_txn.execute(
"DELETE FROM triples WHERE cluster_id = ?",
params![existing_id.to_string()],
)?;
regen_txn.execute(
"INSERT INTO semantic_abstractions
(abstraction_id, cluster_id, content, provenance_json,
confidence, created_at_ms)
VALUES (?, ?, ?, ?, ?, ?)",
params![
new_abstraction.abstraction_id.to_string(),
existing_id.to_string(),
new_abstraction.content,
prov_json,
new_abstraction.confidence.0,
now_ms,
],
)?;
for triple in &new_abstraction.triples {
let tprov = serde_json::to_string(&triple.provenance)
.unwrap_or_else(|_| "{}".to_string());
let object_kind_str = match triple.object_kind {
solo_core::TripleObjectKind::Entity => "entity",
solo_core::TripleObjectKind::Literal => "literal",
};
regen_txn.execute(
"INSERT INTO triples
(triple_id, subject_id, predicate, object_id,
object_kind, valid_from_ms, valid_to_ms,
confidence, provenance_json,
created_at_ms, updated_at_ms, cluster_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
params![
triple.triple_id.to_string(),
triple.subject_id,
triple.predicate,
triple.object_id,
object_kind_str,
triple.valid_from_ms,
triple.valid_to_ms,
triple.confidence.0,
tprov,
now_ms,
now_ms,
existing_id.to_string(),
],
)?;
}
Ok(())
})();
match regen_writes.and_then(|_| regen_txn.commit()) {
Ok(()) => {
report.abstractions_regenerated += 1;
report.triples_built += new_abstraction.triples.len();
new_triples.extend(new_abstraction.triples);
}
Err(e) => {
tracing::warn!(
cluster_id = %existing_id,
error = %e,
"regen: DELETE/INSERT failed; skipping"
);
}
}
}
// -------- Y.4.2: contradiction sweep --------
//
// For every triple we just persisted, look for existing
// triples in SQL that share `(subject_id, predicate)` —
// the cheap pre-filter that mirrors
// `contradiction::is_contradiction_candidate`'s subject
// + predicate short-circuits. Plus pair every
// newly-built triple against the others in this run
// (cross-cluster contradictions surfaced in the same
// consolidation cycle).
//
// Errors (LLM, SQL fetch, INSERT) log + skip — clusters
// / abstractions / triples are already-persisted ground
// truth and never roll back. UNIQUE(a, b, kind) +
// INSERT OR IGNORE gives idempotent re-runs.
//
// v0.5.0 sub-step 2B: gated on `steward.has_llm()`. A
// stub-only steward can build clusters and synthetic
// abstractions, but the LLM judge can't faithfully
// arbitrate two real triples it has never seen —
// running the sweep against canned responses would
// produce nonsense contradictions. We skip + log
// instead so operators see why no contradictions get
// flagged. The cluster + abstraction work above
// already ran; this is a clean early-return on the
// sweep alone.
if !new_triples.is_empty() && !steward.has_llm() {
tracing::warn!(
reason = "no LLM client configured",
"contradiction sweep skipped; set \
ANTHROPIC_API_KEY or OPENAI_API_KEY (or use \
--ollama-model) to enable"
);
} else if !new_triples.is_empty() {
let new_ids: std::collections::HashSet<MemoryId> = new_triples
.iter()
.map(|t| t.triple_id)
.collect();
let mut detected: Vec<solo_core::Contradiction> = Vec::new();
// Observability counters — emitted as a single
// INFO-level structured log at the end of the
// sweep. `candidate_pair_count` is every pair we
// tried; `judge_call_count` is the subset that
// survived the cheap rule filter and actually
// reached the LLM; `judge_rejection_count` is
// the count that ended up classified as NOT a
// contradiction (rule-filtered OR judge-said-no).
// Closes the observability gap from the v0.4.1
// thesis test, where `RUST_LOG=debug` couldn't
// tell whether the judge ran at all.
let mut candidate_pair_count: usize = 0;
let mut judge_call_count: usize = 0;
let mut judge_rejection_count: usize = 0;
// (1) New × Existing — for each new triple, fetch
// existing-DB candidates that match (subject, predicate)
// but aren't part of this run's batch.
for new in &new_triples {
let existing = match self.fetch_triples_for_pair(
&new.subject_id,
&new.predicate,
&new_ids,
) {
Ok(v) => v,
Err(e) => {
tracing::warn!(
error = %e,
subject = %new.subject_id,
predicate = %new.predicate,
"fetch existing triples failed; skipping"
);
continue;
}
};
for old in &existing {
candidate_pair_count += 1;
// Rule-filter the pair locally too so we
// can count judge-vs-filter separately.
// `detect_contradiction` redundantly runs
// the same check internally; cost is a
// handful of integer compares.
if solo_steward::contradiction::is_contradiction_candidate(new, old) {
judge_call_count += 1;
}
match rt.block_on(steward.detect_contradiction(new, old)) {
Ok(Some(c)) => detected.push(c),
Ok(None) => {
judge_rejection_count += 1;
}
Err(e) => {
tracing::warn!(
error = %e,
"detect_contradiction failed; skipping pair"
);
}
}
}
}
// (2) New × New — pairs within this run.
for i in 0..new_triples.len() {
for j in (i + 1)..new_triples.len() {
let a = &new_triples[i];
let b = &new_triples[j];
candidate_pair_count += 1;
if solo_steward::contradiction::is_contradiction_candidate(a, b) {
judge_call_count += 1;
}
match rt.block_on(steward.detect_contradiction(a, b)) {
Ok(Some(c)) => detected.push(c),
Ok(None) => {
judge_rejection_count += 1;
}
Err(e) => {
tracing::warn!(
error = %e,
"detect_contradiction failed; skipping pair"
);
}
}
}
}
tracing::info!(
candidate_pair_count,
judge_call_count,
judge_rejection_count,
prompt_version_hash =
solo_steward::contradiction::prompt_version_hash(),
"contradiction sweep completed"
);
// Persist. UNIQUE(a, b, kind) requires consistent
// (a, b) ordering for dedup across re-runs;
// normalize so a_memory_id <= b_memory_id.
for c in detected {
let (a_str, b_str) = if c.a <= c.b {
(c.a.to_string(), c.b.to_string())
} else {
(c.b.to_string(), c.a.to_string())
};
let kind_str = match c.kind {
solo_core::ContradictionKind::OverlappingSingleValuedPredicate => {
"overlapping_single_valued_predicate"
}
solo_core::ContradictionKind::DirectNegation => "direct_negation",
solo_core::ContradictionKind::NumericInconsistency => {
"numeric_inconsistency"
}
solo_core::ContradictionKind::Other => "other",
};
match self.conn.execute(
"INSERT OR IGNORE INTO contradictions
(a_memory_id, b_memory_id, kind, explanation, detected_at_ms)
VALUES (?, ?, ?, ?, ?)",
params![a_str, b_str, kind_str, c.explanation, now_ms],
) {
Ok(rows_inserted) => {
// Only count rows that actually landed —
// duplicate (UNIQUE) rejects → 0.
report.contradictions_found += rows_inserted;
}
Err(e) => {
tracing::warn!(
error = %e,
a = %a_str,
b = %b_str,
"INSERT contradiction failed; skipping"
);
}
}
}
}
}
tracing::info!(
seen = report.episodes_seen,
clusters = report.clusters_built,
episodes_clustered = report.episodes_clustered,
abstractions = report.abstractions_built,
triples = report.triples_built,
contradictions = report.contradictions_found,
"consolidate complete"
);
Ok(report)
}
/// Fetch every triple that shares `(subject_id, predicate)` with
/// the new triple, excluding the new run's batch (passed via
/// `exclude`). Used by `handle_consolidate`'s contradiction sweep
/// to narrow LLM-judge candidates to plausible pairs only.
///
/// Returns reassembled `Triple` structs. Provenance is parsed
/// from the row's `provenance_json` column; on parse failure we
/// substitute a placeholder `Provenance` (the triple is still
/// usable for contradiction detection — the rule filter doesn't
/// touch provenance, and the LLM judge prompt doesn't include
/// it either).
fn fetch_triples_for_pair(
&self,
subject_id: &str,
predicate: &str,
exclude: &std::collections::HashSet<MemoryId>,
) -> Result<Vec<solo_core::Triple>> {
let mut stmt = self
.conn
.prepare(
"SELECT triple_id, object_id, object_kind, valid_from_ms, valid_to_ms,
confidence, provenance_json
FROM triples
WHERE subject_id = ?1 AND predicate = ?2
AND status = 'active'",
)
.map_err(|e| Error::storage(format!("prepare fetch_triples_for_pair: {e}")))?;
let rows = stmt
.query_map(params![subject_id, predicate], |r| {
Ok((
r.get::<_, String>(0)?,
r.get::<_, String>(1)?,
r.get::<_, String>(2)?,
r.get::<_, i64>(3)?,
r.get::<_, Option<i64>>(4)?,
r.get::<_, f32>(5)?,
r.get::<_, String>(6)?,
))
})
.map_err(|e| Error::storage(format!("query_map triples: {e}")))?;
let mut out = Vec::new();
for row in rows {
let (triple_id_s, object_id, object_kind_s, valid_from_ms, valid_to_ms, conf, prov_s) =
row.map_err(|e| Error::storage(format!("triples row decode: {e}")))?;
let triple_id = MemoryId::from_str(&triple_id_s).map_err(|e| {
Error::storage(format!("parse triple_id `{triple_id_s}`: {e}"))
})?;
if exclude.contains(&triple_id) {
continue;
}
let object_kind = match object_kind_s.as_str() {
"entity" => solo_core::TripleObjectKind::Entity,
"literal" => solo_core::TripleObjectKind::Literal,
other => {
return Err(Error::storage(format!(
"unknown object_kind value `{other}` in triples row"
)));
}
};
let confidence = solo_core::Confidence::new(conf).map_err(|e| {
Error::storage(format!("invalid confidence in triples row: {e}"))
})?;
let provenance: solo_core::Provenance = serde_json::from_str(&prov_s)
.unwrap_or_else(|_| solo_core::Provenance {
derived_from: vec![],
derivation: "(unparseable)".into(),
by: "(unknown)".into(),
at_ms: 0,
});
out.push(solo_core::Triple {
triple_id,
subject_id: subject_id.to_string(),
predicate: predicate.to_string(),
object_id,
object_kind,
valid_from_ms,
valid_to_ms,
confidence,
provenance,
});
}
Ok(out)
}
/// Fetch compact summaries of every existing cluster within the
/// consolidate window. Used by `handle_consolidate`'s cross-run
/// absorb pass to decide which freshly-built clusters fold into
/// pre-existing DB clusters with similar centroids.
///
/// Filters:
///
/// - `cutoff_ms`: when `Some(ms)`, restrict to clusters with
/// `created_at_ms >= ms` (matches the candidate-episode
/// window). When `None`, all clusters in the table.
/// - Centroid must be present (non-null) and dim must equal
/// `expected_dim` — clusters built under a different
/// embedder are skipped (their centroids live in a
/// different vector space and absorb-cosine would be
/// meaningless).
///
/// Returns one row per surviving cluster with its centroid +
/// coherence + episode count. The episode_count is computed via
/// a correlated `COUNT(*)` against `cluster_episodes` — cheap
/// thanks to `idx_cluster_episodes_memory` (well, technically
/// thanks to the `(cluster_id, memory_id)` PRIMARY KEY ordering).
fn fetch_existing_cluster_summaries(
&self,
cutoff_ms: Option<i64>,
expected_dim: usize,
) -> Result<Vec<solo_steward::cluster::ExistingClusterSummary>> {
let (sql, params): (&str, Vec<rusqlite::types::Value>) = match cutoff_ms {
Some(cutoff) => (
"SELECT c.cluster_id, c.centroid, c.centroid_dtype, c.centroid_dim,
c.coherence,
(SELECT COUNT(*) FROM cluster_episodes ce
WHERE ce.cluster_id = c.cluster_id) AS episode_count
FROM clusters c
WHERE c.centroid IS NOT NULL
AND c.centroid_dtype = 'f32'
AND c.centroid_dim = ?1
AND c.created_at_ms >= ?2
ORDER BY c.cluster_id",
vec![(expected_dim as i64).into(), cutoff.into()],
),
None => (
"SELECT c.cluster_id, c.centroid, c.centroid_dtype, c.centroid_dim,
c.coherence,
(SELECT COUNT(*) FROM cluster_episodes ce
WHERE ce.cluster_id = c.cluster_id) AS episode_count
FROM clusters c
WHERE c.centroid IS NOT NULL
AND c.centroid_dtype = 'f32'
AND c.centroid_dim = ?1
ORDER BY c.cluster_id",
vec![(expected_dim as i64).into()],
),
};
let mut stmt = self
.conn
.prepare(sql)
.map_err(|e| Error::storage(format!("prepare existing-cluster summaries: {e}")))?;
let rows = stmt
.query_map(params_from_iter(¶ms), |r| {
Ok((
r.get::<_, String>(0)?, // cluster_id
r.get::<_, Vec<u8>>(1)?, // centroid blob
r.get::<_, String>(2)?, // centroid_dtype
r.get::<_, i64>(3)?, // centroid_dim
r.get::<_, f32>(4)?, // coherence
r.get::<_, i64>(5)?, // episode_count
))
})
.map_err(|e| Error::storage(format!("query_map existing clusters: {e}")))?;
let mut out: Vec<solo_steward::cluster::ExistingClusterSummary> = Vec::new();
for row in rows {
let (cid_s, centroid_bytes, dtype_s, dim_i, coherence, count_i) =
row.map_err(|e| Error::storage(format!("cluster row decode: {e}")))?;
// Defensive: SQL's WHERE already filtered to f32 +
// expected_dim; trust but verify the row contents.
if dtype_s != "f32" || (dim_i as usize) != expected_dim {
continue;
}
let cluster_id = match MemoryId::from_str(&cid_s) {
Ok(id) => id,
Err(e) => {
tracing::warn!(
cluster_id = %cid_s,
error = %e,
"skipping cluster with unparseable cluster_id"
);
continue;
}
};
let centroid = solo_core::Embedding {
dtype: solo_core::EmbeddingDtype::F32,
dim: dim_i as usize,
data: centroid_bytes,
};
// count_i is the exact integer from COUNT(*); negatives
// can't happen, but `as usize` saturates safely on the
// off-chance of an i64 overflow case.
let episode_count = count_i.max(0) as usize;
// Skip clusters with zero episodes (orphaned rows
// shouldn't exist thanks to ON DELETE CASCADE, but
// guarding here keeps absorb math defensible).
if episode_count == 0 {
continue;
}
out.push(solo_steward::cluster::ExistingClusterSummary {
cluster_id,
centroid,
coherence,
episode_count,
});
}
Ok(out)
}
/// Load every existing cluster as a full [`Cluster`] struct
/// (centroid + coherence + complete `episode_ids` Vec). Used by
/// the existing-vs-existing merge pass to feed
/// `solo_steward::cluster::plan_existing_merges`.
///
/// One SELECT joins `clusters` with `cluster_episodes`; rows are
/// aggregated by `cluster_id`. Filters mirror
/// `fetch_existing_cluster_summaries` (centroid present, dtype +
/// dim match the current embedder, optional `created_at_ms` cutoff).
/// Clusters with zero linked episodes are skipped — those are
/// orphan rows that shouldn't exist post-CASCADE invariants.
fn fetch_existing_clusters_full(
&self,
cutoff_ms: Option<i64>,
expected_dim: usize,
) -> Result<Vec<solo_core::Cluster>> {
let (sql, params): (&str, Vec<rusqlite::types::Value>) = match cutoff_ms {
Some(cutoff) => (
"SELECT c.cluster_id, c.centroid, c.centroid_dtype, c.centroid_dim,
c.coherence, ce.memory_id
FROM clusters c
JOIN cluster_episodes ce ON ce.cluster_id = c.cluster_id
WHERE c.centroid IS NOT NULL
AND c.centroid_dtype = 'f32'
AND c.centroid_dim = ?1
AND c.created_at_ms >= ?2
ORDER BY c.cluster_id, ce.memory_id",
vec![(expected_dim as i64).into(), cutoff.into()],
),
None => (
"SELECT c.cluster_id, c.centroid, c.centroid_dtype, c.centroid_dim,
c.coherence, ce.memory_id
FROM clusters c
JOIN cluster_episodes ce ON ce.cluster_id = c.cluster_id
WHERE c.centroid IS NOT NULL
AND c.centroid_dtype = 'f32'
AND c.centroid_dim = ?1
ORDER BY c.cluster_id, ce.memory_id",
vec![(expected_dim as i64).into()],
),
};
let mut stmt = self
.conn
.prepare(sql)
.map_err(|e| Error::storage(format!("prepare existing clusters full: {e}")))?;
let rows = stmt
.query_map(params_from_iter(¶ms), |r| {
Ok((
r.get::<_, String>(0)?, // cluster_id
r.get::<_, Vec<u8>>(1)?, // centroid bytes
r.get::<_, String>(2)?, // dtype
r.get::<_, i64>(3)?, // dim
r.get::<_, f32>(4)?, // coherence
r.get::<_, String>(5)?, // memory_id
))
})
.map_err(|e| Error::storage(format!("query_map clusters full: {e}")))?;
// Aggregate. Rows are ORDER BY cluster_id so we can build
// the output as a single pass.
let mut out: Vec<solo_core::Cluster> = Vec::new();
for row in rows {
let (cid_s, centroid_bytes, dtype_s, dim_i, coherence, memid_s) =
row.map_err(|e| Error::storage(format!("clusters full row decode: {e}")))?;
if dtype_s != "f32" || (dim_i as usize) != expected_dim {
continue;
}
let cluster_id = match MemoryId::from_str(&cid_s) {
Ok(id) => id,
Err(e) => {
tracing::warn!(
cluster_id = %cid_s,
error = %e,
"skipping cluster with unparseable cluster_id"
);
continue;
}
};
let memory_id = match MemoryId::from_str(&memid_s) {
Ok(id) => id,
Err(e) => {
tracing::warn!(
memory_id = %memid_s,
error = %e,
"skipping cluster_episodes row with unparseable memory_id"
);
continue;
}
};
// Append to the in-progress cluster (last entry in
// `out`) when cluster_id matches; otherwise start a new
// entry.
if out.last().map(|c| c.cluster_id) == Some(cluster_id) {
out.last_mut().unwrap().episode_ids.push(memory_id);
} else {
let centroid = solo_core::Embedding {
dtype: solo_core::EmbeddingDtype::F32,
dim: dim_i as usize,
data: centroid_bytes,
};
out.push(solo_core::Cluster {
cluster_id,
episode_ids: vec![memory_id],
centroid: Some(centroid),
coherence,
});
}
}
// Drop empty clusters (defensive — shouldn't happen given
// the JOIN requires at least one cluster_episodes row).
out.retain(|c| !c.episode_ids.is_empty());
Ok(out)
}
/// Load full [`Episode`] structs for every memory_id linked to
/// `cluster_id` via `cluster_episodes`. Used by the absorb→regen
/// path to feed `steward.abstract_cluster` for a cluster whose
/// stale abstraction needs replacing.
///
/// Filters: `episodes.status = 'active'` (forgotten episodes
/// can't drive a fresh abstraction; they get skipped). The
/// SELECT mirrors `handle_consolidate`'s candidate fetch but
/// without the `embedder_id` filter — the regen pass operates
/// on the cluster's full historical episode set, regardless of
/// which embedder produced their vectors.
///
/// Returns episodes ordered by `(ts_ms, rowid)` for
/// deterministic prompts.
fn fetch_episodes_for_cluster(
&self,
cluster_id: &MemoryId,
) -> Result<Vec<Episode>> {
let mut stmt = self
.conn
.prepare(
"SELECT e.memory_id, e.ts_ms, e.source_type, e.source_id,
e.content, e.encoding_context_json, e.provenance_json,
e.confidence, e.strength, e.salience, e.tier
FROM episodes e
JOIN cluster_episodes ce ON ce.memory_id = e.memory_id
WHERE ce.cluster_id = ?1
AND e.status = 'active'
ORDER BY e.ts_ms, e.rowid",
)
.map_err(|e| {
Error::storage(format!("prepare fetch_episodes_for_cluster: {e}"))
})?;
let rows = stmt
.query_map(params![cluster_id.to_string()], |r| {
Ok((
r.get::<_, String>(0)?, // memory_id
r.get::<_, i64>(1)?, // ts_ms
r.get::<_, String>(2)?, // source_type
r.get::<_, Option<String>>(3)?, // source_id
r.get::<_, String>(4)?, // content
r.get::<_, String>(5)?, // encoding_context_json
r.get::<_, Option<String>>(6)?, // provenance_json
r.get::<_, f32>(7)?, // confidence
r.get::<_, f32>(8)?, // strength
r.get::<_, f32>(9)?, // salience
r.get::<_, String>(10)?, // tier
))
})
.map_err(|e| Error::storage(format!("query_map cluster episodes: {e}")))?;
let mut out: Vec<Episode> = Vec::new();
for row in rows {
let (
mid_s,
ts_ms,
source_type,
source_id,
content,
ctx_json,
prov_json,
conf,
strength,
salience,
tier_s,
) = row.map_err(|e| Error::storage(format!("episode row decode: {e}")))?;
let mid = MemoryId::from_str(&mid_s)
.map_err(|e| Error::storage(format!("parse memory_id `{mid_s}`: {e}")))?;
let confidence = solo_core::Confidence::new(conf).map_err(|e| {
Error::storage(format!("invalid confidence in episode row: {e}"))
})?;
let encoding_context: solo_core::EncodingContext =
serde_json::from_str(&ctx_json).unwrap_or_default();
let provenance: Option<solo_core::Provenance> = prov_json
.as_deref()
.and_then(|s| serde_json::from_str(s).ok());
let tier = match tier_s.as_str() {
"hot" => Tier::Hot,
"warm" => Tier::Warm,
"cold" => Tier::Cold,
other => {
return Err(Error::storage(format!(
"unknown tier value `{other}` in episodes row"
)));
}
};
out.push(Episode {
memory_id: mid,
ts_ms,
source_type,
source_id,
content,
encoding_context,
provenance,
confidence,
strength,
salience,
tier,
});
}
Ok(out)
}
fn handle_reembed(&mut self, scope: ReembedScope) -> Result<ReembedReport> {
// Reembed needs the writer to have been spawned with the active
// embedder + a runtime handle (see `spawn_full_with_embedder`).
// The plain `spawn_full` constructor leaves these `None`, which
// is the correct posture for daemon paths that don't dispatch
// Reembed. A clean error here beats a panic.
let current_id = self.embedder_id.ok_or_else(|| {
Error::Other(
"reembed: writer has no current embedder_id (use spawn_full_with_embedder)"
.into(),
)
})?;
let embedder = self.embedder.clone().ok_or_else(|| {
Error::Other(
"reembed: writer has no embedder (use spawn_full_with_embedder)".into(),
)
})?;
let runtime = self.runtime_handle.clone().ok_or_else(|| {
Error::Other(
"reembed: writer has no runtime handle (use spawn_full_with_embedder)"
.into(),
)
})?;
// Optional `from` filter → resolve to embedder_id once. Refuse if
// the user asked to migrate from "current to current" (nothing
// would happen) or if the from-embedder isn't registered.
let from_id: Option<i64> = match &scope.from {
None => None,
Some((name, version)) => {
let id: Option<i64> = self
.conn
.query_row(
"SELECT embedder_id FROM embedders WHERE name = ? AND version = ?",
params![name, version],
|r| r.get::<_, i64>(0),
)
.optional()
.map_err(|e| Error::storage(format!("lookup from embedder: {e}")))?;
match id {
Some(id) if id == current_id => {
return Err(Error::Other(format!(
"reembed: from-embedder ({name}, {version}) IS the current \
embedder; nothing to do"
)));
}
Some(id) => Some(id),
None => {
return Err(Error::not_found(format!(
"reembed: from-embedder ({name}, {version}) not registered \
in `embedders` table"
)));
}
}
}
};
// Build the candidate set. DISTINCT — a memory may have multiple
// stale rows (if the user has rolled through more than one prior
// embedder); we only want to embed each content once.
let candidates: Vec<(String, String)> = {
let (sql, bound_id): (&str, i64) = match from_id {
None => (
"SELECT DISTINCT e.memory_id, e.content
FROM episodes e
JOIN embeddings em ON em.memory_id = e.memory_id
WHERE em.embedder_id != ?1
AND e.status = 'active'
ORDER BY e.rowid",
current_id,
),
Some(fid) => (
"SELECT DISTINCT e.memory_id, e.content
FROM episodes e
JOIN embeddings em ON em.memory_id = e.memory_id
WHERE em.embedder_id = ?1
AND e.status = 'active'
ORDER BY e.rowid",
fid,
),
};
let mut stmt = self
.conn
.prepare(sql)
.map_err(|e| Error::storage(format!("prepare reembed select: {e}")))?;
let rows = stmt
.query_map(params![bound_id], |r| {
Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))
})
.map_err(|e| Error::storage(format!("query_map reembed: {e}")))?;
let mut out = Vec::new();
for row in rows {
out.push(
row.map_err(|e| Error::storage(format!("reembed row decode: {e}")))?,
);
}
out
};
let mut report = ReembedReport {
rows_seen: candidates.len(),
rows_reembedded: 0,
rows_failed: 0,
rows_gc_deleted: 0,
dry_run: scope.dry_run,
};
if scope.dry_run {
tracing::info!(
seen = report.rows_seen,
"reembed --dry-run: would re-embed N memories"
);
return Ok(report);
}
// Cache the dtype string for the current embedder once.
let dtype_str = match embedder.dtype() {
solo_core::EmbeddingDtype::F32 => "f32",
solo_core::EmbeddingDtype::F16 => "f16",
solo_core::EmbeddingDtype::I8 => "i8",
solo_core::EmbeddingDtype::Binary => "binary",
};
let now_ms = chrono::Utc::now().timestamp_millis();
// Per-memory loop. Embed (async, off the writer thread via the
// captured runtime handle), then atomically apply the SQL changes
// for that memory. A failure on one memory does NOT abort the
// run — partial progress is fine because the next reembed pass
// picks up wherever we left off (the SELECT re-evaluates the
// candidate set at start). The per-memory transaction means a
// mid-run crash cannot leave a memory with two `current`
// embedding rows or with the new row but missing the GC.
for (memory_id, content) in candidates {
let embedding_res = runtime.block_on(embedder.embed(&content));
let new_embedding = match embedding_res {
Ok(emb) => emb,
Err(e) => {
tracing::warn!(%memory_id, error = %e, "reembed: embedder failed");
report.rows_failed += 1;
continue;
}
};
if let Err(e) = new_embedding.validate() {
tracing::warn!(%memory_id, error = %e, "reembed: embedding validate failed");
report.rows_failed += 1;
continue;
}
let txn = match self
.conn
.transaction_with_behavior(TransactionBehavior::Immediate)
{
Ok(t) => t,
Err(e) => {
tracing::warn!(%memory_id, error = %e, "reembed: BEGIN failed");
report.rows_failed += 1;
continue;
}
};
// INSERT ... ON CONFLICT(memory_id, embedder_id) DO UPDATE.
// If a partial earlier reembed already wrote the current row
// for this memory, refresh it with the freshly-computed
// vector. (Same content + same embedder = same vector, so
// this is a no-op semantically; we still bump created_at_ms.)
let insert_res = txn.execute(
"INSERT INTO embeddings (memory_id, embedder_id, dtype, dim, vector, created_at_ms)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(memory_id, embedder_id) DO UPDATE SET
dtype = excluded.dtype,
dim = excluded.dim,
vector = excluded.vector,
created_at_ms = excluded.created_at_ms",
params![
memory_id,
current_id,
dtype_str,
new_embedding.dim as i64,
&new_embedding.data[..],
now_ms,
],
);
if let Err(e) = insert_res {
tracing::warn!(%memory_id, error = %e, "reembed: INSERT failed");
report.rows_failed += 1;
continue;
}
let gc_count = if scope.gc {
match txn.execute(
"DELETE FROM embeddings
WHERE memory_id = ? AND embedder_id != ?",
params![memory_id, current_id],
) {
Ok(n) => n,
Err(e) => {
tracing::warn!(%memory_id, error = %e, "reembed: GC DELETE failed");
report.rows_failed += 1;
continue;
}
}
} else {
0
};
if let Err(e) = txn.commit() {
tracing::warn!(%memory_id, error = %e, "reembed: COMMIT failed");
report.rows_failed += 1;
continue;
}
report.rows_reembedded += 1;
report.rows_gc_deleted += gc_count;
}
tracing::info!(
seen = report.rows_seen,
reembedded = report.rows_reembedded,
failed = report.rows_failed,
gc_deleted = report.rows_gc_deleted,
"reembed complete"
);
Ok(report)
}
fn handle_backup(&mut self, dest_path: &std::path::Path) -> Result<()> {
let key = self.key.as_ref().ok_or_else(|| {
Error::storage(
"backup called but writer has no key material configured. \
Spawn the writer with `spawn_full_with_key_and_optional_steward` \
to enable WriteCommand::Backup.",
)
})?;
// Important: route through the writer's existing source connection
// so the backup runs against live in-flight WAL state via SQLite's
// page-level snapshot. SQLite serialises page reads with concurrent
// writes on the same connection, so this is safe even mid-burst.
backup_from_connection(&self.conn, dest_path, key)
}
fn handle_save_snapshot(&mut self) -> Result<()> {
let dir = self.snapshot_dir.as_ref().ok_or_else(|| {
Error::storage("save_snapshot called but writer has no snapshot_dir configured")
})?;
// Delegates to the impl's `save` (HnswIndex routes to crate::snapshot).
// The trait keeps us implementation-agnostic; the StubVectorIndex used
// in unit tests just bumps a counter.
let save_result = self.hnsw.save(dir);
// Piggyback maintenance pragmas on the snapshot cadence (5 min by
// default). ADR-0003 §O5 / §"Final consolidated action items" #9
// call for hourly `PRAGMA optimize` + idle PASSIVE checkpoint.
// Running them every 5 min instead of hourly is harmless (both are
// cheap when there's nothing to do) and avoids a separate timer
// task. Failures are logged but don't fail the save itself —
// they're maintenance, not durability.
self.run_idle_maintenance();
save_result
}
/// Best-effort PRAGMA optimize + wal_checkpoint(PASSIVE). Safe to call
/// on the writer's connection at any time.
fn run_idle_maintenance(&mut self) {
if let Err(e) = self.conn.execute_batch("PRAGMA optimize") {
tracing::debug!(error = %e, "PRAGMA optimize failed (non-fatal)");
}
if let Err(e) = self.conn.execute_batch("PRAGMA wal_checkpoint(PASSIVE)") {
tracing::debug!(error = %e, "PRAGMA wal_checkpoint(PASSIVE) failed (non-fatal)");
}
}
fn shutdown(&mut self) {
if let Err(e) = self
.conn
.pragma_update(None, "wal_checkpoint", "TRUNCATE")
{
tracing::warn!(error = %e, "wal_checkpoint(TRUNCATE) on shutdown failed");
}
tracing::info!("writer actor shutdown complete");
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::{StubVectorIndex, fixture_episode, fixture_embedding, open_test_db};
use std::time::Duration;
fn rt() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
}
#[test]
fn remember_happy_path_round_trip() {
let (conn, _tmp) = open_test_db();
let hnsw = Arc::new(StubVectorIndex::new(4));
let WriterSpawn { handle, join } = WriterActor::spawn(conn, hnsw.clone());
let episode = fixture_episode("test content");
let embedding = fixture_embedding(4);
let mid = rt()
.block_on(handle.remember(episode.clone(), embedding))
.unwrap();
assert_eq!(mid, episode.memory_id);
std::thread::sleep(Duration::from_millis(50));
drop(handle);
std::thread::sleep(Duration::from_millis(50));
assert_eq!(hnsw.add_count(), 1);
let added = hnsw.last_added().unwrap();
assert_eq!(added.0, 1, "rowid should be 1 (first insert)");
assert_eq!(added.1.len(), 4);
}
#[test]
fn dispatch_remember_replies_before_drain() {
let (conn, _tmp) = open_test_db();
let hnsw = Arc::new(StubVectorIndex::new(4));
let (_tx, rx) = mpsc::channel(1);
let mut actor = WriterActor {
conn,
hnsw,
rx,
snapshot_dir: None,
embedder_id: None,
embedder: None,
runtime_handle: None,
steward: None,
key: None,
};
let (reply_tx, reply_rx) = oneshot::channel();
let episode = fixture_episode("ordering test");
let embedding = fixture_embedding(4);
actor.dispatch_remember(episode.clone(), embedding, reply_tx);
let received = reply_rx.blocking_recv().unwrap();
assert_eq!(received.unwrap(), episode.memory_id);
let n: u32 = actor
.conn
.query_row("SELECT COUNT(*) FROM pending_index", [], |row| row.get(0))
.unwrap();
assert_eq!(n, 0);
}
#[test]
fn forget_unknown_memory_id_returns_not_found() {
let (conn, _tmp) = open_test_db();
let hnsw = Arc::new(StubVectorIndex::new(4));
let WriterSpawn { handle, join: _ } = WriterActor::spawn(conn, hnsw);
let mid = MemoryId::new();
let err = rt()
.block_on(handle.forget(mid, "test".into()))
.unwrap_err();
assert!(err.to_string().contains("not found"), "got: {err}");
}
#[test]
fn forget_marks_status_forgotten() {
let (conn, _tmp) = open_test_db();
let hnsw = Arc::new(StubVectorIndex::new(4));
let WriterSpawn { handle, join: _ } = WriterActor::spawn(conn, hnsw.clone());
let episode = fixture_episode("to be forgotten");
let mid = rt()
.block_on(handle.remember(episode.clone(), fixture_embedding(4)))
.unwrap();
rt().block_on(handle.forget(mid, "no longer relevant".into()))
.unwrap();
// Re-open the file (writer holds the DB connection; test-side reopen
// gets read access via SQLite's WAL).
// We can't peek into the writer's connection, so close everything and
// re-open via open_test_db_at on the underlying path. But open_test_db
// returned us an in-memory tmp; we need a different fixture.
// Simpler: use the StubVectorIndex's add_count to verify the embed
// happened, and trust that the handle.forget Ok return means the
// UPDATE ran. The full SQL roundtrip is exercised by the
// reader.rs::reader_sees_writes_committed_through_writer_actor test
// pattern, which we don't replicate here.
assert_eq!(hnsw.add_count(), 1);
let _ = mid; // ensure we exercised the codepath; status check is in
// the reader-pool test below.
}
#[test]
fn forget_is_idempotent_when_already_forgotten() {
let (conn, _tmp) = open_test_db();
let hnsw = Arc::new(StubVectorIndex::new(4));
let WriterSpawn { handle, join: _ } = WriterActor::spawn(conn, hnsw);
let episode = fixture_episode("forget twice");
let mid = rt()
.block_on(handle.remember(episode, fixture_embedding(4)))
.unwrap();
rt().block_on(handle.forget(mid, "first".into())).unwrap();
// Second call: still Ok (idempotent), no error.
rt().block_on(handle.forget(mid, "second".into())).unwrap();
}
#[test]
fn many_concurrent_writes_serialize_correctly() {
let (conn, _tmp) = open_test_db();
let hnsw = Arc::new(StubVectorIndex::new(4));
let WriterSpawn { handle, join } = WriterActor::spawn(conn, hnsw.clone());
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()
.unwrap();
let results: Vec<Result<MemoryId>> = runtime.block_on(async {
let mut tasks = Vec::new();
for i in 0..50 {
let h = handle.clone();
let ep = fixture_episode(&format!("write {i}"));
tasks.push(tokio::spawn(async move {
h.remember(ep, fixture_embedding(4)).await
}));
}
let mut out = Vec::new();
for t in tasks {
out.push(t.await.unwrap());
}
out
});
let mut ids = std::collections::HashSet::new();
for r in results {
let mid = r.expect("remember must succeed");
assert!(ids.insert(mid), "memory_ids must be unique");
}
assert_eq!(ids.len(), 50);
assert_eq!(hnsw.add_count(), 50);
}
}