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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Background monitor daemon.
//!
//! Spawns a detached child process that *owns* the coding agent: it launches
//! the agent, captures its stdout and exit code into `.devflow/`, and — when
//! the agent exits — runs `devflow advance` to advance the stage machine.
//!
//! Owning the agent is the key fix over a CLI-scoped capture thread: because
//! the monitor outlives `devflow start`, the agent's stdout keeps flowing into
//! the capture file and its exit code is still reaped after the CLI exits.
//!
//! This is the core automation primitive — no cron, no scheduler,
//! no agent cooperation needed.
use crate::state::State;
use std::path::Path;
use std::process::{Command, Stdio};
use std::time::Duration;
use tracing::{debug, info};
/// Errors produced by monitor operations.
#[derive(Debug, thiserror::Error)]
pub enum MonitorError {
/// Spawning the monitor process failed.
#[error("failed to spawn monitor: {0}")]
Io(#[from] std::io::Error),
/// Project path is not valid UTF-8.
#[error("project path is not valid UTF-8")]
NonUtf8Path,
/// Could not determine the current executable path.
#[error("could not determine devflow binary path")]
NoBinaryPath,
}
/// Spawn a background monitor that owns the agent for the given workflow state.
///
/// The monitor is a detached shell process that:
/// 1. Launches the agent (`program` + `args`) with stdout redirected to the
/// phase stdout file, recording the agent PID to the agent-pid file
/// 2. Waits for the agent to exit and records its exit code to the exit file
/// 3. Runs `devflow advance --phase N` to advance the workflow through its
/// remaining stages
///
/// Returns the PID of the spawned monitor.
pub fn spawn_monitor(
state: &State,
program: &str,
args: &[String],
envs: &[(String, String)],
) -> Result<u32, MonitorError> {
spawn_monitor_inner(state, program, args, envs, true)
}
fn spawn_monitor_inner(
state: &State,
program: &str,
args: &[String],
envs: &[(String, String)],
run_advance: bool,
) -> Result<u32, MonitorError> {
let project_root = state
.project_root
.to_str()
.ok_or(MonitorError::NonUtf8Path)?;
let binary = std::env::current_exe()
.map_err(|_| MonitorError::NoBinaryPath)?
.to_str()
.ok_or(MonitorError::NonUtf8Path)?
.to_string();
info!(
"spawning monitor for phase {}: {program} {}",
state.phase,
args.join(" ")
);
let stdout_file = crate::agent_result::stdout_path(&state.project_root, state.phase);
let stderr_file = crate::agent_result::stderr_path(&state.project_root, state.phase);
let exit_file = crate::agent_result::exit_code_path(&state.project_root, state.phase);
let pid_file = crate::agent_result::agent_pid_path(&state.project_root, state.phase);
// Ensure the capture directory exists before the detached process runs.
if let Some(parent) = stdout_file.parent() {
crate::workflow::ensure_devflow_dir(parent)?;
}
let stdout_file = stdout_file.to_str().ok_or(MonitorError::NonUtf8Path)?;
let stderr_file = stderr_file.to_str().ok_or(MonitorError::NonUtf8Path)?;
let exit_file = exit_file.to_str().ok_or(MonitorError::NonUtf8Path)?;
let pid_file = pid_file.to_str().ok_or(MonitorError::NonUtf8Path)?;
// The agent runs in its worktree when worktree mode is active; otherwise it
// runs in the project root. Capture/state files and the `devflow check`
// calls below always use the main project root, regardless of cwd.
let workdir = state
.worktree_path
.as_deref()
.unwrap_or(&state.project_root)
.to_str()
.ok_or(MonitorError::NonUtf8Path)?;
// Shell script that launches the agent in the background, captures its
// stdout and exit code, then advances the workflow. Because this process
// is the agent's parent, capture survives the CLI exiting.
//
// stderr is captured to a separate file so it cannot corrupt the (possibly
// JSON) stdout capture that DevFlow parses for DEVFLOW_RESULT. Inspect
// .devflow/phase-NN-stderr.log for agent error output on failures.
//
// `devflow advance --phase N` evaluates the agent result, moves the stage
// machine forward, and (for an agent stage) spawns the next monitor
// itself. The phase is recorded here at spawn time so advance's identity
// never depends on a shared state singleton (13-DEFERRED-CR-03): under
// `devflow parallel`, each phase's monitor advances exactly its own
// stage machine.
//
// Traps SIGTERM and SIGINT for clean shutdown. WR-08 (13-REVIEW.md):
// the trap must also kill the backgrounded agent ($apid) — previously
// it only exited the monitor shell itself, orphaning the agent so it
// kept running/committing unsupervised with nothing left to call
// `devflow advance` once it finished. `apid` is initialized to empty
// before the trap is installed so a signal arriving before the agent is
// even backgrounded doesn't reference an unset variable.
let advance_tail = if run_advance {
format!(
"; {binary} advance {project_root} --phase {phase}",
binary = shell_escape(&binary),
project_root = shell_escape(project_root),
phase = state.phase,
)
} else {
String::new()
};
let script = format!(
"apid=''; cleanup() {{ [ -n \"$apid\" ] && kill \"$apid\" 2>/dev/null; exit 0; }}; \
trap cleanup TERM INT; \
cd {workdir} || exit 1; \
\"$@\" > {stdout_file} 2>{stderr_file} & \
apid=$!; echo $apid > {pid_file}; \
wait $apid; echo $? > {exit_file}{advance_tail}",
workdir = shell_escape(workdir),
stdout_file = shell_escape(stdout_file),
stderr_file = shell_escape(stderr_file),
exit_file = shell_escape(exit_file),
pid_file = shell_escape(pid_file),
);
let child = Command::new("sh")
.arg("-c")
.arg(&script)
.arg("sh")
.arg(program)
.args(args)
// Adapter-scoped env (e.g. Codex's unsigned-commit override) rides
// the whole monitor chain: sh → agent → its git children (13-06).
.envs(envs.iter().map(|(k, v)| (k.as_str(), v.as_str())))
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()?;
let pid = child.id();
info!("monitor spawned with pid {pid}");
Ok(pid)
}
/// Poll for the agent PID that the monitor records, for up to ~1 second.
///
/// Returns the PID once the monitor has launched the agent, or `None` if it
/// does not appear in time (the monitor still runs; only the display PID is lost).
pub fn wait_for_agent_pid(project_root: &Path, phase: u32) -> Option<u32> {
let path = crate::agent_result::agent_pid_path(project_root, phase);
debug!("polling for agent PID for phase {phase}");
for _ in 0..50 {
if let Ok(contents) = std::fs::read_to_string(&path)
&& let Ok(pid) = contents.trim().parse::<u32>()
{
return Some(pid);
}
std::thread::sleep(Duration::from_millis(20));
}
debug!("agent PID not found for phase {phase} after polling");
None
}
/// Escape a string for safe use in a single-quoted shell context.
fn shell_escape(s: &str) -> String {
format!("'{}'", s.replace('\'', "'\\''"))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mode::Mode;
use crate::stage::Stage;
use crate::state::{AgentKind, State};
fn state_in(root: &Path) -> State {
let mut state = State::new(4, AgentKind::Claude, Mode::Auto, root.to_path_buf());
state.stage = Stage::Code;
state
}
#[test]
fn shell_escape_wraps_basic_strings() {
assert_eq!(shell_escape("hello"), "'hello'");
assert_eq!(shell_escape("hello world"), "'hello world'");
assert_eq!(shell_escape("/tmp/devflow"), "'/tmp/devflow'");
}
#[test]
fn shell_escape_handles_single_quotes() {
assert_eq!(shell_escape("can't"), "'can'\\''t'");
assert_eq!(shell_escape("a'b'c"), "'a'\\''b'\\''c'");
}
#[test]
fn shell_escape_handles_empty_string() {
assert_eq!(shell_escape(""), "''");
}
#[test]
fn wait_for_agent_pid_returns_pid_when_file_exists() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join(".devflow")).unwrap();
std::fs::write(
crate::agent_result::agent_pid_path(dir.path(), 4),
"12345\n",
)
.unwrap();
assert_eq!(wait_for_agent_pid(dir.path(), 4), Some(12345));
}
#[test]
fn wait_for_agent_pid_returns_none_when_file_missing() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(wait_for_agent_pid(dir.path(), 4), None);
}
#[test]
fn wait_for_agent_pid_returns_none_for_garbage_content() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join(".devflow")).unwrap();
std::fs::write(
crate::agent_result::agent_pid_path(dir.path(), 4),
"not-a-pid",
)
.unwrap();
assert_eq!(wait_for_agent_pid(dir.path(), 4), None);
}
#[test]
fn spawn_monitor_captures_agent_pid_and_output() {
let dir = tempfile::tempdir().unwrap();
let state = state_in(dir.path());
// Stub agent: write a known marker to stdout, then exit cleanly.
let args = vec!["-c".to_string(), "echo MONITOR_READY".to_string()];
let monitor_pid = spawn_monitor(&state, "sh", &args, &[]).unwrap();
assert!(monitor_pid > 0);
// Observable side effect #1: the monitor records the agent PID to its
// pid file with valid numeric content.
let agent_pid = wait_for_agent_pid(dir.path(), state.phase)
.expect("monitor should record the agent pid");
assert!(agent_pid > 0);
// Observable side effect #2: the agent's stdout is captured to the
// phase stdout file (proving the monitor actually ran the agent).
let stdout_path = crate::agent_result::stdout_path(dir.path(), state.phase);
let mut captured = String::new();
for _ in 0..100 {
if let Ok(contents) = std::fs::read_to_string(&stdout_path)
&& contents.contains("MONITOR_READY")
{
captured = contents;
break;
}
std::thread::sleep(Duration::from_millis(20));
}
assert!(
captured.contains("MONITOR_READY"),
"expected MONITOR_READY in captured stdout, got {captured:?}"
);
}
/// WR-08 (13-REVIEW.md): sending SIGTERM/SIGINT to the monitor must also
/// terminate the agent it owns. Before the fix, `cleanup()` only exited
/// the monitor shell, leaving the agent orphaned and running/committing
/// unsupervised with nothing left to call `devflow advance` for it.
/// A one-line identity/state summary of a pid, for failure diagnostics.
/// `Name`/`State`/`PPid` come from `/proc/<pid>/status`; the cmdline
/// distinguishes a shell that exec'd its command from one that forked it.
/// Test-only; never used in a decision.
fn proc_snapshot(pid: u32) -> String {
let Ok(status) = std::fs::read_to_string(format!("/proc/{pid}/status")) else {
return format!("GONE (no /proc/{pid})");
};
let field = |key: &str| {
status
.lines()
.find(|l| l.starts_with(key))
.map(|l| l.split_whitespace().skip(1).collect::<Vec<_>>().join(" "))
.unwrap_or_else(|| "?".into())
};
let cmdline = std::fs::read(format!("/proc/{pid}/cmdline"))
.map(|raw| {
let joined = raw
.split(|&b| b == 0)
.filter(|a| !a.is_empty())
.map(|a| String::from_utf8_lossy(a).into_owned())
.collect::<Vec<_>>()
.join(" ");
if joined.is_empty() {
"<empty>".to_string()
} else {
joined
}
})
.unwrap_or_else(|e| format!("<unreadable: {e}>"));
format!(
"ALIVE Name={} State={} PPid={} cmdline=[{cmdline}]",
field("Name:"),
field("State:"),
field("PPid:")
)
}
#[test]
fn sigterm_to_monitor_also_kills_the_agent() {
let dir = tempfile::tempdir().unwrap();
let state = state_in(dir.path());
// Stub agent that runs long enough to observe: sleeps well past the
// window this test needs to send SIGTERM and check liveness.
let args = vec!["-c".to_string(), "sleep 30".to_string()];
let monitor_pid = spawn_monitor(&state, "sh", &args, &[]).unwrap();
let agent_pid = wait_for_agent_pid(dir.path(), state.phase)
.expect("monitor should record the agent pid");
assert!(
crate::agent::agent_running(agent_pid),
"agent should be running before SIGTERM"
);
// Snapshot both processes before signalling. This assertion fails in
// containerised CI and cannot be reproduced locally, and a bare
// "still running" message discards everything that could explain it
// — the same antipattern that made 999.47 expensive to diagnose.
let monitor_before = proc_snapshot(monitor_pid);
let agent_before = proc_snapshot(agent_pid);
// SIGTERM the monitor, as an operator (or lock.rs's stale-holder
// reclaim path) would to abort a run.
let kill_rc = unsafe { libc::kill(monitor_pid as libc::pid_t, libc::SIGTERM) };
let kill_err = if kill_rc == 0 {
"ok".to_string()
} else {
format!("errno {}", std::io::Error::last_os_error())
};
// The agent should be killed promptly by the monitor's trap —
// poll rather than sleep a fixed amount to keep this fast and
// avoid flaking under load. (Window widened to 5s: at 2s this
// still flaked under a fully parallel workspace test run.)
//
// 2026-07-26: this was widened 5s -> 15s for the containerised CI
// job and STILL failed, then reverted to 5s. That widening was a
// mistake: 15s is far beyond any plausible trap-and-kill latency,
// so the agent is not being reaped SLOWLY, it is not being reaped.
// Buying silence with a bigger number would have hidden a real
// defect behind a green check — the exact false negative this
// repository keeps getting bitten by.
//
// The trap mechanism itself is verified working: DevFlow's real
// monitor script shape was run under both `bash` and `dash` (the
// container's /bin/sh is dash, the Fedora host's is bash) and both
// killed the backgrounded agent correctly. So the defect is in how
// the agent is spawned or identified under container timing, not in
// the shell trap — see 999.47, whose confirmed transient fork/exec
// window is the prime suspect for the same class of failure here.
//
// Leave this red until that is fixed. Do NOT widen it again.
let mut still_running = true;
for _ in 0..250 {
if !crate::agent::agent_running(agent_pid) {
still_running = false;
break;
}
std::thread::sleep(Duration::from_millis(20));
}
let monitor_after = proc_snapshot(monitor_pid);
let agent_after = proc_snapshot(agent_pid);
let pidfile =
std::fs::read_to_string(crate::agent_result::agent_pid_path(dir.path(), state.phase))
.unwrap_or_else(|e| format!("<unreadable: {e}>"));
assert!(
!still_running,
"agent (pid {agent_pid}) was orphaned — still running after monitor SIGTERM\n\
\x20 monitor pid: {monitor_pid}\n\
\x20 kill(TERM) rc: {kill_rc} ({kill_err})\n\
\x20 monitor before: {monitor_before}\n\
\x20 monitor after: {monitor_after}\n\
\x20 agent pid: {agent_pid}\n\
\x20 agent before: {agent_before}\n\
\x20 agent after: {agent_after}\n\
\x20 pidfile contents: {}\n\
Read the monitor's `after` line first. GONE means the shell died \
without running its trap — most likely SIGTERM arrived before \
`trap` was installed, or it was killed rather than handling the \
signal, either way leaving the agent unreaped. STILL ALIVE means \
the trap never fired or `kill $apid` failed, so compare the agent \
pid against the pidfile and check the agent's PPid: if PPid is not \
the monitor, `$!` did not name the process we are polling. If the \
agent's Name is `sh` rather than `sleep`, the agent shell forked \
rather than exec'd, so killing it leaves its own child behind.",
pidfile.trim()
);
}
#[test]
fn spawn_monitor_runs_agent_in_worktree_but_captures_in_project_root() {
let dir = tempfile::tempdir().unwrap();
let worktree = dir.path().join(".worktrees/phase-04");
std::fs::create_dir_all(&worktree).unwrap();
let mut state = state_in(dir.path());
state.worktree_path = Some(worktree.clone());
// Stub agent: print its cwd so the test proves the monitor changed
// directories before launching the agent.
let args = vec!["-c".to_string(), "pwd; echo WORKTREE_READY".to_string()];
let monitor_pid = spawn_monitor(&state, "sh", &args, &[]).unwrap();
assert!(monitor_pid > 0);
let agent_pid = wait_for_agent_pid(dir.path(), state.phase)
.expect("monitor should record the agent pid in the main project");
assert!(agent_pid > 0);
let stdout_path = crate::agent_result::stdout_path(dir.path(), state.phase);
let mut captured = String::new();
for _ in 0..100 {
if let Ok(contents) = std::fs::read_to_string(&stdout_path)
&& contents.contains("WORKTREE_READY")
{
captured = contents;
break;
}
std::thread::sleep(Duration::from_millis(20));
}
assert!(
captured.contains(&worktree.display().to_string()),
"agent did not run in worktree cwd; captured stdout: {captured:?}"
);
assert!(
stdout_path.exists(),
"stdout capture missing in main .devflow"
);
assert!(
!crate::agent_result::stdout_path(&worktree, state.phase).exists(),
"stdout capture should not be written under the worktree"
);
}
#[test]
fn spawn_monitor_treats_agent_args_as_literal_argv() {
let dir = tempfile::tempdir().unwrap();
let state = state_in(dir.path());
let payload = "value; touch INJECTED";
let args = vec![
"-c".to_string(),
"printf '%s\\n' \"$0\"; echo ARGV_SAFE".to_string(),
payload.to_string(),
];
spawn_monitor(&state, "sh", &args, &[]).unwrap();
wait_for_agent_pid(dir.path(), state.phase).expect("monitor should record the agent pid");
let stdout_path = crate::agent_result::stdout_path(dir.path(), state.phase);
let mut captured = String::new();
for _ in 0..100 {
if let Ok(contents) = std::fs::read_to_string(&stdout_path)
&& contents.contains("ARGV_SAFE")
{
captured = contents;
break;
}
std::thread::sleep(Duration::from_millis(20));
}
assert!(
captured.contains(payload),
"literal argv missing: {captured:?}"
);
assert!(captured.contains("ARGV_SAFE"));
assert!(!dir.path().join("INJECTED").exists());
}
}