Skip to main content

trusty_code/tools/
mod.rs

1//! Tool system for tcode — traits, registry, and the delegate tool.
2//!
3//! Why: The PM loop and sub-agent loops need a polymorphic tool dispatch layer
4//! so new capabilities (web search, file ops, memory, etc.) plug in without
5//! touching the core orchestration code. This module is the assembly point.
6//! What: Re-exports `ToolExecutor`, `AgentRunner`, `RunContext`, `AgentOutput`,
7//! `SearchProvider`, `SearchResult`, `SkillResolver`, and `ToolResult` from
8//! `traits`; `ToolRegistry` from `registry`; `DelegateToAgentTool` from
9//! `delegate`; `FinishTaskTool` (#2072) from `finish_task`; and `UseSkillTool`
10//! plus `USE_SKILL_TOOL_NAME` (#2069's progressive-disclosure on-invoke
11//! loader; the constant lets #2070's compaction pin skill outputs by tool
12//! name) from `skill`.
13//! Test: Unit tests live in each submodule; integration tests use
14//! `DelegateToAgentTool` with a `MockAgentRunner`.
15
16pub mod bash;
17pub mod delegate;
18pub mod finish_task;
19pub mod fs;
20pub mod registry;
21pub mod skill;
22pub mod traits;
23
24// Flat re-exports for `crate::tools::*` convenience.
25#[allow(unused_imports)]
26pub use bash::BashTool;
27#[allow(unused_imports)]
28pub use delegate::DelegateToAgentTool;
29pub use finish_task::{
30    FINISH_TASK_TOOL_NAME, FinishTaskArgs, FinishTaskTool, render_finish_summary,
31};
32pub use fs::{EditTool, ReadFileTool, WriteFileTool};
33pub use registry::ToolRegistry;
34pub use skill::{USE_SKILL_TOOL_NAME, UseSkillTool};
35pub use traits::{
36    AgentOutput, AgentRunner, HistoryMessage, RunContext, SearchProvider, SearchResult,
37    ServiceTier, SkillResolver, ToolExecutor, ToolResult,
38};
39
40// ── Registry integration tests for the FS tools ──────────────────────────────
41
42#[cfg(test)]
43mod fs_registry_tests {
44    use std::sync::Arc;
45
46    use serde_json::json;
47
48    use super::{EditTool, ReadFileTool, ToolRegistry, WriteFileTool};
49
50    /// All three FS tools register and appear in `schemas()`.
51    ///
52    /// Why: Verifies the complete tool plug-in lifecycle — register, schema emit,
53    /// dispatch — for the fs tool set as a group.
54    /// What: Registers `read_file`, `write_file`, and `edit`; asserts all three
55    /// schema names and that `dispatch_gated` routes to each.
56    /// Test: This test.
57    #[tokio::test]
58    async fn fs_tools_register_and_appear_in_schemas() {
59        let tmp = tempfile::tempdir().expect("tempdir");
60        let mut reg = ToolRegistry::new();
61        reg.register(Arc::new(ReadFileTool::new(tmp.path())));
62        reg.register(Arc::new(WriteFileTool::new(tmp.path())));
63        reg.register(Arc::new(EditTool::new(tmp.path())));
64
65        assert!(reg.contains("read_file"), "read_file must be registered");
66        assert!(reg.contains("write_file"), "write_file must be registered");
67        assert!(reg.contains("edit"), "edit must be registered");
68
69        let schemas = reg.schemas();
70        assert_eq!(schemas.len(), 3, "exactly three tools");
71
72        let names: Vec<&str> = schemas
73            .iter()
74            .filter_map(|s| s["function"]["name"].as_str())
75            .collect();
76        assert!(names.contains(&"read_file"));
77        assert!(names.contains(&"write_file"));
78        assert!(names.contains(&"edit"));
79    }
80
81    /// `dispatch_gated` routes `write_file` correctly.
82    ///
83    /// Why: Verifies the dispatch path end-to-end for an fs tool.
84    /// What: Register `write_file`, dispatch with valid args, assert file written.
85    /// Test: This test.
86    #[tokio::test]
87    async fn dispatch_gated_routes_write_file() {
88        let tmp = tempfile::tempdir().expect("tempdir");
89        let mut reg = ToolRegistry::new();
90        reg.register(Arc::new(WriteFileTool::new(tmp.path())));
91
92        let result = reg
93            .dispatch_gated(
94                "write_file",
95                json!({"path": "via_registry.txt", "content": "registry dispatch test"}),
96                None,
97            )
98            .await;
99
100        assert!(
101            !result.is_error(),
102            "dispatch should succeed: {}",
103            result.content()
104        );
105        let content = std::fs::read_to_string(tmp.path().join("via_registry.txt")).expect("read");
106        assert_eq!(content, "registry dispatch test");
107    }
108
109    /// `dispatch_gated` routes `read_file` and returns content.
110    ///
111    /// Why: Verifies the read dispatch path end-to-end.
112    /// What: Write a file, register `read_file`, dispatch, assert content in result.
113    /// Test: This test.
114    #[tokio::test]
115    async fn dispatch_gated_routes_read_file() {
116        let tmp = tempfile::tempdir().expect("tempdir");
117        std::fs::write(tmp.path().join("test.txt"), "hello from registry").expect("seed");
118
119        let mut reg = ToolRegistry::new();
120        reg.register(Arc::new(ReadFileTool::new(tmp.path())));
121
122        let result = reg
123            .dispatch_gated("read_file", json!({"path": "test.txt"}), None)
124            .await;
125
126        assert!(
127            !result.is_error(),
128            "dispatch should succeed: {}",
129            result.content()
130        );
131        assert!(result.content().contains("hello from registry"));
132    }
133
134    /// `dispatch_gated` routes `edit` and the file is modified.
135    ///
136    /// Why: Verifies the edit dispatch path end-to-end.
137    /// What: Write `old`, register `edit`, dispatch replace, assert `new` on disk.
138    /// Test: This test.
139    #[tokio::test]
140    async fn dispatch_gated_routes_edit() {
141        let tmp = tempfile::tempdir().expect("tempdir");
142        std::fs::write(tmp.path().join("target.py"), "x = 0\n").expect("seed");
143
144        let mut reg = ToolRegistry::new();
145        reg.register(Arc::new(EditTool::new(tmp.path())));
146
147        let result = reg
148            .dispatch_gated(
149                "edit",
150                json!({"path": "target.py", "old_string": "x = 0", "new_string": "x = 99"}),
151                None,
152            )
153            .await;
154
155        assert!(
156            !result.is_error(),
157            "dispatch should succeed: {}",
158            result.content()
159        );
160        let content = std::fs::read_to_string(tmp.path().join("target.py")).expect("read");
161        assert!(content.contains("x = 99"));
162    }
163}