gate4agent-node 0.4.5

Native gate4agent node server: wraps providers and owns PTY/inline sessions, the file browser, local git, and worktrees for its machine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
//! Node-envelope HTTP + WebSocket browser bridge.
//!
//! Separate from framed node-wire C2 and from the ops `--api-listen` HTTP
//! observer (`http_api`). Loopback-only TCP bind; optional `GATE4AGENT_BRIDGE_TOKEN`
//! (never logged). Product bytes are HTTP+WS over TCP. Tip 6: optional mesh
//! underlay UDP accept relays those same TCP bytes over tip-5 encrypted path
//! + underlay token barrier (crypto ≠ auth). UDP is not a second observe/drive
//! dialect — see mesh-connectivity-daemon-design tip 6.
//!
//! Observe projection (tip 3): slim per-session inventory over WS / HTTP
//! snapshot — address, status, screen kind, opaque browser_profile_id when
//! bound. No terminal_frame contents, history messages, cookies, or tokens.
//!
//! Drive (tip 4): optional `POST /bridge/drive` maps a small NodeRequest
//! subset (`prompt` / `paste`) through the node envelope
//! (`dispatch_input_bounded` + runtime policy). Bridge token + loopback are
//! the auth barrier (not C2 controller lease). No raw PTY bytes / TerminalBytes
//! / cookies / OAuth as product API.
//!
//! Streaming observe: `/bridge/ws` keeps the connection, sends an initial
//! `bridge.hello` slim snapshot (`seq: 0`), then change-driven `observe.delta`
//! (or forced/full `observe.snapshot`) frames. Unchanged ticks are suppressed.
//! `GATE4AGENT_BRIDGE_OBSERVE_MODE=snapshot-only` restores every-tick full
//! snapshots (streaming tip behavior). Client ping→pong; close or node
//! shutdown ends the loop. Not a C2 controller; no cookies/OAuth/secrets.

use super::NodeShared;
use crate::protocol::{
    BUILD_STAMP, MAX_NODE_TEXT_BYTES, NodeFailure, NodeFailureCode, SessionAddress, SessionKey,
    WorkspaceId,
};
use crate::provider_runtime::ProviderRuntimeRequirement;
use gate4agent_types::{
    AgentInstanceId, InputAction, PreparedInputKind, PromptFraming, PromptPayload, PtyScreenState,
    SessionGeneration, SessionSnapshot, SessionStatus, TerminalSize,
};
use serde::Deserialize;
use serde_json::{json, Value};
use std::collections::BTreeMap;
use std::io;
use std::net::SocketAddr;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tokio::time::timeout;

const SERVICE_NAME: &str = "gate4agent-node-bridge";
const HEADER_LIMIT_BYTES: usize = 16 * 1024;
const HEADER_READ_TIMEOUT: Duration = Duration::from_secs(3);
const WRITE_TIMEOUT: Duration = Duration::from_secs(3);
const MAX_BRIDGE_CONNECTIONS: usize = 8;
/// JSON envelope overhead budget on top of `MAX_NODE_TEXT_BYTES` for drive POST.
const BRIDGE_DRIVE_BODY_OVERHEAD: usize = 1_024;
const MAX_BRIDGE_DRIVE_BODY_BYTES: usize = MAX_NODE_TEXT_BYTES + BRIDGE_DRIVE_BODY_OVERHEAD;
/// Default period between observe ticks on an open `/bridge/ws`.
/// Slim projections only — keep short enough for MLC attach, long enough to
/// avoid busy-looping the node snapshot lock.
const DEFAULT_OBSERVE_PERIOD: Duration = Duration::from_secs(1);
/// Forced full `observe.snapshot` every N emitted delta ticks (~1 min @ 1s).
const SNAPSHOT_FLOOR_TICKS: u32 = 60;
/// If upserts+removes exceed this, emit full snapshot instead of delta.
const DELTA_ROW_CAP: usize = 32;
/// Escape hatch: `snapshot-only` ≡ always-full ticks (streaming tip).
const BRIDGE_OBSERVE_MODE_ENV: &str = "GATE4AGENT_BRIDGE_OBSERVE_MODE";
/// Cap client control-frame payload (RFC6455 ping/pong/close body).
const WS_CONTROL_PAYLOAD_MAX: usize = 125;
/// Cap inbound client data frame for this tip (observe-only; drive is HTTP).
const WS_CLIENT_DATA_PAYLOAD_MAX: usize = 64 * 1024;
/// RFC6455 GUID for `Sec-WebSocket-Accept`.
const WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";

/// Optional node-local bridge shared secret (distinct from NODE_TOKEN).
/// Never log the value. Empty/`None` = loopback bind is the only gate.
#[derive(Clone, Default)]
pub(super) struct BridgeAuth {
    token: Option<String>,
}

impl BridgeAuth {
    pub(super) fn from_optional(token: Option<String>) -> Result<Self, BridgeAuthError> {
        match token {
            None => Ok(Self { token: None }),
            Some(value) if value.is_empty() || value.len() > 4_096 => Err(BridgeAuthError::Invalid),
            Some(value) => Ok(Self { token: Some(value) }),
        }
    }

    fn authorized(&self, provided: Option<&str>) -> bool {
        match self.token.as_deref() {
            None => true,
            Some(expected) => match provided {
                Some(got) => tokens_match(got, expected),
                None => false,
            },
        }
    }
}

#[derive(Debug)]
pub(super) enum BridgeAuthError {
    Invalid,
}

/// Tip 6: accept mesh underlay peers and relay TCP to local `--bridge-listen`.
/// Application HTTP+WS dialect unchanged. HQ never accepts (NodePeer role).
#[derive(Clone)]
pub(super) struct BridgeUnderlayAccept {
    pub bind: SocketAddr,
    pub transport_key: [u8; 32],
    pub underlay_token: String,
}

/// Observe emit mode on `/bridge/ws` (design §5.2).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ObserveMode {
    /// Change-driven: suppress unchanged ticks; emit `observe.delta` or forced full.
    Delta,
    /// Always full `observe.snapshot` each tick (≡ streaming tip `3f5aedc`).
    SnapshotOnly,
}

impl ObserveMode {
    fn from_env() -> Self {
        match std::env::var(BRIDGE_OBSERVE_MODE_ENV).ok().as_deref() {
            Some(value) if value.eq_ignore_ascii_case("snapshot-only") => Self::SnapshotOnly,
            _ => Self::Delta,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct SessionGauges {
    control_plane: usize,
    native_pty: usize,
}

/// Keyed tip-3 slim rows for deterministic diff `(instance_id, generation)`.
type ProjectionMap = BTreeMap<(u64, u64), Value>;

fn projection_row_key(row: &Value) -> Option<(u64, u64)> {
    let instance_id = row.get("instance_id")?.as_u64()?;
    let generation = row.get("generation")?.as_u64()?;
    Some((instance_id, generation))
}

fn projections_to_map(projections: &[Value]) -> ProjectionMap {
    let mut map = BTreeMap::new();
    for row in projections {
        if let Some(key) = projection_row_key(row) {
            map.insert(key, row.clone());
        }
    }
    map
}

fn map_to_projections(map: &ProjectionMap) -> Vec<Value> {
    map.values().cloned().collect()
}

/// Diff last vs now: whole-row upserts + `{instance_id, generation}` removes.
fn diff_projections(last: &ProjectionMap, now: &ProjectionMap) -> (Vec<Value>, Vec<Value>) {
    let mut upserts = Vec::new();
    for (key, row) in now {
        match last.get(key) {
            Some(prev) if prev == row => {}
            _ => upserts.push(row.clone()),
        }
    }
    let mut removes = Vec::new();
    for (instance_id, generation) in last.keys() {
        if !now.contains_key(&(*instance_id, *generation)) {
            removes.push(json!({
                "instance_id": instance_id,
                "generation": generation,
            }));
        }
    }
    (upserts, removes)
}

#[derive(Debug, PartialEq)]
enum TickDecision {
    Suppress,
    Snapshot,
    Delta { upserts: Vec<Value>, removes: Vec<Value> },
}

fn decide_observe_tick(
    mode: ObserveMode,
    last: &ProjectionMap,
    last_gauges: SessionGauges,
    now: &ProjectionMap,
    now_gauges: SessionGauges,
    ticks_since_snapshot: u32,
) -> TickDecision {
    if mode == ObserveMode::SnapshotOnly {
        return TickDecision::Snapshot;
    }
    if last == now && last_gauges == now_gauges {
        return TickDecision::Suppress;
    }
    let (upserts, removes) = diff_projections(last, now);
    let row_churn = upserts.len().saturating_add(removes.len());
    if ticks_since_snapshot.saturating_add(1) >= SNAPSHOT_FLOOR_TICKS {
        return TickDecision::Snapshot;
    }
    if row_churn > DELTA_ROW_CAP {
        return TickDecision::Snapshot;
    }
    // Gauge-only change (no row churn): always delta — design §4.2.
    if row_churn == 0 {
        return TickDecision::Delta { upserts, removes };
    }
    // Size heuristic: if projected delta JSON ≥ ~80% of full snapshot, send full.
    let delta_probe = json!({
        "upserts": &upserts,
        "removes": &removes,
        "sessions": {
            "control_plane": now_gauges.control_plane,
            "native_pty": now_gauges.native_pty,
        },
    });
    let full_probe = json!({
        "session_projections": map_to_projections(now),
        "sessions": {
            "control_plane": now_gauges.control_plane,
            "native_pty": now_gauges.native_pty,
        },
    });
    let delta_len = serde_json::to_vec(&delta_probe).map(|v| v.len()).unwrap_or(0);
    let full_len = serde_json::to_vec(&full_probe).map(|v| v.len()).unwrap_or(1);
    if full_len > 0 && delta_len.saturating_mul(10) >= full_len.saturating_mul(8) {
        return TickDecision::Snapshot;
    }
    TickDecision::Delta { upserts, removes }
}

pub(super) async fn run(
    listen: Option<SocketAddr>,
    auth: BridgeAuth,
    shared: Arc<NodeShared>,
    underlay: Option<BridgeUnderlayAccept>,
) -> io::Result<()> {
    let Some(listen) = listen else {
        // Underlay without local bridge door is refused at config time.
        wait_for_shutdown(&shared).await;
        return Ok(());
    };
    let listener = TcpListener::bind(listen).await?;
    let local_bridge = listener.local_addr()?;
    let underlay_reach = underlay.is_some();
    if let Some(underlay) = underlay {
        let underlay_shared = Arc::clone(&shared);
        let underlay_task = tokio::spawn(async move {
            run_bridge_underlay_accept(underlay, local_bridge, underlay_shared).await
        });
        let result = serve_listener_with_underlay_flag(
            listener,
            auth,
            shared,
            underlay_reach,
        )
        .await;
        underlay_task.abort();
        let _ = underlay_task.await;
        result
    } else {
        serve_listener_with_underlay_flag(listener, auth, shared, false).await
    }
}

async fn serve_listener_with_underlay_flag(
    listener: TcpListener,
    auth: BridgeAuth,
    shared: Arc<NodeShared>,
    underlay_reach: bool,
) -> io::Result<()> {
    serve_listener_with_observe_options(
        listener,
        auth,
        shared,
        DEFAULT_OBSERVE_PERIOD,
        ObserveMode::from_env(),
        underlay_reach,
    )
    .await
}

/// Tip 6 underlay accept loop: NodePeer role, token barrier, TCP relay to
/// local loopback bridge. Re-binds after each session (tip-5 listener is
/// single-accept). Not a WireGuard daemon. HQ DialOnly enforced in underlay.
async fn run_bridge_underlay_accept(
    underlay: BridgeUnderlayAccept,
    local_bridge: SocketAddr,
    shared: Arc<NodeShared>,
) -> io::Result<()> {
    use gate4agent_node_wire::mesh_underlay::{
        accept_peer_on, serve_bridge_tcp_relay, underlay_to_io, MeshUnderlayRole,
        UnderlayAuthToken, UnderlayTransportKey,
    };

    let transport = UnderlayTransportKey::from_bytes(underlay.transport_key);
    let auth = UnderlayAuthToken::new(underlay.underlay_token.clone())
        .map_err(|e| underlay_to_io(e))?;

    loop {
        if shared.shutdown.load(Ordering::Acquire) {
            return Ok(());
        }
        let listener = accept_peer_on(
            MeshUnderlayRole::NodePeer,
            &transport,
            &auth,
            underlay.bind,
        )
        .await
        .map_err(underlay_to_io)?;

        let shutdown = shared.shutdown_notify.notified();
        tokio::pin!(shutdown);
        tokio::select! {
            biased;
            _ = &mut shutdown => {
                if shared.shutdown.load(Ordering::Acquire) {
                    return Ok(());
                }
            }
            accepted = listener.accept() => {
                match accepted {
                    Ok(session) => {
                        let auth = auth.clone();
                        // One relay at a time this tip; ignore relay errors
                        // (bad token / peer drop) and re-accept.
                        let _ = serve_bridge_tcp_relay(session, &auth, local_bridge).await;
                    }
                    Err(err) => return Err(underlay_to_io(err)),
                }
            }
        }
    }
}

async fn wait_for_shutdown(shared: &NodeShared) {
    loop {
        let notified = shared.shutdown_notify.notified();
        tokio::pin!(notified);
        if shared.shutdown.load(Ordering::Acquire) {
            return;
        }
        notified.await;
    }
}

async fn serve_listener(
    listener: TcpListener,
    auth: BridgeAuth,
    shared: Arc<NodeShared>,
) -> io::Result<()> {
    serve_listener_with_observe_options(
        listener,
        auth,
        shared,
        DEFAULT_OBSERVE_PERIOD,
        ObserveMode::from_env(),
        false,
    )
    .await
}

async fn serve_listener_with_observe_period(
    listener: TcpListener,
    auth: BridgeAuth,
    shared: Arc<NodeShared>,
    observe_period: Duration,
) -> io::Result<()> {
    serve_listener_with_observe_options(
        listener,
        auth,
        shared,
        observe_period,
        ObserveMode::from_env(),
        false,
    )
    .await
}

async fn serve_listener_with_observe_options(
    listener: TcpListener,
    auth: BridgeAuth,
    shared: Arc<NodeShared>,
    observe_period: Duration,
    observe_mode: ObserveMode,
    underlay_reach: bool,
) -> io::Result<()> {
    let permits = Arc::new(Semaphore::new(MAX_BRIDGE_CONNECTIONS));
    let mut connections = JoinSet::new();
    loop {
        let shutdown = shared.shutdown_notify.notified();
        tokio::pin!(shutdown);
        if shared.shutdown.load(Ordering::Acquire) {
            break;
        }
        tokio::select! {
            biased;
            _ = &mut shutdown => {
                if shared.shutdown.load(Ordering::Acquire) {
                    break;
                }
            }
            accepted = listener.accept() => {
                let (stream, peer) = accepted?;
                // Defense in depth: refuse non-loopback peers even if bind was loopback.
                if !peer.ip().is_loopback() {
                    drop(stream);
                    continue;
                }
                let Ok(permit) = Arc::clone(&permits).try_acquire_owned() else {
                    drop(stream);
                    continue;
                };
                let connection_shared = Arc::clone(&shared);
                let connection_auth = auth.clone();
                let connection_underlay = underlay_reach;
                connections.spawn(async move {
                    let _permit = permit;
                    let _ = serve_connection(
                        stream,
                        connection_auth,
                        connection_shared,
                        observe_period,
                        observe_mode,
                        connection_underlay,
                    )
                    .await;
                });
            }
        }
        while let Some(result) = connections.try_join_next() {
            result.map_err(|error| io::Error::new(io::ErrorKind::Other, error))?;
        }
    }
    connections.shutdown().await;
    Ok(())
}

async fn serve_connection(
    mut stream: TcpStream,
    auth: BridgeAuth,
    shared: Arc<NodeShared>,
    observe_period: Duration,
    observe_mode: ObserveMode,
    underlay_reach: bool,
) -> io::Result<()> {
    let request = match timeout(HEADER_READ_TIMEOUT, read_request(&mut stream)).await {
        Ok(Ok(request)) => request,
        Ok(Err(ReadRequestError::TooLarge)) => {
            return write_http(&mut stream, HttpResponse::plain(413, "Payload Too Large")).await;
        }
        Ok(Err(ReadRequestError::Closed | ReadRequestError::Invalid)) | Err(_) => return Ok(()),
        Ok(Err(ReadRequestError::Io(error))) => return Err(error),
    };

    let path_only = request
        .target
        .split_once('?')
        .map_or(request.target.as_str(), |(path, _)| path);

    if path_only == "/bridge/ws" {
        return handle_websocket_upgrade(stream, request, &auth, &shared, observe_period, observe_mode).await;
    }

    if path_only == "/bridge/drive" {
        return handle_drive(&mut stream, request, &auth, &shared).await;
    }

    if request.method != "GET" {
        return write_http(
            &mut stream,
            HttpResponse::plain(405, "Method Not Allowed").with_header("Allow", "GET"),
        )
        .await;
    }

    let provided = extract_bridge_token(&request);
    if !auth.authorized(provided.as_deref()) {
        return write_http(
            &mut stream,
            HttpResponse::plain(401, "Unauthorized").with_header("WWW-Authenticate", "Bearer"),
        )
        .await;
    }

    let response = match path_only {
        "/bridge/health" | "/health" => HttpResponse::json(200, health_body(&shared, underlay_reach)),
        "/bridge/snapshot" => HttpResponse::json(200, observe_snapshot_body(&shared)),
        _ => HttpResponse::plain(404, "Not Found"),
    };
    write_http(&mut stream, response).await
}

async fn handle_websocket_upgrade(
    mut stream: TcpStream,
    request: Request,
    auth: &BridgeAuth,
    shared: &NodeShared,
    observe_period: Duration,
    observe_mode: ObserveMode,
) -> io::Result<()> {
    if request.method != "GET" {
        return write_http(
            &mut stream,
            HttpResponse::plain(405, "Method Not Allowed").with_header("Allow", "GET"),
        )
        .await;
    }

    let provided = extract_bridge_token(&request);
    if !auth.authorized(provided.as_deref()) {
        return write_http(
            &mut stream,
            HttpResponse::plain(401, "Unauthorized").with_header("WWW-Authenticate", "Bearer"),
        )
        .await;
    }

    if !is_websocket_upgrade(&request) {
        return write_http(&mut stream, HttpResponse::plain(400, "Bad Request")).await;
    }
    let Some(key) = request.sec_websocket_key.as_deref() else {
        return write_http(&mut stream, HttpResponse::plain(400, "Bad Request")).await;
    };
    if request.sec_websocket_version.as_deref() != Some("13") {
        return write_http(
            &mut stream,
            HttpResponse::plain(426, "Upgrade Required")
                .with_header("Sec-WebSocket-Version", "13"),
        )
        .await;
    }

    let accept = sec_websocket_accept(key);
    let upgrade = format!(
        "HTTP/1.1 101 Switching Protocols\r\n\
Upgrade: websocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Accept: {accept}\r\n\
\r\n"
    );
    timeout(WRITE_TIMEOUT, stream.write_all(upgrade.as_bytes()))
        .await
        .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "bridge ws upgrade write timed out"))??;

    // Initial hello + keep connection for change-driven observe.
    // Drive remains HTTP POST /bridge/drive (tip 4). Mesh underlay later.
    run_ws_observe_loop(&mut stream, shared, observe_period, observe_mode).await
}

/// Keep `/bridge/ws` open: hello once (`seq: 0`), then change-driven ticks.
/// Default mode suppresses unchanged ticks and emits `observe.delta` with
/// `basis_seq`; forced full `observe.snapshot` per floor/cap/size rules.
/// `ObserveMode::SnapshotOnly` restores every-tick full snapshots.
/// Responds to client ping with pong; ends on client close, read error, or
/// node shutdown. Ignores inbound text/binary (drive is HTTP, not WS C2).
async fn run_ws_observe_loop(
    stream: &mut TcpStream,
    shared: &NodeShared,
    observe_period: Duration,
    observe_mode: ObserveMode,
) -> io::Result<()> {
    let (control_plane, native_pty, projections) = observe_core(shared);
    let mut last_map = projections_to_map(&projections);
    let mut last_gauges = SessionGauges {
        control_plane,
        native_pty,
    };
    write_ws_text(
        stream,
        &observe_hello_frame_from(shared, control_plane, native_pty, &projections),
    )
    .await?;

    let period = if observe_period.is_zero() {
        DEFAULT_OBSERVE_PERIOD
    } else {
        observe_period
    };
    let mut ticker = tokio::time::interval(period);
    ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
    // The first interval tick completes immediately; hello already covers it.
    ticker.tick().await;

    let mut seq: u64 = 0;
    let mut ticks_since_snapshot: u32 = 0;
    loop {
        let shutdown = shared.shutdown_notify.notified();
        tokio::pin!(shutdown);
        if shared.shutdown.load(Ordering::Acquire) {
            break;
        }
        tokio::select! {
            biased;
            _ = &mut shutdown => {
                if shared.shutdown.load(Ordering::Acquire) {
                    break;
                }
            }
            _ = ticker.tick() => {
                let (control_plane, native_pty, projections) = observe_core(shared);
                let now_map = projections_to_map(&projections);
                let now_gauges = SessionGauges {
                    control_plane,
                    native_pty,
                };
                let decision = decide_observe_tick(
                    observe_mode,
                    &last_map,
                    last_gauges,
                    &now_map,
                    now_gauges,
                    ticks_since_snapshot,
                );
                match decision {
                    TickDecision::Suppress => {}
                    TickDecision::Snapshot => {
                        seq = seq.saturating_add(1);
                        let frame = observe_snapshot_frame(
                            shared,
                            seq,
                            control_plane,
                            native_pty,
                            &projections,
                        );
                        if write_ws_text(stream, &frame).await.is_err() {
                            break;
                        }
                        last_map = now_map;
                        last_gauges = now_gauges;
                        ticks_since_snapshot = 0;
                    }
                    TickDecision::Delta { upserts, removes } => {
                        let basis_seq = seq;
                        seq = seq.saturating_add(1);
                        let frame = observe_delta_frame(
                            shared,
                            seq,
                            basis_seq,
                            control_plane,
                            native_pty,
                            &upserts,
                            &removes,
                        );
                        if write_ws_text(stream, &frame).await.is_err() {
                            break;
                        }
                        last_map = now_map;
                        last_gauges = now_gauges;
                        ticks_since_snapshot = ticks_since_snapshot.saturating_add(1);
                    }
                }
            }
            frame = read_client_ws_frame(stream) => {
                match frame {
                    Ok(ClientWsFrame::Close) => break,
                    Ok(ClientWsFrame::Ping(payload)) => {
                        if write_ws_control(stream, 0x0A, &payload).await.is_err() {
                            break;
                        }
                    }
                    Ok(ClientWsFrame::Pong | ClientWsFrame::Data) => {}
                    Err(_) => break,
                }
            }
        }
    }

    let _ = write_ws_control(stream, 0x08, &[]).await;
    Ok(())
}

async fn write_ws_text(stream: &mut TcpStream, value: &Value) -> io::Result<()> {
    let payload = serde_json::to_vec(value)
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
    let frame = encode_server_text_frame(&payload);
    timeout(WRITE_TIMEOUT, stream.write_all(&frame))
        .await
        .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "bridge ws frame write timed out"))??;
    Ok(())
}

async fn write_ws_control(stream: &mut TcpStream, opcode: u8, payload: &[u8]) -> io::Result<()> {
    let frame = encode_server_control_frame(opcode, payload);
    timeout(WRITE_TIMEOUT, stream.write_all(&frame))
        .await
        .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "bridge ws control write timed out"))??;
    Ok(())
}

enum ClientWsFrame {
    Data,
    Ping(Vec<u8>),
    Pong,
    Close,
}

/// Read one client WebSocket frame (masked per RFC6455).
async fn read_client_ws_frame(stream: &mut TcpStream) -> io::Result<ClientWsFrame> {
    let mut header = [0u8; 2];
    stream.read_exact(&mut header).await?;
    let opcode = header[0] & 0x0f;
    let masked = header[1] & 0x80 != 0;
    if !masked {
        // RFC6455: client→server frames MUST be masked.
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "bridge ws client frame missing mask",
        ));
    }
    let mut payload_len = (header[1] & 0x7f) as u64;
    if payload_len == 126 {
        let mut ext = [0u8; 2];
        stream.read_exact(&mut ext).await?;
        payload_len = u16::from_be_bytes(ext) as u64;
    } else if payload_len == 127 {
        let mut ext = [0u8; 8];
        stream.read_exact(&mut ext).await?;
        payload_len = u64::from_be_bytes(ext);
    }
    let max = match opcode {
        0x8 | 0x9 | 0xA => WS_CONTROL_PAYLOAD_MAX as u64,
        _ => WS_CLIENT_DATA_PAYLOAD_MAX as u64,
    };
    if payload_len > max {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "bridge ws client frame too large",
        ));
    }
    let mut mask = [0u8; 4];
    stream.read_exact(&mut mask).await?;
    let mut payload = vec![0u8; payload_len as usize];
    if !payload.is_empty() {
        stream.read_exact(&mut payload).await?;
        for (index, byte) in payload.iter_mut().enumerate() {
            *byte ^= mask[index % 4];
        }
    }
    match opcode {
        0x1 | 0x2 | 0x0 => Ok(ClientWsFrame::Data),
        0x8 => Ok(ClientWsFrame::Close),
        0x9 => Ok(ClientWsFrame::Ping(payload)),
        0xA => Ok(ClientWsFrame::Pong),
        _ => Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "bridge ws unsupported opcode",
        )),
    }
}

fn is_websocket_upgrade(request: &Request) -> bool {
    let upgrade = request
        .upgrade
        .as_deref()
        .map(|value| value.eq_ignore_ascii_case("websocket"))
        .unwrap_or(false);
    let connection = request
        .connection
        .as_deref()
        .map(|value| {
            value
                .split(',')
                .any(|part| part.trim().eq_ignore_ascii_case("upgrade"))
        })
        .unwrap_or(false);
    upgrade && connection
}

fn health_body(shared: &NodeShared, underlay_reach: bool) -> Value {
    json!({
        "ok": true,
        "service": SERVICE_NAME,
        "door": "node-envelope-bridge",
        "node_id": shared.node_id,
        "incarnation_id": shared.incarnation_id,
        "version": env!("CARGO_PKG_VERSION"),
        "build_stamp": BUILD_STAMP,
        "started_at_unix_ms": shared.started_at_unix_ms,
        "protocols": {
            "application": ["http", "websocket"],
            "transport_tcp": true,
            // Tip 6: UDP underlay carries TCP to local bridge; not a second app dialect.
            "transport_udp_underlay": if underlay_reach {
                "tip-6-bridge-reach"
            } else {
                "opt-in-via-bridge-underlay-listen"
            },
            "hq_mesh_role": "dial-only",
        },
    })
}

fn observe_snapshot_body(shared: &NodeShared) -> Value {
    observe_hello_frame(shared)
}

/// Typed observe payload with slim per-session projections.
/// Privacy: no terminal_frame contents/formatted, no history messages, no
/// cookies/OAuth, no bridge/node tokens. Screen is kind (+ short classifiers)
/// only — OperatorGate options (on-screen choice labels) are stripped.
fn observe_core(shared: &NodeShared) -> (usize, usize, Vec<Value>) {
    let control = shared.handle.snapshot();
    let control_plane_sessions = control.sessions.len();
    let native_pty_sessions = shared.native_session_gauge.load(Ordering::Relaxed);
    let session_projections = slim_session_projections(shared, &control.sessions);
    (
        control_plane_sessions,
        native_pty_sessions,
        session_projections,
    )
}

fn observe_hello_frame(shared: &NodeShared) -> Value {
    let (control_plane_sessions, native_pty_sessions, session_projections) = observe_core(shared);
    observe_hello_frame_from(
        shared,
        control_plane_sessions,
        native_pty_sessions,
        &session_projections,
    )
}

fn observe_hello_frame_from(
    shared: &NodeShared,
    control_plane_sessions: usize,
    native_pty_sessions: usize,
    session_projections: &[Value],
) -> Value {
    json!({
        "type": "bridge.hello",
        "observe": {
            "type": "observe.snapshot",
            "seq": 0,
            "sessions": {
                "control_plane": control_plane_sessions,
                "native_pty": native_pty_sessions,
            },
            "session_projections": session_projections,
            "note": "slim session projection; drive via POST /bridge/drive (prompt|paste); WS change-driven observe.delta (snapshot-only escape hatch); tip-6 bridge-over-underlay when --bridge-underlay-listen set",
        },
        "node_id": shared.node_id,
        "incarnation_id": shared.incarnation_id,
        "door": "node-envelope-bridge",
    })
}

fn observe_snapshot_frame(
    shared: &NodeShared,
    seq: u64,
    control_plane_sessions: usize,
    native_pty_sessions: usize,
    session_projections: &[Value],
) -> Value {
    json!({
        "type": "observe.snapshot",
        "seq": seq,
        "sessions": {
            "control_plane": control_plane_sessions,
            "native_pty": native_pty_sessions,
        },
        "session_projections": session_projections,
        "node_id": shared.node_id,
        "incarnation_id": shared.incarnation_id,
        "door": "node-envelope-bridge",
    })
}

fn observe_delta_frame(
    shared: &NodeShared,
    seq: u64,
    basis_seq: u64,
    control_plane_sessions: usize,
    native_pty_sessions: usize,
    upserts: &[Value],
    removes: &[Value],
) -> Value {
    json!({
        "type": "observe.delta",
        "seq": seq,
        "basis_seq": basis_seq,
        "sessions": {
            "control_plane": control_plane_sessions,
            "native_pty": native_pty_sessions,
        },
        "upserts": upserts,
        "removes": removes,
        "node_id": shared.node_id,
        "incarnation_id": shared.incarnation_id,
        "door": "node-envelope-bridge",
    })
}

const SLIM_FAILED_MESSAGE_MAX: usize = 160;
const SLIM_PROCESS_NAME_MAX: usize = 64;
const SLIM_FAILING_REASON_MAX: usize = 96;

fn slim_session_projections(
    shared: &NodeShared,
    sessions: &[SessionSnapshot],
) -> Vec<Value> {
    let bindings = shared
        .session_bindings
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    let mut out = Vec::with_capacity(sessions.len());
    for session in sessions {
        let Some(binding) = bindings.get(&session.instance_id) else {
            continue;
        };
        if binding.generation != session.generation {
            continue;
        }
        let browser_profile_id = binding
            .environment_profile
            .as_ref()
            .and_then(|receipt| receipt.browser_profile_id.as_ref())
            .map(|id| id.as_str().to_owned());
        let network_allowlist = binding
            .environment_profile
            .as_ref()
            .and_then(|receipt| receipt.network_allowlist.as_ref())
            .map(|id| id.as_str().to_owned());
        out.push(json!({
            "workspace_id": binding.workspace_id.as_str(),
            "instance_id": session.instance_id.0,
            "generation": session.generation.0,
            "agent_id": session.agent_id.as_str(),
            "transport": session.transport,
            "status": slim_session_status(&session.status),
            "screen": slim_screen_state(&session.screen_state),
            "pending_input": session.pending_input.map(slim_pending_input),
            "terminal_size": session.terminal_size.map(slim_terminal_size),
            "browser_profile_id": browser_profile_id,
            "network_allowlist": network_allowlist,
            // Explicitly absent: terminal_frame, history, provider internals,
            // cwd/paths, cookies, tokens.
        }));
    }
    out
}

fn slim_session_status(status: &SessionStatus) -> Value {
    match status {
        SessionStatus::Registered => json!({ "kind": "registered" }),
        SessionStatus::Starting => json!({ "kind": "starting" }),
        SessionStatus::Running => json!({ "kind": "running" }),
        SessionStatus::Stopping => json!({ "kind": "stopping" }),
        SessionStatus::Exited { exit_code } => json!({
            "kind": "exited",
            "exit_code": exit_code,
        }),
        SessionStatus::Failed { message } => json!({
            "kind": "failed",
            "message": truncate_chars(message, SLIM_FAILED_MESSAGE_MAX),
        }),
    }
}

fn slim_screen_state(screen: &PtyScreenState) -> Value {
    match screen {
        PtyScreenState::Unknown => json!({ "kind": "unknown" }),
        PtyScreenState::NotAgent { observed_process } => json!({
            "kind": "not-agent",
            "observed_process": truncate_chars(
                &basename_process(observed_process),
                SLIM_PROCESS_NAME_MAX,
            ),
        }),
        PtyScreenState::OperatorGate { gate } => json!({
            "kind": "operator-gate",
            // Kind only — strip options (on-screen labels) and subject detail.
            "gate_kind": gate.kind,
        }),
        PtyScreenState::Failing { reason } => json!({
            "kind": "failing",
            "reason": truncate_chars(reason, SLIM_FAILING_REASON_MAX),
        }),
        PtyScreenState::Ready => json!({ "kind": "ready" }),
    }
}

fn slim_pending_input(kind: PreparedInputKind) -> Value {
    // Serialize the enum via serde so rename_all kebab-case stays honest.
    serde_json::to_value(kind).unwrap_or_else(|_| json!("unknown"))
}

fn slim_terminal_size(size: TerminalSize) -> Value {
    json!({ "rows": size.rows, "columns": size.columns })
}

fn basename_process(raw: &str) -> String {
    let trimmed = raw.trim();
    let name = trimmed
        .rsplit(['/', '\\'])
        .next()
        .unwrap_or(trimmed)
        .trim();
    if name.is_empty() {
        "unknown".to_owned()
    } else {
        name.to_owned()
    }
}

fn truncate_chars(value: &str, max: usize) -> String {
    if value.chars().count() <= max {
        return value.to_owned();
    }
    let mut out: String = value.chars().take(max.saturating_sub(1)).collect();
    out.push('…');
    out
}

/// Tip-4 drive envelope: only prompt/paste through node request types.
/// Never TerminalBytes / raw PTY / cookies / OAuth.
#[derive(Debug, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
enum BridgeDriveRequest {
    Prompt {
        workspace_id: String,
        instance_id: u64,
        generation: u64,
        text: String,
    },
    Paste {
        workspace_id: String,
        instance_id: u64,
        generation: u64,
        text: String,
    },
}

impl BridgeDriveRequest {
    fn kind_label(&self) -> &'static str {
        match self {
            Self::Prompt { .. } => "prompt",
            Self::Paste { .. } => "paste",
        }
    }

    fn parts(&self) -> (&str, u64, u64, &str) {
        match self {
            Self::Prompt {
                workspace_id,
                instance_id,
                generation,
                text,
            }
            | Self::Paste {
                workspace_id,
                instance_id,
                generation,
                text,
            } => (workspace_id.as_str(), *instance_id, *generation, text.as_str()),
        }
    }
}

async fn handle_drive(
    stream: &mut TcpStream,
    request: Request,
    auth: &BridgeAuth,
    shared: &NodeShared,
) -> io::Result<()> {
    if request.method != "POST" {
        return write_http(
            stream,
            HttpResponse::plain(405, "Method Not Allowed").with_header("Allow", "POST"),
        )
        .await;
    }

    let provided = extract_bridge_token(&request);
    if !auth.authorized(provided.as_deref()) {
        return write_http(
            stream,
            HttpResponse::plain(401, "Unauthorized").with_header("WWW-Authenticate", "Bearer"),
        )
        .await;
    }

    let body = match read_http_body(stream, &request).await {
        Ok(body) => body,
        Err(BodyReadError::TooLarge) => {
            return write_http(stream, HttpResponse::plain(413, "Payload Too Large")).await;
        }
        Err(BodyReadError::Invalid) => {
            return write_http(stream, HttpResponse::plain(400, "Bad Request")).await;
        }
        Err(BodyReadError::Io(error)) => return Err(error),
        Err(BodyReadError::TimedOut) => return Ok(()),
    };

    let drive = match serde_json::from_slice::<BridgeDriveRequest>(&body) {
        Ok(drive) => drive,
        Err(_) => {
            return write_http(
                stream,
                HttpResponse::json(
                    400,
                    json!({
                        "ok": false,
                        "type": "drive.error",
                        "code": "invalid-request",
                        "message": "drive body must be JSON prompt|paste with session address + text",
                    }),
                ),
            )
            .await;
        }
    };

    let response = match execute_bridge_drive(shared, drive).await {
        Ok(kind) => HttpResponse::json(
            200,
            json!({
                "ok": true,
                "type": "drive.accepted",
                "kind": kind,
                "door": "node-envelope-bridge",
            }),
        ),
        Err(error) => HttpResponse::json(
            drive_http_status(error.code),
            json!({
                "ok": false,
                "type": "drive.error",
                "code": error.code,
                "message": error.message,
                "door": "node-envelope-bridge",
            }),
        ),
    };
    write_http(stream, response).await
}

async fn execute_bridge_drive(
    shared: &NodeShared,
    drive: BridgeDriveRequest,
) -> Result<&'static str, NodeFailure> {
    let kind = drive.kind_label();
    let (workspace_id_raw, instance_id, generation, text) = drive.parts();
    if text.is_empty() {
        return Err(super::failure(
            NodeFailureCode::InvalidRequest,
            "drive text must be non-empty",
        ));
    }
    if text.len() > MAX_NODE_TEXT_BYTES {
        return Err(super::failure(
            NodeFailureCode::InvalidRequest,
            "drive text exceeds the node text byte limit",
        ));
    }
    // Mirror NodeRequest::Prompt/Paste validate_node_text without logging the text.
    super::validate_node_text(kind, text)?;

    let workspace_id = WorkspaceId::new(workspace_id_raw).map_err(|_| {
        super::failure(
            NodeFailureCode::InvalidRequest,
            "workspace_id is not a valid node identifier",
        )
    })?;
    let session = SessionAddress {
        workspace_id,
        session: SessionKey {
            instance_id: AgentInstanceId(instance_id),
            generation: SessionGeneration(generation),
        },
    };

    // Bridge door auth is loopback + optional BRIDGE_TOKEN — not C2 controller.
    let agent_id = shared.validate_address(&session)?;
    shared.require_session_runtime_policy(
        &session,
        ProviderRuntimeRequirement::SemanticPrompt,
    )?;

    let action = match kind {
        "prompt" => InputAction::SubmitPrompt(PromptPayload {
            text: text.to_owned(),
            framing: super::prompt_framing(&agent_id),
        }),
        "paste" => InputAction::InsertDraft(PromptPayload {
            text: text.to_owned(),
            framing: PromptFraming::BracketedPaste,
        }),
        _ => {
            return Err(super::failure(
                NodeFailureCode::InvalidRequest,
                "unsupported drive kind",
            ));
        }
    };
    shared.dispatch_input_bounded(&session, action).await?;
    Ok(kind)
}

fn drive_http_status(code: NodeFailureCode) -> u16 {
    match code {
        NodeFailureCode::UnknownSession | NodeFailureCode::UnknownWorkspace => 404,
        NodeFailureCode::StaleGeneration
        | NodeFailureCode::SessionWorkspaceMismatch
        | NodeFailureCode::TurnInFlight => 409,
        NodeFailureCode::UnsupportedCapability => 422,
        NodeFailureCode::BackendBusy => 503,
        NodeFailureCode::BackendOperationFailed => 502,
        NodeFailureCode::Unauthorized | NodeFailureCode::ControllerRequired => 401,
        _ => 400,
    }
}

enum BodyReadError {
    TooLarge,
    Invalid,
    TimedOut,
    Io(io::Error),
}

async fn read_http_body(stream: &mut TcpStream, request: &Request) -> Result<Vec<u8>, BodyReadError> {
    let Some(content_length) = request.content_length else {
        return Err(BodyReadError::Invalid);
    };
    if content_length > MAX_BRIDGE_DRIVE_BODY_BYTES {
        return Err(BodyReadError::TooLarge);
    }
    let mut body = Vec::with_capacity(content_length);
    body.extend_from_slice(&request.body_prefix);
    if body.len() > content_length {
        return Err(BodyReadError::Invalid);
    }
    while body.len() < content_length {
        let mut chunk = vec![0u8; (content_length - body.len()).min(4_096)];
        match timeout(HEADER_READ_TIMEOUT, stream.read(&mut chunk)).await {
            Ok(Ok(0)) => return Err(BodyReadError::Invalid),
            Ok(Ok(read)) => body.extend_from_slice(&chunk[..read]),
            Ok(Err(error)) => return Err(BodyReadError::Io(error)),
            Err(_) => return Err(BodyReadError::TimedOut),
        }
        if body.len() > content_length {
            return Err(BodyReadError::Invalid);
        }
    }
    Ok(body)
}

fn extract_bridge_token(request: &Request) -> Option<String> {
    if let Some(header) = request.authorization.as_deref() {
        let bearer = header
            .strip_prefix("Bearer ")
            .or_else(|| header.strip_prefix("bearer "));
        if let Some(token) = bearer {
            let trimmed = token.trim();
            if !trimmed.is_empty() {
                return Some(trimmed.to_owned());
            }
        }
    }
    if let Some(header) = request.bridge_token_header.as_deref() {
        let trimmed = header.trim();
        if !trimmed.is_empty() {
            return Some(trimmed.to_owned());
        }
    }
    query_param(&request.target, "bridge_token")
}

fn query_param(target: &str, key: &str) -> Option<String> {
    let query = target.split_once('?')?.1;
    for pair in query.split('&') {
        let (name, value) = pair.split_once('=').unwrap_or((pair, ""));
        if name == key && !value.is_empty() {
            // Percent-decoding is out of scope for this slice; operators
            // should prefer Authorization / header with unescaped secrets.
            return Some(value.to_owned());
        }
    }
    None
}

fn tokens_match(provided: &str, expected: &str) -> bool {
    let a = provided.as_bytes();
    let b = expected.as_bytes();
    // Length mismatch is an immediate refuse; equal-length path is a
    // byte-wise XOR fold (no early exit) so compare cost does not track
    // the first differing index. Distinct from NODE_TOKEN / C2 auth.
    if a.len() != b.len() {
        return false;
    }
    let mut diff = 0u8;
    for (left, right) in a.iter().zip(b.iter()) {
        diff |= left ^ right;
    }
    diff == 0
}

fn sec_websocket_accept(key: &str) -> String {
    let mut material = Vec::with_capacity(key.len() + WS_GUID.len());
    material.extend_from_slice(key.as_bytes());
    material.extend_from_slice(WS_GUID);
    let digest = ring::digest::digest(&ring::digest::SHA1_FOR_LEGACY_USE_ONLY, &material);
    base64_encode(digest.as_ref())
}

fn encode_server_text_frame(payload: &[u8]) -> Vec<u8> {
    encode_server_data_frame(0x01, payload)
}

fn encode_server_control_frame(opcode: u8, payload: &[u8]) -> Vec<u8> {
    // Control frames: payload must fit in one byte length (≤125).
    let truncated = if payload.len() > WS_CONTROL_PAYLOAD_MAX {
        &payload[..WS_CONTROL_PAYLOAD_MAX]
    } else {
        payload
    };
    encode_server_data_frame(opcode & 0x0f, truncated)
}

fn encode_server_data_frame(opcode: u8, payload: &[u8]) -> Vec<u8> {
    let mut frame = Vec::with_capacity(2 + payload.len() + 8);
    frame.push(0x80 | (opcode & 0x0f)); // FIN + opcode
    if payload.len() < 126 {
        frame.push(payload.len() as u8);
    } else if payload.len() <= u16::MAX as usize {
        frame.push(126);
        frame.extend_from_slice(&(payload.len() as u16).to_be_bytes());
    } else {
        frame.push(127);
        frame.extend_from_slice(&(payload.len() as u64).to_be_bytes());
    }
    frame.extend_from_slice(payload);
    frame
}

fn base64_encode(input: &[u8]) -> String {
    const TABLE: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let mut out = String::with_capacity((input.len() + 2) / 3 * 4);
    let mut i = 0;
    while i + 3 <= input.len() {
        let n = ((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8) | (input[i + 2] as u32);
        out.push(TABLE[((n >> 18) & 63) as usize] as char);
        out.push(TABLE[((n >> 12) & 63) as usize] as char);
        out.push(TABLE[((n >> 6) & 63) as usize] as char);
        out.push(TABLE[(n & 63) as usize] as char);
        i += 3;
    }
    match input.len() - i {
        1 => {
            let n = (input[i] as u32) << 16;
            out.push(TABLE[((n >> 18) & 63) as usize] as char);
            out.push(TABLE[((n >> 12) & 63) as usize] as char);
            out.push('=');
            out.push('=');
        }
        2 => {
            let n = ((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8);
            out.push(TABLE[((n >> 18) & 63) as usize] as char);
            out.push(TABLE[((n >> 12) & 63) as usize] as char);
            out.push(TABLE[((n >> 6) & 63) as usize] as char);
            out.push('=');
        }
        _ => {}
    }
    out
}

struct Request {
    method: String,
    target: String,
    authorization: Option<String>,
    bridge_token_header: Option<String>,
    upgrade: Option<String>,
    connection: Option<String>,
    sec_websocket_key: Option<String>,
    sec_websocket_version: Option<String>,
    content_length: Option<usize>,
    /// Bytes already read past the header terminator (may be empty).
    body_prefix: Vec<u8>,
}

enum ReadRequestError {
    Closed,
    Invalid,
    TooLarge,
    Io(io::Error),
}

async fn read_request(stream: &mut TcpStream) -> Result<Request, ReadRequestError> {
    let mut buffer = Vec::with_capacity(512);
    loop {
        if buffer.len() > HEADER_LIMIT_BYTES {
            return Err(ReadRequestError::TooLarge);
        }
        let mut chunk = [0u8; 512];
        let read = stream
            .read(&mut chunk)
            .await
            .map_err(ReadRequestError::Io)?;
        if read == 0 {
            return Err(ReadRequestError::Closed);
        }
        buffer.extend_from_slice(&chunk[..read]);
        if buffer.len() > HEADER_LIMIT_BYTES {
            return Err(ReadRequestError::TooLarge);
        }
        if let Some(header_end) = find_header_end(&buffer) {
            let header = std::str::from_utf8(&buffer[..header_end])
                .map_err(|_| ReadRequestError::Invalid)?;
            let body_prefix = buffer[header_end..].to_vec();
            return parse_request(header, body_prefix);
        }
    }
}

fn find_header_end(buffer: &[u8]) -> Option<usize> {
    buffer.windows(4).position(|window| window == b"\r\n\r\n").map(|index| index + 4)
}

fn parse_request(header: &str, body_prefix: Vec<u8>) -> Result<Request, ReadRequestError> {
    let mut lines = header.split("\r\n");
    let request_line = lines.next().ok_or(ReadRequestError::Invalid)?;
    let mut parts = request_line.split_whitespace();
    let method = parts.next().ok_or(ReadRequestError::Invalid)?.to_owned();
    let target = parts.next().ok_or(ReadRequestError::Invalid)?.to_owned();
    let _version = parts.next().ok_or(ReadRequestError::Invalid)?;
    if parts.next().is_some() {
        return Err(ReadRequestError::Invalid);
    }
    let mut authorization = None;
    let mut bridge_token_header = None;
    let mut upgrade = None;
    let mut connection = None;
    let mut sec_websocket_key = None;
    let mut sec_websocket_version = None;
    let mut content_length = None;
    for line in lines {
        if line.is_empty() {
            break;
        }
        let Some((name, value)) = line.split_once(':') else {
            return Err(ReadRequestError::Invalid);
        };
        let name = name.trim();
        let value = value.trim().to_owned();
        if name.eq_ignore_ascii_case("Authorization") {
            authorization = Some(value);
        } else if name.eq_ignore_ascii_case("X-Gate4agent-Bridge-Token") {
            bridge_token_header = Some(value);
        } else if name.eq_ignore_ascii_case("Upgrade") {
            upgrade = Some(value);
        } else if name.eq_ignore_ascii_case("Connection") {
            connection = Some(value);
        } else if name.eq_ignore_ascii_case("Sec-WebSocket-Key") {
            sec_websocket_key = Some(value);
        } else if name.eq_ignore_ascii_case("Sec-WebSocket-Version") {
            sec_websocket_version = Some(value);
        } else if name.eq_ignore_ascii_case("Content-Length") {
            let parsed = value
                .parse::<usize>()
                .map_err(|_| ReadRequestError::Invalid)?;
            if parsed > MAX_BRIDGE_DRIVE_BODY_BYTES {
                return Err(ReadRequestError::TooLarge);
            }
            content_length = Some(parsed);
        }
    }
    Ok(Request {
        method,
        target,
        authorization,
        bridge_token_header,
        upgrade,
        connection,
        sec_websocket_key,
        sec_websocket_version,
        content_length,
        body_prefix,
    })
}

struct HttpResponse {
    status: u16,
    reason: &'static str,
    headers: Vec<(&'static str, String)>,
    body: Vec<u8>,
}

impl HttpResponse {
    fn plain(status: u16, body: &'static str) -> Self {
        Self {
            status,
            reason: reason_phrase(status),
            headers: vec![("Content-Type", "text/plain; charset=utf-8".into())],
            body: body.as_bytes().to_vec(),
        }
    }

    fn json(status: u16, body: Value) -> Self {
        match serde_json::to_vec(&body) {
            Ok(bytes) => Self {
                status,
                reason: reason_phrase(status),
                headers: vec![("Content-Type", "application/json".into())],
                body: bytes,
            },
            Err(_) => Self::plain(503, "Service Unavailable"),
        }
    }

    fn with_header(mut self, name: &'static str, value: impl Into<String>) -> Self {
        self.headers.push((name, value.into()));
        self
    }
}

fn reason_phrase(status: u16) -> &'static str {
    match status {
        200 => "OK",
        400 => "Bad Request",
        401 => "Unauthorized",
        404 => "Not Found",
        405 => "Method Not Allowed",
        409 => "Conflict",
        413 => "Payload Too Large",
        422 => "Unprocessable Entity",
        426 => "Upgrade Required",
        502 => "Bad Gateway",
        503 => "Service Unavailable",
        _ => "Error",
    }
}

async fn write_http(stream: &mut TcpStream, response: HttpResponse) -> io::Result<()> {
    let mut out = format!(
        "HTTP/1.1 {} {}\r\nContent-Length: {}\r\nConnection: close\r\n",
        response.status,
        response.reason,
        response.body.len()
    );
    for (name, value) in &response.headers {
        out.push_str(name);
        out.push_str(": ");
        out.push_str(value);
        out.push_str("\r\n");
    }
    out.push_str("\r\n");
    timeout(WRITE_TIMEOUT, async {
        stream.write_all(out.as_bytes()).await?;
        stream.write_all(&response.body).await?;
        stream.flush().await
    })
    .await
    .map_err(|_| io::Error::new(io::ErrorKind::TimedOut, "bridge http write timed out"))?
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::{NodeId, WorkspaceId};
    use crate::{NodeServer, NodeServerConfig, WorkspaceConfig};
    use std::path::PathBuf;

    fn test_local_endpoint(label: &str) -> String {
        #[cfg(windows)]
        {
            format!(r"\\.\pipe\gate4agent-bridge-{label}")
        }
        #[cfg(unix)]
        {
            let dir = std::env::temp_dir().join(format!(
                "g4a-bridge-{}-{}",
                label,
                std::process::id()
            ));
            let _ = std::fs::create_dir_all(&dir);
            dir.join("n.sock").to_string_lossy().into_owned()
        }
    }

    fn node_server() -> NodeServer {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let workspace = WorkspaceConfig::new(WorkspaceId::new("test").unwrap(), root).unwrap();
        let config = NodeServerConfig::new(
            test_local_endpoint("unit"),
            "test-token",
            NodeId::new("test-node").unwrap(),
            [workspace],
        )
        .unwrap();
        NodeServer::new(config).unwrap()
    }

    async fn raw_request(address: SocketAddr, request: &str) -> Vec<u8> {
        let mut stream = TcpStream::connect(address).await.unwrap();
        stream.write_all(request.as_bytes()).await.unwrap();
        let mut response = Vec::new();
        // For WS we may leave the connection open briefly; read with a short timeout.
        let _ = timeout(Duration::from_millis(500), stream.read_to_end(&mut response)).await;
        response
    }

    async fn http_text(address: SocketAddr, request: &str) -> String {
        String::from_utf8(raw_request(address, request).await).unwrap()
    }

    #[test]
    fn bridge_listen_is_opt_in_and_refuses_non_loopback() {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let workspace = WorkspaceConfig::new(WorkspaceId::new("test").unwrap(), root).unwrap();
        let config = NodeServerConfig::new(
            test_local_endpoint("config"),
            "test-token",
            NodeId::new("test-node").unwrap(),
            [workspace],
        )
        .unwrap();
        assert_eq!(config.bridge_listen, None);
        let config = config
            .with_bridge_listen("127.0.0.1:0".parse().unwrap())
            .unwrap();
        assert_eq!(config.bridge_listen, Some("127.0.0.1:0".parse().unwrap()));
        assert!(config
            .clone()
            .with_bridge_listen("0.0.0.0:18410".parse().unwrap())
            .is_err());
        assert!(config
            .clone()
            .with_bridge_listen("[::1]:0".parse().unwrap())
            .is_ok());
    }

    #[test]
    fn bridge_underlay_requires_listen_and_token_barriers() {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let workspace = WorkspaceConfig::new(WorkspaceId::new("test").unwrap(), root).unwrap();
        let config = NodeServerConfig::new(
            test_local_endpoint("underlay-cfg"),
            "test-token",
            NodeId::new("test-node").unwrap(),
            [workspace],
        )
        .unwrap();
        // No bridge listen → refuse.
        assert!(matches!(
            config.clone().with_bridge_underlay(
                "127.0.0.1:0".parse().unwrap(),
                [9u8; 32],
                "underlay-token",
            ),
            Err(crate::NodeServerError::BridgeUnderlayRequiresBridgeListen)
        ));
        let config = config
            .with_bridge_listen("127.0.0.1:0".parse().unwrap())
            .unwrap();
        // Listen without application BRIDGE_TOKEN → refuse.
        assert!(matches!(
            config.clone().with_bridge_underlay(
                "127.0.0.1:0".parse().unwrap(),
                [9u8; 32],
                "underlay-token",
            ),
            Err(crate::NodeServerError::BridgeUnderlayRequiresBridgeToken)
        ));
        let config = config.with_bridge_token("bridge-secret").unwrap();
        assert!(matches!(
            config.clone().with_bridge_underlay(
                "127.0.0.1:0".parse().unwrap(),
                [9u8; 32],
                "",
            ),
            Err(crate::NodeServerError::InvalidBridgeUnderlayToken)
        ));
        assert!(config
            .with_bridge_underlay("0.0.0.0:0".parse().unwrap(), [9u8; 32], "underlay-token")
            .is_ok());
    }

    #[test]
    fn sec_websocket_accept_matches_rfc6455_example() {
        // RFC6455 §1.3 / §4.2.2 example key.
        assert_eq!(
            sec_websocket_accept("dGhlIHNhbXBsZSBub25jZQ=="),
            "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
        );
    }

    #[test]
    fn bridge_token_compare_is_constant_time_equality() {
        let auth = BridgeAuth::from_optional(Some("bridge-secret".into())).unwrap();
        assert!(auth.authorized(Some("bridge-secret")));
        assert!(!auth.authorized(Some("wrong-secret!!")));
        assert!(!auth.authorized(None));
        let open = BridgeAuth::from_optional(None).unwrap();
        assert!(open.authorized(None));
        assert!(open.authorized(Some("anything")));
    }

    #[tokio::test]
    async fn bridge_health_returns_200_on_loopback_without_token() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let health = http_text(
            address,
            "GET /bridge/health HTTP/1.1\r\nHost: localhost\r\n\r\n",
        )
        .await;
        assert!(
            health.starts_with("HTTP/1.1 200 OK\r\n"),
            "unexpected health response: {health}"
        );
        assert!(health.contains("\"service\":\"gate4agent-node-bridge\""));
        assert!(health.contains("\"door\":\"node-envelope-bridge\""));
        assert!(health.contains("\"application\":[\"http\",\"websocket\"]"));
        assert!(health.contains("\"transport_udp_underlay\":\"opt-in-via-bridge-underlay-listen\""));
        assert!(health.contains("\"hq_mesh_role\":\"dial-only\""));
        assert!(!health.contains("test-token"));
        assert!(!health.contains("GATE4AGENT"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn bridge_health_reports_tip6_when_underlay_reach_flag_set() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        // underlay_reach=true flips health transport_udp_underlay without
        // starting a real UDP accept (config barriers covered separately).
        let task = tokio::spawn(serve_listener_with_observe_options(
            listener,
            auth,
            Arc::clone(&shared),
            Duration::from_secs(30),
            ObserveMode::SnapshotOnly,
            true,
        ));

        let health = http_text(
            address,
            "GET /bridge/health HTTP/1.1\r\nHost: localhost\r\n\r\n",
        )
        .await;
        assert!(
            health.starts_with("HTTP/1.1 200 OK\r\n"),
            "unexpected health: {health}"
        );
        assert!(health.contains("\"transport_udp_underlay\":\"tip-6-bridge-reach\""));
        assert!(health.contains("\"hq_mesh_role\":\"dial-only\""));
        assert!(!health.contains("opt-in-via-bridge-underlay-listen"));
        assert!(!health.contains("test-token"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn bridge_ws_upgrade_sends_observe_text_frame() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let key = "dGhlIHNhbXBsZSBub25jZQ==";
        let request = format!(
            "GET /bridge/ws HTTP/1.1\r\n\
Host: localhost\r\n\
Upgrade: websocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Key: {key}\r\n\
Sec-WebSocket-Version: 13\r\n\
\r\n"
        );
        let bytes = raw_request(address, &request).await;
        let text = String::from_utf8_lossy(&bytes);
        assert!(
            text.contains("HTTP/1.1 101 Switching Protocols"),
            "missing 101: {text}"
        );
        assert!(text.contains("Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo="));

        // Find start of WS frames (after header blank line).
        let header_end = bytes
            .windows(4)
            .position(|w| w == b"\r\n\r\n")
            .expect("ws response must end headers")
            + 4;
        let frame = &bytes[header_end..];
        assert!(
            !frame.is_empty() && frame[0] & 0x0f == 0x01,
            "expected text opcode, got {:02x?}",
            &frame[..frame.len().min(8)]
        );
        let (payload_start, payload_len) = if frame[1] < 126 {
            (2usize, frame[1] as usize)
        } else if frame[1] == 126 {
            let len = u16::from_be_bytes([frame[2], frame[3]]) as usize;
            (4, len)
        } else {
            panic!("unexpected 64-bit length in test frame");
        };
        let payload = &frame[payload_start..payload_start + payload_len];
        let body = std::str::from_utf8(payload).unwrap();
        assert!(body.contains("\"type\":\"bridge.hello\""));
        assert!(body.contains("\"type\":\"observe.snapshot\""));
        assert!(body.contains("\"session_projections\":[]"));
        assert!(body.contains("slim session projection"));
        assert!(body.contains("observe.delta") || body.contains("change-driven"));
        assert!(!body.contains("test-token"));
        assert!(!body.contains("terminal_frame"));
        assert!(!body.contains("GATE4AGENT"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn bridge_token_gate_rejects_missing_secret_when_configured() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(Some("bridge-secret".into())).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let denied = http_text(
            address,
            "GET /bridge/health HTTP/1.1\r\nHost: localhost\r\n\r\n",
        )
        .await;
        assert!(denied.starts_with("HTTP/1.1 401 Unauthorized\r\n"));
        assert!(!denied.contains("bridge-secret"));

        let ok = http_text(
            address,
            "GET /bridge/health HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer bridge-secret\r\n\r\n",
        )
        .await;
        assert!(ok.starts_with("HTTP/1.1 200 OK\r\n"));
        assert!(!ok.contains("bridge-secret"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[test]
    fn slim_screen_strips_operator_gate_options_and_basenames_process() {
        use gate4agent_types::{
            OperatorGateInput, OperatorGateKind, OperatorGateOption,
            OperatorGateOptionSemantics, OperatorGateState,
        };

        let mut gate = OperatorGateState::new(OperatorGateKind::Authentication);
        // Force an on-screen option label into the gate so we can assert the
        // slim projector strips it (options stay station/operator-local).
        gate.input = OperatorGateInput::NumberedList;
        gate.options = vec![OperatorGateOption {
            text: "sign in with cookie-jar secret".into(),
            semantics: OperatorGateOptionSemantics::Accept,
            selected: false,
        }];
        let projected = slim_screen_state(&PtyScreenState::OperatorGate { gate });
        let text = projected.to_string();
        assert!(text.contains("\"kind\":\"operator-gate\""));
        assert!(text.contains("authentication"));
        assert!(!text.contains("cookie-jar"));
        assert!(!text.contains("sign in"));

        let not_agent = slim_screen_state(&PtyScreenState::NotAgent {
            observed_process: "/home/fixture/bin/apt-get".into(),
        });
        assert_eq!(
            not_agent,
            json!({
                "kind": "not-agent",
                "observed_process": "apt-get",
            })
        );
    }

    #[test]
    fn slim_status_truncates_failed_message() {
        let long = "x".repeat(SLIM_FAILED_MESSAGE_MAX + 40);
        let projected = slim_session_status(&SessionStatus::Failed {
            message: long.clone(),
        });
        let message = projected["message"].as_str().unwrap();
        assert!(message.chars().count() <= SLIM_FAILED_MESSAGE_MAX);
        assert!(message.ends_with('…'));
        assert!(!projected.to_string().contains(&long));
    }

    #[tokio::test]
    async fn bridge_http_snapshot_exposes_slim_projection_shape() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let body = http_text(
            address,
            "GET /bridge/snapshot HTTP/1.1\r\nHost: localhost\r\n\r\n",
        )
        .await;
        assert!(
            body.starts_with("HTTP/1.1 200 OK\r\n"),
            "unexpected snapshot response: {body}"
        );
        assert!(body.contains("\"type\":\"bridge.hello\""));
        assert!(body.contains("\"session_projections\":[]"));
        assert!(body.contains("slim session projection"));
        assert!(body.contains("POST /bridge/drive"));
        assert!(body.contains("observe.delta") || body.contains("change-driven"));
        assert!(!body.contains("test-token"));
        assert!(!body.contains("terminal_frame"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    fn drive_post(body: &str) -> String {
        format!(
            "POST /bridge/drive HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
            body.len(),
            body
        )
    }

    #[test]
    fn bridge_drive_request_parses_prompt_and_paste_only() {
        let prompt: BridgeDriveRequest = serde_json::from_str(
            r#"{"kind":"prompt","workspace_id":"ws","instance_id":1,"generation":2,"text":"hi"}"#,
        )
        .unwrap();
        assert_eq!(prompt.kind_label(), "prompt");
        let paste: BridgeDriveRequest = serde_json::from_str(
            r#"{"kind":"paste","workspace_id":"ws","instance_id":1,"generation":2,"text":"hi"}"#,
        )
        .unwrap();
        assert_eq!(paste.kind_label(), "paste");
        // Refuse raw terminal / unknown kinds — not a product drive surface.
        assert!(serde_json::from_str::<BridgeDriveRequest>(
            r#"{"kind":"terminal-bytes","workspace_id":"ws","instance_id":1,"generation":2,"text":"x"}"#
        )
        .is_err());
        assert!(serde_json::from_str::<BridgeDriveRequest>(
            r#"{"kind":"input","workspace_id":"ws","instance_id":1,"generation":2,"text":"x"}"#
        )
        .is_err());
    }

    #[tokio::test]
    async fn bridge_drive_get_is_method_not_allowed() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let body = http_text(
            address,
            "GET /bridge/drive HTTP/1.1\r\nHost: localhost\r\n\r\n",
        )
        .await;
        assert!(
            body.starts_with("HTTP/1.1 405 Method Not Allowed\r\n"),
            "unexpected: {body}"
        );
        assert!(body.contains("Allow: POST"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn bridge_drive_rejects_malformed_and_unknown_session() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let bad = http_text(address, &drive_post(r#"{"not":"a-drive"}"#)).await;
        assert!(
            bad.starts_with("HTTP/1.1 400 Bad Request\r\n"),
            "unexpected malformed: {bad}"
        );
        assert!(bad.contains("\"type\":\"drive.error\""));
        assert!(!bad.contains("test-token"));

        let missing = http_text(
            address,
            &drive_post(
                r#"{"kind":"prompt","workspace_id":"test","instance_id":99,"generation":1,"text":"hello"}"#,
            ),
        )
        .await;
        assert!(
            missing.starts_with("HTTP/1.1 404 Not Found\r\n")
                || missing.starts_with("HTTP/1.1 400 Bad Request\r\n"),
            "unexpected unknown-session: {missing}"
        );
        assert!(missing.contains("\"type\":\"drive.error\""));
        assert!(
            missing.contains("unknown-session") || missing.contains("unknown-workspace"),
            "expected session/workspace failure code: {missing}"
        );
        assert!(!missing.contains("GATE4AGENT"));

        let empty = http_text(
            address,
            &drive_post(
                r#"{"kind":"paste","workspace_id":"test","instance_id":1,"generation":1,"text":""}"#,
            ),
        )
        .await;
        assert!(
            empty.starts_with("HTTP/1.1 400 Bad Request\r\n"),
            "unexpected empty text: {empty}"
        );

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn bridge_drive_token_gate_rejects_missing_secret() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(Some("bridge-secret".into())).unwrap();
        let task = tokio::spawn(serve_listener(listener, auth, Arc::clone(&shared)));

        let denied = http_text(
            address,
            &drive_post(
                r#"{"kind":"prompt","workspace_id":"test","instance_id":1,"generation":1,"text":"x"}"#,
            ),
        )
        .await;
        assert!(
            denied.starts_with("HTTP/1.1 401 Unauthorized\r\n"),
            "unexpected: {denied}"
        );
        assert!(!denied.contains("bridge-secret"));

        let drive_body =
            r#"{"kind":"prompt","workspace_id":"test","instance_id":1,"generation":1,"text":"x"}"#;
        let authorized = format!(
            "POST /bridge/drive HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer bridge-secret\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
            drive_body.len(),
            drive_body
        );
        let okish = http_text(address, &authorized).await;
        // Auth passed; session still missing → drive.error, never leaks token.
        assert!(!okish.starts_with("HTTP/1.1 401 "), "auth should pass: {okish}");
        assert!(okish.contains("\"type\":\"drive.error\""));
        assert!(!okish.contains("bridge-secret"));

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(1), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[test]
    fn drive_http_status_maps_node_failure_codes() {
        assert_eq!(drive_http_status(NodeFailureCode::UnknownSession), 404);
        assert_eq!(drive_http_status(NodeFailureCode::StaleGeneration), 409);
        assert_eq!(drive_http_status(NodeFailureCode::UnsupportedCapability), 422);
        assert_eq!(drive_http_status(NodeFailureCode::BackendBusy), 503);
        assert_eq!(drive_http_status(NodeFailureCode::InvalidRequest), 400);
    }

    fn decode_server_frames(mut bytes: &[u8]) -> Vec<(u8, Vec<u8>)> {
        let mut frames = Vec::new();
        while bytes.len() >= 2 {
            let opcode = bytes[0] & 0x0f;
            let mut len = (bytes[1] & 0x7f) as usize;
            let mut offset = 2usize;
            if bytes[1] & 0x80 != 0 {
                break;
            }
            if len == 126 {
                if bytes.len() < 4 {
                    break;
                }
                len = u16::from_be_bytes([bytes[2], bytes[3]]) as usize;
                offset = 4;
            } else if len == 127 {
                if bytes.len() < 10 {
                    break;
                }
                len = u64::from_be_bytes(bytes[2..10].try_into().unwrap()) as usize;
                offset = 10;
            }
            if bytes.len() < offset + len {
                break;
            }
            frames.push((opcode, bytes[offset..offset + len].to_vec()));
            bytes = &bytes[offset + len..];
        }
        frames
    }

    fn encode_client_frame(opcode: u8, payload: &[u8], mask: [u8; 4]) -> Vec<u8> {
        let mut frame = Vec::with_capacity(2 + 4 + payload.len());
        frame.push(0x80 | (opcode & 0x0f));
        assert!(payload.len() < 126, "test helper only supports small payloads");
        frame.push(0x80 | (payload.len() as u8));
        frame.extend_from_slice(&mask);
        for (index, byte) in payload.iter().enumerate() {
            frame.push(byte ^ mask[index % 4]);
        }
        frame
    }

    async fn read_ws_after_upgrade(stream: &mut TcpStream, min_text_frames: usize) -> Vec<Vec<u8>> {
        let mut buf = Vec::new();
        let deadline = tokio::time::Instant::now() + Duration::from_secs(3);
        loop {
            if tokio::time::Instant::now() > deadline {
                break;
            }
            let mut chunk = [0u8; 8192];
            match timeout(Duration::from_millis(100), stream.read(&mut chunk)).await {
                Ok(Ok(0)) => break,
                Ok(Ok(n)) => buf.extend_from_slice(&chunk[..n]),
                Ok(Err(_)) => break,
                Err(_) => {}
            }
            let Some(header_end) = buf.windows(4).position(|w| w == b"\r\n\r\n") else {
                continue;
            };
            let header_end = header_end + 4;
            let text_frames: Vec<Vec<u8>> = decode_server_frames(&buf[header_end..])
                .into_iter()
                .filter(|(op, _)| *op == 0x01)
                .map(|(_, payload)| payload)
                .collect();
            if text_frames.len() >= min_text_frames {
                return text_frames;
            }
        }
        let header_end = buf
            .windows(4)
            .position(|w| w == b"\r\n\r\n")
            .map(|i| i + 4)
            .unwrap_or(0);
        decode_server_frames(&buf[header_end..])
            .into_iter()
            .filter(|(op, _)| *op == 0x01)
            .map(|(_, payload)| payload)
            .collect()
    }

    #[tokio::test]
    async fn bridge_ws_streams_periodic_observe_snapshots() {
        // snapshot-only escape hatch ≡ streaming tip: every tick full snapshot.
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener_with_observe_options(
            listener,
            auth,
            Arc::clone(&shared),
            Duration::from_millis(80),
            ObserveMode::SnapshotOnly,
            false,
        ));

        let mut stream = TcpStream::connect(address).await.unwrap();
        let key = "dGhlIHNhbXBsZSBub25jZQ==";
        let request = format!(
            "GET /bridge/ws HTTP/1.1\r\n\
Host: localhost\r\n\
Upgrade: websocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Key: {key}\r\n\
Sec-WebSocket-Version: 13\r\n\
\r\n"
        );
        stream.write_all(request.as_bytes()).await.unwrap();

        let texts = read_ws_after_upgrade(&mut stream, 3).await;
        assert!(
            texts.len() >= 3,
            "expected hello + >=2 ticks, got {}",
            texts.len()
        );
        let hello = std::str::from_utf8(&texts[0]).unwrap();
        assert!(hello.contains("\"type\":\"bridge.hello\""));
        assert!(hello.contains("\"seq\":0"));
        assert!(hello.contains("observe.delta") || hello.contains("change-driven"));
        assert!(!hello.contains("test-token"));
        assert!(!hello.contains("GATE4AGENT"));

        let tick = std::str::from_utf8(&texts[1]).unwrap();
        assert!(
            tick.contains("\"type\":\"observe.snapshot\""),
            "tick1: {tick}"
        );
        assert!(
            tick.contains("\"seq\":1") || tick.contains("\"seq\":2"),
            "tick1 seq: {tick}"
        );
        assert!(!tick.contains("bridge.hello"));
        assert!(!tick.contains("terminal_frame"));
        assert!(!tick.contains("test-token"));

        let tick2 = std::str::from_utf8(&texts[2]).unwrap();
        assert!(tick2.contains("\"type\":\"observe.snapshot\""));
        assert!(tick2.contains("\"session_projections\":[]"));

        let close = encode_client_frame(0x08, &[], [1, 2, 3, 4]);
        let _ = stream.write_all(&close).await;

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(2), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    #[tokio::test]
    async fn bridge_ws_ping_gets_pong() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener_with_observe_period(
            listener,
            auth,
            Arc::clone(&shared),
            Duration::from_secs(30),
        ));

        let mut stream = TcpStream::connect(address).await.unwrap();
        let key = "dGhlIHNhbXBsZSBub25jZQ==";
        let request = format!(
            "GET /bridge/ws HTTP/1.1\r\n\
Host: localhost\r\n\
Upgrade: websocket\r\n\
Connection: Upgrade\r\n\
Sec-WebSocket-Key: {key}\r\n\
Sec-WebSocket-Version: 13\r\n\
\r\n"
        );
        stream.write_all(request.as_bytes()).await.unwrap();

        let texts = read_ws_after_upgrade(&mut stream, 1).await;
        assert!(!texts.is_empty(), "expected hello frame");

        // Drain any bytes already buffered after hello by sending ping on a
        // fresh read window; leftover ticks (unlikely with 30s period) are ok.
        let ping_payload = b"ko";
        let ping = encode_client_frame(0x09, ping_payload, [9, 8, 7, 6]);
        stream.write_all(&ping).await.unwrap();

        let mut buf = Vec::new();
        let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
        let mut saw_pong = false;
        while tokio::time::Instant::now() < deadline && !saw_pong {
            let mut chunk = [0u8; 2048];
            match timeout(Duration::from_millis(100), stream.read(&mut chunk)).await {
                Ok(Ok(0)) => break,
                Ok(Ok(n)) => buf.extend_from_slice(&chunk[..n]),
                Ok(Err(_)) => break,
                Err(_) => continue,
            }
            for (op, payload) in decode_server_frames(&buf) {
                if op == 0x0A {
                    assert_eq!(payload, ping_payload);
                    saw_pong = true;
                    break;
                }
            }
        }
        assert!(saw_pong, "expected pong echoing ping payload; buf={buf:02x?}");

        let close = encode_client_frame(0x08, &[], [1, 2, 3, 4]);
        let _ = stream.write_all(&close).await;

        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(2), task)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    fn sample_row(instance_id: u64, generation: u64, status_kind: &str) -> Value {
        json!({
            "workspace_id": "ws",
            "instance_id": instance_id,
            "generation": generation,
            "agent_id": "agent",
            "transport": "native-pty",
            "status": { "kind": status_kind },
            "screen": { "kind": "ready" },
            "pending_input": null,
            "terminal_size": { "rows": 24, "columns": 80 },
            "browser_profile_id": null,
            "network_allowlist": null,
        })
    }

    #[test]
    fn diff_projections_upserts_removes_and_ignores_unchanged() {
        let last = projections_to_map(&[
            sample_row(1, 1, "running"),
            sample_row(2, 1, "running"),
        ]);
        let now = projections_to_map(&[
            sample_row(1, 1, "running"),
            sample_row(2, 1, "exited"),
            sample_row(3, 1, "starting"),
        ]);
        let (upserts, removes) = diff_projections(&last, &now);
        assert_eq!(upserts.len(), 2);
        let ids: Vec<u64> = upserts
            .iter()
            .map(|row| row["instance_id"].as_u64().unwrap())
            .collect();
        assert!(ids.contains(&2) && ids.contains(&3));
        assert_eq!(removes.len(), 0);

        let now_removed = projections_to_map(&[sample_row(2, 1, "exited")]);
        let (_u2, removes2) = diff_projections(&last, &now_removed);
        assert_eq!(removes2.len(), 1);
        assert_eq!(removes2[0]["instance_id"], 1);
        assert_eq!(removes2[0]["generation"], 1);

        let bumped = projections_to_map(&[sample_row(1, 2, "running")]);
        let only1 = projections_to_map(&[sample_row(1, 1, "running")]);
        let (up_b, rm_b) = diff_projections(&only1, &bumped);
        assert_eq!(rm_b.len(), 1);
        assert_eq!(rm_b[0]["generation"], 1);
        assert_eq!(up_b.len(), 1);
        assert_eq!(up_b[0]["generation"], 2);
    }

    #[test]
    fn decide_observe_tick_suppresses_unchanged_and_forces_floor() {
        let map = projections_to_map(&[sample_row(1, 1, "running")]);
        let gauges = SessionGauges { control_plane: 1, native_pty: 0 };
        assert_eq!(
            decide_observe_tick(ObserveMode::Delta, &map, gauges, &map, gauges, 0),
            TickDecision::Suppress
        );
        let gauges2 = SessionGauges { control_plane: 1, native_pty: 1 };
        match decide_observe_tick(ObserveMode::Delta, &map, gauges, &map, gauges2, 0) {
            TickDecision::Delta { upserts, removes } => {
                assert!(upserts.is_empty() && removes.is_empty());
            }
            other => panic!("expected gauge-only delta, got {other:?}"),
        }
        assert_eq!(
            decide_observe_tick(
                ObserveMode::Delta, &map, gauges, &map, gauges2,
                SNAPSHOT_FLOOR_TICKS.saturating_sub(1),
            ),
            TickDecision::Snapshot
        );
        assert_eq!(
            decide_observe_tick(ObserveMode::SnapshotOnly, &map, gauges, &map, gauges, 0),
            TickDecision::Snapshot
        );
    }

    #[test]
    fn decide_observe_tick_forces_full_on_row_cap() {
        let mut last = ProjectionMap::new();
        let mut now = ProjectionMap::new();
        for i in 0..(DELTA_ROW_CAP as u64 + 2) {
            last.insert((i, 1), sample_row(i, 1, "running"));
            now.insert((i, 1), sample_row(i, 1, "exited"));
        }
        let gauges = SessionGauges { control_plane: now.len(), native_pty: 0 };
        assert_eq!(
            decide_observe_tick(ObserveMode::Delta, &last, gauges, &now, gauges, 0),
            TickDecision::Snapshot
        );
    }

    #[test]
    fn observe_delta_frame_schema_has_basis_seq_and_no_secrets() {
        let server = node_server();
        let shared = &*server.shared;
        let frame = observe_delta_frame(
            shared, 3, 2, 1, 0,
            &[sample_row(9, 1, "running")],
            &[json!({"instance_id": 8u64, "generation": 1u64})],
        );
        let text = frame.to_string();
        assert_eq!(frame["type"], "observe.delta");
        assert_eq!(frame["seq"], 3);
        assert_eq!(frame["basis_seq"], 2);
        assert!(frame["upserts"].is_array() && frame["removes"].is_array());
        assert!(!text.contains("terminal_frame"));
        assert!(!text.contains("test-token"));
        assert!(!text.contains("GATE4AGENT"));
        assert!(!text.contains("cookie"));
    }

    async fn read_more_ws_text_frames(
        stream: &mut TcpStream,
        min_text_frames: usize,
        wait: Duration,
    ) -> Vec<Vec<u8>> {
        let mut buf = Vec::new();
        let deadline = tokio::time::Instant::now() + wait;
        loop {
            if tokio::time::Instant::now() > deadline {
                break;
            }
            let mut chunk = [0u8; 8192];
            match timeout(Duration::from_millis(80), stream.read(&mut chunk)).await {
                Ok(Ok(0)) => break,
                Ok(Ok(n)) => buf.extend_from_slice(&chunk[..n]),
                Ok(Err(_)) => break,
                Err(_) => {}
            }
            let text_frames: Vec<Vec<u8>> = decode_server_frames(&buf)
                .into_iter()
                .filter(|(op, _)| *op == 0x01)
                .map(|(_, payload)| payload)
                .collect();
            if text_frames.len() >= min_text_frames {
                return text_frames;
            }
        }
        decode_server_frames(&buf)
            .into_iter()
            .filter(|(op, _)| *op == 0x01)
            .map(|(_, payload)| payload)
            .collect()
    }

    #[tokio::test]
    async fn bridge_ws_delta_suppresses_unchanged_ticks() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener_with_observe_options(
            listener, auth, Arc::clone(&shared),
            Duration::from_millis(50), ObserveMode::Delta,
            false,
        ));

        let mut stream = TcpStream::connect(address).await.unwrap();
        let key = "dGhlIHNhbXBsZSBub25jZQ==";
        let request = format!(
            "GET /bridge/ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n"
        );
        stream.write_all(request.as_bytes()).await.unwrap();

        let texts = read_ws_after_upgrade(&mut stream, 1).await;
        assert!(!texts.is_empty());
        let more = read_more_ws_text_frames(&mut stream, 1, Duration::from_millis(280)).await;
        assert!(more.is_empty(), "expected suppress; got {} extra frames", more.len());
        let hello = std::str::from_utf8(&texts[0]).unwrap();
        assert!(hello.contains("\"type\":\"bridge.hello\"") && hello.contains("\"seq\":0"));

        let _ = stream.write_all(&encode_client_frame(0x08, &[], [1, 2, 3, 4])).await;
        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(2), task).await.unwrap().unwrap().unwrap();
    }

    #[tokio::test]
    async fn bridge_ws_delta_emits_on_gauge_change() {
        let server = node_server();
        let shared = Arc::clone(&server.shared);
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let auth = BridgeAuth::from_optional(None).unwrap();
        let task = tokio::spawn(serve_listener_with_observe_options(
            listener, auth, Arc::clone(&shared),
            Duration::from_millis(60), ObserveMode::Delta,
            false,
        ));

        let mut stream = TcpStream::connect(address).await.unwrap();
        let key = "dGhlIHNhbXBsZSBub25jZQ==";
        let request = format!(
            "GET /bridge/ws HTTP/1.1\r\nHost: localhost\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: {key}\r\nSec-WebSocket-Version: 13\r\n\r\n"
        );
        stream.write_all(request.as_bytes()).await.unwrap();
        assert!(!read_ws_after_upgrade(&mut stream, 1).await.is_empty());

        shared.native_session_gauge.store(7, Ordering::Relaxed);

        let texts = read_more_ws_text_frames(&mut stream, 1, Duration::from_secs(2)).await;
        let delta = texts
            .iter()
            .map(|p| std::str::from_utf8(p).unwrap())
            .find(|b| b.contains("\"type\":\"observe.delta\""))
            .expect("expected observe.delta after gauge change");
        assert!(delta.contains("\"basis_seq\":0"), "delta: {delta}");
        assert!(delta.contains("\"seq\":1"), "delta: {delta}");
        assert!(delta.contains("\"native_pty\":7"), "delta: {delta}");
        assert!(delta.contains("\"upserts\":[]") && delta.contains("\"removes\":[]"));
        assert!(!delta.contains("terminal_frame") && !delta.contains("test-token"));
        assert!(!delta.contains("GATE4AGENT"));

        let _ = stream.write_all(&encode_client_frame(0x08, &[], [1, 2, 3, 4])).await;
        server.shutdown_handle().request_shutdown().await.unwrap();
        timeout(Duration::from_secs(2), task).await.unwrap().unwrap().unwrap();
    }
}