gid-core 0.3.1

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
//! 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;

/// 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 the task is small enough that a DESIGN.md update 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(result) => {
                        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 (model, max_iterations) = if name == "review" {
            self.review_config_for_triage_size(state)
        } else {
            (self.config.skill_model.clone(), 100)
        };

        // Inject review depth hint into prompt for review phases
        let full_prompt = if name == "review" {
            let depth = match state.triage_size.as_deref().unwrap_or("medium") {
                "small" => "quick",
                "medium" => "standard",
                "large" => "full",
                _ => "standard",
            };
            format!("[REVIEW_DEPTH: {}]\n\n{}", depth, 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) -> (String, usize) {
        let size = state.triage_size.as_deref().unwrap_or("medium");
        
        match size {
            "small" => ("sonnet".to_string(), 30),
            "medium" => (self.config.skill_model.clone(), 50),
            "large" => (self.config.skill_model.clone(), 100),
            _ => (self.config.skill_model.clone(), 50),
        }
    }

    // ═══════════════════════════════════════════════════════════════════════
    // 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"
                    .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"));
    }

    #[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"));
    }
}