loopflow 0.12.1

Run steps and flows with coding agents
Documentation
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, Instant};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

const OPENCODE_REGISTRY_FILE: &str = "runtime/opencode-servers.json";
const REAP_TERM_GRACE: Duration = Duration::from_secs(2);

/// Outcome of reaping orphaned opencode `serve` processes. Public so the
/// per-wave runtime can call [`reap_orphaned_opencode_servers`] at startup to
/// clear servers — and their descendant process trees — left behind by a
/// crashed `lf wave`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct OpenCodeReapReport {
    pub reaped: u32,
    pub errors: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct OpenCodeServerEntry {
    opencode_pid: u32,
    owner_loopflow_pid: u32,
}

pub(crate) fn register_opencode_server(opencode_pid: u32) -> Result<()> {
    register_opencode_server_at_path(&registry_path(), opencode_pid, std::process::id())
}

pub(crate) fn unregister_opencode_server(opencode_pid: u32) -> Result<()> {
    unregister_opencode_server_at_path(&registry_path(), opencode_pid)
}

pub fn reap_orphaned_opencode_servers() -> OpenCodeReapReport {
    reap_orphaned_opencode_servers_at_path(
        &registry_path(),
        pid_is_alive,
        classify_leader,
        process_group_alive,
        terminate_process_group,
    )
}

fn registry_path() -> PathBuf {
    crate::store::lf_home_dir().join(OPENCODE_REGISTRY_FILE)
}

fn register_opencode_server_at_path(
    path: &Path,
    opencode_pid: u32,
    owner_loopflow_pid: u32,
) -> Result<()> {
    let mut entries = read_registry_entries(path)?;
    entries.retain(|entry| entry.opencode_pid != opencode_pid);
    entries.push(OpenCodeServerEntry {
        opencode_pid,
        owner_loopflow_pid,
    });
    write_registry_entries(path, &entries)
}

fn unregister_opencode_server_at_path(path: &Path, opencode_pid: u32) -> Result<()> {
    let mut entries = read_registry_entries(path)?;
    let original_len = entries.len();
    entries.retain(|entry| entry.opencode_pid != opencode_pid);
    if entries.len() == original_len {
        return Ok(());
    }
    write_registry_entries(path, &entries)
}

/// How the leader PID of a registered OpenCode server presents itself when its
/// owner loopflow process is gone. The OpenCode harness spawns `opencode serve`
/// in its own process group (`process_group(0)`), so the registered pid is both
/// the leader and the process-group id; its descendants live in that group.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LeaderState {
    /// PID no longer exists.
    Dead,
    /// PID alive and its command is `opencode ... serve`.
    Opencode,
    /// PID alive but running something else — recycled into an unrelated process.
    Other,
}

fn reap_orphaned_opencode_servers_at_path(
    path: &Path,
    owner_pid_alive: impl Fn(u32) -> bool,
    leader: impl Fn(u32) -> LeaderState,
    group_alive: impl Fn(u32) -> bool,
    terminate_group: impl Fn(u32) -> bool,
) -> OpenCodeReapReport {
    let mut report = OpenCodeReapReport::default();
    let entries = match read_registry_entries(path) {
        Ok(entries) => entries,
        Err(err) => {
            tracing::warn!(path = %path.display(), error = %err, "failed to read OpenCode registry");
            report.errors += 1;
            return report;
        }
    };

    let mut retained = Vec::with_capacity(entries.len());
    for entry in entries {
        if owner_pid_alive(entry.owner_loopflow_pid) {
            retained.push(entry);
            continue;
        }

        // Decide whether to reap this entry's process group. The group id is
        // the registered pid (the harness makes the child its own group
        // leader), so killing the group reaps the server AND any descendants
        // it spawned — accurately, without touching unrelated processes.
        let reap_group = match leader(entry.opencode_pid) {
            LeaderState::Opencode => true,
            LeaderState::Dead => group_alive(entry.opencode_pid),
            LeaderState::Other => {
                tracing::info!(
                    opencode_pid = entry.opencode_pid,
                    "orphaned OpenCode pid reused by an unrelated process; leaving it"
                );
                false
            }
        };

        if reap_group {
            if terminate_group(entry.opencode_pid) {
                report.reaped += 1;
            } else {
                tracing::warn!(
                    opencode_pid = entry.opencode_pid,
                    owner_loopflow_pid = entry.owner_loopflow_pid,
                    "failed to terminate orphaned OpenCode process group"
                );
                report.errors += 1;
                retained.push(entry);
            }
        }
        // Falling through (Dead+empty group, or Other) prunes the entry: it is
        // not pushed to `retained`, so the registry drops it.
    }

    if let Err(err) = write_registry_entries(path, &retained) {
        tracing::warn!(
            path = %path.display(),
            error = %err,
            "failed to update OpenCode registry after orphan cleanup"
        );
        report.errors += 1;
    }

    report
}

fn read_registry_entries(path: &Path) -> Result<Vec<OpenCodeServerEntry>> {
    let content = match std::fs::read_to_string(path) {
        Ok(content) => content,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(err) => return Err(err.into()),
    };

    if content.trim().is_empty() {
        return Ok(Vec::new());
    }

    serde_json::from_str(&content)
        .with_context(|| format!("failed parsing OpenCode registry at {}", path.display()))
}

fn write_registry_entries(path: &Path, entries: &[OpenCodeServerEntry]) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("failed creating runtime dir {}", parent.display()))?;
    }

    let json = serde_json::to_string_pretty(entries)
        .context("failed serializing OpenCode server registry")?;
    std::fs::write(path, json)
        .with_context(|| format!("failed writing OpenCode registry at {}", path.display()))?;
    Ok(())
}

fn classify_leader(pid: u32) -> LeaderState {
    if !pid_is_alive(pid) {
        return LeaderState::Dead;
    }
    if process_looks_like_opencode_serve(pid) {
        LeaderState::Opencode
    } else {
        LeaderState::Other
    }
}

fn process_looks_like_opencode_serve(pid: u32) -> bool {
    let output = match Command::new("ps")
        .arg("-o")
        .arg("command=")
        .arg("-p")
        .arg(pid.to_string())
        .output()
    {
        Ok(output) => output,
        Err(_) => return false,
    };

    if !output.status.success() {
        return false;
    }

    let command = String::from_utf8_lossy(&output.stdout).to_ascii_lowercase();
    command.contains("opencode") && command.contains("serve")
}

#[cfg(unix)]
fn pid_is_alive(pid: u32) -> bool {
    if pid == 0 {
        return false;
    }
    let Ok(raw) = i32::try_from(pid) else {
        return false;
    };
    // SAFETY: signal 0 is an existence/permission probe; no pointers are used.
    let result = unsafe { libc::kill(raw, 0) };
    result == 0 || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
}

#[cfg(not(unix))]
fn pid_is_alive(pid: u32) -> bool {
    if pid == 0 {
        return false;
    }
    Command::new("kill")
        .arg("-0")
        .arg(pid.to_string())
        .status()
        .is_ok_and(|status| status.success())
}

/// Probe whether a process group still has any live member.
#[cfg(unix)]
fn process_group_alive(pgid: u32) -> bool {
    if pgid == 0 {
        return false;
    }
    let Ok(raw) = i32::try_from(pgid) else {
        return false;
    };
    // SAFETY: a negative pid targets the process group; signal 0 only probes.
    let result = unsafe { libc::kill(-raw, 0) };
    result == 0 || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
}

#[cfg(not(unix))]
fn process_group_alive(_pgid: u32) -> bool {
    false
}

/// Reap an orphaned OpenCode process group: SIGTERM (let the server close its
/// port and clean up its own children), wait, then SIGKILL the stragglers.
/// Returns true if the group has no live members afterwards.
#[cfg(unix)]
fn terminate_process_group(pgid: u32) -> bool {
    if pgid == 0 {
        return false;
    }
    if crate::engine::process::current_process_group_id() == Some(pgid) {
        tracing::warn!(pgid, "refusing to reap current process group");
        return false;
    }
    let Ok(raw) = i32::try_from(pgid) else {
        return false;
    };

    if signal_group(raw, libc::SIGTERM) {
        return true;
    }
    if group_gone(raw, REAP_TERM_GRACE) {
        return true;
    }
    signal_group(raw, libc::SIGKILL);
    group_gone(raw, REAP_TERM_GRACE)
}

#[cfg(not(unix))]
fn terminate_process_group(_pgid: u32) -> bool {
    false
}

#[cfg(unix)]
fn signal_group(pgid: i32, signal: libc::c_int) -> bool {
    // SAFETY: kill with a negative pid signals the process group; no pointers.
    let result = unsafe { libc::kill(-pgid, signal) };
    if result == 0 {
        return false;
    }
    let err = std::io::Error::last_os_error();
    if err.raw_os_error() == Some(libc::ESRCH) {
        return true;
    }
    tracing::warn!(pgid, signal, error = %err, "failed to signal OpenCode process group");
    false
}

#[cfg(unix)]
fn group_gone(pgid: i32, grace: Duration) -> bool {
    let deadline = Instant::now() + grace;
    loop {
        // SAFETY: signal 0 probes the process group; no pointers are used.
        let result = unsafe { libc::kill(-pgid, 0) };
        if result != 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH) {
            return true;
        }
        if Instant::now() >= deadline {
            return false;
        }
        std::thread::sleep(Duration::from_millis(50));
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;
    use std::sync::Mutex;

    use tempfile::tempdir;

    use super::*;

    fn registry_path(root: &Path) -> PathBuf {
        root.join("runtime").join("opencode-servers.json")
    }

    fn entry(opencode_pid: u32, owner_loopflow_pid: u32) -> OpenCodeServerEntry {
        OpenCodeServerEntry {
            opencode_pid,
            owner_loopflow_pid,
        }
    }

    #[test]
    fn register_and_unregister_opencode_server_updates_registry() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());

        register_opencode_server_at_path(&path, 111, 222).expect("register pid");
        let entries = read_registry_entries(&path).expect("read entries");
        assert_eq!(entries, vec![entry(111, 222)]);

        register_opencode_server_at_path(&path, 111, 444).expect("overwrite existing pid");
        let entries = read_registry_entries(&path).expect("read entries");
        assert_eq!(entries, vec![entry(111, 444)]);

        unregister_opencode_server_at_path(&path, 111).expect("unregister pid");
        let entries = read_registry_entries(&path).expect("read entries");
        assert!(entries.is_empty());
    }

    #[test]
    fn reap_kills_the_process_group_of_an_orphaned_opencode_server() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());
        // Owner 1 alive (retained); owner 2 dead, leader is a live opencode server.
        write_registry_entries(&path, &[entry(10, 1), entry(11, 2), entry(12, 2)])
            .expect("write registry");

        let owner_alive: HashSet<u32> = [1].into_iter().collect();
        let opencode_pids: HashSet<u32> = [11].into_iter().collect();
        let killed = Mutex::new(Vec::new());

        let report = reap_orphaned_opencode_servers_at_path(
            &path,
            |pid| owner_alive.contains(&pid),
            |pid| {
                if opencode_pids.contains(&pid) {
                    LeaderState::Opencode
                } else {
                    LeaderState::Dead
                }
            },
            |_| false,
            |pid| {
                killed.lock().expect("lock killed list").push(pid);
                true
            },
        );

        assert_eq!(
            report,
            OpenCodeReapReport {
                reaped: 1,
                errors: 0
            }
        );
        assert_eq!(*killed.lock().expect("lock killed list"), vec![11]);
        // Owner-alive entry retained; the dead leader at pid 12 pruned (no group).
        assert_eq!(
            read_registry_entries(&path).expect("read entries"),
            vec![entry(10, 1)]
        );
    }

    #[test]
    fn reap_kills_surviving_children_when_the_leader_is_dead() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());
        // Owner dead, leader pid gone, but children still live in its group.
        write_registry_entries(&path, &[entry(21, 2)]).expect("write registry");

        let alive_groups: HashSet<u32> = [21].into_iter().collect();
        let killed = Mutex::new(Vec::new());

        let report = reap_orphaned_opencode_servers_at_path(
            &path,
            |_| false,
            |_| LeaderState::Dead,
            |pgid| alive_groups.contains(&pgid),
            |pid| {
                killed.lock().expect("lock killed list").push(pid);
                true
            },
        );

        assert_eq!(
            report,
            OpenCodeReapReport {
                reaped: 1,
                errors: 0
            }
        );
        assert_eq!(*killed.lock().expect("lock killed list"), vec![21]);
        assert!(read_registry_entries(&path)
            .expect("read entries")
            .is_empty());
    }

    #[test]
    fn reap_prunes_a_fully_dead_tree_without_signalling() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());
        write_registry_entries(&path, &[entry(31, 2)]).expect("write registry");

        let killed = Mutex::new(Vec::new());
        let report = reap_orphaned_opencode_servers_at_path(
            &path,
            |_| false,
            |_| LeaderState::Dead,
            |_| false,
            |pid| {
                killed.lock().expect("lock killed list").push(pid);
                true
            },
        );

        assert_eq!(report, OpenCodeReapReport::default());
        assert!(killed.lock().expect("lock killed list").is_empty());
        assert!(read_registry_entries(&path)
            .expect("read entries")
            .is_empty());
    }

    #[test]
    fn reap_leaves_a_reused_pid_alone() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());
        // Owner dead, but the pid now runs an unrelated process.
        write_registry_entries(&path, &[entry(41, 2)]).expect("write registry");

        let killed = Mutex::new(Vec::new());
        let report = reap_orphaned_opencode_servers_at_path(
            &path,
            |_| false,
            |_| LeaderState::Other,
            |_| true,
            |pid| {
                killed.lock().expect("lock killed list").push(pid);
                true
            },
        );

        // Not reaped (would risk killing an unrelated group), entry pruned.
        assert_eq!(report, OpenCodeReapReport::default());
        assert!(killed.lock().expect("lock killed list").is_empty());
        assert!(read_registry_entries(&path)
            .expect("read entries")
            .is_empty());
    }

    #[test]
    fn reap_retains_an_entry_when_termination_fails() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());
        write_registry_entries(&path, &[entry(51, 2)]).expect("write registry");

        let report = reap_orphaned_opencode_servers_at_path(
            &path,
            |_| false,
            |_| LeaderState::Opencode,
            |_| true,
            |_| false,
        );

        assert_eq!(
            report,
            OpenCodeReapReport {
                reaped: 0,
                errors: 1
            }
        );
        assert_eq!(
            read_registry_entries(&path).expect("read entries"),
            vec![entry(51, 2)]
        );
    }

    #[test]
    fn reap_is_idempotent() {
        let tmp = tempdir().expect("tempdir");
        let path = registry_path(tmp.path());
        write_registry_entries(&path, &[entry(20, 2)]).expect("write registry");

        let first = reap_orphaned_opencode_servers_at_path(
            &path,
            |_| false,
            |pid| {
                if pid == 20 {
                    LeaderState::Opencode
                } else {
                    LeaderState::Dead
                }
            },
            |_| false,
            |_| true,
        );
        assert_eq!(
            first,
            OpenCodeReapReport {
                reaped: 1,
                errors: 0
            }
        );

        let second = reap_orphaned_opencode_servers_at_path(
            &path,
            |_| false,
            |pid| {
                if pid == 20 {
                    LeaderState::Opencode
                } else {
                    LeaderState::Dead
                }
            },
            |_| false,
            |_| true,
        );
        assert_eq!(second, OpenCodeReapReport::default());
    }
}