brainos-orchestrate 0.5.0

Task orchestrator — decompose, plan, track, and coordinate autonomous execution
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
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
//! Step-action handlers for [`TaskOrchestrator`].
//!
//! Each [`crate::step::StepAction`] variant has a corresponding executor
//! method on `TaskOrchestrator`. Splitting them out of `orchestrator.rs`
//! keeps the lifecycle/state-machine code there and groups the
//! per-action dispatch logic here. All methods follow the same pattern:
//! degrade gracefully (return a synthetic outcome) when the relevant
//! handle is not attached, so a partially-wired orchestrator still
//! finishes plans rather than hard-failing.

use crate::orchestrator::TaskOrchestrator;
use crate::state::StepOutcome;

impl TaskOrchestrator {
    /// Run a `Research` step against the configured LLM. If no LLM is
    /// attached, degrade to a no-op string outcome so a partially-wired
    /// orchestrator still finishes the plan instead of failing.
    pub(super) async fn execute_research_step(&self, query: &str) -> Result<StepOutcome, String> {
        let Some(llm) = self.llm.as_ref() else {
            return Ok(StepOutcome {
                stdout: format!("Research query: {query}"),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![],
                summary: format!("Researched (no LLM attached): {query}"),
            });
        };

        let messages = vec![
            cortex::llm::Message::system(crate::prompts::RESEARCH_SYSTEM),
            cortex::llm::Message::user(query.to_string()),
        ];

        match llm.generate(&messages).await {
            Ok(resp) => Ok(StepOutcome {
                stdout: resp.content.clone(),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![],
                summary: summary_first_line(&resp.content, &format!("Research: {query}")),
            }),
            Err(e) => Err(format!("LLM research failed: {e}")),
        }
    }

    /// Run a `Review` step — asks the LLM to critique the named artifact.
    /// Falls back to a no-op outcome if no LLM is attached.
    pub(super) async fn execute_review_step(&self, artifact: &str) -> Result<StepOutcome, String> {
        let Some(llm) = self.llm.as_ref() else {
            return Ok(StepOutcome {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![artifact.to_string()],
                summary: format!("Review requested (no LLM attached): {artifact}"),
            });
        };

        let messages = vec![
            cortex::llm::Message::system(crate::prompts::REVIEW_SYSTEM),
            cortex::llm::Message::user(format!("Review this artifact: {artifact}")),
        ];

        match llm.generate(&messages).await {
            Ok(resp) => Ok(StepOutcome {
                stdout: resp.content.clone(),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![artifact.to_string()],
                summary: summary_first_line(&resp.content, &format!("Reviewed: {artifact}")),
            }),
            Err(e) => Err(format!("LLM review failed: {e}")),
        }
    }

    /// Deliver a `Notify` step through the channel dispatcher. The
    /// `channel` field of the step is a soft preference — the dispatcher
    /// falls back to learned preferences and initiation channel if the
    /// requested channel is unavailable.
    pub(super) async fn execute_notify_step(
        &self,
        channel: &str,
        message: &str,
    ) -> Result<StepOutcome, String> {
        let Some(dispatcher) = self.dispatcher.as_ref() else {
            return Ok(StepOutcome {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![],
                summary: format!("Notify (no dispatcher attached): {channel}: {message}"),
            });
        };

        let mut intent = channel::DeliveryIntent::new(
            message,
            channel::DeliveryCategory::Report,
            channel::UrgencyLevel::Normal,
        );
        if !channel.is_empty() && channel != "default" {
            intent = intent.with_preferred(channel);
        }

        match dispatcher.dispatch(intent).await {
            Ok(receipt) => Ok(StepOutcome {
                stdout: format!("Delivered via {} ({})", receipt.channel_id, receipt.reason),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![],
                summary: format!("Notified via {}: {message}", receipt.channel_id),
            }),
            // No transports configured / no channel matches the intent — surface
            // the message via the orchestrator's task-completion summary instead
            // of failing the step. Replan-on-failure produces Notify steps as
            // its honest "I cannot do this" path; if delivery itself failed
            // here we'd recurse into more Notify steps and exhaust the replan
            // budget, leaving the user with a cascade of "delivery failed"
            // entries hiding the actual blocker message.
            Err(channel::ChannelError::NoChannelAvailable(_, _)) => Ok(StepOutcome {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: None,
                artifacts: vec![],
                summary: format!(
                    "Notify (no external channel — included in this report): {message}"
                ),
            }),
            Err(e) => Err(format!("Notify delivery failed: {e}")),
        }
    }

    /// Execute a command in the sandbox.
    pub(super) async fn execute_sandbox_step(
        &self,
        command: &str,
        workdir: &std::path::Path,
    ) -> Result<StepOutcome, String> {
        let sandbox = match self.sandbox.as_ref() {
            Some(s) => s,
            None => {
                return Err("Sandbox not available".to_string());
            }
        };

        // The sandbox runs argv directly — no shell. Parse with quote
        // handling and detect a trailing `> path` / `>> path` redirect so
        // commands like `find … > /tmp/file` actually capture their output
        // instead of passing `>` as a literal argument to the binary.
        let parsed = parse_sandbox_command(command)?;
        let (binary, args) = parsed
            .argv
            .split_first()
            .ok_or_else(|| "Empty command".to_string())?;

        let cmd =
            sandbox::SandboxCommand::new(binary, args.to_vec()).with_workdir(workdir.to_path_buf());

        match sandbox.run(cmd).await {
            Err(e) => Err(humanize_sandbox_error(binary, &e)),
            Ok(outcome) if outcome.exit_code != 0 => {
                // A non-zero exit is a real failure — surface it so the
                // orchestrator marks the step Failed (and cascade-skips
                // dependents) instead of recording it as Completed with
                // a misleading "succeeded" status. The audit trail picks
                // up the structured failure from the Err path in
                // `execute_step`.
                let stderr_tail: String = outcome
                    .stderr
                    .lines()
                    .rev()
                    .take(5)
                    .collect::<Vec<_>>()
                    .into_iter()
                    .rev()
                    .collect::<Vec<_>>()
                    .join("\n");
                let mut msg = format!("Command failed (exit {}): {command}", outcome.exit_code);
                if !stderr_tail.trim().is_empty() {
                    msg.push_str("\nstderr: ");
                    msg.push_str(&stderr_tail);
                }
                Err(msg)
            }
            Ok(outcome) => {
                // exit_code == 0 — apply any trailing redirect, then
                // record the success.
                let mut artifacts = Vec::new();
                if let Some((path, append)) = &parsed.redirect {
                    let write_result = if *append {
                        use std::io::Write as _;
                        std::fs::OpenOptions::new()
                            .create(true)
                            .append(true)
                            .open(path)
                            .and_then(|mut f| f.write_all(outcome.stdout.as_bytes()))
                    } else {
                        std::fs::write(path, outcome.stdout.as_bytes())
                    };
                    if let Err(e) = write_result {
                        return Err(format!("Redirect to {} failed: {e}", path.display()));
                    }
                    artifacts.push(path.to_string_lossy().into_owned());
                }

                Ok(StepOutcome {
                    stdout: outcome.stdout,
                    stderr: outcome.stderr,
                    exit_code: Some(outcome.exit_code),
                    artifacts,
                    summary: format!("Command succeeded: {command}"),
                })
            }
        }
    }

    /// Run a shell-wrapped command in the sandbox. Unlike
    /// [`execute_sandbox_step`], the command is passed verbatim to
    /// `sh -c` so the system shell handles pipes, redirects, escaping,
    /// $VAR expansion, and PATH lookup. The per-binary allowlist is
    /// bypassed for the wrapped command — see [`sandbox::SandboxCommand::shell`]
    /// for the safety story.
    pub(super) async fn execute_shell_step(
        &self,
        command: &str,
        workdir: &std::path::Path,
    ) -> Result<StepOutcome, String> {
        let sandbox = match self.sandbox.as_ref() {
            Some(s) => s,
            None => return Err("Sandbox not available".to_string()),
        };

        let trimmed = command.trim();
        if trimmed.is_empty() {
            return Err("Empty shell command".to_string());
        }

        let cmd = sandbox::SandboxCommand::shell(trimmed).with_workdir(workdir.to_path_buf());

        match sandbox.run(cmd).await {
            Err(e) => Err(humanize_sandbox_error("sh", &e)),
            Ok(outcome) if outcome.exit_code != 0 => {
                let stderr_tail: String = outcome
                    .stderr
                    .lines()
                    .rev()
                    .take(5)
                    .collect::<Vec<_>>()
                    .into_iter()
                    .rev()
                    .collect::<Vec<_>>()
                    .join("\n");
                let mut msg = format!(
                    "Shell command failed (exit {}): {trimmed}",
                    outcome.exit_code
                );
                if !stderr_tail.trim().is_empty() {
                    msg.push_str("\nstderr: ");
                    msg.push_str(&stderr_tail);
                }
                Err(msg)
            }
            Ok(outcome) => Ok(StepOutcome {
                stdout: outcome.stdout,
                stderr: outcome.stderr,
                exit_code: Some(outcome.exit_code),
                artifacts: vec![],
                summary: format!("Shell command succeeded: {trimmed}"),
            }),
        }
    }

    /// Hand the step off to a registered [`delegate::AgentDelegate`].
    /// Failures are run through the configured escalation policy — a
    /// primary hang or launch failure transparently falls over to the
    /// declared fallback chain; anything the chain can't recover becomes
    /// a human escalation recorded as a failed step outcome.
    pub(super) async fn delegate_implement_step(
        &self,
        spec: &str,
        agent: &str,
    ) -> Result<StepOutcome, String> {
        let registry = self.agents.as_ref().ok_or_else(|| {
            "No specialist agents are configured — install one (e.g. claude-code, \
             aider, codex) on your PATH and it will be picked up on the next boot."
                .to_string()
        })?;

        let primary = registry.get(agent).map_err(|_| {
            let known = registry.list();
            if known.is_empty() {
                format!(
                    "Specialist agent '{agent}' isn't available — no agents are \
                     currently registered. Install one on your PATH or pick a \
                     different planner."
                )
            } else {
                format!(
                    "Specialist agent '{agent}' isn't available. Available agents: {}.",
                    known.join(", ")
                )
            }
        })?;

        let task_spec = self.build_delegate_task_spec(spec).await;
        let task = delegate::AgentTask::new(task_spec);
        let outcome = delegate::run_with_escalation(
            primary,
            registry.as_ref(),
            task,
            &self.delegation_policy,
        )
        .await;

        match outcome {
            delegate::EscalationOutcome::Succeeded(result) => {
                self.record_delegate_episode(agent, spec, &result, None)
                    .await;
                Ok(StepOutcome {
                    stdout: result.stdout,
                    stderr: result.stderr,
                    exit_code: result.exit_code,
                    artifacts: result
                        .artifacts
                        .iter()
                        .map(|a| a.reference.clone())
                        .collect(),
                    summary: format!("{agent}: {}", result.summary),
                })
            }
            delegate::EscalationOutcome::Recovered { via, result } => {
                self.record_delegate_episode(agent, spec, &result, Some(&via))
                    .await;
                Ok(StepOutcome {
                    stdout: result.stdout,
                    stderr: result.stderr,
                    exit_code: result.exit_code,
                    artifacts: result
                        .artifacts
                        .iter()
                        .map(|a| a.reference.clone())
                        .collect(),
                    summary: format!("{agent} failed; recovered via {via}: {}", result.summary),
                })
            }
            delegate::EscalationOutcome::EscalateToHuman { reason } => Err(reason),
        }
    }
}

/// Parsed result of a shell-style command string we run in the sandbox.
#[derive(Debug)]
pub(crate) struct ParsedCommand {
    pub argv: Vec<String>,
    /// `Some((path, append))` if a trailing `> path` / `>> path` redirect
    /// was extracted. The orchestrator captures stdout to that path after
    /// the sandbox returns — the sandbox itself only sees argv.
    pub redirect: Option<(std::path::PathBuf, bool)>,
}

/// Tokenize a command string with single/double-quote handling, then
/// split off a trailing `> path` / `>> path` redirect. Reject any other
/// shell metacharacter — pipes, sequencing, backgrounding, command
/// substitution — because the sandbox executes argv directly with no
/// shell to interpret them. A clear error here beats silently passing
/// `|` as an argument to `find`.
pub(crate) fn parse_sandbox_command(command: &str) -> Result<ParsedCommand, String> {
    let tokens = tokenize_shell(command)?;
    if tokens.is_empty() {
        return Err("Empty command".to_string());
    }

    for tok in &tokens {
        // Reject metacharacters that would need a real shell. `>`/`>>` are
        // handled below as a structured redirect, not by passing through.
        if tok == "|" || tok == "||" || tok == "&&" || tok == ";" || tok == "&" || tok == "<" {
            return Err(format!(
                "Shell metacharacter {tok:?} not supported — sandbox runs argv directly. \
                 Split this into multiple steps or capture output via the trailing `> path` form."
            ));
        }
        if tok.contains('`') || tok.contains("$(") {
            return Err(format!(
                "Shell substitution in {tok:?} not supported — sandbox runs argv directly."
            ));
        }
    }

    // Strip a trailing redirect: `… > path` or `… >> path`.
    let mut tokens = tokens;
    let mut redirect = None;
    if tokens.len() >= 3 {
        let last = tokens.len() - 1;
        let op = &tokens[last - 1];
        if op == ">" || op == ">>" {
            let append = op == ">>";
            let path = std::path::PathBuf::from(tokens.remove(last));
            tokens.pop(); // remove the operator token
            redirect = Some((path, append));
        }
    }
    // Reject any remaining redirect operators in non-trailing positions.
    if tokens.iter().any(|t| t == ">" || t == ">>") {
        return Err(
            "Multiple or non-trailing `>` redirects are not supported — sandbox runs argv directly."
                .to_string(),
        );
    }

    Ok(ParsedCommand {
        argv: tokens,
        redirect,
    })
}

/// Minimal POSIX-style tokenizer: whitespace splits, single/double quotes
/// group, backslash escapes the next char (outside single quotes). Enough
/// to handle the shapes the LLM decomposer produces; not a full shell.
fn tokenize_shell(input: &str) -> Result<Vec<String>, String> {
    let mut out = Vec::new();
    let mut cur = String::new();
    let mut in_single = false;
    let mut in_double = false;
    let mut chars = input.chars().peekable();

    while let Some(c) = chars.next() {
        match c {
            '\'' if !in_double => {
                in_single = !in_single;
            }
            '"' if !in_single => {
                in_double = !in_double;
            }
            '\\' if !in_single => {
                if let Some(next) = chars.next() {
                    cur.push(next);
                }
            }
            c if c.is_whitespace() && !in_single && !in_double => {
                if !cur.is_empty() {
                    out.push(std::mem::take(&mut cur));
                }
            }
            // Emit `>` / `>>` / `|` / `||` / `&` / `&&` / `;` / `<` as their own tokens
            // so the redirect/metacharacter checks above can spot them
            // even when the LLM doesn't space-separate them.
            c @ ('>' | '<' | '|' | '&' | ';') if !in_single && !in_double => {
                if !cur.is_empty() {
                    out.push(std::mem::take(&mut cur));
                }
                let mut op = String::from(c);
                if let Some(&peek) = chars.peek() {
                    if (c == '>' && peek == '>')
                        || (c == '|' && peek == '|')
                        || (c == '&' && peek == '&')
                    {
                        op.push(peek);
                        chars.next();
                    }
                }
                out.push(op);
            }
            _ => cur.push(c),
        }
    }
    if in_single || in_double {
        return Err("Unterminated quoted string".to_string());
    }
    if !cur.is_empty() {
        out.push(cur);
    }
    Ok(out)
}

/// Translate a `SandboxError` into a one-liner the chat user can act on.
/// Strips the internal "binary X not in allowlist" / "workdir Y not in
/// allowlist" framing in favour of plain language plus the actionable
/// config key. The full structured error still hits the audit trail.
pub(crate) fn humanize_sandbox_error(binary: &str, e: &sandbox::SandboxError) -> String {
    use sandbox::SandboxError;
    match e {
        SandboxError::Forbidden(msg) => {
            if msg.contains("not in allowlist") {
                format!(
                    "Cannot run `{binary}` here — it isn't on the sandbox allowlist. \
                     Add it under `security.exec_allowlist` if this command is safe to run."
                )
            } else if msg.contains("explicitly forbidden") {
                format!("`{binary}` is explicitly forbidden by config and cannot run.")
            } else {
                format!("`{binary}` was blocked: {msg}")
            }
        }
        SandboxError::PathNotAllowed(_) => format!(
            "Working directory for `{binary}` isn't allowed. Add the path under \
             `security.allowed_paths` to permit it."
        ),
        SandboxError::Timeout(_) => format!("`{binary}` was killed for taking too long."),
        other => format!("`{binary}` failed to run: {other}"),
    }
}

/// First non-empty line of `s`, truncated to 160 chars; falls back to
/// `default_label` when the LLM returned only whitespace.
pub(crate) fn summary_first_line(s: &str, default_label: &str) -> String {
    let line = s
        .lines()
        .map(str::trim)
        .find(|l| !l.is_empty())
        .unwrap_or("");
    if line.is_empty() {
        return default_label.to_string();
    }
    if line.chars().count() > 160 {
        let truncated: String = line.chars().take(157).collect();
        format!("{truncated}")
    } else {
        line.to_string()
    }
}

#[cfg(test)]
mod parse_tests {
    use super::*;

    #[test]
    fn parses_simple_argv() {
        let p = parse_sandbox_command("find /tmp -type f").unwrap();
        assert_eq!(p.argv, vec!["find", "/tmp", "-type", "f"]);
        assert!(p.redirect.is_none());
    }

    #[test]
    fn extracts_trailing_redirect() {
        let p = parse_sandbox_command("find /tmp -type f > /tmp/list.txt").unwrap();
        assert_eq!(p.argv, vec!["find", "/tmp", "-type", "f"]);
        let (path, append) = p.redirect.unwrap();
        assert_eq!(path, std::path::PathBuf::from("/tmp/list.txt"));
        assert!(!append);
    }

    #[test]
    fn extracts_trailing_append_redirect() {
        let p = parse_sandbox_command("ls /etc >> /tmp/log").unwrap();
        let (_, append) = p.redirect.unwrap();
        assert!(append);
    }

    #[test]
    fn handles_unspaced_redirect() {
        let p = parse_sandbox_command("find /tmp -type f >/tmp/list.txt").unwrap();
        assert_eq!(p.argv, vec!["find", "/tmp", "-type", "f"]);
        assert_eq!(
            p.redirect.unwrap().0,
            std::path::PathBuf::from("/tmp/list.txt")
        );
    }

    #[test]
    fn rejects_pipe() {
        let err = parse_sandbox_command("ls | grep foo").unwrap_err();
        assert!(err.contains("Shell metacharacter"));
    }

    #[test]
    fn rejects_command_substitution() {
        let err = parse_sandbox_command("echo $(pwd)").unwrap_err();
        assert!(err.contains("Shell substitution"));
    }

    #[test]
    fn respects_quotes() {
        let p = parse_sandbox_command(r#"grep "hello world" file.txt"#).unwrap();
        assert_eq!(p.argv, vec!["grep", "hello world", "file.txt"]);
    }

    #[test]
    fn redirect_inside_quotes_is_argument_not_operator() {
        let p = parse_sandbox_command(r#"echo "a > b""#).unwrap();
        assert_eq!(p.argv, vec!["echo", "a > b"]);
        assert!(p.redirect.is_none());
    }

    #[test]
    fn rejects_redirect_in_middle() {
        let err = parse_sandbox_command("foo > bar baz").unwrap_err();
        assert!(err.contains("non-trailing"));
    }
}