Commit f9a60b73
f9a60b732e73Merged
Scott Nixon <[email protected]> ยท 9 months ago ยท 1 file ยท ~1 modified
Description
Implement decompose CLI command (attractor-wqc)
Details
- Commit
- f9a60b732e73
- Parent
- 6da7c83b794d
crates/attractor-cli/src/main.rsmodifiedView file
@@ -73,6 +73,16 @@
7373
#[arg(short, long)]7474
output: Option<PathBuf>,7575
},76+
77+
/// Decompose a spec into beads epic and tasks78+
Decompose {79+
/// Path to the spec markdown file80+
spec_path: PathBuf,81+
82+
/// Print the generated shell commands without executing them83+
#[arg(long)]84+
dry_run: bool,85+
},7686
}7787
7888
#[tokio::main]@@ -105,6 +115,9 @@
105115
Commands::Plan { prd, spec, from_prompt, output } => {106116
cmd_plan(prd, spec, from_prompt.as_deref(), output.as_deref()).await?;107117
}118+
Commands::Decompose { spec_path, dry_run } => {119+
cmd_decompose(&spec_path, dry_run).await?;120+
}108121
}109122
110123
Ok(())@@ -365,3 +378,129 @@
365378
366379
Ok(())367380
}381+
382+
async fn cmd_decompose(spec_path: &std::path::Path, dry_run: bool) -> anyhow::Result<()> {383+
// Read the spec file384+
let spec_content = std::fs::read_to_string(spec_path)?;385+
386+
// Build prompt for Claude to generate bd commands387+
let prompt = format!(388+
"Read this technical specification and generate a shell script of beads (bd) CLI commands to create an epic and tasks.\n\n\389+
SPEC:\n{}\n\n\390+
INSTRUCTIONS:\n\391+
1. Extract the title from the spec (usually in the first heading)\n\392+
2. Extract implementation phases/tasks from the '## Implementation Phases' or similar section\n\393+
3. Create an epic first: EPIC_ID=$(bd create --title='TITLE' --type=epic --priority=P1 --description='OVERVIEW' --silent)\n\394+
4. For each task/phase: TASK_N=$(bd create --title='TITLE' --type=task --priority=PN --description='DESC' --silent)\n\395+
5. Add dependencies using: bd dep add $BLOCKED_TASK $BLOCKER_TASK (the blocked task depends on the blocker)\n\396+
6. Output ONLY executable shell commands - no markdown code fences (```), no explanations, no commentary\n\397+
7. Use set -e at the start so it fails fast on errors\n\398+
8. Echo the epic ID at the end: echo $EPIC_ID\n\399+
9. Priority should be P2 for most tasks unless critical (P1) or backlog (P3/P4)\n\400+
10. Task descriptions should be 1-2 sentences summarizing what needs to be done\n\401+
11. CRITICAL: Do NOT wrap output in markdown code fences. Start directly with 'set -e'",402+
spec_content403+
);404+
405+
// Call Claude CLI with JSON output format406+
let mut cmd = tokio::process::Command::new("claude");407+
cmd.arg("-p")408+
.arg(&prompt)409+
.arg("--output-format")410+
.arg("json")411+
.arg("--dangerously-skip-permissions")412+
.arg("--no-session-persistence");413+
414+
// Capture output415+
cmd.stdout(std::process::Stdio::piped());416+
cmd.stderr(std::process::Stdio::piped());417+
418+
let output_result = cmd.output().await?;419+
420+
if !output_result.status.success() {421+
let stderr = String::from_utf8_lossy(&output_result.stderr);422+
anyhow::bail!("Claude CLI failed: {}", stderr);423+
}424+
425+
let output_json = String::from_utf8(output_result.stdout)?;426+
let parsed: serde_json::Value = serde_json::from_str(&output_json)?;427+
428+
let mut shell_commands = parsed["result"]429+
.as_str()430+
.ok_or_else(|| anyhow::anyhow!("Claude output missing 'result' field"))?431+
.to_string();432+
433+
// Strip markdown code fences if present434+
if shell_commands.starts_with("```") {435+
let lines: Vec<&str> = shell_commands.lines().collect();436+
if lines.len() > 2 && lines[0].starts_with("```") && lines[lines.len() - 1] == "```" {437+
shell_commands = lines[1..lines.len() - 1].join("\n");438+
}439+
}440+
441+
// Prepend shebang (and set -e if not already present)442+
let full_script = if shell_commands.starts_with("set -e") {443+
format!("#!/bin/sh\n{}", shell_commands)444+
} else {445+
format!("#!/bin/sh\nset -e\n\n{}", shell_commands)446+
};447+
448+
if dry_run {449+
println!("Generated shell commands (dry run):\n");450+
println!("{}", full_script);451+
return Ok(());452+
}453+
454+
// Execute the shell script455+
let temp_script = std::env::temp_dir().join("attractor-decompose.sh");456+
std::fs::write(&temp_script, &full_script)?;457+
458+
// Make executable459+
#[cfg(unix)]460+
{461+
use std::os::unix::fs::PermissionsExt;462+
let mut perms = std::fs::metadata(&temp_script)?.permissions();463+
perms.set_mode(0o755);464+
std::fs::set_permissions(&temp_script, perms)?;465+
}466+
467+
// Execute with sh -e468+
let exec_result = tokio::process::Command::new("sh")469+
.arg("-e")470+
.arg(&temp_script)471+
.output()472+
.await?;473+
474+
// Clean up temp file475+
std::fs::remove_file(&temp_script).ok();476+
477+
if !exec_result.status.success() {478+
let stderr = String::from_utf8_lossy(&exec_result.stderr);479+
anyhow::bail!("Shell script execution failed: {}", stderr);480+
}481+
482+
let output_text = String::from_utf8(exec_result.stdout)?;483+
484+
// Extract epic ID from last line of output485+
let epic_id = output_text486+
.lines()487+
.last()488+
.unwrap_or("")489+
.trim();490+
491+
// Count tasks created (number of lines with TASK_N=)492+
let task_count = shell_commands.matches("TASK_").count();493+
494+
// Count dependencies (number of bd dep add lines)495+
let dep_count = shell_commands.matches("bd dep add").count();496+
497+
println!("โ Decomposition complete");498+
println!(" Epic ID: {}", epic_id);499+
println!(" Tasks created: {}", task_count);500+
println!(" Dependencies: {}", dep_count);501+
println!("\nNext steps:");502+
println!("1. Review tasks: bd list");503+
println!("2. Generate pipeline: attractor scaffold {}", epic_id);504+
505+
Ok(())506+
}