Commit 23d113cf
23d113cfae76Merged
Scott Nixon <[email protected]> ยท 9 months ago ยท 1 file ยท ~1 modified
Description
Implement scaffold CLI command (attractor-i33)
Details
- Commit
- 23d113cfae76
- Parent
- f9a60b732e73
crates/attractor-cli/src/main.rsmodifiedView file
@@ -83,6 +83,16 @@
8383
#[arg(long)]8484
dry_run: bool,8585
},86+
87+
/// Scaffold a pipeline from a beads epic88+
Scaffold {89+
/// Beads epic ID (e.g., beads-xxx)90+
epic_id: String,91+
92+
/// Output file path (default: pipelines/<epic-id>.dot)93+
#[arg(short, long)]94+
output: Option<PathBuf>,95+
},8696
}8797
8898
#[tokio::main]@@ -118,6 +128,9 @@
118128
Commands::Decompose { spec_path, dry_run } => {119129
cmd_decompose(&spec_path, dry_run).await?;120130
}131+
Commands::Scaffold { epic_id, output } => {132+
cmd_scaffold(&epic_id, output.as_deref()).await?;133+
}121134
}122135
123136
Ok(())@@ -504,3 +517,109 @@
504517
505518
Ok(())506519
}520+
521+
async fn cmd_scaffold(epic_id: &str, output: Option<&std::path::Path>) -> anyhow::Result<()> {522+
// Load epic-runner template523+
let template = include_str!("../../../templates/epic-runner.dot");524+
525+
// Get epic details via bd show --json526+
let mut cmd = tokio::process::Command::new("bd");527+
cmd.arg("show")528+
.arg(epic_id)529+
.arg("--json");530+
531+
cmd.stdout(std::process::Stdio::piped());532+
cmd.stderr(std::process::Stdio::piped());533+
534+
let output_result = cmd.output().await?;535+
536+
if !output_result.status.success() {537+
let stderr = String::from_utf8_lossy(&output_result.stderr);538+
anyhow::bail!("bd show failed: {}", stderr);539+
}540+
541+
let json_output = String::from_utf8(output_result.stdout)?;542+
let epic_array: serde_json::Value = serde_json::from_str(&json_output)?;543+
544+
// bd show --json returns an array with one element545+
let epic_data = epic_array546+
.as_array()547+
.and_then(|arr| arr.first())548+
.ok_or_else(|| anyhow::anyhow!("bd show returned empty array"))?;549+
550+
let title = epic_data["title"]551+
.as_str()552+
.unwrap_or("Unknown Epic");553+
let description = epic_data["description"]554+
.as_str()555+
.unwrap_or("");556+
557+
// First, update the goal attribute BEFORE replacing EPIC_ID558+
let goal_text = format!(559+
"Implement all child tasks of epic {}: {}.{}",560+
epic_id,561+
title,562+
if description.is_empty() {563+
String::new()564+
} else {565+
format!(" {}", description)566+
}567+
);568+
569+
let mut pipeline_content = template.replace(570+
"goal=\"Implement all child tasks of epic EPIC_ID, closing each as completed.\"",571+
&format!("goal=\"{}\"", goal_text.replace('"', "\\\""))572+
);573+
574+
// Then replace all remaining EPIC_ID placeholders575+
pipeline_content = pipeline_content.replace("EPIC_ID", epic_id);576+
577+
// Determine output path578+
let output_path = if let Some(path) = output {579+
path.to_path_buf()580+
} else {581+
std::path::PathBuf::from(format!("pipelines/{}.dot", epic_id))582+
};583+
584+
// Create parent directory if needed585+
if let Some(parent) = output_path.parent() {586+
std::fs::create_dir_all(parent)?;587+
}588+
589+
// Write pipeline file590+
std::fs::write(&output_path, &pipeline_content)?;591+
592+
// Validate the generated pipeline593+
let graph = load_pipeline(&output_path)?;594+
let diagnostics = attractor_pipeline::validate(&graph);595+
596+
let has_error = diagnostics.iter().any(|d| {597+
matches!(d.severity, attractor_pipeline::Severity::Error)598+
});599+
600+
if has_error {601+
println!("โ Pipeline generated but has validation errors:");602+
for diag in &diagnostics {603+
if matches!(diag.severity, attractor_pipeline::Severity::Error) {604+
println!(" [ERROR] {}: {}", diag.rule, diag.message);605+
}606+
}607+
}608+
609+
// Count nodes610+
let node_count = graph.all_nodes().count();611+
612+
println!("โ Pipeline scaffolded");613+
println!(" Output: {}", output_path.display());614+
println!(" Epic: {} ({})", epic_id, title);615+
println!(" Nodes: {}", node_count);616+
println!(" Validation: {}", if has_error { "FAILED" } else { "PASSED" });617+
618+
if !has_error {619+
println!("\nNext steps:");620+
println!("1. Review pipeline: cat {}", output_path.display());621+
println!("2. Run pipeline: attractor run {} -w .", output_path.display());622+
}623+
624+
Ok(())625+
}