runner-manager 0.4.15

Local-first autoscaling manager for ephemeral GitHub Actions self-hosted runners, with a CLI and a Ratatui TUI.
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
// owner: b1-local-model-corpus
//
//! The pure transition function, `(model, action) -> (exit class, expected
//! observation, next model)`, and the self-check that keeps it honest.
//!
//! # The three rules of `03-coverage-model.md`, and where each is enforced
//!
//! * *"On a refusal, `next model` equals `model`."* [`check`] refuses a
//!   refusal that carries any delta, unless the transition names a
//!   [`Deviation`] — a product behaviour, recorded here on purpose, where a
//!   refused command does leave something behind. There are three; each is a
//!   finding for a human, not a licence.
//! * *"On success, only the fields owned by the action may change."* Every
//!   change is a [`Delta`], [`Delta::kind`] names the field family it touches,
//!   and [`owned`] lists the families each command leaf may touch. [`check`]
//!   replays the deltas onto the previous model and requires the result to be
//!   *exactly* the expected next model, so an unlisted change and a listed
//!   change that did not happen both fail.
//! * *"Reads never change model state."* A read with any delta fails [`check`].
//!
//! The model is computed before the real command runs, and nothing here reads
//! production state. The order of the checks inside each function mirrors the
//! order the product performs them in, because that order decides which
//! refusal a doubly-wrong command reports.

use std::collections::BTreeSet;

use super::action::{
    Action, ActionKind, AddArgs, AttemptKind, Labels, Seed, Target, TargetKey, WorkspaceSetting,
};
use super::model::{
    BUDGET_ALLOWANCE_PER_HOUR, DEFAULT_HOST_CAPACITY, HostModel, Installation, Mode, Model, Policy,
    PolicyState, Tally,
};
use super::values::{Capacity, HostLabelValue, LabelValue, PathValue, Relation, RepoName, Scope};

// ---------------------------------------------------------------------------
// Exit classes
// ---------------------------------------------------------------------------

/// The failure classes a local chain can meet, with the product's published
/// exit codes restated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Failure {
    NotAuthenticated,
    InvalidArgument,
    NotFound,
    Conflict,
    BudgetRefused,
}

impl Failure {
    #[must_use]
    pub const fn code(self) -> i32 {
        match self {
            Failure::NotAuthenticated => 3,
            Failure::InvalidArgument => 9,
            Failure::NotFound => 10,
            Failure::Conflict => 11,
            Failure::BudgetRefused => 12,
        }
    }

    #[must_use]
    pub const fn token(self) -> &'static str {
        match self {
            Failure::NotAuthenticated => "not_authenticated",
            Failure::InvalidArgument => "invalid_argument",
            Failure::NotFound => "not_found",
            Failure::Conflict => "conflict",
            Failure::BudgetRefused => "budget_refused",
        }
    }
}

/// The expected exit class.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Exit {
    Success,
    Refused(Failure),
}

impl Exit {
    #[must_use]
    pub const fn code(self) -> i32 {
        match self {
            Exit::Success => 0,
            Exit::Refused(failure) => failure.code(),
        }
    }

    #[must_use]
    pub const fn is_success(self) -> bool {
        matches!(self, Exit::Success)
    }

    #[must_use]
    pub fn describe(self) -> String {
        match self {
            Exit::Success => "ok(0)".to_string(),
            Exit::Refused(failure) => format!("{}({})", failure.token(), failure.code()),
        }
    }
}

/// Why a command is refused: the semantic refusal class, finer than the exit
/// code (several distinct refusals share `invalid_argument`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Reason {
    InvalidTarget,
    InvalidHostLabel,
    InvalidLabel,
    ZeroHostCapacity,
    ZeroMaxCapacity,
    LabelsNeedAutoscale,
    LabelsOnMonitorOnly,
    NotAuthenticated,
    AppNotInstalled,
    TargetNotInstalled,
    DuplicateTarget,
    BudgetExceeded,
    EnableMonitorOnly,
    IllegalStateTransition,
    DisableNeedsConfirmation,
    MissingPolicy,
    DerivedLabelNotRemovable,
    PurgeWithActiveAttempts,
    RelativePath,
    OverlapsApplicationData,
    OverlapsHostRoot,
    OverlapsRepositoryRoot,
    ExistingFile,
    MissingParents,
    AttemptsOwnHostRoot,
    AttemptsOwnWorkspace,
    EphemeralRejectsPath,
}

impl Reason {
    pub const ALL: [Reason; 27] = [
        Reason::InvalidTarget,
        Reason::InvalidHostLabel,
        Reason::InvalidLabel,
        Reason::ZeroHostCapacity,
        Reason::ZeroMaxCapacity,
        Reason::LabelsNeedAutoscale,
        Reason::LabelsOnMonitorOnly,
        Reason::NotAuthenticated,
        Reason::AppNotInstalled,
        Reason::TargetNotInstalled,
        Reason::DuplicateTarget,
        Reason::BudgetExceeded,
        Reason::EnableMonitorOnly,
        Reason::IllegalStateTransition,
        Reason::DisableNeedsConfirmation,
        Reason::MissingPolicy,
        Reason::DerivedLabelNotRemovable,
        Reason::PurgeWithActiveAttempts,
        Reason::RelativePath,
        Reason::OverlapsApplicationData,
        Reason::OverlapsHostRoot,
        Reason::OverlapsRepositoryRoot,
        Reason::ExistingFile,
        Reason::MissingParents,
        Reason::AttemptsOwnHostRoot,
        Reason::AttemptsOwnWorkspace,
        Reason::EphemeralRejectsPath,
    ];

    #[must_use]
    pub const fn failure(self) -> Failure {
        match self {
            Reason::NotAuthenticated => Failure::NotAuthenticated,
            Reason::AppNotInstalled | Reason::TargetNotInstalled | Reason::MissingPolicy => {
                Failure::NotFound
            }
            Reason::DuplicateTarget
            | Reason::DisableNeedsConfirmation
            | Reason::PurgeWithActiveAttempts
            | Reason::AttemptsOwnHostRoot
            | Reason::AttemptsOwnWorkspace => Failure::Conflict,
            Reason::BudgetExceeded => Failure::BudgetRefused,
            _ => Failure::InvalidArgument,
        }
    }

    #[must_use]
    pub const fn slug(self) -> &'static str {
        match self {
            Reason::InvalidTarget => "invalid-target",
            Reason::InvalidHostLabel => "invalid-host-label",
            Reason::InvalidLabel => "invalid-label",
            Reason::ZeroHostCapacity => "zero-host-capacity",
            Reason::ZeroMaxCapacity => "zero-max-capacity",
            Reason::LabelsNeedAutoscale => "labels-need-autoscale",
            Reason::LabelsOnMonitorOnly => "labels-on-monitor-only",
            Reason::NotAuthenticated => "not-authenticated",
            Reason::AppNotInstalled => "app-not-installed",
            Reason::TargetNotInstalled => "target-not-installed",
            Reason::DuplicateTarget => "duplicate-target",
            Reason::BudgetExceeded => "budget-exceeded",
            Reason::EnableMonitorOnly => "enable-monitor-only",
            Reason::IllegalStateTransition => "illegal-state-transition",
            Reason::DisableNeedsConfirmation => "disable-needs-confirmation",
            Reason::MissingPolicy => "missing-policy",
            Reason::DerivedLabelNotRemovable => "derived-label-not-removable",
            Reason::PurgeWithActiveAttempts => "purge-with-active-attempts",
            Reason::RelativePath => "relative-path",
            Reason::OverlapsApplicationData => "overlaps-application-data",
            Reason::OverlapsHostRoot => "overlaps-host-root",
            Reason::OverlapsRepositoryRoot => "overlaps-repository-root",
            Reason::ExistingFile => "existing-file",
            Reason::MissingParents => "missing-parents",
            Reason::AttemptsOwnHostRoot => "attempts-own-host-root",
            Reason::AttemptsOwnWorkspace => "attempts-own-workspace",
            Reason::EphemeralRejectsPath => "ephemeral-rejects-path",
        }
    }
}

/// A product behaviour the model predicts faithfully although it bends one of
/// `03-coverage-model.md`'s rules. Each is reported to a human; none may be
/// added to make a failing case pass.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Deviation {
    /// `host set-runtime-root` / `reset-runtime-root` create this machine's
    /// host record before validating, so a refused change on a fresh data root
    /// still leaves a host record behind.
    HostMaterializedByRefusal,
    /// `add --enable` without `--max-capacity` stores the monitor-only policy
    /// and then exits `invalid_argument` because it cannot be armed.
    PartialCommitOnEnable,
    /// A persistent root's owner is compared by the typed spelling, so a
    /// case-variant repository argument meets its own root as "another" one.
    OwnerComparedCaseSensitively,
}

impl Deviation {
    #[must_use]
    pub const fn slug(self) -> &'static str {
        match self {
            Deviation::HostMaterializedByRefusal => "host-materialized-by-refusal",
            Deviation::PartialCommitOnEnable => "partial-commit-on-enable",
            Deviation::OwnerComparedCaseSensitively => "owner-compared-case-sensitively",
        }
    }

    /// The only delta kinds a refusal carrying this deviation may hold.
    #[must_use]
    pub const fn permitted(self) -> &'static [DeltaKind] {
        match self {
            Deviation::HostMaterializedByRefusal => &[DeltaKind::HostMaterialized],
            Deviation::PartialCommitOnEnable => {
                &[DeltaKind::HostMaterialized, DeltaKind::PolicyAdded]
            }
            Deviation::OwnerComparedCaseSensitively => &[],
        }
    }
}

// ---------------------------------------------------------------------------
// The fake GitHub's request history
// ---------------------------------------------------------------------------

/// One request the loopback fake GitHub is expected to answer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Request {
    DeviceCode,
    AccessToken,
    ListInstallations,
    ListRepositories(u64),
}

impl Request {
    /// `METHOD /path`, as the fixture records it.
    #[must_use]
    pub fn render(self) -> String {
        match self {
            Request::DeviceCode => "POST /login/device/code".to_string(),
            Request::AccessToken => "POST /login/oauth/access_token".to_string(),
            Request::ListInstallations => "GET /user/installations".to_string(),
            Request::ListRepositories(id) => format!("GET /user/installations/{id}/repositories"),
        }
    }

    /// Whether this request can change anything on GitHub's side. Only the two
    /// device-flow exchanges are POSTs, and both only create a sign-in.
    #[must_use]
    pub const fn is_read(self) -> bool {
        matches!(
            self,
            Request::ListInstallations | Request::ListRepositories(_)
        )
    }
}

/// The read-only discovery a valid credential triggers.
#[must_use]
pub fn discovery(installation: Installation) -> Vec<Request> {
    let mut requests = vec![Request::ListInstallations];
    requests.extend(
        installation
            .specs()
            .iter()
            .map(|spec| Request::ListRepositories(spec.id)),
    );
    requests
}

// ---------------------------------------------------------------------------
// Deltas
// ---------------------------------------------------------------------------

/// One owned change. Replaying a transition's deltas onto its previous model
/// must produce its next model exactly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Delta {
    CredentialStored,
    CredentialRemoved,
    HostMaterialized,
    HostCapacity {
        from: u16,
        to: u16,
    },
    HostRunnerRoot {
        from: Option<PathValue>,
        to: Option<PathValue>,
    },
    DirectoryCreated(PathValue),
    PolicyAdded {
        key: TargetKey,
        policy: Policy,
    },
    PolicyRemoved {
        key: TargetKey,
        policy: Policy,
    },
    /// `from: None` is the D19 promotion from monitor-only.
    PolicyMaxCapacity {
        key: TargetKey,
        from: Option<u16>,
        to: u16,
    },
    PolicyLabels {
        key: TargetKey,
        added: Vec<String>,
        removed: Vec<String>,
    },
    PolicyScale {
        key: TargetKey,
        from: (bool, PolicyState),
        to: (bool, PolicyState),
    },
    PolicyWorkspace {
        key: TargetKey,
        from: Option<PathValue>,
        to: Option<PathValue>,
    },
    /// A removed policy's attempts stay in the journal as diagnostics.
    AttemptsRetained {
        key: TargetKey,
        tally: Tally,
    },
    PackageCachePurged,
}

/// The field family a delta touches.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DeltaKind {
    CredentialStored,
    CredentialRemoved,
    HostMaterialized,
    HostCapacity,
    HostRunnerRoot,
    DirectoryCreated,
    PolicyAdded,
    PolicyRemoved,
    PolicyMaxCapacity,
    PolicyLabels,
    PolicyScale,
    PolicyWorkspace,
    AttemptsRetained,
    PackageCachePurged,
}

impl Delta {
    #[must_use]
    pub const fn kind(&self) -> DeltaKind {
        match self {
            Delta::CredentialStored => DeltaKind::CredentialStored,
            Delta::CredentialRemoved => DeltaKind::CredentialRemoved,
            Delta::HostMaterialized => DeltaKind::HostMaterialized,
            Delta::HostCapacity { .. } => DeltaKind::HostCapacity,
            Delta::HostRunnerRoot { .. } => DeltaKind::HostRunnerRoot,
            Delta::DirectoryCreated(_) => DeltaKind::DirectoryCreated,
            Delta::PolicyAdded { .. } => DeltaKind::PolicyAdded,
            Delta::PolicyRemoved { .. } => DeltaKind::PolicyRemoved,
            Delta::PolicyMaxCapacity { .. } => DeltaKind::PolicyMaxCapacity,
            Delta::PolicyLabels { .. } => DeltaKind::PolicyLabels,
            Delta::PolicyScale { .. } => DeltaKind::PolicyScale,
            Delta::PolicyWorkspace { .. } => DeltaKind::PolicyWorkspace,
            Delta::AttemptsRetained { .. } => DeltaKind::AttemptsRetained,
            Delta::PackageCachePurged => DeltaKind::PackageCachePurged,
        }
    }

    /// The policy this delta belongs to, if it belongs to one.
    #[must_use]
    pub fn key(&self) -> Option<&TargetKey> {
        match self {
            Delta::PolicyAdded { key, .. }
            | Delta::PolicyRemoved { key, .. }
            | Delta::PolicyMaxCapacity { key, .. }
            | Delta::PolicyLabels { key, .. }
            | Delta::PolicyScale { key, .. }
            | Delta::PolicyWorkspace { key, .. }
            | Delta::AttemptsRetained { key, .. } => Some(key),
            _ => None,
        }
    }
}

/// The field families each leaf owns.
#[must_use]
pub fn owned(kind: ActionKind) -> &'static [DeltaKind] {
    match kind {
        ActionKind::AuthLogin => &[DeltaKind::CredentialStored],
        ActionKind::AuthLogout => &[DeltaKind::CredentialRemoved],
        ActionKind::HostSetCapacity => &[DeltaKind::HostMaterialized, DeltaKind::HostCapacity],
        ActionKind::HostSetRuntimeRoot => &[
            DeltaKind::HostMaterialized,
            DeltaKind::HostRunnerRoot,
            DeltaKind::DirectoryCreated,
        ],
        ActionKind::HostResetRuntimeRoot => {
            &[DeltaKind::HostMaterialized, DeltaKind::HostRunnerRoot]
        }
        ActionKind::RepoAdd | ActionKind::OrgAdd => &[
            DeltaKind::HostMaterialized,
            DeltaKind::PolicyAdded,
            DeltaKind::PolicyScale,
        ],
        ActionKind::RepoSetCapacity | ActionKind::OrgSetCapacity => &[DeltaKind::PolicyMaxCapacity],
        ActionKind::RepoSetScale | ActionKind::OrgSetScale => &[DeltaKind::PolicyScale],
        ActionKind::RepoAddLabel
        | ActionKind::OrgAddLabel
        | ActionKind::RepoRemoveLabel
        | ActionKind::OrgRemoveLabel => &[DeltaKind::PolicyLabels],
        ActionKind::RepoSetWorkspace => &[DeltaKind::PolicyWorkspace, DeltaKind::DirectoryCreated],
        ActionKind::RepoRemove | ActionKind::OrgRemove => &[
            DeltaKind::PolicyRemoved,
            DeltaKind::AttemptsRetained,
            DeltaKind::PackageCachePurged,
        ],
        ActionKind::HostShow
        | ActionKind::RepoList
        | ActionKind::OrgList
        | ActionKind::StatusJson => &[],
    }
}

/// Applies one delta.
///
/// # Errors
/// When the delta does not describe `model` — its `from` side disagrees, or it
/// adds what exists or removes what does not.
pub fn replay_one(model: &mut Model, delta: &Delta) -> Result<(), String> {
    match delta {
        Delta::CredentialStored => {
            if model.credential {
                return Err("CredentialStored over a stored credential".to_string());
            }
            model.credential = true;
        }
        Delta::CredentialRemoved => {
            if !model.credential {
                return Err("CredentialRemoved with none stored".to_string());
            }
            model.credential = false;
        }
        Delta::HostMaterialized => {
            if model.host.is_some() {
                return Err("HostMaterialized over an existing host record".to_string());
            }
            model.host = Some(HostModel {
                capacity: DEFAULT_HOST_CAPACITY,
                runner_root: None,
            });
        }
        Delta::HostCapacity { from, to } => {
            let host = model
                .host
                .as_mut()
                .ok_or("HostCapacity with no host record")?;
            if host.capacity != *from || from == to {
                return Err(format!(
                    "HostCapacity {from}->{to} does not describe capacity {}",
                    host.capacity
                ));
            }
            host.capacity = *to;
        }
        Delta::HostRunnerRoot { from, to } => {
            let host = model
                .host
                .as_mut()
                .ok_or("HostRunnerRoot with no host record")?;
            if host.runner_root != *from || from == to {
                return Err(format!(
                    "HostRunnerRoot {from:?}->{to:?} does not describe {:?}",
                    host.runner_root
                ));
            }
            host.runner_root = *to;
        }
        Delta::DirectoryCreated(path) => {
            if !model.directories.insert(*path) {
                return Err(format!("DirectoryCreated({path:?}) already exists"));
            }
        }
        Delta::PolicyAdded { key, policy } => {
            if model.policies.insert(key.clone(), policy.clone()).is_some() {
                return Err(format!("PolicyAdded over the existing {key}"));
            }
        }
        Delta::PolicyRemoved { key, policy } => match model.policies.remove(key) {
            Some(existing) if existing == *policy => {}
            Some(existing) => {
                return Err(format!(
                    "PolicyRemoved({key}) names {policy:?}, found {existing:?}"
                ));
            }
            None => return Err(format!("PolicyRemoved({key}) with no policy")),
        },
        Delta::PolicyMaxCapacity { key, from, to } => {
            let policy = model
                .policies
                .get_mut(key)
                .ok_or_else(|| format!("PolicyMaxCapacity names {key}, which has no policy"))?;
            if policy.max_capacity() != *from || *from == Some(*to) || *to == 0 {
                return Err(format!(
                    "PolicyMaxCapacity {from:?}->{to} does not describe {:?}",
                    policy.max_capacity()
                ));
            }
            match &mut policy.mode {
                Mode::MonitorOnly => {
                    policy.mode = Mode::Autoscale {
                        max_capacity: *to,
                        extra_labels: BTreeSet::new(),
                    };
                }
                Mode::Autoscale { max_capacity, .. } => *max_capacity = *to,
            }
        }
        Delta::PolicyLabels {
            key,
            added,
            removed,
        } => {
            let policy = model
                .policies
                .get_mut(key)
                .ok_or_else(|| format!("PolicyLabels names {key}, which has no policy"))?;
            let Mode::Autoscale { extra_labels, .. } = &mut policy.mode else {
                return Err(format!("PolicyLabels on the monitor-only {key}"));
            };
            if added.is_empty() && removed.is_empty() {
                return Err("an empty PolicyLabels is not a change".to_string());
            }
            for label in removed {
                if !extra_labels.remove(label) {
                    return Err(format!("PolicyLabels removes {label}, which {key} lacks"));
                }
            }
            for label in added {
                if !extra_labels.insert(label.clone()) {
                    return Err(format!("PolicyLabels adds {label}, which {key} has"));
                }
            }
        }
        Delta::PolicyScale { key, from, to } => {
            let policy = model
                .policies
                .get_mut(key)
                .ok_or_else(|| format!("PolicyScale names {key}, which has no policy"))?;
            if (policy.enabled, policy.state) != *from || from == to {
                return Err(format!(
                    "PolicyScale {from:?}->{to:?} does not describe {:?}",
                    (policy.enabled, policy.state)
                ));
            }
            (policy.enabled, policy.state) = *to;
        }
        Delta::PolicyWorkspace { key, from, to } => {
            let policy = model
                .policies
                .get_mut(key)
                .ok_or_else(|| format!("PolicyWorkspace names {key}, which has no policy"))?;
            if policy.workspace != *from || from == to {
                return Err(format!(
                    "PolicyWorkspace {from:?}->{to:?} does not describe {:?}",
                    policy.workspace
                ));
            }
            policy.workspace = *to;
        }
        Delta::AttemptsRetained { key, tally } => {
            if tally.is_empty() {
                return Err("an empty AttemptsRetained is not a change".to_string());
            }
            let entry = model.retained.entry(key.clone()).or_default();
            *entry = entry.plus(*tally);
        }
        Delta::PackageCachePurged => {
            if !model.package_cache {
                return Err("PackageCachePurged with no cache".to_string());
            }
            model.package_cache = false;
        }
    }
    Ok(())
}

/// Replays every delta, in order.
///
/// # Errors
/// The first delta that does not describe the model it is applied to.
pub fn replay(model: &Model, deltas: &[Delta]) -> Result<Model, String> {
    let mut next = model.clone();
    for delta in deltas {
        replay_one(&mut next, delta)?;
    }
    Ok(next)
}

// ---------------------------------------------------------------------------
// Transitions
// ---------------------------------------------------------------------------

/// Everything the model predicts about one action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transition {
    pub exit: Exit,
    /// The refusal class; `Some` exactly when `exit` is a refusal.
    pub reason: Option<Reason>,
    pub deviation: Option<Deviation>,
    /// Fragments stdout must contain.
    pub stdout: Vec<String>,
    /// Fragments stderr must contain.
    pub stderr: Vec<String>,
    /// The exact requests the fake GitHub must see for this action, in order.
    pub requests: Vec<Request>,
    pub deltas: Vec<Delta>,
    pub next: Model,
    /// Coverage facts this transition witnesses (boundary, cross-scope,
    /// invariant), besides its refusal class.
    pub tags: Vec<String>,
}

/// A transition under construction.
struct Draft<'a> {
    before: &'a Model,
    next: Model,
    deltas: Vec<Delta>,
    stdout: Vec<String>,
    stderr: Vec<String>,
    requests: Vec<Request>,
    tags: Vec<String>,
}

impl<'a> Draft<'a> {
    fn new(before: &'a Model) -> Self {
        Self {
            before,
            next: before.clone(),
            deltas: Vec::new(),
            stdout: Vec::new(),
            stderr: Vec::new(),
            requests: Vec::new(),
            tags: Vec::new(),
        }
    }

    fn change(&mut self, delta: Delta) {
        replay_one(&mut self.next, &delta).unwrap_or_else(|problem| {
            panic!("the model produced an inconsistent delta: {problem}")
        });
        self.deltas.push(delta);
    }

    fn out(&mut self, fragment: impl Into<String>) {
        self.stdout.push(fragment.into());
    }

    fn tag(&mut self, tag: impl Into<String>) {
        self.tags.push(tag.into());
    }

    fn materialize_host(&mut self) {
        if self.next.host.is_none() {
            self.change(Delta::HostMaterialized);
        }
    }

    fn finish(
        self,
        exit: Exit,
        reason: Option<Reason>,
        deviation: Option<Deviation>,
    ) -> Transition {
        let mut tags = self.tags;
        tags.sort();
        tags.dedup();
        Transition {
            exit,
            reason,
            deviation,
            stdout: self.stdout,
            stderr: self.stderr,
            requests: self.requests,
            deltas: self.deltas,
            next: self.next,
            tags,
        }
    }

    fn succeed(self) -> Transition {
        self.finish(Exit::Success, None, None)
    }

    /// A refusal. Every change drafted so far is discarded unless it is one
    /// the product really leaves behind, which is named as a deviation.
    fn refuse(self, reason: Reason, fragment: impl Into<String>) -> Transition {
        let deviation = if self.deltas.is_empty() {
            None
        } else if self
            .deltas
            .iter()
            .all(|delta| delta.kind() == DeltaKind::HostMaterialized)
        {
            Some(Deviation::HostMaterializedByRefusal)
        } else {
            Some(Deviation::PartialCommitOnEnable)
        };
        self.refuse_with(reason, fragment, deviation)
    }

    /// A refusal carrying an explicitly named deviation (or none).
    fn refuse_with(
        mut self,
        reason: Reason,
        fragment: impl Into<String>,
        deviation: Option<Deviation>,
    ) -> Transition {
        self.stderr.push("error: ".to_string());
        self.stderr.push(fragment.into());
        if let Some(deviation) = deviation {
            self.tags.push(format!("deviation:{}", deviation.slug()));
        }
        self.finish(Exit::Refused(reason.failure()), Some(reason), deviation)
    }
}

/// The model's prediction for one action. Pure: `model` is not modified.
#[must_use]
pub fn apply(model: &Model, action: &Action) -> Transition {
    let draft = Draft::new(model);
    match action {
        Action::AuthLogin => login(draft),
        Action::AuthLogout => logout(draft),
        Action::HostSetCapacity(capacity) => host_set_capacity(draft, *capacity),
        Action::HostSetRuntimeRoot(path) => host_runtime_root(draft, Some(*path)),
        Action::HostResetRuntimeRoot => host_runtime_root(draft, None),
        Action::HostShow => host_show(draft),
        Action::Add(args) => add(draft, args),
        Action::List(scope) => list(draft, *scope),
        Action::SetCapacity {
            target,
            max_capacity,
        } => set_capacity(draft, *target, *max_capacity),
        Action::SetScale { target, enabled } => set_scale(draft, *target, *enabled),
        Action::AddLabel { target, labels } => mutate_labels(draft, *target, labels, true),
        Action::RemoveLabel { target, labels } => mutate_labels(draft, *target, labels, false),
        Action::SetWorkspace { repo, setting } => set_workspace(draft, *repo, *setting),
        Action::Remove { target, purge } => remove(draft, *target, *purge),
        Action::StatusJson => status(draft),
    }
}

fn login(mut draft: Draft<'_>) -> Transition {
    if draft.before.credential {
        draft.requests = discovery(draft.before.installation);
        draft.out("Already signed in, so no new code is needed.");
        draft.tag("invariant:login-resumes-without-a-new-code");
    } else {
        draft.requests = vec![Request::DeviceCode, Request::AccessToken];
        draft.requests.extend(discovery(draft.before.installation));
        draft.change(Delta::CredentialStored);
        draft.out("Signed in.");
    }
    draft.succeed()
}

fn logout(mut draft: Draft<'_>) -> Transition {
    if draft.before.credential {
        draft.change(Delta::CredentialRemoved);
        draft.out("Removed the stored credential");
    } else {
        draft.out("There was no stored credential");
        draft.tag("invariant:logout-without-a-credential-is-a-no-op");
    }
    if !draft.before.policies.is_empty() {
        draft.tag("invariant:logout-leaves-policies");
    }
    draft.succeed()
}

fn host_set_capacity(mut draft: Draft<'_>, capacity: Capacity) -> Transition {
    let to = capacity.value();
    if to == 0 {
        return draft.refuse(
            Reason::ZeroHostCapacity,
            "a host capacity of 0 is not a configured host",
        );
    }
    draft.materialize_host();
    let from = draft.next.host_capacity();
    if from != to {
        draft.change(Delta::HostCapacity { from, to });
    } else {
        draft.tag("invariant:host-capacity-rewrite-is-idempotent");
    }
    let in_use = draft.before.in_use();
    draft.out(format!("host_capacity: {from} -> {to}"));
    draft.out(format!("(in use right now: {in_use})"));
    if in_use > to {
        draft.out("Nothing was terminated");
        draft.tag("boundary:host-capacity-below-in-use");
    }
    match capacity {
        Capacity::Max => draft.tag("boundary:host-capacity-u16-max"),
        Capacity::One => draft.tag("boundary:host-capacity-product-default"),
        _ => {}
    }
    draft.succeed()
}

/// Why a candidate root is refused, checked in the product's order: the
/// application data tree, then every other root (the host's first), then what
/// is on disk.
fn root_refusal(
    model: &Model,
    candidate: PathValue,
    others: &[(RootOwner, PathValue)],
) -> Option<(Reason, String, Option<RootOwner>)> {
    if candidate == PathValue::InsideAppState {
        return Some((
            Reason::OverlapsApplicationData,
            "application data must survive".to_string(),
            None,
        ));
    }
    for (owner, root) in others {
        if candidate.relation(*root) != Relation::Disjoint {
            return Some(match owner {
                RootOwner::Host => (
                    Reason::OverlapsHostRoot,
                    "the host runner root".to_string(),
                    Some(owner.clone()),
                ),
                RootOwner::Repository(display) => (
                    Reason::OverlapsRepositoryRoot,
                    format!("the persistent workspace root for {display}"),
                    Some(owner.clone()),
                ),
            });
        }
    }
    if candidate == PathValue::OccupiedFile {
        return Some((
            Reason::ExistingFile,
            "already exists and is not a directory".to_string(),
            None,
        ));
    }
    let parents_missing = candidate == PathValue::DeepMissing
        || candidate
            .creatable_parent()
            .is_some_and(|parent| !model.directory_exists(parent));
    parents_missing.then(|| {
        (
            Reason::MissingParents,
            "more than its last component is missing".to_string(),
            None,
        )
    })
}

/// Who a root in the overlap set belongs to.
#[derive(Debug, Clone, PartialEq, Eq)]
enum RootOwner {
    Host,
    /// By the spelling the product compares: the target as stored.
    Repository(String),
}

fn repository_roots(model: &Model, skip_display: Option<&str>) -> Vec<(RootOwner, PathValue)> {
    model
        .policies
        .values()
        .filter_map(|policy| policy.workspace.map(|root| (policy.display.clone(), root)))
        .filter(|(display, _)| Some(display.as_str()) != skip_display)
        .map(|(display, root)| (RootOwner::Repository(display), root))
        .collect()
}

fn host_runtime_root(mut draft: Draft<'_>, requested: Option<PathValue>) -> Transition {
    if requested == Some(PathValue::Relative) {
        return draft.refuse(
            Reason::RelativePath,
            "cannot be used as the host runner root",
        );
    }
    draft.materialize_host();
    let journal = draft.before.journal();
    if journal.uncleaned() > 0 {
        if draft.before.retained_total().uncleaned() > 0 {
            draft.tag("cross:retained-attempts-block-the-host-root");
        }
        let fragment = format!(
            "{} active and {} awaiting cleanup",
            journal.active, journal.awaiting_cleanup
        );
        return draft.refuse(Reason::AttemptsOwnHostRoot, fragment);
    }
    let from = draft.next.runner_root();
    if let Some(path) = requested {
        let others = repository_roots(draft.before, None);
        if let Some((reason, fragment, owner)) = root_refusal(draft.before, path, &others) {
            if owner.is_some() {
                draft.tag("cross:host-root-overlaps-a-repository-root");
            }
            if path.is_creatable_leaf() {
                draft.tag("invariant:refused-path-creates-nothing");
            }
            return draft.refuse(reason, fragment);
        }
        if path.is_creatable_leaf() && !draft.before.directory_exists(path) {
            draft.change(Delta::DirectoryCreated(path));
            draft.out("Created:");
        }
        draft.out("Runner root configured.");
    } else {
        draft.out("Runner root reset to the platform default.");
    }
    if from != requested {
        draft.change(Delta::HostRunnerRoot {
            from,
            to: requested,
        });
        draft.out("Retained:");
        if from.is_some() {
            draft.tag("invariant:host-root-change-leaves-the-old-directory");
        }
    } else {
        draft.tag("invariant:host-root-rewrite-is-idempotent");
    }
    draft.out("No existing directory was moved or deleted.");
    draft.succeed()
}

fn host_show(mut draft: Draft<'_>) -> Transition {
    if draft.before.host.is_some() {
        draft.out("Host: ");
    } else {
        draft.out("no host record yet");
    }
    draft.out("host_capacity");
    draft.succeed()
}

fn status(mut draft: Draft<'_>) -> Transition {
    draft.out("\"schema_version\"");
    draft.out("\"github_contacted\": false");
    draft.succeed()
}

fn invalid_target(draft: Draft<'_>, target: Target) -> Transition {
    let fragment = match target {
        Target::Repo(RepoName::Malformed) => "OWNER/REPO",
        Target::Repo(_) => "a repository name",
        Target::Org(_) => "an organization login",
    };
    draft.refuse(Reason::InvalidTarget, fragment)
}

fn missing_policy(draft: Draft<'_>, target: Target) -> Transition {
    let fragment = format!("no policy for {} exists", target.token());
    draft.refuse(Reason::MissingPolicy, fragment)
}

/// Every label folded, or `None` when the product refuses any one of them.
fn fold_labels(labels: &[LabelValue]) -> Option<Vec<String>> {
    labels.iter().map(|label| label.canonical()).collect()
}

/// The account a target belongs to: the owner of a repository, or the
/// organization itself.
fn account_of(key: &TargetKey) -> &str {
    match key.scope {
        Scope::Repository => key.slug.split('/').next().unwrap_or_default(),
        Scope::Organization => &key.slug,
    }
}

/// Whether `model` holds a policy of the other scope in `key`'s account.
fn other_scope_shares_account(model: &Model, key: &TargetKey) -> bool {
    let account = account_of(key);
    model
        .policies
        .keys()
        .any(|other| other.scope != key.scope && account_of(other) == account)
}

fn add(mut draft: Draft<'_>, args: &AddArgs) -> Transition {
    let Some(key) = args.target.key() else {
        return invalid_target(draft, args.target);
    };
    let Some(host_label) = args.host_label.canonical() else {
        return draft.refuse(Reason::InvalidHostLabel, "a host label");
    };
    let Some(labels) = fold_labels(&args.labels) else {
        return draft.refuse(Reason::InvalidLabel, "a label");
    };
    if args.max_capacity == Some(Capacity::Zero) {
        return draft.refuse(Reason::ZeroMaxCapacity, "max capacity must be at least 1");
    }
    if args.max_capacity.is_none() && !labels.is_empty() {
        return draft.refuse(
            Reason::LabelsNeedAutoscale,
            "--label needs a policy that starts runners",
        );
    }
    let typed = args.target.token();
    if !draft.before.credential {
        return draft.refuse(Reason::NotAuthenticated, format!("cannot validate {typed}"));
    }
    draft.requests = discovery(draft.before.installation);
    if draft.before.installation == Installation::None {
        return draft.refuse(
            Reason::AppNotInstalled,
            format!("the GitHub App is not installed for {typed}"),
        );
    }
    if !draft.before.installation.reaches(&key) {
        return draft.refuse(
            Reason::TargetNotInstalled,
            format!("the GitHub App is installed, but not on {typed}"),
        );
    }
    draft.materialize_host();
    if draft.before.policies.contains_key(&key) {
        draft.tag("invariant:duplicate-add-keeps-the-existing-policy");
        return draft.refuse(
            Reason::DuplicateTarget,
            format!("a policy for {typed} already exists"),
        );
    }
    let cost = draft.before.admission_cost(&key);
    let projected = draft.before.admitted_cost() + cost;
    if projected > BUDGET_ALLOWANCE_PER_HOUR {
        draft.tag("boundary:budget-ceiling");
        let other = match key.scope {
            Scope::Repository => Scope::Organization,
            Scope::Organization => Scope::Repository,
        };
        if draft.before.has_scope(other) {
            draft.tag("cross:budget-counts-the-other-scope");
        }
        return draft.refuse(Reason::BudgetExceeded, "No policy was stored.");
    }
    if BUDGET_ALLOWANCE_PER_HOUR - projected < super::model::repository_admission_cost() {
        draft.tag("boundary:budget-filled");
    }

    let derived = super::values::derived_symbol(&host_label);
    if labels.contains(&derived) {
        draft.tag("invariant:derived-label-is-never-duplicated");
    }
    let mode = match args.max_capacity {
        Some(maximum) => Mode::Autoscale {
            max_capacity: maximum.value(),
            extra_labels: labels
                .into_iter()
                .filter(|label| *label != derived)
                .collect(),
        },
        None => Mode::MonitorOnly,
    };
    let monitor_only = mode == Mode::MonitorOnly;
    let policy = Policy {
        display: typed.clone(),
        host_label,
        mode,
        enabled: false,
        state: PolicyState::Pending,
        workspace: None,
        attempts: Tally::default(),
    };
    if draft.before.retained.contains_key(&key) {
        draft.tag("invariant:re-added-target-starts-fresh");
    }
    if other_scope_shares_account(draft.before, &key) {
        draft.tag("cross:repository-and-organization-policies-coexist");
    }
    match args.host_label {
        HostLabelValue::Max64 => draft.tag("boundary:host-label-64"),
        HostLabelValue::HomeCase => draft.tag("invariant:host-label-folds-case"),
        _ => {}
    }
    if args.labels.contains(&LabelValue::Max256) {
        draft.tag("boundary:label-256");
    }
    if args.max_capacity == Some(Capacity::Max) {
        draft.tag("boundary:policy-capacity-u16-max");
    }
    draft.change(Delta::PolicyAdded {
        key: key.clone(),
        policy,
    });
    draft.out(format!(
        "Added {} policy for {typed} in pending; scaling is disabled.",
        key.scope.word()
    ));
    if monitor_only {
        draft.out("Monitor-only");
    } else {
        draft.out("Routing labels: ");
    }
    if args.enable {
        if monitor_only {
            return draft.refuse(
                Reason::EnableMonitorOnly,
                "monitor-only policies cannot be enabled",
            );
        }
        draft.change(Delta::PolicyScale {
            key,
            from: (false, PolicyState::Pending),
            to: (true, PolicyState::Active),
        });
        draft.out(format!("Scaling enabled for {typed}."));
    }
    draft.succeed()
}

fn list(mut draft: Draft<'_>, scope: Scope) -> Transition {
    let lines = draft.before.list_lines(scope);
    if lines.is_empty() {
        draft.out(format!("No {} policies.", scope.word()));
    }
    for line in lines {
        draft.out(line);
    }
    let repairs: Vec<String> = draft
        .before
        .policies
        .iter()
        .filter(|(key, policy)| key.scope == scope && policy.state == PolicyState::RepairRequired)
        .map(|(_, policy)| {
            format!(
                "repair: runner-manager {} remove {} --purge",
                scope.word(),
                policy.display
            )
        })
        .collect();
    for repair in repairs {
        draft.out(repair);
    }
    if scope == Scope::Organization && draft.before.has_scope(Scope::Organization) {
        draft.out("persistent workspaces require repository scope");
    }
    draft.succeed()
}

fn set_capacity(mut draft: Draft<'_>, target: Target, capacity: Capacity) -> Transition {
    let Some(key) = target.key() else {
        return invalid_target(draft, target);
    };
    let Some(policy) = draft.before.policies.get(&key).cloned() else {
        if capacity == Capacity::Zero {
            draft.tag("invariant:missing-target-is-reported-before-a-zero-capacity");
        }
        return missing_policy(draft, target);
    };
    if capacity == Capacity::Zero {
        return draft.refuse(Reason::ZeroMaxCapacity, "max capacity must be at least 1");
    }
    let to = capacity.value();
    let from = policy.max_capacity();
    if from.is_none() {
        draft.tag("boundary:monitor-only-promoted");
    }
    if from != Some(to) {
        draft.change(Delta::PolicyMaxCapacity { key, from, to });
    } else {
        draft.tag("invariant:capacity-rewrite-is-idempotent");
    }
    match capacity {
        Capacity::Max => draft.tag("boundary:policy-capacity-u16-max"),
        Capacity::One => draft.tag("boundary:policy-capacity-one"),
        _ => {}
    }
    draft.out(format!(
        "{} max capacity is now {to}; scaling remains {}.",
        target.token(),
        if policy.enabled {
            "enabled"
        } else {
            "disabled"
        }
    ));
    draft.out("Routing labels: ");
    draft.succeed()
}

fn set_scale(mut draft: Draft<'_>, target: Target, enabled: bool) -> Transition {
    let Some(key) = target.key() else {
        return invalid_target(draft, target);
    };
    let Some(policy) = draft.before.policies.get(&key).cloned() else {
        return missing_policy(draft, target);
    };
    let active = policy.attempts.active;
    if !enabled && active > 0 {
        // The runner supplies no stdin, so the drain confirmation reads end of
        // input, which is "no".
        draft.out("Continue? [y/N]");
        return draft.refuse(
            Reason::DisableNeedsConfirmation,
            "disable cancelled; the policy was not changed",
        );
    }
    let from = (policy.enabled, policy.state);
    if enabled {
        if policy.mode == Mode::MonitorOnly {
            return draft.refuse(
                Reason::EnableMonitorOnly,
                "monitor-only policies cannot be enabled",
            );
        }
        if policy.enabled {
            draft.tag("invariant:repeat-enable-is-idempotent");
        } else {
            let entry = if policy.state == PolicyState::Disabled {
                draft.tag("invariant:disabled-policy-can-be-re-armed");
                PolicyState::Pending
            } else {
                policy.state
            };
            if entry != PolicyState::Pending {
                return draft.refuse(
                    Reason::IllegalStateTransition,
                    "is not a legal transition from",
                );
            }
            draft.change(Delta::PolicyScale {
                key,
                from,
                to: (true, PolicyState::Active),
            });
        }
        draft.out(format!("Scaling enabled for {}.", policy.display));
    } else {
        let to = if policy.enabled || policy.state == PolicyState::Draining {
            if policy.state == PolicyState::Draining {
                draft.tag("invariant:repeated-disable-completes-a-drain");
            }
            (false, PolicyState::Disabled)
        } else {
            draft.tag("invariant:disabling-an-unarmed-policy-is-a-no-op");
            from
        };
        if to != from {
            draft.change(Delta::PolicyScale { key, from, to });
        }
        draft.out(format!(
            "{} is disabled with 0 active runner(s)",
            policy.display
        ));
    }
    draft.succeed()
}

fn mutate_labels(
    mut draft: Draft<'_>,
    target: Target,
    labels: &Labels,
    adding: bool,
) -> Transition {
    let Some(key) = target.key() else {
        return invalid_target(draft, target);
    };
    let Some(folded) = fold_labels(labels.values()) else {
        return draft.refuse(Reason::InvalidLabel, "a label");
    };
    let Some(policy) = draft.before.policies.get(&key).cloned() else {
        return missing_policy(draft, target);
    };
    let typed = target.token();
    let Mode::Autoscale { extra_labels, .. } = &policy.mode else {
        return draft.refuse(
            Reason::LabelsOnMonitorOnly,
            format!("{typed} is monitor-only, so it has no routing labels to change"),
        );
    };
    let derived = policy.derived_label();
    let mut extras = extra_labels.clone();
    let mut changed: Vec<String> = Vec::new();
    for label in folded {
        if adding {
            if label == derived {
                draft.tag("invariant:derived-label-is-never-duplicated");
            } else if extras.insert(label.clone()) {
                changed.push(label);
            }
        } else if label == derived {
            if !changed.is_empty() {
                draft.tag("invariant:multi-label-refusal-is-atomic");
            }
            return draft.refuse(Reason::DerivedLabelNotRemovable, "cannot be removed");
        } else if extras.remove(&label) {
            changed.push(label);
        }
    }
    if labels.values().contains(&LabelValue::Max256) && adding {
        draft.tag("boundary:label-256");
    }
    if labels
        .values()
        .iter()
        .any(|value| matches!(value, LabelValue::GpuCase | LabelValue::DerivedCase(_)))
    {
        draft.tag("invariant:labels-fold-case");
    }
    if changed.is_empty() {
        draft.tag("invariant:label-change-is-idempotent");
        draft.out(format!(
            "No label changed; {typed} already had them that way."
        ));
    } else {
        draft.out(format!(
            "{typed} {}: {}",
            if adding {
                "now answers"
            } else {
                "no longer answers"
            },
            changed.join(", ")
        ));
        let (added, removed) = if adding {
            (changed, Vec::new())
        } else {
            (Vec::new(), changed)
        };
        draft.change(Delta::PolicyLabels {
            key,
            added,
            removed,
        });
    }
    draft.out("Routing labels: ");
    if !extras.is_empty() {
        draft.out("warning: a label another runner also answers");
    }
    draft.succeed()
}

fn set_workspace(mut draft: Draft<'_>, repo: RepoName, setting: WorkspaceSetting) -> Transition {
    let target = Target::Repo(repo);
    let Some(key) = target.key() else {
        return invalid_target(draft, target);
    };
    let typed = repo.token();
    let requested = match setting {
        WorkspaceSetting::EphemeralWithPath(_) => {
            return draft.refuse(
                Reason::EphemeralRejectsPath,
                "an ephemeral workspace has none; nothing was changed",
            );
        }
        WorkspaceSetting::Persistent(PathValue::Relative) => {
            return draft.refuse(
                Reason::RelativePath,
                format!("cannot be used as the persistent workspace root for {typed}"),
            );
        }
        WorkspaceSetting::Persistent(path) => Some(path),
        WorkspaceSetting::Ephemeral => None,
    };
    let Some(policy) = draft.before.policies.get(&key).cloned() else {
        return missing_policy(draft, target);
    };
    if policy.attempts.uncleaned() > 0 {
        let fragment = format!(
            "{} active and {} awaiting cleanup",
            policy.attempts.active, policy.attempts.awaiting_cleanup
        );
        return draft.refuse(Reason::AttemptsOwnWorkspace, fragment);
    }
    if let Some(path) = requested {
        let mut others: Vec<(RootOwner, PathValue)> = Vec::new();
        if let Some(host_root) = draft.before.runner_root() {
            others.push((RootOwner::Host, host_root));
        }
        // The owner is compared by the typed spelling against the stored one.
        others.extend(repository_roots(draft.before, Some(typed.as_str())));
        if let Some((reason, fragment, owner)) = root_refusal(draft.before, path, &others) {
            match &owner {
                Some(RootOwner::Host) => draft.tag("cross:repository-root-overlaps-the-host-root"),
                Some(RootOwner::Repository(display)) => {
                    if *display == policy.display {
                        return draft.refuse_with(
                            reason,
                            fragment,
                            Some(Deviation::OwnerComparedCaseSensitively),
                        );
                    }
                    draft.tag("invariant:repository-roots-never-overlap");
                }
                None => {}
            }
            if path.is_creatable_leaf() {
                draft.tag("invariant:refused-path-creates-nothing");
            }
            return draft.refuse(reason, fragment);
        }
        if path.is_creatable_leaf() && !draft.before.directory_exists(path) {
            draft.change(Delta::DirectoryCreated(path));
            draft.out("Created:");
        }
    }
    let from = policy.workspace;
    if from != requested {
        draft.change(Delta::PolicyWorkspace {
            key,
            from,
            to: requested,
        });
        if from.is_some() {
            draft.out("Left in place:");
            draft.tag("invariant:workspace-change-leaves-the-old-root");
        }
    } else {
        draft.tag("invariant:workspace-rewrite-is-idempotent");
    }
    draft.out(format!(
        "Workspace mode: {}",
        if requested.is_some() {
            "persistent"
        } else {
            "ephemeral"
        }
    ));
    if requested.is_some() {
        draft.out(
            "warning: a persistent workspace is a trusted-workflow optimization, not isolation.",
        );
    }
    draft.out("No existing directory was moved or deleted.");
    draft.succeed()
}

fn remove(mut draft: Draft<'_>, target: Target, purge: bool) -> Transition {
    let Some(key) = target.key() else {
        return invalid_target(draft, target);
    };
    let Some(policy) = draft.before.policies.get(&key).cloned() else {
        return missing_policy(draft, target);
    };
    let typed = target.token();
    if purge && policy.attempts.active > 0 {
        let fragment = format!(
            "cannot purge {typed} while {} active runner(s) exist",
            policy.attempts.active
        );
        return draft.refuse(Reason::PurgeWithActiveAttempts, fragment);
    }
    let attempts = policy.attempts;
    draft.change(Delta::PolicyRemoved {
        key: key.clone(),
        policy,
    });
    if other_scope_shares_account(&draft.next, &key) {
        draft.tag("cross:removal-leaves-the-other-scope-policy");
    }
    if purge {
        if draft.next.policies.is_empty() {
            if draft.next.package_cache {
                draft.change(Delta::PackageCachePurged);
            }
            draft.out("purged because no policy still uses it");
        } else {
            draft.out("preserved because another policy still uses it");
            if draft.before.package_cache {
                let other_scope_only = draft
                    .next
                    .policies
                    .keys()
                    .all(|other| other.scope != key.scope);
                draft.tag(if other_scope_only {
                    "cross:purge-keeps-the-cache-for-the-other-scope"
                } else {
                    "invariant:purge-keeps-a-shared-cache-in-use"
                });
            }
        }
        draft.out(format!(
            "Removed {typed} and purged its historical diagnostics."
        ));
    } else {
        if !attempts.is_empty() {
            draft.change(Delta::AttemptsRetained {
                key: key.clone(),
                tally: attempts,
            });
            draft.tag("invariant:non-purge-removal-retains-diagnostics");
            if attempts.active > 0 {
                draft.tag("cross:removed-policy-attempts-still-count-host-wide");
            }
        }
        draft.out(format!(
            "Removed {typed}; cache and historical diagnostics were preserved."
        ));
    }
    draft.succeed()
}

// ---------------------------------------------------------------------------
// Seeds
// ---------------------------------------------------------------------------

/// Applies a seeded fact.
///
/// # Errors
/// When the seed's precondition does not hold in `model`: a seed describes a
/// state the daemon or another process could have produced from *this* one,
/// not an arbitrary edit.
pub fn seed(model: &Model, seed: &Seed) -> Result<Model, String> {
    let mut next = model.clone();
    match seed {
        Seed::Credential => {
            if next.credential {
                return Err("a credential is already stored".to_string());
            }
            next.credential = true;
        }
        Seed::Attempt { target, kind } => {
            let attempts = &mut seeded_policy(&mut next, target)?.attempts;
            match kind {
                AttemptKind::Active => attempts.active += 1,
                AttemptKind::AwaitingCleanup => attempts.awaiting_cleanup += 1,
                AttemptKind::Cleaned => attempts.cleaned += 1,
            }
        }
        Seed::RepairRequired(target) => {
            let policy = seeded_policy(&mut next, target)?;
            if policy.state != PolicyState::Pending {
                return Err(format!(
                    "only a pending policy can become repair_required, not {}",
                    policy.state.token()
                ));
            }
            policy.state = PolicyState::RepairRequired;
        }
        Seed::Drain(target) => {
            let policy = seeded_policy(&mut next, target)?;
            if policy.state != PolicyState::Active {
                return Err(format!(
                    "only an active policy can drain, not {}",
                    policy.state.token()
                ));
            }
            policy.state = PolicyState::Draining;
            policy.enabled = false;
        }
        Seed::PackageCache => {
            if next.package_cache {
                return Err("the package cache already exists".to_string());
            }
            next.package_cache = true;
        }
    }
    Ok(next)
}

/// The existing policy a seed addresses.
fn seeded_policy<'m>(model: &'m mut Model, target: &Target) -> Result<&'m mut Policy, String> {
    let key = target
        .key()
        .ok_or_else(|| format!("{} is not a valid target", target.token()))?;
    model
        .policies
        .get_mut(&key)
        .ok_or_else(|| format!("no policy for {key} to seed"))
}

// ---------------------------------------------------------------------------
// The self-check
// ---------------------------------------------------------------------------

/// Checks a transition against the model's contract without re-running the
/// transition function, so a corrupted expectation is judged by rules rather
/// than by the code that produced it.
///
/// # Errors
/// The first broken rule.
pub fn check(before: &Model, action: &Action, transition: &Transition) -> Result<(), String> {
    let kind = action.kind();
    match (transition.exit, transition.reason) {
        (Exit::Success, None) => {}
        (Exit::Refused(failure), Some(reason)) if reason.failure() == failure => {}
        (exit, reason) => {
            return Err(format!(
                "exit {exit:?} does not agree with reason {reason:?}"
            ));
        }
    }
    let replayed = replay(before, &transition.deltas)?;
    if replayed != transition.next {
        return Err(format!(
            "the deltas do not produce the expected next model:\n  replayed: {replayed:?}\n  expected: {:?}",
            transition.next
        ));
    }
    transition.next.validate()?;
    for delta in &transition.deltas {
        if !owned(kind).contains(&delta.kind()) {
            return Err(format!("{kind} does not own {:?}", delta.kind()));
        }
        if let (Some(key), Some(target)) = (delta.key(), action.target())
            && Some(key) != target.key().as_ref()
        {
            return Err(format!("{kind} on {} changed {key}", target.token()));
        }
    }
    if kind.is_read() && (!transition.deltas.is_empty() || !transition.exit.is_success()) {
        return Err(format!("the read {kind} must succeed and change nothing"));
    }
    if !transition.exit.is_success() {
        match transition.deviation {
            None if !transition.deltas.is_empty() => {
                return Err(format!(
                    "a refusal must leave the model unchanged, but {kind} carries {:?}",
                    transition.deltas
                ));
            }
            Some(deviation) => {
                if let Some(delta) = transition
                    .deltas
                    .iter()
                    .find(|delta| !deviation.permitted().contains(&delta.kind()))
                {
                    return Err(format!("{deviation:?} does not permit {delta:?}"));
                }
            }
            None => {}
        }
    } else if transition.deviation.is_some() {
        return Err("only a refusal can carry a deviation".to_string());
    }
    // GitHub is only ever read, except by the two device-flow exchanges of a
    // sign-in that had no credential to resume.
    let posts = transition
        .requests
        .iter()
        .filter(|request| !request.is_read())
        .count();
    let signing_in = kind == ActionKind::AuthLogin && !before.credential;
    if posts != if signing_in { 2 } else { 0 } {
        return Err(format!(
            "{kind} makes {posts} non-read request(s): {:?}",
            transition.requests
        ));
    }
    let discovers = matches!(
        kind,
        ActionKind::AuthLogin | ActionKind::RepoAdd | ActionKind::OrgAdd
    );
    if !discovers && !transition.requests.is_empty() {
        return Err(format!("{kind} must not contact GitHub"));
    }
    if transition.requests.len() > 2 + discovery(before.installation).len() {
        return Err(format!("{kind} exceeds its request bound"));
    }
    if matches!(kind, ActionKind::RepoAdd | ActionKind::OrgAdd)
        && !before.credential
        && !transition.requests.is_empty()
    {
        return Err("an add with no credential must not contact GitHub".to_string());
    }
    Ok(())
}