agentstategraph-storage 1.3.2

Pluggable storage backends for AgentStateGraph
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
//! PostgreSQL storage backend — multi-tenant, connection-pooled.
//!
//! Uses tokio-postgres + deadpool for connection pooling.
//! Each tenant's data is isolated via a `tenant_id` column on every table.
//!
//! Usage:
//!   let storage = PostgresStorage::connect("postgres://localhost/agentstategraph").await?;
//!   let storage = PostgresStorage::connect_tenant("postgres://...", "tenant-123").await?;
//!   // From sync code, with or without an async runtime:
//!   let storage = PostgresStorage::connect_tenant_blocking("postgres://...", "tenant-123", 8)?;
//!
//! The storage traits are synchronous. Every connection is driven by one
//! process-wide runtime owned by this module, so callers can use the storage
//! from plain threads, from a multi-threaded tokio runtime, or from a
//! single-threaded one, and the runtime that connected needn't outlive it.
//!
//! TLS (the `postgres-tls` feature) is set in the connection string, the way
//! libpq does it:
//!   postgres://user@db.example.com/app?sslmode=verify-full&sslrootcert=/etc/ca.pem
//! - `sslmode=require`, `verify-ca` and `verify-full` all mean verified TLS: the
//!   server's certificate must chain to Mozilla's roots or to a certificate in
//!   `sslrootcert` (or `PGSSLROOTCERT`), and must match the host name. There is
//!   no unverified TLS. `verify-ca` is stricter here than in libpq, because the
//!   host name is checked too.
//! - `sslmode=prefer` uses TLS when the server offers it, and still verifies it.
//! - No `sslmode`, or `sslmode=disable`, is plaintext, as before. Use
//!   `sslmode=require` or `verify-full` for any host that isn't local.
//! Without the `postgres-tls` feature, asking for TLS is an error that names
//! the feature, never a silent plaintext connection.
//!
//! As of 0.6.75-beta.1 this backend persists epochs and sessions too,
//! with the same schema shape as SQLite (JSON-as-TEXT). Multi-tenant
//! isolation continues via a `tenant_id` column on every table.
//!
//! History walks, the project-history rollups and value search work as they
//! do on SQLite. Search uses a trigram index when the `pg_trgm` extension can
//! be installed, and scans the ref's rows when it can't. Requires PostgreSQL
//! 13 or later.
//!
//! GC sweeps one tenant at a time and never makes writers wait: a sweep
//! spares objects written within a grace period (see
//! [`PostgresStorage::with_gc_grace`]) instead of locking writers out.

use deadpool_postgres::{
    Config, ManagerConfig, PoolConfig, RecyclingMethod, Runtime as PoolRuntime,
};
// The pool, its clients and their transactions record each query as a
// database span and `db.client.operation.duration`, and the pool its
// `db.client.connection.*` metrics.
use oxtel_sql::postgres::{GenericClient, Pool};

/// A pooled client, instrumented.
type PgClient = oxtel_sql::postgres::Client<deadpool_postgres::Object>;
/// A transaction on one, instrumented.
type PgTransaction<'a> = oxtel_sql::postgres::Transaction<'a>;
use std::sync::OnceLock;
use tokio::runtime::RuntimeFlavor;
#[cfg(not(feature = "postgres-tls"))]
use tokio_postgres::NoTls;

/// Default Postgres pool cap (v2-M1). Overridable via constructor or
/// the binary's `--pg-pool-size` flag.
pub const DEFAULT_POOL_SIZE: usize = 32;

use agentstategraph_core::{
    Commit, Epoch, EpochScope, EpochStatus, IntentCategory, Namespace, Node, Object, ObjectId,
    Session, SessionStatus, TAG_GIT_REVISION, TAG_PIN_STATE,
};
use chrono::{DateTime, Utc};

use crate::traits::{
    CommitStore, CommitTrace, CommitWalk, DEFAULT_GC_GRACE, EpochStore, GcReachability, GcSweep,
    HISTORY_NAMESPACE_UNKNOWN, HistoryMilestoneRow, HistoryRollupRow, ObjectStore, RefStore,
    SessionStore, StorageError, StoreShape, StoreTotals, TableBytes, TaintStore,
};

/// TLS settings read from a connection string, and the string tokio-postgres
/// is given in its place: it knows only `sslmode=disable|prefer|require` and
/// rejects `sslrootcert`, so `verify-ca`/`verify-full` become `require` (TLS
/// here is always verified) and `sslrootcert` is taken out. A string with no
/// `sslmode` gets `sslmode=disable`, so it stays plaintext as it always was;
/// tokio-postgres alone would default to `prefer`.
#[derive(Debug, PartialEq)]
struct ConnectionTls {
    url: String,
    requires_tls: bool,
    root_cert: Option<std::path::PathBuf>,
}

impl ConnectionTls {
    /// `env_root_cert` is `PGSSLROOTCERT`, used when the string names none.
    fn parse(database_url: &str, env_root_cert: Option<String>) -> Result<Self, StorageError> {
        let is_url =
            database_url.starts_with("postgres://") || database_url.starts_with("postgresql://");
        // URL form keeps options in the query string; key/value form is
        // space-separated `key=value` pairs (quoted values aren't supported
        // for these two keys).
        let (head, options, sep) = if is_url {
            match database_url.split_once('?') {
                Some((head, query)) => (head, query, '&'),
                None => (database_url, "", '&'),
            }
        } else {
            ("", database_url, ' ')
        };

        let mut kept: Vec<String> = Vec::new();
        let mut mode: Option<String> = None;
        let mut root_cert = None;
        for part in options.split(sep).filter(|p| !p.is_empty()) {
            let (key, value) = part.split_once('=').unwrap_or((part, ""));
            match key {
                "sslmode" => {
                    let mapped = match value {
                        "verify-ca" | "verify-full" => "require",
                        other => other,
                    };
                    mode = Some(value.to_string());
                    kept.push(format!("sslmode={mapped}"));
                }
                "sslrootcert" => {
                    let path = if is_url {
                        percent_decode(value)?
                    } else {
                        value.to_string()
                    };
                    root_cert = Some(std::path::PathBuf::from(path));
                }
                _ => kept.push(part.to_string()),
            }
        }
        if mode.is_none() {
            kept.push("sslmode=disable".to_string());
        }
        let options = kept.join(&sep.to_string());
        let url = if is_url {
            format!("{head}?{options}")
        } else {
            options
        };
        Ok(Self {
            url,
            requires_tls: matches!(
                mode.as_deref(),
                Some("require") | Some("verify-ca") | Some("verify-full")
            ),
            root_cert: root_cert.or_else(|| env_root_cert.map(std::path::PathBuf::from)),
        })
    }
}

/// Decode `%XX` escapes in a URL query value (a file path may contain them).
fn percent_decode(value: &str) -> Result<String, StorageError> {
    let bytes = value.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' {
            let hex = value
                .get(i + 1..i + 3)
                .and_then(|h| u8::from_str_radix(h, 16).ok())
                .ok_or_else(|| {
                    StorageError::Backend(format!(
                        "bad percent-escape in connection string: {value}"
                    ))
                })?;
            out.push(hex);
            i += 3;
        } else {
            out.push(bytes[i]);
            i += 1;
        }
    }
    String::from_utf8(out)
        .map_err(|_| StorageError::Backend(format!("connection string value isn't UTF-8: {value}")))
}

/// rustls for tokio-postgres: always-verified TLS against Mozilla's roots
/// plus any `sslrootcert`. Channel binding (SCRAM-SHA-256-PLUS) isn't
/// offered; ordinary SCRAM authentication works over the TLS connection.
#[cfg(feature = "postgres-tls")]
mod tls {
    use std::future::Future;
    use std::io;
    use std::path::Path;
    use std::pin::Pin;
    use std::sync::Arc;
    use std::task::{Context, Poll};

    use rustls::pki_types::pem::PemObject;
    use rustls::pki_types::{CertificateDer, ServerName};
    use rustls::{ClientConfig, RootCertStore};
    use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
    use tokio_postgres::tls::{ChannelBinding, MakeTlsConnect, TlsConnect, TlsStream};

    use crate::traits::StorageError;

    pub(super) fn connector(root_cert: Option<&Path>) -> Result<MakeRustlsConnect, StorageError> {
        let mut roots = RootCertStore {
            roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
        };
        if let Some(path) = root_cert {
            let bad = |e: &dyn std::fmt::Display| {
                StorageError::Backend(format!("sslrootcert {}: {e}", path.display()))
            };
            let certs = CertificateDer::pem_file_iter(path)
                .map_err(|e| bad(&e))?
                .collect::<Result<Vec<_>, _>>()
                .map_err(|e| bad(&e))?;
            if certs.is_empty() {
                return Err(bad(&"no PEM certificates in the file"));
            }
            for cert in certs {
                roots.add(cert).map_err(|e| bad(&e))?;
            }
        }
        let config =
            ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
                .with_safe_default_protocol_versions()
                .map_err(|e| StorageError::Backend(format!("postgres tls: {e}")))?
                .with_root_certificates(roots)
                .with_no_client_auth();
        Ok(MakeRustlsConnect(Arc::new(config)))
    }

    #[derive(Clone)]
    pub(super) struct MakeRustlsConnect(Arc<ClientConfig>);

    impl<S> MakeTlsConnect<S> for MakeRustlsConnect
    where
        S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
    {
        type Stream = RustlsStream<S>;
        type TlsConnect = RustlsConnect;
        type Error = io::Error;

        fn make_tls_connect(&mut self, host: &str) -> io::Result<RustlsConnect> {
            let name = ServerName::try_from(host.to_string())
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
            Ok(RustlsConnect {
                config: Arc::clone(&self.0),
                name,
            })
        }
    }

    pub(super) struct RustlsConnect {
        config: Arc<ClientConfig>,
        name: ServerName<'static>,
    }

    impl<S> TlsConnect<S> for RustlsConnect
    where
        S: AsyncRead + AsyncWrite + Unpin + Send + 'static,
    {
        type Stream = RustlsStream<S>;
        type Error = io::Error;
        type Future = Pin<Box<dyn Future<Output = io::Result<RustlsStream<S>>> + Send>>;

        fn connect(self, stream: S) -> Self::Future {
            Box::pin(async move {
                tokio_rustls::TlsConnector::from(self.config)
                    .connect(self.name, stream)
                    .await
                    .map(RustlsStream)
            })
        }
    }

    pub(super) struct RustlsStream<S>(tokio_rustls::client::TlsStream<S>);

    impl<S: AsyncRead + AsyncWrite + Unpin> TlsStream for RustlsStream<S> {
        fn channel_binding(&self) -> ChannelBinding {
            ChannelBinding::none()
        }
    }

    impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for RustlsStream<S> {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<io::Result<()>> {
            Pin::new(&mut self.get_mut().0).poll_read(cx, buf)
        }
    }

    impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for RustlsStream<S> {
        fn poll_write(
            self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<io::Result<usize>> {
            Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
        }

        fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
            Pin::new(&mut self.get_mut().0).poll_flush(cx)
        }

        fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
            Pin::new(&mut self.get_mut().0).poll_shutdown(cx)
        }
    }
}

/// The schema this build expects. Bump it whenever `SCHEMA_DDL` changes:
/// databases stamped with an older version re-run the (idempotent) DDL once.
///
/// 2: history walks in one query, the history rollup tables, and the leaf
/// search index (Plan D t-008).
/// 3: GC: object write times and speculation pins (Plan D t-009).
/// 4: the trace each commit was made in (Plan H t-009).
const SCHEMA_VERSION: i32 = 4;

/// Every table, column and index, each `IF NOT EXISTS` so it's safe to re-run
/// over any earlier schema, plus the version stamp table.
const SCHEMA_DDL: &str = "
                CREATE TABLE IF NOT EXISTS objects (
                    tenant_id TEXT NOT NULL,
                    id        BYTEA NOT NULL,
                    data      JSONB NOT NULL,
                    PRIMARY KEY (tenant_id, id)
                );

                CREATE TABLE IF NOT EXISTS commits (
                    tenant_id TEXT NOT NULL,
                    id        BYTEA NOT NULL,
                    data      JSONB NOT NULL,
                    timestamp TIMESTAMPTZ NOT NULL,
                    PRIMARY KEY (tenant_id, id)
                );

                CREATE TABLE IF NOT EXISTS namespaces (
                    tenant_id  TEXT NOT NULL,
                    name       TEXT NOT NULL,
                    created_at TEXT NOT NULL,
                    PRIMARY KEY (tenant_id, name)
                );

                CREATE TABLE IF NOT EXISTS refs (
                    tenant_id TEXT NOT NULL,
                    namespace TEXT NOT NULL,
                    name      TEXT NOT NULL,
                    target    BYTEA NOT NULL,
                    PRIMARY KEY (tenant_id, namespace, name)
                );

                CREATE INDEX IF NOT EXISTS idx_commits_tenant_ts
                    ON commits(tenant_id, timestamp DESC);

                CREATE TABLE IF NOT EXISTS epochs (
                    tenant_id       TEXT NOT NULL,
                    id              TEXT NOT NULL,
                    description     TEXT NOT NULL DEFAULT '',
                    status          TEXT NOT NULL DEFAULT 'Active',
                    created_at      TEXT NOT NULL,
                    sealed_at       TEXT,
                    summary         TEXT,
                    root_intents    TEXT NOT NULL DEFAULT '[]',
                    agents          TEXT NOT NULL DEFAULT '[]',
                    tags            TEXT NOT NULL DEFAULT '[]',
                    commit_count    INTEGER NOT NULL DEFAULT 0,
                    sealed_commits  TEXT NOT NULL DEFAULT '[]',
                    PRIMARY KEY (tenant_id, id)
                );

                CREATE TABLE IF NOT EXISTS sessions (
                    tenant_id       TEXT NOT NULL,
                    id              TEXT NOT NULL,
                    agent_id        TEXT NOT NULL,
                    parent_id       TEXT,
                    scope_path      TEXT,
                    scope_branch    TEXT,
                    scope_namespace TEXT,
                    status          TEXT NOT NULL DEFAULT 'Active',
                    created_at      TEXT NOT NULL,
                    ended_at        TEXT,
                    metadata        TEXT NOT NULL DEFAULT '{}',
                    commit_count    INTEGER NOT NULL DEFAULT 0,
                    PRIMARY KEY (tenant_id, id)
                );

                CREATE TABLE IF NOT EXISTS taints (
                    tenant_id       TEXT NOT NULL,
                    id              TEXT NOT NULL,
                    path            TEXT NOT NULL,
                    name            TEXT NOT NULL,
                    kind            TEXT NOT NULL,
                    effect          TEXT NOT NULL,
                    severity        TEXT NOT NULL DEFAULT 'medium',
                    reason          TEXT NOT NULL,
                    agent_id        TEXT NOT NULL,
                    commit_id       TEXT NOT NULL DEFAULT '',
                    created_at      TEXT NOT NULL,
                    expires_at      TEXT,
                    resolved_at     TEXT,
                    resolved_by     TEXT,
                    resolved_reason TEXT,
                    resolved_proof  TEXT,
                    propagate       BOOLEAN NOT NULL DEFAULT true,
                    metadata        TEXT NOT NULL DEFAULT '{}',
                    PRIMARY KEY (tenant_id, id)
                );

                CREATE INDEX IF NOT EXISTS idx_epochs_tenant_status
                    ON epochs(tenant_id, status);
                CREATE INDEX IF NOT EXISTS idx_sessions_tenant_agent
                    ON sessions(tenant_id, agent_id);
                CREATE TABLE IF NOT EXISTS reminders (
                    tenant_id       TEXT NOT NULL,
                    id              TEXT NOT NULL,
                    title           TEXT NOT NULL,
                    instructions    TEXT NOT NULL,
                    commands        TEXT NOT NULL DEFAULT '[]',
                    refs            TEXT NOT NULL DEFAULT '[]',
                    priority        INTEGER NOT NULL DEFAULT 3,
                    due_at          TEXT NOT NULL,
                    schedule        TEXT,
                    autonomous      BOOLEAN NOT NULL DEFAULT true,
                    created_by      TEXT NOT NULL,
                    created_at      TEXT NOT NULL,
                    status          TEXT NOT NULL DEFAULT 'pending',
                    snoozed_until   TEXT,
                    executions      TEXT NOT NULL DEFAULT '[]',
                    tags            TEXT NOT NULL DEFAULT '[]',
                    PRIMARY KEY (tenant_id, id)
                );

                CREATE INDEX IF NOT EXISTS idx_taints_tenant_path
                    ON taints(tenant_id, path);
                CREATE INDEX IF NOT EXISTS idx_taints_tenant_kind
                    ON taints(tenant_id, kind);
                CREATE UNIQUE INDEX IF NOT EXISTS idx_taints_unique_active
                    ON taints(tenant_id, path, name, kind) WHERE resolved_at IS NULL;
                CREATE INDEX IF NOT EXISTS idx_reminders_tenant_status
                    ON reminders(tenant_id, status);
                CREATE INDEX IF NOT EXISTS idx_reminders_tenant_due
                    ON reminders(tenant_id, due_at);
                CREATE INDEX IF NOT EXISTS idx_reminders_tenant_priority
                    ON reminders(tenant_id, priority);
                CREATE INDEX IF NOT EXISTS idx_reminders_tenant_created_by
                    ON reminders(tenant_id, created_by);

                ALTER TABLE commits ADD COLUMN IF NOT EXISTS epoch_id TEXT;
                ALTER TABLE commits ADD COLUMN IF NOT EXISTS session_id TEXT;
                ALTER TABLE sessions ADD COLUMN IF NOT EXISTS scope_namespace TEXT;

                -- Epoch fidelity and scoping, as in the SQLite backend: the
                -- seal digest, the owning namespace, and what the seal covers.
                -- Nullable or defaulted, so existing rows read back as before
                -- (global, scope = all).
                ALTER TABLE epochs ADD COLUMN IF NOT EXISTS seal_hash BYTEA;
                ALTER TABLE epochs ADD COLUMN IF NOT EXISTS namespace TEXT;
                ALTER TABLE epochs ADD COLUMN IF NOT EXISTS scope TEXT NOT NULL DEFAULT 'all';
                CREATE INDEX IF NOT EXISTS idx_epochs_tenant_namespace
                    ON epochs(tenant_id, namespace);

                CREATE INDEX IF NOT EXISTS idx_commits_tenant_epoch
                    ON commits(tenant_id, epoch_id);
                CREATE INDEX IF NOT EXISTS idx_commits_tenant_session
                    ON commits(tenant_id, session_id);

                -- History walks (version 2). A walk follows parent ids from
                -- the commit JSON to the commits holding them, in one
                -- recursive query; this index makes each step a lookup.
                -- Reading the JSON rather than a separate edges table means a
                -- commit written by an older binary is walkable too.
                CREATE INDEX IF NOT EXISTS idx_commits_data_id
                    ON commits USING hash ((data -> 'id'));

                -- The id of the transaction that wrote each commit (version
                -- 2), which orders commits for the history extractor. Rows
                -- from before this column read 0, so they come first.
                ALTER TABLE commits ADD COLUMN IF NOT EXISTS xid BIGINT NOT NULL DEFAULT 0;
                ALTER TABLE commits
                    ALTER COLUMN xid SET DEFAULT (pg_current_xact_id()::text::bigint);
                CREATE INDEX IF NOT EXISTS idx_commits_tenant_xid
                    ON commits(tenant_id, xid, id);

                -- Distilled project history (version 2), as in the SQLite
                -- backend. Derived and rebuildable, never authoritative. Text
                -- columns compare bytewise (COLLATE \"C\"), as SQLite's do, so
                -- RFC 3339 timestamps order correctly and output order
                -- matches SQLite's.
                CREATE TABLE IF NOT EXISTS asg_history_cursor (
                    tenant_id TEXT PRIMARY KEY,
                    xid       BIGINT NOT NULL,
                    commit_id BYTEA NOT NULL,
                    folded    BIGINT NOT NULL
                );
                CREATE TABLE IF NOT EXISTS asg_history_commit_rollup (
                    tenant_id       TEXT NOT NULL,
                    day             TEXT COLLATE \"C\" NOT NULL,
                    namespace       TEXT COLLATE \"C\" NOT NULL,
                    agent_id        TEXT COLLATE \"C\" NOT NULL,
                    intent_category TEXT COLLATE \"C\" NOT NULL,
                    commit_count    BIGINT NOT NULL DEFAULT 0,
                    first_ts        TEXT COLLATE \"C\" NOT NULL,
                    last_ts         TEXT COLLATE \"C\" NOT NULL,
                    PRIMARY KEY (tenant_id, day, namespace, agent_id, intent_category)
                );
                CREATE TABLE IF NOT EXISTS asg_history_milestone (
                    tenant_id   TEXT NOT NULL,
                    commit_id   BYTEA NOT NULL,
                    kind        TEXT NOT NULL,
                    timestamp   TEXT COLLATE \"C\" NOT NULL,
                    day         TEXT NOT NULL,
                    namespace   TEXT NOT NULL,
                    agent_id    TEXT NOT NULL,
                    description TEXT NOT NULL,
                    state_root  BYTEA,
                    git_sha     TEXT,
                    PRIMARY KEY (tenant_id, commit_id, kind)
                );
                CREATE INDEX IF NOT EXISTS idx_asg_history_milestone_ts
                    ON asg_history_milestone(tenant_id, timestamp);

                -- Leaf-value search index (version 2), as in the SQLite
                -- backend: the string leaves of a (namespace, ref), kept up to
                -- date as the ref moves. A path holds one value. `root` is the
                -- state root the set reflects (NULL when unknown), so a diff
                -- applies only to the tree it was computed from.
                CREATE TABLE IF NOT EXISTS leaf_rows (
                    tenant_id TEXT NOT NULL,
                    namespace TEXT NOT NULL,
                    ref       TEXT NOT NULL,
                    path      TEXT NOT NULL,
                    value     TEXT NOT NULL,
                    PRIMARY KEY (tenant_id, namespace, ref, path)
                );
                CREATE TABLE IF NOT EXISTS leaf_index_refs (
                    tenant_id TEXT NOT NULL,
                    namespace TEXT NOT NULL,
                    ref       TEXT NOT NULL,
                    root      BYTEA,
                    PRIMARY KEY (tenant_id, namespace, ref)
                );
                -- Substring search uses a trigram index when pg_trgm can be
                -- installed (it is a trusted extension, so a role with CREATE
                -- on the database can). Without it, search still works by
                -- scanning the ref's rows.
                DO $$
                BEGIN
                    CREATE EXTENSION IF NOT EXISTS pg_trgm;
                    CREATE INDEX IF NOT EXISTS idx_leaf_rows_value_trgm
                        ON leaf_rows USING gin (value gin_trgm_ops);
                EXCEPTION WHEN OTHERS THEN
                    RAISE NOTICE 'leaf search runs without a trigram index: %', SQLERRM;
                END
                $$;

                -- GC (version 3). When each object was last written: a sweep
                -- spares unreachable objects written within its grace period,
                -- which is what keeps a write in flight safe (it stores its
                -- objects before committing them). Rows from before read
                -- -infinity, so they can go as soon as nothing reaches them.
                ALTER TABLE objects
                    ADD COLUMN IF NOT EXISTS written_at TIMESTAMPTZ NOT NULL DEFAULT '-infinity';
                ALTER TABLE objects ALTER COLUMN written_at SET DEFAULT now();
                -- Roots of uncommitted work (open speculations) that a sweep
                -- must keep, from any process. See gc_pin_root.
                CREATE TABLE IF NOT EXISTS gc_pending_roots (
                    tenant_id  TEXT NOT NULL,
                    key        TEXT NOT NULL,
                    root       BYTEA NOT NULL,
                    updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
                    PRIMARY KEY (tenant_id, key)
                );

                -- The trace each commit was made in (version 4): beside the
                -- commit, not in it, so commit ids don't depend on telemetry.
                CREATE TABLE IF NOT EXISTS commit_traces (
                    tenant_id TEXT NOT NULL,
                    commit_id BYTEA NOT NULL,
                    trace_id  TEXT NOT NULL,
                    span_id   TEXT NOT NULL,
                    PRIMARY KEY (tenant_id, commit_id)
                );

                CREATE TABLE IF NOT EXISTS asg_schema (
                    component TEXT PRIMARY KEY,
                    version   INTEGER NOT NULL
                );
";

/// A database error with its whole cause chain. tokio-postgres's Display for
/// a server error is just "db error"; the server's message is the source.
fn with_causes(context: &str, e: &dyn std::error::Error) -> String {
    let mut msg = format!("{context}: {e}");
    let mut source = e.source();
    while let Some(cause) = source {
        msg.push_str(&format!(": {cause}"));
        source = cause.source();
    }
    msg
}

/// A failure to get a pooled connection, with its whole cause chain: the pool
/// and tokio-postgres errors alone say only "error performing TLS handshake",
/// and the reason (an untrusted issuer, a name mismatch, a refused login) is
/// further down the chain.
fn conn_error(e: impl std::error::Error) -> StorageError {
    let mut msg = format!("get conn: {e}");
    let mut source = e.source();
    while let Some(cause) = source {
        msg.push_str(&format!(": {cause}"));
        source = cause.source();
    }
    StorageError::Backend(msg)
}

/// What storing an object that already exists does: refresh its write time
/// if it is over a minute old. A sweep spares recently written objects, so an
/// object a writer has just stored (or stored again, since content addressing
/// dedups) can't be taken before the writer commits it. The conflicting row is
/// locked whether or not it is updated, and the sweep re-checks the write
/// time when it deletes, so the two can't interleave.
const FRESHEN_ON_CONFLICT: &str = "ON CONFLICT (tenant_id, id) DO UPDATE SET written_at = now()
                         WHERE objects.written_at < now() - interval '1 minute'";

/// Advisory-lock key that serializes schema setup across every process using
/// a database ("asg-sch" as bytes).
const SCHEMA_LOCK_KEY: i64 = 0x6173_672d_7363_68;

/// The runtime that drives every `PostgresStorage` connection in the process.
///
/// A pooled connection lives on the runtime it was opened in. Owning that
/// runtime here means no caller's runtime has to exist, be multi-threaded, or
/// outlive the pool, and it is never dropped inside an async context.
fn runtime() -> &'static tokio::runtime::Runtime {
    static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RUNTIME.get_or_init(|| {
        let workers = std::thread::available_parallelism().map_or(2, |n| n.get().clamp(2, 8));
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(workers)
            .thread_name("asg-postgres")
            .enable_all()
            .build()
            .expect("start the Postgres storage runtime")
    })
}

/// PostgreSQL-backed storage with connection pooling and optional multi-tenancy.
pub struct PostgresStorage {
    pool: Pool,
    tenant_id: String,
    /// How recently written an unreachable object must be for a GC sweep to
    /// spare it. See [`PostgresStorage::with_gc_grace`].
    gc_grace: std::time::Duration,
    /// The connection holding this tenant's sweep lock, between
    /// `history_gc_lock_writers` and `history_gc_unlock_writers`.
    gc_conn: std::sync::Mutex<Option<PgClient>>,
}

impl PostgresStorage {
    /// Connect to Postgres with the default tenant.
    pub async fn connect(database_url: &str) -> Result<Self, StorageError> {
        Self::connect_tenant(database_url, "default").await
    }

    /// Connect to Postgres with a specific tenant ID for data isolation.
    ///
    /// Applies the default pool size cap (`DEFAULT_POOL_SIZE`).
    pub async fn connect_tenant(database_url: &str, tenant_id: &str) -> Result<Self, StorageError> {
        Self::connect_tenant_with_pool_size(database_url, tenant_id, DEFAULT_POOL_SIZE).await
    }

    /// Connect to Postgres with a specific tenant ID and an explicit pool
    /// size cap (v2-M1). `max_size` is clamped to at least 1.
    pub async fn connect_tenant_with_pool_size(
        database_url: &str,
        tenant_id: &str,
        max_size: usize,
    ) -> Result<Self, StorageError> {
        let storage = Self::with_pool(database_url, tenant_id, max_size)?;
        // Schema setup runs on the storage runtime, so the connection it opens
        // (which then goes back into the pool) is driven there rather than by
        // the caller's runtime, which may be gone when that connection is reused.
        let setup = Self {
            pool: storage.pool.clone(),
            tenant_id: storage.tenant_id.clone(),
            gc_grace: storage.gc_grace,
            gc_conn: std::sync::Mutex::new(None),
        };
        runtime()
            .spawn(async move { setup.init_tables().await })
            .await
            .map_err(|e| StorageError::Backend(format!("postgres setup task: {e}")))??;
        Ok(storage)
    }

    /// Connect from synchronous code. Needs no async runtime, and also works
    /// when called from inside one.
    pub fn connect_tenant_blocking(
        database_url: &str,
        tenant_id: &str,
        max_size: usize,
    ) -> Result<Self, StorageError> {
        let storage = Self::with_pool(database_url, tenant_id, max_size)?;
        storage.block_on(storage.init_tables())?;
        Ok(storage)
    }

    fn with_pool(
        database_url: &str,
        tenant_id: &str,
        max_size: usize,
    ) -> Result<Self, StorageError> {
        let tls = ConnectionTls::parse(
            database_url,
            std::env::var("PGSSLROOTCERT")
                .ok()
                .filter(|v| !v.is_empty()),
        )?;
        let mut cfg = Config::new();
        cfg.url = Some(tls.url.clone());
        // What the pool's spans and metrics say about the database.
        let pg_config = cfg
            .get_pg_config()
            .map_err(|e| StorageError::Backend(format!("postgres config: {e}")))?;
        cfg.manager = Some(ManagerConfig {
            recycling_method: RecyclingMethod::Fast,
        });
        cfg.pool = Some(PoolConfig {
            max_size: max_size.max(1),
            ..Default::default()
        });

        #[cfg(feature = "postgres-tls")]
        let pool = cfg.create_pool(
            Some(PoolRuntime::Tokio1),
            tls::connector(tls.root_cert.as_deref())?,
        );
        #[cfg(not(feature = "postgres-tls"))]
        let pool = {
            if tls.requires_tls || tls.root_cert.is_some() {
                return Err(StorageError::Backend(
                    "the connection string asks for TLS (sslmode=require/verify-*, or \
                     sslrootcert), but this build has no Postgres TLS support: build with \
                     the `postgres-tls` feature"
                        .into(),
                ));
            }
            cfg.create_pool(Some(PoolRuntime::Tokio1), NoTls)
        };
        let pool = pool.map_err(|e| StorageError::Backend(format!("postgres pool: {}", e)))?;
        let pool = Pool::new(pool, &pg_config);
        Ok(Self {
            pool,
            tenant_id: tenant_id.to_string(),
            gc_grace: DEFAULT_GC_GRACE,
            gc_conn: std::sync::Mutex::new(None),
        })
    }

    /// Set how recently written an unreachable object must be for a GC sweep
    /// to spare it (default [`DEFAULT_GC_GRACE`]).
    ///
    /// Writers don't wait for a sweep here; the grace period protects them
    /// instead. A write stores its new objects before it commits them, and an
    /// object stored again refreshes its write time (at most a minute old
    /// after any write), so a sweep never takes an object a write in flight
    /// is about to commit. A sweep spares everything written since it started,
    /// whatever the grace. A grace under a minute narrows the protection for
    /// writes already in flight when a sweep starts; tests use zero to see
    /// garbage go at once.
    pub fn with_gc_grace(mut self, grace: std::time::Duration) -> Self {
        self.gc_grace = grace;
        self
    }

    /// Return the configured pool's maximum size. Useful for tests.
    pub fn pool_max_size(&self) -> usize {
        self.pool.status().max_size
    }

    /// The storage's connection pool, configured as for the storage itself
    /// (TLS, pool size), with every query recorded as a database span. For a
    /// host that keeps tables of its own in the same database: scope their
    /// rows by [`tenant_id`](Self::tenant_id), and use the pool inside
    /// [`PostgresStorage::run`], so a connection it opens is driven by the
    /// storage's runtime like the storage's own, not by a caller's runtime that
    /// may be gone when the connection is reused. Take the pool and tenant
    /// before handing the storage to a `Repository`. Create the tables in a
    /// transaction holding an advisory lock (`pg_advisory_xact_lock`):
    /// `CREATE TABLE IF NOT EXISTS` from two processes at once can still fail
    /// on Postgres's catalog index. The client crates are re-exported as
    /// `agentstategraph_storage::{oxtel_sql, tokio_postgres}`.
    pub fn pool(&self) -> Pool {
        self.pool.clone()
    }

    /// The tenant this storage reads and writes.
    pub fn tenant_id(&self) -> &str {
        &self.tenant_id
    }

    /// Bring the schema up to date and seed this tenant's default namespace.
    ///
    /// The DDL runs only when the database's stamped schema version is behind
    /// this build's. `ALTER TABLE ... IF NOT EXISTS` takes an exclusive table
    /// lock even when it changes nothing, so running it on every connect let a
    /// starting process deadlock with writers holding locks on several tables
    /// (any multi-statement transaction). Up-to-date databases now take no
    /// table locks at startup. An actual upgrade still can collide with
    /// writers, so a deadlock victim retries.
    async fn init_tables(&self) -> Result<(), StorageError> {
        let mut client = self.pool.get().await.map_err(conn_error)?;
        let mut attempt = 0;
        loop {
            attempt += 1;
            match self.try_init_tables(&mut client).await {
                Err(e)
                    if attempt < 5
                        && e.code()
                            == Some(&tokio_postgres::error::SqlState::T_R_DEADLOCK_DETECTED) =>
                {
                    continue;
                }
                Err(e) => return Err(StorageError::Backend(with_causes("init tables", &e))),
                Ok(()) => return Ok(()),
            }
        }
    }

    async fn try_init_tables(&self, client: &mut PgClient) -> Result<(), tokio_postgres::Error> {
        // Serialized across every process using the database.
        let tx = client.transaction().await?;
        tx.execute("SELECT pg_advisory_xact_lock($1)", &[&SCHEMA_LOCK_KEY])
            .await?;
        let stamped: bool = tx
            .query_one("SELECT to_regclass('asg_schema') IS NOT NULL", &[])
            .await?
            .get(0);
        let version: Option<i32> = if stamped {
            tx.query_opt(
                "SELECT version FROM asg_schema WHERE component = 'postgres'",
                &[],
            )
            .await?
            .map(|row| row.get(0))
        } else {
            None
        };
        if version.is_none_or(|v| v < SCHEMA_VERSION) {
            tx.batch_execute(SCHEMA_DDL).await?;
            tx.execute(
                "INSERT INTO asg_schema (component, version) VALUES ('postgres', $1)
                 ON CONFLICT (component) DO UPDATE SET version = EXCLUDED.version",
                &[&SCHEMA_VERSION],
            )
            .await?;
        }
        // Seed the default namespace for this tenant.
        tx.execute(
            "INSERT INTO namespaces (tenant_id, name, created_at)
             VALUES ($1, 'default', NOW()::TEXT)
             ON CONFLICT DO NOTHING",
            &[&self.tenant_id],
        )
        .await?;
        tx.commit().await
    }

    /// Run an async operation for the sync storage API, on whatever kind of
    /// thread the caller is on. See [`run`](Self::run).
    fn block_on<F, T>(&self, f: F) -> Result<T, StorageError>
    where
        F: std::future::Future<Output = Result<T, StorageError>> + Send,
        T: Send,
    {
        Self::run(f)
    }

    /// Run `f` to completion on the storage's runtime (one per process), from
    /// whatever kind of thread the caller is on: a plain thread, or a thread of
    /// either kind of tokio runtime. Every synchronous storage method goes
    /// through this; a host uses it with [`pool`](Self::pool) for its own
    /// tables. (Before Plan D t-002 this assumed a multi-threaded tokio
    /// runtime: plain threads panicked, as did single-threaded runtimes.)
    pub fn run<F, T>(f: F) -> T
    where
        F: std::future::Future<Output = T> + Send,
        T: Send,
    {
        let rt = runtime().handle();
        match tokio::runtime::Handle::try_current().map(|h| h.runtime_flavor()) {
            // Not on a runtime: drive the operation on this thread.
            Err(_) => rt.block_on(f),
            // A multi-threaded runtime's thread: hand its other work off first.
            Ok(RuntimeFlavor::MultiThread) => tokio::task::block_in_place(|| rt.block_on(f)),
            // A single-threaded runtime can neither give up its only thread nor
            // start another runtime on it, so wait on a helper thread.
            Ok(_) => std::thread::scope(|scope| {
                scope
                    .spawn(|| rt.block_on(f))
                    .join()
                    .unwrap_or_else(|panic| std::panic::resume_unwind(panic))
            }),
        }
    }

    async fn require_namespace(
        &self,
        client: &PgClient,
        namespace: &Namespace,
    ) -> Result<(), StorageError> {
        let row = client
            .query_opt(
                "SELECT 1 FROM namespaces WHERE tenant_id = $1 AND name = $2",
                &[&self.tenant_id, &namespace.as_str()],
            )
            .await
            .map_err(|e| StorageError::Backend(format!("check namespace: {}", e)))?;
        match row {
            Some(_) => Ok(()),
            None => Err(StorageError::NamespaceNotFound(
                namespace.as_str().to_string(),
            )),
        }
    }
}

impl PostgresStorage {
    /// Replace the `(namespace, ref)` leaf set with `entries`, marking it
    /// built at `root` (unknown when `None`). The set's row is written first,
    /// so a build and an advance on one set lock in the same order.
    fn leaf_set_replace(
        &self,
        namespace: &str,
        ref_name: &str,
        root: Option<&ObjectId>,
        entries: &[(String, String)],
    ) -> Result<(), StorageError> {
        let root = root.map(|r| r.as_bytes().as_slice());
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            let tx = client
                .transaction()
                .await
                .map_err(|e| leaf_error("build", &e))?;
            tx.execute(
                "INSERT INTO leaf_index_refs (tenant_id, namespace, ref, root)
                 VALUES ($1, $2, $3, $4)
                 ON CONFLICT (tenant_id, namespace, ref) DO UPDATE SET root = EXCLUDED.root",
                &[&self.tenant_id, &namespace, &ref_name, &root],
            )
            .await
            .map_err(|e| leaf_error("build mark", &e))?;
            tx.execute(
                "DELETE FROM leaf_rows WHERE tenant_id = $1 AND namespace = $2 AND ref = $3",
                &[&self.tenant_id, &namespace, &ref_name],
            )
            .await
            .map_err(|e| leaf_error("build clear", &e))?;
            upsert_leaves(&tx, &self.tenant_id, namespace, ref_name, entries).await?;
            tx.commit()
                .await
                .map_err(|e| leaf_error("build commit", &e))
        })
    }
}

// ─── ObjectStore ────────────────────────────────────────────

impl ObjectStore for PostgresStorage {
    fn get_object(&self, id: &ObjectId) -> Result<Option<Object>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let row = client
                .query_opt(
                    "SELECT data FROM objects WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get object: {}", e)))?;

            match row {
                Some(row) => {
                    let data: serde_json::Value = row.get("data");
                    let obj: Object = serde_json::from_value(data)
                        .map_err(|e| StorageError::Serialization(e.to_string()))?;
                    Ok(Some(obj))
                }
                None => Ok(None),
            }
        })
    }

    fn put_object(&self, obj: &Object) -> Result<ObjectId, StorageError> {
        let id = obj.id();
        let data =
            serde_json::to_value(obj).map_err(|e| StorageError::Serialization(e.to_string()))?;

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            client
                .execute(
                    &format!(
                        "INSERT INTO objects (tenant_id, id, data) VALUES ($1, $2, $3)
                         {FRESHEN_ON_CONFLICT}"
                    ),
                    &[&self.tenant_id, &id.as_bytes().as_slice(), &data],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("put object: {}", e)))?;

            Ok(id)
        })
    }

    /// One multi-row INSERT per chunk, all in one transaction: callers such as
    /// merge write a batch as a unit, and the trait's default made one
    /// independent round trip per object.
    fn batch_put_objects(&self, objs: &[Object]) -> Result<Vec<ObjectId>, StorageError> {
        // Each row is two parameters ($1 is the tenant), well under Postgres's
        // 65,535 per statement.
        const CHUNK: usize = 1000;
        let ids: Vec<ObjectId> = objs.iter().map(Object::id).collect();
        // One row per id: an upsert may not touch a row twice in a statement.
        let mut seen = std::collections::HashSet::with_capacity(ids.len());
        let rows: Vec<(ObjectId, serde_json::Value)> = objs
            .iter()
            .zip(&ids)
            .filter(|(_, id)| seen.insert(**id))
            .map(|(obj, id)| {
                serde_json::to_value(obj)
                    .map(|data| (*id, data))
                    .map_err(|e| StorageError::Serialization(e.to_string()))
            })
            .collect::<Result<_, _>>()?;
        if rows.is_empty() {
            return Ok(Vec::new());
        }
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            let tx = client
                .transaction()
                .await
                .map_err(|e| StorageError::Backend(format!("begin batch put: {e}")))?;
            for chunk in rows.chunks(CHUNK) {
                let ids: Vec<&[u8]> = chunk
                    .iter()
                    .map(|(id, _)| id.as_bytes().as_slice())
                    .collect();
                let mut params: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> =
                    Vec::with_capacity(1 + chunk.len() * 2);
                params.push(&self.tenant_id);
                let mut values = Vec::with_capacity(chunk.len());
                for (i, (_, data)) in chunk.iter().enumerate() {
                    values.push(format!("($1, ${}, ${})", 2 + i * 2, 3 + i * 2));
                    params.push(&ids[i]);
                    params.push(data);
                }
                tx.execute(
                    &format!(
                        "INSERT INTO objects (tenant_id, id, data) VALUES {}
                         {FRESHEN_ON_CONFLICT}",
                        values.join(", ")
                    ),
                    &params,
                )
                .await
                .map_err(|e| StorageError::Backend(format!("batch put objects: {e}")))?;
            }
            tx.commit()
                .await
                .map_err(|e| StorageError::Backend(format!("commit batch put: {e}")))?;
            Ok(())
        })?;
        Ok(ids)
    }

    /// One query for the whole batch, results in the order asked.
    fn batch_get_objects(&self, ids: &[ObjectId]) -> Result<Vec<Option<Object>>, StorageError> {
        if ids.is_empty() {
            return Ok(Vec::new());
        }
        let wanted: Vec<Vec<u8>> = ids.iter().map(|id| id.as_bytes().to_vec()).collect();
        let found = self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "SELECT id, data FROM objects WHERE tenant_id = $1 AND id = ANY($2)",
                    &[&self.tenant_id, &wanted],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("batch get objects: {e}")))?;
            let mut found = std::collections::HashMap::with_capacity(rows.len());
            for row in rows {
                let id: Vec<u8> = row.get("id");
                let data: serde_json::Value = row.get("data");
                let obj: Object = serde_json::from_value(data)
                    .map_err(|e| StorageError::Serialization(e.to_string()))?;
                found.insert(id, obj);
            }
            Ok(found)
        })?;
        Ok(wanted.iter().map(|id| found.get(id).cloned()).collect())
    }

    fn leaf_index_is_built(&self, namespace: &str, ref_name: &str) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            leaf_set_built(&client, &self.tenant_id, namespace, ref_name).await
        })
    }

    fn leaf_index_build(
        &self,
        namespace: &str,
        ref_name: &str,
        entries: &[(String, String)],
    ) -> Result<(), StorageError> {
        self.leaf_set_replace(namespace, ref_name, None, entries)
    }

    fn leaf_index_apply(
        &self,
        namespace: &str,
        ref_name: &str,
        removed_paths: &[String],
        added: &[(String, String)],
    ) -> Result<(), StorageError> {
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            let tx = client
                .transaction()
                .await
                .map_err(|e| leaf_error("apply", &e))?;
            // Only a built set is maintained; an unbuilt one is filled from
            // the current tree on its first search. The set no longer
            // reflects a known tree, so its root is cleared.
            let built = tx
                .execute(
                    "UPDATE leaf_index_refs SET root = NULL
                     WHERE tenant_id = $1 AND namespace = $2 AND ref = $3",
                    &[&self.tenant_id, &namespace, &ref_name],
                )
                .await
                .map_err(|e| leaf_error("apply check", &e))?
                > 0;
            if !built {
                return Ok(());
            }
            if !removed_paths.is_empty() {
                tx.execute(
                    "DELETE FROM leaf_rows
                     WHERE tenant_id = $1 AND namespace = $2 AND ref = $3 AND path = ANY($4)",
                    &[&self.tenant_id, &namespace, &ref_name, &removed_paths],
                )
                .await
                .map_err(|e| leaf_error("apply delete", &e))?;
            }
            upsert_leaves(&tx, &self.tenant_id, namespace, ref_name, added).await?;
            tx.commit()
                .await
                .map_err(|e| leaf_error("apply commit", &e))
        })
    }

    fn leaf_index_tracks_roots(&self) -> bool {
        true
    }

    fn leaf_index_root(
        &self,
        namespace: &str,
        ref_name: &str,
    ) -> Result<Option<ObjectId>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_opt(
                    "SELECT root FROM leaf_index_refs
                     WHERE tenant_id = $1 AND namespace = $2 AND ref = $3",
                    &[&self.tenant_id, &namespace, &ref_name],
                )
                .await
                .map_err(|e| leaf_error("root", &e))?;
            Ok(row.and_then(|row| {
                row.get::<_, Option<&[u8]>>(0)
                    .and_then(object_id_from_bytes)
            }))
        })
    }

    fn leaf_index_build_at(
        &self,
        namespace: &str,
        ref_name: &str,
        root: &ObjectId,
        entries: &[(String, String)],
    ) -> Result<(), StorageError> {
        self.leaf_set_replace(namespace, ref_name, Some(root), entries)
    }

    fn leaf_index_advance(
        &self,
        namespace: &str,
        ref_name: &str,
        from: &ObjectId,
        to: &ObjectId,
        removed_paths: &[String],
        added: &[(String, String)],
    ) -> Result<bool, StorageError> {
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            let tx = client
                .transaction()
                .await
                .map_err(|e| leaf_error("advance", &e))?;
            // Moving the root locks the set's row, so racing advances take
            // turns, and a loser finds the root already moved.
            let moved = tx
                .execute(
                    "UPDATE leaf_index_refs SET root = $5
                     WHERE tenant_id = $1 AND namespace = $2 AND ref = $3 AND root = $4",
                    &[
                        &self.tenant_id,
                        &namespace,
                        &ref_name,
                        &from.as_bytes().as_slice(),
                        &to.as_bytes().as_slice(),
                    ],
                )
                .await
                .map_err(|e| leaf_error("advance root", &e))?
                > 0;
            if !moved {
                return Ok(false);
            }
            if !removed_paths.is_empty() {
                tx.execute(
                    "DELETE FROM leaf_rows
                     WHERE tenant_id = $1 AND namespace = $2 AND ref = $3 AND path = ANY($4)",
                    &[&self.tenant_id, &namespace, &ref_name, &removed_paths],
                )
                .await
                .map_err(|e| leaf_error("advance delete", &e))?;
            }
            upsert_leaves(&tx, &self.tenant_id, namespace, ref_name, added).await?;
            tx.commit()
                .await
                .map_err(|e| leaf_error("advance commit", &e))?;
            Ok(true)
        })
    }

    /// Case-insensitive substring match. The trigram index serves it when
    /// pg_trgm is installed; `%`, `_` and `\` in the query match themselves.
    fn leaf_index_search(
        &self,
        namespace: &str,
        ref_name: &str,
        query_lower: &str,
        limit: usize,
    ) -> Result<Option<Vec<(String, String)>>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            if !leaf_set_built(&client, &self.tenant_id, namespace, ref_name).await? {
                return Ok(None);
            }
            let rows = client
                .query(
                    "SELECT path, value FROM leaf_rows
                     WHERE tenant_id = $1 AND namespace = $2 AND ref = $3 AND value ILIKE $4
                     LIMIT $5",
                    &[
                        &self.tenant_id,
                        &namespace,
                        &ref_name,
                        &like_contains(query_lower),
                        &sql_count(limit),
                    ],
                )
                .await
                .map_err(|e| leaf_error("search", &e))?;
            Ok(Some(
                rows.iter()
                    .map(|row| (row.get("path"), row.get("value")))
                    .collect(),
            ))
        })
    }

    fn has_object(&self, id: &ObjectId) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let row = client
                .query_one(
                    "SELECT EXISTS(SELECT 1 FROM objects WHERE tenant_id = $1 AND id = $2) as e",
                    &[&self.tenant_id, &id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("has object: {}", e)))?;

            Ok(row.get::<_, bool>("e"))
        })
    }
}

// ─── CommitStore ────────────────────────────────────────────

impl CommitStore for PostgresStorage {
    fn put_commit_traced(
        &self,
        commit: &Commit,
        trace: Option<&CommitTrace>,
    ) -> Result<(), StorageError> {
        let Some(trace) = trace else {
            return self.put_commit(commit);
        };
        let data =
            serde_json::to_value(commit).map_err(|e| StorageError::Serialization(e.to_string()))?;
        let timestamp = commit.timestamp;
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            // One statement, so one round trip.
            client
                .execute(
                    "WITH stored AS (
                         INSERT INTO commits (tenant_id, id, data, timestamp)
                         VALUES ($1, $2, $3, $4)
                         ON CONFLICT (tenant_id, id) DO NOTHING
                     )
                     INSERT INTO commit_traces (tenant_id, commit_id, trace_id, span_id)
                     VALUES ($1, $2, $5, $6)
                     ON CONFLICT (tenant_id, commit_id) DO NOTHING",
                    &[
                        &self.tenant_id,
                        &commit.id.as_bytes().as_slice(),
                        &data,
                        &timestamp,
                        &trace.trace_id,
                        &trace.span_id,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("put commit: {}", e)))?;
            Ok(())
        })
    }

    fn set_commit_trace(
        &self,
        commit_id: &ObjectId,
        trace: &CommitTrace,
    ) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            client
                .execute(
                    "INSERT INTO commit_traces (tenant_id, commit_id, trace_id, span_id)
                     VALUES ($1, $2, $3, $4)
                     ON CONFLICT (tenant_id, commit_id) DO NOTHING",
                    &[
                        &self.tenant_id,
                        &commit_id.as_bytes().as_slice(),
                        &trace.trace_id,
                        &trace.span_id,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("set commit trace: {}", e)))?;
            Ok(())
        })
    }

    fn get_commit_trace(&self, commit_id: &ObjectId) -> Result<Option<CommitTrace>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_opt(
                    "SELECT trace_id, span_id FROM commit_traces
                     WHERE tenant_id = $1 AND commit_id = $2",
                    &[&self.tenant_id, &commit_id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get commit trace: {}", e)))?;
            Ok(row.map(|row| CommitTrace {
                trace_id: row.get(0),
                span_id: row.get(1),
            }))
        })
    }

    fn all_commit_ids(&self) -> Result<Vec<ObjectId>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "SELECT id FROM commits WHERE tenant_id = $1",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("all commit ids: {}", e)))?;
            let mut ids = Vec::new();
            for row in rows {
                let bytes: Vec<u8> = row.get("id");
                if bytes.len() == 32 {
                    let mut arr = [0u8; 32];
                    arr.copy_from_slice(&bytes);
                    ids.push(ObjectId::from_bytes(arr));
                }
            }
            Ok(ids)
        })
    }

    fn get_commit(&self, id: &ObjectId) -> Result<Option<Commit>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let row = client
                .query_opt(
                    "SELECT data FROM commits WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get commit: {}", e)))?;

            match row {
                Some(row) => {
                    let data: serde_json::Value = row.get("data");
                    let commit: Commit = serde_json::from_value(data)
                        .map_err(|e| StorageError::Serialization(e.to_string()))?;
                    Ok(Some(commit))
                }
                None => Ok(None),
            }
        })
    }

    fn put_commit(&self, commit: &Commit) -> Result<(), StorageError> {
        let data =
            serde_json::to_value(commit).map_err(|e| StorageError::Serialization(e.to_string()))?;
        let timestamp = commit.timestamp;

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            client
                .execute(
                    "INSERT INTO commits (tenant_id, id, data, timestamp) VALUES ($1, $2, $3, $4)
                     ON CONFLICT (tenant_id, id) DO NOTHING",
                    &[
                        &self.tenant_id,
                        &commit.id.as_bytes().as_slice(),
                        &data,
                        &timestamp,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("put commit: {}", e)))?;

            Ok(())
        })
    }

    fn has_commit(&self, id: &ObjectId) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let row = client
                .query_one(
                    "SELECT EXISTS(SELECT 1 FROM commits WHERE tenant_id = $1 AND id = $2) as e",
                    &[&self.tenant_id, &id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("has commit: {}", e)))?;

            Ok(row.get::<_, bool>("e"))
        })
    }

    /// The first-parent line in one recursive query, instead of one round
    /// trip per commit.
    fn list_commits(&self, from: &ObjectId, limit: usize) -> Result<Vec<Commit>, StorageError> {
        if limit == 0 {
            return Ok(Vec::new());
        }
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "WITH RECURSIVE line(data, n) AS (
                         SELECT data, 1::BIGINT FROM commits WHERE tenant_id = $1 AND id = $2
                       UNION ALL
                         SELECT parent.data, line.n + 1
                         FROM line
                         CROSS JOIN LATERAL (
                             SELECT c.data FROM commits c
                             WHERE c.data -> 'id' = line.data -> 'parents' -> 0
                               AND c.tenant_id = $1
                             LIMIT 1
                         ) parent
                         WHERE line.n < $3
                     )
                     SELECT data FROM line ORDER BY n",
                    &[
                        &self.tenant_id,
                        &from.as_bytes().as_slice(),
                        &sql_count(limit),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(with_causes("list commits", &e)))?;
            rows.iter()
                .map(|row| commit_from_row(row, "data"))
                .collect()
        })
    }

    /// Every parent edge in one recursive query. `UNION` keeps each commit
    /// once however many paths reach it, so merge-heavy histories stay
    /// linear, and the walk stops once it has one commit more than `limit`
    /// (which is how truncation is detected). Commits come out breadth
    /// first; order within one generation is unspecified. Each parent is
    /// looked up through `LATERAL ... LIMIT 1`, which keeps the step an index
    /// lookup: given a plain join, the planner may hash the tenant's whole
    /// commit table instead.
    fn list_commits_dag(&self, from: &ObjectId, limit: usize) -> Result<CommitWalk, StorageError> {
        let mut commits = self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "WITH RECURSIVE walk(id, parents) AS (
                         SELECT id, data -> 'parents' FROM commits
                         WHERE tenant_id = $1 AND id = $2
                       UNION
                         SELECT parent.id, parent.parents
                         FROM walk
                         CROSS JOIN LATERAL jsonb_array_elements(walk.parents) AS p(id)
                         CROSS JOIN LATERAL (
                             SELECT c.id, c.data -> 'parents' AS parents FROM commits c
                             WHERE c.data -> 'id' = p.id AND c.tenant_id = $1
                             LIMIT 1
                         ) parent
                     ),
                     reached AS (
                         SELECT id, row_number() OVER () AS n
                         FROM (SELECT id FROM walk LIMIT $3) first
                     )
                     SELECT c.data
                     FROM reached JOIN commits c ON c.tenant_id = $1 AND c.id = reached.id
                     ORDER BY reached.n",
                    &[
                        &self.tenant_id,
                        &from.as_bytes().as_slice(),
                        &sql_count(limit).saturating_add(1),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(with_causes("list commits dag", &e)))?;
            rows.iter()
                .map(|row| commit_from_row(row, "data"))
                .collect::<Result<Vec<_>, _>>()
        })?;
        let truncated = commits.len() > limit;
        commits.truncate(limit);
        Ok(CommitWalk { commits, truncated })
    }

    /// Fold the next commits into the history tables, as the SQLite backend
    /// does, but ordered by the id of the transaction that wrote each commit
    /// rather than by insertion order. With several writers, a commit can
    /// become visible after a later-inserted one has already been folded, and
    /// an insertion-order cursor would step past it for good. Only commits
    /// whose transaction is older than every transaction still running are
    /// read, so no commit can appear behind the cursor later. When commits
    /// are waiting on an older transaction, this waits up to `HISTORY_WAIT`
    /// for it to finish; a writing transaction that runs longer than that,
    /// anywhere on the server, delays history until it ends.
    fn history_extract_batch(&self, batch_size: usize) -> Result<usize, StorageError> {
        let batch_size = sql_count(batch_size.max(1));
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            let tx = client
                .transaction()
                .await
                .map_err(|e| history_error("begin", &e))?;
            // The cursor row is the lock: one extractor per tenant at a time,
            // so concurrent extractors can't fold a commit twice.
            tx.execute(
                "INSERT INTO asg_history_cursor (tenant_id, xid, commit_id, folded)
                 VALUES ($1, 0, ''::BYTEA, 0)
                 ON CONFLICT (tenant_id) DO NOTHING",
                &[&self.tenant_id],
            )
            .await
            .map_err(|e| history_error("cursor", &e))?;
            let cursor = tx
                .query_one(
                    "SELECT xid, commit_id FROM asg_history_cursor
                     WHERE tenant_id = $1 FOR UPDATE",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| history_error("cursor", &e))?;
            let (after_xid, after_id): (i64, Vec<u8>) = (cursor.get(0), cursor.get(1));

            // A commit made outside a scoped session is attributed to
            // HISTORY_NAMESPACE_UNKNOWN, as in SQLite.
            let deadline = tokio::time::Instant::now() + HISTORY_WAIT;
            let rows = loop {
                let rows = tx
                    .query(
                        "SELECT c.xid, c.id, c.data, COALESCE(s.scope_namespace, $4) AS namespace
                         FROM commits c
                         LEFT JOIN sessions s
                           ON s.tenant_id = c.tenant_id AND s.id = c.session_id
                         WHERE c.tenant_id = $1
                           AND (c.xid, c.id) > ($2, $3)
                           AND c.xid < pg_snapshot_xmin(pg_current_snapshot())::text::BIGINT
                         ORDER BY c.xid, c.id
                         LIMIT $5",
                        &[
                            &self.tenant_id,
                            &after_xid,
                            &after_id,
                            &HISTORY_NAMESPACE_UNKNOWN,
                            &batch_size,
                        ],
                    )
                    .await
                    .map_err(|e| history_error("select", &e))?;
                if !rows.is_empty() {
                    break rows;
                }
                // Nothing is old enough. If newer commits are waiting on an
                // older transaction, give it a moment to finish.
                let waiting: bool = tx
                    .query_one(
                        "SELECT EXISTS (SELECT 1 FROM commits
                                        WHERE tenant_id = $1 AND (xid, id) > ($2, $3))",
                        &[&self.tenant_id, &after_xid, &after_id],
                    )
                    .await
                    .map_err(|e| history_error("pending", &e))?
                    .get(0);
                if !waiting || tokio::time::Instant::now() >= deadline {
                    return Ok(0);
                }
                tokio::time::sleep(std::time::Duration::from_millis(5)).await;
            };
            let last = rows.last().expect("a non-empty batch");
            let (last_xid, last_id): (i64, Vec<u8>) = (last.get("xid"), last.get("id"));

            let mut fold = HistoryFold::default();
            for row in &rows {
                let commit = commit_from_row(row, "data")?;
                fold.add(&commit, row.get("namespace"));
            }
            fold.write(&tx, &self.tenant_id).await?;

            // The cursor moves in the same transaction as the rows it covers.
            tx.execute(
                "UPDATE asg_history_cursor SET xid = $2, commit_id = $3, folded = folded + $4
                 WHERE tenant_id = $1",
                &[&self.tenant_id, &last_xid, &last_id, &(rows.len() as i64)],
            )
            .await
            .map_err(|e| history_error("cursor write", &e))?;
            tx.commit().await.map_err(|e| history_error("commit", &e))?;
            Ok(rows.len())
        })
    }

    /// How many commits have been folded. (SQLite reports its last folded
    /// rowid; either way it only grows, and 0 means nothing is extracted.)
    fn history_cursor(&self) -> Result<i64, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_opt(
                    "SELECT folded FROM asg_history_cursor WHERE tenant_id = $1",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| history_error("cursor read", &e))?;
            Ok(row.map_or(0, |row| row.get(0)))
        })
    }

    fn history_undistilled_commit_count(&self) -> Result<i64, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_one(
                    "SELECT COUNT(*) FROM commits c
                     LEFT JOIN asg_history_cursor k ON k.tenant_id = c.tenant_id
                     WHERE c.tenant_id = $1
                       AND (c.xid, c.id) > (COALESCE(k.xid, 0), COALESCE(k.commit_id, ''::BYTEA))",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| history_error("undistilled count", &e))?;
            Ok(row.get(0))
        })
    }

    fn history_is_commit_distilled(&self, id: &ObjectId) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_one(
                    "SELECT EXISTS (
                         SELECT 1 FROM commits c
                         JOIN asg_history_cursor k ON k.tenant_id = c.tenant_id
                         WHERE c.tenant_id = $1 AND c.id = $2
                           AND (c.xid, c.id) <= (k.xid, k.commit_id)
                     )",
                    &[&self.tenant_id, &id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| history_error("distilled", &e))?;
            Ok(row.get(0))
        })
    }

    fn history_rollup(&self) -> Result<Vec<HistoryRollupRow>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "SELECT day, namespace, agent_id, intent_category, commit_count,
                            first_ts, last_ts
                     FROM asg_history_commit_rollup
                     WHERE tenant_id = $1
                     ORDER BY day, namespace, agent_id, intent_category",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| history_error("rollup read", &e))?;
            Ok(rows
                .iter()
                .map(|row| HistoryRollupRow {
                    day: row.get(0),
                    namespace: row.get(1),
                    agent_id: row.get(2),
                    intent_category: row.get(3),
                    commit_count: row.get(4),
                    first_ts: row.get(5),
                    last_ts: row.get(6),
                })
                .collect())
        })
    }

    fn history_milestones(&self, limit: usize) -> Result<Vec<HistoryMilestoneRow>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "SELECT commit_id, kind, timestamp, day, namespace, agent_id, description,
                            state_root, git_sha
                     FROM asg_history_milestone
                     WHERE tenant_id = $1
                     ORDER BY timestamp DESC
                     LIMIT $2",
                    &[&self.tenant_id, &sql_count(limit)],
                )
                .await
                .map_err(|e| history_error("milestones read", &e))?;
            Ok(rows
                .iter()
                .filter_map(|row| {
                    let commit_id = object_id_from_bytes(row.get(0))?;
                    let state_root: Option<&[u8]> = row.get(7);
                    Some(HistoryMilestoneRow {
                        commit_id,
                        kind: row.get(1),
                        timestamp: row.get(2),
                        day: row.get(3),
                        namespace: row.get(4),
                        agent_id: row.get(5),
                        description: row.get(6),
                        state_root: state_root.and_then(object_id_from_bytes),
                        git_sha: row.get(8),
                    })
                })
                .collect())
        })
    }

    fn history_retained_state_roots(&self) -> Result<Vec<ObjectId>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "SELECT DISTINCT state_root FROM asg_history_milestone
                     WHERE tenant_id = $1 AND state_root IS NOT NULL",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| history_error("retained roots", &e))?;
            Ok(rows
                .iter()
                .filter_map(|row| object_id_from_bytes(row.get(0)))
                .collect())
        })
    }

    fn history_gc_reachability(
        &self,
        roots: &[ObjectId],
        batch: usize,
    ) -> Result<GcReachability, StorageError> {
        let batch = sql_count(batch.max(1));
        self.block_on(async {
            // The mark lives in temp tables, so it runs on a connection of
            // its own that is closed afterwards rather than pooled.
            let client = self.pool.get().await.map_err(conn_error)?;
            let result = async {
                gc_mark(&client, &self.tenant_id, roots, batch).await?;
                let row = client
                    .query_one(
                        "SELECT (SELECT count(*) FROM gc_mark),
                                (SELECT count(*) FROM objects WHERE tenant_id = $1)",
                        &[&self.tenant_id],
                    )
                    .await
                    .map_err(|e| gc_error("counts", &e))?;
                Ok::<_, StorageError>((row.get::<_, i64>(0), row.get::<_, i64>(1)))
            }
            .await;
            drop(deadpool_postgres::Object::take(client.into_inner()));
            let (live, total) = result?;
            Ok(GcReachability {
                total_objects: total,
                live_objects: live,
                reclaimable_objects: (total - live).max(0),
                roots_walked: roots.len(),
            })
        })
    }

    /// Mark everything reachable from `roots`, the current ref tips and the
    /// pinned roots, then delete this tenant's other objects in batches, each
    /// its own transaction. Writers keep going meanwhile: whatever was written
    /// since the sweep started, or within the grace period before it, is
    /// spared, and each delete re-checks the write time, so an object stored
    /// again mid-sweep stays. `deleted_target` can therefore exceed
    /// `objects_deleted`.
    fn history_gc_sweep(&self, roots: &[ObjectId], batch: usize) -> Result<GcSweep, StorageError> {
        let batch = sql_count(batch.max(1));
        let held = self
            .gc_conn
            .lock()
            .map_err(|e| StorageError::Backend(e.to_string()))?
            .take();
        self.block_on(async {
            let (client, own) = match held {
                Some(client) => (client, false),
                None => (self.gc_lock().await?, true),
            };
            let result = gc_sweep(
                &client,
                &self.tenant_id,
                roots,
                batch,
                self.gc_grace.as_secs_f64(),
            )
            .await;
            if own {
                gc_release(client).await;
            } else {
                *self
                    .gc_conn
                    .lock()
                    .map_err(|e| StorageError::Backend(e.to_string()))? = Some(client);
            }
            result
        })
    }

    /// Take this tenant's sweep lock, so two sweeps can't run at once. Unlike
    /// SQLite's, it doesn't hold writers off: the grace period keeps their
    /// work safe (see [`PostgresStorage::with_gc_grace`]).
    fn history_gc_lock_writers(&self) -> Result<(), StorageError> {
        let mut held = self
            .gc_conn
            .lock()
            .map_err(|e| StorageError::Backend(e.to_string()))?;
        if held.is_some() {
            return Err(StorageError::Backend(
                "gc lock writers: a sweep already holds the lock".into(),
            ));
        }
        *held = Some(self.block_on(self.gc_lock())?);
        Ok(())
    }

    /// Release the sweep lock. A sweep's deletes are already committed batch
    /// by batch, so `commit` changes nothing here.
    fn history_gc_unlock_writers(&self, _commit: bool) -> Result<(), StorageError> {
        let held = self
            .gc_conn
            .lock()
            .map_err(|e| StorageError::Backend(e.to_string()))?
            .take();
        if let Some(client) = held {
            self.block_on(async {
                gc_release(client).await;
                Ok(())
            })?;
        }
        Ok(())
    }

    fn gc_pin_root(&self, key: &str, root: &ObjectId) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            client
                .execute(
                    "INSERT INTO gc_pending_roots (tenant_id, key, root, updated_at)
                     VALUES ($1, $2, $3, now())
                     ON CONFLICT (tenant_id, key)
                     DO UPDATE SET root = EXCLUDED.root, updated_at = now()",
                    &[&self.tenant_id, &key, &root.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| gc_error("pin root", &e))?;
            Ok(())
        })
    }

    fn gc_unpin_root(&self, key: &str) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            client
                .execute(
                    "DELETE FROM gc_pending_roots WHERE tenant_id = $1 AND key = $2",
                    &[&self.tenant_id, &key],
                )
                .await
                .map_err(|e| gc_error("unpin root", &e))?;
            Ok(())
        })
    }

    fn gc_pinned_roots(&self, max_age: std::time::Duration) -> Result<Vec<ObjectId>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            gc_pinned(&client, &self.tenant_id, max_age.as_secs_f64()).await
        })
    }

    /// Newest first, by the order the commits' transactions got ids (commits
    /// from before that was recorded come last, newest first by timestamp).
    fn commit_state_roots_recent_first(&self) -> Result<Vec<ObjectId>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let rows = client
                .query(
                    "SELECT data -> 'state_root' FROM commits WHERE tenant_id = $1
                     ORDER BY xid DESC, timestamp DESC, id DESC",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| gc_error("commit roots", &e))?;
            rows.iter()
                .map(|row| {
                    serde_json::from_value(row.get(0))
                        .map_err(|e| StorageError::Serialization(e.to_string()))
                })
                .collect()
        })
    }

    /// This tenant's object and commit counts, and the bytes its rows take
    /// in each table (before indexes and free space; the tables are shared
    /// with other tenants). Reads every row, so it is slow on a large store.
    /// This tenant's objects and commits. The tables are shared by tenants,
    /// so there is no size of the tenant's own to report.
    fn store_totals(&self) -> Result<Option<StoreTotals>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_one(
                    "SELECT (SELECT count(*) FROM objects WHERE tenant_id = $1),
                            (SELECT count(*) FROM commits WHERE tenant_id = $1)",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| gc_error("store totals", &e))?;
            Ok(Some(StoreTotals {
                objects: row.get(0),
                commits: row.get(1),
                bytes: None,
            }))
        })
    }

    fn history_store_shape(&self) -> Result<StoreShape, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_one(
                    "SELECT (SELECT count(*) FROM objects WHERE tenant_id = $1),
                            (SELECT count(*) FROM commits WHERE tenant_id = $1),
                            (SELECT COALESCE(sum(pg_column_size(o.*)), 0)::BIGINT
                               FROM objects o WHERE tenant_id = $1),
                            (SELECT COALESCE(sum(pg_column_size(c.*)), 0)::BIGINT
                               FROM commits c WHERE tenant_id = $1)",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| gc_error("store shape", &e))?;
            let (objects_bytes, commits_bytes): (i64, i64) = (row.get(2), row.get(3));
            let mut tables = vec![
                TableBytes {
                    name: "objects".into(),
                    bytes: objects_bytes,
                },
                TableBytes {
                    name: "commits".into(),
                    bytes: commits_bytes,
                },
            ];
            tables.sort_by_key(|t| std::cmp::Reverse(t.bytes));
            Ok(StoreShape {
                objects: row.get(0),
                commits: row.get(1),
                total_bytes: objects_bytes + commits_bytes,
                tables,
                dbstat_available: true,
            })
        })
    }
}

impl PostgresStorage {
    /// A connection holding this tenant's sweep lock (a session advisory
    /// lock). Fails at once if another sweep holds it. Closing the connection
    /// releases the lock, so it is never returned to the pool.
    async fn gc_lock(&self) -> Result<PgClient, StorageError> {
        let client = self.pool.get().await.map_err(conn_error)?;
        let locked: bool = client
            .query_one(
                "SELECT pg_try_advisory_lock(hashtextextended('asg-gc:' || $1, 0))",
                &[&self.tenant_id],
            )
            .await
            .map_err(|e| gc_error("lock", &e))?
            .get(0);
        if !locked {
            return Err(StorageError::Backend(
                "gc lock writers: another sweep is running on this store".into(),
            ));
        }
        Ok(client)
    }
}

/// Release a sweep connection: unlock, then close it rather than pool it, so
/// its temp tables go too. The explicit unlock matters: the server only
/// notices a closed connection a moment later, and a sweep starting in that
/// moment would find the lock still held. (If the unlock fails, the
/// connection is broken and the server releases the lock with it.)
async fn gc_release(client: PgClient) {
    let _ = client
        .batch_execute("SELECT pg_advisory_unlock_all()")
        .await;
    drop(deadpool_postgres::Object::take(client.into_inner()));
}

/// Fill the temp table `gc_mark` with every object of `tenant_id` reachable
/// from `roots`, breadth first in batches of `batch`, so the mark set lives in
/// the database rather than in memory. Each statement commits on its own: a
/// long transaction would hold back every tenant's history extraction.
async fn gc_mark(
    client: &PgClient,
    tenant_id: &str,
    roots: &[ObjectId],
    batch: i64,
) -> Result<(), StorageError> {
    client
        .batch_execute(
            "CREATE TEMP TABLE IF NOT EXISTS gc_mark (id BYTEA PRIMARY KEY);
             CREATE TEMP TABLE IF NOT EXISTS gc_frontier (id BYTEA PRIMARY KEY);
             TRUNCATE gc_mark, gc_frontier;",
        )
        .await
        .map_err(|e| gc_error("mark setup", &e))?;
    // Marks only ids that are stored objects, so the mark is a subset of the
    // tenant's objects; each newly marked id joins the frontier.
    let mark = "WITH marked AS (
                    INSERT INTO gc_mark (id)
                    SELECT o.id FROM objects o WHERE o.tenant_id = $1 AND o.id = ANY($2)
                    ON CONFLICT DO NOTHING
                    RETURNING id
                )
                INSERT INTO gc_frontier (id) SELECT id FROM marked";
    let seeds: Vec<&[u8]> = roots.iter().map(|r| r.as_bytes().as_slice()).collect();
    client
        .execute(mark, &[&tenant_id, &seeds])
        .await
        .map_err(|e| gc_error("mark roots", &e))?;
    loop {
        let rows = client
            .query(
                "WITH next AS (
                     DELETE FROM gc_frontier
                     WHERE id IN (SELECT id FROM gc_frontier LIMIT $2)
                     RETURNING id
                 )
                 SELECT o.data FROM next
                 LEFT JOIN objects o ON o.tenant_id = $1 AND o.id = next.id",
                &[&tenant_id, &batch],
            )
            .await
            .map_err(|e| gc_error("mark frontier", &e))?;
        if rows.is_empty() {
            return Ok(());
        }
        let mut children = std::collections::HashSet::new();
        for row in &rows {
            let Some(data) = row.get::<_, Option<serde_json::Value>>(0) else {
                continue;
            };
            let obj: Object = serde_json::from_value(data)
                .map_err(|e| StorageError::Serialization(e.to_string()))?;
            children.extend(child_ids(&obj));
        }
        let children: Vec<&[u8]> = children.iter().map(|c| c.as_bytes().as_slice()).collect();
        client
            .execute(mark, &[&tenant_id, &children])
            .await
            .map_err(|e| gc_error("mark children", &e))?;
    }
}

/// The body of [`PostgresStorage::history_gc_sweep`], on a connection that
/// holds the tenant's sweep lock.
async fn gc_sweep(
    client: &PgClient,
    tenant_id: &str,
    roots: &[ObjectId],
    batch: i64,
    grace_secs: f64,
) -> Result<GcSweep, StorageError> {
    // Fixed before anything is read: everything written after this time, or
    // within the grace period before it, is spared.
    let cutoff: DateTime<Utc> = client
        .query_one("SELECT now() - make_interval(secs => $1)", &[&grace_secs])
        .await
        .map_err(|e| gc_error("cutoff", &e))?
        .get(0);
    let count = || async {
        client
            .query_one(
                "SELECT count(*) FROM objects WHERE tenant_id = $1",
                &[&tenant_id],
            )
            .await
            .map(|row| row.get::<_, i64>(0))
            .map_err(|e| gc_error("count", &e))
    };
    let objects_before = count().await?;

    // Whatever the caller's roots, keep every current ref tip and every
    // open speculation.
    let mut keep = roots.to_vec();
    let tips = client
        .query(
            "SELECT c.data -> 'state_root' FROM refs r
             JOIN commits c ON c.tenant_id = r.tenant_id AND c.id = r.target
             WHERE r.tenant_id = $1",
            &[&tenant_id],
        )
        .await
        .map_err(|e| gc_error("ref tips", &e))?;
    for row in &tips {
        keep.push(
            serde_json::from_value(row.get(0))
                .map_err(|e| StorageError::Serialization(e.to_string()))?,
        );
    }
    keep.extend(gc_pinned(client, tenant_id, GC_PIN_MAX_AGE.as_secs_f64()).await?);

    gc_mark(client, tenant_id, &keep, batch).await?;

    // Walk the tenant's objects in key order, a batch of unmarked ones at a
    // time, and delete each batch on its own. Each statement is short, so no
    // long write transaction holds back history extraction (every tenant's).
    // The delete re-checks the write time: an object stored again since the
    // batch was read stays.
    let mut after: Vec<u8> = Vec::new();
    let (mut deleted_target, mut objects_deleted) = (0, 0);
    loop {
        let dead: Vec<Vec<u8>> = client
            .query(
                "SELECT o.id FROM objects o
                 WHERE o.tenant_id = $1 AND o.id > $2 AND o.written_at < $3
                   AND NOT EXISTS (SELECT 1 FROM gc_mark m WHERE m.id = o.id)
                 ORDER BY o.id
                 LIMIT $4",
                &[&tenant_id, &after, &cutoff, &batch],
            )
            .await
            .map_err(|e| gc_error("dead batch", &e))?
            .iter()
            .map(|row| row.get(0))
            .collect();
        let Some(last) = dead.last() else {
            break;
        };
        after = last.clone();
        deleted_target += dead.len() as i64;
        objects_deleted += client
            .execute(
                "DELETE FROM objects
                 WHERE tenant_id = $1 AND id = ANY($2) AND written_at < $3",
                &[&tenant_id, &dead, &cutoff],
            )
            .await
            .map_err(|e| gc_error("delete", &e))? as i64;
    }

    Ok(GcSweep {
        objects_before,
        objects_deleted,
        objects_after: count().await?,
        deleted_target,
    })
}

async fn gc_pinned(
    client: &impl GenericClient,
    tenant_id: &str,
    max_age_secs: f64,
) -> Result<Vec<ObjectId>, StorageError> {
    let rows = client
        .query(
            "SELECT root FROM gc_pending_roots
             WHERE tenant_id = $1 AND updated_at >= now() - make_interval(secs => $2)",
            &[&tenant_id, &max_age_secs],
        )
        .await
        .map_err(|e| gc_error("pinned roots", &e))?;
    Ok(rows
        .iter()
        .filter_map(|row| object_id_from_bytes(row.get(0)))
        .collect())
}

/// The child object ids a node references (none for atoms): the edges the
/// GC mark follows.
fn child_ids(obj: &Object) -> Vec<ObjectId> {
    match obj {
        Object::Node(Node::Map(entries)) => entries.values().copied().collect(),
        Object::Node(Node::List(items)) | Object::Node(Node::Set(items)) => items.clone(),
        Object::Atom(_) => Vec::new(),
    }
}

/// How long an open speculation's pin protects its objects without being
/// refreshed, as in SQLite.
const GC_PIN_MAX_AGE: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60);

fn gc_error(step: &str, e: &tokio_postgres::Error) -> StorageError {
    StorageError::Backend(with_causes(&format!("gc {step}"), e))
}

async fn leaf_set_built(
    client: &impl GenericClient,
    tenant_id: &str,
    namespace: &str,
    ref_name: &str,
) -> Result<bool, StorageError> {
    let row = client
        .query_one(
            "SELECT EXISTS (SELECT 1 FROM leaf_index_refs
                            WHERE tenant_id = $1 AND namespace = $2 AND ref = $3)",
            &[&tenant_id, &namespace, &ref_name],
        )
        .await
        .map_err(|e| leaf_error("built check", &e))?;
    Ok(row.get(0))
}

/// Write `(path, value)` leaves, replacing any value already at a path. A
/// path given twice keeps its last value.
async fn upsert_leaves(
    tx: &PgTransaction<'_>,
    tenant_id: &str,
    namespace: &str,
    ref_name: &str,
    leaves: &[(String, String)],
) -> Result<(), StorageError> {
    const CHUNK: usize = 1000;
    let latest: std::collections::HashMap<&str, &str> = leaves
        .iter()
        .map(|(path, value)| (path.as_str(), value.as_str()))
        .collect();
    let latest: Vec<(&str, &str)> = latest.into_iter().collect();
    for chunk in latest.chunks(CHUNK) {
        let paths: Vec<&str> = chunk.iter().map(|(p, _)| *p).collect();
        let values: Vec<&str> = chunk.iter().map(|(_, v)| *v).collect();
        tx.execute(
            "INSERT INTO leaf_rows (tenant_id, namespace, ref, path, value)
             SELECT $1, $2, $3, l.path, l.value FROM unnest($4::TEXT[], $5::TEXT[]) AS l(path, value)
             ON CONFLICT (tenant_id, namespace, ref, path) DO UPDATE SET value = EXCLUDED.value",
            &[&tenant_id, &namespace, &ref_name, &paths, &values],
        )
        .await
        .map_err(|e| leaf_error("write", &e))?;
    }
    Ok(())
}

/// An `ILIKE` pattern matching values that contain `needle` literally.
fn like_contains(needle: &str) -> String {
    let mut pattern = String::with_capacity(needle.len() + 2);
    pattern.push('%');
    for ch in needle.chars() {
        if matches!(ch, '%' | '_' | '\\') {
            pattern.push('\\');
        }
        pattern.push(ch);
    }
    pattern.push('%');
    pattern
}

fn leaf_error(step: &str, e: &tokio_postgres::Error) -> StorageError {
    StorageError::Backend(with_causes(&format!("leaf index {step}"), e))
}

/// How long the history extractor waits for an older transaction that newer
/// commits are waiting on. Writes take milliseconds; this only runs out when
/// some write transaction on the server stays open.
const HISTORY_WAIT: std::time::Duration = std::time::Duration::from_secs(2);

/// A `usize` count or limit as the `BIGINT` Postgres compares it with.
fn sql_count(n: usize) -> i64 {
    i64::try_from(n).unwrap_or(i64::MAX)
}

fn object_id_from_bytes(bytes: &[u8]) -> Option<ObjectId> {
    <[u8; 32]>::try_from(bytes).ok().map(ObjectId::from_bytes)
}

fn commit_from_row(row: &tokio_postgres::Row, column: &str) -> Result<Commit, StorageError> {
    let data: serde_json::Value = row.get(column);
    serde_json::from_value(data).map_err(|e| StorageError::Serialization(e.to_string()))
}

fn history_error(step: &str, e: &tokio_postgres::Error) -> StorageError {
    StorageError::Backend(with_causes(&format!("history {step}"), e))
}

/// A rollup bucket's key: (day, namespace, agent, intent category).
type BucketKey = (String, String, String, String);

/// A rollup bucket's commit count and first and last timestamps.
type Bucket = (i64, String, String);

/// One batch of commits folded into rollup buckets and milestones, written
/// with one statement each.
#[derive(Default)]
struct HistoryFold {
    rollup: std::collections::BTreeMap<BucketKey, Bucket>,
    milestones: Vec<Milestone>,
}

struct Milestone {
    commit_id: Vec<u8>,
    timestamp: String,
    day: String,
    namespace: String,
    agent_id: String,
    description: String,
    state_root: Option<Vec<u8>>,
    git_sha: Option<String>,
}

impl HistoryFold {
    /// The same fields, bucketing and milestone rules as SQLite's extractor.
    fn add(&mut self, commit: &Commit, namespace: String) {
        let day = commit.timestamp.format("%Y-%m-%d").to_string();
        let ts = commit.timestamp.to_rfc3339();
        let key = (
            day.clone(),
            namespace.clone(),
            commit.agent_id.clone(),
            format!("{:?}", commit.intent.category),
        );
        let bucket = self
            .rollup
            .entry(key)
            .or_insert_with(|| (0, ts.clone(), ts.clone()));
        bucket.0 += 1;
        if ts < bucket.1 {
            bucket.1 = ts.clone();
        }
        if ts > bucket.2 {
            bucket.2 = ts.clone();
        }

        if commit.intent.category == IntentCategory::Checkpoint {
            // Pin the snapshot only when the checkpoint asks to (see SQLite).
            let state_root = commit
                .intent
                .tags
                .iter()
                .any(|t| t == TAG_PIN_STATE)
                .then(|| commit.state_root.as_bytes().to_vec());
            let git_sha = commit
                .intent
                .tags
                .iter()
                .find_map(|t| t.strip_prefix(TAG_GIT_REVISION))
                .filter(|sha| !sha.is_empty())
                .map(str::to_string);
            self.milestones.push(Milestone {
                commit_id: commit.id.as_bytes().to_vec(),
                timestamp: ts,
                day,
                namespace,
                agent_id: commit.agent_id.clone(),
                description: commit.intent.description.clone(),
                state_root,
                git_sha,
            });
        }
    }

    async fn write(self, tx: &PgTransaction<'_>, tenant_id: &str) -> Result<(), StorageError> {
        let mut days = Vec::new();
        let mut namespaces = Vec::new();
        let mut agents = Vec::new();
        let mut categories = Vec::new();
        let mut counts = Vec::new();
        let mut firsts = Vec::new();
        let mut lasts = Vec::new();
        for ((day, namespace, agent, category), (count, first, last)) in self.rollup {
            days.push(day);
            namespaces.push(namespace);
            agents.push(agent);
            categories.push(category);
            counts.push(count);
            firsts.push(first);
            lasts.push(last);
        }
        tx.execute(
            "INSERT INTO asg_history_commit_rollup AS r
                 (tenant_id, day, namespace, agent_id, intent_category, commit_count,
                  first_ts, last_ts)
             SELECT $1, * FROM unnest($2::TEXT[], $3::TEXT[], $4::TEXT[], $5::TEXT[],
                                      $6::BIGINT[], $7::TEXT[], $8::TEXT[])
             ON CONFLICT (tenant_id, day, namespace, agent_id, intent_category) DO UPDATE SET
                 commit_count = r.commit_count + EXCLUDED.commit_count,
                 first_ts = LEAST(r.first_ts, EXCLUDED.first_ts),
                 last_ts = GREATEST(r.last_ts, EXCLUDED.last_ts)",
            &[
                &tenant_id,
                &days,
                &namespaces,
                &agents,
                &categories,
                &counts,
                &firsts,
                &lasts,
            ],
        )
        .await
        .map_err(|e| history_error("rollup upsert", &e))?;

        if self.milestones.is_empty() {
            return Ok(());
        }
        let m = &self.milestones;
        let ids: Vec<&[u8]> = m.iter().map(|m| m.commit_id.as_slice()).collect();
        let timestamps: Vec<&str> = m.iter().map(|m| m.timestamp.as_str()).collect();
        let days: Vec<&str> = m.iter().map(|m| m.day.as_str()).collect();
        let namespaces: Vec<&str> = m.iter().map(|m| m.namespace.as_str()).collect();
        let agents: Vec<&str> = m.iter().map(|m| m.agent_id.as_str()).collect();
        let descriptions: Vec<&str> = m.iter().map(|m| m.description.as_str()).collect();
        let roots: Vec<Option<&[u8]>> = m.iter().map(|m| m.state_root.as_deref()).collect();
        let shas: Vec<Option<&str>> = m.iter().map(|m| m.git_sha.as_deref()).collect();
        tx.execute(
            "INSERT INTO asg_history_milestone
                 (tenant_id, commit_id, kind, timestamp, day, namespace, agent_id,
                  description, state_root, git_sha)
             SELECT $1, m.id, 'checkpoint', m.ts, m.day, m.ns, m.agent, m.description,
                    m.root, m.sha
             FROM unnest($2::BYTEA[], $3::TEXT[], $4::TEXT[], $5::TEXT[], $6::TEXT[],
                         $7::TEXT[], $8::BYTEA[], $9::TEXT[])
                  AS m(id, ts, day, ns, agent, description, root, sha)
             ON CONFLICT DO NOTHING",
            &[
                &tenant_id,
                &ids,
                &timestamps,
                &days,
                &namespaces,
                &agents,
                &descriptions,
                &roots,
                &shas,
            ],
        )
        .await
        .map_err(|e| history_error("milestones", &e))?;
        Ok(())
    }
}

// ─── RefStore ───────────────────────────────────────────────

impl RefStore for PostgresStorage {
    fn create_namespace(&self, namespace: &Namespace) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let rows = client
                .execute(
                    "INSERT INTO namespaces (tenant_id, name, created_at)
                     VALUES ($1, $2, NOW()::TEXT)
                     ON CONFLICT DO NOTHING",
                    &[&self.tenant_id, &namespace.as_str()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("create namespace: {}", e)))?;
            if rows == 0 {
                return Err(StorageError::NamespaceAlreadyExists(
                    namespace.as_str().to_string(),
                ));
            }
            Ok(())
        })
    }

    fn list_namespaces(&self) -> Result<Vec<Namespace>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let rows = client
                .query(
                    "SELECT name FROM namespaces WHERE tenant_id = $1 ORDER BY name",
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("list namespaces: {}", e)))?;

            let mut result = Vec::new();
            for row in rows {
                let name: String = row.get("name");
                let ns = Namespace::new(name).map_err(|e| {
                    StorageError::Backend(format!("invalid namespace in db: {}", e))
                })?;
                result.push(ns);
            }
            Ok(result)
        })
    }

    fn get_ref(&self, namespace: &Namespace, name: &str) -> Result<Option<ObjectId>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let ns_row = client
                .query_opt(
                    "SELECT 1 FROM namespaces WHERE tenant_id = $1 AND name = $2",
                    &[&self.tenant_id, &namespace.as_str()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("check namespace: {}", e)))?;
            if ns_row.is_none() {
                return Err(StorageError::NamespaceNotFound(
                    namespace.as_str().to_string(),
                ));
            }

            let row = client
                .query_opt(
                    "SELECT target FROM refs
                     WHERE tenant_id = $1 AND namespace = $2 AND name = $3",
                    &[&self.tenant_id, &namespace.as_str(), &name],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get ref: {}", e)))?;

            match row {
                Some(row) => {
                    let bytes: Vec<u8> = row.get("target");
                    let mut arr = [0u8; 32];
                    if bytes.len() == 32 {
                        arr.copy_from_slice(&bytes);
                    }
                    Ok(Some(ObjectId::from_bytes(arr)))
                }
                None => Ok(None),
            }
        })
    }

    fn set_ref(
        &self,
        namespace: &Namespace,
        name: &str,
        target: ObjectId,
    ) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let ns_row = client
                .query_opt(
                    "SELECT 1 FROM namespaces WHERE tenant_id = $1 AND name = $2",
                    &[&self.tenant_id, &namespace.as_str()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("check namespace: {}", e)))?;
            if ns_row.is_none() {
                return Err(StorageError::NamespaceNotFound(
                    namespace.as_str().to_string(),
                ));
            }

            client
                .execute(
                    "INSERT INTO refs (tenant_id, namespace, name, target)
                     VALUES ($1, $2, $3, $4)
                     ON CONFLICT (tenant_id, namespace, name) DO UPDATE SET target = $4",
                    &[
                        &self.tenant_id,
                        &namespace.as_str(),
                        &name,
                        &target.as_bytes().as_slice(),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("set ref: {}", e)))?;

            Ok(())
        })
    }

    fn cas_ref(
        &self,
        namespace: &Namespace,
        name: &str,
        expected: ObjectId,
        new: ObjectId,
    ) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let ns_row = client
                .query_opt(
                    "SELECT 1 FROM namespaces WHERE tenant_id = $1 AND name = $2",
                    &[&self.tenant_id, &namespace.as_str()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("check namespace: {}", e)))?;
            if ns_row.is_none() {
                return Err(StorageError::NamespaceNotFound(
                    namespace.as_str().to_string(),
                ));
            }

            let rows = client
                .execute(
                    "UPDATE refs SET target = $1
                     WHERE tenant_id = $2 AND namespace = $3
                       AND name = $4 AND target = $5",
                    &[
                        &new.as_bytes().as_slice(),
                        &self.tenant_id,
                        &namespace.as_str(),
                        &name,
                        &expected.as_bytes().as_slice(),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("cas ref: {}", e)))?;

            Ok(rows > 0)
        })
    }

    fn create_ref(
        &self,
        namespace: &Namespace,
        name: &str,
        target: ObjectId,
    ) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            self.require_namespace(&client, namespace).await?;
            let rows = client
                .execute(
                    "INSERT INTO refs (tenant_id, namespace, name, target)
                     VALUES ($1, $2, $3, $4)
                     ON CONFLICT DO NOTHING",
                    &[
                        &self.tenant_id,
                        &namespace.as_str(),
                        &name,
                        &target.as_bytes().as_slice(),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("create ref: {}", e)))?;
            Ok(rows > 0)
        })
    }

    fn cas_delete_ref(
        &self,
        namespace: &Namespace,
        name: &str,
        expected: ObjectId,
    ) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            self.require_namespace(&client, namespace).await?;
            let rows = client
                .execute(
                    "DELETE FROM refs
                     WHERE tenant_id = $1 AND namespace = $2
                       AND name = $3 AND target = $4",
                    &[
                        &self.tenant_id,
                        &namespace.as_str(),
                        &name,
                        &expected.as_bytes().as_slice(),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("cas delete ref: {}", e)))?;
            Ok(rows > 0)
        })
    }

    fn list_refs(
        &self,
        namespace: &Namespace,
        prefix: &str,
    ) -> Result<Vec<(String, ObjectId)>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let ns_row = client
                .query_opt(
                    "SELECT 1 FROM namespaces WHERE tenant_id = $1 AND name = $2",
                    &[&self.tenant_id, &namespace.as_str()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("check namespace: {}", e)))?;
            if ns_row.is_none() {
                return Err(StorageError::NamespaceNotFound(
                    namespace.as_str().to_string(),
                ));
            }

            let pattern = format!("{}%", prefix);
            let rows = client
                .query(
                    "SELECT name, target FROM refs
                     WHERE tenant_id = $1 AND namespace = $2 AND name LIKE $3
                     ORDER BY name",
                    &[&self.tenant_id, &namespace.as_str(), &pattern],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("list refs: {}", e)))?;

            let mut result = Vec::new();
            for row in rows {
                let name: String = row.get("name");
                let bytes: Vec<u8> = row.get("target");
                let mut arr = [0u8; 32];
                if bytes.len() == 32 {
                    arr.copy_from_slice(&bytes);
                }
                result.push((name, ObjectId::from_bytes(arr)));
            }

            Ok(result)
        })
    }

    fn delete_ref(&self, namespace: &Namespace, name: &str) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let ns_row = client
                .query_opt(
                    "SELECT 1 FROM namespaces WHERE tenant_id = $1 AND name = $2",
                    &[&self.tenant_id, &namespace.as_str()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("check namespace: {}", e)))?;
            if ns_row.is_none() {
                return Err(StorageError::NamespaceNotFound(
                    namespace.as_str().to_string(),
                ));
            }

            let rows = client
                .execute(
                    "DELETE FROM refs WHERE tenant_id = $1 AND namespace = $2 AND name = $3",
                    &[&self.tenant_id, &namespace.as_str(), &name],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("delete ref: {}", e)))?;

            Ok(rows > 0)
        })
    }

    fn delete_namespace(&self, namespace: &Namespace) -> Result<bool, StorageError> {
        if namespace.as_str() == Namespace::DEFAULT {
            return Err(StorageError::InvalidOperation(
                "cannot delete the 'default' namespace".to_string(),
            ));
        }
        let ns = namespace.as_str().to_string();
        let tid = self.tenant_id.clone();
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            // Refs and the namespace row go together or not at all; separately,
            // a failure in between left refs deleted under a namespace that
            // still existed.
            let tx = client
                .transaction()
                .await
                .map_err(|e| StorageError::Backend(format!("begin delete namespace: {e}")))?;
            tx.execute(
                "DELETE FROM refs WHERE tenant_id = $1 AND namespace = $2",
                &[&tid, &ns],
            )
            .await
            .map_err(|e| StorageError::Backend(format!("delete namespace refs: {e}")))?;
            let rows = tx
                .execute(
                    "DELETE FROM namespaces WHERE tenant_id = $1 AND name = $2",
                    &[&tid, &ns],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("delete namespace: {e}")))?;
            tx.commit()
                .await
                .map_err(|e| StorageError::Backend(format!("commit delete namespace: {e}")))?;
            Ok(rows > 0)
        })
    }
}

// ---------------------------------------------------------------------------
// Helpers for Epoch / Session row assembly. Parallel to the SQLite backend.
// ---------------------------------------------------------------------------

fn epoch_status_str(s: &EpochStatus) -> &'static str {
    match s {
        EpochStatus::Active => "Active",
        EpochStatus::Sealed => "Sealed",
        EpochStatus::Archived => "Archived",
    }
}

fn parse_epoch_status(s: &str) -> EpochStatus {
    match s {
        "Sealed" => EpochStatus::Sealed,
        "Archived" => EpochStatus::Archived,
        _ => EpochStatus::Active,
    }
}

fn parse_rfc3339(s: &str) -> Result<DateTime<Utc>, StorageError> {
    DateTime::parse_from_rfc3339(s)
        .map(|dt| dt.with_timezone(&Utc))
        .map_err(|e| StorageError::Serialization(format!("timestamp parse: {}", e)))
}

#[allow(clippy::too_many_arguments)]
fn row_to_epoch(
    id: String,
    description: String,
    status: String,
    created_at: String,
    sealed_at: Option<String>,
    summary: Option<String>,
    root_intents: String,
    agents: String,
    tags: String,
    sealed_commits: String,
    seal_hash: Option<Vec<u8>>,
    namespace: Option<String>,
    scope: String,
) -> Result<Epoch, StorageError> {
    let root_intents: Vec<String> = serde_json::from_str(&root_intents)
        .map_err(|e| StorageError::Serialization(format!("root_intents: {}", e)))?;
    let agents: Vec<String> = serde_json::from_str(&agents)
        .map_err(|e| StorageError::Serialization(format!("agents: {}", e)))?;
    let tags: Vec<String> = serde_json::from_str(&tags)
        .map_err(|e| StorageError::Serialization(format!("tags: {}", e)))?;
    let sealed_commits: Vec<ObjectId> = serde_json::from_str(&sealed_commits)
        .map_err(|e| StorageError::Serialization(format!("sealed_commits: {}", e)))?;

    Ok(Epoch {
        id,
        description,
        root_intents,
        status: parse_epoch_status(&status),
        created_at: parse_rfc3339(&created_at)?,
        sealed_at: sealed_at.as_deref().map(parse_rfc3339).transpose()?,
        seal_summary: summary,
        // A 32-byte digest or nothing; a short or corrupt value reads as
        // absent rather than failing the whole read, as in SQLite.
        seal_hash: seal_hash.and_then(|b| {
            <[u8; 32]>::try_from(b.as_slice())
                .ok()
                .map(ObjectId::from_bytes)
        }),
        commits: Vec::new(),
        agents,
        branches: Vec::new(),
        scope: EpochScope::from_storage_str(&scope),
        namespace,
        tags,
        sealed_commits,
    })
}

/// `commit_count` is an `INTEGER` column, so it must be bound as `i32`:
/// binding an `i64` fails every statement ("error serializing parameter").
fn commit_count_param(n: usize) -> i32 {
    i32::try_from(n).unwrap_or(i32::MAX)
}

/// The columns `row_to_epoch` reads, in the order it takes them.
const EPOCH_COLUMNS: &str = "id, description, status, created_at, sealed_at, summary,
     root_intents, agents, tags, sealed_commits, seal_hash, namespace, scope";

fn epoch_from_row(row: &tokio_postgres::Row) -> Result<Epoch, StorageError> {
    row_to_epoch(
        row.get("id"),
        row.get("description"),
        row.get("status"),
        row.get("created_at"),
        row.get("sealed_at"),
        row.get("summary"),
        row.get("root_intents"),
        row.get("agents"),
        row.get("tags"),
        row.get("sealed_commits"),
        row.get("seal_hash"),
        row.get("namespace"),
        row.get("scope"),
    )
}

#[allow(clippy::too_many_arguments)]
fn row_to_session(
    id: String,
    agent_id: String,
    parent_id: Option<String>,
    scope_path: Option<String>,
    scope_branch: Option<String>,
    scope_namespace: Option<String>,
    status: String,
    created_at: String,
    ended_at: Option<String>,
    metadata: String,
) -> Result<Session, StorageError> {
    #[derive(serde::Deserialize, Default)]
    struct Meta {
        #[serde(default)]
        head: Option<ObjectId>,
        #[serde(default)]
        delegated_intent: Option<String>,
        #[serde(default)]
        report_to: Option<String>,
        #[serde(default)]
        working_branch: Option<String>,
        #[serde(default)]
        scope_tenant: Option<String>,
    }
    let meta: Meta = serde_json::from_str(&metadata).unwrap_or_default();
    let head = meta.head.unwrap_or_else(|| ObjectId::hash(b""));
    let working_branch = meta
        .working_branch
        .unwrap_or_else(|| scope_branch.clone().unwrap_or_default());
    Ok(Session {
        id,
        agent_id,
        working_branch,
        head,
        parent_session: parent_id,
        delegated_intent: meta.delegated_intent,
        report_to: meta.report_to,
        path_scope: scope_path,
        scope_tenant: meta.scope_tenant,
        scope_namespace: scope_namespace
            .as_deref()
            .and_then(|s| Namespace::new(s).ok()),
        status: SessionStatus::from_wire(&status),
        created_at: parse_rfc3339(&created_at)?,
        ended_at: ended_at.as_deref().map(parse_rfc3339).transpose()?,
    })
}

// ---------------------------------------------------------------------------
// EpochStore
// ---------------------------------------------------------------------------

impl EpochStore for PostgresStorage {
    fn create_epoch(&self, epoch: &Epoch) -> Result<(), StorageError> {
        let root_intents = serde_json::to_string(&epoch.root_intents)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;
        let agents = serde_json::to_string(&epoch.agents)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;
        let tags = serde_json::to_string(&epoch.tags)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;
        let sealed_commits = serde_json::to_string(&epoch.sealed_commits)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            client
                .execute(
                    "INSERT INTO epochs
                     (tenant_id, id, description, status, created_at, sealed_at,
                      summary, root_intents, agents, tags, commit_count, sealed_commits,
                      namespace, scope)
                     VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)",
                    &[
                        &self.tenant_id,
                        &epoch.id,
                        &epoch.description,
                        &epoch_status_str(&epoch.status),
                        &epoch.created_at.to_rfc3339(),
                        &epoch.sealed_at.map(|t| t.to_rfc3339()),
                        &epoch.seal_summary,
                        &root_intents,
                        &agents,
                        &tags,
                        &commit_count_param(epoch.commits.len()),
                        &sealed_commits,
                        &epoch.namespace,
                        &epoch.scope.as_storage_str(),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("create epoch: {}", e)))?;
            Ok(())
        })
    }

    fn seal_epoch(
        &self,
        id: &str,
        summary: &str,
        sealed_at: DateTime<Utc>,
        sealed_commits: &[ObjectId],
        seal_hash: &ObjectId,
    ) -> Result<(), StorageError> {
        let sc_json = serde_json::to_string(sealed_commits)
            .map_err(|e| StorageError::Serialization(e.to_string()))?;

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            // One conditional UPDATE, so two sealers can't both pass a status
            // check and overwrite each other's seal.
            let sealed = client
                .execute(
                    "UPDATE epochs
                     SET status = 'Sealed', sealed_at = $1, summary = $2,
                         sealed_commits = $3, seal_hash = $4, commit_count = $5
                     WHERE tenant_id = $6 AND id = $7
                       AND status NOT IN ('Sealed', 'Archived')",
                    &[
                        &sealed_at.to_rfc3339(),
                        &summary,
                        &sc_json,
                        &seal_hash.as_bytes().as_slice(),
                        &commit_count_param(sealed_commits.len()),
                        &self.tenant_id,
                        &id,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("seal epoch: {}", e)))?;
            if sealed > 0 {
                return Ok(());
            }
            let exists = client
                .query_opt(
                    "SELECT 1 FROM epochs WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("seal lookup: {}", e)))?;
            match exists {
                Some(_) => Err(StorageError::EpochAlreadySealed { id: id.to_string() }),
                None => Err(StorageError::Backend(format!("epoch not found: {}", id))),
            }
        })
    }

    fn assign_epoch_namespace(&self, id: &str, namespace: &str) -> Result<bool, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            // `namespace IS NULL` makes this assign-only — see the trait docs.
            let changed = client
                .execute(
                    "UPDATE epochs SET namespace = $1
                     WHERE tenant_id = $2 AND id = $3 AND namespace IS NULL",
                    &[&namespace, &self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("assign epoch namespace: {}", e)))?;
            if changed > 0 {
                return Ok(true);
            }
            let row = client
                .query_opt(
                    "SELECT 1 FROM epochs WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("assign epoch lookup: {}", e)))?;
            match row {
                Some(_) => Ok(false),
                None => Err(StorageError::Backend(format!("epoch not found: {}", id))),
            }
        })
    }

    fn list_epochs(&self) -> Result<Vec<Epoch>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let rows = client
                .query(
                    &format!(
                        "SELECT {EPOCH_COLUMNS} FROM epochs
                         WHERE tenant_id = $1
                         ORDER BY created_at DESC"
                    ),
                    &[&self.tenant_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("list epochs: {}", e)))?;
            rows.iter().map(epoch_from_row).collect()
        })
    }

    fn get_epoch(&self, id: &str) -> Result<Option<Epoch>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let row = client
                .query_opt(
                    &format!("SELECT {EPOCH_COLUMNS} FROM epochs WHERE tenant_id = $1 AND id = $2"),
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get epoch: {}", e)))?;

            match row {
                None => Ok(None),
                Some(row) => {
                    let mut epoch = epoch_from_row(&row)?;
                    // Rehydrate the commit list in commit order. `commits` has
                    // no `created_at`; ordering by it failed every call. The id
                    // breaks ties between commits made in the same instant.
                    let rows = client
                        .query(
                            "SELECT id FROM commits
                              WHERE tenant_id = $1 AND epoch_id = $2
                              ORDER BY timestamp, id",
                            &[&self.tenant_id, &epoch.id],
                        )
                        .await
                        .map_err(|e| StorageError::Backend(format!("epoch commits: {}", e)))?;
                    epoch.commits = rows
                        .iter()
                        .filter_map(|r| {
                            let b: Vec<u8> = r.get("id");
                            if b.len() == 32 {
                                let mut arr = [0u8; 32];
                                arr.copy_from_slice(&b);
                                Some(ObjectId::from_bytes(arr))
                            } else {
                                None
                            }
                        })
                        .collect();
                    Ok(Some(epoch))
                }
            }
        })
    }

    fn archive_epoch(&self, id: &str) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let n = client
                .execute(
                    "UPDATE epochs SET status = 'Archived'
                      WHERE tenant_id = $1 AND id = $2 AND status = 'Sealed'",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("archive epoch: {}", e)))?;
            if n == 0 {
                return Err(StorageError::Backend(format!(
                    "epoch '{}' not found or not sealed",
                    id
                )));
            }
            Ok(())
        })
    }

    fn set_commit_epoch(&self, commit_id: &ObjectId, epoch_id: &str) -> Result<(), StorageError> {
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            // One transaction with the epoch row locked: a seal waits for it,
            // so a commit can't join an epoch that is being sealed, and the
            // status check can't go stale before the write.
            let tx = client
                .transaction()
                .await
                .map_err(|e| StorageError::Backend(format!("begin assoc: {e}")))?;
            let row = tx
                .query_opt(
                    "SELECT status FROM epochs WHERE tenant_id = $1 AND id = $2 FOR UPDATE",
                    &[&self.tenant_id, &epoch_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("assoc lookup: {}", e)))?;
            match row.as_ref().map(|r| r.get::<_, String>("status")) {
                None => {
                    return Err(StorageError::Backend(format!(
                        "epoch not found: {}",
                        epoch_id
                    )));
                }
                Some(s) if s == "Sealed" || s == "Archived" => {
                    return Err(StorageError::EpochAlreadySealed {
                        id: epoch_id.to_string(),
                    });
                }
                _ => {}
            }
            let joined = tx
                .execute(
                    "UPDATE commits SET epoch_id = $1
                     WHERE tenant_id = $2 AND id = $3 AND epoch_id IS DISTINCT FROM $1",
                    &[&epoch_id, &self.tenant_id, &commit_id.as_bytes().as_slice()],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("set commit epoch: {}", e)))?;
            // A running count, bumped only when the commit actually joins (the
            // old full COUNT(*) per commit grew with the epoch). Sealing writes
            // the exact count.
            if joined > 0 {
                tx.execute(
                    "UPDATE epochs SET commit_count = commit_count + 1
                     WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &epoch_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("update commit_count: {}", e)))?;
            }
            tx.commit()
                .await
                .map_err(|e| StorageError::Backend(format!("commit assoc: {e}")))?;
            Ok(())
        })
    }
}

// ---------------------------------------------------------------------------
// SessionStore
// ---------------------------------------------------------------------------

impl SessionStore for PostgresStorage {
    fn create_session(&self, session: &Session) -> Result<(), StorageError> {
        let metadata = serde_json::json!({
            "head": session.head,
            "delegated_intent": session.delegated_intent,
            "report_to": session.report_to,
            "working_branch": session.working_branch,
            "scope_tenant": session.scope_tenant,
        })
        .to_string();

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            client
                .execute(
                    "INSERT INTO sessions
                     (tenant_id, id, agent_id, parent_id, scope_path, scope_branch,
                      scope_namespace, status, created_at, ended_at, metadata, commit_count)
                     VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
                             COALESCE(
                                 (SELECT commit_count FROM sessions
                                  WHERE tenant_id = $1 AND id = $2),
                                 0))
                     ON CONFLICT (tenant_id, id) DO UPDATE
                       SET agent_id = EXCLUDED.agent_id,
                           parent_id = EXCLUDED.parent_id,
                           scope_path = EXCLUDED.scope_path,
                           scope_branch = EXCLUDED.scope_branch,
                           scope_namespace = EXCLUDED.scope_namespace,
                           status = EXCLUDED.status,
                           created_at = EXCLUDED.created_at,
                           ended_at = EXCLUDED.ended_at,
                           metadata = EXCLUDED.metadata",
                    &[
                        &self.tenant_id,
                        &session.id,
                        &session.agent_id,
                        &session.parent_session,
                        &session.path_scope,
                        &session.working_branch,
                        &session
                            .scope_namespace
                            .as_ref()
                            .map(|n| n.as_str().to_string()),
                        &session.status.as_str(),
                        &session.created_at.to_rfc3339(),
                        &session.ended_at.map(|t| t.to_rfc3339()),
                        &metadata,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("create session: {}", e)))?;
            Ok(())
        })
    }

    fn end_session(
        &self,
        id: &str,
        status: SessionStatus,
        ended_at: DateTime<Utc>,
    ) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            // One conditional UPDATE, so two callers ending one session can't
            // both pass a status check; exactly one ends it.
            let ended = client
                .execute(
                    "UPDATE sessions SET status = $1, ended_at = $2
                     WHERE tenant_id = $3 AND id = $4 AND status = 'Active'",
                    &[
                        &status.as_str(),
                        &ended_at.to_rfc3339(),
                        &self.tenant_id,
                        &id,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("end session: {}", e)))?;
            if ended > 0 {
                return Ok(());
            }
            let exists = client
                .query_opt(
                    "SELECT 1 FROM sessions WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("end session lookup: {}", e)))?;
            match exists {
                Some(_) => Err(StorageError::SessionEnded { id: id.to_string() }),
                None => Err(StorageError::Backend(format!("session not found: {}", id))),
            }
        })
    }

    fn list_sessions(&self, agent_filter: Option<&str>) -> Result<Vec<Session>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let rows = client
                .query(
                    "SELECT id, agent_id, parent_id, scope_path, scope_branch, scope_namespace,
                            status, created_at, ended_at, metadata
                     FROM sessions
                     WHERE tenant_id = $1 AND ($2::text IS NULL OR agent_id = $2)
                     ORDER BY created_at DESC",
                    &[&self.tenant_id, &agent_filter],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("list sessions: {}", e)))?;

            let mut out = Vec::with_capacity(rows.len());
            for row in rows {
                out.push(row_to_session(
                    row.get("id"),
                    row.get("agent_id"),
                    row.get("parent_id"),
                    row.get("scope_path"),
                    row.get("scope_branch"),
                    row.get("scope_namespace"),
                    row.get("status"),
                    row.get("created_at"),
                    row.get("ended_at"),
                    row.get("metadata"),
                )?);
            }
            Ok(out)
        })
    }

    fn get_session(&self, id: &str) -> Result<Option<Session>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let row = client
                .query_opt(
                    "SELECT id, agent_id, parent_id, scope_path, scope_branch, scope_namespace,
                            status, created_at, ended_at, metadata
                     FROM sessions WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get session: {}", e)))?;

            match row {
                None => Ok(None),
                Some(row) => Ok(Some(row_to_session(
                    row.get("id"),
                    row.get("agent_id"),
                    row.get("parent_id"),
                    row.get("scope_path"),
                    row.get("scope_branch"),
                    row.get("scope_namespace"),
                    row.get("status"),
                    row.get("created_at"),
                    row.get("ended_at"),
                    row.get("metadata"),
                )?)),
            }
        })
    }

    fn set_commit_session(
        &self,
        commit_id: &ObjectId,
        session_id: &str,
    ) -> Result<(), StorageError> {
        self.block_on(async {
            let mut client = self.pool.get().await.map_err(conn_error)?;
            // As set_commit_epoch: one transaction, the session row locked so
            // ending the session waits and the status check can't go stale.
            let tx = client
                .transaction()
                .await
                .map_err(|e| StorageError::Backend(format!("begin assoc: {e}")))?;
            let row = tx
                .query_opt(
                    "SELECT status FROM sessions WHERE tenant_id = $1 AND id = $2 FOR UPDATE",
                    &[&self.tenant_id, &session_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("assoc lookup: {}", e)))?;
            match row.as_ref().map(|r| r.get::<_, String>("status")) {
                None => {
                    return Err(StorageError::Backend(format!(
                        "session not found: {}",
                        session_id
                    )));
                }
                Some(s) if s != "Active" => {
                    return Err(StorageError::SessionEnded {
                        id: session_id.to_string(),
                    });
                }
                _ => {}
            }
            let joined = tx
                .execute(
                    "UPDATE commits SET session_id = $1
                     WHERE tenant_id = $2 AND id = $3 AND session_id IS DISTINCT FROM $1",
                    &[
                        &session_id,
                        &self.tenant_id,
                        &commit_id.as_bytes().as_slice(),
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("set commit session: {}", e)))?;
            if joined > 0 {
                tx.execute(
                    "UPDATE sessions SET commit_count = commit_count + 1
                     WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &session_id],
                )
                .await
                .map_err(|e| {
                    StorageError::Backend(format!("update session commit_count: {}", e))
                })?;
            }
            tx.commit()
                .await
                .map_err(|e| StorageError::Backend(format!("commit assoc: {e}")))?;
            Ok(())
        })
    }
}

// ---------------------------------------------------------------------------
// TaintStore for PostgresStorage (0.7.75-beta.2)
// ---------------------------------------------------------------------------

fn pg_kind_to_str(k: agentstategraph_taint::TaintKind) -> &'static str {
    match k {
        agentstategraph_taint::TaintKind::Taint => "taint",
        agentstategraph_taint::TaintKind::Quarantine => "quarantine",
        agentstategraph_taint::TaintKind::Watch => "watch",
    }
}

fn pg_kind_from_str(s: &str) -> Result<agentstategraph_taint::TaintKind, StorageError> {
    match s {
        "taint" => Ok(agentstategraph_taint::TaintKind::Taint),
        "quarantine" => Ok(agentstategraph_taint::TaintKind::Quarantine),
        "watch" => Ok(agentstategraph_taint::TaintKind::Watch),
        other => Err(StorageError::Backend(format!(
            "unknown taint kind: {other}"
        ))),
    }
}

fn pg_effect_to_str(e: agentstategraph_taint::TaintEffect) -> &'static str {
    match e {
        agentstategraph_taint::TaintEffect::Warn => "warn",
        agentstategraph_taint::TaintEffect::Block => "block",
        agentstategraph_taint::TaintEffect::Review => "review",
        agentstategraph_taint::TaintEffect::Isolate => "isolate",
        agentstategraph_taint::TaintEffect::Advisory => "advisory",
    }
}

fn pg_effect_from_str(s: &str) -> Result<agentstategraph_taint::TaintEffect, StorageError> {
    match s {
        "warn" => Ok(agentstategraph_taint::TaintEffect::Warn),
        "block" => Ok(agentstategraph_taint::TaintEffect::Block),
        "review" => Ok(agentstategraph_taint::TaintEffect::Review),
        "isolate" => Ok(agentstategraph_taint::TaintEffect::Isolate),
        "advisory" => Ok(agentstategraph_taint::TaintEffect::Advisory),
        other => Err(StorageError::Backend(format!(
            "unknown taint effect: {other}"
        ))),
    }
}

fn pg_severity_to_str(s: agentstategraph_taint::TaintSeverity) -> &'static str {
    match s {
        agentstategraph_taint::TaintSeverity::Low => "low",
        agentstategraph_taint::TaintSeverity::Medium => "medium",
        agentstategraph_taint::TaintSeverity::High => "high",
        agentstategraph_taint::TaintSeverity::Critical => "critical",
    }
}

fn pg_severity_from_str(s: &str) -> Result<agentstategraph_taint::TaintSeverity, StorageError> {
    match s {
        "low" => Ok(agentstategraph_taint::TaintSeverity::Low),
        "medium" => Ok(agentstategraph_taint::TaintSeverity::Medium),
        "high" => Ok(agentstategraph_taint::TaintSeverity::High),
        "critical" => Ok(agentstategraph_taint::TaintSeverity::Critical),
        other => Err(StorageError::Backend(format!(
            "unknown taint severity: {other}"
        ))),
    }
}

fn pg_row_to_taint(
    row: &tokio_postgres::Row,
) -> Result<agentstategraph_taint::Taint, StorageError> {
    use agentstategraph_taint::TaintMetadata;
    let kind: String = row.get("kind");
    let effect: String = row.get("effect");
    let severity: String = row.get("severity");
    let metadata_raw: String = row.get("metadata");
    let created_at_s: String = row.get("created_at");
    let expires_at_s: Option<String> = row.get("expires_at");
    let resolved_at_s: Option<String> = row.get("resolved_at");

    let metadata: TaintMetadata = serde_json::from_str(&metadata_raw)
        .map_err(|e| StorageError::Serialization(format!("taint metadata: {e}")))?;
    let created_at = DateTime::parse_from_rfc3339(&created_at_s)
        .map_err(|e| StorageError::Serialization(format!("created_at: {e}")))?
        .with_timezone(&Utc);
    let expires_at = match expires_at_s {
        Some(s) => Some(
            DateTime::parse_from_rfc3339(&s)
                .map_err(|e| StorageError::Serialization(format!("expires_at: {e}")))?
                .with_timezone(&Utc),
        ),
        None => None,
    };
    let resolved_at = match resolved_at_s {
        Some(s) => Some(
            DateTime::parse_from_rfc3339(&s)
                .map_err(|e| StorageError::Serialization(format!("resolved_at: {e}")))?
                .with_timezone(&Utc),
        ),
        None => None,
    };

    Ok(agentstategraph_taint::Taint {
        id: row.get("id"),
        path: row.get("path"),
        name: row.get("name"),
        kind: pg_kind_from_str(&kind)?,
        effect: pg_effect_from_str(&effect)?,
        severity: pg_severity_from_str(&severity)?,
        reason: row.get("reason"),
        agent_id: row.get("agent_id"),
        commit_id: row.get("commit_id"),
        created_at,
        expires_at,
        resolved_at,
        resolved_by: row.get("resolved_by"),
        resolved_reason: row.get("resolved_reason"),
        resolved_proof: row.get("resolved_proof"),
        propagate: row.get("propagate"),
        metadata,
    })
}

impl TaintStore for PostgresStorage {
    fn create_taint(&self, taint: &agentstategraph_taint::Taint) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let metadata_json = serde_json::to_string(&taint.metadata)
                .map_err(|e| StorageError::Serialization(format!("taint metadata: {e}")))?;
            let expires = taint.expires_at.map(|t| t.to_rfc3339());
            let resolved_at = taint.resolved_at.map(|t| t.to_rfc3339());
            client
                .execute(
                    "INSERT INTO taints (
                        tenant_id, id, path, name, kind, effect, severity, reason,
                        agent_id, commit_id, created_at, expires_at, resolved_at,
                        resolved_by, resolved_reason, resolved_proof, propagate, metadata
                    ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18)",
                    &[
                        &self.tenant_id,
                        &taint.id,
                        &taint.path,
                        &taint.name,
                        &pg_kind_to_str(taint.kind),
                        &pg_effect_to_str(taint.effect),
                        &pg_severity_to_str(taint.severity),
                        &taint.reason,
                        &taint.agent_id,
                        &taint.commit_id,
                        &taint.created_at.to_rfc3339(),
                        &expires,
                        &resolved_at,
                        &taint.resolved_by,
                        &taint.resolved_reason,
                        &taint.resolved_proof,
                        &taint.propagate,
                        &metadata_json,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("insert taint: {e}")))?;
            Ok(())
        })
    }

    fn resolve_taint(
        &self,
        id: &str,
        resolved_by: &str,
        reason: &str,
        proof: Option<&str>,
        resolved_at: DateTime<Utc>,
    ) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            // Guard: fail on already-resolved / not-found. Partial
            // unique index would force failures on re-activation;
            // we want explicit control here so the error is legible.
            let row = client
                .query_opt(
                    "SELECT resolved_at FROM taints WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("resolve check: {e}")))?
                .ok_or_else(|| StorageError::Backend(format!("taint {id} not found")))?;
            let already: Option<String> = row.get(0);
            if already.is_some() {
                return Err(StorageError::Backend(format!(
                    "taint {id} is already resolved"
                )));
            }
            let proof_owned = proof.map(str::to_string);
            client
                .execute(
                    "UPDATE taints
                        SET resolved_at = $3, resolved_by = $4,
                            resolved_reason = $5, resolved_proof = $6
                      WHERE tenant_id = $1 AND id = $2",
                    &[
                        &self.tenant_id,
                        &id,
                        &resolved_at.to_rfc3339(),
                        &resolved_by,
                        &reason,
                        &proof_owned,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("resolve taint: {e}")))?;
            Ok(())
        })
    }

    fn list_taints(
        &self,
        path_prefix: Option<&str>,
        kind: Option<agentstategraph_taint::TaintKind>,
        include_resolved: bool,
    ) -> Result<Vec<agentstategraph_taint::Taint>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            // Build dynamic WHERE without sacrificing parameterization:
            // we always pass tenant_id + optional prefix/plike/kind.
            let prefix = path_prefix.map(|p| p.trim_end_matches('/').to_string());
            let plike = prefix.as_ref().map(|p| format!("{p}/%"));
            let kind_str = kind.map(|k| pg_kind_to_str(k).to_string());

            let rows = match (prefix.as_ref(), kind_str.as_ref(), include_resolved) {
                (None, None, true) => {
                    client
                        .query(
                            "SELECT * FROM taints WHERE tenant_id = $1
                              ORDER BY created_at DESC",
                            &[&self.tenant_id],
                        )
                        .await
                }
                (None, None, false) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND resolved_at IS NULL
                              ORDER BY created_at DESC",
                            &[&self.tenant_id],
                        )
                        .await
                }
                (Some(p), None, true) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND (path = $2 OR path LIKE $3)
                              ORDER BY created_at DESC",
                            &[&self.tenant_id, p, plike.as_ref().unwrap()],
                        )
                        .await
                }
                (Some(p), None, false) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND resolved_at IS NULL
                                AND (path = $2 OR path LIKE $3)
                              ORDER BY created_at DESC",
                            &[&self.tenant_id, p, plike.as_ref().unwrap()],
                        )
                        .await
                }
                (None, Some(k), true) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND kind = $2
                              ORDER BY created_at DESC",
                            &[&self.tenant_id, k],
                        )
                        .await
                }
                (None, Some(k), false) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND resolved_at IS NULL AND kind = $2
                              ORDER BY created_at DESC",
                            &[&self.tenant_id, k],
                        )
                        .await
                }
                (Some(p), Some(k), true) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND kind = $2
                                AND (path = $3 OR path LIKE $4)
                              ORDER BY created_at DESC",
                            &[&self.tenant_id, k, p, plike.as_ref().unwrap()],
                        )
                        .await
                }
                (Some(p), Some(k), false) => {
                    client
                        .query(
                            "SELECT * FROM taints
                              WHERE tenant_id = $1 AND resolved_at IS NULL AND kind = $2
                                AND (path = $3 OR path LIKE $4)
                              ORDER BY created_at DESC",
                            &[&self.tenant_id, k, p, plike.as_ref().unwrap()],
                        )
                        .await
                }
            }
            .map_err(|e| StorageError::Backend(format!("list_taints: {e}")))?;

            let mut out = Vec::with_capacity(rows.len());
            for row in rows {
                out.push(pg_row_to_taint(&row)?);
            }
            Ok(out)
        })
    }

    fn check_taint(
        &self,
        request_path: &str,
    ) -> Result<Vec<agentstategraph_taint::Taint>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let now = Utc::now().to_rfc3339();
            // Ancestor match: exact OR (propagate=true AND request_path
            // LIKE path || '/%'). Postgres LIKE is case-sensitive.
            let rows = client
                .query(
                    "SELECT * FROM taints
                      WHERE tenant_id = $1
                        AND resolved_at IS NULL
                        AND (expires_at IS NULL OR expires_at > $2)
                        AND (path = $3 OR (propagate AND $3 LIKE path || '/%'))
                      ORDER BY created_at DESC",
                    &[&self.tenant_id, &now, &request_path],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("check_taint: {e}")))?;
            let mut out = Vec::with_capacity(rows.len());
            for row in rows {
                out.push(pg_row_to_taint(&row)?);
            }
            Ok(out)
        })
    }

    fn get_taint(&self, id: &str) -> Result<Option<agentstategraph_taint::Taint>, StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_opt(
                    "SELECT * FROM taints WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get_taint: {e}")))?;
            match row {
                Some(r) => Ok(Some(pg_row_to_taint(&r)?)),
                None => Ok(None),
            }
        })
    }

    fn set_taint_commit_id(&self, id: &str, commit_id: &str) -> Result<(), StorageError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            client
                .execute(
                    "UPDATE taints SET commit_id = $3
                      WHERE tenant_id = $1 AND id = $2 AND resolved_at IS NULL",
                    &[&self.tenant_id, &id, &commit_id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("set_taint_commit_id: {e}")))?;
            Ok(())
        })
    }
}

// ---------------------------------------------------------------------------
// ReminderStore — durable Postgres reminder storage
// ---------------------------------------------------------------------------

fn pg_reminder_status_to_str(s: agentstategraph_reminders::types::ReminderStatus) -> &'static str {
    use agentstategraph_reminders::types::ReminderStatus;
    match s {
        ReminderStatus::Pending => "pending",
        ReminderStatus::Due => "due",
        ReminderStatus::AwaitingPermission => "awaiting_permission",
        ReminderStatus::InProgress => "in_progress",
        ReminderStatus::Completed => "completed",
        ReminderStatus::Snoozed => "snoozed",
        ReminderStatus::Cancelled => "cancelled",
    }
}

fn pg_reminder_status_from_str(
    s: &str,
) -> Result<agentstategraph_reminders::types::ReminderStatus, StorageError> {
    use agentstategraph_reminders::types::ReminderStatus;
    match s {
        "pending" => Ok(ReminderStatus::Pending),
        "due" => Ok(ReminderStatus::Due),
        "awaiting_permission" => Ok(ReminderStatus::AwaitingPermission),
        "in_progress" => Ok(ReminderStatus::InProgress),
        "completed" => Ok(ReminderStatus::Completed),
        "snoozed" => Ok(ReminderStatus::Snoozed),
        "cancelled" => Ok(ReminderStatus::Cancelled),
        other => Err(StorageError::Backend(format!(
            "unknown reminder status: {other}"
        ))),
    }
}

fn pg_priority_to_i32(p: agentstategraph_reminders::types::Priority) -> i32 {
    p.as_u8() as i32
}

fn pg_priority_from_i32(
    n: i32,
) -> Result<agentstategraph_reminders::types::Priority, StorageError> {
    use agentstategraph_reminders::types::Priority;
    match n {
        1 => Ok(Priority::Critical),
        2 => Ok(Priority::High),
        3 => Ok(Priority::Medium),
        4 => Ok(Priority::Low),
        5 => Ok(Priority::Minimal),
        other => Err(StorageError::Backend(format!(
            "unknown priority value: {other}"
        ))),
    }
}

/// Write every field of `reminder` over its row, on a plain connection or
/// inside a transaction. Returns the rows changed: 0 means no such reminder.
async fn write_reminder<C: GenericClient>(
    client: &C,
    tenant_id: &str,
    reminder: &agentstategraph_reminders::Reminder,
) -> Result<u64, StorageError> {
    let json = |what: &str, v: Result<String, serde_json::Error>| {
        v.map_err(|e| StorageError::Serialization(format!("{what}: {e}")))
    };
    let commands_json = json("commands", serde_json::to_string(&reminder.commands))?;
    let refs_json = json("refs", serde_json::to_string(&reminder.refs))?;
    let schedule_json = reminder
        .schedule
        .as_ref()
        .map(|s| json("schedule", serde_json::to_string(s)))
        .transpose()?;
    let executions_json = json("executions", serde_json::to_string(&reminder.executions))?;
    let tags_json = json("tags", serde_json::to_string(&reminder.tags))?;
    let priority = pg_priority_to_i32(reminder.priority);
    let status = pg_reminder_status_to_str(reminder.status).to_string();
    let due_at = reminder.due_at.to_rfc3339();
    let created_at = reminder.created_at.to_rfc3339();
    let snoozed_until = reminder.snoozed_until.map(|t| t.to_rfc3339());
    client
        .execute(
            "UPDATE reminders SET
                title = $3, instructions = $4, commands = $5, refs = $6,
                priority = $7, due_at = $8, schedule = $9, autonomous = $10,
                created_by = $11, created_at = $12, status = $13,
                snoozed_until = $14, executions = $15, tags = $16
             WHERE tenant_id = $1 AND id = $2",
            &[
                &tenant_id,
                &reminder.id,
                &reminder.title,
                &reminder.instructions,
                &commands_json,
                &refs_json,
                &priority,
                &due_at,
                &schedule_json,
                &reminder.autonomous,
                &reminder.created_by,
                &created_at,
                &status,
                &snoozed_until,
                &executions_json,
                &tags_json,
            ],
        )
        .await
        .map_err(|e| StorageError::Backend(format!("update reminder: {e}")))
}

fn pg_row_to_reminder(
    row: &tokio_postgres::Row,
) -> Result<agentstategraph_reminders::Reminder, StorageError> {
    use agentstategraph_reminders::types::ExecutionRecord;
    use agentstategraph_reminders::{Reminder, types::ReminderRef, types::Schedule};

    let status_s: String = row.get("status");
    let priority_n: i32 = row.get("priority");
    let due_at_s: String = row.get("due_at");
    let created_at_s: String = row.get("created_at");
    let snoozed_until_s: Option<String> = row.get("snoozed_until");
    let commands_s: String = row.get("commands");
    let refs_s: String = row.get("refs");
    let schedule_s: Option<String> = row.get("schedule");
    let executions_s: String = row.get("executions");
    let tags_s: String = row.get("tags");
    let autonomous: bool = row.get("autonomous");

    let status = pg_reminder_status_from_str(&status_s)?;
    let priority = pg_priority_from_i32(priority_n)?;
    let due_at = DateTime::parse_from_rfc3339(&due_at_s)
        .map_err(|e| StorageError::Serialization(format!("due_at: {e}")))?
        .with_timezone(&Utc);
    let created_at = DateTime::parse_from_rfc3339(&created_at_s)
        .map_err(|e| StorageError::Serialization(format!("created_at: {e}")))?
        .with_timezone(&Utc);
    let snoozed_until = match snoozed_until_s {
        Some(s) => Some(
            DateTime::parse_from_rfc3339(&s)
                .map_err(|e| StorageError::Serialization(format!("snoozed_until: {e}")))?
                .with_timezone(&Utc),
        ),
        None => None,
    };
    let commands: Vec<String> = serde_json::from_str(&commands_s)
        .map_err(|e| StorageError::Serialization(format!("commands: {e}")))?;
    let refs: Vec<ReminderRef> = serde_json::from_str(&refs_s)
        .map_err(|e| StorageError::Serialization(format!("refs: {e}")))?;
    let schedule: Option<Schedule> = match schedule_s {
        Some(s) => Some(
            serde_json::from_str(&s)
                .map_err(|e| StorageError::Serialization(format!("schedule: {e}")))?,
        ),
        None => None,
    };
    let executions: Vec<ExecutionRecord> = serde_json::from_str(&executions_s)
        .map_err(|e| StorageError::Serialization(format!("executions: {e}")))?;
    let tags: Vec<String> = serde_json::from_str(&tags_s)
        .map_err(|e| StorageError::Serialization(format!("tags: {e}")))?;

    Ok(Reminder {
        id: row.get("id"),
        title: row.get("title"),
        instructions: row.get("instructions"),
        commands,
        refs,
        priority,
        due_at,
        schedule,
        autonomous,
        created_by: row.get("created_by"),
        created_at,
        status,
        snoozed_until,
        executions,
        tags,
    })
}

impl agentstategraph_reminders::ReminderStore for PostgresStorage {
    fn save(
        &self,
        reminder: &agentstategraph_reminders::Reminder,
    ) -> Result<(), agentstategraph_reminders::ReminderError> {
        let commands_json = serde_json::to_string(&reminder.commands).map_err(|e| {
            agentstategraph_reminders::ReminderError::Store(format!("commands: {e}"))
        })?;
        let refs_json = serde_json::to_string(&reminder.refs)
            .map_err(|e| agentstategraph_reminders::ReminderError::Store(format!("refs: {e}")))?;
        let schedule_json = reminder
            .schedule
            .as_ref()
            .map(|s| serde_json::to_string(s))
            .transpose()
            .map_err(|e| {
                agentstategraph_reminders::ReminderError::Store(format!("schedule: {e}"))
            })?;
        let executions_json = serde_json::to_string(&reminder.executions).map_err(|e| {
            agentstategraph_reminders::ReminderError::Store(format!("executions: {e}"))
        })?;
        let tags_json = serde_json::to_string(&reminder.tags)
            .map_err(|e| agentstategraph_reminders::ReminderError::Store(format!("tags: {e}")))?;
        let priority = pg_priority_to_i32(reminder.priority);
        let status = pg_reminder_status_to_str(reminder.status).to_string();
        let due_at = reminder.due_at.to_rfc3339();
        let created_at = reminder.created_at.to_rfc3339();
        let snoozed_until = reminder.snoozed_until.map(|t| t.to_rfc3339());

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            client
                .execute(
                    "INSERT INTO reminders (
                        tenant_id, id, title, instructions, commands, refs,
                        priority, due_at, schedule, autonomous, created_by,
                        created_at, status, snoozed_until, executions, tags
                    ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)",
                    &[
                        &self.tenant_id,
                        &reminder.id,
                        &reminder.title,
                        &reminder.instructions,
                        &commands_json,
                        &refs_json,
                        &priority,
                        &due_at,
                        &schedule_json,
                        &reminder.autonomous,
                        &reminder.created_by,
                        &created_at,
                        &status,
                        &snoozed_until,
                        &executions_json,
                        &tags_json,
                    ],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("insert reminder: {e}")))?;
            Ok(())
        })
        .map_err(|e| agentstategraph_reminders::ReminderError::Store(e.to_string()))
    }

    fn get(
        &self,
        id: &str,
    ) -> Result<Option<agentstategraph_reminders::Reminder>, agentstategraph_reminders::ReminderError>
    {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let row = client
                .query_opt(
                    "SELECT * FROM reminders WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("get reminder: {e}")))?;
            match row {
                Some(r) => Ok(Some(pg_row_to_reminder(&r)?)),
                None => Ok(None),
            }
        })
        .map_err(|e| agentstategraph_reminders::ReminderError::Store(e.to_string()))
    }

    fn update(
        &self,
        reminder: &agentstategraph_reminders::Reminder,
    ) -> Result<(), agentstategraph_reminders::ReminderError> {
        let written = self
            .block_on(async {
                let client = self.pool.get().await.map_err(conn_error)?;
                write_reminder(&client, &self.tenant_id, reminder).await
            })
            .map_err(|e| agentstategraph_reminders::ReminderError::Store(e.to_string()))?;
        if written == 0 {
            return Err(agentstategraph_reminders::ReminderError::NotFound(
                reminder.id.clone(),
            ));
        }
        Ok(())
    }

    /// Compare and write in one transaction with the row locked, so no other
    /// writer can change the reminder in between. The trait's default compared
    /// and wrote separately: six racing updates from one snapshot all "won".
    fn update_if_unchanged(
        &self,
        expected: &agentstategraph_reminders::Reminder,
        new: &agentstategraph_reminders::Reminder,
    ) -> Result<bool, agentstategraph_reminders::ReminderError> {
        enum Outcome {
            Missing,
            Changed,
            Written,
        }
        let outcome = self
            .block_on(async {
                let mut client = self.pool.get().await.map_err(conn_error)?;
                let tx = client
                    .transaction()
                    .await
                    .map_err(|e| StorageError::Backend(format!("begin reminder update: {e}")))?;
                let row = tx
                    .query_opt(
                        "SELECT * FROM reminders WHERE tenant_id = $1 AND id = $2 FOR UPDATE",
                        &[&self.tenant_id, &expected.id],
                    )
                    .await
                    .map_err(|e| StorageError::Backend(format!("get reminder: {e}")))?;
                let Some(row) = row else {
                    return Ok(Outcome::Missing);
                };
                if !agentstategraph_reminders::same_reminder(&pg_row_to_reminder(&row)?, expected) {
                    return Ok(Outcome::Changed); // dropping the transaction rolls it back
                }
                write_reminder(&tx, &self.tenant_id, new).await?;
                tx.commit()
                    .await
                    .map_err(|e| StorageError::Backend(format!("commit reminder update: {e}")))?;
                Ok(Outcome::Written)
            })
            .map_err(|e| agentstategraph_reminders::ReminderError::Store(e.to_string()))?;
        match outcome {
            Outcome::Missing => Err(agentstategraph_reminders::ReminderError::NotFound(
                expected.id.clone(),
            )),
            Outcome::Changed => Ok(false),
            Outcome::Written => Ok(true),
        }
    }

    fn delete(&self, id: &str) -> Result<bool, agentstategraph_reminders::ReminderError> {
        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;
            let n = client
                .execute(
                    "DELETE FROM reminders WHERE tenant_id = $1 AND id = $2",
                    &[&self.tenant_id, &id],
                )
                .await
                .map_err(|e| StorageError::Backend(format!("delete reminder: {e}")))?;
            Ok(n > 0)
        })
        .map_err(|e| agentstategraph_reminders::ReminderError::Store(e.to_string()))
    }

    fn list(
        &self,
        filter: &agentstategraph_reminders::ReminderFilter,
    ) -> Result<Vec<agentstategraph_reminders::Reminder>, agentstategraph_reminders::ReminderError>
    {
        // Pre-compute owned values so they outlive the async block.
        let status_str = filter
            .status
            .map(|s| pg_reminder_status_to_str(s).to_string());
        let priority_val: Option<i32> = filter.priority_at_most.map(pg_priority_to_i32);
        let created_by = filter.created_by.clone();
        let due_before = filter.due_before.map(|d| d.to_rfc3339());
        let ref_id = filter.ref_id.clone();
        let tags = filter.tags.clone();

        self.block_on(async {
            let client = self.pool.get().await.map_err(conn_error)?;

            let mut sql = "SELECT * FROM reminders WHERE tenant_id = $1".to_string();
            let mut idx = 2usize;
            if status_str.is_some() {
                sql.push_str(&format!(" AND status = ${idx}"));
                idx += 1;
            }
            if priority_val.is_some() {
                sql.push_str(&format!(" AND priority <= ${idx}"));
                idx += 1;
            }
            if created_by.is_some() {
                sql.push_str(&format!(" AND created_by = ${idx}"));
                idx += 1;
            }
            if due_before.is_some() {
                sql.push_str(&format!(" AND due_at <= ${idx}"));
                // idx not needed after this
            }
            sql.push_str(" ORDER BY priority ASC, due_at ASC");

            let mut params: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> = vec![&self.tenant_id];
            if let Some(ref s) = status_str {
                params.push(s);
            }
            if let Some(ref p) = priority_val {
                params.push(p);
            }
            if let Some(ref c) = created_by {
                params.push(c);
            }
            if let Some(ref d) = due_before {
                params.push(d);
            }

            let rows = client
                .query(&sql, &params)
                .await
                .map_err(|e| StorageError::Backend(format!("list reminders: {e}")))?;

            let mut out = Vec::with_capacity(rows.len());
            for row in rows {
                let reminder = pg_row_to_reminder(&row)?;
                if let Some(ref rid) = ref_id {
                    if !reminder.refs.iter().any(|rf| &rf.id == rid) {
                        continue;
                    }
                }
                if tags.iter().any(|tag| !reminder.tags.contains(tag)) {
                    continue;
                }
                out.push(reminder);
            }
            Ok(out)
        })
        .map_err(|e| agentstategraph_reminders::ReminderError::Store(e.to_string()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Verifies the constructor applies the supplied `max_size` to the
    /// underlying deadpool. Does NOT open a Postgres connection —
    /// `create_pool` only validates config.
    #[test]
    fn connect_with_pool_size_applies_cap() {
        let mut cfg = Config::new();
        cfg.url = Some("postgres://localhost/agentstategraph_unused".to_string());
        cfg.manager = Some(ManagerConfig {
            recycling_method: RecyclingMethod::Fast,
        });
        cfg.pool = Some(PoolConfig {
            max_size: 7,
            ..Default::default()
        });
        let pool = cfg
            .create_pool(Some(PoolRuntime::Tokio1), tokio_postgres::NoTls)
            .expect("build pool");
        assert_eq!(pool.status().max_size, 7);
    }

    fn tls_of(url: &str) -> ConnectionTls {
        ConnectionTls::parse(url, None).unwrap()
    }

    #[test]
    fn no_sslmode_stays_plaintext() {
        let t = tls_of("postgres://u@h/db");
        assert_eq!(t.url, "postgres://u@h/db?sslmode=disable");
        assert!(!t.requires_tls);
        assert_eq!(t.root_cert, None);
        assert_eq!(
            tls_of("postgres://u@h/db?application_name=asg").url,
            "postgres://u@h/db?application_name=asg&sslmode=disable"
        );
    }

    #[test]
    fn verify_modes_become_require_and_sslrootcert_is_taken_out() {
        let t = tls_of(
            "postgres://u@h/db?sslmode=verify-full&sslrootcert=/etc/ssl/My%20CA.pem&application_name=asg",
        );
        assert_eq!(
            t.url,
            "postgres://u@h/db?sslmode=require&application_name=asg"
        );
        assert!(t.requires_tls);
        assert_eq!(
            t.root_cert,
            Some(std::path::PathBuf::from("/etc/ssl/My CA.pem"))
        );
        assert!(tls_of("postgresql://h/db?sslmode=verify-ca").requires_tls);
        assert!(tls_of("postgres://h/db?sslmode=require").requires_tls);
        let prefer = tls_of("postgres://h/db?sslmode=prefer");
        assert!(!prefer.requires_tls);
        assert_eq!(prefer.url, "postgres://h/db?sslmode=prefer");
    }

    #[test]
    fn key_value_strings_are_handled_too() {
        let t = tls_of("host=db.example.com user=asg sslmode=verify-full sslrootcert=/ca.pem");
        assert_eq!(t.url, "host=db.example.com user=asg sslmode=require");
        assert!(t.requires_tls);
        assert_eq!(t.root_cert, Some(std::path::PathBuf::from("/ca.pem")));
    }

    #[test]
    fn pgsslrootcert_is_the_fallback() {
        let env = Some("/env/ca.pem".to_string());
        let t = ConnectionTls::parse("postgres://h/db?sslmode=require", env.clone()).unwrap();
        assert_eq!(t.root_cert, Some(std::path::PathBuf::from("/env/ca.pem")));
        let t = ConnectionTls::parse("postgres://h/db?sslrootcert=/url/ca.pem", env).unwrap();
        assert_eq!(
            t.root_cert,
            Some(std::path::PathBuf::from("/url/ca.pem")),
            "the connection string wins"
        );
    }

    #[cfg(not(feature = "postgres-tls"))]
    #[test]
    fn asking_for_tls_without_the_feature_is_an_error() {
        let err = PostgresStorage::with_pool("postgres://h/db?sslmode=require", "t", 1)
            .err()
            .expect("refused");
        assert!(format!("{err:?}").contains("postgres-tls"), "{err:?}");
        assert!(
            PostgresStorage::with_pool("postgres://h/db?sslmode=prefer", "t", 1).is_ok(),
            "prefer may fall back to plaintext"
        );
    }

    #[cfg(feature = "postgres-tls")]
    #[test]
    fn an_unreadable_sslrootcert_is_a_clear_error() {
        let err = PostgresStorage::with_pool(
            "postgres://h/db?sslmode=require&sslrootcert=/nonexistent/ca.pem",
            "t",
            1,
        )
        .err()
        .expect("refused");
        assert!(format!("{err:?}").contains("sslrootcert"), "{err:?}");
    }

    #[test]
    fn default_pool_size_is_32() {
        assert_eq!(DEFAULT_POOL_SIZE, 32);
    }

    // -----------------------------------------------------------------------
    // ReminderStore helper unit tests (no live Postgres connection needed)
    // -----------------------------------------------------------------------

    use agentstategraph_reminders::types::{Priority, ReminderStatus};

    #[test]
    fn pg_reminder_status_round_trip() {
        let statuses = [
            ReminderStatus::Pending,
            ReminderStatus::Due,
            ReminderStatus::AwaitingPermission,
            ReminderStatus::InProgress,
            ReminderStatus::Completed,
            ReminderStatus::Snoozed,
            ReminderStatus::Cancelled,
        ];
        for s in statuses {
            let encoded = pg_reminder_status_to_str(s);
            let decoded = pg_reminder_status_from_str(encoded)
                .unwrap_or_else(|e| panic!("decode failed for {s:?}: {e}"));
            assert_eq!(decoded, s, "status round-trip failed for {s:?}");
        }
    }

    #[test]
    fn pg_reminder_status_from_str_rejects_unknown() {
        assert!(pg_reminder_status_from_str("bogus").is_err());
    }

    #[test]
    fn pg_priority_round_trip() {
        let priorities = [
            Priority::Critical,
            Priority::High,
            Priority::Medium,
            Priority::Low,
            Priority::Minimal,
        ];
        for p in priorities {
            let encoded = pg_priority_to_i32(p);
            let decoded = pg_priority_from_i32(encoded)
                .unwrap_or_else(|e| panic!("decode failed for {p:?}: {e}"));
            assert_eq!(decoded, p, "priority round-trip failed for {p:?}");
        }
    }

    #[test]
    fn pg_priority_from_i32_rejects_out_of_range() {
        assert!(pg_priority_from_i32(0).is_err());
        assert!(pg_priority_from_i32(6).is_err());
        assert!(pg_priority_from_i32(-1).is_err());
    }

    #[test]
    fn pg_priority_values_match_ordinal() {
        assert_eq!(pg_priority_to_i32(Priority::Critical), 1);
        assert_eq!(pg_priority_to_i32(Priority::High), 2);
        assert_eq!(pg_priority_to_i32(Priority::Medium), 3);
        assert_eq!(pg_priority_to_i32(Priority::Low), 4);
        assert_eq!(pg_priority_to_i32(Priority::Minimal), 5);
    }

    #[test]
    fn pg_reminder_status_strings_are_snake_case() {
        assert_eq!(
            pg_reminder_status_to_str(ReminderStatus::AwaitingPermission),
            "awaiting_permission"
        );
        assert_eq!(
            pg_reminder_status_to_str(ReminderStatus::InProgress),
            "in_progress"
        );
    }
}