agentmux_srv/drone/replay.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Replay from a step (docs/specs/SPEC_DRONE_GENERAL_AUTOMATION_2026_11_11.md ยง3.3, "Replay and time travel").
5//!
6//! A replay is a **new run** that starts from checkpoint N of an earlier run, on the same graph
7//! version, with the same state. It does not repeat the world: a block that sends, files, starts or
8//! writes is **stubbed**, and says what it said in the run it replays, unless the person asks for it
9//! "for real". Blocks that only read run as they do.
10//!
11//! This module is the part that needs no engine: the new run's first checkpoint, and what a stub says.
12
13use std::collections::BTreeMap;
14
15use serde_json::{json, Value};
16
17use crate::drone::checkpoint::{Checkpoint, Fork};
18
19/// What a replaying run is told: where it came from, and what its stubbed blocks say.
20#[derive(Debug, Clone, PartialEq)]
21pub struct Replay {
22 pub fork: Fork,
23 /// What each block said in the run replayed, by block id (its newest output).
24 pub original: BTreeMap<String, Value>,
25 /// The taint bits each block's output had there, which a stub keeps: what the block said is as
26 /// clean or as tainted as it was.
27 pub bits: BTreeMap<String, (bool, bool)>,
28}
29
30impl Replay {
31 /// What a stubbed block comes to: what it said there, or, when it failed there and its graph
32 /// handled the failure, that failure again, so the replay takes the same road out of it.
33 pub fn said(&self, block_id: &str) -> Result<Value, String> {
34 if let Some(Value::Object(said)) = self.original.get(block_id) {
35 if said.get("failed") == Some(&json!(true)) {
36 let error = said.get("error").and_then(Value::as_str).unwrap_or("it failed");
37 // The block's end wraps an error as `block <id> failed: ...`; that wrapper is put back there.
38 let wrapper = format!("block {block_id} failed: ");
39 return Err(error.strip_prefix(wrapper.as_str()).unwrap_or(error).to_string());
40 }
41 }
42 Ok(self.stub(block_id))
43 }
44
45 /// What a stubbed block says: what it said in the run replayed, marked `stubbed`, without what
46 /// it cost there (nothing ran, so the replay spends nothing); a block that said nothing there
47 /// (it had not run, or failed) says only that it was stubbed.
48 pub fn stub(&self, block_id: &str) -> Value {
49 match self.original.get(block_id) {
50 Some(Value::Object(said)) => {
51 let mut said = said.clone();
52 said.remove("history");
53 said.remove("cost_usd");
54 said.insert("stubbed".to_string(), json!(true));
55 Value::Object(said)
56 }
57 Some(other) => json!({ "stubbed": true, "value": other }),
58 None => json!({ "stubbed": true }),
59 }
60 }
61}
62
63/// The first checkpoint of the run that replays from `at`, the checkpoint of an earlier run:
64/// the same state, position and graph, under a new run id, with nothing spent, no time gone, and
65/// no wait still pending. A block that was running or waiting at `at` is ready again: the new
66/// run starts it from its start.
67pub fn fork_checkpoint(at: &Checkpoint, run_id: &str, fork: Fork) -> Checkpoint {
68 let mut c = at.clone();
69 c.run_id = run_id.to_string();
70 c.step = 0;
71 c.fork = Some(fork);
72 let running = std::mem::take(&mut c.frontier.running);
73 let waiting = std::mem::take(&mut c.frontier.waiting);
74 let ready = std::mem::take(&mut c.frontier.ready);
75 let mut all = Vec::new();
76 for id in running.into_iter().chain(waiting).chain(ready) {
77 if !all.contains(&id) {
78 all.push(id);
79 }
80 }
81 c.frontier.ready = all;
82 c.deadlines.clear();
83 c.resume_on.clear();
84 c.wait_timeouts.clear();
85 c.cost_usd = 0.0;
86 c.elapsed_ms = 0;
87 c.position.spent_by.clear();
88 c.position.loop_elapsed_ms.clear();
89 c
90}
91
92#[cfg(test)]
93#[path = "replay_tests.rs"]
94mod tests;