Skip to main content

trusty_code/agents/
config.rs

1//! Agent configuration schema for tcode.
2//!
3//! Why: Sub-agents (and the PM itself) are defined declaratively so model,
4//! prompt, and LLM parameters can evolve without code changes. As of #2897
5//! Slice D, the on-disk source format is Markdown+frontmatter
6//! (`.claude/agents/<name>.md`) ONLY — the original TOML loader
7//! (`AgentConfig::from_toml_str`/`AgentConfig::load`) has been retired; see
8//! `agents::md_loader` for the current loader and
9//! `scripts/migrate-tcode-agents-toml-to-md.py` for the one-time converter a
10//! project with pre-#2897 `.toml` agent files should run.
11//! What: `AgentConfig` and all nested config types. These are pure data
12//! structs — format-agnostic — that `agents::md_loader::project_to_agent_config`
13//! populates from a composed `.md` document's frontmatter + body.
14//! Test: `agent_config_default_is_empty`, `runner_kind_defaults_to_in_process`;
15//! the loading path itself is covered by `agents::md_loader::tests::*`.
16
17use serde::{Deserialize, Serialize};
18
19/// Top-level agent configuration.
20///
21/// Why: Declarative agent configs let operators define or override agents
22/// without touching Rust code.
23/// What: `agent` carries identity/role; `llm` carries model/token params;
24/// `system_prompt` carries the prompt text; optional sections add capabilities.
25/// Populated by `agents::md_loader::project_to_agent_config` from a composed
26/// `.md` agent document.
27/// Test: `agent_config_default_is_empty`; `agents::md_loader::tests::*` cover
28/// the actual field-projection contract end-to-end.
29#[derive(Debug, Clone, Serialize, Deserialize, Default)]
30pub struct AgentConfig {
31    /// Core identity fields.
32    pub agent: AgentInfo,
33    /// LLM parameters for this agent.
34    #[serde(default)]
35    pub llm: LlmParams,
36    /// System prompt content.
37    #[serde(default)]
38    pub system_prompt: SystemPrompt,
39    /// Optional tool permissions.
40    #[serde(default)]
41    pub tools: Option<ToolsConfig>,
42    /// Optional runner override.
43    #[serde(default)]
44    pub runner: Option<RunnerConfig>,
45}
46
47/// Core identity fields for an agent.
48///
49/// Why: Every agent needs a stable `name` (the dispatch key) and `model`.
50/// What: `name`, optional `role`, optional `model`.
51/// Test: `agent_config_default_is_empty` asserts the zero-value default;
52/// `agents::md_loader::tests::load_md_agent_base_case` covers real loading.
53#[derive(Debug, Clone, Serialize, Deserialize, Default)]
54pub struct AgentInfo {
55    /// The agent's dispatch key (e.g. `"engineer"`, `"python-engineer"`).
56    pub name: String,
57    /// Optional free-form role description.
58    #[serde(default)]
59    pub role: Option<String>,
60    /// LLM model override (e.g. `"anthropic/claude-sonnet-4-6"`,
61    /// `"bedrock/us.anthropic.claude-sonnet-4-6"`).
62    #[serde(default)]
63    pub model: Option<String>,
64    /// Human-readable description of what this agent does.
65    #[serde(default)]
66    pub description: Option<String>,
67}
68
69/// LLM parameters for a single agent.
70///
71/// Why: Each agent may need different temperature, token budget, or model. The
72/// backend (OpenRouter vs. Bedrock) is no longer a boolean here — it is derived
73/// from the resolved model slug by `provider::provider_for` (#1021), so a single
74/// slug like `bedrock/...` selects the backend without a separate flag.
75/// What: Standard LLM knobs, all optional with sensible defaults.
76/// Test: `agent_config_llm_params`.
77#[derive(Debug, Clone, Serialize, Deserialize, Default)]
78pub struct LlmParams {
79    /// Sampling temperature (0.0–1.0).
80    #[serde(default)]
81    pub temperature: Option<f32>,
82    /// Maximum tokens in the LLM response.
83    #[serde(default)]
84    pub max_tokens: Option<u32>,
85    /// Model override at the `[llm]` level (lower precedence than `[agent].model`).
86    #[serde(default)]
87    pub model_override: Option<String>,
88}
89
90/// System prompt for an agent.
91///
92/// Why: Keeping the prompt in config (not source code) lets operators tune
93/// agent behavior without a Rust recompile.
94/// What: `content` is the raw prompt text; `append_skills` is a list of skill
95/// names to inject at startup.
96/// Test: `agent_config_system_prompt`.
97#[derive(Debug, Clone, Serialize, Deserialize, Default)]
98pub struct SystemPrompt {
99    /// Raw prompt text.
100    #[serde(default)]
101    pub content: String,
102    /// Skill names to inject into the prompt.
103    #[serde(default)]
104    pub append_skills: Vec<String>,
105}
106
107/// Per-agent tool permissions.
108///
109/// Why: Restricts which tools an agent may call, preventing e.g. the
110/// plan-agent from shelling out.
111/// What: `allowed` is an explicit allowlist; `None` means "all registered tools".
112/// Test: `ToolRegistry::dispatch_gated` tests exercise the allowlist.
113#[derive(Debug, Clone, Serialize, Deserialize, Default)]
114pub struct ToolsConfig {
115    /// Explicit allowlist of tool names. `None` = all tools permitted.
116    #[serde(default)]
117    pub allowed: Option<Vec<String>>,
118}
119
120/// Runner backend selection for an agent.
121///
122/// Why: Different agents may use different execution backends (subprocess,
123/// in-process, Claude Code CLI).
124/// What: `kind` selects the backend; defaults to `SubProcess`.
125/// Test: `agent_config_runner_kind`.
126#[derive(Debug, Clone, Serialize, Deserialize, Default)]
127pub struct RunnerConfig {
128    /// Which backend to use for this agent.
129    #[serde(default)]
130    pub kind: RunnerKind,
131}
132
133/// Execution backend for agent invocations.
134///
135/// Why: Abstracts over the concrete runner selected at startup so config can
136/// swap backends without code changes. As of #1029 the in-process runner is the
137/// real, production default — the M1 model-comparison harness drives a delegated
138/// sub-agent inside the PM's own process (cheap, observable, usage rolls up onto
139/// one transcript). The subprocess and Claude-Code CLI backends are retained as
140/// future variants for deployments that need process isolation.
141/// What: `InProcess` is the default (drives the sub-agent's `AgentLoop` in the
142/// same process — see `crate::runner::InProcessAgentRunner`); `SubProcess` spawns
143/// a new process via NDJSON IPC; `ClaudeCode` wraps the `claude` CLI binary.
144/// Test: `runner_kind_defaults_to_in_process`.
145#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
146#[serde(rename_all = "snake_case")]
147pub enum RunnerKind {
148    /// Drive the sub-agent's `AgentLoop` in the PM's own process (the default).
149    #[default]
150    InProcess,
151    /// Spawn a subprocess; communicate via NDJSON over stdin/stdout.
152    SubProcess,
153    /// Use the `claude` CLI binary as the runner.
154    ClaudeCode,
155}
156
157// ── Tests ─────────────────────────────────────────────────────────────────────
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    /// `AgentConfig::default()` yields the expected all-empty baseline.
164    ///
165    /// Why: #2897 Slice D retired the TOML parsing tests (`from_toml_str`
166    /// itself is gone); the actual frontmatter -> `AgentConfig` field mapping
167    /// is now covered end-to-end by `agents::md_loader::tests::*`
168    /// (`load_md_agent_base_case`, `parallel fixture` field assertions,
169    /// etc.). What remains testable in THIS module is the plain-data
170    /// `Default` contract every construction path relies on.
171    /// What: every field is the type's zero value; `tools`/`runner` are
172    /// `None`.
173    /// Test: this test.
174    #[test]
175    fn agent_config_default_is_empty() {
176        let cfg = AgentConfig::default();
177        assert_eq!(cfg.agent.name, "");
178        assert!(cfg.agent.model.is_none());
179        assert!(cfg.tools.is_none());
180        assert!(cfg.runner.is_none());
181        assert_eq!(cfg.system_prompt.content, "");
182    }
183
184    /// `RunnerKind` defaults to `InProcess` (the real default since #1029).
185    ///
186    /// Why: The acceptance criterion for #1029 makes the in-process runner the
187    /// production default; a regression that flipped it back to `SubProcess`
188    /// would silently route every undeclared agent through the wrong backend.
189    /// What: `RunnerKind::default()` equals `InProcess`; a default-constructed
190    /// `AgentConfig` (mirroring an agent with no `[runner]`/no runner
191    /// frontmatter) leaves `runner` `None` (callers treat absence as default).
192    /// Test: this test.
193    #[test]
194    fn runner_kind_defaults_to_in_process() {
195        assert_eq!(RunnerKind::default(), RunnerKind::InProcess);
196        let cfg = AgentConfig::default();
197        assert!(
198            cfg.runner.is_none(),
199            "absent runner config leaves runner None; callers apply the InProcess default"
200        );
201    }
202}