1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! DevFlow state machine.
//!
//! Drives the development workflow through a single linear chain of five stages:
//! Define → Plan → Code → Validate → Ship. See [`crate::stage::Stage`].
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::mode::Mode;
use crate::stage::Stage;
/// Full workflow state persisted to `.devflow/state.json`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct State {
/// Current workflow stage.
pub stage: Stage,
/// Phase number being worked on.
pub phase: u32,
/// Which coding agent was launched.
pub agent: AgentKind,
/// How the pipeline is driven (auto vs. supervise).
pub mode: Mode,
/// Whether a gate has been written and is awaiting a human response.
#[serde(default)]
pub gate_pending: bool,
/// Consecutive Validate failures — drives the Auto-mode forced gate after
/// [`crate::mode::MAX_CONSECUTIVE_FAILURES`] failures. Persisted across
/// `devflow advance` invocations so the counter survives monitor restarts.
#[serde(default)]
pub consecutive_failures: u32,
/// Consecutive infrastructure-class faults (`ResourceKilled`,
/// `AgentUnavailable`) — distinct from [`Self::consecutive_failures`]
/// (D-08, 17-01). Gates at [`crate::mode::MAX_INFRA_FAILURES`]. Any
/// increment (wired in Plan 04) must use `saturating_add` so a
/// long-running stuck loop cannot overflow `u32`. A serde-absent value
/// (older persisted state) defaults to 0. Reset to 0 on every successful
/// stage transition, alongside `consecutive_failures` (CR-01, 17-06 gap
/// closure), so the ceiling bounds a stuck loop, not a phase's lifetime.
#[serde(default)]
pub infra_failures: u32,
/// How many times a preflight gate has been resolved and retried for
/// this phase (18f). Bounded by [`crate::mode::MAX_PREFLIGHT_RETRIES`].
/// Persisted rather than recursion-scoped because the documented wedge
/// spanned separate `devflow` invocations after a monitor death — an
/// in-process recursion-depth counter would reset to zero on every new
/// process and fail to bound the exact incident it exists to prevent.
/// Reset to 0 whenever preflight passes and whenever a human explicitly
/// approves (`GateAction::Advance`), both inside `run_preflight`. Unlike
/// [`Self::consecutive_failures`] and [`Self::infra_failures`], this
/// counter is NOT touched by `transition()`.
#[serde(default)]
pub preflight_retries: u32,
/// When the phase started (Unix seconds).
pub started_at: String,
/// Path to the project root.
pub project_root: PathBuf,
/// Working directory for the agent when running in a git worktree.
///
/// `None` means the agent runs in `project_root`. State and capture files
/// always live under the main `project_root`; only the agent's cwd changes.
#[serde(default)]
pub worktree_path: Option<PathBuf>,
/// PID of the detached monitor process that owns the agent for the
/// current stage, recorded by `launch_stage` at spawn time. `None` means
/// no monitor has been spawned for this state yet, OR the state was
/// written by a binary predating this field — in both cases the
/// liveness probe reports Unknown, never Stuck.
#[serde(default)]
pub monitor_pid: Option<u32>,
}
/// Supported coding agents.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AgentKind {
/// Anthropic Claude Code CLI.
Claude,
/// OpenAI Codex CLI.
Codex,
/// OpenCode CLI.
OpenCode,
}
impl fmt::Display for AgentKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
AgentKind::Claude => "claude",
AgentKind::Codex => "codex",
AgentKind::OpenCode => "opencode",
};
f.write_str(name)
}
}
impl FromStr for AgentKind {
type Err = AgentParseError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value.to_ascii_lowercase().as_str() {
"claude" => Ok(AgentKind::Claude),
"codex" => Ok(AgentKind::Codex),
"opencode" | "open-code" => Ok(AgentKind::OpenCode),
other => Err(AgentParseError(other.to_string())),
}
}
}
/// Error returned when parsing an unsupported agent name.
#[derive(Debug, Clone, thiserror::Error)]
#[error("unsupported agent `{0}`; expected claude, codex, or opencode")]
pub struct AgentParseError(String);
impl State {
/// Create a new state for starting a phase at the [`Stage::Define`] stage.
pub fn new(phase: u32, agent: AgentKind, mode: Mode, project_root: PathBuf) -> Self {
State {
stage: Stage::Define,
phase,
agent,
mode,
gate_pending: false,
consecutive_failures: 0,
infra_failures: 0,
preflight_retries: 0,
started_at: timestamp_now(),
project_root,
worktree_path: None,
monitor_pid: None,
}
}
}
fn timestamp_now() -> String {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(duration) => format!("{}", duration.as_secs()),
Err(_) => String::from("0"),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn agent_name_and_display() {
use crate::agents::adapter_for;
assert_eq!(adapter_for(AgentKind::Claude).name(), "Claude Code");
assert_eq!(adapter_for(AgentKind::Codex).name(), "OpenAI Codex");
assert_eq!(adapter_for(AgentKind::OpenCode).name(), "OpenCode");
assert_eq!(AgentKind::Claude.to_string(), "claude");
assert_eq!(AgentKind::Codex.to_string(), "codex");
assert_eq!(AgentKind::OpenCode.to_string(), "opencode");
}
#[test]
fn agent_from_str_accepts_canonical_and_aliases() {
assert_eq!("claude".parse::<AgentKind>().unwrap(), AgentKind::Claude);
assert_eq!("CLAUDE".parse::<AgentKind>().unwrap(), AgentKind::Claude);
assert_eq!("codex".parse::<AgentKind>().unwrap(), AgentKind::Codex);
assert_eq!(
"opencode".parse::<AgentKind>().unwrap(),
AgentKind::OpenCode
);
assert_eq!(
"open-code".parse::<AgentKind>().unwrap(),
AgentKind::OpenCode
);
}
#[test]
fn agent_from_str_rejects_unknown() {
let err = "aider".parse::<AgentKind>().unwrap_err();
assert!(err.to_string().contains("aider"));
}
#[test]
fn new_state_starts_at_define() {
let state = State::new(2, AgentKind::Claude, Mode::Auto, PathBuf::from("/repo"));
assert_eq!(state.stage, Stage::Define);
assert_eq!(state.phase, 2);
assert_eq!(state.agent, AgentKind::Claude);
assert_eq!(state.mode, Mode::Auto);
assert!(!state.gate_pending);
assert_eq!(state.consecutive_failures, 0);
assert_eq!(state.infra_failures, 0);
assert_eq!(state.preflight_retries, 0);
assert!(!state.started_at.is_empty());
assert_eq!(state.monitor_pid, None);
}
#[test]
fn state_serde_round_trips() {
let state = State::new(9, AgentKind::Codex, Mode::Supervise, PathBuf::from("/repo"));
let json = serde_json::to_string(&state).unwrap();
let back: State = serde_json::from_str(&json).unwrap();
assert_eq!(back.phase, 9);
assert_eq!(back.agent, AgentKind::Codex);
assert_eq!(back.stage, Stage::Define);
assert_eq!(back.mode, Mode::Supervise);
}
#[test]
fn consecutive_failures_persists_across_advance_calls() {
let mut state = State::new(1, AgentKind::Claude, Mode::Auto, PathBuf::from("/repo"));
state.consecutive_failures = 3;
let json = serde_json::to_string(&state).unwrap();
assert!(
json.contains("consecutive_failures"),
"consecutive_failures must appear in persisted JSON"
);
let loaded: State = serde_json::from_str(&json).unwrap();
assert_eq!(
loaded.consecutive_failures, 3,
"consecutive_failures must round-trip through serde"
);
}
/// D-08 (17-01): a distinct infra-failure counter round-trips through
/// serde and its own key appears in the persisted JSON.
#[test]
fn infra_failures_round_trips_through_serde() {
let mut state = State::new(1, AgentKind::Claude, Mode::Auto, PathBuf::from("/repo"));
state.infra_failures = 4;
let json = serde_json::to_string(&state).unwrap();
assert!(
json.contains("infra_failures"),
"infra_failures must appear in persisted JSON"
);
let loaded: State = serde_json::from_str(&json).unwrap();
assert_eq!(
loaded.infra_failures, 4,
"infra_failures must round-trip through serde"
);
}
/// A serde-absent `infra_failures` (older persisted state.json without
/// the field) must default to 0, not fail to deserialize.
#[test]
fn infra_failures_absent_from_json_defaults_to_zero() {
let json = r#"{
"stage": "code",
"phase": 1,
"agent": "claude",
"mode": "auto",
"started_at": "0",
"project_root": "/repo"
}"#;
let loaded: State = serde_json::from_str(json).unwrap();
assert_eq!(loaded.infra_failures, 0);
}
/// D-18f: `preflight_retries` round-trips through serde (its own key
/// appears in the persisted JSON) — the wedge this counter bounds spans
/// separate `devflow` invocations, so it must survive a save/load
/// cycle, not just live in memory — and a serde-absent value (state
/// written by a pre-18f binary) deserializes to 0, not a hard error.
#[test]
fn preflight_retries_round_trips_through_serde() {
let mut state = State::new(1, AgentKind::Claude, Mode::Auto, PathBuf::from("/repo"));
state.preflight_retries = 2;
let json = serde_json::to_string(&state).unwrap();
assert!(
json.contains("preflight_retries"),
"preflight_retries must appear in persisted JSON"
);
let loaded: State = serde_json::from_str(&json).unwrap();
assert_eq!(
loaded.preflight_retries, 2,
"preflight_retries must round-trip through serde"
);
let absent_json = r#"{
"stage": "code",
"phase": 1,
"agent": "claude",
"mode": "auto",
"started_at": "0",
"project_root": "/repo"
}"#;
let loaded_absent: State = serde_json::from_str(absent_json).unwrap();
assert_eq!(loaded_absent.preflight_retries, 0);
}
/// `monitor_pid` round-trips through serde as an exact `u32` (18b).
#[test]
fn monitor_pid_round_trips_through_serde() {
let mut state = State::new(1, AgentKind::Claude, Mode::Auto, PathBuf::from("/repo"));
state.monitor_pid = Some(4242);
let json = serde_json::to_string(&state).unwrap();
assert!(
json.contains("monitor_pid"),
"monitor_pid must appear in persisted JSON"
);
let loaded: State = serde_json::from_str(&json).unwrap();
assert_eq!(
loaded.monitor_pid,
Some(4242),
"monitor_pid must round-trip through serde"
);
}
/// A serde-absent `monitor_pid` (state written by a pre-18b binary) must
/// deserialize to `None`, not `Some(0)` — a `Some(0)` default would let a
/// pre-18b state file render as a monitor at pid 0.
#[test]
fn monitor_pid_absent_from_json_defaults_to_none() {
let json = r#"{
"stage": "code",
"phase": 1,
"agent": "claude",
"mode": "auto",
"started_at": "0",
"project_root": "/repo"
}"#;
let loaded: State = serde_json::from_str(json).unwrap();
assert_eq!(loaded.monitor_pid, None);
}
}