sessions : am sessions --resume <id> (issue #32) - relance en arriere-plan avec les arguments et le cwd d'origine, cwd optionnel dans spawn_background, gardes running/repl/erreurs claires

This commit is contained in:
2026-08-17 08:48:30 -04:00
parent 314583ceee
commit 30cc49de3c
8 changed files with 96 additions and 12 deletions
+46
View File
@@ -44,10 +44,14 @@ pub fn run(
project: Option<&str>,
status: Option<&str>,
show: Option<&str>,
resume: Option<&str>,
) -> Result<i32> {
if let Some(id) = show {
return show_one(app, id);
}
if let Some(id) = resume {
return resume_session(app, id);
}
sessions::reconcile(app)?;
let sf = sessions::load(app)?;
let mut rows: Vec<SessionRow> = sf
@@ -130,6 +134,48 @@ fn show_one(app: &App, id: &str) -> Result<i32> {
Ok(0)
}
/// Resume a finished session: relaunch the agent in the background with
/// its recorded arguments and working directory (issue #32).
fn resume_session(app: &App, id: &str) -> Result<i32> {
sessions::reconcile(app)?;
let sf = sessions::load(app)?;
let Some(r) = sf.sessions.get(id).cloned() else {
bail!("unknown session '{id}' — 'am sessions' lists every session");
};
if r.kind != "agent" {
bail!("session '{id}' is a REPL session — start it again with 'am'");
}
if r.status == "running" {
app.log.info(&format!("session '{id}' is still running"));
return Ok(0);
}
let Some(agent_name) = r.agent.clone() else {
bail!("session '{id}' has no agent");
};
require_agent(app, &agent_name)?;
if app.dry_run() {
app.log.dry(format!(
"would resume session {id}: start {} --background with {} arg(s)",
agent_name,
r.args.len()
));
return Ok(0);
}
let cwd = r.cwd.as_deref().map(std::path::Path::new);
let opts = crate::cli::StartArgs {
agent: Some(agent_name.clone()),
background: true,
args: r.args.clone(),
..Default::default()
};
app.log.info(&format!(
"resuming session {id}: {} ({} arg(s))",
agent_name,
r.args.len()
));
crate::commands::run_cmd::start_with_cwd(app, &opts, cwd)
}
/// "20260815_143926_a1b2c3" -> "20260815_143926"
fn short_id(id: &str) -> String {
id.chars().take(15).collect()