Skip to main content

trusty_code/runner/
error.rs

1//! Error type for the in-process agent runner.
2//!
3//! Why: The runner is library code, so per the crate convention it surfaces a
4//! structured `thiserror` enum rather than `unwrap()` or bare `anyhow` strings.
5//! Each variant names a distinct failure the caller (the delegate tool, the
6//! orchestrator) may want to react to differently — a missing/invalid agent
7//! config is the caller's mistake, whereas an `AgentLoop` failure is downstream.
8//! What: `RunnerError` enumerates config-load, unknown-agent, and loop-failure
9//! cases. It converts to `anyhow::Error` automatically so it slots into the
10//! `AgentRunner` trait's `anyhow::Result` return type via `?`.
11//! Test: `runner::tests::unknown_agent_errors` and the config-load tests assert
12//! on these variants' `Display` text.
13
14use std::path::PathBuf;
15
16use crate::agent_loop::AgentLoopError;
17
18/// Failure modes of an in-process agent run.
19///
20/// Why: A typed error lets call sites distinguish "you asked for an agent that
21/// does not exist" (recoverable, surface to the model) from "the agent's loop
22/// blew up" (propagate). Bundling them in one enum keeps the runner's signature
23/// a single `Result<_, RunnerError>`.
24/// What: `UnknownAgent` and `ConfigLoad` cover resolving the agent's `.md`
25/// config (#2897 Slice D); `Loop` wraps an `AgentLoopError` from the
26/// multi-turn loop.
27/// Test: `runner::tests::*` exercise `UnknownAgent` and `Loop` paths.
28#[derive(Debug, thiserror::Error)]
29pub enum RunnerError {
30    /// No agent config was found for the requested name in the config directory.
31    #[error("unknown agent '{name}': no config found in {dir}")]
32    UnknownAgent {
33        /// The requested agent slug.
34        name: String,
35        /// The directory that was searched for `<name>.md`.
36        dir: PathBuf,
37    },
38
39    /// The agent's config file exists but could not be read or parsed.
40    #[error("failed to load agent config for '{name}': {source}")]
41    ConfigLoad {
42        /// The requested agent slug.
43        name: String,
44        /// The underlying load/parse error.
45        #[source]
46        source: anyhow::Error,
47    },
48
49    /// The multi-turn agent loop returned an error (turn cap, timeout, LLM, …).
50    #[error("agent '{name}' loop failed: {source}")]
51    Loop {
52        /// The agent slug whose loop failed.
53        name: String,
54        /// The underlying loop error.
55        #[source]
56        source: AgentLoopError,
57    },
58}