ignition-core 1.1.0

Core library for ign: config, profiles, gateway client, actions, error taxonomy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
//! EAM task actions (07-02, BKUP-02) — the read-heavy surface with
//! guarded writes, now carrying the full write LIFECYCLE (10-03).
//! Reads: run history (the runtime seam — the controller state gate
//! classifies honestly) and task definitions (the config-resource
//! seam — available on stock gateways). Writes: create (the guard
//! ladder), suspend/resume/cancel (find-first lifecycle verbs),
//! modify (full-record RMW) and delete (signature-keyed) — every
//! verb preceded by the blast-radius preview composer
//! ([`build_blast_radius`]) when the caller needs the pre-flight.
//!
//! Two-layer naming (LOCKED): the CLIENT models are wire-faithful;
//! HERE the agent-stable summary re-exposes under unit-explicit keys
//! (`name`/`task_type`/`schedule_mode`/`current_state`), and history
//! items pass through VERBATIM (the gateway's own camelCase keys —
//! execution outcomes are DATA: a `Failed` level with GNET
//! not-connected detail is an exit-0 read, never hidden, research
//! Pitfall 3).

use std::collections::BTreeSet;

use serde::Serialize;
use serde_json::{Map, Value};

use crate::client::GatewayApi;
use crate::client::eam::{
    DeleteOutcome, EamHistoryItem, EamScheduledTask, EamTaskRecord, ModifyOutcome, ResourceChange,
};
use crate::error::CoreError;

/// The planner-locked create ladder's verdict (07-02 Task 3) — a
/// PURE function over `(task_type, schedule_mode)` so main.rs
/// (pre-resolution, zero network) and the TUI (Confirm gating) and
/// the action (authoritative re-check) all classify IDENTICALLY.
///
/// | verdict | meaning |
/// |---|---|
/// | `Unguarded` | `eam_backup` + OnDemand — fires only when forced, never mutates targets autonomously |
/// | `NeedsYes` | mutating types (restart/send*/licenses) OR any non-OnDemand schedule (arms autonomous actions) |
/// | `Refused` | `eam_restoreBackup`/`eam_installModules`/`eam_remoteUpgrade` — fleet-destructive, EXT-03 (v2) scope |
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskCreateVerdict {
    /// No `--yes` needed (OnDemand backup).
    Unguarded,
    /// `require_confirmation` must fire.
    NeedsYes,
    /// Outright refusal (`EamTaskTypeRefused`).
    Refused,
}

/// The openapi taxonomy's REFUSED set — fleet-destructive types the
/// CLI refuses outright (push backups/modules/upgrades to every
/// agent target).
const REFUSED_TYPES: [&str; 3] = [
    "eam_restoreBackup",
    "eam_installModules",
    "eam_remoteUpgrade",
];

/// The taxonomy's mutating-but-allowed set — they act on target
/// agents when dispatched, so their DEFINITIONS need `--yes`.
const MUTATING_TYPES: [&str; 7] = [
    "eam_restart",
    "eam_sendProject",
    "eam_sendResource",
    "eam_sendTags",
    "eam_activateLicense",
    "eam_updateLicense",
    "eam_unactivateLicense",
];

/// THE guard ladder (pure): refused types first (highest rung);
/// then any non-OnDemand schedule (arms autonomous actions); then
/// the mutating type set; `eam_backup` + OnDemand lands unguarded.
/// An UNKNOWN type classifies `NeedsYes` — fail-safe (a `--yes`
/// costs nothing; an unrecognized fleet verb firing unguarded could
/// cost plenty; the server's own validation remains the backstop).
pub fn task_create_guard(task_type: &str, schedule_mode: &str) -> TaskCreateVerdict {
    if REFUSED_TYPES.contains(&task_type) {
        return TaskCreateVerdict::Refused;
    }
    if !schedule_mode.eq("OnDemand") {
        return TaskCreateVerdict::NeedsYes;
    }
    if MUTATING_TYPES.contains(&task_type) || task_type != "eam_backup" {
        return TaskCreateVerdict::NeedsYes;
    }
    TaskCreateVerdict::Unguarded
}

/// `ign eam task new` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamTaskCreateResult {
    /// The created definition's name.
    pub name: String,
    /// The `profile.type` token.
    pub task_type: String,
    /// The `profile.scheduleMode` token.
    pub schedule_mode: String,
    /// The composed definition body that rode the array POST
    /// (verbatim — the agent's read-back of what was created).
    pub definition: Value,
}

/// `ign eam task force` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamTaskForceResult {
    /// The dispatched task's name.
    pub task: String,
    /// The owner the force POST targeted (from the healthcheck's
    /// `scheduledTaskState.details.owner`, fallback `"eam"`).
    pub owner: String,
    /// Always `true` on this shape — the 2xx IS dispatch acceptance
    /// (execution outcomes land in history as data).
    pub dispatched: bool,
    /// The newest matching history entry after dispatch (null when
    /// none is visible yet) — its `level`/`detail` honestly surface
    /// GNET-not-connected / trial-expired outcomes.
    pub history: Option<EamHistoryItem>,
    /// The composed blast-radius preview (10-03, EAMW-04: force
    /// composes with the preview surface) — the same pre-flight the
    /// CLI's confirmation prompt renders: targets, pending
    /// executions, the factual controller impact. Additive field;
    /// the serde shape stays agent-stable (all keys always).
    pub preview: BlastRadiusPreview,
}

/// One `--setting K=V` parsed with scalar auto-typing (the 05-04
/// tags-write `--value` precedent): a value that parses cleanly as
/// bool (`true`/`false`) or integer serializes as a JSON bool /
/// number; anything else stays a string. Arrays/objects are OUT of
/// scope for K=V (the `--definition` path owns them).
pub fn parse_setting(raw: &str) -> Result<(String, Value), CoreError> {
    let Some((key, value)) = raw.split_once('=') else {
        return Err(CoreError::InvalidInput {
            reason: format!(
                "--setting expects K=V (got {raw:?}) — a value that parses as \
                 bool/int rides typed, anything else stays a string; arrays and \
                 objects need --definition <PATH>"
            ),
        });
    };
    if key.is_empty() || value.is_empty() {
        return Err(CoreError::InvalidInput {
            reason: format!("--setting expects non-empty K and V (got {raw:?})"),
        });
    }
    Ok((key.to_string(), auto_type(value)))
}

/// The scalar auto-typing rule: `true`/`false` → JSON bool; a clean
/// i64 parse → JSON number; anything else → the string verbatim.
fn auto_type(value: &str) -> Value {
    if value == "true" {
        return Value::Bool(true);
    }
    if value == "false" {
        return Value::Bool(false);
    }
    if let Ok(int) = value.parse::<i64>() {
        return Value::Number(int.into());
    }
    Value::String(value.to_string())
}

/// Deep-merge `overlay` onto `base` (objects merge recursively —
/// base keys win only when the overlay carries nothing at that
/// path; arrays and scalars REPLACE, never merge — the documented
/// settings-merge semantics for `--definition`).
fn deep_merge(base: &mut Value, overlay: &Value) {
    match (base, overlay) {
        (Value::Object(base_map), Value::Object(overlay_map)) => {
            for (key, overlay_value) in overlay_map {
                match base_map.get_mut(key) {
                    Some(base_value @ Value::Object(_)) if overlay_value.is_object() => {
                        deep_merge(base_value, overlay_value);
                    }
                    _ => {
                        base_map.insert(key.clone(), overlay_value.clone());
                    }
                }
            }
        }
        (base, overlay) => *base = overlay.clone(),
    }
}

/// Compose the `eam task new` definition body (pure — the
/// unit-testable core of [`eam_task_create`]). The live 8.3.3
/// controller requires the profile/settings SPLIT (captured in
/// `.planning/debug/eam-working-definition.json`; the pre-split body
/// 422'd with "Settings cannot be null"):
///
/// - `config.profile` = `{type, scheduleMode}` ONLY (`isSuspended` is
///   server-owned — never sent on create);
/// - `config.settings` = `{targetGateways, targetGroups}` + every
///   `--setting K=V` (auto-typed scalars) with the `--definition`
///   overlay deep-merged over the composed SETTINGS object (objects
///   merge, arrays/scalars replace).
///
/// Zero `--target` values default to the controller itself —
/// `targetGateways: ["_controller"]`, the live-captured zero-config
/// default on a controller-mode gateway; explicit targets replace it
/// wholesale. `targetGroups` is always `[]` (no `--group` flag
/// exists).
fn compose_task_definition(
    name: &str,
    task_type: &str,
    targets: &[String],
    settings: &[String],
    definition: Option<&Value>,
    schedule_mode: &str,
) -> Result<Value, CoreError> {
    let mut profile = Map::new();
    profile.insert("type".to_string(), Value::String(task_type.to_string()));
    profile.insert(
        "scheduleMode".to_string(),
        Value::String(schedule_mode.to_string()),
    );

    let mut composed_settings = Map::new();
    composed_settings.insert(
        "targetGateways".to_string(),
        if targets.is_empty() {
            Value::Array(vec![Value::String("_controller".to_string())])
        } else {
            Value::Array(targets.iter().map(|t| Value::String(t.clone())).collect())
        },
    );
    composed_settings.insert("targetGroups".to_string(), Value::Array(vec![]));
    for raw in settings {
        let (key, value) = parse_setting(raw)?;
        composed_settings.insert(key, value);
    }
    let mut settings_value = Value::Object(composed_settings);
    if let Some(overlay) = definition {
        deep_merge(&mut settings_value, overlay);
    }

    Ok(serde_json::json!({
        "name": name,
        "config": {
            "profile": Value::Object(profile),
            "settings": settings_value,
        },
    }))
}

/// `ign eam task new` — compose the definition, run the ladder's
/// authoritative re-check, POST the array body.
///
/// Composition (the live 8.3.3 `config.settings` shape — see
/// [`compose_task_definition`]): `{name, config: {profile: {type,
/// scheduleMode}, settings: {targetGateways, targetGroups, ...--setting
/// K=V}}}`; a `--definition` file's top-level object deep-merges over
/// the composed SETTINGS (the typed/array path — mutually exclusive
/// with `--setting` at clap). The refusal ladder runs AGAIN here
/// (main.rs already guarded by verdict; the re-check keeps the pure fn
/// authoritative in core — the double-check is cheap).
pub async fn eam_task_create(
    api: &dyn GatewayApi,
    name: &str,
    task_type: &str,
    targets: &[String],
    settings: &[String],
    definition: Option<&Value>,
    schedule_mode: &str,
) -> Result<EamTaskCreateResult, CoreError> {
    // The ladder is authoritative HERE (the CLI's pre-resolution
    // guard is the fast path; this is the correctness path).
    if let TaskCreateVerdict::Refused = task_create_guard(task_type, schedule_mode) {
        return Err(CoreError::EamTaskTypeRefused {
            task_type: task_type.to_string(),
        });
    }

    let composed = compose_task_definition(
        name,
        task_type,
        targets,
        settings,
        definition,
        schedule_mode,
    )?;
    api.eam_task_create(&composed).await?;
    Ok(EamTaskCreateResult {
        name: name.to_string(),
        task_type: task_type.to_string(),
        schedule_mode: schedule_mode.to_string(),
        definition: composed,
    })
}

/// `ign eam task force` — the preview IS the pre-flight (EAMW-04):
/// [`build_blast_radius`] runs the find (owner resolution via the
/// healthcheck's `scheduledTaskState.details.owner`, live-captured
/// fallback `"eam"`) AND the pending reads the confirmation prompt
/// renders → force POST (2xx = dispatched) → history re-read (the
/// newest matching entry, `level`/`detail` as data). Correctness
/// over latency (the 05-04 precondition precedent): the extra
/// scheduled reads are the confirmation surface's data source.
pub async fn eam_task_force(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<EamTaskForceResult, CoreError> {
    let preview = build_blast_radius(api, "force", name).await?;
    let owner = preview.owner.clone().unwrap_or_else(|| "eam".to_string());

    api.eam_task_force(&owner, name).await?;

    let history = api
        .eam_task_history(Some(20), Some(name))
        .await
        .ok()
        .and_then(|page| {
            page.items.into_iter().find(|item| {
                let forced = format!("{name} (forced)");
                item.task_name == name || item.task_name == forced
            })
        });

    Ok(EamTaskForceResult {
        task: name.to_string(),
        owner,
        dispatched: true,
        history,
        preview,
    })
}

// ---- 10-03 Task 1: the runtime lifecycle (suspend/resume/cancel) ----
//
// Capture-locked per 10-LIVE-CAPTURES.md (both rigs, 8.3.3 + 8.3.6):
// the verbs 204 on success and PERSIST `config.profile.isSuspended`
// (Decision 1); failures are 500s with indistinguishable Jetty HTML
// (§7) — so find-before-write is the ONLY honest name validation,
// and each action carries an authoritative re-check of verb
// applicability BEFORE the write fires (the task_create double-check
// pattern: main.rs/TUI pre-resolve on the same pure fns).

/// `ign eam task suspend|resume|cancel` output model — all keys
/// always (the agent-stable shape; nulls are honest absence).
#[derive(Debug, Serialize)]
pub struct EamLifecycleResult {
    /// The lifecycle target's name.
    pub task: String,
    /// What ran: `"suspended"`, `"resumed"`, or `"cancelled"`.
    pub action: String,
    /// The find healthcheck's `currentState` BEFORE the write (the
    /// captured vocabulary rides [`crate::client::eam::EAM_CURRENT_STATES`]).
    pub previous_state: Option<String>,
    /// The post-write find read-back of `config.profile.isSuspended`
    /// — the capture-locked persistence proof (suspend ⇒ `true`,
    /// resume ⇒ `false`, 10-LIVE-CAPTURES Decision 1). `null` for
    /// cancel (no definition flag rides that verb).
    pub config_suspended: Option<bool>,
    /// cancel only: the POST-write pending read filtered to the task
    /// — `null` when nothing was pending (the honest no-op) or the
    /// pending row is gone (the cancel landed). Suspend/resume leave
    /// it `null` (they target the scheduler trigger, not a row).
    pub pending: Option<EamScheduledTask>,
    /// Whether the lifecycle POST actually rode the wire. Carries the
    /// cancel no-op honesty (`fired: false` ⇔ the no-op result's
    /// "`cancelled: false`") — a write we declined to fire is
    /// reported, never disguised as a success.
    pub fired: bool,
    /// Why nothing fired: `"no pending execution"` on the cancel
    /// no-op, or the gateway's own `canCancel=false` report on an
    /// uncancellable row. Always `null` on a fired write.
    pub reason: Option<String>,
}

/// The lifecycle name precheck (pure, ZERO network — the
/// guard-ladder three-place rule: main.rs pre-resolution, the TUI's
/// Confirm gating, and the action's authoritative re-check all call
/// THIS fn). Name-empty/whitespace refusals ONLY — every other
/// applicability question needs the find (the action's Tier-3 job),
/// and inventing state rules the captures don't support is exactly
/// the wire-dishonesty pitfall.
pub fn lifecycle_precheck(action: &str, task_name: &str) -> Result<(), CoreError> {
    if task_name.trim().is_empty() {
        return Err(CoreError::InvalidInput {
            reason: format!("eam task {action}: the task name must not be empty/whitespace"),
        });
    }
    Ok(())
}

/// The suspend re-check (pure): the capture-locked refusal branch.
/// A suspended task's trigger is parked (`nextScheduled: "N/A"`,
/// `currentState: "Suspended"` — 10-LIVE-CAPTURES §1c), so the
/// gateway's own answer to the POST is the INDISTINGUISHABLE 500
/// "Task could not be suspended" (§1a/§7 — same answer an
/// unknown-name or OnDemand suspend gets). When find PROVES
/// `isSuspended: true`, refuse pre-write exit 2 naming the task —
/// a distinct, actionable message instead of the ambiguous page.
/// Anything else (`false`, absent, unparseable) fires — no invented
/// state machine.
pub fn suspend_recheck(record: &EamTaskRecord) -> Result<(), CoreError> {
    let suspended = record
        .config
        .get("profile")
        .and_then(|profile| profile.get("isSuspended"))
        .and_then(Value::as_bool);
    if suspended == Some(true) {
        return Err(CoreError::InvalidInput {
            reason: format!(
                "task {:?} is already suspended (config.profile.isSuspended=true) — \
                 nothing to suspend; `eam task resume` it first",
                record.name
            ),
        });
    }
    Ok(())
}

/// The cancel decision (pure) over the task's pending row (from the
/// scheduled reads, filtered to the name). Every branch mirrors a
/// captured fact — nothing here is an invented state machine:
///
/// - [`CancelDecision::Fire`] — a pending row with `canCancel: true`
///   (the captured `Scheduled` cell, §2): the gateway says the
///   cancel CAN fire.
/// - [`CancelDecision::NoPending`] — no row: the gateway's own
///   answer to cancel-with-nothing-pending is a SILENT 204 (§7) —
///   mirrored as an honest no-op result WITHOUT the wire round trip
///   (`fired: false`, reason `"no pending execution"`).
/// - [`CancelDecision::NotPermitted`] — a row whose `canCancel` is
///   `false` (an unobserved cell — Running rows never materialized
///   on the capture rigs): the gateway's own capability flag says
///   no; the flag is reported verbatim, the POST is not fired.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CancelDecision {
    /// Fire the cancel POST (a pending, cancellable execution exists).
    Fire,
    /// Honest no-op — nothing pending (`fired: false`).
    NoPending,
    /// Honest no-op — the row's `canCancel` is `false` (`fired: false`).
    NotPermitted,
}

/// The pure decision read (see [`CancelDecision`]).
pub fn cancel_decision(pending: Option<&EamScheduledTask>) -> CancelDecision {
    match pending {
        None => CancelDecision::NoPending,
        Some(row) if row.can_cancel => CancelDecision::Fire,
        Some(_) => CancelDecision::NotPermitted,
    }
}

/// The find healthcheck's `currentState` (pure projection).
fn current_state_of(record: &EamTaskRecord) -> Option<String> {
    record
        .scheduled_task_state
        .as_ref()
        .and_then(|state| state.get("currentState"))
        .and_then(Value::as_str)
        .map(str::to_string)
}

/// The definition flag `config.profile.isSuspended` (pure projection).
fn is_suspended_of(record: &EamTaskRecord) -> Option<bool> {
    record
        .config
        .get("profile")
        .and_then(|profile| profile.get("isSuspended"))
        .and_then(Value::as_bool)
}

/// The task's FIRST pending row (both literal segments — see
/// [`pending_rows_for`] for the both-segments discipline). The
/// cancel action's decision input.
async fn pending_row_for(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<Option<EamScheduledTask>, CoreError> {
    Ok(pending_rows_for(api, name).await?.into_iter().next())
}

/// `ign eam task suspend` — find (the unknown-name 500 is
/// indistinguishable on this seam, §7 — the config-resource find's
/// `not_found` is the honest refusal) → the authoritative
/// already-suspended re-check → the POST → the find read-back that
/// reports the capture-locked `isSuspended` persistence (Decision 1).
/// A suspended task refuses exit 2 BEFORE the wire; an OnDemand or
/// untriggered task FIRES and reports the gateway's verbatim 500
/// (wire honesty over cleverness — no invented rules).
pub async fn eam_task_suspend(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<EamLifecycleResult, CoreError> {
    lifecycle_precheck("suspend", name)?;
    let record = api.eam_task_find(name).await?;
    let previous_state = current_state_of(&record);
    suspend_recheck(&record)?;
    api.eam_task_suspend(name).await?;
    // Decision 1: the runtime verbs PERSIST the flag into the
    // definition — the read-back is the proof the result reports.
    let readback = api.eam_task_find(name).await?;
    Ok(EamLifecycleResult {
        task: name.to_string(),
        action: "suspended".to_string(),
        previous_state,
        config_suspended: is_suspended_of(&readback),
        pending: None,
        fired: true,
        reason: None,
    })
}

/// `ign eam task resume` — the suspend inverse with NO refusal
/// re-check: the capture answers resume of a never-suspended task
/// with a silent 204 (§1b) — the gateway accepts the verb regardless,
/// so we fire and report (wire honesty over cleverness). The
/// read-back reports the persisted flag honestly whatever it is.
pub async fn eam_task_resume(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<EamLifecycleResult, CoreError> {
    lifecycle_precheck("resume", name)?;
    let record = api.eam_task_find(name).await?;
    let previous_state = current_state_of(&record);
    api.eam_task_resume(name).await?;
    let readback = api.eam_task_find(name).await?;
    Ok(EamLifecycleResult {
        task: name.to_string(),
        action: "resumed".to_string(),
        previous_state,
        config_suspended: is_suspended_of(&readback),
        pending: None,
        fired: true,
        reason: None,
    })
}

/// `ign eam task cancel` — find (unknown names answer a SILENT 204
/// on this seam, §7 — cancel can never be a name-validation tool;
/// the config-resource find's `not_found` is) → the pending reads →
/// the pure [`cancel_decision`] → fire or honest no-op → the
/// post-write pending read (`pending` reports what REMAINS).
pub async fn eam_task_cancel(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<EamLifecycleResult, CoreError> {
    lifecycle_precheck("cancel", name)?;
    let record = api.eam_task_find(name).await?;
    let previous_state = current_state_of(&record);
    let pending = pending_row_for(api, name).await?;
    match cancel_decision(pending.as_ref()) {
        CancelDecision::Fire => {
            api.eam_task_cancel(name).await?;
            let after = pending_row_for(api, name).await?;
            Ok(EamLifecycleResult {
                task: name.to_string(),
                action: "cancelled".to_string(),
                previous_state,
                config_suspended: None,
                pending: after,
                fired: true,
                reason: None,
            })
        }
        CancelDecision::NoPending => Ok(EamLifecycleResult {
            task: name.to_string(),
            action: "cancelled".to_string(),
            previous_state,
            config_suspended: None,
            pending: None,
            fired: false,
            reason: Some("no pending execution".to_string()),
        }),
        CancelDecision::NotPermitted => Ok(EamLifecycleResult {
            task: name.to_string(),
            action: "cancelled".to_string(),
            previous_state,
            config_suspended: None,
            pending,
            fired: false,
            reason: Some(
                "the gateway reports canCancel=false for the pending execution".to_string(),
            ),
        }),
    }
}

// ---- 10-03 Task 2: config mutations (modify + delete) ----
//
// Both are capture-locked: the PUT is a FULL-RECORD echo-modify
// carrying the ORIGINAL signature (§6a — omitting config.settings is
// the 422 trap, §6b/Decision 6), and the DELETE is signature-keyed
// with the gateway-side confirm policy (§3 + Decision 3: NO confirm
// by default; the unobserved confirm-demand shape retries ONCE with
// confirm=true — never hard-coded).

/// A targeted modify request (the keys the caller wants changed —
/// everything else rides the found record untouched). Builder-ish:
/// start from [`TaskChange::default`], set what applies.
///
/// **Deliberately ABSENT, per wire honesty:**
///
/// - **rename** — capture §5/Decision 5: a PUT with a changed `name`
///   and the original signature answers **404 empty** (the modify
///   route resolves the resource BY the body's name and finds
///   nothing — no rename, no create, no error body). A rename verb
///   must compose create-new + delete-old; that composite is the
///   CLI tier's (10-04) documented workflow, NEVER this PUT.
/// - **suspend flag** — capture §6a proved only a full-record echo
///   (whose `isSuspended` happened to be `false`) landing; a
///   PUT-DRIVEN `isSuspended` mutation is unproven. The
///   capture-locked path for the flag is the suspend/resume VERBS
///   (Decision 1) — so `TaskChange` carries no flag field at all
///   rather than faking one.
#[derive(Debug, Clone, Default)]
pub struct TaskChange {
    /// Flip the record's top-level `enabled` key.
    pub enabled: Option<bool>,
    /// Rewrite the record's top-level `description` key.
    pub description: Option<String>,
    /// Rewrite `config.profile.scheduleMode`. NOTE: scheduleDetails
    /// rides ONLY via the found record's clone — schedule-mode
    /// changes that also need a new cron/delay string are out of
    /// this change's scope (the capture vocabulary: §12 — the wire
    /// accepts the mode change; an unsupported pairing lands as
    /// gateway-side state, visible in the read-back as data).
    pub schedule_mode: Option<String>,
    /// Deep-merge over the found `config.settings` (objects merge
    /// recursively, arrays/scalars replace — the documented settings
    /// semantics). The settings OBJECT always rides (the 422 trap).
    pub settings_overlay: Option<Value>,
}

/// The `ign eam task modify` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamModifyResult {
    /// The post-write name (rename is NOT supported — always the
    /// name as found).
    pub task: String,
    /// The dotted key paths the change touched: `"enabled"`,
    /// `"description"`, `"config.profile.scheduleMode"`,
    /// `"config.settings"`.
    pub changed: Vec<String>,
    /// The verbatim PUT body (the single record — the client wraps
    /// it in the one-element array envelope): the agent's read-back
    /// of exactly what was sent, signature + collection included.
    pub definition: Value,
    /// The captured 200 outcome
    /// ([`ModifyOutcome`] — `changes[].newSignature` is
    /// authoritative for the NEXT mutation, §6a; its `problem` rides
    /// verbatim on the refusal shapes). `null` when a 2xx carried no
    /// body (the lenient client Option).
    pub put_outcome: Option<ModifyOutcome>,
    /// The post-PUT find, serialized — the echo semantics make the
    /// read-back meaningful (§6a: `newSignature` == read-back
    /// signature, mutated keys landed, unknown round-trip keys
    /// preserved). `null` when the read-back itself failed (the PUT
    /// already succeeded — a read blip must not mask a landed write).
    pub readback: Value,
}

/// The `ign eam task delete` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamDeleteResult {
    /// The deleted (or refused) task's name.
    pub task: String,
    /// Whether the gateway answered the captured success shape
    /// (`success: true`, §3b).
    pub deleted: bool,
    /// The captured `changes[]` verbatim (the deleted resource's
    /// final signature rides `changes[].newSignature`, §9).
    pub changes: Vec<ResourceChange>,
    /// Agent/resource names the delete touched: the `changes[]`
    /// names plus any string-shaped `references` entries (the
    /// affected-resources element shape is UNOBSERVED — §3d — so
    /// the extraction is lenient: JSON strings ride; objects
    /// contribute a `name` key when present).
    pub affected: Vec<String>,
}

/// Apply the targeted mutations to the FULL-record clone (pure —
/// the unit-testable core of [`eam_task_modify`]). Returns the
/// dotted key paths touched. ONLY the targeted keys change:
/// `config.settings`, `signature`, `collection`, and every
/// unknown round-trip key ride the clone byte-for-byte (the
/// never-compose-from-scratch invariant — compose_task_definition
/// composes CREATE bodies and must never feed a modify).
fn apply_task_change(body: &mut Value, change: &TaskChange) -> Vec<String> {
    let mut changed = Vec::new();
    let TaskChange {
        enabled,
        description,
        schedule_mode,
        settings_overlay,
    } = change;
    if let Some(enabled) = enabled {
        *slot(body, "enabled") = Value::Bool(*enabled);
        changed.push("enabled".to_string());
    }
    if let Some(description) = description {
        *slot(body, "description") = Value::String(description.clone());
        changed.push("description".to_string());
    }
    if let Some(schedule_mode) = schedule_mode {
        let profile = slot(slot(body, "config"), "profile");
        if !profile.is_object() {
            *profile = Value::Object(Map::new());
        }
        *slot(profile, "scheduleMode") = Value::String(schedule_mode.clone());
        changed.push("config.profile.scheduleMode".to_string());
    }
    if let Some(overlay) = settings_overlay {
        let settings = slot(slot(body, "config"), "settings");
        deep_merge(settings, overlay);
        changed.push("config.settings".to_string());
    }
    changed
}

/// A mutable slot helper: ensures `parent` is an object and hands
/// back the (created-if-absent) entry for `key`.
fn slot<'a>(parent: &'a mut Value, key: &str) -> &'a mut Value {
    if !parent.is_object() {
        *parent = Value::Object(Map::new());
    }
    parent
        .as_object_mut()
        .expect("just ensured an object")
        .entry(key.to_string())
        .or_insert(Value::Null)
}

/// Serialize a record round-trip (the full-record clone — every key
/// the find answered, unknown ones included, rides the Value).
/// The model hoists the runtime healthcheck under a top-level
/// `scheduledTaskState` key; when the find answer carried the state
/// under `healthchecks` (the captured §0 envelope shape) that field
/// is `None` and would serialize a null PLACEHOLDER — dropped here,
/// so the clone is the find body + nothing (the PUT must never
/// carry a key the wire never answered).
fn record_to_value(record: &EamTaskRecord) -> Result<Value, CoreError> {
    let mut value = serde_json::to_value(record).map_err(|err| {
        CoreError::Internal(format!(
            "task record failed to serialize for the clone: {err}"
        ))
    })?;
    if value.get("scheduledTaskState") == Some(&Value::Null)
        && let Some(map) = value.as_object_mut()
    {
        map.remove("scheduledTaskState");
    }
    Ok(value)
}

/// The stale-signature diagnostic (the client/eam.rs FINDING +
/// capture Decision 4, classified HERE — the client stays
/// classification-free): a signature mismatch answers HTTP 500 with
/// a JSON `problem` whose stable `signature mismatch` substring
/// never reaches this layer (the classifier's Internal fallback
/// drops non-HTML bodies). Classify on EVIDENCE instead: a
/// post-failure find whose signature differs from the one we sent
/// PROVES a concurrent write (the capture also proves mismatches
/// leave the resource untouched — §3a/§4 read-backs) — the
/// client-fixable conflict rides exit 2 (re-run to apply against
/// the current signature). No evidence → the original error
/// propagates verbatim (no invented claims). No new slugs — the
/// existing taxonomy exclusively.
async fn reclassify_stale_signature(
    api: &dyn GatewayApi,
    name: &str,
    sent_signature: &str,
    err: CoreError,
) -> CoreError {
    let stale = matches!(
        api.eam_task_find(name).await,
        Ok(fresh) if fresh.signature.as_deref().is_some_and(|sig| sig != sent_signature)
    );
    if stale {
        return CoreError::InvalidInput {
            reason: format!(
                "definition {name:?} changed concurrently (signature mismatch on write) — \
                 the gateway answers mismatches with a 500 and leaves the resource untouched; \
                 re-run to apply against the current signature"
            ),
        };
    }
    err
}

/// `ign eam task modify` — the FULL-RECORD read-modify-write
/// (never compose-from-scratch: `config.settings: null` is the 422
/// trap, §6b/Decision 6 — the create-composer composes CREATE
/// bodies, not this): find (not_found honesty + the signature
/// source) → clone EVERY key find answered → apply ONLY the
/// targeted [`TaskChange`] keys → PUT the single-element array
/// carrying the ORIGINAL signature + collection → the post-PUT find
/// read-back (the echo semantics make it meaningful, §6a). An
/// all-`None` change refuses exit 2 pre-network (a no-op PUT would
/// still rotate the server-side signature).
pub async fn eam_task_modify(
    api: &dyn GatewayApi,
    name: &str,
    change: TaskChange,
) -> Result<EamModifyResult, CoreError> {
    lifecycle_precheck("modify", name)?;
    let TaskChange {
        enabled,
        description,
        schedule_mode,
        settings_overlay,
    } = &change;
    if enabled.is_none()
        && description.is_none()
        && schedule_mode.is_none()
        && settings_overlay.is_none()
    {
        return Err(CoreError::InvalidInput {
            reason: format!(
                "eam task modify {name:?}: no targeted keys — a modify must change \
                 something (enabled / description / schedule-mode / settings overlay)"
            ),
        });
    }

    let record = api.eam_task_find(name).await?;
    let signature = record
        .signature
        .clone()
        .ok_or_else(|| CoreError::InvalidInput {
            reason: format!(
                "the found record for {name:?} carries no mutation signature — modify \
             requires it (list-shape records don't carry one; re-find)"
            ),
        })?;
    let mut body = record_to_value(&record)?;
    let changed = apply_task_change(&mut body, &change);
    let definition = body.clone();

    let put_outcome = match api.eam_task_modify(&body).await {
        Ok(outcome) => outcome,
        Err(err) => {
            return Err(reclassify_stale_signature(api, name, &signature, err).await);
        }
    };

    let readback = match api.eam_task_find(name).await {
        Ok(fresh) => record_to_value(&fresh)?,
        Err(_) => Value::Null,
    };

    Ok(EamModifyResult {
        task: name.to_string(),
        changed,
        definition,
        put_outcome,
        readback,
    })
}

/// The lenient affected-names extraction (pure): `changes[]` names
/// are capture-proven; `references` element shape is UNOBSERVED
/// (§3d) — JSON strings ride, objects contribute a `name` key when
/// present, everything else is skipped honestly. Deduped, wire
/// order preserved.
fn affected_resources(outcome: &DeleteOutcome) -> Vec<String> {
    let mut names: Vec<String> = outcome
        .changes
        .iter()
        .map(|change| change.name.clone())
        .collect();
    if let Some(references) = &outcome.references {
        for reference in references {
            match reference {
                Value::String(name) => names.push(name.clone()),
                Value::Object(map) => {
                    if let Some(Value::String(name)) = map.get("name") {
                        names.push(name.clone());
                    }
                }
                _ => {}
            }
        }
    }
    let mut seen = BTreeSet::new();
    names
        .into_iter()
        .filter(|name| seen.insert(name.clone()))
        .collect()
}

/// `ign eam task delete` — find first (the `not_found` honesty AND
/// the signature source — never ask the caller for it) → DELETE
/// with `?collection=core` and NO confirm (capture §3b/Decision 3:
/// a lone-resource delete succeeds without it; hard-coding
/// `confirm=true` would bypass a genuine multi-resource warning we
/// cannot yet see) → on the body-level `success: false` (the
/// UNOBSERVED confirm-demand shape, §3d) retry ONCE with
/// `confirm=true` — the CLI's `--yes` gate already ran before this
/// correctness-path action. Signature-mismatch 500s classify on
/// evidence ([`reclassify_stale_signature`]).
pub async fn eam_task_delete(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<EamDeleteResult, CoreError> {
    lifecycle_precheck("delete", name)?;
    let record = api.eam_task_find(name).await?;
    let signature = record
        .signature
        .clone()
        .ok_or_else(|| CoreError::InvalidInput {
            reason: format!(
                "the found record for {name:?} carries no mutation signature — delete \
             is signature-keyed (list-shape records don't carry one; re-find)"
            ),
        })?;

    let outcome = match api.eam_task_delete(name, &signature, false).await {
        Ok(outcome) if outcome.success => outcome,
        Ok(demand) => {
            // The confirm-demand shape (success:false + the
            // affected-resources evidence in references/changes) —
            // the one sanctioned retry, Decision 3 as written.
            let _ = demand;
            match api.eam_task_delete(name, &signature, true).await {
                Ok(retry) => retry,
                Err(err) => {
                    return Err(reclassify_stale_signature(api, name, &signature, err).await);
                }
            }
        }
        Err(err) => {
            return Err(reclassify_stale_signature(api, name, &signature, err).await);
        }
    };

    Ok(EamDeleteResult {
        task: name.to_string(),
        deleted: outcome.success,
        changes: outcome.changes.clone(),
        affected: affected_resources(&outcome),
    })
}

// ---- 10-03 Task 3: the blast-radius preview ----
//
// The read-only pre-flight that feeds every guard prompt: ONE
// composer (find + both scheduled segments) behind the pure
// projection all three caller tiers (CLI refusal message, TUI
// Confirm body, action re-checks) render identically. History is
// deliberately EXCLUDED: 10-RESEARCH lists it as optional, the
// radius answers who/what/targets/pending — the guards consult
// nothing history carries, and the read would double the pre-flight
// traffic for data nobody renders pre-write.

/// The blast-radius preview — the composed pre-write facts (all keys
/// always). Unknown-task names refuse `not_found` at the composer:
/// the preview IS the pre-flight, so a bad name never reaches any
/// confirm prompt.
#[derive(Debug, Serialize)]
pub struct BlastRadiusPreview {
    /// The target task's name.
    pub task: String,
    /// `config.profile.type` (the token, e.g. `eam_backup`) — the
    /// CONFIG seam's vocabulary (the scheduled rows' `type` is a
    /// DIFFERENT, human-label vocabulary — never conflated).
    pub task_type: Option<String>,
    /// `config.profile.scheduleMode`.
    pub schedule_mode: Option<String>,
    /// The find healthcheck's `currentState`.
    pub state: Option<String>,
    /// The healthcheck's `details.owner` (the force verb's owner
    /// segment source; `"eam"` fallback lives at the caller).
    pub owner: Option<String>,
    /// `config.profile.isSuspended` — the definition flag the
    /// lifecycle verbs persist (Decision 1).
    pub config_suspended: Option<bool>,
    /// `config.settings.targetGateways` — the AGENTS the write
    /// touches; empty when the record names none (the controller
    /// itself is then the effective target, per the create
    /// composer's zero-config default — reported as empty here
    /// because the found record, not the composer, owns the list).
    pub target_gateways: Vec<String>,
    /// The task's pending executions (from BOTH literal scheduled
    /// segments, filtered to the name; each row carries the
    /// gateway-owned canPause/canResume/canCancel + taskState).
    pub pending_executions: Vec<EamScheduledTask>,
    /// One factual sentence: what the verb does to the task and how
    /// many agents it touches. No dramatization — the CLI's
    /// require_confirmation string and any future TUI body render
    /// THIS text ([`render_preview_line`]).
    pub controller_impact: String,
    /// The verb the preview was composed for (`suspend`/`resume`/
    /// `cancel`/`force`/`modify`/`delete`).
    pub verb: String,
}

/// The agent-count fragment (`1 agent` / `2 agents` / `0 agents`).
fn agents_fragment(count: usize) -> String {
    format!("{count} agent{}", if count == 1 { "" } else { "s" })
}

/// The per-verb factual impact sentence (pure). Names the task, the
/// profile type when known, and the agent count; the pending count
/// only where the verb targets executions. Capture-honest: nothing
/// here predicts outcomes (execution results are history DATA).
fn controller_impact(
    verb: &str,
    task: &str,
    task_type: Option<&str>,
    agents: usize,
    pending: usize,
) -> String {
    let type_note = task_type.map(|t| format!(" ({t})")).unwrap_or_default();
    let agents = agents_fragment(agents);
    match verb {
        "suspend" => format!(
            "suspends task {task}{type_note} — future scheduled dispatches to {agents} stop until resumed"
        ),
        "resume" => format!(
            "resumes task {task}{type_note} — scheduled dispatches to {agents} can fire again"
        ),
        "cancel" if pending > 0 => format!(
            "cancels the pending execution of task {task}{type_note}{pending} queued dispatch{} to {agents}",
            if pending == 1 { "" } else { "es" }
        ),
        "cancel" => format!("task {task}{type_note} has no pending execution to cancel"),
        "force" => format!("dispatches task {task}{type_note} now to {agents}"),
        "modify" => format!(
            "rewrites the definition of task {task}{type_note} — dispatch behavior to {agents} follows the new body"
        ),
        "delete" => {
            format!("deletes task {task}{type_note} permanently — dispatches to {agents} stop")
        }
        other => format!("examines task {task}{type_note} for {other} — targets {agents}"),
    }
}

/// The pure projection: find record + filtered pending rows + verb
/// → the preview (the async composer's testable core).
fn compose_blast_radius(
    record: &EamTaskRecord,
    pending_executions: Vec<EamScheduledTask>,
    verb: &str,
) -> BlastRadiusPreview {
    // Filter to THIS task here (the pure fn owns the rule, so every
    // caller — and every test — provably drops other tasks' rows).
    let pending_executions: Vec<EamScheduledTask> = pending_executions
        .into_iter()
        .filter(|row| row.name == record.name)
        .collect();
    let profile = record.config.get("profile");
    let task_type = profile
        .and_then(|p| p.get("type"))
        .and_then(Value::as_str)
        .map(str::to_string);
    let schedule_mode = profile
        .and_then(|p| p.get("scheduleMode"))
        .and_then(Value::as_str)
        .map(str::to_string);
    let config_suspended = profile
        .and_then(|p| p.get("isSuspended"))
        .and_then(Value::as_bool);
    let state = current_state_of(record);
    let owner = record
        .scheduled_task_state
        .as_ref()
        .and_then(|s| s.get("details"))
        .and_then(|d| d.get("owner"))
        .and_then(Value::as_str)
        .map(str::to_string);
    let target_gateways: Vec<String> = record
        .config
        .get("settings")
        .and_then(|settings| settings.get("targetGateways"))
        .and_then(Value::as_array)
        .map(|gateways| {
            gateways
                .iter()
                .filter_map(Value::as_str)
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default();
    let controller_impact = controller_impact(
        verb,
        &record.name,
        task_type.as_deref(),
        target_gateways.len(),
        pending_executions.len(),
    );
    BlastRadiusPreview {
        task: record.name.clone(),
        task_type,
        schedule_mode,
        state,
        owner,
        config_suspended,
        target_gateways,
        pending_executions,
        controller_impact,
        verb: verb.to_string(),
    }
}

/// The task's pending rows from BOTH literal scheduled segments
/// (`false` then `true` — 10-LIVE-CAPTURES §2), filtered to the
/// task name, tolerating the quiet empty-list body (§8). BOTH
/// segments always read: a Running row lives only in the `true`
/// segment (uncapturable on the 10-01 rigs — Decision 2) and a
/// short-circuit would report "nothing pending" while an execution
/// is in flight — the one lie this composer must never tell.
async fn pending_rows_for(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<Vec<EamScheduledTask>, CoreError> {
    let mut rows = Vec::new();
    for running in [false, true] {
        rows.extend(
            api.eam_tasks_scheduled(running)
                .await?
                .into_iter()
                .filter(|row| row.name == name),
        );
    }
    Ok(rows)
}

/// Compose the blast-radius preview (the read-only pre-flight):
/// find first — an unknown name refuses `not_found` HERE, before
/// any confirm prompt or write (capture Decision 7: find-before-
/// write is the only honest name validation on the lifecycle seam)
/// — then both scheduled segments. History deliberately excluded
/// (module doc). One composer, every guarded verb.
pub async fn build_blast_radius(
    api: &dyn GatewayApi,
    verb: &str,
    task_name: &str,
) -> Result<BlastRadiusPreview, CoreError> {
    let record = api.eam_task_find(task_name).await?;
    let pending_executions = pending_rows_for(api, task_name).await?;
    Ok(compose_blast_radius(&record, pending_executions, verb))
}

/// The single-line render the CLI's `require_confirmation` operation
/// string embeds (ONE fn so the CLI refusal and any future TUI body
/// agree): `"{verb} {task}: {controller_impact} targets: [a, b]
/// pending: {n}"`.
pub fn render_preview_line(preview: &BlastRadiusPreview) -> String {
    format!(
        "{verb} {task}: {impact} targets: [{targets}] pending: {pending}",
        verb = preview.verb,
        task = preview.task,
        impact = preview.controller_impact,
        targets = preview.target_gateways.join(", "),
        pending = preview.pending_executions.len(),
    )
}

/// `ign eam history` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamHistoryResult {
    /// The run items, wire-faithful passthrough (newest first as the
    /// gateway orders them).
    pub items: Vec<EamHistoryItem>,
    /// How many items came back (the explicit limit's page).
    pub count: usize,
}

/// One task-definition summary row (the agent-stable shape).
#[derive(Debug, Serialize)]
pub struct EamTaskSummary {
    /// Definition name.
    pub name: String,
    /// `config.profile.type` (`eam_backup`, …) — null when the
    /// record's config carries no profile type.
    pub task_type: Option<String>,
    /// `config.profile.scheduleMode` (`OnDemand`, …) — null when
    /// absent.
    pub schedule_mode: Option<String>,
    /// `scheduledTaskState.currentState` — null when the list shape
    /// carries no state (find answers do).
    pub current_state: Option<String>,
}

/// `ign eam tasks` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamTasksResult {
    /// The definition summary rows.
    pub tasks: Vec<EamTaskSummary>,
}

/// `ign eam tasks <NAME>` output model — all keys always.
#[derive(Debug, Serialize)]
pub struct EamTaskDetailResult {
    /// Definition name.
    pub name: String,
    /// The full definition record (config + resource keys,
    /// passthrough as JSON).
    pub definition: serde_json::Value,
    /// The `scheduledTaskState` healthcheck (null when absent —
    /// `currentState`/`nextScheduled`/`owner` under `details`).
    pub state: serde_json::Value,
}

/// `ign eam history` — the runtime read (controller gate honestly
/// classified at the wire seam).
pub async fn eam_history(
    api: &dyn GatewayApi,
    limit: Option<u32>,
    search: Option<&str>,
) -> Result<EamHistoryResult, CoreError> {
    let page = api.eam_task_history(limit, search).await?;
    Ok(EamHistoryResult {
        count: page.items.len(),
        items: page.items,
    })
}

/// `ign eam tasks` — the definitions read (config-resource seam).
pub async fn eam_tasks(api: &dyn GatewayApi) -> Result<EamTasksResult, CoreError> {
    let page = api.eam_task_definitions().await?;
    Ok(EamTasksResult {
        tasks: page.items.iter().map(summary_from).collect(),
    })
}

/// `ign eam tasks <NAME>` — one definition's full record + state;
/// unknown names ride the config-resource `not_found` path.
pub async fn eam_task_detail(
    api: &dyn GatewayApi,
    name: &str,
) -> Result<EamTaskDetailResult, CoreError> {
    let record = api.eam_task_find(name).await?;
    Ok(EamTaskDetailResult {
        name: record.name.clone(),
        definition: serde_json::to_value(&record).unwrap_or(serde_json::Value::Null),
        state: record
            .scheduled_task_state
            .clone()
            .unwrap_or(serde_json::Value::Null),
    })
}

/// The summary projection from one record (the agent-stable keys).
fn summary_from(record: &EamTaskRecord) -> EamTaskSummary {
    let profile = record.config.get("profile");
    EamTaskSummary {
        name: record.name.clone(),
        task_type: profile
            .and_then(|p| p.get("type"))
            .and_then(serde_json::Value::as_str)
            .map(str::to_string),
        schedule_mode: profile
            .and_then(|p| p.get("scheduleMode"))
            .and_then(serde_json::Value::as_str)
            .map(str::to_string),
        current_state: record
            .scheduled_task_state
            .as_ref()
            .and_then(|state| state.get("currentState"))
            .and_then(serde_json::Value::as_str)
            .map(str::to_string),
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CancelDecision, EamLifecycleResult, EamTaskRecord, TaskChange, TaskCreateVerdict,
        affected_resources, apply_task_change, auto_type, cancel_decision, compose_blast_radius,
        compose_task_definition, deep_merge, lifecycle_precheck, parse_setting,
        render_preview_line, summary_from, suspend_recheck, task_create_guard,
    };
    use crate::client::eam::{DeleteOutcome, EamScheduledTask};

    /// The ladder EXHAUSTIVELY over the openapi taxonomy's 11 types
    /// × the schedule modes — the planner-locked breadth pinned as a
    /// pure function.
    #[test]
    fn guard_ladder_is_exhaustive_over_the_taxonomy() {
        // eam_backup + OnDemand = the ONLY unguarded cell.
        assert_eq!(
            task_create_guard("eam_backup", "OnDemand"),
            TaskCreateVerdict::Unguarded
        );

        // The refused trio — the ladder's top rung fires regardless
        // of schedule.
        for refused in [
            "eam_restoreBackup",
            "eam_installModules",
            "eam_remoteUpgrade",
        ] {
            assert_eq!(
                task_create_guard(refused, "OnDemand"),
                TaskCreateVerdict::Refused,
                "{refused} refuses even OnDemand"
            );
            assert_eq!(
                task_create_guard(refused, "Scheduled"),
                TaskCreateVerdict::Refused,
                "{refused} refuses under any schedule"
            );
        }

        // The mutating seven — --yes under OnDemand.
        for mutating in [
            "eam_restart",
            "eam_sendProject",
            "eam_sendResource",
            "eam_sendTags",
            "eam_activateLicense",
            "eam_updateLicense",
            "eam_unactivateLicense",
        ] {
            assert_eq!(
                task_create_guard(mutating, "OnDemand"),
                TaskCreateVerdict::NeedsYes,
                "{mutating} needs --yes"
            );
        }

        // ANY non-OnDemand schedule arms autonomous actions — even
        // eam_backup (the openapi schedule tokens + unknown modes).
        for mode in ["Immediate", "Scheduled", "AtTime", "AtDelay", "weird-mode"] {
            assert_eq!(
                task_create_guard("eam_backup", mode),
                TaskCreateVerdict::NeedsYes,
                "scheduleMode {mode} arms the task"
            );
        }

        // Unknown types classify fail-safe (guarded, never silently
        // unguarded — the server's validation is the backstop).
        assert_eq!(
            task_create_guard("eam_unknownFutureType", "OnDemand"),
            TaskCreateVerdict::NeedsYes
        );
    }

    /// The K=V scalar auto-typing rule: bool/int ride typed,
    /// everything else stays a string; malformed input refuses
    /// `invalid_input`.
    #[test]
    fn setting_parsing_auto_types_scalars() {
        assert_eq!(
            parse_setting("concurrentBackups=2").unwrap(),
            ("concurrentBackups".to_string(), serde_json::json!(2))
        );
        assert_eq!(
            parse_setting("forceBackups=true").unwrap(),
            ("forceBackups".to_string(), serde_json::json!(true))
        );
        assert_eq!(
            parse_setting("forceBackups=false").unwrap(),
            ("forceBackups".to_string(), serde_json::json!(false))
        );
        // Negative + big ints ride typed.
        assert_eq!(
            parse_setting("n=-7").unwrap(),
            ("n".to_string(), serde_json::json!(-7))
        );
        // Strings stay strings — including numeric-looking text
        // with units and values that aren't clean ints.
        assert_eq!(
            parse_setting("note=hello world").unwrap(),
            ("note".to_string(), serde_json::json!("hello world"))
        );
        assert_eq!(
            parse_setting("v=1.5").unwrap(),
            ("v".to_string(), serde_json::json!("1.5")),
            "floats are NOT auto-typed (the tags-write rule: bool/int only)"
        );

        let err = parse_setting("noequalsign").expect_err("refuses");
        assert_eq!(err.exit_code(), 2);
        assert_eq!(err.code(), "invalid_input");
        assert!(err.to_string().contains("--definition"));
        assert!(parse_setting("=v").is_err(), "empty key refuses");
        assert!(parse_setting("k=").is_err(), "empty value refuses");
    }

    /// The auto-typing helper's direct pins.
    #[test]
    fn auto_type_covers_bool_int_string() {
        assert_eq!(auto_type("true"), serde_json::json!(true));
        assert_eq!(auto_type("false"), serde_json::json!(false));
        assert_eq!(auto_type("42"), serde_json::json!(42));
        assert_eq!(auto_type("text"), serde_json::json!("text"));
        assert_eq!(auto_type("True"), serde_json::json!("True"), "case matters");
    }

    /// The --definition merge semantics: objects merge recursively,
    /// arrays and scalars REPLACE.
    #[test]
    fn deep_merge_merges_objects_replaces_arrays() {
        let mut base = serde_json::json!({
            "type": "eam_backup",
            "scheduleMode": "OnDemand",
            "targetGateways": ["gw-a"],
            "settingsNested": {"a": 1, "b": {"x": 1}}
        });
        deep_merge(
            &mut base,
            &serde_json::json!({
                "targetGateways": ["gw-b", "gw-c"],
                "targetGroups": [],
                "concurrentBackups": 2,
                "forceBackups": true,
                "settingsNested": {"b": {"y": 2}}
            }),
        );
        assert_eq!(
            base,
            serde_json::json!({
                "type": "eam_backup",
                "scheduleMode": "OnDemand",
                "targetGateways": ["gw-b", "gw-c"],
                "targetGroups": [],
                "concurrentBackups": 2,
                "forceBackups": true,
                "settingsNested": {"a": 1, "b": {"x": 1, "y": 2}}
            })
        );
    }

    /// The composition pins (07-05 gap 3 — the live `config.settings`
    /// shape): profile carries type/scheduleMode ONLY; settings owns
    /// targetGateways/targetGroups + the K=V scalars; a bare create
    /// (no --target) defaults to the controller itself.
    #[test]
    fn composition_splits_profile_and_settings_the_live_shape() {
        // Bare create: targetGateways defaults to ["_controller"]
        // (the live-captured zero-config default on a
        // controller-mode gateway).
        let bare =
            compose_task_definition("uat-backup-demo", "eam_backup", &[], &[], None, "OnDemand")
                .expect("bare composition");
        assert_eq!(bare["name"], serde_json::json!("uat-backup-demo"));
        assert_eq!(
            bare["config"]["profile"],
            serde_json::json!({"type": "eam_backup", "scheduleMode": "OnDemand"}),
            "profile carries type + scheduleMode ONLY (isSuspended is server-owned)"
        );
        assert_eq!(
            bare["config"]["settings"],
            serde_json::json!({"targetGateways": ["_controller"], "targetGroups": []})
        );

        // Explicit --target values replace the default wholesale.
        let targeted = compose_task_definition(
            "nightly-backup",
            "eam_backup",
            &["gw-a".to_string()],
            &[
                "concurrentBackups=2".to_string(),
                "forceBackups=true".to_string(),
            ],
            None,
            "OnDemand",
        )
        .expect("targeted composition");
        assert_eq!(
            targeted["config"]["settings"]["targetGateways"],
            serde_json::json!(["gw-a"])
        );
        assert_eq!(
            targeted["config"]["settings"]["concurrentBackups"],
            serde_json::json!(2),
            "K=V lands in config.SETTINGS"
        );
        assert!(
            targeted["config"]["profile"]
                .get("concurrentBackups")
                .is_none(),
            "profile carries NO settings keys"
        );
        assert!(
            targeted["config"]["profile"]
                .get("targetGateways")
                .is_none(),
            "targetGateways lives in settings, not profile"
        );

        // The --definition overlay deep-merges over the composed
        // SETTINGS object (arrays/scalars replace, objects merge).
        let overlayed = compose_task_definition(
            "t3",
            "eam_backup",
            &["gw-a".to_string()],
            &[],
            Some(&serde_json::json!({
                "targetGateways": ["gw-b", "gw-c"],
                "concurrentBackups": 5
            })),
            "OnDemand",
        )
        .expect("overlay composition");
        assert_eq!(
            overlayed["config"]["settings"]["targetGateways"],
            serde_json::json!(["gw-b", "gw-c"]),
            "the overlay's array REPLACES the composed default"
        );
        assert_eq!(overlayed["config"]["settings"]["concurrentBackups"], 5);
        assert_eq!(
            overlayed["config"]["settings"]["targetGroups"],
            serde_json::json!([]),
            "composed keys the overlay omits survive the merge"
        );
    }

    /// The summary projection carries the profile type/scheduleMode
    /// and degrades to nulls when the list shape carries neither the
    /// state nor a profile.
    #[test]
    fn summary_projects_the_agent_stable_keys() {
        let record: EamTaskRecord = serde_json::from_value(serde_json::json!({
            "name": "nightly-backup",
            "config": {"profile": {"type": "eam_backup", "scheduleMode": "OnDemand"}},
            "scheduledTaskState": {"currentState": "IDLE", "details": {"owner": "eam"}}
        }))
        .expect("record parses");
        let summary = summary_from(&record);
        assert_eq!(summary.name, "nightly-backup");
        assert_eq!(summary.task_type.as_deref(), Some("eam_backup"));
        assert_eq!(summary.schedule_mode.as_deref(), Some("OnDemand"));
        assert_eq!(summary.current_state.as_deref(), Some("IDLE"));

        let bare: EamTaskRecord = serde_json::from_value(serde_json::json!({
            "name": "bare"
        }))
        .expect("bare record parses");
        let summary = summary_from(&bare);
        assert_eq!(summary.task_type, None);
        assert_eq!(summary.schedule_mode, None);
        assert_eq!(summary.current_state, None);
    }

    // ---- 10-03 Task 1: the runtime lifecycle ----

    /// The lifecycle result model serializes ALL keys always (the
    /// agent-stable shape — nulls are honest absence, never omitted
    /// keys), for both a fired write and the cancel no-op.
    #[test]
    fn lifecycle_result_serializes_all_keys_always() {
        let fired = EamLifecycleResult {
            task: "nightly-backup".to_string(),
            action: "suspended".to_string(),
            previous_state: Some("Scheduled".to_string()),
            config_suspended: Some(true),
            pending: None,
            fired: true,
            reason: None,
        };
        let json = serde_json::to_value(&fired).expect("serializes");
        let map = json.as_object().expect("object shape");
        for key in [
            "task",
            "action",
            "previous_state",
            "config_suspended",
            "pending",
            "fired",
            "reason",
        ] {
            assert!(map.contains_key(key), "key {key} always rides");
        }
        assert_eq!(json["pending"], serde_json::Value::Null);
        assert_eq!(json["reason"], serde_json::Value::Null);
        assert_eq!(json["config_suspended"], serde_json::json!(true));

        let noop = EamLifecycleResult {
            task: "t".to_string(),
            action: "cancelled".to_string(),
            previous_state: None,
            config_suspended: None,
            pending: None,
            fired: false,
            reason: Some("no pending execution".to_string()),
        };
        let json = serde_json::to_value(&noop).expect("serializes");
        assert_eq!(json["fired"], serde_json::json!(false));
        assert_eq!(json["reason"], serde_json::json!("no pending execution"));
        assert_eq!(
            json["previous_state"],
            serde_json::Value::Null,
            "no state on a find that carried none — null, not omitted"
        );
    }

    /// The pure name precheck refuses empty/whitespace names exit 2
    /// (and ONLY those — every other question needs the find).
    #[test]
    fn lifecycle_precheck_refuses_empty_and_whitespace_names() {
        for name in ["", "   ", "\t\n"] {
            for action in ["suspend", "resume", "cancel"] {
                let err =
                    lifecycle_precheck(action, name).expect_err("empty/whitespace names refuse");
                assert_eq!(err.exit_code(), 2, "usage class");
                assert_eq!(err.code(), "invalid_input");
                let message = err.to_string();
                assert!(
                    message.contains(action),
                    "the refusal names the verb: {message}"
                );
            }
        }
        lifecycle_precheck("suspend", "nightly-backup").expect("real names pass");
        lifecycle_precheck("cancel", " x ")
            .expect("trimmed-nonempty passes (the gateway owns identifier rules)");
    }

    /// The suspend re-check (pure) refuses ONLY the capture-proven
    /// already-suspended case (exit 2 naming the task); false,
    /// absent, and unparseable flags all fire.
    #[test]
    fn suspend_recheck_refuses_only_already_suspended() {
        let record_of = |is_suspended: serde_json::Value| -> EamTaskRecord {
            serde_json::from_value(serde_json::json!({
                "name": "nightly-backup",
                "config": {"profile": {"isSuspended": is_suspended}}
            }))
            .expect("record parses")
        };

        let err = suspend_recheck(&record_of(serde_json::json!(true)))
            .expect_err("already-suspended refuses pre-write");
        assert_eq!(err.exit_code(), 2);
        assert_eq!(err.code(), "invalid_input");
        let message = err.to_string();
        assert!(
            message.contains("nightly-backup") && message.contains("already suspended"),
            "the refusal names the task + state: {message}"
        );

        suspend_recheck(&record_of(serde_json::json!(false)))
            .expect("false fires (the normal case)");
        suspend_recheck(&record_of(serde_json::Value::Null))
            .expect("absent flag fires (no invented rules)");
        suspend_recheck(&record_of(serde_json::json!("weird")))
            .expect("unparseable flag fires (the gateway's 500 is the honest answer)");
    }

    /// The cancel decision (pure) over the pending row — every
    /// branch mirrors a captured fact (the §2 can* truth cell, §7's
    /// silent-204 nothing-pending answer).
    #[test]
    fn cancel_decision_branches_mirror_the_captures() {
        assert_eq!(
            cancel_decision(None),
            CancelDecision::NoPending,
            "nothing pending → the honest no-op, no doomed POST"
        );

        let row_of = |can_cancel: bool| -> EamScheduledTask {
            serde_json::from_value(serde_json::json!({
                "name": "nightly-backup",
                "owner": "eam",
                "type": "Collect Backup",
                "execStart": null,
                "message": "",
                "repeats": true,
                "canPause": true,
                "canResume": false,
                "canCancel": can_cancel,
                "taskState": "Scheduled",
                "isForced": false,
                "isRunning": false,
                "progress": 0.0
            }))
            .expect("the captured row shape parses")
        };

        assert_eq!(
            cancel_decision(Some(&row_of(true))),
            CancelDecision::Fire,
            "the captured Scheduled cell (canCancel: true) fires"
        );
        assert_eq!(
            cancel_decision(Some(&row_of(false))),
            CancelDecision::NotPermitted,
            "the gateway's own canCancel=false is reported, not overridden"
        );
    }

    // ---- 10-03 Task 2: modify + delete ----

    /// The FULL-record fixture — the captured find shape (§0's key
    /// inventory: type/name/description/enabled/version/collection/
    /// collections/signature/config{profile,settings}/data/
    /// attributes/metrics/healthchecks) with the §6a baseline values.
    fn full_record_fixture() -> serde_json::Value {
        serde_json::json!({
            "type": "com.inductiveautomation.eam/eam-tasks",
            "name": "ign-p10-scratch-sched",
            "description": "scratch",
            "enabled": true,
            "version": 1,
            "collection": "core",
            "collections": ["core"],
            "signature": "e5ac8bee3a6ba85e40923c0e02d29507600c57519eb8e4d78bd8c258197fe9c6",
            "config": {
                "profile": {
                    "type": "eam_backup",
                    "isSuspended": false,
                    "scheduleMode": "Scheduled",
                    "scheduleDetails": "0/30 * * * * ?"
                },
                "settings": {
                    "targetGateways": ["_controller"],
                    "targetGroups": [],
                    "concurrentBackups": 0,
                    "forceBackups": false
                }
            },
            "data": ["config.json"],
            "attributes": {"uuid": "c1aa2b52-46ad-46ea-962b-9d2498f35db1", "enabled": true},
            "metrics": {},
            "healthchecks": {"scheduledTaskState": {"currentState": "Scheduled"}}
        })
    }

    /// THE never-compose-from-scratch invariant: the modify PUT body
    /// preserves the fixture's config.settings byte-equal on a
    /// settings-free change; ONLY targeted keys move; signature/
    /// collection/unknown round-trip keys ride the clone untouched.
    #[test]
    fn modify_put_body_preserves_the_fixture_record_except_targeted_keys() {
        // A description-only change: EVERYTHING else byte-equal.
        let mut body = full_record_fixture();
        let changed = apply_task_change(
            &mut body,
            &TaskChange {
                description: Some("rewritten note".to_string()),
                ..Default::default()
            },
        );
        assert_eq!(changed, vec!["description"]);
        let fixture = full_record_fixture();
        assert_eq!(
            body["config"]["settings"], fixture["config"]["settings"],
            "config.settings rides VERBATIM — omitting/reshaping it is the 422 trap"
        );
        assert_eq!(
            body["signature"], fixture["signature"],
            "the ORIGINAL signature"
        );
        assert_eq!(body["collection"], fixture["collection"]);
        assert_eq!(body["config"]["profile"], fixture["config"]["profile"]);
        assert_eq!(
            body["data"], fixture["data"],
            "unknown round-trip keys survive"
        );
        assert_eq!(body["attributes"], fixture["attributes"]);
        assert_eq!(body["description"], serde_json::json!("rewritten note"));

        // Targeted enabled + settings overlay: ONLY those keys move
        // (the overlay deep-merges; the rest of settings stays).
        let mut body = full_record_fixture();
        let changed = apply_task_change(
            &mut body,
            &TaskChange {
                enabled: Some(false),
                settings_overlay: Some(serde_json::json!({"concurrentBackups": 4})),
                ..Default::default()
            },
        );
        assert_eq!(changed, vec!["enabled", "config.settings"]);
        assert_eq!(body["enabled"], serde_json::json!(false));
        let mut expected_settings = fixture["config"]["settings"].clone();
        expected_settings["concurrentBackups"] = serde_json::json!(4);
        assert_eq!(body["config"]["settings"], expected_settings);
        assert_eq!(body["signature"], fixture["signature"]);

        // A schedule-mode change touches ONLY the profile's mode key.
        let mut body = full_record_fixture();
        let changed = apply_task_change(
            &mut body,
            &TaskChange {
                schedule_mode: Some("OnDemand".to_string()),
                ..Default::default()
            },
        );
        assert_eq!(changed, vec!["config.profile.scheduleMode"]);
        assert_eq!(
            body["config"]["profile"]["scheduleMode"],
            serde_json::json!("OnDemand")
        );
        assert_eq!(
            body["config"]["profile"]["scheduleDetails"],
            fixture["config"]["profile"]["scheduleDetails"],
            "scheduleDetails rides the clone (this change's scope is the mode key)"
        );
        assert_eq!(body["config"]["settings"], fixture["config"]["settings"]);
    }

    /// The modify result model serializes ALL keys always.
    #[test]
    fn modify_result_serializes_all_keys_always() {
        let result = super::EamModifyResult {
            task: "t".to_string(),
            changed: vec!["enabled".to_string()],
            definition: serde_json::json!({"name": "t"}),
            put_outcome: None,
            readback: serde_json::Value::Null,
        };
        let json = serde_json::to_value(&result).expect("serializes");
        for key in ["task", "changed", "definition", "put_outcome", "readback"] {
            assert!(
                json.as_object().unwrap().contains_key(key),
                "{key} always rides"
            );
        }
        assert_eq!(json["put_outcome"], serde_json::Value::Null);
    }

    /// The delete result's affected-names extraction: `changes[]`
    /// names are capture-proven; the `references` element shape is
    /// UNOBSERVED (§3d) — strings ride, name-keyed objects
    /// contribute, the rest is skipped honestly; dedup, wire order.
    #[test]
    fn delete_result_extracts_affected_names_from_changes_and_references() {
        let success: DeleteOutcome = serde_json::from_value(serde_json::json!({
            "success": true,
            "changes": [
                {
                    "name": "ign-p10-scratch-sched",
                    "type": "com.inductiveautomation.eam/eam-tasks",
                    "collection": "core",
                    "newSignature": "ec961ee921c63b18013094870ed2664331e965c4770fdf84bfe136e0b4164244"
                }
            ],
            "problem": null,
            "references": []
        }))
        .expect("the captured success body parses");
        assert_eq!(
            affected_resources(&success),
            vec!["ign-p10-scratch-sched".to_string()],
            "a lone-resource delete touches exactly the deleted resource"
        );

        // The confirm-demand shape is UNOBSERVED — the extraction is
        // lenient over spec-shaped reference elements (marked as
        // such; nothing here is capture-proven beyond `[]`/null).
        let demanded: DeleteOutcome = serde_json::from_value(serde_json::json!({
            "success": false,
            "changes": [
                {"name": "task-a", "type": "com.inductiveautomation.eam/eam-tasks", "collection": "core", "newSignature": "x"}
            ],
            "problem": null,
            "references": ["agent-b", {"name": "task-c"}, {"shapeless": true}, 42, "task-a"]
        }))
        .expect("the lenient shape parses");
        assert_eq!(
            affected_resources(&demanded),
            vec![
                "task-a".to_string(),
                "agent-b".to_string(),
                "task-c".to_string()
            ],
            "strings + name-keyed objects ride; shapeless elements skipped; dedup holds"
        );
    }

    // ---- 10-03 Task 3: the blast-radius preview ----

    /// The captured scheduled row, name-adjustable (the §2 verbatim
    /// shape) — the preview's pending-input fixture.
    fn scheduled_row(name: &str) -> EamScheduledTask {
        serde_json::from_value(serde_json::json!({
            "name": name,
            "owner": "eam",
            "type": "Collect Backup",
            "execStart": null,
            "message": "",
            "repeats": true,
            "canPause": true,
            "canResume": false,
            "canCancel": true,
            "taskState": "Scheduled",
            "isForced": false,
            "isRunning": false,
            "progress": 0.0
        }))
        .expect("the captured row shape parses")
    }

    /// The composer over fixture find + scheduled bodies: the task
    /// targets 2 agents with 1 pending execution; rows for OTHER
    /// tasks are filtered; the agent-stable keys ride.
    #[test]
    fn preview_composes_over_fixture_find_and_scheduled() {
        let record: EamTaskRecord = serde_json::from_value(serde_json::json!({
            "name": "ign-p10-scratch-sched",
            "config": {
                "profile": {"type": "eam_backup", "isSuspended": false, "scheduleMode": "Scheduled"},
                "settings": {"targetGateways": ["gw-a", "gw-b"], "targetGroups": []}
            },
            "signature": "sig",
            "scheduledTaskState": {"currentState": "Scheduled", "details": {"owner": "eam"}}
        }))
        .expect("fixture record parses");
        let pending = vec![
            scheduled_row("ign-p10-scratch-sched"),
            scheduled_row("some-other-task"),
        ];

        let preview = compose_blast_radius(&record, pending, "suspend");
        assert_eq!(preview.task, "ign-p10-scratch-sched");
        assert_eq!(preview.task_type.as_deref(), Some("eam_backup"));
        assert_eq!(preview.schedule_mode.as_deref(), Some("Scheduled"));
        assert_eq!(preview.state.as_deref(), Some("Scheduled"));
        assert_eq!(preview.owner.as_deref(), Some("eam"));
        assert_eq!(preview.config_suspended, Some(false));
        assert_eq!(
            preview.target_gateways,
            vec!["gw-a".to_string(), "gw-b".to_string()],
            "the AGENTS the write touches"
        );
        assert_eq!(
            preview.pending_executions.len(),
            1,
            "rows for other tasks are filtered out"
        );
        assert!(preview.pending_executions[0].can_cancel);
        assert_eq!(preview.verb, "suspend");
        let impact = &preview.controller_impact;
        assert!(
            impact.contains("ign-p10-scratch-sched")
                && impact.contains("eam_backup")
                && impact.contains("2 agents")
                && impact.contains("stop until resumed"),
            "the impact names task + type + agent count + consequence: {impact}"
        );

        // All keys always — serialization never drops a key.
        let json = serde_json::to_value(&preview).expect("serializes");
        for key in [
            "task",
            "task_type",
            "schedule_mode",
            "state",
            "owner",
            "config_suspended",
            "target_gateways",
            "pending_executions",
            "controller_impact",
            "verb",
        ] {
            assert!(
                json.as_object().unwrap().contains_key(key),
                "{key} always rides"
            );
        }
    }

    /// The empty case: a bare record (no settings, no healthcheck)
    /// and zero pending rows compose an honest preview — empty
    /// lists, nulls, and an impact that still names the task.
    #[test]
    fn preview_tolerates_empty_targets_and_pending() {
        let record: EamTaskRecord = serde_json::from_value(serde_json::json!({
            "name": "bare",
            "config": {}
        }))
        .expect("bare record parses");
        let preview = compose_blast_radius(&record, Vec::new(), "cancel");
        assert_eq!(preview.target_gateways, Vec::<String>::new());
        assert_eq!(preview.pending_executions, Vec::<EamScheduledTask>::new());
        assert_eq!(preview.task_type, None);
        assert_eq!(preview.owner, None);
        assert!(
            preview.controller_impact.contains("bare")
                && preview.controller_impact.contains("no pending execution"),
            "the cancel impact names the empty case factually: {}",
            preview.controller_impact
        );
    }

    /// Per-verb impact sentences DIFFER where the verbs differ —
    /// one composer, six honest sentences (plus the fallback).
    #[test]
    fn preview_impacts_differ_per_verb() {
        let record: EamTaskRecord = serde_json::from_value(serde_json::json!({
            "name": "nightly-backup",
            "config": {
                "profile": {"type": "eam_backup", "scheduleMode": "Scheduled"},
                "settings": {"targetGateways": ["gw-a"]}
            },
            "scheduledTaskState": {"currentState": "Scheduled", "details": {"owner": "eam"}}
        }))
        .expect("fixture record parses");
        let pending = vec![scheduled_row("nightly-backup")];

        let mut impacts = Vec::new();
        for verb in ["suspend", "resume", "cancel", "force", "modify", "delete"] {
            let preview = compose_blast_radius(&record, pending.clone(), verb);
            let impact = preview.controller_impact.clone();
            assert!(
                impact.contains("nightly-backup") && impact.contains("1 agent"),
                "{verb}'s impact names the task + agent count: {impact}"
            );
            impacts.push(impact);
        }
        let distinct: std::collections::BTreeSet<&String> = impacts.iter().collect();
        assert_eq!(
            distinct.len(),
            impacts.len(),
            "each verb's factual sentence is distinct: {impacts:?}"
        );
        // The pending-aware cancel names the execution count; the
        // empty-pending cancel says so.
        let cancel_with = compose_blast_radius(&record, pending.clone(), "cancel");
        assert!(cancel_with.controller_impact.contains("1 queued dispatch"));
        let cancel_without = compose_blast_radius(&record, Vec::new(), "cancel");
        assert!(
            cancel_without
                .controller_impact
                .contains("no pending execution")
        );
        // Unknown verbs get the factual fallback (never a panic).
        let odd = compose_blast_radius(&record, Vec::new(), "teleport");
        assert!(odd.controller_impact.contains("teleport"));
    }

    /// THE render pin: `"{verb} {task}: {impact} targets: [a, b]
    /// pending: {n}"` — the ONE line both the CLI refusal and the
    /// future TUI body embed.
    #[test]
    fn render_preview_line_contains_task_agents_verb_and_pending() {
        let record: EamTaskRecord = serde_json::from_value(serde_json::json!({
            "name": "ign-p10-scratch",
            "config": {
                "profile": {"type": "eam_backup", "scheduleMode": "OnDemand"},
                "settings": {"targetGateways": ["_controller"]}
            }
        }))
        .expect("fixture record parses");
        let preview = compose_blast_radius(
            &record,
            vec![
                scheduled_row("ign-p10-scratch"),
                scheduled_row("ign-p10-scratch"),
            ],
            "delete",
        );
        assert_eq!(
            preview.pending_executions.len(),
            2,
            "same-name rows from both segments both count"
        );
        let line = render_preview_line(&preview);
        assert_eq!(
            line,
            "delete ign-p10-scratch: deletes task ign-p10-scratch (eam_backup) permanently \
             — dispatches to 1 agent stop targets: [_controller] pending: 2"
        );
        assert!(line.contains("ign-p10-scratch"));
        assert!(line.contains("1 agent"));

        // Empty targets render as empty brackets (never "[ ]" or a
        // dropped key — the format is agent-stable).
        let bare = compose_blast_radius(
            &serde_json::from_value::<EamTaskRecord>(serde_json::json!({
                "name": "bare", "config": {}
            }))
            .expect("bare parses"),
            Vec::new(),
            "resume",
        );
        assert_eq!(
            render_preview_line(&bare),
            "resume bare: resumes task bare — scheduled dispatches to 0 agents can fire again targets: [] pending: 0"
        );
    }
}