feat: restart a failed or escalated session at the state where it stopped
Adds orch session --restart sN and R in the TUI. A restart event gives the session fresh budgets and retry counts. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0e57b04d25
commit
e50f90d299
9 files changed
+140
-10
No files matched your search
@@ -369,6 +369,23 @@ impl Db {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Turns a failed or escalated session back into a suspended one at the state where it stopped, so the
|
||||
/// resume path continues it. Budgets and catch counts start again from the `restart` event.
|
||||
pub fn reopen(&self, id: &str) -> Result<()> {
|
||||
let s = self
|
||||
.session(id)?
|
||||
.with_context(|| format!("no session {id}"))?;
|
||||
anyhow::ensure!(
|
||||
matches!(s.status.as_str(), "failed" | "escalated"),
|
||||
"session {id} is {}; only a failed or escalated session can be restarted",
|
||||
s.status
|
||||
);
|
||||
self.set_session(id, &s.state, "suspended")?;
|
||||
self.set_pid(id, None)?;
|
||||
self.emit(id, "restart", serde_json::json!({"from": s.status, "state": s.state}))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_pid(&self, id: &str, pid: Option<u32>) -> Result<()> {
|
||||
self.conn.execute(
|
||||
"update sessions set pid = ?2 where id = ?1",
|
||||
@@ -493,10 +510,12 @@ impl Db {
|
||||
}
|
||||
|
||||
/// How often `catch` fired in `state` of this session (rebuilt from events, so it survives restarts).
|
||||
/// Catches before the last `restart` do not count: a restarted session gets its retries back.
|
||||
pub fn catch_count(&self, session: &str, state: &str, catch: &str) -> Result<u32> {
|
||||
let n: i64 = self.conn.query_row(
|
||||
"select count(*) from events where session_id = ?1 and kind = 'catch'
|
||||
and json_extract(payload, '$.state') = ?2 and json_extract(payload, '$.catch') = ?3",
|
||||
and json_extract(payload, '$.state') = ?2 and json_extract(payload, '$.catch') = ?3
|
||||
and id > coalesce((select max(id) from events where session_id = ?1 and kind = 'restart'), 0)",
|
||||
params![session, state, catch],
|
||||
|r| r.get(0),
|
||||
)?;
|
||||
@@ -566,6 +585,18 @@ mod tests {
|
||||
assert_eq!(db.catch_count("s1", "test", "U").unwrap(), 0);
|
||||
assert_eq!(db.events(Some("s1"), 0).unwrap().len(), 2);
|
||||
assert_eq!(db.events(None, last - 1).unwrap().len(), 1);
|
||||
// only a failed or escalated session reopens; it keeps its state and gets its retries back
|
||||
assert!(db.reopen("s1").is_err(), "running");
|
||||
db.set_session("s1", "test", "failed").unwrap();
|
||||
db.reopen("s1").unwrap();
|
||||
let s = db.session("s1").unwrap().unwrap();
|
||||
assert_eq!((s.status.as_str(), s.state.as_str()), ("suspended", "test"));
|
||||
assert_eq!(db.catch_count("s1", "test", "T").unwrap(), 0);
|
||||
db.emit("s1", "catch", json!({"state": "test", "catch": "T"}))
|
||||
.unwrap();
|
||||
assert_eq!(db.catch_count("s1", "test", "T").unwrap(), 1);
|
||||
assert!(db.reopen("s1").is_err(), "suspended");
|
||||
db.set_session("s1", "plan", "running").unwrap();
|
||||
assert!(db.events(Some("s9"), 0).unwrap().is_empty());
|
||||
// checks keep their last result; decisions list in insertion order
|
||||
let c = db.add_check("build", "true", Some("s1")).unwrap();
|
||||
|
||||
Reference in new issue
Block a user