gwk-tui 0.0.3

The GridWork terminal console — the thin client that renders kernel projections
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
//! The estate frame: deterministic spatial layout over caller-normalized facts.
//!
//! This lens fetches nothing and reads no clock. Sequence values carry only
//! ledger provenance and ordering; callers provide every fact needed to build
//! a frame. Districts stack vertically, stations run horizontally, and every
//! visible agent occupies the same two cells used by hit testing.

use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::rc::Rc;
use std::time::Duration;

use gwk_domain::ids::{Seq, Timestamp};
use gwk_theme::marks::{GlyphSet, Mark};
use gwk_theme::tier::ColorTier;
use ratatui::buffer::Buffer;
use ratatui::layout::{Offset, Position, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::symbols::Marker;
use ratatui::text::Span;
use ratatui::widgets::Widget;
use ratatui::widgets::canvas::Canvas;
use tachyonfx::fx::RepeatMode;
use tachyonfx::{EffectManager, fx};
use unicode_width::UnicodeWidthStr;

use crate::input::HitMap;
use crate::theme;

const VIEW_ID_BUDGET: usize = 64;
const STATION_GUTTER: u16 = 2;
const EXPANDED_DISTRICT_HEIGHT: u16 = 3;

/// One full eight-frame expression cycle at the ruled console cadence.
pub const TICK_CYCLE: Duration = crate::input::TICK.saturating_mul(8);
/// The attention reorder movement boundary.
pub const PULSE_DURATION: Duration = Duration::from_millis(400);
/// The character-domain departure boundary.
pub const DECAY_DURATION: Duration = Duration::from_millis(600);

/// Why a normalized view identifier cannot enter the frame.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ViewIdError {
    Empty(&'static str),
    WrongNamespace {
        expected: &'static str,
        value: String,
    },
    TooLong {
        namespace: &'static str,
        length: usize,
    },
    Unretypable {
        namespace: &'static str,
        character: char,
    },
}

impl fmt::Display for ViewIdError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ViewIdError::Empty(namespace) => write!(formatter, "{namespace} id is empty"),
            ViewIdError::WrongNamespace { expected, value } => {
                write!(formatter, "{value:?} is not in the {expected} namespace")
            }
            ViewIdError::TooLong { namespace, length } => write!(
                formatter,
                "{namespace} id is {length} characters (budget {VIEW_ID_BUDGET})"
            ),
            ViewIdError::Unretypable {
                namespace,
                character,
            } => write!(
                formatter,
                "{namespace} id contains unretypable character {character:?}"
            ),
        }
    }
}

impl std::error::Error for ViewIdError {}

macro_rules! view_id {
    ($name:ident, $namespace:literal) => {
        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
        pub struct $name(String);

        impl $name {
            pub fn new(value: impl Into<String>) -> Result<Self, ViewIdError> {
                let value = value.into();
                if value.is_empty() {
                    return Err(ViewIdError::Empty($namespace));
                }
                if !value.starts_with(concat!($namespace, "-")) {
                    return Err(ViewIdError::WrongNamespace {
                        expected: $namespace,
                        value,
                    });
                }
                if let Some(character) = value
                    .chars()
                    .find(|character| !character.is_ascii_graphic())
                {
                    return Err(ViewIdError::Unretypable {
                        namespace: $namespace,
                        character,
                    });
                }
                if value.len() > VIEW_ID_BUDGET {
                    return Err(ViewIdError::TooLong {
                        namespace: $namespace,
                        length: value.len(),
                    });
                }
                Ok(Self(value))
            }

            pub fn as_str(&self) -> &str {
                &self.0
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str(&self.0)
            }
        }
    };
}

view_id!(DistrictId, "district");
view_id!(StationId, "station");
view_id!(AgentId, "agent");
view_id!(AttentionId, "attention");

/// The normalized state vocabulary that selects the right expression mark.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentState {
    Idle,
    Queued,
    Starting,
    Running,
    Canceling,
    NeedsAttention,
    Blocked,
    Failed,
    Done,
    Canceled,
    Unknown,
}

impl AgentState {
    fn binding_name(self) -> &'static str {
        match self {
            AgentState::Idle => "idle",
            AgentState::Queued => "queued",
            AgentState::Starting => "starting",
            AgentState::Running => "running",
            AgentState::Canceling => "canceling",
            AgentState::NeedsAttention => "needs_attention",
            AgentState::Blocked => "blocked",
            AgentState::Failed => "failed",
            AgentState::Done => "done",
            AgentState::Canceled => "canceled",
            AgentState::Unknown => "unknown",
        }
    }
}

/// One two-cell agent slot. `changed_seq` is provenance, never elapsed time.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Agent {
    pub id: AgentId,
    pub role: Option<String>,
    pub state: AgentState,
    /// The engine-reported runtime start. Elapsed labels are derived at the
    /// render boundary; sequence values never stand in for time.
    pub started_at: Option<Timestamp>,
    /// Caller-formatted standing liveness text, visible even with motion off.
    pub duration: Option<String>,
    pub changed_seq: Seq,
}

/// One horizontal station in a district.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Station {
    pub id: StationId,
    pub label: String,
    pub template_ordinal: u16,
    pub agents: Vec<Agent>,
    pub changed_seq: Seq,
}

/// One vertical district in the estate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct District {
    pub id: DistrictId,
    pub label: String,
    pub stations: Vec<Station>,
    /// Terminal-state agents older than the caller's aging boundary, collapsed
    /// out of the agent slots into one heading count.
    pub aged_done: usize,
    pub changed_seq: Seq,
}

/// A mutable focus fact supplied by the caller.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Focus {
    pub district: DistrictId,
    pub changed_seq: Seq,
}

/// One attention fact and the sequence that raised or resolved it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attention {
    pub id: AttentionId,
    pub district: DistrictId,
    pub unresolved: bool,
    pub changed_seq: Seq,
}

/// Everything needed to render one deterministic estate frame.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FrameInput {
    pub districts: Vec<District>,
    pub focus: Option<Focus>,
    pub attention: Vec<Attention>,
    pub watermark: Option<Seq>,
}

/// The target represented by one painted agent pair.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HallTarget {
    Agent(AgentId),
}

/// The caller-resolved motion posture for this frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MotionMode {
    Off,
    Reduced,
    Full,
}

/// The three ratified motion verbs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MotionVerb {
    Tick,
    Pulse,
    Decay,
}

/// A typed namespace for the ledger entity that owns an effect.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MotionEntity {
    Agent(AgentId),
    District(DistrictId),
    Attention(AttentionId),
}

/// Effect identity: entity plus the exact ledger change that triggered it.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MotionKey {
    pub entity: MotionEntity,
    pub changed_seq: Seq,
}

// tachyonfx 0.25.1's derived EffectManager default unnecessarily carries a
// K: Default bound. The manager never reads this value; real effects always
// enter through add_unique_effect with a caller-provided provenance key.
impl Default for MotionKey {
    fn default() -> Self {
        Self {
            entity: MotionEntity::Agent(AgentId("agent-effect-manager".into())),
            changed_seq: Seq::new(0),
        }
    }
}

/// One caller-timed effect input. Sequence is provenance only; both time axes
/// are explicit durations and are never derived from sequence subtraction.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MotionInput {
    pub key: MotionKey,
    pub verb: MotionVerb,
    pub elapsed: Duration,
    pub frame_delta: Duration,
    pub source: Rect,
    pub target: Rect,
}

/// The stateful effects driver and active inputs for one rendered frame.
pub struct MotionFrame<'a> {
    pub driver: &'a mut MotionDriver,
    pub inputs: &'a [MotionInput],
}

/// Motion inputs plus caller-owned page cursors for one rendered frame.
pub struct PagedMotionFrame<'a> {
    pub pages: &'a PagingState,
    pub motion: MotionFrame<'a>,
}

/// The literal density rung selected for the frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DensityRung {
    Empty,
    Baseline,
    GutterShrink,
    DistrictCollapse,
    PairShrink,
    Paging,
}

/// A density decision, kept separate from painting for direct verification.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DensityPlan {
    pub rung: DensityRung,
    pub collapsed: Vec<DistrictId>,
    pub paging: usize,
}

/// Caller-owned page cursors. Each district cycles its agents independently.
/// The district cursor rotates every visible district so even an urgent set
/// taller than the viewport remains reachable; page zero stays attention-first.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PagingState {
    district_page: usize,
    agent_pages: BTreeMap<DistrictId, usize>,
}

impl PagingState {
    pub fn next(&mut self, district: &DistrictId) {
        let page = self.agent_pages.entry(district.clone()).or_default();
        *page = page.saturating_add(1);
    }

    pub fn previous(&mut self, district: &DistrictId) {
        let page = self.agent_pages.entry(district.clone()).or_default();
        *page = page.saturating_sub(1);
    }

    pub fn next_district(&mut self) {
        self.district_page = self.district_page.saturating_add(1);
    }

    pub fn previous_district(&mut self) {
        self.district_page = self.district_page.saturating_sub(1);
    }

    fn agent_page(&self, district: &DistrictId) -> usize {
        self.agent_pages.get(district).copied().unwrap_or_default()
    }
}

fn active(district: &District) -> bool {
    district.aged_done > 0
        || district
            .stations
            .iter()
            .any(|station| !station.agents.is_empty())
}

fn unresolved_districts(input: &FrameInput) -> BTreeSet<&str> {
    input
        .attention
        .iter()
        .filter(|attention| attention.unresolved)
        .map(|attention| attention.district.as_str())
        .collect()
}

fn unresolved_attention_count(input: &FrameInput, district: &DistrictId) -> usize {
    input
        .attention
        .iter()
        .filter(|attention| attention.unresolved && attention.district == *district)
        .count()
}

/// District stack order: attention first, then the typed id byte order.
pub fn district_stack_order(input: &FrameInput) -> Vec<DistrictId> {
    let pinned = unresolved_districts(input);
    let mut districts: Vec<&District> = input.districts.iter().collect();
    districts.sort_by(|left, right| {
        (!pinned.contains(left.id.as_str()))
            .cmp(&(!pinned.contains(right.id.as_str())))
            .then_with(|| left.id.cmp(&right.id))
    });
    districts
        .into_iter()
        .map(|district| district.id.clone())
        .collect()
}

/// Independent collapse order: oldest change first, excluding focus and pins.
pub fn collapse_order(input: &FrameInput) -> Vec<DistrictId> {
    let pinned = unresolved_districts(input);
    let focused = input.focus.as_ref().map(|focus| focus.district.as_str());
    let mut districts: Vec<&District> = input
        .districts
        .iter()
        .filter(|district| active(district))
        .filter(|district| Some(district.id.as_str()) != focused)
        .filter(|district| !pinned.contains(district.id.as_str()))
        .collect();
    districts.sort_by(|left, right| {
        left.changed_seq
            .cmp(&right.changed_seq)
            .then_with(|| left.id.cmp(&right.id))
    });
    districts
        .into_iter()
        .map(|district| district.id.clone())
        .collect()
}

/// Station order: template ordinal, then typed id.
pub fn station_order(district: &District) -> Vec<StationId> {
    let mut stations: Vec<&Station> = district.stations.iter().collect();
    stations.sort_by(|left, right| {
        left.template_ordinal
            .cmp(&right.template_ordinal)
            .then_with(|| left.id.cmp(&right.id))
    });
    stations
        .into_iter()
        .map(|station| station.id.clone())
        .collect()
}

/// Agent slot order: typed id byte order.
pub fn agent_order(station: &Station) -> Vec<AgentId> {
    let mut agents: Vec<&Agent> = station.agents.iter().collect();
    agents.sort_by(|left, right| left.id.cmp(&right.id));
    agents.into_iter().map(|agent| agent.id.clone()).collect()
}

fn ordered_visible_districts(input: &FrameInput) -> Vec<&District> {
    let pinned = unresolved_districts(input);
    let mut districts: Vec<&District> = input
        .districts
        .iter()
        .filter(|district| active(district) || pinned.contains(district.id.as_str()))
        .collect();
    districts.sort_by(|left, right| {
        (!pinned.contains(left.id.as_str()))
            .cmp(&(!pinned.contains(right.id.as_str())))
            .then_with(|| left.id.cmp(&right.id))
    });
    districts
}

fn ordered_stations(district: &District) -> Vec<&Station> {
    let mut stations: Vec<&Station> = district
        .stations
        .iter()
        .filter(|station| !station.agents.is_empty())
        .collect();
    stations.sort_by(|left, right| {
        left.template_ordinal
            .cmp(&right.template_ordinal)
            .then_with(|| left.id.cmp(&right.id))
    });
    stations
}

fn ordered_agents(station: &Station) -> Vec<&Agent> {
    let mut agents: Vec<&Agent> = station.agents.iter().collect();
    agents.sort_by(|left, right| left.id.cmp(&right.id));
    agents
}

fn safe_width(text: &str, budget: u16) -> u16 {
    let safe = theme::safe_text(text, usize::from(budget));
    u16::try_from(UnicodeWidthStr::width(safe.as_ref())).unwrap_or(u16::MAX)
}

fn agent_slot_width(agent: &Agent, budget: u16, pair_width: u16) -> u16 {
    if pair_width == 1 {
        return 1;
    }
    agent.duration.as_deref().map_or(2, |duration| {
        3u16.saturating_add(safe_width(duration, budget.saturating_sub(3)))
    })
}

fn agent_row_width(station: &Station, gutter: u16, pair_width: u16, budget: u16) -> u16 {
    let agents = ordered_agents(station);
    let slots = agents
        .iter()
        .map(|agent| agent_slot_width(agent, budget, pair_width))
        .fold(0u16, u16::saturating_add);
    slots.saturating_add(
        u16::try_from(agents.len().saturating_sub(1))
            .unwrap_or(u16::MAX)
            .saturating_mul(gutter),
    )
}

fn station_width(station: &Station, gutter: u16, pair_width: u16, budget: u16) -> u16 {
    safe_width(&station.label, budget).max(agent_row_width(station, gutter, pair_width, budget))
}

fn collapsed_text(district: &District, budget: u16) -> String {
    let agents = district
        .stations
        .iter()
        .map(|station| station.agents.len())
        .sum::<usize>();
    let suffix = format!("  {agents}");
    let label_budget = budget.saturating_sub(u16::try_from(2 + suffix.len()).unwrap_or(u16::MAX));
    let label = theme::safe_text(&district.label, usize::from(label_budget));
    format!("+ {label}{suffix}")
}

fn district_heading(district: &District, input: &FrameInput, budget: u16) -> String {
    let attention = unresolved_attention_count(input, &district.id);
    if district.aged_done == 0 {
        return attention_text(&district.label, attention, budget);
    }
    let suffix = format!(" +{} done", district.aged_done);
    let suffix_width = safe_width(&suffix, budget);
    if suffix_width >= budget {
        return attention_text(&district.label, attention, budget);
    }
    let head = attention_text(
        &district.label,
        attention,
        budget.saturating_sub(suffix_width),
    );
    format!("{head}{suffix}")
}

fn attention_text(label: &str, attention: usize, budget: u16) -> String {
    if attention == 0 {
        return bounded(label, budget);
    }
    let cue = format!("!{attention}");
    let cue_width = safe_width(&cue, budget);
    if cue_width >= budget {
        return bounded(&cue, budget);
    }
    let label = bounded(label, budget.saturating_sub(cue_width.saturating_add(1)));
    if label.is_empty() {
        bounded(&cue, budget)
    } else {
        format!("{label} {cue}")
    }
}

fn district_width(
    district: &District,
    input: &FrameInput,
    gutter: u16,
    pair_width: u16,
    collapsed: bool,
    budget: u16,
) -> u16 {
    if collapsed {
        return safe_width(&collapsed_text(district, budget), budget);
    }
    let stations = ordered_stations(district);
    let station_widths = stations
        .iter()
        .map(|station| station_width(station, gutter, pair_width, budget))
        .fold(0u16, u16::saturating_add);
    let heading_budget = if unresolved_attention_count(input, &district.id) == 0 {
        budget
    } else {
        budget.saturating_sub(1)
    };
    safe_width(
        &district_heading(district, input, heading_budget),
        heading_budget,
    )
    .max(
        station_widths.saturating_add(
            u16::try_from(stations.len().saturating_sub(1))
                .unwrap_or(u16::MAX)
                .saturating_mul(STATION_GUTTER),
        ),
    )
}

fn fits(
    area: Rect,
    input: &FrameInput,
    districts: &[&District],
    gutter: u16,
    pair_width: u16,
    collapsed: &BTreeSet<String>,
) -> bool {
    if area.width == 0 || area.height == 0 {
        return false;
    }
    let measure_budget = area.width.saturating_add(1);
    let height = districts.iter().fold(0u16, |height, district| {
        height.saturating_add(if collapsed.contains(district.id.as_str()) {
            1
        } else {
            EXPANDED_DISTRICT_HEIGHT
        })
    });
    height <= area.height
        && districts.iter().all(|district| {
            district_width(
                district,
                input,
                gutter,
                pair_width,
                collapsed.contains(district.id.as_str()),
                measure_budget,
            ) <= area.width
        })
}

fn paging_count(
    area: Rect,
    input: &FrameInput,
    districts: &[&District],
    gutter: u16,
    pair_width: u16,
    collapsed: &BTreeSet<String>,
) -> usize {
    let mut used_height = 0u16;
    let mut visible = 0usize;
    let measure_budget = area.width.saturating_add(1);
    for district in districts {
        let is_collapsed = collapsed.contains(district.id.as_str());
        let height = if is_collapsed {
            1
        } else {
            EXPANDED_DISTRICT_HEIGHT
        };
        let fits_width = district_width(
            district,
            input,
            gutter,
            pair_width,
            is_collapsed,
            measure_budget,
        ) <= area.width;
        let fits_height = used_height.saturating_add(height) <= area.height;
        if !fits_width || !fits_height {
            break;
        }
        used_height = used_height.saturating_add(height);
        visible += 1;
    }
    districts.len().saturating_sub(visible).max(1)
}

/// Walk the fixed density ladder: baseline, gutter shrink, collapses, one-cell
/// expression glyphs, then paging.
///
/// This ladder compacts Hall layout only; it does not raise the hosted-screen
/// transport ceiling. A full screen is practically limited to about 13.8k
/// cells by the 2 MiB publish budget at roughly 152 serialized bytes per cell,
/// until the wire gains a compacter cell serialization.
pub fn solve_density(area: Rect, input: &FrameInput) -> DensityPlan {
    let districts = ordered_visible_districts(input);
    if districts.is_empty() {
        return DensityPlan {
            rung: DensityRung::Empty,
            collapsed: Vec::new(),
            paging: 0,
        };
    }

    let mut collapsed = BTreeSet::new();
    if fits(area, input, &districts, 1, 2, &collapsed) {
        return DensityPlan {
            rung: DensityRung::Baseline,
            collapsed: Vec::new(),
            paging: 0,
        };
    }
    if fits(area, input, &districts, 0, 2, &collapsed) {
        return DensityPlan {
            rung: DensityRung::GutterShrink,
            collapsed: Vec::new(),
            paging: 0,
        };
    }

    let candidates = collapse_order(input);
    for candidate in &candidates {
        collapsed.insert(candidate.as_str().to_owned());
        if fits(area, input, &districts, 0, 2, &collapsed) {
            return DensityPlan {
                rung: DensityRung::DistrictCollapse,
                collapsed: candidates
                    .iter()
                    .take_while(|id| id.as_str() != candidate.as_str())
                    .cloned()
                    .chain(std::iter::once(candidate.clone()))
                    .collect(),
                paging: 0,
            };
        }
    }

    if fits(area, input, &districts, 0, 1, &collapsed) {
        return DensityPlan {
            rung: DensityRung::PairShrink,
            collapsed: candidates,
            paging: 0,
        };
    }

    DensityPlan {
        rung: DensityRung::Paging,
        collapsed: candidates,
        paging: paging_count(area, input, &districts, 0, 1, &collapsed),
    }
}

/// Full deterministic keyboard order, including agents hidden by density.
pub fn target_order(input: &FrameInput) -> Vec<HallTarget> {
    let mut targets = Vec::new();
    for district in ordered_visible_districts(input) {
        for station in ordered_stations(district) {
            targets.extend(
                ordered_agents(station)
                    .into_iter()
                    .map(|agent| HallTarget::Agent(agent.id.clone())),
            );
        }
    }
    targets
}

#[derive(Debug)]
struct EffectSlot {
    manager: EffectManager<MotionKey>,
    verb: MotionVerb,
    elapsed: Duration,
}

#[derive(Clone, Copy)]
struct MotionGeometry<'a> {
    lens: Rect,
    agent_regions: &'a BTreeMap<AgentId, Rect>,
    hits: &'a HitMap<HallTarget>,
}

impl EffectSlot {
    fn new(verb: MotionVerb) -> Self {
        Self {
            manager: EffectManager::default(),
            verb,
            elapsed: Duration::ZERO,
        }
    }
}

/// Frame-scoped tachyonfx managers keyed by entity plus ledger provenance.
#[derive(Debug)]
pub struct MotionDriver {
    mode: MotionMode,
    slots: BTreeMap<MotionKey, EffectSlot>,
}

impl MotionDriver {
    pub fn new(mode: MotionMode) -> Self {
        Self {
            mode,
            slots: BTreeMap::new(),
        }
    }

    pub fn mode(&self) -> MotionMode {
        self.mode
    }

    pub fn set_mode(&mut self, mode: MotionMode) {
        self.mode = mode;
        if mode == MotionMode::Off {
            self.slots.clear();
        }
    }

    /// Active PULSE/DECAY keys after same-key replacement.
    pub fn active_one_shots(&self) -> usize {
        self.slots
            .values()
            .filter(|slot| match slot.verb {
                MotionVerb::Tick => false,
                MotionVerb::Pulse => slot.elapsed < PULSE_DURATION,
                MotionVerb::Decay => slot.elapsed < DECAY_DURATION,
            })
            .count()
    }

    fn allows(&self, verb: MotionVerb) -> bool {
        match self.mode {
            MotionMode::Off => false,
            MotionMode::Reduced => verb == MotionVerb::Tick,
            MotionMode::Full => true,
        }
    }

    fn admits(&self, motion: &MotionInput) -> bool {
        self.allows(motion.verb)
            && (motion.verb == MotionVerb::Pulse
                || matches!(&motion.key.entity, MotionEntity::Agent(_)))
    }

    fn begin_frame(&mut self, motions: &[MotionInput]) {
        if self.mode == MotionMode::Off {
            self.slots.clear();
            return;
        }
        let active: BTreeSet<MotionKey> = motions
            .iter()
            .filter(|motion| self.admits(motion))
            .map(|motion| motion.key.clone())
            .collect();
        self.slots.retain(|key, _| active.contains(key));
    }

    fn active_inputs(&self, motions: &[MotionInput]) -> Vec<MotionInput> {
        let mut latest = BTreeMap::new();
        for motion in motions.iter().filter(|motion| self.admits(motion)) {
            latest.insert(motion.key.clone(), motion.clone());
        }
        latest.into_values().collect()
    }

    fn slot(&mut self, motion: &MotionInput) -> &mut EffectSlot {
        let slot = self
            .slots
            .entry(motion.key.clone())
            .or_insert_with(|| EffectSlot::new(motion.verb));
        // Absolute elapsed reconstructs the shader each frame; retaining it would
        // let tachyonfx process a cancelled effect once at stale geometry.
        slot.manager = EffectManager::default();
        slot.verb = motion.verb;
        slot.elapsed = motion.elapsed;
        slot
    }

    fn prepare_pulses(
        &mut self,
        motions: &[MotionInput],
        buf: &mut Buffer,
        lens: Rect,
    ) -> Vec<PulseTransform> {
        let mut transforms = Vec::new();
        for motion in motions {
            if motion.verb != MotionVerb::Pulse || !self.allows(motion.verb) {
                continue;
            }
            let current = Rc::new(RefCell::new(None));
            let probe = Rc::clone(&current);
            let inner = fx::effect_fn((), PULSE_DURATION, move |_state, context, _cells| {
                *probe.borrow_mut() = Some(context.area)
            });
            let offset = Offset {
                x: i32::from(motion.target.x) - i32::from(motion.source.x),
                y: i32::from(motion.target.y) - i32::from(motion.source.y),
            };
            let mut effect = fx::translate(inner, offset, PULSE_DURATION).with_area(motion.source);
            let (warm, delta) = split_elapsed(motion, PULSE_DURATION);
            if !warm.is_zero() {
                let mut scratch = Buffer::empty(buf.area);
                effect.process(warm, &mut scratch, motion.source);
            }
            let slot = self.slot(motion);
            slot.manager.add_unique_effect(motion.key.clone(), effect);
            slot.manager.process_effects(delta, buf, lens);
            let translated = *current.borrow();
            transforms.push(PulseTransform {
                target: motion.target,
                current: translated,
            });
        }
        transforms
    }

    fn apply_characters(
        &mut self,
        motions: &[MotionInput],
        frame: &FrameInput,
        glyphs: GlyphSet,
        buf: &mut Buffer,
        geometry: MotionGeometry<'_>,
    ) {
        for motion in motions {
            if !matches!(motion.verb, MotionVerb::Tick | MotionVerb::Decay)
                || !self.allows(motion.verb)
            {
                continue;
            }
            let MotionEntity::Agent(id) = &motion.key.entity else {
                continue;
            };
            let Some(area) = agent_expression_area(id, geometry.agent_regions, geometry.hits)
            else {
                continue;
            };
            match motion.verb {
                MotionVerb::Tick => {
                    let mark = tick_mark(frame, id);
                    let delta = motion.frame_delta.min(motion.elapsed);
                    let state = TickState {
                        elapsed: motion.elapsed.saturating_sub(delta),
                        mark,
                        glyphs,
                    };
                    let effect = fx::repeat(
                        fx::effect_fn(state, TICK_CYCLE, |state, context, cells| {
                            state.elapsed = state.elapsed.saturating_add(context.last_tick);
                            let tick_ms = crate::input::TICK.as_millis();
                            let frame = usize::try_from((state.elapsed.as_millis() / tick_ms) % 8)
                                .unwrap_or_default();
                            let glyph = theme::glyph(state.mark, frame, state.glyphs);
                            for (_, cell) in cells {
                                cell.set_char(glyph);
                            }
                        })
                        .with_area(area),
                        RepeatMode::Forever,
                    );
                    let slot = self.slot(motion);
                    slot.manager.add_unique_effect(motion.key.clone(), effect);
                    slot.manager.process_effects(delta, buf, geometry.lens);
                }
                MotionVerb::Decay => {
                    let mut effect = fx::effect_fn((), DECAY_DURATION, |_state, context, cells| {
                        for (_, cell) in cells {
                            if context.alpha() >= 1.0 {
                                cell.set_char(' ');
                            } else if context.alpha() >= 0.5 {
                                cell.set_char('.');
                            }
                        }
                    })
                    .with_area(area);
                    let (warm, delta) = split_elapsed(motion, DECAY_DURATION);
                    if !warm.is_zero() {
                        let mut scratch = Buffer::empty(buf.area);
                        effect.process(warm, &mut scratch, area);
                    }
                    let slot = self.slot(motion);
                    slot.manager.add_unique_effect(motion.key.clone(), effect);
                    slot.manager.process_effects(delta, buf, geometry.lens);
                }
                MotionVerb::Pulse => {}
            }
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct PulseTransform {
    target: Rect,
    current: Option<Rect>,
}

#[derive(Clone)]
struct TickState {
    elapsed: Duration,
    mark: &'static Mark,
    glyphs: GlyphSet,
}

fn split_elapsed(motion: &MotionInput, boundary: Duration) -> (Duration, Duration) {
    let elapsed = motion.elapsed.min(boundary);
    let mut delta = motion.frame_delta.min(elapsed);
    if delta.is_zero() && !elapsed.is_zero() {
        delta = crate::input::TICK.min(elapsed);
    }
    (elapsed.saturating_sub(delta), delta)
}

fn agent_expression_area(
    id: &AgentId,
    regions: &BTreeMap<AgentId, Rect>,
    hits: &HitMap<HallTarget>,
) -> Option<Rect> {
    let pair = *regions.get(id)?;
    if !matches!(pair.width, 1 | 2) || pair.height != 1 {
        return None;
    }
    let owns_pair = (pair.x..pair.right())
        .all(|x| matches!(hits.hit(x, pair.y), Some(HallTarget::Agent(owner)) if owner == id));
    owns_pair.then(|| {
        Rect::new(
            pair.x.saturating_add(pair.width.saturating_sub(1)),
            pair.y,
            1,
            1,
        )
    })
}

fn tick_mark(frame: &FrameInput, id: &AgentId) -> &'static Mark {
    let state = frame
        .districts
        .iter()
        .flat_map(|district| &district.stations)
        .flat_map(|station| &station.agents)
        .find(|agent| agent.id == *id)
        .map_or(AgentState::Unknown, |agent| agent.state);
    expression_mark(state)
}

#[derive(Debug, Clone)]
struct CanvasText {
    x: u16,
    y: u16,
    text: String,
    style: Style,
    agent_pair: bool,
}

#[derive(Clone, Copy)]
struct PaintContext<'a> {
    tier: ColorTier,
    glyphs: GlyphSet,
    pages: &'a PagingState,
    transforms: &'a [PulseTransform],
}

fn intersect_rect(left: Rect, right: Rect) -> Option<Rect> {
    let x = left.x.max(right.x);
    let y = left.y.max(right.y);
    let right_edge = left.right().min(right.right());
    let bottom_edge = left.bottom().min(right.bottom());
    (right_edge > x && bottom_edge > y).then(|| Rect::new(x, y, right_edge - x, bottom_edge - y))
}

fn rect_inside(inner: Rect, outer: Rect) -> bool {
    inner.width > 0
        && inner.height > 0
        && inner.x >= outer.x
        && inner.y >= outer.y
        && inner.right() <= outer.right()
        && inner.bottom() <= outer.bottom()
}

fn shift_rect(rect: Rect, dx: i32, dy: i32) -> Option<Rect> {
    let x = i32::from(rect.x).checked_add(dx)?;
    let y = i32::from(rect.y).checked_add(dy)?;
    Some(Rect::new(
        u16::try_from(x).ok()?,
        u16::try_from(y).ok()?,
        rect.width,
        rect.height,
    ))
}

fn pulse_for(position: Position, transforms: &[PulseTransform]) -> Option<PulseTransform> {
    transforms
        .iter()
        .rev()
        .find(|transform| transform.target.contains(position))
        .copied()
}

fn transform_text(
    area: Rect,
    item: &CanvasText,
    transforms: &[PulseTransform],
) -> Option<CanvasText> {
    let absolute = Position::new(area.x + item.x, area.y + item.y);
    let Some(transform) = pulse_for(absolute, transforms) else {
        return Some(item.clone());
    };
    let current = transform.current?;
    let dx = i32::from(current.x) - i32::from(transform.target.x);
    let dy = i32::from(current.y) - i32::from(transform.target.y);
    let footprint = Rect::new(absolute.x, absolute.y, u16::from(item.agent_pair) + 1, 1);
    let shifted = shift_rect(footprint, dx, dy)?;
    let visible = intersect_rect(current, area)?;
    if item.agent_pair {
        if !rect_inside(shifted, visible) || !rect_inside(shifted, area) {
            return None;
        }
    } else if !visible.contains(Position::new(shifted.x, shifted.y)) {
        return None;
    }
    Some(CanvasText {
        x: shifted.x - area.x,
        y: shifted.y - area.y,
        ..item.clone()
    })
}

fn transform_region(area: Rect, region: Rect, transforms: &[PulseTransform]) -> Option<Rect> {
    let Some(transform) = pulse_for(Position::new(region.x, region.y), transforms) else {
        return rect_inside(region, area).then_some(region);
    };
    let current = transform.current?;
    let dx = i32::from(current.x) - i32::from(transform.target.x);
    let dy = i32::from(current.y) - i32::from(transform.target.y);
    let shifted = shift_rect(region, dx, dy)?;
    let visible = intersect_rect(current, area)?;
    (rect_inside(shifted, visible) && rect_inside(shifted, area)).then_some(shifted)
}

fn paint_frame(
    area: Rect,
    buf: &mut Buffer,
    hits: &mut HitMap<HallTarget>,
    text: &[CanvasText],
    regions: &[(Rect, HallTarget)],
    transforms: &[PulseTransform],
) -> BTreeMap<AgentId, Rect> {
    let mut transformed = Vec::new();
    for moving in [false, true] {
        for item in text {
            let position = Position::new(area.x + item.x, area.y + item.y);
            if pulse_for(position, transforms).is_some() != moving {
                continue;
            }
            if let Some(item) = transform_text(area, item, transforms) {
                transformed.push(item);
            }
        }
    }
    canvas_paint(area, buf, &transformed);
    let mut visible_agents = BTreeMap::new();
    for moving in [false, true] {
        for (region, target) in regions {
            if pulse_for(Position::new(region.x, region.y), transforms).is_some() != moving {
                continue;
            }
            if let Some(region) = transform_region(area, *region, transforms) {
                hits.register(region, target.clone());
                let HallTarget::Agent(id) = target;
                visible_agents.insert(id.clone(), region);
            }
        }
    }
    visible_agents
}

fn canvas_paint(area: Rect, buf: &mut Buffer, text: &[CanvasText]) {
    if area.width == 0 || area.height == 0 {
        return;
    }
    let x_max = f64::from(area.width.saturating_sub(1).max(1));
    let y_max = f64::from(area.height.saturating_sub(1).max(1));
    Canvas::default()
        .marker(Marker::Dot)
        .x_bounds([0.0, x_max])
        .y_bounds([0.0, y_max])
        .paint(|context| {
            for item in text {
                context.print(
                    f64::from(item.x),
                    y_max - f64::from(item.y),
                    Span::styled(item.text.clone(), item.style),
                );
            }
        })
        .render(area, buf);
}

fn bounded(text: &str, budget: u16) -> String {
    theme::safe_text(text, usize::from(budget)).into_owned()
}

fn attention_rank(state: AgentState) -> u8 {
    match state {
        AgentState::NeedsAttention => 0,
        AgentState::Blocked => 1,
        AgentState::Failed => 2,
        AgentState::Running | AgentState::Canceling => 3,
        AgentState::Queued | AgentState::Starting => 4,
        AgentState::Idle | AgentState::Unknown => 5,
        AgentState::Done | AgentState::Canceled => 6,
    }
}

struct AgentPage<'a> {
    agents: Vec<&'a Agent>,
    hidden: usize,
    current: usize,
    count: usize,
}

fn agent_page<'a>(district: &'a District, width: u16, requested: usize) -> AgentPage<'a> {
    let mut agents: Vec<&Agent> = district
        .stations
        .iter()
        .flat_map(|station| &station.agents)
        .collect();
    agents.sort_by(|left, right| {
        attention_rank(left.state)
            .cmp(&attention_rank(right.state))
            .then_with(|| left.id.cmp(&right.id))
    });
    let total = agents.len();
    if total <= usize::from(width) {
        return AgentPage {
            agents,
            hidden: 0,
            current: 0,
            count: 1,
        };
    }

    let pinned = agents
        .iter()
        .take_while(|agent| agent.state == AgentState::NeedsAttention)
        .copied()
        .collect::<Vec<_>>();
    let width = usize::from(width);
    let suffix_width = format!(" +{total}").len().min(width);
    let capacity = width.saturating_sub(suffix_width).max(1);
    if pinned.len() >= capacity {
        let count = total.div_ceil(capacity).max(1);
        let current = requested % count;
        let start = current.saturating_mul(capacity);
        let visible = agents
            .iter()
            .skip(start)
            .take(capacity)
            .copied()
            .collect::<Vec<_>>();
        return AgentPage {
            hidden: total.saturating_sub(visible.len()),
            agents: visible,
            current,
            count,
        };
    }

    let rest = agents[pinned.len()..].to_vec();
    let rest_capacity = capacity.saturating_sub(pinned.len()).max(1);
    let count = rest.len().div_ceil(rest_capacity).max(1);
    let current = requested % count;
    let start = current.saturating_mul(rest_capacity);
    let mut visible = pinned;
    visible.extend(rest.iter().skip(start).take(rest_capacity).copied());
    AgentPage {
        hidden: total.saturating_sub(visible.len()),
        agents: visible,
        current,
        count,
    }
}

fn paged_districts<'a>(input: &'a FrameInput, pages: &PagingState) -> Vec<&'a District> {
    let mut districts = ordered_visible_districts(input);
    if !districts.is_empty() {
        let offset = pages.district_page % districts.len();
        districts.rotate_left(offset);
    }
    districts
}

/// The district rectangle used by non-paged motion. Dense paging has no stable
/// off-page geometry, so callers suppress movement there rather than guess.
pub fn district_region(area: Rect, input: &FrameInput, district_id: &DistrictId) -> Option<Rect> {
    let plan = solve_density(area, input);
    if matches!(plan.rung, DensityRung::Empty | DensityRung::Paging) {
        return None;
    }
    let collapsed: BTreeSet<&str> = plan.collapsed.iter().map(DistrictId::as_str).collect();
    let mut y = 0u16;
    for district in ordered_visible_districts(input) {
        let height = if collapsed.contains(district.id.as_str()) {
            1
        } else {
            EXPANDED_DISTRICT_HEIGHT
        };
        if district.id == *district_id {
            return (y.saturating_add(height) <= area.height)
                .then(|| Rect::new(area.x, area.y.saturating_add(y), area.width, height));
        }
        y = y.saturating_add(height);
    }
    None
}

/// The style a district heading paints with. The focused district carries
/// the ratified `focus` SIGNAL token patched over the bold base — accent
/// color at the color tiers, reverse video at Ansi16/Mono, the one attribute
/// every tier can express — so j/k has visible feedback at every density
/// rung instead of only steering which district survives a collapse.
fn heading_style(is_focused: bool, tier: ColorTier) -> Style {
    let base = Style::default().add_modifier(Modifier::BOLD);
    if !is_focused {
        return base;
    }
    gwk_theme::SIGNAL
        .iter()
        .find(|token| token.name == "gws_focus")
        .map_or(base, |token| base.patch(theme::token_style(token, tier)))
}

fn render_paging_frame(
    area: Rect,
    buf: &mut Buffer,
    input: &FrameInput,
    hits: &mut HitMap<HallTarget>,
    plan: &DensityPlan,
    context: PaintContext<'_>,
) -> BTreeMap<AgentId, Rect> {
    let muted = theme::state_style(theme::binding("idle"), context.tier);
    let collapsed: BTreeSet<&str> = plan.collapsed.iter().map(DistrictId::as_str).collect();
    let mut text = Vec::new();
    let mut regions = Vec::new();
    let mut y = 0u16;
    let mut hidden_districts = 0usize;
    let districts = paged_districts(input, context.pages);
    for (index, district) in districts.iter().copied().enumerate() {
        let is_collapsed = collapsed.contains(district.id.as_str());
        let height = if is_collapsed {
            1
        } else {
            EXPANDED_DISTRICT_HEIGHT
        };
        if y.saturating_add(height) > area.height {
            hidden_districts = districts.len().saturating_sub(index);
            break;
        }
        if is_collapsed {
            text.push(CanvasText {
                x: 0,
                y,
                text: collapsed_text(district, area.width),
                style: muted,
                agent_pair: false,
            });
            y = y.saturating_add(1);
            continue;
        }

        text.push(CanvasText {
            x: 0,
            y,
            text: district_heading(district, input, area.width),
            style: heading_style(
                input
                    .focus
                    .as_ref()
                    .is_some_and(|focus| focus.district == district.id),
                context.tier,
            ),
            agent_pair: false,
        });
        let page = agent_page(district, area.width, context.pages.agent_page(&district.id));
        text.push(CanvasText {
            x: 0,
            y: y + 1,
            text: bounded(
                &format!("page {}/{}", page.current.saturating_add(1), page.count),
                area.width,
            ),
            style: muted,
            agent_pair: false,
        });
        let mut x = 0u16;
        for agent in page.agents {
            let binding = theme::binding(agent.state.binding_name());
            text.push(CanvasText {
                x,
                y: y + 2,
                text: theme::glyph(expression_mark(agent.state), 0, context.glyphs).to_string(),
                style: theme::state_style(binding, context.tier),
                agent_pair: false,
            });
            regions.push((
                Rect::new(area.x + x, area.y + y + 2, 1, 1),
                HallTarget::Agent(agent.id.clone()),
            ));
            x = x.saturating_add(1);
        }
        if page.hidden > 0 && x < area.width {
            text.push(CanvasText {
                x,
                y: y + 2,
                text: bounded(&format!(" +{}", page.hidden), area.width.saturating_sub(x)),
                style: muted,
                agent_pair: false,
            });
        }
        y = y.saturating_add(EXPANDED_DISTRICT_HEIGHT);
    }

    if hidden_districts > 0 {
        let first = districts
            .first()
            .map(|district| district_heading(district, input, area.width))
            .unwrap_or_default();
        text.push(CanvasText {
            x: 0,
            // Headings and collapsed summaries own no hit targets. Replacing
            // the first one names overflow without covering an agent cell.
            y: 0,
            text: bounded(
                &format!("{first} +{hidden_districts} districts paging"),
                area.width,
            ),
            style: theme::state_style(theme::binding("needs_attention"), context.tier),
            agent_pair: false,
        });
    }
    paint_frame(area, buf, hits, &text, &regions, context.transforms)
}

fn identity_glyph(role: Option<&str>, glyphs: GlyphSet) -> char {
    let Some(name) = role else {
        let mark = gwk_theme::marks::mark("role_absent").expect("role_absent mark is pinned");
        return theme::glyph(mark, 0, glyphs);
    };
    // The wire's role vocabulary is open, and the real roster names are
    // fleet-prefixed families — `gw-code-reviewer`, `gw-security-auditor` —
    // while the ratified identity marks carry the bare family words. Strip
    // the fleet prefix, then try the whole name and its trailing family word
    // against the identity marks, so a roster role lands on its family's
    // mark instead of collapsing to a first-letter 'G' with the rest of the
    // fleet. Only identity marks qualify: a role that happens to share an
    // expression or graph-tier name is not that thing.
    let family = name.strip_prefix("gw-").unwrap_or(name);
    let identity = |candidate: &str| {
        gwk_theme::marks::mark(candidate)
            .filter(|mark| mark.kind == gwk_theme::marks::MarkKind::Identity)
    };
    let family_mark = identity(family).or_else(|| family.rsplit('-').next().and_then(identity));
    if let Some(mark) = family_mark {
        return theme::glyph(mark, 0, glyphs);
    }
    // Unrecognized names stay deterministic without collapsing: the letter
    // comes from the name AFTER the fleet prefix, so `gw-rust-pro` reads 'R'
    // where every `gw-*` role once read 'G'.
    family
        .bytes()
        .find(u8::is_ascii_alphabetic)
        .map(char::from)
        .map(|character| character.to_ascii_uppercase())
        .unwrap_or('?')
}

fn expression_mark(state: AgentState) -> &'static Mark {
    let binding = theme::binding(state.binding_name());
    gwk_theme::marks::mark(binding.mark).expect("state binding mark is pinned")
}

/// Paint one frame with every expression frozen at inventory frame zero.
pub fn render(
    area: Rect,
    buf: &mut Buffer,
    input: &FrameInput,
    tier: ColorTier,
    glyphs: GlyphSet,
    hits: &mut HitMap<HallTarget>,
) {
    let pages = PagingState::default();
    render_frame(
        area,
        buf,
        input,
        hits,
        PaintContext {
            tier,
            glyphs,
            pages: &pages,
            transforms: &[],
        },
    );
}

/// Paint one frame with caller-owned page cursors and no motion.
pub fn render_with_pages(
    area: Rect,
    buf: &mut Buffer,
    input: &FrameInput,
    tier: ColorTier,
    glyphs: GlyphSet,
    hits: &mut HitMap<HallTarget>,
    pages: &PagingState,
) {
    render_frame(
        area,
        buf,
        input,
        hits,
        PaintContext {
            tier,
            glyphs,
            pages,
            transforms: &[],
        },
    );
}

/// Paint one frame with caller-timed tachyonfx motion.
pub fn render_with_motion(
    area: Rect,
    buf: &mut Buffer,
    input: &FrameInput,
    tier: ColorTier,
    glyphs: GlyphSet,
    hits: &mut HitMap<HallTarget>,
    motion: MotionFrame<'_>,
) {
    let pages = PagingState::default();
    render_with_motion_and_pages(
        area,
        buf,
        input,
        tier,
        glyphs,
        hits,
        PagedMotionFrame {
            pages: &pages,
            motion,
        },
    );
}

/// Paint one caller-paged frame with caller-timed tachyonfx motion.
pub fn render_with_motion_and_pages(
    area: Rect,
    buf: &mut Buffer,
    input: &FrameInput,
    tier: ColorTier,
    glyphs: GlyphSet,
    hits: &mut HitMap<HallTarget>,
    paged: PagedMotionFrame<'_>,
) {
    let PagedMotionFrame { pages, motion } = paged;
    let active = motion.driver.active_inputs(motion.inputs);
    motion.driver.begin_frame(&active);
    let transforms = motion.driver.prepare_pulses(&active, buf, area);
    let agent_regions = render_frame(
        area,
        buf,
        input,
        hits,
        PaintContext {
            tier,
            glyphs,
            pages,
            transforms: &transforms,
        },
    );
    motion.driver.apply_characters(
        &active,
        input,
        glyphs,
        buf,
        MotionGeometry {
            lens: area,
            agent_regions: &agent_regions,
            hits,
        },
    );
}

fn render_frame(
    area: Rect,
    buf: &mut Buffer,
    input: &FrameInput,
    hits: &mut HitMap<HallTarget>,
    context: PaintContext<'_>,
) -> BTreeMap<AgentId, Rect> {
    hits.clear();
    if area.width == 0 || area.height == 0 {
        return BTreeMap::new();
    }

    let plan = solve_density(area, input);
    let muted = theme::state_style(theme::binding("idle"), context.tier);
    let mut text = Vec::new();
    let mut regions = Vec::new();
    if plan.rung == DensityRung::Empty {
        let first_y = area.height.saturating_sub(2) / 2;
        let x = u16::from(area.width > 2);
        text.push(CanvasText {
            x,
            y: first_y,
            text: bounded("No active work yet", area.width.saturating_sub(x)),
            style: Style::default().add_modifier(Modifier::BOLD),
            agent_pair: false,
        });
        if first_y + 1 < area.height {
            text.push(CanvasText {
                x,
                y: first_y + 1,
                text: bounded(
                    "Projects and running agents appear when the ledger records them.",
                    area.width.saturating_sub(x),
                ),
                style: muted,
                agent_pair: false,
            });
        }
        return paint_frame(area, buf, hits, &text, &regions, context.transforms);
    }

    if plan.rung == DensityRung::Paging {
        return render_paging_frame(area, buf, input, hits, &plan, context);
    }

    let gutter = u16::from(plan.rung == DensityRung::Baseline);
    let pair_width = u16::from(plan.rung != DensityRung::PairShrink) + 1;
    let collapsed: BTreeSet<&str> = plan.collapsed.iter().map(DistrictId::as_str).collect();
    let mut y = 0u16;
    for district in ordered_visible_districts(input) {
        if collapsed.contains(district.id.as_str()) {
            text.push(CanvasText {
                x: 0,
                y,
                text: collapsed_text(district, area.width),
                style: muted,
                agent_pair: false,
            });
            y = y.saturating_add(1);
            continue;
        }

        text.push(CanvasText {
            x: 0,
            y,
            text: district_heading(district, input, area.width),
            style: heading_style(
                input
                    .focus
                    .as_ref()
                    .is_some_and(|focus| focus.district == district.id),
                context.tier,
            ),
            agent_pair: false,
        });
        let mut station_x = 0u16;
        for station in ordered_stations(district) {
            let width = station_width(station, gutter, pair_width, area.width);
            text.push(CanvasText {
                x: station_x,
                y: y + 1,
                text: bounded(&station.label, width),
                style: muted,
                agent_pair: false,
            });
            let mut agent_x = station_x;
            for agent in ordered_agents(station) {
                let x = agent_x;
                let binding = theme::binding(agent.state.binding_name());
                let style = theme::state_style(binding, context.tier);
                let pair = if pair_width == 1 {
                    theme::glyph(expression_mark(agent.state), 0, context.glyphs).to_string()
                } else {
                    String::from_iter([
                        identity_glyph(agent.role.as_deref(), context.glyphs),
                        theme::glyph(expression_mark(agent.state), 0, context.glyphs),
                    ])
                };
                text.push(CanvasText {
                    x,
                    y: y + 2,
                    text: pair,
                    style,
                    agent_pair: pair_width == 2,
                });
                regions.push((
                    Rect::new(area.x + x, area.y + y + 2, pair_width, 1),
                    HallTarget::Agent(agent.id.clone()),
                ));
                if pair_width == 2
                    && let Some(duration) = &agent.duration
                {
                    let duration_x = x.saturating_add(3);
                    let duration_budget =
                        agent_slot_width(agent, area.width, pair_width).saturating_sub(3);
                    text.push(CanvasText {
                        x: duration_x,
                        y: y + 2,
                        text: bounded(duration, duration_budget),
                        style: muted,
                        agent_pair: false,
                    });
                }
                agent_x = agent_x
                    .saturating_add(agent_slot_width(agent, area.width, pair_width))
                    .saturating_add(gutter);
            }
            station_x = station_x
                .saturating_add(width)
                .saturating_add(STATION_GUTTER);
        }
        y = y.saturating_add(EXPANDED_DISTRICT_HEIGHT);
    }
    paint_frame(area, buf, hits, &text, &regions, context.transforms)
}

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

    #[test]
    fn roster_roles_resolve_family_marks_instead_of_collapsing() {
        // Fleet-prefixed roster names land on their family's identity mark at
        // both tiers, exactly as the bare family words always have.
        assert_eq!(identity_glyph(Some("reviewer"), GlyphSet::Ascii), 'R');
        assert_eq!(
            identity_glyph(Some("gw-code-reviewer"), GlyphSet::Ascii),
            'R'
        );
        assert_eq!(
            identity_glyph(Some("gw-code-reviewer"), GlyphSet::Unicode),
            '◃'
        );
        assert_eq!(
            identity_glyph(Some("gw-security-auditor"), GlyphSet::Ascii),
            'U'
        );
        assert_eq!(
            identity_glyph(Some("gw-phase-researcher"), GlyphSet::Ascii),
            'S'
        );
        assert_eq!(identity_glyph(Some("gw-architect"), GlyphSet::Ascii), 'A');
        assert_eq!(
            identity_glyph(Some("gw-orchestrator"), GlyphSet::Ascii),
            'O'
        );
        // Unmapped roles stay deterministic without collapsing: the letter
        // follows the name after the fleet prefix, so the fleet no longer
        // reads as a column of 'G'.
        assert_eq!(identity_glyph(Some("gw-rust-pro"), GlyphSet::Ascii), 'R');
        assert_eq!(
            identity_glyph(Some("gw-typescript-pro"), GlyphSet::Ascii),
            'T'
        );
        assert_eq!(
            identity_glyph(Some("general-purpose"), GlyphSet::Ascii),
            'G'
        );
        // A role sharing a non-identity mark's name is not that thing: no
        // graph-tier or expression glyph ever poses as a WHO cell.
        assert_eq!(identity_glyph(Some("task"), GlyphSet::Ascii), 'T');
        // Absent stays the pinned role_absent mark.
        assert_eq!(identity_glyph(None, GlyphSet::Ascii), '.');
        assert_eq!(identity_glyph(None, GlyphSet::Unicode), '∙');
    }
}