perspt-agent 0.5.7

SRBN Orchestrator and Agent logic for Perspt
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
//! Architect planning: task decomposition, node creation, and fallback graphs.

use super::*;

impl SRBNOrchestrator {
    ///
    /// The Architect analyzes the task and produces a structured Task DAG.
    /// This step retries until a valid JSON plan is produced or max attempts reached.
    pub(super) async fn step_sheafify(&mut self, task: String) -> Result<()> {
        log::info!("Step 1: Sheafification - Planning task decomposition");
        self.emit_log("🏗️ Architect is analyzing the task...".to_string());

        const MAX_ATTEMPTS: usize = 3;
        let mut last_error: Option<String> = None;

        for attempt in 1..=MAX_ATTEMPTS {
            log::info!(
                "Sheafification attempt {}/{}: requesting structured plan",
                attempt,
                MAX_ATTEMPTS
            );

            // Build the structured prompt
            let prompt = self.build_architect_prompt(&task, last_error.as_deref())?;

            // Call the Architect with tier-aware fallback for structured-output failures
            let response = self
                .call_llm_with_tier_fallback(
                    &self.get_architect_model(),
                    &prompt,
                    None,
                    ModelTier::Architect,
                    |resp| {
                        // Validate that the response contains parseable JSON plan
                        if resp.contains("tasks") && (resp.contains('{') && resp.contains('}')) {
                            Ok(())
                        } else {
                            Err("Response does not contain a JSON task plan".to_string())
                        }
                    },
                )
                .await
                .context("Failed to get Architect response")?;

            log::debug!("Architect response length: {} chars", response.len());

            // Try to parse the JSON plan
            match self.parse_task_plan(&response) {
                Ok(plan) => {
                    // Validate the plan
                    if let Err(e) = plan.validate() {
                        log::warn!("Plan validation failed (attempt {}): {}", attempt, e);
                        last_error = Some(format!("Validation error: {}", e));

                        if attempt >= MAX_ATTEMPTS {
                            self.emit_log(format!(
                                "❌ Failed to get valid plan after {} attempts",
                                MAX_ATTEMPTS
                            ));
                            // Fall back to single-task execution
                            return self.create_deterministic_fallback_graph(&task);
                        }
                        continue;
                    }

                    // Check complexity gating
                    if plan.len() > self.context.complexity_k && !self.auto_approve {
                        self.emit_log(format!(
                            "⚠️ Plan has {} tasks (exceeds K={})",
                            plan.len(),
                            self.context.complexity_k
                        ));
                        // TODO: Implement interactive approval
                        // For now, auto-approve in headless mode
                    }

                    // FeatureCharter file-budget gate: reject plans that exceed
                    // the session charter (if one is registered).
                    if let Ok(Some(charter)) = self.ledger.get_feature_charter() {
                        let file_count = plan.tasks.iter().flat_map(|t| &t.output_files).count();
                        if let Some(max_files) = charter.max_files {
                            if file_count > max_files as usize {
                                self.emit_log(format!(
                                    "⚠️ Plan produces {} files but charter allows max {}",
                                    file_count, max_files
                                ));
                            }
                        }
                        if let Some(max_modules) = charter.max_modules {
                            if plan.len() > max_modules as usize {
                                self.emit_log(format!(
                                    "⚠️ Plan has {} tasks but charter allows max {} modules",
                                    plan.len(),
                                    max_modules
                                ));
                            }
                        }
                    }

                    self.emit_log(format!(
                        "✅ Architect produced plan with {} task(s)",
                        plan.len()
                    ));

                    // Emit plan generated event
                    self.emit_event(perspt_core::AgentEvent::PlanGenerated(plan.clone()));

                    // Record initial plan revision for audit trail
                    let revision = perspt_core::types::PlanRevision::initial(
                        &self.context.session_id,
                        plan.clone(),
                    );
                    if let Err(e) = self.ledger.record_plan_revision(&revision) {
                        log::warn!("Failed to persist initial plan revision: {}", e);
                    }

                    // Create nodes from the plan
                    self.create_nodes_from_plan(&plan)?;

                    // Post-plan: reconcile workspace structure if plan
                    // introduces sub-crate / sub-package directories.
                    self.reconcile_workspace_structure(&plan);

                    return Ok(());
                }
                Err(e) => {
                    log::warn!("Plan parsing failed (attempt {}): {}", attempt, e);
                    last_error = Some(format!("JSON parse error: {}", e));

                    if attempt >= MAX_ATTEMPTS {
                        self.emit_log(
                            "⚠️ Could not parse structured plan, using single task".to_string(),
                        );
                        return self.create_deterministic_fallback_graph(&task);
                    }
                }
            }
        }

        // Should not reach here
        self.create_deterministic_fallback_graph(&task)
    }

    /// Build the Architect prompt requesting structured JSON output
    ///
    /// PSP-5 Fix F: Delegates to `ArchitectAgent::build_task_decomposition_prompt`
    /// so the JSON schema contract lives in one place.
    fn build_architect_prompt(&self, task: &str, last_error: Option<&str>) -> Result<String> {
        let mut project_context = self.gather_project_context();

        let error_feedback = if let Some(e) = last_error {
            format!(
                "\n## Previous Attempt Failed\nError: {}\nPlease fix the JSON format and try again.\n",
                e
            )
        } else {
            String::new()
        };

        // PSP-5: For existing projects, prepend a structured project summary
        // and gather evidence (API seams, module boundaries, test layout).
        let (template, evidence_section) = if matches!(
            self.context.workspace_state,
            WorkspaceState::ExistingProject { .. }
        ) {
            let retriever = ContextRetriever::new(self.context.working_dir.clone());
            let summary = retriever.get_project_summary();
            if !summary.is_empty() {
                project_context = format!("{}\n\n{}", summary, project_context);
            }
            let evidence = retriever.gather_architect_evidence();
            (crate::prompts::ARCHITECT_EXISTING, evidence)
        } else {
            (crate::prompts::ARCHITECT_GREENFIELD, String::new())
        };

        Ok(crate::prompts::render_architect(
            template,
            task,
            &self.context.working_dir,
            &project_context,
            &error_feedback,
            &evidence_section,
            &self.context.active_plugins,
        ))
    }

    /// Gather existing project context for the Architect prompt
    /// Uses ContextRetriever to read key configuration files
    fn gather_project_context(&self) -> String {
        let mut context_parts = Vec::new();
        let working_dir = &self.context.working_dir;
        let retriever = ContextRetriever::new(working_dir.clone())
            .with_max_file_bytes(8 * 1024) // 8KB per file for config files
            .with_max_context_bytes(32 * 1024); // 32KB total context

        // Key config files to read (in priority order)
        let config_files = [
            "pyproject.toml",
            "Cargo.toml",
            "package.json",
            "requirements.txt",
        ];

        // Read and include config file contents (up to max_file_bytes)
        let mut found_configs = Vec::new();
        for file in &config_files {
            let path = working_dir.join(file);
            if path.exists() {
                if let Ok(content) = retriever.read_file_truncated(&path) {
                    context_parts.push(format!("### {}\n```\n{}\n```", file, content));
                    found_configs.push(*file);
                }
            }
        }

        // List directory structure
        if let Ok(entries) = std::fs::read_dir(working_dir) {
            let mut dirs = Vec::new();
            let mut files = Vec::new();
            for entry in entries.flatten() {
                let name = entry.file_name().to_string_lossy().to_string();
                if name.starts_with('.') {
                    continue; // Skip hidden files/dirs
                }
                if entry.path().is_dir() {
                    dirs.push(name);
                } else if !found_configs.contains(&name.as_str()) {
                    files.push(name);
                }
            }

            if !dirs.is_empty() {
                context_parts.push(format!("### Directories\n{}", dirs.join(", ")));
            }
            if !files.is_empty() && files.len() <= 15 {
                context_parts.push(format!("### Other Files\n{}", files.join(", ")));
            } else if !files.is_empty() {
                context_parts.push(format!(
                    "### Other Files\n{} files (not listed)",
                    files.len()
                ));
            }
        }

        // Detect Python src-layout package name so the architect uses the
        // correct path prefix (e.g. src/test_spectral/) instead of inventing
        // its own package name.
        let src_dir = working_dir.join("src");
        if src_dir.is_dir() {
            if let Ok(entries) = std::fs::read_dir(&src_dir) {
                let pkgs: Vec<String> = entries
                    .flatten()
                    .filter(|e| {
                        e.path().is_dir() && !e.file_name().to_string_lossy().starts_with('.')
                    })
                    .map(|e| e.file_name().to_string_lossy().to_string())
                    .collect();
                if !pkgs.is_empty() {
                    context_parts.push(format!(
                        "### Source Layout (IMPORTANT — use these exact paths)\n\
                         The project already has the following package(s) under `src/`:\n\
                         {}\n\
                         ALL Python source files MUST be placed under `src/{}/` — \
                         do NOT invent a different package name.",
                        pkgs.iter()
                            .map(|p| format!("- `src/{}/`", p))
                            .collect::<Vec<_>>()
                            .join("\n"),
                        pkgs[0]
                    ));
                }
            }
        }

        if context_parts.is_empty() {
            "Empty directory (greenfield project)".to_string()
        } else {
            context_parts.join("\n\n")
        }
    }

    /// Parse JSON response into TaskPlan
    ///
    /// PSP-5 Phase 4: Uses the provider-neutral normalization layer to extract
    /// JSON from LLM responses regardless of fencing, wrapper text, or provider
    /// formatting quirks. Falls back to raw content parsing if normalization
    /// finds no JSON.
    fn parse_task_plan(&self, content: &str) -> Result<TaskPlan> {
        // PSP-5 Phase 4: Use normalized extraction
        match perspt_core::normalize::extract_and_deserialize::<TaskPlan>(content) {
            Ok((plan, method)) => {
                log::info!("Parsed TaskPlan via normalization ({})", method);
                return Ok(plan);
            }
            Err(e) => {
                log::warn!(
                    "Normalization could not extract TaskPlan: {}. Attempting raw parse.",
                    e
                );
            }
        }

        // Legacy fallback: try direct deserialization of trimmed content
        let trimmed = content.trim();
        log::debug!(
            "Attempting legacy JSON parse: {}...",
            &trimmed[..trimmed.len().min(200)]
        );
        serde_json::from_str(trimmed).context("Failed to parse TaskPlan JSON")
    }

    /// Create SRBN nodes from a parsed TaskPlan
    pub(super) fn create_nodes_from_plan(&mut self, plan: &TaskPlan) -> Result<()> {
        // PSP-5: Validate plan structure including ownership closure before creating nodes
        plan.validate()
            .map_err(|e| anyhow::anyhow!("Plan validation failed: {}", e))?;

        log::info!("Creating {} nodes from plan", plan.len());

        // Create all nodes first
        let mut node_map: HashMap<String, NodeIndex> = HashMap::new();

        for task in &plan.tasks {
            let node = task.to_srbn_node(ModelTier::Actuator);
            let idx = self.add_node(node);
            node_map.insert(task.id.clone(), idx);
            log::info!("  Created node: {} - {}", task.id, task.goal);
        }

        // Wire up dependencies
        for task in &plan.tasks {
            for dep_id in &task.dependencies {
                if let (Some(&from_idx), Some(&to_idx)) =
                    (node_map.get(dep_id), node_map.get(&task.id))
                {
                    self.graph.add_edge(
                        from_idx,
                        to_idx,
                        Dependency {
                            kind: "depends_on".to_string(),
                        },
                    );
                    log::debug!("  Wired dependency: {} -> {}", dep_id, task.id);

                    // PSP-5 Phase 8: Persist graph edge for resume reconstruction
                    if let Err(e) =
                        self.ledger
                            .record_task_graph_edge(dep_id, &task.id, "depends_on")
                    {
                        log::warn!(
                            "Failed to persist graph edge {} -> {}: {}",
                            dep_id,
                            task.id,
                            e
                        );
                    }
                }
            }
        }

        // PSP-5 Phase 2: Build ownership manifest from plan output_files
        self.build_ownership_manifest_from_plan(plan);

        Ok(())
    }

    /// PSP-5 Phase 2: Build ownership manifest from a TaskPlan
    ///
    /// Assigns each task's output_files to the owning node, detecting the
    /// language plugin from file extension via the plugin registry.
    /// Uses majority-vote across ALL output files instead of first-file-only heuristic.
    fn build_ownership_manifest_from_plan(&mut self, plan: &TaskPlan) {
        let registry = perspt_core::plugin::PluginRegistry::new();

        for task in &plan.tasks {
            // Detect plugin via majority vote across ALL output files
            let mut plugin_votes: HashMap<String, usize> = HashMap::new();
            for f in &task.output_files {
                let detected = registry
                    .all()
                    .iter()
                    .find(|p| p.owns_file(f))
                    .map(|p| p.name().to_string())
                    .unwrap_or_else(|| "unknown".to_string());
                *plugin_votes.entry(detected).or_insert(0) += 1;
            }

            let plugin_name = plugin_votes
                .into_iter()
                .max_by_key(|(_, count)| *count)
                .map(|(name, _)| name)
                .unwrap_or_else(|| "unknown".to_string());

            // Warn on mixed-plugin tasks (non-Integration nodes)
            if task.node_class != perspt_core::types::NodeClass::Integration {
                let mixed: Vec<String> = task
                    .output_files
                    .iter()
                    .filter_map(|f| {
                        let det = registry
                            .all()
                            .iter()
                            .find(|p| p.owns_file(f))
                            .map(|p| p.name().to_string())
                            .unwrap_or_else(|| "unknown".to_string());
                        if det != plugin_name {
                            Some(format!("'{}' ({})", f, det))
                        } else {
                            None
                        }
                    })
                    .collect();
                if !mixed.is_empty() {
                    log::warn!(
                        "Task '{}' has mixed-plugin outputs (primary: {}): {}",
                        task.id,
                        plugin_name,
                        mixed.join(", ")
                    );
                }
            }

            // Set owner_plugin on the node if we can find it
            if let Some(idx) = self.node_indices.get(&task.id) {
                self.graph[*idx].owner_plugin = plugin_name.clone();
            }

            // Register each output file in the manifest
            for file in &task.output_files {
                // Detect per-file plugin for accurate manifest entries
                let file_plugin = registry
                    .all()
                    .iter()
                    .find(|p| p.owns_file(file))
                    .map(|p| p.name().to_string())
                    .unwrap_or_else(|| plugin_name.clone());

                self.context.ownership_manifest.assign(
                    file.clone(),
                    task.id.clone(),
                    file_plugin,
                    task.node_class,
                );
            }
        }

        log::info!(
            "Built ownership manifest: {} entries",
            self.context.ownership_manifest.len()
        );
    }

    /// Post-plan workspace reconciliation.
    ///
    /// When the architect produces a plan with sub-crate paths (e.g.,
    /// `crates/<name>/Cargo.toml`), the init-created root manifest is a
    /// single-package `[package]` that doesn't know about workspace members.
    /// This function:
    /// 1. Detects sub-crate/sub-package prefixes from the plan's output_files.
    /// 2. Rewrites the root manifest to a workspace manifest.
    /// 3. Removes the init-scaffolded `src/` directory to avoid confusion.
    fn reconcile_workspace_structure(&self, plan: &TaskPlan) {
        let all_output_files: Vec<&str> = plan
            .tasks
            .iter()
            .flat_map(|t| t.output_files.iter().map(|s| s.as_str()))
            .collect();

        // Detect Rust workspace: any file under crates/<name>/
        let rust_crate_dirs: std::collections::BTreeSet<String> = all_output_files
            .iter()
            .filter_map(|f| {
                let parts: Vec<&str> = f.split('/').collect();
                if parts.len() >= 2 && parts[0] == "crates" {
                    Some(parts[1].to_string())
                } else {
                    None
                }
            })
            .collect();

        if !rust_crate_dirs.is_empty() {
            self.convert_to_rust_workspace(&rust_crate_dirs);
            return;
        }

        // Detect Node.js workspace: any file under packages/<name>/
        let js_pkg_dirs: std::collections::BTreeSet<String> = all_output_files
            .iter()
            .filter_map(|f| {
                let parts: Vec<&str> = f.split('/').collect();
                if parts.len() >= 2 && parts[0] == "packages" {
                    Some(parts[1].to_string())
                } else {
                    None
                }
            })
            .collect();

        if !js_pkg_dirs.is_empty() {
            self.convert_to_js_workspace(&js_pkg_dirs);
        }
    }

    /// Convert root Cargo.toml from `[package]` to `[workspace]` and remove
    /// the init-scaffolded `src/` directory.
    fn convert_to_rust_workspace(&self, crate_dirs: &std::collections::BTreeSet<String>) {
        let root = &self.context.working_dir;
        let cargo_toml = root.join("Cargo.toml");

        if !cargo_toml.exists() {
            return;
        }

        // Build explicit member paths for better cargo compatibility
        let members: Vec<String> = crate_dirs
            .iter()
            .map(|d| format!("\"crates/{}\"", d))
            .collect();
        let members_str = members.join(", ");

        let workspace_content = format!(
            "[workspace]\nmembers = [{}]\nresolver = \"2\"\n",
            members_str
        );

        if let Err(e) = std::fs::write(&cargo_toml, &workspace_content) {
            log::warn!("Failed to convert root Cargo.toml to workspace: {}", e);
            return;
        }

        log::info!(
            "Converted root Cargo.toml to workspace with members: {:?}",
            crate_dirs
        );
        self.emit_log(format!(
            "📦 Converted to Cargo workspace with {} member(s)",
            crate_dirs.len()
        ));

        // Remove init-scaffolded src/ directory — the plan's sub-crates
        // define their own src/ directories.
        let scaffold_src = root.join("src");
        if scaffold_src.is_dir() {
            if let Err(e) = std::fs::remove_dir_all(&scaffold_src) {
                log::warn!("Failed to remove init-scaffolded src/: {}", e);
            } else {
                log::info!("Removed init-scaffolded src/ directory");
            }
        }

        // Create crate directories and stub Cargo.toml for each member so
        // `cargo check` can resolve the workspace even before all nodes run.
        for crate_name in crate_dirs {
            let crate_dir = root.join("crates").join(crate_name);
            let src_dir = crate_dir.join("src");
            if !src_dir.exists() {
                if let Err(e) = std::fs::create_dir_all(&src_dir) {
                    log::debug!("Could not pre-create {}: {}", src_dir.display(), e);
                }
            }

            // Create a stub Cargo.toml so cargo recognises this member.
            // The actuator's bundle will overwrite it with the real manifest.
            let stub_cargo = crate_dir.join("Cargo.toml");
            if !stub_cargo.exists() {
                let stub_content = format!(
                    "[package]\nname = \"{}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
                    crate_name
                );
                if let Err(e) = std::fs::write(&stub_cargo, &stub_content) {
                    log::debug!("Could not write stub {}: {}", stub_cargo.display(), e);
                }
            }

            // Create a stub src/lib.rs so cargo check doesn't fail on missing entry
            let stub_lib = src_dir.join("lib.rs");
            if !stub_lib.exists() {
                let _ = std::fs::write(&stub_lib, "// stub — will be replaced by agent\n");
            }
        }
    }

    /// Convert root package.json to a workspace (npm/yarn/pnpm).
    fn convert_to_js_workspace(&self, _pkg_dirs: &std::collections::BTreeSet<String>) {
        let root = &self.context.working_dir;
        let pkg_json = root.join("package.json");

        if !pkg_json.exists() {
            return;
        }

        // Read existing package.json and add workspaces field
        if let Ok(content) = std::fs::read_to_string(&pkg_json) {
            if let Ok(mut json) = serde_json::from_str::<serde_json::Value>(&content) {
                if json.get("workspaces").is_none() {
                    json["workspaces"] = serde_json::json!(["packages/*"]);
                    if let Ok(new_content) = serde_json::to_string_pretty(&json) {
                        if let Err(e) = std::fs::write(&pkg_json, new_content) {
                            log::warn!("Failed to add workspaces to package.json: {}", e);
                        } else {
                            log::info!("Added workspaces field to package.json");
                            self.emit_log("📦 Converted to npm workspace".to_string());
                        }
                    }
                }
            }
        }
    }

    /// Get the Architect model name
    fn get_architect_model(&self) -> String {
        self.architect_model.clone()
    }

    /// PSP-5: Create a deterministic fallback execution graph
    ///
    /// When the Architect fails to produce a valid JSON plan after MAX_ATTEMPTS,
    /// this creates a minimal 3-node graph: scaffold → implement → test.
    pub(super) fn create_deterministic_fallback_graph(&mut self, task: &str) -> Result<()> {
        log::warn!("Using deterministic fallback graph (PSP-5)");
        self.emit_log("📦 Using deterministic fallback plan");

        // Emit FallbackPlanner event
        self.emit_event(perspt_core::AgentEvent::FallbackPlanner {
            reason: "Architect failed to produce valid JSON plan".to_string(),
        });

        // Detect language for file extensions
        let lang = self.detect_language_from_task(task).unwrap_or("python");
        let ext = match lang {
            "rust" => "rs",
            "javascript" => "js",
            _ => "py",
        };

        // Determine file names based on language
        let (main_file, test_file) = match lang {
            "rust" => (
                "src/main.rs".to_string(),
                "tests/integration_test.rs".to_string(),
            ),
            "javascript" => ("index.js".to_string(), "test/index.test.js".to_string()),
            _ => ("main.py".to_string(), format!("tests/test_main.{}", ext)),
        };

        // Node 1: Scaffold/structure
        let scaffold_task = perspt_core::types::PlannedTask {
            id: "scaffold".to_string(),
            goal: format!("Set up project structure for: {}", task),
            context_files: vec![],
            output_files: vec![main_file.clone()],
            dependencies: vec![],
            task_type: perspt_core::types::TaskType::Code,
            contract: Default::default(),
            command_contract: None,
            node_class: perspt_core::types::NodeClass::Interface,
            dependency_expectations: Default::default(),
        };

        // Node 2: Core implementation
        let impl_task = perspt_core::types::PlannedTask {
            id: "implement".to_string(),
            goal: format!("Implement core logic for: {}", task),
            context_files: vec![main_file.clone()],
            output_files: vec![main_file],
            dependencies: vec!["scaffold".to_string()],
            task_type: perspt_core::types::TaskType::Code,
            contract: Default::default(),
            command_contract: None,
            node_class: perspt_core::types::NodeClass::Implementation,
            dependency_expectations: Default::default(),
        };

        // Node 3: Tests
        let test_task = perspt_core::types::PlannedTask {
            id: "test".to_string(),
            goal: format!("Write tests for: {}", task),
            context_files: vec![],
            output_files: vec![test_file],
            dependencies: vec!["implement".to_string()],
            task_type: perspt_core::types::TaskType::UnitTest,
            contract: Default::default(),
            command_contract: None,
            node_class: perspt_core::types::NodeClass::Integration,
            dependency_expectations: Default::default(),
        };

        // Build plan and emit
        let mut plan = perspt_core::types::TaskPlan::new();
        plan.tasks.push(scaffold_task);
        plan.tasks.push(impl_task);
        plan.tasks.push(test_task);

        self.emit_event(perspt_core::AgentEvent::PlanGenerated(plan.clone()));
        self.create_nodes_from_plan(&plan)?;

        Ok(())
    }
}