indra 0.12.1

Run, watch and orchestrate AI coding agents (Claude Code, Codex, Gemini, Ollama) across machines: a web console, workflows with conditions and loops, boards, an MCP server and graph memory, in one binary.
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
//! End-to-end orchestration over the SSH transport. A stand-in `ssh` drops
//! the options and destination and runs the remote command through `sh -c`,
//! exactly as sshd would, so the whole path is real: hub -> ssh -> remote
//! `indra node exec` -> runner. Unix only (the stand-in is a shell script).
#![cfg(unix)]

use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

const INDRA: &str = env!("CARGO_BIN_EXE_indra");

struct Env {
    dir: PathBuf,
}

impl Env {
    fn new(name: &str) -> Env {
        let dir = std::env::temp_dir().join(format!("indra-orch-{}-{}", std::process::id(), name));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(dir.join("remote-api")).unwrap();
        fs::create_dir_all(dir.join("local-web")).unwrap();
        let ssh = dir.join("fake-ssh");
        fs::write(&ssh, "#!/bin/sh\nwhile [ \"$#\" -gt 0 ]; do if [ \"$1\" = \"--\" ]; then shift; break; fi; shift; done\nexec sh -c \"$*\"\n").unwrap();
        fs::set_permissions(&ssh, fs::Permissions::from_mode(0o755)).unwrap();
        fs::write(
            dir.join("remote.toml"),
            "builtins = false\n\
             [[runner]]\nname = \"echo\"\ncommand = \"echo\"\nargs = [\"remote says: {prompt}\"]\n\
             [[runner]]\nname = \"fail\"\ncommand = \"false\"\nargs = [\"{prompt}\"]\n\
             [[runner]]\nname = \"hold\"\ncommand = \"sh\"\nargs = [\"-c\", \"echo $$ > \\\"$0\\\"; exec sleep 600\", \"{prompt}\"]\n",
        )
        .unwrap();
        Env { dir }
    }

    fn p(&self, rel: &str) -> String {
        self.dir.join(rel).display().to_string()
    }

    /// The hub config: a local echo runner, the `box` node, and `workflows`.
    fn hub(&self, workflows: &str) -> PathBuf {
        let cfg = format!(
            "builtins = false\n\
             [local]\nmax_parallel = 2\n\
             [[runner]]\nname = \"echo\"\ncommand = \"echo\"\nargs = [\"local says: {{prompt}}\"]\n\
             [[node]]\nname = \"box\"\nssh = \"me@box.internal\"\nssh_command = \"{ssh}\"\nindra = \"{indra}\"\n\
             args = [\"--config\", \"{remote}\"]\ntargets = [\"{api}\"]\n{workflows}",
            ssh = self.p("fake-ssh"),
            indra = INDRA,
            remote = self.p("remote.toml"),
            api = self.p("remote-api"),
            workflows = workflows,
        );
        let path = self.dir.join("hub.toml");
        fs::write(&path, cfg).unwrap();
        path
    }

    fn indra(&self, args: &[&str]) -> Command {
        let mut c = Command::new(INDRA);
        c.args(args)
            // A space in the data path, as on macOS (~/Library/Application Support).
            .env("XDG_DATA_HOME", self.dir.join("app data"))
            .env("HOME", &self.dir)
            .current_dir(&self.dir);
        c
    }
}

impl Drop for Env {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.dir);
    }
}

#[test]
fn nodes_are_checked_over_ssh() {
    let e = Env::new("nodes");
    let hub = e.hub("");
    let out = e
        .indra(&["nodes", "--config", hub.to_str().unwrap(), "--json"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "{}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(v[0]["name"], "local");
    assert_eq!(v[1]["name"], "box");
    let runners: Vec<&str> = v[1]["info"]["runners"]
        .as_array()
        .unwrap()
        .iter()
        .map(|r| r["name"].as_str().unwrap())
        .collect();
    // Its own config first, then the built-in runners (claude, codex, gemini).
    assert_eq!(
        &runners[..3],
        ["echo", "fail", "hold"],
        "the remote reports its own runners: {:?}",
        runners
    );

    // A node that cannot be reached is reported, and the command fails.
    let bad = fs::read_to_string(&hub)
        .unwrap()
        .replace(&e.p("fake-ssh"), "/bin/false");
    fs::write(&hub, bad).unwrap();
    let out = e
        .indra(&["nodes", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(!out.status.success());
    assert!(String::from_utf8_lossy(&out.stdout).contains("DOWN"));
}

#[test]
fn workflow_orders_retries_skips_and_never_shells_the_prompt() {
    let e = Env::new("run");
    let pwned = e.p("pwned");
    let hub = e.hub(&format!(
        "[[workflow]]\nname = \"demo\"\n\
         [[workflow.task]]\nid = \"api\"\nnode = \"box\"\ntarget = \"{api}\"\nrunner = \"echo\"\nprompt = \"review {{target}} on {{node}}; $(touch {pwned}) `touch {pwned}`\"\n\
         [[workflow.task]]\nid = \"web\"\ntarget = \"local-web\"\nrunner = \"echo\"\nprompt = \"web after api\"\nneeds = [\"api\"]\n\
         [[workflow.task]]\nid = \"flaky\"\nnode = \"box\"\ntarget = \"{api}\"\nrunner = \"fail\"\nprompt = \"x\"\nretries = 1\n\
         [[workflow.task]]\nid = \"report\"\ntarget = \"local-web\"\nrunner = \"echo\"\nprompt = \"summary\"\nneeds = [\"web\", \"flaky\"]\n",
        api = e.p("remote-api"),
        pwned = pwned,
    ));
    let out = e
        .indra(&["run", "demo", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(
        out.status.code(),
        Some(1),
        "a failed task fails the run:\n{}",
        text
    );
    let final_state = |id: &str| {
        text.lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .and_then(|l| l.split_whitespace().nth(2))
            .unwrap_or("")
            .to_string()
    };
    assert_eq!(final_state("api"), "succeeded", "{}", text);
    assert_eq!(final_state("web"), "succeeded", "{}", text);
    assert_eq!(final_state("flaky"), "failed", "{}", text);
    assert_eq!(final_state("report"), "skipped", "{}", text);
    assert!(
        text.contains("attempt 2"),
        "flaky was retried once:\n{}",
        text
    );
    // web started only after api succeeded.
    let api_done = text
        .find(&format!("{:<18} {:<12} succeeded", "api", "box"))
        .unwrap();
    let web_run = text
        .find(&format!("{:<18} {:<12} running", "web", "local"))
        .unwrap();
    assert!(api_done < web_run, "{}", text);

    assert!(
        !Path::new(&pwned).exists(),
        "the prompt must never reach a shell"
    );
    // The run's summary names each task's log; the data directory differs by
    // OS (XDG on Linux, ~/Library/Application Support on macOS).
    let log_of = |id: &str| -> String {
        let line = text
            .lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .unwrap_or_else(|| panic!("no summary line for {}:\n{}", id, text));
        // id, node and state are single words; the path is the rest of the
        // line and may contain spaces (~/Library/Application Support).
        let mut rest = line.trim_start();
        for _ in 0..3 {
            rest = rest
                .split_once(char::is_whitespace)
                .map(|(_, r)| r.trim_start())
                .unwrap_or_else(|| panic!("no log path for {}: {}", id, line));
        }
        let path = rest.trim_end();
        fs::read_to_string(path).unwrap_or_else(|err| panic!("cannot read {}: {}", path, err))
    };
    let logs = format!("{}\n{}", log_of("api"), log_of("web"));
    assert!(
        logs.contains(&format!(
            "remote says: review {} on box; $(touch {})",
            e.p("remote-api"),
            pwned
        )),
        "{}",
        logs
    );
    assert!(logs.contains("local says: web after api"), "{}", logs);
}

/// Shell steps (here and on a node), conditions, failure handling, loops
/// and `until`, through `indra run`; input values never become shell text.
#[test]
fn control_flow_commands_conditions_and_loops() {
    let e = Env::new("flow");
    let pwned = e.p("pwned");
    let hub = e.hub(&format!(
        "[[workflow]]\nname = \"flow\"\n\
         [[workflow.input]]\nname = \"mode\"\n\
         [[workflow.input]]\nname = \"who\"\n\
         [[workflow.task]]\nid = \"check\"\ntarget = \"local-web\"\nrun = \"echo version=3; echo \\\"hello $INDRA_INPUT_WHO\\\"\"\n\
         [[workflow.task]]\nid = \"per-item\"\nnode = \"box\"\ntarget = \"{api}\"\nrun = \"echo item=$INDRA_ITEM\"\nforeach = \"a, b\"\n\
         [[workflow.task]]\nid = \"flaky\"\ntarget = \"local-web\"\nrun = \"echo trying; exit 3\"\nallow_failure = true\n\
         [[workflow.task]]\nid = \"handler\"\ntarget = \"local-web\"\nneeds = [\"flaky\"]\non = \"failure\"\nrun = \"echo handled exit $INDRA_EXIT_FLAKY\"\n\
         [[workflow.task]]\nid = \"yes\"\ntarget = \"local-web\"\nneeds = [\"check\"]\nwhen = \"check.output contains 'version=3' and input.mode == 'fast'\"\nrunner = \"echo\"\nprompt = \"saw {{steps.check.output}}\"\n\
         [[workflow.task]]\nid = \"no\"\ntarget = \"local-web\"\nneeds = [\"check\"]\nwhen = \"check.exit_code != 0\"\nrun = \"echo never\"\n\
         [[workflow.task]]\nid = \"after\"\ntarget = \"local-web\"\nneeds = [\"per-item\", \"flaky\"]\nrun = \"echo after loop\"\n\
         [[workflow.task]]\nid = \"count\"\ntarget = \"local-web\"\nrun = \"n=$(cat count 2>/dev/null || echo 0); n=$((n+1)); echo $n > count; echo run $n\"\nuntil = \"self.output contains 'run 3'\"\nmax_runs = 5\n",
        api = e.p("remote-api"),
    ));
    let out = e
        .indra(&[
            "run",
            "flow",
            "--config",
            hub.to_str().unwrap(),
            "--input",
            "mode=fast",
            "--input",
            &format!("who=$(touch {}) `touch {}`", pwned, pwned),
        ])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    let err = String::from_utf8_lossy(&out.stderr);
    assert_eq!(
        out.status.code(),
        Some(0),
        "only an allowed failure:\n{}\n{}",
        text,
        err
    );
    let state = |id: &str| {
        text.lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .and_then(|l| l.split_whitespace().nth(2))
            .unwrap_or("")
            .to_string()
    };
    for (id, want) in [
        ("check", "succeeded"),
        ("per-item-1", "succeeded"),
        ("per-item-2", "succeeded"),
        ("flaky", "failed"),
        ("handler", "succeeded"),
        ("yes", "succeeded"),
        ("no", "skipped"),
        ("after", "succeeded"),
        ("count", "succeeded"),
    ] {
        assert_eq!(state(id), want, "{}: {}", id, text);
    }
    let log = |id: &str| -> String {
        let line = text
            .lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .unwrap();
        let mut rest = line.trim_start();
        for _ in 0..3 {
            rest = rest
                .split_once(char::is_whitespace)
                .map(|(_, r)| r.trim_start())
                .unwrap();
        }
        fs::read_to_string(rest.trim_end()).unwrap_or_default()
    };
    assert!(
        log("per-item-2").contains("item=b"),
        "{}",
        log("per-item-2")
    );
    assert!(
        log("handler").contains("handled exit 3"),
        "{}",
        log("handler")
    );
    assert!(log("yes").contains("saw version=3"), "{}", log("yes"));
    assert!(log("count").contains("run 3"), "{}", log("count"));
    assert_eq!(
        fs::read_to_string(e.dir.join("local-web/count"))
            .unwrap()
            .trim(),
        "3"
    );
    assert!(log("check").contains("hello $(touch"), "{}", log("check"));
    assert!(!Path::new(&pwned).exists(), "an input value must never run");
}

/// A stop step ends the run as it says: what has not started is skipped,
/// what runs is stopped.
#[test]
fn stop_steps_end_the_run_early() {
    let e = Env::new("stop");
    let hub = e.hub(
        "[[workflow]]\nname = \"clean\"\n\
         [[workflow.task]]\nid = \"slow\"\ntarget = \"local-web\"\nrun = \"sleep 30\"\n\
         [[workflow.task]]\nid = \"check\"\ntarget = \"local-web\"\nrun = \"echo clean\"\n\
         [[workflow.task]]\nid = \"done\"\nneeds = [\"check\"]\nstop = \"success\"\nwhen = \"check.output contains 'clean'\"\nprompt = \"nothing to fix\"\n\
         [[workflow.task]]\nid = \"fix\"\ntarget = \"local-web\"\nneeds = [\"check\"]\nrun = \"echo fixing\"\n\
         [[workflow]]\nname = \"bail\"\n\
         [[workflow.task]]\nid = \"check\"\ntarget = \"local-web\"\nrun = \"echo broken\"\n\
         [[workflow.task]]\nid = \"give-up\"\nneeds = [\"check\"]\nstop = \"failure\"\nwhen = \"check.output contains 'broken'\"\n",
    );
    let started = Instant::now();
    let out = e
        .indra(&["run", "clean", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(
        out.status.code(),
        Some(0),
        "{}\n{}",
        text,
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(
        started.elapsed() < Duration::from_secs(20),
        "slow was stopped, not waited for"
    );
    let state = |t: &str, id: &str| {
        t.lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .and_then(|l| l.split_whitespace().nth(2))
            .unwrap_or("")
            .to_string()
    };
    assert_eq!(state(&text, "done"), "succeeded", "{}", text);
    assert_eq!(state(&text, "fix"), "skipped", "{}", text);
    assert_eq!(state(&text, "slow"), "cancelled", "{}", text);
    let out = e
        .indra(&["run", "bail", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(
        out.status.code(),
        Some(1),
        "a failure stop fails the run:\n{}",
        text
    );
    assert_eq!(state(&text, "give-up"), "succeeded", "{}", text);
}

/// An account's credential reaches the agent, here and on a node with no
/// login of its own; its runs are counted.
#[test]
fn accounts_give_agents_their_credentials_on_every_node() {
    let e = Env::new("accounts");
    // The node has a claude runner but no credential of its own.
    let remote = fs::read_to_string(e.dir.join("remote.toml")).unwrap()
        + "[[runner]]\nname = \"claude\"\ncommand = \"sh\"\nargs = [\"-c\", \"echo key-len=${#ANTHROPIC_API_KEY}\", \"{prompt}\"]\n";
    fs::write(e.dir.join("remote.toml"), remote).unwrap();
    let hub = e.hub(&format!(
        "[[runner]]\nname = \"claude\"\ncommand = \"sh\"\nargs = [\"-c\", \"echo key-len=${{#ANTHROPIC_API_KEY}}\", \"{{prompt}}\"]\n\
         [[account]]\nname = \"work\"\ncredential = \"secret:anthropic-work\"\n\
         [[workflow]]\nname = \"both\"\n\
         [[workflow.task]]\nid = \"here\"\ntarget = \"local-web\"\nrunner = \"claude\"\nprompt = \"x\"\n\
         [[workflow.task]]\nid = \"there\"\nnode = \"box\"\ntarget = \"{api}\"\nrunner = \"claude\"\nprompt = \"x\"\n",
        api = e.p("remote-api"),
    ));
    let mut set = e
        .indra(&["secret", "set", "anthropic-work"])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    use std::io::Write;
    set.stdin
        .take()
        .unwrap()
        .write_all(b"sk-test-123456\n")
        .unwrap();
    assert!(set.wait().unwrap().success());
    let out = e
        .indra(&["run", "both", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(
        out.status.code(),
        Some(0),
        "{}\n{}",
        text,
        String::from_utf8_lossy(&out.stderr)
    );
    let log = |id: &str| -> String {
        let line = text
            .lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .unwrap();
        let mut rest = line.trim_start();
        for _ in 0..3 {
            rest = rest
                .split_once(char::is_whitespace)
                .map(|(_, r)| r.trim_start())
                .unwrap();
        }
        fs::read_to_string(rest.trim_end()).unwrap_or_default()
    };
    assert!(log("here").contains("key-len=14"), "{}", log("here"));
    assert!(
        log("there").contains("key-len=14"),
        "the node got it from the hub: {}",
        log("there")
    );
    assert!(!log("here").contains("sk-test-123456"));
    // Both runs are counted on the account.
    // The data directory is per OS (XDG on Linux, ~/Library/Application
    // Support on macOS): look for the file anywhere under the test's home.
    fn find(dir: &Path, name: &str) -> Option<PathBuf> {
        for e in fs::read_dir(dir).ok()?.flatten() {
            let p = e.path();
            if p.is_dir() {
                if let Some(f) = find(&p, name) {
                    return Some(f);
                }
            } else if p.file_name().is_some_and(|n| n == name) {
                return Some(p);
            }
        }
        None
    }
    let data = find(&e.dir, "accounts.json").expect("accounts.json");
    let usage: serde_json::Value =
        serde_json::from_str(&fs::read_to_string(data).unwrap()).unwrap();
    let days = usage["usage"]["work"]["days"].as_object().unwrap();
    assert_eq!(days.values().next().unwrap()[0], 2, "{}", usage);
}

#[test]
fn bad_workflows_are_rejected_before_anything_runs() {
    let e = Env::new("invalid");
    let hub = e.hub(
        "[[workflow]]\nname = \"loop\"\n\
         [[workflow.task]]\nid = \"a\"\ntarget = \".\"\nprompt = \"x\"\nneeds = [\"b\"]\n\
         [[workflow.task]]\nid = \"b\"\ntarget = \".\"\nprompt = \"x\"\nneeds = [\"a\"]\n",
    );
    let out = e
        .indra(&["run", "loop", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(!out.status.success());
    assert!(String::from_utf8_lossy(&out.stderr).contains("dependency cycle"));

    let bad_dest = fs::read_to_string(&hub)
        .unwrap()
        .replace("me@box.internal", "-oProxyCommand=touch");
    fs::write(&hub, bad_dest).unwrap();
    let out = e
        .indra(&["nodes", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(String::from_utf8_lossy(&out.stderr).contains("invalid ssh destination"));
}

fn alive(pid: i32) -> bool {
    // SAFETY: signal 0 only checks that the process exists.
    unsafe { libc::kill(pid, 0) == 0 }
}

#[test]
fn cancelling_stops_the_remote_agent() {
    let e = Env::new("cancel");
    let pidfile = e.p("agent.pid");
    let hub = e.hub(&format!(
        "[[workflow]]\nname = \"long\"\n[[workflow.task]]\nid = \"hold\"\nnode = \"box\"\ntarget = \"{api}\"\nrunner = \"hold\"\nprompt = \"{pidfile}\"\n",
        api = e.p("remote-api"),
        pidfile = pidfile,
    ));
    let mut child = e
        .indra(&["run", "long", "--config", hub.to_str().unwrap()])
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();
    let start = Instant::now();
    let pid = loop {
        if let Some(p) = fs::read_to_string(&pidfile)
            .ok()
            .and_then(|s| s.trim().parse::<i32>().ok())
        {
            break p;
        }
        assert!(
            start.elapsed() < Duration::from_secs(20),
            "the remote agent never started"
        );
        std::thread::sleep(Duration::from_millis(50));
    };
    assert!(alive(pid));
    // Ctrl-C on `indra run` cancels the workflow.
    unsafe {
        libc::kill(child.id() as i32, libc::SIGINT);
    }
    let status = child.wait().unwrap();
    assert_eq!(status.code(), Some(1));
    let deadline = Instant::now() + Duration::from_secs(10);
    while alive(pid) && Instant::now() < deadline {
        std::thread::sleep(Duration::from_millis(50));
    }
    assert!(
        !alive(pid),
        "the agent on the node must be stopped when the run is cancelled"
    );
}

/// A profile's model and environment reach an agent on a remote node, which
/// is driven over Claude Code's control channel and finishes on its own.
#[test]
fn profiles_reach_remote_agents_over_the_control_channel() {
    let e = Env::new("profile");
    let fake = e.dir.join("fake-claude.py");
    fs::write(
        &fake,
        "#!/usr/bin/env python3\nimport json, os, sys\n\
         args = sys.argv[1:]\n\
         model = args[args.index('--model') + 1] if '--model' in args else '-'\n\
         first = sys.stdin.readline()\n\
         prompt = json.loads(sys.stdin.readline())['message']['content']\n\
         print(json.dumps({'type': 'system', 'subtype': 'init', 'model': model, 'session_id': 'r-1'}), flush=True)\n\
         say = 'model=%s env=%s prompt=%s interactive=%s' % (model, os.environ.get('INDRA_TEST_ENV', '-'), prompt, '--input-format' in args)\n\
         print(json.dumps({'type': 'assistant', 'message': {'content': [{'type': 'text', 'text': say}]}}), flush=True)\n\
         print(json.dumps({'type': 'result', 'subtype': 'success', 'num_turns': 1, 'duration_ms': 1, 'result': 'ok'}), flush=True)\n\
         sys.stdin.read()\n",
    )
    .unwrap();
    fs::set_permissions(&fake, fs::Permissions::from_mode(0o755)).unwrap();
    let remote = fs::read_to_string(e.dir.join("remote.toml")).unwrap()
        + &format!(
            "[[runner]]\nname = \"fakec\"\ncommand = \"{}\"\nargs = [\"-p\", \"{{prompt}}\"]\nformat = \"claude-stream-json\"\n",
            fake.display()
        );
    fs::write(e.dir.join("remote.toml"), remote).unwrap();
    let hub = e.hub(&format!(
        "[[profile]]\nname = \"remote-sonnet\"\nrunner = \"fakec\"\nmodel = \"sonnet\"\nenv = {{ INDRA_TEST_ENV = \"from-hub\" }}\n\
         [[workflow]]\nname = \"p\"\n[[workflow.task]]\nid = \"t\"\nnode = \"box\"\ntarget = \"{api}\"\nprofile = \"remote-sonnet\"\nprompt = \"review {{node}}\"\n",
        api = e.p("remote-api"),
    ));
    let out = e
        .indra(&["run", "p", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    assert!(
        out.status.success(),
        "{}\n{}",
        text,
        String::from_utf8_lossy(&out.stderr)
    );
    let line = text
        .lines()
        .rev()
        .find(|l| l.split_whitespace().next() == Some("t"))
        .unwrap();
    let mut rest = line.trim_start();
    for _ in 0..3 {
        rest = rest
            .split_once(char::is_whitespace)
            .map(|(_, r)| r.trim_start())
            .unwrap();
    }
    let log = fs::read_to_string(rest.trim_end()).unwrap();
    assert!(
        log.contains("model=sonnet env=from-hub prompt=review box interactive=True"),
        "{}",
        log
    );
}

/// An If step answers yes or no; the side not taken is skipped, and a step
/// both sides lead to runs after the side that ran.
#[test]
fn if_steps_take_one_side_and_the_sides_meet_again() {
    let e = Env::new("ifs");
    let hub = e.hub(
        "[[workflow]]\nname = \"gate\"\n\
         [[workflow.task]]\nid = \"test\"\ntarget = \"local-web\"\nrun = \"echo tests FAILED\"\n\
         [[workflow.task]]\nid = \"passed\"\ntarget = \"local-web\"\nneeds = [\"test\"]\nif = \"test.output contains 'PASSED'\"\n\
         [[workflow.task]]\nid = \"ship\"\ntarget = \"local-web\"\nneeds = [\"passed\"]\nbranch = \"yes\"\nrun = \"echo shipping\"\n\
         [[workflow.task]]\nid = \"fix\"\ntarget = \"local-web\"\nneeds = [\"passed\"]\nbranch = \"no\"\nrun = \"echo fixing\"\n\
         [[workflow.task]]\nid = \"after-ship\"\ntarget = \"local-web\"\nneeds = [\"ship\"]\nrun = \"echo never\"\n\
         [[workflow.task]]\nid = \"report\"\ntarget = \"local-web\"\nneeds = [\"after-ship\", \"fix\"]\nrun = \"echo report\"\n",
    );
    let out = e
        .indra(&["run", "gate", "--config", hub.to_str().unwrap()])
        .output()
        .unwrap();
    let text = String::from_utf8_lossy(&out.stdout);
    assert_eq!(
        out.status.code(),
        Some(0),
        "{}\n{}",
        text,
        String::from_utf8_lossy(&out.stderr)
    );
    let state = |id: &str| {
        text.lines()
            .rev()
            .find(|l| l.split_whitespace().next() == Some(id))
            .and_then(|l| l.split_whitespace().nth(2))
            .unwrap_or("")
            .to_string()
    };
    for (id, want) in [
        ("test", "succeeded"),
        ("passed", "succeeded"),
        ("ship", "skipped"),
        ("after-ship", "skipped"),
        ("fix", "succeeded"),
        ("report", "succeeded"),
    ] {
        assert_eq!(state(id), want, "{}: {}", id, text);
    }
    // A branch must follow an If, and an If runs nothing itself.
    let bad = e.hub(
        "[[workflow]]\nname = \"bad\"\n\
         [[workflow.task]]\nid = \"a\"\ntarget = \"local-web\"\nrun = \"true\"\n\
         [[workflow.task]]\nid = \"b\"\ntarget = \"local-web\"\nneeds = [\"a\"]\nbranch = \"yes\"\nrun = \"true\"\n",
    );
    let out = e
        .indra(&["run", "bad", "--config", bad.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(!out.status.success());
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("branch needs an If step"),
        "{:?}",
        out
    );
    let bad = e.hub(
        "[[workflow]]\nname = \"bad\"\nschedule = \"every day\"\n\
         [[workflow.task]]\nid = \"a\"\ntarget = \"local-web\"\nrun = \"true\"\n",
    );
    let out = e
        .indra(&["run", "bad", "--config", bad.to_str().unwrap()])
        .output()
        .unwrap();
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("schedule"),
        "{:?}",
        out
    );
}