rkat 0.8.1

CLI for the Meerkat agent platform — run LLM agents from the terminal
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
#![cfg(feature = "integration-real-tests")]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::path::{Path, PathBuf};
use std::process::Stdio;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
use tokio::time::{Duration, timeout};

use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use tempfile::TempDir;

fn rkat_binary_path() -> Option<PathBuf> {
    if let Some(path) = std::env::var_os("CARGO_BIN_EXE_rkat") {
        let path = PathBuf::from(path);
        if path.exists() {
            return Some(path.canonicalize().unwrap_or(path));
        }
    }

    if let Some(target_dir) = std::env::var_os("CARGO_TARGET_DIR") {
        let target_dir = PathBuf::from(target_dir);
        let debug = target_dir.join("debug/rkat");
        if debug.exists() {
            return Some(debug);
        }
        let release = target_dir.join("release/rkat");
        if release.exists() {
            return Some(release);
        }
    }

    let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let workspace_root = manifest_dir.parent()?;
    let codex_debug = workspace_root.join("target-codex/debug/rkat");
    if codex_debug.exists() {
        return Some(codex_debug);
    }
    let codex_release = workspace_root.join("target-codex/release/rkat");
    if codex_release.exists() {
        return Some(codex_release);
    }
    let debug = workspace_root.join("target/debug/rkat");
    if debug.exists() {
        return Some(debug);
    }
    let release = workspace_root.join("target/release/rkat");
    if release.exists() {
        return Some(release);
    }
    None
}

fn first_env(vars: &[&str]) -> Option<String> {
    for name in vars {
        if let Ok(value) = std::env::var(name)
            && !value.is_empty()
        {
            return Some(value);
        }
    }
    None
}

fn anthropic_api_key() -> Option<String> {
    first_env(&["RKAT_ANTHROPIC_API_KEY", "ANTHROPIC_API_KEY"])
}

fn smoke_model() -> String {
    std::env::var("SMOKE_MODEL").unwrap_or_else(|_| "claude-sonnet-4-5".to_string())
}

fn skip_if_no_prereqs() -> bool {
    if rkat_binary_path().is_none() {
        eprintln!("Skipping: rkat binary not found (build with `cargo build -p meerkat-cli`)");
        return true;
    }
    false
}

fn skip_if_no_api_prereqs() -> bool {
    if skip_if_no_prereqs() {
        return true;
    }
    if anthropic_api_key().is_none() {
        eprintln!(
            "Skipping: no Anthropic API key (set ANTHROPIC_API_KEY or RKAT_ANTHROPIC_API_KEY)"
        );
        return true;
    }
    false
}

async fn write_mobpack_fixture(project_dir: &Path) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mob_dir = project_dir.join("mobpack-fixture");
    tokio::fs::create_dir_all(mob_dir.join("skills")).await?;

    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        "[mobpack]\nname = \"smoke-mobpack\"\nversion = \"1.0.0\"\n",
    )
    .await?;

    let definition = format!(
        r#"{{
  "id":"smoke-mobpack",
  "orchestrator":{{"profile":"lead"}},
  "profiles":{{
    "lead":{{
      "model":"{}",
      "skills":[],
      "tools":{{"comms":true}},
      "peer_description":"Lead",
      "external_addressable":true
    }}
  }},
  "skills":{{}}
}}"#,
        smoke_model()
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition).await?;
    tokio::fs::write(mob_dir.join("skills").join("review.md"), "# Review\n").await?;

    Ok(mob_dir)
}

async fn run_rkat(
    rkat: &Path,
    cwd: &Path,
    args: &[&str],
    api_key: Option<&str>,
) -> Result<std::process::Output, Box<dyn std::error::Error>> {
    let mut cmd = Command::new(rkat);
    cmd.current_dir(cwd)
        .env("HOME", cwd)
        .env("XDG_DATA_HOME", cwd.join("data"))
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    if let Some(key) = api_key {
        cmd.env("ANTHROPIC_API_KEY", key)
            .env("RKAT_ANTHROPIC_API_KEY", key);
    }
    cmd.args(args);
    let output = timeout(Duration::from_secs(180), cmd.output()).await??;
    Ok(output)
}

fn output_ok_or_err(output: std::process::Output, args: &[&str]) -> Result<String, String> {
    if !output.status.success() {
        return Err(format!(
            "command failed (exit {:?}): rkat {}\nstdout:\n{}\nstderr:\n{}",
            output.status.code(),
            args.join(" "),
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        ));
    }
    Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn leading_json(stdout: &str) -> Result<Value, Box<dyn std::error::Error>> {
    let json_text = stdout
        .split("\nwarning\t")
        .next()
        .ok_or("missing leading JSON output")?
        .trim();
    Ok(serde_json::from_str(json_text)?)
}

fn signer_from_pack(pack_bytes: &[u8]) -> Result<(String, String), Box<dyn std::error::Error>> {
    let files = meerkat_mob_pack::targz::extract_targz_safe(pack_bytes)?;
    let sig = files
        .get("signature.toml")
        .ok_or("missing signature.toml")?;
    let value: toml::Value = toml::from_str(std::str::from_utf8(sig)?)?;
    let signer_id = value
        .get("signer_id")
        .and_then(toml::Value::as_str)
        .ok_or("missing signer_id")?
        .to_string();
    let public_key = value
        .get("public_key")
        .and_then(toml::Value::as_str)
        .ok_or("missing public_key")?
        .to_string();
    Ok((signer_id, public_key))
}

async fn write_rpc_state_probe_mobpack_fixture(
    project_dir: &Path,
    mob_id: &str,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mob_dir = project_dir.join(format!("{mob_id}-fixture"));
    tokio::fs::create_dir_all(&mob_dir).await?;
    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        format!("[mobpack]\nname = \"{mob_id}\"\nversion = \"1.0.0\"\n"),
    )
    .await?;

    let definition = format!(
        r#"{{
  "id":"{mob_id}",
  "profiles":{{
    "lead":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "external_addressable":true,
      "peer_description":"Lead coordinator"
    }},
    "worker":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "external_addressable":true,
      "peer_description":"Worker specialist"
    }},
    "reviewer":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "peer_description":"Review specialist"
    }}
  }},
  "wiring":{{
    "auto_wire_orchestrator":false,
    "role_wiring":[{{"a":"lead","b":"worker"}},{{"a":"worker","b":"reviewer"}}]
  }},
  "skills":{{}}
}}"#,
        model = smoke_model()
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition).await?;
    Ok(mob_dir)
}

async fn write_flow_probe_mobpack_fixture(
    project_dir: &Path,
    mob_id: &str,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mob_dir = project_dir.join(format!("{mob_id}-fixture"));
    tokio::fs::create_dir_all(&mob_dir).await?;
    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        format!("[mobpack]\nname = \"{mob_id}\"\nversion = \"1.0.0\"\n"),
    )
    .await?;

    let definition = format!(
        r#"{{
  "id":"{mob_id}",
  "profiles":{{
    "lead":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "external_addressable":true,
      "peer_description":"Lead synthesizer"
    }},
    "analyst":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "peer_description":"Analyst"
    }},
    "reviewer":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "peer_description":"Reviewer"
    }}
  }},
  "wiring":{{
    "auto_wire_orchestrator":false,
    "role_wiring":[{{"a":"lead","b":"analyst"}},{{"a":"lead","b":"reviewer"}},{{"a":"analyst","b":"reviewer"}}]
  }},
  "flows":{{
    "main":{{
      "description":"Three-step swarm synthesis smoke",
      "steps":{{
        "analyze":{{
          "role":"analyst",
          "message":"Analyze the assigned task and include the literal token ANALYZE_OK in your answer.",
          "timeout_ms":120000
        }},
        "review":{{
          "role":"reviewer",
          "message":"Review the prior analysis and include the literal token REVIEW_OK in your answer.",
          "depends_on":["analyze"],
          "timeout_ms":120000
        }},
        "synthesize":{{
          "role":"lead",
          "message":"Synthesize the prior outputs and include the literal token FLOW_MATRIX_23 in your answer.",
          "depends_on":["review"],
          "timeout_ms":120000
        }}
      }}
    }}
  }},
  "skills":{{}}
}}"#,
        model = smoke_model()
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition).await?;
    Ok(mob_dir)
}

async fn write_callable_flow_mobpack_fixture(
    project_dir: &Path,
    mob_id: &str,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mob_dir = project_dir.join(format!("{mob_id}-fixture"));
    tokio::fs::create_dir_all(&mob_dir).await?;
    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        format!("[mobpack]\nname = \"{mob_id}\"\nversion = \"1.0.0\"\n"),
    )
    .await?;

    let definition = format!(
        r#"{{
  "id":"{mob_id}",
  "profiles":{{
    "worker":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "peer_description":"Callable smoke worker"
    }}
  }},
  "flows":{{
    "main":{{
      "description":"Single-step callable smoke",
      "steps":{{
        "answer":{{
          "role":"worker",
          "message":"The caller prompt is: {{ params.prompt }}. Reply in one sentence and include the literal tokens CALLABLE_FLOW_OK and CALLABLE_PROMPT_NONCE_91 exactly once.",
          "output_format":"text",
          "collection_policy":{{"type":"any"}},
          "timeout_ms":120000
        }}
      }}
    }}
  }},
  "skills":{{}}
}}"#,
        model = smoke_model()
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition).await?;
    Ok(mob_dir)
}

async fn write_adaptive_finish_mobpack_fixture(
    project_dir: &Path,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mob_dir = project_dir.join("adaptive-finish-fixture");
    tokio::fs::create_dir_all(mob_dir.join("adaptive")).await?;
    tokio::fs::create_dir_all(mob_dir.join("schemas")).await?;

    let policy = r"[limits]
max_depth = 3
max_total_decisions = 3
max_repair_attempts = 2
max_layer_failures = 2
max_attempts_per_layer = 1
max_members_per_layer = 4
max_total_spawned_members = 8
max_active_members = 4
max_retained_layer_mobs = 1
max_wall_clock_ms = 180000
max_aggregate_tokens = 200000
max_aggregate_tool_calls = 100
";
    let policy_digest = format!("sha256:{:x}", Sha256::digest(policy.as_bytes()));

    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        format!(
            r#"[mobpack]
name = "adaptive-smoke"
version = "1.0.0"

[adaptive]
flowmaster_profile = "flowmaster"
objective_class = "smoke"
policy_digest = "{policy_digest}"
"#
        ),
    )
    .await?;
    tokio::fs::write(mob_dir.join("adaptive").join("policies.toml"), policy).await?;
    tokio::fs::write(
        mob_dir.join("adaptive").join("flowmaster.prompt.md"),
        "Return only typed Adaptive Flow JSON decisions.\n",
    )
    .await?;
    // No hand-written adaptive/layer-decision.schema.json: `rkat mob pack`
    // emits the canonical LayerDecision schema into the archive.
    tokio::fs::write(mob_dir.join("schemas").join("registry.json"), "{}").await?;

    let definition = format!(
        r#"{{
  "id":"adaptive-smoke",
  "profiles":{{
    "flowmaster":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "external_addressable":true,
      "peer_description":"Adaptive FlowMaster planner"
    }}
  }},
  "wiring":{{"auto_wire_orchestrator":false,"role_wiring":[]}},
  "flows":{{
    "plan":{{
      "description":"Adaptive smoke planning turn",
      "steps":{{
        "plan":{{
          "role":"flowmaster",
          "message":"Return only valid JSON, with no markdown. Read this objective exactly: {{{{params.objective}}}}. The top-level JSON object must have decision set to finish, reason set to smoke complete followed by the objective nonce, and result set to an object whose only field is result. That nested result object must have nonce copied from the objective nonce and summary copied from the objective summary. Do not invent the nonce or summary.",
          "output_format":"json",
          "collection_policy":{{"type":"any"}},
          "timeout_ms":120000
        }}
      }}
    }}
  }},
  "skills":{{}}
}}"#,
        model = smoke_model()
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition).await?;
    Ok(mob_dir)
}

async fn write_turn_probe_mobpack_fixture(
    project_dir: &Path,
    mob_id: &str,
) -> Result<PathBuf, Box<dyn std::error::Error>> {
    let mob_dir = project_dir.join(format!("{mob_id}-fixture"));
    tokio::fs::create_dir_all(&mob_dir).await?;
    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        format!("[mobpack]\nname = \"{mob_id}\"\nversion = \"1.0.0\"\n"),
    )
    .await?;

    let definition = format!(
        r#"{{
  "id":"{mob_id}",
  "profiles":{{
    "worker":{{
      "model":"{model}",
      "tools":{{"comms":true}},
      "external_addressable":true,
      "peer_description":"Turn-driven worker"
    }}
  }},
  "wiring":{{"auto_wire_orchestrator":false,"role_wiring":[]}},
  "skills":{{}}
}}"#,
        model = smoke_model()
    );
    tokio::fs::write(mob_dir.join("definition.json"), definition).await?;
    Ok(mob_dir)
}

struct RpcSurfaceChild {
    child: Child,
    stdin: ChildStdin,
    stdout: BufReader<ChildStdout>,
}

async fn spawn_mob_rpc_surface(
    rkat: &Path,
    cwd: &Path,
    pack: &Path,
    prompt: &str,
    api_key: Option<&str>,
) -> Result<RpcSurfaceChild, Box<dyn std::error::Error>> {
    let mut cmd = Command::new(rkat);
    cmd.current_dir(cwd)
        .env("HOME", cwd)
        .env("XDG_DATA_HOME", cwd.join("data"))
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .args([
            "mob",
            "deploy",
            &pack.display().to_string(),
            prompt,
            "--surface",
            "rpc",
            "--trust-policy",
            "permissive",
        ]);
    if let Some(key) = api_key {
        cmd.env("ANTHROPIC_API_KEY", key)
            .env("RKAT_ANTHROPIC_API_KEY", key);
    }
    let mut child = cmd.spawn()?;
    let stdin = child.stdin.take().ok_or("missing child stdin")?;
    let stdout = child.stdout.take().ok_or("missing child stdout")?;
    Ok(RpcSurfaceChild {
        child,
        stdin,
        stdout: BufReader::new(stdout),
    })
}

async fn rpc_send(
    surface: &mut RpcSurfaceChild,
    request: &Value,
) -> Result<(), Box<dyn std::error::Error>> {
    let line = format!("{}\n", serde_json::to_string(request)?);
    surface.stdin.write_all(line.as_bytes()).await?;
    surface.stdin.flush().await?;
    Ok(())
}

async fn rpc_read_response(
    surface: &mut RpcSurfaceChild,
    timeout_secs: u64,
) -> Result<Value, Box<dyn std::error::Error>> {
    loop {
        let mut line = String::new();
        timeout(
            Duration::from_secs(timeout_secs),
            surface.stdout.read_line(&mut line),
        )
        .await??;
        if line.trim().is_empty() {
            continue;
        }
        // Skip non-JSON lines (e.g. deploy status output) gracefully.
        let parsed: Value = match serde_json::from_str(line.trim()) {
            Ok(value) => value,
            Err(_) => continue,
        };
        if parsed.get("id").is_some() {
            return Ok(parsed);
        }
    }
}

async fn rpc_call(
    surface: &mut RpcSurfaceChild,
    id: u64,
    method: &str,
    params: Value,
    timeout_secs: u64,
) -> Result<Value, Box<dyn std::error::Error>> {
    rpc_send(
        surface,
        &json!({
            "jsonrpc": "2.0",
            "id": id,
            "method": method,
            "params": params,
        }),
    )
    .await?;
    let response = rpc_read_response(surface, timeout_secs).await?;
    if !response["error"].is_null() {
        return Err(format!("rpc {method} failed: {response}").into());
    }
    Ok(response["result"].clone())
}

async fn shutdown_rpc_surface(
    mut surface: RpcSurfaceChild,
) -> Result<(), Box<dyn std::error::Error>> {
    drop(surface.stdin);
    let status = timeout(Duration::from_secs(20), surface.child.wait()).await??;
    if !status.success() {
        return Err(format!("rpc surface exited unsuccessfully: {status}").into());
    }
    Ok(())
}

async fn poll_flow_status_until_terminal(
    surface: &mut RpcSurfaceChild,
    mob_id: &str,
    run_id: &str,
) -> Result<Value, Box<dyn std::error::Error>> {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(180);
    let mut request_id = 10_000u64;
    loop {
        let status = rpc_call(
            surface,
            request_id,
            "mob/flow_status",
            json!({
                "mob_id": mob_id,
                "run_id": run_id,
            }),
            30,
        )
        .await?;
        request_id += 1;
        let Some(run) = status.get("run") else {
            return Err("mob/flow_status returned no run payload".into());
        };
        let state = run
            .get("status")
            .and_then(Value::as_str)
            .ok_or("mob/flow_status missing run.status")?;
        if matches!(state, "completed" | "failed" | "canceled") {
            return Ok(status);
        }
        if tokio::time::Instant::now() >= deadline {
            return Err(format!("flow {run_id} did not reach terminal state: {status}").into());
        }
        tokio::time::sleep(Duration::from_millis(250)).await;
    }
}

async fn poll_members_until(
    surface: &mut RpcSurfaceChild,
    mob_id: &str,
    predicate: impl Fn(&Value) -> bool,
) -> Result<Value, Box<dyn std::error::Error>> {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(30);
    let mut request_id = 20_000u64;
    loop {
        let members = rpc_call(
            surface,
            request_id,
            "mob/members",
            json!({ "mob_id": mob_id }),
            15,
        )
        .await?;
        request_id += 1;
        if predicate(&members) {
            return Ok(members);
        }
        if tokio::time::Instant::now() >= deadline {
            return Err(format!("mob/members predicate did not converge: {members}").into());
        }
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
}

#[tokio::test]
#[ignore = "lane:e2e-system"]
async fn e2e_smoke_mobpack_pack_inspect_validate() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_prereqs() {
        return Ok(());
    }

    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = write_mobpack_fixture(&project_dir).await?;
    let pack = project_dir.join("smoke.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, None).await?;
    let pack_stdout = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;
    assert!(
        !pack_stdout.is_empty(),
        "mob pack should print digest output, got empty stdout"
    );

    let inspect_args = [
        "mob".to_string(),
        "inspect".to_string(),
        pack.display().to_string(),
    ];
    let inspect_refs: Vec<&str> = inspect_args.iter().map(String::as_str).collect();
    let inspect_out = run_rkat(&rkat, &project_dir, &inspect_refs, None).await?;
    let inspect_stdout =
        output_ok_or_err(inspect_out, &inspect_refs).map_err(std::io::Error::other)?;
    assert!(
        inspect_stdout.contains("name\tsmoke-mobpack") && inspect_stdout.contains("digest\t"),
        "inspect output missing expected fields: {inspect_stdout}"
    );

    let validate_args = [
        "mob".to_string(),
        "validate".to_string(),
        pack.display().to_string(),
        // The fixture is a deliberately unsigned dev pack; this test covers
        // the inspect/validate SURFACE, not trust. Strict rejection of
        // unsigned packs is covered by the signed-strict scenarios.
        "--trust-policy".to_string(),
        "permissive".to_string(),
    ];
    let validate_refs: Vec<&str> = validate_args.iter().map(String::as_str).collect();
    let validate_out = run_rkat(&rkat, &project_dir, &validate_refs, None).await?;
    let validate_stdout =
        output_ok_or_err(validate_out, &validate_refs).map_err(std::io::Error::other)?;
    assert!(
        validate_stdout.starts_with("valid\t"),
        "validate output should start with valid<TAB>, got: {validate_stdout}"
    );

    Ok(())
}

#[tokio::test]
#[ignore = "lane:e2e-live"]
async fn e2e_smoke_mobpack_deploy_unsigned_permissive_live()
-> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let api_key = anthropic_api_key().ok_or("missing API key")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = write_mobpack_fixture(&project_dir).await?;
    let pack = project_dir.join("unsigned.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let deploy_args = [
        "mob".to_string(),
        "deploy".to_string(),
        pack.display().to_string(),
        "Reply with OK".to_string(),
        "--trust-policy".to_string(),
        "permissive".to_string(),
    ];
    let deploy_refs: Vec<&str> = deploy_args.iter().map(String::as_str).collect();
    let deploy_out = run_rkat(&rkat, &project_dir, &deploy_refs, Some(&api_key)).await?;
    let deploy_stdout =
        output_ok_or_err(deploy_out, &deploy_refs).map_err(std::io::Error::other)?;
    assert!(
        deploy_stdout.contains("deployed\tmob=smoke-mobpack\tsurface=cli"),
        "deploy output missing expected deployment marker: {deploy_stdout}"
    );
    assert!(
        deploy_stdout.contains("warning\tunsigned pack accepted in permissive mode"),
        "permissive unsigned warning expected: {deploy_stdout}"
    );

    Ok(())
}

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_smoke_mobpack_callable_flow_run_live() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let api_key = anthropic_api_key().ok_or("missing API key")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = write_callable_flow_mobpack_fixture(&project_dir, "callable-flow-smoke").await?;
    let pack = project_dir.join("callable-flow.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let run_args = [
        "mob".to_string(),
        "run".to_string(),
        pack.display().to_string(),
        "--flow".to_string(),
        "main".to_string(),
        "--prompt".to_string(),
        "Produce the required smoke tokens for CALLABLE_PROMPT_NONCE_91.".to_string(),
        "--trust-policy".to_string(),
        "permissive".to_string(),
        "--json".to_string(),
    ];
    let run_refs: Vec<&str> = run_args.iter().map(String::as_str).collect();
    let run_out = run_rkat(&rkat, &project_dir, &run_refs, Some(&api_key)).await?;
    let run_stderr = String::from_utf8_lossy(&run_out.stderr).to_string();
    let run_stdout = output_ok_or_err(run_out, &run_refs).map_err(std::io::Error::other)?;
    let envelope = leading_json(&run_stdout)?;
    assert_eq!(envelope["mob_id"], "callable-flow-smoke");
    assert_eq!(envelope["flow_id"], "main");
    assert_eq!(
        envelope["status"], "completed",
        "callable flow smoke should complete; stdout:\n{run_stdout}\nstderr:\n{run_stderr}"
    );
    assert!(
        envelope["result"].to_string().contains("CALLABLE_FLOW_OK"),
        "typed run envelope should include callable smoke token: {run_stdout}"
    );
    assert!(
        envelope["result"]
            .to_string()
            .contains("CALLABLE_PROMPT_NONCE_91"),
        "typed run envelope should prove --prompt was bound to params.prompt: {run_stdout}"
    );
    assert!(
        run_stdout.contains("warning\tunsigned pack accepted in permissive mode"),
        "permissive unsigned warning expected: {run_stdout}"
    );

    Ok(())
}

async fn run_adaptive_finish_smoke(
    prompt: &str,
    expected_nonce: &str,
    expected_summary: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let api_key = anthropic_api_key().ok_or("missing API key")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = write_adaptive_finish_mobpack_fixture(&project_dir).await?;
    let pack = project_dir.join("adaptive.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let run_args = [
        "mob".to_string(),
        "run".to_string(),
        pack.display().to_string(),
        "--prompt".to_string(),
        prompt.to_string(),
        "--trust-policy".to_string(),
        "permissive".to_string(),
        "--json".to_string(),
    ];
    let run_refs: Vec<&str> = run_args.iter().map(String::as_str).collect();
    let run_out = run_rkat(&rkat, &project_dir, &run_refs, Some(&api_key)).await?;
    let run_stdout = output_ok_or_err(run_out, &run_refs).map_err(std::io::Error::other)?;
    let envelope = leading_json(&run_stdout)?;
    assert!(
        envelope["run_id"]
            .as_str()
            .is_some_and(|value| !value.is_empty()),
        "adaptive run output missing generic run_id: {run_stdout}"
    );
    assert_eq!(envelope["mob_id"], "adaptive-smoke");
    assert_eq!(envelope["status"], "completed");
    assert!(
        envelope["result_digest"]
            .as_str()
            .is_some_and(|value| value.starts_with("sha256:")),
        "run output missing result digest: {run_stdout}"
    );
    assert!(
        !run_stdout
            .lines()
            .any(|line| line.starts_with("adaptive\t")),
        "run output leaked adaptive surface marker: {run_stdout}"
    );
    let result = envelope
        .get("result")
        .ok_or("run output missing result envelope field")?;
    assert_eq!(
        result.get("nonce").and_then(Value::as_str),
        Some(expected_nonce),
        "adaptive result did not preserve prompt nonce: {run_stdout}"
    );
    assert_eq!(
        result.get("summary").and_then(Value::as_str),
        Some(expected_summary),
        "adaptive result did not preserve prompt summary: {run_stdout}"
    );
    assert!(
        run_stdout.contains("warning\tunsigned pack accepted in permissive mode"),
        "permissive unsigned warning expected: {run_stdout}"
    );

    Ok(())
}

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_smoke_s88_adaptive_mobpack_finish_decision_live()
-> Result<(), Box<dyn std::error::Error>> {
    run_adaptive_finish_smoke(
        "Finish the adaptive smoke run. nonce=ADAPTIVE_SMOKE_ALPHA summary=prompt-alpha-ok.",
        "ADAPTIVE_SMOKE_ALPHA",
        "prompt-alpha-ok",
    )
    .await
}

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_smoke_s88_adaptive_mobpack_context_finish_decision_live()
-> Result<(), Box<dyn std::error::Error>> {
    run_adaptive_finish_smoke(
        "Finish after considering this context: operator asked for a one-turn smoke proof. nonce=ADAPTIVE_SMOKE_BETA summary=context-beta-ok.",
        "ADAPTIVE_SMOKE_BETA",
        "context-beta-ok",
    )
    .await
}

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_scenario_28_cli_mobpack_deploy_signed_strict_live()
-> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let api_key = anthropic_api_key().ok_or("missing API key")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = write_mobpack_fixture(&project_dir).await?;
    let pack = project_dir.join("signed.mobpack");
    let key_path = project_dir.join("signing.key");
    tokio::fs::write(
        &key_path,
        "0707070707070707070707070707070707070707070707070707070707070707",
    )
    .await?;
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
        "--sign".to_string(),
        key_path.display().to_string(),
        "--signer-id".to_string(),
        "smoke-ci".to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let pack_bytes = tokio::fs::read(&pack).await?;
    let (signer_id, public_key) = signer_from_pack(&pack_bytes)?;
    let trust_path = project_dir.join(".rkat").join("trusted-signers.toml");
    tokio::fs::create_dir_all(trust_path.parent().ok_or("missing trust parent")?).await?;
    tokio::fs::write(
        &trust_path,
        format!("[signers]\n{signer_id} = \"{public_key}\"\n"),
    )
    .await?;

    let deploy_args = [
        "mob".to_string(),
        "deploy".to_string(),
        pack.display().to_string(),
        "Reply with OK".to_string(),
        "--trust-policy".to_string(),
        "strict".to_string(),
    ];
    let deploy_refs: Vec<&str> = deploy_args.iter().map(String::as_str).collect();
    let deploy_out = run_rkat(&rkat, &project_dir, &deploy_refs, Some(&api_key)).await?;
    let deploy_stdout =
        output_ok_or_err(deploy_out, &deploy_refs).map_err(std::io::Error::other)?;
    assert!(
        deploy_stdout.contains("deployed\tmob=smoke-mobpack\tsurface=cli"),
        "strict signed deploy output missing expected marker: {deploy_stdout}"
    );
    assert!(
        !deploy_stdout.contains("\nwarning\t"),
        "strict signed deploy should not emit warnings: {deploy_stdout}"
    );

    Ok(())
}

/// Locate the repo's built wasm32 runtime artifact for `mob web build --wasm`.
/// The CLI deliberately refuses to emit placeholder bundles, so the surface
/// tests need the real artifact from the meerkat-web-runtime pipeline; when
/// it has not been built locally, skip (mirrors the no-API-key skip).
fn prebuilt_web_runtime_wasm() -> Option<std::path::PathBuf> {
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent()?;
    let path = root.join("sdks/web/wasm/meerkat_web_runtime_bg.wasm");
    path.exists().then_some(path)
}

#[tokio::test]
#[ignore = "lane:e2e-system"]
async fn e2e_smoke_wasm_surface_gate() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_prereqs() {
        return Ok(());
    }
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = write_mobpack_fixture(&project_dir).await?;
    let pack = project_dir.join("wasm-smoke.mobpack");

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, None).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let Some(runtime_wasm) = prebuilt_web_runtime_wasm() else {
        eprintln!("Skipping: sdks/web/wasm runtime artifact not built");
        return Ok(());
    };
    let wasm_args = [
        "mob".to_string(),
        "web".to_string(),
        "build".to_string(),
        pack.display().to_string(),
        "--trust-policy".to_string(),
        "permissive".to_string(),
        "--wasm".to_string(),
        runtime_wasm.display().to_string(),
        "-o".to_string(),
        project_dir.join("web-out").display().to_string(),
    ];
    let wasm_refs: Vec<&str> = wasm_args.iter().map(String::as_str).collect();
    let wasm_out = run_rkat(&rkat, &project_dir, &wasm_refs, None).await?;
    if !wasm_out.status.success() {
        return Err(format!(
            "wasm smoke command failed\nstdout:\n{}\nstderr:\n{}",
            String::from_utf8_lossy(&wasm_out.stdout),
            String::from_utf8_lossy(&wasm_out.stderr)
        )
        .into());
    }
    let out_dir = project_dir.join("web-out");
    for file in [
        "index.html",
        "meerkat-bootstrap.js",
        "meerkat_web_runtime.js",
        "meerkat_web_runtime_bg.wasm",
        "mobpack.bin",
        "manifest.web.toml",
    ] {
        assert!(
            out_dir.join(file).exists(),
            "wasm build succeeded but artifact is missing: {}",
            out_dir.join(file).display()
        );
    }
    Ok(())
}

#[tokio::test]
#[ignore = "lane:e2e-system"]
async fn e2e_smoke_wasm_forbidden_capability_rejected() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_prereqs() {
        return Ok(());
    }
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_dir = project_dir.join("forbidden-mobpack");
    tokio::fs::create_dir_all(mob_dir.join("skills")).await?;
    tokio::fs::write(
        mob_dir.join("manifest.toml"),
        r#"[mobpack]
name = "forbidden-web"
version = "1.0.0"

[requires]
capabilities = ["shell"]
"#,
    )
    .await?;
    tokio::fs::write(
        mob_dir.join("definition.json"),
        br#"{"id":"forbidden-web"}"#,
    )
    .await?;
    let pack = project_dir.join("forbidden-web.mobpack");

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, None).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let wasm_args = [
        "mob".to_string(),
        "web".to_string(),
        "build".to_string(),
        pack.display().to_string(),
        "--trust-policy".to_string(),
        "permissive".to_string(),
        "-o".to_string(),
        project_dir.join("web-out").display().to_string(),
    ];
    let wasm_refs: Vec<&str> = wasm_args.iter().map(String::as_str).collect();
    let wasm_out = run_rkat(&rkat, &project_dir, &wasm_refs, None).await?;
    assert!(
        !wasm_out.status.success(),
        "web build should reject forbidden capabilities"
    );
    let stderr = String::from_utf8_lossy(&wasm_out.stderr);
    assert!(
        stderr.contains("forbidden capability 'shell' is not allowed for web builds"),
        "unexpected stderr: {stderr}"
    );
    Ok(())
}

// ===========================================================================
// Supplemental: CLI mob RPC surface state-machine probe
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-live"]
async fn e2e_cli_mob_rpc_state_machine_probe() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_id = "scenario-29-swarm";
    let mob_dir = write_rpc_state_probe_mobpack_fixture(&project_dir, mob_id).await?;
    let pack = project_dir.join("scenario-29.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, None).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let mut surface = spawn_mob_rpc_surface(&rkat, &project_dir, &pack, "bootstrap", None).await?;

    let initialize = rpc_call(&mut surface, 1, "initialize", json!({}), 20).await?;
    let methods = initialize["methods"]
        .as_array()
        .ok_or("initialize must return methods array")?;
    assert!(
        methods
            .iter()
            .any(|value| value.as_str() == Some("mob/status"))
            && methods
                .iter()
                .any(|value| value.as_str() == Some("mob/flow_run")),
        "initialize missing mob methods: {initialize}"
    );

    let listed = rpc_call(&mut surface, 2, "mob/list", json!({}), 15).await?;
    let mobs = listed["mobs"]
        .as_array()
        .ok_or("mob/list missing mobs array")?;
    assert!(
        mobs.iter()
            .any(|entry| entry["mob_id"].as_str() == Some(mob_id) && entry["status"] == "Running"),
        "deployed mob should be visible and running: {listed}"
    );

    let spawned = rpc_call(
        &mut surface,
        3,
        "mob/spawn_many",
        json!({
            "mob_id": mob_id,
            "specs": [
                {"profile":"lead","agent_identity":"lead-1","runtime_mode":"turn_driven"},
                {"profile":"worker","agent_identity":"worker-1","runtime_mode":"turn_driven"},
                {"profile":"reviewer","agent_identity":"reviewer-1","runtime_mode":"turn_driven"}
            ]
        }),
        30,
    )
    .await?;
    let results = spawned["results"]
        .as_array()
        .ok_or("mob/spawn_many missing results array")?;
    assert_eq!(results.len(), 3, "expected three spawn results: {spawned}");
    assert!(
        results.iter().all(|entry| {
            entry["status"] == "spawned"
                && entry["result"]["agent_identity"].is_string()
                && entry["result"]["member_ref"].is_string()
                && entry.get("ok").is_none()
                && entry.get("agent_identity").is_none()
        }),
        "all spawn_many entries should use typed spawned results: {spawned}"
    );
    let worker_identity = results[1]["result"]["agent_identity"]
        .as_str()
        .ok_or("worker spawn result missing result.agent_identity")?
        .to_string();

    let members = poll_members_until(&mut surface, mob_id, |payload| {
        payload["members"]
            .as_array()
            .is_some_and(|members| members.len() == 3)
    })
    .await?;
    let members_array = members["members"]
        .as_array()
        .ok_or("members array missing")?;
    assert!(
        members_array
            .iter()
            .any(|entry| entry["agent_identity"].as_str() == Some("worker-1")),
        "worker should appear in mob/members: {members}"
    );

    let _ = rpc_call(
        &mut surface,
        4,
        "mob/wire",
        json!({
            "mob_id": mob_id,
            "member": "lead-1",
            "peer": { "local": "worker-1" }
        }),
        15,
    )
    .await?;
    let wired_members = poll_members_until(&mut surface, mob_id, |payload| {
        payload["members"].as_array().is_some_and(|members| {
            members.iter().any(|entry| {
                entry["agent_identity"].as_str() == Some("lead-1")
                    && entry["wired_to"].as_array().is_some_and(|wired| {
                        wired.iter().any(|peer| peer.as_str() == Some("worker-1"))
                    })
            })
        })
    })
    .await?;
    assert!(
        wired_members["members"].as_array().is_some(),
        "wired members payload malformed: {wired_members}"
    );

    let _ = rpc_call(
        &mut surface,
        5,
        "mob/unwire",
        json!({
            "mob_id": mob_id,
            "member": "lead-1",
            "peer": { "local": "worker-1" }
        }),
        15,
    )
    .await?;

    let appended = rpc_call(
        &mut surface,
        6,
        "mob/append_system_context",
        json!({
            "mob_id": mob_id,
            "agent_identity": "worker-1",
            "text": "Always include the token CTX_MOB_29.",
            "source": "mob",
            "idempotency_key": "scenario-29-worker"
        }),
        15,
    )
    .await?;
    // Verify the append targeted the correct member via identity.
    assert!(!worker_identity.is_empty(), "worker identity should be set");
    assert_eq!(appended["status"], "staged");

    let send_err = rpc_call(
        &mut surface,
        7,
        "mob/send",
        json!({
            "mob_id": mob_id,
            "agent_identity": "worker-1",
            "content": "Reply with TURN_PROBE_29 and include CTX_MOB_29."
        }),
        120,
    )
    .await
    .expect_err("mob/send should be removed from the public RPC surface");
    assert!(
        send_err.to_string().contains("Method not found"),
        "removed mob/send route should reject CLI smoke traffic: {send_err}"
    );

    let _ = rpc_call(
        &mut surface,
        9,
        "mob/retire",
        json!({"mob_id": mob_id, "agent_identity":"reviewer-1"}),
        15,
    )
    .await?;
    let after_retire = poll_members_until(&mut surface, mob_id, |payload| {
        payload["members"]
            .as_array()
            .is_some_and(|members| members.len() == 2)
    })
    .await?;
    assert!(
        after_retire["members"].as_array().is_some_and(|members| {
            members
                .iter()
                .all(|entry| entry["agent_identity"].as_str() != Some("reviewer-1"))
        }),
        "retired reviewer should disappear from members: {after_retire}"
    );

    let _ = rpc_call(
        &mut surface,
        10,
        "mob/respawn",
        json!({"mob_id": mob_id, "agent_identity":"worker-1"}),
        15,
    )
    .await?;
    let after_respawn = poll_members_until(&mut surface, mob_id, |payload| {
        payload["members"].as_array().is_some_and(|members| {
            members.iter().any(|entry| {
                entry["agent_identity"].as_str() == Some("worker-1") && entry["state"] == "active"
            })
        })
    })
    .await?;
    assert!(
        after_respawn["members"]
            .as_array()
            .is_some_and(|members| members.len() == 2),
        "respawn should preserve two active members: {after_respawn}"
    );
    // Verify respawned worker is present in roster with identity.
    assert!(
        after_respawn["members"]
            .as_array()
            .is_some_and(|members| members
                .iter()
                .any(|entry| entry["agent_identity"].as_str() == Some("worker-1"))),
        "respawned worker-1 should be present in members: {after_respawn}"
    );
    let send_after_respawn_err = rpc_call(
        &mut surface,
        11,
        "mob/send",
        json!({
            "mob_id": mob_id,
            "agent_identity": "worker-1",
            "content": "Reply with RESPAWN_PROBE_29."
        }),
        120,
    )
    .await
    .expect_err("mob/send should stay unavailable after respawn");
    assert!(
        send_after_respawn_err
            .to_string()
            .contains("Method not found"),
        "removed mob/send route should stay unavailable after respawn: {send_after_respawn_err}"
    );
    let stopped = rpc_call(
        &mut surface,
        12,
        "mob/lifecycle",
        json!({"mob_id": mob_id, "action":"stop"}),
        15,
    )
    .await?;
    assert_eq!(stopped["ok"], true);
    let stopped_status = rpc_call(
        &mut surface,
        14,
        "mob/status",
        json!({"mob_id": mob_id}),
        15,
    )
    .await?;
    assert_eq!(stopped_status["status"], "Stopped");

    let resumed = rpc_call(
        &mut surface,
        15,
        "mob/lifecycle",
        json!({"mob_id": mob_id, "action":"resume"}),
        15,
    )
    .await?;
    assert_eq!(resumed["ok"], true);
    let resumed_status = rpc_call(
        &mut surface,
        16,
        "mob/status",
        json!({"mob_id": mob_id}),
        15,
    )
    .await?;
    assert_eq!(resumed_status["status"], "Running");

    let events = rpc_call(
        &mut surface,
        17,
        "mob/events",
        json!({"mob_id": mob_id, "after_cursor": 0, "limit": 200}),
        15,
    )
    .await?;
    let event_count = events["events"].as_array().map_or(0, Vec::len);
    assert!(
        event_count >= 5,
        "state-machine probe should emit a non-trivial event ledger: {events}"
    );

    let destroyed = rpc_call(
        &mut surface,
        18,
        "mob/lifecycle",
        json!({"mob_id": mob_id, "action":"destroy"}),
        15,
    )
    .await?;
    assert_eq!(destroyed["ok"], true);
    let listed_after_destroy = rpc_call(&mut surface, 19, "mob/list", json!({}), 15).await?;
    assert!(
        listed_after_destroy["mobs"]
            .as_array()
            .is_some_and(|mobs| mobs
                .iter()
                .all(|entry| entry["mob_id"].as_str() != Some(mob_id))),
        "destroyed mob should disappear from mob/list: {listed_after_destroy}"
    );

    shutdown_rpc_surface(surface).await?;
    Ok(())
}

// ===========================================================================
// Scenario 30: CLI mob RPC surface live flow probe
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-smoke"]
async fn e2e_scenario_30_cli_mob_rpc_flow_probe() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let api_key = anthropic_api_key().ok_or("missing API key")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_id = "scenario-30-flow";
    let mob_dir = write_flow_probe_mobpack_fixture(&project_dir, mob_id).await?;
    let pack = project_dir.join("scenario-30.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let mut surface =
        spawn_mob_rpc_surface(&rkat, &project_dir, &pack, "bootstrap", Some(&api_key)).await?;
    let _ = rpc_call(&mut surface, 101, "initialize", json!({}), 20).await?;

    let spawned = rpc_call(
        &mut surface,
        102,
        "mob/spawn_many",
        json!({
            "mob_id": mob_id,
            "specs": [
                {"profile":"lead","agent_identity":"lead-1","runtime_mode":"turn_driven"},
                {"profile":"analyst","agent_identity":"analyst-1","runtime_mode":"turn_driven"},
                {"profile":"reviewer","agent_identity":"reviewer-1","runtime_mode":"turn_driven"}
            ]
        }),
        30,
    )
    .await?;
    let results = spawned["results"]
        .as_array()
        .ok_or("mob/spawn_many missing results array")?;
    assert!(
        results.iter().all(|entry| {
            entry["status"] == "spawned"
                && entry["result"]["agent_identity"].is_string()
                && entry["result"]["member_ref"].is_string()
                && entry.get("ok").is_none()
                && entry.get("agent_identity").is_none()
        }),
        "flow smoke spawn_many entries should use typed spawned results: {spawned}"
    );

    let flows = rpc_call(
        &mut surface,
        103,
        "mob/flows",
        json!({"mob_id": mob_id}),
        15,
    )
    .await?;
    assert!(
        flows["flows"]
            .as_array()
            .is_some_and(|flows| flows.iter().any(|flow| flow.as_str() == Some("main"))),
        "mob should expose the main flow: {flows}"
    );

    let started = rpc_call(
        &mut surface,
        104,
        "mob/flow_run",
        json!({
            "mob_id": mob_id,
            "flow_id": "main",
            "params": { "ticket": "FLOW-23" }
        }),
        20,
    )
    .await?;
    let run_id = started["run_id"]
        .as_str()
        .ok_or("mob/flow_run missing run_id")?
        .to_string();

    let terminal = poll_flow_status_until_terminal(&mut surface, mob_id, &run_id).await?;
    let run = terminal["run"]
        .as_object()
        .ok_or("terminal flow status missing run object")?;
    let terminal_status = run
        .get("status")
        .and_then(Value::as_str)
        .ok_or("run.status missing")?;
    assert!(
        matches!(terminal_status, "completed" | "failed"),
        "live flow probe should reach a non-canceled terminal status: {terminal}"
    );
    let step_ledger = run
        .get("step_ledger")
        .and_then(Value::as_array)
        .ok_or("run.step_ledger missing")?;
    assert!(
        step_ledger.len() >= 3,
        "flow should record at least three step ledger entries: {terminal}"
    );
    let failures = run
        .get("failure_ledger")
        .and_then(Value::as_array)
        .ok_or("run.failure_ledger missing")?;

    let events = rpc_call(
        &mut surface,
        105,
        "mob/events",
        json!({"mob_id": mob_id, "after_cursor": 0, "limit": 200}),
        15,
    )
    .await?;
    let items = events["events"]
        .as_array()
        .ok_or("mob/events missing events array")?;
    match terminal_status {
        "completed" => {
            assert!(
                step_ledger.iter().any(|entry| {
                    entry["step_id"].as_str() == Some("synthesize")
                        && entry["status"].as_str() == Some("completed")
                }),
                "completed flow should include a completed synthesize step: {terminal}"
            );
            assert!(
                failures.is_empty(),
                "completed flow should not record failure ledger entries: {terminal}"
            );
            assert!(
                items.iter().any(|event| {
                    event["kind"]["type"].as_str() == Some("flow_completed")
                        && event["kind"]["run_id"].as_str() == Some(run_id.as_str())
                }),
                "event ledger should record FlowCompleted: {events}"
            );
        }
        "failed" => {
            assert!(
                !failures.is_empty(),
                "failed flow should surface failure ledger entries: {terminal}"
            );
            assert!(
                items.iter().any(|event| {
                    event["kind"]["type"].as_str() == Some("flow_failed")
                        && event["kind"]["run_id"].as_str() == Some(run_id.as_str())
                }),
                "event ledger should record FlowFailed for terminal failures: {events}"
            );
        }
        _ => unreachable!("terminal status filtered above"),
    }

    let _ = rpc_call(
        &mut surface,
        106,
        "mob/lifecycle",
        json!({"mob_id": mob_id, "action":"destroy"}),
        15,
    )
    .await?;
    shutdown_rpc_surface(surface).await?;
    Ok(())
}

// ===========================================================================
// Scenario 29: CLI mob RPC surface member-turn probe
// ===========================================================================

#[tokio::test]
#[ignore = "lane:e2e-live"]
async fn e2e_scenario_29_cli_mob_rpc_member_turn_probe() -> Result<(), Box<dyn std::error::Error>> {
    if skip_if_no_api_prereqs() {
        return Ok(());
    }

    let api_key = anthropic_api_key().ok_or("missing API key")?;
    let tmp = TempDir::new()?;
    let project_dir = tmp.path().join("project");
    tokio::fs::create_dir_all(project_dir.join("data")).await?;
    let mob_id = "scenario-29-turn";
    let mob_dir = write_turn_probe_mobpack_fixture(&project_dir, mob_id).await?;
    let pack = project_dir.join("scenario-29.mobpack");
    let rkat = rkat_binary_path().ok_or("rkat binary not found")?;

    let pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        mob_dir.display().to_string(),
        "-o".to_string(),
        pack.display().to_string(),
    ];
    let pack_refs: Vec<&str> = pack_args.iter().map(String::as_str).collect();
    let pack_out = run_rkat(&rkat, &project_dir, &pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(pack_out, &pack_refs).map_err(std::io::Error::other)?;

    let mut surface =
        spawn_mob_rpc_surface(&rkat, &project_dir, &pack, "bootstrap", Some(&api_key)).await?;
    let _ = rpc_call(&mut surface, 201, "initialize", json!({}), 20).await?;

    let spawned = rpc_call(
        &mut surface,
        202,
        "mob/spawn",
        json!({
            "mob_id": mob_id,
            "profile": "worker",
            "agent_identity": "worker-1",
            "runtime_mode": "turn_driven"
        }),
        20,
    )
    .await?;
    let spawned_identity = spawned["agent_identity"]
        .as_str()
        .ok_or("mob/spawn missing agent_identity")?
        .to_string();
    assert_eq!(spawned_identity, "worker-1");

    let appended = rpc_call(
        &mut surface,
        203,
        "mob/append_system_context",
        json!({
            "mob_id": mob_id,
            "agent_identity": "worker-1",
            "text": "Always include the token CTX_MOB_29.",
            "source": "mob",
            "idempotency_key": "scenario-29-context"
        }),
        20,
    )
    .await?;
    assert_eq!(appended["status"], "staged");

    let send_err = rpc_call(
        &mut surface,
        204,
        "mob/send",
        json!({
            "mob_id": mob_id,
            "agent_identity": "worker-1",
            "content": "Reply with TURN_PROBE_29 and include CTX_MOB_29."
        }),
        120,
    )
    .await
    .expect_err("mob/send should be removed from the public RPC surface");
    assert!(
        send_err.to_string().contains("Method not found"),
        "removed mob/send route should reject member turn probes: {send_err}"
    );

    // Load-time model validation: a definition whose profile model is
    // neither catalogued, custom-defined ([models.<id>]), nor
    // provider-annotated fails fast at mob deploy instead of bricking the
    // member at first delivery.
    let broken_mob_id = "scenario-29-broken";
    let broken_dir = project_dir.join(format!("{broken_mob_id}-fixture"));
    tokio::fs::create_dir_all(&broken_dir).await?;
    tokio::fs::write(
        broken_dir.join("manifest.toml"),
        format!("[mobpack]\nname = \"{broken_mob_id}\"\nversion = \"1.0.0\"\n"),
    )
    .await?;
    tokio::fs::write(
        broken_dir.join("definition.json"),
        format!(
            r#"{{
  "id":"{broken_mob_id}",
  "profiles":{{
    "broken":{{
      "model":"definitely-invalid-live-smoke-model",
      "tools":{{"comms":true}},
      "external_addressable":true,
      "peer_description":"Deterministic failure worker"
    }}
  }},
  "wiring":{{"auto_wire_orchestrator":false,"role_wiring":[]}},
  "skills":{{}}
}}"#
        ),
    )
    .await?;
    let broken_pack = project_dir.join("scenario-29-broken.mobpack");
    let broken_pack_args = [
        "mob".to_string(),
        "pack".to_string(),
        broken_dir.display().to_string(),
        "-o".to_string(),
        broken_pack.display().to_string(),
    ];
    let broken_pack_refs: Vec<&str> = broken_pack_args.iter().map(String::as_str).collect();
    let broken_pack_out = run_rkat(&rkat, &project_dir, &broken_pack_refs, Some(&api_key)).await?;
    let _ = output_ok_or_err(broken_pack_out, &broken_pack_refs).map_err(std::io::Error::other)?;
    let broken_deploy_args = [
        "mob".to_string(),
        "deploy".to_string(),
        broken_pack.display().to_string(),
        "bootstrap".to_string(),
        "--trust-policy".to_string(),
        "permissive".to_string(),
    ];
    let broken_deploy_refs: Vec<&str> = broken_deploy_args.iter().map(String::as_str).collect();
    let broken_deploy_out =
        run_rkat(&rkat, &project_dir, &broken_deploy_refs, Some(&api_key)).await?;
    let broken_deploy_err = output_ok_or_err(broken_deploy_out, &broken_deploy_refs)
        .expect_err("deploying a definition with an unknown model must fail fast at load");
    assert!(
        broken_deploy_err.contains("unknown_model")
            && broken_deploy_err.contains("definitely-invalid-live-smoke-model"),
        "broken deploy should fail with the load-time unknown_model diagnostic: {broken_deploy_err}"
    );

    let _ = rpc_call(
        &mut surface,
        209,
        "mob/respawn",
        json!({"mob_id": mob_id, "agent_identity":"worker-1"}),
        20,
    )
    .await?;
    let respawned_members = poll_members_until(&mut surface, mob_id, |payload| {
        payload["members"].as_array().is_some_and(|members| {
            members.iter().any(|entry| {
                entry["agent_identity"].as_str() == Some("worker-1") && entry["state"] == "active"
            })
        })
    })
    .await?;
    assert!(
        respawned_members["members"]
            .as_array()
            .is_some_and(|members| members
                .iter()
                .any(|entry| entry["agent_identity"].as_str() == Some("worker-1"))),
        "respawned worker-1 should be present: {respawned_members}"
    );
    let send_after_respawn_err = rpc_call(
        &mut surface,
        210,
        "mob/send",
        json!({
            "mob_id": mob_id,
            "agent_identity": "worker-1",
            "content": "Reply with RESPAWN_PROBE_29."
        }),
        120,
    )
    .await
    .expect_err("mob/send should stay unavailable after respawn");
    assert!(
        send_after_respawn_err
            .to_string()
            .contains("Method not found"),
        "removed mob/send route should stay unavailable after respawn: {send_after_respawn_err}"
    );
    let events = rpc_call(
        &mut surface,
        212,
        "mob/events",
        json!({"mob_id": mob_id, "after_cursor": 0, "limit": 200}),
        20,
    )
    .await?;
    assert!(
        events["events"]
            .as_array()
            .is_some_and(|items| !items.is_empty()),
        "member-turn probe should expose a non-empty event ledger: {events}"
    );

    let _ = rpc_call(
        &mut surface,
        213,
        "mob/lifecycle",
        json!({"mob_id": mob_id, "action":"destroy"}),
        15,
    )
    .await?;
    shutdown_rpc_surface(surface).await?;
    Ok(())
}