Skip to content

Python SDK

Broker

nautilus.core.broker.Broker

Public Nautilus facade — the sole entry point per design §3.1.

Construct via :meth:from_config for the normal flow; the constructor is kept public for unit tests that wire collaborators directly.

Source code in nautilus/core/broker.py
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
class Broker:
    """Public Nautilus facade — the sole entry point per design §3.1.

    Construct via :meth:`from_config` for the normal flow; the constructor
    is kept public for unit tests that wire collaborators directly.
    """

    def __init__(
        self,
        *,
        config: NautilusConfig,
        registry: SourceRegistry,
        intent_analyzer: IntentAnalyzer | FallbackIntentAnalyzer,
        router: FathomRouter,
        adapters: dict[str, Adapter],
        synthesizer: Synthesizer,
        audit_logger: AuditLogger,
        attestation: AttestationService | None,
        session_store: SessionStore | AsyncSessionStore,
        agent_registry: AgentRegistry | None = None,
        attestation_sink: AttestationSink | None = None,
        key_ring: KeyRing | None = None,
        session_token_ttl_s: int = 3600,
        broker_instance_id: str | None = None,
        base_dir: Path | None = None,
    ) -> None:
        # Everything a request reads out of the config file, in one object so
        # a reload can install a new one with a single assignment. A request
        # pins the generation it started on (``_pinned_generation``) and reads
        # every field through it, so a SIGHUP landing mid-request cannot move
        # the sources, the rules or the per-request limits out from under it.
        self._generation = _ConfigGeneration(
            config=config,
            registry=registry,
            router=router,
            adapters=adapters,
            intent_analyzer=intent_analyzer,
        )
        # Serialises reload swaps against each other. Held on the event loop
        # only, and never across the adapter/router teardown of the outgoing
        # generation, so a second SIGHUP waits for the swap and not for the
        # drain.
        self._reload_lock = asyncio.Lock()
        # Called after a reload installs a new generation. The auth guard reads
        # a credential list the ASGI app copied out of the config at startup,
        # and ``reload`` replaces the config object rather than mutating it, so
        # a rotation the broker adopted would otherwise never reach the door.
        self._reload_listeners: list[Callable[[], None]] = []
        self._last_reload_notes: list[str] = []
        # How long a reload waits in silence for the previous generation to
        # drain before saying so in the log. It bounds the log line, never the
        # wait -- see :meth:`_retire`. Tune it to the slowest source's
        # ``timeout_s`` if reloads routinely go quiet.
        self.reload_drain_timeout_s: float = 60.0
        self._synthesizer = synthesizer
        self._audit_logger = audit_logger
        self._attestation = attestation
        self._session_store = session_store
        # Phase-1 YAML (no ``agents:``) yields an empty registry — preserves
        # NFR-5 backwards compatibility. Threaded into ``FathomRouter.route``
        # per design §2.2; the Phase-2 agent-fact enrichment rules consume it,
        # Phase-1 rules ignore it and materialize ``agent`` from ``context``.
        self._agent_registry: AgentRegistry = agent_registry or AgentRegistry({})
        # Attestation sink default is :class:`NullAttestationSink` so Phase-1
        # YAML without ``attestation.sink`` preserves NFR-5 backwards compat.
        # The token is still signed and returned on ``BrokerResponse``;
        # ``NullAttestationSink`` only skips the store-and-forward hop
        # (AC-14.4).
        self._attestation_sink: AttestationSink = attestation_sink or NullAttestationSink()
        self._closed: bool = False
        # 4.1 -- the sync facade owns one long-lived loop on its own thread.
        # ``asyncio.run`` per call gave every call a fresh loop while adapter
        # clients stayed cached on the adapter, so call #2 used a pool bound
        # to a closed loop and every source landed in ``sources_errored``.
        # Built lazily: an async-only caller never starts a thread.
        self._sync_loop: asyncio.AbstractEventLoop | None = None
        self._sync_thread: threading.Thread | None = None
        # 4.2 -- ``_prepare_adapter`` does check-then-set across an await, so
        # concurrent first-requests to one source each built their own pool
        # and every loser was unreachable and survived aclose(). One lock per
        # source; the set is bounded by the number of configured sources.
        self._connect_locks: dict[str, asyncio.Lock] = {}
        # B3 -- the session ledger is read at routing time and written after
        # adapter fan-out, so concurrent requests on one session all merged
        # onto the same stale snapshot and the last writer won. The lock
        # spans the whole pipeline because routing policy reads the ledger
        # too: merging at write time alone would still route on stale
        # cumulative exposure.
        # ponytail: one entry per session id, never evicted. Swap for a TTL
        # map if session churn becomes a memory problem.
        self._session_locks: dict[str, asyncio.Lock] = {}
        # Tracks which adapter ids have already been ``connect()``-ed so
        # ``arequest`` can lazy-connect on first use and skip on subsequent
        # calls (design §3.5 — adapter lifecycle is owned by the broker).
        self._connected_adapters: set[str] = set()
        # Whether ``setup()`` has run. First use calls it if the caller did not;
        # see ``_ensure_setup``.
        self._setup_done: bool = False
        # When each source's last connect attempt failed, so a source that has
        # gone away is not re-dialled on every request. Without it one dead
        # source charged its whole ``timeout_s`` — 15 s by default — to every
        # request for as long as it stayed away, and every co-queried healthy
        # source waited with it.
        self._connect_failures: dict[str, float] = {}
        self.connect_cooldown_s: float = 30.0
        # Schema baselines live for the life of the broker, and on disk under
        # ``base_dir`` when one is known (``from_config`` passes ``state_dir``,
        # defaulting to the config file's directory). A per-call store would be
        # empty on every call, so every check would read as a first
        # registration and drift could never be detected — neither within a
        # process nor across a restart.
        self._fingerprint_store = SchemaFingerprintStore(root=self._fingerprint_root(base_dir))
        # Adapters quarantined due to major schema drift (AC-21.e, PM Q3 LOCKED).
        # Requests targeting a quarantined adapter surface as ADAPTER_QUARANTINED
        # error records instead of routing to the adapter. Other adapters keep
        # serving normally — quarantine is per-adapter, NOT broker-wide.
        self._quarantined_adapters: set[str] = set()
        # Session-provenance tokens (#18, AC-18.a–g). Active iff a KeyRing is
        # injected — ``from_config`` passes one only when
        # ``session_tokens.enabled: true``, so Phase-1 YAML keeps the token
        # path (and its audit events) entirely off (NFR-5). ``_instance_id``
        # scopes minted tokens to this broker (AC-18.d
        # broker_instance_mismatch); the KeyRing is in-memory, so transports
        # MUST share this ring (via :attr:`key_ring`) for verification to work.
        # It comes from configuration when there is one, because a per-process
        # uuid4 is unshareable: behind a load balancer every token minted by
        # one replica is rejected by the next, however carefully the operator
        # shares the key ring.
        self._instance_id: str = broker_instance_id or str(uuid.uuid4())
        self._key_ring: KeyRing | None = key_ring
        self._session_tokens: SessionTokenService | None = (
            SessionTokenService(
                key_ring=key_ring,
                broker_instance_id=self._instance_id,
                ttl_seconds=session_token_ttl_s,
            )
            if key_ring is not None
            else None
        )

    # ------------------------------------------------------------------
    # The reloadable generation — read through, never assigned directly
    # ------------------------------------------------------------------

    @property
    def _live(self) -> _ConfigGeneration:
        """The generation this call must read: the pinned one, or the current one.

        A request pins at entry and every read inside it resolves here, so a
        reload that lands halfway through cannot change the sources it is
        querying or the rules it was routed by. Calls outside a request --
        ``GET /v1/sources``, the metrics ruleset label, the CLI's read-only
        surfaces -- see the current generation, which is what they are for.
        """
        pinned = _PINNED_GENERATION.get()
        if pinned is not None and pinned[0] is self:
            return pinned[1]
        return self._generation

    # Each of the five reads through :attr:`_live` and writes through to the
    # *current* generation. The setters exist because unit tests wire fakes in
    # by assignment and did so before there was a generation to hold them; they
    # write to the live generation, never to a pinned one, so a fake installed
    # mid-request would be seen by the next request and not by that one.

    @property
    def _config(self) -> NautilusConfig:
        return self._live.config

    @_config.setter
    def _config(self, value: NautilusConfig) -> None:
        self._generation.config = value

    @property
    def _registry(self) -> SourceRegistry:
        return self._live.registry

    @_registry.setter
    def _registry(self, value: SourceRegistry) -> None:
        self._generation.registry = value

    @property
    def _router(self) -> FathomRouter:
        return self._live.router

    @_router.setter
    def _router(self, value: FathomRouter) -> None:
        self._generation.router = value

    @property
    def _adapters(self) -> dict[str, Adapter]:
        return self._live.adapters

    @_adapters.setter
    def _adapters(self, value: dict[str, Adapter]) -> None:
        self._generation.adapters = value

    @property
    def _intent_analyzer(self) -> IntentAnalyzer | FallbackIntentAnalyzer:
        return self._live.intent_analyzer

    @_intent_analyzer.setter
    def _intent_analyzer(self, value: IntentAnalyzer | FallbackIntentAnalyzer) -> None:
        self._generation.intent_analyzer = value

    @contextlib.contextmanager
    def _pinned_generation(self) -> Iterator[None]:
        """Hold this call to the generation that was live when it started."""
        gen = self._generation
        gen.inflight += 1
        token = _PINNED_GENERATION.set((self, gen))
        try:
            yield
        finally:
            _PINNED_GENERATION.reset(token)
            gen.inflight -= 1
            if gen.inflight == 0 and gen.drained is not None:
                gen.drained.set()

    # ------------------------------------------------------------------
    # Construction
    # ------------------------------------------------------------------

    @classmethod
    async def afrom_config(cls, path: str | Path) -> Broker:
        """Async counterpart of :meth:`from_config` (README "What Ships Today").

        Construction is entirely blocking -- reading the YAML, reading the
        attestation key, compiling the rule tree into a CLIPS environment --
        so it runs on a worker thread rather than stalling the caller's event
        loop. Adapters are still connected lazily by the first request, exactly
        as with :meth:`from_config`.
        """
        return await asyncio.to_thread(cls.from_config, path)

    @classmethod
    def from_config(cls, path: str | Path) -> Broker:
        """Build a fully-wired :class:`Broker` from a ``nautilus.yaml`` path.

        Order of operations mirrors design §15 build sequence:
        1. Load + validate config.
        2. Build :class:`SourceRegistry`.
        3. Build :class:`PatternMatchingIntentAnalyzer` from the keyword map
           auto-generated from each source's ``data_types`` (#24), overlaid
           with explicit ``analysis.keyword_map`` entries.
        4. Build :class:`FathomRouter` against the built-in rules tree +
           any configured user rules.
        5. Build per-source :class:`Adapter` instances (NOT connected —
           ``connect()`` is async; first ``arequest`` is responsible).
        6. Build :class:`AuditLogger` over ``FileSink(audit.path)``.
        7. Build :class:`AttestationService` (auto-generate unless
           ``private_key_path``; return ``None`` if disabled).
        8. Build :class:`InMemorySessionStore`.

        Raises :class:`ConfigError` on bad YAML / missing env vars and
        :class:`PolicyEngineError` on engine construction failure.
        """
        config = load_config(path)

        registry = SourceRegistry(config.sources)
        agent_registry = AgentRegistry(config.agents)
        if not config.agents:
            # An empty registry makes the router read clearance, compartments
            # and purpose from ``context`` — i.e. from the caller. That is the
            # bootstrap default and getting-started teaches exactly this shape,
            # but it is the single largest posture difference between a demo
            # and a deployment, so it is said out loud rather than inferred.
            log.warning(
                "No 'agents:' are declared in %r, so every request declares its own "
                "clearance, compartments and purpose and the broker enforces them "
                "against the sources it knows. Declare agents to turn enforcement on.",
                str(path),
            )

        # Auto-generate base intent vocabulary from each source's declared
        # ``data_types`` (#24); explicit ``analysis.keyword_map`` entries
        # overlay and win on key collision.
        keyword_map = build_keyword_map(registry.sources, config.analysis.keyword_map)
        pattern_analyzer = PatternMatchingIntentAnalyzer(
            keyword_map=keyword_map,
        )
        # The configured vocabulary the router will intersect the analyzer's
        # answer against, handed to the LLM provider so the model is asked to
        # pick from it rather than to guess at it (§4.17).
        known_data_types = sorted({dt for src in registry.sources for dt in src.data_types})
        intent_analyzer = cls._build_intent_analyzer(config, pattern_analyzer, known_data_types)

        base_dir = Path(path).parent
        attestation = cls._build_attestation(config, base_dir)
        attestation_sink = cls._build_attestation_sink(config, attestation, base_dir)

        user_rules_dirs = [cls._resolve(base_dir, d) for d in config.rules.user_rules_dirs]
        router = FathomRouter(
            built_in_rules_dir=BUILT_IN_RULES_DIR,
            user_rules_dirs=user_rules_dirs,
            attestation=attestation,
            check_consistency=config.rules.consistency_checks,
            rule_packs=config.rules.packs,
        )

        # B1 -- an unrecognised level is ranked below ``unclassified`` by
        # ``fathom.engine.dominates``, so a typo in the primary access-control
        # field publishes the source to every agent. Reject at startup, where
        # the operator can still see the typo, rather than denying silently
        # for the life of the deployment.
        _validate_classification_labels(config, router)

        # AC-35.2.b/d — load manual relationship facts into the engine at
        # startup; re-reading on every broker construction gives restart
        # persistence. The directory is optional: missing = no-op.
        _facts_dir = Path(path).parent / "facts" / "relationships"
        if _facts_dir.is_dir():
            load_manual_relationships(router.engine, _facts_dir)

        # Broker-default embedder: strict NoopEmbedder (design §3.10 — fail
        # loudly on missing embedder rather than silent zero vectors).
        broker_default_embedder: Embedder = NoopEmbedder(strict=True)

        # Merge static registry with entry-point discovered plugins and
        # local-path adapters (#17). Explicit config wins over discovery.
        adapter_registry = {
            **ADAPTER_REGISTRY,
            **_discover_adapters(),
            **_load_local_adapters(config.adapters, base_dir=Path(path).parent),
        }

        adapters: dict[str, Adapter] = {}
        for source in registry:
            adapters[source.id] = cls._build_adapter(
                source, broker_default_embedder, adapter_registry
            )

        audit_path = cls._resolve(base_dir, config.audit.path)
        try:
            audit_path.parent.mkdir(parents=True, exist_ok=True)
        except OSError as exc:
            # In the hardened image this is where a missing ``audit``
            # volumeMount lands: ``/var/log`` exists and UID 65532 cannot write
            # it, so the broker refuses before it binds. Measured, that read as
            # a bare ``[Errno 13] Permission denied: '/var/log/nautilus'`` --
            # an operator following the deployment guide went looking at
            # ``/readyz``, which nothing was listening on. Name the directory's
            # job and the two ways it goes wrong.
            raise OSError(
                f"cannot open the audit log directory {audit_path.parent}: {exc}. "
                f"Nautilus records one line per decision and will not serve "
                f"unrecorded requests, so this refuses startup rather than "
                f"degrading. Either the volume for it is not mounted, or it is "
                f"not writable by the UID this process runs as."
            ) from exc
        try:
            audit_logger = AuditLogger(sink=build_audit_sink(config, audit_path, attestation))
        except OSError as exc:
            # The second of the two causes named above arrives here, not at the
            # mkdir: ``exist_ok=True`` succeeds on a directory that exists, so a
            # mounted-but-unwritable volume walks straight past it and fails on
            # the open instead -- and refused with a bare
            # ``[Errno 13] Permission denied: '.../audit.jsonl'``. Same
            # consequence, same sentence.
            raise OSError(
                f"cannot open the audit log {audit_path}: {exc}. Nautilus "
                f"records one line per decision and will not serve unrecorded "
                f"requests, so this refuses startup rather than degrading. The "
                f"audit log directory exists but is not writable by the UID this "
                f"process runs as, or the file itself is not."
            ) from exc

        session_store = cls._build_session_store(config, base_dir=base_dir)

        synthesizer = BasicSynthesizer()

        # Session-provenance tokens (#18) — KeyRing only when enabled, so
        # Phase-1 YAML keeps the token path entirely off (NFR-5). A configured
        # ``key_ring_path`` persists the ring so replicas share signing keys.
        key_ring_path = config.session_tokens.key_ring_path
        key_ring = (
            KeyRing(cls._resolve(base_dir, key_ring_path) if key_ring_path else None)
            if config.session_tokens.enabled
            else None
        )
        # A token names the broker that minted it so it cannot be replayed
        # against an unrelated one that happens to share key material. That id
        # has to be the same on every replica of one deployment, or the check
        # rejects every token behind a load balancer. Configured id wins; a
        # shared ring means a shared deployment, so it defaults to a constant
        # there — deriving it from the ring's key material would change under
        # rotation and invalidate every token in flight.
        broker_instance_id = config.session_tokens.broker_instance_id or (
            "shared" if key_ring_path else None
        )

        if log.isEnabledFor(logging.DEBUG):
            # ``config check`` prints this; a running server did not, so
            # "which file is this process actually serving, and what did its
            # relative paths resolve to" was answerable only by reading the
            # deployment. ``dials`` goes through the same allowlist redactor as
            # every other endpoint the broker publishes, so a DSN password in
            # the file does not reach the process log with it.
            log.debug(
                "loaded config %s: %d source(s), %d agent(s), audit -> %s, "
                "session store %r, session tokens %s, state dir %s",
                Path(path).resolve(),
                len(registry.sources),
                len(config.agents),
                audit_path,
                config.session_store.backend,
                "on" if config.session_tokens.enabled else "off",
                cls._resolve(base_dir, config.state_dir) if config.state_dir else base_dir,
            )
            for source in registry:
                log.debug(
                    "config source %r: type %r, classification %r, data types %s, dials %s",
                    source.id,
                    source.type,
                    source.classification,
                    source.data_types,
                    redact_connection(source.connection) or "nothing",
                )

        return cls(
            config=config,
            registry=registry,
            intent_analyzer=intent_analyzer,
            router=router,
            adapters=adapters,
            synthesizer=synthesizer,
            audit_logger=audit_logger,
            attestation=attestation,
            session_store=session_store,
            agent_registry=agent_registry,
            attestation_sink=attestation_sink,
            key_ring=key_ring,
            session_token_ttl_s=config.session_tokens.ttl_seconds,
            broker_instance_id=broker_instance_id,
            # Where this broker's own state goes. ``state_dir`` exists because
            # the default -- the config file's directory -- is read-only in
            # every deployment shape this repo ships.
            base_dir=(cls._resolve(base_dir, config.state_dir) if config.state_dir else base_dir),
        )

    @classmethod
    def _build_intent_analyzer(
        cls,
        config: NautilusConfig,
        pattern_analyzer: PatternMatchingIntentAnalyzer,
        known_data_types: list[str] | None = None,
    ) -> IntentAnalyzer | FallbackIntentAnalyzer:
        """Construct the wired intent analyzer per ``config.analysis.mode``.

        - ``"pattern"`` (default) → return ``pattern_analyzer`` unchanged so
          the broker hot path stays sync and Phase-1 audit JSONL round-trips
          byte-identically (NFR-5/NFR-6).
        - ``"llm-first"`` / ``"llm-only"`` → wrap a provider built from
          ``config.analysis.provider`` in :class:`FallbackIntentAnalyzer`
          with ``pattern_analyzer`` as the deterministic fallback (FR-14,
          AC-6.2).

        Raises :class:`ConfigError` when an LLM mode is requested without a
        provider spec (AC-6.4 surfaces the same failure under the CLI's
        ``--air-gapped`` override).
        """
        analysis = config.analysis
        if analysis.mode == "pattern":
            return pattern_analyzer
        if analysis.provider is None:
            raise ConfigError(
                f"analysis.mode={analysis.mode!r} requires analysis.provider to be set"
            )
        provider = cls._build_llm_provider(analysis.provider, known_data_types)
        return FallbackIntentAnalyzer(
            primary=provider,
            fallback=pattern_analyzer,
            timeout_s=analysis.timeout_s,
            mode=analysis.mode,
        )

    @staticmethod
    def _build_llm_provider(
        spec: AnalysisProviderSpec, known_data_types: list[str] | None = None
    ) -> LLMIntentProvider:
        """Instantiate an :class:`LLMIntentProvider` from a config spec (design §3.8).

        Discriminated-union dispatch on ``spec.type``; provider modules are
        imported lazily so optional extras (``llm-anthropic`` /
        ``llm-openai``) only blow up when actually requested.
        """
        if isinstance(spec, AnthropicProviderSpec):
            from nautilus.analysis.llm.anthropic_provider import AnthropicProvider

            return AnthropicProvider(
                api_key_env=spec.api_key_env,
                model=spec.model,
                timeout_s=spec.timeout_s,
                known_data_types=known_data_types,
            )
        if isinstance(spec, OpenAIProviderSpec):
            from nautilus.analysis.llm.openai_provider import OpenAIProvider

            return OpenAIProvider(
                api_key_env=spec.api_key_env,
                model=spec.model,
                timeout_s=spec.timeout_s,
                known_data_types=known_data_types,
            )
        # Discriminated union — only the local spec remains.
        assert isinstance(spec, LocalInferenceProviderSpec)
        from nautilus.analysis.llm.local_provider import LocalInferenceProvider

        return LocalInferenceProvider(
            base_url=spec.base_url,
            model=spec.model,
            api_key_env=spec.api_key_env,
            timeout_s=spec.timeout_s,
            known_data_types=known_data_types,
        )

    @staticmethod
    def _build_session_store(
        config: NautilusConfig, *, base_dir: Path
    ) -> SessionStore | AsyncSessionStore:
        """Construct the session store per ``config.session_store.backend``.

        - ``memory`` (default) → :class:`InMemorySessionStore` (Phase-1 compat,
          NFR-5).
        - ``postgres`` → :class:`PostgresSessionStore` over ``dsn`` (or
          ``TEST_PG_DSN`` env var when ``dsn`` is unset, so integration
          fixtures reuse pg_container without duplicating YAML plumbing);
          ``on_failure`` selects ``fail_closed``, ``fallback_memory``, or
          ``fallback_sqlite`` at ``sqlite_path`` (NFR-7, #26).
        - ``sqlite`` → :class:`SqliteSessionStore` at ``sqlite_path`` —
          durable single-node store, no Postgres required (#26).

        ``ttl_seconds`` reaches every backend: a session idle for longer than
        the TTL reads as absent, so cumulative exposure state does not
        accumulate against one session id forever.

        A relative ``sqlite_path`` is resolved against ``base_dir`` (the
        config file's directory — same convention as the ``facts/`` dir)
        so the database location does not depend on the process CWD; a
        restart from a different working directory must reopen the SAME
        store, not silently mint an empty one.
        """
        sess_cfg = config.session_store
        sqlite_path = Path(sess_cfg.sqlite_path)
        if not sqlite_path.is_absolute():
            sqlite_path = base_dir / sqlite_path
        if sess_cfg.backend == "postgres":
            import os

            dsn = sess_cfg.dsn
            if not dsn:
                dsn = os.environ.get("TEST_PG_DSN")
                if dsn:
                    # A test-shaped name reaching a production code path. It stays
                    # (the integration fixtures dial a container DSN through it),
                    # but silently: an operator who deleted `session_store.dsn`
                    # from the ConfigMap got a session store pointed at whatever
                    # TEST_PG_DSN happened to hold, and nothing said so.
                    log.warning(
                        "session_store.dsn is unset; falling back to the TEST_PG_DSN "
                        "environment variable. Set session_store.dsn explicitly -- "
                        "TEST_PG_DSN exists for the integration fixtures and is not "
                        "a supported way to configure a deployment."
                    )
            if not dsn:
                raise ConfigError(
                    "session_store.backend=postgres requires 'dsn' or TEST_PG_DSN env var"
                )
            return PostgresSessionStore(
                dsn,
                on_failure=sess_cfg.on_failure,
                sqlite_path=sqlite_path,
                ttl_seconds=sess_cfg.ttl_seconds,
                pool_min_size=sess_cfg.pool_min_size,
                pool_max_size=sess_cfg.pool_max_size,
                lock_pool_max_size=sess_cfg.lock_pool_max_size,
                acquire_timeout_s=sess_cfg.acquire_timeout_s,
            )
        if sess_cfg.backend == "sqlite":
            return SqliteSessionStore(sqlite_path, sess_cfg.ttl_seconds)
        return InMemorySessionStore(sess_cfg.ttl_seconds)

    @staticmethod
    def _resolve(base_dir: Path, value: str | Path) -> Path:
        """Resolve a config-declared path against the config file's directory.

        Relative paths in ``nautilus.yaml`` used to resolve against the
        process's working directory, so the same config produced a different
        audit log, a different attestation log and a different set of rule
        directories depending on where the operator happened to run
        ``nautilus serve`` from. Absolute paths are returned unchanged.
        """
        path = Path(value)
        return path if path.is_absolute() else base_dir / path

    @staticmethod
    def _build_attestation_sink(
        config: NautilusConfig, attestation: AttestationService | None, base_dir: Path
    ) -> AttestationSink:
        """Construct the attestation sink per design §3.14 / FR-28.

        Selects the concrete :class:`AttestationSink` implementation based on
        ``config.attestation.sink.type``:

        - ``"null"`` (default) → :class:`NullAttestationSink` — no-op; preserves
          NFR-5 for Phase-1 YAML fixtures with no ``attestation.sink`` entry.
        - ``"file"`` → :class:`FileAttestationSink` — append-only JSONL with
          per-emit ``flush`` + ``os.fsync`` (AC-14.2); with ``chained: true``,
          :class:`ChainedFileAttestationSink` — hash-chained + JWS-signed
          lines verifiable offline via ``nautilus attestation verify``.
        - ``"http"`` → :class:`HttpAttestationSink` — POST to verifier URL with
          retry + dead-letter spill (AC-14.3).
        """
        sink_spec = config.attestation.sink
        if isinstance(sink_spec, FileSinkSpec):
            if sink_spec.chained:
                if attestation is None:
                    msg = "attestation.sink.chained requires attestation.enabled with a signing key"
                    raise ValueError(msg)
                return ChainedFileAttestationSink(
                    Broker._resolve(base_dir, sink_spec.path),
                    attestation,
                    checkpoint_interval=sink_spec.checkpoint_interval,
                )
            return FileAttestationSink(Broker._resolve(base_dir, sink_spec.path))
        if isinstance(sink_spec, HttpSinkSpec):
            rp_spec = sink_spec.retry_policy
            retry_policy = RetryPolicy(
                max_retries=rp_spec.max_retries,
                initial_backoff_s=rp_spec.initial_backoff_s,
                max_backoff_s=rp_spec.max_backoff_s,
            )
            dead_letter = (
                Broker._resolve(base_dir, sink_spec.dead_letter_path)
                if sink_spec.dead_letter_path
                else None
            )
            return HttpAttestationSink(
                url=sink_spec.url,
                retry_policy=retry_policy,
                dead_letter_path=dead_letter,
            )
        # Must be NullSinkSpec by virtue of the pydantic discriminated union.
        assert isinstance(sink_spec, NullSinkSpec)
        return NullAttestationSink()

    @staticmethod
    def _build_attestation(config: NautilusConfig, base_dir: Path) -> AttestationService | None:
        """Construct the attestation service per design §9.4.

        - ``enabled: false`` → ``None`` (token omitted on every response).
        - ``private_key_path`` set → load PEM from path.
        - Otherwise → generate an ephemeral Ed25519 keypair.
        """
        if not config.attestation.enabled:
            return None
        key_path = config.attestation.private_key_path
        if key_path:
            key_bytes = Broker._resolve(base_dir, key_path).read_bytes()
            return AttestationService.from_private_key_bytes(key_bytes)
        return AttestationService.generate_keypair()

    @staticmethod
    def _build_adapter(
        source: SourceConfig,
        broker_default_embedder: Embedder,
        adapter_registry: dict[str, type[Adapter]] | None = None,
    ) -> Adapter:
        """Instantiate the right adapter class for ``source.type``.

        Looks up ``source.type`` in the merged adapter registry (static
        built-ins + entry-point discovered plugins).  ``pgvector`` is
        special-cased because it requires the broker-default embedder.
        """
        registry = adapter_registry if adapter_registry is not None else ADAPTER_REGISTRY

        adapter_cls = registry.get(source.type)
        if adapter_cls is None:
            raise ConfigError(f"Unsupported source type '{source.type}' for id='{source.id}'")

        # The driver-backed adapters are optional extras, and the registry
        # holds a stand-in for any whose driver is not installed. Fail here,
        # at startup, naming the extra -- not at first request with a
        # ModuleNotFoundError from three frames inside the adapter.
        extra = getattr(adapter_cls, "missing_extra", None)
        if extra is not None:
            raise ConfigError(
                f"source id='{source.id}' has type '{source.type}', whose driver is not "
                f"installed -- {getattr(adapter_cls, 'install_hint', extra)} "
                f"(import failed: {getattr(adapter_cls, 'import_error', '?')})"
            )

        # pgvector needs the embedder kwarg — special-case it. The class comes
        # from the registry, not a top-level import: asyncpg and pgvector are
        # optional extras, and importing the adapter module here made
        # ``import nautilus`` fail outright on a lean install, three lines below
        # the stand-in that exists to keep exactly that working.
        if source.type == "pgvector":
            return adapter_cls(broker_default_embedder=broker_default_embedder)  # type: ignore[call-arg]
        return adapter_cls()

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    @property
    def sources(self) -> list[SourceConfig]:
        """Registered source configs (identifier + metadata) — design §3.1."""
        return self._registry.sources

    def sources_visible_to(self, agent_id: str | None) -> list[SourceConfig]:
        """The catalogue as ``agent_id`` is allowed to know it.

        A source's name, prose description and data types are what a caller
        needs in order to ask for it — which is the reconnaissance step the
        classification rules refuse. Listing every source to every caller told
        an unclassified key exactly what it was being kept from, so the
        catalogue is filtered by the same clearance ladder the router routes
        by.

        ``None`` means no bound agent — a bare key, which is documented as
        "any agent_id, every capability" and has no clearance to filter
        against — or a deployment with no ``classification`` hierarchy
        registered, which has no ladder at all. Both see the whole catalogue.
        """
        if agent_id is None:
            return self.sources
        levels = self._router.hierarchy_levels()
        if not levels:
            return self.sources
        try:
            clearance = self._agent_registry.get(agent_id).clearance
        except UnknownAgentError:
            return self.sources
        if clearance not in levels:
            return self.sources
        rank = levels.index(clearance)
        # An unrecognised source label ranks below everything for fathom, so it
        # is visible on the same terms the router routes it on.
        return [
            s
            for s in self.sources
            if s.classification not in levels or levels.index(s.classification) <= rank
        ]

    @property
    def config(self) -> NautilusConfig:
        """The validated config this broker was built from.

        Exposed so a governance surface served by this broker uses the same
        ``rkm.sandbox`` and ``rules`` settings the broker itself runs under.
        """
        return self._config

    @property
    def audit_path(self) -> Path | None:
        """The JSONL file this broker's decisions are written to.

        The sandbox replays this log, so a governance surface that queues a
        proposal has to be able to find it rather than guessing
        ``./audit.jsonl`` relative to wherever the server was started.
        ``None`` when the sink is not a file.
        """
        return self._audit_logger.path

    @staticmethod
    def _fingerprint_root(base_dir: Path | None) -> str | None:
        """Directory the schema baselines persist under, or ``None`` for memory-only.

        A seam, not indirection: the test suite redirects baselines here so
        many brokers built from one fixture config do not share — or write
        into the source tree — a store that changes their behaviour.
        """
        return str(base_dir) if base_dir is not None else None

    @property
    def audit_logger(self) -> AuditLogger:
        """The sink every decision this broker records goes to.

        Exposed because the governance decisions taken over REST — approve,
        reject, retract, rollback — are made against this broker and belong in
        the same log as the routing decisions they change.
        """
        return self._audit_logger

    @property
    def fingerprint_store(self) -> SchemaFingerprintStore:
        """Schema baselines this broker compares against (AC-21.c).

        Exposed so ``nautilus adapters schema-diff`` / ``schema-ack`` read and
        write the same baselines the broker does, rather than a second store
        rooted somewhere else.
        """
        return self._fingerprint_store

    @property
    def api_config(self) -> ApiConfig:
        """``api:`` subsection of the loaded config (host, port, keys, auth).

        Exposed so ``nautilus serve`` can honour ``api.host`` / ``api.port``
        without re-loading the YAML or reaching into private state.
        """
        return self._config.api

    @property
    def ruleset_hash(self) -> str:
        """``sha256:…`` over the rules this broker is deciding with."""
        return self._router.ruleset_hash

    def rules_in_force(self) -> list[dict[str, str]]:
        """Every rule loaded into the running engine (module + name)."""
        return self._router.rules_in_force()

    @property
    def agent_registry(self) -> AgentRegistry:
        """Registered agent identities (design §3.5, FR-9)."""
        return self._agent_registry

    @property
    def session_store(self) -> SessionStore | AsyncSessionStore:
        """Active session store (sync or async surface) — design §3.2 / §3.9.

        Exposed so transports (``/readyz`` probe in :mod:`nautilus.transport.
        fastapi_app`) can call ``aget`` / ``get`` against the backing
        store without reaching into private state.
        """
        return self._session_store

    @property
    def instance_id(self) -> str:
        """Per-process broker identity baked into session-token claims (#18).

        Tokens minted by this broker carry ``broker_instance_id`` and fail
        verification (``broker_instance_mismatch``) against any other
        instance (AC-18.d).
        """
        return self._instance_id

    @property
    def key_ring(self) -> KeyRing | None:
        """Signing/verification key ring for session tokens (#18, AC-18.e).

        ``None`` when ``session_tokens.enabled`` is false. The ring is
        in-memory — transports MUST reuse this instance (not construct a
        fresh :class:`KeyRing`) or token verification cannot succeed.
        """
        return self._key_ring

    @property
    def session_tokens(self) -> SessionTokenService | None:
        """Active :class:`SessionTokenService`, or ``None`` when disabled (#18)."""
        return self._session_tokens

    def issue_session_token(
        self,
        *,
        session_id: str,
        agent_id: str,
        purpose: str,
    ) -> str:
        """Mint a session token + emit ``session_token_issued`` audit (AC-18.f).

        Public surface for transports (``POST /v1/sessions``) so token
        issuance is always audited through the broker's single JSONL stream.

        ``clearance`` is read from the AgentRegistry, never taken from the
        caller. It used to be a parameter, and ``POST /v1/sessions`` passed
        ``body["clearance"]`` into it, so the broker would sign
        ``clearance: top-secret`` for an agent whose record says
        ``unclassified`` -- an authorization assertion, signed by Nautilus,
        verifiable by anyone against the unauthenticated JWKS, and forwarded to
        downstream sources in ``X-Nautilus-Session-Token``. The in-request
        minting path already read the registry; taking the parameter away means
        no caller can disagree with it.

        ``purpose`` gets the same treatment. It is caller free text, and it is
        a live authorization input: the router denies every source when it falls
        outside the agent's ``allowed_purposes``. Minting it unchecked meant a
        caller could ask this route for a signed assertion of a purpose the
        broker would refuse to act on, and be handed one.

        Raises:
            RuntimeError: when session tokens are disabled.
            PurposeNotPermittedError: when the agent may not claim ``purpose``.
        """
        if self._session_tokens is None:
            raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
        try:
            record = self._agent_registry.get(agent_id)
        except UnknownAgentError:
            record = None
        if record is not None and not record.may_claim(purpose):
            raise PurposeNotPermittedError(
                f"purpose {purpose!r} is not one of the purposes agent "
                f"{agent_id!r} may claim ({sorted(record.allowed_purposes)})"
            )
        token = self._session_tokens.issue(
            session_id=session_id,
            agent_id=agent_id,
            purpose=purpose,
            clearance=record.clearance if record is not None else "",
        )
        self._emit_session_token_event(
            "session_token_issued",
            agent_id=agent_id,
            session_id=session_id,
            request_id=str(uuid.uuid4()),
        )
        return token

    def verify_session_token(self, token: str) -> SessionTokenClaims:
        """Verify a session token; audit failures (AC-18.d + AC-18.f).

        Returns the decoded claims on success. On failure, emits a
        ``session_token_verification_failed`` audit entry carrying the
        ``reason_code`` as an :class:`ErrorRecord` and re-raises the
        :class:`SessionTokenError` (fail-closed).

        Raises:
            RuntimeError: when session tokens are disabled.
            SessionTokenError: tampered / expired / unknown-kid /
                wrong-instance token.
        """
        if self._session_tokens is None:
            raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
        try:
            return self._session_tokens.verify(token)
        except SessionTokenError as exc:
            request_id = str(uuid.uuid4())
            self._emit_session_token_event(
                "session_token_verification_failed",
                agent_id="",
                session_id="",
                request_id=request_id,
                errors=[
                    ErrorRecord(
                        source_id="<broker>",
                        error_type=exc.reason_code,
                        message=str(exc),
                        trace_id=request_id,
                    )
                ],
            )
            raise

    def rotate_signing_key(self, *, reviewer: str) -> str:
        """Rotate the session-token signing key on a LIVE broker (#25).

        Mints a new primary in the shared :class:`KeyRing`; the previous
        primary moves to ``rotating-out`` so in-flight tokens keep
        verifying (grace window). Agents presenting old-kid tokens are
        lazily re-signed on their next request (see
        :meth:`_process_session_token`). The grace window is closed
        explicitly via :meth:`revoke_signing_key`.

        Emits a ``signing_key_rotated`` audit entry recording the reviewer
        and the previous/new kid linkage.

        Returns:
            The new primary kid.

        Raises:
            RuntimeError: when session tokens are disabled.
        """
        if self._key_ring is None:
            raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
        previous = self._key_ring.primary().kid
        new_entry = self._key_ring.rotate()
        self._emit_session_token_event(
            "signing_key_rotated",
            agent_id="",
            session_id="",
            request_id=str(uuid.uuid4()),
            trace=[
                f"reviewer={reviewer}",
                f"previous_kid={previous}",
                f"new_kid={new_entry.kid}",
            ],
        )
        return new_entry.kid

    def revoke_signing_key(self, kid: str, *, reason: str, reviewer: str) -> None:
        """Revoke a signing key immediately on a LIVE broker (#25).

        Tokens signed by ``kid`` stop verifying at once —
        :meth:`SessionTokenService.verify` rejects revoked entries — so
        this is the explicit end of a rotation's grace window.

        Emits a ``signing_key_revoked`` audit entry.

        Raises:
            RuntimeError: when session tokens are disabled.
            KeyError: when ``kid`` is not in the ring.
            ValueError: when ``kid`` is the current primary — revoking it
                would make :meth:`KeyRing.primary` silently auto-generate
                an unaudited replacement (security review C1). Rotate
                first, then revoke the rotated-out key.
        """
        if self._key_ring is None:
            raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
        if self._key_ring.verifier_for(kid) is None:
            raise KeyError(kid)
        if kid == self._key_ring.primary().kid:
            raise ValueError(f"kid {kid!r} is the current primary; rotate first, then revoke")
        self._key_ring.revoke(kid, reason=reason, reviewer=reviewer)
        self._emit_session_token_event(
            "signing_key_revoked",
            agent_id="",
            session_id="",
            request_id=str(uuid.uuid4()),
            trace=[f"reviewer={reviewer}", f"kid={kid}", f"reason={reason}"],
        )

    def request(
        self,
        agent_id: str,
        intent: str,
        context: dict[str, Any] | None = None,
        *,
        fact_set_hash: str | None = None,
    ) -> BrokerResponse:
        """Sync request: guards against nested event loops, then runs pipeline.

        Per design §8, calling this while inside a running event loop
        raises :class:`RuntimeError` whose message mentions ``arequest``
        (UQ-4, AC-8.5). Outside a loop, we delegate to
        :meth:`arequest` via ``asyncio.run``.

        ``fact_set_hash`` (US-6 / FR-62 opaque round-trip) is accepted
        and echoed onto :attr:`BrokerResponse.fact_set_hash`; populating
        the audit/cost-cap pipeline that consumes it is staged work.
        """
        try:
            asyncio.get_running_loop()
        except RuntimeError:
            # No running loop — safe to take ownership of a fresh one.
            pass
        else:
            raise RuntimeError(
                "Broker.request() called inside a running event loop. "
                "Use Broker.arequest() (async) from async contexts."
            )
        return self._run_sync(self.arequest(agent_id, intent, context, fact_set_hash=fact_set_hash))

    def _run_sync(self, coro: Coroutine[Any, Any, _T]) -> _T:
        """Run *coro* on the broker's own loop and block until it finishes.

        The loop and the connection pools it owns have to outlive the call:
        an ``asyncio.run`` per call closes the loop underneath pools that the
        adapter still holds, and the failure surfaces as an empty result set
        rather than an error.
        """
        loop = self._sync_loop
        if loop is None:
            loop = asyncio.new_event_loop()
            thread = threading.Thread(
                target=loop.run_forever, name="nautilus-broker-sync", daemon=True
            )
            thread.start()
            self._sync_loop = loop
            self._sync_thread = thread
        return asyncio.run_coroutine_threadsafe(coro, loop).result()

    async def arequest(
        self,
        agent_id: str,
        intent: str,
        context: dict[str, Any] | None = None,
        *,
        fact_set_hash: str | None = None,
        caller: dict[str, str] | None = None,
    ) -> BrokerResponse:
        """Async request pipeline (design §3.1, §8, §9).

        ``caller`` is the transport's view of who is calling -- ``{"auth":
        <authenticated principal>, "peer": <network address>}`` -- and is what
        keys the cumulative-exposure ledger (§4.15). It is a parameter rather
        than a ``context`` key precisely because ``context`` is the caller's to
        fill in: a client that could set it could reset its own ledger. An
        in-library caller passes nothing and is identified by ``agent_id``
        alone.

        Linear sequence of awaits; heavy lifting lives in private helpers
        (`_run_pipeline`, `_build_adapter_jobs`, `_gather_adapter_results`,
        `_build_response`, `_emit_audit`). On policy-engine or unexpected
        failure, a single audit entry is still emitted before re-raising.
        """
        self._refuse_if_closed("arequest")
        await self._ensure_setup()
        with self._pinned_generation():
            return await self._arequest_pinned(agent_id, intent, context, fact_set_hash, caller)

    async def _arequest_pinned(
        self,
        agent_id: str,
        intent: str,
        context: dict[str, Any] | None,
        fact_set_hash: str | None,
        caller: dict[str, str] | None,
    ) -> BrokerResponse:
        """:meth:`arequest`'s body, running on a pinned config generation.

        Split out so the pin is one ``with`` at the entry point rather than an
        indentation change over a hundred lines: everything below reads
        ``self._registry`` / ``self._router`` / ``self._adapters`` /
        ``self._config`` through :attr:`_live`, which resolves to the
        generation pinned here even if a SIGHUP installs a new one mid-request.
        """
        context = dict(context) if context else {}
        state = _new_request_state(context, intent)
        state.fact_set_hash = fact_set_hash
        state.principal_id = derive_principal_id(
            agent_id,
            auth_principal=(caller or {}).get("auth"),
            peer=(caller or {}).get("peer"),
        )
        # Session ownership is a boundary between callers, and an in-library
        # call has no other caller on the far side of it: no credential, no
        # peer, one process orchestrating its own agents. Transports always
        # pass one of the two, so the HTTP and MCP surfaces are always gated.
        state.caller_bound = bool((caller or {}).get("auth") or (caller or {}).get("peer"))
        # Session-provenance gate (#18) — verify a presented token (fail-closed
        # with its own audit entry) or mint one for a fresh session, BEFORE the
        # pipeline runs so adapters see the token in ``context`` (AC-18.b).
        if self._session_tokens is not None:
            self._process_session_token(agent_id, context, state)
        _started = time.perf_counter()
        with broker_span(SPAN_BROKER_REQUEST, build_request_attributes(agent_id)):
            _metrics.requests_total.add(1)
            try:
                await self._run_pipeline(agent_id, intent, context, state)
            except PolicyEngineError as exc:
                # #27 — record the engine failure (e.g. ConsistencyError
                # check name) on the audit entry before failing closed.
                state.errored.append(_broker_error(exc, state.request_id))
                with broker_span(SPAN_AUDIT_EMIT):
                    # ``state.attestation_token``, not None: a failure AFTER
                    # signing (the session write is the one such await) still
                    # produced a real token that reached the sink, and the
                    # canonical entry for this request must not deny it.
                    self._emit_audit(agent_id, state, state.attestation_token)
                raise
            except BaseException as exc:
                # BaseException, not Exception: a cancelled or timed-out
                # request is exactly the one whose audit entry matters, and
                # CancelledError inherits from BaseException.
                state.errored.append(_broker_error(exc, state.request_id))
                with broker_span(SPAN_AUDIT_EMIT):
                    self._emit_audit(agent_id, state, state.attestation_token)
                raise
            with broker_span(SPAN_AUDIT_EMIT):
                self._emit_audit(agent_id, state, state.attestation_token)
            _metrics.request_duration.record(
                time.perf_counter() - _started,
            )
        return self._build_response(state)

    async def declare_handoff(
        self,
        *,
        source_agent_id: str,
        receiving_agent_id: str,
        session_id: str,
        data_classifications: list[str],
        rule_trace_refs: list[str] | None = None,
        data_compartments: list[str] | None = None,
        session_token: str | None = None,
    ) -> HandoffDecision:
        """Declare an agent-to-agent handoff and evaluate the handoff rule pack.

        Reasoning-only path (design §3.6, FR-8, FR-10, AC-4.1): zero adapter
        calls. One session-store write, and only on ``action="allow"``: the
        receiving agent is recorded on the session row so
        :meth:`_gate_session_ownership` lets it continue a session another
        principal owns. Without that record a declared handoff was a statement
        with nothing behind it — the two parties merely agreed on a string, and
        so did anyone else who guessed the same one. Flow:

        1. Resolve both agents via :class:`AgentRegistry`. An unknown id
           short-circuits to ``action="deny"`` with a synthetic
           ``unknown-agent`` :class:`DenialRecord` (AC-4.2).
        2. Assert one ``data_handoff`` fact per declared classification
           with ``from_clearance`` / ``to_clearance`` read from the
           registered :class:`AgentRecord` entries.
        3. Call :meth:`fathom.Engine.evaluate` — the
           ``information-flow-violation`` default rule + any user rules
           matching ``data_handoff`` fire here.
        4. Collect ``denial_record`` facts; ``action`` is ``"allow"``
           when none fired, ``"deny"`` otherwise. ``"escalate"`` is
           reserved for escalation-pack-driven denials and is not
           produced by the default rule set (AC-4.3).
        5. Emit exactly one :class:`AuditEntry` with
           ``event_type="handoff_declared"`` and the populated
           :class:`HandoffDecision`; never more (AC-4.4, NFR-15
           parallel).

        ``rule_trace_refs`` and ``data_compartments`` are accepted for
        forward-compat with the Phase-3 forensic worker + compartment-
        aware handoff rules; the default rule pack ignores both (empty
        compartments in the ``fathom-dominates`` calls).
        """
        self._refuse_if_closed("declare_handoff")
        await self._ensure_setup()
        with self._pinned_generation():
            return await self._declare_handoff_pinned(
                source_agent_id=source_agent_id,
                receiving_agent_id=receiving_agent_id,
                session_id=session_id,
                data_classifications=data_classifications,
                rule_trace_refs=rule_trace_refs,
                data_compartments=data_compartments,
                session_token=session_token,
            )

    async def _declare_handoff_pinned(
        self,
        *,
        source_agent_id: str,
        receiving_agent_id: str,
        session_id: str,
        data_classifications: list[str],
        rule_trace_refs: list[str] | None,
        data_compartments: list[str] | None,
        session_token: str | None,
    ) -> HandoffDecision:
        """:meth:`declare_handoff`'s body, on a pinned config generation."""
        del rule_trace_refs, data_compartments  # Phase-3 / forensic forward-compat.
        started = time.perf_counter()
        handoff_id = str(uuid.uuid4())

        # #18 — when session tokens are enabled, a handoff REQUIRES the
        # originating agent's token; missing/invalid/mismatched tokens deny
        # before any agent resolution or engine work. The verified-token
        # trace marker lands in ``rule_trace`` so the handoff audit entry
        # records the token reference (kid + session) alongside both agent
        # ids (AC: "audit entry records both agent_ids and the token
        # reference").
        token_trace: list[str] = []
        if self._session_tokens is not None:
            token_denial = self._gate_handoff_token(
                session_token, source_agent_id, session_id, token_trace
            )
            if token_denial is not None:
                decision = HandoffDecision(
                    handoff_id=handoff_id,
                    action="deny",
                    denial_records=[token_denial],
                    rule_trace=list(token_trace),
                )
                self._emit_handoff_audit(
                    source_agent_id=source_agent_id,
                    receiving_agent_id=receiving_agent_id,
                    session_id=session_id,
                    data_classifications=data_classifications,
                    decision=decision,
                    started=started,
                )
                return decision

        # AC-4.2 — unknown-agent short-circuit: resolve BOTH agents before
        # touching the engine so a bogus id never asserts facts.
        try:
            source_agent = self._agent_registry.get(source_agent_id)
            receiving_agent = self._agent_registry.get(receiving_agent_id)
        except UnknownAgentError as exc:
            decision = HandoffDecision(
                handoff_id=handoff_id,
                action="deny",
                denial_records=[
                    DenialRecord(
                        source_id=session_id,
                        reason=str(exc),
                        rule_name="unknown-agent",
                    )
                ],
                rule_trace=list(token_trace),
            )
            self._emit_handoff_audit(
                source_agent_id=source_agent_id,
                receiving_agent_id=receiving_agent_id,
                session_id=session_id,
                data_classifications=data_classifications,
                decision=decision,
                started=started,
            )
            return decision

        # B1 -- the handoff rules need ``fathom-dominates`` to be TRUE before
        # they can deny, and an unrecognised classification ranks -1, so it is
        # dominated by every clearance and the handoff is allowed. Caller-
        # supplied labels cannot be checked at startup, so deny here.
        levels = self._router.hierarchy_levels()
        unknown = [c for c in data_classifications if c not in levels]
        if levels and unknown:
            decision = HandoffDecision(
                handoff_id=handoff_id,
                action="deny",
                denial_records=[
                    DenialRecord(
                        source_id=session_id,
                        reason=(
                            f"classification {unknown[0]!r} is not a level of the "
                            f"'classification' hierarchy ({', '.join(levels)})"
                        ),
                        rule_name="unknown-classification",
                    )
                ],
                rule_trace=list(token_trace),
            )
            self._emit_handoff_audit(
                source_agent_id=source_agent_id,
                receiving_agent_id=receiving_agent_id,
                session_id=session_id,
                data_classifications=data_classifications,
                decision=decision,
                started=started,
            )
            return decision

        # Assert one data_handoff per declared classification, run engine,
        # and collect any denial_record facts. The engine is shared with
        # arequest() so we guard it with the same PolicyEngineError shape.
        engine = self._router.engine
        try:
            engine.clear_facts()
            for classification in data_classifications:
                engine.assert_fact(
                    "data_handoff",
                    {
                        "from_agent": source_agent_id,
                        "to_agent": receiving_agent_id,
                        "session_id": session_id,
                        "classification": classification,
                        "from_clearance": source_agent.clearance,
                        "to_clearance": receiving_agent.clearance,
                    },
                )
            eval_result = engine.evaluate()
            raw_denials = engine.query("denial_record")
        except Exception as exc:  # noqa: BLE001 — re-wrap as PolicyEngineError per §3.4
            raise PolicyEngineError(
                f"Broker.declare_handoff() failed for source={source_agent_id!r}"
                f" receiving={receiving_agent_id!r}: {exc}"
            ) from exc

        denials = [
            DenialRecord(
                source_id=str(d["source_id"]),
                reason=str(d["reason"]),
                rule_name=str(d["rule_name"]),
            )
            for d in raw_denials
        ]
        rule_trace = token_trace + list(getattr(eval_result, "rule_trace", []) or [])
        action: Literal["allow", "deny", "escalate"] = "deny" if denials else "allow"

        decision = HandoffDecision(
            handoff_id=handoff_id,
            action=action,
            denial_records=denials,
            rule_trace=rule_trace,
        )
        if action == "allow" and session_id:
            await self._record_handoff_partner(session_id, receiving_agent_id)
        self._emit_handoff_audit(
            source_agent_id=source_agent_id,
            receiving_agent_id=receiving_agent_id,
            session_id=session_id,
            data_classifications=data_classifications,
            decision=decision,
            started=started,
        )
        return decision

    async def _record_handoff_partner(self, session_id: str, receiving_agent_id: str) -> None:
        """Let the receiving agent of an allowed handoff into the session.

        Read-merge-write on the session row's ``handoff_agents``. Best effort:
        a store that cannot take the note must not turn an allowed handoff into
        a denial — the receiving agent is refused later by the ownership gate,
        which is the safe direction to fail.
        """
        try:
            row = await self._session_get(session_id)
            prior: Any = row.get("handoff_agents")
            partners: list[str] = (
                [str(a) for a in cast("list[Any]", prior)] if isinstance(prior, list) else []
            )
            if receiving_agent_id in partners:
                return
            entry = {"handoff_agents": [*partners, receiving_agent_id]}
            if hasattr(self._session_store, "aupdate"):
                await self._session_store.aupdate(session_id, entry)  # type: ignore[attr-defined]
            else:
                sync_store: SessionStore = self._session_store  # type: ignore[assignment]
                sync_store.update(session_id, entry)
        except Exception:  # noqa: BLE001 — the gate refuses on its own if this is lost
            log.warning(
                "could not record the handoff to agent_id=%r on session %r; the "
                "receiving agent will be refused that session until the store "
                "accepts the note",
                receiving_agent_id,
                session_id,
            )

    def _emit_handoff_audit(
        self,
        *,
        source_agent_id: str,
        receiving_agent_id: str,
        session_id: str,
        data_classifications: list[str],
        decision: HandoffDecision,
        started: float,
    ) -> None:
        """Write the single ``event_type="handoff_declared"`` audit entry (AC-4.4).

        Uses the same :class:`AuditLogger` as ``arequest`` so operators
        see one JSONL stream. Non-handoff fields collapse to their
        zero values: no ``intent``, no ``routing_decisions``, no
        adapter-touching ``sources_*`` buckets. ``handoff_id`` and
        ``handoff_decision`` carry the full payload.
        """
        duration_ms = int((time.perf_counter() - started) * 1000)
        entry = AuditEntry(
            timestamp=AuditLogger.utcnow(),
            request_id=decision.handoff_id,
            agent_id=source_agent_id,
            session_id=session_id or None,
            raw_intent="",
            intent_analysis=None,
            facts_asserted_summary={"data_handoff": len(data_classifications)},
            routing_decisions=[],
            scope_constraints=[],
            denial_records=list(decision.denial_records),
            error_records=[],
            rule_trace=list(decision.rule_trace),
            sources_queried=[],
            sources_denied=[],
            sources_skipped=[],
            sources_errored=[],
            attestation_token=None,
            duration_ms=duration_ms,
            session_store_mode=self._session_store_mode(),
            event_type="handoff_declared",
            handoff_id=decision.handoff_id,
            handoff_decision=decision,
        )
        # receiving_agent_id is carried implicitly via handoff_decision context
        # on the surrounding AuditEntry; no dedicated column at this phase.
        del receiving_agent_id
        self._audit_logger.emit(entry)

    def _gate_handoff_token(
        self,
        session_token: str | None,
        source_agent_id: str,
        session_id: str,
        token_trace: list[str],
    ) -> DenialRecord | None:
        """Validate the originating agent's token for a handoff (#18).

        Returns a :class:`DenialRecord` when the handoff must be denied
        (missing / invalid / agent-mismatched token), or ``None`` when the
        token verifies — in which case a ``session-token:verified`` marker
        (kid + agent + session) is appended to ``token_trace`` so the
        handoff audit entry carries the token reference.
        """
        assert self._session_tokens is not None  # noqa: S101 — caller gates
        if not session_token:
            return DenialRecord(
                source_id=session_id,
                reason="handoff requires the originating agent's session token",
                rule_name="session-token-required",
            )
        try:
            claims = self._session_tokens.verify(session_token)
        except SessionTokenError as exc:
            request_id = str(uuid.uuid4())
            self._emit_session_token_event(
                "session_token_verification_failed",
                agent_id=source_agent_id,
                session_id=session_id,
                request_id=request_id,
                errors=[
                    ErrorRecord(
                        source_id="<broker>",
                        error_type=exc.reason_code,
                        message=str(exc),
                        trace_id=request_id,
                    )
                ],
            )
            return DenialRecord(
                source_id=session_id,
                reason=f"session token rejected: {exc.reason_code}",
                rule_name="session-token-invalid",
            )
        if claims.agent_id != source_agent_id:
            return DenialRecord(
                source_id=session_id,
                reason=(
                    f"session token agent {claims.agent_id!r} does not match "
                    f"declared source agent {source_agent_id!r}"
                ),
                rule_name="session-token-agent-mismatch",
            )
        token_trace.append(
            f"session-token:verified kid={claims.kid} "
            f"agent={claims.agent_id} session={claims.session_id}"
        )
        return None

    async def _analyze_intent(
        self,
        intent: str,
        context: dict[str, Any],
        state: _RequestState,
    ) -> None:
        """Run the wired intent analyzer; stamp LLM provenance when present.

        Two code paths (design §3.8, AC-6.5):

        * **Pattern-only (Phase-1 default).** ``self._intent_analyzer`` is a
          plain :class:`IntentAnalyzer` (sync ``analyze``). State carries a
          ``None`` :attr:`_RequestState.llm_provenance`, and the audit entry
          omits all LLM fields — preserving Phase-1 byte-identical JSONL
          (NFR-5/NFR-6).
        * **Fallback (``analysis.mode in {"llm-first","llm-only"}``).**
          ``self._intent_analyzer`` is a :class:`FallbackIntentAnalyzer`
          whose async ``analyze`` returns a ``(IntentAnalysis, LLMProvenance)``
          tuple. The provenance is stashed on ``state`` so
          :func:`_build_audit_entry` can copy each field onto the audit
          entry (FR-14, AC-6.5).
        """
        # The minted session JWS rides in ``context`` so adapters can forward it
        # downstream (AC-18.b), but every LLM provider ``json.dumps`` the whole
        # context into its prompt -- so a live credential left the process on
        # every request with ``session_tokens.enabled: true``. It appears in no
        # context contract (``analysis/llm/base.py``) and no analyzer reads it.
        context = {k: v for k, v in context.items() if k != "session_token"}
        analyzer = self._intent_analyzer
        if isinstance(analyzer, FallbackIntentAnalyzer):
            analysis, provenance = await analyzer.analyze(intent, context)
            state.intent_analysis = analysis
            state.llm_provenance = provenance
            return
        state.intent_analysis = analyzer.analyze(intent, context)

    def _refuse_if_closed(self, method: str) -> None:
        """Raise if the broker has been closed.

        Nothing checked ``_closed``, so a post-close request opened a fresh
        pool and returned real rows with a signed attestation token while the
        sink was already closed and swallowed every line -- data egress with
        no receipt. The second ``aclose()`` then returned early and those
        connections were never released.
        """
        if self._closed:
            raise RuntimeError(
                f"Broker.{method}() called after close(); the attestation sink "
                f"and session store are already shut down, so this request "
                f"could not be receipted. Build a new Broker."
            )

    def _ledger_lock(self, *keys: str) -> AbstractAsyncContextManager[None]:
        """Serialise the read-merge-write of every exposure ledger this request touches.

        A request accumulates under two keys -- the declared session and the
        caller's principal -- and both are read-modify-written. Locking only the
        session left the principal record racing: two requests declaring
        different session ids took different locks, both read the principal
        ledger empty, and the loser's exposure was dropped. That is the B3
        defeat one key over, and it is reachable with ``asyncio.gather``.

        Keys are acquired in sorted order so two requests that share one key and
        differ on the other cannot deadlock. A no-op when there is no key at all.
        """
        wanted = sorted({k for k in keys if k})
        if not wanted:
            return contextlib.nullcontext()
        return self._hold_ledger_locks(wanted)

    @contextlib.asynccontextmanager
    async def _hold_ledger_locks(self, keys: list[str]) -> AsyncIterator[None]:
        """Hold every lock in ``keys`` (already sorted) for the block's duration.

        Two locks per key, not one: the ``asyncio.Lock`` serialises this
        process's own concurrent requests, and the session store's lock — when
        the store has one, i.e. when it is shared — serialises this replica
        against every other replica writing the same ledger.
        """
        # ``alock_all`` holds every key on one pooled connection. Entering the
        # store's per-key lock separately took a connection per key, which made
        # the store's ceiling ``pool_max_size / keys-per-request``.
        store_lock_all = getattr(self._session_store, "alock_all", None)
        store_lock = getattr(self._session_store, "alock", None)
        # The wait is bounded because the hold is long: the lock spans the whole
        # pipeline, source query included, so one caller's concurrent requests
        # run strictly one at a time. That serialisation is the guarantee -- two
        # requests that both read the ledger empty both pass a cumulative cap --
        # but ``SourceConfig.timeout_s`` only starts once the lock is won, so
        # the queueing used to sit outside every deadline the config had.
        budget = self._config.session_store.lock_timeout_s
        # Resolved once, from the store rather than from the config: with
        # ``session_store.dsn`` unset the broker falls back to ``TEST_PG_DSN``,
        # and the config would then name nothing while the process dials
        # something. ``None`` for a store with no remote dependency.
        endpoint = self._session_store_endpoint()
        deadline = None if budget is None else time.monotonic() + budget

        async def _hold(cm: AbstractAsyncContextManager[Any], what: str) -> Any:
            if deadline is None:
                return await stack.enter_async_context(cm)
            remaining = deadline - time.monotonic()
            if remaining <= 0:
                raise BrokerBusyError(_busy_message(what, budget, endpoint), endpoint=endpoint)
            try:
                return await asyncio.wait_for(stack.enter_async_context(cm), remaining)
            except TimeoutError as exc:
                raise BrokerBusyError(
                    _busy_message(what, budget, endpoint), endpoint=endpoint
                ) from exc

        async with contextlib.AsyncExitStack() as stack:
            for key in keys:
                await _hold(self._session_locks.setdefault(key, asyncio.Lock()), key)
            if store_lock_all is not None:
                await _hold(store_lock_all(keys), ", ".join(keys))
            elif store_lock is not None:
                for key in keys:
                    await _hold(store_lock(key), key)
            yield

    async def _run_pipeline(
        self,
        agent_id: str,
        intent: str,
        context: dict[str, Any],
        state: _RequestState,
    ) -> None:
        """Happy-path pipeline body — mutates ``state`` in place."""
        async with self._ledger_lock(state.session_id, state.principal_id):
            with broker_span(SPAN_INTENT_ANALYSIS):
                await self._analyze_intent(intent, context, state)
            with broker_span(SPAN_FATHOM_ROUTING):
                await self._route(agent_id, context, state)
            self._merge_context_scope_constraints(context, state)
            self._apply_temporal_filter(state)
            with broker_span(SPAN_ADAPTER_FAN_OUT):
                tasks, task_source_ids = await self._build_adapter_jobs(state, context)
                successful = await self._gather_adapter_results(
                    state,
                    tasks,
                    task_source_ids,
                )
            with broker_span(SPAN_SYNTHESIS):
                state.data = self._synthesizer.merge(successful)
            if self._attestation is not None:
                with broker_span(SPAN_ATTESTATION_SIGN):
                    token, scope_hash_version, nautilus_payload = self._sign(
                        request_id=state.request_id,
                        agent_id=agent_id,
                        sources_queried=state.sources_queried,
                        scope_by_source=state.scope_by_source,
                        rule_trace=state.rule_trace,
                        session_id=state.session_id,
                        principal_id=state.principal_id,
                        response=state.data or None,
                        source_response_hashes=state.source_response_hashes or None,
                    )
                state.attestation_token = token
                state.scope_hash_version = scope_hash_version
                await self._emit_attestation(token, nautilus_payload, state.request_id)
            await self._update_session(state, context)

    async def _emit_attestation(
        self,
        token: str,
        nautilus_payload: dict[str, Any],
        request_id: str,
    ) -> None:
        """Store-and-forward the attestation payload; NEVER fails the hot path.

        Wraps ``self._attestation_sink.emit(...)`` in ``try/except Exception``
        and logs at WARNING on failure (AC-14.5, NFR-16). The audit entry is
        emitted regardless — the audit-first invariant means a sink outage
        cannot gate the request response. Per design §3.14 the token is
        still returned on :class:`BrokerResponse` (AC-14.4).

        Emits an ``attestation_emitted`` audit event (AC-19.b) after a
        successful sink write attempt. The event is schema_version=2 per
        design §4.9 / shared.md line 799.
        """
        payload = AttestationPayload(
            token=token,
            nautilus_payload=nautilus_payload,
            emitted_at=datetime.now(tz=UTC),
        )
        try:
            await self._attestation_sink.emit(payload)
        except Exception as exc:  # noqa: BLE001 — audit-first invariant (AC-14.5)
            log.warning("attestation_sink.emit failed: %s", exc)
            _metrics.attestation_total.add(1, {"outcome": "sink_error"})
        else:
            _metrics.attestation_total.add(1, {"outcome": "emitted"})
        # AC-19.b — emit attestation_emitted audit event regardless of sink outcome.
        response_hash: str | None = nautilus_payload.get("response_hash")
        hash_skipped: bool = bool(nautilus_payload.get("hash_skipped", False))
        legacy: bool = response_hash is None and not hash_skipped
        # AC-19 / issue #56 review — persist the per-source digests so the
        # ``source_response_hashes`` claim is independently verifiable from the
        # audit log, not only from the signed JWT.
        source_response_hashes: dict[str, str] | None = nautilus_payload.get(
            "source_response_hashes"
        )
        self._audit_logger.emit(
            AuditEntry(
                timestamp=AuditLogger.utcnow(),
                request_id=request_id,
                agent_id="",
                facts_asserted_summary={},
                denial_records=[],
                error_records=[],
                rule_trace=[],
                sources_queried=[],
                sources_denied=[],
                sources_errored=[],
                duration_ms=0,
                event_type="attestation_emitted",
                schema_version=2,
                trace_id=request_id,
                raw_response_hash=response_hash if not legacy else None,
                source_response_hashes=source_response_hashes,
            )
        )

    @staticmethod
    def _merge_context_scope_constraints(
        context: dict[str, Any],
        state: _RequestState,
    ) -> None:
        """Fold ``context["scope_constraints"]`` into ``state.scope_by_source``.

        Additive channel so callers (notably the POC integration test) can
        attach row-level predicates that carry ``expires_at`` / ``valid_from``
        windows without a dedicated rule. Values must be
        :class:`ScopeConstraint` instances (or dicts coercible into one); the
        merge is a straight append per source_id so router-emitted constraints
        are preserved. A missing / empty key is a no-op (NFR-5).
        """
        raw: Any = context.get("scope_constraints")
        if not raw:
            return
        items: list[Any] = list(raw) if isinstance(raw, (list, tuple)) else [raw]  # pyright: ignore[reportUnknownArgumentType]
        for item in items:
            try:
                constraint = (
                    item
                    if isinstance(item, ScopeConstraint)
                    else ScopeConstraint.model_validate(item)
                )
            except ValidationError as exc:
                # Caller input, so a typo is a client error. Unwrapped, the
                # ValidationError left arequest as a 500 with no audit entry.
                reasons = "; ".join(
                    f"{'.'.join(str(p) for p in e['loc']) or '(root)'}: {e['msg']}"
                    for e in exc.errors()
                )
                raise ValueError(
                    f"context['scope_constraints'] entry is not a scope constraint: {reasons}"
                ) from exc
            state.scope_by_source.setdefault(constraint.source_id, []).append(constraint)

    def _apply_temporal_filter(self, state: _RequestState) -> None:
        """Deny any source that loses a scope constraint to the temporal window.

        Wires :meth:`TemporalFilter.apply` into ``arequest`` per design
        §3.9 / FR-17. Dropped constraints produce ``scope-expired``
        :class:`DenialRecord` entries appended to ``state.denial_records``.
        """
        filtered, temporal_denials = TemporalFilter.apply(
            state.scope_by_source,
            now=datetime.now(tz=UTC),
        )
        state.scope_by_source = filtered
        if temporal_denials:
            self._record_temporal_denials(state, temporal_denials)

    @staticmethod
    def _record_temporal_denials(
        state: _RequestState,
        denials: list[DenialRecord],
    ) -> None:
        """Fold temporal-filter denials into request state, denying the source.

        A scope constraint is a *restriction*, so dropping one can only widen
        what the adapter returns. The fan-out treats an empty scope list as
        "no restrictions", which made an expired grant strictly more
        permissive than a live one: the adapter ran with no WHERE clause and
        the caller got the rows the constraint existed to hide.

        The source is therefore denied for this request rather than queried
        under whatever restrictions happen to survive — the fail-closed
        reading of the window that :mod:`nautilus.core.temporal` documents.
        Re-granting means issuing a fresh, unexpired constraint.
        """
        state.denial_records = list(state.denial_records) + list(denials)
        expired_ids = {d.source_id for d in denials}
        state.routing_decisions = [
            rd for rd in state.routing_decisions if rd.source_id not in expired_ids
        ]
        for source_id in expired_ids:
            state.scope_by_source.pop(source_id, None)
        state.sources_denied = sorted(set(state.sources_denied) | expired_ids)
        state.sources_skipped = [s for s in state.sources_skipped if s not in expired_ids]
        state.skip_records = [r for r in state.skip_records if r.source_id not in expired_ids]

    async def _route(self, agent_id: str, context: dict[str, Any], state: _RequestState) -> None:
        """Invoke the Fathom router and classify sources into queried/denied/skipped.

        Prefers the async :meth:`AsyncSessionStore.aget` when the implementer
        provides it (design §3.2 — Phase-2 broker prefers async).
        """
        session_state = await self._session_get(state.session_id) if state.session_id else {}
        if state.session_id:
            session_state.setdefault("id", state.session_id)
        state.session_row = dict(session_state)
        self._gate_session_ownership(agent_id, state)
        # Fold the caller's principal-wide exposure in before the rules see it.
        # Without this a caller escaped cumulative escalation by declaring a
        # session id it had never used -- the ledger is the control, and the
        # key was the caller's to pick (§4.15). The union is the policy input
        # only; each record is written back with its own history.
        state.principal_row = await self._session_get(state.principal_id)
        session_state = _merge_exposure(state.principal_row, session_state)
        state.session_state = dict(session_state)
        route_result = self._router.route(
            agent_id=agent_id,
            context=context,
            intent=state.intent_analysis,
            sources=self._registry.sources,
            session=session_state,
            agent_registry=self._agent_registry,
        )
        state.apply_route_result(route_result)
        # One per decision, not one per request: incremented at the call site it
        # made ``nautilus_routing_decisions_total`` a second copy of
        # ``nautilus_requests_total``, so the dashboard panel plotted the
        # request rate under a routing label.
        _metrics.routing_decisions_total.add(len(route_result.routing_decisions))
        _metrics.fathom_evaluation_duration.record(route_result.duration_us / 1_000_000)
        for denial in route_result.denial_records:
            _metrics.scope_denials_total.add(1, {"rule_name": denial.rule_name})
        exposure_facts = route_result.facts_asserted_summary.get("session_exposure", 0)
        if exposure_facts:
            _metrics.session_exposure_flags_total.add(exposure_facts)
        state.sources_denied = sorted({d.source_id for d in state.denial_records})
        selected_ids = {rd.source_id for rd in state.routing_decisions}
        denied_ids = set(state.sources_denied)
        state.sources_skipped = sorted(
            s.id for s in self._registry if s.id not in selected_ids and s.id not in denied_ids
        )
        state.skip_records = self._skip_records(state)
        state.denial_records = [self._annotate_denial(d, state) for d in state.denial_records]
        if log.isEnabledFor(logging.DEBUG):
            self._log_route(state)

    def _log_route(self, state: _RequestState) -> None:
        """Put this request's routing reasoning on the process log (DEBUG).

        The reasoning existed only in the audit entry's ``rule_trace``, written
        after the request finished and readable only by whoever holds the audit
        file -- so "two brokers behind one address answered the same request
        with different ``sources_skipped``" was a question the process log could
        not answer at any verbosity. One summary line (what the intent parsed
        to, how many rules were evaluated, which fired), then one line per
        configured source giving its disposition and the reason the response
        already carries.

        Called under :meth:`~logging.Logger.isEnabledFor` because the loop is
        O(sources) of formatting on the path every request takes, and it has to
        cost nothing at the default level.
        """
        log.debug(
            "request %r: intent %r parsed to data types %r; %d rules in force, %d fired: %s",
            state.request_id,
            state.intent_analysis.raw_intent,
            state.intent_analysis.data_types_needed,
            len(self._router.rules_in_force()),
            len(state.rule_trace),
            state.rule_trace,
        )
        # Later assignments win, and the three sets are disjoint by
        # construction (``sources_skipped`` excludes both of the others).
        dispositions = {rd.source_id: ("queried", rd.reason) for rd in state.routing_decisions}
        dispositions.update({d.source_id: ("denied", d.reason) for d in state.denial_records})
        dispositions.update({s.source_id: ("skipped", s.reason) for s in state.skip_records})
        for source_id, (disposition, reason) in sorted(dispositions.items()):
            log.debug(
                "request %r: source %r %s -- %s",
                state.request_id,
                source_id,
                disposition,
                reason,
            )

    def _gate_session_ownership(self, agent_id: str, state: _RequestState) -> None:
        """Refuse a session that belongs to somebody else (§4.15 follow-on).

        §4.15 closed the *reset* — a caller could not wipe its own history by
        picking a new session id, because exposure also accumulates under a
        principal key it does not choose. The per-session record stayed
        write-shared: a session id is a name, and any credential that knew one
        could add to its ledger. That is a denial of service against another
        caller on the same broker (pour PII in, and cumulative escalation
        denies them every source) and a side channel out of it (which rules
        fire reports back what the ledger now holds).

        So the first principal to touch a session owns it. A session
        legitimately spans agents — a handoff is keyed on
        ``(session_id, source_agent, receiving_agent)`` — so the receiving
        agent of a handoff that was *declared and allowed in this session*
        joins, and inherits what the session has seen. Everyone else is
        refused before the router runs and before any adapter is touched.

        Only requests that arrived with a caller identity are gated. An
        in-library call presents no credential and no peer: there is no second
        caller for the boundary to be between, and one process running several
        of its own agents through one session is the supported shape.
        """
        if not state.caller_bound:
            return
        owner = str(state.session_row.get("owner_principal") or "")
        if not owner or owner == state.principal_id:
            return
        joined = state.session_row.get("joined_principals")
        if isinstance(joined, list) and state.principal_id in joined:
            return
        partners = state.session_row.get("handoff_agents")
        if isinstance(partners, list) and agent_id in partners:
            state.session_joining = True
            return
        raise SessionNotOwnedError(
            f"session_not_yours: session {state.session_id!r} belongs to "
            f"another principal. A session id is not a credential — either use "
            f"your own, or have its owner declare a handoff to "
            f"agent_id={agent_id!r} in it first."
        )

    def _annotate_denial(self, denial: DenialRecord, state: _RequestState) -> DenialRecord:
        """Say whether a refusal concerned the request, and what would work.

        Two things a denial cannot say for itself. The rules refuse a source on
        its own terms, with no view of whether the intent ever wanted it — so
        relevance is decided here, from the same data-type overlap
        :meth:`_skip_records` already uses. And a purpose refusal that never
        names an acceptable purpose is a dead end: a model measured against
        this surface spent 21 of 21 attempts on one guess, because nothing told
        it the guess was in the wrong vocabulary rather than merely refused.
        The allowed set is metadata the operator wrote, published already at
        ``GET /v1/sources``; the rule cannot interpolate it (fathom emits
        ``reason`` verbatim), so the broker appends it.
        """
        try:
            source = self._registry.get(denial.source_id)
        except Exception:  # noqa: BLE001 — a denial can name a source we cannot resolve
            return denial

        needed = set(state.intent_analysis.data_types_needed)
        # No identifiable data types means nothing to be relevant *to*: the
        # request was unanswerable, not refused.
        relevant = bool(needed) and bool(set(source.data_types) & needed)

        reason = denial.reason
        allowed = list(source.allowed_purposes or [])
        if allowed and "purpose" in reason.lower():
            names = ", ".join(sorted(allowed))
            reason = f"{reason} (source '{source.id}' allows purposes: {names})"
        return denial.model_copy(update={"relevant": relevant, "reason": reason})

    def _skip_records(self, state: _RequestState) -> list[SkipRecord]:
        """Say why each source in ``sources_skipped`` took no part.

        Almost always the data types: the source has nothing the intent asked
        for. Naming that turns "my source is missing from the response" from a
        support question into a one-line config fix.
        """
        needed = set(state.intent_analysis.data_types_needed)
        records: list[SkipRecord] = []
        for source_id in state.sources_skipped:
            offered = set(self._registry.get(source_id).data_types)
            if needed and not (offered & needed):
                reason = (
                    f"no data type in common with the intent: source '{source_id}' offers "
                    f"{sorted(offered)}, the request needed {sorted(needed)}"
                )
            else:
                reason = f"no routing rule selected source '{source_id}' for this request"
            records.append(SkipRecord(source_id=source_id, reason=reason))
        return records

    async def _update_session(self, state: _RequestState, context: dict[str, Any]) -> None:
        """Cumulative-exposure bookkeeping (design §3.9 — update at end).

        Prefers :meth:`AsyncSessionStore.aupdate` when available; falls back
        to the sync Phase-1 surface for :class:`InMemorySessionStore`.
        """
        if not state.session_id:
            return
        common: dict[str, Any] = {
            "last_request_id": state.request_id,
            "last_sources_queried": state.sources_queried,
        }
        # Two records, each folded onto its OWN prior. The session record is
        # what an operator reads and what TTL ages out, so it must describe that
        # session; the principal record is what survives the caller picking a
        # new session id. Writing the merged union to both made every session
        # row claim the caller's whole history.
        session_entry = common | self._accumulate_exposure(state, context, state.session_row)
        # Claim on first touch, and record a handoff partner that joined, so
        # the gate above has an owner to compare against next time. Written
        # only after the request survived, so a refused one claims nothing.
        if not state.caller_bound:
            pass
        elif not state.session_row.get("owner_principal"):
            session_entry["owner_principal"] = state.principal_id
        elif state.session_joining:
            prior_joined: Any = state.session_row.get("joined_principals")
            joined: list[str] = (
                [str(p) for p in cast("list[Any]", prior_joined)]
                if isinstance(prior_joined, list)
                else []
            )
            if state.principal_id not in joined:
                session_entry["joined_principals"] = [*joined, state.principal_id]
        principal_entry = common | self._accumulate_exposure(state, context, state.principal_row)
        if hasattr(self._session_store, "aupdate"):
            await self._session_store.aupdate(state.session_id, session_entry)  # type: ignore[attr-defined]
            await self._session_store.aupdate(state.principal_id, principal_entry)  # type: ignore[attr-defined]
            return
        # Sync fallback — only reachable when the store implements the Phase-1
        # :class:`SessionStore` Protocol (``update``). The union type widens to
        # include :class:`AsyncSessionStore` so pyright needs the explicit cast.
        sync_store: SessionStore = self._session_store  # type: ignore[assignment]
        sync_store.update(state.session_id, session_entry)
        sync_store.update(state.principal_id, principal_entry)

    def _accumulate_exposure(
        self, state: _RequestState, context: dict[str, Any], prior: dict[str, Any]
    ) -> dict[str, Any]:
        """Fold this request's exposure into ``prior``'s cumulative slots.

        :meth:`FathomRouter._assert_session` reads ``data_types_seen``,
        ``sources_visited``, ``pii_sources_accessed_list``,
        ``pii_sources_accessed``, ``purpose_start_ts`` and
        ``purpose_ttl_seconds`` off the session dict, and nothing wrote any of
        them: every request asserted zero ``session_exposure`` facts, so
        cross-request aggregation policy never saw accumulated exposure and
        the shipped ``purpose-expired-deny`` rule could never fire.

        Accumulation is over sources actually **queried** — a denied or
        skipped source exposed nothing. The purpose window starts on the
        first request of a session and restarts whenever the declared purpose
        changes, since a new purpose is a new grant.
        """
        by_id = {s.id: s for s in self._registry}
        queried = [by_id[sid] for sid in state.sources_queried if sid in by_id]

        visited = _merge_unique(prior.get("sources_visited"), state.sources_queried)
        data_types = _merge_unique(
            prior.get("data_types_seen"), [dt for s in queried for dt in s.data_types]
        )
        pii_sources = _merge_unique(
            prior.get("pii_sources_accessed_list"),
            [s.id for s in queried if _PII_DATA_TYPE in s.data_types],
        )
        entry: dict[str, Any] = {
            "sources_visited": visited,
            "data_types_seen": data_types,
            "pii_sources_accessed_list": pii_sources,
            "pii_sources_accessed": len(pii_sources),
        }

        ttl = self._config.session_store.purpose_ttl_seconds
        if ttl > 0:
            purpose = str(context.get("purpose", ""))
            restarted = purpose != str(prior.get("purpose", "")) or not prior.get(
                "purpose_start_ts"
            )
            entry["purpose"] = purpose
            entry["purpose_ttl_seconds"] = float(ttl)
            entry["purpose_start_ts"] = (
                time.time() if restarted else float(prior["purpose_start_ts"])
            )
        return entry

    async def _session_get(self, session_id: str) -> dict[str, Any]:
        """Read session state — async path when the store provides it."""
        if hasattr(self._session_store, "aget"):
            return await self._session_store.aget(session_id)  # type: ignore[attr-defined]
        sync_store: SessionStore = self._session_store  # type: ignore[assignment]
        return sync_store.get(session_id)

    async def _build_adapter_jobs(
        self,
        state: _RequestState,
        context: dict[str, Any],
    ) -> tuple[list[asyncio.Task[AdapterResult | None]], list[str]]:
        """Lazy-connect + spawn one task per routed source (design §3.1).

        Per SOURCE, not per routing decision: two rules can route the same
        source (a user rule alongside a built-in one), and each fact would
        otherwise execute the adapter again — double cost and double side
        effects for a non-idempotent source — and duplicate the id in the
        signed ``sources_queried`` claim.
        """
        tasks: list[asyncio.Task[AdapterResult | None]] = []
        task_source_ids: list[str] = []
        seen: set[str] = set()
        for rd in state.routing_decisions:
            if rd.source_id in seen:
                continue
            seen.add(rd.source_id)
            # 4.18 -- connect runs inside the task, not here: awaiting it in
            # this loop meant one source with a hanging connect() delayed the
            # fan-out of every source after it in the list.
            tasks.append(asyncio.create_task(self._run_source(rd.source_id, state, context)))
            task_source_ids.append(rd.source_id)
        return tasks, task_source_ids

    async def _run_source(
        self, source_id: str, state: _RequestState, context: dict[str, Any]
    ) -> AdapterResult | None:
        """Connect and execute one source under its own wall-clock budget.

        Returns ``None`` when :meth:`_prepare_adapter` already recorded a
        per-source error (unknown, quarantined, or failed to connect).
        """
        source = self._registry.get(source_id)
        timeout = getattr(source, "timeout_s", None)
        try:
            async with asyncio.timeout(timeout):
                adapter = await self._prepare_adapter(source_id, state)
                if adapter is None:
                    return None
                scope = state.scope_by_source.get(source_id, [])
                return await self._execute_adapter(
                    adapter, source_id, state.intent_analysis, scope, context
                )
        except TimeoutError:
            # A budget spent without ever getting connected is a connect
            # failure, whichever side of the deadline noticed: it is the case
            # that costs every later request the same wait, so it arms the
            # cooldown in :meth:`_prepare_adapter`.
            if source_id not in self._connected_adapters:
                self._connect_failures[source_id] = time.monotonic()
            raise

    async def _prepare_adapter(self, source_id: str, state: _RequestState) -> Adapter | None:
        """Resolve and lazy-connect the adapter for ``source_id``.

        Records per-source :class:`ErrorRecord`\\ s on lookup / connect failure
        and returns ``None`` so the caller can skip this source.

        Quarantined adapters return an ADAPTER_QUARANTINED error record so the
        broker never silently routes to a drifted adapter (AC-21.e, PM Q3).
        """
        adapter = self._adapters.get(source_id)
        if source_id in self._quarantined_adapters and not (
            adapter is not None and await self._clear_quarantine_if_acked(source_id, adapter)
        ):
            self._fail_source(
                state,
                _source_error(
                    source_id,
                    "ADAPTER_QUARANTINED",
                    f"Adapter '{source_id}' is quarantined due to major schema drift. "
                    "Operator must acknowledge drift via schema-ack before resuming.",
                    state.request_id,
                ),
            )
            return None

        if adapter is None:
            self._fail_source(
                state,
                _source_error(
                    source_id,
                    "AdapterError",
                    f"No adapter registered for source '{source_id}'",
                    state.request_id,
                ),
            )
            return None
        if source_id in self._connected_adapters and source_id not in self._quarantined_adapters:
            return adapter
        failed_at = self._connect_failures.get(source_id)
        if failed_at is not None:
            waited = time.monotonic() - failed_at
            if waited < self.connect_cooldown_s:
                self._fail_source(
                    state,
                    _source_error(
                        source_id,
                        "AdapterError",
                        f"connect() failed {waited:.1f}s ago; not retried for another "
                        f"{self.connect_cooldown_s - waited:.1f}s",
                        state.request_id,
                    ),
                )
                return None
            del self._connect_failures[source_id]
        async with self._connect_locks.setdefault(source_id, asyncio.Lock()):
            # Re-check under the lock: the request that lost the race must
            # reuse the winner's pool, not build a second one -- and must see
            # the winner's drift verdict, which is why the gate below runs
            # before the adapter is published as connected. Checking after the
            # publish let the loser skip the gate and query a drifted source.
            if source_id in self._connected_adapters:
                drifted = source_id in self._quarantined_adapters
            else:
                # What this source dials, and how long dialling took. A source
                # pointed at a stopped backend answered 200 with a
                # ``sources_errored[]`` entry and left nothing on any
                # ``nautilus.*`` logger until it failed; a source that connects
                # normally left nothing at all, so "which backends did this
                # request actually open, and which one was slow" had no answer.
                # ``endpoint`` is rebuilt by allowlist from the connection
                # string, so the DSN password beside it cannot ride out here.
                endpoint = self._source_endpoint(source_id) or "no remote endpoint"
                log.debug(
                    "request %r: source %r dialling %s via %s",
                    state.request_id,
                    source_id,
                    endpoint,
                    type(adapter).__name__,
                )
                dial_started = time.monotonic()
                try:
                    await adapter.connect(self._registry.get(source_id))
                except Exception as exc:  # noqa: BLE001 — surface as per-source error
                    self._connect_failures[source_id] = time.monotonic()
                    self._fail_source(
                        state,
                        _source_error(
                            source_id,
                            type(exc).__name__,
                            f"connect() failed: {exc}",
                            state.request_id,
                        ),
                    )
                    return None
                self._connect_failures.pop(source_id, None)
                log.debug(
                    "request %r: source %r connected to %s in %.1f ms",
                    state.request_id,
                    source_id,
                    endpoint,
                    (time.monotonic() - dial_started) * 1000,
                )
                # Connect is lazy, so this is where an adapter's schema is
                # first reachable — checking only in setup() left the gate dead.
                drifted = await self._check_adapter_schema(source_id, adapter)
                self._connected_adapters.add(source_id)
        if drifted:
            self._fail_source(
                state,
                _source_error(
                    source_id,
                    "ADAPTER_QUARANTINED",
                    f"Adapter '{source_id}' is quarantined due to major schema drift. "
                    "Operator must acknowledge drift via schema-ack before resuming.",
                    state.request_id,
                ),
            )
            return None
        return adapter

    def _source_endpoint(self, source_id: str) -> str | None:
        """What ``source_id`` dials, as ``scheme://host[:port]``, or ``None``.

        Rebuilt from the source's ``connection`` by
        :func:`~nautilus.config.models.redact_connection`, which copies out
        scheme/host/port and nothing else, so a DSN password or a URL token
        cannot ride out of the config file on an error record.
        """
        try:
            source = self._registry.get(source_id)
        except KeyError:
            return None  # an id with no source: nothing to name
        return redact_connection(source.connection)

    async def aprobe_source(self, source_id: str) -> SourceProbe:
        """Dial what ``source_id`` connects to and report whether it answers.

        Sources do not gate readiness -- a broker that can still serve three of
        four is ready, and draining it would remove the only thing that can
        answer for the three -- so nothing at HTTP level distinguished a
        healthy broker from one whose every backend was gone. This is what
        does: it opens the source's ``scheme://host[:port]``, makes the server
        answer (see :data:`_PROBE_HELLO`), and is bounded by that source's own
        ``timeout_s``, capped at :data:`_PROBE_TIMEOUT_CEILING_S`.

        It is a deliberately smaller claim than "this source will serve a
        query": a server that answers says nothing about credentials, about a
        database that is up but still recovering, or about a model the endpoint
        does not host. It is exactly the difference between a backend that is
        *gone* and one that is there, which is the question an operator has.

        ``reachable`` is ``None`` -- never ``True`` -- when there was nothing to
        dial. A ``static`` source has no address, and a probe that answers for
        what it did not check is the failure this exists to end.
        """
        endpoint = self._source_endpoint(source_id)
        if endpoint is None:
            return SourceProbe(None, None, "no remote endpoint")
        parts = urlsplit(endpoint)
        host = parts.hostname
        # No entry in the table means no probe: guessing a port is the same
        # mistake ``redact_connection`` refuses to make when it guesses a host.
        port = parts.port or _DEFAULT_PORTS.get(parts.scheme)
        if host is None or port is None:
            return SourceProbe(
                endpoint,
                None,
                f"no port in {endpoint!r} and no default for scheme {parts.scheme!r}",
            )
        try:
            budget = self._registry.get(source_id).timeout_s
        except KeyError:  # pragma: no cover -- _source_endpoint already returned
            budget = None
        timeout_s = min(budget or _PROBE_TIMEOUT_CEILING_S, _PROBE_TIMEOUT_CEILING_S)
        try:
            note = await asyncio.wait_for(_dial(host, port, parts.scheme), timeout_s)
        except TimeoutError:
            return SourceProbe(
                endpoint, False, f"no answer from {endpoint} within {timeout_s:.1f}s"
            )
        # Total, deliberately. This is the whole of one source's answer and
        # ``GET /v1/adapters?probe=true`` gathers every source's, so anything
        # that escapes here 500s the endpoint and discards every healthy
        # sibling's result. Enumerating classes did not hold: ``except OSError``
        # missed ``UnicodeEncodeError`` from a host the resolver cannot encode,
        # and adding ``UnicodeError`` still missed ``OverflowError`` from a port
        # above 65535. A malformed address is a probe failure, not a crash, and
        # the class name is what the operator needs to see.
        except Exception as exc:
            return SourceProbe(endpoint, False, f"{type(exc).__name__}: {exc}")
        return SourceProbe(endpoint, True, note)

    def _session_store_endpoint(self) -> str | None:
        """What the session store dials, as ``scheme://host[:port]``, or ``None``.

        The source-side twin of :meth:`_source_endpoint`. ``None`` for a store
        with no remote dependency -- ``backend: memory`` holds an in-process
        dict and ``backend: sqlite`` a local file, and neither has an address
        an operator could dial -- and for a Postgres DSN with no host in it.
        """
        endpoint: object = getattr(self._session_store, "endpoint", None)
        return endpoint if isinstance(endpoint, str) else None

    def _timeout_message(self, source_id: str) -> str:
        """Message for a source that spent its whole wall-clock budget.

        ``TimeoutError`` carries no text, so interpolating it rendered
        ``exceeded the source's timeout_s budget: `` -- a sentence ending in a
        colon and nothing after it, and that was the entire text an operator
        got. Name the budget that was spent instead. ``timeout_s: null`` means
        the broker set no deadline, so a ``TimeoutError`` from that source came
        from inside the driver and there is no budget to quote.
        """
        budget = self._registry.get(source_id).timeout_s
        if budget is None:
            return "exceeded the source's timeout_s budget"
        return f"exceeded the source's timeout_s budget of {budget}s"

    def _fail_source(self, state: _RequestState, record: ErrorRecord) -> None:
        """Name the backend, log the failure, bucket it. One place, all paths.

        Every per-source failure -- unknown id, connect cooldown, connect
        error, schema quarantine, wall-clock timeout, adapter contract
        violation, and the typed record an adapter returns -- converges here
        before it reaches ``state.errored``, which is what both the response's
        ``sources_errored`` and the audit entry's ``error_records`` are built
        from.

        Two things happen that did not before, and both answer the same
        question -- *which dependency broke?*:

        * ``record.endpoint`` is filled from the source's ``connection``, so
          the address is in the durable audit trail. A ``source_id`` is the
          operator's label for a dependency, not the dependency's address, and
          the address appeared in nothing the broker wrote: an unreachable
          backend produced a 200, an error bucket, and no way to learn what
          was unreachable without already knowing.
        * One ``WARNING`` is emitted on the process's own logger. The broker's
          own loggers said nothing at all about a dead source at any level,
          including ``--log-level debug``; the only lines naming the host came
          from ``httpcore``/``httpx`` and were emitted identically on success,
          so they distinguished nothing.

        The endpoint is credential-free by construction -- see
        :meth:`_source_endpoint`. It is also, deliberately, in the response the
        requesting agent reads, not only in the operator's copy: the agent
        already receives ``source_id`` and the driver's own error text, and an
        endpoint an operator can act on is worth more than the hostname
        secrecy it trades away.
        """
        if record.endpoint is None:
            record = record.model_copy(update={"endpoint": self._source_endpoint(record.source_id)})
        log.warning(
            "source %r failed (endpoint=%s, error_type=%s, trace_id=%s): %s",
            record.source_id,
            record.endpoint or "<none configured>",
            record.error_type,
            record.trace_id,
            record.message,
        )
        state.errored.append(record)

    async def _gather_adapter_results(
        self,
        state: _RequestState,
        tasks: list[asyncio.Task[AdapterResult | None]],
        task_source_ids: list[str],
    ) -> list[AdapterResult]:
        """Await ``tasks`` and split into successes / errors (into state)."""
        raw = await asyncio.gather(*tasks, return_exceptions=True)
        successful: list[AdapterResult] = []
        for source_id, res in zip(task_source_ids, raw, strict=True):
            if isinstance(res, BaseException):
                message = (
                    self._timeout_message(source_id) if isinstance(res, TimeoutError) else str(res)
                )
                self._fail_source(
                    state, _source_error(source_id, type(res).__name__, message, state.request_id)
                )
                continue
            if res is None:
                # _prepare_adapter already recorded why this source is out.
                continue
            if not isinstance(res, AdapterResult):  # pyright: ignore[reportUnnecessaryIsInstance]
                coerced = _coerce_adapter_result(res)
                if coerced is None:
                    # B5 -- ``res.error`` below is outside any try block, so
                    # an adapter that *returns* the wrong type (rather than
                    # raising) took down every co-queried source with it.
                    self._fail_source(
                        state,
                        _source_error(
                            source_id,
                            "AdapterContractError",
                            f"adapter returned {type(res).__name__}, expected AdapterResult",
                            state.request_id,
                        ),
                    )
                    continue
                res = coerced
            if res.error is not None:
                # ``_execute_adapter`` cannot see the request id, so it leaves
                # trace_id empty for the typed errors and this is the caller
                # that fills it — otherwise one request's audit entry mixes
                # correlated and uncorrelated rows.
                self._fail_source(
                    state,
                    res.error
                    if res.error.trace_id
                    else res.error.model_copy(update={"trace_id": state.request_id}),
                )
                _metrics.adapter_errors_total.add(
                    1, {"source_id": source_id, "error_type": res.error.error_type}
                )
                continue
            res = self._bound_rows(source_id, res)
            successful.append(res)
            state.sources_queried.append(source_id)
            if res.truncated:
                state.truncated_sources.append(source_id)
            # Per-source chain-of-custody hash (issue #19, AC-19), computed
            # centrally by the broker over each source's raw rows at this
            # pre-synthesis boundary. The digest is ALWAYS derived from the rows
            # the broker actually returns and attests; the adapter does not (and
            # cannot) supply it, so a malicious or buggy adapter cannot inject an
            # arbitrary hash into the signed attestation token (issue #56 review).
            # Non-deterministic adapters (llm) declare the ``non_deterministic``
            # capability and are omitted so ``_sign`` still emits
            # ``hash_skipped=True`` (AC-19.g).
            if not self._is_non_deterministic(source_id):
                state.source_response_hashes[source_id] = compute_response_hash(res.rows)
        return successful

    def _bound_rows(self, source_id: str, result: AdapterResult) -> AdapterResult:
        """Hold every adapter to the source's ``max_response_bytes``.

        The adapters that can stop reading do -- postgres streams and breaks at
        the budget rather than materialising the whole result first, which is
        the only thing that bounds the peak. This is the backstop for the ones
        that cannot, and the single place the rule is stated for all of them:
        the row cap was the only bound in the code, and a row can be any size.
        """
        try:
            budget = self._registry.get(source_id).max_response_bytes
        except Exception:  # noqa: BLE001 — never fail a served source over a budget lookup
            return result
        rows, trimmed = bounded_rows(result.rows, budget)
        if not trimmed:
            return result
        log.warning(
            "source %r returned more than max_response_bytes (%s); kept %d of %d rows",
            source_id,
            budget,
            len(rows),
            len(result.rows),
        )
        return result.model_copy(update={"rows": rows, "truncated": True})

    def _is_non_deterministic(self, source_id: str) -> bool:
        """True iff ``source_id``'s adapter declares the ``non_deterministic``
        capability (e.g. the llm adapter); such sources are never hashed (AC-19.g).
        """
        return "non_deterministic" in getattr(
            self._adapters.get(source_id), "capabilities", set[str]()
        )

    def _build_response(self, state: _RequestState) -> BrokerResponse:
        """Materialize the user-facing :class:`BrokerResponse` from ``state``."""
        return BrokerResponse(
            request_id=state.request_id,
            data=state.data,
            sources_queried=sorted(state.sources_queried),
            truncated_sources=sorted(state.truncated_sources),
            sources_denied=state.sources_denied,
            sources_skipped=state.sources_skipped,
            skip_records=state.skip_records,
            sources_errored=state.errored,
            denial_records=list(state.denial_records),
            rule_trace=list(state.rule_trace),
            scope_restrictions=state.scope_by_source,
            attestation_token=state.attestation_token,
            duration_ms=state.duration_ms(),
            fact_set_hash=state.fact_set_hash,
            session_token=state.session_token,
            source_info=self._source_info(state.sources_queried),
            ruleset_hash=self.ruleset_hash,
        )

    def _source_info(self, source_ids: list[str]) -> dict[str, SourceInfo] | None:
        """What each queried source is, so its rows can be read for what they are."""
        info: dict[str, SourceInfo] = {}
        for source_id in sorted(source_ids):
            try:
                source = self._registry.get(source_id)
            except Exception:  # noqa: BLE001 — never fail a served request over metadata
                continue
            info[source_id] = SourceInfo(
                description=source.description,
                classification=source.classification,
                data_types=list(source.data_types),
            )
        return info or None

    def _request_grant(self, agent_id: str, context: dict[str, Any]) -> tuple[str, str]:
        """The (purpose, clearance) this request actually runs under.

        One definition, read by both the mint path and the verify path, so a
        presented token can be compared against the same grant a fresh one
        would carry.
        """
        record: AgentRecord | None
        try:
            record = self._agent_registry.get(agent_id)
        except UnknownAgentError:
            record = None
        purpose = str(context.get("purpose") or "") or (
            (record.default_purpose or "") if record is not None else ""
        )
        clearance = record.clearance if record is not None else ""
        return purpose, clearance

    def _may_claim_purpose(self, agent_id: str, purpose: str) -> bool:
        """Whether a token for ``agent_id`` may carry this purpose claim.

        The router denies every source when the purpose is outside the agent's
        ``allowed_purposes``; the token used to be minted from the purpose the
        caller typed, so a request refused on exactly this ground still handed
        back a validly signed artefact asserting it — forwarded downstream,
        where the signature and the claims are all a reader has.
        """
        try:
            record = self._agent_registry.get(agent_id)
        except UnknownAgentError:
            # An undeclared agent is bounded by nothing, which is the shape
            # every config written before allowed_purposes existed has.
            return True
        return record.may_claim(purpose)

    def _process_session_token(
        self,
        agent_id: str,
        context: dict[str, Any],
        state: _RequestState,
    ) -> None:
        """Verify or mint the session-provenance token for this request (#18).

        Presented token (``context["session_token"]``):
        - invalid → emit ``session_token_verification_failed`` audit entry
          (ErrorRecord.error_type = reason_code) and re-raise
          :class:`SessionTokenError` — fail-closed, no pipeline run (AC-18.d).
        - valid → the token's ``session_id`` claim OVERRIDES any
          caller-declared session id. This is the core property: exposure
          tracking cannot be reset by declaring a fresh ``session_id``
          while presenting an old token (issue #18 "Why it matters").

        No token → mint one bound to (session_id, agent_id, purpose,
        clearance-from-registry), emit ``session_token_issued`` (AC-18.f),
        and inject it into ``context`` so adapters can forward it
        downstream (AC-18.b).
        """
        assert self._session_tokens is not None  # noqa: S101 — caller gates
        presented = context.get("session_token")
        if presented is not None:
            # Only the ABSENT key means "no token". Any present value —
            # including falsy ones like 0 / False / "" — goes through the
            # verification path so a caller can never suppress verification
            # (and force a session-resetting re-mint) by sending junk.
            try:
                if not isinstance(presented, str):
                    raise SessionTokenError("missing", "session_token must be a string")
                claims = self._session_tokens.verify(presented)
                # A token is minted for one agent. Verifying the signature and
                # then taking only its ``session_id`` let any agent replay
                # another's token and inherit that session's exposure ledger —
                # the same substitution ``_verify_handoff_token`` already
                # refuses on the handoff path.
                if claims.agent_id != agent_id:
                    raise SessionTokenError(
                        "agent_mismatch",
                        f"session token was minted for agent {claims.agent_id!r}, "
                        f"presented by {agent_id!r}",
                    )
            except SessionTokenError as exc:
                state.errored.append(
                    ErrorRecord(
                        source_id="<broker>",
                        error_type=exc.reason_code,
                        message=str(exc),
                        trace_id=state.request_id,
                    )
                )
                self._emit_session_token_event(
                    "session_token_verification_failed",
                    agent_id=agent_id,
                    session_id=state.session_id,
                    request_id=state.request_id,
                    errors=list(state.errored),
                )
                raise
            state.session_id = claims.session_id
            state.session_token = presented
            # #25 lazy re-sign: a token signed by a rotating-out key still
            # verifies (grace window), but we hand back a fresh primary-signed
            # token on the response so agents converge on the new key without
            # a push channel — tokens are bearer credentials. The re-signed
            # token keeps the ORIGINAL expiry (security review C2): re-keying
            # must not extend the session's lifetime.
            assert self._key_ring is not None  # noqa: S101 — service implies ring
            # The token is a bearer credential adapters forward downstream, and
            # a downstream reader has nothing but the signature and the claims.
            # ``purpose`` and ``clearance`` are written at mint time, so a
            # session carried into a second purpose kept attesting to the
            # first. Re-mint when either drifts, so what is signed is what was
            # enforced. The ORIGINAL expiry is kept in both cases (security
            # review C2): neither re-keying nor re-binding extends a session.
            purpose, clearance = self._request_grant(agent_id, context)
            claimable = self._may_claim_purpose(agent_id, purpose)
            stale_kid = claims.kid != self._key_ring.primary().kid
            drifted = claims.purpose != purpose or claims.clearance != clearance
            if drifted and not claimable:
                # The router is about to refuse this purpose. Leave the token
                # describing the last request that was actually served rather
                # than re-signing it into a claim the policy denies.
                return
            if stale_kid or drifted:
                fresh = self._session_tokens.issue(
                    session_id=claims.session_id,
                    agent_id=claims.agent_id,
                    purpose=purpose,
                    clearance=clearance,
                    expires_at=claims.expires_at,
                )
                trace: list[str] = []
                if stale_kid:
                    trace.append(f"resigned-from-kid={claims.kid}")
                if drifted:
                    trace.append(
                        f"rebound-from-purpose={claims.purpose!r}/clearance={claims.clearance!r}"
                    )
                state.session_token = fresh
                context["session_token"] = fresh
                self._emit_session_token_event(
                    "session_token_issued",
                    agent_id=claims.agent_id,
                    session_id=claims.session_id,
                    request_id=state.request_id,
                    trace=trace,
                )
            return
        # First request in the session — mint a token. A missing session_id
        # gets a broker-generated one so the token always pins a session.
        if not state.session_id:
            state.session_id = str(uuid.uuid4())
            context["session_id"] = state.session_id
            why = "the caller declared no session_id, so it is its own session"
        else:
            why = "no session_token was presented for the declared session_id"
        purpose, clearance = self._request_grant(agent_id, context)
        if not self._may_claim_purpose(agent_id, purpose):
            # The request still runs and the router still denies it. What must
            # not happen is a signature over the refused claim.
            log.warning(
                "not minting a session token for agent %r: purpose %r is not one "
                "it may claim, and the request will be denied on that ground",
                agent_id,
                purpose,
            )
            return
        # Every request from a caller that carries no session_id mints a token,
        # which writes a session_token_issued entry beside the request's own --
        # three audit lines where the documentation describes two. That is a
        # property of the caller, invisible from outside the process, so the
        # reason is said here rather than inferred from the entry count.
        log.debug(
            "minting a session token for agent %r in session %r (purpose %r, "
            "clearance %r): %s. This writes a session_token_issued audit entry "
            "beside the request's own.",
            agent_id,
            state.session_id,
            purpose,
            clearance,
            why,
        )
        token = self._session_tokens.issue(
            session_id=state.session_id,
            agent_id=agent_id,
            purpose=purpose,
            clearance=clearance,
        )
        state.session_token = token
        context["session_token"] = token
        self._emit_session_token_event(
            "session_token_issued",
            agent_id=agent_id,
            session_id=state.session_id,
            request_id=state.request_id,
        )

    def _emit_session_token_event(
        self,
        event_type: Literal[
            "session_token_issued",
            "session_token_verification_failed",
            "signing_key_rotated",
            "signing_key_revoked",
        ],
        *,
        agent_id: str,
        session_id: str,
        request_id: str,
        errors: list[ErrorRecord] | None = None,
        trace: list[str] | None = None,
    ) -> None:
        """Minimal audit entry for token/key lifecycle events (AC-18.f, #25).

        Mirrors the ``attestation_emitted`` pattern in
        :meth:`_emit_attestation` — non-request fields collapse to zero
        values; ``trace_id`` correlates back to the triggering request.
        ``trace`` markers (reviewer, kid linkage) land in ``rule_trace``.
        """
        self._audit_logger.emit(
            AuditEntry(
                timestamp=AuditLogger.utcnow(),
                request_id=request_id,
                agent_id=agent_id,
                session_id=session_id or None,
                facts_asserted_summary={},
                denial_records=[],
                error_records=list(errors) if errors else [],
                rule_trace=list(trace) if trace else [],
                sources_queried=[],
                sources_denied=[],
                sources_errored=[],
                duration_ms=0,
                event_type=event_type,
                schema_version=2,
                trace_id=request_id,
            )
        )

    def _emit_audit(
        self,
        agent_id: str,
        state: _RequestState,
        attestation_token: str | None,
    ) -> None:
        """Build and hand the :class:`AuditEntry` to the logger (NFR-8, §9.2)."""
        self._audit_logger.emit(
            _build_audit_entry(
                agent_id,
                state,
                attestation_token,
                self._session_store_mode(),
                self.ruleset_hash,
            )
        )

    def _session_store_mode(
        self,
    ) -> Literal["primary", "degraded_memory", "degraded_sqlite"] | None:
        """Surface the session-store mode for the audit entry (NFR-7, design §3.2).

        :class:`PostgresSessionStore` exposes a ``mode`` property; the Phase-1
        in-memory store does not — Phase-1 audit lines therefore continue to
        carry ``session_store_mode: null`` (NFR-5 round-trip).
        """
        mode: Any = getattr(self._session_store, "mode", None)
        if mode in ("primary", "degraded_memory", "degraded_sqlite"):
            return mode  # type: ignore[no-any-return]
        return None

    async def _execute_adapter(
        self,
        adapter: Adapter,
        source_id: str,
        intent: IntentAnalysis,
        scope: list[ScopeConstraint],
        context: dict[str, Any],
    ) -> AdapterResult:
        """Run one adapter; catch scope/adapter errors into a typed AdapterResult."""
        started = time.perf_counter()
        try:
            return await adapter.execute(intent, scope, context)
        except ScopeEnforcementError as exc:
            return AdapterResult(
                source_id=source_id,
                rows=[],
                duration_ms=0,
                error=ErrorRecord(
                    source_id=source_id,
                    error_type="ScopeEnforcementError",
                    message=str(exc),
                    trace_id="",  # filled in by _gather_adapter_results
                ),
            )
        except AdapterError as exc:
            return AdapterResult(
                source_id=source_id,
                rows=[],
                duration_ms=0,
                error=ErrorRecord(
                    source_id=source_id,
                    error_type=type(exc).__name__,
                    message=str(exc),
                    trace_id="",
                ),
            )
        finally:
            # Timed here rather than derived from ``AdapterResult.duration_ms``:
            # that field is an int, so every sub-millisecond call recorded as
            # exactly 0.0 seconds and the first histogram bucket was the only
            # one that ever filled. Failures are timed too -- an adapter that
            # takes ten seconds to fail is a latency event.
            _metrics.adapter_latency.record(time.perf_counter() - started, {"source_id": source_id})

    def _sign(
        self,
        *,
        request_id: str,
        agent_id: str,
        sources_queried: list[str],
        scope_by_source: dict[str, list[ScopeConstraint]],
        rule_trace: list[str],
        session_id: str,
        principal_id: str = "",
        response: dict[str, Any] | None = None,
        source_response_hashes: dict[str, str] | None = None,
    ) -> tuple[str, Literal["v1", "v2"], dict[str, Any]]:
        """Compose the Nautilus attestation payload and sign it (design §9.3).

        Uses :func:`nautilus.core.attestation_payload.build_payload` so the
        ``scope_hash`` / ``rule_trace_hash`` derivation is deterministic
        (NFR-14) and unit-testable in isolation.

        Signs via ``AttestationService.sign_claims``, which takes a claim set
        verbatim. The six claims ``AttestationService.sign`` would have
        emitted are reproduced exactly, plus the four ``verify-a-token.md``
        documents (``request_id``, ``response_hash``, ``hash_skipped``,
        ``source_response_hashes``). The Nautilus payload is still what the
        ``input_hash`` covers, so that binding — and every verifier reading
        it — is unchanged.

        Returns ``(token, scope_hash_version, nautilus_payload)`` so callers
        can (1) stamp the version into :attr:`AuditEntry.scope_hash_version`
        (D-7, FR-19) and (2) hand the signed claim set to the attestation
        sink (design §3.14). The internal ``scope_by_source`` dict is passed
        straight to :func:`build_payload` so temporal-slot detection sees the
        raw :class:`ScopeConstraint` attributes; the v1 path flattens it
        back to the Phase-1 4-key shape in the legacy iteration order so
        Phase-1 tokens remain bit-for-bit reproducible (NFR-6).
        """
        if self._attestation is None:
            # pragma: no cover — caller guards on self._attestation
            raise RuntimeError("attestation is disabled")

        # AC-19.g — if any queried adapter declares non-deterministic capability,
        # skip the *whole-response* hash and sign a hash_skipped=True claim
        # instead (DQ2 LOCKED). Adapters without a ``capabilities`` attribute
        # default to deterministic. Note: in a MIXED request (some deterministic
        # sources + an llm source) this is True yet ``source_response_hashes``
        # still carries the deterministic sources' per-source digests — see the
        # coexistence contract documented in ``build_payload``.
        hash_skipped = any(self._is_non_deterministic(sid) for sid in sources_queried)
        # AC-19.a — compute response hash; omit for non-deterministic adapters
        # and for legacy path (response=None, e.g. no data returned).
        if hash_skipped or response is None:
            response_hash: str | None = None
        else:
            response_hash = compute_response_hash(response)

        nautilus_payload, scope_hash_version = build_payload(
            request_id,
            agent_id,
            sources_queried,
            scope_by_source,
            list(rule_trace),
            response_hash=response_hash,
            hash_skipped=hash_skipped,
            source_response_hashes=source_response_hashes,
        )

        # Nautilus-specific decision marker; the Fathom JWT carries this as
        # the ``decision`` claim. The request_id and agent_id are embedded
        # so downstream verifiers don't need a separate Nautilus payload.
        # ``principal`` joins them because ``agent_id`` is a name the caller
        # asserts: signing it alone proves a decision was made about a string
        # someone typed, and two credentials asserting the same name produce
        # the identical claim.
        decision = f"nautilus:{request_id}:agent={agent_id}:principal={principal_id}"

        # Pass the full Nautilus payload as a single synthetic fact so the
        # JWT's ``input_hash`` binds both ``scope_hash`` and
        # ``rule_trace_hash`` (plus request_id / agent_id / sources_queried).
        input_facts: list[dict[str, Any]] = [nautilus_payload]
        session_ref = session_id or request_id
        # ``sign_claims`` rather than ``sign``: the latter reduces the whole
        # Nautilus payload to an opaque ``input_hash``, so every check
        # ``docs/how-to/verify-a-token.md`` tells an offline verifier to make
        # raised KeyError. The six claims ``sign`` would have produced are
        # reproduced verbatim (existing verifiers keep working) and the four
        # documented ones are promoted alongside them. ``hash_skipped`` most of
        # all: it is sold in three docs as the honest disclosure that a
        # non-deterministic source makes a response unverifiable, and it never
        # reached the caller in any form.
        claims: dict[str, Any] = {
            "iss": "fathom",
            "iat": int(time.time()),
            "decision": decision,
            "rule_trace": list(rule_trace),
            "input_hash": canonical_input_hash(input_facts),
            "session_id": session_ref,
        }
        for documented in ("request_id", "response_hash", "hash_skipped", "source_response_hashes"):
            if documented in nautilus_payload:
                claims[documented] = nautilus_payload[documented]
        token = self._attestation.sign_claims(claims)
        return token, scope_hash_version, nautilus_payload

    # ------------------------------------------------------------------
    # Lifecycle
    # ------------------------------------------------------------------

    async def setup(self) -> None:
        """Idempotent async setup — stand up persistent session schema.

        Calls :meth:`PostgresSessionStore.setup` when the broker is wired with
        a Postgres-backed session store (design §3.2, UQ-1 / D-2). No-op for
        the Phase-1 :class:`~nautilus.core.session.InMemorySessionStore`.
        Safe to call multiple times; each implementer owns its own idempotency.

        Also runs schema fingerprint checks for all registered adapters per
        AC-21.c/e. Adapters with major drift are quarantined.
        """
        if isinstance(self._session_store, (PostgresSessionStore, SqliteSessionStore)):
            await self._session_store.setup()
        await self._check_schema_fingerprints()
        self._setup_done = True

    async def _ensure_setup(self) -> None:
        """Stand the broker up on first use if the caller never did it.

        ``from_config``/``afrom_config`` connect adapters lazily and say so, so
        a durable session store that refused until an explicit ``setup()`` was
        the one piece that did not follow the documented shape — and it refused
        with ``SessionStoreUnavailableError``, which is exactly the class
        ``session_store.on_failure: fallback_memory`` exists to degrade past.
        One forgotten call therefore dropped a durable ledger to memory in one
        config and failed loudly in another.
        """
        if self._setup_done:
            return
        await self.setup()

    async def _check_schema_fingerprints(self) -> None:
        """Check schema drift for every connected adapter; quarantine on major drift.

        Called from :meth:`setup`. Adapters that are not connected yet are
        skipped here and checked by :meth:`_prepare_adapter` at first connect
        — connect is lazy, so at startup this loop usually has nothing to do
        and the connect-time check is what actually runs.

        AC-21.c: record fingerprint on first registration.
        AC-21.e: emit schema_drift_detected; quarantine on major drift.
        """
        for source_id, adapter in self._adapters.items():
            if source_id in self._connected_adapters:
                await self._check_adapter_schema(source_id, adapter)

    async def _check_adapter_schema(self, source_id: str, adapter: Adapter) -> bool:
        """Fingerprint one connected adapter against its baseline.

        Returns ``True`` when the adapter was quarantined by this check, so a
        caller mid-request can refuse to route to it. A drifted adapter must
        not serve the very request that discovered the drift.
        """
        if not hasattr(adapter, "get_schema"):
            return False
        try:
            schema = await adapter.get_schema()  # type: ignore[union-attr]
        except Exception:  # noqa: BLE001
            log.warning(
                "schema fetch failed for adapter %r; skipping fingerprint check",
                source_id,
            )
            return False

        current_fp = schema.fingerprint()
        stored_fp = self._fingerprint_store.get(source_id)
        if stored_fp is None:
            # First registration — record baseline.
            self._fingerprint_store.record(source_id, current_fp)
            return False

        if stored_fp == current_fp:
            return False

        # Drift detected. Classifying minor vs major needs the PREVIOUS schema
        # object, and only its fingerprint was kept, so every mismatch counts
        # as major (fail closed, AC-21.e). An operator clears it with
        # ``nautilus adapters schema-ack``, which re-baselines the adapter.
        log.warning(
            "schema drift detected for adapter %r (previous=%s current=%s severity=major)",
            source_id,
            stored_fp[:16],
            current_fp[:16],
        )
        self.emit_adapter_event("schema_drift_detected", source_id)
        self.emit_adapter_event("adapter_quarantined", source_id)
        self._quarantined_adapters.add(source_id)
        return True

    # ------------------------------------------------------------------
    # Reload (SIGHUP) — see nautilus/cli/serve.py for the signal wiring
    # ------------------------------------------------------------------

    def on_reload(self, listener: Callable[[], None]) -> None:
        """Call ``listener`` after every adopted reload, on the reload's task.

        For state derived from the config and held outside the broker -- the
        credential allow-list a transport copies onto ``app.state`` at startup
        and re-reads per request. The reload has already installed the new
        generation when this runs, so a listener reads :attr:`config` and gets
        what was just adopted.

        A listener that raises does not fail the reload, does not stop the
        ones after it, and is named in :attr:`last_reload_notes` so the audit
        entry records that some outside state did not follow the config.

        There is no ``off_reload``: a listener stays registered for the
        lifetime of the broker. Building two apps against one broker therefore
        pins the first app's closure until the broker is closed. Acceptable
        rather than fixed: ``create_app`` registers once per app, one broker
        per process is the only shape ``serve`` builds, and a listener that
        writes to a dead app's ``state`` is a write nothing reads.
        """
        self._reload_listeners.append(listener)

    @property
    def last_reload_notes(self) -> list[str]:
        """What the last adopted reload did beyond adopting keys.

        Cumulative-exposure ledgers it left with nothing to accumulate under,
        and listeners that failed. Both are things the audit entry has to say
        and the adopted-key list cannot: one is a security budget cleared by a
        routine operation, the other is state outside the broker that no longer
        matches the config the broker is running.
        """
        return list(self._last_reload_notes)

    async def reload(self, candidate: Broker) -> list[str]:
        """Adopt ``candidate``'s sources, rules, credentials and live limits.

        ``candidate`` is a broker built from the new file by the *same*
        function ``nautilus serve`` and ``nautilus config check`` use
        (:func:`nautilus.cli.serve.broker_for_serve`), so a config this
        process would refuse to start on has already raised before this is
        called and in the same words. Nothing here re-validates: a second
        validator is a validator that drifts.

        What is adopted: the source registry and its adapters, the Fathom
        router (rule files are re-read, not just the ``rules:`` stanza, so an
        edit inside a ``user_rules_dirs`` directory arrives too), the intent
        analyzer -- its keyword map is generated from ``sources`` (#24), so a
        source change that did not rebuild it would route on a stale
        vocabulary -- ``api.keys``, which is how a credential is rotated
        without a restart, and the two ``session_store`` limits the request
        path reads per request.

        A transport holds the credential list on its own ``app.state`` and
        re-reads it per request, so it is handed the new one through
        :meth:`on_reload` rather than by reaching into the ASGI app from
        here.

        What is not: everything :data:`RELOADABLE_TOP_LEVEL_KEYS`,
        :data:`RELOADABLE_API_KEYS` and :data:`RELOADABLE_SESSION_STORE_KEYS`
        leave out. A file that changes
        one of those is refused whole rather than half-applied; see
        :func:`_restart_only_changes`.

        Adapter identity is preserved for every source whose spec is
        byte-identical: the live adapter object keeps its pool, its connect
        cooldown and its ``_connected_adapters`` entry, and the candidate's
        unused twin is closed. A source that changed or vanished has its
        adapter retired and its connect bookkeeping dropped so the replacement
        dials on next use. Schema-drift baselines and quarantines are keyed by
        source id and survive a reload untouched: re-pointing a source at
        another table must not be a way to clear a quarantine
        ``nautilus adapters schema-ack`` is the way to clear.

        Returns:
            The dotted names of the keys actually adopted, in file order.
            Empty when the new file is equivalent to the running one.

        Raises:
            ConfigNotReloadableError: When the new file changes a key that is
                only read at startup. ``candidate`` is closed first.
        """
        self._refuse_if_closed("reload")
        incoming = candidate._generation  # noqa: SLF001 - transplanting into self
        async with self._reload_lock:
            outgoing = self._generation
            blocked = _restart_only_changes(outgoing.config, incoming.config)
            if blocked:
                await candidate.aclose()
                raise ConfigNotReloadableError(
                    "these keys are read once at startup and cannot be reloaded: "
                    + ", ".join(blocked)
                    + ". Restart the process to adopt them."
                )

            adapters, retired = self._reconcile_adapters(outgoing, incoming)
            self._generation = _ConfigGeneration(
                config=incoming.config,
                registry=incoming.registry,
                router=incoming.router,
                adapters=adapters,
                intent_analyzer=incoming.intent_analyzer,
            )
            adopted = _adopted_changes(outgoing.config, incoming.config)

        notes: list[str] = []
        self._last_reload_notes = notes
        if "api.keys" in adopted:
            orphaned = _warn_about_orphaned_ledgers(outgoing.config, incoming.config)
            if orphaned:
                notes.append(f"cleared {orphaned} cumulative-exposure ledger(s)")
        for listener in self._reload_listeners:
            # ``on_reload`` is public, so this loop calls code the broker does
            # not own, after the generation is already installed and before the
            # outgoing one is retired. An escaping raise left the reload applied
            # but unrecorded (``reload_config`` catches only
            # ``ConfigNotReloadableError``), leaked the outgoing router and
            # retired adapters, and skipped every listener after the first --
            # so a second transport went on serving a retired credential while
            # the audit log said nothing. The failure is recorded instead, on
            # the receipt as well as in the log, because a listener that did
            # not run is state that no longer matches the adopted config.
            try:
                listener()
            except Exception:
                log.exception("a post-reload listener failed; the reload stands")
                # Its name, never its ``repr``: a listener bound to the
                # credential list would print it.
                named = getattr(listener, "__qualname__", type(listener).__name__)
                notes.append(f"a post-reload listener failed: {named}")

        # The candidate is now a shell around exactly what this reload retired:
        # the outgoing router, the adapters no longer in service, and its own
        # never-used audit sink, attestation sink and session store. One
        # ``aclose`` disposes of all of it, in the order ``aclose`` documents.
        candidate._generation = _ConfigGeneration(  # noqa: SLF001 - retiring it
            config=incoming.config,
            registry=incoming.registry,
            router=outgoing.router,
            adapters={str(i): adapter for i, adapter in enumerate(retired)},
            intent_analyzer=incoming.intent_analyzer,
        )
        await self._retire(outgoing, candidate)
        return adopted

    def _reconcile_adapters(
        self, outgoing: _ConfigGeneration, incoming: _ConfigGeneration
    ) -> tuple[dict[str, Adapter], list[Adapter]]:
        """Split the two generations' adapters into "keep serving" and "retire".

        An unchanged source keeps the object that is already connected --
        replacing it would drop a live pool and re-dial on the next request
        while ``_connected_adapters`` still claimed it was up.
        """
        before = {source.id: source for source in outgoing.registry}
        after = {source.id: source for source in incoming.registry}
        adapters: dict[str, Adapter] = {}
        retired: list[Adapter] = []
        for source_id, spec in after.items():
            if before.get(source_id) == spec and source_id in outgoing.adapters:
                adapters[source_id] = outgoing.adapters[source_id]
                retired.append(incoming.adapters[source_id])
                continue
            adapters[source_id] = incoming.adapters[source_id]
            if source_id in outgoing.adapters:
                retired.append(outgoing.adapters[source_id])
            self._forget_connect_state(source_id)
        for source_id, adapter in outgoing.adapters.items():
            if source_id not in after:
                retired.append(adapter)
                self._forget_connect_state(source_id)
        return adapters, retired

    def _forget_connect_state(self, source_id: str) -> None:
        """Drop the connect bookkeeping for a source whose adapter is being replaced.

        Not the quarantine set and not the schema baseline: both are the record
        of a drift an operator has to acknowledge, and a config edit is not an
        acknowledgement.
        """
        self._connected_adapters.discard(source_id)
        self._connect_failures.pop(source_id, None)
        self._connect_locks.pop(source_id, None)

    async def _retire(self, outgoing: _ConfigGeneration, shell: Broker) -> None:
        """Close ``shell`` once nothing is still reading ``outgoing``.

        The wait is what makes "in-flight requests finish on the config they
        started with" true rather than merely likely: the swap is atomic, but
        the adapters and the CLIPS environment the previous generation is
        still using are real objects, and closing them under a running request
        would fail it. The timeout only decides when to say so out loud -- it
        never shortens the wait, because a request holding the old generation
        is a request whose adapter must stay open.
        """
        outgoing.drained = asyncio.Event()
        if outgoing.inflight == 0:
            outgoing.drained.set()
        waiter = asyncio.ensure_future(outgoing.drained.wait())
        try:
            await asyncio.wait_for(asyncio.shield(waiter), self.reload_drain_timeout_s)
        except TimeoutError:
            log.warning(
                "reload: %d request(s) still running on the previous config after "
                "%.0fs; its adapters and rule engine stay open until they finish",
                outgoing.inflight,
                self.reload_drain_timeout_s,
            )
            await waiter
        await shell.aclose()

    def emit_reload_event(self, event_type: ReloadEventType, detail: str) -> None:
        """Write one config-reload entry to the audit log.

        A reload changes which sources exist and which rules decide, so the
        log that has to explain a decision has to record when that changed and
        when a change was refused. ``detail`` is the operator-facing sentence:
        the adopted keys, or the reason for the refusal -- the same words
        ``serve`` prints, when the refusal came from the config validator.
        """
        self._audit_logger.emit(
            AuditEntry(
                timestamp=AuditLogger.utcnow(),
                request_id=str(uuid.uuid4()),
                agent_id="<broker>",
                session_id=None,
                raw_intent=detail,
                intent_analysis=IntentAnalysis(raw_intent="", data_types_needed=[], entities=[]),
                facts_asserted_summary={},
                routing_decisions=[],
                scope_constraints=[],
                denial_records=[],
                error_records=[],
                rule_trace=[],
                ruleset_hash=self.ruleset_hash,
                sources_queried=[],
                sources_denied=[],
                sources_skipped=[],
                sources_errored=[],
                attestation_token=None,
                duration_ms=0,
                event_type=event_type,
            )
        )

    def emit_adapter_event(self, event_type: AdapterEventType, adapter_id: str) -> None:
        """Write one adapter-lifecycle entry to the audit log.

        These events belong to an adapter, not to a request: no intent, no
        sources, no decisions. ``adapter_id`` is the only thing that
        distinguishes one from another, which is why the entry carries it.

        Public because ``nautilus adapters schema-ack`` records the operator's
        override through the same broker and the same log as the drift entry
        it overrides.
        """
        self._audit_logger.emit(
            AuditEntry(
                timestamp=AuditLogger.utcnow(),
                request_id=str(uuid.uuid4()),
                agent_id="<broker>",
                session_id=None,
                raw_intent="",
                intent_analysis=IntentAnalysis(raw_intent="", data_types_needed=[], entities=[]),
                facts_asserted_summary={},
                routing_decisions=[],
                scope_constraints=[],
                denial_records=[],
                error_records=[],
                rule_trace=[],
                sources_queried=[],
                sources_denied=[],
                sources_skipped=[],
                sources_errored=[],
                attestation_token=None,
                duration_ms=0,
                event_type=event_type,
                adapter_id=adapter_id,
            )
        )

    async def _clear_quarantine_if_acked(self, source_id: str, adapter: Adapter) -> bool:
        """Re-check a quarantined adapter against its stored baseline.

        ``nautilus adapters schema-ack`` re-baselines the adapter on disk, and
        the quarantine set lives in memory: without this the ADAPTER_QUARANTINED
        message ("acknowledge drift via schema-ack before resuming") was untrue
        for the running process, which stayed quarantined until restart.

        Returns ``True`` when the quarantine was lifted.
        """
        if not hasattr(adapter, "get_schema"):
            return False
        try:
            schema = await adapter.get_schema()  # type: ignore[union-attr]
        except Exception:  # noqa: BLE001 — an unreachable adapter stays quarantined
            return False
        if self._fingerprint_store.get(source_id) != schema.fingerprint():
            return False
        self._quarantined_adapters.discard(source_id)
        log.info("quarantine lifted for adapter %r; baseline now matches", source_id)
        self.emit_adapter_event("adapter_unquarantined", source_id)
        return True

    @property
    def closed(self) -> bool:
        """True once :meth:`close` or :meth:`aclose` has run."""
        return self._closed

    def __enter__(self) -> Broker:
        """Enter the sync lifecycle: run :meth:`setup`, hand back the broker.

        ``setup()`` is mandatory for the persistent session stores — it is
        what creates their schema — and every example spelled it out by hand,
        or forgot to. So does ``close()``: it refuses to run inside a running
        event loop, which is why the docs' ``try/finally`` is doing real work
        and why the async form below exists separately.
        """
        self._run_sync(self.setup())
        return self

    def __exit__(self, *exc_info: object) -> None:
        """Close on the way out, whatever happened inside."""
        self.close()

    async def __aenter__(self) -> Broker:
        """Async counterpart of :meth:`__enter__`."""
        await self.setup()
        return self

    async def __aexit__(self, *exc_info: object) -> None:
        """Async counterpart of :meth:`__exit__`."""
        await self.aclose()

    def close(self) -> None:
        """Idempotent sync close — FR-17, AC-8.6."""
        try:
            asyncio.get_running_loop()
        except RuntimeError:
            pass
        else:
            raise RuntimeError(
                "Broker.close() called inside a running event loop. "
                "Use Broker.aclose() (async) from async contexts."
            )
        if self._sync_loop is None:
            # Nothing ran on the sync facade, so there is no loop to close on.
            asyncio.run(self.aclose())
            return
        loop, thread = self._sync_loop, self._sync_thread
        try:
            self._run_sync(self.aclose())
        finally:
            loop.call_soon_threadsafe(loop.stop)
            if thread is not None:
                thread.join(timeout=30)
            loop.close()
            self._sync_loop = None
            self._sync_thread = None

    async def aclose(self) -> None:
        """Idempotent async close. Safe to call multiple times (FR-17).

        Ordering contract (D-8, design §3.14, AC-14.6):
        ``session_store.aclose()`` → ``attestation_sink.close()`` →
        adapter-pool release. Session-store flush must precede sink close
        (session writes during request must land before sink teardown);
        adapter release comes last so in-flight emits can still reference
        pooled connections above. Any close is best-effort (one failing
        backend must not prevent others from closing).
        """
        if self._closed:
            return
        self._closed = True
        # 1. Session store: flush any in-flight writes before downstream close.
        if hasattr(self._session_store, "aclose"):
            with contextlib.suppress(Exception):
                await self._session_store.aclose()  # type: ignore[attr-defined]
        # 2. Attestation sink: release the store-and-forward handle AFTER
        #    session writes have flushed but BEFORE adapter pools go down —
        #    in-flight emits from step 1's session-state finalization may
        #    still reference adapter connections.
        with contextlib.suppress(Exception):
            await self._attestation_sink.close()
        # 2b. Audit sink: a chained audit log holds an exclusive writer lock
        #     and an open file handle. Nothing closed it before, so a process
        #     that built two brokers in sequence refused its own second one.
        audit_close = getattr(self._audit_logger.sink, "close", None)
        if audit_close is not None:
            with contextlib.suppress(Exception):
                audit_close()
        # 3. Adapters — release pools last so in-flight attestation can still
        #    reference their connections above.
        for adapter in self._adapters.values():
            try:
                await adapter.close()
            except Exception:
                # Best-effort, but not silent: swallowing this is how a pool
                # that never closed looked like a clean shutdown.
                log.warning("adapter close failed during aclose()", exc_info=True)
                continue
        self._router.close()

    # ------------------------------------------------------------------
    # Hashing helpers (exposed for tests / §9.3 verifiers)
    # ------------------------------------------------------------------

    @staticmethod
    def _hash_scope(scope_by_source: dict[str, list[ScopeConstraint]]) -> str:
        """SHA-256 of the stringified scope constraints — design §9.3."""
        buf: list[str] = []
        for source_id in sorted(scope_by_source):
            for c in scope_by_source[source_id]:
                buf.append(f"{source_id}|{c.field}|{c.operator}|{c.value!r}")
        return hashlib.sha256("\n".join(buf).encode()).hexdigest()

sources property

Registered source configs (identifier + metadata) — design §3.1.

config property

The validated config this broker was built from.

Exposed so a governance surface served by this broker uses the same rkm.sandbox and rules settings the broker itself runs under.

audit_path property

The JSONL file this broker's decisions are written to.

The sandbox replays this log, so a governance surface that queues a proposal has to be able to find it rather than guessing ./audit.jsonl relative to wherever the server was started. None when the sink is not a file.

audit_logger property

The sink every decision this broker records goes to.

Exposed because the governance decisions taken over REST — approve, reject, retract, rollback — are made against this broker and belong in the same log as the routing decisions they change.

fingerprint_store property

Schema baselines this broker compares against (AC-21.c).

Exposed so nautilus adapters schema-diff / schema-ack read and write the same baselines the broker does, rather than a second store rooted somewhere else.

api_config property

api: subsection of the loaded config (host, port, keys, auth).

Exposed so nautilus serve can honour api.host / api.port without re-loading the YAML or reaching into private state.

ruleset_hash property

sha256:… over the rules this broker is deciding with.

agent_registry property

Registered agent identities (design §3.5, FR-9).

session_store property

Active session store (sync or async surface) — design §3.2 / §3.9.

Exposed so transports (/readyz probe in :mod:nautilus.transport. fastapi_app) can call aget / get against the backing store without reaching into private state.

instance_id property

Per-process broker identity baked into session-token claims (#18).

Tokens minted by this broker carry broker_instance_id and fail verification (broker_instance_mismatch) against any other instance (AC-18.d).

key_ring property

Signing/verification key ring for session tokens (#18, AC-18.e).

None when session_tokens.enabled is false. The ring is in-memory — transports MUST reuse this instance (not construct a fresh :class:KeyRing) or token verification cannot succeed.

session_tokens property

Active :class:SessionTokenService, or None when disabled (#18).

last_reload_notes property

What the last adopted reload did beyond adopting keys.

Cumulative-exposure ledgers it left with nothing to accumulate under, and listeners that failed. Both are things the audit entry has to say and the adopted-key list cannot: one is a security budget cleared by a routine operation, the other is state outside the broker that no longer matches the config the broker is running.

closed property

True once :meth:close or :meth:aclose has run.

afrom_config(path) async classmethod

Async counterpart of :meth:from_config (README "What Ships Today").

Construction is entirely blocking -- reading the YAML, reading the attestation key, compiling the rule tree into a CLIPS environment -- so it runs on a worker thread rather than stalling the caller's event loop. Adapters are still connected lazily by the first request, exactly as with :meth:from_config.

Source code in nautilus/core/broker.py
@classmethod
async def afrom_config(cls, path: str | Path) -> Broker:
    """Async counterpart of :meth:`from_config` (README "What Ships Today").

    Construction is entirely blocking -- reading the YAML, reading the
    attestation key, compiling the rule tree into a CLIPS environment --
    so it runs on a worker thread rather than stalling the caller's event
    loop. Adapters are still connected lazily by the first request, exactly
    as with :meth:`from_config`.
    """
    return await asyncio.to_thread(cls.from_config, path)

from_config(path) classmethod

Build a fully-wired :class:Broker from a nautilus.yaml path.

Order of operations mirrors design §15 build sequence: 1. Load + validate config. 2. Build :class:SourceRegistry. 3. Build :class:PatternMatchingIntentAnalyzer from the keyword map auto-generated from each source's data_types (#24), overlaid with explicit analysis.keyword_map entries. 4. Build :class:FathomRouter against the built-in rules tree + any configured user rules. 5. Build per-source :class:Adapter instances (NOT connected — connect() is async; first arequest is responsible). 6. Build :class:AuditLogger over FileSink(audit.path). 7. Build :class:AttestationService (auto-generate unless private_key_path; return None if disabled). 8. Build :class:InMemorySessionStore.

Raises :class:ConfigError on bad YAML / missing env vars and :class:PolicyEngineError on engine construction failure.

Source code in nautilus/core/broker.py
@classmethod
def from_config(cls, path: str | Path) -> Broker:
    """Build a fully-wired :class:`Broker` from a ``nautilus.yaml`` path.

    Order of operations mirrors design §15 build sequence:
    1. Load + validate config.
    2. Build :class:`SourceRegistry`.
    3. Build :class:`PatternMatchingIntentAnalyzer` from the keyword map
       auto-generated from each source's ``data_types`` (#24), overlaid
       with explicit ``analysis.keyword_map`` entries.
    4. Build :class:`FathomRouter` against the built-in rules tree +
       any configured user rules.
    5. Build per-source :class:`Adapter` instances (NOT connected —
       ``connect()`` is async; first ``arequest`` is responsible).
    6. Build :class:`AuditLogger` over ``FileSink(audit.path)``.
    7. Build :class:`AttestationService` (auto-generate unless
       ``private_key_path``; return ``None`` if disabled).
    8. Build :class:`InMemorySessionStore`.

    Raises :class:`ConfigError` on bad YAML / missing env vars and
    :class:`PolicyEngineError` on engine construction failure.
    """
    config = load_config(path)

    registry = SourceRegistry(config.sources)
    agent_registry = AgentRegistry(config.agents)
    if not config.agents:
        # An empty registry makes the router read clearance, compartments
        # and purpose from ``context`` — i.e. from the caller. That is the
        # bootstrap default and getting-started teaches exactly this shape,
        # but it is the single largest posture difference between a demo
        # and a deployment, so it is said out loud rather than inferred.
        log.warning(
            "No 'agents:' are declared in %r, so every request declares its own "
            "clearance, compartments and purpose and the broker enforces them "
            "against the sources it knows. Declare agents to turn enforcement on.",
            str(path),
        )

    # Auto-generate base intent vocabulary from each source's declared
    # ``data_types`` (#24); explicit ``analysis.keyword_map`` entries
    # overlay and win on key collision.
    keyword_map = build_keyword_map(registry.sources, config.analysis.keyword_map)
    pattern_analyzer = PatternMatchingIntentAnalyzer(
        keyword_map=keyword_map,
    )
    # The configured vocabulary the router will intersect the analyzer's
    # answer against, handed to the LLM provider so the model is asked to
    # pick from it rather than to guess at it (§4.17).
    known_data_types = sorted({dt for src in registry.sources for dt in src.data_types})
    intent_analyzer = cls._build_intent_analyzer(config, pattern_analyzer, known_data_types)

    base_dir = Path(path).parent
    attestation = cls._build_attestation(config, base_dir)
    attestation_sink = cls._build_attestation_sink(config, attestation, base_dir)

    user_rules_dirs = [cls._resolve(base_dir, d) for d in config.rules.user_rules_dirs]
    router = FathomRouter(
        built_in_rules_dir=BUILT_IN_RULES_DIR,
        user_rules_dirs=user_rules_dirs,
        attestation=attestation,
        check_consistency=config.rules.consistency_checks,
        rule_packs=config.rules.packs,
    )

    # B1 -- an unrecognised level is ranked below ``unclassified`` by
    # ``fathom.engine.dominates``, so a typo in the primary access-control
    # field publishes the source to every agent. Reject at startup, where
    # the operator can still see the typo, rather than denying silently
    # for the life of the deployment.
    _validate_classification_labels(config, router)

    # AC-35.2.b/d — load manual relationship facts into the engine at
    # startup; re-reading on every broker construction gives restart
    # persistence. The directory is optional: missing = no-op.
    _facts_dir = Path(path).parent / "facts" / "relationships"
    if _facts_dir.is_dir():
        load_manual_relationships(router.engine, _facts_dir)

    # Broker-default embedder: strict NoopEmbedder (design §3.10 — fail
    # loudly on missing embedder rather than silent zero vectors).
    broker_default_embedder: Embedder = NoopEmbedder(strict=True)

    # Merge static registry with entry-point discovered plugins and
    # local-path adapters (#17). Explicit config wins over discovery.
    adapter_registry = {
        **ADAPTER_REGISTRY,
        **_discover_adapters(),
        **_load_local_adapters(config.adapters, base_dir=Path(path).parent),
    }

    adapters: dict[str, Adapter] = {}
    for source in registry:
        adapters[source.id] = cls._build_adapter(
            source, broker_default_embedder, adapter_registry
        )

    audit_path = cls._resolve(base_dir, config.audit.path)
    try:
        audit_path.parent.mkdir(parents=True, exist_ok=True)
    except OSError as exc:
        # In the hardened image this is where a missing ``audit``
        # volumeMount lands: ``/var/log`` exists and UID 65532 cannot write
        # it, so the broker refuses before it binds. Measured, that read as
        # a bare ``[Errno 13] Permission denied: '/var/log/nautilus'`` --
        # an operator following the deployment guide went looking at
        # ``/readyz``, which nothing was listening on. Name the directory's
        # job and the two ways it goes wrong.
        raise OSError(
            f"cannot open the audit log directory {audit_path.parent}: {exc}. "
            f"Nautilus records one line per decision and will not serve "
            f"unrecorded requests, so this refuses startup rather than "
            f"degrading. Either the volume for it is not mounted, or it is "
            f"not writable by the UID this process runs as."
        ) from exc
    try:
        audit_logger = AuditLogger(sink=build_audit_sink(config, audit_path, attestation))
    except OSError as exc:
        # The second of the two causes named above arrives here, not at the
        # mkdir: ``exist_ok=True`` succeeds on a directory that exists, so a
        # mounted-but-unwritable volume walks straight past it and fails on
        # the open instead -- and refused with a bare
        # ``[Errno 13] Permission denied: '.../audit.jsonl'``. Same
        # consequence, same sentence.
        raise OSError(
            f"cannot open the audit log {audit_path}: {exc}. Nautilus "
            f"records one line per decision and will not serve unrecorded "
            f"requests, so this refuses startup rather than degrading. The "
            f"audit log directory exists but is not writable by the UID this "
            f"process runs as, or the file itself is not."
        ) from exc

    session_store = cls._build_session_store(config, base_dir=base_dir)

    synthesizer = BasicSynthesizer()

    # Session-provenance tokens (#18) — KeyRing only when enabled, so
    # Phase-1 YAML keeps the token path entirely off (NFR-5). A configured
    # ``key_ring_path`` persists the ring so replicas share signing keys.
    key_ring_path = config.session_tokens.key_ring_path
    key_ring = (
        KeyRing(cls._resolve(base_dir, key_ring_path) if key_ring_path else None)
        if config.session_tokens.enabled
        else None
    )
    # A token names the broker that minted it so it cannot be replayed
    # against an unrelated one that happens to share key material. That id
    # has to be the same on every replica of one deployment, or the check
    # rejects every token behind a load balancer. Configured id wins; a
    # shared ring means a shared deployment, so it defaults to a constant
    # there — deriving it from the ring's key material would change under
    # rotation and invalidate every token in flight.
    broker_instance_id = config.session_tokens.broker_instance_id or (
        "shared" if key_ring_path else None
    )

    if log.isEnabledFor(logging.DEBUG):
        # ``config check`` prints this; a running server did not, so
        # "which file is this process actually serving, and what did its
        # relative paths resolve to" was answerable only by reading the
        # deployment. ``dials`` goes through the same allowlist redactor as
        # every other endpoint the broker publishes, so a DSN password in
        # the file does not reach the process log with it.
        log.debug(
            "loaded config %s: %d source(s), %d agent(s), audit -> %s, "
            "session store %r, session tokens %s, state dir %s",
            Path(path).resolve(),
            len(registry.sources),
            len(config.agents),
            audit_path,
            config.session_store.backend,
            "on" if config.session_tokens.enabled else "off",
            cls._resolve(base_dir, config.state_dir) if config.state_dir else base_dir,
        )
        for source in registry:
            log.debug(
                "config source %r: type %r, classification %r, data types %s, dials %s",
                source.id,
                source.type,
                source.classification,
                source.data_types,
                redact_connection(source.connection) or "nothing",
            )

    return cls(
        config=config,
        registry=registry,
        intent_analyzer=intent_analyzer,
        router=router,
        adapters=adapters,
        synthesizer=synthesizer,
        audit_logger=audit_logger,
        attestation=attestation,
        session_store=session_store,
        agent_registry=agent_registry,
        attestation_sink=attestation_sink,
        key_ring=key_ring,
        session_token_ttl_s=config.session_tokens.ttl_seconds,
        broker_instance_id=broker_instance_id,
        # Where this broker's own state goes. ``state_dir`` exists because
        # the default -- the config file's directory -- is read-only in
        # every deployment shape this repo ships.
        base_dir=(cls._resolve(base_dir, config.state_dir) if config.state_dir else base_dir),
    )

sources_visible_to(agent_id)

The catalogue as agent_id is allowed to know it.

A source's name, prose description and data types are what a caller needs in order to ask for it — which is the reconnaissance step the classification rules refuse. Listing every source to every caller told an unclassified key exactly what it was being kept from, so the catalogue is filtered by the same clearance ladder the router routes by.

None means no bound agent — a bare key, which is documented as "any agent_id, every capability" and has no clearance to filter against — or a deployment with no classification hierarchy registered, which has no ladder at all. Both see the whole catalogue.

Source code in nautilus/core/broker.py
def sources_visible_to(self, agent_id: str | None) -> list[SourceConfig]:
    """The catalogue as ``agent_id`` is allowed to know it.

    A source's name, prose description and data types are what a caller
    needs in order to ask for it — which is the reconnaissance step the
    classification rules refuse. Listing every source to every caller told
    an unclassified key exactly what it was being kept from, so the
    catalogue is filtered by the same clearance ladder the router routes
    by.

    ``None`` means no bound agent — a bare key, which is documented as
    "any agent_id, every capability" and has no clearance to filter
    against — or a deployment with no ``classification`` hierarchy
    registered, which has no ladder at all. Both see the whole catalogue.
    """
    if agent_id is None:
        return self.sources
    levels = self._router.hierarchy_levels()
    if not levels:
        return self.sources
    try:
        clearance = self._agent_registry.get(agent_id).clearance
    except UnknownAgentError:
        return self.sources
    if clearance not in levels:
        return self.sources
    rank = levels.index(clearance)
    # An unrecognised source label ranks below everything for fathom, so it
    # is visible on the same terms the router routes it on.
    return [
        s
        for s in self.sources
        if s.classification not in levels or levels.index(s.classification) <= rank
    ]

rules_in_force()

Every rule loaded into the running engine (module + name).

Source code in nautilus/core/broker.py
def rules_in_force(self) -> list[dict[str, str]]:
    """Every rule loaded into the running engine (module + name)."""
    return self._router.rules_in_force()

issue_session_token(*, session_id, agent_id, purpose)

Mint a session token + emit session_token_issued audit (AC-18.f).

Public surface for transports (POST /v1/sessions) so token issuance is always audited through the broker's single JSONL stream.

clearance is read from the AgentRegistry, never taken from the caller. It used to be a parameter, and POST /v1/sessions passed body["clearance"] into it, so the broker would sign clearance: top-secret for an agent whose record says unclassified -- an authorization assertion, signed by Nautilus, verifiable by anyone against the unauthenticated JWKS, and forwarded to downstream sources in X-Nautilus-Session-Token. The in-request minting path already read the registry; taking the parameter away means no caller can disagree with it.

purpose gets the same treatment. It is caller free text, and it is a live authorization input: the router denies every source when it falls outside the agent's allowed_purposes. Minting it unchecked meant a caller could ask this route for a signed assertion of a purpose the broker would refuse to act on, and be handed one.

Raises:

Type Description
RuntimeError

when session tokens are disabled.

PurposeNotPermittedError

when the agent may not claim purpose.

Source code in nautilus/core/broker.py
def issue_session_token(
    self,
    *,
    session_id: str,
    agent_id: str,
    purpose: str,
) -> str:
    """Mint a session token + emit ``session_token_issued`` audit (AC-18.f).

    Public surface for transports (``POST /v1/sessions``) so token
    issuance is always audited through the broker's single JSONL stream.

    ``clearance`` is read from the AgentRegistry, never taken from the
    caller. It used to be a parameter, and ``POST /v1/sessions`` passed
    ``body["clearance"]`` into it, so the broker would sign
    ``clearance: top-secret`` for an agent whose record says
    ``unclassified`` -- an authorization assertion, signed by Nautilus,
    verifiable by anyone against the unauthenticated JWKS, and forwarded to
    downstream sources in ``X-Nautilus-Session-Token``. The in-request
    minting path already read the registry; taking the parameter away means
    no caller can disagree with it.

    ``purpose`` gets the same treatment. It is caller free text, and it is
    a live authorization input: the router denies every source when it falls
    outside the agent's ``allowed_purposes``. Minting it unchecked meant a
    caller could ask this route for a signed assertion of a purpose the
    broker would refuse to act on, and be handed one.

    Raises:
        RuntimeError: when session tokens are disabled.
        PurposeNotPermittedError: when the agent may not claim ``purpose``.
    """
    if self._session_tokens is None:
        raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
    try:
        record = self._agent_registry.get(agent_id)
    except UnknownAgentError:
        record = None
    if record is not None and not record.may_claim(purpose):
        raise PurposeNotPermittedError(
            f"purpose {purpose!r} is not one of the purposes agent "
            f"{agent_id!r} may claim ({sorted(record.allowed_purposes)})"
        )
    token = self._session_tokens.issue(
        session_id=session_id,
        agent_id=agent_id,
        purpose=purpose,
        clearance=record.clearance if record is not None else "",
    )
    self._emit_session_token_event(
        "session_token_issued",
        agent_id=agent_id,
        session_id=session_id,
        request_id=str(uuid.uuid4()),
    )
    return token

verify_session_token(token)

Verify a session token; audit failures (AC-18.d + AC-18.f).

Returns the decoded claims on success. On failure, emits a session_token_verification_failed audit entry carrying the reason_code as an :class:ErrorRecord and re-raises the :class:SessionTokenError (fail-closed).

Raises:

Type Description
RuntimeError

when session tokens are disabled.

SessionTokenError

tampered / expired / unknown-kid / wrong-instance token.

Source code in nautilus/core/broker.py
def verify_session_token(self, token: str) -> SessionTokenClaims:
    """Verify a session token; audit failures (AC-18.d + AC-18.f).

    Returns the decoded claims on success. On failure, emits a
    ``session_token_verification_failed`` audit entry carrying the
    ``reason_code`` as an :class:`ErrorRecord` and re-raises the
    :class:`SessionTokenError` (fail-closed).

    Raises:
        RuntimeError: when session tokens are disabled.
        SessionTokenError: tampered / expired / unknown-kid /
            wrong-instance token.
    """
    if self._session_tokens is None:
        raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
    try:
        return self._session_tokens.verify(token)
    except SessionTokenError as exc:
        request_id = str(uuid.uuid4())
        self._emit_session_token_event(
            "session_token_verification_failed",
            agent_id="",
            session_id="",
            request_id=request_id,
            errors=[
                ErrorRecord(
                    source_id="<broker>",
                    error_type=exc.reason_code,
                    message=str(exc),
                    trace_id=request_id,
                )
            ],
        )
        raise

rotate_signing_key(*, reviewer)

Rotate the session-token signing key on a LIVE broker (#25).

Mints a new primary in the shared :class:KeyRing; the previous primary moves to rotating-out so in-flight tokens keep verifying (grace window). Agents presenting old-kid tokens are lazily re-signed on their next request (see :meth:_process_session_token). The grace window is closed explicitly via :meth:revoke_signing_key.

Emits a signing_key_rotated audit entry recording the reviewer and the previous/new kid linkage.

Returns:

Type Description
str

The new primary kid.

Raises:

Type Description
RuntimeError

when session tokens are disabled.

Source code in nautilus/core/broker.py
def rotate_signing_key(self, *, reviewer: str) -> str:
    """Rotate the session-token signing key on a LIVE broker (#25).

    Mints a new primary in the shared :class:`KeyRing`; the previous
    primary moves to ``rotating-out`` so in-flight tokens keep
    verifying (grace window). Agents presenting old-kid tokens are
    lazily re-signed on their next request (see
    :meth:`_process_session_token`). The grace window is closed
    explicitly via :meth:`revoke_signing_key`.

    Emits a ``signing_key_rotated`` audit entry recording the reviewer
    and the previous/new kid linkage.

    Returns:
        The new primary kid.

    Raises:
        RuntimeError: when session tokens are disabled.
    """
    if self._key_ring is None:
        raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
    previous = self._key_ring.primary().kid
    new_entry = self._key_ring.rotate()
    self._emit_session_token_event(
        "signing_key_rotated",
        agent_id="",
        session_id="",
        request_id=str(uuid.uuid4()),
        trace=[
            f"reviewer={reviewer}",
            f"previous_kid={previous}",
            f"new_kid={new_entry.kid}",
        ],
    )
    return new_entry.kid

revoke_signing_key(kid, *, reason, reviewer)

Revoke a signing key immediately on a LIVE broker (#25).

Tokens signed by kid stop verifying at once — :meth:SessionTokenService.verify rejects revoked entries — so this is the explicit end of a rotation's grace window.

Emits a signing_key_revoked audit entry.

Raises:

Type Description
RuntimeError

when session tokens are disabled.

KeyError

when kid is not in the ring.

ValueError

when kid is the current primary — revoking it would make :meth:KeyRing.primary silently auto-generate an unaudited replacement (security review C1). Rotate first, then revoke the rotated-out key.

Source code in nautilus/core/broker.py
def revoke_signing_key(self, kid: str, *, reason: str, reviewer: str) -> None:
    """Revoke a signing key immediately on a LIVE broker (#25).

    Tokens signed by ``kid`` stop verifying at once —
    :meth:`SessionTokenService.verify` rejects revoked entries — so
    this is the explicit end of a rotation's grace window.

    Emits a ``signing_key_revoked`` audit entry.

    Raises:
        RuntimeError: when session tokens are disabled.
        KeyError: when ``kid`` is not in the ring.
        ValueError: when ``kid`` is the current primary — revoking it
            would make :meth:`KeyRing.primary` silently auto-generate
            an unaudited replacement (security review C1). Rotate
            first, then revoke the rotated-out key.
    """
    if self._key_ring is None:
        raise RuntimeError("session tokens are disabled (session_tokens.enabled: false)")
    if self._key_ring.verifier_for(kid) is None:
        raise KeyError(kid)
    if kid == self._key_ring.primary().kid:
        raise ValueError(f"kid {kid!r} is the current primary; rotate first, then revoke")
    self._key_ring.revoke(kid, reason=reason, reviewer=reviewer)
    self._emit_session_token_event(
        "signing_key_revoked",
        agent_id="",
        session_id="",
        request_id=str(uuid.uuid4()),
        trace=[f"reviewer={reviewer}", f"kid={kid}", f"reason={reason}"],
    )

request(agent_id, intent, context=None, *, fact_set_hash=None)

Sync request: guards against nested event loops, then runs pipeline.

Per design §8, calling this while inside a running event loop raises :class:RuntimeError whose message mentions arequest (UQ-4, AC-8.5). Outside a loop, we delegate to :meth:arequest via asyncio.run.

fact_set_hash (US-6 / FR-62 opaque round-trip) is accepted and echoed onto :attr:BrokerResponse.fact_set_hash; populating the audit/cost-cap pipeline that consumes it is staged work.

Source code in nautilus/core/broker.py
def request(
    self,
    agent_id: str,
    intent: str,
    context: dict[str, Any] | None = None,
    *,
    fact_set_hash: str | None = None,
) -> BrokerResponse:
    """Sync request: guards against nested event loops, then runs pipeline.

    Per design §8, calling this while inside a running event loop
    raises :class:`RuntimeError` whose message mentions ``arequest``
    (UQ-4, AC-8.5). Outside a loop, we delegate to
    :meth:`arequest` via ``asyncio.run``.

    ``fact_set_hash`` (US-6 / FR-62 opaque round-trip) is accepted
    and echoed onto :attr:`BrokerResponse.fact_set_hash`; populating
    the audit/cost-cap pipeline that consumes it is staged work.
    """
    try:
        asyncio.get_running_loop()
    except RuntimeError:
        # No running loop — safe to take ownership of a fresh one.
        pass
    else:
        raise RuntimeError(
            "Broker.request() called inside a running event loop. "
            "Use Broker.arequest() (async) from async contexts."
        )
    return self._run_sync(self.arequest(agent_id, intent, context, fact_set_hash=fact_set_hash))

arequest(agent_id, intent, context=None, *, fact_set_hash=None, caller=None) async

Async request pipeline (design §3.1, §8, §9).

caller is the transport's view of who is calling -- {"auth": <authenticated principal>, "peer": <network address>} -- and is what keys the cumulative-exposure ledger (§4.15). It is a parameter rather than a context key precisely because context is the caller's to fill in: a client that could set it could reset its own ledger. An in-library caller passes nothing and is identified by agent_id alone.

Linear sequence of awaits; heavy lifting lives in private helpers (_run_pipeline, _build_adapter_jobs, _gather_adapter_results, _build_response, _emit_audit). On policy-engine or unexpected failure, a single audit entry is still emitted before re-raising.

Source code in nautilus/core/broker.py
async def arequest(
    self,
    agent_id: str,
    intent: str,
    context: dict[str, Any] | None = None,
    *,
    fact_set_hash: str | None = None,
    caller: dict[str, str] | None = None,
) -> BrokerResponse:
    """Async request pipeline (design §3.1, §8, §9).

    ``caller`` is the transport's view of who is calling -- ``{"auth":
    <authenticated principal>, "peer": <network address>}`` -- and is what
    keys the cumulative-exposure ledger (§4.15). It is a parameter rather
    than a ``context`` key precisely because ``context`` is the caller's to
    fill in: a client that could set it could reset its own ledger. An
    in-library caller passes nothing and is identified by ``agent_id``
    alone.

    Linear sequence of awaits; heavy lifting lives in private helpers
    (`_run_pipeline`, `_build_adapter_jobs`, `_gather_adapter_results`,
    `_build_response`, `_emit_audit`). On policy-engine or unexpected
    failure, a single audit entry is still emitted before re-raising.
    """
    self._refuse_if_closed("arequest")
    await self._ensure_setup()
    with self._pinned_generation():
        return await self._arequest_pinned(agent_id, intent, context, fact_set_hash, caller)

declare_handoff(*, source_agent_id, receiving_agent_id, session_id, data_classifications, rule_trace_refs=None, data_compartments=None, session_token=None) async

Declare an agent-to-agent handoff and evaluate the handoff rule pack.

Reasoning-only path (design §3.6, FR-8, FR-10, AC-4.1): zero adapter calls. One session-store write, and only on action="allow": the receiving agent is recorded on the session row so :meth:_gate_session_ownership lets it continue a session another principal owns. Without that record a declared handoff was a statement with nothing behind it — the two parties merely agreed on a string, and so did anyone else who guessed the same one. Flow:

  1. Resolve both agents via :class:AgentRegistry. An unknown id short-circuits to action="deny" with a synthetic unknown-agent :class:DenialRecord (AC-4.2).
  2. Assert one data_handoff fact per declared classification with from_clearance / to_clearance read from the registered :class:AgentRecord entries.
  3. Call :meth:fathom.Engine.evaluate — the information-flow-violation default rule + any user rules matching data_handoff fire here.
  4. Collect denial_record facts; action is "allow" when none fired, "deny" otherwise. "escalate" is reserved for escalation-pack-driven denials and is not produced by the default rule set (AC-4.3).
  5. Emit exactly one :class:AuditEntry with event_type="handoff_declared" and the populated :class:HandoffDecision; never more (AC-4.4, NFR-15 parallel).

rule_trace_refs and data_compartments are accepted for forward-compat with the Phase-3 forensic worker + compartment- aware handoff rules; the default rule pack ignores both (empty compartments in the fathom-dominates calls).

Source code in nautilus/core/broker.py
async def declare_handoff(
    self,
    *,
    source_agent_id: str,
    receiving_agent_id: str,
    session_id: str,
    data_classifications: list[str],
    rule_trace_refs: list[str] | None = None,
    data_compartments: list[str] | None = None,
    session_token: str | None = None,
) -> HandoffDecision:
    """Declare an agent-to-agent handoff and evaluate the handoff rule pack.

    Reasoning-only path (design §3.6, FR-8, FR-10, AC-4.1): zero adapter
    calls. One session-store write, and only on ``action="allow"``: the
    receiving agent is recorded on the session row so
    :meth:`_gate_session_ownership` lets it continue a session another
    principal owns. Without that record a declared handoff was a statement
    with nothing behind it — the two parties merely agreed on a string, and
    so did anyone else who guessed the same one. Flow:

    1. Resolve both agents via :class:`AgentRegistry`. An unknown id
       short-circuits to ``action="deny"`` with a synthetic
       ``unknown-agent`` :class:`DenialRecord` (AC-4.2).
    2. Assert one ``data_handoff`` fact per declared classification
       with ``from_clearance`` / ``to_clearance`` read from the
       registered :class:`AgentRecord` entries.
    3. Call :meth:`fathom.Engine.evaluate` — the
       ``information-flow-violation`` default rule + any user rules
       matching ``data_handoff`` fire here.
    4. Collect ``denial_record`` facts; ``action`` is ``"allow"``
       when none fired, ``"deny"`` otherwise. ``"escalate"`` is
       reserved for escalation-pack-driven denials and is not
       produced by the default rule set (AC-4.3).
    5. Emit exactly one :class:`AuditEntry` with
       ``event_type="handoff_declared"`` and the populated
       :class:`HandoffDecision`; never more (AC-4.4, NFR-15
       parallel).

    ``rule_trace_refs`` and ``data_compartments`` are accepted for
    forward-compat with the Phase-3 forensic worker + compartment-
    aware handoff rules; the default rule pack ignores both (empty
    compartments in the ``fathom-dominates`` calls).
    """
    self._refuse_if_closed("declare_handoff")
    await self._ensure_setup()
    with self._pinned_generation():
        return await self._declare_handoff_pinned(
            source_agent_id=source_agent_id,
            receiving_agent_id=receiving_agent_id,
            session_id=session_id,
            data_classifications=data_classifications,
            rule_trace_refs=rule_trace_refs,
            data_compartments=data_compartments,
            session_token=session_token,
        )

aprobe_source(source_id) async

Dial what source_id connects to and report whether it answers.

Sources do not gate readiness -- a broker that can still serve three of four is ready, and draining it would remove the only thing that can answer for the three -- so nothing at HTTP level distinguished a healthy broker from one whose every backend was gone. This is what does: it opens the source's scheme://host[:port], makes the server answer (see :data:_PROBE_HELLO), and is bounded by that source's own timeout_s, capped at :data:_PROBE_TIMEOUT_CEILING_S.

It is a deliberately smaller claim than "this source will serve a query": a server that answers says nothing about credentials, about a database that is up but still recovering, or about a model the endpoint does not host. It is exactly the difference between a backend that is gone and one that is there, which is the question an operator has.

reachable is None -- never True -- when there was nothing to dial. A static source has no address, and a probe that answers for what it did not check is the failure this exists to end.

Source code in nautilus/core/broker.py
async def aprobe_source(self, source_id: str) -> SourceProbe:
    """Dial what ``source_id`` connects to and report whether it answers.

    Sources do not gate readiness -- a broker that can still serve three of
    four is ready, and draining it would remove the only thing that can
    answer for the three -- so nothing at HTTP level distinguished a
    healthy broker from one whose every backend was gone. This is what
    does: it opens the source's ``scheme://host[:port]``, makes the server
    answer (see :data:`_PROBE_HELLO`), and is bounded by that source's own
    ``timeout_s``, capped at :data:`_PROBE_TIMEOUT_CEILING_S`.

    It is a deliberately smaller claim than "this source will serve a
    query": a server that answers says nothing about credentials, about a
    database that is up but still recovering, or about a model the endpoint
    does not host. It is exactly the difference between a backend that is
    *gone* and one that is there, which is the question an operator has.

    ``reachable`` is ``None`` -- never ``True`` -- when there was nothing to
    dial. A ``static`` source has no address, and a probe that answers for
    what it did not check is the failure this exists to end.
    """
    endpoint = self._source_endpoint(source_id)
    if endpoint is None:
        return SourceProbe(None, None, "no remote endpoint")
    parts = urlsplit(endpoint)
    host = parts.hostname
    # No entry in the table means no probe: guessing a port is the same
    # mistake ``redact_connection`` refuses to make when it guesses a host.
    port = parts.port or _DEFAULT_PORTS.get(parts.scheme)
    if host is None or port is None:
        return SourceProbe(
            endpoint,
            None,
            f"no port in {endpoint!r} and no default for scheme {parts.scheme!r}",
        )
    try:
        budget = self._registry.get(source_id).timeout_s
    except KeyError:  # pragma: no cover -- _source_endpoint already returned
        budget = None
    timeout_s = min(budget or _PROBE_TIMEOUT_CEILING_S, _PROBE_TIMEOUT_CEILING_S)
    try:
        note = await asyncio.wait_for(_dial(host, port, parts.scheme), timeout_s)
    except TimeoutError:
        return SourceProbe(
            endpoint, False, f"no answer from {endpoint} within {timeout_s:.1f}s"
        )
    # Total, deliberately. This is the whole of one source's answer and
    # ``GET /v1/adapters?probe=true`` gathers every source's, so anything
    # that escapes here 500s the endpoint and discards every healthy
    # sibling's result. Enumerating classes did not hold: ``except OSError``
    # missed ``UnicodeEncodeError`` from a host the resolver cannot encode,
    # and adding ``UnicodeError`` still missed ``OverflowError`` from a port
    # above 65535. A malformed address is a probe failure, not a crash, and
    # the class name is what the operator needs to see.
    except Exception as exc:
        return SourceProbe(endpoint, False, f"{type(exc).__name__}: {exc}")
    return SourceProbe(endpoint, True, note)

setup() async

Idempotent async setup — stand up persistent session schema.

Calls :meth:PostgresSessionStore.setup when the broker is wired with a Postgres-backed session store (design §3.2, UQ-1 / D-2). No-op for the Phase-1 :class:~nautilus.core.session.InMemorySessionStore. Safe to call multiple times; each implementer owns its own idempotency.

Also runs schema fingerprint checks for all registered adapters per AC-21.c/e. Adapters with major drift are quarantined.

Source code in nautilus/core/broker.py
async def setup(self) -> None:
    """Idempotent async setup — stand up persistent session schema.

    Calls :meth:`PostgresSessionStore.setup` when the broker is wired with
    a Postgres-backed session store (design §3.2, UQ-1 / D-2). No-op for
    the Phase-1 :class:`~nautilus.core.session.InMemorySessionStore`.
    Safe to call multiple times; each implementer owns its own idempotency.

    Also runs schema fingerprint checks for all registered adapters per
    AC-21.c/e. Adapters with major drift are quarantined.
    """
    if isinstance(self._session_store, (PostgresSessionStore, SqliteSessionStore)):
        await self._session_store.setup()
    await self._check_schema_fingerprints()
    self._setup_done = True

on_reload(listener)

Call listener after every adopted reload, on the reload's task.

For state derived from the config and held outside the broker -- the credential allow-list a transport copies onto app.state at startup and re-reads per request. The reload has already installed the new generation when this runs, so a listener reads :attr:config and gets what was just adopted.

A listener that raises does not fail the reload, does not stop the ones after it, and is named in :attr:last_reload_notes so the audit entry records that some outside state did not follow the config.

There is no off_reload: a listener stays registered for the lifetime of the broker. Building two apps against one broker therefore pins the first app's closure until the broker is closed. Acceptable rather than fixed: create_app registers once per app, one broker per process is the only shape serve builds, and a listener that writes to a dead app's state is a write nothing reads.

Source code in nautilus/core/broker.py
def on_reload(self, listener: Callable[[], None]) -> None:
    """Call ``listener`` after every adopted reload, on the reload's task.

    For state derived from the config and held outside the broker -- the
    credential allow-list a transport copies onto ``app.state`` at startup
    and re-reads per request. The reload has already installed the new
    generation when this runs, so a listener reads :attr:`config` and gets
    what was just adopted.

    A listener that raises does not fail the reload, does not stop the
    ones after it, and is named in :attr:`last_reload_notes` so the audit
    entry records that some outside state did not follow the config.

    There is no ``off_reload``: a listener stays registered for the
    lifetime of the broker. Building two apps against one broker therefore
    pins the first app's closure until the broker is closed. Acceptable
    rather than fixed: ``create_app`` registers once per app, one broker
    per process is the only shape ``serve`` builds, and a listener that
    writes to a dead app's ``state`` is a write nothing reads.
    """
    self._reload_listeners.append(listener)

reload(candidate) async

Adopt candidate's sources, rules, credentials and live limits.

candidate is a broker built from the new file by the same function nautilus serve and nautilus config check use (:func:nautilus.cli.serve.broker_for_serve), so a config this process would refuse to start on has already raised before this is called and in the same words. Nothing here re-validates: a second validator is a validator that drifts.

What is adopted: the source registry and its adapters, the Fathom router (rule files are re-read, not just the rules: stanza, so an edit inside a user_rules_dirs directory arrives too), the intent analyzer -- its keyword map is generated from sources (#24), so a source change that did not rebuild it would route on a stale vocabulary -- api.keys, which is how a credential is rotated without a restart, and the two session_store limits the request path reads per request.

A transport holds the credential list on its own app.state and re-reads it per request, so it is handed the new one through :meth:on_reload rather than by reaching into the ASGI app from here.

What is not: everything :data:RELOADABLE_TOP_LEVEL_KEYS, :data:RELOADABLE_API_KEYS and :data:RELOADABLE_SESSION_STORE_KEYS leave out. A file that changes one of those is refused whole rather than half-applied; see :func:_restart_only_changes.

Adapter identity is preserved for every source whose spec is byte-identical: the live adapter object keeps its pool, its connect cooldown and its _connected_adapters entry, and the candidate's unused twin is closed. A source that changed or vanished has its adapter retired and its connect bookkeeping dropped so the replacement dials on next use. Schema-drift baselines and quarantines are keyed by source id and survive a reload untouched: re-pointing a source at another table must not be a way to clear a quarantine nautilus adapters schema-ack is the way to clear.

Returns:

Type Description
list[str]

The dotted names of the keys actually adopted, in file order.

list[str]

Empty when the new file is equivalent to the running one.

Raises:

Type Description
ConfigNotReloadableError

When the new file changes a key that is only read at startup. candidate is closed first.

Source code in nautilus/core/broker.py
async def reload(self, candidate: Broker) -> list[str]:
    """Adopt ``candidate``'s sources, rules, credentials and live limits.

    ``candidate`` is a broker built from the new file by the *same*
    function ``nautilus serve`` and ``nautilus config check`` use
    (:func:`nautilus.cli.serve.broker_for_serve`), so a config this
    process would refuse to start on has already raised before this is
    called and in the same words. Nothing here re-validates: a second
    validator is a validator that drifts.

    What is adopted: the source registry and its adapters, the Fathom
    router (rule files are re-read, not just the ``rules:`` stanza, so an
    edit inside a ``user_rules_dirs`` directory arrives too), the intent
    analyzer -- its keyword map is generated from ``sources`` (#24), so a
    source change that did not rebuild it would route on a stale
    vocabulary -- ``api.keys``, which is how a credential is rotated
    without a restart, and the two ``session_store`` limits the request
    path reads per request.

    A transport holds the credential list on its own ``app.state`` and
    re-reads it per request, so it is handed the new one through
    :meth:`on_reload` rather than by reaching into the ASGI app from
    here.

    What is not: everything :data:`RELOADABLE_TOP_LEVEL_KEYS`,
    :data:`RELOADABLE_API_KEYS` and :data:`RELOADABLE_SESSION_STORE_KEYS`
    leave out. A file that changes
    one of those is refused whole rather than half-applied; see
    :func:`_restart_only_changes`.

    Adapter identity is preserved for every source whose spec is
    byte-identical: the live adapter object keeps its pool, its connect
    cooldown and its ``_connected_adapters`` entry, and the candidate's
    unused twin is closed. A source that changed or vanished has its
    adapter retired and its connect bookkeeping dropped so the replacement
    dials on next use. Schema-drift baselines and quarantines are keyed by
    source id and survive a reload untouched: re-pointing a source at
    another table must not be a way to clear a quarantine
    ``nautilus adapters schema-ack`` is the way to clear.

    Returns:
        The dotted names of the keys actually adopted, in file order.
        Empty when the new file is equivalent to the running one.

    Raises:
        ConfigNotReloadableError: When the new file changes a key that is
            only read at startup. ``candidate`` is closed first.
    """
    self._refuse_if_closed("reload")
    incoming = candidate._generation  # noqa: SLF001 - transplanting into self
    async with self._reload_lock:
        outgoing = self._generation
        blocked = _restart_only_changes(outgoing.config, incoming.config)
        if blocked:
            await candidate.aclose()
            raise ConfigNotReloadableError(
                "these keys are read once at startup and cannot be reloaded: "
                + ", ".join(blocked)
                + ". Restart the process to adopt them."
            )

        adapters, retired = self._reconcile_adapters(outgoing, incoming)
        self._generation = _ConfigGeneration(
            config=incoming.config,
            registry=incoming.registry,
            router=incoming.router,
            adapters=adapters,
            intent_analyzer=incoming.intent_analyzer,
        )
        adopted = _adopted_changes(outgoing.config, incoming.config)

    notes: list[str] = []
    self._last_reload_notes = notes
    if "api.keys" in adopted:
        orphaned = _warn_about_orphaned_ledgers(outgoing.config, incoming.config)
        if orphaned:
            notes.append(f"cleared {orphaned} cumulative-exposure ledger(s)")
    for listener in self._reload_listeners:
        # ``on_reload`` is public, so this loop calls code the broker does
        # not own, after the generation is already installed and before the
        # outgoing one is retired. An escaping raise left the reload applied
        # but unrecorded (``reload_config`` catches only
        # ``ConfigNotReloadableError``), leaked the outgoing router and
        # retired adapters, and skipped every listener after the first --
        # so a second transport went on serving a retired credential while
        # the audit log said nothing. The failure is recorded instead, on
        # the receipt as well as in the log, because a listener that did
        # not run is state that no longer matches the adopted config.
        try:
            listener()
        except Exception:
            log.exception("a post-reload listener failed; the reload stands")
            # Its name, never its ``repr``: a listener bound to the
            # credential list would print it.
            named = getattr(listener, "__qualname__", type(listener).__name__)
            notes.append(f"a post-reload listener failed: {named}")

    # The candidate is now a shell around exactly what this reload retired:
    # the outgoing router, the adapters no longer in service, and its own
    # never-used audit sink, attestation sink and session store. One
    # ``aclose`` disposes of all of it, in the order ``aclose`` documents.
    candidate._generation = _ConfigGeneration(  # noqa: SLF001 - retiring it
        config=incoming.config,
        registry=incoming.registry,
        router=outgoing.router,
        adapters={str(i): adapter for i, adapter in enumerate(retired)},
        intent_analyzer=incoming.intent_analyzer,
    )
    await self._retire(outgoing, candidate)
    return adopted

emit_reload_event(event_type, detail)

Write one config-reload entry to the audit log.

A reload changes which sources exist and which rules decide, so the log that has to explain a decision has to record when that changed and when a change was refused. detail is the operator-facing sentence: the adopted keys, or the reason for the refusal -- the same words serve prints, when the refusal came from the config validator.

Source code in nautilus/core/broker.py
def emit_reload_event(self, event_type: ReloadEventType, detail: str) -> None:
    """Write one config-reload entry to the audit log.

    A reload changes which sources exist and which rules decide, so the
    log that has to explain a decision has to record when that changed and
    when a change was refused. ``detail`` is the operator-facing sentence:
    the adopted keys, or the reason for the refusal -- the same words
    ``serve`` prints, when the refusal came from the config validator.
    """
    self._audit_logger.emit(
        AuditEntry(
            timestamp=AuditLogger.utcnow(),
            request_id=str(uuid.uuid4()),
            agent_id="<broker>",
            session_id=None,
            raw_intent=detail,
            intent_analysis=IntentAnalysis(raw_intent="", data_types_needed=[], entities=[]),
            facts_asserted_summary={},
            routing_decisions=[],
            scope_constraints=[],
            denial_records=[],
            error_records=[],
            rule_trace=[],
            ruleset_hash=self.ruleset_hash,
            sources_queried=[],
            sources_denied=[],
            sources_skipped=[],
            sources_errored=[],
            attestation_token=None,
            duration_ms=0,
            event_type=event_type,
        )
    )

emit_adapter_event(event_type, adapter_id)

Write one adapter-lifecycle entry to the audit log.

These events belong to an adapter, not to a request: no intent, no sources, no decisions. adapter_id is the only thing that distinguishes one from another, which is why the entry carries it.

Public because nautilus adapters schema-ack records the operator's override through the same broker and the same log as the drift entry it overrides.

Source code in nautilus/core/broker.py
def emit_adapter_event(self, event_type: AdapterEventType, adapter_id: str) -> None:
    """Write one adapter-lifecycle entry to the audit log.

    These events belong to an adapter, not to a request: no intent, no
    sources, no decisions. ``adapter_id`` is the only thing that
    distinguishes one from another, which is why the entry carries it.

    Public because ``nautilus adapters schema-ack`` records the operator's
    override through the same broker and the same log as the drift entry
    it overrides.
    """
    self._audit_logger.emit(
        AuditEntry(
            timestamp=AuditLogger.utcnow(),
            request_id=str(uuid.uuid4()),
            agent_id="<broker>",
            session_id=None,
            raw_intent="",
            intent_analysis=IntentAnalysis(raw_intent="", data_types_needed=[], entities=[]),
            facts_asserted_summary={},
            routing_decisions=[],
            scope_constraints=[],
            denial_records=[],
            error_records=[],
            rule_trace=[],
            sources_queried=[],
            sources_denied=[],
            sources_skipped=[],
            sources_errored=[],
            attestation_token=None,
            duration_ms=0,
            event_type=event_type,
            adapter_id=adapter_id,
        )
    )

__enter__()

Enter the sync lifecycle: run :meth:setup, hand back the broker.

setup() is mandatory for the persistent session stores — it is what creates their schema — and every example spelled it out by hand, or forgot to. So does close(): it refuses to run inside a running event loop, which is why the docs' try/finally is doing real work and why the async form below exists separately.

Source code in nautilus/core/broker.py
def __enter__(self) -> Broker:
    """Enter the sync lifecycle: run :meth:`setup`, hand back the broker.

    ``setup()`` is mandatory for the persistent session stores — it is
    what creates their schema — and every example spelled it out by hand,
    or forgot to. So does ``close()``: it refuses to run inside a running
    event loop, which is why the docs' ``try/finally`` is doing real work
    and why the async form below exists separately.
    """
    self._run_sync(self.setup())
    return self

__exit__(*exc_info)

Close on the way out, whatever happened inside.

Source code in nautilus/core/broker.py
def __exit__(self, *exc_info: object) -> None:
    """Close on the way out, whatever happened inside."""
    self.close()

__aenter__() async

Async counterpart of :meth:__enter__.

Source code in nautilus/core/broker.py
async def __aenter__(self) -> Broker:
    """Async counterpart of :meth:`__enter__`."""
    await self.setup()
    return self

__aexit__(*exc_info) async

Async counterpart of :meth:__exit__.

Source code in nautilus/core/broker.py
async def __aexit__(self, *exc_info: object) -> None:
    """Async counterpart of :meth:`__exit__`."""
    await self.aclose()

close()

Idempotent sync close — FR-17, AC-8.6.

Source code in nautilus/core/broker.py
def close(self) -> None:
    """Idempotent sync close — FR-17, AC-8.6."""
    try:
        asyncio.get_running_loop()
    except RuntimeError:
        pass
    else:
        raise RuntimeError(
            "Broker.close() called inside a running event loop. "
            "Use Broker.aclose() (async) from async contexts."
        )
    if self._sync_loop is None:
        # Nothing ran on the sync facade, so there is no loop to close on.
        asyncio.run(self.aclose())
        return
    loop, thread = self._sync_loop, self._sync_thread
    try:
        self._run_sync(self.aclose())
    finally:
        loop.call_soon_threadsafe(loop.stop)
        if thread is not None:
            thread.join(timeout=30)
        loop.close()
        self._sync_loop = None
        self._sync_thread = None

aclose() async

Idempotent async close. Safe to call multiple times (FR-17).

Ordering contract (D-8, design §3.14, AC-14.6): session_store.aclose() → attestation_sink.close() → adapter-pool release. Session-store flush must precede sink close (session writes during request must land before sink teardown); adapter release comes last so in-flight emits can still reference pooled connections above. Any close is best-effort (one failing backend must not prevent others from closing).

Source code in nautilus/core/broker.py
async def aclose(self) -> None:
    """Idempotent async close. Safe to call multiple times (FR-17).

    Ordering contract (D-8, design §3.14, AC-14.6):
    ``session_store.aclose()`` → ``attestation_sink.close()`` →
    adapter-pool release. Session-store flush must precede sink close
    (session writes during request must land before sink teardown);
    adapter release comes last so in-flight emits can still reference
    pooled connections above. Any close is best-effort (one failing
    backend must not prevent others from closing).
    """
    if self._closed:
        return
    self._closed = True
    # 1. Session store: flush any in-flight writes before downstream close.
    if hasattr(self._session_store, "aclose"):
        with contextlib.suppress(Exception):
            await self._session_store.aclose()  # type: ignore[attr-defined]
    # 2. Attestation sink: release the store-and-forward handle AFTER
    #    session writes have flushed but BEFORE adapter pools go down —
    #    in-flight emits from step 1's session-state finalization may
    #    still reference adapter connections.
    with contextlib.suppress(Exception):
        await self._attestation_sink.close()
    # 2b. Audit sink: a chained audit log holds an exclusive writer lock
    #     and an open file handle. Nothing closed it before, so a process
    #     that built two brokers in sequence refused its own second one.
    audit_close = getattr(self._audit_logger.sink, "close", None)
    if audit_close is not None:
        with contextlib.suppress(Exception):
            audit_close()
    # 3. Adapters — release pools last so in-flight attestation can still
    #    reference their connections above.
    for adapter in self._adapters.values():
        try:
            await adapter.close()
        except Exception:
            # Best-effort, but not silent: swallowing this is how a pool
            # that never closed looked like a clean shutdown.
            log.warning("adapter close failed during aclose()", exc_info=True)
            continue
    self._router.close()

BrokerResponse

nautilus.core.models.BrokerResponse

Bases: BaseModel

Public result of :meth:Broker.arequest (design §4.8).

Aggregates per-source successes, denials, skips, and errors plus the optional attestation JWT. data maps each successful source_id to the list of returned rows.

Source code in nautilus/core/models.py
class BrokerResponse(BaseModel):
    """Public result of :meth:`Broker.arequest` (design §4.8).

    Aggregates per-source successes, denials, skips, and errors plus the
    optional attestation JWT. ``data`` maps each successful ``source_id``
    to the list of returned rows.
    """

    request_id: str
    data: dict[str, list[dict[str, Any]]]
    sources_queried: list[str]
    sources_denied: list[str]
    sources_skipped: list[str]
    sources_errored: list[ErrorRecord]
    # Why each denied source was denied, and which rule said so. The router
    # produces these for every request and the audit entry has always carried
    # them; returning ids alone left the agent that has to decide what to do
    # next reading a JSONL file on the broker's host. Defaults empty so a
    # Phase-1 response round-trips unchanged (NFR-5).
    denial_records: list[DenialRecord] = Field(default_factory=list["DenialRecord"])
    # Why each skipped source took no part.
    skip_records: list[SkipRecord] = Field(default_factory=list["SkipRecord"])
    # The rules that fired, in order — the same trace the audit entry records.
    rule_trace: list[str] = Field(default_factory=list[str])
    scope_restrictions: dict[str, list[ScopeConstraint]]
    attestation_token: str | None
    duration_ms: int
    cap_breached: bool | None = None
    fact_set_hash: str | None = None
    # Sources whose rows were cut short — by the adapter's own row cap, or by
    # a transport bound such as ``mcp.max_response_bytes``. Either way it means
    # the caller was given a subset. Empty (not ``None``) when nothing
    # truncated, so a Phase-1 response round-trips unchanged (NFR-5).
    truncated_sources: list[str] = Field(default_factory=list[str])
    source_session_signatures: dict[str, dict[str, Any]] | None = None
    # Session-provenance JWS (#18, AC-18.a). Populated only when the broker
    # is configured with ``session_tokens.enabled: true``; ``None`` otherwise
    # so Phase-1 responses round-trip unchanged (NFR-5).
    session_token: str | None = None
    # What each queried source is, keyed by id. ``None`` when the broker did
    # not populate it, so a Phase-1 response round-trips unchanged (NFR-5).
    source_info: dict[str, SourceInfo] | None = None
    # Which ruleset answered. Every rolling deploy passes through a state where
    # two replicas hold different rulesets, and the identical request then
    # alternates allowed / denied behind the load balancer -- so a caller who is
    # denied retries and is allowed. The audit entry has carried this all along;
    # the response the caller actually reads did not. ``None`` when unset, so a
    # Phase-1 response round-trips unchanged (NFR-5).
    ruleset_hash: str | None = None

    @computed_field  # serialized, so REST and MCP callers get it too
    @property
    def outcome(self) -> Literal["allowed", "denied", "errored", "skipped"]:
        """One word for what happened.

        ``allowed`` if any source answered; else ``denied`` if a source the
        request actually concerned was refused; else ``errored`` if any
        source failed; else ``skipped``.

        Denials used to outrank errors unconditionally, and every rules-refused
        source emits a record whether or not the intent had anything to do with
        it. So a request whose one relevant database was unreachable reported
        ``denied`` — sending the reader after a policy rule while a service was
        down — and a question nothing configured could answer reported ``denied``
        too, citing sources that held nothing like what was asked for.

        The ``sources_denied`` clause is the compatibility path: a response
        deserialized without ``denial_records`` has no relevance to read, so its
        denials still count.
        """
        if self.sources_queried:
            return "allowed"
        if any(d.relevant for d in self.denial_records) or (
            self.sources_denied and not self.denial_records
        ):
            return "denied"
        if self.sources_errored:
            return "errored"
        return "skipped"

outcome property

One word for what happened.

allowed if any source answered; else denied if a source the request actually concerned was refused; else errored if any source failed; else skipped.

Denials used to outrank errors unconditionally, and every rules-refused source emits a record whether or not the intent had anything to do with it. So a request whose one relevant database was unreachable reported denied — sending the reader after a policy rule while a service was down — and a question nothing configured could answer reported denied too, citing sources that held nothing like what was asked for.

The sources_denied clause is the compatibility path: a response deserialized without denial_records has no relevance to read, so its denials still count.