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 TOML;
25/// `Loop` wraps an `AgentLoopError` from the multi-turn loop.
26/// Test: `runner::tests::*` exercise `UnknownAgent` and `Loop` paths.
27#[derive(Debug, thiserror::Error)]
28pub enum RunnerError {
29 /// No agent config was found for the requested name in the config directory.
30 #[error("unknown agent '{name}': no config found in {dir}")]
31 UnknownAgent {
32 /// The requested agent slug.
33 name: String,
34 /// The directory that was searched for `<name>.toml`.
35 dir: PathBuf,
36 },
37
38 /// The agent's config file exists but could not be read or parsed.
39 #[error("failed to load agent config for '{name}': {source}")]
40 ConfigLoad {
41 /// The requested agent slug.
42 name: String,
43 /// The underlying load/parse error.
44 #[source]
45 source: anyhow::Error,
46 },
47
48 /// The multi-turn agent loop returned an error (turn cap, timeout, LLM, …).
49 #[error("agent '{name}' loop failed: {source}")]
50 Loop {
51 /// The agent slug whose loop failed.
52 name: String,
53 /// The underlying loop error.
54 #[source]
55 source: AgentLoopError,
56 },
57}