terraphim_orchestrator 1.20.3

AI Dark Factory orchestrator wiring spawner, router, supervisor into a reconciliation loop
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
//! adf-ctl: CLI control for the AI Dark Factory orchestrator.
//!
//! Triggers agents, queries status, and cancels running agents via SSH+curl
//! to the orchestrator webhook endpoint, or directly in local mode with
//! `--local`. Requires SSH access to bigbox when not using local mode.

use anyhow::{bail, Context, Result};
use clap::{Parser, Subcommand, ValueEnum};
use hmac::{Hmac, Mac};
use jiff::Timestamp;
use serde::Serialize;
use sha2::Sha256;
#[cfg(unix)]
use std::io::Read;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};

type HmacSha256 = Hmac<Sha256>;

const DEFAULT_HOST: &str = "bigbox";
const DEFAULT_ENDPOINT: &str = "http://172.18.0.1:9091/webhooks/gitea";
const DEFAULT_LOCAL_ENDPOINT: &str = "http://127.0.0.1:9091/webhooks/gitea";
const DEFAULT_ORCHESTRATOR_TOML: &str = "/opt/ai-dark-factory/orchestrator.toml";
const DEFAULT_WAIT_TIMEOUT_SECS: u64 = 1200;

#[derive(Debug, Clone, Copy, ValueEnum)]
enum DirectEventKind {
    Push,
    Pr,
}

#[derive(Parser, Debug)]
#[command(name = "adf-ctl", about = "Control the AI Dark Factory orchestrator")]
struct Cli {
    /// Run commands directly on local machine instead of via SSH
    #[arg(long, global = true)]
    local: bool,

    #[command(subcommand)]
    command: AdfSub,
}

/// Output format for subcommands that support both human-readable text and
/// machine-parseable JSON. Default is `Human` for back-compatibility; the
/// `adf-orchestrate` skill and other automation should pass `--format json`.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
#[clap(rename_all = "lowercase")]
enum OutputFormat {
    #[default]
    Human,
    Json,
}

#[derive(Subcommand, Debug)]
enum AdfSub {
    /// Trigger an agent or persona by name
    Trigger {
        /// Agent or persona name (e.g. meta-learning, security-sentinel).
        /// Use `project/agent` for project-qualified dispatch.
        name: String,
        /// Optional context appended to the @adf: mention
        #[arg(long, default_value = "")]
        context: String,
        /// SSH host alias
        #[arg(long, default_value = DEFAULT_HOST)]
        host: String,
        /// Webhook endpoint URL (defaults to remote or local endpoint based on --local)
        #[arg(long)]
        endpoint: Option<String>,
        /// Webhook HMAC secret (default: auto-resolve from env/TOML)
        #[arg(long)]
        secret: Option<String>,
        /// Wait for agent to complete before returning
        #[arg(long, default_value_t = false)]
        wait: bool,
        /// Timeout in seconds when --wait is used
        #[arg(long, default_value_t = DEFAULT_WAIT_TIMEOUT_SECS)]
        timeout: u64,
        /// Dispatch directly via Unix domain socket (local mode only).
        /// Bypasses HTTP webhook and HMAC verification.
        #[arg(long, default_value_t = false)]
        direct: bool,
        /// Synthetic event type for direct dispatch of event-only agents.
        #[arg(long, value_enum)]
        event: Option<DirectEventKind>,
        /// Git SHA for push events (used with --event push).
        #[arg(long)]
        sha: Option<String>,
        /// Git ref name for push events (e.g. refs/heads/main).
        #[arg(long)]
        ref_name: Option<String>,
        /// PR number for pull_request events (used with --event pr).
        #[arg(long)]
        pr: Option<u64>,
        /// Head SHA for pull_request events.
        #[arg(long)]
        head_sha: Option<String>,
        /// Author login for pull_request events.
        #[arg(long)]
        author: Option<String>,
        /// Title for pull_request events.
        #[arg(long)]
        title: Option<String>,
    },
    /// Show running agents and recent exits [best-effort via SSH or local]
    Status {
        /// SSH host alias
        #[arg(long, default_value = DEFAULT_HOST)]
        host: String,
        /// journalctl --since value (e.g. 1h, 30m)
        #[arg(long, default_value = "1h")]
        since: String,
        /// Output format: human (default) or json for automation
        #[arg(long, value_enum, default_value_t)]
        format: OutputFormat,
    },
    /// Kill a running agent by name [best-effort via SSH pgrep or local]
    Cancel {
        /// Agent name to cancel
        name: String,
        /// SSH host alias
        #[arg(long, default_value = DEFAULT_HOST)]
        host: String,
    },
    /// List all configured agent names from orchestrator TOML
    Agents {
        /// SSH host alias
        #[arg(long, default_value = DEFAULT_HOST)]
        host: String,
        /// Output format: human (default) or json for automation
        #[arg(long, value_enum, default_value_t)]
        format: OutputFormat,
    },
    /// Run a named flow from a local `.terraphim/flows/\<name\>.toml` file
    Flow {
        /// Flow name (e.g. adf-useful-work-proof)
        name: String,
        /// Key=value context passed to the flow (e.g. "issue=1890")
        #[arg(long, default_value = "")]
        context: String,
        /// Path to local orchestrator TOML (reserved for future flow runtime wiring)
        #[arg(long)]
        config: Option<String>,
    },
    /// Show ADF pipeline stage artefact completion for an issue
    PipelineStatus {
        /// Issue number (e.g. 1887)
        issue: String,
        /// Base directory containing per-issue artefact subdirectories
        #[arg(long, default_value = ".docs/adf")]
        base_dir: PathBuf,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    run(cli.local, cli.command)
}

fn run(local: bool, sub: AdfSub) -> Result<()> {
    match sub {
        AdfSub::Trigger {
            name,
            context,
            host,
            endpoint,
            secret,
            wait,
            timeout,
            direct,
            event,
            sha,
            ref_name,
            pr,
            head_sha,
            author,
            title,
        } => {
            let resolved_endpoint = resolve_endpoint(local, endpoint.as_deref());
            cmd_trigger(
                local,
                &name,
                &context,
                &host,
                &resolved_endpoint,
                secret.as_deref(),
                wait,
                timeout,
                direct,
                event,
                sha.as_deref(),
                ref_name.as_deref(),
                pr,
                head_sha.as_deref(),
                author.as_deref(),
                title.as_deref(),
            )
        }
        AdfSub::Status {
            host,
            since,
            format,
        } => cmd_status(local, &host, &since, format),
        AdfSub::Cancel { name, host } => cmd_cancel(local, &name, &host),
        AdfSub::Agents { host, format } => cmd_agents(local, &host, format),
        AdfSub::Flow {
            name,
            context,
            config,
        } => cmd_flow(&name, &context, config.as_deref()),
        AdfSub::PipelineStatus { issue, base_dir } => cmd_pipeline_status(&issue, &base_dir),
    }
}

// --- Endpoint resolution ---

/// Resolve the webhook endpoint: explicit arg takes precedence, otherwise
/// uses the local endpoint when `local` is true, or the remote default.
fn resolve_endpoint(local: bool, explicit: Option<&str>) -> String {
    if let Some(ep) = explicit {
        return ep.to_string();
    }
    if local {
        DEFAULT_LOCAL_ENDPOINT.to_string()
    } else {
        DEFAULT_ENDPOINT.to_string()
    }
}

// --- Local config discovery ---

/// Walk up from the current working directory to find `.terraphim/adf.toml`.
/// Returns `None` if no such file exists in any ancestor directory.
fn discover_local_config() -> Option<PathBuf> {
    let mut current = std::env::current_dir().ok()?;
    loop {
        let candidate = current.join(".terraphim").join("adf.toml");
        if candidate.exists() {
            return Some(candidate);
        }
        if !current.pop() {
            break;
        }
    }
    None
}

/// Parse agent `name = "..."` entries from a TOML file using `strip_prefix`
/// and `strip_suffix` for safe extraction.
fn parse_agent_names_from_toml(path: &Path) -> Result<Vec<String>> {
    let content = std::fs::read_to_string(path)
        .with_context(|| format!("Failed to read {}", path.display()))?;
    let mut names = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("name = \"") {
            if let Some(name) = rest.strip_suffix('"') {
                names.push(name.to_string());
            }
        }
    }
    Ok(names)
}

// --- Payload construction ---

fn build_payload(agent_name: &str, context: &str) -> String {
    let body = if context.is_empty() {
        format!("@adf:{}", agent_name)
    } else {
        format!("@adf:{} {}", agent_name, context)
    };
    let now = Timestamp::now().to_string();
    // issue_number: 0 bypasses should_skip_dispatch in the orchestrator (lib.rs:4135)
    serde_json::json!({
        "action": "created",
        "comment": {
            "id": 1,
            "body": body,
            "user": { "login": "adf-cli" },
            "created_at": now
        },
        "issue": {
            "number": 0,
            "title": "CLI trigger",
            "state": "open"
        },
        "repository": {
            "full_name": "terraphim/terraphim-ai"
        }
    })
    .to_string()
}

fn sign_payload(secret: &str, payload: &[u8]) -> String {
    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC accepts any key size");
    mac.update(payload);
    hex::encode(mac.finalize().into_bytes())
}

// --- Secret resolution ---

fn resolve_secret(local: bool, explicit: Option<&str>, host: &str) -> Result<String> {
    if let Some(s) = explicit {
        return Ok(s.to_string());
    }
    if let Ok(s) = std::env::var("ADF_WEBHOOK_SECRET") {
        if !s.is_empty() {
            return Ok(s);
        }
    }
    if local {
        // Read secret from local config files
        if let Some(config_path) = discover_local_config() {
            let content = std::fs::read_to_string(&config_path)
                .with_context(|| format!("Failed to read {}", config_path.display()))?;
            for line in content.lines() {
                if let Some(rest) = line.trim().strip_prefix("secret = \"") {
                    if let Some(secret) = rest.strip_suffix('"') {
                        return Ok(secret.to_string());
                    }
                }
            }
        }
        bail!(
            "Could not read webhook secret from local config.\n\
             Set ADF_WEBHOOK_SECRET env var or pass --secret"
        );
    }
    let cmd = format!(
        "grep 'secret' {} | awk -F'\"' '{{print $2}}' | head -1",
        DEFAULT_ORCHESTRATOR_TOML
    );
    let (stdout, _, code) = ssh_run(host, &cmd)?;
    let secret = stdout.trim().to_string();
    if code != 0 || secret.is_empty() {
        bail!(
            "Could not read webhook secret from {}:{}\n\
             Set ADF_WEBHOOK_SECRET env var or pass --secret",
            host,
            DEFAULT_ORCHESTRATOR_TOML
        );
    }
    Ok(secret)
}

// --- SSH transport ---

fn ssh_run(host: &str, remote_cmd: &str) -> Result<(String, String, i32)> {
    let output = Command::new("ssh")
        .arg("-o")
        .arg("BatchMode=yes")
        .arg(host)
        .arg(remote_cmd)
        .output()
        .with_context(|| format!("failed to run ssh {}", host))?;
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let code = output
        .status
        .code()
        .unwrap_or_else(|| ExitStatus::default().code().unwrap_or(-1));
    Ok((stdout, stderr, code))
}

// --- Direct local command runner ---

/// Run a command directly on the local machine (used in `--local` mode).
fn local_run(cmd: &str) -> Result<(String, String, i32)> {
    let output = Command::new("sh")
        .arg("-c")
        .arg(cmd)
        .output()
        .with_context(|| format!("failed to run command: {}", cmd))?;
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let code = output
        .status
        .code()
        .unwrap_or_else(|| ExitStatus::default().code().unwrap_or(-1));
    Ok((stdout, stderr, code))
}

// --- Direct dispatch via Unix domain socket ---

#[cfg(unix)]
const DEFAULT_SOCKET_PATH: &str = "/tmp/adf-ctl.sock";

#[cfg(unix)]
fn resolve_socket_path() -> Result<PathBuf> {
    if let Ok(p) = std::env::var("ADF_DIRECT_SOCKET") {
        if !p.is_empty() {
            return Ok(PathBuf::from(p));
        }
    }
    if let Some(config_path) = discover_local_config() {
        if let Some(path) = parse_socket_path_from_toml(&config_path) {
            return Ok(path);
        }
    }
    if let Ok(orch_path) = std::env::var("ADF_ORCHESTRATOR_TOML") {
        if !orch_path.is_empty() {
            if let Some(path) = parse_socket_path_from_toml(Path::new(&orch_path)) {
                return Ok(path);
            }
        }
    }
    let orch_toml = Path::new("/opt/ai-dark-factory/orchestrator.toml");
    if let Some(path) = parse_socket_path_from_toml(orch_toml) {
        return Ok(path);
    }
    Ok(PathBuf::from(DEFAULT_SOCKET_PATH))
}

#[cfg(unix)]
fn parse_socket_path_from_toml(path: &Path) -> Option<PathBuf> {
    let content = std::fs::read_to_string(path).ok()?;
    let parsed: toml::Value = toml::from_str(&content).ok()?;
    let socket = parsed.get("direct_dispatch")?.get("socket_path")?;
    socket.as_str().map(PathBuf::from)
}

#[cfg(unix)]
fn direct_dispatch_via_socket(
    socket_path: &Path,
    agent_name: &str,
    project: Option<&str>,
    context: Option<&str>,
    synthetic_event: Option<serde_json::Value>,
) -> Result<()> {
    let mut payload_map = serde_json::Map::new();
    payload_map.insert(
        "agent".to_string(),
        serde_json::Value::String(agent_name.to_string()),
    );
    if let Some(p) = project {
        payload_map.insert(
            "project".to_string(),
            serde_json::Value::String(p.to_string()),
        );
    }
    if let Some(c) = context.filter(|c| !c.is_empty()) {
        payload_map.insert(
            "context".to_string(),
            serde_json::Value::String(c.to_string()),
        );
    }
    if let Some(se) = synthetic_event {
        payload_map.insert("synthetic_event".to_string(), se);
    }
    let payload = serde_json::Value::Object(payload_map);

    let mut stream = std::os::unix::net::UnixStream::connect(socket_path)
        .with_context(|| format!("failed to connect to {}", socket_path.display()))?;

    // Send newline-terminated JSON.
    writeln!(stream, "{payload}").context("failed to write to direct dispatch socket")?;

    // Read response.
    let mut response = String::new();
    stream
        .read_to_string(&mut response)
        .context("failed to read from direct dispatch socket")?;

    let response: serde_json::Value = serde_json::from_str(response.trim())
        .with_context(|| format!("invalid JSON from orchestrator: {}", response))?;

    match response.get("status").and_then(|s| s.as_str()) {
        Some("ok") => {
            println!("Agent dispatched via direct socket: {}", agent_name);
            println!("Monitor: journalctl -u adf-orchestrator -f");
            Ok(())
        }
        Some("error") => {
            let msg = response
                .get("message")
                .and_then(|m| m.as_str())
                .unwrap_or("unknown error");
            bail!("Direct dispatch error: {}", msg);
        }
        _ => {
            bail!("Unexpected direct dispatch response: {}", response);
        }
    }
}

// --- Subcommand implementations ---

fn split_project_agent(input: &str) -> (Option<String>, String) {
    match input.split_once('/') {
        Some((project, agent)) => (Some(project.to_string()), agent.to_string()),
        None => (None, input.to_string()),
    }
}

fn build_synthetic_event(
    event: Option<DirectEventKind>,
    sha: Option<&str>,
    ref_name: Option<&str>,
    pr: Option<u64>,
    head_sha: Option<&str>,
    author: Option<&str>,
    title: Option<&str>,
) -> Result<Option<serde_json::Value>> {
    let event = match event {
        Some(e) => e,
        None => return Ok(None),
    };

    let value = match event {
        DirectEventKind::Push => {
            let sha = sha.unwrap_or("0000000000000000000000000000000000000000");
            let ref_name = ref_name.unwrap_or("refs/heads/main");
            serde_json::json!({
                "Push": {
                    "sha": sha,
                    "ref_name": ref_name,
                    "pusher": author.unwrap_or("local-user"),
                    "files": <Vec<String>>::new(),
                }
            })
        }
        DirectEventKind::Pr => {
            let number = pr.unwrap_or(0);
            let head_sha = head_sha.unwrap_or("0000000000000000000000000000000000000000");
            let author = author.unwrap_or("local-user");
            let title = title.unwrap_or("Local direct dispatch");
            serde_json::json!({
                "PullRequest": {
                    "number": number,
                    "head_sha": head_sha,
                    "author": author,
                    "title": title,
                    "diff_loc": 0usize,
                }
            })
        }
    };

    Ok(Some(value))
}

fn validate_agent_name_for_shell(name: &str) -> Result<String> {
    if name.is_empty() {
        bail!("agent name cannot be empty");
    }
    if name.len() > 64 {
        bail!("agent name too long (max 64 chars)");
    }
    if !name
        .chars()
        .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
    {
        bail!(
            "agent name '{}' contains invalid characters (only alphanumeric, '-', '_' allowed)",
            name
        );
    }
    Ok(name.to_string())
}

fn validate_since_for_shell(since: &str) -> Result<String> {
    if since.is_empty() {
        bail!("--since value cannot be empty");
    }
    let mut chars = since.chars();
    match (chars.next(), chars.next_back()) {
        (Some(n), Some(u))
            if n.is_ascii_digit() && "smhdw".contains(u) && chars.all(|c| c.is_ascii_digit()) =>
        {
            Ok(since.to_string())
        }
        _ => {
            bail!(
                "--since '{}' must match ^[0-9]+[smhdw]$ (e.g. 30m, 1h, 2d, 1w)",
                since
            );
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn cmd_trigger(
    local: bool,
    name: &str,
    context: &str,
    host: &str,
    endpoint: &str,
    secret: Option<&str>,
    wait: bool,
    timeout: u64,
    direct: bool,
    event: Option<DirectEventKind>,
    sha: Option<&str>,
    ref_name: Option<&str>,
    pr: Option<u64>,
    head_sha: Option<&str>,
    author: Option<&str>,
    title: Option<&str>,
) -> Result<()> {
    if direct && !local {
        anyhow::bail!("--direct requires --local");
    }

    #[cfg(not(unix))]
    if direct {
        anyhow::bail!("--direct dispatch requires Unix (UDS not available on this platform)");
    }

    if local {
        println!("[local mode]");
    }

    #[cfg(unix)]
    if direct {
        let socket_path = resolve_socket_path()?;
        let synthetic_event =
            build_synthetic_event(event, sha, ref_name, pr, head_sha, author, title)?;
        let (project, agent_name) = split_project_agent(name);
        direct_dispatch_via_socket(
            &socket_path,
            &agent_name,
            project.as_deref(),
            Some(context),
            synthetic_event,
        )?;
        if wait {
            println!(
                "Waiting for agent '{}' to complete (timeout: {}s)...",
                name, timeout
            );
            wait_for_agent_exit(local, &agent_name, host, timeout)?;
        }
        return Ok(());
    }

    let secret = resolve_secret(local, secret, host)?;
    let payload = build_payload(name, context);
    let sig = sign_payload(&secret, payload.as_bytes());

    if local {
        // Direct curl call (no SSH)
        let mut child = Command::new("curl")
            .arg("-s")
            .arg("-o")
            .arg("/dev/null")
            .arg("-w")
            .arg("%{http_code}")
            .arg("-X")
            .arg("POST")
            .arg(endpoint)
            .arg("-H")
            .arg("X-Gitea-Event: issue_comment")
            .arg("-H")
            .arg(format!("X-Gitea-Signature: sha256={}", sig))
            .arg("-H")
            .arg("Content-Type: application/json")
            .arg("--data-binary")
            .arg("@-")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("failed to spawn curl")?;

        child
            .stdin
            .take()
            .expect("stdin is piped")
            .write_all(payload.as_bytes())
            .context("failed to write payload to curl stdin")?;

        let output = child.wait_with_output().context("curl wait failed")?;
        let http_code = String::from_utf8_lossy(&output.stdout).trim().to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();

        if !stderr.is_empty() {
            eprintln!("curl stderr: {}", stderr);
        }

        match http_code.as_str() {
            "200" | "202" | "204" => {
                println!("Agent dispatched: @adf:{} (HTTP {})", name, http_code);
                println!("Monitor: journalctl -u adf-orchestrator -f");
            }
            "401" => bail!("Webhook authentication failed (check secret)"),
            "400" => bail!("Bad request (HTTP 400) - check payload format"),
            "503" => bail!("Orchestrator unavailable (HTTP 503)"),
            "" => bail!("No HTTP response - is the orchestrator running locally?"),
            code => bail!("Unexpected HTTP status: {}", code),
        }
    } else {
        // SSH-based dispatch
        let curl_cmd = format!(
            "curl -s -o /dev/null -w '%{{http_code}}' \
             -X POST {} \
             -H 'X-Gitea-Event: issue_comment' \
             -H 'X-Gitea-Signature: sha256={}' \
             -H 'Content-Type: application/json' \
             --data-binary @-",
            endpoint, sig
        );

        // Pipe JSON payload via stdin to avoid shell quoting issues with the JSON body
        let mut child = Command::new("ssh")
            .arg("-o")
            .arg("BatchMode=yes")
            .arg(host)
            .arg(&curl_cmd)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .with_context(|| format!("failed to spawn ssh {}", host))?;

        child
            .stdin
            .take()
            .expect("stdin is piped")
            .write_all(payload.as_bytes())
            .context("failed to write payload to ssh stdin")?;

        let output = child.wait_with_output().context("ssh wait failed")?;
        let http_code = String::from_utf8_lossy(&output.stdout).trim().to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();

        if !stderr.is_empty() {
            eprintln!("ssh stderr: {}", stderr);
        }

        match http_code.as_str() {
            "200" | "202" | "204" => {
                println!("Agent dispatched: @adf:{} (HTTP {})", name, http_code);
                println!("Monitor: ssh {} journalctl -u adf-orchestrator -f", host);
            }
            "401" => bail!("Webhook authentication failed (check secret)"),
            "400" => bail!("Bad request (HTTP 400) - check payload format"),
            "503" => bail!("Orchestrator unavailable (HTTP 503)"),
            "" => bail!(
                "No HTTP response - is the orchestrator running on {}?",
                host
            ),
            code => bail!("Unexpected HTTP status: {}", code),
        }
    }

    if wait {
        println!(
            "Waiting for agent '{}' to complete (timeout: {}s)...",
            name, timeout
        );
        wait_for_agent_exit(local, name, host, timeout)?;
    }

    Ok(())
}

fn wait_for_agent_exit(local: bool, name: &str, host: &str, timeout_secs: u64) -> Result<()> {
    let validated_name = validate_agent_name_for_shell(name)?;
    let start = std::time::Instant::now();
    let poll_interval = std::time::Duration::from_secs(10);

    loop {
        if start.elapsed().as_secs() >= timeout_secs {
            bail!(
                "Timed out waiting for agent '{}' to complete after {}s",
                validated_name,
                timeout_secs
            );
        }

        let elapsed_secs = start.elapsed().as_secs() + 1;
        let since = format!("{} seconds ago", elapsed_secs);
        let cmd = format!(
            "journalctl -u adf-orchestrator --since '{}' --no-pager 2>/dev/null \
             | grep 'exit classified agent={}'",
            since, validated_name
        );

        let (stdout, _, _) = if local {
            local_run(&cmd)?
        } else {
            ssh_run(host, &cmd)?
        };

        if !stdout.trim().is_empty() {
            for line in stdout.lines() {
                println!("{}", line);
            }
            if stdout.contains("exit_class=success") || stdout.contains("exit_class=empty_success")
            {
                println!("Agent '{}' completed successfully.", validated_name);
                return Ok(());
            } else {
                bail!(
                    "Agent '{}' exited with non-success status:\n{}",
                    validated_name,
                    stdout.trim()
                );
            }
        }

        std::thread::sleep(poll_interval);
    }
}

fn cmd_status(local: bool, host: &str, since: &str, format: OutputFormat) -> Result<()> {
    if local {
        println!("[local mode]");
    }
    let since = validate_since_for_shell(since)?;
    let journal_cmd = format!(
        "journalctl -u adf-orchestrator --since '{} ago' --no-pager 2>/dev/null \
         | grep -E 'exit classified|spawning agent|Agent spawned' | tail -30",
        since
    );
    let (journal_stdout, journal_stderr, _) = if local {
        local_run(&journal_cmd)?
    } else {
        ssh_run(host, &journal_cmd)?
    };
    let journal_output = JournalOutput {
        stdout: journal_stdout,
        stderr: journal_stderr,
    };

    let pgrep_cmd = if local {
        "ps -o pid,etimes,cputime,comm -p $(pgrep -d, -x 'claude|opencode|pi' 2>/dev/null) 2>/dev/null \
         || echo '(no agent CLI processes running)'"
            .to_string()
    } else {
        "ps -o pid,etimes,cputime,comm -p $(pgrep -d, claude 2>/dev/null) 2>/dev/null \
         || echo '(no agent CLI processes running)'"
            .to_string()
    };
    let (pgrep_stdout, _, _) = if local {
        local_run(&pgrep_cmd)?
    } else {
        ssh_run(host, &pgrep_cmd)?
    };

    let activity = parse_journal_activity(&journal_output.stdout);
    let processes = parse_running_processes(&pgrep_stdout);

    match format {
        OutputFormat::Human => {
            if !local {
                println!(
                    "[best-effort via SSH process scan; not authoritative without admin socket]"
                );
            } else {
                println!("[best-effort via local process scan]");
            }
            println!();
            println!("=== Recent agent activity (last {}) ===", since);
            if !journal_output.stderr.is_empty() {
                eprintln!("stderr: {}", journal_output.stderr);
            }
            if journal_output.stdout.trim().is_empty() {
                println!("(no recent activity found)");
            } else {
                print!("{}", journal_output.stdout);
            }
            println!();
            if local {
                println!("=== Running agent CLI processes ===");
            } else {
                println!("=== Running claude processes ===");
            }
            print!("{}", pgrep_stdout);
        }
        OutputFormat::Json => {
            let report = StatusReport {
                host,
                since: &since,
                recent_activity: activity,
                running_processes: processes,
                best_effort: true,
                note: if local {
                    "best-effort via local process scan"
                } else {
                    "best-effort via SSH process scan; not authoritative without admin socket"
                },
            };
            println!("{}", serde_json::to_string_pretty(&report)?);
        }
    }

    Ok(())
}

#[derive(Debug, Serialize)]
struct StatusReport<'a> {
    host: &'a str,
    since: &'a str,
    recent_activity: Vec<JournalEvent>,
    running_processes: Vec<ProcessInfo>,
    best_effort: bool,
    note: &'a str,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
struct JournalEvent {
    line: String,
}

/// Raw output from a journalctl invocation, used in `cmd_status`.
#[derive(Debug)]
struct JournalOutput {
    stdout: String,
    stderr: String,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
struct ProcessInfo {
    pid: String,
    etimes: String,
    cputime: String,
    comm: String,
}

/// Parse `journalctl` output filtered for orchestrator events into structured
/// records. Empty input or a single placeholder line yields an empty Vec so
/// JSON consumers see a stable `[]` rather than a noisy string.
fn parse_journal_activity(stdout: &str) -> Vec<JournalEvent> {
    stdout
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .map(|l| JournalEvent {
            line: l.to_string(),
        })
        .collect()
}

/// Parse `ps -o pid,etimes,cputime,comm` output. The header row is dropped, as
/// is the `(no agent CLI processes running)` fallback emitted by the shell when
/// `pgrep` matches nothing.
fn parse_running_processes(stdout: &str) -> Vec<ProcessInfo> {
    stdout
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty() && !l.starts_with("PID") && !l.starts_with("(no agent CLI"))
        .filter_map(|l| {
            let mut parts = l.split_whitespace();
            let pid = parts.next()?.to_string();
            let etimes = parts.next()?.to_string();
            let cputime = parts.next()?.to_string();
            let comm = parts.next()?.to_string();
            Some(ProcessInfo {
                pid,
                etimes,
                cputime,
                comm,
            })
        })
        .collect()
}

fn cmd_cancel(local: bool, name: &str, host: &str) -> Result<()> {
    let validated_name = validate_agent_name_for_shell(name)?;
    if local {
        println!("[local mode]");
    }
    if !local {
        println!("[best-effort via SSH process scan; not authoritative without admin socket]");
    }
    println!(
        "Searching for agent '{}' processes on {}...",
        validated_name, host
    );

    let find_cmd = if local {
        format!("ls .worktrees/ 2>/dev/null | grep '^{}-'", validated_name)
    } else {
        format!(
            "ls /tmp/adf-worktrees/ 2>/dev/null | grep '^{}-'",
            validated_name
        )
    };
    let (worktrees, _, _) = if local {
        local_run(&find_cmd)?
    } else {
        ssh_run(host, &find_cmd)?
    };

    let pgrep_cmd = if local {
        "pgrep -a -x 'claude|opencode|pi' 2>/dev/null | grep -v defunct".to_string()
    } else {
        "pgrep -a claude 2>/dev/null | grep -v defunct".to_string()
    };
    let (procs, _, _) = if local {
        local_run(&pgrep_cmd)?
    } else {
        ssh_run(host, &pgrep_cmd)?
    };

    if worktrees.trim().is_empty() && procs.trim().is_empty() {
        println!(
            "No active worktrees or agent CLI processes found for '{}'.",
            validated_name
        );
        return Ok(());
    }

    if !worktrees.trim().is_empty() {
        println!("Active worktrees for '{}':", validated_name);
        for wt in worktrees.lines() {
            if local {
                println!("  .worktrees/{}", wt.trim());
            } else {
                println!("  /tmp/adf-worktrees/{}", wt.trim());
            }
        }
        println!();
    }

    if !procs.trim().is_empty() {
        println!("Running agent CLI processes:");
        for line in procs.lines() {
            println!("  {}", line);
        }
        println!();
        if local {
            println!("To kill a specific PID: kill <PID>");
        } else {
            println!("To kill a specific PID: ssh {} kill <PID>", host);
        }
        println!("(Phase 2 admin socket will provide authoritative cancel)");
    }

    Ok(())
}

fn cmd_agents(local: bool, host: &str, format: OutputFormat) -> Result<()> {
    if local {
        println!("[local mode]");
    }
    let agents = if local {
        // Discover local config and parse agent names from it
        let mut names = if let Some(config_path) = discover_local_config() {
            parse_agent_names_from_toml(&config_path)?
        } else {
            Vec::new()
        };
        // Fallback to orchestrator.toml if no local config found or it had no names
        if names.is_empty() {
            let orchestrator_toml = Path::new(DEFAULT_ORCHESTRATOR_TOML);
            if orchestrator_toml.exists() {
                names = parse_agent_names_from_toml(orchestrator_toml)?;
            }
        }
        names.sort();
        names.dedup();
        names
    } else {
        let cmd = "grep '^name = ' /opt/ai-dark-factory/conf.d/*.toml \
                   /opt/ai-dark-factory/orchestrator.toml 2>/dev/null \
                   | awk -F'\"' '{print $2}' | sort -u";
        let (stdout, stderr, code) = ssh_run(host, cmd)?;
        if code != 0 && stdout.trim().is_empty() {
            eprintln!("ssh stderr: {}", stderr);
            bail!("Failed to list agents from {}", host);
        }
        parse_agents_list(&stdout)
    };

    match format {
        OutputFormat::Human => {
            if local {
                println!("Configured agents (local):");
            } else {
                println!("Configured agents on {}:", host);
            }
            for a in &agents {
                println!("  {}", a);
            }
        }
        OutputFormat::Json => {
            let report = AgentsReport { host, agents };
            println!("{}", serde_json::to_string_pretty(&report)?);
        }
    }
    Ok(())
}

/// Discover a flow definition file from `.terraphim/flows/<name>.toml`, walking
/// up from the current directory so nested workspace commands still work.
fn discover_flow_file(cwd: &Path, name: &str) -> Option<PathBuf> {
    let mut current = Some(cwd.to_path_buf());
    while let Some(dir) = current {
        let flow_path = dir
            .join(".terraphim")
            .join("flows")
            .join(format!("{}.toml", name));
        if flow_path.is_file() {
            return Some(flow_path);
        }
        current = dir.parent().map(|p| p.to_path_buf());
    }
    None
}

fn validate_flow_context_atom(label: &str, value: &str) -> Result<()> {
    if value.is_empty() {
        bail!("flow context {} cannot be empty", label);
    }
    if !value
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.')
    {
        bail!(
            "flow context {} '{}' contains invalid characters (only alphanumeric, '-', '_', '.' allowed)",
            label,
            value
        );
    }
    Ok(())
}

fn parse_context(context: &str) -> Result<std::collections::HashMap<String, String>> {
    let mut map = std::collections::HashMap::new();
    for token in context.split_whitespace() {
        if let Some((key, value)) = token.split_once('=') {
            if !key.is_empty() && !value.is_empty() {
                validate_flow_context_atom("key", key)?;
                validate_flow_context_atom("value", value)?;
                map.insert(key.to_string(), value.to_string());
            }
        }
    }
    Ok(map)
}

/// Run a named flow definition locally. The current proof path is intentionally
/// one-slot (`k=1`) via a committed fixture, so it proves executor behaviour
/// without requiring runtime matrix rewriting.
fn cmd_flow(name: &str, context: &str, _config_path: Option<&str>) -> Result<()> {
    let cwd = std::env::current_dir().context("failed to get current working directory")?;

    let flow_path = discover_flow_file(&cwd, name).with_context(|| {
        format!(
            "flow '{}' not found in any .terraphim/flows/ directory from {} upward",
            name,
            cwd.display()
        )
    })?;

    println!("Loading flow from: {}", flow_path.display());

    let flow_content = std::fs::read_to_string(&flow_path)
        .with_context(|| format!("failed to read {}", flow_path.display()))?;

    let flow: terraphim_orchestrator::flow::config::FlowDefinition = toml::from_str(&flow_content)
        .with_context(|| format!("failed to parse flow TOML from {}", flow_path.display()))?;

    println!("Flow '{}' loaded: {} step(s)", flow.name, flow.steps.len());

    let ctx_map = parse_context(context)?;
    let issue = ctx_map.get("issue").cloned();

    let mut state = terraphim_orchestrator::flow::state::FlowRunState::new(&flow.name);
    if let Some(ref issue) = issue {
        println!("Issue context: {}", issue);
        state = state.with_issue(issue.clone());
    }

    let flow_state_dir = cwd.join(".terraphim").join("flow-state");
    std::fs::create_dir_all(&flow_state_dir).with_context(|| {
        format!(
            "failed to create flow state dir {}",
            flow_state_dir.display()
        )
    })?;

    let project_runtime = terraphim_orchestrator::flow::executor::ProjectRuntime {
        working_dir: cwd.clone(),
        gitea_owner: Some("terraphim".to_string()),
        gitea_repo: Some("terraphim-ai".to_string()),
    };

    let executor =
        terraphim_orchestrator::flow::executor::FlowExecutor::new(cwd.clone(), flow_state_dir)
            .with_projects(std::collections::HashMap::from([(
                flow.project.clone(),
                project_runtime,
            )]));

    println!("Running flow '{}'...", flow.name);
    let rt = tokio::runtime::Runtime::new().context("failed to create Tokio runtime")?;

    let final_state = rt
        .block_on(async { executor.run(&flow, Some(state)).await })
        .map_err(|e| anyhow::anyhow!("flow '{}' failed: {}", flow.name, e))?;

    println!();
    println!("Flow '{}' finished: {:?}", flow.name, final_state.status);
    if let Some(ref err) = final_state.error {
        println!("Error: {}", err);
    }
    println!(
        "Steps completed: {}/{}",
        final_state.next_step_index,
        flow.steps.len()
    );
    let matrix_slots: usize = final_state.matrix_envelopes.values().map(Vec::len).sum();
    if matrix_slots > 0 {
        println!("Matrix slots completed: {}", matrix_slots);
    }

    Ok(())
}

/// Canonical ADF disciplined-development stage artefact filenames (in order).
const STAGE_ARTEFACTS: &[&str] = &["research.md", "design.md", "implementation.md", "review.md"];

/// Print ADF pipeline artefact completion summary for a given issue.
///
/// Reads `.docs/adf/<issue>/` (or `base_dir/<issue>/`) and shows each
/// canonical stage artefact with its completion status, line count, and
/// last-modified timestamp.  Returns `Ok(())` when the directory exists
/// (even with partial artefacts); bails with exit code 1 when the directory
/// is missing.
fn cmd_pipeline_status(issue: &str, base_dir: &Path) -> Result<()> {
    let issue_dir = base_dir.join(issue);

    if !issue_dir.exists() {
        bail!(
            "artefact directory '{}' does not exist",
            issue_dir.display()
        );
    }

    println!("Issue: #{}", issue);
    println!("Directory: {}/", issue_dir.display());
    println!();
    println!("Stage Artefacts:");

    let mut complete: usize = 0;
    let total = STAGE_ARTEFACTS.len();

    for &stage in STAGE_ARTEFACTS {
        let path = issue_dir.join(stage);
        if path.exists() {
            let content = std::fs::read_to_string(&path)
                .with_context(|| format!("failed to read {}", path.display()))?;
            let lines = content.lines().count();
            let meta = std::fs::metadata(&path)
                .with_context(|| format!("failed to stat {}", path.display()))?;
            let mtime = meta
                .modified()
                .with_context(|| format!("failed to get mtime for {}", path.display()))?;
            let ts = jiff::Timestamp::try_from(mtime)
                .map(|t| t.to_string())
                .unwrap_or_else(|_| "unknown".to_string());
            println!("  {:<20} | COMPLETE | {:>5} lines | {}", stage, lines, ts);
            complete += 1;
        } else {
            println!("  {:<20} | MISSING  | {:>5}       | -", stage, "-");
        }
    }

    println!();
    println!("Summary: {}/{} artefacts complete", complete, total);

    Ok(())
}

#[derive(Debug, Serialize)]
struct AgentsReport<'a> {
    host: &'a str,
    agents: Vec<String>,
}

/// Parse the deduplicated agent-name list emitted by the remote grep+awk
/// pipeline. Empty lines are dropped so JSON consumers see only real names.
fn parse_agents_list(stdout: &str) -> Vec<String> {
    stdout
        .lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .map(str::to_string)
        .collect()
}

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

    #[test]
    fn test_build_payload_no_context() {
        let p = build_payload("meta-learning", "");
        let v: serde_json::Value = serde_json::from_str(&p).unwrap();
        assert_eq!(v["comment"]["body"], "@adf:meta-learning");
        assert_eq!(v["action"], "created");
    }

    #[test]
    fn test_build_payload_with_context() {
        let p = build_payload("meta-learning", "fleet health check");
        let v: serde_json::Value = serde_json::from_str(&p).unwrap();
        assert_eq!(
            v["comment"]["body"],
            "@adf:meta-learning fleet health check"
        );
    }

    #[test]
    fn test_build_payload_issue_number_zero() {
        let p = build_payload("security-sentinel", "");
        let v: serde_json::Value = serde_json::from_str(&p).unwrap();
        assert_eq!(v["issue"]["number"], 0);
    }

    #[test]
    fn test_build_payload_repo_full_name() {
        let p = build_payload("any-agent", "");
        let v: serde_json::Value = serde_json::from_str(&p).unwrap();
        assert_eq!(v["repository"]["full_name"], "terraphim/terraphim-ai");
    }

    #[test]
    fn test_sign_payload_matches_orchestrator() {
        let secret = "test-secret";
        let body = b"hello world";
        let sig = sign_payload(secret, body);
        assert!(terraphim_orchestrator::webhook::verify_signature(
            secret, body, &sig
        ));
    }

    #[test]
    fn test_sign_payload_hex_format() {
        let sig = sign_payload("test-secret", b"hello world");
        assert!(!sig.is_empty());
        assert!(sig.chars().all(|c| c.is_ascii_hexdigit()));
        assert_eq!(sig.len(), 64);
    }

    #[test]
    fn test_parse_agents_list_basic() {
        let stdout = "meta-learning\nsecurity-sentinel\nbuild-runner\n";
        let parsed = parse_agents_list(stdout);
        assert_eq!(
            parsed,
            vec![
                "meta-learning".to_string(),
                "security-sentinel".to_string(),
                "build-runner".to_string(),
            ]
        );
    }

    #[test]
    fn test_parse_agents_list_drops_blank_lines() {
        let stdout = "\n\nmeta-learning\n\n  \nsecurity-sentinel\n";
        let parsed = parse_agents_list(stdout);
        assert_eq!(
            parsed,
            vec!["meta-learning".to_string(), "security-sentinel".to_string()]
        );
    }

    #[test]
    fn test_parse_agents_list_empty_returns_empty() {
        assert!(parse_agents_list("").is_empty());
        assert!(parse_agents_list("\n\n\n").is_empty());
    }

    #[test]
    fn test_parse_context_key_values() {
        let parsed = parse_context("issue=1890 k=1").unwrap();
        assert_eq!(parsed.get("issue").map(String::as_str), Some("1890"));
        assert_eq!(parsed.get("k").map(String::as_str), Some("1"));
    }

    #[test]
    fn test_parse_context_rejects_shell_metacharacters() {
        let result = parse_context("issue=1890;rm");
        assert!(result.is_err());
    }

    #[test]
    fn test_agents_report_json_shape() {
        let report = AgentsReport {
            host: "bigbox",
            agents: vec!["meta-learning".to_string(), "build-runner".to_string()],
        };
        let json = serde_json::to_value(&report).unwrap();
        assert_eq!(json["host"], "bigbox");
        assert_eq!(json["agents"][0], "meta-learning");
        assert_eq!(json["agents"][1], "build-runner");
        assert_eq!(json["agents"].as_array().unwrap().len(), 2);
    }

    #[test]
    fn test_parse_journal_activity_filters_blanks() {
        let stdout = "May 16 12:00 exit classified agent=meta-learning exit_class=success\n\n\
                      May 16 12:05 spawning agent=build-runner\n";
        let parsed = parse_journal_activity(stdout);
        assert_eq!(parsed.len(), 2);
        assert!(parsed[0].line.contains("meta-learning"));
        assert!(parsed[1].line.contains("build-runner"));
    }

    #[test]
    fn test_parse_running_processes_strips_header_and_placeholder() {
        let stdout = "  PID  ELAPSED     TIME COMMAND\n\
                      12345     3600 00:05:23 claude\n\
                      12346      120 00:00:11 claude\n";
        let parsed = parse_running_processes(stdout);
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0].pid, "12345");
        assert_eq!(parsed[0].etimes, "3600");
        assert_eq!(parsed[0].cputime, "00:05:23");
        assert_eq!(parsed[0].comm, "claude");
    }

    #[test]
    fn test_parse_running_processes_no_cli_placeholder() {
        let parsed = parse_running_processes("(no agent CLI processes running)\n");
        assert!(parsed.is_empty());
    }

    #[test]
    fn test_status_report_json_includes_best_effort_flag() {
        let report = StatusReport {
            host: "bigbox",
            since: "1h",
            recent_activity: vec![],
            running_processes: vec![],
            best_effort: true,
            note: "test",
        };
        let json = serde_json::to_value(&report).unwrap();
        assert_eq!(json["host"], "bigbox");
        assert_eq!(json["since"], "1h");
        assert_eq!(json["best_effort"], true);
        assert!(json["recent_activity"].is_array());
        assert!(json["running_processes"].is_array());
    }

    #[test]
    fn test_output_format_default_is_human() {
        assert_eq!(OutputFormat::default(), OutputFormat::Human);
    }

    #[test]
    fn test_resolve_secret() {
        std::env::remove_var("ADF_WEBHOOK_SECRET");
        let result = resolve_secret(false, Some("mysecret"), "unused-host-in-unit-test");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "mysecret");

        std::env::set_var("ADF_WEBHOOK_SECRET", "env-secret");
        let result = resolve_secret(false, None, "unused-host-in-unit-test");
        std::env::remove_var("ADF_WEBHOOK_SECRET");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "env-secret");
    }

    #[test]
    fn test_resolve_endpoint_local() {
        // Explicit overrides local
        let ep = resolve_endpoint(true, Some("http://custom:9090/webhook"));
        assert_eq!(ep, "http://custom:9090/webhook");
        // Local without explicit
        let ep = resolve_endpoint(true, None);
        assert_eq!(ep, DEFAULT_LOCAL_ENDPOINT);
    }

    #[test]
    fn test_resolve_endpoint_remote() {
        // Explicit overrides remote
        let ep = resolve_endpoint(false, Some("http://custom:9090/webhook"));
        assert_eq!(ep, "http://custom:9090/webhook");
        // Remote without explicit
        let ep = resolve_endpoint(false, None);
        assert_eq!(ep, DEFAULT_ENDPOINT);
    }

    #[test]
    fn test_parse_agent_names_from_toml_basic() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.toml");
        std::fs::write(
            &path,
            r#"
            [agents]
            name = "meta-learning"
            name = "security-sentinel"
            name = "build-runner"
            "#,
        )
        .unwrap();
        let names = parse_agent_names_from_toml(&path).unwrap();
        assert_eq!(
            names,
            vec!["meta-learning", "security-sentinel", "build-runner"]
        );
    }

    #[test]
    fn test_parse_agent_names_from_toml_empty() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("no-names.toml");
        std::fs::write(&path, "[other]\nkey = \"value\"\n").unwrap();
        let names = parse_agent_names_from_toml(&path).unwrap();
        assert!(names.is_empty());
    }

    #[test]
    fn test_discover_local_config_not_found() {
        // In a typical test run there's unlikely to be .terraphim/adf.toml above
        let result = discover_local_config();
        // We just verify it doesn't panic
        let _ = result;
    }

    #[test]
    fn test_parse_agent_names_from_toml_strip_prefix() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("names.toml");
        std::fs::write(
            &path,
            "  name = \"test-agent\"  \n  # comment\n  name = \"other-agent\"\n",
        )
        .unwrap();
        let names = parse_agent_names_from_toml(&path).unwrap();
        assert_eq!(names, vec!["test-agent", "other-agent"]);
    }

    #[test]
    fn test_trigger_direct_requires_local() {
        let result = cmd_trigger(
            false,
            "meta-learning",
            "",
            "localhost",
            "http://localhost:9090/webhook",
            None,
            false,
            60,
            true,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        );
        assert!(result.is_err(), "direct without local should fail");
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("--direct requires --local"),
            "error message should mention --direct requires --local: {}",
            err
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_parse_socket_path_from_toml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("orchestrator.toml");
        std::fs::write(
            &path,
            "[direct_dispatch]\nsocket_path = \"/var/run/adf-ctl.sock\"\n",
        )
        .unwrap();
        let result = super::parse_socket_path_from_toml(&path);
        assert_eq!(
            result,
            Some(std::path::PathBuf::from("/var/run/adf-ctl.sock"))
        );
    }

    #[cfg(unix)]
    #[test]
    fn test_parse_socket_path_from_toml_missing_section() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("orchestrator.toml");
        std::fs::write(&path, "agents = []\n").unwrap();
        let result = super::parse_socket_path_from_toml(&path);
        assert_eq!(result, None);
    }

    #[cfg(unix)]
    #[test]
    fn test_parse_socket_path_from_toml_missing_field() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("orchestrator.toml");
        std::fs::write(&path, "[direct_dispatch]\nother_field = \"value\"\n").unwrap();
        let result = super::parse_socket_path_from_toml(&path);
        assert_eq!(result, None);
    }

    #[test]
    fn test_split_project_agent_bare() {
        let (project, agent) = super::split_project_agent("meta-learning");
        assert_eq!(project, None);
        assert_eq!(agent, "meta-learning");
    }

    #[test]
    fn test_split_project_agent_qualified() {
        let (project, agent) = super::split_project_agent("terraphim-ai/build-runner");
        assert_eq!(project, Some("terraphim-ai".to_string()));
        assert_eq!(agent, "build-runner");
    }

    #[test]
    fn test_validate_agent_name_for_shell_valid() {
        super::validate_agent_name_for_shell("meta-learning").unwrap();
        super::validate_agent_name_for_shell("build_runner").unwrap();
        super::validate_agent_name_for_shell("agent-123").unwrap();
    }

    #[test]
    fn test_validate_agent_name_for_shell_rejects_slash() {
        let result = super::validate_agent_name_for_shell("project/agent");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("invalid characters"),
            "error should mention invalid characters: {}",
            err
        );
    }

    #[test]
    fn test_validate_agent_name_for_shell_rejects_empty() {
        let result = super::validate_agent_name_for_shell("");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_agent_name_for_shell_rejects_too_long() {
        let long_name = "a".repeat(65);
        let result = super::validate_agent_name_for_shell(&long_name);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("too long"),
            "error should mention too long: {}",
            err
        );
    }

    #[test]
    fn test_validate_since_for_shell_valid() {
        super::validate_since_for_shell("30m").unwrap();
        super::validate_since_for_shell("1h").unwrap();
        super::validate_since_for_shell("2d").unwrap();
        super::validate_since_for_shell("1w").unwrap();
        super::validate_since_for_shell("10s").unwrap();
    }

    #[test]
    fn test_validate_since_for_shell_rejects_empty() {
        let result = super::validate_since_for_shell("");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("cannot be empty"),
            "error should mention empty: {}",
            err
        );
    }

    #[test]
    fn test_validate_since_for_shell_rejects_now() {
        let result = super::validate_since_for_shell("now");
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_since_for_shell_rejects_injection() {
        let result = super::validate_since_for_shell("1h'; rm -rf /");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("must match"),
            "error should mention grammar: {}",
            err
        );
    }

    #[test]
    fn test_validate_since_for_shell_rejects_no_unit() {
        let result = super::validate_since_for_shell("30");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("must match"),
            "error should mention grammar: {}",
            err
        );
    }

    #[test]
    fn test_pipeline_status_missing_directory_returns_error() {
        let dir = tempfile::tempdir().unwrap();
        let base = dir.path();
        let result = super::cmd_pipeline_status("9999", base);
        assert!(result.is_err());
        let msg = result.unwrap_err().to_string();
        assert!(
            msg.contains("does not exist"),
            "error should mention missing directory: {}",
            msg
        );
    }

    #[test]
    fn test_pipeline_status_all_artefacts_present() {
        let dir = tempfile::tempdir().unwrap();
        let issue_dir = dir.path().join("42");
        std::fs::create_dir_all(&issue_dir).unwrap();
        for stage in super::STAGE_ARTEFACTS {
            std::fs::write(
                issue_dir.join(stage),
                format!("# {}\n\nContent line.\n", stage),
            )
            .unwrap();
        }
        let result = super::cmd_pipeline_status("42", dir.path());
        assert!(result.is_ok(), "should succeed when all artefacts present");
    }

    #[test]
    fn test_pipeline_status_partial_artefacts() {
        let dir = tempfile::tempdir().unwrap();
        let issue_dir = dir.path().join("100");
        std::fs::create_dir_all(&issue_dir).unwrap();
        // Only write research.md; design.md, implementation.md, review.md are missing
        std::fs::write(issue_dir.join("research.md"), "# Research\n\nDone.\n").unwrap();
        let result = super::cmd_pipeline_status("100", dir.path());
        assert!(result.is_ok(), "should succeed even with partial artefacts");
    }

    #[test]
    fn test_pipeline_status_empty_directory() {
        let dir = tempfile::tempdir().unwrap();
        let issue_dir = dir.path().join("0");
        std::fs::create_dir_all(&issue_dir).unwrap();
        // No artefacts at all — directory exists but is empty
        let result = super::cmd_pipeline_status("0", dir.path());
        assert!(
            result.is_ok(),
            "should succeed when directory exists with no artefacts"
        );
    }
}