refactor(headless): unify --once execution path into shared execute_once function
This commit is contained in:
@@ -37,40 +37,8 @@ pub fn run(config_or_task: &str, cwd_override: Option<&str>, name_override: Opti
|
|||||||
return run_once(&configs);
|
return run_once(&configs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --once: run multi-agent once with AI integration, no loop
|
|
||||||
if once {
|
if once {
|
||||||
let running = Arc::new(AtomicBool::new(true));
|
return execute_once(configs);
|
||||||
setup_ctrlc(running.clone());
|
|
||||||
let agents = spawn_and_wait(&configs, &running);
|
|
||||||
if agents.is_empty() { return Ok(()); }
|
|
||||||
write_state(&agents);
|
|
||||||
|
|
||||||
if agents.len() > 1 {
|
|
||||||
eprintln!("\n ◐ AI integrating...");
|
|
||||||
match integrate_results(&agents, "", 1) {
|
|
||||||
Ok(d) => {
|
|
||||||
let data = serde_json::json!({
|
|
||||||
"cycle": 1,
|
|
||||||
"agents": agents.iter().map(|a| a.to_ai_json()).collect::<Vec<_>>(),
|
|
||||||
"decision": &d,
|
|
||||||
});
|
|
||||||
atomic_write(
|
|
||||||
&format!("{STATE_DIR}/decision.json"),
|
|
||||||
serde_json::to_string_pretty(&data).unwrap_or_default().as_bytes(),
|
|
||||||
);
|
|
||||||
eprintln!(" ✓ Done\n");
|
|
||||||
println!("{d}");
|
|
||||||
}
|
|
||||||
Err(e) => eprintln!(" ✗ {e}"),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("{}", strip_dir_listing(&agents[0].output));
|
|
||||||
}
|
|
||||||
|
|
||||||
let all: Vec<usize> = (0..agents.len()).collect();
|
|
||||||
let path = save_session(1, &agents, "", &all);
|
|
||||||
eprintln!(" saved: {path}");
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let running = Arc::new(AtomicBool::new(true));
|
let running = Arc::new(AtomicBool::new(true));
|
||||||
@@ -183,13 +151,14 @@ pub fn run_preset(preset_name: &str) -> Result<(), String> {
|
|||||||
.ok_or_else(|| format!("unknown preset: {preset_name}"))?;
|
.ok_or_else(|| format!("unknown preset: {preset_name}"))?;
|
||||||
let once = std::env::args().any(|a| a == "--once");
|
let once = std::env::args().any(|a| a == "--once");
|
||||||
if once {
|
if once {
|
||||||
run_once_multi(configs)
|
execute_once(configs)
|
||||||
} else {
|
} else {
|
||||||
run_with_configs(configs)
|
run_with_configs(configs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_once_multi(configs: Vec<config::AgentConfig>) -> Result<(), String> {
|
/// Single-cycle execution: agents → AI integration → save → exit.
|
||||||
|
fn execute_once(configs: Vec<config::AgentConfig>) -> Result<(), String> {
|
||||||
let _ = std::fs::remove_dir_all(STATE_DIR);
|
let _ = std::fs::remove_dir_all(STATE_DIR);
|
||||||
create_state_dir();
|
create_state_dir();
|
||||||
let running = Arc::new(AtomicBool::new(true));
|
let running = Arc::new(AtomicBool::new(true));
|
||||||
@@ -198,7 +167,7 @@ fn run_once_multi(configs: Vec<config::AgentConfig>) -> Result<(), String> {
|
|||||||
if agents.is_empty() { return Ok(()); }
|
if agents.is_empty() { return Ok(()); }
|
||||||
write_state(&agents);
|
write_state(&agents);
|
||||||
|
|
||||||
if agents.len() > 1 {
|
let decision = if agents.len() > 1 {
|
||||||
eprintln!("\n ◐ AI integrating...");
|
eprintln!("\n ◐ AI integrating...");
|
||||||
match integrate_results(&agents, "", 1) {
|
match integrate_results(&agents, "", 1) {
|
||||||
Ok(d) => {
|
Ok(d) => {
|
||||||
@@ -213,15 +182,18 @@ fn run_once_multi(configs: Vec<config::AgentConfig>) -> Result<(), String> {
|
|||||||
);
|
);
|
||||||
eprintln!(" ✓ Done\n");
|
eprintln!(" ✓ Done\n");
|
||||||
println!("{d}");
|
println!("{d}");
|
||||||
|
d
|
||||||
}
|
}
|
||||||
Err(e) => eprintln!(" ✗ {e}"),
|
Err(e) => { eprintln!(" ✗ {e}"); String::new() }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("{}", strip_dir_listing(&agents[0].output));
|
let out = strip_dir_listing(&agents[0].output).to_string();
|
||||||
}
|
println!("{out}");
|
||||||
|
out
|
||||||
|
};
|
||||||
|
|
||||||
let all: Vec<usize> = (0..agents.len()).collect();
|
let all: Vec<usize> = (0..agents.len()).collect();
|
||||||
let path = save_session(1, &agents, "", &all);
|
let path = save_session(1, &agents, &decision, &all);
|
||||||
eprintln!(" saved: {path}");
|
eprintln!(" saved: {path}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user