lean-ctx-cloud 3.12.9

The LeanCTX Cloud connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
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
// SPDX-License-Identifier: Apache-2.0
//! One connected bus session (E14 `GET /v1/bus`, first frame `hello` with
//! `protocol: "2.0"`). Decisions live in `relay_v2`; this loop registers
//! the workers it announces (E10), moves frames, sends queued lease
//! releases (C5) and keeps the state file current.

use std::collections::{BTreeMap, BTreeSet};

use futures::{SinkExt as _, StreamExt as _};
use serde_json::{Value, json};
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::tungstenite::client::IntoClientRequest as _;

use super::super::plan_events::{self, PlanOutcome};
use super::super::relay_v2::{self, StateV2};
use super::super::task_events::{self, Origin, TaskEvent};
use super::super::v2::{
    Inbound, PresenceEntry, PresenceIdentity, machine_name, parse_frame, working_on,
};
use super::{SessionEnd, TICK, WAKE_POLL, bus_held, handshake_error, header, open_socket, ws_url};
use crate::CloudError;
use crate::auth::Session;
use crate::config::CloudConfig;
use crate::knowledge::screening::Screen;
use crate::state::CloudStore;
use crate::transport::{PROTOCOL_V2, user_agent};
use crate::vault::VaultMap;
use crate::workers::attach::{self, LocalWorker};
use crate::workers::card::{Availability, Budget, WorkerCard};
use crate::workers::register::ensure_registered;
use lean_ctx::core::agents::{AgentEntry, AgentRegistry, DirectiveStore};
use lean_ctx_protocol::cloud::lease::CloudRight;
use lean_ctx_protocol::cloud::{HEADER_INSTANCE, HEADER_PROTOCOL};

/// The worker a local agent attaches to (host app from its transport or
/// agent type, role from `ctx_agent register`).
fn attach_agent(
    store: &CloudStore,
    instance_id: &str,
    agent: &AgentEntry,
) -> Result<LocalWorker, CloudError> {
    let (host, role) = attach::agent_worker_key(agent);
    Ok(attach::attach(store, instance_id, &host, &role)?.worker)
}

/// The worker of `agent` in the worker book as it is now: by the same
/// `(host_app, role)` key it attaches with ([`attach::agent_worker_key`]),
/// so a session that registered after the relay connected maps to its
/// worker too (#28).
fn book_worker(book: &[LocalWorker], agent: &AgentEntry) -> Option<String> {
    let (host, role) = attach::agent_worker_key(agent);
    book.iter()
        .find(|w| w.host_app == host && w.role == role)
        .map(|w| w.worker_id.clone())
}

/// Register (E10, once) the workers of the active local agents in the
/// subscribed vaults; agent id → registered worker. A failed registration
/// leaves that agent out until the next pass; its error is kept under the
/// agent id for the relay's report.
fn workers_of(
    session: &Session,
    agents: &[AgentEntry],
) -> (BTreeMap<String, LocalWorker>, BTreeMap<String, String>) {
    let instance_id = session.instance.instance_id.clone();
    let mut out = BTreeMap::new();
    let mut failed = BTreeMap::new();
    for agent in agents {
        let result = attach_agent(&session.store, &instance_id, agent).and_then(|worker| {
            let card = WorkerCard::new(
                &worker.host_app,
                None,
                Vec::new(),
                Budget::default(),
                Availability::Online,
            );
            ensure_registered(session, &worker, &card)
        });
        match result {
            Ok(worker) => {
                out.insert(agent.agent_id.clone(), worker);
            }
            Err(error) => {
                session
                    .store
                    .record_error(&format!("bus worker registration: {error}"));
                failed.insert(agent.agent_id.clone(), error.to_string());
            }
        }
    }
    (out, failed)
}

/// What a hello Cloud acknowledged announced, in the words `cloud bus relay`
/// prints and `bus/announced.txt` keeps (#252): each worker with its role
/// and state on which buses, and for every agent that is not on a bus,
/// why. `linked` tells whether an agent's checkout belongs to a vault this
/// machine resolved; `denied` are the addresses the hello.ack refused.
fn announce_report(
    subscribe: &[String],
    announced: &[PresenceEntry],
    agents: &[AgentEntry],
    workers: &BTreeMap<String, LocalWorker>,
    failed: &BTreeMap<String, String>,
    linked: impl Fn(&str) -> bool,
    denied: &[String],
) -> Vec<String> {
    if agents.is_empty() {
        return vec![
            "bus relay: connected to LeanCTX Cloud, but no agent is active on this machine's local bus: nobody was announced (an agent is announced once it registers with ctx_agent)".into(),
        ];
    }
    let mut lines = vec![if subscribe.is_empty() {
        "bus relay: connected to LeanCTX Cloud, but no active agent works in a checkout linked to a LeanCTX Cloud vault: LeanCTX Cloud shows nobody on a bus (run `lean-ctx cloud connect` in the checkout)".to_string()
    } else {
        format!(
            "bus relay: connected to LeanCTX Cloud; announced on {}:",
            subscribe.join(", ")
        )
    }];
    for agent in agents {
        let role = agent.role.as_deref().unwrap_or_default();
        let shown_role = if role.is_empty() { "no role" } else { role };
        let line = match workers.get(&agent.agent_id) {
            Some(worker) => {
                let state = announced
                    .iter()
                    .find(|e| e.worker_id == worker.worker_id)
                    .and_then(|e| serde_json::to_value(e.state).ok())
                    .and_then(|v| v.as_str().map(str::to_owned))
                    .unwrap_or_default();
                let mut line = format!(
                    "  {} · {shown_role} · {state}  (agent {} in {})",
                    worker.worker_id, agent.agent_id, agent.project_root
                );
                if !linked(&agent.project_root) {
                    line.push_str(
                        " — not on a bus: its checkout is not linked to a LeanCTX Cloud vault",
                    );
                } else if role.is_empty() {
                    line.push_str(" — no role: register with role=… to claim tasks that need one");
                }
                line
            }
            // Unlinked: no worker by design (SPEC-ENGINE §3.7.3).
            None if !linked(&agent.project_root) => format!(
                "  agent {} ({shown_role}) in {} — not on a bus: its checkout is not linked to a LeanCTX Cloud vault",
                agent.agent_id, agent.project_root
            ),
            None => format!(
                "  agent {} ({shown_role}) in {}: not announced, its worker could not be registered: {}",
                agent.agent_id,
                agent.project_root,
                failed
                    .get(&agent.agent_id)
                    .map_or("unknown error", String::as_str)
            ),
        };
        lines.push(line);
    }
    for address in denied {
        lines.push(format!(
            "  {address}: refused by LeanCTX Cloud (see `lean-ctx cloud status`)"
        ));
    }
    lines
}

/// True when every agent in `fresh` was announced with the same role (an
/// agent that left needs no new hello; its worker goes idle).
fn attached_alike(fresh: &[AgentEntry], announced: &[AgentEntry]) -> bool {
    let key = |a: &AgentEntry| (a.agent_id.clone(), a.role.clone());
    let announced: BTreeSet<_> = announced.iter().map(key).collect();
    fresh.iter().all(|a| announced.contains(&key(a)))
}

/// Presence entries, one per worker (active if any of its sessions is),
/// each with the worker's name and this Instance (QN0).
fn presence(
    state: &StateV2,
    agents: &[AgentEntry],
    workers: &BTreeMap<String, LocalWorker>,
    instance: &crate::state::InstanceRecord,
) -> Vec<PresenceEntry> {
    let mut by_worker: BTreeMap<String, (String, bool, Option<String>)> = BTreeMap::new();
    for agent in agents {
        if let Some(worker) = workers.get(&agent.agent_id) {
            let active = agent.status == lean_ctx::core::agents::AgentStatus::Active;
            let slot = by_worker
                .entry(worker.worker_id.clone())
                .or_insert_with(|| (worker.role.clone(), false, None));
            slot.1 |= active;
            if slot.2.is_none() {
                slot.2 = agent
                    .display_name
                    .as_deref()
                    .and_then(presence_line)
                    .map(str::to_owned);
            }
        }
    }
    by_worker
        .into_iter()
        .map(|(worker_id, (role, active, display_name))| PresenceEntry {
            state: relay_v2::presence_state(state, &worker_id, active),
            worker_id,
            role,
            display_name,
            instance_id: Some(instance.instance_id.clone()),
            // h7b: the label's short name, never a domain (a raw
            // `gethostname()` on Linux is often the full name).
            instance_name: machine_name(&instance.label),
        })
        .collect()
}

/// `value` as a presence name: a trimmed single line of at most 128
/// characters, else none.
fn presence_line(value: &str) -> Option<&str> {
    let value = value.trim();
    (!value.is_empty() && value.chars().count() <= 128 && !value.chars().any(char::is_control))
        .then_some(value)
}

/// What each worker's team sees besides its role and state (h7b): this
/// machine's short name and, redacted, the status line the agent of the
/// worker's first session that has one wrote itself (LeanCTX's lifecycle
/// notes such as "connection closed" are never one, see
/// [`AgentEntry::agent_status_line`]). Empty when the names are off
/// (`[cloud] presence_names = false`) or Cloud did not acknowledge
/// `presence_identity`, so nothing of it leaves the machine then.
fn identities(
    on: bool,
    agents: &[AgentEntry],
    workers: &BTreeMap<String, LocalWorker>,
    instance: &crate::state::InstanceRecord,
) -> BTreeMap<String, PresenceIdentity> {
    let mut out: BTreeMap<String, PresenceIdentity> = BTreeMap::new();
    if !on {
        return out;
    }
    let machine = machine_name(&instance.label);
    for agent in agents {
        let Some(worker) = workers.get(&agent.agent_id) else {
            continue;
        };
        let slot = out
            .entry(worker.worker_id.clone())
            .or_insert_with(|| PresenceIdentity {
                machine_name: machine.clone(),
                working_on: None,
            });
        if slot.working_on.is_none() {
            slot.working_on = agent.agent_status_line().and_then(working_on);
        }
    }
    out
}

/// One `presence.update` frame for `entry` on `address`, with the team's
/// names when there are any.
fn presence_update(
    address: &str,
    entry: &PresenceEntry,
    identity: Option<&PresenceIdentity>,
    at: u64,
) -> Value {
    let mut update = json!({"t": "presence.update", "address": address, "at": at});
    if let Some(frame) = update.as_object_mut() {
        if let Ok(Value::Object(fields)) = serde_json::to_value(entry) {
            frame.extend(fields);
        }
        if let Some(Ok(Value::Object(fields))) = identity.map(serde_json::to_value) {
            frame.extend(fields);
        }
    }
    update
}

/// The agents whose project is a root of a linked vault (SPEC-ENGINE
/// §3.7.3: presence only where a session is bound). An agent in a project
/// that is not shared is neither registered as a worker nor announced.
fn in_linked_vaults<'a>(map: &VaultMap, agents: Vec<&'a AgentEntry>) -> Vec<&'a AgentEntry> {
    agents
        .into_iter()
        .filter(|agent| relay_v2::record_for(map, &agent.project_root).is_some())
        .collect()
}

/// The vaults each worker works in (worker id → addresses): those of its
/// agents' checkouts. The relay subscribes every linked vault (rc4-82), but
/// a worker is present only where one of its agents works (rc4-86).
fn worker_places(
    map: &VaultMap,
    agents: &[AgentEntry],
    workers: &BTreeMap<String, LocalWorker>,
) -> BTreeMap<String, BTreeSet<String>> {
    let mut out: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    for agent in agents {
        if let (Some(worker), Some(record)) = (
            workers.get(&agent.agent_id),
            relay_v2::record_for(map, &agent.project_root),
        ) {
            out.entry(worker.worker_id.clone())
                .or_default()
                .insert(relay_v2::address_of(record));
        }
    }
    out
}

/// The subscribed vaults `worker_id` works in, in subscription order.
fn places_in<'a>(
    places: &BTreeMap<String, BTreeSet<String>>,
    worker_id: &str,
    subscribe: &'a [String],
) -> Vec<&'a String> {
    let Some(own) = places.get(worker_id) else {
        return Vec::new();
    };
    subscribe.iter().filter(|a| own.contains(*a)).collect()
}

/// The entries a hello may carry: Cloud upserts each one into every vault
/// the hello subscribes, so only workers that work in all of them. The
/// others follow as a `presence.update` per vault of theirs.
fn hello_presence(
    announced: &[PresenceEntry],
    places: &BTreeMap<String, BTreeSet<String>>,
    subscribe: &[String],
) -> Vec<PresenceEntry> {
    announced
        .iter()
        .filter(|entry| places_in(places, &entry.worker_id, subscribe).len() == subscribe.len())
        .cloned()
        .collect()
}

/// What the relay sees of this machine's local bus.
struct LocalView {
    /// Active agents in linked vaults: the only ones that get a worker,
    /// presence or any other outgoing payload.
    agents: Vec<AgentEntry>,
    /// Active agents in checkouts no vault links. Local only: they appear
    /// in the relay's own report (`cloud bus relay`, `bus/announced.txt`,
    /// #252) with the reason they are on no bus, never in a frame.
    unlinked: Vec<AgentEntry>,
    subscribe: Vec<String>,
}

/// Active local agents, split by whether a linked vault holds their
/// checkout, and the subscriptions the linked ones make.
fn local_view(map: &VaultMap) -> LocalView {
    let registry = AgentRegistry::load_or_create();
    view_of(map, registry.list_active(None))
}

fn view_of(map: &VaultMap, all: Vec<&AgentEntry>) -> LocalView {
    // rc4-86: an MCP server's own `context-engine` presence is no agent;
    // it never becomes a worker, linked or not.
    let all: Vec<&AgentEntry> = all
        .into_iter()
        .filter(|agent| !agent.is_engine_placeholder())
        .collect();
    let active = in_linked_vaults(map, all.clone());
    let unlinked = all
        .into_iter()
        .filter(|agent| relay_v2::record_for(map, &agent.project_root).is_none())
        .cloned()
        .collect();
    let subscribe = relay_v2::subscriptions(map);
    let agents = active.into_iter().cloned().collect();
    LocalView {
        agents,
        unlinked,
        subscribe,
    }
}

/// Code of the frames that say this instance was revoked (Cloud'
/// heartbeat sends `place.denied` per place, then closes the socket).
const INSTANCE_REVOKED: &str = "instance_revoked";

/// Whether `frame` says this instance was revoked: a `place.denied`, a
/// denied hello address or an `error` with code `instance_revoked`.
fn revocation_in(frame: &Inbound) -> bool {
    match frame {
        Inbound::PlaceDenied { code, .. } | Inbound::Error { code, .. } => code == INSTANCE_REVOKED,
        Inbound::HelloAck(ack) => ack["denied"]
            .as_array()
            .into_iter()
            .flatten()
            .any(|d| d["code"] == INSTANCE_REVOKED),
        _ => false,
    }
}

/// The refusal a publish `error` frame of this code means when it is no
/// refusal for good: a used-up quota (`quota_exceeded`; the bus counts
/// `bus_messages`) or a policy hold (`policy_denied`). A quota frame's own
/// `resource, limit, used, reset_at, retry_after_s` are kept, so the bus
/// throttle waits `retry_after_s`, else until `reset_at`; a frame with
/// only the code waits for the calendar month's reset (API §8.1).
fn publish_hold(code: &str, quota: Option<&Value>) -> Option<CloudError> {
    let body = match code {
        "quota_exceeded" => {
            let mut error = json!({"code": code, "message": "", "resource": "bus_messages"});
            if let (Some(fields), Some(error)) =
                (quota.and_then(Value::as_object), error.as_object_mut())
            {
                error.extend(fields.iter().map(|(k, v)| (k.clone(), v.clone())));
            }
            json!({ "error": error })
        }
        "policy_denied" => json!({"error": {"code": code, "message": ""}}),
        _ => return None,
    };
    let status = if code == "policy_denied" { 403 } else { 429 };
    Some(CloudError::from_response(status, &body))
}

/// Whether the metrics sharing a hello named (`Some`, only for a Cloud with
/// the capability) differs from `[cloud] metrics_upload` now. A hello that
/// named nothing never asks for a fresh one: the next connect reads the
/// capability again.
fn sharing_changed(sent: Option<bool>, now: &CloudConfig) -> bool {
    sent.is_some_and(|sent| sent != now.metrics_upload)
}

fn now_ms() -> u64 {
    u64::try_from(lean_ctx_time::UtcDateTime::now().timestamp_millis()).unwrap_or_default()
}

/// One connected session; `Ok(Reannounce)` when the subscriptions or
/// the announced workers changed and a fresh hello is due. `connected` is
/// set once the socket is open, so the caller tells a session that was up
/// from a failed connect.
pub(super) async fn session_once(
    store: &CloudStore,
    config: &CloudConfig,
    connected: &mut bool,
    report: &super::super::Report,
) -> Result<SessionEnd, CloudError> {
    let instance = store.instance()?.ok_or(CloudError::NotConnected)?;
    let root = crate::root_cert::trust_root(store, config, &instance.url)?;
    let session = Session::load(store.clone(), root)?;
    if !session.lease.lease.grants(CloudRight::BusRelay) {
        return Err(CloudError::Forbidden);
    }
    let data = store.data_dir().to_path_buf();
    let mut state = StateV2::load(&data);
    state.begin_connection();
    // The hello below announces the agents as they are now, which answers
    // every announce request made so far (a `ctx_agent register` before
    // the relay ran, the one that ended the last session). Left in place,
    // such a request closed the socket right after the hello and dropped
    // the presence while a claim was on its way (Weave, !386).
    let _ = super::super::presence::take_announce(&data);
    let mut map = crate::resolution::current_view(&data);
    let LocalView {
        agents,
        unlinked,
        subscribe,
    } = local_view(&map);
    // Only a cursor of the place its address resolves to now is sent.
    let places = relay_v2::local_places(&map);
    if relay_v2::drop_moved_cursors(&mut state, &places) {
        state.save(&data)?;
    }
    let (session, (workers, failed)) = {
        let agents = agents.clone();
        tokio::task::spawn_blocking(move || {
            // Control keys come from key set v3 (E13); a refused or missing
            // set keeps the last verified one, and without one every
            // control is ignored and counted.
            if let Err(error) = crate::keyset_v3::refresh(&session) {
                session.store.record_error(&format!("key set v3: {error}"));
            }
            let workers = workers_of(&session, &agents);
            (session, workers)
        })
        .await
        .map_err(|e| CloudError::Local(e.to_string()))?
    };
    let keys = crate::keyset_v3::load(store, session.root_key())
        .map(|set| set.keys_of(crate::keyset_v3::Purpose::Control))
        .unwrap_or_default();
    let token = session.bearer_token(false)?;
    let mut request = ws_url(&instance.url)
        .into_client_request()
        .map_err(|e| CloudError::Config(format!("bus URL: {e}")))?;
    let headers = request.headers_mut();
    headers.insert(
        "Authorization",
        header(&format!("Bearer {}", token.as_str()))?,
    );
    headers.insert(HEADER_INSTANCE, header(&instance.instance_id)?);
    // The request header decides the protocol Cloud answers in: this session
    // speaks C0 2.0 (bus v2 frames), so the upgrade says so.
    headers.insert(HEADER_PROTOCOL, header(PROTOCOL_V2)?);
    headers.insert("User-Agent", header(&user_agent())?);
    let (socket, _) = open_socket(request).await.map_err(handshake_error)?;
    *connected = true;
    let (mut sink, mut stream) = socket.split();
    let text = |frame: &Value| Message::Text(frame.to_string().into());
    let offline = |e: tokio_tungstenite::tungstenite::Error| CloudError::Offline(e.to_string());
    // A person's post leaves only signed by the bound presence key: the
    // hello names `person_publish` only while it can be signed here.
    let person_signer = crate::consent::person_sign::person_signer(store, &instance.instance_id);
    // rc4-32: LeanCTX Code's chat key signs a person's post ahead; with it
    // bound, a prepared and signed post can leave too.
    state.chat_key = crate::consent::chat_key::ChatKeyCheck::load(store, &instance.instance_id);
    let mut announced = presence(&state, &agents, &workers, &instance);
    // rc4-86: Cloud marks each hello entry present in every subscribed
    // vault; a worker of fewer vaults is announced in its own ones only.
    let places = worker_places(&map, &agents, &workers);
    let in_hello = hello_presence(&announced, &places, &subscribe);
    // F29: whether this machine shares metrics, named only for a Cloud that
    // announced `instance_metrics_sharing`; a change re-announces (tick).
    let sent_sharing = relay_v2::metrics_sharing(store, config);
    sink.send(text(&relay_v2::with_metrics_sharing(
        relay_v2::hello(
            &subscribe,
            &state,
            &in_hello,
            person_signer.is_some() || state.chat_key.is_some(),
        ),
        sent_sharing,
    )))
    .await
    .map_err(offline)?;
    for entry in announced.iter().filter(|e| !in_hello.contains(e)) {
        for address in places_in(&places, &entry.worker_id, &subscribe) {
            let update = presence_update(address, entry, None, now_ms());
            sink.send(text(&update)).await.map_err(offline)?;
        }
    }
    let local_workers: BTreeSet<String> = workers.values().map(|w| w.worker_id.clone()).collect();
    let mut screen = Screen::for_installation();
    let mut tick = tokio::time::interval(TICK);
    let mut wake = tokio::time::interval(WAKE_POLL);
    let mut releases: Option<tokio::task::JoinHandle<()>> = None;
    // The task catch-up after the hello (task.event has no replay), over
    // HTTP and off this loop; its events are applied here.
    let mut catchup: Option<tokio::task::JoinHandle<(Vec<TaskEvent>, Vec<String>)>> = None;
    // §37: plan pulls (HTTP) run off this loop and send what they verified
    // here; the agents' answers go back from a background pass whenever the
    // directive records change (retried after a minute when Cloud failed).
    let (plan_tx, mut plan_rx) = tokio::sync::mpsc::unbounded_channel::<Vec<PlanOutcome>>();
    let mut plans_on = false;
    let mut plan_report: Option<tokio::task::JoinHandle<Vec<String>>> = None;
    let mut plan_report_seen: Option<std::time::SystemTime> = None;
    let mut plan_retry_at: Option<std::time::Instant> = None;
    let mut replay = relay_v2::Replay::default();
    // Cloud takes the `policy.applied` frame (its `hello.ack` names it).
    let mut policy_applied = false;
    // Cloud shows team presence names to the team only (its `hello.ack`
    // names `presence_identity`) and this machine shares them (h7b).
    let mut identity_on = false;
    let mut sent_identity: BTreeMap<String, PresenceIdentity> = BTreeMap::new();
    loop {
        tokio::select! {
            caught = async { catchup.as_mut().expect("guarded by is_some").await }, if catchup.is_some() => {
                catchup = None;
                let Ok((events, errors)) = caught else { continue };
                for error in errors {
                    store.record_error(&format!("bus task catch-up: {error}"));
                }
                for event in &events {
                    let applied = AgentRegistry::mutate_locked(|registry| {
                        task_events::apply(registry, &map, &data, &mut state.tasks, event, Origin::CatchUp)
                    });
                    if let Ok((_, Err(error))) = applied {
                        store.record_error(&format!("bus task event: {error}"));
                    }
                }
                state.save(&data)?;
            }
            Some(outcomes) = plan_rx.recv() => {
                let book = attach::list(store, &instance.instance_id).unwrap_or_default();
                let worker_of = |agent: &AgentEntry| {
                    workers
                        .get(&agent.agent_id)
                        .map(|w| w.worker_id.clone())
                        .or_else(|| book_worker(&book, agent))
                };
                for outcome in outcomes {
                    match outcome {
                        // `409 not_ready` (§37.2): a state shown once, not an error.
                        PlanOutcome::Note(note) if note.ends_with(plan_events::PLANS_NOT_READY) => {
                            report.say(plan_events::PLANS_NOT_READY);
                        }
                        PlanOutcome::Note(note) => store.record_error(&format!("bus plan: {note}")),
                        PlanOutcome::Verified { vault, vault_id, root, doc } => {
                            // Recorded as applied only once its directives are out.
                            let mut plans = state.plans.clone();
                            let applied = DirectiveStore::mutate(|registry, directives| {
                                plan_events::apply(
                                    registry,
                                    directives,
                                    &mut plans,
                                    (&vault, &vault_id, &root, &*doc),
                                    &worker_of,
                                )
                            });
                            match applied {
                                Ok(_) => state.plans = plans,
                                Err(error) => store.record_error(&format!("bus plan: {error}")),
                            }
                        }
                    }
                }
                state.save(&data)?;
            }
            // A local agent registered or changed role, or `cloud bus relay`
            // asked: a fresh hello announces this machine's workers again
            // before the next claim, and its record answers the CLI (#252).
            _ = wake.tick() => {
                if plans_on && plan_report.as_ref().is_none_or(tokio::task::JoinHandle::is_finished) {
                    if let Some(job) = plan_report.take()
                        && let Ok(errors) = job.await
                    {
                        plan_retry_at = (!errors.is_empty())
                            .then(|| std::time::Instant::now() + std::time::Duration::from_secs(60));
                        for error in errors {
                            store.record_error(&format!("bus plan: {error}"));
                        }
                    }
                    let modified = lean_ctx::core::agents::directives_modified();
                    let retry = plan_retry_at.is_some_and(|at| std::time::Instant::now() >= at);
                    if modified.is_some() && (modified != plan_report_seen || retry) {
                        plan_report_seen = modified;
                        plan_retry_at = None;
                        let (map, data) = (map.clone(), data.clone());
                        let carried = lean_ctx::core::context_admission::capture::carry();
                        plan_report = Some(tokio::task::spawn_blocking(move || {
                            carried.enter(|| plan_events::report(&map, &data))
                        }));
                    }
                }
                if super::super::presence::take_announce(&data) {
                    state.save(&data)?;
                    let _ = sink.send(Message::Close(None)).await;
                    return Ok(SessionEnd::Reannounce);
                }
            }
            incoming = stream.next() => {
                let raw = match incoming {
                    Some(Ok(Message::Text(raw))) => raw.to_string(),
                    Some(Ok(Message::Ping(payload))) => {
                        let _ = sink.send(Message::Pong(payload)).await;
                        continue;
                    }
                    Some(Ok(Message::Close(_))) | None => {
                        state.save(&data)?;
                        return Ok(SessionEnd::Closed);
                    }
                    Some(Ok(_)) => continue,
                    Some(Err(error)) => return Err(CloudError::Offline(error.to_string())),
                };
                let Ok(frame) = parse_frame(&raw) else {
                    continue;
                };
                let stamp = lean_ctx_time::UtcDateTime::now().to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true);
                // A pending rebuild (M12) ends once Cloud's replay is complete.
                relay_v2::note_replay(&mut state, &mut replay, &frame, !subscribe.is_empty());
                // A revoked instance: stop at the first frame that says so.
                if revocation_in(&frame) {
                    state.save(&data)?;
                    return Err(CloudError::Revoked(crate::Revocation::default()));
                }
                match frame {
                    Inbound::HelloAck(ack) => {
                        store.record_success("bus", &stamp);
                        // A policy change missed while offline is pulled at
                        // once on every (re)connect (a 304 when none).
                        let policy = ack["policy_version"].as_u64();
                        if policy.is_some() {
                            crate::policy::note_nudges_seen(store.data_dir());
                        }
                        crate::scheduler::nudge_policy(policy);
                        state.person_publish = relay_v2::acks_person_publish(&ack);
                        policy_applied = relay_v2::acks_feature(&ack, relay_v2::POLICY_APPLIED);
                        identity_on = config.presence_names
                            && relay_v2::acks_feature(&ack, relay_v2::PRESENCE_IDENTITY);
                        sent_identity.clear();
                        // A bus without a cursor keeps the replay start Cloud
                        // named, saved now: the next reconnect resumes there
                        // instead of at Cloud's head. A bus of another place
                        // or epoch (or an unbound seq) is reset to 0 and subscribed afresh (a
                        // hello is only a socket's first frame), so Cloud
                        // replays it from its start.
                        let adopted = relay_v2::adopt_ack_cursors(&mut state, &subscribe, &ack);
                        if adopted.changed {
                            state.save(&data)?;
                        }
                        if adopted.resubscribe {
                            let _ = sink.send(Message::Close(None)).await;
                            return Ok(SessionEnd::Replay);
                        }
                        let mut refused = Vec::new();
                        for denied in ack["denied"].as_array().into_iter().flatten() {
                            let line = format!(
                                "{} denied ({})",
                                denied["address"].as_str().unwrap_or("?"),
                                denied["code"].as_str().unwrap_or("?")
                            );
                            store.record_error(&format!("bus: {line}"));
                            refused.push(line);
                        }
                        // What Cloud now shows of this machine (#252).
                        // The report is local only: unlinked agents
                        // appear here, never in a frame.
                        let local: Vec<AgentEntry> =
                            agents.iter().chain(&unlinked).cloned().collect();
                        let lines = announce_report(
                            &subscribe,
                            &announced,
                            &local,
                            &workers,
                            &failed,
                            |root| relay_v2::record_for(&map, root).is_some(),
                            &refused,
                        );
                        super::super::presence::note_announced(&data, &lines);
                        report.say(&lines.join("\n"));
                        // §37: plan versions published while away, pulled once.
                        plans_on = plan_events::frames_taken(&ack);
                        if plans_on && !subscribe.is_empty() {
                            let (map, subscribe, seen, keys, data, tx) = (
                                map.clone(),
                                subscribe.clone(),
                                state.plans.clone(),
                                keys.clone(),
                                data.clone(),
                                plan_tx.clone(),
                            );
                            let carried = lean_ctx::core::context_admission::capture::carry();
                            tokio::task::spawn_blocking(move || {
                                carried.enter(|| {
                                    let (events, errors) = plan_events::catch_up(&map, &subscribe, &seen);
                                    let mut out: Vec<PlanOutcome> = errors
                                        .into_iter()
                                        .map(|e| PlanOutcome::Note(format!("catch-up {e}")))
                                        .collect();
                                    out.extend(plan_events::fetch(&map, &data, &seen, &keys, &events));
                                    let _ = tx.send(out);
                                });
                            });
                        }
                        // Subscribed now: what changed before is read once.
                        if !subscribe.is_empty() && catchup.is_none() {
                            let (map, subscribe) = (map.clone(), subscribe.clone());
                            catchup = Some(tokio::task::spawn_blocking(move || {
                                task_events::catch_up(&map, &subscribe)
                            }));
                        }
                    }
                    Inbound::Stored { client_msg_id, id, .. } => {
                        // A handoff this machine offered: its message id
                        // is what a maintainer's veto names.
                        if let Err(error) = super::super::handoff::stored(&data, &client_msg_id, &id) {
                            store.record_error(&format!("bus handoff: {error}"));
                        }
                        state.stored(&client_msg_id);
                        relay_v2::settle_queued(&data, &client_msg_id)?;
                        store.record_success("bus", &stamp);
                    }
                    Inbound::Error { client_msg_id: Some(id), code, quota, .. } => {
                        if let Some(refusal) = publish_hold(&code, quota.as_deref()) {
                            // A used-up bus quota or a policy hold: the post
                            // is not refused, it stays queued and goes again
                            // once the throttle ends (nothing is published
                            // meanwhile).
                            state.in_flight.remove(&id);
                            store.record_error(&format!("bus: {refusal}"));
                            let _ = crate::throttle::note(
                                store,
                                crate::throttle::Scope::Bus,
                                refusal,
                                lean_ctx_time::UtcDateTime::now(),
                            );
                        } else {
                            // Remembered for its sender, so no agent passes
                            // the refused message on (process v1 §7).
                            let sender = relay_v2::queued_worker(&data, &id);
                            if let Err(error) = super::super::person_rules::note_refused(
                                &data,
                                &id,
                                sender.as_deref(),
                                &code,
                            ) {
                                store.record_error(&format!("bus refused record: {error}"));
                            }
                            state.refused(&id);
                            relay_v2::settle_queued(&data, &id)?;
                            store.record_error(&format!("bus publish refused: {code}"));
                        }
                    }
                    Inbound::Error { client_msg_id: None, code, reason, .. } => {
                        store.record_error(&format!(
                            "bus: {code}{}",
                            reason.map(|r| format!(" ({r})")).unwrap_or_default()
                        ));
                    }
                    Inbound::Deliver(message) => {
                        // Who a message for one worker reaches is decided now,
                        // from the live registry and the worker book, not from
                        // what this connection saw when it opened (#28).
                        let book = attach::list(store, &instance.instance_id).unwrap_or_default();
                        let mine: BTreeSet<String> = local_workers
                            .iter()
                            .cloned()
                            .chain(book.iter().map(|w| w.worker_id.clone()))
                            .collect();
                        // §40: an exchange from another machine passes the
                        // same rules as an outgoing one and is audited; a
                        // refused one moves the cursor only. A record that
                        // cannot be written leaves the cursor, so the
                        // delivery comes again.
                        let apply = super::super::exchange::admit(
                            &data,
                            &map,
                            &mut state,
                            &message,
                            &mine,
                            lean_ctx_time::UtcDateTime::now(),
                        )
                        .unwrap_or_else(|error| {
                            store.record_error(&format!("bus exchange audit: {error}"));
                            false
                        });
                        if apply {
                            let worker_of = |agent: &AgentEntry| book_worker(&book, agent);
                            let applied = AgentRegistry::mutate_locked(|registry| {
                                relay_v2::apply_delivery(registry, &map, &mut state, &message, &mine, &worker_of)
                            });
                            if let Ok((_, Err(error))) = applied {
                                store.record_error(&format!("bus delivery: {error}"));
                            }
                        }
                    }
                    Inbound::TaskEvent(event) => {
                        let applied = AgentRegistry::mutate_locked(|registry| {
                            task_events::apply(registry, &map, &data, &mut state.tasks, &event, Origin::Frame)
                        });
                        if let Ok((_, Err(error))) = applied {
                            store.record_error(&format!("bus task event: {error}"));
                        }
                    }
                    // §37.1: without `plan_updates` in the hello.ack both
                    // frames are ignored; nothing is pulled or issued.
                    Inbound::PlanAdvanced(_) | Inbound::DecisionChanged(_) if !plans_on => {}
                    // §37: pulled and verified off this loop, applied above.
                    Inbound::PlanAdvanced(event) => {
                        let (map, seen, keys, data, tx) = (
                            map.clone(),
                            state.plans.clone(),
                            keys.clone(),
                            data.clone(),
                            plan_tx.clone(),
                        );
                        let carried = lean_ctx::core::context_admission::capture::carry();
                        tokio::task::spawn_blocking(move || {
                            carried.enter(|| {
                                let _ = tx.send(plan_events::fetch(&map, &data, &seen, &keys, &[*event]));
                            });
                        });
                    }
                    Inbound::Control { lifted, control } => {
                        // A verified veto of a host lock or handoff drops it
                        // here and refuses its holder once (§3.7.4, V9.5).
                        let veto = (!lifted
                            && control.action == super::super::v2::ControlAction::Veto)
                            .then(|| (*control).clone());
                        if state.apply_control(lifted, *control, &keys)
                            && let Some(control) = veto
                        {
                            let worker_of = |agent: &str| {
                                workers.get(agent).map(|w| w.worker_id.clone())
                            };
                            if let Err(error) =
                                super::super::veto::apply(store, &control, &worker_of)
                            {
                                store.record_error(&format!("bus veto: {error}"));
                            }
                        }
                    }
                    Inbound::PlaceDenied { place, code } => {
                        store.record_error(&format!("bus: {place} denied ({code})"));
                        state.denied.insert(place, code);
                    }
                    // A cleared queue: reconnect, the hello resumes from the cursors.
                    Inbound::Resync { .. } => {
                        state.save(&data)?;
                        return Ok(SessionEnd::Reannounce);
                    }
                    Inbound::Ping => {
                        sink.send(text(&json!({"t": "pong"}))).await.map_err(offline)?;
                    }
                    Inbound::ConsentAnswered { consent_id } => {
                        crate::consent::web::note_answered(&data, &consent_id);
                    }
                    // Who works where: other machines' workers, for
                    // `ctx_agent list` (this machine's own are local).
                    Inbound::Presence { address, worker_id, role, state: presence_state, at, names } => {
                        if !local_workers.contains(&worker_id) {
                            let recorded = super::super::presence::PresenceMap::mutate(&data, |map| {
                                map.apply_named(&address, &worker_id, &role, presence_state, at, names)
                            });
                            if let Err(error) = recorded {
                                store.record_error(&format!("bus presence: {error}"));
                            }
                        }
                    }
                    // Background sync tails the place now (hints only: the
                    // replica verifies whatever it fetches).
                    Inbound::LogAdvanced { place, purge } => {
                        let trigger = if purge {
                            crate::sync::auto::Trigger::BusPurge
                        } else {
                            crate::sync::auto::Trigger::Bus
                        };
                        crate::sync::auto::nudge_place(&place, trigger);
                    }
                    Inbound::TrustAdvanced { place } => {
                        crate::sync::auto::nudge_place(&place, crate::sync::auto::Trigger::Bus);
                    }
                    Inbound::PolicyAdvanced { version } => {
                        crate::policy::note_nudges_seen(store.data_dir());
                        crate::scheduler::nudge_policy(Some(version));
                    }
                    // §37: Cloud sends it only after the person's confirmed
                    // "Send to agents"; delivered here and nowhere else.
                    Inbound::DecisionChanged(event) => {
                        let delivered = DirectiveStore::mutate(|registry, directives| {
                            plan_events::deliver_decision(registry, directives, &map, &event)
                        });
                        if let Err(error) = delivered {
                            store.record_error(&format!("bus decision: {error}"));
                        }
                        // rc4-69: recorded for an older Cloud that lists no
                        // `sent_at`, so agents that start later get it too.
                        if let Err(error) = crate::team_decisions::note_sent(store.data_dir(), &event) {
                            store.record_error(&format!("bus decision record: {error}"));
                        }
                    }
                    Inbound::Other(_) => {}
                }
                state.save(&data)?;
            }
            _ = tick.tick() => {
                // Switched off in `[cloud]` meanwhile: close; `run` waits.
                let Some(now_config) = super::super::relay_config() else {
                    state.save(&data)?;
                    let _ = sink.send(Message::Close(None)).await;
                    return Ok(SessionEnd::Closed);
                };
                // F29: `cloud.metrics_upload` changed since the hello named
                // it: a fresh hello carries the new value.
                if sharing_changed(sent_sharing, &now_config) {
                    state.save(&data)?;
                    let _ = sink.send(Message::Close(None)).await;
                    return Ok(SessionEnd::Reannounce);
                }
                map = crate::resolution::current_view(&data);
                let LocalView {
                    agents: fresh_agents,
                    subscribe: fresh_subscribe,
                    ..
                } = local_view(&map);
                // A new agent, or one that registered again with another
                // role, attaches to another worker: announce it afresh.
                if fresh_subscribe != subscribe || !attached_alike(&fresh_agents, &agents) {
                    state.save(&data)?;
                    let _ = sink.send(Message::Close(None)).await;
                    return Ok(SessionEnd::Reannounce);
                }
                let fresh = presence(&state, &fresh_agents, &workers, &instance);
                let fresh_identity = identities(identity_on, &fresh_agents, &workers, &instance);
                if fresh != announced || fresh_identity != sent_identity {
                    // A worker changed state (held, idle) or what its team
                    // sees of it (h7b): tell each place.
                    let changed: Vec<&PresenceEntry> = fresh
                        .iter()
                        .filter(|e| {
                            !announced.contains(e)
                                || fresh_identity.get(&e.worker_id)
                                    != sent_identity.get(&e.worker_id)
                        })
                        .collect();
                    let places = worker_places(&map, &fresh_agents, &workers);
                    for entry in &changed {
                        for address in places_in(&places, &entry.worker_id, &subscribe) {
                            let update = presence_update(
                                address,
                                entry,
                                fresh_identity.get(&entry.worker_id),
                                now_ms(),
                            );
                            sink.send(text(&update)).await.map_err(offline)?;
                        }
                    }
                    announced = fresh;
                    sent_identity = fresh_identity;
                }
                // D4: once per version and digest, tell Cloud which signed
                // policy this Instance applied and how many live sessions
                // enforce it (verified, in force and acked only).
                if policy_applied
                    && let Some((frame, reported)) =
                        crate::policy_sessions::applied_frame(&data, lean_ctx_time::UtcDateTime::now())
                {
                    sink.send(text(&frame)).await.map_err(offline)?;
                    crate::policy_sessions::note_reported(&data, &reported);
                }
                // A bus quota or policy throttle: posts stay local until it ends.
                if !bus_held(store) {
                    let registry = AgentRegistry::load_or_create();
                    let worker_of = |agent: &AgentEntry| workers.get(&agent.agent_id).map(|w| w.worker_id.clone());
                    let records = super::super::person_rules::Records::load(&data);
                    for item in relay_v2::outgoing(&registry, &map, &mut state, &mut screen, &worker_of, &records, person_signer.as_deref()) {
                        sink.send(text(&item.frame)).await.map_err(offline)?;
                    }
                    for frame in relay_v2::queued_frames(&data, &mut state) {
                        sink.send(text(&frame)).await.map_err(offline)?;
                    }
                }
                // C5: lease releases the tool calls queued, over HTTP and
                // off this loop, one pass at a time.
                if releases.as_ref().is_none_or(tokio::task::JoinHandle::is_finished) {
                    let (store, root) = (store.clone(), *session.root_key());
                    releases = Some(tokio::task::spawn_blocking(move || {
                        if let Ok(session) = Session::load(store.clone(), root) {
                            super::super::leases::send_releases(&store, &session);
                        }
                    }));
                }
                state.save(&data)?;
            }
        }
    }
}

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

    fn frame(value: &Value) -> Inbound {
        parse_frame(&value.to_string()).unwrap()
    }

    /// Cloud' heartbeat on a revoked instance (`place.denied
    /// instance_revoked` per place, then close): the first such frame
    /// stops the relay; other denials do not.
    #[test]
    fn the_first_revocation_frame_stops_the_session() {
        assert!(revocation_in(&frame(
            &json!({"t": "place.denied", "place": "vault:x", "code": "instance_revoked"})
        )));
        assert!(revocation_in(&frame(
            &json!({"t": "error", "code": "instance_revoked"})
        )));
        assert!(revocation_in(&frame(&json!({"t": "hello.ack", "denied": [
            {"address": "#a/b", "code": "not_member"},
            {"address": "#a/c", "code": "instance_revoked"}
        ]}))));
        assert!(!revocation_in(&frame(
            &json!({"t": "place.denied", "place": "vault:x", "code": "not_member"})
        )));
        assert!(!revocation_in(&frame(
            &json!({"t": "hello.ack", "denied": []})
        )));
    }

    /// E2E 0.5.0: every active agent of the machine was announced and
    /// registered as a worker, also those in projects no vault links.
    #[test]
    fn only_agents_in_linked_vaults_are_announced_or_registered() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let shared = tempfile::tempdir().unwrap();
        let private = tempfile::tempdir().unwrap();
        let shared_root = shared.path().to_string_lossy().into_owned();
        let private_root = private.path().to_string_lossy().into_owned();
        let mut map = VaultMap::default();
        map.vaults.insert(
            "s".repeat(64),
            crate::vault::VaultRecord {
                vault_id: "vlt_shared".into(),
                ns: "acme".into(),
                name: "payments".into(),
                sealed: false,
                roots: vec![shared_root.clone()],
            },
        );
        let mut registry = AgentRegistry::new();
        registry
            .register("claude", Some("coder"), &shared_root, None)
            .unwrap();
        // One registration per process: the second agent is the same
        // entry in a project no vault links.
        let mut other = registry.agents[0].clone();
        other.agent_id = "codex-private".into();
        other.project_root = private_root.clone();
        registry.agents.push(other);
        let all: Vec<&AgentEntry> = registry.agents.iter().collect();
        assert_eq!(all.len(), 2);

        let bound = in_linked_vaults(&map, all);
        let roots: Vec<&str> = bound.iter().map(|a| a.project_root.as_str()).collect();
        assert_eq!(roots, vec![shared_root.as_str()], "only the shared project");
        let none = in_linked_vaults(&VaultMap::default(), registry.agents.iter().collect());
        assert!(none.is_empty(), "no linked vault, no presence");

        // The relay's own report still says why the private agent is on no
        // bus (#252) — and nothing that leaves the machine names it.
        let view = view_of(&map, registry.agents.iter().collect());
        assert_eq!(view.agents.len(), 1);
        assert_eq!(view.unlinked.len(), 1);
        let mut workers = BTreeMap::new();
        for agent in &view.agents {
            workers.insert(agent.agent_id.clone(), coder_worker());
        }
        let instance = instance_labelled("dev-box");
        let state = StateV2::default();
        let entries = presence(&state, &view.agents, &workers, &instance);
        let names = identities(true, &view.agents, &workers, &instance);
        let outgoing = [
            relay_v2::hello(&view.subscribe, &state, &entries, false).to_string(),
            serde_json::to_string(&entries).unwrap(),
            serde_json::to_string(&names).unwrap(),
            view.subscribe.join(","),
        ];
        assert_eq!(entries.len(), 1, "the shared agent is present");
        for frame in &outgoing {
            assert!(!frame.contains("codex-private"), "{frame}");
            assert!(!frame.contains(&private_root), "{frame}");
        }
        let local: Vec<AgentEntry> = view.agents.iter().chain(&view.unlinked).cloned().collect();
        let lines = announce_report(
            &view.subscribe,
            &entries,
            &local,
            &workers,
            &BTreeMap::new(),
            |root| relay_v2::record_for(&map, root).is_some(),
            &[],
        );
        assert!(
            lines.iter().any(|line| line.contains("codex-private")
                && line
                    .contains("not on a bus: its checkout is not linked to a LeanCTX Cloud vault")),
            "{lines:?}"
        );
        assert!(
            !lines
                .iter()
                .any(|line| line.contains("could not be registered")),
            "{lines:?}"
        );
    }

    /// rc4-86: an MCP server's own `context-engine` presence in a linked
    /// project showed up in the team as an "unknown" agent.
    #[test]
    fn an_engine_placeholder_is_neither_announced_nor_a_worker() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let shared = tempfile::tempdir().unwrap();
        let shared_root = shared.path().to_string_lossy().into_owned();
        let mut map = VaultMap::default();
        map.vaults.insert(
            "s".repeat(64),
            crate::vault::VaultRecord {
                vault_id: "vlt_shared".into(),
                ns: "acme".into(),
                name: "payments".into(),
                sealed: false,
                roots: vec![shared_root.clone()],
            },
        );
        let mut registry = AgentRegistry::new();
        registry
            .register("claude", Some("coder"), &shared_root, None)
            .unwrap();
        let mut engine = registry.agents[0].clone();
        engine.agent_id = "mcp-engine".into();
        engine.agent_type = "mcp".into();
        engine.role = Some("context-engine".into());
        engine.role_source = Some(lean_ctx::core::agents::RoleSource::Assumed);
        registry.agents.push(engine);

        let view = view_of(&map, registry.agents.iter().collect());
        let ids: Vec<&str> = view.agents.iter().map(|a| a.agent_id.as_str()).collect();
        assert_eq!(ids, vec![registry.agents[0].agent_id.as_str()]);
        assert!(view.unlinked.is_empty(), "nor reported as off the bus");
    }

    /// F29: switching `cloud.metrics_upload` re-announces only a session
    /// whose hello named it (a Cloud with the capability).
    #[test]
    fn a_metrics_sharing_change_needs_a_fresh_hello_only_where_it_was_named() {
        let on = CloudConfig::default();
        let off = CloudConfig {
            metrics_upload: false,
            ..CloudConfig::default()
        };
        assert!(sharing_changed(Some(true), &off));
        assert!(sharing_changed(Some(false), &on));
        assert!(!sharing_changed(Some(true), &on));
        assert!(!sharing_changed(Some(false), &off));
        assert!(!sharing_changed(None, &off));
        assert!(!sharing_changed(None, &on));
    }

    /// Walk 02.11.: the MCP connection registers as `coder`, then
    /// `ctx_agent register role=tester` changes the role of the same agent.
    /// It attaches to another worker, so the session announces afresh; an
    /// agent that left needs no new hello.
    #[test]
    fn a_role_change_of_an_announced_agent_needs_a_fresh_hello() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path().to_string_lossy().into_owned();
        let mut registry = AgentRegistry::new();
        registry
            .register("codex", Some("coder"), &root, None)
            .unwrap();
        // One process holds one presence: the second agent is another one's.
        let mut other = registry.agents[0].clone();
        other.agent_id = "claude-2-x".into();
        other.role = Some("dev".into());
        let announced = vec![registry.agents[0].clone(), other];
        assert!(attached_alike(&announced, &announced));
        assert!(attached_alike(&announced[..1], &announced), "one left");

        let mut changed = announced.clone();
        changed[0].role = Some("tester".into());
        assert!(!attached_alike(&changed, &announced), "role changed");
        assert!(!attached_alike(&announced, &announced[..1]), "a new agent");
    }

    /// #28: the relay finds a session's worker in the book as it is now,
    /// by host app and normalized role, so a session that reconnected under
    /// a new agent id still gets its worker's message; another role or
    /// host gets none.
    #[test]
    fn book_worker_maps_sessions_by_host_and_normalized_role() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let mut registry = AgentRegistry::new();
        registry
            .register("claude", Some("Coder"), "/src/p", None)
            .unwrap();
        let coder = registry.agents[0].clone();
        let (host, _) = attach::agent_worker_key(&coder);
        let worker = |id: &str, host: &str, role: &str| LocalWorker {
            worker_id: id.into(),
            host_app: host.into(),
            role: role.into(),
            name: None,
            registered_at: None,
            aliases: BTreeMap::new(),
            last_attached: None,
            per_session: false,
        };
        let book = vec![
            worker("wkr_tester", &host, "tester"),
            worker("wkr_coder", &host, "coder"),
            worker("wkr_other_host", "some-other-host", "reviewer"),
        ];

        assert_eq!(
            book_worker(&book, &coder).as_deref(),
            Some("wkr_coder"),
            "`Coder` maps to the `coder` worker"
        );
        let mut sibling = coder.clone();
        sibling.agent_id = "claude-reconnected".into();
        sibling.role = Some("coder".into());
        assert_eq!(
            book_worker(&book, &sibling).as_deref(),
            Some("wkr_coder"),
            "a second session with the same host and role maps to the same worker"
        );
        let mut other_role = coder.clone();
        other_role.role = Some("security-reviewer".into());
        assert_eq!(book_worker(&book, &other_role), None, "another role");
        let mut other_host = coder.clone();
        other_host.role = Some("reviewer".into());
        assert_ne!(host, "some-other-host");
        assert_eq!(book_worker(&book, &other_host), None, "another host");
    }

    /// #252: `cloud bus relay` said nothing, so nobody saw that the tester
    /// was not on the bus. Its report names each announced worker with role
    /// and state, and for every agent that is not on a bus, why.
    #[test]
    fn the_relay_report_says_who_was_announced_and_why_not() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let mut registry = AgentRegistry::new();
        registry
            .register("claude", Some("tester"), "/src/pounce-cloud", None)
            .unwrap();
        // One process holds one agent: the others are other sessions'.
        let agents: Vec<AgentEntry> = [
            ("tester", "/src/pounce-cloud"),
            ("dev", "/src/unlinked"),
            ("", "/src/pounce-cloud"),
            ("ops", "/src/pounce-cloud"),
        ]
        .iter()
        .enumerate()
        .map(|(n, (role, root))| {
            let mut agent = registry.agents[0].clone();
            agent.agent_id = format!("claude-{n}-x");
            agent.role = Some((*role).to_string()).filter(|r| !r.is_empty());
            agent.project_root = (*root).to_string();
            agent
        })
        .collect();
        let worker = |id: &str, role: &str| LocalWorker {
            worker_id: id.into(),
            host_app: "claude-code".into(),
            role: role.into(),
            name: None,
            registered_at: Some(1),
            aliases: BTreeMap::new(),
            last_attached: None,
            per_session: false,
        };
        let mut workers = BTreeMap::new();
        workers.insert(agents[0].agent_id.clone(), worker("wkr_tester", "tester"));
        workers.insert(agents[1].agent_id.clone(), worker("wkr_dev", "dev"));
        workers.insert(agents[2].agent_id.clone(), worker("wkr_none", ""));
        let mut failed = BTreeMap::new();
        failed.insert(
            agents[3].agent_id.clone(),
            "LeanCTX Cloud answered 503".to_string(),
        );
        let announced = vec![PresenceEntry {
            worker_id: "wkr_tester".into(),
            role: "tester".into(),
            state: super::super::super::v2::PresenceState::Active,
            display_name: None,
            instance_id: None,
            instance_name: None,
        }];
        let subscribe = vec!["#yves/pounce-cloud".to_string()];
        let lines = announce_report(
            &subscribe,
            &announced,
            &agents,
            &workers,
            &failed,
            |root| root == "/src/pounce-cloud",
            &["#yves/secret denied (not_member)".into()],
        );
        let text = lines.join("\n");
        assert_eq!(
            lines[0],
            "bus relay: connected to LeanCTX Cloud; announced on #yves/pounce-cloud:"
        );
        assert!(
            text.contains(&format!(
                "  wkr_tester · tester · active  (agent {} in /src/pounce-cloud)",
                agents[0].agent_id
            )),
            "{text}"
        );
        assert!(
            text.contains("wkr_dev · dev") && text.contains("not linked to a LeanCTX Cloud vault"),
            "{text}"
        );
        assert!(
            text.contains("wkr_none · no role ·") && text.contains("register with role="),
            "{text}"
        );
        assert!(
            text.contains("(ops) in /src/pounce-cloud: not announced, its worker could not be registered: LeanCTX Cloud answered 503"),
            "{text}"
        );
        assert!(
            text.contains("#yves/secret denied (not_member): refused by LeanCTX Cloud"),
            "{text}"
        );

        let none = announce_report(&[], &[], &[], &workers, &failed, |_| true, &[]);
        assert!(none[0].contains("no agent is active"), "{none:?}");
        let unlinked = announce_report(
            &[],
            &announced,
            &agents[..1],
            &workers,
            &failed,
            |_| false,
            &[],
        );
        assert!(
            unlinked[0].contains("no active agent works in a checkout linked"),
            "{unlinked:?}"
        );
    }

    /// rc4-86 under rc4-82: the relay subscribes every linked vault, and
    /// Cloud marks each hello entry present in all of them. A worker of one
    /// vault is announced there only, by a `presence.update`; with a single
    /// subscription the hello carries it as before.
    #[test]
    fn a_worker_is_present_only_in_the_vaults_its_agents_work_in() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let (pay, docs) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
        let pay_root = pay.path().to_string_lossy().into_owned();
        let mut map = VaultMap::default();
        for (key, name, root) in [
            ("p", "payments", pay_root.clone()),
            ("d", "docs", docs.path().to_string_lossy().into_owned()),
        ] {
            map.vaults.insert(
                key.repeat(64),
                crate::vault::VaultRecord {
                    vault_id: format!("vlt_{name}"),
                    ns: "acme".into(),
                    name: name.into(),
                    sealed: false,
                    roots: vec![root],
                },
            );
        }
        let mut registry = AgentRegistry::new();
        registry
            .register("claude", Some("coder"), &pay_root, None)
            .unwrap();
        let agent = registry.agents[0].clone();
        let workers = BTreeMap::from([(agent.agent_id.clone(), coder_worker())]);
        let entry = PresenceEntry {
            worker_id: "wkr_coder".into(),
            role: "coder".into(),
            state: super::super::super::v2::PresenceState::Active,
            display_name: None,
            instance_id: None,
            instance_name: None,
        };
        let announced = vec![entry];
        let places = worker_places(&map, std::slice::from_ref(&agent), &workers);

        let both = relay_v2::subscriptions(&map);
        assert_eq!(
            both,
            vec!["#acme/docs".to_string(), "#acme/payments".into()]
        );
        assert!(hello_presence(&announced, &places, &both).is_empty());
        assert_eq!(
            places_in(&places, "wkr_coder", &both),
            vec!["#acme/payments"]
        );
        assert!(places_in(&places, "wkr_other", &both).is_empty());

        let one = vec!["#acme/payments".to_string()];
        assert_eq!(hello_presence(&announced, &places, &one), announced);
    }

    fn coder_worker() -> LocalWorker {
        LocalWorker {
            worker_id: "wkr_coder".into(),
            host_app: "claude-code".into(),
            role: "coder".into(),
            name: None,
            registered_at: Some(1),
            aliases: BTreeMap::new(),
            last_attached: None,
            per_session: false,
        }
    }

    fn instance_labelled(label: &str) -> crate::state::InstanceRecord {
        crate::state::InstanceRecord {
            instance_id: "inst_1".into(),
            org_id: "org_1".into(),
            org_name: "Platform".into(),
            label: label.into(),
            url: "https://cloud.example".into(),
            org_salt_id: "salt".into(),
            enrolled_at: "2026-11-17T00:00:00Z".into(),
            web_url: None,
            org_kind: Some("team".into()),
            company_name: None,
        }
    }

    /// h7b team presence: with the names on and acknowledged, each
    /// worker's `presence.update` carries this machine's short name (never
    /// its domain) and the status line its agent wrote; switched off
    /// (`[cloud] presence_names = false`) or not acknowledged, the frame is
    /// what it was: role and state only.
    #[test]
    fn team_presence_names_ride_only_when_on() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let mut registry = AgentRegistry::new();
        let id = registry
            .register("claude", Some("coder"), "/src/app", None)
            .unwrap();
        registry
            .set_status_by_agent(
                &id,
                lean_ctx::core::agents::AgentStatus::Active,
                Some("Fixing the login page\nstep 2"),
            )
            .unwrap();
        let agent = registry.agents[0].clone();
        let mut idle = agent.clone();
        idle.agent_id = "claude-2-x".into();
        idle.status_message = None;
        let mut workers = BTreeMap::new();
        workers.insert(idle.agent_id.clone(), coder_worker());
        workers.insert(agent.agent_id.clone(), coder_worker());
        let instance = instance_labelled("build-01.corp.example.com");
        let agents = vec![idle, agent];
        let on = identities(true, &agents, &workers, &instance);
        let named = on.get("wkr_coder").expect("one identity per worker");
        assert_eq!(named.machine_name.as_deref(), Some("build-01"));
        assert_eq!(named.working_on.as_deref(), Some("Fixing the login page"));
        assert!(identities(false, &agents, &workers, &instance).is_empty());

        let entry = PresenceEntry {
            worker_id: "wkr_coder".into(),
            role: "coder".into(),
            state: super::super::super::v2::PresenceState::Active,
            display_name: None,
            instance_id: Some("inst_1".into()),
            instance_name: None,
        };
        let with = presence_update("#acme/app", &entry, Some(named), 7);
        assert_eq!(with["machine_name"], "build-01");
        assert_eq!(with["working_on"], "Fixing the login page");
        assert_eq!(with["t"], "presence.update");
        assert_eq!(with["worker_id"], "wkr_coder");
        // A cleared status is sent as an explicit null, so it replaces the
        // old line on a Cloud that merges entries too.
        let cleared = PresenceIdentity {
            machine_name: Some("build-01".into()),
            working_on: None,
        };
        let cleared = presence_update("#acme/app", &entry, Some(&cleared), 8);
        assert!(cleared["working_on"].is_null());
        assert!(cleared.as_object().unwrap().contains_key("working_on"));
        let without = presence_update("#acme/app", &entry, None, 7);
        let keys: BTreeSet<&str> = without
            .as_object()
            .unwrap()
            .keys()
            .map(String::as_str)
            .collect();
        assert_eq!(
            keys,
            BTreeSet::from([
                "t",
                "address",
                "at",
                "worker_id",
                "role",
                "state",
                "instance_id"
            ])
        );
    }

    /// h7b blocking 2: LeanCTX's own lifecycle notes (lease expired,
    /// transport idle, connection closed) are never sent as what a worker
    /// is working on; only a line the agent wrote itself is.
    #[test]
    fn h7b_lifecycle_notes_never_ride_as_working_on() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let mut registry = AgentRegistry::new();
        let id = registry
            .register("claude", Some("coder"), "/src/app", None)
            .unwrap();
        registry
            .set_status(
                &id,
                lean_ctx::core::agents::AgentStatus::Idle,
                Some("worker lease expired; register again before continuing"),
            )
            .unwrap();
        let mut transport_idle = registry.agents[0].clone();
        transport_idle.agent_id = "claude-3-x".into();
        transport_idle.status_message =
            Some("connected; no tool call within the worker lease".into());
        let agents = vec![registry.agents[0].clone(), transport_idle];
        let mut workers = BTreeMap::new();
        for agent in &agents {
            workers.insert(agent.agent_id.clone(), coder_worker());
        }
        let instance = instance_labelled("ada-mac.local");
        let on = identities(true, &agents, &workers, &instance);
        let named = on.get("wkr_coder").expect("one identity per worker");
        assert_eq!(named.working_on, None, "{named:?}");
        assert_eq!(named.machine_name.as_deref(), Some("ada-mac"));
    }

    /// h7b blocking 3: the Instance name in every hello and
    /// `presence.update` is the label's short name, never a domain.
    #[test]
    fn h7b_presence_instance_name_never_carries_a_domain() {
        let _data = lean_ctx::core::data_dir::isolated_data_dir();
        let mut registry = AgentRegistry::new();
        registry
            .register("claude", Some("coder"), "/src/app", None)
            .unwrap();
        let agents = vec![registry.agents[0].clone()];
        let mut workers = BTreeMap::new();
        workers.insert(agents[0].agent_id.clone(), coder_worker());
        let state = StateV2::default();
        for (label, shown) in [
            ("build-01.corp.example.com", "build-01"),
            ("Yves's laptop", "Yves's laptop"),
            ("10.0.0.5", "10.0.0.5"),
        ] {
            let entries = presence(&state, &agents, &workers, &instance_labelled(label));
            assert_eq!(entries[0].instance_name.as_deref(), Some(shown), "{label}");
        }
    }

    /// A quota or policy refusal of a publish keeps the post; any other
    /// code is a refusal for good.
    #[test]
    fn quota_and_policy_publish_errors_hold_instead_of_refusing() {
        let quota = publish_hold("quota_exceeded", None).unwrap();
        assert!(
            matches!(&quota, CloudError::QuotaExceeded(q) if q.resource == "bus_messages" && q.waits())
        );
        let now = lean_ctx_time::OffsetDateTime::parse_from_rfc3339("2026-10-26T12:00:00Z")
            .unwrap()
            .to_utc();
        let (_, throttle) =
            crate::throttle::of_error(Some(crate::throttle::Scope::Bus), &quota, now).unwrap();
        assert_eq!(
            throttle.until, "2026-11-01T00:00:00Z",
            "until the month resets"
        );
        assert!(matches!(
            publish_hold("policy_denied", None),
            Some(CloudError::PolicyDenied(_))
        ));
        assert!(publish_hold("screen_blocked", None).is_none());
        assert!(publish_hold("invalid_state", None).is_none());
    }

    /// The bus throttle `until` a `quota_exceeded` publish frame sets at
    /// `now` (2026-10-26 12:00 UTC).
    fn bus_until(raw: &Value) -> String {
        bus_until_at(raw, "2026-10-26T12:00:00Z")
    }

    fn bus_until_at(raw: &Value, now: &str) -> String {
        let Inbound::Error { code, quota, .. } = frame(raw) else {
            panic!("not an error frame: {raw}");
        };
        let refusal = publish_hold(&code, quota.as_deref()).unwrap();
        let now = lean_ctx_time::OffsetDateTime::parse_from_rfc3339(now)
            .unwrap()
            .to_utc();
        crate::throttle::of_error(Some(crate::throttle::Scope::Bus), &refusal, now)
            .unwrap()
            .1
            .until
    }

    /// C0 `bus/frame.error.quota_exceeded.json` (c/follow-final 24d7ecab):
    /// every quota member is read, and its `retry_after_s` (5.5 days) sets
    /// the wait, not its `reset_at`.
    #[test]
    fn the_c0_quota_frame_sets_the_bus_wait_from_its_own_fields() {
        let path = format!(
            "{}/crates/lean-ctx-protocol/tests/fixtures/cloud-v2/bus/frame.error.quota_exceeded.json",
            concat!(env!("CARGO_MANIFEST_DIR"), "/../..")
        );
        let raw: Value = serde_json::from_slice(&std::fs::read(&path).expect(&path)).unwrap();
        let Inbound::Error {
            client_msg_id,
            code,
            quota,
            ..
        } = frame(&raw)
        else {
            panic!("not an error frame");
        };
        assert_eq!(client_msg_id.as_deref(), raw["client_msg_id"].as_str());
        assert_eq!(code, "quota_exceeded");
        let refusal = publish_hold(&code, quota.as_deref()).unwrap();
        let CloudError::QuotaExceeded(q) = &refusal else {
            panic!("not a quota: {refusal:?}");
        };
        assert_eq!(q.resource, raw["resource"].as_str().unwrap());
        assert_eq!(q.limit, raw["limit"].as_u64());
        assert_eq!(q.used, raw["used"].as_u64());
        assert_eq!(q.reset_at.as_deref(), raw["reset_at"].as_str());
        assert_eq!(q.retry_after, raw["retry_after_s"].as_u64());
        assert!(q.waits());
        // 475 200 s after 2026-10-30 00:00 is 2026-11-04 12:00, past the
        // frame's reset_at (2026-11-01): retry_after_s wins.
        assert_eq!(
            bus_until_at(&raw, "2026-10-30T00:00:00Z"),
            "2026-11-04T12:00:00Z"
        );
    }

    /// A quota frame's `retry_after_s` sets the bus wait, else its
    /// `reset_at`; a frame without them (an older Cloud) or with fields of
    /// another type keeps the calendar-month fallback.
    #[test]
    fn a_quota_frame_waits_for_its_own_retry_after_or_reset_at() {
        let error = |extra: Value| {
            let mut f = json!({"t": "error", "client_msg_id": "01M5NKA2Z8B000000000000012",
                "code": "quota_exceeded"});
            f.as_object_mut()
                .unwrap()
                .extend(extra.as_object().unwrap().clone());
            f
        };
        let full = error(
            json!({"resource": "bus_messages", "limit": 10_000, "used": 10_000,
            "reset_at": "2026-11-01T00:00:00Z", "retry_after_s": 600}),
        );
        assert_eq!(
            bus_until(&full),
            "2026-10-26T12:10:00Z",
            "retry_after_s first"
        );
        let Inbound::Error { quota, .. } = frame(&full) else {
            unreachable!()
        };
        let refusal = publish_hold("quota_exceeded", quota.as_deref()).unwrap();
        assert!(matches!(&refusal, CloudError::QuotaExceeded(q)
            if q.resource == "bus_messages" && q.limit == Some(10_000) && q.used == Some(10_000)));
        assert_eq!(
            bus_until(&error(json!({"reset_at": "2026-10-28T06:30:00Z"}))),
            "2026-10-28T06:30:00Z",
            "then reset_at"
        );
        assert_eq!(
            bus_until(&error(json!({}))),
            "2026-11-01T00:00:00Z",
            "no fields: the month's reset"
        );
        assert_eq!(
            bus_until(&error(
                json!({"limit": "many", "retry_after_s": -5, "reset_at": 7})
            )),
            "2026-11-01T00:00:00Z",
            "odd types are ignored, not fatal"
        );
        assert_eq!(
            bus_until(&error(json!({"used": 3, "retry_after_s": 90}))),
            "2026-10-26T12:01:30Z",
            "a good field survives an absent one"
        );
    }

    /// A hostile frame's huge `retry_after_s` holds the bus for the longest
    /// wait (32 days); it never overflows the time type, so the relay loop lives.
    #[test]
    fn a_huge_retry_after_in_a_quota_frame_is_capped_not_a_panic() {
        for huge in [u64::MAX, i64::MAX.unsigned_abs()] {
            let raw = json!({"t": "error", "client_msg_id": "01M5NKA2Z8B000000000000012",
                "code": "quota_exceeded", "retry_after_s": huge});
            let Inbound::Error { quota, .. } = frame(&raw) else {
                unreachable!()
            };
            assert_eq!(
                quota.as_deref().and_then(|q| q["retry_after_s"].as_u64()),
                Some(crate::error::RETRY_AFTER_CAP_S),
                "quota_fields caps {huge}"
            );
            assert_eq!(bus_until(&raw), "2026-11-27T12:00:00Z", "{huge}");
        }
    }
}