eidetic-engine 0.15.1

Durable, local-first, explainable memory for coding agents.
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
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
//! Unix-domain socket accept loop + per-connection dispatcher
//! (bd-oja31 skeleton). Wraps the framing in
//! [`super::protocol`] with the seed dispatch table for
//! `ee.daemon.capabilities`, `ee.daemon.echo`,
//! `ee.daemon.shutdown`, and the workspace-bound
//! `ee.daemon.context` pack path.
//!
//! Threading: each accepted connection is dispatched onto a bounded
//! worker pool (capped at [`super::DAEMON_MAX_INFLIGHT`], overridable
//! via `EE_DAEMON_MAX_INFLIGHT`). When the pool is saturated the
//! accept loop refuses the new connection with a framed
//! `daemon_overloaded` response and closes it rather than queueing —
//! that keeps total daemon RSS amplification bounded at
//! `inflight × per-worker-footprint` rather than `unbounded × per-worker`.
//! A shutdown signal (an `Arc<AtomicBool>`) is checked between
//! accepts; the accept loop breaks on the next iteration when the
//! signal flips. A follow-up slice will wrap this with Asupersync
//! supervision; the wire framing and dispatch table are stable
//! across that refactor. See bd-jnyui for the bounded-pool fix.
//!
//! Platform: this module is `#[cfg(unix)]`; Windows builds skip it and
//! the CLI handler short-circuits with
//! [`super::DaemonStartError::PlatformUnsupported`].

#![cfg(unix)]

use std::collections::BTreeMap;
use std::fs::{self, File, OpenOptions};
use std::io;
use std::os::unix::fs::{DirBuilderExt, FileTypeExt, MetadataExt, PermissionsExt};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::{Arc, Condvar, Mutex, mpsc};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use rustix::fs::{FlockOperation, flock};

use crate::config::env_registry::{self, EnvVar};
use crate::core::context::{
    ContextPackError, ContextPackOptions, ContextPackOutputOptionOverrides,
    ContextPackOutputOptions, ContextSearchAdvisorySnapshot,
    attach_context_cached_search_advisories_for_delivery,
    attach_context_search_advisories_for_delivery, attach_pack_dna_to_context_response,
    run_context_pack_with_performance_controlled,
};
use crate::core::search::{
    PERFORMANCE_EXPLAIN_SCHEMA_V1, PERFORMANCE_FALLBACK_REDACTED_MESSAGE,
    SearchAdvisoryDeliveryReservation, SearchAdvisorySession, SearchAdvisorySettlement,
    SearchDedupMode, SearchOptions, SearchPerformanceTrace, SearchReport, SearchSourceMode,
    TypedMemoryFieldFilter, elapsed_timing_json, normalize_memory_kind_filter,
    run_search_with_performance_and_filters,
};
use crate::models::{MemoryScope, QueryFilters, RedactionLevel};
use crate::output::{ContextJsonRenderOptions, render_context_response_json_with_options};
use crate::pack::{ContextPackProfile, DEFAULT_COORDINATION_STALE_AFTER_MS, PackResourceProfile};
use crate::search::SpeedMode;

use super::protocol::{
    DAEMON_MEMORY_READ_RESPONSE_SCHEMA_V1, DAEMON_ORIENT_HOOK_REQUEST_SCHEMA_V1,
    DAEMON_RECALL_REQUEST_SCHEMA_V1, DaemonMemoryReadResult, DaemonOrientHookParams,
    DaemonRecallParams, DaemonRequest, DaemonResponse, FrameReadError, METHOD_ORIENT_HOOK,
    METHOD_RECALL, read_request, write_response,
};
pub use super::protocol::{
    DAEMON_SEARCH_REQUEST_SCHEMA_V2, DAEMON_SEARCH_RESPONSE_SCHEMA_V3, METHOD_SEARCH,
};
use super::{
    DAEMON_DEFAULT_RPC_TIMEOUT, DAEMON_MAX_INFLIGHT, DAEMON_METHOD_UNAUTHORIZED_CODE,
    DAEMON_OVERLOADED_CODE, DAEMON_PEER_UNAUTHORIZED_CODE, DAEMON_SETSOCKOPT_FAILED_CODE,
    DaemonStartError, current_euid,
};

/// Method dispatch name for the round-trip integrity check.
pub const METHOD_ECHO: &str = "ee.daemon.echo";

/// Method dispatch name for daemon protocol discovery.
pub const METHOD_CAPABILITIES: &str = "ee.daemon.capabilities";

/// Method dispatch name for graceful daemon shutdown.
pub const METHOD_SHUTDOWN: &str = "ee.daemon.shutdown";

/// Error code returned when the diagnostic echo method is not enabled.
pub const DAEMON_ECHO_DISABLED_CODE: &str = "daemon_echo_disabled";

/// Method dispatch name for the warm-loaded `ee pack` path.
pub const METHOD_CONTEXT: &str = "ee.daemon.context";

/// Error code returned when `ee.daemon.search` params fail strict decoding.
pub const DAEMON_SEARCH_PARAMS_INVALID_CODE: &str = "daemon_search_params_invalid";

/// Error code returned when canonical search execution or response encoding fails.
pub const DAEMON_SEARCH_EXECUTION_FAILED_CODE: &str = "daemon_search_execution_failed";

/// Warm-up posture of the daemon's process-cached search stack (GH #37).
///
/// The semantic arm builds a ~500k-piece Unigram tokenizer and materialises a
/// 512 MB F32 embedding matrix. Those live in process-global caches, so a warm
/// `ee.daemon.search` measured 0.70s / 59 MB in the client against 3.30s /
/// 1.58 GB for the same in-process search. A cold one does not: before this
/// warm-up the daemon stayed cold until a client asked for a search, so
/// every short-lived `ee search --use-daemon` raced a cold daemon,
/// blew the client's bounded capability deadline, fell back, and then paid the
/// full 1.6 GB in-process load itself — while the daemon loaded the same model
/// concurrently. Warming at startup is what makes `--use-daemon` actually
/// amortise the model load instead of duplicating it.
mod warm_posture {
    /// No warm-up has been attempted (unbound daemon, or warm-up disabled).
    pub const COLD: u8 = 0;
    /// A warm-up is in flight; the search stack is not resident yet.
    pub const WARMING: u8 = 1;
    /// The search stack is resident and `ee.daemon.search` is hot.
    pub const READY: u8 = 2;
    /// Warm-up ran and failed; the daemon still serves, just cold.
    pub const FAILED: u8 = 3;
}

/// Process-global warm posture. One daemon per process, so a plain static is
/// the whole state: [`start_daemon`] is the only writer that moves it off
/// [`warm_posture::COLD`], and the warm thread is the only writer thereafter.
static DAEMON_SEARCH_WARM_STATE: AtomicU8 = AtomicU8::new(warm_posture::COLD);

/// Query used to drive the warm-up. Deliberately a fixed, content-free token:
/// warm-up must never depend on, or leak, workspace content.
const DAEMON_WARM_QUERY: &str = "ee daemon warmup";

/// Wire label for the current warm posture, as advertised by
/// `ee.daemon.capabilities` under `warm.posture`.
#[must_use]
pub fn daemon_search_warm_posture() -> &'static str {
    warm_posture_label(DAEMON_SEARCH_WARM_STATE.load(Ordering::Acquire))
}

/// Pure state-to-wire-label mapping. Split out from
/// [`daemon_search_warm_posture`] so it can be tested without touching the
/// process-global state that a concurrently starting daemon also writes.
const fn warm_posture_label(state: u8) -> &'static str {
    match state {
        warm_posture::WARMING => "warming",
        warm_posture::READY => "ready",
        warm_posture::FAILED => "failed",
        _ => "cold",
    }
}

/// Error code returned when `ee.daemon.context` params cannot be
/// mapped to the canonical pack request shape.
pub const DAEMON_CONTEXT_PARAMS_INVALID_CODE: &str = "daemon_context_params_invalid";

/// Error code returned when `ee.daemon.context` refuses a request
/// before pack execution because its explicit deadline/budget has
/// already expired.
pub const DAEMON_CONTEXT_DEADLINE_EXCEEDED_CODE: &str = "daemon_context_deadline_exceeded";

/// Error code returned when the canonical pack path fails before it can
/// produce an `ee.response.v2` envelope.
pub const DAEMON_CONTEXT_EXECUTION_FAILED_CODE: &str = "daemon_context_execution_failed";

/// Method dispatch name for the live in-process contention telemetry
/// snapshot. Because the handler runs INSIDE the long-lived daemon process,
/// the group-commit / singleflight counters it returns reflect real
/// accumulated load — coalescing that a one-shot `ee` CLI invocation
/// (single-writer fallback, process-local atomics reset per process) can
/// never observe. Surfaced to operators via `ee diag contention --use-daemon`.
/// bd-d67os.12.
pub const METHOD_TELEMETRY: &str = "ee.daemon.telemetry";

/// Error code returned when the daemon fails to serialize its live
/// contention telemetry snapshot into the `ee.diag.contention.v1` result
/// payload. Non-fatal to the daemon; the client falls back to the
/// in-process snapshot path. bd-d67os.12.
pub const DAEMON_TELEMETRY_ENCODE_FAILED_CODE: &str = "daemon_telemetry_encode_failed";

/// Method dispatch name for routing a durable memory write through the
/// daemon. Inc 1 (bd-wx6ou.2) executes the write via the existing direct
/// `remember_memory` path (no actor yet) so daemon-routed writes are
/// byte-identical to `ee remember`; later increments coalesce them through a
/// long-lived group-commit actor. Workspace-scoped (`SameUidWorkspace`) like
/// `ee.daemon.context`, because a write mutates one specific workspace.
pub const METHOD_WRITE: &str = "ee.daemon.write";

/// Error code returned when `ee.daemon.write` params cannot be mapped to a
/// remember request shape (missing content, bad workspace path, etc.).
pub const DAEMON_WRITE_PARAMS_INVALID_CODE: &str = "daemon_write_params_invalid";

/// Method dispatch name for routing a durable journal write through the daemon
/// (Inc 4, bd-wx6ou.5). Like `ee.daemon.write` but for journal entries: the
/// long-lived actor coalesces a batch of journal writes into one
/// database transaction boundary (Inc 3). Workspace-scoped
/// (`SameUidWorkspace`).
pub const METHOD_WRITE_JOURNAL: &str = "ee.daemon.write_journal";

/// Error code returned when `ee.daemon.write_journal` params cannot be decoded
/// into a journal write request shape.
pub const DAEMON_JOURNAL_PARAMS_INVALID_CODE: &str = "daemon_journal_params_invalid";

/// Error code returned when a request's `method` field does not match
/// any registered handler.
pub const DAEMON_UNKNOWN_METHOD_CODE: &str = "daemon_unknown_method";

/// Error code returned when the request envelope failed to decode
/// (malformed JSON, missing required field, schema mismatch).
pub const DAEMON_REQUEST_DECODE_FAILED_CODE: &str = "daemon_request_decode_failed";

/// Error code returned when the request envelope's `schema` field does
/// not match [`super::DAEMON_REQUEST_SCHEMA_V1`].
pub const DAEMON_REQUEST_SCHEMA_MISMATCH_CODE: &str = "daemon_request_schema_mismatch";

/// Error code returned when a connection-handler panics mid-dispatch.
/// [`handle_connection`] wraps the dispatch call in
/// `std::panic::catch_unwind` so the accept loop survives, the client
/// receives a structured `daemon_handler_panic` envelope instead of a
/// torn-down connection, and the panic message is sanitized before
/// being logged to the daemon's stderr — never written back on the
/// wire — so workspace ids, memory body bytes, or anything else that
/// happened to be on the stack at the panic site cannot leak through
/// the response. bd-b82q4.
pub const DAEMON_HANDLER_PANIC_CODE: &str = "daemon_handler_panic";

/// Hard cap on the byte length of a sanitized panic message written
/// to the daemon's stderr by [`handle_connection`]. The wire envelope
/// never carries panic details; this only bounds the log line so a
/// pathological panic payload (e.g. a `Display` impl that walked a
/// huge memory body) cannot blow out the journal.
const DAEMON_PANIC_LOG_MAX_BYTES: usize = 512;
const DAEMON_WORKER_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);
const DAEMON_SCHEDULER_JOIN_TIMEOUT: Duration = Duration::from_millis(750);
const SEARCH_ADVISORY_SETTLEMENT_RETRY_LIMIT: usize = 64;

/// Per-daemon dispatch policy that is resolved at daemon start and
/// then shared by every accepted connection. Connection-level peer
/// credentials still gate local UID; this policy gates method-specific
/// workspace authority inside dispatch. bd-3mbao.
// Eq/PartialEq were derived but unused; dropped because the optional
// `write_router` (an asupersync runtime + write handle) is not comparable.
#[derive(Clone, Debug, Default)]
pub struct DaemonDispatchPolicy {
    bound_workspace_id: Option<String>,
    /// Search advisories are emitted once per active workspace condition.
    search_advisory_session: Arc<Mutex<SearchAdvisorySession>>,
    /// Set once at daemon start when the workspace is bound and the long-lived
    /// write-owner actor is hosted (Inc 2, bd-wx6ou.3). Carries the shared
    /// runtime + a clone of the actor's submit handle to `dispatch_write`
    /// without threading a new parameter through the accept/connection path,
    /// which already carries `Arc<DaemonDispatchPolicy>`. `None` ⇒ the
    /// in-process direct write path (Inc 1) is used.
    write_router: Option<DaemonWriteRouter>,
}

impl DaemonDispatchPolicy {
    /// Bind workspace-scoped daemon methods to one workspace id.
    #[must_use]
    pub fn for_workspace(workspace_id: impl Into<String>) -> Self {
        Self {
            bound_workspace_id: Some(workspace_id.into()),
            ..Self::default()
        }
    }

    fn bound_workspace_id(&self) -> Option<&str> {
        self.bound_workspace_id.as_deref()
    }

    fn write_router(&self) -> Option<&DaemonWriteRouter> {
        self.write_router.as_ref()
    }

    fn search_advisory_session(&self) -> &Mutex<SearchAdvisorySession> {
        &self.search_advisory_session
    }
}

/// Own a provisional advisory emission until it is either attached to a
/// socket response or abandoned. Early returns and unwinds release the
/// reservation without consuming the once-per-active-episode advisory.
struct PendingSearchAdvisoryDelivery<'a> {
    session: &'a Mutex<SearchAdvisorySession>,
    reservation: Option<SearchAdvisoryDeliveryReservation>,
}

impl<'a> PendingSearchAdvisoryDelivery<'a> {
    fn new(session: &'a Mutex<SearchAdvisorySession>, workspace_id: &str) -> Self {
        let reservation = session
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .reserve_delivery(workspace_id);
        Self {
            session,
            reservation: Some(reservation),
        }
    }
    // The reservation is Some from construction until `finish`/`Drop` takes it;
    // no other path can empty it (same invariant class as lock_linearization_gate).
    #[allow(clippy::expect_used)]
    fn reservation_mut(&mut self) -> &mut SearchAdvisoryDeliveryReservation {
        self.reservation
            .as_mut()
            .expect("pending advisory delivery must retain its reservation")
    }

    #[allow(clippy::expect_used)]
    fn finish(
        mut self,
        response: DaemonResponse,
        defer_until_socket_write: bool,
    ) -> DaemonResponse {
        let reservation = self
            .reservation
            .take()
            .expect("pending advisory delivery must retain its reservation");
        if !reservation.requires_settlement() {
            return response;
        }
        if defer_until_socket_write {
            return response.with_search_advisory_delivery(
                reservation.workspace_id(),
                reservation.token(),
                reservation.large_gap_capacity_busy(),
            );
        }
        settle_search_advisory_delivery(
            self.session,
            reservation.workspace_id(),
            reservation.token(),
            true,
            reservation.large_gap_capacity_busy(),
        );
        response
    }
}

impl Drop for PendingSearchAdvisoryDelivery<'_> {
    fn drop(&mut self) {
        let Some(reservation) = self.reservation.take() else {
            return;
        };
        if reservation.requires_settlement() {
            settle_search_advisory_delivery(
                self.session,
                reservation.workspace_id(),
                reservation.token(),
                false,
                reservation.large_gap_capacity_busy(),
            );
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DaemonAuthority {
    SameUid,
    SameUidWorkspace,
}

impl DaemonAuthority {
    const fn as_wire_label(self) -> &'static str {
        match self {
            Self::SameUid => "same_uid",
            Self::SameUidWorkspace => "same_uid_workspace",
        }
    }
}

#[derive(Debug)]
struct DaemonSocketPublishLock {
    _file: File,
}

/// Bounded-pool permit. A clone-on-acquire counter that decrements on
/// drop, used to cap the number of in-flight per-connection worker
/// threads. The cap defends against the bd-jnyui local-DoS vector:
/// before this gate, a single attacker could fork-bomb the daemon
/// because `run_accept_loop` spawned a new `std::thread::Builder` per
/// accept with no semaphore or queue depth.
#[derive(Debug)]
struct InflightPermit {
    pool: Arc<InflightPool>,
}

#[allow(clippy::expect_used)]
impl Drop for InflightPermit {
    fn drop(&mut self) {
        let mut current = self
            .pool
            .inflight
            .lock()
            .expect("daemon inflight mutex must not be poisoned");
        *current = current.saturating_sub(1);
        if *current == 0 {
            self.pool.idle.notify_all();
        }
    }
}

/// Shared, hand-rolled counting semaphore for the accept loop. The
/// counter is bumped on `try_acquire` (returning a permit) and
/// decremented when the permit is dropped — typically when the worker
/// thread exits.
#[derive(Debug)]
struct InflightPool {
    capacity: usize,
    inflight: Mutex<usize>,
    idle: Condvar,
}

#[allow(clippy::expect_used)]
impl InflightPool {
    fn new(capacity: usize) -> Arc<Self> {
        // A capacity of zero would refuse every connection and is never
        // useful in production. Clamp to one so an operator misreading
        // the env var still gets a serializable daemon rather than a
        // dead socket.
        let capacity = capacity.max(1);
        Arc::new(Self {
            capacity,
            inflight: Mutex::new(0),
            idle: Condvar::new(),
        })
    }

    fn try_acquire(self: &Arc<Self>) -> Option<InflightPermit> {
        let mut current = self
            .inflight
            .lock()
            .expect("daemon inflight mutex must not be poisoned");
        if *current >= self.capacity {
            return None;
        }
        *current += 1;
        Some(InflightPermit {
            pool: Arc::clone(self),
        })
    }

    fn wait_until_idle(&self, timeout: Duration) -> bool {
        let deadline = Instant::now() + timeout;
        let mut current = self
            .inflight
            .lock()
            .expect("daemon inflight mutex must not be poisoned");
        while *current > 0 {
            let now = Instant::now();
            if now >= deadline {
                return false;
            }
            let remaining = deadline.saturating_duration_since(now);
            let (next, result) = self
                .idle
                .wait_timeout(current, remaining)
                .expect("daemon inflight condvar must not be poisoned");
            current = next;
            if result.timed_out() && *current > 0 {
                return false;
            }
        }
        true
    }
}

#[derive(Debug)]
enum SchedulerThreadExit {
    Returned,
    Panicked,
}

#[derive(Debug)]
struct SchedulerThreadHandle {
    join: JoinHandle<()>,
    done_rx: mpsc::Receiver<SchedulerThreadExit>,
}

enum SchedulerJoinOutcome {
    Joined(io::Result<()>),
    StillRunning(SchedulerThreadHandle),
}

impl SchedulerThreadHandle {
    fn join_with_timeout(self, timeout: Duration) -> SchedulerJoinOutcome {
        match self.done_rx.recv_timeout(timeout) {
            Ok(SchedulerThreadExit::Returned) => SchedulerJoinOutcome::Joined(
                self.join
                    .join()
                    .map_err(|_| io::Error::other("daemon scheduler thread panicked")),
            ),
            Ok(SchedulerThreadExit::Panicked) => {
                let _ = self.join.join();
                SchedulerJoinOutcome::Joined(Err(io::Error::other(
                    "daemon scheduler thread panicked",
                )))
            }
            Err(mpsc::RecvTimeoutError::Timeout) => SchedulerJoinOutcome::StillRunning(self),
            Err(mpsc::RecvTimeoutError::Disconnected) => SchedulerJoinOutcome::Joined(
                self.join
                    .join()
                    .map_err(|_| io::Error::other("daemon scheduler thread panicked")),
            ),
        }
    }
}

/// Resolve the configured per-daemon worker cap. Reads
/// `EE_DAEMON_MAX_INFLIGHT` through the central env registry so the
/// override is reflected in `ee capabilities` alongside other tuning
/// knobs; a missing, unparseable, or zero value falls back to
/// [`super::DAEMON_MAX_INFLIGHT`].
fn configured_max_inflight() -> usize {
    env_registry::read(EnvVar::DaemonMaxInflight)
        .and_then(|raw| raw.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(DAEMON_MAX_INFLIGHT)
}

/// Whether the diagnostic echo method should be exposed. The method
/// reflects caller-supplied content, so production daemons keep it off
/// unless a local diagnostic run explicitly opts in.
fn daemon_echo_enabled() -> bool {
    env_registry::read_or_default(EnvVar::DaemonEnableEcho)
        .is_some_and(|value| daemon_echo_env_value_truthy(&value))
}

fn daemon_echo_env_value_truthy(value: &str) -> bool {
    let value = value.trim();
    value == "1"
        || value.eq_ignore_ascii_case("true")
        || value.eq_ignore_ascii_case("yes")
        || value.eq_ignore_ascii_case("on")
}

/// Handle returned by [`start_server`]. Holds the accept-loop thread
/// and the shutdown signal; dropping it does NOT stop the server
/// (callers must call [`DaemonServerHandle::shutdown`] explicitly so
/// the socket file is unlinked deterministically).
pub struct DaemonServerHandle {
    socket_path: PathBuf,
    shutdown: Arc<AtomicBool>,
    pool: Arc<InflightPool>,
    accept_thread: Option<JoinHandle<()>>,
    /// Background steward scheduler thread (bd-2ohzq). `None` when the
    /// daemon was started without a bound workspace (e.g. via the bare
    /// `start_server` entry point). The thread runs until `shutdown` fires.
    scheduler_thread: Option<SchedulerThreadHandle>,
    /// Startup search-stack warm-up thread (GH #37). `None` when the daemon has
    /// no bound workspace or `EE_DAEMON_WARM=off`. Detached (not joined) at
    /// shutdown: a cold-cache warm can take tens of seconds and cannot be
    /// interrupted mid-search, so joining it would stall teardown.
    warm_thread: Option<JoinHandle<()>>,
    /// Once-guard for accept-loop and socket teardown. The first
    /// shutdown call stops the listener and unlinks the socket; later
    /// calls skip that irreversible section but may still wait for a
    /// previously timed-out worker drain.
    shutdown_done: AtomicBool,
    /// Set only after all per-connection worker permits have dropped.
    /// Kept separate from `shutdown_done` so a timed-out explicit
    /// shutdown does not make later calls falsely report success.
    workers_drained: AtomicBool,
    /// The retained clone of the write-owner actor's submit handle (Inc 2,
    /// bd-wx6ou.3). Connection threads use clones carried in the dispatch
    /// policy's `DaemonWriteRouter`; this one is dropped LAST in shutdown so the
    /// actor's mpsc closes (→ `recv` returns `Disconnected` → the actor loop
    /// breaks) only after the accept loop and connection workers have drained.
    /// `None` when the daemon was started without a bound workspace.
    write_handle: Option<crate::core::write_owner::WriteHandle>,
    /// Join handle for the long-lived write-owner actor task. Joined during
    /// shutdown after the write handle drops, so the final batch commits before
    /// the runtime is torn down.
    write_owner_task: Option<asupersync::runtime::JoinHandle<()>>,
    /// The asupersync runtime that drives the actor task and connection-thread
    /// `block_on` submits. Dropped last (joins its worker threads). Shared as
    /// `Arc` with the dispatch policy's `DaemonWriteRouter`.
    write_runtime: Option<Arc<asupersync::runtime::Runtime>>,
}

impl std::fmt::Debug for DaemonServerHandle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // The write runtime/actor handles have no meaningful Debug; summarize
        // whether the actor is hosted rather than recursing into them.
        f.debug_struct("DaemonServerHandle")
            .field("socket_path", &self.socket_path)
            .field("accept_thread", &self.accept_thread.is_some())
            .field("scheduler_thread", &self.scheduler_thread.is_some())
            .field("warm_thread", &self.warm_thread.is_some())
            .field("write_actor_hosted", &self.write_handle.is_some())
            .finish_non_exhaustive()
    }
}

impl DaemonServerHandle {
    /// Return the bound socket path for status surfaces / tests.
    #[must_use]
    pub fn socket_path(&self) -> &Path {
        &self.socket_path
    }

    /// Whether any daemon control path has requested shutdown. The
    /// foreground CLI process uses this to turn an RPC shutdown request
    /// into an explicit [`DaemonServerHandle::shutdown`] call so the
    /// accept loop, background scheduler, and socket teardown all join
    /// on the owning thread.
    #[must_use]
    pub fn shutdown_requested(&self) -> bool {
        self.shutdown.load(Ordering::SeqCst)
    }

    /// Signal the accept loop to stop and wait for it to drain. Also
    /// removes the socket file from the filesystem so a subsequent
    /// `start_server` call against the same path does not need a
    /// manual cleanup step.
    pub fn shutdown(&mut self) -> io::Result<()> {
        self.shutdown_with_worker_drain_timeout(DAEMON_WORKER_DRAIN_TIMEOUT)
    }

    fn shutdown_with_worker_drain_timeout(
        &mut self,
        worker_drain_timeout: Duration,
    ) -> io::Result<()> {
        if self.workers_drained.load(Ordering::Acquire) && self.scheduler_thread.is_none() {
            return Ok(());
        }

        self.shutdown.store(true, Ordering::SeqCst);
        // The warm-up is a pure optimisation running a single bounded search.
        // It checks `shutdown` before it starts but cannot be interrupted once
        // inside the search, and a cold-cache warm can take tens of seconds.
        // Detach it rather than joining so shutdown stays prompt; the thread
        // holds no socket, no listener and no write handle, and dies with the
        // process.
        drop(self.warm_thread.take());
        let first_teardown = !self.shutdown_done.swap(true, Ordering::AcqRel);
        let unlink_result = if first_teardown {
            // Wake the accept loop by connecting to the socket from the
            // current process; the loop checks the shutdown flag between
            // accepts but blocks inside `accept()` itself. The connect-
            // and-immediately-drop pattern unblocks the listener cheaply.
            let _ = UnixStream::connect(&self.socket_path);
            if let Some(handle) = self.accept_thread.take() {
                handle
                    .join()
                    .map_err(|_| io::Error::other("daemon accept thread panicked"))?;
            }
            // Idempotent guarded unlink (bd-wj6v9, bd-2z3e8): tolerate
            // `NotFound`, but do not let shutdown delete an arbitrary
            // regular file if the socket path was swapped underneath us.
            SocketBroker::new(self.socket_path.clone()).remove_owned_socket_file()
        } else {
            Ok(())
        };
        let scheduler_result = self.join_scheduler_with_timeout();

        let workers_drained = self.pool.wait_until_idle(worker_drain_timeout);
        if workers_drained {
            self.workers_drained.store(true, Ordering::Release);
        }

        // Tear down the write-owner actor (Inc 2, bd-wx6ou.3) AFTER the accept
        // loop joined and connection workers drained: by now the dispatch
        // policy's router (a WriteHandle clone) is dropped, so dropping the
        // retained handle closes the actor's mpsc, its `recv` returns
        // Disconnected, and `run_group_commit` returns. Joining the actor task
        // ensures the final write commits before the runtime is dropped (which
        // joins its worker threads). The `is_finished` guard plus `catch_unwind`
        // keep a force-cancelled/panicked actor from hanging or unwinding
        // shutdown. (TODO Inc 7: bound `block_on` with a timeout so a write
        // wedged inside `remember_memory` cannot stall shutdown.)
        self.write_handle.take();
        if let (Some(runtime), Some(task)) =
            (self.write_runtime.as_ref(), self.write_owner_task.take())
            && !task.is_finished()
        {
            let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                runtime.block_on(task);
            }));
        }
        self.write_runtime.take();

        unlink_result?;
        scheduler_result?;
        if !workers_drained {
            return Err(io::Error::new(
                io::ErrorKind::TimedOut,
                format!(
                    "daemon worker threads did not drain within {}ms",
                    worker_drain_timeout.as_millis()
                ),
            ));
        }
        Ok(())
    }

    fn join_scheduler_with_timeout(&mut self) -> io::Result<()> {
        let Some(handle) = self.scheduler_thread.take() else {
            return Ok(());
        };
        match handle.join_with_timeout(DAEMON_SCHEDULER_JOIN_TIMEOUT) {
            SchedulerJoinOutcome::Joined(result) => result,
            SchedulerJoinOutcome::StillRunning(handle) => {
                self.scheduler_thread = Some(handle);
                Err(io::Error::new(
                    io::ErrorKind::TimedOut,
                    format!(
                        "daemon scheduler thread did not stop within {}ms after shutdown",
                        DAEMON_SCHEDULER_JOIN_TIMEOUT.as_millis()
                    ),
                ))
            }
        }
    }
}

impl Drop for DaemonServerHandle {
    fn drop(&mut self) {
        // Best-effort cleanup: signal shutdown and join the accept
        // thread so the socket file is unlinked even if the caller
        // forgot to call `shutdown`. Errors are intentionally
        // swallowed in Drop; the explicit `shutdown` method surfaces
        // them when callers care.
        let _ = self.shutdown();
    }
}

#[derive(Debug)]
/// Owns daemon UDS publication and guarded cleanup invariants.
struct SocketBroker {
    socket_path: PathBuf,
}

impl SocketBroker {
    fn new(socket_path: impl Into<PathBuf>) -> Self {
        Self {
            socket_path: socket_path.into(),
        }
    }

    fn socket_path(&self) -> &Path {
        &self.socket_path
    }

    fn publish_listener(
        &self,
    ) -> Result<(UnixListener, DaemonSocketPublishLock), DaemonStartError> {
        self.ensure_private_parent()?;
        let publish_lock = self.acquire_publish_lock()?;
        self.refuse_non_socket_or_live_existing()?;
        let listener = self.bind_secured_temp_listener()?;
        Ok((listener, publish_lock))
    }

    fn ensure_private_parent(&self) -> Result<(), DaemonStartError> {
        if let Some(parent) = self.socket_path.parent()
            && !parent.as_os_str().is_empty()
        {
            // The parent directory must be a same-user private boundary:
            // it contains the publish lock, temp socket, and canonical
            // socket path. `DirBuilder` applies 0o700 only to components it
            // creates, so validate the resulting parent before opening the
            // start lock or binding a socket inside it.
            fs::DirBuilder::new()
                .recursive(true)
                .mode(0o700)
                .create(parent)
                .map_err(|source| DaemonStartError::SocketDirCreate {
                    path: parent.to_path_buf(),
                    source,
                })?;
            Self::validate_socket_parent(parent)?;
            Self::validate_socket_ancestor_chain(parent)?;
        }
        Ok(())
    }

    fn acquire_publish_lock(&self) -> Result<DaemonSocketPublishLock, DaemonStartError> {
        let lock_path = self.socket_publish_lock_path();
        let file = Self::open_daemon_socket_lock_file(&lock_path).map_err(|source| {
            DaemonStartError::Bind {
                path: self.socket_path.clone(),
                source,
            }
        })?;
        flock(&file, FlockOperation::LockExclusive).map_err(|source| DaemonStartError::Bind {
            path: self.socket_path.clone(),
            source: io::Error::from(source),
        })?;
        Ok(DaemonSocketPublishLock { _file: file })
    }

    fn refuse_non_socket_or_live_existing(&self) -> Result<(), DaemonStartError> {
        // Refuse to clobber a non-socket file already occupying the
        // canonical path, but do NOT pre-`remove_file` it. The former
        // stat -> remove_file -> bind sequence had two TOCTOU windows: an
        // attacker with write access to the parent directory could swap
        // the socket for a regular file between the `is_socket()` check
        // and the `remove_file` (falsifying the daemon's "this is my
        // stale socket" belief), or recreate the path between
        // `remove_file` and `bind`. We instead bind a per-attempt temp
        // path and `rename(2)` it onto the canonical name atomically
        // (below); `rename` replaces any existing socket cleanly with no
        // unlink step, collapsing both windows into one operation. The
        // parent directory is per-UID 0o700 (bd-3j0td), so no other UID
        // can race us inside it. Sentinel: bd-3ik2d atomic-rename.
        match fs::symlink_metadata(&self.socket_path) {
            Ok(metadata) => {
                if !metadata.file_type().is_socket() {
                    return Err(DaemonStartError::SocketPathOccupied {
                        path: self.socket_path.clone(),
                    });
                }
                if self.existing_socket_accepts_connection() {
                    return Err(DaemonStartError::AlreadyRunning {
                        path: self.socket_path.clone(),
                    });
                }
                // A dead socket from a prior daemon: the `rename` below
                // atomically replaces it, so there is nothing to unlink.
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
            Err(source) => {
                return Err(DaemonStartError::Bind {
                    path: self.socket_path.clone(),
                    source,
                });
            }
        }
        Ok(())
    }

    fn bind_secured_temp_listener(&self) -> Result<UnixListener, DaemonStartError> {
        // Bind a per-attempt temporary path inside the same (per-UID
        // 0o700) parent directory, tighten its mode to 0o600 BEFORE it is
        // ever visible at the canonical name, then `rename(2)` it into
        // place. Because the chmod happens on the temp path, the canonical
        // path never exists in a world-connectable (0o755) state for even
        // an instant — a strict improvement over chmod-after-bind. The
        // temp name carries a UUID v7 so concurrent and post-crash binds
        // do not reuse a predictable pathname, and each `rename` is a clean
        // atomic publish. Sentinel: bd-3ik2d atomic-rename.
        let tmp_path = self.temp_bind_path();
        self.bind_secured_temp_listener_at(&tmp_path)
    }

    fn bind_secured_temp_listener_at(
        &self,
        tmp_path: &Path,
    ) -> Result<UnixListener, DaemonStartError> {
        // Never unlink an unexpected pre-existing temp path. The randomized
        // UUID makes a collision vanishingly unlikely, while bind(2)'s
        // create-only behavior turns either a crash remnant or a planted
        // path into an explicit refusal instead of deleting somebody else's
        // file under a daemon-controlled name.

        let listener = UnixListener::bind(tmp_path).map_err(|source| DaemonStartError::Bind {
            path: self.socket_path.clone(),
            source,
        })?;

        // Tighten the temp socket to mode 0o600 before it is published.
        // `UnixListener::bind` honours the process umask (typically 0o022
        // -> mode 0o755): world-connectable on every Unix host. Without
        // this chmod, any local UID could `connect(2)` and reach the
        // dispatch table — the attack surface documented in bd-3j0td. The
        // chmod failure is surfaced (not swallowed) so an operator on a
        // filesystem that rejects `chmod` sees the bind step error rather
        // than a silently world-open socket. Sentinel: bd-3j0td chmod-0600.
        if let Err(source) = fs::set_permissions(tmp_path, fs::Permissions::from_mode(0o600)) {
            // The temp socket is bound but world-open at this instant;
            // remove it before returning so a half-secured artifact does
            // not linger under the temp name.
            let _ = Self::remove_owned_socket_path(tmp_path);
            return Err(DaemonStartError::Bind {
                path: self.socket_path.clone(),
                source,
            });
        }

        // Atomically publish the secured socket at the canonical path.
        // `rename(2)` is atomic and replaces a stale socket left by a
        // prior daemon in a single step.
        if let Err(source) = fs::rename(tmp_path, &self.socket_path) {
            // Publish failed (e.g. cross-device move, or the parent dir
            // was removed underneath us). Drop the temp socket so it does
            // not linger, then surface the failure.
            let _ = Self::remove_owned_socket_path(tmp_path);
            return Err(DaemonStartError::Bind {
                path: self.socket_path.clone(),
                source,
            });
        }

        Ok(listener)
    }

    fn remove_owned_socket_file(&self) -> io::Result<()> {
        Self::remove_owned_socket_path(&self.socket_path)
    }

    fn remove_owned_socket_path(path: &Path) -> io::Result<()> {
        match fs::symlink_metadata(path) {
            Ok(metadata) => {
                if !metadata.file_type().is_socket() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidInput,
                        format!(
                            "refusing to remove non-socket daemon path {}",
                            path.display()
                        ),
                    ));
                }
                let euid = current_euid();
                if metadata.uid() != euid {
                    return Err(io::Error::new(
                        io::ErrorKind::PermissionDenied,
                        format!(
                            "refusing to remove daemon socket {} owned by uid {}, current uid {euid}",
                            path.display(),
                            metadata.uid()
                        ),
                    ));
                }
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
            Err(error) => return Err(error),
        }

        match fs::remove_file(path) {
            Ok(()) => Ok(()),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
            Err(error) => Err(error),
        }
    }

    /// Construct a per-attempt temporary socket path next to `socket_path`,
    /// of the form `<socket>.tmp.<uuid-v7>`. UUID v7 combines process-local
    /// monotonicity with random bits, keeping concurrent and post-crash
    /// attempts distinct without ever deleting a colliding path.
    /// Sentinel: bd-3ik2d atomic-rename.
    fn temp_bind_path(&self) -> PathBuf {
        let suffix = format!(".tmp.{}", uuid::Uuid::now_v7().simple());
        let mut file_name = self
            .socket_path
            .file_name()
            .map(|name| name.to_os_string())
            .unwrap_or_default();
        file_name.push(&suffix);
        let mut tmp_path = self.socket_path.clone();
        tmp_path.set_file_name(file_name);
        tmp_path
    }

    fn socket_publish_lock_path(&self) -> PathBuf {
        let mut file_name = self
            .socket_path
            .file_name()
            .map(|name| name.to_os_string())
            .unwrap_or_else(|| std::ffi::OsString::from("daemon.sock"));
        file_name.push(".start.lock");
        let mut lock_path = self.socket_path.clone();
        lock_path.set_file_name(file_name);
        lock_path
    }

    fn validate_socket_parent(parent: &Path) -> Result<(), DaemonStartError> {
        let metadata =
            fs::symlink_metadata(parent).map_err(|source| DaemonStartError::SocketDirCreate {
                path: parent.to_path_buf(),
                source,
            })?;
        if !metadata.file_type().is_dir() {
            return Err(DaemonStartError::InsecureSocketParent {
                path: parent.to_path_buf(),
                reason: "parent is not a real directory".to_owned(),
            });
        }

        let euid = current_euid();
        if metadata.uid() != euid {
            return Err(DaemonStartError::InsecureSocketParent {
                path: parent.to_path_buf(),
                reason: format!(
                    "parent is owned by uid {}, not current uid {euid}",
                    metadata.uid()
                ),
            });
        }

        let mode = metadata.permissions().mode() & 0o777;
        if mode & 0o077 != 0 {
            return Err(DaemonStartError::InsecureSocketParent {
                path: parent.to_path_buf(),
                reason: format!(
                    "parent mode 0o{mode:o} grants group or other access; expected 0o700 or stricter"
                ),
            });
        }

        Ok(())
    }

    fn validate_socket_ancestor_chain(parent: &Path) -> Result<(), DaemonStartError> {
        let euid = current_euid();
        let mut ancestors = parent.ancestors().collect::<Vec<_>>();
        ancestors.reverse();

        for ancestor in ancestors {
            let metadata = fs::symlink_metadata(ancestor).map_err(|source| {
                DaemonStartError::SocketDirCreate {
                    path: ancestor.to_path_buf(),
                    source,
                }
            })?;
            if metadata.file_type().is_symlink() {
                // System-owned compatibility links such as macOS `/var` ->
                // `/private/var` are stable because their containing parent
                // is checked earlier in this root-to-leaf walk. A link owned
                // by an unrelated uid is not a trustworthy socket ancestor.
                if !matches!(metadata.uid(), 0) && metadata.uid() != euid {
                    return Err(DaemonStartError::InsecureSocketParent {
                        path: ancestor.to_path_buf(),
                        reason: format!(
                            "ancestor symlink is owned by uid {}, not root or current uid {euid}",
                            metadata.uid()
                        ),
                    });
                }
                continue;
            }
            if !metadata.file_type().is_dir() {
                return Err(DaemonStartError::InsecureSocketParent {
                    path: ancestor.to_path_buf(),
                    reason: "ancestor is not a directory or trusted symlink".to_owned(),
                });
            }

            let mode = metadata.permissions().mode();
            let writable_by_other_principals = mode & 0o022 != 0;
            let sticky = mode & 0o1000 != 0;
            if writable_by_other_principals && !sticky {
                return Err(DaemonStartError::InsecureSocketParent {
                    path: ancestor.to_path_buf(),
                    reason: format!(
                        "ancestor mode 0o{:o} permits non-owner rename without sticky protection",
                        mode & 0o7777
                    ),
                });
            }
        }

        Ok(())
    }

    fn open_daemon_socket_lock_file(path: &Path) -> io::Result<File> {
        let mut options = OpenOptions::new();
        options.create(true).truncate(false).read(true).write(true);
        Self::configure_daemon_socket_lock_options(&mut options);
        options.open(path)
    }

    fn configure_daemon_socket_lock_options(options: &mut OpenOptions) {
        use std::os::unix::fs::OpenOptionsExt;

        options
            .mode(0o600)
            .custom_flags(rustix::fs::OFlags::NOFOLLOW.bits() as i32);
    }

    fn existing_socket_accepts_connection(&self) -> bool {
        UnixStream::connect(&self.socket_path).is_ok()
    }
}

/// Bind a UDS at `socket_path` and spawn the accept loop. Returns a
/// handle the caller can use to query the bound path and to signal
/// shutdown. The accept loop runs until [`DaemonServerHandle::shutdown`]
/// is called or the handle is dropped.
///
/// The function refuses to overwrite a non-socket file at
/// `socket_path`; the operator must remove a stale path explicitly so
/// the daemon never silently truncates a regular file someone
/// accidentally pointed it at.
pub fn start_server(
    socket_path: impl Into<PathBuf>,
) -> Result<DaemonServerHandle, DaemonStartError> {
    start_server_with_dispatch_policy(socket_path, DaemonDispatchPolicy::default())
}

/// Bind a UDS at `socket_path` for a daemon scoped to `workspace_id`.
/// Workspace-bound methods (currently `ee.daemon.context`) must carry
/// the same workspace id in their request envelope or dispatch refuses
/// them with `daemon_method_unauthorized`.
pub fn start_server_for_workspace(
    socket_path: impl Into<PathBuf>,
    workspace_id: impl Into<String>,
) -> Result<DaemonServerHandle, DaemonStartError> {
    start_server_with_dispatch_policy(
        socket_path,
        DaemonDispatchPolicy::for_workspace(workspace_id),
    )
}

fn start_server_with_dispatch_policy(
    socket_path: impl Into<PathBuf>,
    mut dispatch_policy: DaemonDispatchPolicy,
) -> Result<DaemonServerHandle, DaemonStartError> {
    let broker = SocketBroker::new(socket_path);
    let (listener, _publish_lock) = broker.publish_listener()?;
    let socket_path = broker.socket_path().to_path_buf();

    let shutdown = Arc::new(AtomicBool::new(false));
    let shutdown_in_thread = Arc::clone(&shutdown);
    let listener_path_in_thread = socket_path.clone();
    let pool = InflightPool::new(configured_max_inflight());
    let pool_in_thread = Arc::clone(&pool);

    // Host the long-lived write-owner actor when bound to a workspace (Inc 2,
    // bd-wx6ou.3): a current_thread asupersync runtime drives the actor task;
    // a `DaemonWriteRouter` (shared runtime + handle clone) is stashed in the
    // dispatch policy so `dispatch_write` routes through it. The accept and
    // connection paths already carry `Arc<DaemonDispatchPolicy>`, so no extra
    // parameter threading is needed.
    let (write_runtime, write_handle, write_owner_task) =
        if dispatch_policy.bound_workspace_id.is_some() {
            let runtime = Arc::new(crate::core::build_cli_runtime().map_err(|error| {
                DaemonStartError::Bind {
                    path: socket_path.clone(),
                    source: io::Error::other(format!(
                        "failed to build daemon write runtime: {error}"
                    )),
                }
            })?);
            let (owner, write_handle) = crate::core::write_owner::WriteOwner::new(
                crate::core::write_owner::DEFAULT_CHANNEL_CAPACITY,
            );
            let owner_task = runtime
                .handle()
                .try_spawn(async move {
                    let Some(cx) = asupersync::Cx::current() else {
                        return;
                    };
                    // Inc 3/5: enable group-commit so the actor accumulates a
                    // batch; transaction-coalescible daemon journal/outcome ops
                    // share one DB transaction boundary, while remember ops
                    // (which open their own connection) stay per-op.
                    let write_config = crate::core::write_owner::WriteHotPathConfig {
                        enabled: true,
                        group_commit_max_rows: 64,
                        group_commit_max_us: 1_000,
                        ..crate::core::write_owner::WriteHotPathConfig::default()
                    };
                    let _ = owner
                        .run_group_commit(&cx, write_config, |operations| {
                            if batch_is_daemon_txn_coalescible(operations) {
                                execute_daemon_txn_batch(operations)
                            } else {
                                Ok(operations
                                    .iter()
                                    .map(execute_write_operation)
                                    .collect::<Vec<_>>())
                            }
                        })
                        .await;
                })
                .map_err(|error| DaemonStartError::Bind {
                    path: socket_path.clone(),
                    source: io::Error::other(format!(
                        "failed to spawn daemon write actor: {error}"
                    )),
                })?;
            dispatch_policy.write_router = Some(DaemonWriteRouter {
                runtime: Arc::clone(&runtime),
                handle: write_handle.clone(),
            });
            (Some(runtime), Some(write_handle), Some(owner_task))
        } else {
            (None, None, None)
        };

    let dispatch_policy = Arc::new(dispatch_policy);
    let dispatch_policy_in_thread = Arc::clone(&dispatch_policy);
    let (accept_ready_tx, accept_ready_rx) = mpsc::channel();

    let accept_thread = thread::Builder::new()
        .name("ee-daemon-accept".to_owned())
        .spawn(move || {
            let _ = accept_ready_tx.send(());
            run_accept_loop(
                listener,
                listener_path_in_thread,
                shutdown_in_thread,
                pool_in_thread,
                dispatch_policy_in_thread,
            );
        })
        .map_err(|source| DaemonStartError::Bind {
            path: socket_path.clone(),
            source,
        })?;
    if let Err(source) = accept_ready_rx.recv() {
        let _ = accept_thread.join();
        return Err(DaemonStartError::Bind {
            path: socket_path,
            source: io::Error::other(format!(
                "daemon accept thread did not report readiness: {source}"
            )),
        });
    }

    // Spawn the background steward scheduler when the daemon is bound to a
    // workspace (bd-2ohzq). Uses the same `shutdown` signal as the accept
    // loop so a single `DaemonServerHandle::shutdown()` call stops both.
    let scheduler_thread = dispatch_policy
        .bound_workspace_id
        .as_deref()
        .and_then(|workspace| {
            let scheduler_shutdown = Arc::clone(&shutdown);
            let workspace = workspace.to_owned();
            let (done_tx, done_rx) = mpsc::channel();
            let join = thread::Builder::new()
                .name("ee-daemon-steward".to_owned())
                .spawn(move || {
                    let exit = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                        crate::steward::run_daemon_background_scheduler(
                            &workspace,
                            scheduler_shutdown,
                        );
                    })) {
                        Ok(()) => SchedulerThreadExit::Returned,
                        Err(_) => SchedulerThreadExit::Panicked,
                    };
                    let _ = done_tx.send(exit);
                })
                .ok()?;
            Some(SchedulerThreadHandle { join, done_rx })
        });

    // GH #37: warm the search stack once, at startup, for a workspace-bound
    // daemon. Without this the daemon stays cold until the first client
    // request, so the first `--use-daemon` search blows the client's bounded
    // deadline, falls back, and pays the whole model load in-process while the
    // daemon loads it too. `bound_workspace_id` is the canonical workspace
    // path (see `DaemonSearchParams::into_search_parts`).
    let warm_thread = dispatch_policy
        .bound_workspace_id
        .as_deref()
        .and_then(|workspace| spawn_search_warm_thread(PathBuf::from(workspace), &shutdown));

    Ok(DaemonServerHandle {
        socket_path,
        shutdown,
        pool,
        accept_thread: Some(accept_thread),
        scheduler_thread,
        warm_thread,
        shutdown_done: AtomicBool::new(false),
        workers_drained: AtomicBool::new(false),
        write_handle,
        write_owner_task,
        write_runtime,
    })
}

/// Whether startup warm-up is enabled (`EE_DAEMON_WARM`, default on).
fn daemon_warm_enabled() -> bool {
    daemon_warm_enabled_from_env_value(env_registry::read(EnvVar::DaemonWarm).as_deref())
}

/// Pure policy seam for [`daemon_warm_enabled`], so the opt-out is testable
/// without mutating process environment from a parallel test.
fn daemon_warm_enabled_from_env_value(value: Option<&str>) -> bool {
    !value.is_some_and(|value| {
        matches!(
            value.trim().to_ascii_lowercase().as_str(),
            "off" | "0" | "false" | "no"
        )
    })
}

/// Canonical warm-up: run one bounded search through the very same path
/// `ee.daemon.search` uses, so every process-global cache the hot path depends
/// on (embedder stack, tokenizer, embedding matrix, index reader, read pool) is
/// resident before the first client request arrives.
///
/// Deliberately reuses [`run_search_with_performance_and_filters`] rather than
/// reaching into the embedder internals: warming a *different* path than the
/// one being served is how warm-ups silently stop warming anything.
fn warm_daemon_search_stack(workspace_path: &Path) -> Result<(), crate::core::search::SearchError> {
    let options = SearchOptions {
        workspace_path: workspace_path.to_path_buf(),
        database_path: None,
        index_dir: None,
        query: DAEMON_WARM_QUERY.to_owned(),
        limit: 1,
        speed: SpeedMode::default(),
        explain: false,
        as_of: None,
        include_tombstoned: false,
        include_expired: false,
        include_future: false,
        include_stale: false,
        relevance_floor: None,
        dedup_mode: SearchDedupMode::default(),
        source_mode: SearchSourceMode::default(),
        strict_source_mode: false,
        memory_scope: MemoryScope::default(),
        strict_scope: false,
    };
    run_search_with_performance_and_filters(&options, None, &[]).map(|_| ())
}

/// Spawn the startup warm-up thread for a workspace-bound daemon.
///
/// Returns `None` when warm-up is disabled or the thread cannot be spawned;
/// neither is fatal, because a cold daemon still serves correctly — just
/// slowly, which is exactly the state this exists to avoid.
fn spawn_search_warm_thread(
    workspace_path: PathBuf,
    shutdown: &Arc<AtomicBool>,
) -> Option<JoinHandle<()>> {
    if !daemon_warm_enabled() {
        return None;
    }
    let shutdown = Arc::clone(shutdown);
    DAEMON_SEARCH_WARM_STATE.store(warm_posture::WARMING, Ordering::Release);
    let spawned = thread::Builder::new()
        .name("ee-daemon-warm".to_owned())
        .spawn(move || {
            if shutdown.load(Ordering::SeqCst) {
                DAEMON_SEARCH_WARM_STATE.store(warm_posture::COLD, Ordering::Release);
                return;
            }
            let started = Instant::now();
            // A panic in the warm path must not take the daemon down: it is a
            // pure optimisation, and the accept loop is already serving.
            let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                warm_daemon_search_stack(&workspace_path)
            }));
            match outcome {
                Ok(Ok(())) => {
                    DAEMON_SEARCH_WARM_STATE.store(warm_posture::READY, Ordering::Release);
                    tracing::info!(
                        target: "ee::daemon::warm",
                        elapsed_ms = u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX),
                        "daemon search stack warmed"
                    );
                }
                Ok(Err(error)) => {
                    DAEMON_SEARCH_WARM_STATE.store(warm_posture::FAILED, Ordering::Release);
                    tracing::warn!(
                        target: "ee::daemon::warm",
                        error = %error,
                        "daemon search warm-up failed; daemon still serves from a cold cache"
                    );
                }
                Err(_) => {
                    DAEMON_SEARCH_WARM_STATE.store(warm_posture::FAILED, Ordering::Release);
                    tracing::warn!(
                        target: "ee::daemon::warm",
                        "daemon search warm-up panicked; daemon still serves from a cold cache"
                    );
                }
            }
        });
    match spawned {
        Ok(handle) => Some(handle),
        Err(_) => {
            DAEMON_SEARCH_WARM_STATE.store(warm_posture::COLD, Ordering::Release);
            None
        }
    }
}

trait ConnectionWorkerSpawner {
    fn spawn_connection_worker(
        &self,
        stream: UnixStream,
        shutdown: Arc<AtomicBool>,
        dispatch_policy: Arc<DaemonDispatchPolicy>,
        metrics: Arc<dyn super::metrics::DaemonMetricsCollector>,
        permit: InflightPermit,
    ) -> io::Result<JoinHandle<()>>;
}

#[derive(Clone, Copy)]
struct ThreadConnectionWorkerSpawner;

impl ConnectionWorkerSpawner for ThreadConnectionWorkerSpawner {
    fn spawn_connection_worker(
        &self,
        stream: UnixStream,
        shutdown: Arc<AtomicBool>,
        dispatch_policy: Arc<DaemonDispatchPolicy>,
        metrics: Arc<dyn super::metrics::DaemonMetricsCollector>,
        permit: InflightPermit,
    ) -> io::Result<JoinHandle<()>> {
        thread::Builder::new()
            .name("ee-daemon-conn".to_owned())
            .spawn(move || {
                // Permit is held for the lifetime of the worker; on drop
                // the counter decrements and the next accept can proceed.
                handle_connection(stream, shutdown, dispatch_policy, metrics);
                drop(permit);
            })
    }
}

fn run_accept_loop(
    listener: UnixListener,
    socket_path: PathBuf,
    shutdown: Arc<AtomicBool>,
    pool: Arc<InflightPool>,
    dispatch_policy: Arc<DaemonDispatchPolicy>,
) {
    let metrics: Arc<dyn super::metrics::DaemonMetricsCollector> =
        Arc::new(super::metrics::NoopMetricsCollector);
    run_accept_loop_with_spawner(
        listener,
        socket_path,
        shutdown,
        pool,
        dispatch_policy,
        metrics,
        ThreadConnectionWorkerSpawner,
    );
}

fn run_accept_loop_with_spawner<S>(
    listener: UnixListener,
    socket_path: PathBuf,
    shutdown: Arc<AtomicBool>,
    pool: Arc<InflightPool>,
    dispatch_policy: Arc<DaemonDispatchPolicy>,
    metrics: Arc<dyn super::metrics::DaemonMetricsCollector>,
    spawner: S,
) where
    S: ConnectionWorkerSpawner,
{
    for incoming in listener.incoming() {
        if shutdown.load(Ordering::SeqCst) {
            // We've been signalled to stop, but `incoming` may still be
            // a freshly accepted connection: the shutdown wake itself
            // connects to unblock `accept`, and a legitimate client can
            // race in between the shutdown signal and the listener
            // teardown. Hand that peer a framed `daemon_shutting_down`
            // envelope before we stop, rather than dropping the stream
            // and leaving the client to interpret a bare connection
            // reset. The wake connection (which drops its end without
            // reading) just sees the best-effort write fail, harmlessly.
            // bd-36dp2.
            if let Ok(mut stream) = incoming {
                write_shutting_down_response(&mut stream);
            }
            break;
        }
        match incoming {
            Ok(stream) => {
                if let Some(permit) = pool.try_acquire() {
                    match stream.try_clone() {
                        Ok(worker_stream) => {
                            let worker_shutdown = Arc::clone(&shutdown);
                            let worker_policy = Arc::clone(&dispatch_policy);
                            let worker_metrics = Arc::clone(&metrics);
                            let spawn_result = spawner.spawn_connection_worker(
                                worker_stream,
                                worker_shutdown,
                                worker_policy,
                                worker_metrics,
                                permit,
                            );
                            if let Err(error) = spawn_result {
                                // Thread spawn itself failed (resource
                                // exhaustion). The failed closure drops the
                                // permit immediately; keep the original stream
                                // available so the client still receives the
                                // bounded-pool refusal envelope.
                                metrics.record_worker_spawn_failure(error.kind());
                                let mut rejected = stream;
                                write_overloaded_response(&mut rejected);
                            }
                        }
                        Err(error) => {
                            // Duplicating the stream failed before the worker
                            // owned a descriptor. Release the permit and refuse
                            // the client with the same overloaded envelope; the
                            // daemon is unable to service this connection.
                            metrics.record_stream_clone_failure(error.kind());
                            drop(permit);
                            let mut rejected = stream;
                            write_overloaded_response(&mut rejected);
                        }
                    }
                } else {
                    // Pool saturated — refuse the connection with a
                    // framed daemon_overloaded response and close.
                    // Queueing was rejected as a fix because every
                    // queued connection still holds a UDS file
                    // descriptor and a per-peer 30s read timeout
                    // budget; backpressure must propagate to the
                    // client immediately so it can fall back to
                    // in-process execution.
                    let mut rejected = stream;
                    write_overloaded_response(&mut rejected);
                }
            }
            Err(error) => {
                // Accept errors that are not transient bring down the
                // accept loop; the next `start_server` call after the
                // operator fixes the cause will bind fresh.
                if error.kind() == io::ErrorKind::Interrupted {
                    continue;
                }
                trace_daemon_accept_loop_terminated(&socket_path, &error);
                metrics.record_accept_loop_terminated(error.kind());
                break;
            }
        }
    }
}

/// Write a framed `daemon_overloaded` response on a connection that
/// was accepted but for which no worker slot is available, then close.
/// The response uses the canonical envelope shape so consumers parse
/// it through the same `read_response` path as any other daemon
/// reply; we do not yet know the client's `request_id` (we never
/// read the request frame), so it is set to `"<overloaded>"`. The
/// caller's `agent_id` is likewise unknown at this refusal point.
fn write_overloaded_response(stream: &mut UnixStream) {
    // Use a tight write timeout so a dead client cannot block the
    // accept loop. The accept loop is single-threaded; a slow client
    // here would amplify the very DoS the bounded pool defends
    // against.
    let _ = stream.set_write_timeout(Some(Duration::from_secs(2)));
    let response = DaemonResponse::err(
        "<overloaded>",
        "<unknown>",
        None,
        DAEMON_OVERLOADED_CODE,
        "daemon worker pool saturated; retry after existing connections drain or fall back \
         to the in-process CLI path.",
    )
    .with_degraded(DAEMON_OVERLOADED_CODE);
    let _ = write_response(stream, &response);
}

/// Write a framed `daemon_shutting_down` response on a connection that
/// was accepted after the shutdown latch flipped, then let the caller
/// drop it. Mirrors [`write_overloaded_response`]: a tight write
/// timeout keeps a dead peer from stalling the (single-threaded) accept
/// loop on its way out, and we never learned the client's `request_id`
/// or `agent_id` (we never read a request frame), so they are set to
/// sentinels.
/// bd-36dp2.
fn write_shutting_down_response(stream: &mut UnixStream) {
    let _ = stream.set_write_timeout(Some(Duration::from_secs(2)));
    let response = daemon_shutting_down_response("<shutdown>", "<unknown>", None);
    let _ = write_response(stream, &response);
}

fn daemon_shutting_down_response(
    request_id: impl Into<String>,
    agent_id: impl Into<String>,
    workspace_id: Option<String>,
) -> DaemonResponse {
    DaemonResponse::err(
        request_id,
        agent_id,
        workspace_id,
        super::DAEMON_SHUTTING_DOWN_CODE,
        "daemon is shutting down and is no longer accepting connections; retry against a \
         fresh daemon or fall back to the in-process CLI path.",
    )
    .with_degraded(super::DAEMON_SHUTTING_DOWN_CODE)
}

/// Wall-clock milliseconds since `started`, saturating at `u64::MAX`
/// per the tracing-field convention
/// (docs/observability/tracing_field_convention.md).
fn elapsed_ms_since(started: std::time::Instant) -> u64 {
    u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX)
}

/// Classify a dispatch [`DaemonResponse`] into the observability tuple
/// bd-qwlzu records: the outcome code (`"ok"` or the wire error code)
/// plus the `schema_mismatch` / `unknown_method` protocol-shape booleans
/// the audit stream branches on. Pure so the per-method attribution
/// mapping is unit-testable without a live socket. The `result` /
/// `params` payload is deliberately NOT inspected — payload redaction is
/// bd-3uev6's surface; this reads only the stable error code.
#[must_use]
fn classify_dispatch_response(response: &DaemonResponse) -> (&str, bool, bool) {
    match response.error.as_ref() {
        None => ("ok", false, false),
        Some(error) => (
            error.code.as_str(),
            error.code == DAEMON_REQUEST_SCHEMA_MISMATCH_CODE,
            error.code == DAEMON_UNKNOWN_METHOD_CODE,
        ),
    }
}

/// Stable, leak-free category for a frame read failure. Returns a fixed
/// discriminant string rather than the error `Display` so a decode
/// failure can be recorded in the audit stream WITHOUT echoing any
/// attacker-controlled frame bytes into the log (orthogonal to the
/// wire-side reflection tracked by bd-3uev6).
#[must_use]
fn frame_error_kind(error: &FrameReadError) -> &'static str {
    match error {
        FrameReadError::Eof => "eof",
        FrameReadError::TooLarge { .. } => "too_large",
        FrameReadError::Truncated { .. } => "truncated",
        FrameReadError::Io(_) => "io",
        FrameReadError::Decode(_) => "decode",
    }
}

#[must_use]
fn frame_error_closes_without_response(error: &FrameReadError) -> bool {
    match error {
        FrameReadError::Eof | FrameReadError::Truncated { .. } => true,
        FrameReadError::Io(source) => matches!(
            source.kind(),
            io::ErrorKind::TimedOut
                | io::ErrorKind::WouldBlock
                | io::ErrorKind::UnexpectedEof
                | io::ErrorKind::ConnectionReset
                | io::ErrorKind::ConnectionAborted
                | io::ErrorKind::BrokenPipe
        ),
        FrameReadError::TooLarge { .. } | FrameReadError::Decode(_) => false,
    }
}

/// Emit one structured `ee.daemon.rpc` tracing event for a completed
/// dispatch (bd-qwlzu). Fields follow the canonical tracing-field
/// convention (workspace_id / request_id / bead_id / surface / phase /
/// elapsed_ms / degraded_codes) plus daemon-specific `method` + `peer_uid`
/// attribution and the `schema_mismatch` / `unknown_method` booleans.
/// This is the per-RPC audit trail the daemon dispatch path previously
/// lacked entirely.
fn trace_daemon_rpc_dispatch(
    request_id: &str,
    method: &str,
    peer: u32,
    elapsed_ms: u64,
    code: &str,
    degraded_codes: &[&str],
    schema_mismatch: bool,
    unknown_method: bool,
) {
    tracing::info!(
        workspace_id = "daemon-rpc",
        request_id,
        bead_id = option_env!("EE_TRACE_BEAD_ID").unwrap_or("bd-qwlzu"),
        surface = "daemon_rpc",
        phase = "dispatch",
        elapsed_ms,
        method,
        peer_uid = peer,
        code,
        degraded_codes = ?degraded_codes,
        schema_mismatch,
        unknown_method,
        "ee.daemon.rpc dispatch outcome"
    );
}

/// Emit a `warn`-level `ee.daemon.rpc` event for a peer-credential
/// refusal (bd-qwlzu × bd-3j0td). The `SO_PEERCRED` / `getpeereid` gate
/// writes a refusal envelope on the wire but, before this, left no
/// server-side record — cross-UID probing was invisible to operators.
/// `peer` is `-1` when the credential lookup itself failed (the UID
/// could not be verified).
fn trace_daemon_rpc_unauthorized(peer: i64, elapsed_ms: u64, reason: &str) {
    let degraded = [DAEMON_PEER_UNAUTHORIZED_CODE];
    tracing::warn!(
        workspace_id = "daemon-rpc",
        request_id = "<unauthorized>",
        bead_id = option_env!("EE_TRACE_BEAD_ID").unwrap_or("bd-qwlzu"),
        surface = "daemon_rpc",
        phase = "dependency_check",
        elapsed_ms,
        method = "<unauthenticated>",
        peer_uid = peer,
        code = DAEMON_PEER_UNAUTHORIZED_CODE,
        degraded_codes = ?degraded,
        reason,
        "ee.daemon.rpc peer authorization refused"
    );
}

/// Emit a `warn`-level `ee.daemon.rpc` event for a frame that failed to
/// decode (bd-qwlzu). Carries only the stable `frame_error_kind`
/// category — never the raw bytes — so the audit stream cannot itself
/// become the leak channel bd-3uev6 warns about.
fn trace_daemon_rpc_decode_failure(peer: u32, elapsed_ms: u64, kind: &str) {
    let degraded = [DAEMON_REQUEST_DECODE_FAILED_CODE];
    tracing::warn!(
        workspace_id = "daemon-rpc",
        request_id = "<undecoded>",
        bead_id = option_env!("EE_TRACE_BEAD_ID").unwrap_or("bd-qwlzu"),
        surface = "daemon_rpc",
        phase = "input",
        elapsed_ms,
        method = "<undecoded>",
        peer_uid = peer,
        code = DAEMON_REQUEST_DECODE_FAILED_CODE,
        degraded_codes = ?degraded,
        frame_error_kind = kind,
        "ee.daemon.rpc request decode failed"
    );
}

fn trace_daemon_accept_loop_terminated(socket_path: &Path, error: &io::Error) {
    let io_error_kind = io_error_kind_label(error.kind());
    tracing::warn!(
        workspace_id = "daemon-rpc",
        request_id = "<accept-loop>",
        bead_id = option_env!("EE_TRACE_BEAD_ID").unwrap_or("bd-n0o5m"),
        surface = "daemon_rpc",
        phase = "accept_loop",
        event = "ee.daemon.accept_loop_terminated",
        socket_path = %socket_path.display(),
        io_error_kind,
        "ee.daemon.accept_loop_terminated"
    );
}

fn io_error_kind_label(kind: io::ErrorKind) -> &'static str {
    match kind {
        io::ErrorKind::NotFound => "not_found",
        io::ErrorKind::PermissionDenied => "permission_denied",
        io::ErrorKind::ConnectionRefused => "connection_refused",
        io::ErrorKind::ConnectionReset => "connection_reset",
        io::ErrorKind::ConnectionAborted => "connection_aborted",
        io::ErrorKind::NotConnected => "not_connected",
        io::ErrorKind::AddrInUse => "addr_in_use",
        io::ErrorKind::AddrNotAvailable => "addr_not_available",
        io::ErrorKind::BrokenPipe => "broken_pipe",
        io::ErrorKind::AlreadyExists => "already_exists",
        io::ErrorKind::WouldBlock => "would_block",
        io::ErrorKind::InvalidInput => "invalid_input",
        io::ErrorKind::InvalidData => "invalid_data",
        io::ErrorKind::TimedOut => "timed_out",
        io::ErrorKind::WriteZero => "write_zero",
        io::ErrorKind::Interrupted => "interrupted",
        io::ErrorKind::Unsupported => "unsupported",
        io::ErrorKind::UnexpectedEof => "unexpected_eof",
        io::ErrorKind::OutOfMemory => "out_of_memory",
        _ => "other",
    }
}

fn handle_connection(
    mut stream: UnixStream,
    shutdown: Arc<AtomicBool>,
    dispatch_policy: Arc<DaemonDispatchPolicy>,
    metrics: Arc<dyn super::metrics::DaemonMetricsCollector>,
) {
    // Install the per-connection deadlines BEFORE any read. The read
    // timeout is the only backstop that stops a half-open peer from
    // pinning this worker thread forever; if `setsockopt` fails (low
    // memory, a seccomp filter that blocks SO_RCVTIMEO/SO_SNDTIMEO,
    // certain BSD kernel modes) we MUST NOT fall through into a
    // deadline-less `read_request`. The pre-fix code discarded these
    // errors with `let _ =`, leaving exactly that permanent-hang path.
    // Set the write deadline first so the refusal envelope below cannot
    // itself block on a wedged client. Sentinel: bd-3pnno setsockopt.
    if let Err(error) = stream.set_write_timeout(Some(DAEMON_DEFAULT_RPC_TIMEOUT)) {
        // No write deadline could be installed. We still attempt a
        // best-effort framed refusal — a freshly accepted UDS send
        // buffer almost always accepts a small envelope without
        // blocking — then drop. `write_response`'s own error is
        // swallowed; the connection closes either way and the worker
        // exits instead of hanging.
        let response = DaemonResponse::err(
            "<setsockopt>",
            "<unknown>",
            None,
            DAEMON_SETSOCKOPT_FAILED_CODE,
            format!("daemon could not set the connection write timeout: {error}"),
        )
        .with_degraded(DAEMON_SETSOCKOPT_FAILED_CODE);
        let _ = write_response(&mut stream, &response);
        return;
    }
    if let Err(error) = stream.set_read_timeout(Some(DAEMON_DEFAULT_RPC_TIMEOUT)) {
        // The critical case: without a read deadline, `read_request`
        // could block forever on a peer that opened the connection and
        // stopped sending. Refuse with a framed envelope and drop so
        // the worker thread is reclaimed.
        let response = DaemonResponse::err(
            "<setsockopt>",
            "<unknown>",
            None,
            DAEMON_SETSOCKOPT_FAILED_CODE,
            format!("daemon could not set the connection read timeout: {error}"),
        )
        .with_degraded(DAEMON_SETSOCKOPT_FAILED_CODE);
        let _ = write_response(&mut stream, &response);
        return;
    }

    let started = std::time::Instant::now();

    // Peer-credential gate (bd-3j0td). Even with the socket file
    // tightened to 0o600 — and the per-UID parent dir at 0o700 — the
    // accept side validates the peer's effective UID against the
    // daemon process's own UID before dispatching. This is defense
    // in depth: the dispatch table currently bypasses the canonical
    // CLI redaction pipeline (see bd-3uev6), so the auth gate IS the
    // cross-tenant exfil defense until redaction is wired through.
    // We do not read the request frame on refusal because doing so
    // would leak the request body's framed length back to a peer the
    // daemon is intentionally refusing. Sentinel: bd-3j0td getpeereid.
    let peer = match peer_uid(&stream) {
        Ok(peer) => {
            let own = current_euid();
            if peer != own {
                let response = DaemonResponse::err(
                    "<unauthorized>",
                    "<unknown>",
                    None,
                    DAEMON_PEER_UNAUTHORIZED_CODE,
                    format!(
                        "daemon refuses peer uid {peer}; only uid {own} (the daemon owner) \
                         may connect to this socket"
                    ),
                )
                .with_degraded(DAEMON_PEER_UNAUTHORIZED_CODE);
                trace_daemon_rpc_unauthorized(
                    i64::from(peer),
                    elapsed_ms_since(started),
                    "peer uid does not match daemon owner uid",
                );
                let _ = write_response(&mut stream, &response);
                return;
            }
            peer
        }
        Err(error) => {
            // getpeereid/SO_PEERCRED itself failed — treat this as a
            // hard refusal rather than a silent allow. The framed
            // response carries the same `daemon_peer_unauthorized`
            // code so the client cannot distinguish "you are the
            // wrong UID" from "we could not verify your UID"; the
            // bias is intentional, the daemon must close on auth
            // ambiguity.
            let response = DaemonResponse::err(
                "<unauthorized>",
                "<unknown>",
                None,
                DAEMON_PEER_UNAUTHORIZED_CODE,
                format!("daemon could not verify peer credential: {error}"),
            )
            .with_degraded(DAEMON_PEER_UNAUTHORIZED_CODE);
            trace_daemon_rpc_unauthorized(
                -1,
                elapsed_ms_since(started),
                "peer credential lookup failed",
            );
            let _ = write_response(&mut stream, &response);
            return;
        }
    };

    // Skeleton: one request per accepted connection. A follow-up
    // multiplexing slice will loop here so a single client can run
    // many RPCs over the same socket; the framing already supports
    // that because each frame is self-contained.
    let request = match read_request(&mut stream) {
        Ok(request) => request,
        Err(error) if frame_error_closes_without_response(&error) => return,
        Err(other) => {
            let kind = frame_error_kind(&other);
            // bd-3uev6: the wire message is a FIXED string. `other.to_string()`
            // for `FrameReadError::Decode` embeds an attacker-controlled snippet
            // of the input near the parse failure — a log-injection vector once
            // these envelopes reach the flight recorder / obs pipeline. The full
            // diagnostic stays server-side in the structured `frame_error_kind`
            // tracing event below; the peer sent the bytes and does not need them
            // reflected.
            let response = DaemonResponse::err(
                "<unknown>",
                "<unknown>",
                None,
                DAEMON_REQUEST_DECODE_FAILED_CODE,
                "request body failed to decode",
            );
            trace_daemon_rpc_decode_failure(peer, elapsed_ms_since(started), kind);
            let _ = write_response(&mut stream, &response);
            return;
        }
    };

    if shutdown.load(Ordering::SeqCst) {
        let response = daemon_shutting_down_response(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
        );
        let degraded: Vec<&str> = response.degraded_codes.iter().map(String::as_str).collect();
        trace_daemon_rpc_dispatch(
            &request.request_id,
            &request.method,
            peer,
            elapsed_ms_since(started),
            super::DAEMON_SHUTTING_DOWN_CODE,
            &degraded,
            false,
            false,
        );
        let _ = write_response(&mut stream, &response);
        return;
    }

    // Panic supervision (bd-b82q4): if `dispatch` (or any method it
    // delegates to in a future warm-load slice) panics, `catch_unwind`
    // converts the unwinding panic into an `Err(payload)` value. We
    // log a sanitized one-line summary to stderr and return a
    // structured `daemon_handler_panic` envelope to the client. The
    // accept loop is unaffected — sibling connections continue to be
    // served. Critically, the panic payload is NEVER written on the
    // wire: a panic that traversed a `Display` for a memory body or a
    // workspace path could otherwise leak that content to the
    // cross-tenant client.
    //
    // `AssertUnwindSafe` is sound here because the only state
    // captured by the closure is a borrow of `request`; the
    // `DaemonResponse` value is produced fresh inside the closure and
    // the `UnixStream` is touched only after the closure returns.
    //
    // Metrics seam (bd-3vkyp): the live dispatch goes through
    // `instrument_dispatch` with the zero-cost `NoopMetricsCollector`,
    // so a perf-investigation build turns on per-method counters /
    // histograms by swapping the collector at THIS call site — no edit
    // to `dispatch` or any of its match arms, hence no recompile of the
    // hot dispatch table.
    let request_id = request.request_id.clone();
    let dispatched = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        super::metrics::instrument_dispatch(&request.method, metrics.as_ref(), || {
            dispatch_with_policy_and_shutdown(&request, dispatch_policy.as_ref(), shutdown.as_ref())
        })
    }));
    let mut response = match dispatched {
        Ok(response) => response,
        Err(payload) => {
            metrics.record_handler_panic(&request.method);
            build_panic_response(&request, payload.as_ref())
        }
    };
    let (code, schema_mismatch, unknown_method) = classify_dispatch_response(&response);
    let degraded: Vec<&str> = response.degraded_codes.iter().map(String::as_str).collect();
    trace_daemon_rpc_dispatch(
        &request_id,
        &request.method,
        peer,
        elapsed_ms_since(started),
        code,
        &degraded,
        schema_mismatch,
        unknown_method,
    );
    write_and_settle_daemon_response(&mut stream, dispatch_policy.as_ref(), &mut response);
}

fn write_and_settle_daemon_response(
    stream: &mut UnixStream,
    dispatch_policy: &DaemonDispatchPolicy,
    response: &mut DaemonResponse,
) -> bool {
    let delivered = write_response(stream, response).is_ok();
    settle_daemon_response_delivery(dispatch_policy, response, delivered);
    delivered
}

fn settle_daemon_response_delivery(
    dispatch_policy: &DaemonDispatchPolicy,
    response: &mut DaemonResponse,
    delivered: bool,
) {
    let Some(delivery) = response.take_delivery() else {
        return;
    };
    settle_search_advisory_delivery(
        dispatch_policy.search_advisory_session(),
        delivery.workspace_id(),
        delivery.search_advisory_token(),
        delivered,
        delivery.search_large_gap_capacity_busy(),
    );
}

fn settle_search_advisory_delivery(
    session: &Mutex<SearchAdvisorySession>,
    workspace_id: &str,
    token: u64,
    delivered: bool,
    large_gap_capacity_busy: bool,
) {
    for attempt in 0..=SEARCH_ADVISORY_SETTLEMENT_RETRY_LIMIT {
        let settlement = session
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .settle_delivery(workspace_id, token, delivered, large_gap_capacity_busy);
        if settlement == SearchAdvisorySettlement::Complete {
            return;
        }
        if attempt == SEARCH_ADVISORY_SETTLEMENT_RETRY_LIMIT {
            // The advisory was already delivered fail-open. Leaving it
            // unconsumed can cause duplicate prose later, but can never hide a
            // first affected response under sustained capacity pressure.
            tracing::warn!(
                workspace_id,
                token,
                "search advisory capacity remained busy after bounded settlement retries"
            );
            return;
        }
        std::thread::yield_now();
    }
}

/// Construct the structured envelope returned to a client whose
/// connection-handler panicked, and log a sanitized one-line summary
/// of the panic to the daemon's stderr. The wire envelope carries a
/// fixed generic message — never the raw panic payload — so a panic
/// inside a `Display` impl that touched a memory body cannot leak
/// that content to the client. bd-b82q4.
fn build_panic_response(
    request: &DaemonRequest,
    payload: &(dyn std::any::Any + Send),
) -> DaemonResponse {
    let raw = extract_panic_payload_str(payload);
    let sanitized = sanitize_panic_message(&raw);
    // Single-line stderr log, capped, so a hostile panic payload
    // cannot blow out the journal nor inject log-forging characters.
    eprintln!("ee daemon handler panicked: {sanitized}");
    DaemonResponse::err(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        DAEMON_HANDLER_PANIC_CODE,
        "daemon handler panicked; the accept loop is intact, retry the request or fall back \
         to the in-process CLI path.",
    )
    .with_degraded(DAEMON_HANDLER_PANIC_CODE)
}

/// Best-effort extraction of a printable `&str` from a panic payload.
/// `std::panic::catch_unwind` boxes the payload as `Box<dyn Any +
/// Send>`. The two payload types Rust ships out of the box are
/// `&'static str` (for `panic!("literal")`) and `String` (for
/// `panic!("{fmt}", x)`); anything else is logged as `<non-string
/// panic payload>` rather than `Debug`-formatted, because `Debug` on
/// an unknown type could itself touch sensitive state.
fn extract_panic_payload_str(payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(text) = payload.downcast_ref::<&'static str>() {
        (*text).to_owned()
    } else if let Some(text) = payload.downcast_ref::<String>() {
        text.clone()
    } else {
        "<non-string panic payload>".to_owned()
    }
}

/// Sanitize a panic message for the daemon's stderr log: drop ASCII
/// control characters (defends against log-line injection via
/// embedded `\r`, `\n`, escape sequences), and truncate to
/// [`DAEMON_PANIC_LOG_MAX_BYTES`]. The sanitized output is never
/// written back to the client on the wire — only logged.
fn sanitize_panic_message(raw: &str) -> String {
    let mut out = String::with_capacity(raw.len().min(DAEMON_PANIC_LOG_MAX_BYTES));
    for ch in raw.chars() {
        if out.len() + ch.len_utf8() > DAEMON_PANIC_LOG_MAX_BYTES {
            out.push_str("...");
            break;
        }
        if ch.is_ascii_control() {
            out.push(' ');
        } else {
            out.push(ch);
        }
    }
    out
}

/// Read the peer's effective UID from a connected `UnixStream`. Linux exposes
/// this through Rustix's safe `SO_PEERCRED` wrapper. Other Unix targets fail
/// closed until a safe platform wrapper is wired in, so the daemon never
/// bypasses auth just because credential lookup is unavailable. Sentinel:
/// bd-3j0td peer_uid.
#[cfg(target_os = "linux")]
fn peer_uid(stream: &UnixStream) -> io::Result<u32> {
    rustix::net::sockopt::socket_peercred(stream)
        .map(|credentials| credentials.uid.as_raw())
        .map_err(io::Error::from)
}

#[cfg(target_vendor = "apple")]
fn peer_uid(stream: &UnixStream) -> io::Result<u32> {
    stream.peer_cred().map(|credentials| credentials.uid)
}

#[cfg(all(not(target_os = "linux"), not(target_vendor = "apple")))]
fn peer_uid(_stream: &UnixStream) -> io::Result<u32> {
    Err(io::Error::other(
        "safe peer credential lookup is not implemented on this Unix target; \
         refusing daemon peer rather than bypassing authorization",
    ))
}

/// Pure dispatch: map a parsed [`DaemonRequest`] to a
/// [`DaemonResponse`]. Exposed for unit tests that exercise the
/// dispatch table without paying for a UDS round-trip.
#[must_use]
pub fn dispatch(request: &DaemonRequest) -> DaemonResponse {
    dispatch_with_echo_policy(request, daemon_echo_enabled())
}

fn dispatch_with_policy_and_shutdown(
    request: &DaemonRequest,
    policy: &DaemonDispatchPolicy,
    shutdown: &AtomicBool,
) -> DaemonResponse {
    dispatch_with_echo_policy_and_workspace_inner(
        request,
        daemon_echo_enabled(),
        policy.bound_workspace_id(),
        shutdown,
        policy.write_router(),
        policy.search_advisory_session(),
        true,
    )
}

fn dispatch_with_echo_policy(request: &DaemonRequest, echo_enabled: bool) -> DaemonResponse {
    let shutdown = AtomicBool::new(false);
    let search_advisory_session = Mutex::new(SearchAdvisorySession::default());
    dispatch_with_echo_policy_and_workspace(
        request,
        echo_enabled,
        None,
        &shutdown,
        None,
        &search_advisory_session,
    )
}

fn dispatch_with_echo_policy_and_workspace(
    request: &DaemonRequest,
    echo_enabled: bool,
    bound_workspace_id: Option<&str>,
    shutdown: &AtomicBool,
    write_router: Option<&DaemonWriteRouter>,
    search_advisory_session: &Mutex<SearchAdvisorySession>,
) -> DaemonResponse {
    dispatch_with_echo_policy_and_workspace_inner(
        request,
        echo_enabled,
        bound_workspace_id,
        shutdown,
        write_router,
        search_advisory_session,
        false,
    )
}

fn dispatch_with_echo_policy_and_workspace_inner(
    request: &DaemonRequest,
    echo_enabled: bool,
    bound_workspace_id: Option<&str>,
    shutdown: &AtomicBool,
    write_router: Option<&DaemonWriteRouter>,
    search_advisory_session: &Mutex<SearchAdvisorySession>,
    defer_advisory_until_socket_write: bool,
) -> DaemonResponse {
    if request.schema != super::DAEMON_REQUEST_SCHEMA_V1 {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_REQUEST_SCHEMA_MISMATCH_CODE,
            format!(
                "expected schema `{}`, got `{}`",
                super::DAEMON_REQUEST_SCHEMA_V1,
                request.schema,
            ),
        );
    }

    let Some(authority) = daemon_method_authority(&request.method) else {
        let other = request.method.as_str();
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_UNKNOWN_METHOD_CODE,
            format!("unknown daemon method `{other}`"),
        );
    };
    if let Err(response) = authorize_daemon_method(request, authority, bound_workspace_id) {
        return response;
    }

    match request.method.as_str() {
        METHOD_CAPABILITIES => DaemonResponse::ok(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            daemon_capabilities_result(),
        ),
        // bd-3uev6: echo is the one public dispatch method that returns
        // caller-supplied content, so it MUST route through the same
        // canonical redaction pipeline every other content-bearing
        // surface uses (core::outcome, support_bundle, mcp). Reflecting
        // `params` verbatim would make the socket a redaction-bypassing
        // round-trip oracle. The integrity contract becomes "echo returns
        // the redaction-stable form of what you sent", and only when the
        // diagnostic method is explicitly enabled for the local daemon.
        METHOD_ECHO if echo_enabled => DaemonResponse::ok(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            crate::core::support_bundle::redact_json_value(&request.params),
        ),
        METHOD_ECHO => DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_ECHO_DISABLED_CODE,
            "ee.daemon.echo is disabled by default; set EE_DAEMON_ENABLE_ECHO=1 for local diagnostics.",
        ),
        METHOD_SHUTDOWN => {
            shutdown.store(true, Ordering::SeqCst);
            DaemonResponse::ok(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                serde_json::json!({
                    "schema": "ee.daemon.shutdown.v1",
                    "accepted": true
                }),
            )
        }
        METHOD_CONTEXT => dispatch_context(
            request,
            shutdown,
            search_advisory_session,
            defer_advisory_until_socket_write,
        ),
        METHOD_SEARCH => dispatch_search(
            request,
            shutdown,
            search_advisory_session,
            defer_advisory_until_socket_write,
        ),
        METHOD_ORIENT_HOOK | METHOD_RECALL => dispatch_memory_read(request),
        METHOD_TELEMETRY => dispatch_telemetry(request),
        METHOD_WRITE => dispatch_write(request, write_router),
        METHOD_WRITE_JOURNAL => dispatch_journal(request, write_router),
        _ => unreachable!("registered daemon methods are handled above"),
    }
}

fn dispatch_memory_read(request: &DaemonRequest) -> DaemonResponse {
    let result = (|| -> Result<DaemonMemoryReadResult, String> {
        // Authorization has already checked the envelope's workspace. Resolve
        // all filesystem input from that workspace, never a second RPC path.
        let workspace = fs::canonicalize(request.workspace_id.as_deref().unwrap_or_default())
            .map_err(|error| format!("Workspace is unavailable: {error}"))?;
        let database =
            canonical_contained_path(&workspace, &workspace.join(".ee/ee.db"), "database")?;
        let (response, markdown) = if request.method == METHOD_ORIENT_HOOK {
            let params: DaemonOrientHookParams = serde_json::from_value(request.params.clone())
                .map_err(|error| format!("Invalid orientation request: {error}"))?;
            if params.schema != DAEMON_ORIENT_HOOK_REQUEST_SCHEMA_V1 {
                return Err("Unsupported orientation request schema".to_owned());
            }
            let index =
                canonical_contained_path(&workspace, &workspace.join(".ee/index"), "index")?;
            let response = crate::core::orient::orient_hook_response(
                &crate::core::orient::OrientFastContentOptions {
                    workspace_path: &workspace,
                    database_path: Some(&database),
                    index_dir: Some(&index),
                    task: &params.task,
                    max_tokens: params.max_tokens,
                    candidate_pool: params.candidate_pool,
                },
                params.include_primer,
            )
            .map_err(|error| error.to_string())?;
            let markdown = response
                .pointer("/data/ambientContext/text")
                .and_then(serde_json::Value::as_str)
                .unwrap_or_default()
                .to_owned();
            (response, markdown)
        } else {
            let params: DaemonRecallParams = serde_json::from_value(request.params.clone())
                .map_err(|error| format!("Invalid recall request: {error}"))?;
            if params.schema != DAEMON_RECALL_REQUEST_SCHEMA_V1 {
                return Err("Unsupported recall request schema".to_owned());
            }
            let (report, degraded) = crate::core::recall::recall_for_workspace(
                &workspace,
                Some(&database),
                &params.query,
                params.cursor.as_deref(),
            )
            .map_err(|error| error.to_string())?;
            let markdown = crate::core::recall::render_recall_markdown(&report, &degraded);
            let response = serde_json::json!({
                "schema": crate::models::RESPONSE_SCHEMA_V2,
                "success": true,
                "data": crate::core::recall::recall_data_json(&report, &params.query),
                "degraded": degraded.iter().map(crate::core::recall::RecallDegradedEntry::to_json).collect::<Vec<_>>(),
            });
            (response, markdown)
        };
        Ok(DaemonMemoryReadResult {
            schema: DAEMON_MEMORY_READ_RESPONSE_SCHEMA_V1.to_owned(),
            response,
            markdown,
        })
    })();
    match result.and_then(|result| serde_json::to_value(result).map_err(|error| error.to_string()))
    {
        Ok(result) => DaemonResponse::ok(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            result,
        ),
        Err(error) => DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            "daemon_memory_read_failed",
            error,
        ),
    }
}

/// Dispatch `ee.daemon.telemetry`: snapshot the daemon process's live,
/// process-global contention telemetry and return it as a serialized
/// `ee.diag.contention.v1` report. Running inside the daemon process is the
/// whole point — the group-commit counters here reflect real coalescing
/// accumulated across many writes, which a one-shot CLI invocation (a
/// single-writer fallback with its own zeroed atomics) cannot see. Sources
/// that require a live actor/pool handle (write-owner queue depth/wait,
/// read-pool stats, ledger lock-wait percentiles) are left `None` and surface
/// as `unavailableSources` in the report; they are wired in a follow-up once
/// their process-global accessors are threaded through. bd-d67os.12.
fn dispatch_telemetry(request: &DaemonRequest) -> DaemonResponse {
    let group_commit = crate::core::write_owner::write_group_commit_telemetry(None);
    let singleflight = crate::core::singleflight::singleflight_posture_report();
    let flock_gate = crate::db::flock_gate_telemetry();
    let inputs = crate::core::contention::ContentionInputs {
        singleflight: Some(singleflight),
        group_commit: Some((&group_commit).into()),
        flock_gate: Some((&flock_gate).into()),
        ..crate::core::contention::ContentionInputs::default()
    };
    let report = crate::core::contention::build_contention_report(&inputs);
    match serde_json::to_value(&report) {
        Ok(result) => DaemonResponse::ok(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            result,
        ),
        Err(error) => DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_TELEMETRY_ENCODE_FAILED_CODE,
            format!("failed to serialize contention telemetry: {error}"),
        ),
    }
}

fn daemon_search_default_limit() -> u32 {
    10
}

fn deserialize_daemon_search_limit<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
    let limit = json_number_to_u64(&value)
        .and_then(|value| u32::try_from(value).ok())
        .ok_or_else(|| {
            <D::Error as serde::de::Error>::custom(
                "limit must be a mathematical integer between 0 and 4294967295",
            )
        })?;
    Ok(limit)
}

fn daemon_search_default_speed() -> String {
    "default".to_owned()
}

fn daemon_search_default_dedupe() -> String {
    "doc_id".to_owned()
}

fn daemon_search_default_source_mode() -> String {
    "hybrid".to_owned()
}

fn daemon_search_default_memory_scope() -> String {
    "swarm".to_owned()
}

/// Strict, method-specific request payload for [`METHOD_SEARCH`]. The daemon
/// envelope remains `ee.daemon.request.v1`; this nested schema lets clients
/// negotiate search semantics independently of the framing version.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DaemonSearchParams {
    schema: String,
    query: String,
    workspace_path: PathBuf,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    database_path: Option<PathBuf>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    index_dir: Option<PathBuf>,
    #[serde(
        default = "daemon_search_default_limit",
        deserialize_with = "deserialize_daemon_search_limit"
    )]
    limit: u32,
    #[serde(default = "daemon_search_default_speed")]
    speed: String,
    #[serde(default)]
    explain: bool,
    #[serde(default)]
    explain_performance: bool,
    #[serde(default)]
    include_tombstoned: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    as_of: Option<String>,
    #[serde(default)]
    include_expired: bool,
    #[serde(default)]
    include_future: bool,
    #[serde(default)]
    include_stale: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    kind: Option<String>,
    #[serde(default)]
    field_filters: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    relevance_floor: Option<f32>,
    #[serde(default = "daemon_search_default_dedupe")]
    dedupe: String,
    #[serde(default = "daemon_search_default_source_mode")]
    source_mode: String,
    #[serde(default)]
    strict_source_mode: bool,
    #[serde(default = "daemon_search_default_memory_scope")]
    memory_scope: String,
    #[serde(default)]
    strict_scope: bool,
}

impl DaemonSearchParams {
    /// Build the canonical daemon request from already-validated CLI options.
    #[must_use]
    pub fn from_search_options(
        options: &SearchOptions,
        kind: Option<&str>,
        field_filters: &[String],
        explain_performance: bool,
    ) -> Self {
        Self {
            schema: DAEMON_SEARCH_REQUEST_SCHEMA_V2.to_owned(),
            query: options.query.clone(),
            workspace_path: options.workspace_path.clone(),
            database_path: options.database_path.clone(),
            index_dir: options.index_dir.clone(),
            limit: options.limit,
            speed: options.speed.as_str().to_owned(),
            explain: options.explain,
            explain_performance,
            include_tombstoned: options.include_tombstoned,
            as_of: options.as_of.as_ref().map(chrono::DateTime::to_rfc3339),
            include_expired: options.include_expired,
            include_future: options.include_future,
            include_stale: options.include_stale,
            kind: kind.map(str::to_owned),
            field_filters: field_filters.to_vec(),
            relevance_floor: options.relevance_floor,
            dedupe: options.dedup_mode.as_str().to_owned(),
            source_mode: options.source_mode.as_str().to_owned(),
            strict_source_mode: options.strict_source_mode,
            memory_scope: options.memory_scope.as_str().to_owned(),
            strict_scope: options.strict_scope,
        }
    }

    fn from_value(value: &serde_json::Value) -> Result<Self, String> {
        serde_json::from_value(value.clone())
            .map_err(|_| "params do not match ee.daemon.search.request.v2".to_owned())
    }

    fn into_search_parts(
        self,
        authorized_workspace_id: &str,
    ) -> Result<
        (
            SearchOptions,
            Option<String>,
            Vec<TypedMemoryFieldFilter>,
            bool,
        ),
        String,
    > {
        if self.schema != DAEMON_SEARCH_REQUEST_SCHEMA_V2 {
            return Err(format!(
                "field `schema` must equal `{DAEMON_SEARCH_REQUEST_SCHEMA_V2}`"
            ));
        }
        if self.query.trim().is_empty() {
            return Err("field `query` must not be blank".to_owned());
        }
        if self
            .relevance_floor
            .is_some_and(|floor| !floor.is_finite() || !(0.0..=1.0).contains(&floor))
        {
            return Err("field `relevanceFloor` must be between 0.0 and 1.0".to_owned());
        }
        let speed = parse_daemon_speed_mode(&self.speed)?;
        let source_mode = parse_daemon_source_mode(&self.source_mode)?;
        let dedup_mode = match self.dedupe.as_str() {
            "doc_id" => SearchDedupMode::DocId,
            "mi" => SearchDedupMode::MutualInformation,
            _ => return Err("field `dedupe` must be `doc_id` or `mi`".to_owned()),
        };
        let memory_scope = MemoryScope::parse(&self.memory_scope)
            .filter(|scope| scope.as_str() == self.memory_scope)
            .ok_or_else(|| {
                "field `memoryScope` must be self, team, global, workspace, verified, or swarm"
                    .to_owned()
            })?;
        let as_of = self
            .as_of
            .as_deref()
            .map(chrono::DateTime::parse_from_rfc3339)
            .transpose()
            .map_err(|_| "field `asOf` must be an RFC3339 timestamp".to_owned())?
            .map(|value| value.with_timezone(&chrono::Utc));
        let kind = self
            .kind
            .as_deref()
            .map(normalize_memory_kind_filter)
            .transpose()?;
        let field_filters = self
            .field_filters
            .iter()
            .map(|raw| TypedMemoryFieldFilter::parse(raw))
            .collect::<Result<Vec<_>, _>>()?;
        let workspace_path = canonical_workspace_path(&self.workspace_path, "workspacePath")?;
        let authorized_workspace =
            canonical_workspace_path(Path::new(authorized_workspace_id), "workspace_id")?;
        if workspace_path != authorized_workspace {
            return Err(
                "field `workspacePath` must identify the authorized envelope `workspace_id`"
                    .to_owned(),
            );
        }
        let database_path = self
            .database_path
            .as_deref()
            .map(|path| canonical_contained_path(&workspace_path, path, "databasePath"))
            .transpose()?;
        let index_dir = self
            .index_dir
            .as_deref()
            .map(|path| canonical_contained_path(&workspace_path, path, "indexDir"))
            .transpose()?;
        Ok((
            SearchOptions {
                workspace_path,
                database_path,
                index_dir,
                query: self.query,
                limit: self.limit,
                speed,
                explain: self.explain,
                as_of,
                include_tombstoned: self.include_tombstoned,
                include_expired: self.include_expired,
                include_future: self.include_future,
                include_stale: self.include_stale,
                relevance_floor: self.relevance_floor,
                dedup_mode,
                source_mode,
                strict_source_mode: self.strict_source_mode,
                memory_scope,
                strict_scope: self.strict_scope,
            },
            kind,
            field_filters,
            self.explain_performance,
        ))
    }
}

fn canonical_workspace_path(path: &Path, field: &str) -> Result<PathBuf, String> {
    if !path.is_absolute() {
        return Err(format!("field `{field}` must be an absolute path"));
    }
    let canonical = fs::canonicalize(path)
        .map_err(|error| format!("field `{field}` could not be canonicalized: {error}"))?;
    if !canonical.is_dir() {
        return Err(format!("field `{field}` must identify a directory"));
    }
    Ok(canonical)
}

fn canonical_contained_path(
    canonical_workspace: &Path,
    path: &Path,
    field: &str,
) -> Result<PathBuf, String> {
    if !path.is_absolute() {
        return Err(format!("field `{field}` must be an absolute path"));
    }
    let canonical = match fs::canonicalize(path) {
        Ok(path) => path,
        Err(error) if error.kind() == io::ErrorKind::NotFound => {
            if fs::symlink_metadata(path).is_ok_and(|metadata| metadata.file_type().is_symlink()) {
                return Err(format!(
                    "field `{field}` must not identify a dangling symbolic link"
                ));
            }
            let parent = path
                .parent()
                .ok_or_else(|| format!("field `{field}` has no parent directory"))?;
            let file_name = path
                .file_name()
                .ok_or_else(|| format!("field `{field}` has no final path component"))?;
            fs::canonicalize(parent)
                .map_err(|parent_error| {
                    format!("field `{field}` parent could not be canonicalized: {parent_error}")
                })?
                .join(file_name)
        }
        Err(error) => {
            return Err(format!(
                "field `{field}` could not be canonicalized: {error}"
            ));
        }
    };
    if !canonical.starts_with(canonical_workspace) {
        return Err(format!(
            "field `{field}` must remain inside the canonical workspace"
        ));
    }
    Ok(canonical)
}

#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct DaemonSearchReuseContract {
    daemon_process: String,
    default_search_embedder: String,
    search_index: String,
}

impl Default for DaemonSearchReuseContract {
    fn default() -> Self {
        Self {
            daemon_process: "long_lived".to_owned(),
            default_search_embedder: "process_scoped".to_owned(),
            search_index: "per_request".to_owned(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct DaemonSearchTimingMeasurement {
    elapsed_ms: f64,
    elapsed_ms_bucket: String,
    nondeterministic: bool,
}

impl DaemonSearchTimingMeasurement {
    fn from_duration(duration: Duration) -> Self {
        let elapsed_ms = duration.as_secs_f64() * 1_000.0;
        let rendered = elapsed_timing_json(elapsed_ms);
        Self {
            elapsed_ms: rendered
                .get("elapsedMs")
                .and_then(serde_json::Value::as_f64)
                .unwrap_or(elapsed_ms),
            elapsed_ms_bucket: rendered
                .get("elapsedMsBucket")
                .and_then(serde_json::Value::as_str)
                .unwrap_or("gte_1000ms")
                .to_owned(),
            nondeterministic: true,
        }
    }

    fn validate(&self, field: &str) -> Result<(), String> {
        if !self.elapsed_ms.is_finite() || self.elapsed_ms < 0.0 {
            return Err(format!(
                "timing `{field}.elapsedMs` must be finite and non-negative"
            ));
        }
        if !matches!(
            self.elapsed_ms_bucket.as_str(),
            "lt_1ms" | "1_9ms" | "10_49ms" | "50_99ms" | "100_499ms" | "500_999ms" | "gte_1000ms"
        ) {
            return Err(format!("timing `{field}.elapsedMsBucket` drifted"));
        }
        if !self.nondeterministic {
            return Err(format!(
                "timing `{field}` must declare nondeterministic=true"
            ));
        }
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct DaemonSearchTiming {
    daemon_total: DaemonSearchTimingMeasurement,
    embedder_preparation: Option<DaemonSearchTimingMeasurement>,
    index_open: Option<DaemonSearchTimingMeasurement>,
    query: Option<DaemonSearchTimingMeasurement>,
}

impl DaemonSearchTiming {
    fn from_trace(daemon_total: Duration, trace: &SearchPerformanceTrace) -> Self {
        Self {
            daemon_total: DaemonSearchTimingMeasurement::from_duration(daemon_total),
            embedder_preparation: aggregate_search_timings(trace, &["search::embedderPrepare"]),
            index_open: aggregate_search_timings(
                trace,
                &[
                    "searchSync::lexicalOpen",
                    "searchSync::twoTierOpen",
                    "searchSync::attachLexical",
                ],
            ),
            query: aggregate_search_timings(
                trace,
                &["searchSync::lexicalSearch", "searchSync::searchCollect"],
            ),
        }
    }

    fn validate(&self) -> Result<(), String> {
        self.daemon_total.validate("daemonTotal")?;
        for (field, timing) in [
            ("embedderPreparation", self.embedder_preparation.as_ref()),
            ("indexOpen", self.index_open.as_ref()),
            ("query", self.query.as_ref()),
        ] {
            if let Some(timing) = timing {
                timing.validate(field)?;
            }
        }
        Ok(())
    }
}

fn aggregate_search_timings(
    trace: &SearchPerformanceTrace,
    names: &[&str],
) -> Option<DaemonSearchTimingMeasurement> {
    let durations = trace
        .timings()
        .filter(|(name, _)| names.contains(name))
        .map(|(_, elapsed)| elapsed)
        .collect::<Vec<_>>();
    (!durations.is_empty()).then(|| {
        DaemonSearchTimingMeasurement::from_duration(
            durations
                .into_iter()
                .fold(Duration::ZERO, Duration::saturating_add),
        )
    })
}

/// Strict method-specific success payload returned by [`METHOD_SEARCH`].
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct DaemonSearchResult {
    schema: String,
    response: serde_json::Value,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    performance: Option<serde_json::Value>,
    human: String,
    reuse_contract: DaemonSearchReuseContract,
    timing: DaemonSearchTiming,
}

/// Validated public renderings plus daemon-only performance diagnostics.
///
/// Ordinary search output uses `response` and `human`. The explicit
/// `--explain-performance` surface also consumes the exact negotiated reuse
/// contract and timing objects instead of discarding them after validation.
#[derive(Clone, Debug, PartialEq)]
pub struct DaemonSearchRenderings {
    pub response: serde_json::Value,
    pub performance: Option<serde_json::Value>,
    pub human: String,
    pub reuse_contract: serde_json::Value,
    pub timing: serde_json::Value,
}

// Keep this in one-to-one correspondence with the 39 `$defs/uint64` references
// in ee.daemon.search.response.v3. Optional performance paths are skipped when
// the negotiated response omits that block.
const DAEMON_SEARCH_UINT64_INSTANCE_POINTERS: &[&str] = &[
    "/response/data/resultCount",
    "/response/data/rerank/topK",
    "/response/data/rerank/rerankScoreCount",
    "/response/data/rerank/advisorySummary/distinctCount",
    "/response/data/rerank/advisorySummary/emittedCount",
    "/response/data/rerank/advisorySummary/suppressedCount",
    "/response/data/rerank/advisorySummary/sessionOccurrenceCount",
    "/response/data/rerank/advisorySummary/sessionSuppressedCount",
    "/performance/data/query/lengthBytes",
    "/performance/data/queryPlan/requestedLimit",
    "/performance/data/queryPlan/candidateBudget",
    "/performance/data/dbReads/indexStatusChecks",
    "/performance/data/dbReads/memoryReads",
    "/performance/data/dbReads/tagReads",
    "/performance/data/dbReads/artifactLinkReads",
    "/performance/data/profileRuntime/budgets/search/candidateLimit",
    "/performance/data/profileRuntime/budgets/search/concurrentIndexReaders",
    "/performance/data/profileRuntime/budgets/pack/maxTokens",
    "/performance/data/profileRuntime/budgets/pack/maxCandidateMemories",
    "/performance/data/profileRuntime/budgets/cache/memoryCapMb",
    "/performance/data/profileRuntime/budgets/cache/entryCap",
    "/performance/data/profileRuntime/budgets/cache/hotsetPrewarmLimit",
    "/performance/data/profileRuntime/budgets/writeSpool/queueCap",
    "/performance/data/profileRuntime/budgets/writeSpool/batchCap",
    "/performance/data/profileRuntime/budgets/writeSpool/retryBudget",
    "/performance/data/profileRuntime/budgets/steward/maintenanceWindowMs",
    "/performance/data/profileRuntime/budgets/steward/graphRefreshBudget",
    "/performance/data/search/returnedHits",
    "/performance/data/search/sourceCounts/lexical",
    "/performance/data/search/sourceCounts/semanticFast",
    "/performance/data/search/sourceCounts/semanticQuality",
    "/performance/data/search/sourceCounts/hybrid",
    "/performance/data/search/sourceCounts/reranked",
    "/performance/data/search/fieldCoverage/fastScoreCount",
    "/performance/data/search/fieldCoverage/qualityScoreCount",
    "/performance/data/search/fieldCoverage/lexicalScoreCount",
    "/performance/data/search/fieldCoverage/rerankScoreCount",
    "/performance/data/search/fieldCoverage/metadataCount",
    "/performance/data/search/fieldCoverage/explanationCount",
];

fn parse_json_decimal_exponent(raw: &str) -> Option<i64> {
    let (negative, digits) = raw
        .strip_prefix('-')
        .map_or((false, raw), |digits| (true, digits));
    let digits = digits.strip_prefix('+').unwrap_or(digits);
    if digits.is_empty() || !digits.bytes().all(|byte| byte.is_ascii_digit()) {
        return None;
    }
    let magnitude = digits.bytes().fold(0_i64, |value, byte| {
        value
            .saturating_mul(10)
            .saturating_add(i64::from(byte - b'0'))
    });
    Some(if negative {
        magnitude.saturating_neg()
    } else {
        magnitude
    })
}

fn rendered_json_number_to_u64(rendered: &str) -> Option<u64> {
    let (negative, rendered) = rendered
        .strip_prefix('-')
        .map_or((false, rendered), |unsigned| (true, unsigned));
    let (mantissa, exponent) =
        rendered
            .find(['e', 'E'])
            .map_or(Some((rendered, 0_i64)), |index| {
                parse_json_decimal_exponent(&rendered[index + 1..])
                    .map(|exponent| (&rendered[..index], exponent))
            })?;
    let (whole, fraction) = mantissa.split_once('.').unwrap_or((mantissa, ""));
    if whole.is_empty()
        || !whole.bytes().all(|byte| byte.is_ascii_digit())
        || !fraction.bytes().all(|byte| byte.is_ascii_digit())
    {
        return None;
    }

    let digits = whole.bytes().chain(fraction.bytes()).collect::<Vec<_>>();
    if digits.iter().all(|byte| *byte == b'0') {
        return Some(0);
    }
    if negative {
        return None;
    }

    let fraction_len = i64::try_from(fraction.len()).ok()?;
    let scale = exponent.saturating_sub(fraction_len);
    let integer_digits = if scale < 0 {
        let fractional_digits = usize::try_from(scale.saturating_neg()).ok()?;
        if fractional_digits >= digits.len() {
            return None;
        }
        let integer_len = digits.len() - fractional_digits;
        if digits[integer_len..].iter().any(|byte| *byte != b'0') {
            return None;
        }
        &digits[..integer_len]
    } else {
        &digits[..]
    };
    let mut value = 0_u64;
    for byte in integer_digits.iter().skip_while(|byte| **byte == b'0') {
        value = value
            .checked_mul(10)?
            .checked_add(u64::from(*byte - b'0'))?;
    }
    if scale > 0 {
        let trailing_zeros = usize::try_from(scale).ok()?;
        if trailing_zeros > 20 {
            return None;
        }
        for _ in 0..trailing_zeros {
            value = value.checked_mul(10)?;
        }
    }
    Some(value)
}

// Draft 2020-12's `integer` type is mathematical, not lexical: `1.0` is an
// integer. Serde's arbitrary-precision number representation preserves the raw
// decimal token so values near u64::MAX never pass through f64 rounding.
fn json_number_to_u64(value: &serde_json::Value) -> Option<u64> {
    let number = value.as_number()?;
    number
        .as_u64()
        .or_else(|| rendered_json_number_to_u64(&number.to_string()))
}

fn canonicalize_daemon_search_uint64s(value: &mut serde_json::Value) -> Result<(), String> {
    for pointer in DAEMON_SEARCH_UINT64_INSTANCE_POINTERS {
        let Some(field) = value.pointer_mut(pointer) else {
            continue;
        };
        let unsigned = json_number_to_u64(field)
            .ok_or_else(|| format!("daemon search unsigned field `{pointer}` is not a uint64"))?;
        *field = serde_json::Value::from(unsigned);
    }
    Ok(())
}

impl DaemonSearchResult {
    #[cfg(test)]
    fn from_report(
        report: &SearchReport,
        explain: bool,
        workspace_id: &str,
        advisory_session: &mut SearchAdvisorySession,
        timing: DaemonSearchTiming,
        performance: Option<serde_json::Value>,
    ) -> Self {
        Self::from_report_inner(
            report,
            explain,
            workspace_id,
            advisory_session,
            None,
            timing,
            performance,
        )
    }

    fn from_report_for_delivery(
        report: &SearchReport,
        explain: bool,
        workspace_id: &str,
        advisory_session: &mut SearchAdvisorySession,
        reservation: &mut SearchAdvisoryDeliveryReservation,
        timing: DaemonSearchTiming,
        performance: Option<serde_json::Value>,
    ) -> Self {
        Self::from_report_inner(
            report,
            explain,
            workspace_id,
            advisory_session,
            Some(reservation),
            timing,
            performance,
        )
    }

    fn from_report_inner(
        report: &SearchReport,
        explain: bool,
        workspace_id: &str,
        advisory_session: &mut SearchAdvisorySession,
        reservation: Option<&mut SearchAdvisoryDeliveryReservation>,
        timing: DaemonSearchTiming,
        performance: Option<serde_json::Value>,
    ) -> Self {
        let mut data = match reservation {
            Some(reservation) => report.data_json_with_advisory_delivery_reservation(
                advisory_session,
                workspace_id,
                reservation,
            ),
            None => {
                report.data_json_with_advisory_session_for_workspace(advisory_session, workspace_id)
            }
        };
        let human = daemon_search_human_summary(report, &data);
        if explain && let Some(object) = data.as_object_mut() {
            object.insert(
                "resultPath".to_owned(),
                serde_json::Value::String("data.results".to_owned()),
            );
        }
        let degraded = crate::output::response_degraded_from_data(&data);
        let response = serde_json::json!({
            "schema": crate::models::RESPONSE_SCHEMA_V2,
            "success": true,
            "data": data,
            "degraded": degraded,
        });
        Self {
            schema: DAEMON_SEARCH_RESPONSE_SCHEMA_V3.to_owned(),
            response,
            performance,
            human,
            reuse_contract: DaemonSearchReuseContract::default(),
            timing,
        }
    }

    /// Decode and validate the complete method-specific response contract.
    pub fn from_value(mut value: serde_json::Value) -> Result<Self, String> {
        canonicalize_daemon_search_uint64s(&mut value)?;
        let result: Self = serde_json::from_value(value)
            .map_err(|_| "result does not match ee.daemon.search.response.v3".to_owned())?;
        result.validate()?;
        Ok(result)
    }

    fn validate(&self) -> Result<(), String> {
        if self.schema != DAEMON_SEARCH_RESPONSE_SCHEMA_V3 {
            return Err(format!(
                "result schema must equal `{DAEMON_SEARCH_RESPONSE_SCHEMA_V3}`"
            ));
        }
        if self.reuse_contract != DaemonSearchReuseContract::default() {
            return Err("daemon search reuse contract drifted".to_owned());
        }
        let response = self
            .response
            .as_object()
            .ok_or_else(|| "daemon search response must be an object".to_owned())?;
        if response.len() != 4
            || !["schema", "success", "data", "degraded"]
                .iter()
                .all(|field| response.contains_key(*field))
        {
            return Err("canonical daemon search response field set drifted".to_owned());
        }
        if response.get("schema").and_then(serde_json::Value::as_str)
            != Some(crate::models::RESPONSE_SCHEMA_V2)
        {
            return Err("canonical daemon search response schema drifted".to_owned());
        }
        if response.get("success").and_then(serde_json::Value::as_bool) != Some(true)
            || !response
                .get("data")
                .is_some_and(serde_json::Value::is_object)
            || !response
                .get("degraded")
                .is_some_and(serde_json::Value::is_array)
        {
            return Err("canonical daemon search response shape drifted".to_owned());
        }
        validate_canonical_degradations(
            response
                .get("degraded")
                .ok_or_else(|| "canonical daemon search degraded list missing".to_owned())?,
        )?;
        validate_canonical_search_data(
            response
                .get("data")
                .ok_or_else(|| "canonical daemon search data missing".to_owned())?,
        )?;
        if let Some(performance) = self.performance.as_ref() {
            validate_search_performance_explain(performance)?;
        }
        self.timing.validate()
    }

    /// Split the validated payload into canonical renderings and diagnostics.
    pub fn into_renderings(self) -> Result<DaemonSearchRenderings, String> {
        Ok(DaemonSearchRenderings {
            response: self.response,
            performance: self.performance,
            human: self.human,
            reuse_contract: serde_json::to_value(self.reuse_contract).map_err(|error| {
                format!("daemon search reuse contract could not encode: {error}")
            })?,
            timing: serde_json::to_value(self.timing)
                .map_err(|error| format!("daemon search timing could not encode: {error}"))?,
        })
    }
}

fn validate_search_performance_explain(value: &serde_json::Value) -> Result<(), String> {
    validate_exact_object_fields(
        value,
        "daemon search performance",
        &["schema", "success", "data"],
        &[],
    )?;
    if value.get("schema").and_then(serde_json::Value::as_str)
        != Some(PERFORMANCE_EXPLAIN_SCHEMA_V1)
        || value.get("success").and_then(serde_json::Value::as_bool) != Some(true)
    {
        return Err("daemon search performance envelope drifted".to_owned());
    }
    let data = value
        .get("data")
        .ok_or_else(|| "daemon search performance data missing".to_owned())?;
    validate_exact_object_fields(
        data,
        "daemon search performance data",
        &[
            "command",
            "query",
            "queryPlan",
            "profileRuntime",
            "dbReads",
            "search",
            "timings",
            "pack",
            "cache",
            "graph",
            "fallbacks",
            "redaction",
        ],
        &[],
    )?;
    if data.get("command").and_then(serde_json::Value::as_str) != Some("search") {
        return Err("daemon search performance command drifted".to_owned());
    }
    validate_search_performance_query(
        data.get("query")
            .ok_or_else(|| "daemon search performance query missing".to_owned())?,
    )?;
    validate_search_performance_query_plan(
        data.get("queryPlan")
            .ok_or_else(|| "daemon search performance queryPlan missing".to_owned())?,
    )?;
    validate_search_performance_runtime_profile(
        data.get("profileRuntime")
            .ok_or_else(|| "daemon search performance profileRuntime missing".to_owned())?,
    )?;
    validate_search_performance_db_reads(
        data.get("dbReads")
            .ok_or_else(|| "daemon search performance dbReads missing".to_owned())?,
    )?;
    validate_search_performance_search(
        data.get("search")
            .ok_or_else(|| "daemon search performance search missing".to_owned())?,
    )?;
    validate_search_performance_timings(
        data.get("timings")
            .ok_or_else(|| "daemon search performance timings missing".to_owned())?,
        "daemon search performance timings",
    )?;
    for field in ["pack", "cache", "graph"] {
        validate_search_performance_not_used(
            data.get(field)
                .ok_or_else(|| format!("daemon search performance {field} missing"))?,
            field,
        )?;
    }
    let fallbacks = data
        .get("fallbacks")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| "daemon search performance fallbacks must be an array".to_owned())?;
    for (index, fallback) in fallbacks.iter().enumerate() {
        validate_exact_object_fields(
            fallback,
            &format!("daemon search performance fallback {index}"),
            &["code", "severity", "message", "sources"],
            &[],
        )?;
        if !fallback
            .get("code")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|code| !code.is_empty())
            || !matches!(
                fallback.get("severity").and_then(serde_json::Value::as_str),
                Some("info" | "low" | "warning" | "medium" | "high" | "critical")
            )
            || fallback.get("message").and_then(serde_json::Value::as_str)
                != Some(PERFORMANCE_FALLBACK_REDACTED_MESSAGE)
            || fallback.get("sources") != Some(&serde_json::json!(["search"]))
        {
            return Err(format!(
                "daemon search performance fallback {index} field types or values drifted"
            ));
        }
    }
    validate_search_performance_redaction(
        data.get("redaction")
            .ok_or_else(|| "daemon search performance redaction missing".to_owned())?,
    )?;
    Ok(())
}

fn validate_search_performance_query(value: &serde_json::Value) -> Result<(), String> {
    validate_exact_object_fields(
        value,
        "daemon search performance query",
        &["textIncluded", "lengthBytes", "fingerprint"],
        &[],
    )?;
    if value
        .get("textIncluded")
        .and_then(serde_json::Value::as_bool)
        != Some(false)
        || !value.get("lengthBytes").is_some_and(is_json_unsigned)
        || !value
            .get("fingerprint")
            .and_then(serde_json::Value::as_str)
            .is_some_and(valid_blake3_fingerprint)
    {
        return Err("daemon search performance query field types or values drifted".to_owned());
    }
    Ok(())
}

fn valid_blake3_fingerprint(value: &str) -> bool {
    value.strip_prefix("blake3:").is_some_and(|digest| {
        digest.len() == 64
            && digest
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    })
}

fn validate_search_performance_query_plan(value: &serde_json::Value) -> Result<(), String> {
    const REQUIRED: &[&str] = &[
        "retrievalMode",
        "requestedLimit",
        "candidateBudget",
        "usesEmbeddings",
        "scoreExplanationsRequested",
        "sourceModeRequested",
        "sourceModeApplied",
        "strictSourceMode",
        "fallbackApplied",
        "memoryScope",
        "strictScope",
    ];
    validate_exact_object_fields(value, "daemon search performance queryPlan", REQUIRED, &[])?;
    let string_is = |field: &str, allowed: &[&str]| {
        value
            .get(field)
            .and_then(serde_json::Value::as_str)
            .is_some_and(|actual| allowed.contains(&actual))
    };
    if !string_is("retrievalMode", &["instant", "default", "quality"])
        || !string_is(
            "sourceModeRequested",
            &["lexical_only", "semantic_only", "hybrid"],
        )
        || !string_is(
            "sourceModeApplied",
            &["lexical_only", "semantic_only", "hybrid"],
        )
        || !string_is(
            "memoryScope",
            &["self", "team", "global", "workspace", "verified", "swarm"],
        )
        || !["requestedLimit", "candidateBudget"]
            .iter()
            .all(|field| value.get(*field).is_some_and(is_json_unsigned))
        || ![
            "usesEmbeddings",
            "scoreExplanationsRequested",
            "strictSourceMode",
            "fallbackApplied",
            "strictScope",
        ]
        .iter()
        .all(|field| value.get(*field).is_some_and(serde_json::Value::is_boolean))
    {
        return Err("daemon search performance queryPlan field types or values drifted".to_owned());
    }
    Ok(())
}

fn validate_search_performance_runtime_profile(value: &serde_json::Value) -> Result<(), String> {
    validate_exact_object_fields(
        value,
        "daemon search performance profileRuntime",
        &["schema", "activeProfile", "source", "budgets"],
        &[],
    )?;
    if value.get("schema").and_then(serde_json::Value::as_str)
        != Some(crate::core::profile::RUNTIME_PROFILE_SCHEMA_V1)
        || !matches!(
            value
                .get("activeProfile")
                .and_then(serde_json::Value::as_str),
            Some("constrained" | "portable" | "workstation" | "swarm")
        )
        || !value
            .get("source")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|source| !source.is_empty())
    {
        return Err(
            "daemon search performance profileRuntime field types or values drifted".to_owned(),
        );
    }
    let budgets = value
        .get("budgets")
        .ok_or_else(|| "daemon search performance profileRuntime.budgets missing".to_owned())?;
    validate_exact_object_fields(
        budgets,
        "daemon search performance profileRuntime.budgets",
        &[
            "search",
            "pack",
            "cache",
            "writeSpool",
            "steward",
            "verification",
            "diagnostics",
        ],
        &[],
    )?;
    validate_performance_unsigned_object(
        budgets.get("cache"),
        "daemon search performance profileRuntime.budgets.cache",
        &["memoryCapMb", "entryCap", "hotsetPrewarmLimit"],
    )?;
    validate_performance_unsigned_object(
        budgets.get("writeSpool"),
        "daemon search performance profileRuntime.budgets.writeSpool",
        &["queueCap", "batchCap", "retryBudget"],
    )?;

    let search = required_performance_object(
        budgets.get("search"),
        "daemon search performance profileRuntime.budgets.search",
        &[
            "candidateLimit",
            "concurrentIndexReaders",
            "staleIndexTolerance",
        ],
    )?;
    if !["candidateLimit", "concurrentIndexReaders"]
        .iter()
        .all(|field| search.get(*field).is_some_and(is_json_unsigned))
        || !matches!(
            search
                .get("staleIndexTolerance")
                .and_then(serde_json::Value::as_str),
            Some("strict" | "repair_hint")
        )
    {
        return Err("daemon search performance profileRuntime.budgets.search drifted".to_owned());
    }

    let pack = required_performance_object(
        budgets.get("pack"),
        "daemon search performance profileRuntime.budgets.pack",
        &["maxTokens", "maxCandidateMemories", "explanationVerbosity"],
    )?;
    if !["maxTokens", "maxCandidateMemories"]
        .iter()
        .all(|field| pack.get(*field).is_some_and(is_json_unsigned))
        || !matches!(
            pack.get("explanationVerbosity")
                .and_then(serde_json::Value::as_str),
            Some("standard" | "full")
        )
    {
        return Err("daemon search performance profileRuntime.budgets.pack drifted".to_owned());
    }

    let steward = required_performance_object(
        budgets.get("steward"),
        "daemon search performance profileRuntime.budgets.steward",
        &["maintenanceWindowMs", "graphRefreshBudget", "daemonPrewarm"],
    )?;
    if !["maintenanceWindowMs", "graphRefreshBudget"]
        .iter()
        .all(|field| steward.get(*field).is_some_and(is_json_unsigned))
        || !steward
            .get("daemonPrewarm")
            .is_some_and(serde_json::Value::is_boolean)
    {
        return Err("daemon search performance profileRuntime.budgets.steward drifted".to_owned());
    }

    let verification = required_performance_object(
        budgets.get("verification"),
        "daemon search performance profileRuntime.budgets.verification",
        &[
            "recipe",
            "targetDirPosture",
            "timeoutClass",
            "heavyStrategy",
        ],
    )?;
    for (field, allowed) in [
        ("recipe", &["quick", "workspace", "full"][..]),
        ("targetDirPosture", &["shared", "isolated"][..]),
        ("timeoutClass", &["short", "standard", "extended"][..]),
        (
            "heavyStrategy",
            &["manual", "rch_preferred", "rch_default"][..],
        ),
    ] {
        if !verification
            .get(field)
            .and_then(serde_json::Value::as_str)
            .is_some_and(|value| allowed.contains(&value))
        {
            return Err(format!(
                "daemon search performance profileRuntime.budgets.verification.{field} drifted"
            ));
        }
    }

    let diagnostics = required_performance_object(
        budgets.get("diagnostics"),
        "daemon search performance profileRuntime.budgets.diagnostics",
        &["supportBundleProfile", "redaction"],
    )?;
    if !diagnostics
        .get("supportBundleProfile")
        .and_then(serde_json::Value::as_str)
        .is_some_and(|value| ["minimal", "standard", "full"].contains(&value))
        || !diagnostics
            .get("redaction")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|value| ["strict", "policy_applied"].contains(&value))
    {
        return Err(
            "daemon search performance profileRuntime.budgets.diagnostics drifted".to_owned(),
        );
    }
    Ok(())
}

fn validate_search_performance_search(value: &serde_json::Value) -> Result<(), String> {
    const FIELDS: &[&str] = &[
        "status",
        "returnedHits",
        "sourceCounts",
        "scoreDistribution",
        "fieldCoverage",
        "errors",
        "elapsed",
        "timings",
    ];
    validate_exact_object_fields(value, "daemon search performance search", FIELDS, &[])?;
    if !matches!(
        value.get("status").and_then(serde_json::Value::as_str),
        Some("success" | "no_results" | "index_not_found" | "index_error")
    ) || !value.get("returnedHits").is_some_and(is_json_unsigned)
        || !value
            .get("errors")
            .and_then(serde_json::Value::as_array)
            .is_some_and(|errors| errors.iter().all(serde_json::Value::is_string))
    {
        return Err("daemon search performance search field types or values drifted".to_owned());
    }
    validate_performance_unsigned_object(
        value.get("sourceCounts"),
        "daemon search performance search.sourceCounts",
        &[
            "lexical",
            "semanticFast",
            "semanticQuality",
            "hybrid",
            "reranked",
        ],
    )?;
    validate_performance_optional_number_object(
        value.get("scoreDistribution"),
        "daemon search performance search.scoreDistribution",
        &["top", "min", "max", "mean"],
    )?;
    validate_performance_unsigned_object(
        value.get("fieldCoverage"),
        "daemon search performance search.fieldCoverage",
        &[
            "fastScoreCount",
            "qualityScoreCount",
            "lexicalScoreCount",
            "rerankScoreCount",
            "metadataCount",
            "explanationCount",
        ],
    )?;
    validate_search_performance_elapsed(
        value
            .get("elapsed")
            .ok_or_else(|| "daemon search performance search.elapsed missing".to_owned())?,
        "daemon search performance search.elapsed",
        false,
    )?;
    validate_search_performance_timings(
        value
            .get("timings")
            .ok_or_else(|| "daemon search performance search.timings missing".to_owned())?,
        "daemon search performance search.timings",
    )
}

fn validate_search_performance_timings(
    value: &serde_json::Value,
    context: &str,
) -> Result<(), String> {
    let timings = value
        .as_array()
        .ok_or_else(|| format!("{context} must be an array"))?;
    for (index, timing) in timings.iter().enumerate() {
        validate_search_performance_elapsed(timing, &format!("{context}[{index}]"), true)?;
    }
    Ok(())
}

fn validate_search_performance_elapsed(
    value: &serde_json::Value,
    context: &str,
    named: bool,
) -> Result<(), String> {
    let required = if named {
        &["elapsedMs", "elapsedMsBucket", "nondeterministic", "name"][..]
    } else {
        &["elapsedMs", "elapsedMsBucket", "nondeterministic"][..]
    };
    validate_exact_object_fields(value, context, required, &[])?;
    if !value
        .get("elapsedMs")
        .and_then(serde_json::Value::as_f64)
        .is_some_and(|elapsed| elapsed.is_finite() && elapsed >= 0.0)
        || !matches!(
            value
                .get("elapsedMsBucket")
                .and_then(serde_json::Value::as_str),
            Some(
                "lt_1ms"
                    | "1_9ms"
                    | "10_49ms"
                    | "50_99ms"
                    | "100_499ms"
                    | "500_999ms"
                    | "gte_1000ms"
            )
        )
        || value
            .get("nondeterministic")
            .and_then(serde_json::Value::as_bool)
            != Some(true)
        || named
            && !value
                .get("name")
                .and_then(serde_json::Value::as_str)
                .is_some_and(|name| !name.is_empty())
    {
        return Err(format!("{context} field types or values drifted"));
    }
    Ok(())
}

fn required_performance_object<'a>(
    value: Option<&'a serde_json::Value>,
    context: &str,
    fields: &[&str],
) -> Result<&'a serde_json::Map<String, serde_json::Value>, String> {
    let value = value.ok_or_else(|| format!("{context} missing"))?;
    validate_exact_object_fields(value, context, fields, &[])?;
    value
        .as_object()
        .ok_or_else(|| format!("{context} must be an object"))
}

fn validate_performance_unsigned_object(
    value: Option<&serde_json::Value>,
    context: &str,
    fields: &[&str],
) -> Result<(), String> {
    let object = required_performance_object(value, context, fields)?;
    if fields
        .iter()
        .all(|field| object.get(*field).is_some_and(is_json_unsigned))
    {
        Ok(())
    } else {
        Err(format!("{context} field types drifted"))
    }
}

fn validate_performance_optional_number_object(
    value: Option<&serde_json::Value>,
    context: &str,
    fields: &[&str],
) -> Result<(), String> {
    let object = required_performance_object(value, context, fields)?;
    if fields.iter().all(|field| {
        object.get(*field).is_some_and(|value| {
            value.is_null() || value.as_f64().is_some_and(|number| number.is_finite())
        })
    }) {
        Ok(())
    } else {
        Err(format!("{context} field types drifted"))
    }
}

fn is_json_unsigned(value: &serde_json::Value) -> bool {
    json_number_to_u64(value).is_some()
}

fn validate_search_performance_db_reads(value: &serde_json::Value) -> Result<(), String> {
    const FIELDS: &[&str] = &[
        "indexStatusChecks",
        "memoryReads",
        "tagReads",
        "artifactLinkReads",
    ];
    validate_exact_object_fields(value, "daemon search performance dbReads", FIELDS, &[])?;
    if FIELDS
        .iter()
        .all(|field| value.get(*field).is_some_and(is_json_unsigned))
    {
        Ok(())
    } else {
        Err("daemon search performance dbReads field types drifted".to_owned())
    }
}

fn validate_search_performance_not_used(
    value: &serde_json::Value,
    field: &str,
) -> Result<(), String> {
    let context = format!("daemon search performance {field}");
    validate_exact_object_fields(value, &context, &["status", "reason"], &[])?;
    if value.get("status").and_then(serde_json::Value::as_str) != Some("not_used")
        || !value
            .get("reason")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|reason| !reason.is_empty())
    {
        return Err(format!("{context} field types or values drifted"));
    }
    Ok(())
}

fn validate_search_performance_redaction(value: &serde_json::Value) -> Result<(), String> {
    validate_exact_object_fields(
        value,
        "daemon search performance redaction",
        &["memoryContentIncluded", "queryTextIncluded", "safeFields"],
        &[],
    )?;
    if value
        .get("memoryContentIncluded")
        .and_then(serde_json::Value::as_bool)
        != Some(false)
        || value
            .get("queryTextIncluded")
            .and_then(serde_json::Value::as_bool)
            != Some(false)
        || value.get("safeFields")
            != Some(&serde_json::json!([
                "counts",
                "elapsedMs",
                "elapsedMsBucket",
                "status",
                "fingerprints",
                "degradationCodes"
            ]))
    {
        return Err("daemon search performance redaction contract drifted".to_owned());
    }
    Ok(())
}

fn daemon_search_human_summary(report: &SearchReport, response_data: &serde_json::Value) -> String {
    let large_gap_active = response_data
        .pointer("/indexFreshness/largeGap")
        .and_then(serde_json::Value::as_bool)
        == Some(true);
    let large_gap_advisory_emitted = response_data
        .get("degraded")
        .and_then(serde_json::Value::as_array)
        .is_some_and(|entries| {
            entries.iter().any(|entry| {
                entry.get("code").and_then(serde_json::Value::as_str)
                    == Some("search_index_large_gap")
            })
        });
    if large_gap_advisory_emitted {
        return report.human_summary();
    }

    let mut visible_report = report.clone();
    visible_report.degraded.retain(|entry| {
        entry.code != "search_index_large_gap"
            && !(large_gap_active && entry.code == "search_index_stale")
    });
    visible_report.human_summary()
}

fn daemon_search_degraded_codes(result: &DaemonSearchResult) -> Vec<String> {
    result
        .response
        .get("degraded")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|entry| entry.get("code").and_then(serde_json::Value::as_str))
        .map(str::to_owned)
        .collect()
}

fn validate_canonical_degradations(degraded: &serde_json::Value) -> Result<(), String> {
    const REQUIRED: &[&str] = &["code", "severity", "message"];
    const OPTIONAL: &[&str] = &["repair", "repairKind", "sources", "details"];
    let entries = degraded
        .as_array()
        .ok_or_else(|| "canonical daemon search degraded list must be an array".to_owned())?;
    for (index, entry) in entries.iter().enumerate() {
        let context = format!("canonical daemon search degraded[{index}]");
        validate_exact_object_fields(entry, &context, REQUIRED, OPTIONAL)?;
        let object = entry
            .as_object()
            .ok_or_else(|| format!("{context} must be an object"))?;
        if !["code", "message"]
            .iter()
            .all(|field| object.get(*field).is_some_and(serde_json::Value::is_string))
        {
            return Err(format!("{context} code and message must be strings"));
        }
        if !matches!(
            object.get("severity").and_then(serde_json::Value::as_str),
            Some("info" | "low" | "warning" | "medium" | "high" | "critical")
        ) {
            return Err(format!("{context} severity drifted"));
        }
        if object.get("repair").is_some_and(|value| !value.is_string())
            || object.get("repairKind").is_some_and(|value| {
                !matches!(
                    value.as_str(),
                    Some("actionable" | "template" | "placeholder" | "unknown" | "empty")
                )
            })
            || object.get("sources").is_some_and(|value| {
                !value
                    .as_array()
                    .is_some_and(|sources| sources.iter().all(serde_json::Value::is_string))
            })
            || object
                .get("details")
                .is_some_and(|value| !value.is_object())
        {
            return Err(format!("{context} optional field shape drifted"));
        }
    }
    Ok(())
}

fn validate_exact_object_fields(
    value: &serde_json::Value,
    context: &str,
    required: &[&str],
    optional: &[&str],
) -> Result<(), String> {
    let object = value
        .as_object()
        .ok_or_else(|| format!("{context} must be an object"))?;
    if let Some(missing) = required.iter().find(|field| !object.contains_key(**field)) {
        return Err(format!("{context} is missing required field `{missing}`"));
    }
    if let Some(unknown) = object
        .keys()
        .find(|field| !required.contains(&field.as_str()) && !optional.contains(&field.as_str()))
    {
        return Err(format!("{context} contains unknown field `{unknown}`"));
    }
    Ok(())
}

fn validate_canonical_search_data(data: &serde_json::Value) -> Result<(), String> {
    const REQUIRED: &[&str] = &[
        "command",
        "status",
        "embed_backend",
        "query",
        "request",
        "scopeStats",
        "results",
        "consensus",
        "conflicts",
        "resultCount",
        "elapsedMs",
        "metrics",
        "rerank",
        "profileRuntime",
        "errors",
        "degraded",
    ];
    validate_exact_object_fields(
        data,
        "canonical search data",
        REQUIRED,
        &["queryAssist", "resultPath", "indexFreshness"],
    )?;
    if data.get("command").and_then(serde_json::Value::as_str) != Some("search") {
        return Err("canonical search data command drifted".to_owned());
    }
    if !matches!(
        data.get("status").and_then(serde_json::Value::as_str),
        Some("success" | "no_results" | "index_not_found" | "index_error")
    ) {
        return Err("canonical search data status drifted".to_owned());
    }
    if !matches!(
        data.get("embed_backend")
            .and_then(serde_json::Value::as_str),
        Some("neural_local" | "hash_fallback")
    ) {
        return Err("canonical search embed_backend drifted".to_owned());
    }
    if !data.get("query").is_some_and(serde_json::Value::is_string)
        || !["request", "scopeStats", "metrics", "profileRuntime"]
            .iter()
            .all(|field| data.get(*field).is_some_and(serde_json::Value::is_object))
        || !["consensus", "conflicts", "errors", "degraded"]
            .iter()
            .all(|field| data.get(*field).is_some_and(serde_json::Value::is_array))
    {
        return Err("canonical search nested data shape drifted".to_owned());
    }
    validate_canonical_search_rerank(
        data.get("rerank")
            .ok_or_else(|| "canonical search rerank posture missing".to_owned())?,
    )?;
    if let Some(index_freshness) = data.get("indexFreshness") {
        validate_canonical_search_index_freshness(index_freshness)?;
    }
    let elapsed_ms = data
        .get("elapsedMs")
        .and_then(serde_json::Value::as_f64)
        .ok_or_else(|| "canonical search elapsedMs must be a number".to_owned())?;
    if !elapsed_ms.is_finite() || elapsed_ms < 0.0 {
        return Err("canonical search elapsedMs must be finite and non-negative".to_owned());
    }
    let results = data
        .get("results")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| "canonical search results must be an array".to_owned())?;
    let result_count = data
        .get("resultCount")
        .and_then(json_number_to_u64)
        .ok_or_else(|| "canonical search resultCount must be a non-negative integer".to_owned())?;
    if result_count != results.len() as u64 {
        return Err("canonical search resultCount does not match results length".to_owned());
    }
    for (index, result) in results.iter().enumerate() {
        validate_canonical_search_result(result, index)?;
    }
    if data
        .get("queryAssist")
        .is_some_and(|value| !value.is_object())
    {
        return Err("canonical search queryAssist must be an object".to_owned());
    }
    if data
        .get("resultPath")
        .is_some_and(|value| value.as_str() != Some("data.results"))
    {
        return Err("canonical search resultPath drifted".to_owned());
    }
    Ok(())
}

fn validate_canonical_search_index_freshness(value: &serde_json::Value) -> Result<(), String> {
    validate_exact_object_fields(
        value,
        "canonical search indexFreshness",
        &["stale", "largeGap"],
        &["dbGeneration", "indexGeneration", "generationGap"],
    )?;
    if !value
        .get("stale")
        .is_some_and(serde_json::Value::is_boolean)
        || !value
            .get("largeGap")
            .is_some_and(serde_json::Value::is_boolean)
        || ["dbGeneration", "indexGeneration", "generationGap"]
            .iter()
            .any(|field| {
                value
                    .get(*field)
                    .is_some_and(|number| !is_json_unsigned(number))
            })
    {
        return Err("canonical search indexFreshness field types drifted".to_owned());
    }
    Ok(())
}

fn validate_canonical_search_rerank(value: &serde_json::Value) -> Result<(), String> {
    const REQUIRED: &[&str] = &[
        "schema",
        "mode",
        "configured",
        "topK",
        "rerankScoreCount",
        "scoreKind",
        "available",
        "degradedCode",
        "advisory",
        "advisorySummary",
    ];
    validate_exact_object_fields(value, "canonical search rerank posture", REQUIRED, &[])?;
    if value.get("schema").and_then(serde_json::Value::as_str) != Some("ee.rerank_posture.v1")
        || !matches!(
            value.get("mode").and_then(serde_json::Value::as_str),
            Some("reranked" | "fusion_only_degraded" | "fusion_only")
        )
        || !matches!(
            value.get("configured").and_then(serde_json::Value::as_str),
            Some("auto" | "off")
        )
        || !["topK", "rerankScoreCount"]
            .iter()
            .all(|field| value.get(*field).is_some_and(is_json_unsigned))
        || !matches!(
            value.get("scoreKind").and_then(serde_json::Value::as_str),
            Some("reranked" | "rrf_fused")
        )
        || !value
            .get("available")
            .is_some_and(serde_json::Value::is_boolean)
        || !value.get("degradedCode").is_some_and(|code| {
            code.is_null() || code.as_str().is_some_and(|code| !code.is_empty())
        })
    {
        return Err("canonical search rerank posture field types or values drifted".to_owned());
    }

    let advisory = value
        .get("advisory")
        .ok_or_else(|| "canonical search rerank advisory missing".to_owned())?;
    if !advisory.is_null() {
        validate_exact_object_fields(
            advisory,
            "canonical search rerank advisory",
            &[
                "code",
                "severity",
                "permanent",
                "message",
                "repair",
                "resolution",
            ],
            &[],
        )?;
        if !advisory
            .get("code")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|code| !code.is_empty())
            || !matches!(
                advisory.get("severity").and_then(serde_json::Value::as_str),
                Some("info" | "low" | "warning" | "medium" | "high" | "critical")
            )
            || !advisory
                .get("permanent")
                .is_some_and(serde_json::Value::is_boolean)
            || !advisory
                .get("message")
                .is_some_and(serde_json::Value::is_string)
            || !advisory
                .get("repair")
                .is_some_and(|repair| repair.is_null() || repair.is_string())
            || !matches!(
                advisory
                    .get("resolution")
                    .and_then(serde_json::Value::as_str),
                Some("automatic_repair_unavailable" | "retry_or_inspect_local_registry")
            )
        {
            return Err(
                "canonical search rerank advisory field types or values drifted".to_owned(),
            );
        }
    }

    let summary_value = value
        .get("advisorySummary")
        .ok_or_else(|| "canonical search rerank advisorySummary missing".to_owned())?;
    validate_exact_object_fields(
        summary_value,
        "canonical search rerank advisorySummary",
        &[
            "scope",
            "permanent",
            "distinctCount",
            "emittedCount",
            "suppressedCount",
            "sessionOccurrenceCount",
            "sessionSuppressedCount",
        ],
        &[],
    )?;
    let summary = summary_value
        .as_object()
        .ok_or_else(|| "canonical search rerank advisorySummary must be an object".to_owned())?;
    if !matches!(
        summary.get("scope").and_then(serde_json::Value::as_str),
        Some(
            crate::core::search::SEARCH_ADVISORY_SCOPE_INVOCATION
                | crate::core::search::SEARCH_ADVISORY_SCOPE_PROCESS
                | "response",
        )
    ) || !summary
        .get("permanent")
        .is_some_and(|permanent| permanent.is_null() || permanent.is_boolean())
        || ![
            "distinctCount",
            "emittedCount",
            "suppressedCount",
            "sessionOccurrenceCount",
            "sessionSuppressedCount",
        ]
        .iter()
        .all(|field| summary.get(*field).is_some_and(is_json_unsigned))
    {
        return Err(
            "canonical search rerank advisorySummary field types or values drifted".to_owned(),
        );
    }
    Ok(())
}

fn validate_canonical_search_result(
    result: &serde_json::Value,
    index: usize,
) -> Result<(), String> {
    const REQUIRED: &[&str] = &[
        "docId",
        "score",
        "relevanceScore",
        "scoreKind",
        "scoreInterval",
        "coverageGuarantee",
        "calibrated",
        "source",
        "why",
        "provenance",
    ];
    const OPTIONAL: &[&str] = &[
        "memoryId",
        "fastScore",
        "qualityScore",
        "lexicalScore",
        "rerankScore",
        "metadata",
        "driftHint",
        "meshProvenance",
        "meshTrustAdjustment",
        "content",
        "content_truncated",
        "contentRedacted",
        "redactions",
        "tombstoned",
        "tombstonedAt",
        "validFrom",
        "validTo",
        "validityStatus",
        "validityWindowKind",
        "explanation",
    ];
    let context = format!("canonical search result[{index}]");
    validate_exact_object_fields(result, &context, REQUIRED, OPTIONAL)?;
    if !["docId", "why"]
        .iter()
        .all(|field| result.get(*field).is_some_and(serde_json::Value::is_string))
        || !result
            .get("provenance")
            .is_some_and(serde_json::Value::is_array)
        || !result
            .get("calibrated")
            .is_some_and(serde_json::Value::is_boolean)
    {
        return Err(format!("{context} required field types drifted"));
    }
    for field in ["score", "relevanceScore"] {
        let value = result
            .get(field)
            .and_then(serde_json::Value::as_f64)
            .ok_or_else(|| format!("{context}.{field} must be a number"))?;
        if !value.is_finite() {
            return Err(format!("{context}.{field} must be finite"));
        }
    }
    let relevance = result["relevanceScore"].as_f64().unwrap_or_default();
    if !(0.0..=1.0).contains(&relevance) {
        return Err(format!("{context}.relevanceScore must be between 0 and 1"));
    }
    if !matches!(
        result.get("scoreKind").and_then(serde_json::Value::as_str),
        Some("unit_normalized" | "cosine_similarity" | "rrf_fused" | "reranked")
    ) || !matches!(
        result.get("source").and_then(serde_json::Value::as_str),
        Some(
            "lexical"
                | "semantic_fast"
                | "semantic_quality"
                | "hash_control"
                | "hybrid"
                | "reranked"
        )
    ) {
        return Err(format!("{context} score/source vocabulary drifted"));
    }
    let interval = result
        .get("scoreInterval")
        .and_then(serde_json::Value::as_array)
        .ok_or_else(|| format!("{context}.scoreInterval must be an array"))?;
    if interval.len() != 2
        || !interval
            .iter()
            .all(|value| value.as_f64().is_some_and(f64::is_finite))
    {
        return Err(format!(
            "{context}.scoreInterval must contain two finite numbers"
        ));
    }
    if !result.get("coverageGuarantee").is_some_and(|value| {
        value.is_null()
            || value
                .as_f64()
                .is_some_and(|number| number.is_finite() && (0.0..=1.0).contains(&number))
    }) {
        return Err(format!("{context}.coverageGuarantee drifted"));
    }
    Ok(())
}

fn dispatch_search(
    request: &DaemonRequest,
    shutdown: &AtomicBool,
    search_advisory_session: &Mutex<SearchAdvisorySession>,
    defer_advisory_until_socket_write: bool,
) -> DaemonResponse {
    let daemon_total_start = Instant::now();
    if shutdown.load(Ordering::SeqCst) {
        return daemon_shutting_down_response(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
        );
    }
    let params = match DaemonSearchParams::from_value(&request.params) {
        Ok(params) => params,
        Err(message) => return daemon_search_params_error(request, &message),
    };
    let Some(authorized_workspace_id) = request.workspace_id.as_deref() else {
        return daemon_search_params_error(
            request,
            "authorized envelope `workspace_id` is missing",
        );
    };
    let (options, kind, field_filters, explain_performance) =
        match params.into_search_parts(authorized_workspace_id) {
            Ok(parts) => parts,
            Err(message) => return daemon_search_params_error(request, &message),
        };
    let search_run =
        match run_search_with_performance_and_filters(&options, kind.as_deref(), &field_filters) {
            Ok(run) => run,
            Err(error) => {
                return DaemonResponse::err(
                    request.request_id.clone(),
                    request.agent_id.clone(),
                    request.workspace_id.clone(),
                    DAEMON_SEARCH_EXECUTION_FAILED_CODE,
                    format!("ee.daemon.search could not execute canonical search: {error}"),
                );
            }
        };
    let advisory_workspace_id =
        crate::core::workspace::bound_workspace_id_from_path(&options.workspace_path);
    let performance = explain_performance.then(|| {
        search_run.report.performance_explain_json_with_trace(
            options.speed,
            options.explain,
            &search_run.performance,
        )
    });
    let report = search_run.report;
    let mut pending_delivery =
        PendingSearchAdvisoryDelivery::new(search_advisory_session, &advisory_workspace_id);
    let timing =
        DaemonSearchTiming::from_trace(daemon_total_start.elapsed(), &search_run.performance);
    let mut method_result = {
        let mut advisory_session = search_advisory_session
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        DaemonSearchResult::from_report_for_delivery(
            &report,
            options.explain,
            &advisory_workspace_id,
            &mut advisory_session,
            pending_delivery.reservation_mut(),
            timing,
            performance,
        )
    };
    method_result.timing.daemon_total =
        DaemonSearchTimingMeasurement::from_duration(daemon_total_start.elapsed());
    let response_degraded_codes = daemon_search_degraded_codes(&method_result);
    let result = match serde_json::to_value(method_result) {
        Ok(result) => result,
        Err(error) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_SEARCH_EXECUTION_FAILED_CODE,
                format!("ee.daemon.search could not encode its response: {error}"),
            );
        }
    };
    let mut response = DaemonResponse::ok(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        result,
    );
    for code in response_degraded_codes {
        response = response.with_degraded(code);
    }
    if !daemon_response_fits(&response, super::DAEMON_RESPONSE_MAX_BYTES) {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_SEARCH_EXECUTION_FAILED_CODE,
            "ee.daemon.search response exceeded the daemon response cap; lower --limit or use canonical in-process search.",
        );
    }
    pending_delivery.finish(response, defer_advisory_until_socket_write)
}

fn daemon_response_fits(response: &DaemonResponse, max_bytes: usize) -> bool {
    serde_json::to_vec(response).is_ok_and(|encoded| encoded.len() <= max_bytes)
}

fn daemon_search_params_error(request: &DaemonRequest, message: &str) -> DaemonResponse {
    DaemonResponse::err(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        DAEMON_SEARCH_PARAMS_INVALID_CODE,
        format!("invalid ee.daemon.search params: {message}"),
    )
}

/// Per-connection carrier for the write-routing path (Inc 2, bd-wx6ou.3): the
/// daemon's shared asupersync runtime plus a clone of the long-lived
/// write-owner actor's submit handle. Cloned into each connection worker so
/// `dispatch_write` can submit a `WriteOperation` and `block_on` the resulting
/// `WriteResult`. `Runtime` is `Send + Sync` (it wraps `Arc<RuntimeInner>`), so
/// `Arc<Runtime>` shares safely across the accept/connection threads.
#[derive(Clone)]
struct DaemonWriteRouter {
    runtime: Arc<asupersync::runtime::Runtime>,
    handle: crate::core::write_owner::WriteHandle,
}

impl std::fmt::Debug for DaemonWriteRouter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // The runtime and write handle have no meaningful Debug; this exists
        // only so `DaemonDispatchPolicy` can keep deriving Debug.
        f.debug_struct("DaemonWriteRouter").finish_non_exhaustive()
    }
}

/// Parsed `ee.daemon.write` params: the inputs `remember_memory` needs, carried
/// over the socket so the daemon executes a write byte-identical to
/// `ee remember`. Owns its strings so `RememberMemoryOptions` can borrow them.
///
/// Serde is derived (snake_case) so these params can ride through the
/// long-lived write-owner actor as a `WriteOperation::Custom` payload (Inc 2,
/// bd-wx6ou.3): the low-level `WriteOperation::MemoryCreate` variant lacks the
/// `confidence`/`workflow_id`/`auto_link`/`propose_candidates` fields a faithful
/// `ee remember` needs, so we carry the whole owned params object instead.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
struct DaemonWriteParams {
    workspace_path: PathBuf,
    content: String,
    level: String,
    kind: String,
    tags: Option<String>,
    confidence: f32,
    source: Option<String>,
    workflow_id: Option<String>,
    auto_link: bool,
    propose_candidates: bool,
}

impl DaemonWriteParams {
    fn from_value(value: &serde_json::Value) -> Result<Self, String> {
        let object = value
            .as_object()
            .ok_or_else(|| "`params` must be a JSON object for ee.daemon.write".to_string())?;
        #[allow(clippy::cast_possible_truncation)]
        let confidence = object
            .get("confidence")
            .and_then(serde_json::Value::as_f64)
            .map_or(0.8_f32, |value| value as f32);
        Ok(Self {
            workspace_path: required_path_any(
                object,
                &["workspacePath", "workspace_path", "workspace"],
            )?,
            content: required_string_any(object, &["content"])?,
            level: optional_string_any(object, &["level"])?
                .unwrap_or_else(|| "episodic".to_string()),
            kind: optional_string_any(object, &["kind"])?.unwrap_or_else(|| "fact".to_string()),
            tags: optional_string_any(object, &["tags"])?,
            confidence,
            source: optional_string_any(object, &["source"])?,
            workflow_id: optional_string_any(object, &["workflow", "workflowId", "workflow_id"])?,
            auto_link: optional_bool_any(object, &["autoLink", "auto_link"])?.unwrap_or(true),
            propose_candidates: optional_bool_any(
                object,
                &["proposeCandidates", "propose_candidates"],
            )?
            .unwrap_or(true),
        })
    }

    fn options(&self) -> crate::core::memory::RememberMemoryOptions<'_> {
        crate::core::memory::RememberMemoryOptions {
            workspace_path: &self.workspace_path,
            database_path: None,
            content: &self.content,
            workflow_id: self.workflow_id.as_deref(),
            level: &self.level,
            kind: &self.kind,
            tags: self.tags.as_deref(),
            confidence: self.confidence,
            source: self.source.as_deref(),
            allow_secret_mention: false,
            valid_from: None,
            valid_to: None,
            dry_run: false,
            auto_link: self.auto_link,
            propose_candidates: self.propose_candidates,
        }
    }

    fn database_path(&self) -> PathBuf {
        self.workspace_path.join(".ee").join("ee.db")
    }

    /// `operation_type` tag for the `WriteOperation::Custom` that carries these
    /// params through the write-owner actor (Inc 2).
    const ACTOR_OPERATION_TYPE: &'static str = "ee.daemon.remember";

    /// Serialize into the `WriteOperation::Custom` payload submitted to the
    /// actor. Round-trips with [`DaemonWriteParams::from_payload`].
    fn to_payload(&self) -> serde_json::Value {
        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
    }

    /// Reconstruct from a `WriteOperation::Custom` payload inside the actor's
    /// `process_batch`. Errors map to a domain write failure for that op.
    fn from_payload(payload: &serde_json::Value) -> Result<Self, String> {
        serde_json::from_value(payload.clone()).map_err(|error| error.to_string())
    }
}

/// Bind a write's `params.workspacePath` to the authorized envelope `workspace_id`.
///
/// `ee.daemon.write` and `ee.daemon.write_journal` are classified
/// [`DaemonAuthority::SameUidWorkspace`] and advertised as `same_uid_workspace`
/// by `ee.daemon.capabilities`, but `authorize_daemon_method` only compares the
/// ENVELOPE `workspace_id` against the daemon's bound workspace. The read
/// methods close the loop by additionally binding their own `workspacePath`
/// param — see `DaemonSearchParams::into_search_parts` and the context
/// dispatcher. The write methods did not, so a caller could present an
/// authorized envelope while pointing `params.workspacePath` at any other
/// initialized workspace on the machine: `RememberMemoryOptions.workspace_path`
/// and `database_path()` both derive from that field, so the write landed in
/// the other workspace's database. Same confinement check, applied before
/// anything reaches the write owner.
///
/// This binds the field that selects the DATABASE. A journal payload also
/// carries its own `workspace_id`, which only tags the row inside that
/// database and lives in the stable-id namespace rather than the path
/// namespace the envelope uses, so it is deliberately not compared here.
fn bind_write_workspace_to_authorized(
    request: &DaemonRequest,
    params_workspace_path: &Path,
) -> Result<(), String> {
    let Some(authorized_workspace_id) = request.workspace_id.as_deref() else {
        return Err("authorized envelope `workspace_id` is missing".to_owned());
    };
    let workspace_path = canonical_workspace_path(params_workspace_path, "workspacePath")?;
    let authorized_workspace =
        canonical_workspace_path(Path::new(authorized_workspace_id), "workspace_id")?;
    if workspace_path != authorized_workspace {
        return Err(
            "field `workspacePath` must identify the authorized envelope `workspace_id`".to_owned(),
        );
    }
    Ok(())
}

/// Dispatch `ee.daemon.write`: execute a durable memory write. Inc 1 routes it
/// straight through `remember_memory` (no actor), so the result is identical to
/// `ee remember`. RPC-level failures (bad params) become a `DaemonResponse::err`;
/// a domain-level write failure becomes an `ok` response carrying
/// `{success:false, error:{code,message}}` so the client can distinguish "the
/// daemon could not accept this" from "the write itself failed". bd-wx6ou.2.
fn dispatch_write(
    request: &DaemonRequest,
    write_router: Option<&DaemonWriteRouter>,
) -> DaemonResponse {
    let params = match DaemonWriteParams::from_value(&request.params) {
        Ok(params) => params,
        Err(message) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_WRITE_PARAMS_INVALID_CODE,
                message,
            );
        }
    };
    if let Err(message) = bind_write_workspace_to_authorized(request, &params.workspace_path) {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_WRITE_PARAMS_INVALID_CODE,
            message,
        );
    }
    // When the long-lived write-owner actor is hosted (bound workspace), route
    // the write through it so it can coalesce with siblings (Inc 2 wires the
    // path; real batching is Inc 3). Otherwise fall through to the in-process
    // direct path (Inc 1), which is the source of truth and is never removed.
    if let Some(router) = write_router {
        return dispatch_write_via_actor(request, &params, router);
    }
    let result = match crate::core::memory::remember_memory(&params.options()) {
        Ok(report) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": true,
            "entityId": report.memory_id.to_string(),
        }),
        Err(error) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": false,
            "error": {
                "code": error.code(),
                "message": error.message(),
            },
        }),
    };
    DaemonResponse::ok(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        result,
    )
}

/// Route a parsed write through the long-lived write-owner actor and block the
/// connection thread until the (eventually coalesced) `WriteResult` returns.
/// The connection thread is a plain std::thread, so `runtime.block_on` parks it
/// while the actor task (on the runtime's worker thread) runs `process_batch`
/// and sends on the oneshot — no deadlock (distinct threads). bd-wx6ou.3.
fn dispatch_write_via_actor(
    request: &DaemonRequest,
    params: &DaemonWriteParams,
    router: &DaemonWriteRouter,
) -> DaemonResponse {
    let operation = crate::core::write_owner::WriteOperation::Custom {
        operation_type: DaemonWriteParams::ACTOR_OPERATION_TYPE.to_string(),
        payload: params.to_payload(),
    };
    submit_op_via_actor(request, router, operation)
}

/// Submit a prepared write op to the long-lived actor and block the connection
/// thread on the (eventually coalesced) `WriteResult`, mapping it to the
/// `ee.daemon.write.v1` response shape. Shared by the remember (Inc 2) and
/// journal (Inc 4) daemon write paths. bd-wx6ou.
fn submit_op_via_actor(
    request: &DaemonRequest,
    router: &DaemonWriteRouter,
    operation: crate::core::write_owner::WriteOperation,
) -> DaemonResponse {
    use crate::core::write_owner::WriteResult;
    let Some(mut receiver) = router.handle.try_submit(operation) else {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            super::DAEMON_OVERLOADED_CODE,
            "write-owner actor queue is saturated; retry shortly",
        )
        .with_degraded(super::DAEMON_OVERLOADED_CODE);
    };
    let write_result = router.runtime.block_on(async {
        // Invariant: block_on always installs an ambient Cx for the closure.
        #[allow(clippy::expect_used)]
        let cx = asupersync::Cx::current().expect("Runtime::block_on installs an ambient Cx");
        receiver.recv(&cx).await
    });
    let result = match write_result {
        Ok(WriteResult::Success { entity_id }) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": true,
            "entityId": entity_id,
        }),
        Ok(WriteResult::Failed { error }) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": false,
            "error": { "code": error.code(), "message": error.message() },
        }),
        Ok(WriteResult::Shutdown) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": false,
            "error": {
                "code": super::DAEMON_SHUTTING_DOWN_CODE,
                "message": "write-owner actor is shutting down",
            },
        }),
        Err(_) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": false,
            "error": {
                "code": "write_owner_unavailable",
                "message": "write-owner actor dropped the request before responding",
            },
        }),
    };
    DaemonResponse::ok(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        result,
    )
}

/// Dispatch `ee.daemon.write_journal`: route a journal write through the actor
/// (coalesced with sibling journal writes) when the actor is hosted, else
/// execute it directly as a single-op batch (still durable, no coalescing). The
/// request params ARE the `DaemonJournalParams` payload, carried verbatim into
/// the `ee.daemon.journal` op. bd-wx6ou.5 (Inc 4 daemon half).
fn dispatch_journal(
    request: &DaemonRequest,
    write_router: Option<&DaemonWriteRouter>,
) -> DaemonResponse {
    use crate::core::write_owner::{WriteOperation, WriteResult};
    let journal_params = match DaemonJournalParams::from_payload(&request.params) {
        Ok(params) => params,
        Err(message) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_JOURNAL_PARAMS_INVALID_CODE,
                message,
            );
        }
    };
    if let Err(message) =
        bind_write_workspace_to_authorized(request, &journal_params.workspace_path)
    {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_JOURNAL_PARAMS_INVALID_CODE,
            message,
        );
    }
    let operation = WriteOperation::Custom {
        operation_type: DaemonJournalParams::ACTOR_OPERATION_TYPE.to_string(),
        payload: request.params.clone(),
    };
    if let Some(router) = write_router {
        return submit_op_via_actor(request, router, operation);
    }
    // No actor (unbound daemon): execute the journal write directly as a
    // single-op batch — durable, just not coalesced.
    let result = match execute_journal_batch(std::slice::from_ref(&operation)) {
        Ok(mut results) => {
            let entity_id = match results.pop() {
                Some(WriteResult::Success { entity_id }) => entity_id,
                _ => None,
            };
            serde_json::json!({
                "schema": "ee.daemon.write.v1",
                "success": true,
                "entityId": entity_id,
            })
        }
        Err(error) => serde_json::json!({
            "schema": "ee.daemon.write.v1",
            "success": false,
            "error": { "code": error.code(), "message": error.message() },
        }),
    };
    DaemonResponse::ok(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        result,
    )
}

/// Execute one `WriteOperation` inside the write-owner actor's `process_batch`
/// (Inc 2, bd-wx6ou.3). Only the `ee.daemon.remember` Custom op is supported —
/// the daemon write path carries `DaemonWriteParams` in the payload and runs
/// the same `remember_memory` the direct path uses, so a daemon-routed write is
/// byte-identical to `ee remember`. Unsupported ops fail that single request
/// (the actor maps a per-op error onto its `WriteResult`).
fn execute_write_operation(
    operation: &crate::core::write_owner::WriteOperation,
) -> crate::core::write_owner::WriteResult {
    use crate::core::write_owner::{WriteOperation, WriteResult};
    let WriteOperation::Custom {
        operation_type,
        payload,
    } = operation
    else {
        return WriteResult::Failed {
            error: crate::models::DomainError::Storage {
                message: format!(
                    "write-owner actor received an unsupported operation: {}",
                    operation.operation_type()
                ),
                repair: Some("only ee.daemon.remember is wired in this build".to_string()),
            },
        };
    };
    if operation_type != DaemonWriteParams::ACTOR_OPERATION_TYPE {
        return WriteResult::Failed {
            error: crate::models::DomainError::Storage {
                message: format!(
                    "write-owner actor received an unsupported custom op: {operation_type}"
                ),
                repair: Some("only ee.daemon.remember is wired in this build".to_string()),
            },
        };
    }
    let params = match DaemonWriteParams::from_payload(payload) {
        Ok(params) => params,
        Err(message) => {
            return WriteResult::Failed {
                error: crate::models::DomainError::Storage {
                    message: format!("daemon write payload decode failed: {message}"),
                    repair: Some("retry the write".to_string()),
                },
            };
        }
    };
    match crate::core::memory::remember_memory(&params.options()) {
        Ok(report) => WriteResult::Success {
            entity_id: Some(report.memory_id.to_string()),
        },
        Err(error) => WriteResult::Failed { error },
    }
}

/// Parsed `ee.daemon.journal` op payload (Inc 3, bd-wx6ou.4): the inputs a
/// journal write needs, carried over the socket. Owns its strings. Defines our
/// own payload (rather than reusing the direct-path `journal_append` shape) so
/// the daemon journal path does not depend on `core::journal` internals.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct DaemonJournalParams {
    workspace_path: PathBuf,
    workspace_id: String,
    /// Caller-supplied id for idempotency (Inc 4); generated when absent.
    entry_id: Option<String>,
    agent_name: Option<String>,
    session_key: Option<String>,
    kind: String,
    source: String,
    body: String,
    structured: Option<String>,
    redaction_report: String,
    instruction_risk: String,
}

impl DaemonJournalParams {
    /// `operation_type` tag for the `WriteOperation::Custom` carrying a journal
    /// write through the actor. Distinct from the direct-path `journal_append`.
    const ACTOR_OPERATION_TYPE: &'static str = "ee.daemon.journal";

    fn from_payload(payload: &serde_json::Value) -> Result<Self, String> {
        serde_json::from_value(payload.clone()).map_err(|error| error.to_string())
    }

    fn database_path(&self) -> PathBuf {
        self.workspace_path.join(".ee").join("ee.db")
    }

    fn into_create_input(self) -> crate::db::CreateJournalEntryInput {
        crate::db::CreateJournalEntryInput {
            entry_id: self
                .entry_id
                .unwrap_or_else(crate::core::journal::generate_journal_entry_id),
            workspace_id: self.workspace_id,
            agent_name: self.agent_name,
            session_key: self.session_key,
            kind: self.kind,
            source: self.source,
            body: self.body,
            structured: self.structured,
            redaction_report: self.redaction_report,
            instruction_risk: self.instruction_risk,
        }
    }
}

/// Parsed prevalidated outcome write payload for the daemon write-owner actor.
///
/// This is intentionally narrower than the public `ee outcome` CLI: callers
/// supply the already-normalized feedback row and audit id, and the actor owns
/// only the transaction/coalescing boundary. The ordinary CLI path continues to
/// do validation, quarantine decisions, and derived follow-up updates.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
struct DaemonOutcomeParams {
    workspace_path: PathBuf,
    event_id: String,
    workspace_id: String,
    target_type: String,
    target_id: String,
    signal: String,
    weight: f32,
    source_type: String,
    source_id: Option<String>,
    reason: Option<String>,
    evidence_json: Option<String>,
    session_id: Option<String>,
    actor: Option<String>,
    audit_id: String,
    details: Option<String>,
}

impl DaemonOutcomeParams {
    const ACTOR_OPERATION_TYPE: &'static str = "ee.daemon.outcome";

    fn from_payload(payload: &serde_json::Value) -> Result<Self, String> {
        serde_json::from_value(payload.clone()).map_err(|error| error.to_string())
    }

    fn database_path(&self) -> PathBuf {
        self.workspace_path.join(".ee").join("ee.db")
    }

    fn into_parts(self) -> (String, crate::db::AuditedFeedbackEventInput, String) {
        let event_id = self.event_id;
        let audit_id = self.audit_id;
        let input = crate::db::AuditedFeedbackEventInput {
            event: crate::db::CreateFeedbackEventInput {
                workspace_id: self.workspace_id,
                target_type: self.target_type,
                target_id: self.target_id,
                signal: self.signal,
                weight: self.weight,
                source_type: self.source_type,
                source_id: self.source_id,
                reason: self.reason,
                evidence_json: self.evidence_json,
                session_id: self.session_id,
            },
            actor: self.actor,
            details: self.details,
        };
        (event_id, input, audit_id)
    }
}

enum DaemonTxnBatchEntry {
    Journal(DaemonJournalParams),
    Outcome(DaemonOutcomeParams),
    Remember(DaemonWriteParams),
}

enum PreparedDaemonTxnBatchEntry {
    Journal(DaemonJournalParams),
    Outcome(DaemonOutcomeParams),
    Remember(crate::core::memory::PreparedRememberTxnWrite),
}

impl DaemonTxnBatchEntry {
    fn database_path(&self) -> PathBuf {
        match self {
            Self::Journal(params) => params.database_path(),
            Self::Outcome(params) => params.database_path(),
            Self::Remember(params) => params.database_path(),
        }
    }
}

/// True when every op in the batch can share one daemon transaction.
fn batch_is_daemon_txn_coalescible(
    operations: &[crate::core::write_owner::WriteOperation],
) -> bool {
    use crate::core::write_owner::WriteOperation;
    !operations.is_empty()
        && operations.iter().all(|operation| {
            matches!(
                operation,
                WriteOperation::Custom { operation_type, .. }
                    if operation_type == DaemonJournalParams::ACTOR_OPERATION_TYPE
                        || operation_type == DaemonOutcomeParams::ACTOR_OPERATION_TYPE
                        || operation_type == DaemonWriteParams::ACTOR_OPERATION_TYPE
            )
        })
}

fn parse_daemon_txn_batch_entry(
    operation: &crate::core::write_owner::WriteOperation,
) -> Result<DaemonTxnBatchEntry, crate::models::DomainError> {
    use crate::core::write_owner::WriteOperation;
    let WriteOperation::Custom {
        operation_type,
        payload,
    } = operation
    else {
        return Err(crate::models::DomainError::Storage {
            message: "daemon transaction batch received a non-Custom operation".to_string(),
            repair: Some("only daemon journal/outcome/remember ops are batched here".to_string()),
        });
    };
    match operation_type.as_str() {
        DaemonJournalParams::ACTOR_OPERATION_TYPE => DaemonJournalParams::from_payload(payload)
            .map(DaemonTxnBatchEntry::Journal)
            .map_err(|message| crate::models::DomainError::Storage {
                message: format!("daemon journal payload decode failed: {message}"),
                repair: Some("retry the write".to_string()),
            }),
        DaemonOutcomeParams::ACTOR_OPERATION_TYPE => DaemonOutcomeParams::from_payload(payload)
            .map(DaemonTxnBatchEntry::Outcome)
            .map_err(|message| crate::models::DomainError::Storage {
                message: format!("daemon outcome payload decode failed: {message}"),
                repair: Some("retry the write".to_string()),
            }),
        DaemonWriteParams::ACTOR_OPERATION_TYPE => DaemonWriteParams::from_payload(payload)
            .map(DaemonTxnBatchEntry::Remember)
            .map_err(|message| crate::models::DomainError::Storage {
                message: format!("daemon remember payload decode failed: {message}"),
                repair: Some("retry the write".to_string()),
            }),
        _ => Err(crate::models::DomainError::Storage {
            message: format!("unsupported daemon transaction op: {operation_type}"),
            repair: Some("only daemon journal/outcome/remember ops are batched here".to_string()),
        }),
    }
}

/// Execute a homogeneous batch of `ee.daemon.journal` ops in ONE transaction
/// (Inc 3, bd-wx6ou.4): open the workspace DB connection once, insert all N
/// journal entries inside a single `with_transaction` so the whole batch shares
/// ONE commit boundary — one `FileWriteOwnerGuard` acquisition, one WAL commit
/// frame, one COMMIT round-trip — which is the coalescing win. (Under WAL
/// `synchronous=NORMAL` (`src/db/mod.rs:1709`), COMMIT does not fsync; the WAL
/// is synced only at checkpoint, so the saving is commit-boundary overhead, not
/// per-commit fsyncs. The daemon ACK is therefore app/process-crash-durable, not
/// power-loss-durable — see ADR 0077 and bd-d67os.16.) All ops target the
/// daemon's bound workspace, so one connection serves them. All-or-nothing: a
/// failed insert rolls back the batch and fails every request in it (the CLI
/// falls back to the direct path per Inc 4).
fn execute_journal_batch(
    operations: &[crate::core::write_owner::WriteOperation],
) -> Result<Vec<crate::core::write_owner::WriteResult>, crate::models::DomainError> {
    execute_daemon_txn_batch(operations)
}

fn execute_daemon_txn_batch(
    operations: &[crate::core::write_owner::WriteOperation],
) -> Result<Vec<crate::core::write_owner::WriteResult>, crate::models::DomainError> {
    use crate::core::write_owner::WriteResult;
    let mut entries = Vec::with_capacity(operations.len());
    for operation in operations {
        entries.push(parse_daemon_txn_batch_entry(operation)?);
    }
    let Some(first) = entries.first() else {
        return Ok(Vec::new());
    };
    let database_path = first.database_path();
    for entry in &entries {
        if entry.database_path() != database_path {
            return Err(crate::models::DomainError::Storage {
                message: "daemon transaction batch crossed database paths".to_string(),
                repair: Some("retry the writes separately".to_string()),
            });
        }
        if let DaemonTxnBatchEntry::Remember(params) = entry
            && let Some(error) = crate::core::memory::remember_level_kind_cross_wire_error(
                &params.level,
                &params.kind,
            )
        {
            return Err(error);
        }
    }
    // The daemon owns batching, not store creation. Preflight the exact path
    // before the create-capable database open so a queued write addressed at
    // a misspelled workspace cannot plant a new store as a side effect
    // (bd-workspace-miss-init-suggestion-sfjvq).
    crate::core::ensure_addressed_database_exists(&database_path)?;
    let connection = crate::db::DbConnection::open_file(&database_path).map_err(|error| {
        crate::models::DomainError::Storage {
            message: format!(
                "daemon transaction batch could not open {}: {error}",
                database_path.display()
            ),
            repair: Some("ensure the workspace is initialized".to_string()),
        }
    })?;
    let mut prepared_entries = Vec::with_capacity(entries.len());
    for entry in entries {
        match entry {
            DaemonTxnBatchEntry::Journal(entry) => {
                prepared_entries.push(PreparedDaemonTxnBatchEntry::Journal(entry));
            }
            DaemonTxnBatchEntry::Outcome(entry) => {
                prepared_entries.push(PreparedDaemonTxnBatchEntry::Outcome(entry));
            }
            DaemonTxnBatchEntry::Remember(entry) => {
                let write = crate::core::memory::prepare_remember_txn_write_for_connection(
                    &connection,
                    &entry.options(),
                    true,
                )?;
                prepared_entries.push(PreparedDaemonTxnBatchEntry::Remember(write));
            }
        }
    }
    let mut results = connection
        .with_transaction(|| {
            let mut out = Vec::with_capacity(prepared_entries.len());
            for entry in &prepared_entries {
                match entry {
                    PreparedDaemonTxnBatchEntry::Journal(entry) => {
                        let workspace_id = crate::core::journal::ensure_workspace(
                            &connection,
                            &entry.workspace_id,
                            &entry.workspace_path,
                        )
                        .map_err(|error| {
                            crate::db::DbError::MalformedRow {
                                operation: crate::db::DbOperation::Execute,
                                message: error.message().to_string(),
                            }
                        })?;
                        let mut input = entry.clone().into_create_input();
                        input.workspace_id = workspace_id;
                        let entry_id = input.entry_id.clone();
                        connection.insert_journal_entry(&input)?;
                        out.push(WriteResult::Success {
                            entity_id: Some(entry_id),
                        });
                    }
                    PreparedDaemonTxnBatchEntry::Outcome(entry) => {
                        let workspace_id = crate::core::workspace::ensure_bound_workspace(
                            &connection,
                            &entry.workspace_id,
                            &[entry.workspace_path.as_path()],
                        )
                        .map_err(|error| {
                            crate::db::DbError::MalformedRow {
                                operation: crate::db::DbOperation::Execute,
                                message: error.message().to_string(),
                            }
                        })?;
                        let (event_id, mut input, audit_id) = entry.clone().into_parts();
                        input.event.workspace_id = workspace_id;
                        crate::core::outcome::record_outcome_feedback_event_in_txn(
                            &connection,
                            &event_id,
                            &input,
                            audit_id,
                        )?;
                        out.push(WriteResult::Success {
                            entity_id: Some(event_id),
                        });
                    }
                    PreparedDaemonTxnBatchEntry::Remember(write) => {
                        crate::core::memory::record_prepared_remember_txn_write_in_txn(
                            &connection,
                            write,
                        )?;
                        out.push(WriteResult::Success {
                            entity_id: Some(write.memory_id().to_owned()),
                        });
                    }
                }
            }
            Ok(out)
        })
        .map_err(|error| crate::models::DomainError::Storage {
            message: format!("daemon transaction batch failed: {error}"),
            repair: Some("retry the write".to_string()),
        })?;

    let mut index_drains: BTreeMap<String, (String, PathBuf)> = BTreeMap::new();
    for (index, entry) in prepared_entries.into_iter().enumerate() {
        if let PreparedDaemonTxnBatchEntry::Remember(write) = entry {
            let index_dir = write.index_dir().to_path_buf();
            match crate::core::memory::finish_prepared_remember_txn_write(&connection, write) {
                Ok(report) => {
                    index_drains.insert(
                        report.workspace_id.clone(),
                        (report.workspace_id.clone(), index_dir),
                    );
                    results[index] = WriteResult::Success {
                        entity_id: Some(report.memory_id.to_string()),
                    };
                }
                Err(error) => {
                    results[index] = WriteResult::Failed { error };
                }
            }
        }
    }
    for (_, (workspace_id, index_dir)) in index_drains {
        match crate::core::memory::reconcile_pending_remember_index_jobs(
            &connection,
            &workspace_id,
            &index_dir,
        ) {
            Ok(None) => {}
            Ok(Some(report))
                if matches!(
                    report.outcome.as_str(),
                    "completed" | "completed_no_documents"
                ) => {}
            Ok(Some(report)) => {
                tracing::warn!(
                    target: "ee::daemon::write_owner",
                    workspace_id,
                    job_id = report.job_id.as_str(),
                    outcome = report.outcome.as_str(),
                    processing_mode = report.processing_mode.as_str(),
                    "daemon remember batch committed source rows while derived index reconciliation remained nonterminal"
                );
            }
            Err(error) => {
                // The transaction above is already durable. Rewriting every
                // successful journal/outcome/remember result as failed would
                // invite duplicate source writes on retry and falsely blame
                // unrelated operations for a derived-index problem. The
                // pending index jobs remain the recovery mechanism.
                tracing::warn!(
                    target: "ee::daemon::write_owner",
                    workspace_id,
                    error = %error,
                    "daemon remember batch committed source rows but could not reconcile the derived index; preserving durable write success"
                );
            }
        }
    }
    Ok(results)
}

#[derive(Clone, Debug, PartialEq)]
struct DaemonContextParams {
    query: String,
    workspace_path: PathBuf,
    database_path: Option<PathBuf>,
    index_dir: Option<PathBuf>,
    max_tokens: Option<u32>,
    candidate_pool: Option<u32>,
    max_results: Option<u32>,
    profile: Option<ContextPackProfile>,
    speed: SpeedMode,
    source_mode: SearchSourceMode,
    strict_source_mode: bool,
    pack_profile: crate::core::context::ContextPackOutputProfile,
    resource_profile: PackResourceProfile,
    no_coverage_fill: Option<bool>,
    no_rendered_text: Option<bool>,
    no_skipped: Option<bool>,
    no_meta: Option<bool>,
    include_non_affecting_degradations: Option<bool>,
    explain: bool,
    no_pack_dna: bool,
    read_only: bool,
    timeout_ms: Option<u64>,
}

impl DaemonContextParams {
    fn from_value(value: &serde_json::Value) -> Result<Self, String> {
        let object = value
            .as_object()
            .ok_or_else(|| "`params` must be a JSON object for ee.daemon.context".to_string())?;
        let query = required_string_any(object, &["task", "query"])?;
        let workspace_path =
            required_path_any(object, &["workspacePath", "workspace_path", "workspace"])?;
        let profile = Some(
            optional_string_any(object, &["profile"])?
                .map(|value| parse_daemon_context_profile(&value))
                .transpose()?
                .unwrap_or(ContextPackProfile::Balanced),
        );
        let speed = optional_string_any(object, &["speed"])?
            .map(|value| parse_daemon_speed_mode(&value))
            .transpose()?
            .unwrap_or_default();
        let source_mode = optional_string_any(object, &["sourceMode", "source_mode"])?
            .map(|value| parse_daemon_source_mode(&value))
            .transpose()?
            .unwrap_or_default();
        let pack_profile = optional_string_any(object, &["packProfile", "pack_profile"])?
            .map(|value| parse_daemon_pack_output_profile(&value))
            .transpose()?
            .unwrap_or_default();
        let resource_profile =
            optional_string_any(object, &["resourceProfile", "resource_profile"])?
                .map(|value| {
                    value
                        .parse::<PackResourceProfile>()
                        .map_err(|error| error.to_string())
                })
                .transpose()?
                .unwrap_or_default();

        Ok(Self {
            query,
            workspace_path,
            database_path: optional_path_any(
                object,
                &["databasePath", "database_path", "database"],
            )?,
            index_dir: optional_path_any(object, &["indexDir", "index_dir"])?,
            max_tokens: optional_u32_any(object, &["maxTokens", "max_tokens"])?,
            candidate_pool: optional_u32_any(object, &["candidatePool", "candidate_pool"])?,
            max_results: optional_u32_any(object, &["maxResults", "max_results"])?,
            profile,
            speed,
            source_mode,
            strict_source_mode: optional_bool_any(
                object,
                &["strictSourceMode", "strict_source_mode"],
            )?
            .unwrap_or(false),
            pack_profile,
            resource_profile,
            no_coverage_fill: optional_bool_any(object, &["noCoverageFill", "no_coverage_fill"])?,
            no_rendered_text: optional_bool_any(object, &["noRenderedText", "no_rendered_text"])?,
            no_skipped: optional_bool_any(object, &["noSkipped", "no_skipped"])?,
            no_meta: optional_bool_any(object, &["noMeta", "no_meta"])?,
            include_non_affecting_degradations: optional_bool_any(
                object,
                &[
                    "includeNonAffectingDegradations",
                    "include_non_affecting_degradations",
                ],
            )?,
            explain: optional_bool_any(object, &["explain"])?.unwrap_or(false),
            no_pack_dna: optional_bool_any(object, &["noPackDna", "no_pack_dna"])?.unwrap_or(false),
            read_only: optional_bool_any(object, &["readOnly", "read_only"])?.unwrap_or(false),
            timeout_ms: optional_u64_any(
                object,
                &["timeoutMs", "timeout_ms", "deadlineMs", "deadline_ms"],
            )?,
        })
    }

    fn output_options(&self) -> ContextPackOutputOptions {
        ContextPackOutputOptions::for_profile(self.pack_profile)
            .with_overrides(ContextPackOutputOptionOverrides {
                no_coverage_fill: self.no_coverage_fill,
                no_rendered_text: self.no_rendered_text,
                no_skipped: self.no_skipped,
                no_meta: self.no_meta,
                include_non_affecting_degradations: self.include_non_affecting_degradations,
            })
            .with_resource_profile(self.resource_profile)
    }

    fn context_options(&self, authorized_workspace_id: &str) -> Result<ContextPackOptions, String> {
        let workspace_path = canonical_workspace_path(&self.workspace_path, "workspacePath")?;
        let authorized_workspace =
            canonical_workspace_path(Path::new(authorized_workspace_id), "workspace_id")?;
        if workspace_path != authorized_workspace {
            return Err(
                "field `workspacePath` must identify the authorized envelope `workspace_id`"
                    .to_owned(),
            );
        }
        let database_path = self
            .database_path
            .as_deref()
            .map(|path| canonical_contained_path(&workspace_path, path, "databasePath"))
            .transpose()?;
        let index_dir = self
            .index_dir
            .as_deref()
            .map(|path| canonical_contained_path(&workspace_path, path, "indexDir"))
            .transpose()?;
        Ok(ContextPackOptions {
            workspace_path,
            database_path,
            index_dir,
            query: self.query.clone(),
            speed: self.speed,
            source_mode: self.source_mode,
            strict_source_mode: self.strict_source_mode,
            filters: QueryFilters::default(),
            profile: self.profile,
            max_tokens: self.max_tokens,
            candidate_pool: self.candidate_pool,
            max_results: self.max_results,
            include_tombstoned: false,
            as_of: None,
            include_expired: false,
            include_future: false,
            include_stale: false,
            require_fresh_sentinels: false,
            relevance_floor: None,
            redaction_level: RedactionLevel::Minimal,
            memory_scope: MemoryScope::Swarm,
            strict_scope: false,
            ppr_weight: None,
            changed_symbols: Vec::new(),
            changed_symbols_from_git: false,
            pagination: None,
            coordination_snapshot_path: None,
            coordination_stale_after_ms: DEFAULT_COORDINATION_STALE_AFTER_MS,
            task_lens: None,
            output_options: self.output_options(),
            persist_pack: !self.read_only,
            baseline_write: None,
            // bd-1n0np.5.8: the daemon does not expose `--no-lod`; keep LOD
            // tiering on (the default), matching the one-shot CLI default.
            no_lod: false,
        })
    }
}

fn dispatch_context(
    request: &DaemonRequest,
    shutdown: &AtomicBool,
    search_advisory_session: &Mutex<SearchAdvisorySession>,
    defer_advisory_until_socket_write: bool,
) -> DaemonResponse {
    if shutdown.load(Ordering::SeqCst) {
        return daemon_shutting_down_response(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
        );
    }
    let params = match DaemonContextParams::from_value(&request.params) {
        Ok(params) => params,
        Err(message) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_CONTEXT_PARAMS_INVALID_CODE,
                format!("invalid ee.daemon.context params: {message}"),
            );
        }
    };
    let Some(authorized_workspace_id) = request.workspace_id.as_deref() else {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_CONTEXT_PARAMS_INVALID_CODE,
            "authorized envelope `workspace_id` is missing",
        );
    };
    let context_started = Instant::now();
    if matches!(params.timeout_ms, Some(0)) {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_CONTEXT_DEADLINE_EXCEEDED_CODE,
            "ee.daemon.context deadline expired before pack execution started.",
        );
    }

    let options = match params.context_options(authorized_workspace_id) {
        Ok(options) => options,
        Err(message) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_CONTEXT_PARAMS_INVALID_CODE,
                format!("invalid ee.daemon.context params: {message}"),
            );
        }
    };
    let advisory_workspace_id =
        crate::core::workspace::bound_workspace_id_from_path(&options.workspace_path);
    let deadline = params.timeout_ms.map(Duration::from_millis);
    let context_run = match run_context_pack_with_performance_controlled(
        &options,
        "pack",
        deadline,
        Some(shutdown),
    ) {
        Ok(run) => run,
        Err(ContextPackError::DeadlineExceeded(error)) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_CONTEXT_DEADLINE_EXCEEDED_CODE,
                format!(
                    "ee.daemon.context deadline expired: {}",
                    crate::core::outcome::cancel_message(&error)
                ),
            );
        }
        Err(ContextPackError::Cancelled(_)) => {
            return daemon_shutting_down_response(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
            );
        }
        Err(error) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_CONTEXT_EXECUTION_FAILED_CODE,
                format!("ee.daemon.context could not assemble the canonical pack: {error}"),
            );
        }
    };
    let mut context_response = context_run.response;
    let context_search_report = context_run.search_report;
    let context_search_advisory_snapshot = context_run.search_advisory_snapshot;

    if shutdown.load(Ordering::SeqCst) {
        return daemon_shutting_down_response(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
        );
    }
    if daemon_context_deadline_expired(context_started, params.timeout_ms) {
        return daemon_context_deadline_response(
            request,
            "ee.daemon.context deadline expired after pack execution.",
        );
    }
    if params.explain && !params.no_pack_dna {
        let database_path = options
            .database_path
            .clone()
            .unwrap_or_else(|| options.workspace_path.join(".ee").join("ee.db"));
        attach_pack_dna_to_context_response(&database_path, &mut context_response);
    }
    if daemon_context_deadline_expired(context_started, params.timeout_ms) {
        return daemon_context_deadline_response(
            request,
            "ee.daemon.context deadline expired while attaching pack DNA.",
        );
    }

    let render_options = ContextJsonRenderOptions::from(options.output_options);
    let rendered = render_context_response_json_with_options(&context_response, render_options);
    if daemon_context_deadline_expired(context_started, params.timeout_ms) {
        return daemon_context_deadline_response(
            request,
            "ee.daemon.context deadline expired while rendering the response.",
        );
    }
    let result = match serde_json::from_str::<serde_json::Value>(&rendered) {
        Ok(result) => result,
        Err(error) => {
            return DaemonResponse::err(
                request.request_id.clone(),
                request.agent_id.clone(),
                request.workspace_id.clone(),
                DAEMON_CONTEXT_EXECUTION_FAILED_CODE,
                format!("ee.daemon.context rendered invalid canonical JSON: {error}"),
            );
        }
    };
    let mut pending_delivery =
        PendingSearchAdvisoryDelivery::new(search_advisory_session, &advisory_workspace_id);
    let mut result = result;
    {
        let mut advisory_session = search_advisory_session
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        attach_daemon_context_search_advisories_for_delivery(
            &mut result,
            context_search_report.as_ref(),
            &context_search_advisory_snapshot,
            &mut advisory_session,
            &advisory_workspace_id,
            pending_delivery.reservation_mut(),
        );
    }
    if daemon_context_deadline_expired(context_started, params.timeout_ms) {
        return daemon_context_deadline_response(
            request,
            "ee.daemon.context deadline expired while finalizing degradations.",
        );
    }
    let degraded_codes = result
        .pointer("/data/degraded")
        .and_then(serde_json::Value::as_array)
        .into_iter()
        .flatten()
        .filter_map(|entry| entry.get("code").and_then(serde_json::Value::as_str))
        .map(str::to_owned)
        .collect::<Vec<_>>();
    let mut response = DaemonResponse::ok(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        result,
    );
    for code in degraded_codes {
        response = response.with_degraded(code);
    }
    if !daemon_response_fits(&response, super::DAEMON_RESPONSE_MAX_BYTES) {
        return DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            request.workspace_id.clone(),
            DAEMON_CONTEXT_EXECUTION_FAILED_CODE,
            format!(
                "ee.daemon.context response exceeded the {}-byte daemon response cap; lower maxTokens or use the in-process CLI pack path.",
                super::DAEMON_RESPONSE_MAX_BYTES
            ),
        );
    }
    pending_delivery.finish(response, defer_advisory_until_socket_write)
}

fn attach_daemon_context_search_advisories_for_delivery(
    response: &mut serde_json::Value,
    search_report: Option<&SearchReport>,
    search_advisory_snapshot: &ContextSearchAdvisorySnapshot,
    session: &mut SearchAdvisorySession,
    workspace_id: &str,
    reservation: &mut SearchAdvisoryDeliveryReservation,
) {
    if let Some(search_report) = search_report {
        attach_context_search_advisories_for_delivery(
            response,
            search_report,
            session,
            workspace_id,
            reservation,
        );
    } else {
        attach_context_cached_search_advisories_for_delivery(
            response,
            search_advisory_snapshot,
            session,
            workspace_id,
            reservation,
        );
    }
}

#[cfg(test)]
fn filter_context_large_gap_advisory_for_delivery(
    response: &mut serde_json::Value,
    session: &mut SearchAdvisorySession,
    workspace_id: &str,
    reservation: &mut SearchAdvisoryDeliveryReservation,
) {
    filter_context_large_gap_advisory_inner(response, session, workspace_id, Some(reservation));
}

#[cfg(test)]
fn filter_context_large_gap_advisory_inner(
    response: &mut serde_json::Value,
    session: &mut SearchAdvisorySession,
    workspace_id: &str,
    reservation: Option<&mut SearchAdvisoryDeliveryReservation>,
) {
    const CODE: &str = "search_index_large_gap";
    let has_large_gap = ["/degraded", "/data/degraded"].into_iter().any(|pointer| {
        response
            .pointer(pointer)
            .and_then(serde_json::Value::as_array)
            .is_some_and(|entries| {
                entries.iter().any(|entry| {
                    entry.get("code").and_then(serde_json::Value::as_str) == Some(CODE)
                })
            })
    });
    let emit_large_gap = match reservation {
        Some(reservation) => session.emit_large_gap_while_active_for_delivery(
            workspace_id,
            has_large_gap,
            reservation,
        ),
        None => session.emit_large_gap_while_active(workspace_id, has_large_gap),
    }
    .should_emit();
    if emit_large_gap {
        return;
    }
    if !has_large_gap {
        return;
    }
    for pointer in ["/degraded", "/data/degraded"] {
        if let Some(entries) = response
            .pointer_mut(pointer)
            .and_then(serde_json::Value::as_array_mut)
        {
            entries.retain(|entry| {
                entry.get("code").and_then(serde_json::Value::as_str) != Some(CODE)
            });
        }
    }
}

fn daemon_context_deadline_expired(started: Instant, timeout_ms: Option<u64>) -> bool {
    timeout_ms.is_some_and(|timeout_ms| started.elapsed() >= Duration::from_millis(timeout_ms))
}

fn daemon_context_deadline_response(
    request: &DaemonRequest,
    message: &'static str,
) -> DaemonResponse {
    DaemonResponse::err(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        DAEMON_CONTEXT_DEADLINE_EXCEEDED_CODE,
        message,
    )
}

fn required_string_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<String, String> {
    let Some(value) = optional_string_any(object, keys)? else {
        return Err(format!("missing required field `{}`", keys[0]));
    };
    if value.trim().is_empty() {
        return Err(format!("field `{}` must not be blank", keys[0]));
    }
    Ok(value)
}

fn optional_string_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<Option<String>, String> {
    for key in keys {
        if let Some(value) = object.get(*key) {
            return value
                .as_str()
                .map(|text| Some(text.to_owned()))
                .ok_or_else(|| format!("field `{key}` must be a string"));
        }
    }
    Ok(None)
}

fn required_path_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<PathBuf, String> {
    let value = required_string_any(object, keys)?;
    Ok(PathBuf::from(value))
}

fn optional_path_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<Option<PathBuf>, String> {
    optional_string_any(object, keys).map(|value| value.map(PathBuf::from))
}

fn optional_bool_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<Option<bool>, String> {
    for key in keys {
        if let Some(value) = object.get(*key) {
            return value
                .as_bool()
                .map(Some)
                .ok_or_else(|| format!("field `{key}` must be a boolean"));
        }
    }
    Ok(None)
}

fn optional_u32_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<Option<u32>, String> {
    optional_u64_any(object, keys)?
        .map(|value| {
            u32::try_from(value).map_err(|_| format!("field `{}` must fit in u32", keys[0]))
        })
        .transpose()
}

fn optional_u64_any(
    object: &serde_json::Map<String, serde_json::Value>,
    keys: &[&str],
) -> Result<Option<u64>, String> {
    for key in keys {
        if let Some(value) = object.get(*key) {
            return value
                .as_u64()
                .map(Some)
                .ok_or_else(|| format!("field `{key}` must be an unsigned integer"));
        }
    }
    Ok(None)
}

fn parse_daemon_context_profile(value: &str) -> Result<ContextPackProfile, String> {
    match value.trim().to_ascii_lowercase().as_str() {
        "compact" => Ok(ContextPackProfile::Compact),
        "balanced" => Ok(ContextPackProfile::Balanced),
        "grounding" => Ok(ContextPackProfile::Grounding),
        "orientation" => Ok(ContextPackProfile::Orientation),
        "thorough" => Ok(ContextPackProfile::Thorough),
        "submodular" => Ok(ContextPackProfile::Submodular),
        _ => Err(format!(
            "Invalid context profile `{value}`. Expected compact, balanced, grounding, orientation, thorough, or submodular."
        )),
    }
}

fn parse_daemon_speed_mode(value: &str) -> Result<SpeedMode, String> {
    match value {
        "instant" => Ok(SpeedMode::Instant),
        "default" => Ok(SpeedMode::Default),
        "quality" => Ok(SpeedMode::Quality),
        _ => Err(format!(
            "Invalid speed mode `{value}`. Expected instant, default, or quality."
        )),
    }
}

fn parse_daemon_source_mode(value: &str) -> Result<SearchSourceMode, String> {
    match value {
        "lexical_only" => Ok(SearchSourceMode::LexicalOnly),
        "semantic_only" => Ok(SearchSourceMode::SemanticOnly),
        "hybrid" => Ok(SearchSourceMode::Hybrid),
        _ => Err(format!(
            "Invalid source mode `{value}`. Expected lexical_only, semantic_only, or hybrid."
        )),
    }
}

fn parse_daemon_pack_output_profile(
    value: &str,
) -> Result<crate::core::context::ContextPackOutputProfile, String> {
    match value.trim().to_ascii_lowercase().as_str() {
        "lean" => Ok(crate::core::context::ContextPackOutputProfile::Lean),
        "standard" => Ok(crate::core::context::ContextPackOutputProfile::Standard),
        "verbose" => Ok(crate::core::context::ContextPackOutputProfile::Verbose),
        _ => Err(format!(
            "Invalid pack output profile `{value}`. Expected lean, standard, or verbose."
        )),
    }
}

fn daemon_method_authority(method: &str) -> Option<DaemonAuthority> {
    match method {
        METHOD_CAPABILITIES | METHOD_ECHO | METHOD_SHUTDOWN | METHOD_TELEMETRY => {
            Some(DaemonAuthority::SameUid)
        }
        METHOD_CONTEXT | METHOD_SEARCH | METHOD_WRITE | METHOD_WRITE_JOURNAL
        | METHOD_ORIENT_HOOK | METHOD_RECALL => Some(DaemonAuthority::SameUidWorkspace),
        _ => None,
    }
}

fn authorize_daemon_method(
    request: &DaemonRequest,
    authority: DaemonAuthority,
    bound_workspace_id: Option<&str>,
) -> Result<(), DaemonResponse> {
    match authority {
        DaemonAuthority::SameUid => Ok(()),
        DaemonAuthority::SameUidWorkspace => {
            let workspace_id = request
                .workspace_id
                .as_deref()
                .map(str::trim)
                .filter(|workspace_id| !workspace_id.is_empty());
            let Some(workspace_id) = workspace_id else {
                return Err(method_unauthorized_response(
                    request,
                    "registered daemon method requires a non-empty workspace_id",
                ));
            };
            if let Some(bound_workspace_id) = bound_workspace_id
                && !workspace_ids_match(workspace_id, bound_workspace_id)
            {
                return Err(method_unauthorized_response(
                    request,
                    "registered daemon method is not authorized for this daemon workspace",
                ));
            }
            Ok(())
        }
    }
}

fn workspace_ids_match(requested: &str, bound: &str) -> bool {
    if requested == bound {
        return true;
    }
    let requested = Path::new(requested);
    let bound = Path::new(bound);
    fs::canonicalize(requested)
        .ok()
        .zip(fs::canonicalize(bound).ok())
        .is_some_and(|(requested, bound)| requested == bound)
}

fn method_unauthorized_response(request: &DaemonRequest, message: &'static str) -> DaemonResponse {
    DaemonResponse::err(
        request.request_id.clone(),
        request.agent_id.clone(),
        request.workspace_id.clone(),
        DAEMON_METHOD_UNAUTHORIZED_CODE,
        message,
    )
    .with_degraded(DAEMON_METHOD_UNAUTHORIZED_CODE)
}

#[allow(clippy::expect_used)]
fn daemon_capabilities_result() -> serde_json::Value {
    serde_json::json!({
        "protocol": "ee.daemon",
        "request_schemas": [super::DAEMON_REQUEST_SCHEMA_V1],
        "response_schemas": [super::DAEMON_RESPONSE_SCHEMA_V1],
        "methods": [
            METHOD_CAPABILITIES,
            METHOD_CONTEXT,
            METHOD_ECHO,
            METHOD_SEARCH,
            METHOD_ORIENT_HOOK,
            METHOD_RECALL,
            METHOD_SHUTDOWN,
            METHOD_TELEMETRY,
            METHOD_WRITE,
            METHOD_WRITE_JOURNAL
        ],
        "authorization": {
            "ee.daemon.capabilities": daemon_method_authority(METHOD_CAPABILITIES).expect("registered method").as_wire_label(),
            "ee.daemon.context": daemon_method_authority(METHOD_CONTEXT).expect("registered method").as_wire_label(),
            "ee.daemon.echo": daemon_method_authority(METHOD_ECHO).expect("registered method").as_wire_label(),
            "ee.daemon.search": daemon_method_authority(METHOD_SEARCH).expect("registered method").as_wire_label(),
            "ee.daemon.orient_hook": daemon_method_authority(METHOD_ORIENT_HOOK).expect("registered method").as_wire_label(),
            "ee.daemon.recall": daemon_method_authority(METHOD_RECALL).expect("registered method").as_wire_label(),
            "ee.daemon.shutdown": daemon_method_authority(METHOD_SHUTDOWN).expect("registered method").as_wire_label(),
            "ee.daemon.telemetry": daemon_method_authority(METHOD_TELEMETRY).expect("registered method").as_wire_label(),
            "ee.daemon.write": daemon_method_authority(METHOD_WRITE).expect("registered method").as_wire_label(),
            "ee.daemon.write_journal": daemon_method_authority(METHOD_WRITE_JOURNAL).expect("registered method").as_wire_label()
        },
        "method_schemas": {
            "ee.daemon.orient_hook": {
                "request": DAEMON_ORIENT_HOOK_REQUEST_SCHEMA_V1,
                "response": DAEMON_MEMORY_READ_RESPONSE_SCHEMA_V1
            },
            "ee.daemon.recall": {
                "request": DAEMON_RECALL_REQUEST_SCHEMA_V1,
                "response": DAEMON_MEMORY_READ_RESPONSE_SCHEMA_V1
            },
            "ee.daemon.search": {
                "request": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
                "response": DAEMON_SEARCH_RESPONSE_SCHEMA_V3
            }
        },
        // GH #37: lets a client tell "this daemon is still loading its model"
        // apart from "this daemon cannot serve search", so a bounded fallback
        // can say something actionable instead of a bare deadline message.
        "warm": {
            "posture": daemon_search_warm_posture()
        },
        "forward_compat": {
            "v1_unknown_fields": "rejected",
            "v1_unknown_methods": DAEMON_UNKNOWN_METHOD_CODE,
            "v2_migration": "Call ee.daemon.capabilities with ee.daemon.request.v1 before sending any non-v1 schema or method; downgrade to an advertised schema/method when absent."
        }
    })
}

/// Open a UDS client connection to a running daemon and send exactly
/// one request, returning the parsed response. The CLI client uses
/// this for `ee daemon status` round-trip checks and for any future
/// hot-path proxy calls.
pub fn client_round_trip(
    socket_path: &Path,
    request: &DaemonRequest,
) -> Result<DaemonResponse, ClientError> {
    client_round_trip_before(
        socket_path,
        request,
        Instant::now() + DAEMON_DEFAULT_RPC_TIMEOUT,
    )
}

/// Apply a caller-owned cumulative deadline to one daemon round-trip's framed
/// I/O. The deadline may be shared across capability negotiation and the
/// method call so per-request socket timeouts cannot accidentally multiply it.
/// The CLI additionally bounds the whole worker attempt, including connect.
pub fn client_round_trip_before(
    socket_path: &Path,
    request: &DaemonRequest,
    deadline: Instant,
) -> Result<DaemonResponse, ClientError> {
    ensure_client_deadline(deadline)?;
    let mut stream = UnixStream::connect(socket_path).map_err(ClientError::Connect)?;
    set_client_deadline(&stream, deadline)?;

    let body = serde_json::to_vec(request).map_err(ClientError::Encode)?;
    use std::io::Write;
    let length = u32::try_from(body.len())
        .map_err(|_| ClientError::RequestTooLarge { actual: body.len() })?;
    set_client_deadline(&stream, deadline)?;
    stream
        .write_all(&length.to_be_bytes())
        .map_err(|error| client_io_error(error, deadline))?;
    set_client_deadline(&stream, deadline)?;
    stream
        .write_all(&body)
        .map_err(|error| client_io_error(error, deadline))?;
    set_client_deadline(&stream, deadline)?;
    stream
        .flush()
        .map_err(|error| client_io_error(error, deadline))?;

    // Read the response with the same frame shape.
    let mut response_prefix = [0_u8; 4];
    use std::io::Read;
    set_client_deadline(&stream, deadline)?;
    stream
        .read_exact(&mut response_prefix)
        .map_err(|error| client_io_error(error, deadline))?;
    let announced = u32::from_be_bytes(response_prefix);
    let announced_usize =
        usize::try_from(announced).map_err(|_| ClientError::ResponseTooLarge { announced })?;
    if announced_usize > super::DAEMON_RESPONSE_MAX_BYTES {
        return Err(ClientError::ResponseTooLarge { announced });
    }
    let mut buffer = vec![0_u8; announced_usize];
    set_client_deadline(&stream, deadline)?;
    stream
        .read_exact(&mut buffer)
        .map_err(|error| client_io_error(error, deadline))?;
    let response: DaemonResponse = serde_json::from_slice(&buffer).map_err(ClientError::Decode)?;
    if response.schema != super::DAEMON_RESPONSE_SCHEMA_V1 {
        return Err(ClientError::ResponseSchemaMismatch {
            expected: super::DAEMON_RESPONSE_SCHEMA_V1,
            actual: response.schema,
        });
    }
    if response.request_id != request.request_id {
        return Err(ClientError::ResponseRequestIdMismatch {
            expected: request.request_id.clone(),
            actual: response.request_id,
        });
    }
    if response.agent_id != request.agent_id {
        return Err(ClientError::ResponseAgentIdMismatch {
            expected: request.agent_id.clone(),
            actual: response.agent_id,
        });
    }
    if response.workspace_id != request.workspace_id {
        return Err(ClientError::ResponseWorkspaceIdMismatch {
            expected: request.workspace_id.clone(),
            actual: response.workspace_id,
        });
    }
    Ok(response)
}

fn ensure_client_deadline(deadline: Instant) -> Result<Duration, ClientError> {
    deadline
        .checked_duration_since(Instant::now())
        .filter(|remaining| !remaining.is_zero())
        .ok_or(ClientError::DeadlineExceeded)
}

fn set_client_deadline(stream: &UnixStream, deadline: Instant) -> Result<(), ClientError> {
    let remaining = ensure_client_deadline(deadline)?;
    let result = stream
        .set_read_timeout(Some(remaining))
        .and_then(|()| stream.set_write_timeout(Some(remaining)));
    // Darwin rejects SO_RCVTIMEO/SO_SNDTIMEO with EINVAL after the peer
    // closes, even while its complete reply is buffered. Drain that reply
    // without blocking instead of discarding it as a timeout-setting error.
    // Nonblocking mode also preserves bounded I/O if EINVAL has another cause.
    #[cfg(target_vendor = "apple")]
    if result
        .as_ref()
        .is_err_and(|error| error.raw_os_error() == Some(libc::EINVAL))
    {
        return stream.set_nonblocking(true).map_err(ClientError::Io);
    }
    result.map_err(ClientError::Io)
}

fn client_io_error(error: io::Error, deadline: Instant) -> ClientError {
    if matches!(
        error.kind(),
        io::ErrorKind::TimedOut | io::ErrorKind::WouldBlock
    ) || Instant::now() >= deadline
    {
        ClientError::DeadlineExceeded
    } else {
        ClientError::Io(error)
    }
}

/// Errors that can occur on the client side of a UDS round-trip.
#[derive(Debug)]
pub enum ClientError {
    DeadlineExceeded,
    Connect(io::Error),
    Io(io::Error),
    Encode(serde_json::Error),
    Decode(serde_json::Error),
    RequestTooLarge {
        actual: usize,
    },
    ResponseTooLarge {
        announced: u32,
    },
    ResponseSchemaMismatch {
        expected: &'static str,
        actual: String,
    },
    ResponseRequestIdMismatch {
        expected: String,
        actual: String,
    },
    ResponseAgentIdMismatch {
        expected: String,
        actual: String,
    },
    ResponseWorkspaceIdMismatch {
        expected: Option<String>,
        actual: Option<String>,
    },
}

impl std::fmt::Display for ClientError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::DeadlineExceeded => write!(formatter, "daemon round-trip deadline exceeded"),
            Self::Connect(source) => {
                write!(formatter, "failed to connect to daemon socket: {source}")
            }
            Self::Io(source) => write!(formatter, "io error during daemon round-trip: {source}"),
            Self::Encode(source) => write!(formatter, "failed to encode daemon request: {source}"),
            Self::Decode(source) => write!(formatter, "failed to decode daemon response: {source}"),
            Self::RequestTooLarge { actual } => write!(
                formatter,
                "daemon request is {actual} bytes which exceeds the {}-byte cap",
                super::DAEMON_REQUEST_MAX_BYTES
            ),
            Self::ResponseTooLarge { announced } => write!(
                formatter,
                "daemon response announced {announced} bytes which exceeds the {}-byte cap",
                super::DAEMON_RESPONSE_MAX_BYTES
            ),
            Self::ResponseSchemaMismatch { expected, actual } => write!(
                formatter,
                "daemon response schema mismatch: expected {expected}, got {actual}"
            ),
            Self::ResponseRequestIdMismatch { expected, actual } => write!(
                formatter,
                "daemon response request_id mismatch: sent {expected}, got {actual}"
            ),
            Self::ResponseAgentIdMismatch { expected, actual } => write!(
                formatter,
                "daemon response agent_id mismatch: sent {expected}, got {actual}"
            ),
            Self::ResponseWorkspaceIdMismatch { expected, actual } => write!(
                formatter,
                "daemon response workspace_id mismatch: sent {}, got {}",
                expected.as_deref().unwrap_or("<absent>"),
                actual.as_deref().unwrap_or("<absent>")
            ),
        }
    }
}

impl std::error::Error for ClientError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::DeadlineExceeded => None,
            Self::Connect(source) | Self::Io(source) => Some(source),
            Self::Encode(source) | Self::Decode(source) => Some(source),
            Self::RequestTooLarge { .. }
            | Self::ResponseTooLarge { .. }
            | Self::ResponseSchemaMismatch { .. }
            | Self::ResponseRequestIdMismatch { .. }
            | Self::ResponseAgentIdMismatch { .. }
            | Self::ResponseWorkspaceIdMismatch { .. } => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::search::MAX_SEARCH_ADVISORY_WORKSPACES;
    use crate::daemon::protocol::DaemonResponseDelivery;

    const TEST_AGENT_ID: &str = "agent-daemon-server-test";
    const TEST_WORKSPACE_ID: &str = "workspace-daemon-server-test";

    fn context_request(
        request_id: &'static str,
        agent_id: &'static str,
        params: serde_json::Value,
    ) -> DaemonRequest {
        let mut request = DaemonRequest::new(request_id, agent_id, METHOD_CONTEXT, params);
        request.workspace_id = Some(TEST_WORKSPACE_ID.to_owned());
        request
    }

    fn search_request(params: serde_json::Value) -> DaemonRequest {
        let mut request = DaemonRequest::new(
            "req-search-contract-001",
            TEST_AGENT_ID,
            METHOD_SEARCH,
            params,
        );
        request.workspace_id = Some("/tmp/ee-daemon-search-contract".to_owned());
        request
    }

    #[test]
    fn daemon_response_cap_rejects_undeliverable_candidate() {
        let response = DaemonResponse::ok(
            "req-cap-001",
            TEST_AGENT_ID,
            Some(TEST_WORKSPACE_ID.to_owned()),
            serde_json::json!({"payload": "too large for a one-byte cap"}),
        );

        assert!(!daemon_response_fits(&response, 1));
        assert!(daemon_response_fits(&response, usize::MAX));
    }

    fn permanent_reranker_advisory_report() -> SearchReport {
        SearchReport {
            index_freshness: None,
            status: crate::core::search::SearchStatus::NoResults,
            embed_backend: crate::models::EmbedBackend::HashFallback,
            query: "release".to_owned(),
            requested_limit: 10,
            results: Vec::new(),
            elapsed_ms: 1.0,
            errors: Vec::new(),
            degraded: vec![crate::core::search::SearchDegradation {
                code: "rerank_model_unavailable".to_owned(),
                severity: "warning".to_owned(),
                message: crate::core::search::RERANK_MODEL_UNAVAILABLE_ADVISORY.to_owned(),
                repair: None,
            }],
            runtime_profile: crate::core::profile::RuntimeProfileReport::for_profile(
                crate::core::profile::OperatingProfile::Workstation,
                "daemon_advisory_delivery_test",
            ),
            rerank_configured_mode: crate::config::SearchRerankMode::Auto,
            rerank_configured_top_k: 50,
            rerank_runtime_available: false,
            relevance_floor_applied: Some(0.0),
            candidates_below_floor: 0,
            query_assist: None,
            source_mode_requested: SearchSourceMode::Hybrid,
            source_mode_applied: SearchSourceMode::Hybrid,
            source_mode_fallback: false,
            strict_source_mode: false,
            memory_scope: MemoryScope::Swarm,
            strict_scope: false,
            scope_stats: crate::models::MemoryScopeStats::new(MemoryScope::Swarm, false, None, 0),
        }
    }

    fn stale_index_advisory_report(db_generation: u64, index_generation: u64) -> SearchReport {
        let mut report = permanent_reranker_advisory_report();
        report.index_freshness = Some(crate::core::search::SearchIndexFreshness {
            stale: true,
            db_generation: Some(db_generation),
            index_generation: Some(index_generation),
            generation_gap: Some(db_generation.saturating_sub(index_generation)),
            large_gap: db_generation.saturating_sub(index_generation)
                > crate::core::search::SEARCH_INDEX_LARGE_GAP_THRESHOLD,
        });
        report.degraded = vec![
            crate::core::search::SearchDegradation {
                code: "search_index_stale".to_owned(),
                severity: "medium".to_owned(),
                message: format!(
                    "Search index is stale. Database generation is {db_generation}; index generation is {index_generation}."
                ),
                repair: Some("ee index rebuild --workspace .".to_owned()),
            },
            crate::core::search::SearchDegradation {
                code: "search_index_large_gap".to_owned(),
                severity: "medium".to_owned(),
                message: format!(
                    "Search index generation gap is {}; automatic read repair was skipped.",
                    db_generation.saturating_sub(index_generation)
                ),
                repair: Some("ee index rebuild --workspace .".to_owned()),
            },
        ];
        report
    }

    fn advisory_delivery_candidate(
        report: &SearchReport,
        policy: &DaemonDispatchPolicy,
        workspace_id: &str,
    ) -> (DaemonResponse, serde_json::Value) {
        let mut pending =
            PendingSearchAdvisoryDelivery::new(policy.search_advisory_session(), workspace_id);
        let result = {
            let mut session = policy
                .search_advisory_session()
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            serde_json::to_value(DaemonSearchResult::from_report_for_delivery(
                report,
                false,
                workspace_id,
                &mut session,
                pending.reservation_mut(),
                DaemonSearchTiming::from_trace(
                    Duration::from_millis(1),
                    &SearchPerformanceTrace::default(),
                ),
                None,
            ))
            .expect("encode method result")
        };
        let response = DaemonResponse::ok(
            "req-advisory-delivery",
            TEST_AGENT_ID,
            Some(workspace_id.to_owned()),
            result.clone(),
        );
        (pending.finish(response, true), result)
    }

    fn context_advisory_delivery_candidate(
        report: &SearchReport,
        policy: &DaemonDispatchPolicy,
        workspace_id: &str,
    ) -> (DaemonResponse, serde_json::Value) {
        let mut pending =
            PendingSearchAdvisoryDelivery::new(policy.search_advisory_session(), workspace_id);
        let mut result = serde_json::json!({
            "schema": crate::models::RESPONSE_SCHEMA_V2,
            "success": true,
            "data": {"command": "pack", "degraded": []},
            "degraded": [],
        });
        {
            let mut session = policy
                .search_advisory_session()
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            attach_context_search_advisories_for_delivery(
                &mut result,
                report,
                &mut session,
                workspace_id,
                pending.reservation_mut(),
            );
        }
        let response = DaemonResponse::ok(
            "req-context-advisory-delivery",
            TEST_AGENT_ID,
            Some(workspace_id.to_owned()),
            result.clone(),
        );
        (pending.finish(response, true), result)
    }

    fn cached_context_advisory_delivery_candidate(
        snapshot: &ContextSearchAdvisorySnapshot,
        policy: &DaemonDispatchPolicy,
        workspace_id: &str,
    ) -> (DaemonResponse, serde_json::Value) {
        let mut pending =
            PendingSearchAdvisoryDelivery::new(policy.search_advisory_session(), workspace_id);
        let mut result = serde_json::json!({
            "schema": crate::models::RESPONSE_SCHEMA_V2,
            "success": true,
            "data": {"command": "pack", "degraded": []},
            "degraded": [],
        });
        {
            let mut session = policy
                .search_advisory_session()
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            attach_daemon_context_search_advisories_for_delivery(
                &mut result,
                None,
                snapshot,
                &mut session,
                workspace_id,
                pending.reservation_mut(),
            );
        }
        let response = DaemonResponse::ok(
            "req-cached-context-advisory-delivery",
            TEST_AGENT_ID,
            Some(workspace_id.to_owned()),
            result.clone(),
        );
        (pending.finish(response, true), result)
    }

    #[test]
    fn successful_socket_delivery_consumes_permanent_advisory() {
        let report = permanent_reranker_advisory_report();
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let (mut delivered_response, delivered_result) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert_eq!(
            delivered_result
                .pointer("/response/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
        let (mut server_side, mut client_side) = UnixStream::pair().expect("socketpair");
        let reader = thread::spawn(move || read_framed_daemon_response(&mut client_side));
        assert!(write_and_settle_daemon_response(
            &mut server_side,
            &policy,
            &mut delivered_response,
        ));
        assert!(delivered_response.delivery.is_none());
        let wire_response = reader.join().expect("socket reader must not panic");
        assert_eq!(wire_response.result, Some(delivered_result));

        let (mut repeated_response, repeated_result) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(repeated_response.delivery.is_some());
        assert!(
            repeated_result
                .pointer("/response/data/rerank/advisory")
                .is_some_and(serde_json::Value::is_null)
        );
        assert_eq!(
            repeated_result
                .pointer("/response/data/rerank/advisorySummary/sessionSuppressedCount")
                .and_then(serde_json::Value::as_u64),
            Some(1)
        );
        settle_daemon_response_delivery(&policy, &mut repeated_response, true);
    }

    #[test]
    fn disconnected_socket_does_not_consume_permanent_advisory() {
        let report = permanent_reranker_advisory_report();
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let (mut failed_response, failed_result) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(failed_response.error.is_none());
        assert_eq!(
            failed_result
                .pointer("/response/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
        let (mut server_side, client_side) = UnixStream::pair().expect("socketpair");
        drop(client_side);
        assert!(!write_and_settle_daemon_response(
            &mut server_side,
            &policy,
            &mut failed_response,
        ));
        assert!(failed_response.delivery.is_none());

        let (retry_response, retry_result) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(retry_response.delivery.is_some());
        assert_eq!(
            retry_result
                .pointer("/response/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
    }

    #[test]
    fn disconnected_context_socket_does_not_consume_permanent_advisory() {
        let report = permanent_reranker_advisory_report();
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let (mut failed_response, failed_result) =
            context_advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert_eq!(
            failed_result
                .pointer("/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
        let (mut server_side, client_side) = UnixStream::pair().expect("socketpair");
        drop(client_side);
        assert!(!write_and_settle_daemon_response(
            &mut server_side,
            &policy,
            &mut failed_response,
        ));

        let (retry_response, retry_result) =
            context_advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(retry_response.delivery.is_some());
        assert_eq!(
            retry_result
                .pointer("/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
    }

    #[test]
    fn cached_context_snapshot_uses_socket_settlement_and_shared_once_ledger() {
        let report = permanent_reranker_advisory_report();
        let snapshot = ContextSearchAdvisorySnapshot::from_search_report(&report);
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let (mut failed_response, failed_result) =
            cached_context_advisory_delivery_candidate(&snapshot, &policy, TEST_WORKSPACE_ID);
        assert_eq!(
            failed_result
                .pointer("/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
        let (mut failed_server, failed_client) = UnixStream::pair().expect("socketpair");
        drop(failed_client);
        assert!(!write_and_settle_daemon_response(
            &mut failed_server,
            &policy,
            &mut failed_response,
        ));

        let (mut retry_response, retry_result) =
            cached_context_advisory_delivery_candidate(&snapshot, &policy, TEST_WORKSPACE_ID);
        assert_eq!(
            retry_result
                .pointer("/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable"),
            "failed cached delivery must preserve the permanent advisory"
        );
        let (mut retry_server, mut retry_client) = UnixStream::pair().expect("socketpair");
        let retry_reader = thread::spawn(move || read_framed_daemon_response(&mut retry_client));
        assert!(write_and_settle_daemon_response(
            &mut retry_server,
            &policy,
            &mut retry_response,
        ));
        assert_eq!(
            retry_reader.join().expect("cached retry reader").result,
            Some(retry_result)
        );

        let (mut fresh_response, fresh_result) =
            context_advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(
            fresh_result
                .pointer("/data/rerank/advisory")
                .is_some_and(serde_json::Value::is_null),
            "fresh and cached context paths must share one process ledger"
        );
        settle_daemon_response_delivery(&policy, &mut fresh_response, true);
    }

    #[test]
    fn partial_socket_delivery_does_not_consume_permanent_advisory() {
        use std::io::Read;
        use std::net::Shutdown;

        let report = permanent_reranker_advisory_report();
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let (mut partial_response, _) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(partial_response.error.is_none());
        partial_response.result = Some(serde_json::json!({
            "payload": "x".repeat(3 * 1024 * 1024),
        }));
        let (mut server_side, mut client_side) = UnixStream::pair().expect("socketpair");
        server_side
            .set_write_timeout(Some(Duration::from_secs(2)))
            .expect("server write timeout");
        let reader = thread::spawn(move || {
            let mut prefix = [0_u8; 4];
            client_side
                .read_exact(&mut prefix)
                .expect("length prefix must arrive");
            let announced = u32::from_be_bytes(prefix) as usize;
            let mut partial = [0_u8; 16 * 1024];
            client_side
                .read_exact(&mut partial)
                .expect("partial body must arrive");
            client_side
                .shutdown(Shutdown::Both)
                .expect("disconnect partial reader");
            (announced, partial.len())
        });

        assert!(!write_and_settle_daemon_response(
            &mut server_side,
            &policy,
            &mut partial_response,
        ));
        let (announced, observed) = reader.join().expect("partial reader must not panic");
        assert!(announced > observed, "fixture must disconnect mid-frame");
        assert!(partial_response.delivery.is_none());

        let (retry_response, retry_result) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert!(retry_response.delivery.is_some());
        assert_eq!(
            retry_result
                .pointer("/response/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable")
        );
    }

    #[test]
    fn concurrent_socket_capacity_fail_open_settles_all_first_advisories() {
        const WORKSPACES: usize = 65;

        let report = stale_index_advisory_report(200, 1);
        let policy = DaemonDispatchPolicy::default();
        let barrier = Arc::new(std::sync::Barrier::new(WORKSPACES + 1));
        let workers = (0..WORKSPACES)
            .map(|index| {
                let report = report.clone();
                let policy = policy.clone();
                let barrier = Arc::clone(&barrier);
                thread::spawn(move || {
                    barrier.wait();
                    let workspace_id = format!("capacity-workspace-{index:03}");
                    let (response, result) =
                        advisory_delivery_candidate(&report, &policy, &workspace_id);
                    (workspace_id, response, result)
                })
            })
            .collect::<Vec<_>>();
        barrier.wait();
        let mut outcomes = workers
            .into_iter()
            .map(|worker| worker.join().expect("capacity reservation worker"))
            .collect::<Vec<_>>();

        for (_, response, result) in &outcomes {
            assert!(response.delivery.is_some());
            let codes = result["response"]["data"]["degraded"]
                .as_array()
                .expect("first response degraded array")
                .iter()
                .filter_map(|entry| entry["code"].as_str())
                .collect::<std::collections::BTreeSet<_>>();
            assert!(codes.contains("search_index_stale"));
            assert!(codes.contains("search_index_large_gap"));
            assert_eq!(
                result["response"]["data"]["indexFreshness"]["largeGap"],
                true
            );
        }

        let capacity_busy_indices = outcomes
            .iter()
            .enumerate()
            .filter_map(|(index, (_, response, _))| {
                response
                    .delivery
                    .as_ref()
                    .is_some_and(DaemonResponseDelivery::search_large_gap_capacity_busy)
                    .then_some(index)
            })
            .collect::<Vec<_>>();
        assert_eq!(capacity_busy_indices.len(), 1);
        let capacity_busy_index = capacity_busy_indices[0];
        let capacity_busy_workspace = outcomes[capacity_busy_index].0.clone();

        let release_index = outcomes
            .iter()
            .enumerate()
            .find(|(index, _)| *index != capacity_busy_index)
            .map(|(index, _)| index)
            .expect("one ordinary reservation to settle first");
        let (_, mut release_response, release_result) = outcomes.swap_remove(release_index);
        let (mut release_server, mut release_client) = UnixStream::pair().expect("socketpair");
        let release_reader =
            thread::spawn(move || read_framed_daemon_response(&mut release_client));
        assert!(write_and_settle_daemon_response(
            &mut release_server,
            &policy,
            &mut release_response,
        ));
        assert_eq!(
            release_reader.join().expect("release socket reader").result,
            Some(release_result)
        );

        let failed_workspace = outcomes
            .iter()
            .find(|(workspace_id, response, _)| {
                workspace_id != &capacity_busy_workspace
                    && response
                        .delivery
                        .as_ref()
                        .is_some_and(|delivery| !delivery.search_large_gap_capacity_busy())
            })
            .map(|(workspace_id, _, _)| workspace_id.clone())
            .expect("one ordinary reservation must exercise failed delivery");
        let delivery_workers = outcomes
            .into_iter()
            .map(|(workspace_id, mut response, expected_result)| {
                let policy = policy.clone();
                let failed_workspace = failed_workspace.clone();
                thread::spawn(move || {
                    let (mut server_side, mut client_side) =
                        UnixStream::pair().expect("socketpair");
                    if workspace_id == failed_workspace {
                        drop(client_side);
                        let delivered = write_and_settle_daemon_response(
                            &mut server_side,
                            &policy,
                            &mut response,
                        );
                        return (workspace_id, delivered);
                    }
                    let reader =
                        thread::spawn(move || read_framed_daemon_response(&mut client_side));
                    let delivered =
                        write_and_settle_daemon_response(&mut server_side, &policy, &mut response);
                    let wire = reader.join().expect("capacity socket reader");
                    assert_eq!(wire.result, Some(expected_result));
                    (workspace_id, delivered)
                })
            })
            .collect::<Vec<_>>();
        let deliveries = delivery_workers
            .into_iter()
            .map(|worker| worker.join().expect("capacity delivery worker"))
            .collect::<Vec<_>>();
        assert!(deliveries.iter().all(|(workspace_id, delivered)| {
            *delivered == (workspace_id != &failed_workspace)
        }));

        let (mut retry_response, retry_result) =
            advisory_delivery_candidate(&report, &policy, &failed_workspace);
        assert!(retry_response.delivery.is_some());
        assert!(
            retry_result["response"]["data"]["degraded"]
                .as_array()
                .expect("failed-delivery retry degraded array")
                .iter()
                .any(|entry| entry["code"] == "search_index_large_gap")
        );
        let (mut retry_server, mut retry_client) = UnixStream::pair().expect("socketpair");
        let retry_reader = thread::spawn(move || read_framed_daemon_response(&mut retry_client));
        assert!(write_and_settle_daemon_response(
            &mut retry_server,
            &policy,
            &mut retry_response,
        ));
        assert_eq!(
            retry_reader.join().expect("retry socket reader").result,
            Some(retry_result)
        );

        for workspace_id in [&capacity_busy_workspace, &failed_workspace] {
            let (mut repeated_response, repeated_result) =
                advisory_delivery_candidate(&report, &policy, workspace_id);
            assert!(repeated_response.delivery.is_some());
            assert!(
                repeated_result["response"]["data"]["degraded"]
                    .as_array()
                    .expect("settled workspace repeat degraded array")
                    .iter()
                    .all(|entry| {
                        !matches!(
                            entry["code"].as_str(),
                            Some("search_index_stale" | "search_index_large_gap")
                        )
                    })
            );
            settle_daemon_response_delivery(&policy, &mut repeated_response, true);
        }
        assert_eq!(
            policy
                .search_advisory_session()
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .tracked_workspace_count(),
            MAX_SEARCH_ADVISORY_WORKSPACES
        );
    }

    #[test]
    fn unbound_daemon_concurrent_workspace_reservations_share_one_process_winner() {
        const THREADS_PER_WORKSPACE: usize = 8;

        let report = permanent_reranker_advisory_report();
        let policy = DaemonDispatchPolicy::default();
        assert!(policy.bound_workspace_id().is_none());
        let barrier = Arc::new(std::sync::Barrier::new(THREADS_PER_WORKSPACE * 2 + 1));
        let progress_gate = Arc::new(std::sync::Barrier::new(3));
        let (progress_tx, progress_rx) = std::sync::mpsc::channel();
        let mut workers = Vec::new();
        for workspace_id in ["workspace-a", "workspace-b"] {
            for worker_index in 0..THREADS_PER_WORKSPACE {
                let report = report.clone();
                let policy = policy.clone();
                let barrier = Arc::clone(&barrier);
                let progress_gate = Arc::clone(&progress_gate);
                let progress_tx = progress_tx.clone();
                workers.push(thread::spawn(move || {
                    barrier.wait();
                    let (response, result) =
                        advisory_delivery_candidate(&report, &policy, workspace_id);
                    if worker_index == 0 {
                        progress_tx
                            .send(workspace_id)
                            .expect("cross-workspace progress receiver must remain live");
                        progress_gate.wait();
                    }
                    (workspace_id, response, result)
                }));
            }
        }
        drop(progress_tx);
        barrier.wait();
        let progressed_workspaces = [
            progress_rx
                .recv()
                .expect("workspace-a or workspace-b must make reservation progress"),
            progress_rx
                .recv()
                .expect("both workspaces must make reservation progress"),
        ]
        .into_iter()
        .collect::<std::collections::BTreeSet<_>>();
        progress_gate.wait();
        assert_eq!(
            progressed_workspaces,
            ["workspace-a", "workspace-b"].into_iter().collect(),
            "both workspaces must complete a reservation while the concurrent wave is still in flight"
        );
        let mut outcomes = workers
            .into_iter()
            .map(|worker| worker.join().expect("reservation worker must not panic"))
            .collect::<Vec<_>>();

        for workspace_id in ["workspace-a", "workspace-b"] {
            let workspace_outcomes = outcomes
                .iter()
                .filter(|(actual, _, _)| *actual == workspace_id)
                .collect::<Vec<_>>();
            assert_eq!(workspace_outcomes.len(), THREADS_PER_WORKSPACE);
            assert!(
                workspace_outcomes
                    .iter()
                    .all(|(_, response, _)| response.error.is_none())
            );
        }
        assert_eq!(
            outcomes
                .iter()
                .filter(|(_, _, result)| {
                    result
                        .pointer("/response/data/rerank/advisory/code")
                        .and_then(serde_json::Value::as_str)
                        == Some("rerank_model_unavailable")
                })
                .count(),
            1,
            "one process-wide permanent advisory identity must have one winner across all workspaces"
        );
        for (_, response, result) in &outcomes {
            assert!(
                response.delivery.is_some(),
                "every provisional occurrence must carry socket settlement"
            );
            let advisory = result
                .pointer("/response/data/rerank/advisory")
                .expect("rerank advisory field must remain present");
            if advisory.is_object() {
                assert_eq!(
                    advisory.get("code").and_then(serde_json::Value::as_str),
                    Some("rerank_model_unavailable"),
                    "the process-wide winner must own the permanent advisory"
                );
            } else {
                assert!(
                    advisory.is_null(),
                    "every competing process-wide nonwinner must carry rerank.advisory=null"
                );
            }
        }
        for (_, response, _) in &mut outcomes {
            settle_daemon_response_delivery(&policy, response, true);
        }

        let (mut repeated_response_a, repeated_a) =
            advisory_delivery_candidate(&report, &policy, "workspace-a");
        let (mut repeated_response_b, repeated_b) =
            advisory_delivery_candidate(&report, &policy, "workspace-b");
        for repeated in [&repeated_a, &repeated_b] {
            assert!(
                repeated
                    .pointer("/response/data/rerank/advisory")
                    .is_some_and(serde_json::Value::is_null)
            );
        }
        settle_daemon_response_delivery(&policy, &mut repeated_response_a, true);
        settle_daemon_response_delivery(&policy, &mut repeated_response_b, true);
    }

    #[test]
    fn same_workspace_concurrent_reservations_suppress_duplicates_without_errors() {
        const THREADS: usize = 16;

        let report = permanent_reranker_advisory_report();
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let barrier = Arc::new(std::sync::Barrier::new(THREADS + 1));
        let workers = (0..THREADS)
            .map(|_| {
                let report = report.clone();
                let policy = policy.clone();
                let barrier = Arc::clone(&barrier);
                thread::spawn(move || {
                    barrier.wait();
                    advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID)
                })
            })
            .collect::<Vec<_>>();
        barrier.wait();
        let mut outcomes = workers
            .into_iter()
            .map(|worker| worker.join().expect("reservation worker must not panic"))
            .collect::<Vec<_>>();
        assert!(
            outcomes
                .iter()
                .all(|(response, _)| response.error.is_none())
        );
        assert_eq!(
            outcomes
                .iter()
                .filter(|(_, result)| {
                    result
                        .pointer("/response/data/rerank/advisory/code")
                        .and_then(serde_json::Value::as_str)
                        == Some("rerank_model_unavailable")
                })
                .count(),
            1,
            "only one thread may own the same-workspace delivery reservation"
        );
        assert_eq!(
            outcomes
                .iter()
                .filter(|(response, _)| response.delivery.is_some())
                .count(),
            THREADS,
            "suppressed occurrences also require delivery settlement"
        );
        for (response, result) in &outcomes {
            assert!(response.delivery.is_some());
            let advisory = result
                .pointer("/response/data/rerank/advisory")
                .expect("rerank advisory field must remain present");
            if advisory.is_object() {
                assert_eq!(
                    advisory.get("code").and_then(serde_json::Value::as_str),
                    Some("rerank_model_unavailable"),
                    "the reservation winner must own the advisory"
                );
            } else {
                assert!(
                    advisory.is_null(),
                    "every competing same-workspace nonwinner must carry rerank.advisory=null"
                );
            }
        }
        let winner_index = outcomes
            .iter()
            .position(|(_, result)| {
                result
                    .pointer("/response/data/rerank/advisory/code")
                    .and_then(serde_json::Value::as_str)
                    == Some("rerank_model_unavailable")
            })
            .expect("one response must own the advisory reservation");
        for (index, (response, _)) in outcomes.iter_mut().enumerate() {
            settle_daemon_response_delivery(&policy, response, index != winner_index);
        }
        let (mut retry_response, retry_result) =
            advisory_delivery_candidate(&report, &policy, TEST_WORKSPACE_ID);
        assert_eq!(
            retry_result
                .pointer("/response/data/rerank/advisory/code")
                .and_then(serde_json::Value::as_str),
            Some("rerank_model_unavailable"),
            "a failed delivery must release the advisory for the next response"
        );
        settle_daemon_response_delivery(&policy, &mut retry_response, true);
    }

    #[test]
    fn large_gap_warning_is_once_but_daemon_structured_freshness_remains() {
        let first_report = stale_index_advisory_report(108, 1);
        let changed_generation_report = stale_index_advisory_report(112, 2);
        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);

        let (mut failed_response, failed_result) =
            advisory_delivery_candidate(&first_report, &policy, TEST_WORKSPACE_ID);
        assert!(
            failed_result
                .pointer("/response/data/degraded")
                .and_then(serde_json::Value::as_array)
                .is_some_and(|entries| entries.iter().any(|entry| {
                    entry.get("code").and_then(serde_json::Value::as_str)
                        == Some("search_index_large_gap")
                }))
        );
        settle_daemon_response_delivery(&policy, &mut failed_response, false);

        let (mut delivered_response, delivered_result) =
            advisory_delivery_candidate(&first_report, &policy, TEST_WORKSPACE_ID);
        assert!(daemon_response_fits(&delivered_response, usize::MAX));
        assert!(
            delivered_result
                .get("human")
                .and_then(serde_json::Value::as_str)
                .is_some_and(|human| {
                    human.contains("search_index_stale") && human.contains("search_index_large_gap")
                })
        );
        assert!(
            delivered_result
                .pointer("/response/degraded")
                .and_then(serde_json::Value::as_array)
                .is_some_and(|entries| entries.iter().any(|entry| {
                    entry.get("code").and_then(serde_json::Value::as_str)
                        == Some("search_index_large_gap")
                }))
        );
        settle_daemon_response_delivery(&policy, &mut delivered_response, true);

        let (_, repeated_value) =
            advisory_delivery_candidate(&changed_generation_report, &policy, TEST_WORKSPACE_ID);
        let repeated_result = DaemonSearchResult::from_value(repeated_value)
            .expect("repeated result must remain a valid success response");
        assert!(daemon_search_degraded_codes(&repeated_result).is_empty());
        assert!(repeated_result.human.contains("No results for \"release\""));
        assert!(!repeated_result.human.contains("search_index_stale"));
        assert!(!repeated_result.human.contains("search_index_large_gap"));
        assert_eq!(repeated_result.response["degraded"], serde_json::json!([]));
        assert_eq!(
            repeated_result.response["data"]["indexFreshness"],
            serde_json::json!({
                "stale": true,
                "dbGeneration": 112,
                "indexGeneration": 2,
                "generationGap": 110,
                "largeGap": true,
            })
        );
    }

    #[test]
    fn daemon_human_renderer_keeps_small_gap_stale_warning() {
        let mut report = stale_index_advisory_report(2, 1);
        report
            .degraded
            .retain(|entry| entry.code != "search_index_large_gap");
        let response_data = report.data_json();

        assert_eq!(response_data["indexFreshness"]["largeGap"], false);
        let human = daemon_search_human_summary(&report, &response_data);
        assert!(human.contains("search_index_stale"));
        assert!(!human.contains("search_index_large_gap"));
    }

    #[test]
    fn context_large_gap_advisory_is_consumed_only_after_successful_delivery() {
        fn context_response() -> serde_json::Value {
            let degraded = serde_json::json!([
                {"code": "search_index_stale", "severity": "medium", "message": "stale"},
                {"code": "search_index_large_gap", "severity": "medium", "message": "large"}
            ]);
            serde_json::json!({
                "schema": crate::models::RESPONSE_SCHEMA_V2,
                "success": true,
                "data": {"degraded": degraded.clone()},
                "degraded": degraded,
            })
        }

        fn context_delivery_candidate(
            policy: &DaemonDispatchPolicy,
        ) -> (DaemonResponse, serde_json::Value) {
            let mut pending = PendingSearchAdvisoryDelivery::new(
                policy.search_advisory_session(),
                TEST_WORKSPACE_ID,
            );
            let mut result = context_response();
            {
                let mut session = policy
                    .search_advisory_session()
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                filter_context_large_gap_advisory_for_delivery(
                    &mut result,
                    &mut session,
                    TEST_WORKSPACE_ID,
                    pending.reservation_mut(),
                );
            }
            let response = DaemonResponse::ok(
                "req-context-advisory-delivery",
                TEST_AGENT_ID,
                Some(TEST_WORKSPACE_ID.to_owned()),
                result.clone(),
            );
            (pending.finish(response, true), result)
        }

        let policy = DaemonDispatchPolicy::for_workspace(TEST_WORKSPACE_ID);
        let (mut failed_response, failed) = context_delivery_candidate(&policy);
        assert!(failed["degraded"].as_array().is_some_and(|entries| {
            entries
                .iter()
                .any(|entry| entry["code"] == "search_index_large_gap")
        }));
        settle_daemon_response_delivery(&policy, &mut failed_response, false);

        let (mut delivered_response, delivered) = context_delivery_candidate(&policy);
        assert!(delivered["degraded"].as_array().is_some_and(|entries| {
            entries
                .iter()
                .any(|entry| entry["code"] == "search_index_large_gap")
        }));
        settle_daemon_response_delivery(&policy, &mut delivered_response, true);

        let (_, repeated) = context_delivery_candidate(&policy);
        for pointer in ["/degraded", "/data/degraded"] {
            let codes = repeated
                .pointer(pointer)
                .and_then(serde_json::Value::as_array)
                .expect("degraded array")
                .iter()
                .filter_map(|entry| entry["code"].as_str())
                .collect::<Vec<_>>();
            assert_eq!(codes, vec!["search_index_stale"]);
        }
    }

    #[cfg(target_vendor = "apple")]
    #[test]
    fn apple_peer_uid_uses_safe_std_peer_credentials() {
        let (_client, server) = UnixStream::pair().expect("create UnixStream pair");
        assert_eq!(
            peer_uid(&server).expect("read Apple peer credentials"),
            current_euid()
        );
    }

    #[test]
    fn dispatch_search_rejects_method_schema_drift_and_unknown_fields() {
        for params in [
            serde_json::json!({
                "schema": "ee.daemon.search.request.v1",
                "query": "release",
                "workspacePath": "/tmp/ee-daemon-search-contract"
            }),
            serde_json::json!({
                "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
                "query": "release",
                "workspacePath": "/tmp/ee-daemon-search-contract",
                "unknownField": true
            }),
        ] {
            let response = dispatch(&search_request(params));
            assert!(response.result.is_none());
            assert_eq!(
                response.error.as_ref().map(|error| error.code.as_str()),
                Some(DAEMON_SEARCH_PARAMS_INVALID_CODE)
            );
        }
    }

    #[test]
    fn daemon_search_request_v2_carries_explicit_performance_bit() {
        let workspace = tempfile::tempdir().expect("tempdir");
        let params = DaemonSearchParams::from_value(&serde_json::json!({
            "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
            "query": "release",
            "workspacePath": workspace.path(),
            "explainPerformance": true
        }))
        .expect("request v2 must accept its explicit performance bit");
        let (_, _, _, explain_performance) = params
            .into_search_parts(&workspace.path().display().to_string())
            .expect("valid request v2 must map to canonical search options");
        assert!(
            explain_performance,
            "performance request bit must survive strict wire decoding"
        );
    }

    #[test]
    fn daemon_search_request_v2_accepts_exact_integral_decimal_limits() {
        let workspace = tempfile::tempdir().expect("tempdir");
        for (raw, expected) in [
            ("1", 1_u32),
            ("1.0", 1_u32),
            ("1e0", 1_u32),
            ("4294967295.0", u32::MAX),
        ] {
            let mut value = serde_json::json!({
                "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
                "query": "release",
                "workspacePath": workspace.path()
            });
            value["limit"] = serde_json::from_str(raw).expect("exact numeric fixture");
            let params = DaemonSearchParams::from_value(&value)
                .unwrap_or_else(|error| panic!("limit {raw} must decode: {error}"));
            assert_eq!(params.limit, expected, "limit {raw}");
        }
    }

    #[test]
    fn daemon_search_request_v2_rejects_non_u32_limits() {
        let workspace = tempfile::tempdir().expect("tempdir");
        for raw in ["-1", "1.5", "4294967296", "1e400"] {
            let mut value = serde_json::json!({
                "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
                "query": "release",
                "workspacePath": workspace.path()
            });
            value["limit"] = serde_json::from_str(raw).expect("exact numeric fixture");
            assert!(
                DaemonSearchParams::from_value(&value).is_err(),
                "limit {raw} must be rejected"
            );
        }
    }

    #[test]
    fn daemon_search_request_v2_accepts_only_published_enum_spellings() {
        let workspace = tempfile::tempdir().expect("tempdir");
        let workspace_id = workspace.path().display().to_string();
        let base = serde_json::json!({
            "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
            "query": "release",
            "workspacePath": workspace.path(),
            "speed": "default",
            "dedupe": "doc_id",
            "sourceMode": "hybrid",
            "memoryScope": "swarm"
        });

        for (field, canonical) in [
            ("speed", "quality"),
            ("dedupe", "mi"),
            ("sourceMode", "lexical_only"),
            ("memoryScope", "workspace"),
        ] {
            let mut value = base.clone();
            value[field] = serde_json::json!(canonical);
            DaemonSearchParams::from_value(&value)
                .expect("canonical request shape")
                .into_search_parts(&workspace_id)
                .unwrap_or_else(|error| panic!("canonical {field}={canonical:?}: {error}"));
        }

        for (field, noncanonical) in [
            ("speed", " Quality "),
            ("dedupe", "doc-id"),
            ("sourceMode", "lexical"),
            ("sourceMode", "Semantic_Only"),
            ("memoryScope", " SWARM "),
        ] {
            let mut value = base.clone();
            value[field] = serde_json::json!(noncanonical);
            let error = DaemonSearchParams::from_value(&value)
                .expect("string enum request shape")
                .into_search_parts(&workspace_id)
                .expect_err("noncanonical wire enum must be rejected");
            assert!(
                !error.is_empty(),
                "empty error for {field}={noncanonical:?}"
            );
        }
    }

    #[test]
    fn dispatch_search_binds_params_to_authorized_workspace() {
        let response = dispatch(&search_request(serde_json::json!({
            "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
            "query": "release",
            "workspacePath": "/tmp/a-different-workspace"
        })));
        assert_eq!(
            response.error.as_ref().map(|error| error.code.as_str()),
            Some(DAEMON_SEARCH_PARAMS_INVALID_CODE)
        );
    }

    #[test]
    fn dispatch_write_binds_params_to_authorized_workspace() {
        // The envelope `workspace_id` is authorized, but `params.workspacePath`
        // addresses a different real workspace. `params.workspacePath` selects
        // the database the write lands in, so it must be refused before the
        // write is routed — the same binding `dispatch_search` enforces above.
        let temp = tempfile::tempdir().expect("tempdir");
        let authorized = temp.path().join("authorized");
        let other = temp.path().join("other");
        fs::create_dir_all(authorized.join(".ee")).expect("authorized workspace");
        fs::create_dir_all(other.join(".ee")).expect("other workspace");

        let mut request = DaemonRequest::new(
            "req-write-binding-001",
            TEST_AGENT_ID,
            METHOD_WRITE,
            serde_json::json!({
                "workspacePath": other,
                "content": "an off-workspace daemon write must be refused"
            }),
        );
        request.workspace_id = Some(authorized.display().to_string());

        let response = dispatch_write(&request, None);
        let error = response
            .error
            .as_ref()
            .expect("off-workspace write refused");
        assert_eq!(error.code.as_str(), DAEMON_WRITE_PARAMS_INVALID_CODE);
        // Assert the specific reason: `workspacePath` and `content` are the only
        // required params, so a generic parse failure here would otherwise let
        // this test pass without ever exercising the binding.
        assert!(
            error
                .message
                .contains("must identify the authorized envelope"),
            "expected the workspace-binding refusal, got: {}",
            error.message
        );
    }

    #[cfg(unix)]
    #[test]
    fn daemon_search_paths_are_canonical_and_workspace_contained() {
        let temp = tempfile::tempdir().expect("tempdir");
        let workspace = temp.path().join("workspace");
        let state_dir = workspace.join(".ee");
        fs::create_dir_all(&state_dir).expect("workspace state dir");
        let workspace_alias = temp.path().join("workspace-alias");
        std::os::unix::fs::symlink(&workspace, &workspace_alias).expect("workspace symlink");

        let params = DaemonSearchParams::from_value(&serde_json::json!({
            "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
            "query": "release",
            "workspacePath": workspace_alias,
            "databasePath": workspace.join(".ee/ee.db"),
            "indexDir": workspace.join(".ee/index")
        }))
        .expect("strict params");
        let (options, _, _, _) = params
            .into_search_parts(&workspace.display().to_string())
            .expect("canonical aliases inside the workspace are accepted");
        assert_eq!(
            options.workspace_path,
            fs::canonicalize(&workspace).unwrap()
        );
        assert_eq!(options.database_path, Some(workspace.join(".ee/ee.db")));
        assert_eq!(options.index_dir, Some(workspace.join(".ee/index")));
    }

    #[cfg(unix)]
    #[test]
    fn daemon_search_rejects_symlink_escape_for_database_and_index() {
        let temp = tempfile::tempdir().expect("tempdir");
        let workspace = temp.path().join("workspace");
        let state_dir = workspace.join(".ee");
        let outside = temp.path().join("outside");
        fs::create_dir_all(&state_dir).expect("workspace state dir");
        fs::create_dir_all(&outside).expect("outside dir");
        let escape = state_dir.join("escape");
        std::os::unix::fs::symlink(&outside, &escape).expect("escape symlink");

        for (field, escaped_path) in [
            ("databasePath", escape.join("ee.db")),
            ("indexDir", escape.clone()),
        ] {
            let mut value = serde_json::json!({
                "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
                "query": "release",
                "workspacePath": workspace
            });
            value[field] = serde_json::json!(escaped_path);
            let params = DaemonSearchParams::from_value(&value).expect("strict params");
            let error = params
                .into_search_parts(&workspace.display().to_string())
                .expect_err("symlink escape must be rejected");
            assert!(error.contains("must remain inside the canonical workspace"));
        }
    }

    #[cfg(unix)]
    #[test]
    fn daemon_search_rejects_dangling_final_symlink() {
        let temp = tempfile::tempdir().expect("tempdir");
        let workspace = temp.path().join("workspace");
        let state_dir = workspace.join(".ee");
        fs::create_dir_all(&state_dir).expect("workspace state dir");
        let dangling = state_dir.join("ee.db");
        std::os::unix::fs::symlink(temp.path().join("outside-missing.db"), &dangling)
            .expect("dangling database symlink");

        let params = DaemonSearchParams::from_value(&serde_json::json!({
            "schema": DAEMON_SEARCH_REQUEST_SCHEMA_V2,
            "query": "release",
            "workspacePath": workspace,
            "databasePath": dangling
        }))
        .expect("strict params");
        let error = params
            .into_search_parts(&workspace.display().to_string())
            .expect_err("dangling final symlink must be rejected");
        assert!(error.contains("must not identify a dangling symbolic link"));
    }

    #[test]
    fn daemon_search_result_rejects_schema_and_field_drift() {
        let canonical = serde_json::json!({
            "schema": crate::models::RESPONSE_SCHEMA_V2,
            "success": true,
            "data": {
                "command": "search",
                "status": "no_results",
                "embed_backend": "hash_fallback",
                "query": "release",
                "request": {},
                "scopeStats": {},
                "results": [],
                "consensus": [],
                "conflicts": [],
                "resultCount": 0,
                "elapsedMs": 1.0,
                "metrics": {},
                "rerank": {
                    "schema": "ee.rerank_posture.v1",
                    "mode": "fusion_only",
                    "configured": "off",
                    "topK": 0,
                    "rerankScoreCount": 0,
                    "scoreKind": "rrf_fused",
                    "available": false,
                    "degradedCode": null,
                    "advisory": null,
                    "advisorySummary": {
                        "scope": "response",
                        "permanent": null,
                        "distinctCount": 0,
                        "emittedCount": 0,
                        "suppressedCount": 0,
                        "sessionOccurrenceCount": 0,
                        "sessionSuppressedCount": 0
                    }
                },
                "profileRuntime": {},
                "errors": [],
                "degraded": []
            },
            "degraded": []
        });
        let base = serde_json::json!({
            "schema": DAEMON_SEARCH_RESPONSE_SCHEMA_V3,
            "response": canonical.clone(),
            "human": "Search results\n",
            "reuseContract": {
                "daemonProcess": "long_lived",
                "defaultSearchEmbedder": "process_scoped",
                "searchIndex": "per_request"
            },
            "timing": {
                "daemonTotal": {
                    "elapsedMs": 2.0,
                    "elapsedMsBucket": "1_9ms",
                    "nondeterministic": true
                },
                "embedderPreparation": null,
                "indexOpen": null,
                "query": null
            }
        });
        let renderings = DaemonSearchResult::from_value(base.clone())
            .expect("valid daemon search result")
            .into_renderings()
            .expect("validated daemon search diagnostics must remain serializable");
        assert_eq!(
            renderings.reuse_contract, base["reuseContract"],
            "validated reuse contract must survive CLI rendering conversion"
        );
        assert_eq!(
            renderings.timing, base["timing"],
            "validated timing must survive CLI rendering conversion"
        );

        let mut degradation_drift = base.clone();
        degradation_drift["response"]["degraded"] = serde_json::json!([{
            "code": "rerank_model_unavailable",
            "severity": "info",
            "message": "reranker unavailable",
            "unexpected": true
        }]);
        assert!(DaemonSearchResult::from_value(degradation_drift).is_err());

        let mut wrong_schema = base.clone();
        wrong_schema["schema"] = serde_json::json!("ee.daemon.search.response.v1");
        assert!(DaemonSearchResult::from_value(wrong_schema).is_err());

        let mut unknown_field = base;
        unknown_field["unexpected"] = serde_json::json!(true);
        assert!(DaemonSearchResult::from_value(unknown_field).is_err());

        let mut nested_drift = serde_json::json!({
            "schema": DAEMON_SEARCH_RESPONSE_SCHEMA_V3,
            "response": canonical,
            "human": "Search results\n",
            "reuseContract": {
                "daemonProcess": "long_lived",
                "defaultSearchEmbedder": "process_scoped",
                "searchIndex": "per_request"
            },
            "timing": {
                "daemonTotal": {
                    "elapsedMs": 2.0,
                    "elapsedMsBucket": "1_9ms",
                    "nondeterministic": true
                },
                "embedderPreparation": null,
                "indexOpen": null,
                "query": null
            }
        });
        nested_drift["response"]["data"]["unknown"] = serde_json::json!(true);
        assert!(DaemonSearchResult::from_value(nested_drift).is_err());
    }

    #[test]
    fn daemon_search_uint64_conversion_is_exact_and_bounded() {
        assert_eq!(DAEMON_SEARCH_UINT64_INSTANCE_POINTERS.len(), 39);
        for (value, expected) in [
            (serde_json::json!(0), 0),
            (serde_json::json!(1.0), 1),
            (serde_json::json!(1e3), 1_000),
            (
                serde_json::json!(18_446_744_073_709_549_568.0),
                18_446_744_073_709_550_000,
            ),
            (serde_json::json!(u64::MAX), u64::MAX),
        ] {
            assert_eq!(json_number_to_u64(&value), Some(expected), "{value}");
        }

        for (raw, expected) in [
            ("18446744073709551615.0", u64::MAX),
            ("18446744073709551614.0", u64::MAX - 1),
            ("184467440737095516150e-1", u64::MAX),
        ] {
            let value: serde_json::Value =
                serde_json::from_str(raw).expect("exact decimal fixture must parse");
            assert_eq!(json_number_to_u64(&value), Some(expected), "{raw}");
        }

        let over_u64: serde_json::Value = serde_json::from_str("18446744073709551616")
            .expect("over-u64 JSON number must parse for contract validation");
        let over_u64_decimal: serde_json::Value = serde_json::from_str("18446744073709551616.0")
            .expect("over-u64 decimal fixture must parse");
        let near_boundary_fraction: serde_json::Value =
            serde_json::from_str("18446744073709551614.5")
                .expect("near-boundary fractional fixture must parse");
        for value in [
            serde_json::json!(-1),
            serde_json::json!(1.5),
            over_u64,
            over_u64_decimal,
            near_boundary_fraction,
            serde_json::json!("1"),
        ] {
            assert_eq!(json_number_to_u64(&value), None, "{value}");
        }
        for unrepresentable in ["NaN", "Infinity", "-Infinity"] {
            assert!(
                serde_json::from_str::<serde_json::Value>(unrepresentable).is_err(),
                "nonfinite JSON number must fail before uint64 conversion: {unrepresentable}"
            );
        }
        let huge_finite: serde_json::Value = serde_json::from_str("1e400")
            .expect("arbitrary-precision JSON must preserve huge finite numbers");
        assert_eq!(
            json_number_to_u64(&huge_finite),
            None,
            "finite values outside uint64 must fail conversion"
        );
    }

    #[test]
    fn daemon_search_performance_preserves_canonical_in_process_report() {
        let report = permanent_reranker_advisory_report();
        let trace = SearchPerformanceTrace::default();
        let canonical =
            report.performance_explain_json_with_trace(SpeedMode::Default, false, &trace);
        let timing = DaemonSearchTiming::from_trace(Duration::from_millis(2), &trace);
        let mut advisory_session = SearchAdvisorySession::default();
        let encoded = serde_json::to_value(DaemonSearchResult::from_report(
            &report,
            false,
            TEST_WORKSPACE_ID,
            &mut advisory_session,
            timing,
            Some(canonical.clone()),
        ))
        .expect("daemon search result must encode");

        assert_eq!(
            encoded["performance"], canonical,
            "daemon transport must carry the in-process canonical performance payload unchanged"
        );
        let renderings = DaemonSearchResult::from_value(encoded)
            .expect("canonical performance response must validate")
            .into_renderings()
            .expect("validated performance response must remain serializable");
        assert_eq!(
            renderings.performance.as_ref(),
            Some(&canonical),
            "client decoding must preserve every canonical performance field"
        );
    }

    #[test]
    fn daemon_search_performance_strict_v3_rejects_raw_text_and_type_drift() {
        let report = stale_index_advisory_report(108, 1);
        let trace = SearchPerformanceTrace::default();
        let canonical =
            report.performance_explain_json_with_trace(SpeedMode::Default, false, &trace);
        assert!(
            canonical["data"]["fallbacks"]
                .as_array()
                .is_some_and(|fallbacks| !fallbacks.is_empty()),
            "fixture must exercise the strict fallback item validator"
        );
        let timing = DaemonSearchTiming::from_trace(Duration::from_millis(2), &trace);
        let mut advisory_session = SearchAdvisorySession::default();
        let encoded = serde_json::to_value(DaemonSearchResult::from_report(
            &report,
            false,
            TEST_WORKSPACE_ID,
            &mut advisory_session,
            timing,
            Some(canonical),
        ))
        .expect("daemon search result must encode");
        DaemonSearchResult::from_value(encoded.clone())
            .expect("canonical strict-v3 performance payload must validate");

        let mut raw_text = encoded.clone();
        raw_text["performance"]["data"]["query"]["rawText"] = serde_json::json!("release secret");
        assert!(
            DaemonSearchResult::from_value(raw_text).is_err(),
            "strict v3 must reject raw query text even when all required fields remain"
        );

        let mut query_type_drift = encoded.clone();
        query_type_drift["performance"]["data"]["query"]["lengthBytes"] = serde_json::json!("7");
        assert!(DaemonSearchResult::from_value(query_type_drift).is_err());

        let mut plan_type_drift = encoded.clone();
        plan_type_drift["performance"]["data"]["queryPlan"]["usesEmbeddings"] =
            serde_json::json!("false");
        assert!(DaemonSearchResult::from_value(plan_type_drift).is_err());

        let mut read_type_drift = encoded.clone();
        read_type_drift["performance"]["data"]["dbReads"]["memoryReads"] = serde_json::json!(-1);
        assert!(DaemonSearchResult::from_value(read_type_drift).is_err());

        let mut profile_nested_drift = encoded.clone();
        profile_nested_drift["performance"]["data"]["profileRuntime"]["budgets"]["search"]["unexpected"] =
            serde_json::json!(1);
        assert!(DaemonSearchResult::from_value(profile_nested_drift).is_err());

        let mut search_type_drift = encoded.clone();
        search_type_drift["performance"]["data"]["search"]["returnedHits"] = serde_json::json!("1");
        assert!(DaemonSearchResult::from_value(search_type_drift).is_err());

        let mut timing_nested_drift = encoded.clone();
        timing_nested_drift["performance"]["data"]["search"]["elapsed"]["nondeterministic"] =
            serde_json::json!(false);
        assert!(DaemonSearchResult::from_value(timing_nested_drift).is_err());

        let mut fallback_type_drift = encoded.clone();
        fallback_type_drift["performance"]["data"]["fallbacks"][0]["sources"] =
            serde_json::json!([7]);
        assert!(DaemonSearchResult::from_value(fallback_type_drift).is_err());

        let mut redaction_value_drift = encoded;
        redaction_value_drift["performance"]["data"]["redaction"]["safeFields"] =
            serde_json::json!(["counts"]);
        assert!(DaemonSearchResult::from_value(redaction_value_drift).is_err());
    }

    fn read_framed_daemon_response(stream: &mut UnixStream) -> DaemonResponse {
        use std::io::Read;

        let mut prefix = [0_u8; 4];
        stream
            .read_exact(&mut prefix)
            .expect("length prefix must arrive");
        let announced = u32::from_be_bytes(prefix) as usize;
        let mut body = vec![0_u8; announced];
        stream.read_exact(&mut body).expect("body must arrive");
        serde_json::from_slice(&body).expect("body must parse as daemon response")
    }

    #[test]
    fn client_deadline_preserves_buffered_response_after_peer_closes() {
        use std::io::Read;

        let (mut server, mut client) = UnixStream::pair().expect("socketpair");
        let deadline = Instant::now() + Duration::from_secs(2);
        set_client_deadline(&client, deadline).expect("install initial deadline");
        let response = DaemonResponse::ok(
            "req-buffered-reply",
            TEST_AGENT_ID,
            None,
            serde_json::json!({"reply": "complete before peer close"}),
        );
        write_response(&mut server, &response).expect("write complete response");
        drop(server);

        set_client_deadline(&client, deadline).expect("refresh after peer close");
        assert_eq!(read_framed_daemon_response(&mut client), response);
        set_client_deadline(&client, deadline).expect("refresh at EOF");
        assert_eq!(client.read(&mut [0_u8; 1]).expect("read EOF"), 0);
        assert!(matches!(
            set_client_deadline(&client, Instant::now()),
            Err(ClientError::DeadlineExceeded)
        ));
    }

    fn client_round_trip_against_single_response(
        request: &DaemonRequest,
        response: DaemonResponse,
    ) -> Result<DaemonResponse, ClientError> {
        let temp = tempfile::tempdir().expect("tempdir");
        let socket_path = temp.path().join("ee-daemon-client-test.sock");
        let listener = UnixListener::bind(&socket_path).expect("bind one-shot daemon socket");
        let worker = thread::spawn(move || {
            let (mut stream, _) = listener.accept().expect("accept one client");
            let _request = read_request(&mut stream).expect("client request frame must parse");
            write_response(&mut stream, &response).expect("response frame must write");
        });

        let result = client_round_trip(&socket_path, request);
        worker
            .join()
            .expect("one-shot daemon thread must not panic");
        result
    }

    #[test]
    fn daemon_echo_env_value_truthy_accepts_only_explicit_true_values() {
        for value in ["1", "true", "TRUE", "yes", "YES", "on", "ON", " true "] {
            assert!(
                daemon_echo_env_value_truthy(value),
                "{value:?} should enable daemon echo"
            );
        }
        for value in ["", "0", "false", "no", "off", "enabled", "please"] {
            assert!(
                !daemon_echo_env_value_truthy(value),
                "{value:?} should not enable daemon echo"
            );
        }
    }

    #[test]
    fn dispatch_echo_disabled_returns_error_by_default() {
        let request = DaemonRequest::new(
            "req-echo-disabled-001",
            TEST_AGENT_ID,
            METHOD_ECHO,
            serde_json::json!({"k": "v", "n": 7}),
        );
        let response = dispatch_with_echo_policy(&request, false);
        assert_eq!(response.request_id, "req-echo-disabled-001");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        assert!(response.result.is_none());
        let error = response.error.as_ref().expect("echo must be disabled");
        assert_eq!(error.code, DAEMON_ECHO_DISABLED_CODE);
    }

    #[test]
    fn dispatch_echo_enabled_returns_benign_params_unchanged() {
        let params = serde_json::json!({"k": "v", "n": 7});
        let request =
            DaemonRequest::new("req-echo-001", TEST_AGENT_ID, METHOD_ECHO, params.clone());
        let response = dispatch_with_echo_policy(&request, true);
        assert_eq!(response.request_id, "req-echo-001");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        assert_eq!(response.result, Some(params));
        assert!(response.error.is_none());
        assert!(response.degraded_codes.is_empty());
    }

    // bd-3uev6: echo must route params through the canonical redaction
    // pipeline (core::support_bundle::redact_json_value) so the dispatch
    // table is not a reflection oracle that bypasses every other
    // content-bearing surface's redaction. Benign params round-trip
    // unchanged (pinned above); secret-shaped values must NOT.
    #[test]
    fn dispatch_echo_redacts_secret_shaped_params() {
        let secret = "sk_live_abcdefghijklmnopqrstuvwxyz0123456789";
        let request = DaemonRequest::new(
            "req-echo-redact-001",
            TEST_AGENT_ID,
            METHOD_ECHO,
            serde_json::json!({"token": secret, "note": "hello"}),
        );
        let response = dispatch_with_echo_policy(&request, true);
        assert!(response.error.is_none());
        let result = response.result.expect("echo returns a result");
        let serialized = result.to_string();
        assert!(
            !serialized.contains("sk_live_abcdefghijklmnopqrstuvwxyz"),
            "echo must redact secret-shaped params via redact_json_value; leaked: {serialized}"
        );
        assert_eq!(
            result.get("note").and_then(serde_json::Value::as_str),
            Some("hello"),
            "non-secret fields must round-trip unchanged"
        );
    }

    #[test]
    fn client_round_trip_rejects_agent_id_mismatch() {
        let request = DaemonRequest::new(
            "req-agent-mismatch-001",
            TEST_AGENT_ID,
            METHOD_ECHO,
            serde_json::json!({}),
        );
        let response = DaemonResponse::err(
            request.request_id.clone(),
            "agent-spoofed",
            None,
            DAEMON_ECHO_DISABLED_CODE,
            "echo disabled",
        );

        let error = client_round_trip_against_single_response(&request, response)
            .expect_err("agent_id mismatch must be rejected");

        match error {
            ClientError::ResponseAgentIdMismatch { expected, actual } => {
                assert_eq!(expected, TEST_AGENT_ID);
                assert_eq!(actual, "agent-spoofed");
            }
            other => panic!("expected ResponseAgentIdMismatch, got {other:?}"),
        }
    }

    #[test]
    fn client_round_trip_rejects_workspace_id_mismatch() {
        let mut request = DaemonRequest::new(
            "req-workspace-mismatch-001",
            TEST_AGENT_ID,
            METHOD_CONTEXT,
            serde_json::json!({}),
        );
        request.workspace_id = Some(TEST_WORKSPACE_ID.to_owned());
        let response = DaemonResponse::err(
            request.request_id.clone(),
            request.agent_id.clone(),
            Some("workspace-spoofed".to_owned()),
            DAEMON_CONTEXT_PARAMS_INVALID_CODE,
            "invalid ee.daemon.context params",
        );

        let error = client_round_trip_against_single_response(&request, response)
            .expect_err("workspace_id mismatch must be rejected");

        match error {
            ClientError::ResponseWorkspaceIdMismatch { expected, actual } => {
                assert_eq!(expected.as_deref(), Some(TEST_WORKSPACE_ID));
                assert_eq!(actual.as_deref(), Some("workspace-spoofed"));
            }
            other => panic!("expected ResponseWorkspaceIdMismatch, got {other:?}"),
        }
    }

    #[test]
    fn dispatch_context_rejects_missing_workspace_path() {
        let request = context_request(
            "req-ctx-001",
            TEST_AGENT_ID,
            serde_json::json!({"task": "ship daemon"}),
        );
        let response = dispatch(&request);
        assert!(response.result.is_none());
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_CONTEXT_PARAMS_INVALID_CODE);
        assert!(response.degraded_codes.is_empty());
    }

    #[test]
    fn dispatch_context_zero_timeout_fails_before_pack_execution() {
        let request = context_request(
            "req-ctx-timeout-001",
            TEST_AGENT_ID,
            serde_json::json!({
                "task": "ship daemon",
                "workspacePath": "/tmp/ee-daemon-context-timeout-test",
                "timeoutMs": 0
            }),
        );
        let response = dispatch(&request);
        assert!(response.result.is_none());
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_CONTEXT_DEADLINE_EXCEEDED_CODE);
        assert!(response.degraded_codes.is_empty());
    }

    // GH #37: the daemon amortises a ~500k-piece tokenizer build and a 512 MB
    // embedding-matrix load across invocations, but only once its search stack
    // is resident. These lock the warm-up contract that makes that possible.

    #[test]
    fn daemon_warm_posture_labels_every_state() {
        // The label is a wire value read by the CLI fallback path, so drift
        // here silently turns "still warming" back into a bare deadline.
        assert_eq!(
            warm_posture::COLD,
            0,
            "cold must stay the zero value so the static initialises to it"
        );
        assert_eq!(warm_posture_label(warm_posture::COLD), "cold");
        assert_eq!(warm_posture_label(warm_posture::WARMING), "warming");
        assert_eq!(warm_posture_label(warm_posture::READY), "ready");
        assert_eq!(warm_posture_label(warm_posture::FAILED), "failed");
        // An unknown state must read as cold rather than as capacity: a client
        // that mistakes garbage for "ready" would wait out the whole deadline.
        assert_eq!(warm_posture_label(u8::MAX), "cold");
    }

    #[test]
    fn daemon_capabilities_advertise_the_warm_posture() {
        let capabilities = daemon_capabilities_result();
        let posture = capabilities
            .pointer("/warm/posture")
            .and_then(serde_json::Value::as_str)
            .expect("capabilities must advertise /warm/posture for GH #37 clients");
        assert!(
            matches!(posture, "cold" | "warming" | "ready" | "failed"),
            "unexpected warm posture label: {posture}"
        );
    }

    #[test]
    fn daemon_warm_is_enabled_unless_explicitly_switched_off() {
        assert!(daemon_warm_enabled_from_env_value(None));
        assert!(daemon_warm_enabled_from_env_value(Some("on")));
        assert!(daemon_warm_enabled_from_env_value(Some("")));
        assert!(
            daemon_warm_enabled_from_env_value(Some("1")),
            "an affirmative value must not be read as an opt-out"
        );
        for off in ["off", "OFF", " off ", "0", "false", "no"] {
            assert!(
                !daemon_warm_enabled_from_env_value(Some(off)),
                "{off} must disable startup warm-up"
            );
        }
    }

    #[test]
    fn warm_query_carries_no_workspace_content() {
        // The warm-up runs a real search. Its query must be a fixed token so a
        // warm-up can never depend on, or surface, workspace content.
        assert_eq!(DAEMON_WARM_QUERY, "ee daemon warmup");
    }

    #[test]
    fn dispatch_context_without_workspace_returns_method_unauthorized() {
        let request = DaemonRequest::new(
            "req-ctx-no-workspace-001",
            TEST_AGENT_ID,
            METHOD_CONTEXT,
            serde_json::json!({"task": "ship daemon"}),
        );
        let response = dispatch(&request);
        assert!(response.result.is_none());
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_METHOD_UNAUTHORIZED_CODE);
        assert!(
            response
                .degraded_codes
                .contains(&DAEMON_METHOD_UNAUTHORIZED_CODE.to_owned())
        );
    }

    #[test]
    fn dispatch_context_blank_workspace_returns_method_unauthorized() {
        let mut request = DaemonRequest::new(
            "req-ctx-blank-workspace-001",
            TEST_AGENT_ID,
            METHOD_CONTEXT,
            serde_json::json!({"task": "ship daemon"}),
        );
        request.workspace_id = Some("  ".to_owned());
        let response = dispatch(&request);
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_METHOD_UNAUTHORIZED_CODE);
    }

    #[test]
    fn dispatch_context_workspace_mismatch_returns_method_unauthorized() {
        let mut request = context_request(
            "req-ctx-wrong-workspace-001",
            TEST_AGENT_ID,
            serde_json::json!({"task": "ship daemon"}),
        );
        request.workspace_id = Some("workspace-other".to_owned());
        let shutdown = AtomicBool::new(false);
        let search_advisory_session = Mutex::new(SearchAdvisorySession::default());
        let response = dispatch_with_echo_policy_and_workspace(
            &request,
            false,
            Some(TEST_WORKSPACE_ID),
            &shutdown,
            None,
            &search_advisory_session,
        );
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_METHOD_UNAUTHORIZED_CODE);
    }

    #[test]
    fn daemon_method_authority_classifies_seed_methods() {
        assert_eq!(
            daemon_method_authority(METHOD_CAPABILITIES),
            Some(DaemonAuthority::SameUid)
        );
        assert_eq!(
            daemon_method_authority(METHOD_ECHO),
            Some(DaemonAuthority::SameUid)
        );
        assert_eq!(
            daemon_method_authority(METHOD_SHUTDOWN),
            Some(DaemonAuthority::SameUid)
        );
        assert_eq!(
            daemon_method_authority(METHOD_CONTEXT),
            Some(DaemonAuthority::SameUidWorkspace)
        );
        assert_eq!(
            daemon_method_authority(METHOD_SEARCH),
            Some(DaemonAuthority::SameUidWorkspace)
        );
        assert_eq!(daemon_method_authority("ee.daemon.nope"), None);
    }

    #[test]
    fn dispatch_capabilities_advertises_strict_v1_migration_contract() {
        let request = DaemonRequest::new(
            "req-capabilities-001",
            TEST_AGENT_ID,
            METHOD_CAPABILITIES,
            serde_json::json!({}),
        );
        let response = dispatch(&request);
        assert!(response.error.is_none());
        assert!(response.degraded_codes.is_empty());

        let result = response
            .result
            .as_ref()
            .expect("capabilities returns result");
        assert_eq!(
            result.get("protocol").and_then(serde_json::Value::as_str),
            Some("ee.daemon")
        );
        assert_eq!(
            result.get("request_schemas"),
            Some(&serde_json::json!([super::super::DAEMON_REQUEST_SCHEMA_V1]))
        );
        assert_eq!(
            result.get("response_schemas"),
            Some(&serde_json::json!([
                super::super::DAEMON_RESPONSE_SCHEMA_V1
            ]))
        );
        assert_eq!(
            result.get("methods"),
            Some(&serde_json::json!([
                METHOD_CAPABILITIES,
                METHOD_CONTEXT,
                METHOD_ECHO,
                METHOD_SEARCH,
                METHOD_ORIENT_HOOK,
                METHOD_RECALL,
                METHOD_SHUTDOWN,
                METHOD_TELEMETRY,
                METHOD_WRITE,
                METHOD_WRITE_JOURNAL
            ]))
        );
        assert_eq!(
            result
                .pointer("/method_schemas/ee.daemon.search/request")
                .and_then(serde_json::Value::as_str),
            Some(DAEMON_SEARCH_REQUEST_SCHEMA_V2)
        );
        assert_eq!(
            result
                .pointer("/method_schemas/ee.daemon.search/response")
                .and_then(serde_json::Value::as_str),
            Some(DAEMON_SEARCH_RESPONSE_SCHEMA_V3)
        );
        assert_eq!(
            result
                .pointer("/authorization/ee.daemon.telemetry")
                .and_then(serde_json::Value::as_str),
            Some(DaemonAuthority::SameUid.as_wire_label())
        );
        assert_eq!(
            result
                .pointer("/authorization/ee.daemon.write_journal")
                .and_then(serde_json::Value::as_str),
            Some(DaemonAuthority::SameUidWorkspace.as_wire_label())
        );
        assert_eq!(
            result
                .pointer("/authorization/ee.daemon.write")
                .and_then(serde_json::Value::as_str),
            Some(DaemonAuthority::SameUidWorkspace.as_wire_label())
        );
        assert_eq!(
            result
                .pointer("/forward_compat/v1_unknown_fields")
                .and_then(serde_json::Value::as_str),
            Some("rejected")
        );
        assert_eq!(
            result
                .pointer("/forward_compat/v1_unknown_methods")
                .and_then(serde_json::Value::as_str),
            Some(DAEMON_UNKNOWN_METHOD_CODE)
        );
        assert!(
            result
                .pointer("/forward_compat/v2_migration")
                .and_then(serde_json::Value::as_str)
                .is_some_and(|policy| policy.contains(METHOD_CAPABILITIES))
        );
        assert_eq!(
            result
                .pointer("/authorization/ee.daemon.context")
                .and_then(serde_json::Value::as_str),
            Some(DaemonAuthority::SameUidWorkspace.as_wire_label())
        );
        assert_eq!(
            result
                .pointer("/authorization/ee.daemon.shutdown")
                .and_then(serde_json::Value::as_str),
            Some(DaemonAuthority::SameUid.as_wire_label())
        );
    }

    #[test]
    fn dispatch_telemetry_returns_contention_report() {
        let request = DaemonRequest::new(
            "req-telemetry-001",
            TEST_AGENT_ID,
            METHOD_TELEMETRY,
            serde_json::json!({}),
        );
        let response = dispatch(&request);
        assert!(
            response.error.is_none(),
            "telemetry dispatch should succeed: {:?}",
            response.error
        );
        let result = response.result.as_ref().expect("telemetry returns result");
        assert_eq!(
            result.get("schemaTag").and_then(serde_json::Value::as_str),
            Some(crate::models::contention::CONTENTION_DIAG_SCHEMA_V1),
            "telemetry result is an ee.diag.contention.v1 report"
        );
        // The report always carries an overall posture, and the two
        // process-global sources the daemon snapshots (group-commit,
        // singleflight) are present rather than listed as unavailable.
        assert!(
            result.get("overallPosture").is_some(),
            "report carries an overall posture"
        );
        assert!(
            result.get("singleflight").is_some(),
            "singleflight posture is snapshotted in-process"
        );
        assert!(
            result.get("groupCommit").is_some(),
            "group-commit telemetry is snapshotted in-process"
        );
    }

    #[test]
    fn telemetry_method_is_same_uid_authorized() {
        assert_eq!(
            daemon_method_authority(METHOD_TELEMETRY),
            Some(DaemonAuthority::SameUid)
        );
    }

    #[test]
    fn daemon_write_params_from_value_parses_remember_inputs() {
        let value = serde_json::json!({
            "workspacePath": "/tmp/ws",
            "content": "remember this",
            "tags": "a,b",
            "confidence": 0.5,
        });
        let params = DaemonWriteParams::from_value(&value).expect("valid write params");
        assert_eq!(params.workspace_path, std::path::PathBuf::from("/tmp/ws"));
        assert_eq!(params.content, "remember this");
        assert_eq!(params.level, "episodic", "level defaults to episodic");
        assert_eq!(params.kind, "fact", "kind defaults to fact");
        assert_eq!(params.tags.as_deref(), Some("a,b"));
        assert!((params.confidence - 0.5).abs() < f32::EPSILON);
        assert!(
            params.auto_link,
            "auto_link defaults true for ee-remember parity"
        );
        assert!(
            params.propose_candidates,
            "propose_candidates defaults true"
        );
        // options() borrows the owned strings without panicking.
        assert_eq!(params.options().content, "remember this");
    }

    #[test]
    fn daemon_write_params_require_content_and_workspace() {
        let missing_content = serde_json::json!({ "workspacePath": "/tmp/ws" });
        assert!(DaemonWriteParams::from_value(&missing_content).is_err());
        let missing_workspace = serde_json::json!({ "content": "x" });
        assert!(DaemonWriteParams::from_value(&missing_workspace).is_err());
        let not_object = serde_json::json!("nope");
        assert!(DaemonWriteParams::from_value(&not_object).is_err());
    }

    #[test]
    fn write_method_is_same_uid_workspace_authorized() {
        assert_eq!(
            daemon_method_authority(METHOD_WRITE),
            Some(DaemonAuthority::SameUidWorkspace)
        );
    }

    #[test]
    fn write_journal_method_is_same_uid_workspace_authorized() {
        assert_eq!(
            daemon_method_authority(METHOD_WRITE_JOURNAL),
            Some(DaemonAuthority::SameUidWorkspace)
        );
    }

    #[test]
    fn dispatch_journal_rejects_invalid_params() {
        // No router (unbound) + bad params -> params-invalid RPC error, before
        // any DB work. Exercises the journal dispatch path without a workspace.
        let mut request = DaemonRequest::new(
            "req-journal-bad",
            TEST_AGENT_ID,
            METHOD_WRITE_JOURNAL,
            serde_json::json!({ "not": "journal params" }),
        );
        request.workspace_id = Some("journal-ws".to_string());
        let response = dispatch_journal(&request, None);
        let error = response.error.as_ref().expect("invalid params -> error");
        assert_eq!(error.code, DAEMON_JOURNAL_PARAMS_INVALID_CODE);
    }

    #[test]
    fn mixed_journal_outcome_batch_rolls_back_as_one_transaction() {
        let dir = tempfile::Builder::new()
            .prefix("daemon-mixed-batch")
            .tempdir_in(std::env::temp_dir())
            .expect("tempdir");
        let workspace_path = dir.path().canonicalize().expect("canonical workspace");
        let database_path = workspace_path.join(".ee").join("ee.db");
        std::fs::create_dir_all(workspace_path.join(".ee")).expect("create .ee");
        let workspace_id = "wsp_00000000000000000000009991".to_string();
        let memory_id = "mem_00000000000000000000009992".to_string();
        let connection = crate::db::DbConnection::open_file(&database_path).expect("open db");
        connection.migrate().expect("migrate");
        connection
            .insert_workspace(
                &workspace_id,
                &crate::db::CreateWorkspaceInput {
                    path: workspace_path.to_string_lossy().into_owned(),
                    name: Some("daemon mixed batch".to_string()),
                },
            )
            .expect("workspace");
        connection
            .insert_memory(
                &memory_id,
                &crate::db::CreateMemoryInput {
                    workspace_id: workspace_id.clone(),
                    level: "procedural".to_string(),
                    kind: "rule".to_string(),
                    content: "Use daemon mixed batch transaction tests.".to_string(),
                    workflow_id: None,
                    confidence: 0.8,
                    utility: 0.7,
                    importance: 0.6,
                    provenance_uri: Some("test://daemon-mixed-batch".to_string()),
                    trust_class: "human_explicit".to_string(),
                    trust_subclass: None,
                    tags: Vec::new(),
                    valid_from: None,
                    valid_to: None,
                },
            )
            .expect("memory");

        let journal_id = crate::core::journal::generate_journal_entry_id();
        let journal = crate::core::write_owner::WriteOperation::Custom {
            operation_type: DaemonJournalParams::ACTOR_OPERATION_TYPE.to_string(),
            payload: serde_json::to_value(DaemonJournalParams {
                workspace_path: workspace_path.clone(),
                workspace_id: workspace_id.clone(),
                entry_id: Some(journal_id),
                agent_name: Some("daemon-test".to_string()),
                session_key: None,
                kind: "note".to_string(),
                source: "manual".to_string(),
                body: "journal row staged before failing outcome".to_string(),
                structured: None,
                redaction_report: "{}".to_string(),
                instruction_risk: "none".to_string(),
            })
            .expect("journal payload"),
        };
        let invalid_outcome = crate::core::write_owner::WriteOperation::Custom {
            operation_type: DaemonOutcomeParams::ACTOR_OPERATION_TYPE.to_string(),
            payload: serde_json::to_value(DaemonOutcomeParams {
                workspace_path: workspace_path.clone(),
                event_id: "bad-feedback-id".to_string(),
                workspace_id: workspace_id.clone(),
                target_type: "memory".to_string(),
                target_id: memory_id,
                signal: "helpful".to_string(),
                weight: 1.0,
                source_type: "outcome_observed".to_string(),
                source_id: Some("daemon-test".to_string()),
                reason: Some("force invalid event id rollback".to_string()),
                evidence_json: None,
                session_id: None,
                actor: Some("daemon-test".to_string()),
                audit_id: crate::db::generate_audit_id(),
                details: None,
            })
            .expect("outcome payload"),
        };

        let result = execute_daemon_txn_batch(&[journal, invalid_outcome]);
        assert!(result.is_err(), "invalid outcome id should fail the batch");
        let entries = connection
            .list_journal_entries(
                &workspace_id,
                &crate::db::JournalEntryListFilter {
                    limit: 10,
                    ..crate::db::JournalEntryListFilter::default()
                },
            )
            .expect("list journal entries");
        assert!(
            entries.is_empty(),
            "journal insert must roll back when a later outcome op fails"
        );
    }

    #[test]
    fn invalid_daemon_remember_batch_does_not_create_store() {
        let dir = tempfile::Builder::new()
            .prefix("daemon-invalid-remember-batch")
            .tempdir_in(std::env::temp_dir())
            .expect("tempdir");
        let workspace_path = dir.path().canonicalize().expect("canonical workspace");
        let store_dir = workspace_path.join(".ee");
        let database_path = store_dir.join("ee.db");
        let remember_op =
            |content: &str, kind: &str| crate::core::write_owner::WriteOperation::Custom {
                operation_type: DaemonWriteParams::ACTOR_OPERATION_TYPE.to_string(),
                payload: serde_json::to_value(DaemonWriteParams {
                    workspace_path: workspace_path.clone(),
                    content: content.to_string(),
                    level: "episodic".to_string(),
                    kind: kind.to_string(),
                    tags: None,
                    confidence: 0.8,
                    source: Some("manual://daemon-invalid-remember-batch".to_string()),
                    workflow_id: None,
                    auto_link: false,
                    propose_candidates: false,
                })
                .expect("remember payload"),
            };

        let result = execute_daemon_txn_batch(&[
            remember_op("Valid row before the invalid row.", "fact"),
            remember_op("Cross-wired row must reject the batch.", "semantic"),
        ]);
        let error = result.expect_err("cross-wired remember must reject the batch");
        assert_eq!(
            error.code(),
            crate::core::memory::REMEMBER_KIND_IS_LEVEL_CODE
        );
        assert!(
            !database_path.exists(),
            "invalid daemon remember batch must not create the database"
        );
        assert!(
            !store_dir.exists(),
            "invalid daemon remember batch must not create the store directory"
        );
    }

    #[test]
    fn valid_daemon_remember_batch_requires_an_initialized_store() {
        let dir = tempfile::Builder::new()
            .prefix("daemon-storeless-remember-batch")
            .tempdir_in(std::env::temp_dir())
            .expect("tempdir");
        let workspace_path = dir.path().join("addressed-but-uninitialized");
        let database_path = workspace_path.join(".ee").join("ee.db");
        let operation = crate::core::write_owner::WriteOperation::Custom {
            operation_type: DaemonWriteParams::ACTOR_OPERATION_TYPE.to_string(),
            payload: serde_json::to_value(DaemonWriteParams {
                workspace_path: workspace_path.clone(),
                content: "A valid daemon write must not initialize its own store.".to_string(),
                level: "episodic".to_string(),
                kind: "fact".to_string(),
                tags: None,
                confidence: 0.8,
                source: Some("manual://daemon-storeless-remember-batch".to_string()),
                workflow_id: None,
                auto_link: false,
                propose_candidates: false,
            })
            .expect("remember payload"),
        };

        let error = execute_daemon_txn_batch(&[operation])
            .expect_err("a daemon write must reject an uninitialized addressed store");
        assert!(matches!(
            error,
            crate::models::DomainError::WorkspaceStoreMissing { .. }
        ));
        assert!(!workspace_path.exists());
        assert!(!database_path.exists());
    }

    #[test]
    fn daemon_remember_batch_commits_memories_and_drains_index_jobs() {
        let dir = tempfile::Builder::new()
            .prefix("daemon-remember-batch")
            .tempdir_in(std::env::temp_dir())
            .expect("tempdir");
        let workspace_path = dir.path().canonicalize().expect("canonical workspace");
        let database_path = workspace_path.join(".ee").join("ee.db");
        std::fs::create_dir_all(workspace_path.join(".ee")).expect("create .ee");
        let initialized = crate::db::DbConnection::open_file(&database_path)
            .expect("initialize daemon fixture database");
        initialized
            .migrate()
            .expect("migrate daemon fixture database");
        initialized.close().expect("close daemon fixture database");

        let remember_op = |content: &str| crate::core::write_owner::WriteOperation::Custom {
            operation_type: DaemonWriteParams::ACTOR_OPERATION_TYPE.to_string(),
            payload: serde_json::to_value(DaemonWriteParams {
                workspace_path: workspace_path.clone(),
                content: content.to_string(),
                level: "procedural".to_string(),
                kind: "rule".to_string(),
                tags: Some("daemon-batch".to_string()),
                confidence: 0.8,
                source: Some("manual://daemon-remember-batch".to_string()),
                workflow_id: None,
                auto_link: false,
                propose_candidates: false,
            })
            .expect("remember payload"),
        };

        let results = execute_daemon_txn_batch(&[
            remember_op("Daemon remember batch row one."),
            remember_op("Daemon remember batch row two."),
        ])
        .expect("remember batch");
        assert_eq!(results.len(), 2);
        let memory_ids = results
            .into_iter()
            .map(|result| match result {
                crate::core::write_owner::WriteResult::Success {
                    entity_id: Some(id),
                } => id,
                other => panic!("expected successful memory result, got {other:?}"),
            })
            .collect::<Vec<_>>();

        let connection = crate::db::DbConnection::open_file(&database_path).expect("open db");
        let mut workspace_id = None;
        for memory_id in &memory_ids {
            let memory = connection
                .get_memory(memory_id)
                .expect("query memory")
                .expect("memory exists");
            workspace_id = Some(memory.workspace_id);
        }
        let workspace_id = workspace_id.expect("workspace id");
        let pending = connection
            .list_pending_search_index_jobs(&workspace_id, None)
            .expect("pending jobs");
        assert!(
            pending.is_empty(),
            "daemon remember batch should drain deferred memory index jobs"
        );
        let status =
            crate::core::index::get_index_status(&crate::core::index::IndexStatusOptions {
                workspace_path: workspace_path.clone(),
                database_path: Some(database_path.clone()),
                index_dir: None,
            })
            .expect("daemon batch index status");
        assert_eq!(status.health, crate::core::index::IndexHealth::Ready);
        assert_eq!(status.db_generation, status.index_generation);
        assert_eq!(
            status
                .index_document_counts
                .as_ref()
                .map(|counts| counts.memories),
            Some(2)
        );
        let search = crate::core::search::run_search_with_filters(
            &crate::core::search::SearchOptions {
                workspace_path,
                database_path: Some(database_path),
                index_dir: None,
                query: "Daemon remember batch row".to_owned(),
                limit: 10,
                speed: crate::search::SpeedMode::Instant,
                explain: false,
                as_of: None,
                include_tombstoned: false,
                include_expired: false,
                include_future: false,
                include_stale: false,
                relevance_floor: Some(0.0),
                dedup_mode: crate::core::search::SearchDedupMode::DocId,
                source_mode: crate::core::search::SearchSourceMode::LexicalOnly,
                strict_source_mode: true,
                memory_scope: crate::models::MemoryScope::Workspace,
                strict_scope: false,
            },
            None,
            &[],
        )
        .expect("daemon batch search");
        let actual_ids = search
            .results
            .into_iter()
            .map(|hit| hit.doc_id)
            .collect::<std::collections::BTreeSet<_>>();
        assert_eq!(
            actual_ids,
            memory_ids.into_iter().collect(),
            "daemon batch response IDs must be exactly searchable without a manual rebuild"
        );
    }

    #[test]
    fn write_actor_hosting_round_trips_a_result_without_deadlock() {
        // Behavioral proof of the Inc 2 runtime hosting (bd-wx6ou.3): build the
        // same current_thread runtime + WriteOwner actor `start_server` hosts,
        // submit an op, and block_on the WriteResult from a different thread.
        // Uses an UNSUPPORTED Custom op so `execute_write_operation` returns
        // Failed immediately (no `remember_memory`, so no embedding hang in the
        // worker sandbox) — this still exercises spawn → submit → process_batch
        // → oneshot send → block_on(recv), which is where a runtime-lifecycle
        // deadlock would surface.
        use crate::core::write_owner::{
            DEFAULT_CHANNEL_CAPACITY, WriteHotPathConfig, WriteOperation, WriteOwner, WriteResult,
        };
        let runtime = crate::core::build_cli_runtime().expect("build runtime");
        let (owner, handle) = WriteOwner::new(DEFAULT_CHANNEL_CAPACITY);
        let task = runtime
            .handle()
            .try_spawn(async move {
                let Some(cx) = asupersync::Cx::current() else {
                    return;
                };
                let _ = owner
                    .run_group_commit(&cx, WriteHotPathConfig::default(), |ops| {
                        Ok(ops.iter().map(execute_write_operation).collect::<Vec<_>>())
                    })
                    .await;
            })
            .expect("spawn write actor");

        let operation = WriteOperation::Custom {
            operation_type: "ee.daemon.unsupported".to_string(),
            payload: serde_json::json!({}),
        };
        let mut receiver = handle.try_submit(operation).expect("submit accepted");
        let result = runtime.block_on(async {
            let cx = asupersync::Cx::current().expect("block_on installs an ambient Cx");
            receiver.recv(&cx).await
        });
        assert!(
            matches!(result, Ok(WriteResult::Failed { .. })),
            "an unsupported op round-trips as a Failed result"
        );

        // Clean shutdown: dropping the handle closes the mpsc so the actor's
        // recv returns Disconnected and the task completes; the join must not
        // hang.
        drop(handle);
        runtime.block_on(task);
    }

    #[test]
    fn dispatch_shutdown_sets_shutdown_latch() {
        let request = DaemonRequest::new(
            "req-shutdown-001",
            TEST_AGENT_ID,
            METHOD_SHUTDOWN,
            serde_json::json!({}),
        );
        let shutdown = AtomicBool::new(false);
        let search_advisory_session = Mutex::new(SearchAdvisorySession::default());
        let response = dispatch_with_echo_policy_and_workspace(
            &request,
            false,
            None,
            &shutdown,
            None,
            &search_advisory_session,
        );

        assert!(response.error.is_none());
        assert!(shutdown.load(Ordering::SeqCst));
        assert_eq!(
            response
                .result
                .as_ref()
                .and_then(|value| value.pointer("/schema"))
                .and_then(serde_json::Value::as_str),
            Some("ee.daemon.shutdown.v1")
        );
        assert_eq!(
            response
                .result
                .as_ref()
                .and_then(|value| value.pointer("/accepted"))
                .and_then(serde_json::Value::as_bool),
            Some(true)
        );
    }

    #[test]
    fn dispatch_unknown_method_returns_unknown_method_code() {
        let request = DaemonRequest::new(
            "req-unk-001",
            TEST_AGENT_ID,
            "ee.daemon.nope",
            serde_json::Value::Null,
        );
        let response = dispatch(&request);
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_UNKNOWN_METHOD_CODE);
    }

    // bd-qwlzu: the dispatch-observability classifiers feed the
    // `ee.daemon.rpc` audit event. Pin the code+boolean mapping so the
    // per-method attribution cannot silently drift.
    #[test]
    fn classify_dispatch_response_ok_has_no_error_flags() {
        let response = DaemonResponse::ok("r", TEST_AGENT_ID, None, serde_json::json!({"x": 1}));
        assert_eq!(classify_dispatch_response(&response), ("ok", false, false));
    }

    #[test]
    fn classify_dispatch_response_schema_mismatch_sets_only_schema_flag() {
        let bogus = DaemonRequest {
            schema: "ee.daemon.request.v0_wrong".to_owned(),
            request_id: "r".to_owned(),
            agent_id: TEST_AGENT_ID.to_owned(),
            workspace_id: None,
            method: METHOD_ECHO.to_owned(),
            params: serde_json::Value::Null,
        };
        let response = dispatch(&bogus);
        let (code, schema_mismatch, unknown_method) = classify_dispatch_response(&response);
        assert_eq!(code, DAEMON_REQUEST_SCHEMA_MISMATCH_CODE);
        assert!(schema_mismatch);
        assert!(!unknown_method);
    }

    #[test]
    fn classify_dispatch_response_unknown_method_sets_only_unknown_flag() {
        let request = DaemonRequest::new(
            "r",
            TEST_AGENT_ID,
            "ee.daemon.nope",
            serde_json::Value::Null,
        );
        let response = dispatch(&request);
        let (code, schema_mismatch, unknown_method) = classify_dispatch_response(&response);
        assert_eq!(code, DAEMON_UNKNOWN_METHOD_CODE);
        assert!(!schema_mismatch);
        assert!(unknown_method);
    }

    #[test]
    fn frame_error_kind_categories_are_stable_and_leak_free() {
        assert_eq!(frame_error_kind(&FrameReadError::Eof), "eof");
        assert_eq!(
            frame_error_kind(&FrameReadError::TooLarge {
                announced: 9,
                max: 4
            }),
            "too_large"
        );
        assert_eq!(
            frame_error_kind(&FrameReadError::Truncated {
                expected: 9,
                got: 2
            }),
            "truncated"
        );
        assert_eq!(
            frame_error_kind(&FrameReadError::Io(std::io::Error::other("boom"))),
            "io"
        );
        let decode_err = serde_json::from_str::<DaemonRequest>("{").unwrap_err();
        assert_eq!(
            frame_error_kind(&FrameReadError::Decode(decode_err)),
            "decode"
        );
    }

    #[test]
    fn frame_transport_failures_close_without_decode_envelope() {
        assert!(frame_error_closes_without_response(&FrameReadError::Eof));
        assert!(frame_error_closes_without_response(
            &FrameReadError::Truncated {
                expected: 9,
                got: 2,
            }
        ));
        assert!(frame_error_closes_without_response(&FrameReadError::Io(
            io::Error::new(io::ErrorKind::TimedOut, "read timeout")
        )));
        assert!(frame_error_closes_without_response(&FrameReadError::Io(
            io::Error::new(io::ErrorKind::WouldBlock, "timeout on unix")
        )));
        assert!(frame_error_closes_without_response(&FrameReadError::Io(
            io::Error::new(io::ErrorKind::ConnectionReset, "peer reset")
        )));
        assert!(frame_error_closes_without_response(&FrameReadError::Io(
            io::Error::new(io::ErrorKind::UnexpectedEof, "short read")
        )));

        assert!(!frame_error_closes_without_response(
            &FrameReadError::TooLarge {
                announced: 9,
                max: 4,
            }
        ));
        assert!(!frame_error_closes_without_response(&FrameReadError::Io(
            io::Error::other("non-timeout read failure")
        )));
        let decode_err = serde_json::from_str::<DaemonRequest>("{").unwrap_err();
        assert!(!frame_error_closes_without_response(
            &FrameReadError::Decode(decode_err)
        ));
    }

    #[test]
    fn accept_loop_terminated_io_error_kind_labels_are_stable() {
        assert_eq!(
            io_error_kind_label(io::ErrorKind::Interrupted),
            "interrupted"
        );
        assert_eq!(io_error_kind_label(io::ErrorKind::AddrInUse), "addr_in_use");
        assert_eq!(io_error_kind_label(io::ErrorKind::TimedOut), "timed_out");
        assert_eq!(
            io_error_kind_label(io::ErrorKind::UnexpectedEof),
            "unexpected_eof"
        );
    }

    #[test]
    fn dispatch_schema_mismatch_returns_schema_mismatch_code() {
        let bogus = DaemonRequest {
            schema: "ee.daemon.request.v0_wrong".to_owned(),
            request_id: "req-schema-001".to_owned(),
            agent_id: TEST_AGENT_ID.to_owned(),
            workspace_id: None,
            method: METHOD_ECHO.to_owned(),
            params: serde_json::Value::Null,
        };
        let response = dispatch(&bogus);
        let error = response.error.as_ref().expect("must have error");
        assert_eq!(error.code, DAEMON_REQUEST_SCHEMA_MISMATCH_CODE);
    }

    #[test]
    fn daemon_cancellation_drains_in_flight_workers_before_shutdown_reports_idle() {
        let pool = InflightPool::new(1);
        let permit = pool.try_acquire().expect("first permit must acquire");

        assert!(
            !pool.wait_until_idle(Duration::from_millis(1)),
            "pool with a held permit must not report idle"
        );

        drop(permit);

        assert!(
            pool.wait_until_idle(Duration::from_millis(50)),
            "pool must report idle after the last permit drops"
        );
    }

    #[test]
    fn daemon_shutdown_timeout_can_be_retried_after_workers_drain() {
        let pool = InflightPool::new(1);
        let permit = pool.try_acquire().expect("first permit must acquire");
        let mut handle = DaemonServerHandle {
            socket_path: std::env::temp_dir().join(format!(
                "ee-daemon-drain-test-{}-{}",
                std::process::id(),
                uuid::Uuid::now_v7()
            )),
            shutdown: Arc::new(AtomicBool::new(false)),
            pool,
            accept_thread: None,
            scheduler_thread: None,
            warm_thread: None,
            shutdown_done: AtomicBool::new(false),
            workers_drained: AtomicBool::new(false),
            write_handle: None,
            write_owner_task: None,
            write_runtime: None,
        };

        let error = handle
            .shutdown_with_worker_drain_timeout(Duration::from_millis(1))
            .expect_err("held worker permit must make shutdown time out");
        assert_eq!(error.kind(), io::ErrorKind::TimedOut);
        assert!(
            handle.shutdown_done.load(Ordering::Acquire),
            "listener/socket teardown should not be retried after the first shutdown attempt"
        );
        assert!(
            !handle.workers_drained.load(Ordering::Acquire),
            "timed-out worker drain must not be recorded as success"
        );

        drop(permit);

        handle
            .shutdown_with_worker_drain_timeout(Duration::from_millis(50))
            .expect("shutdown retry should succeed once workers drain");
        assert!(
            handle.workers_drained.load(Ordering::Acquire),
            "successful retry must latch drained worker state"
        );
    }

    #[test]
    fn daemon_scheduler_join_timeout_can_be_retried_after_busy_task_exits() {
        let (entered_tx, entered_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel();
        let (done_tx, done_rx) = mpsc::channel();
        let join = thread::spawn(move || {
            entered_tx.send(()).expect("notify scheduler entered");
            release_rx.recv().expect("wait for busy scheduler release");
            let _ = done_tx.send(SchedulerThreadExit::Returned);
        });
        entered_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("scheduler test thread must enter busy section");

        let mut handle = DaemonServerHandle {
            socket_path: std::env::temp_dir().join(format!(
                "ee-daemon-scheduler-timeout-test-{}-{}",
                std::process::id(),
                uuid::Uuid::now_v7()
            )),
            shutdown: Arc::new(AtomicBool::new(false)),
            pool: InflightPool::new(1),
            accept_thread: None,
            scheduler_thread: Some(SchedulerThreadHandle { join, done_rx }),
            warm_thread: None,
            shutdown_done: AtomicBool::new(false),
            workers_drained: AtomicBool::new(false),
            write_handle: None,
            write_owner_task: None,
            write_runtime: None,
        };

        let started = Instant::now();
        let error = handle
            .shutdown_with_worker_drain_timeout(Duration::from_millis(20))
            .expect_err("busy scheduler must make shutdown return a bounded timeout");
        assert_eq!(error.kind(), io::ErrorKind::TimedOut);
        assert!(
            started.elapsed() < DAEMON_SCHEDULER_JOIN_TIMEOUT + Duration::from_millis(500),
            "scheduler shutdown must be bounded; elapsed {:?}",
            started.elapsed()
        );
        assert!(
            handle.scheduler_thread.is_some(),
            "timed-out scheduler join must remain retryable"
        );

        release_tx.send(()).expect("release busy scheduler");
        handle
            .shutdown_with_worker_drain_timeout(Duration::from_millis(50))
            .expect("shutdown retry must join scheduler after busy task exits");
        assert!(
            handle.scheduler_thread.is_none(),
            "successful retry must consume scheduler join handle"
        );
    }

    /// bd-b82q4: a connection-handler panic must surface to the client
    /// as a structured `daemon_handler_panic` envelope rather than a
    /// torn-down connection, and the accept loop must keep running.
    /// `dispatch` has no panicking method today, so this test exercises
    /// the exact `catch_unwind` + `build_panic_response` composition
    /// that `handle_connection` runs, pinning the contract before a
    /// warm-load method that *can* panic lands behind the dispatch
    /// table.
    #[test]
    fn handle_connection_panicking_method_returns_structured_envelope_not_connection_reset() {
        let request_id = "req-panic-001";
        let request = DaemonRequest::new(
            request_id,
            TEST_AGENT_ID,
            METHOD_CONTEXT,
            serde_json::json!({"task": "panic"}),
        );
        let dispatched =
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> DaemonResponse {
                panic!("simulated warm-load bounds-check failure");
            }));
        let response = match dispatched {
            Ok(response) => response,
            Err(payload) => build_panic_response(&request, payload.as_ref()),
        };
        // The client gets a real, parseable envelope — not a reset.
        assert_eq!(response.request_id, request_id);
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        let error = response
            .error
            .as_ref()
            .expect("panic must yield an error envelope");
        assert_eq!(error.code, DAEMON_HANDLER_PANIC_CODE);
        assert!(
            response
                .degraded_codes
                .contains(&DAEMON_HANDLER_PANIC_CODE.to_owned())
        );
        // The wire message is the fixed generic string — the raw panic
        // payload must NOT leak onto the wire.
        assert!(
            error.message.contains("daemon handler panicked"),
            "envelope message should be the generic panic notice, got: {}",
            error.message
        );
        assert!(
            !error.message.contains("bounds-check"),
            "raw panic payload must not leak onto the wire, got: {}",
            error.message
        );
    }

    #[test]
    fn sanitize_panic_message_strips_control_chars_and_truncates() {
        // Control characters (here a CRLF and a TAB) collapse to spaces
        // so a hostile panic payload cannot forge extra log lines.
        let dirty = "line one\r\nline two\tend";
        let clean = sanitize_panic_message(dirty);
        assert_eq!(clean, "line one  line two end");
        assert!(!clean.contains('\n'));
        assert!(!clean.contains('\r'));
        assert!(!clean.contains('\t'));

        // Oversized payloads truncate with an ellipsis marker so the
        // log line stays bounded.
        let huge = "a".repeat(DAEMON_PANIC_LOG_MAX_BYTES * 2);
        let bounded = sanitize_panic_message(&huge);
        assert!(bounded.len() <= DAEMON_PANIC_LOG_MAX_BYTES + 3);
        assert!(bounded.ends_with("..."));
    }

    #[test]
    fn extract_panic_payload_str_handles_str_string_and_other() {
        let str_payload: Box<dyn std::any::Any + Send> = Box::new("static str panic");
        assert_eq!(
            extract_panic_payload_str(str_payload.as_ref()),
            "static str panic"
        );

        let string_payload: Box<dyn std::any::Any + Send> = Box::new(String::from("owned panic"));
        assert_eq!(
            extract_panic_payload_str(string_payload.as_ref()),
            "owned panic"
        );

        let other_payload: Box<dyn std::any::Any + Send> = Box::new(42_u32);
        assert_eq!(
            extract_panic_payload_str(other_payload.as_ref()),
            "<non-string panic payload>"
        );
    }

    fn private_tempdir() -> tempfile::TempDir {
        // Pinned remote verification can set TMPDIR to a long materialized
        // checkout path whose ancestors intentionally fail the daemon's
        // security policy (or whose socket names exceed SUN_LEN). Keep the
        // fixture under the canonical system temp root instead: /tmp is
        // sticky on Unix, while the fresh leaf remains owner-only.
        let temp_root = fs::canonicalize("/tmp").expect("canonical Unix temp root");
        let temp = tempfile::Builder::new()
            .prefix("ee-sb-")
            .tempdir_in(temp_root)
            .expect("private short socket tempdir");
        fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o700))
            .expect("make tempdir private");
        temp
    }

    #[test]
    fn start_server_then_echo_is_disabled_by_default() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-test.sock");
        let mut handle = start_server(&socket_path).expect("server must start");

        let request = DaemonRequest::new(
            "req-roundtrip-001",
            TEST_AGENT_ID,
            METHOD_ECHO,
            serde_json::json!({"ping": "pong"}),
        );
        let response = client_round_trip(handle.socket_path(), &request).expect("round-trip");
        assert_eq!(response.request_id, "req-roundtrip-001");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        assert!(response.result.is_none());
        let error = response.error.as_ref().expect("echo must be disabled");
        assert_eq!(error.code, DAEMON_ECHO_DISABLED_CODE);

        handle.shutdown().expect("shutdown");
        // After shutdown, the socket file must be gone so a subsequent
        // start_server against the same path does not need manual
        // cleanup.
        assert!(!socket_path.exists(), "socket file must be unlinked");
    }

    #[test]
    fn start_server_refuses_non_socket_existing_file() {
        let temp = private_tempdir();
        let path = temp.path().join("not-a-socket");
        fs::write(&path, b"i am a regular file").expect("write");
        let error = start_server(&path).expect_err("must refuse non-socket");
        assert!(matches!(error, DaemonStartError::SocketPathOccupied { .. }));
        // The regular file must still exist after the refused start;
        // the daemon must not silently overwrite arbitrary paths.
        assert!(path.exists());
    }

    #[test]
    fn start_server_refuses_group_or_world_accessible_parent() {
        let temp = tempfile::tempdir().expect("tempdir");
        let parent = temp.path().join("shared-parent");
        fs::create_dir(&parent).expect("create shared parent");
        fs::set_permissions(&parent, fs::Permissions::from_mode(0o755))
            .expect("make parent group/other accessible");
        let socket_path = parent.join("ee-daemon.sock");

        let error = start_server(&socket_path).expect_err("must refuse unsafe parent");

        match error {
            DaemonStartError::InsecureSocketParent { path, reason } => {
                assert_eq!(path, parent);
                assert!(
                    reason.contains("group or other access"),
                    "unsafe-mode reason should name group/other access, got {reason}"
                );
            }
            other => panic!("expected InsecureSocketParent, got {other:?}"),
        }
    }

    #[test]
    fn start_server_refuses_symlink_parent() {
        let temp = tempfile::tempdir().expect("tempdir");
        let real_parent = temp.path().join("real-parent");
        let symlink_parent = temp.path().join("symlink-parent");
        fs::create_dir(&real_parent).expect("create real parent");
        std::os::unix::fs::symlink(&real_parent, &symlink_parent).expect("create parent symlink");
        let socket_path = symlink_parent.join("ee-daemon.sock");

        let error = start_server(&socket_path).expect_err("must refuse symlink parent");

        match error {
            DaemonStartError::InsecureSocketParent { path, reason } => {
                assert_eq!(path, symlink_parent);
                assert!(
                    reason.contains("not a real directory"),
                    "symlink-parent reason should name real-directory requirement, got {reason}"
                );
            }
            other => panic!("expected InsecureSocketParent, got {other:?}"),
        }
    }

    #[test]
    fn start_server_refuses_live_existing_socket() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-live.sock");
        let mut first = start_server(&socket_path).expect("first server must start");

        let error = start_server(&socket_path).expect_err("second start must refuse live socket");
        assert!(
            matches!(error, DaemonStartError::AlreadyRunning { .. }),
            "second start against a live daemon must return AlreadyRunning; got {error:?}",
        );

        let request = context_request(
            "req-live-existing-001",
            TEST_AGENT_ID,
            serde_json::json!({"still": "first"}),
        );
        let response =
            client_round_trip(first.socket_path(), &request).expect("first daemon remains live");
        assert_eq!(response.request_id, "req-live-existing-001");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        let error = response.error.as_ref().expect("context params error");
        assert_eq!(error.code, DAEMON_CONTEXT_PARAMS_INVALID_CODE);

        first.shutdown().expect("shutdown");
    }

    /// Regression test for bd-3j0td. The UDS file must land on disk
    /// with mode 0o600 — owner-rw only — so that no other local UID
    /// can `connect(2)` even when the parent directory's 0o700 gate
    /// is bypassed (custom XDG_RUNTIME_DIR, operator-set TMPDIR, etc.).
    /// A future refactor that loses the chmod step would re-open the
    /// world-connectable hole this pin defends against; sentinel
    /// string `metadata.mode() & 0o777 == 0o600` per the bd-3j0td
    /// proposed-fix bullet 4.
    #[test]
    fn start_server_socket_file_is_owner_rw_only() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-mode.sock");
        let mut handle = start_server(&socket_path).expect("server must start");
        // The chmod runs synchronously inside start_server before it
        // returns, so the mode is observable immediately. Read via
        // `symlink_metadata` so a follow-up refactor that swapped the
        // bound path for a symlink would surface as a test failure.
        let metadata = fs::symlink_metadata(handle.socket_path()).expect("socket metadata");
        let mode = metadata.permissions().mode() & 0o777;
        assert_eq!(
            mode,
            0o600,
            "UDS socket file at {} must be mode 0o600 (owner-rw only) per bd-3j0td; \
             observed 0o{mode:o}. World-connectable sockets are the cross-tenant \
             exfil attack surface the chmod step defends.",
            handle.socket_path().display()
        );
        handle.shutdown().expect("shutdown");
    }

    /// Regression test for bd-3j0td peer-credential gate. A second
    /// connection from the same UID round-trips (the gate compares
    /// effective UIDs; both peer and daemon are this test process).
    /// The same-UID path landing here means the negative path —
    /// peer_uid != own — can only fire on a real cross-UID connection,
    /// which the test harness cannot create without elevated
    /// privileges; the structural pin is that the gate IS in the
    /// dispatch path and does not refuse the legitimate same-UID
    /// caller.
    #[test]
    fn handle_connection_admits_same_uid_peer() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-peer.sock");
        let mut handle = start_server(&socket_path).expect("server must start");

        let request = context_request(
            "req-peer-001",
            TEST_AGENT_ID,
            serde_json::json!({"peer": "self"}),
        );
        let response = client_round_trip(handle.socket_path(), &request).expect("round-trip");
        assert_eq!(response.request_id, "req-peer-001");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        let error = response
            .error
            .as_ref()
            .expect("same-UID peer reaches dispatch");
        assert_eq!(error.code, DAEMON_CONTEXT_PARAMS_INVALID_CODE);

        handle.shutdown().expect("shutdown");
    }

    #[test]
    fn start_server_for_workspace_rejects_same_uid_context_for_wrong_workspace() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-workspace-auth.sock");
        let mut handle = start_server_for_workspace(&socket_path, TEST_WORKSPACE_ID)
            .expect("workspace-bound server must start");

        let mut request = context_request(
            "req-peer-wrong-workspace-001",
            TEST_AGENT_ID,
            serde_json::json!({"peer": "self"}),
        );
        request.workspace_id = Some("workspace-other".to_owned());
        let response = client_round_trip(handle.socket_path(), &request).expect("round-trip");
        assert_eq!(response.request_id, "req-peer-wrong-workspace-001");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        let error = response
            .error
            .as_ref()
            .expect("wrong workspace reaches method authorization");
        assert_eq!(error.code, DAEMON_METHOD_UNAUTHORIZED_CODE);
        assert!(
            response
                .degraded_codes
                .contains(&DAEMON_METHOD_UNAUTHORIZED_CODE.to_owned())
        );

        handle.shutdown().expect("shutdown");
    }

    /// bd-3ik2d: a stale socket left by a crashed prior daemon must be
    /// replaced atomically. The fix binds a temp path and `rename(2)`s
    /// it over the stale socket — there is no `remove_file` step that a
    /// racing attacker could exploit (the former TOCTOU window). Here we
    /// leave a dead socket file behind (bind, then drop the listener
    /// without unlinking) and assert the next `start_server` publishes a
    /// fresh, live, 0o600 socket over it.
    #[test]
    fn start_server_replaces_stale_socket_atomically() {
        use std::os::unix::fs::FileTypeExt;

        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-stale.sock");

        // Simulate a crashed daemon: bind then drop the listener WITHOUT
        // unlinking, leaving a dead socket file on disk.
        {
            let stale = UnixListener::bind(&socket_path).expect("stale bind");
            drop(stale);
        }
        assert!(
            fs::symlink_metadata(&socket_path)
                .expect("stale socket must remain on disk")
                .file_type()
                .is_socket(),
            "precondition: a stale socket file occupies the path",
        );

        let mut handle = start_server(&socket_path).expect("server must replace stale socket");

        let metadata = fs::symlink_metadata(handle.socket_path()).expect("socket metadata");
        assert!(
            metadata.file_type().is_socket(),
            "published path must be a socket"
        );
        assert_eq!(
            metadata.permissions().mode() & 0o777,
            0o600,
            "replaced socket must be 0o600 (bd-3j0td invariant preserved across rename)",
        );
        let request = context_request(
            "req-stale-001",
            TEST_AGENT_ID,
            serde_json::json!({"ping": 1}),
        );
        let response = client_round_trip(handle.socket_path(), &request).expect("round-trip");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        let error = response.error.as_ref().expect("fresh socket dispatches");
        assert_eq!(error.code, DAEMON_CONTEXT_PARAMS_INVALID_CODE);

        handle.shutdown().expect("shutdown");
    }

    /// bd-3ik2d + bd-14dmn: two concurrent `start_server` calls on the
    /// same path must never leave the canonical path in a corrupt state
    /// or split-brain two live daemons. The publish lock serializes the
    /// inspect → bind-temp → chmod → rename window, then the follower
    /// probes the now-live canonical socket and refuses with
    /// `AlreadyRunning`.
    #[test]
    fn start_server_concurrent_binds_no_toctou() {
        use std::os::unix::fs::FileTypeExt;
        use std::sync::Barrier;

        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-race.sock");

        let barrier = Arc::new(Barrier::new(2));
        let threads: Vec<_> = (0..2)
            .map(|_| {
                let path = socket_path.clone();
                let barrier = Arc::clone(&barrier);
                thread::spawn(move || {
                    barrier.wait();
                    start_server(&path)
                })
            })
            .collect();

        let results: Vec<_> = threads
            .into_iter()
            .map(|t| t.join().expect("bind thread must not panic"))
            .collect();

        // Exactly one daemon owns the canonical socket. The follower
        // must not replace it and create a split-brain listener.
        let ok_count = results.iter().filter(|r| r.is_ok()).count();
        let already_running_count = results
            .iter()
            .filter(|result| matches!(result, Err(DaemonStartError::AlreadyRunning { .. })))
            .count();
        assert_eq!(ok_count, 1, "exactly one concurrent daemon start must win");
        assert_eq!(
            already_running_count, 1,
            "exactly one concurrent daemon start must refuse the live winner",
        );

        // The canonical path always resolves to a live 0o600 socket —
        // never absent, never a regular file, never world-open.
        let metadata = fs::symlink_metadata(&socket_path).expect("canonical path must exist");
        assert!(
            metadata.file_type().is_socket(),
            "canonical path must be a socket after a concurrent-bind race",
        );
        assert_eq!(
            metadata.permissions().mode() & 0o777,
            0o600,
            "canonical socket must be 0o600 after a concurrent-bind race",
        );

        let mut handle = results
            .into_iter()
            .find_map(Result::ok)
            .expect("one daemon start must succeed");
        handle.shutdown().expect("shutdown winning daemon");
    }

    /// bd-2z3e8: shutdown cleanup must never turn a swapped daemon
    /// socket path into an arbitrary-file unlink. The helper is tested
    /// directly so the test can simulate hostile path state without
    /// deadlocking the accept thread's wakeup connection.
    #[test]
    fn guarded_socket_unlink_refuses_regular_file() {
        let temp = tempfile::tempdir().expect("tempdir");
        let path = temp.path().join("not-a-daemon-socket");
        fs::write(&path, b"operator data").expect("write regular file");

        let error = SocketBroker::new(path.clone())
            .remove_owned_socket_file()
            .expect_err("regular file must be refused");

        assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
        assert!(
            path.exists(),
            "regular file must remain on disk after refused daemon cleanup"
        );
    }

    #[test]
    fn guarded_socket_unlink_removes_owned_socket_and_tolerates_absent_path() {
        let temp = tempfile::tempdir().expect("tempdir");
        let socket_path = temp.path().join("owned.sock");
        let listener = UnixListener::bind(&socket_path).expect("bind test socket");
        drop(listener);

        SocketBroker::new(socket_path.clone())
            .remove_owned_socket_file()
            .expect("owned socket cleanup must succeed");
        assert!(
            !socket_path.exists(),
            "owned socket file must be removed by guarded cleanup"
        );

        SocketBroker::new(socket_path.clone())
            .remove_owned_socket_file()
            .expect("absent path is already clean");
    }

    // ------------------------------------------------------------------
    // bd-2yg7d.3: adversarial SocketBroker lifecycle fixtures (ADR 0055).
    // These pin the broker's fail-closed publish/cleanup invariants
    // directly at the SocketBroker API so a refactor cannot silently
    // reopen a prior P0/P1 class without a named test failing.
    // ------------------------------------------------------------------

    /// Sample the canonical socket path in a tight loop until `stop`
    /// flips, recording every observation that violates the ADR 0055
    /// publish invariant: once the canonical path exists it must be a
    /// socket with mode 0o600. The chmod runs on the temp path BEFORE
    /// the atomic `rename(2)`, so correct code can never expose an
    /// insecure intermediate state at the canonical name — any recorded
    /// violation means the temp-bind + chmod + rename mechanism
    /// regressed. `allow_missing` covers fresh publishes where the
    /// canonical path legitimately does not exist until the rename
    /// lands; stale replacement must never let the path go missing.
    fn spawn_canonical_path_invariant_watcher(
        path: PathBuf,
        allow_missing: bool,
        stop: Arc<AtomicBool>,
    ) -> (
        JoinHandle<(u64, Vec<String>)>,
        Arc<std::sync::atomic::AtomicU64>,
    ) {
        use std::os::unix::fs::FileTypeExt;

        let observed_samples = Arc::new(std::sync::atomic::AtomicU64::new(0));
        let watcher_samples = Arc::clone(&observed_samples);
        let watcher = thread::spawn(move || {
            let mut samples = 0_u64;
            let mut violations = Vec::new();
            while !stop.load(Ordering::Acquire) {
                samples += 1;
                let observed = match fs::symlink_metadata(&path) {
                    Ok(metadata) => {
                        if metadata.file_type().is_socket() {
                            let mode = metadata.permissions().mode() & 0o777;
                            (mode != 0o600).then(|| {
                                format!(
                                    "sample {samples}: canonical socket observed with mode 0o{mode:o}"
                                )
                            })
                        } else {
                            Some(format!(
                                "sample {samples}: canonical path exists as a non-socket"
                            ))
                        }
                    }
                    Err(error) if error.kind() == io::ErrorKind::NotFound => (!allow_missing)
                        .then(|| format!("sample {samples}: canonical path observed missing")),
                    Err(error) => Some(format!("sample {samples}: stat failed: {error}")),
                };
                if let Some(violation) = observed
                    && violations.len() < 16
                {
                    violations.push(violation);
                }
                watcher_samples.store(samples, Ordering::Release);
                thread::yield_now();
            }
            (samples, violations)
        });
        (watcher, observed_samples)
    }

    fn wait_for_canonical_path_watcher_sample(
        observed_samples: &std::sync::atomic::AtomicU64,
        previous_samples: u64,
    ) -> u64 {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            let samples = observed_samples.load(Ordering::Acquire);
            if samples > previous_samples {
                return samples;
            }
            assert!(
                Instant::now() < deadline,
                "canonical-path watcher must make bounded observation progress",
            );
            thread::yield_now();
        }
    }

    /// Accept the single pending connection on `listener` without
    /// risking an unbounded blocking `accept()` if the publish
    /// mechanism regressed. UDS `connect(2)` only succeeds once the
    /// connection is queued on the listener, so the bounded retry is a
    /// formality — `WouldBlock` persisting for the full deadline means
    /// the canonical path no longer routes to this listener.
    fn accept_pending_connection(listener: &UnixListener) -> UnixStream {
        listener
            .set_nonblocking(true)
            .expect("listener nonblocking");
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            match listener.accept() {
                Ok((stream, _)) => return stream,
                Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
                    assert!(
                        Instant::now() < deadline,
                        "the published listener must receive the connection made against the \
                         canonical socket path (ADR 0055: the atomic rename must publish THIS \
                         listener, not leave a different socket at the canonical name)",
                    );
                    thread::yield_now();
                }
                Err(error) => panic!("accept on published listener failed: {error}"),
            }
        }
    }

    /// bd-2yg7d.3 (ADR 0055: never publish in a shared parent). The
    /// broker must refuse every parent mode that grants group or other
    /// access — including a /tmp-style 0o1777 sticky directory — and
    /// the refusal must fire BEFORE the lock file or temp socket is
    /// created, so the hostile directory never receives an ee artifact
    /// another uid could tamper with.
    #[test]
    fn socket_broker_publish_refuses_shared_parent_without_artifacts() {
        for mode in [0o777_u32, 0o1777, 0o770, 0o707, 0o750] {
            let temp = tempfile::tempdir().expect("tempdir");
            let parent = temp.path().join("shared-parent");
            fs::create_dir(&parent).expect("create shared parent");
            fs::set_permissions(&parent, fs::Permissions::from_mode(mode))
                .expect("loosen parent mode");

            let broker = SocketBroker::new(parent.join("ee-daemon.sock"));
            let error = broker.publish_listener().expect_err(
                "SocketBroker must refuse to publish into a group/other-accessible parent \
                 (ADR 0055 invariant: never publish in a shared parent)",
            );
            match error {
                DaemonStartError::InsecureSocketParent { path, reason } => {
                    assert_eq!(
                        path, parent,
                        "insecure-parent refusal must name the offending parent directory",
                    );
                    assert!(
                        reason.contains("group or other access"),
                        "refusal reason for parent mode 0o{mode:o} must name the group/other \
                         access grant; got {reason}",
                    );
                }
                other => panic!(
                    "parent mode 0o{mode:o} must be refused as InsecureSocketParent (ADR 0055: \
                     never publish in a shared parent); got {other:?}"
                ),
            }

            let leftovers: Vec<PathBuf> = fs::read_dir(&parent)
                .expect("read refused parent")
                .map(|entry| entry.expect("dir entry").path())
                .collect();
            assert!(
                leftovers.is_empty(),
                "refusing an insecure parent (mode 0o{mode:o}) must not leave lock/temp \
                 artifacts inside it; found {leftovers:?}",
            );
        }
    }

    #[test]
    fn socket_broker_publish_refuses_nonsticky_writable_ancestor() {
        let temp = private_tempdir();
        let shared_ancestor = temp.path().join("shared-ancestor");
        let private_parent = shared_ancestor.join("private-child");
        fs::create_dir_all(&private_parent).expect("create nested socket parent");
        fs::set_permissions(&shared_ancestor, fs::Permissions::from_mode(0o777))
            .expect("make ancestor replaceable by another uid");
        fs::set_permissions(&private_parent, fs::Permissions::from_mode(0o700))
            .expect("keep immediate parent private");

        let socket_path = private_parent.join("ee-daemon.sock");
        let error = SocketBroker::new(socket_path.clone())
            .publish_listener()
            .expect_err("a private leaf under a replaceable ancestor must be refused");
        match error {
            DaemonStartError::InsecureSocketParent { path, reason } => {
                assert_eq!(path, shared_ancestor);
                assert!(
                    reason.contains("non-owner rename without sticky protection"),
                    "ancestor refusal must explain the rename risk; got {reason}",
                );
            }
            other => panic!("writable ancestor must be an insecure-parent error; got {other:?}"),
        }
        assert!(
            !socket_path.exists(),
            "ancestor refusal must happen before the canonical socket is created",
        );
        assert!(
            !private_parent.join("ee-daemon.sock.start.lock").exists(),
            "ancestor refusal must happen before the publish lock is created",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: never overwrite a non-socket path). A
    /// regular file or directory squatting the canonical socket path
    /// must be refused as `SocketPathOccupied` and left untouched —
    /// publish must never repossess a path the operator pointed at
    /// real data.
    #[test]
    fn socket_broker_publish_refuses_non_socket_canonical_path() {
        // Regular file at the canonical path.
        {
            let temp = private_tempdir();
            let socket_path = temp.path().join("ee-daemon-occupied.sock");
            fs::write(&socket_path, b"operator data").expect("write squatting file");

            let error = SocketBroker::new(socket_path.clone())
                .publish_listener()
                .expect_err(
                    "a regular file at the canonical path must refuse publish (ADR 0055: never \
                     overwrite a non-socket path)",
                );
            match error {
                DaemonStartError::SocketPathOccupied { path } => assert_eq!(
                    path, socket_path,
                    "regular-file refusal must name the canonical path",
                ),
                other => panic!(
                    "a regular file at the canonical path must be SocketPathOccupied (ADR 0055: \
                     never overwrite a non-socket path); got {other:?}"
                ),
            }
            assert_eq!(
                fs::read(&socket_path).expect("squatting file must survive"),
                b"operator data",
                "refused publish must leave the squatting regular file byte-identical",
            );
        }

        // Directory at the canonical path.
        {
            let temp = private_tempdir();
            let socket_path = temp.path().join("ee-daemon-occupied-dir.sock");
            fs::create_dir(&socket_path).expect("create squatting directory");

            let error = SocketBroker::new(socket_path.clone())
                .publish_listener()
                .expect_err(
                    "a directory at the canonical path must refuse publish (ADR 0055: never \
                     overwrite a non-socket path)",
                );
            match error {
                DaemonStartError::SocketPathOccupied { path } => assert_eq!(
                    path, socket_path,
                    "directory refusal must name the canonical path",
                ),
                other => panic!(
                    "a directory at the canonical path must be SocketPathOccupied (ADR 0055: \
                     never overwrite a non-socket path); got {other:?}"
                ),
            }
            assert!(
                socket_path.is_dir(),
                "refused publish must leave the squatting directory in place",
            );
        }
    }

    /// bd-2yg7d.3 (ADR 0055: never overwrite a non-socket path). The
    /// occupancy check must classify the canonical path with
    /// `symlink_metadata` — the link itself, never its target — so a
    /// planted symlink cannot launder a "this is my stale socket"
    /// belief through whatever it points at.
    #[test]
    fn socket_broker_publish_refuses_symlink_at_canonical_path() {
        let temp = private_tempdir();
        let target = temp.path().join("symlink-target");
        fs::write(&target, b"victim bytes").expect("write symlink target");
        let socket_path = temp.path().join("ee-daemon-link.sock");
        std::os::unix::fs::symlink(&target, &socket_path).expect("plant symlink at canonical path");

        let error = SocketBroker::new(socket_path.clone())
            .publish_listener()
            .expect_err(
                "a symlink at the canonical path must refuse publish without following it \
                 (ADR 0055: never overwrite a non-socket path)",
            );
        assert!(
            matches!(error, DaemonStartError::SocketPathOccupied { .. }),
            "symlink refusal must be SocketPathOccupied (classified via symlink_metadata, never \
             the target); got {error:?}",
        );
        assert!(
            fs::symlink_metadata(&socket_path)
                .expect("symlink must survive refused publish")
                .file_type()
                .is_symlink(),
            "the planted symlink must remain at the canonical path after refusal",
        );
        assert_eq!(
            fs::read(&target).expect("symlink target must survive"),
            b"victim bytes",
            "refused publish must not write through or replace the symlink target",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: stale socket replacement is temp-bind +
    /// atomic rename). Publishing over a dead socket left by a crashed
    /// daemon must succeed, must route the canonical path to the NEW
    /// listener, must consume the temp-bind artifact, and — because the
    /// replacement is a single `rename(2)` — must never expose a window
    /// where the canonical path is missing, a non-socket, or
    /// group/other-accessible.
    #[test]
    fn socket_broker_replaces_stale_socket_with_no_observable_gap() {
        use std::os::unix::fs::FileTypeExt;

        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-stale-gap.sock");
        {
            let stale = UnixListener::bind(&socket_path).expect("stale bind");
            // Pin the stale socket to 0o600 so every watcher sample has
            // exactly one expectation: an existing 0o600 socket, before,
            // during, and after the replacement.
            fs::set_permissions(&socket_path, fs::Permissions::from_mode(0o600))
                .expect("chmod stale socket");
            drop(stale);
        }

        let stop = Arc::new(AtomicBool::new(false));
        let (watcher, observed_samples) =
            spawn_canonical_path_invariant_watcher(socket_path.clone(), false, Arc::clone(&stop));
        wait_for_canonical_path_watcher_sample(&observed_samples, 0);

        let broker = SocketBroker::new(socket_path.clone());
        let (listener, _publish_lock) = broker.publish_listener().expect(
            "publish over a dead stale socket must succeed via temp-bind + atomic rename \
             (ADR 0055 stale replacement)",
        );
        let after_publish = observed_samples.load(Ordering::Acquire);
        wait_for_canonical_path_watcher_sample(&observed_samples, after_publish);

        stop.store(true, Ordering::Release);
        let (samples, violations) = watcher.join().expect("watcher thread must not panic");
        assert!(
            samples > 0,
            "watcher must observe the canonical path at least once"
        );
        assert!(
            violations.is_empty(),
            "stale replacement must be atomic: no sample may show the canonical path missing, \
             non-socket, or insecure (ADR 0055 temp-bind + rename); observed {violations:?}",
        );

        // The canonical path must now route to the fresh listener; the
        // stale socket was dead, so a successful connect + accept proves
        // the rename published THIS listener.
        let _client = UnixStream::connect(&socket_path)
            .expect("replaced canonical socket must accept connections");
        let _server_side = accept_pending_connection(&listener);

        let metadata = fs::symlink_metadata(&socket_path).expect("published socket metadata");
        assert!(
            metadata.file_type().is_socket(),
            "canonical path must be a socket after stale replacement",
        );
        assert_eq!(
            metadata.permissions().mode() & 0o777,
            0o600,
            "replaced socket must be mode 0o600 (chmod-on-temp survives the rename; ADR 0055)",
        );

        let residue: Vec<String> = fs::read_dir(temp.path())
            .expect("read socket parent")
            .map(|entry| {
                entry
                    .expect("dir entry")
                    .file_name()
                    .to_string_lossy()
                    .into_owned()
            })
            .filter(|name| name.contains(".tmp."))
            .collect();
        assert!(
            residue.is_empty(),
            "the temp-bind artifact must be consumed by the atomic rename, leaving no residue \
             next to the canonical socket (ADR 0055); found {residue:?}",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: chmod-before-publish). On a fresh path the
    /// socket must already carry mode 0o600 by the time the canonical
    /// name exists at all: `UnixListener::bind` honours the umask
    /// (typically yielding 0o755), so a refactor that renamed first and
    /// chmodded second would expose a world-connectable socket at the
    /// canonical path. The watcher pins that no sample ever sees the
    /// canonical path as anything but absent or a 0o600 socket; the
    /// fixture lives in a fresh private tempdir, never global /tmp.
    #[test]
    fn socket_broker_fresh_publish_chmods_socket_before_canonical_publish() {
        use std::os::unix::fs::FileTypeExt;

        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-fresh-chmod.sock");

        let stop = Arc::new(AtomicBool::new(false));
        let (watcher, observed_samples) =
            spawn_canonical_path_invariant_watcher(socket_path.clone(), true, Arc::clone(&stop));
        wait_for_canonical_path_watcher_sample(&observed_samples, 0);

        let broker = SocketBroker::new(socket_path.clone());
        let (listener, _publish_lock) = broker
            .publish_listener()
            .expect("fresh publish in a private parent must succeed");
        let after_publish = observed_samples.load(Ordering::Acquire);
        wait_for_canonical_path_watcher_sample(&observed_samples, after_publish);

        // The instant publish_listener returns, the canonical path is
        // connectable — so it must ALREADY be 0o600.
        let metadata = fs::symlink_metadata(&socket_path).expect("published socket metadata");
        assert!(
            metadata.file_type().is_socket(),
            "canonical path must be a socket once published",
        );
        assert_eq!(
            metadata.permissions().mode() & 0o777,
            0o600,
            "the socket must receive mode 0o600 BEFORE it becomes connectable at the canonical \
             path (ADR 0055 chmod-before-publish; bd-3j0td)",
        );
        let _client = UnixStream::connect(&socket_path)
            .expect("published socket must be connectable by the owning uid");
        let _server_side = accept_pending_connection(&listener);

        stop.store(true, Ordering::Release);
        let (samples, violations) = watcher.join().expect("watcher thread must not panic");
        assert!(
            samples > 0,
            "watcher must sample the canonical path at least once"
        );
        assert!(
            violations.is_empty(),
            "during fresh publish the canonical path may only ever be observed absent or as a \
             0o600 socket — the chmod must precede the atomic rename (ADR 0055 \
             chmod-before-publish); observed {violations:?}",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: temp-bind + atomic rename mechanism). The
    /// per-attempt temp path must live in the SAME parent directory as
    /// the canonical socket (same-directory `rename(2)` is what makes
    /// the publish atomic — a cross-directory temp could land on a
    /// different filesystem and would inherit a different privacy
    /// boundary), must extend the canonical file name, must embed the
    /// a valid UUID v7, and must be unique per attempt so concurrent and
    /// post-crash publishes never collide.
    #[test]
    fn socket_broker_temp_bind_path_is_parent_local_and_unique() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-temp-name.sock");
        let broker = SocketBroker::new(socket_path.clone());

        let first = broker.temp_bind_path();
        let second = broker.temp_bind_path();

        assert_ne!(
            first, second,
            "temp bind paths must be unique per attempt so concurrent publishes never collide \
             and each rename is a clean atomic publish (ADR 0055)",
        );
        for tmp_path in [&first, &second] {
            assert_eq!(
                tmp_path.parent(),
                socket_path.parent(),
                "temp bind path must stay inside the validated private parent of the canonical \
                 socket so the rename(2) publish is same-directory atomic (ADR 0055)",
            );
            let name = tmp_path
                .file_name()
                .and_then(|name| name.to_str())
                .expect("temp bind file name must be valid UTF-8 in this fixture");
            assert!(
                name.starts_with("ee-daemon-temp-name.sock.tmp."),
                "temp bind name must extend the canonical socket name with a .tmp. suffix; \
                 got {name}",
            );
            let uuid_suffix = name
                .strip_prefix("ee-daemon-temp-name.sock.tmp.")
                .expect("prefix was checked above");
            let parsed = uuid::Uuid::parse_str(uuid_suffix)
                .expect("temp bind suffix must be a syntactically valid UUID");
            assert_eq!(
                parsed.get_version_num(),
                7,
                "temp bind suffix must be UUID v7 for time-ordered uniqueness; got {name}",
            );
        }
    }

    #[test]
    fn socket_broker_temp_collision_never_unlinks_planted_regular_file() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon.sock");
        let broker = SocketBroker::new(socket_path);
        let planted_path = broker.temp_bind_path();
        fs::write(&planted_path, b"operator bytes").expect("plant temp-path regular file");

        broker
            .bind_secured_temp_listener_at(&planted_path)
            .expect_err("bind must refuse a pre-existing temp path");
        assert_eq!(
            fs::read(&planted_path).expect("planted path must remain readable"),
            b"operator bytes",
            "temp collision handling must not unlink or overwrite a non-socket path",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: publish-lock path properties). The publish
    /// lock must be derived as `<socket>.start.lock` inside the same
    /// validated-private parent — a lock outside the 0o700 boundary
    /// could be squatted or flocked by another uid to wedge or race
    /// daemon starts — and the lock file itself must be a regular file
    /// with no group/other access, owned by the publishing euid.
    #[test]
    fn socket_broker_publish_lock_is_owner_only_sibling_file() {
        use std::os::unix::fs::MetadataExt;

        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-lock-props.sock");
        let broker = SocketBroker::new(socket_path.clone());

        let lock_path = broker.socket_publish_lock_path();
        assert_eq!(
            lock_path.parent(),
            socket_path.parent(),
            "publish lock must live inside the same validated private parent as the canonical \
             socket (ADR 0055 publish-lock properties)",
        );
        assert_eq!(
            lock_path.file_name().and_then(|name| name.to_str()),
            Some("ee-daemon-lock-props.sock.start.lock"),
            "publish lock name must be '<socket file name>.start.lock'",
        );

        let (_listener, publish_lock) = broker
            .publish_listener()
            .expect("publish in a private parent must succeed");
        let metadata = fs::symlink_metadata(&lock_path)
            .expect("publish lock file must exist after a successful publish");
        assert!(
            metadata.file_type().is_file(),
            "publish lock must be a regular file (O_NOFOLLOW open), not a socket/symlink/dir",
        );
        let mode = metadata.permissions().mode() & 0o777;
        assert_eq!(
            mode & 0o077,
            0,
            "publish lock mode 0o{mode:o} must grant no group/other access (created 0o600; \
             ADR 0055 publish-lock properties)",
        );
        assert_eq!(
            metadata.uid(),
            current_euid(),
            "publish lock must be owned by the publishing euid",
        );
        drop(publish_lock);
    }

    /// bd-2yg7d.3 (ADR 0055: publish-lock path properties). The lock
    /// file opens with `O_NOFOLLOW`: a symlink planted at the derived
    /// lock path must abort the publish (no socket appears) and must
    /// not open, create, or truncate whatever the link points at.
    #[test]
    fn socket_broker_publish_refuses_symlinked_lock_path() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-lock-link.sock");
        let broker = SocketBroker::new(socket_path.clone());

        let target = temp.path().join("lock-symlink-target");
        fs::write(&target, b"victim bytes").expect("write lock symlink target");
        std::os::unix::fs::symlink(&target, broker.socket_publish_lock_path())
            .expect("plant symlink at the publish-lock path");

        let error = broker.publish_listener().expect_err(
            "a symlinked publish-lock path must abort the publish (O_NOFOLLOW; ADR 0055 \
             publish-lock properties)",
        );
        assert!(
            matches!(error, DaemonStartError::Bind { .. }),
            "symlinked-lock refusal surfaces as the Bind error wrapping the O_NOFOLLOW open \
             failure; got {error:?}",
        );
        assert_eq!(
            fs::read(&target).expect("lock symlink target must survive"),
            b"victim bytes",
            "the publish-lock open must not write through the planted symlink",
        );
        assert!(
            !socket_path.exists(),
            "no socket may be published when the publish lock cannot be acquired safely",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: never delete a non-socket during cleanup).
    /// A directory at the daemon socket path must be refused by
    /// `remove_owned_socket_file` and left on disk, exactly like the
    /// regular-file case pinned above.
    #[test]
    fn guarded_socket_unlink_refuses_directory() {
        let temp = tempfile::tempdir().expect("tempdir");
        let path = temp.path().join("daemon-sock-dir");
        fs::create_dir(&path).expect("create directory at socket path");

        let error = SocketBroker::new(path.clone())
            .remove_owned_socket_file()
            .expect_err(
                "cleanup must refuse a directory at the socket path (ADR 0055: never delete a \
                 non-socket during cleanup)",
            );
        assert_eq!(
            error.kind(),
            io::ErrorKind::InvalidInput,
            "non-socket cleanup refusal must be InvalidInput naming the path; got {error}",
        );
        assert!(
            path.is_dir(),
            "the directory must remain on disk after refused cleanup",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: never delete a non-socket during cleanup).
    /// Cleanup classifies via `symlink_metadata`: a symlink pointing at
    /// a REAL socket is still a symlink, and removing it — or worse,
    /// following it — would let a planted link turn shutdown into an
    /// arbitrary unlink. Both the link and the socket behind it must
    /// survive.
    #[test]
    fn guarded_socket_unlink_refuses_symlink_to_socket() {
        use std::os::unix::fs::FileTypeExt;

        let temp = private_tempdir();
        let real_socket = temp.path().join("real-daemon.sock");
        let _listener = UnixListener::bind(&real_socket).expect("bind real socket");
        let link = temp.path().join("link-to-daemon.sock");
        std::os::unix::fs::symlink(&real_socket, &link).expect("plant symlink to socket");

        let error = SocketBroker::new(link.clone())
            .remove_owned_socket_file()
            .expect_err(
                "cleanup must refuse a symlink even when it points at a real socket (ADR 0055: \
                 never delete a non-socket during cleanup)",
            );
        assert_eq!(
            error.kind(),
            io::ErrorKind::InvalidInput,
            "symlink cleanup refusal must be InvalidInput; got {error}",
        );
        assert!(
            fs::symlink_metadata(&link)
                .expect("symlink must survive refused cleanup")
                .file_type()
                .is_symlink(),
            "the planted symlink must remain after refused cleanup",
        );
        assert!(
            fs::symlink_metadata(&real_socket)
                .expect("real socket must survive refused cleanup")
                .file_type()
                .is_socket(),
            "the socket behind the planted symlink must remain after refused cleanup",
        );
    }

    /// bd-2yg7d.3 (ADR 0055: never delete an other-owned file during
    /// cleanup). Creating a socket owned by a foreign uid requires
    /// euid 0, so this pin only exercises on a root runner (e.g. a
    /// container CI job) and skips gracefully elsewhere — mirroring how
    /// platform-gated coverage in this module degrades rather than
    /// asserting vacuously.
    #[test]
    fn guarded_socket_unlink_refuses_other_uid_socket_when_root() {
        if current_euid() != 0 {
            eprintln!(
                "skipping guarded_socket_unlink_refuses_other_uid_socket_when_root: \
                 chown to a foreign uid requires euid 0"
            );
            return;
        }

        let temp = tempfile::tempdir().expect("tempdir");
        let socket_path = temp.path().join("foreign-owned.sock");
        let listener = UnixListener::bind(&socket_path).expect("bind fixture socket");
        drop(listener);
        // uid 1 ("daemon" on Linux and macOS) is a stable foreign uid.
        std::os::unix::fs::chown(&socket_path, Some(1), None)
            .expect("chown fixture socket to a foreign uid");

        let error = SocketBroker::new(socket_path.clone())
            .remove_owned_socket_file()
            .expect_err(
                "cleanup must refuse a socket owned by another uid (ADR 0055: never delete an \
                 other-owned file during cleanup)",
            );
        assert_eq!(
            error.kind(),
            io::ErrorKind::PermissionDenied,
            "other-uid cleanup refusal must be PermissionDenied; got {error}",
        );
        assert!(
            error.to_string().contains("owned by uid"),
            "other-uid refusal must name the owning uid; got {error}",
        );
        assert!(
            socket_path.exists(),
            "the foreign-owned socket must remain on disk after refused cleanup",
        );
    }

    struct FailingConnectionWorkerSpawner;

    #[derive(Default)]
    struct CapturingDaemonMetricsCollector {
        worker_spawn_failures: Mutex<Vec<io::ErrorKind>>,
    }

    impl super::super::metrics::DaemonMetricsCollector for CapturingDaemonMetricsCollector {
        fn record_dispatch(
            &self,
            _method: &str,
            _outcome: super::super::metrics::DispatchOutcome,
            _elapsed: Duration,
        ) {
        }

        fn record_worker_spawn_failure(&self, kind: io::ErrorKind) {
            self.worker_spawn_failures
                .lock()
                .expect("metrics capture mutex must not be poisoned")
                .push(kind);
        }
    }

    impl ConnectionWorkerSpawner for FailingConnectionWorkerSpawner {
        fn spawn_connection_worker(
            &self,
            stream: UnixStream,
            shutdown: Arc<AtomicBool>,
            dispatch_policy: Arc<DaemonDispatchPolicy>,
            metrics: Arc<dyn super::super::metrics::DaemonMetricsCollector>,
            permit: InflightPermit,
        ) -> io::Result<JoinHandle<()>> {
            drop(stream);
            drop(shutdown);
            drop(dispatch_policy);
            drop(metrics);
            drop(permit);
            Err(io::Error::other("simulated pthread_create failure"))
        }
    }

    /// Regression test for bd-poxok. A real `thread::Builder::spawn`
    /// failure is host-resource dependent, so the accept loop exposes a
    /// test-only spawn seam and pins the observable contract: the client
    /// receives a framed `daemon_overloaded` response instead of a bare
    /// connection reset.
    #[test]
    fn accept_loop_spawn_failure_returns_overloaded_envelope() {
        let temp = tempfile::tempdir().expect("tempdir");
        let socket_path = temp.path().join("ee-daemon-spawn-fails.sock");
        let listener = UnixListener::bind(&socket_path).expect("bind daemon socket");
        let shutdown = Arc::new(AtomicBool::new(false));
        let pool = InflightPool::new(1);
        let runner_shutdown = Arc::clone(&shutdown);
        let runner_pool = Arc::clone(&pool);
        let runner_socket_path = socket_path.clone();
        let metrics = Arc::new(CapturingDaemonMetricsCollector::default());
        let runner_metrics: Arc<dyn super::super::metrics::DaemonMetricsCollector> =
            metrics.clone();

        let runner = thread::spawn(move || {
            run_accept_loop_with_spawner(
                listener,
                runner_socket_path,
                runner_shutdown,
                runner_pool,
                Arc::new(DaemonDispatchPolicy::default()),
                runner_metrics,
                FailingConnectionWorkerSpawner,
            );
        });

        let mut client = UnixStream::connect(&socket_path).expect("client must connect");
        client
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("client read timeout");
        let response = read_framed_daemon_response(&mut client);
        let error = response.error.as_ref().expect("must carry an error");

        assert_eq!(response.request_id, "<overloaded>");
        assert_eq!(response.agent_id, "<unknown>");
        assert_eq!(error.code, DAEMON_OVERLOADED_CODE);
        assert!(
            response
                .degraded_codes
                .contains(&DAEMON_OVERLOADED_CODE.to_owned()),
            "spawn failure envelope must surface daemon_overloaded in degraded[]",
        );

        shutdown.store(true, Ordering::SeqCst);
        let _ = UnixStream::connect(&socket_path);
        runner.join().expect("accept loop thread must not panic");
        let failures = metrics
            .worker_spawn_failures
            .lock()
            .expect("metrics capture mutex must not be poisoned");
        assert_eq!(failures.as_slice(), &[io::ErrorKind::Other]);
    }

    /// Regression test for bd-wj6v9. A second `shutdown()` call (the
    /// explicit-then-`Drop` pattern, or any repeated invocation) must
    /// be a clean `Ok(())` no-op rather than surfacing a misleading
    /// `ENOENT` from re-unlinking an already-removed socket. The
    /// once-guard short-circuits the second pass; the idempotent
    /// `remove_file` (NotFound-tolerant) backstops the case where the
    /// socket vanished out from under the first pass.
    #[test]
    fn shutdown_is_idempotent_across_repeated_calls() {
        let temp = private_tempdir();
        let socket_path = temp.path().join("ee-daemon-idempotent.sock");
        let mut handle = start_server(&socket_path).expect("server must start");

        handle.shutdown().expect("first shutdown must succeed");
        assert!(
            !socket_path.exists(),
            "socket file must be unlinked after the first shutdown"
        );
        // Second explicit call: pre-bd-wj6v9 this re-entered the
        // teardown; it must now be a guarded no-op returning Ok.
        handle
            .shutdown()
            .expect("second shutdown must be an idempotent no-op, not ENOENT");
        // A third call (mirroring the implicit `Drop`-after-explicit
        // path) must also be Ok; `Drop` itself runs at end of scope.
        handle
            .shutdown()
            .expect("third shutdown must also be a no-op");
    }

    /// Regression test for bd-36dp2. A connection accepted while the
    /// daemon is shutting down must receive a framed
    /// `daemon_shutting_down` envelope rather than being dropped
    /// silently (which the client would observe as a bare connection
    /// reset). We exercise the envelope writer directly over a
    /// `UnixStream` pair — deterministic, no race against the accept
    /// loop — and assert the wire frame parses to the expected code.
    #[test]
    fn write_shutting_down_response_emits_framed_daemon_shutting_down_envelope() {
        let (mut server_side, mut client_side) = UnixStream::pair().expect("socketpair");
        write_shutting_down_response(&mut server_side);
        drop(server_side);

        let response = read_framed_daemon_response(&mut client_side);
        let error = response.error.as_ref().expect("must carry an error");
        assert_eq!(error.code, crate::daemon::DAEMON_SHUTTING_DOWN_CODE);
        assert!(
            response
                .degraded_codes
                .contains(&crate::daemon::DAEMON_SHUTTING_DOWN_CODE.to_owned()),
            "shutdown envelope must surface the code in degraded[]"
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn handle_connection_refuses_parsed_request_after_shutdown_latch() {
        use std::io::Write;

        let (mut client_side, server_side) = UnixStream::pair().expect("socketpair");
        client_side
            .set_read_timeout(Some(Duration::from_secs(2)))
            .expect("client read timeout");
        client_side
            .set_write_timeout(Some(Duration::from_secs(2)))
            .expect("client write timeout");
        let shutdown = Arc::new(AtomicBool::new(true));

        let dispatch_policy = Arc::new(DaemonDispatchPolicy::for_workspace(
            "workspace-worker-shutdown",
        ));
        let metrics = Arc::new(super::super::metrics::NoopMetricsCollector);
        let worker = thread::spawn(move || {
            handle_connection(server_side, shutdown, dispatch_policy, metrics)
        });

        let mut request = DaemonRequest::new(
            "req-worker-shutdown",
            TEST_AGENT_ID,
            METHOD_CONTEXT,
            serde_json::json!({"task": "drain worker"}),
        );
        request.workspace_id = Some("workspace-worker-shutdown".to_owned());
        let body = serde_json::to_vec(&request).expect("request must encode");
        let length = u32::try_from(body.len()).expect("body length fits u32");
        client_side
            .write_all(&length.to_be_bytes())
            .expect("write length");
        client_side.write_all(&body).expect("write body");
        client_side.flush().expect("flush request");

        let response = read_framed_daemon_response(&mut client_side);
        worker.join().expect("worker thread must not panic");

        assert_eq!(response.request_id, "req-worker-shutdown");
        assert_eq!(response.agent_id, TEST_AGENT_ID);
        assert_eq!(
            response.workspace_id.as_deref(),
            Some("workspace-worker-shutdown")
        );
        let error = response.error.as_ref().expect("must carry an error");
        assert_eq!(error.code, crate::daemon::DAEMON_SHUTTING_DOWN_CODE);
        assert!(
            response
                .degraded_codes
                .contains(&crate::daemon::DAEMON_SHUTTING_DOWN_CODE.to_owned()),
            "worker shutdown envelope must surface the code in degraded[]"
        );
    }
}