Skip to main content

harn_hostlib/tools/
mod.rs

1//! Deterministic tools capability.
2//!
3//! Provides search (ripgrep via `grep-searcher` + `ignore`), file I/O,
4//! listing, file outline, git inspection, and
5//! process lifecycle (`run_command`, `wait_command`, `run_test`,
6//! `run_build_command`, `inspect_test_results`, `manage_packages`,
7//! `cancel_handle`).
8//!
9//! Implementation status:
10//!
11//! | Method                  | Status                          |
12//! |-------------------------|---------------------------------|
13//! | `search`                | implemented                     |
14//! | `read_file`             | implemented                     |
15//! | `write_file`            | implemented                     |
16//! | `delete_file`           | implemented                     |
17//! | `list_directory`        | implemented                     |
18//! | `get_file_outline`      | implemented (regex extractor)   |
19//! | `git`                   | implemented (system git CLI)    |
20//! | `run_command`           | implemented                     |
21//! | `wait_command`          | implemented                     |
22//! | `run_test`              | implemented                     |
23//! | `run_build_command`     | implemented                     |
24//! | `inspect_test_results`  | implemented                     |
25//! | `manage_packages`       | implemented                     |
26//! | `cancel_handle`         | implemented                     |
27//!
28//! ### Per-session opt-in
29//!
30//! All deterministic tools are gated by a per-thread feature flag.
31//! Pipelines must call `hostlib_enable("tools:deterministic")` (registered
32//! by [`ToolsCapability::register_builtins`]) before any of the tool
33//! methods will execute. Until then, calls return
34//! [`HostlibError::Backend`] with an explanatory message. The per-session
35//! opt-in model keeps the deterministic-tool surface sandbox-friendly.
36
37use harn_vm::VmDictExt;
38
39use harn_vm::VmValue;
40
41use crate::error::HostlibError;
42use crate::registry::{BuiltinRegistry, HostlibCapability};
43
44pub(crate) mod args;
45mod cancel_handle;
46mod diagnostics;
47mod file_io;
48mod git;
49mod inspect_test_results;
50mod lang;
51pub mod long_running;
52mod manage_packages;
53mod outline;
54mod payload;
55pub mod permissions;
56mod proc;
57mod read_command_output;
58mod response;
59mod run_build_command;
60mod run_command;
61mod run_test;
62mod search;
63mod test_parsers;
64mod toolchain_facts;
65mod wait_command;
66
67pub use permissions::FEATURE_TOOLS_DETERMINISTIC;
68
69/// Tools capability handle.
70#[derive(Default)]
71pub struct ToolsCapability;
72
73impl HostlibCapability for ToolsCapability {
74    fn module_name(&self) -> &'static str {
75        "tools"
76    }
77
78    fn register_builtins(&self, registry: &mut BuiltinRegistry) {
79        // Register the session-cleanup hook once per process so long-running
80        // tool handles are killed when the agent-loop session ends.
81        long_running::register_cleanup_hook();
82
83        registry.register_gated_fn("tools", "hostlib_tools_search", "search", search::run);
84        registry.register_gated_fn(
85            "tools",
86            "hostlib_tools_read_file",
87            "read_file",
88            file_io::read_file,
89        );
90        registry.register_gated_fn(
91            "tools",
92            "hostlib_tools_write_file",
93            "write_file",
94            file_io::write_file,
95        );
96        registry.register_gated_fn(
97            "tools",
98            "hostlib_tools_delete_file",
99            "delete_file",
100            file_io::delete_file,
101        );
102        registry.register_gated_fn(
103            "tools",
104            "hostlib_tools_list_directory",
105            "list_directory",
106            file_io::list_directory,
107        );
108        registry.register_gated_fn(
109            "tools",
110            "hostlib_tools_get_file_outline",
111            "get_file_outline",
112            outline::run,
113        );
114        registry.register_gated_fn("tools", "hostlib_tools_git", "git", git::run);
115
116        registry.register_gated_fn(
117            "tools",
118            "hostlib_tools_run_command",
119            "run_command",
120            run_command::handle,
121        );
122        registry.register_gated_fn(
123            "tools",
124            read_command_output::NAME,
125            "read_command_output",
126            read_command_output::handle,
127        );
128        registry.register_gated_fn(
129            "tools",
130            wait_command::NAME,
131            "wait_command",
132            wait_command::handle,
133        );
134        registry.register_gated_fn(
135            "tools",
136            "hostlib_tools_run_test",
137            "run_test",
138            run_test::handle,
139        );
140        registry.register_gated_fn(
141            "tools",
142            "hostlib_tools_run_build_command",
143            "run_build_command",
144            run_build_command::handle,
145        );
146        registry.register_gated_fn(
147            "tools",
148            "hostlib_tools_inspect_test_results",
149            "inspect_test_results",
150            inspect_test_results::handle,
151        );
152        registry.register_gated_fn(
153            "tools",
154            "hostlib_tools_manage_packages",
155            "manage_packages",
156            manage_packages::handle,
157        );
158        registry.register_gated_fn(
159            "tools",
160            cancel_handle::NAME,
161            "cancel_handle",
162            cancel_handle::handle,
163        );
164        registry.register_gated_fn(
165            "tools",
166            toolchain_facts::NAME,
167            "toolchain_facts",
168            toolchain_facts::handle,
169        );
170
171        // The opt-in builtin lives in the `tools` module so embedders that
172        // don't compose `ToolsCapability` don't accidentally expose it.
173        registry.register_fn("tools", "hostlib_enable", "enable", handle_enable);
174    }
175}
176
177/// Implementation of the `hostlib_enable` builtin. Accepts either a bare
178/// string (`hostlib_enable("tools:deterministic")`) or a dict carrying a
179/// `feature` key (`hostlib_enable({feature: "..."})`) so callers can
180/// supply structured payloads in the future without breaking back-compat.
181fn handle_enable(args: &[VmValue]) -> Result<VmValue, HostlibError> {
182    let feature = match args.first() {
183        Some(VmValue::String(s)) => s.to_string(),
184        Some(VmValue::Dict(dict)) => match dict.get("feature") {
185            Some(VmValue::String(s)) => s.to_string(),
186            _ => {
187                return Err(HostlibError::MissingParameter {
188                    builtin: "hostlib_enable",
189                    param: "feature",
190                });
191            }
192        },
193        _ => {
194            return Err(HostlibError::MissingParameter {
195                builtin: "hostlib_enable",
196                param: "feature",
197            });
198        }
199    };
200
201    match feature.as_str() {
202        permissions::FEATURE_TOOLS_DETERMINISTIC => {
203            let newly_enabled = permissions::enable(&feature);
204            let mut map: harn_vm::value::DictMap = harn_vm::value::DictMap::new();
205            map.put_str("feature", feature);
206            map.insert(harn_vm::value::intern_key("enabled"), VmValue::Bool(true));
207            map.insert(
208                harn_vm::value::intern_key("newly_enabled"),
209                VmValue::Bool(newly_enabled),
210            );
211            Ok(VmValue::dict(map))
212        }
213        other => Err(HostlibError::InvalidParameter {
214            builtin: "hostlib_enable",
215            param: "feature",
216            message: format!("unknown feature `{other}`; supported: [`tools:deterministic`]"),
217        }),
218    }
219}