gid-core 0.3.2

Graph-Indexed Development core library — graph-based project management and code analysis for AI agents
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
//! V2 Ritual Executor — Bridges the pure state machine to real IO.
//!
//! Takes `RitualAction`s produced by `transition()` and executes them,
//! producing `RitualEvent`s that feed back into the state machine.
//!
//! Responsibilities:
//! - DetectProject → filesystem scan → ProjectDetected
//! - RunPlanning → read DESIGN.md + LLM call → PlanDecided
//! - RunSkill → load skill prompt + LLM → SkillCompleted/SkillFailed
//! - RunShell → tokio::process::Command → ShellCompleted/ShellFailed
//! - Notify, SaveState, UpdateGraph, Cleanup → fire-and-forget (no event)

use std::path::PathBuf;
use std::sync::Arc;
use anyhow::Result;
use tracing::{info, warn, error};

use super::composer::ProjectState as ComposerProjectState;
use super::llm::{LlmClient, ToolDefinition};
use super::scope::default_scope_for_phase;
use super::state_machine::{
    RitualAction, RitualEvent, RitualState, ImplementStrategy,
    ProjectState as V2ProjectState,
};
use crate::graph::{Graph, NodeStatus};
use crate::harness::assemble_task_context;

/// Review depth tier for design/requirements/task review phases.
/// Scales with triage_size — smaller tasks get lighter review.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReviewDepth {
    /// 10 core checks: #1, #2, #5, #6, #7, #8, #11, #13, #21, #27
    Light,
    /// All 28 checks
    Full,
}

/// Configuration for review skill execution.
#[derive(Debug, Clone)]
pub struct ReviewConfig {
    /// LLM model to use for review
    pub model: String,
    /// Maximum iterations for the review sub-agent
    pub max_iterations: usize,
    /// Which checks to run
    pub depth: ReviewDepth,
}

/// Callback for sending notifications (fire-and-forget).
pub type NotifyFn = Arc<dyn Fn(String) + Send + Sync>;

/// Build the triage prompt for a given task and project context.
/// Single source of truth — used by both gid-core and external consumers (e.g., RustClaw).
pub fn build_triage_prompt(task: &str, project_ctx: &str) -> String {
    format!(
        r#"You are a triage agent. Assess this development task quickly.

{project_ctx}

Task: "{task}"

Respond with ONLY a JSON object (no markdown, no explanation):
{{
  "clarity": "clear" or "ambiguous",
  "clarify_questions": ["question1", ...] (only if ambiguous, otherwise empty array),
  "size": "small", "medium", or "large",
  "skip_design": true/false,
  "skip_graph": true/false
}}

Guidelines:
- "small": bug fix, add a simple command, change a config value, rename something
- "medium": add a feature that touches 2-3 files, refactor a module
- "large": new subsystem, architectural change, multi-file feature
- skip_design=true if has_design=true (design already exists — don't redo it) OR if the task is small enough that a design adds no value
- skip_graph=true ONLY if the task modifies existing code without adding new modules, files, or components
- skip_graph=false if the task adds ANY new files, modules, subsystems, or architectural components — even if a graph already exists, it needs to be UPDATED with new nodes
- "ambiguous" if the task description is vague, could mean multiple things, or lacks critical info
- Short ≠ simple. "fix the bug" is ambiguous. "fix the auth retry loop in llm.rs" is clear and small."#
    )
}

/// V2 executor configuration.
pub struct V2ExecutorConfig {
    /// Project root directory.
    pub project_root: PathBuf,
    /// LLM client for skill execution and planning.
    pub llm_client: Option<Arc<dyn LlmClient>>,
    /// Notification callback (e.g., send Telegram message).
    pub notify: Option<NotifyFn>,
    /// Model to use for skill phases.
    pub skill_model: String,
    /// Model to use for planning (cheaper).
    pub planning_model: String,
}

impl Default for V2ExecutorConfig {
    fn default() -> Self {
        Self {
            project_root: PathBuf::from("."),
            llm_client: None,
            notify: None,
            skill_model: "opus".to_string(),
            planning_model: "sonnet".to_string(),
        }
    }
}

/// The V2 executor — executes actions, returns events.
pub struct V2Executor {
    config: V2ExecutorConfig,
}

impl V2Executor {
    pub fn new(config: V2ExecutorConfig) -> Self {
        Self { config }
    }

    /// Execute an action. Returns Some(event) for event-producing actions, None for fire-and-forget.
    pub async fn execute(&self, action: &RitualAction, state: &RitualState) -> Option<RitualEvent> {
        match action {
            RitualAction::DetectProject => Some(self.detect_project().await),
            RitualAction::RunTriage { task } => Some(self.run_triage(task, state).await),
            RitualAction::RunSkill { name, context } => {
                Some(self.run_skill(name, context, state).await)
            }
            RitualAction::RunShell { command } => Some(self.run_shell(command).await),
            RitualAction::RunPlanning => Some(self.run_planning(state).await),
            RitualAction::RunHarness { tasks } => Some(self.run_harness(tasks, state).await),
            RitualAction::Notify { message } => {
                self.notify(message);
                None
            }
            RitualAction::SaveState => {
                self.save_state(state);
                None
            }
            RitualAction::UpdateGraph { description } => {
                self.update_graph(description);
                None
            }
            RitualAction::ApplyReview { approved } => {
                // Fire-and-forget: apply review findings via apply-review skill
                // In gid-core context, this is a no-op (RustClaw executor handles it)
                tracing::info!("ApplyReview (approved: {})", approved);
                None
            }
            RitualAction::Cleanup => {
                self.cleanup();
                None
            }
        }
    }

    /// Execute all actions from a transition, returning the event-producing action's event.
    /// Fire-and-forget actions are executed first, then the event-producing action.
    pub async fn execute_actions(
        &self,
        actions: &[RitualAction],
        state: &RitualState,
    ) -> Option<RitualEvent> {
        let mut event = None;

        for action in actions {
            if action.is_fire_and_forget() {
                // Execute fire-and-forget immediately
                let _ = self.execute(action, state).await;
            } else {
                // Event-producing: execute and capture the event
                event = self.execute(action, state).await;
            }
        }

        event
    }

    // ═══════════════════════════════════════════════════════════════════════
    // Event-producing actions
    // ═══════════════════════════════════════════════════════════════════════

    async fn detect_project(&self) -> RitualEvent {
        info!(project_root = %self.config.project_root.display(), "Detecting project state");

        let cs = ComposerProjectState::detect(&self.config.project_root);

        // Read verify command from .gid/config.yml if it exists
        let verify_command = self.read_verify_command();

        let ps = V2ProjectState {
            has_requirements: cs.has_requirements,
            has_design: cs.has_design,
            has_graph: cs.has_graph,
            has_source: cs.has_source_code,
            has_tests: cs.has_tests,
            language: cs.language.map(|l| format!("{:?}", l)),
            source_file_count: cs.source_file_count,
            verify_command,
        };

        info!(
            has_design = ps.has_design,
            has_graph = ps.has_graph,
            has_source = ps.has_source,
            source_files = ps.source_file_count,
            "Project state detected"
        );

        RitualEvent::ProjectDetected(ps)
    }

    async fn run_triage(&self, task: &str, state: &RitualState) -> RitualEvent {
        info!(task = task, "Running triage (haiku)");

        let llm = match &self.config.llm_client {
            Some(c) => c.clone(),
            None => {
                warn!("No LLM client — defaulting to full flow");
                return RitualEvent::TriageCompleted(super::state_machine::TriageResult {
                    clarity: "clear".into(),
                    clarify_questions: vec![],
                    size: "large".into(),
                    skip_design: false,
                    skip_graph: false,
                });
            }
        };

        // Build project context summary for triage
        let project_ctx = if let Some(ps) = &state.project {
            format!(
                "Project: lang={}, has_design={}, has_graph={}, source_files={}, has_tests={}",
                ps.language.as_deref().unwrap_or("unknown"),
                ps.has_design, ps.has_graph,
                ps.source_file_count, ps.has_tests
            )
        } else {
            "Project: unknown state".into()
        };

        let prompt = build_triage_prompt(task, &project_ctx);

        match llm.chat(&prompt, "haiku").await {
            Ok(response) => {
                // Parse JSON from response
                let json_str = extract_json(&response);
                match serde_json::from_str::<super::state_machine::TriageResult>(json_str) {
                    Ok(mut result) => {
                        // Deterministic override: if design already exists, skip design phase
                        // regardless of what Haiku says. LLM triage is advisory, not authoritative
                        // for facts we can verify deterministically.
                        if let Some(ps) = &state.project {
                            if ps.has_design && !result.skip_design {
                                info!("Override: skip_design=true (design already exists)");
                                result.skip_design = true;
                            }
                        }

                        info!(
                            clarity = result.clarity,
                            size = result.size,
                            skip_design = result.skip_design,
                            skip_graph = result.skip_graph,
                            "Triage complete"
                        );
                        RitualEvent::TriageCompleted(result)
                    }
                    Err(e) => {
                        warn!("Failed to parse triage JSON: {}. Defaulting to full flow.", e);
                        RitualEvent::TriageCompleted(super::state_machine::TriageResult {
                            clarity: "clear".into(),
                            clarify_questions: vec![],
                            size: "large".into(),
                            skip_design: false,
                            skip_graph: false,
                        })
                    }
                }
            }
            Err(e) => {
                warn!("Triage LLM call failed: {}. Defaulting to full flow.", e);
                RitualEvent::TriageCompleted(super::state_machine::TriageResult {
                    clarity: "clear".into(),
                    clarify_questions: vec![],
                    size: "large".into(),
                    skip_design: false,
                    skip_graph: false,
                })
            }
        }
    }

    async fn run_skill(&self, name: &str, context: &str, state: &RitualState) -> RitualEvent {
        info!(skill = name, "Running skill phase");

        let llm = match &self.config.llm_client {
            Some(c) => c.clone(),
            None => {
                error!("No LLM client configured for skill execution");
                return RitualEvent::SkillFailed {
                    phase: name.to_string(),
                    error: "No LLM client configured".to_string(),
                };
            }
        };

        // Load skill prompt
        let base_prompt = match self.load_skill_prompt(name) {
            Ok(p) => p,
            Err(e) => {
                return RitualEvent::SkillFailed {
                    phase: name.to_string(),
                    error: format!("Failed to load skill prompt: {}", e),
                };
            }
        };

        // Enrich context for implement phases
        let effective_context = if name == "implement" {
            self.enrich_implement_context(context, state)
        } else {
            context.to_string()
        };

        // Compose full prompt with context injection (§4)
        let full_prompt = if effective_context.is_empty() {
            base_prompt
        } else {
            format!("## USER TASK\n{}\n\n## INSTRUCTIONS\n{}", effective_context, base_prompt)
        };

        // Select model and adjust iterations for review phases based on triage size
        let review_config = if name.starts_with("review") {
            Some(self.review_config_for_triage_size(state))
        } else {
            None
        };
        let model = review_config.as_ref().map(|c| c.model.clone()).unwrap_or_else(|| self.config.skill_model.clone());
        let max_iterations = review_config.as_ref().map(|c| c.max_iterations).unwrap_or(100);

        // Inject review depth hint into prompt for review phases
        let full_prompt = if let Some(ref config) = review_config {
            let depth_label = match config.depth {
                ReviewDepth::Light => "quick",
                ReviewDepth::Full => "full",
            };
            if config.depth == ReviewDepth::Light {
                format!(
                    "[REVIEW_DEPTH: {}]\n\n## REVIEW SCOPE: LIGHT\nRun ONLY checks #1, #2, #5, #6, #7, #8, #11, #13, #21, #27.\nSkip all other checks. Write findings to file.\n\n{}",
                    depth_label, full_prompt
                )
            } else {
                format!("[REVIEW_DEPTH: {}]\n\n{}", depth_label, full_prompt)
            }
        } else {
            full_prompt
        };

        // Get tool scope for this phase
        let scope = default_scope_for_phase(name);
        let tools = self.scope_to_tool_definitions(&scope);

        match llm
            .run_skill(
                &full_prompt,
                tools,
                &model,
                &self.config.project_root,
                max_iterations,
            )
            .await
        {
            Ok(result) => {
                info!(skill = name, "Skill completed successfully");
                RitualEvent::SkillCompleted {
                    phase: name.to_string(),
                    artifacts: result.artifacts_created.iter().map(|p| p.to_string_lossy().to_string()).collect(),
                }
            }
            Err(e) => {
                warn!(skill = name, error = %e, "Skill failed");
                RitualEvent::SkillFailed {
                    phase: name.to_string(),
                    error: e.to_string(),
                }
            }
        }
    }

    async fn run_shell(&self, command: &str) -> RitualEvent {
        info!(command = command, "Running shell command");

        match tokio::process::Command::new("sh")
            .arg("-c")
            .arg(command)
            .current_dir(&self.config.project_root)
            .output()
            .await
        {
            Ok(output) => {
                let stdout = String::from_utf8_lossy(&output.stdout).to_string();
                let stderr = String::from_utf8_lossy(&output.stderr).to_string();
                let exit_code = output.status.code().unwrap_or(-1);

                if output.status.success() {
                    info!(exit_code, "Shell command completed successfully");
                    RitualEvent::ShellCompleted {
                        stdout: format!("{}{}", stdout, stderr),
                        exit_code,
                    }
                } else {
                    warn!(exit_code, "Shell command failed");
                    RitualEvent::ShellFailed {
                        stderr: format!("{}{}", stderr, stdout),
                        exit_code,
                    }
                }
            }
            Err(e) => {
                error!(error = %e, "Failed to execute shell command");
                RitualEvent::ShellFailed {
                    stderr: e.to_string(),
                    exit_code: -1,
                }
            }
        }
    }

    async fn run_planning(&self, state: &RitualState) -> RitualEvent {
        info!("Running planning phase");

        let llm = match &self.config.llm_client {
            Some(c) => c.clone(),
            None => {
                warn!("No LLM client for planning, defaulting to SingleLlm");
                return RitualEvent::PlanDecided(ImplementStrategy::SingleLlm);
            }
        };

        // Read DESIGN.md
        let design_path = self.config.project_root.join("DESIGN.md");
        let design_content = match std::fs::read_to_string(&design_path) {
            Ok(c) => c,
            Err(_) => {
                info!("No DESIGN.md found, defaulting to SingleLlm");
                return RitualEvent::PlanDecided(ImplementStrategy::SingleLlm);
            }
        };

        // Truncate if too long (save tokens)
        let design_truncated = if design_content.len() > 15000 {
            format!("{}...\n[TRUNCATED — {} bytes total]", Self::safe_truncate(&design_content, 15000), design_content.len())
        } else {
            design_content
        };

        let prompt = format!(
            r#"You are a project planning assistant. Based on the DESIGN.md below and the task description, decide the implementation strategy.

## TASK
{}

## DESIGN.md
{}

## Instructions
Analyze the scope:
1. How many files need to change?
2. Are the changes independent enough for parallel work?
3. Is this a small fix or a large feature?

Output ONLY a JSON object (no markdown, no explanation):
- Small/focused change: {{"strategy": "single_llm"}}
- Large multi-file change with independent parts: {{"strategy": "multi_agent", "tasks": ["task description 1", "task description 2"]}}

Default to "single_llm" unless you're confident the work is large AND parallelizable."#,
            state.task,
            design_truncated
        );

        match llm
            .run_skill(
                &prompt,
                vec![], // No tools needed for planning
                &self.config.planning_model,
                &self.config.project_root,
                20,
            )
            .await
        {
            Ok(result) => self.parse_planning_result(&result.output),
            Err(e) => {
                warn!(error = %e, "Planning LLM call failed, defaulting to SingleLlm");
                RitualEvent::PlanDecided(ImplementStrategy::SingleLlm)
            }
        }
    }

    async fn run_harness(&self, tasks: &[String], state: &RitualState) -> RitualEvent {
        // Harness execution is complex — for now, treat as a single skill call
        // with all tasks concatenated. Real harness support comes later.
        info!(task_count = tasks.len(), "Running harness (simplified)");

        let context = tasks
            .iter()
            .enumerate()
            .map(|(i, t)| format!("{}. {}", i + 1, t))
            .collect::<Vec<_>>()
            .join("\n");

        self.run_skill("implement", &context, state).await
    }

    // ═══════════════════════════════════════════════════════════════════════
    // Fire-and-forget actions
    // ═══════════════════════════════════════════════════════════════════════

    fn notify(&self, message: &str) {
        if let Some(ref notify_fn) = self.config.notify {
            notify_fn(message.to_string());
        } else {
            info!(message = message, "Ritual notification (no handler)");
        }
    }

    fn save_state(&self, state: &RitualState) {
        let state_path = self.config.project_root.join(".gid").join("ritual-state.json");

        // Ensure .gid/ exists
        if let Some(parent) = state_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        match serde_json::to_string_pretty(state) {
            Ok(json) => {
                if let Err(e) = std::fs::write(&state_path, &json) {
                    warn!(error = %e, "Failed to save ritual state");
                }
            }
            Err(e) => {
                warn!(error = %e, "Failed to serialize ritual state");
            }
        }
    }

    fn update_graph(&self, description: &str) {
        use crate::graph::{Graph, NodeStatus};

        let graph_path = self.config.project_root.join(".gid").join("graph.yml");
        if !graph_path.exists() {
            info!("No graph.yml found, skipping graph update");
            return;
        }

        // Load graph
        let content = match std::fs::read_to_string(&graph_path) {
            Ok(c) => c,
            Err(e) => {
                warn!("Failed to read graph.yml: {}", e);
                return;
            }
        };
        let mut graph: Graph = match serde_yaml::from_str(&content) {
            Ok(g) => g,
            Err(e) => {
                warn!("Failed to parse graph.yml: {}", e);
                return;
            }
        };

        // Find matching node by fuzzy description match
        // Strategy: check if any node's title or description contains the task text (or vice versa)
        let desc_lower = description.to_lowercase();
        let matched_id = graph.nodes.iter()
            .filter(|n| {
                matches!(n.status, NodeStatus::Todo | NodeStatus::InProgress)
            })
            .find(|n| {
                let title_lower = n.title.to_lowercase();
                let node_desc_lower = n.description.as_deref().unwrap_or("").to_lowercase();
                // Match if task description contains node title or vice versa
                desc_lower.contains(&title_lower)
                    || title_lower.contains(&desc_lower)
                    || (!node_desc_lower.is_empty() && (
                        desc_lower.contains(&node_desc_lower)
                        || node_desc_lower.contains(&desc_lower)
                    ))
            })
            .map(|n| n.id.clone());

        if let Some(id) = matched_id {
            if graph.mark_task_done(&id) {
                // Save back
                match serde_yaml::to_string(&graph) {
                    Ok(yaml) => {
                        if let Err(e) = std::fs::write(&graph_path, &yaml) {
                            warn!("Failed to write graph.yml: {}", e);
                        } else {
                            info!(node_id = %id, "Marked graph node as done");
                        }
                    }
                    Err(e) => warn!("Failed to serialize graph: {}", e),
                }
            }
        } else {
            info!(description = description, "No matching graph node found for task");
        }
    }

    fn cleanup(&self) {
        info!("Ritual cleanup");
        // Remove temporary files, ritual-state.json on success, etc.
    }

    // ═══════════════════════════════════════════════════════════════════════
    // Helpers
    // ═══════════════════════════════════════════════════════════════════════

    /// Select model and iteration count for review phase based on triage size (§9).
    fn review_config_for_triage_size(&self, state: &RitualState) -> ReviewConfig {
        let size = state.triage_size.as_deref().unwrap_or("medium");
        
        match size {
            "small" => ReviewConfig {
                model: "sonnet".to_string(),
                max_iterations: 30,
                depth: ReviewDepth::Light,
            },
            "medium" => ReviewConfig {
                model: self.config.skill_model.clone(),
                max_iterations: 50,
                depth: ReviewDepth::Light,
            },
            "large" => ReviewConfig {
                model: self.config.skill_model.clone(),
                max_iterations: 100,
                depth: ReviewDepth::Full,
            },
            _ => ReviewConfig {
                model: self.config.skill_model.clone(),
                max_iterations: 50,
                depth: ReviewDepth::Light,
            },
        }
    }

    // ═══════════════════════════════════════════════════════════════════════
    // Private helpers (existing)
    // ═══════════════════════════════════════════════════════════════════════

    fn load_skill_prompt(&self, skill_name: &str) -> Result<String> {
        // Priority: .gid/skills/{name}.md → built-in prompts

        let gid_skill = self
            .config
            .project_root
            .join(".gid")
            .join("skills")
            .join(format!("{}.md", skill_name));

        if gid_skill.exists() {
            return Ok(std::fs::read_to_string(&gid_skill)?);
        }

        // Check home-relative skills directories (RustClaw, etc.)
        if let Some(home) = std::env::var_os("HOME") {
            let home = PathBuf::from(home);
            let rustclaw_skill = home
                .join("rustclaw")
                .join("skills")
                .join(skill_name)
                .join("SKILL.md");

            if rustclaw_skill.exists() {
                return Ok(std::fs::read_to_string(&rustclaw_skill)?);
            }
        }

        // Built-in fallback prompts
        match skill_name {
            "draft-design" => Ok(include_str!("prompts/draft_design.txt").to_string()),
            "update-design" => Ok(
                "Read the existing DESIGN.md and the user's task. Update the design document \
                 to incorporate the new requirements. Write the updated DESIGN.md."
                    .to_string(),
            ),
            "generate-graph" | "design-to-graph" => Ok(
                "Read DESIGN.md from the project root. Generate a GID graph in YAML format \
                 and write it to .gid/graph.yml.\n\n\
                 The graph has multiple node types:\n\
                 ```yaml\n\
                 nodes:\n\
                   # Feature/module nodes (semantic — the architecture)\n\
                   - id: feat-dashboard\n\
                     title: \"Dashboard Module\"\n\
                     type: feature\n\
                     status: todo\n\
                     tags: [module]\n\
                     description: \"HTTP dashboard server\"\n\
                   # File nodes (what gets changed)\n\
                   - id: file-dashboard-rs\n\
                     title: \"src/dashboard.rs\"\n\
                     type: file\n\
                     status: todo\n\
                   # Task nodes (concrete work items)\n\
                   - id: task-add-health-endpoint\n\
                     title: \"Add health check endpoint\"\n\
                     type: task\n\
                     status: todo\n\
                     tags: [implementation]\n\
                     description: \"Add health check endpoint returning uptime and stats\"\n\
                 edges:\n\
                   - from: task-add-health-endpoint\n\
                     to: feat-dashboard\n\
                     relation: implements\n\
                   - from: feat-dashboard\n\
                     to: file-dashboard-rs\n\
                     relation: contains\n\
                   - from: task-a\n\
                     to: task-b\n\
                     relation: depends_on\n\
                 ```\n\n\
                 Node types: feature, component, file, task, layer, doc\n\
                 Edge relations: depends_on, implements, modifies, contains, tests, related_to\n\n\
                 Rules:\n\
                 - Create feature/component nodes for modules and architectural units\n\
                 - Create file nodes for files being created/modified\n\
                 - Create task nodes for concrete work items (status: todo)\n\
                 - Link tasks to features they implement (relation: implements)\n\
                 - Link features to files they contain (relation: contains)\n\
                 - Link tasks to tasks they depend on (relation: depends_on)\n\
                 - Include metadata (design_ref, goals, file_path) on task nodes\n\
                 Use the Read tool to read DESIGN.md, then Write tool to create .gid/graph.yml."
                    .to_string(),
            ),
            "update-graph" => Ok(
                "Read the existing .gid/graph.yml and DESIGN.md. Update the graph to reflect \
                 any new tasks or changes from the design.\n\n\
                 CRITICAL RULES:\n\
                 - Read the existing graph FIRST\n\
                 - PRESERVE all existing nodes and edges — do NOT delete or modify them\n\
                 - Only ADD new nodes (task, feature, component, file) and edges for the new work\n\
                 - New task nodes should have status: todo\n\
                 - Link new tasks to features they implement (relation: implements)\n\
                 - Link tasks to tasks they depend on (relation: depends_on)\n\n\
                 Node types: feature, component, file, task, layer, doc\n\
                 Edge relations: depends_on, implements, modifies, contains, tests, related_to\n\n\
                 Use Read to load existing graph and DESIGN.md, then Write to update .gid/graph.yml."
                    .to_string(),
            ),
            "implement" => Ok(
                "Implement the described changes following the graph-driven layer approach:\n\n\
                 PROCESS:\n\
                 1. Read .gid/graph.yml to find all task nodes and their layer assignments\n\
                 2. Process layers in order (Layer 0 first, then Layer 1, etc.)\n\
                 3. Within each layer, implement each task node sequentially:\n\
                    a. Read the design doc section relevant to this task\n\
                    b. Read any dependency modules (from prior layers) to understand their public API\n\
                    c. Write the code for this task\n\
                    d. Update the task node's status to 'done' in graph.yml\n\
                 4. After completing ALL tasks in a layer, run the project's build/check command\n\
                    to verify compilation before proceeding to the next layer\n\
                 5. If build fails, fix the issues within the current layer before moving on\n\n\
                 RULES:\n\
                 - Follow existing patterns and conventions in the codebase\n\
                 - Only implement tasks that are status: todo — skip tasks already marked done\n\
                 - Layer order is mandatory: never implement a task before its dependencies are done\n\
                 - Update graph.yml status incrementally, not all at once at the end\n\
                 - FINAL STEP: After ALL layers are done, run `cargo check` (Rust), `npm run build` (TS),\n\
                   or `python -m py_compile` (Python) as a final compilation gate. If it fails, fix before exiting.\n\
                   Do NOT mark the implementation complete until the final check passes."
                    .to_string(),
            ),
            _ => anyhow::bail!("Unknown skill: {}", skill_name),
        }
    }

    fn read_verify_command(&self) -> Option<String> {
        let config_path = self.config.project_root.join(".gid").join("config.yml");
        if !config_path.exists() {
            // Default based on project type
            let composer_state = ComposerProjectState::detect(&self.config.project_root);
            return match composer_state.language {
                Some(super::composer::ProjectLanguage::Rust) => {
                    Some("cargo build 2>&1 && cargo test 2>&1".to_string())
                }
                Some(super::composer::ProjectLanguage::TypeScript) => {
                    Some("npm run build 2>&1 && npm test 2>&1".to_string())
                }
                Some(super::composer::ProjectLanguage::Python) => {
                    Some("python -m pytest 2>&1".to_string())
                }
                _ => None,
            };
        }

        // Parse .gid/config.yml for verify_command
        match std::fs::read_to_string(&config_path) {
            Ok(content) => {
                // Simple YAML parsing: look for verify_command: ...
                for line in content.lines() {
                    let trimmed = line.trim();
                    if let Some(cmd) = trimmed.strip_prefix("verify_command:") {
                        let cmd = cmd.trim().trim_matches('"').trim_matches('\'');
                        if !cmd.is_empty() {
                            return Some(cmd.to_string());
                        }
                    }
                }
                None
            }
            Err(_) => None,
        }
    }

    fn parse_planning_result(&self, output: &str) -> RitualEvent {
        // Try to extract JSON from the output
        let json_str = extract_json(output);

        match serde_json::from_str::<serde_json::Value>(json_str) {
            Ok(v) => {
                let strategy = v["strategy"].as_str().unwrap_or("single_llm");
                match strategy {
                    "multi_agent" => {
                        let tasks: Vec<String> = v["tasks"]
                            .as_array()
                            .map(|arr| {
                                arr.iter()
                                    .filter_map(|v| v.as_str().map(String::from))
                                    .collect()
                            })
                            .unwrap_or_default();

                        if tasks.is_empty() {
                            RitualEvent::PlanDecided(ImplementStrategy::SingleLlm)
                        } else {
                            RitualEvent::PlanDecided(ImplementStrategy::MultiAgent { tasks })
                        }
                    }
                    _ => RitualEvent::PlanDecided(ImplementStrategy::SingleLlm),
                }
            }
            Err(e) => {
                warn!(error = %e, "Failed to parse planning JSON, defaulting to SingleLlm");
                RitualEvent::PlanDecided(ImplementStrategy::SingleLlm)
            }
        }
    }

    /// Safe UTF-8 truncation — finds nearest char boundary.
    fn safe_truncate(s: &str, max_bytes: usize) -> &str {
        if s.len() <= max_bytes {
            return s;
        }
        let mut end = max_bytes;
        while end > 0 && !s.is_char_boundary(end) {
            end -= 1;
        }
        &s[..end]
    }

    /// Resolve the .gid/ root directory from state or config.
    fn resolve_gid_root(&self, state: &RitualState) -> PathBuf {
        if let Some(ref target_root) = state.target_root {
            PathBuf::from(target_root).join(".gid")
        } else {
            self.config.project_root.join(".gid")
        }
    }

    /// Load graph and assemble context for all pending task nodes.
    /// Returns None if graph doesn't exist or has no task nodes.
    fn build_graph_context(&self, state: &RitualState) -> Option<String> {
        let gid_root = self.resolve_gid_root(state);
        let graph_path = gid_root.join("graph.yml");

        let content = std::fs::read_to_string(&graph_path)
            .map_err(|e| tracing::debug!("No graph.yml found: {}", e))
            .ok()?;
        let graph: Graph = serde_yaml::from_str(&content)
            .map_err(|e| tracing::warn!("Failed to parse graph: {}", e))
            .ok()?;

        let task_ids: Vec<&str> = graph
            .nodes
            .iter()
            .filter(|n| n.node_type.as_deref() == Some("task"))
            .filter(|n| !matches!(n.status, NodeStatus::Done))
            .map(|n| n.id.as_str())
            .collect();

        if task_ids.is_empty() {
            return None;
        }

        let contexts: Vec<String> = task_ids
            .iter()
            .filter_map(|id| {
                assemble_task_context(&graph, id, &gid_root)
                    .map_err(|e| tracing::warn!("Failed to assemble context for {}: {}", id, e))
                    .ok()
            })
            .map(|ctx| ctx.render_prompt())
            .collect();

        if contexts.is_empty() {
            None
        } else {
            Some(contexts.join("\n\n---\n\n"))
        }
    }

    /// Enrich the task context for implementation phases.
    /// Falls back gracefully to raw_context if graph is unavailable.
    fn enrich_implement_context(&self, raw_context: &str, state: &RitualState) -> String {
        let graph_context = self.build_graph_context(state);

        match graph_context {
            Some(ctx) => format!("{}\n\n## Original Task\n{}", ctx, raw_context),
            None => raw_context.to_string(),
        }
    }

    fn scope_to_tool_definitions(&self, scope: &super::scope::ToolScope) -> Vec<ToolDefinition> {
        scope
            .allowed_tools
            .iter()
            .map(|name| ToolDefinition {
                name: name.clone(),
                description: format!("{} tool", name),
                input_schema: serde_json::json!({"type": "object"}),
            })
            .collect()
    }
}

/// Extract JSON from LLM output (handles markdown code fences).
fn extract_json(output: &str) -> &str {
    // Try to find ```json ... ``` block
    if let Some(start) = output.find("```json") {
        let json_start = start + 7;
        if let Some(end) = output[json_start..].find("```") {
            return output[json_start..json_start + end].trim();
        }
    }
    // Try to find ``` ... ``` block
    if let Some(start) = output.find("```") {
        let json_start = start + 3;
        if let Some(end) = output[json_start..].find("```") {
            return output[json_start..json_start + end].trim();
        }
    }
    // Try to find { ... } directly
    if let Some(start) = output.find('{') {
        if let Some(end) = output.rfind('}') {
            return &output[start..=end];
        }
    }
    output.trim()
}

// ═══════════════════════════════════════════════════════════════════════════════
// V2 Engine Loop — drives the state machine to completion
// ═══════════════════════════════════════════════════════════════════════════════

/// Run the full ritual state machine to completion.
///
/// This is the main entry point: takes a task string, creates the initial state,
/// and runs transition() + executor in a loop until terminal state.
pub async fn run_ritual(
    task: &str,
    executor: &V2Executor,
) -> Result<RitualState> {
    use super::state_machine::transition;

    let mut state = RitualState::new();
    let (new_state, actions) = transition(&state, RitualEvent::Start { task: task.to_string() });
    state = new_state;

    // Execute initial actions
    let mut event = executor.execute_actions(&actions, &state).await;

    let max_iterations = 50; // Safety limit
    let mut iteration = 0;

    while let Some(ev) = event {
        iteration += 1;
        if iteration > max_iterations {
            error!("Ritual exceeded max iterations ({}), escalating", max_iterations);
            let (final_state, final_actions) = transition(
                &state,
                RitualEvent::SkillFailed {
                    phase: "engine".to_string(),
                    error: format!("Max iterations ({}) exceeded", max_iterations),
                },
            );
            state = final_state;
            executor.execute_actions(&final_actions, &state).await;
            break;
        }

        let (new_state, actions) = transition(&state, ev);
        state = new_state;

        if state.phase.is_terminal() || state.phase.is_paused() {
            // Execute remaining fire-and-forget actions (Notify, SaveState)
            executor.execute_actions(&actions, &state).await;
            break;
        }

        event = executor.execute_actions(&actions, &state).await;
    }

    info!(
        phase = ?state.phase,
        iterations = iteration,
        "Ritual completed"
    );

    Ok(state)
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

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

    #[test]
    fn test_extract_json_bare() {
        let input = r#"{"strategy": "single_llm"}"#;
        assert_eq!(extract_json(input), r#"{"strategy": "single_llm"}"#);
    }

    #[test]
    fn test_extract_json_fenced() {
        let input = "Here's the plan:\n```json\n{\"strategy\": \"single_llm\"}\n```\n";
        assert_eq!(extract_json(input), r#"{"strategy": "single_llm"}"#);
    }

    #[test]
    fn test_extract_json_code_block() {
        let input = "```\n{\"strategy\": \"multi_agent\", \"tasks\": [\"a\"]}\n```";
        assert_eq!(
            extract_json(input),
            r#"{"strategy": "multi_agent", "tasks": ["a"]}"#
        );
    }

    #[test]
    fn test_extract_json_with_text() {
        let input = "I think single LLM is best.\n{\"strategy\": \"single_llm\"}\nDone.";
        assert_eq!(extract_json(input), r#"{"strategy": "single_llm"}"#);
    }

    #[test]
    fn test_parse_planning_single() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let event = executor.parse_planning_result(r#"{"strategy": "single_llm"}"#);
        assert!(matches!(event, RitualEvent::PlanDecided(ImplementStrategy::SingleLlm)));
    }

    #[test]
    fn test_parse_planning_multi() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let event = executor.parse_planning_result(
            r#"{"strategy": "multi_agent", "tasks": ["impl auth", "impl dashboard"]}"#,
        );
        match event {
            RitualEvent::PlanDecided(ImplementStrategy::MultiAgent { tasks }) => {
                assert_eq!(tasks.len(), 2);
                assert_eq!(tasks[0], "impl auth");
            }
            _ => panic!("Expected MultiAgent"),
        }
    }

    #[test]
    fn test_parse_planning_invalid_json() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let event = executor.parse_planning_result("this is not json at all");
        assert!(matches!(event, RitualEvent::PlanDecided(ImplementStrategy::SingleLlm)));
    }

    #[test]
    fn test_parse_planning_multi_empty_tasks() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let event = executor.parse_planning_result(r#"{"strategy": "multi_agent", "tasks": []}"#);
        // Empty tasks should fall back to SingleLlm
        assert!(matches!(event, RitualEvent::PlanDecided(ImplementStrategy::SingleLlm)));
    }

    #[test]
    fn test_safe_truncate_ascii() {
        assert_eq!(V2Executor::safe_truncate("hello world", 5), "hello");
        assert_eq!(V2Executor::safe_truncate("hello", 100), "hello");
    }

    #[test]
    fn test_safe_truncate_utf8() {
        let s = "你好世界"; // 4 chars, 12 bytes
        assert_eq!(V2Executor::safe_truncate(s, 6), "你好"); // 6 bytes = 2 chars
        assert_eq!(V2Executor::safe_truncate(s, 7), "你好"); // 7 is mid-char, round down
        assert_eq!(V2Executor::safe_truncate(s, 12), s); // exact fit
        assert_eq!(V2Executor::safe_truncate(s, 100), s); // larger than string
    }

    #[test]
    fn test_render_prompt_full() {
        use crate::harness::types::{TaskContext, TaskInfo};
        let ctx = TaskContext {
            task_info: TaskInfo {
                id: "task-1".into(),
                title: "Add auth".into(),
                description: "Implement JWT auth middleware".into(),
                goals: vec!["GOAL-1".into()],
                verify: None,
                estimated_turns: 15,
                depends_on: vec![],
                design_ref: None,
                satisfies: vec!["GOAL-1".into()],
            },
            goals_text: vec!["GOAL-1: All endpoints require valid JWT".into()],
            design_excerpt: Some("§3.1 Auth uses RS256 tokens...".into()),
            dependency_interfaces: vec![],
            guards: vec!["GUARD-1: No plaintext passwords".into()],
        };
        let rendered = ctx.render_prompt();
        assert!(rendered.contains("## Task: Add auth"));
        assert!(rendered.contains("JWT auth middleware"));
        assert!(rendered.contains("## Design Reference"));
        assert!(rendered.contains("RS256"));
        assert!(rendered.contains("## Requirements"));
        assert!(rendered.contains("GOAL-1"));
        assert!(rendered.contains("## Guards"));
        assert!(rendered.contains("GUARD-1"));
    }

    // ── Review skill name matching ──

    #[test]
    fn test_review_design_triggers_review_config() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let mut state = RitualState::new();
        state.triage_size = Some("large".into());
        let config = executor.review_config_for_triage_size(&state);
        // "review-design" starts with "review" so it should use review config
        let name = "review-design";
        assert!(name.starts_with("review"), "review-design should match review prefix");
        assert_eq!(config.model, "opus");
        assert_eq!(config.max_iterations, 100);
        assert_eq!(config.depth, ReviewDepth::Full);
    }

    #[test]
    fn test_review_requirements_triggers_review_config() {
        let name = "review-requirements";
        assert!(name.starts_with("review"), "review-requirements should match review prefix");
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let mut state = RitualState::new();
        state.triage_size = Some("small".into());
        let config = executor.review_config_for_triage_size(&state);
        assert_eq!(config.model, "sonnet");
        assert_eq!(config.max_iterations, 30);
        assert_eq!(config.depth, ReviewDepth::Light);
    }

    #[test]
    fn test_review_tasks_triggers_review_config() {
        let name = "review-tasks";
        assert!(name.starts_with("review"), "review-tasks should match review prefix");
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let mut state = RitualState::new();
        state.triage_size = Some("medium".into());
        let config = executor.review_config_for_triage_size(&state);
        assert_eq!(config.model, "opus");
        assert_eq!(config.max_iterations, 50);
        assert_eq!(config.depth, ReviewDepth::Light);
    }

    #[test]
    fn test_implement_does_not_trigger_review_config() {
        let name = "implement";
        assert!(!name.starts_with("review"), "implement should NOT match review prefix");
    }

    #[test]
    fn test_review_depth_hint_injected_for_review_phases() {
        // Verify that the review depth hint logic activates for review-* names
        for name in &["review-design", "review-requirements", "review-tasks"] {
            assert!(name.starts_with("review"),
                "'{}' should trigger review depth injection", name);
        }
        // Verify depth mapping
        let state_small = {
            let mut s = RitualState::new();
            s.triage_size = Some("small".into());
            s
        };
        let depth = match state_small.triage_size.as_deref().unwrap_or("medium") {
            "small" => "quick",
            "medium" => "standard",
            "large" => "full",
            _ => "standard",
        };
        assert_eq!(depth, "quick");

        let state_large = {
            let mut s = RitualState::new();
            s.triage_size = Some("large".into());
            s
        };
        let depth = match state_large.triage_size.as_deref().unwrap_or("medium") {
            "small" => "quick",
            "medium" => "standard",
            "large" => "full",
            _ => "standard",
        };
        assert_eq!(depth, "full");
    }

    #[test]
    fn test_review_depth_hint_not_injected_for_non_review_phases() {
        for name in &["implement", "draft-design", "generate-graph", "draft-requirements"] {
            assert!(!name.starts_with("review"),
                "'{}' should NOT trigger review depth injection", name);
        }
    }

    #[test]
    fn test_render_prompt_partial() {
        use crate::harness::types::{TaskContext, TaskInfo};
        let ctx = TaskContext {
            task_info: TaskInfo {
                id: "task-2".into(),
                title: "Fix bug".into(),
                description: String::new(),
                goals: vec![],
                verify: None,
                estimated_turns: 5,
                depends_on: vec![],
                design_ref: None,
                satisfies: vec![],
            },
            goals_text: vec![],
            design_excerpt: None,
            dependency_interfaces: vec![],
            guards: vec![],
        };
        let rendered = ctx.render_prompt();
        assert!(rendered.contains("## Task: Fix bug"));
        assert!(!rendered.contains("## Design Reference"));
        assert!(!rendered.contains("## Requirements"));
        assert!(!rendered.contains("## Guards"));
    }

    // ── T1.2: Enrichment & ReviewConfig unit tests ──

    #[test]
    fn test_enrich_with_graph_context() {
        // Set up a temp dir with .gid/graph.yml containing a task node
        let tmp = tempfile::tempdir().unwrap();
        let gid_dir = tmp.path().join(".gid");
        std::fs::create_dir_all(&gid_dir).unwrap();

        // Create a minimal graph with a task node
        let mut graph = Graph::new();
        let mut task_node = crate::graph::Node::new("task-auth", "Implement auth middleware");
        task_node.node_type = Some("task".into());
        task_node.description = Some("Add JWT-based auth middleware to API gateway".into());
        graph.add_node(task_node);

        let yaml = serde_yaml::to_string(&graph).unwrap();
        std::fs::write(gid_dir.join("graph.yml"), &yaml).unwrap();

        // Create executor pointing to the temp dir
        let executor = V2Executor::new(V2ExecutorConfig {
            project_root: tmp.path().to_path_buf(),
            ..V2ExecutorConfig::default()
        });

        let mut state = RitualState::new();
        state.task = "implement auth".into();

        let enriched = executor.enrich_implement_context("implement auth", &state);

        // Should contain the task title from the graph
        assert!(enriched.contains("Implement auth middleware"),
            "enriched context should include task title from graph. Got: {}", enriched);
        // Should also contain the original task
        assert!(enriched.contains("implement auth"),
            "enriched context should include original task text");
    }

    #[test]
    fn test_enrich_no_graph() {
        // Temp dir with no .gid/graph.yml — should fall back to raw context
        let tmp = tempfile::tempdir().unwrap();

        let executor = V2Executor::new(V2ExecutorConfig {
            project_root: tmp.path().to_path_buf(),
            ..V2ExecutorConfig::default()
        });

        let mut state = RitualState::new();
        state.task = "fix the bug".into();

        let enriched = executor.enrich_implement_context("fix the bug", &state);
        assert_eq!(enriched, "fix the bug",
            "with no graph, should return raw context unchanged");
    }

    #[test]
    fn test_enrich_no_task_nodes() {
        // Graph exists but has only code nodes (no task nodes)
        let tmp = tempfile::tempdir().unwrap();
        let gid_dir = tmp.path().join(".gid");
        std::fs::create_dir_all(&gid_dir).unwrap();

        let mut graph = Graph::new();
        let mut code_node = crate::graph::Node::new("file-main", "src/main.rs");
        code_node.node_type = Some("code".into());
        graph.add_node(code_node);

        let yaml = serde_yaml::to_string(&graph).unwrap();
        std::fs::write(gid_dir.join("graph.yml"), &yaml).unwrap();

        let executor = V2Executor::new(V2ExecutorConfig {
            project_root: tmp.path().to_path_buf(),
            ..V2ExecutorConfig::default()
        });

        let mut state = RitualState::new();
        state.task = "refactor main".into();

        let enriched = executor.enrich_implement_context("refactor main", &state);
        assert_eq!(enriched, "refactor main",
            "with no task nodes, should fall back to raw context");
    }

    #[test]
    fn test_enrich_with_error_context() {
        // Simulate a verify-fix cycle: raw_context contains the error message
        let tmp = tempfile::tempdir().unwrap();
        let gid_dir = tmp.path().join(".gid");
        std::fs::create_dir_all(&gid_dir).unwrap();

        let mut graph = Graph::new();
        let mut task_node = crate::graph::Node::new("task-api", "Implement API endpoint");
        task_node.node_type = Some("task".into());
        graph.add_node(task_node);

        let yaml = serde_yaml::to_string(&graph).unwrap();
        std::fs::write(gid_dir.join("graph.yml"), &yaml).unwrap();

        let executor = V2Executor::new(V2ExecutorConfig {
            project_root: tmp.path().to_path_buf(),
            ..V2ExecutorConfig::default()
        });

        let mut state = RitualState::new();
        state.task = "implement API endpoint".into();

        let error_context = "FIX BUILD ERROR:\nerror[E0433]: failed to resolve: use of undeclared crate\n\nOriginal task: implement API endpoint";
        let enriched = executor.enrich_implement_context(error_context, &state);

        // Should contain both the graph context AND the error
        assert!(enriched.contains("Implement API endpoint"),
            "should include task from graph");
        assert!(enriched.contains("FIX BUILD ERROR"),
            "should include error message from raw context");
        assert!(enriched.contains("E0433"),
            "should preserve full error detail");
    }

    #[test]
    fn test_review_config_medium() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let mut state = RitualState::new();
        state.triage_size = Some("medium".into());

        let config = executor.review_config_for_triage_size(&state);
        assert_eq!(config.depth, ReviewDepth::Light,
            "medium tasks should get Light review depth");
        assert_eq!(config.max_iterations, 50);
        // Model should be the skill_model from config (default: "opus")
        assert_eq!(config.model, "opus");
    }

    #[test]
    fn test_review_config_large() {
        let executor = V2Executor::new(V2ExecutorConfig::default());
        let mut state = RitualState::new();
        state.triage_size = Some("large".into());

        let config = executor.review_config_for_triage_size(&state);
        assert_eq!(config.depth, ReviewDepth::Full,
            "large tasks should get Full review depth");
        assert_eq!(config.max_iterations, 100);
        assert_eq!(config.model, "opus");
    }

    #[test]
    fn test_light_review_prompt_injection() {
        // Verify that light review depth injects the correct check scope instructions
        let config = ReviewConfig {
            model: "sonnet".into(),
            max_iterations: 30,
            depth: ReviewDepth::Light,
        };

        // Simulate what run_skill does for review phases
        let base_prompt = "# Review Design\nRun all checks...";
        let depth_label = match config.depth {
            ReviewDepth::Light => "quick",
            ReviewDepth::Full => "full",
        };
        let injected = if config.depth == ReviewDepth::Light {
            format!(
                "[REVIEW_DEPTH: {}]\n\n## REVIEW SCOPE: LIGHT\nRun ONLY checks #1, #2, #5, #6, #7, #8, #11, #13, #21, #27.\nSkip all other checks. Write findings to file.\n\n{}",
                depth_label, base_prompt
            )
        } else {
            format!("[REVIEW_DEPTH: {}]\n\n{}", depth_label, base_prompt)
        };

        assert!(injected.contains("[REVIEW_DEPTH: quick]"),
            "light review should inject quick depth label");
        assert!(injected.contains("REVIEW SCOPE: LIGHT"),
            "light review should inject scope heading");
        assert!(injected.contains("#1, #2, #5, #6, #7, #8, #11, #13, #21, #27"),
            "light review should list the 10 core checks");
        assert!(injected.contains("Skip all other checks"),
            "light review should instruct to skip non-core checks");

        // Full review should NOT inject scope restriction
        let full_config = ReviewConfig {
            model: "opus".into(),
            max_iterations: 55,
            depth: ReviewDepth::Full,
        };
        let full_label = match full_config.depth {
            ReviewDepth::Light => "quick",
            ReviewDepth::Full => "full",
        };
        let full_injected = if full_config.depth == ReviewDepth::Light {
            format!("[REVIEW_DEPTH: {}]\n\n## REVIEW SCOPE: LIGHT\n...\n\n{}", full_label, base_prompt)
        } else {
            format!("[REVIEW_DEPTH: {}]\n\n{}", full_label, base_prompt)
        };

        assert!(full_injected.contains("[REVIEW_DEPTH: full]"),
            "full review should inject full depth label");
        assert!(!full_injected.contains("REVIEW SCOPE: LIGHT"),
            "full review should NOT inject scope restriction");
    }
}