Kill and resume a workflow
Durability is easy to claim and hard to prove, so this example proves it. The kill_resume example starts a three-tool workflow in a SQLite file and kills the worker process with SIGKILL in the middle of the second tool. Then it resumes the run in a new process and compares the result with a run that was never interrupted. It exits 0 only when the two match.
Run it
cargo run -p troupekit --example kill_resume
You need the sqlite feature, which is on by default, and no model or key, because every node is a tool. The example prints five numbered lines, ending with 5. same as an uninterrupted run: true.
The workflow
Three tool nodes in a line, each reading the state the one before it wrote:
name = "three"
start = "first"
[state]
goal = "string"
a = "string"
b = "string"
c = "string"
[[nodes]]
id = "first"
kind = "tool"
tool = "first"
reads = ["goal"]
writes = "a"
[[nodes]]
id = "second"
kind = "tool"
tool = "second"
reads = ["a"]
writes = "b"
[[nodes]]
id = "third"
kind = "tool"
tool = "third"
reads = ["b"]
writes = "c"
[[nodes]]
id = "done"
kind = "end"
[[edges]]
from = "first"
to = "second"
[[edges]]
from = "second"
to = "third"
[[edges]]
from = "third"
to = "done"
Start and enqueue
We parse the workflow, create the run with its input and put it on the job queue:
#![allow(unused)]
fn main() {
async fn start(troupe: &Troupe) -> RunId {
let wf = Workflow::parse("three.toml", WORKFLOW).expect("valid workflow");
let input: Map<String, Value> = [("goal".to_string(), json!("survive"))]
.into_iter()
.collect();
let io = io(troupe, false);
let run = GraphRun::new(wf, input, Limits::default(), io.as_ref())
.await
.expect("run");
troupe.runner(io).enqueue(&run).await.expect("enqueue");
run.run_id()
}
}
Work until done
A worker opens the same file, builds a runner with a 200 ms lease and ticks until the job is done. The short lease matters: when a worker dies, its job becomes free again once the lease runs out.
#![allow(unused)]
fn main() {
async fn work(path: &Path, id: RunId, gate: bool) {
let troupe = Troupe::builder().sqlite(path).build().await.expect("open");
let runner = troupe.runner(io(&troupe, gate)).with_lease_ms(200);
let store = SqliteStore::open(path).await.expect("open");
let start = Instant::now();
while store.job(id).await.expect("job") != Some(JobState::Done) {
assert!(start.elapsed() < DEADLINE, "worker ran past its deadline");
runner.tick(TickOptions::default()).await.expect("tick");
tokio::time::sleep(Duration::from_millis(10)).await;
}
}
}
What it proves
Here is what happens, in order:
- The parent process creates the run and spawns a worker.
- The worker's second tool writes a marker blob and then never answers. The parent sees the marker and kills the worker.
- The killed worker's lease expires. A second worker claims the job, steps on from the latest checkpoint and finishes.
- The parent runs the same workflow uninterrupted in another file and compares state, steps and usage.
The second tool runs twice in this story: once in the killed worker and once after the resume. That is safe here, but a real tool might send an email. Each tool call gets an idempotency_key that stays the same when a step replays, so a tool with side effects can recognise the replay. Tools explains it.