rhei-cli 0.3.0

Command-line driver for the Rhei agent runtime.
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
/// When a supervising state's task is woken, in the words of its
/// `execute_on:` value.
///
/// The value is a scope and an event, and a reader of `rhei states` is asking
/// one question of it: which moves under this task bring it back. The bare
/// value answers that only to someone who already knows the grammar.
// §FS-rhei-supervision.1.1 §FS-rhei-states-cmd.4
fn executes_on_phrase(execute_on: rhei_validator::ExecuteOn) -> &'static str {
    match execute_on {
        rhei_validator::ExecuteOn::ChildTerminal => "every finished child",
        rhei_validator::ExecuteOn::ChildTransition => "every child transition",
        rhei_validator::ExecuteOn::DescendantTerminal => "every finished descendant",
        rhei_validator::ExecuteOn::DescendantTransition => {
            "every descendant transition \u{2014} one invocation per hop"
        }
    }
}

fn render_state_machine_text(machine: &rhei_validator::StateMachine) -> String {
    let mut out = String::new();
    out.push_str(&format!(
        "State machine: {} (version: {})\n",
        machine.name,
        format_version(&machine.version)
    ));
    if !machine.models.is_empty() {
        out.push_str(&format!("Models: {}\n", machine.models.join(", ")));
    }
    if !machine.prompt_templates.is_empty() {
        out.push_str(&format!(
            "Prompt templates: {}\n",
            machine.prompt_templates.keys().cloned().collect::<Vec<_>>().join(", ")
        ));
    }
    if let Some(profiles) = machine.profiles.as_ref() {
        out.push_str("Profiles:\n");
        for (name, profile) in profiles {
            out.push_str(&format!(
                "  {name}: initial={}, allowed=[{}]\n",
                profile.initial,
                profile.allowed.join(", ")
            ));
        }
    }
    if let Some(policy) = machine.node_policy.as_ref() {
        out.push_str(&format!(
            "Node policy: root={}, default={}\n",
            policy.root, policy.default
        ));
    }

    out.push_str("\nStates:\n");
    if machine.states.is_empty() {
        out.push_str("  (none defined)\n");
    } else {
        for (idx, (name, def)) in machine.states.iter().enumerate() {
            if idx > 0 {
                out.push('\n');
            }
            let mut flags = Vec::new();
            if def.terminal {
                flags.push("final");
            }
            if def.gating {
                flags.push("gating");
            }
            if def.concurrent {
                flags.push("concurrent");
            }
            let flag_suffix =
                if flags.is_empty() { String::new() } else { format!(" [{}]", flags.join(", ")) };
            let description = def.description.as_deref().unwrap_or("");
            out.push_str(&format!("  {name}{flag_suffix}"));
            if !description.is_empty() {
                out.push_str(&format!(" — {description}"));
            }
            out.push('\n');
            if let Some(visits) = def.visits {
                out.push_str(&format!("      Visits: {visits}\n"));
            }
            // §FS-rhei-supervision.1.1: when the supervisor wakes, in the
            // words of the value — the bare value read as a noun ("task") said
            // nothing about which events reach it.
            if let Some(execute_on) = def.execute_on() {
                out.push_str(&format!("      Executes on: {}\n", executes_on_phrase(execute_on)));
            }
            if let Some(poll) = def.poll.as_ref() {
                out.push_str(&format!(
                    "      Poll: interval={}, max_attempts={}\n",
                    poll.interval, poll.max_attempts
                ));
            }
            if let Some(target) = def.target.as_deref() {
                out.push_str(&format!("      Target: {target}\n"));
            }
            if !def.all_targets.is_empty() {
                out.push_str(&format!("      Targets: {}\n", def.all_targets.join(", ")));
            }
            if !def.all_models.is_empty() {
                out.push_str(&format!("      Models: {}\n", def.all_models.join(", ")));
            } else if let Some(model) = def.model.as_deref() {
                out.push_str(&format!("      Model: {model}\n"));
            }
            if let Some(agent) = def.agent.as_ref() {
                out.push_str(&format!("      Agent: {}\n", agent.id()));
            }
            if let Some(mode) = def.agent_mode.as_deref() {
                out.push_str(&format!("      Agent mode: {mode}\n"));
            }
            if let Some(timeout) = def.agent_timeout.as_deref() {
                out.push_str(&format!("      Agent timeout: {timeout}\n"));
            }
            if def.program.is_some() {
                out.push_str("      Program: configured\n");
            }
            if let Some(timeout) = def.program_timeout.as_deref() {
                out.push_str(&format!("      Program timeout: {timeout}\n"));
            }
            if let Some(reference) = def.prompt_template.as_ref() {
                out.push_str(&format!("      Prompt template: {}\n", reference.name()));
            }
            if let Some(mcp_servers) = def.mcp_servers.as_ref() {
                let ids = mcp_servers.iter().map(|entry| entry.id()).collect::<Vec<_>>();
                out.push_str(&format!("      MCP servers: {}\n", ids.join(", ")));
            }
            if let Some(skills) = def.skills.as_ref() {
                let ids = skills.iter().map(|entry| entry.id()).collect::<Vec<_>>();
                out.push_str(&format!("      Skills: {}\n", ids.join(", ")));
            }
            if def.snapshot.is_some() {
                out.push_str("      Snapshot: configured\n");
            }
            if !def.inputs.is_empty() {
                out.push_str("      Inputs:\n");
                for artifact in &def.inputs {
                    out.push_str(&format!("        - {}: {}\n", artifact.name, artifact.path));
                }
            }
            if !def.outputs.is_empty() {
                out.push_str("      Outputs:\n");
                for artifact in &def.outputs {
                    out.push_str(&format!("        - {}: {}\n", artifact.name, artifact.path));
                }
            }
            if let Some(personality) =
                def.personality.as_deref().map(str::trim).filter(|s| !s.is_empty())
            {
                out.push_str(&format!("      Personality: {personality}\n"));
            }
            if let Some(instructions) = def.instructions.as_deref() {
                for line in instructions.lines() {
                    out.push_str(&format!("      {line}\n"));
                }
            }
        }
    }

    out.push_str("\nTransitions:\n");
    if machine.transitions.is_empty() {
        out.push_str("  (none declared)\n");
    } else {
        for rule in &machine.transitions {
            out.push_str(&format!("  {} -> {}", rule.from.0, rule.to.0));
            let mut annotations = Vec::new();
            if let Some(cb) = rule.on_leave.as_ref() {
                annotations.push(format!("on_leave={}", cb.0));
            }
            if let Some(cb) = rule.on_enter.as_ref() {
                annotations.push(format!("on_enter={}", cb.0));
            }
            if let Some(cond) = rule.condition.as_ref() {
                annotations.push(format!("when={cond}"));
            }
            if let Some(t) = rule.timeout.as_ref() {
                annotations.push(format!("timeout={t}"));
            }
            if !annotations.is_empty() {
                out.push_str(&format!(" ({})", annotations.join(", ")));
            }
            out.push('\n');
        }
    }

    out
}

fn render_state_machine_json(machine: &rhei_validator::StateMachine) -> Result<String> {
    let states: Vec<serde_json::Value> = machine
        .states
        .iter()
        .map(|(name, def)| {
            serde_json::json!({
                "name": name,
                "description": &def.description,
                "prompt_template": &def.prompt_template,
                "instructions": &def.instructions,
                "personality": &def.personality,
                "final": def.terminal,
                "gating": def.gating,
                "concurrent": def.concurrent,
                "poll": &def.poll,
                "visits": def.visits,
                "execute_on": &def.execute_on,
                "target": &def.target,
                "all_targets": &def.all_targets,
                "all_models": &def.all_models,
                "model": &def.model,
                "agent": def.agent.as_ref().map(|agent| agent.id()),
                "agent_mode": &def.agent_mode,
                "agent_timeout": &def.agent_timeout,
                "program": &def.program,
                "program_timeout": &def.program_timeout,
                "mcp_servers": &def.mcp_servers,
                "skills": &def.skills,
                "snapshot": &def.snapshot,
                "inputs": &def.inputs,
                "outputs": &def.outputs,
            })
        })
        .collect();

    let transitions =
        serde_json::to_value(&machine.transitions).context("serialize transitions")?;
    let version =
        serde_json::to_value(&machine.version).context("serialize state machine version")?;

    let payload = serde_json::json!({
        "name": machine.name,
        "models": &machine.models,
        "prompt_templates": &machine.prompt_templates,
        "profiles": &machine.profiles,
        "node_policy": &machine.node_policy,
        "version": version,
        "states": states,
        "transitions": transitions,
    });

    serde_json::to_string_pretty(&payload).context("render state machine as JSON")
}

/// The same payload as [`render_state_machine_json`], unserialized — for the
/// heterogeneous-project array shape. §FS-rhei-states-cmd.5
fn render_state_machine_json_value(machine: &rhei_validator::StateMachine) -> serde_json::Value {
    match render_state_machine_json(machine)
        .ok()
        .and_then(|rendered| serde_json::from_str(&rendered).ok())
    {
        Some(value) => value,
        None => serde_json::json!({ "name": machine.name }),
    }
}

fn format_version(value: &serde_yaml::Value) -> String {
    match value {
        serde_yaml::Value::String(s) => s.clone(),
        other => serde_yaml::to_string(other)
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|_| "unknown".to_string()),
    }
}

/// Read the markdown plan source file from disk.
fn read_input_file(path: &Path) -> MietteResult<String> {
    fs::read_to_string(path).map_err(|err| file_io_report(path, "failed to read input file", err))
}

/// A loaded plan with optional workspace task-to-file mapping.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LoadedPlanKind {
    SingleFile,
    Workspace,
    PantaProject,
}

/// Name any member rhei carrying its own `.agents/rhei/settings.json`: settings
/// resolve once, at the project root, so a copy inside a member is read by
/// nothing and its agent registry silently vanished. §FS-rhei-agents.1.1
fn ignored_member_settings_warnings(input: &Path, loaded: &LoadedPlan) -> Vec<String> {
    if !loaded.is_panta_project() {
        return Vec::new();
    }
    let Some(project) = workspace::panta_project_dir(input) else {
        return Vec::new();
    };
    let Ok(entries) = workspace::discover_rhei_entries(&project) else {
        return Vec::new();
    };
    entries
        .into_iter()
        .map(|entry| entry.join(".agents/rhei/settings.json"))
        .filter(|settings| settings.is_file())
        .map(|settings| {
            format!(
                "{} is ignored: settings resolve at the project root, so move its contents into \
                 {} — nothing reads a member rhei's own settings file",
                settings.display(),
                project.join(".agents/rhei/settings.json").display()
            )
        })
        .collect()
}

/// One warning per rhei that loaded but holds no tickets. Empty is valid, but a
/// mistyped `tasks/` loads identically, so name it rather than report a bare
/// green. §FS-rhei-plan-language.1.2
fn empty_rhei_warnings(loaded: &LoadedPlan) -> Vec<String> {
    match loaded.kind {
        LoadedPlanKind::PantaProject => loaded
            .rhei_ids
            .iter()
            .filter(|id| {
                let prefix = format!("{id}.");
                !loaded.rhei.tasks.iter().any(|task| task.id.to_string().starts_with(&prefix))
            })
            .map(|id| empty_rhei_help(id))
            .collect(),
        LoadedPlanKind::Workspace if loaded.rhei.tasks.is_empty() => vec![
            "this workspace holds no tickets: task files are the non-hidden `*.md` files \
             under `tasks/`"
                .to_string(),
        ],
        // A single-file rhei may be empty too, and an emptied one looks exactly
        // like a freshly created one. §FS-rhei-plan-language.1.1
        LoadedPlanKind::SingleFile if loaded.rhei.tasks.is_empty() => vec![
            "this rhei holds no tickets: its tickets are the task nodes under its \
             `## Tasks` section"
                .to_string(),
        ],
        _ => Vec::new(),
    }
}

struct LoadedPlan {
    rhei: rhei_core::ast::Rhei,
    kind: LoadedPlanKind,
    /// For directory workspaces: maps task ID string → source file path.
    /// For Panta projects: maps project-qualified task IDs to owning files.
    /// Empty for single-file plans.
    task_sources: HashMap<String, PathBuf>,
    /// For Panta projects: maps project-qualified task IDs to owning rhei roots.
    task_roots: HashMap<String, PathBuf>,
    /// For Panta projects: link bases for merged content sections, in section order.
    content_section_roots: Vec<PathBuf>,
    /// For Panta projects: rhei ids in load order (`basin` last when present).
    rhei_ids: Vec<String>,
    /// Machine name each rhei declared with its own `**States:**`, when it
    /// did. §DA-per-rhei-state-machines
    rhei_machines: HashMap<String, String>,
    /// Execution root of each rhei, keyed by rhei id. §AR-rhei-panta.4
    rhei_roots: HashMap<String, PathBuf>,
    /// Title of each rhei, keyed by rhei id. §FS-rhei-memory.3.1
    rhei_titles: HashMap<String, String>,
    /// Plan document of each rhei, keyed by rhei id. §FS-rhei-memory.3.4
    rhei_plans: HashMap<String, PathBuf>,
    /// Rheis a lenient load skipped, one message each; empty for a strict load.
    unloadable: Vec<String>,
}

impl LoadedPlan {
    /// Return the file path that contains the given task.
    /// For single-file plans, returns `fallback` (the plan file itself).
    fn task_file(&self, task_id: &str, fallback: &Path) -> PathBuf {
        self.task_sources.get(task_id).cloned().unwrap_or_else(|| fallback.to_path_buf())
    }

    fn task_root(&self, task_id: &str, fallback: &Path) -> PathBuf {
        self.task_roots.get(task_id).cloned().unwrap_or_else(|| fallback.to_path_buf())
    }

    fn is_panta_project(&self) -> bool {
        self.kind == LoadedPlanKind::PantaProject
    }

    /// Resolve where a merged-graph ticket's rewrites land: heading file,
    /// metadata file, in-file id, and the owning rhei's execution root.
    /// §FS-rhei-panta.6.1 routes every rewrite to the owning rhei.
    fn task_route(&self, task_id: &str, input: &Path) -> TaskRoute {
        let task_file = self.task_file(task_id, input);
        let fallback_root = match self.kind {
            LoadedPlanKind::SingleFile => {
                input.parent().unwrap_or(Path::new(".")).to_path_buf()
            }
            _ => input.to_path_buf(),
        };
        let execution_root = self.task_root(task_id, &fallback_root);
        // Ticket headings inside the owning file are rhei-local: strip the
        // project-qualifying rhei id segment. §AR-rhei-panta.3
        let local_id = rhei_local_id_str(task_id).to_string();
        // A workspace-shaped rhei keeps ticket metadata in its own index, a
        // single-file rhei in the rhei file itself. The basin has no authored
        // index, so its metadata lands in the manifest. §FS-rhei-panta.6.1
        if self.is_basin_task(task_id) {
            // `input` may name the project by directory or by manifest file;
            // resolve it rather than assuming, since not every caller
            // normalizes before routing.
            let project_dir = workspace::panta_project_dir(input)
                .unwrap_or_else(|| input.to_path_buf());
            return TaskRoute {
                task_file,
                metadata_file: project_dir.join(workspace::PANTA_INDEX_FILE),
                local_id,
                metadata_id: task_id.to_string(),
                execution_root,
            };
        }
        let metadata_file = if workspace::is_workspace(&execution_root) {
            execution_root.join("index.rhei.md")
        } else {
            task_file.clone()
        };
        let metadata_id = local_id.clone();
        TaskRoute { task_file, metadata_file, local_id, metadata_id, execution_root }
    }

    /// True when `task_id` belongs to the synthetic basin rhei of a project.
    fn is_basin_task(&self, task_id: &str) -> bool {
        self.is_panta_project()
            && task_id.split_once('.').map(|(rhei, _)| rhei) == Some(workspace::BASIN_RHEI_ID)
    }
}

/// Strip the project-qualifying rhei segment from a ticket id string. Mirrors
/// [`LoadedPlan::task_route`] heading routing; an unqualified id passes
/// through unchanged. §AR-rhei-panta.3
fn rhei_local_id_str(task_id: &str) -> &str {
    task_id.split_once('.').map(|(_, rest)| rest).unwrap_or(task_id)
}

/// True when `value` has the shape of a ticket id (`3`, `auth.1`, `auth.1.2`)
/// rather than a filesystem path: dot-separated segments, no separators, and
/// no markdown extension.
fn looks_like_task_id(value: &Path) -> bool {
    let Some(text) = value.to_str() else {
        return false;
    };
    if text.is_empty() || text.contains('/') || text.contains('\\') || text.ends_with(".md") {
        return false;
    }
    text.split('.').all(|segment| {
        !segment.is_empty()
            && segment.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
    })
}

/// Resolve an omitted plan target: walk up from the current directory to the
/// nearest project (`index.panta.md`) or workspace rhei (`index.rhei.md`); a
/// lone rhei resolves in the current directory only. §FS-rhei-panta.6
fn resolve_plan_target(input: Option<PathBuf>) -> MietteResult<PlanTarget> {
    let resolved = resolve_plan_path(input)?;
    // A rhei inside a project is loaded through the project and narrowed to
    // itself, so cross-rhei priors resolve. §FS-rhei-panta.6
    match workspace::panta_member(&resolved) {
        Some((project, id)) => Ok(PlanTarget { path: project, implied_scope: vec![id] }),
        None => Ok(PlanTarget::whole(resolved)),
    }
}

fn resolve_plan_path(input: Option<PathBuf>) -> MietteResult<PathBuf> {
    if let Some(input) = input {
        // The positional slot takes a plan, but it sits where a ticket id
        // looks like it belongs — and commands that select a ticket take it
        // through `--task`. Name that mistake instead of failing later with a
        // path error about something the user never meant as a path.
        if !input.exists() {
            if looks_like_task_id(&input) {
                return Err(miette!(
                    help = "this argument takes a plan or project path, not a ticket id.",
                    "'{}' is not a path. This argument takes a plan or project; select a \
                     ticket with `--task {}` and let the plan resolve on its own.",
                    input.display(),
                    input.display()
                ));
            }
            return Err(miette!(
                help = io_error_help(&input, std::io::ErrorKind::NotFound),
                "no plan or project at '{}'. Pass a `.rhei.md` file, a workspace \
                 directory, or omit the argument to use the enclosing project.",
                input.display()
            ));
        }
        return Ok(input);
    }
    let cwd = std::env::current_dir()
        .map_err(|err| miette!(
            help = cwd_help(),
            "failed to read the current directory: {err}"
        ))?;
    let mut dir = Some(cwd.as_path());
    while let Some(current) = dir {
        if current.join(workspace::PANTA_INDEX_FILE).is_file() {
            return Ok(current.to_path_buf());
        }
        // §FS-rhei-panta.6: the conventional `panta/` child `rhei init`
        // creates resolves from anywhere in the host repository.
        let conventional = current.join("panta");
        if conventional.join(workspace::PANTA_INDEX_FILE).is_file() {
            return Ok(conventional);
        }
        if current.join("index.rhei.md").is_file() {
            return Ok(current.to_path_buf());
        }
        // §FS-rhei-panta.6: a loose rhei — counted exactly as project
        // discovery counts it — resolves in the invocation directory only;
        // ancestors are adopted solely through explicit manifests.
        if current == cwd {
            let mut plans = workspace::discover_rhei_entries(current).unwrap_or_default();
            match plans.len() {
                0 => {}
                1 => return Ok(plans.remove(0)),
                _ => {
                    let names: Vec<String> = plans
                        .iter()
                        .filter_map(|path| path.file_name())
                        .map(|name| name.to_string_lossy().into_owned())
                        .collect();
                    return Err(miette!(
help = "run `rhei init --here` to make the directory a project, or pass one of the rheis above.",

                        "{} holds {} rheis ({}) but no `index.panta.md`, so there is no \
                         single plan to pick. Pass one explicitly, or run `rhei init` to \
                         make the directory a project (writes index.panta.md)",
                        current.display(),
                        names.len(),
                        names.join(", ")
                    ));
                }
            }
        }
        dir = current.parent();
    }
    // Resolution only walks up, so a project adopted in a subdirectory is
    // invisible from the repo root — the one place bare commands get run.
    // §FS-rhei-panta.6
    let nearby = nearby_projects(&cwd);
    if !nearby.is_empty() {
        let listed = nearby
            .iter()
            .map(|path| format!("  rhei <command> {}", path.display()))
            .collect::<Vec<_>>()
            .join("\n");
        return Err(miette!(
            help = "run the command against one of the paths listed above, or cd into it.",
            "no Rhei plan found at or above {}. Target resolution only walks up, but there \
             {} below it:\n{}",
            cwd.display(),
            if nearby.len() == 1 { "is a project" } else { "are projects" },
            listed
        ));
    }
    Err(miette!(
help = "pass a plan path, or run `rhei init` to make this directory a project.",

        "no Rhei plan found: neither {} nor any parent directory contains an \
         `index.panta.md` project manifest, a workspace `index.rhei.md`, or a \
         `*.rhei.md` plan file. Pass a plan path (`rhei <command> path/to/plan.rhei.md`) \
         or run inside a project",
        cwd.display()
    ))
}

/// Panta projects within a few levels below `root`, for a resolution failure
/// that would otherwise just say "not found". Bounded in depth and breadth so
/// the search stays cheap, and skipping hidden and build directories.
fn nearby_projects(root: &Path) -> Vec<PathBuf> {
    const MAX_DEPTH: usize = 3;
    const MAX_HITS: usize = 5;
    let mut found = Vec::new();
    let mut frontier = vec![(root.to_path_buf(), 0usize)];
    while let Some((dir, depth)) = frontier.pop() {
        if depth >= MAX_DEPTH || found.len() >= MAX_HITS {
            continue;
        }
        let Ok(entries) = fs::read_dir(&dir) else {
            continue;
        };
        let mut children: Vec<PathBuf> = entries
            .flatten()
            .map(|entry| entry.path())
            .filter(|path| path.is_dir())
            .filter(|path| {
                path.file_name().and_then(|name| name.to_str()).is_some_and(|name| {
                    !name.starts_with('.') && !matches!(name, "target" | "node_modules")
                })
            })
            .collect();
        children.sort();
        for child in children {
            if workspace::is_panta_project(&child) {
                found.push(child);
                if found.len() >= MAX_HITS {
                    break;
                }
            } else {
                frontier.push((child, depth + 1));
            }
        }
    }
    found.sort();
    found
}

/// A resolved plan target: the document to load, plus the rhei ids the
/// invocation is implicitly narrowed to.
///
/// `implied_scope` is non-empty only when the caller pointed at a rhei that
/// belongs to a Panta project. The project is what gets loaded — a member rhei
/// cannot resolve its cross-rhei `**Prior:**` or its state machine without it —
/// and the id it was pointed at narrows the tickets acted on, exactly as
/// `--rhei <id>` would.
// §FS-rhei-panta.6: a rhei that belongs to a project always loads through it.
#[derive(Debug, Clone)]
struct PlanTarget {
    path: PathBuf,
    implied_scope: Vec<String>,
}

impl PlanTarget {
    fn whole(path: PathBuf) -> Self {
        Self { path, implied_scope: Vec::new() }
    }

    fn path(&self) -> &Path {
        &self.path
    }

    /// The `--rhei` selection this invocation should use: the flag's value when
    /// given, otherwise the rhei the target pointed at.
    fn scope_with(&self, selected: &[String]) -> Vec<String> {
        if selected.is_empty() {
            self.implied_scope.clone()
        } else {
            selected.to_vec()
        }
    }
}

/// The set of rhei ids a project-scoped invocation is narrowed to. `None` is
/// the whole project. §FS-rhei-panta.6
type RheiScope = Option<BTreeSet<String>>;

/// Validate a `--rhei` selection against the loaded project and resolve it into
/// a scope. An unknown id is an error that names the available rheis; an empty
/// selection leaves the invocation project-wide. §FS-rhei-panta.6
fn resolve_rhei_scope(loaded: &LoadedPlan, selected: &[String]) -> MietteResult<RheiScope> {
    if selected.is_empty() {
        return Ok(None);
    }
    let available: BTreeSet<&str> = loaded.rhei_ids.iter().map(String::as_str).collect();
    let mut scope = BTreeSet::new();
    for name in selected {
        let name = name.trim();
        if !available.contains(name) {
            return Err(miette!(
                help = did_you_mean(name, &loaded.rhei_ids)
                    .unwrap_or_else(|| "this project holds no rheis.".to_string()),
                "unknown rhei '{}'; this project has: {}",
                name,
                loaded.rhei_ids.join(", ")
            ));
        }
        scope.insert(name.to_string());
    }
    Ok(Some(scope))
}

/// True when a project-qualified ticket id belongs to an in-scope rhei.
/// Narrowing selects candidate tickets only — prior resolution still spans the
/// whole project. §FS-rhei-panta.6.1
fn task_in_rhei_scope(scope: &RheiScope, task_id: &str) -> bool {
    let Some(scope) = scope else { return true };
    let owner = task_id.split_once('.').map(|(head, _)| head).unwrap_or(task_id);
    scope.contains(owner)
}

/// Build a scope set from an already-validated `--rhei` selection. Validation
/// against the project's rhei ids happens once at command entry.
fn rhei_scope_set(selected: &[String]) -> RheiScope {
    if selected.is_empty() {
        None
    } else {
        Some(selected.iter().map(|id| id.trim().to_string()).collect())
    }
}

/// Drop candidate tickets outside the invocation's `--rhei` scope. Applied to
/// the readiness result so priors still resolve across the whole project.
/// §FS-rhei-panta.6.1
fn narrow_to_rhei_scope<'a>(
    tasks: Vec<&'a rhei_core::ast::Task>,
    scope: &RheiScope,
) -> Vec<&'a rhei_core::ast::Task> {
    if scope.is_none() {
        return tasks;
    }
    tasks.into_iter().filter(|task| task_in_rhei_scope(scope, &task.id.to_string())).collect()
}

/// Resolve a CLI ticket target to its project-qualified id: the qualified id
/// itself, or a rhei-local shorthand unambiguous within the scope. A `--rhei`
/// narrowing bounds both. §FS-rhei-panta.6 §FS-rhei-panta.6.1
fn resolve_cli_task_id(
    loaded: &LoadedPlan,
    task_id_str: &str,
    scope: &RheiScope,
) -> MietteResult<String> {
    let target = parse_task_id(task_id_str);
    if find_task_by_id(&loaded.rhei.tasks, &target).is_some() {
        if !task_in_rhei_scope(scope, task_id_str) {
            return Err(miette!(
help = rhei_scope_help(),

                "task '{}' is outside the --rhei scope ({})",
                task_id_str,
                scope_label(scope)
            ));
        }
        return Ok(task_id_str.to_string());
    }
    let candidates: Vec<String> = loaded
        .rhei_ids
        .iter()
        .filter(|rhei_id| scope.as_ref().is_none_or(|scope| scope.contains(*rhei_id)))
        .map(|rhei_id| format!("{rhei_id}.{task_id_str}"))
        .filter(|qualified| {
            find_task_by_id(&loaded.rhei.tasks, &parse_task_id(qualified)).is_some()
        })
        .collect();
    match candidates.len() {
        0 if scope.is_some() => Err(miette!(
help = rhei_scope_help(),

            "task '{}' not found in the --rhei scope ({})",
            task_id_str,
            scope_label(scope)
        )),
        0 => {
            // Ticket ids are project-qualified now; point a user typing a
            // stale or partial id at the closest real ones. §FS-rhei-panta.6
            let scope_noun = if loaded.is_panta_project() { "project" } else { "rhei" };
            let similar = similar_task_ids(loaded, task_id_str);
            if similar.is_empty() {
                // Nothing close enough to suggest — name the next step rather
                // than leaving a dead end.
                Err(miette!(
help = task_id_help(),

                    "task '{}' not found in this {}; `rhei list` shows every ticket id",
                    task_id_str,
                    scope_noun
                ))
            } else {
                Err(miette!(
help = task_id_help(),

                    "task '{}' not found in this {}; closest ids: {}",
                    task_id_str,
                    scope_noun,
                    similar.join(", ")
                ))
            }
        }
        1 => Ok(candidates.into_iter().next().expect("one candidate")),
        _ => Err(miette!(
help = "a qualified id is <rhei>.<ticket>. Copy one from: rhei list",

            "task id '{}' is ambiguous across rheis; use a qualified id: {}",
            task_id_str,
            candidates.join(", ")
        )),
    }
}

/// Qualified ids that contain the attempted id as a substring, for
/// did-you-mean hints. Capped so a large project stays readable.
fn similar_task_ids(loaded: &LoadedPlan, needle: &str) -> Vec<String> {
    if needle.is_empty() {
        return Vec::new();
    }
    let mut tasks = Vec::new();
    collect_plan_tasks(&loaded.rhei.tasks, &mut tasks);
    tasks
        .iter()
        .map(|task| task.id.to_string())
        .filter(|id| id.contains(needle))
        .take(5)
        .collect()
}

/// Render a scope for diagnostics: the named rheis, or the whole project.
fn scope_label(scope: &RheiScope) -> String {
    match scope {
        Some(scope) => scope.iter().cloned().collect::<Vec<_>>().join(", "),
        None => "whole project".to_string(),
    }
}

/// Resolved write routing for one ticket; see [`LoadedPlan::task_route`].
struct TaskRoute {
    /// File whose headings contain the ticket.
    task_file: PathBuf,
    /// File whose frontmatter owns the ticket's runtime metadata.
    metadata_file: PathBuf,
    /// The ticket id as written inside `task_file`.
    local_id: String,
    /// The `metadata.tasks.<id>` key inside `metadata_file`: rhei-local for an
    /// authored rhei, project-qualified for the synthetic basin, whose
    /// metadata shares the project manifest. §FS-rhei-panta.6.1
    metadata_id: String,
    /// Root directory for the owning rhei's `runtime/` artifacts.
    execution_root: PathBuf,
}

/// Explain that a member rhei validated its whole project.
///
/// Pointing validation at one rhei of a project widens rather than narrows, and
/// silently reporting another rhei's errors under a command the user aimed at
/// this one would be bewildering. Say what happened and why.
// §FS-rhei-validate.1.1: validation takes no `--rhei`, so it widens instead.
fn report_validation_widened(target: &PlanTarget) {
    let Some(id) = target.implied_scope.first() else {
        return;
    };
    println!(
        "Scope: rhei '{id}' belongs to the project at {}, and its state machine, settings, and \
         cross-rhei **Prior:** resolve only there — validating the whole project.",
        display_path(target.path())
    );
}

fn report_panta_scope_narrowed(loaded: &LoadedPlan, command: &str, scope: &RheiScope) {
    if !(loaded.is_panta_project() || loaded.rhei_ids.len() > 1) {
        return;
    }
    let affected: Vec<&str> = loaded
        .rhei_ids
        .iter()
        .map(String::as_str)
        .filter(|id| scope.as_ref().is_none_or(|scope| scope.contains(*id)))
        .collect();
    let qualifier = if scope.is_some() { "narrowed to" } else { "operates project-wide across" };
    let noun = if affected.len() == 1 { "rhei" } else { "rheis" };
    let line = format!(
        "Scope: `rhei {}` {} {} {}: {}",
        command,
        qualifier,
        affected.len(),
        noun,
        affected.join(", ")
    );
    // Prose, so it moves to stderr when stdout is a record stream rather than
    // being dropped: a JSON consumer must not have to parse around it, and an
    // operator watching the run should still see it. §FS-rhei-run-json.1
    if stdout_carries_json_records() {
        eprintln!("{line}");
    } else {
        println!("{line}");
    }
}

/// Load a plan from a file or directory workspace.
fn load_plan(path: &Path) -> MietteResult<LoadedPlan> {
    load_plan_with(path, false)
}

/// Load a plan, and for a project skip rheis that fail to load rather than
/// failing the whole project. Only read-only surfaces that can report the skip
/// may use this: a partial graph cannot decide readiness. §FS-rhei-panta.6
fn load_plan_leniently(path: &Path) -> MietteResult<LoadedPlan> {
    load_plan_with(path, true)
}

fn load_plan_with(path: &Path, lenient: bool) -> MietteResult<LoadedPlan> {
    if let Some(project_dir) = workspace::panta_project_dir(path) {
        let project = if lenient {
            workspace::load_panta_project_lenient(&project_dir)
        } else {
            workspace::load_panta_project(&project_dir)
        }
        .map_err(|err| nested_parse_report(&err))?;
        Ok(LoadedPlan {
            rhei: project.rhei,
            kind: LoadedPlanKind::PantaProject,
            task_sources: project.task_sources,
            task_roots: project.task_roots,
            content_section_roots: project.content_section_roots,
            rhei_ids: project.rhei_ids,
            rhei_machines: project.rhei_machines,
            rhei_roots: project.rhei_roots,
            rhei_titles: project.rhei_titles,
            rhei_plans: project.rhei_plans,
            unloadable: project.unloadable,
        })
    } else if let Some(ws_dir) = workspace::workspace_dir(path) {
        // §AR-rhei-panta.2: a bare Directory Workspace is the single rhei of
        // an implicit Panta; the graph shape matches an explicit project.
        let ws = workspace::load_workspace(&ws_dir).map_err(|err| nested_parse_report(&err))?;
        let project = workspace::wrap_rhei_as_implicit_panta(ws, &ws_dir)
            .map_err(|err| nested_parse_report(&err))?;
        Ok(implicit_loaded_plan(project, LoadedPlanKind::Workspace))
    } else {
        let input = read_input_file(path)?;
        let rhei = rhei_core::parse(&input).map_err(|err| parse_report(path, &input, &err))?;
        // §AR-rhei-panta.2/.3: a bare rhei file wraps as an implicit Panta
        // with its id derived from the file stem.
        let project = workspace::implicit_panta_from_file_rhei(rhei, path)
            .map_err(|err| nested_parse_report(&err))?;
        Ok(implicit_loaded_plan(project, LoadedPlanKind::SingleFile))
    }
}

/// Build a [`LoadedPlan`] from an implicit-Panta wrap, keeping the original
/// input shape in `kind` for shape-specific behavior (labels, parallel gating).
fn implicit_loaded_plan(
    project: rhei_core::workspace::PantaProject,
    kind: LoadedPlanKind,
) -> LoadedPlan {
    LoadedPlan {
        rhei: project.rhei,
        kind,
        task_sources: project.task_sources,
        task_roots: project.task_roots,
        content_section_roots: project.content_section_roots,
        rhei_ids: project.rhei_ids,
        rhei_machines: project.rhei_machines,
        rhei_roots: project.rhei_roots,
        rhei_titles: project.rhei_titles,
        rhei_plans: project.rhei_plans,
        unloadable: project.unloadable,
    }
}

/// Load a plan for `rhei validate`, collecting recoverable parse errors where
/// validation promises batch diagnostics.
fn load_plan_for_validation(path: &Path) -> MietteResult<LoadedPlan> {
    if let Some(project_dir) = workspace::panta_project_dir(path) {
        let project = workspace::load_panta_project(&project_dir)
            .map_err(|err| nested_parse_report(&err))?;
        return Ok(LoadedPlan {
            rhei: project.rhei,
            kind: LoadedPlanKind::PantaProject,
            task_sources: project.task_sources,
            task_roots: project.task_roots,
            content_section_roots: project.content_section_roots,
            rhei_ids: project.rhei_ids,
            rhei_machines: project.rhei_machines,
            rhei_roots: project.rhei_roots,
            rhei_titles: project.rhei_titles,
            rhei_plans: project.rhei_plans,
            unloadable: project.unloadable,
        });
    }

    if let Some(ws_dir) = workspace::workspace_dir(path) {
        return load_workspace_for_validation(&ws_dir);
    }

    let raw = read_input_file(path)?;
    let (maybe_rhei, errs) = rhei_core::parser::parse_collect(&raw);
    match (maybe_rhei, errs.is_empty()) {
        (Some(rhei), true) => {
            let project = workspace::implicit_panta_from_file_rhei(rhei, path)
                .map_err(|err| nested_parse_report(&err))?;
            Ok(implicit_loaded_plan(project, LoadedPlanKind::SingleFile))
        }
        (_, false) | (None, _) => Err(parse_errors_report(path, &raw, &errs)),
    }
}

fn load_workspace_for_validation(ws_dir: &Path) -> MietteResult<LoadedPlan> {
    let index_path = ws_dir.join("index.rhei.md");
    let index_raw = read_input_file(&index_path)?;
    let index = rhei_core::parser::parse_workspace_index(&index_raw)
        .map_err(|err| parse_report(&index_path, &index_raw, &err))?;

    let tasks_dir = ws_dir.join("tasks");
    let mut all_tasks = Vec::new();
    let mut task_sources = HashMap::new();
    let mut parse_error_groups = Vec::new();
    let mut duplicate_task_error: Option<String> = None;

    if tasks_dir.is_dir() {
        let task_files = workspace::discover_task_files(&tasks_dir)
            .map_err(|err| nested_parse_report(&err))?;

        for path in task_files {
            let raw = read_input_file(&path)?;
            let (maybe_tasks, errors) =
                rhei_core::parser::parse_workspace_tasks_collect_with_structure(
                    &raw,
                    &index.structure,
                );
            if !errors.is_empty() {
                parse_error_groups.push(ParseErrorGroup { path, input: raw, errors });
                continue;
            }
            let Some(tasks) = maybe_tasks else {
                continue;
            };
            for task in &tasks {
                if duplicate_task_error.is_none() {
                    if let Err(err) =
                        collect_workspace_task_sources(task, &path, &mut task_sources)
                    {
                        duplicate_task_error = Some(err.message);
                    }
                }
            }
            all_tasks.extend(tasks);
        }
    }

    if !parse_error_groups.is_empty() {
        return Err(workspace_parse_errors_report(&parse_error_groups));
    }
    if let Some(error) = duplicate_task_error {
        return Err(miette!(
            help = "two task files in tasks/ declare the same id. Renumber one of them, then \
                    re-check with: rhei validate <workspace>",
            "{error}"
        ));
    }

    // An empty workspace is a valid, empty rhei; `rhei validate` warns rather
    // than failing the whole project's load. §FS-rhei-plan-language.1.2

    let ws = rhei_core::workspace::Workspace {
        rhei: rhei_core::ast::Rhei {
            title: index.title,
            states: index.states,
            states_declared: index.states_declared,
            structure: index.structure,
            metadata: index.metadata,
            content_sections: index.content_sections,
            tasks: all_tasks,
        },
        task_sources,
    };
    let project = workspace::wrap_rhei_as_implicit_panta(ws, ws_dir)
        .map_err(|err| nested_parse_report(&err))?;
    Ok(implicit_loaded_plan(project, LoadedPlanKind::Workspace))
}

fn collect_workspace_task_sources(
    task: &rhei_core::ast::Task,
    path: &Path,
    task_sources: &mut HashMap<String, PathBuf>,
) -> rhei_core::parser::Result<()> {
    let id = task.id.to_string();
    if let Some(existing) = task_sources.get(&id) {
        return Err(rhei_core::parser::ParseError::new(
            format!(
                "duplicate task ID '{}': defined in both {} and {}",
                id,
                existing.display(),
                path.display()
            ),
            None,
        ));
    }
    task_sources.insert(id, path.to_path_buf());

    for child in &task.children {
        collect_workspace_task_sources(child, path, task_sources)?;
    }

    Ok(())
}

/// Execute the `validate` subcommand once or in watch mode.
fn validate_command(input: &Path, state_machine: Option<&Path>, watch: bool) -> MietteResult<()> {
    if watch {
        watch_validation_command(input, state_machine)
    } else {
        run_validation_once(input, state_machine)
    }
}

/// Parse a plan, load the selected states, and print validation results.
fn run_validation_once(input: &Path, state_machine: Option<&Path>) -> MietteResult<()> {
    let warnings = validation_warnings_or_error(input, state_machine)?;
    print_validation_report(&warnings);
    Ok(())
}

/// One whole validation pass, before anything decides what to do with it.
///
/// `rhei validate` collapses this straight into a report and a set of warnings,
/// but a command that validates its *own* write compares two passes to tell the
/// errors it introduced from the ones it found — which needs the error strings
/// themselves, not a rendered diagnostic.
// §FS-rhei-new.5.2
struct ValidationPass {
    errors: Vec<String>,
    warnings: Vec<String>,
    /// Guidance the errors carry without owning: the project-global lists a
    /// create elsewhere can change, kept out of the error text so the pre/post
    /// diff stays stable. §FS-rhei-new.5.2
    help: Vec<String>,
    /// The states file the pass resolved, for an error report to name.
    state_machine: Option<PathBuf>,
}

/// Run the whole validation pass, failing on the first error report and
/// returning its warnings otherwise.
///
/// Split out so a command that validates its *own* write can reuse the exact
/// pass `rhei validate` runs without its output — `rhei new` checks the project
/// still loads before it reports success.
// §FS-rhei-new.5.1
fn validation_warnings_or_error(
    input: &Path,
    state_machine: Option<&Path>,
) -> MietteResult<Vec<String>> {
    let pass = validation_pass(input, state_machine)?;
    if !pass.errors.is_empty() {
        return Err(validation_report(
            input,
            pass.state_machine.as_deref(),
            &pass.errors,
            &pass.help,
        ));
    }
    Ok(pass.warnings)
}

/// The validation pass itself, reported as data. §FS-rhei-new.5.2
fn validation_pass(input: &Path, state_machine: Option<&Path>) -> MietteResult<ValidationPass> {
    let loaded = load_plan_for_validation(input)?;

    let resolved = resolve_state_machines_for_loaded_plan(input, &loaded, state_machine)?;
    let machines = resolved.validator_set();
    let normalized_input = normalize_workspace_input(input);
    let base_path = if normalized_input.is_dir() {
        normalized_input.as_path()
    } else {
        normalized_input.parent().unwrap_or(Path::new("."))
    };
    let mut report = if loaded.is_panta_project() {
        // Panta task links validate against each ticket's owning rhei root. §AR-rhei-panta.5
        rhei_validator::validate_with_machine_set_and_link_bases(
            &loaded.rhei,
            &machines,
            base_path,
            &loaded.task_roots,
            &loaded.content_section_roots,
        )
    } else {
        let mut report = rhei_validator::Validator::with_machines(machines.clone())
            .validate_with_base(&loaded.rhei, Some(base_path));
        report.warnings.dedup();
        report
    };
    let workspace_root = execution_workspace_root(input);
    let settings = load_merged_settings(&workspace_root)?;
    for machine in machines.distinct() {
        report.errors.extend(validate_machine_settings_references(machine, &settings));
    }
    report
        .errors
        .extend(validate_task_execution_override_settings_references(&loaded.rhei, &settings));
    report.errors.extend(validate_snapshot_plan_context(&loaded, &resolved));
    report.warnings.extend(snapshot_orphan_validation_warnings(
        &workspace_root,
        &loaded,
        &resolved,
        &settings,
    )?);
    // §FS-rhei-panta.6: an empty project is valid, but say discovery found
    // nothing — a misnamed or misplaced plan is otherwise silently invisible
    // behind a green validation.
    if loaded.is_panta_project() && loaded.rhei_ids.is_empty() {
        report.warnings.push(format!(
            "the project holds no rheis: discovery looks only for `*.rhei.md` files and \
             workspace directories placed directly next to index.panta.md — {}",
            add_a_rhei_hint()
        ));
    }
    // An empty rhei is valid, but a mistyped `tasks/` looks identical to a
    // deliberately empty one — name it rather than let it pass unremarked.
    // §FS-rhei-plan-language.1.2
    report.warnings.extend(empty_rhei_warnings(&loaded));
    report.warnings.extend(ignored_member_settings_warnings(input, &loaded));

    report.help.dedup();
    Ok(ValidationPass {
        errors: report.errors,
        warnings: report.warnings,
        help: report.help,
        state_machine: resolved.default.path.clone(),
    })
}

/// Print success output and any non-fatal validation warnings.
fn print_validation_report(warnings: &[String]) {
    println!("Validation succeeded");
    for warning in warnings {
        println!("warning: {warning}");
    }
}

/// Watch the plan and states files and re-run validation on relevant changes.
fn watch_validation_command(input: &Path, state_machine: Option<&Path>) -> MietteResult<()> {
    let mut watch_plan = validation_watch_plan(input, state_machine);

    println!(
        "Watch mode started for '{}' (states: {})",
        input.display(),
        watch_plan.state_machine_label,
    );

    let (tx, rx) = mpsc::channel();
    let mut watcher: RecommendedWatcher = RecommendedWatcher::new(
        move |res| {
            let _ = tx.send(res);
        },
        Config::default(),
    )
    .map_err(|err| miette!(
        help = watch_help(),
        "failed to initialize file watcher: {err}"
    ))?;

    let mut watched = Vec::new();
    register_watch_roots(&mut watcher, &watch_plan.roots, &mut watched)?;

    run_validation_pass(input, state_machine);

    loop {
        let event = match rx.recv() {
            Ok(Ok(event)) => event,
            Ok(Err(err)) => {
                eprintln!("watch error: {err}");
                continue;
            }
            Err(err) => return Err(miette!(
                help = watch_help(),
                "watch channel disconnected: {err}"
            )),
        };

        if !should_revalidate(&event, &watch_plan.targets) {
            continue;
        }

        while debounce_has_relevant_event(&rx, &watch_plan.targets) {}

        println!("--- change detected, revalidating ---");
        run_validation_pass(input, state_machine);

        // `prompt_templates/` is optional, so the initial plan may have had no
        // directory to watch recursively. Re-plan after every pass: once the
        // author creates it, the next pass picks up edits to the files inside
        // instead of watching a directory that no longer describes the tree.
        watch_plan = validation_watch_plan(input, state_machine);
        register_watch_roots(&mut watcher, &watch_plan.roots, &mut watched)?;
    }
}

/// Watch every planned root that is not already being watched.
fn register_watch_roots(
    watcher: &mut RecommendedWatcher,
    roots: &[WatchRoot],
    watched: &mut Vec<WatchRoot>,
) -> MietteResult<()> {
    for root in roots {
        if watched.contains(root) {
            continue;
        }
        watcher.watch(&root.path, root.mode).map_err(|err| {
            miette!(help = watch_help(), "failed to watch '{}': {err}", root.path.display())
        })?;
        watched.push(root.clone());
    }
    Ok(())
}

/// Run one validation pass in watch mode, writing any failure to stderr.
fn run_validation_pass(input: &Path, state_machine: Option<&Path>) {
    if let Err(err) = run_validation_once(input, state_machine) {
        eprintln!("{err:?}");
    }
}

fn debounce_has_relevant_event(
    rx: &mpsc::Receiver<notify::Result<Event>>,
    targets: &[WatchTarget],
) -> bool {
    match rx.recv_timeout(Duration::from_millis(250)) {
        Ok(Ok(event)) => should_revalidate(&event, targets),
        Ok(Err(err)) => {
            eprintln!("watch error: {err}");
            false
        }
        Err(RecvTimeoutError::Timeout) => false,
        Err(RecvTimeoutError::Disconnected) => false,
    }
}

fn should_revalidate(event: &Event, targets: &[WatchTarget]) -> bool {
    if !is_relevant_event_kind(&event.kind) {
        return false;
    }

    event.paths.iter().any(|path| path_matches(path, targets))
}

fn is_relevant_event_kind(kind: &EventKind) -> bool {
    matches!(
        kind,
        EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) | EventKind::Any
    )
}

fn path_matches(path: &Path, targets: &[WatchTarget]) -> bool {
    // Exclusions win: a path inside an excluded artifact tree never revalidates,
    // even though it sits under a watched descendant root.
    if targets
        .iter()
        .any(|target| matches!(target, WatchTarget::ExcludedDir(name) if path_has_component(path, name)))
    {
        return false;
    }
    targets.iter().any(|target| match target {
        WatchTarget::Exact(watched) => paths_equivalent(path, watched),
        WatchTarget::Descendant(root) | WatchTarget::OptionalDescendant(root) => {
            path_is_under(path, root)
        }
        WatchTarget::ExcludedDir(_) => false,
    })
}

/// True if any path component is exactly `name` (e.g. a `runtime` directory
/// anywhere in the path).
fn path_has_component(path: &Path, name: &str) -> bool {
    path.components().any(|component| component.as_os_str() == name)
}

fn paths_equivalent(candidate: &Path, watched: &Path) -> bool {
    match (normalize_path(candidate), normalize_path(watched)) {
        (Some(candidate), Some(watched)) => return candidate == watched,
        (Some(candidate), None) if candidate == watched => return true,
        (None, Some(watched)) if candidate == watched => return true,
        (None, None) => {}
        _ => {}
    }

    let candidate_file_name = candidate.file_name();
    let watched_file_name = watched.file_name();

    candidate_file_name.is_some()
        && candidate_file_name == watched_file_name
        && candidate.components().last() == watched.components().last()
}

fn path_is_under(candidate: &Path, root: &Path) -> bool {
    match (normalize_path(candidate), normalize_path(root)) {
        (Some(candidate), Some(root)) => candidate.starts_with(root),
        _ => candidate.starts_with(root),
    }
}

#[derive(Debug, Clone)]
struct ValidationWatchPlan {
    targets: Vec<WatchTarget>,
    roots: Vec<WatchRoot>,
    state_machine_label: String,
}

#[derive(Debug, Clone, Eq, PartialEq)]
enum WatchTarget {
    Exact(PathBuf),
    Descendant(PathBuf),
    OptionalDescendant(PathBuf),
    /// Ignore any event whose path passes through a directory with this name,
    /// at any depth (e.g. a `runtime/` artifact tree the tools write into —
    /// including the per-rhei `runtime/` trees nested under workspace rheis).
    ExcludedDir(&'static str),
}

#[derive(Debug, Clone, Eq, PartialEq)]
struct WatchRoot {
    path: PathBuf,
    mode: RecursiveMode,
}

fn validation_watch_plan(input: &Path, state_machine: Option<&Path>) -> ValidationWatchPlan {
    let state_machine_path = state_machine.map(Path::to_path_buf).or_else(|| {
        let candidate = watch_auto_state_machine_path(input);
        if candidate.is_file() { Some(candidate) } else { None }
    });
    let state_machine_label = state_machine_label(state_machine_path.as_deref());

    let mut targets = plan_watch_targets(input);
    let watched_state_machine_path = if let Some(path) = state_machine {
        path.to_path_buf()
    } else {
        watch_auto_state_machine_path(input)
    };
    targets.push(WatchTarget::Exact(canonical_watch_path(&watched_state_machine_path)));
    targets.push(WatchTarget::OptionalDescendant(canonical_watch_path(
        &rhei_validator::prompt_templates_dir(&watched_state_machine_path),
    )));

    let mut roots = Vec::new();
    for target in &targets {
        add_watch_root_for_target(&mut roots, target);
    }

    ValidationWatchPlan { targets, roots, state_machine_label }
}

fn plan_watch_targets(input: &Path) -> Vec<WatchTarget> {
    if let Some(project_root) = workspace::panta_project_dir(input) {
        return panta_watch_targets(&project_root);
    }

    if let Some(workspace_root) = workspace::workspace_dir(input) {
        return workspace_watch_targets(&workspace_root);
    }

    if input.is_dir() {
        workspace_watch_targets(input)
    } else {
        vec![WatchTarget::Exact(canonical_watch_path(input))]
    }
}

fn panta_watch_targets(project_root: &Path) -> Vec<WatchTarget> {
    vec![
        // The project directory holds the manifest, every rhei, and the synthetic
        // basin, so one descendant watch covers them all. §AR-rhei-panta.1
        WatchTarget::Descendant(canonical_watch_path(project_root)),
        // Exclude every `runtime/` artifact tree (the project's and the per-rhei
        // ones nested under workspace rheis) at any depth, so a re-render that
        // writes there never re-triggers itself. §AR-rhei-panta.5
        WatchTarget::ExcludedDir("runtime"),
    ]
}

fn workspace_watch_targets(workspace_root: &Path) -> Vec<WatchTarget> {
    vec![
        WatchTarget::Exact(canonical_watch_path(&workspace_root.join("index.rhei.md"))),
        WatchTarget::Descendant(canonical_watch_path(&workspace_root.join("tasks"))),
    ]
}

fn watch_auto_state_machine_path(input: &Path) -> PathBuf {
    if let Some(project_root) = workspace::panta_project_dir(input) {
        project_root.join("states.yaml")
    } else if let Some(workspace_root) = workspace::workspace_dir(input) {
        workspace_root.join("states.yaml")
    } else if input.is_dir() {
        input.join("states.yaml")
    } else {
        input.parent().unwrap_or_else(|| Path::new(".")).join("states.yaml")
    }
}

#[cfg(test)]
fn canonical_watched_paths(input: &Path, state_machine: &Path) -> Vec<WatchTarget> {
    let mut targets = plan_watch_targets(input);
    targets.push(WatchTarget::Exact(canonical_watch_path(state_machine)));
    targets.push(WatchTarget::OptionalDescendant(canonical_watch_path(
        &rhei_validator::prompt_templates_dir(state_machine),
    )));
    targets
}

fn add_watch_root_for_target(roots: &mut Vec<WatchRoot>, target: &WatchTarget) {
    let (path, mode) = match target {
        // An exclusion only filters events; it is not itself a watch root.
        WatchTarget::ExcludedDir(_) => return,
        WatchTarget::Exact(path) => {
            let root = path.parent().unwrap_or_else(|| Path::new("."));
            (canonical_watch_path(root), RecursiveMode::NonRecursive)
        }
        WatchTarget::Descendant(path) => {
            if path.is_dir() {
                (canonical_watch_path(path), RecursiveMode::Recursive)
            } else {
                let root = path.parent().unwrap_or_else(|| Path::new("."));
                (canonical_watch_path(root), RecursiveMode::Recursive)
            }
        }
        WatchTarget::OptionalDescendant(path) => {
            if path.is_dir() {
                (canonical_watch_path(path), RecursiveMode::Recursive)
            } else {
                let root = path.parent().unwrap_or_else(|| Path::new("."));
                (canonical_watch_path(root), RecursiveMode::NonRecursive)
            }
        }
    };

    // A recursive watch already covers everything beneath it, so a non-recursive
    // root on the same path or a descendant is redundant (e.g. the Panta project
    // dir watched recursively also covers its `index.panta.md` and `states.yaml`).
    if mode == RecursiveMode::NonRecursive
        && roots
            .iter()
            .any(|existing| existing.mode == RecursiveMode::Recursive && path.starts_with(&existing.path))
    {
        return;
    }
    // Conversely, a new recursive root supersedes any non-recursive roots beneath it.
    if mode == RecursiveMode::Recursive {
        roots.retain(|existing| {
            !(existing.mode == RecursiveMode::NonRecursive && existing.path.starts_with(&path))
        });
    }

    let root = WatchRoot { path, mode };
    if !roots.iter().any(|existing| existing == &root) {
        roots.push(root);
    }
}

fn canonical_watch_path(path: &Path) -> PathBuf {
    if let Some(normalized) = normalize_path(path) {
        return normalized;
    }

    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")).join(path)
    };

    let Some(parent) = absolute.parent() else {
        return absolute;
    };
    let Some(normalized_parent) = normalize_path(parent) else {
        return absolute;
    };

    absolute
        .file_name()
        .map(|name| normalized_parent.join(name))
        .unwrap_or(normalized_parent)
}

fn normalize_path(path: &Path) -> Option<PathBuf> {
    path.canonicalize().ok()
}