claux 20260730.0.1

Terminal AI coding assistant with tool execution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
pub(crate) mod agent;
mod bash;
mod edit;
mod glob;
mod grep;
pub(crate) mod mcp;
pub(crate) mod read;
mod search_filter;
pub(crate) mod todo;
mod web_fetch;
mod write;

use anyhow::Result;
use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;

use crate::api::ToolDefinition;
use crate::command_sandbox::CommandSandbox;
use crate::sandbox::SandboxPolicy;

/// Output from a tool execution.
#[derive(Debug, Clone)]
pub struct ToolOutput {
    pub content: String,
    pub is_error: bool,
}

fn interrupted_output() -> ToolOutput {
    ToolOutput {
        content: "Interrupted by user.".to_string(),
        is_error: true,
    }
}

fn sandbox_denied_output(error: anyhow::Error) -> ToolOutput {
    ToolOutput {
        content: error.to_string(),
        is_error: true,
    }
}

/// Every tool implements this trait.
#[async_trait]
pub trait Tool: Send + Sync {
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn input_schema(&self) -> Value;
    fn is_read_only(&self) -> bool;

    /// Clear state that must not carry into another conversation.
    fn reset_session(&self) {}

    /// Short human-readable summary of what this invocation does.
    /// Shown to the user while the tool runs.
    fn summarize(&self, _input: &Value) -> String {
        self.name().to_string()
    }

    /// Execute the tool. `cancel` is signaled if the user has interrupted —
    /// long-running tools should monitor it and clean up. Tools without a
    /// natural interrupt point can ignore it.
    async fn execute(&self, input: Value, cancel: CancellationToken) -> Result<ToolOutput>;
}

/// Registry holding all available tools.
pub struct ToolRegistry {
    tools: Vec<Box<dyn Tool>>,
}

impl ToolRegistry {
    /// Create a registry with Agent tool using a provider factory. The
    /// `permission_mode` is inherited by sub-agents the Agent tool spawns,
    /// so a sub-agent can't run with more authority than the parent session.
    pub fn new_with_agent_factory(
        factory: agent::ProviderFactory,
        model: String,
        permission_mode: crate::permissions::PermissionMode,
        sandbox_policy: Arc<SandboxPolicy>,
        command_sandbox: Arc<CommandSandbox>,
    ) -> Self {
        let todo_state = todo::new_todo_state();
        Self {
            tools: vec![
                Box::new(read::ReadTool::new(sandbox_policy.clone())),
                Box::new(write::WriteTool::new(sandbox_policy.clone())),
                Box::new(edit::EditTool::new(sandbox_policy.clone())),
                Box::new(glob::GlobTool::new(sandbox_policy.clone())),
                Box::new(grep::GrepTool::new(sandbox_policy.clone())),
                Box::new(bash::BashTool::new(command_sandbox.clone())),
                Box::new(web_fetch::WebFetchTool::new()),
                Box::new(agent::AgentTool::new(
                    factory,
                    model,
                    permission_mode,
                    sandbox_policy,
                    command_sandbox,
                )),
                Box::new(todo::TodoWriteTool::new(todo_state)),
            ],
        }
    }

    /// Create a registry without Agent (for sub-agents to prevent recursion).
    pub fn without_agent(
        sandbox_policy: Arc<SandboxPolicy>,
        command_sandbox: Arc<CommandSandbox>,
    ) -> Self {
        let todo_state = todo::new_todo_state();
        Self {
            tools: vec![
                Box::new(read::ReadTool::new(sandbox_policy.clone())),
                Box::new(write::WriteTool::new(sandbox_policy.clone())),
                Box::new(edit::EditTool::new(sandbox_policy.clone())),
                Box::new(glob::GlobTool::new(sandbox_policy.clone())),
                Box::new(grep::GrepTool::new(sandbox_policy)),
                Box::new(bash::BashTool::new(command_sandbox)),
                Box::new(web_fetch::WebFetchTool::new()),
                Box::new(todo::TodoWriteTool::new(todo_state)),
            ],
        }
    }

    /// Create a basic registry (no Agent).
    #[cfg(test)]
    pub fn new() -> Self {
        Self::without_agent_for_tests()
    }

    #[cfg(test)]
    pub fn without_agent_for_tests() -> Self {
        Self::without_agent(
            Arc::new(SandboxPolicy::unrestricted_for_tests()),
            Arc::new(CommandSandbox::unrestricted_for_tests()),
        )
    }

    /// Add external tools (e.g. from MCP servers).
    pub fn add_tools(&mut self, tools: Vec<Box<dyn Tool>>) {
        self.tools.extend(tools);
    }

    /// Clear conversation-scoped state held by registered tools.
    pub fn reset_session(&self) {
        for tool in &self.tools {
            tool.reset_session();
        }
    }

    /// Get tool definitions for the API request.
    pub fn definitions(&self) -> Vec<ToolDefinition> {
        self.tools
            .iter()
            .map(|t| ToolDefinition {
                name: t.name().to_string(),
                description: t.description().to_string(),
                input_schema: t.input_schema(),
            })
            .collect()
    }

    /// Execute a tool by name.
    ///
    /// Never fails: an unknown tool name or a tool-level error is returned
    /// as an error ToolOutput so it becomes a tool_result block the model
    /// can see and recover from. Propagating an Err here would abort the
    /// whole turn and leave a dangling tool_use in history, which the API
    /// rejects on the next request.
    pub async fn execute(&self, name: &str, input: Value, cancel: CancellationToken) -> ToolOutput {
        let Some(tool) = self.tools.iter().find(|t| t.name() == name) else {
            let available: Vec<&str> = self.tools.iter().map(|t| t.name()).collect();
            return ToolOutput {
                content: format!(
                    "Unknown tool: {name}. Available tools: {}",
                    available.join(", ")
                ),
                is_error: true,
            };
        };

        match tool.execute(input, cancel).await {
            Ok(output) => output,
            Err(e) => ToolOutput {
                content: format!("Tool {name} failed: {e}"),
                is_error: true,
            },
        }
    }

    /// Get a human-readable summary of what the tool invocation will do.
    pub fn summarize(&self, name: &str, input: &Value) -> String {
        self.tools
            .iter()
            .find(|t| t.name() == name)
            .map(|t| t.summarize(input))
            .unwrap_or_else(|| name.to_string())
    }

    /// Check if a tool is read-only.
    pub fn is_read_only(&self, name: &str) -> bool {
        self.tools
            .iter()
            .find(|t| t.name() == name)
            .is_some_and(|t| t.is_read_only())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn registry_has_core_tools() {
        let reg = ToolRegistry::new();
        let defs = reg.definitions();
        let names: Vec<&str> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(names.contains(&"Read"));
        assert!(names.contains(&"Write"));
        assert!(names.contains(&"Edit"));
        assert!(names.contains(&"Glob"));
        assert!(names.contains(&"Grep"));
        assert!(names.contains(&"Bash"));
    }

    #[test]
    fn registry_without_agent_has_no_agent() {
        let reg = ToolRegistry::without_agent_for_tests();
        let defs = reg.definitions();
        let names: Vec<&str> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(!names.contains(&"Agent"));
    }

    #[test]
    fn registry_with_agent_has_agent() {
        use crate::api::AnthropicProvider;
        use crate::config::AnthropicApiKey;

        let factory: agent::ProviderFactory = Box::new(|| {
            Box::new(AnthropicProvider::new(
                AnthropicApiKey::new("fake".into()),
                "model",
            ))
        });
        let reg = ToolRegistry::new_with_agent_factory(
            factory,
            "model".into(),
            crate::permissions::PermissionMode::Default,
            Arc::new(SandboxPolicy::unrestricted_for_tests()),
            Arc::new(CommandSandbox::unrestricted_for_tests()),
        );
        let defs = reg.definitions();
        let names: Vec<&str> = defs.iter().map(|d| d.name.as_str()).collect();
        assert!(names.contains(&"Agent"));
    }

    #[tokio::test]
    async fn native_tools_enforce_workspace_roots() {
        let parent = tempfile::tempdir().unwrap();
        let workspace = parent.path().join("workspace");
        std::fs::create_dir(&workspace).unwrap();
        std::fs::write(workspace.join("inside.txt"), "inside").unwrap();
        let outside = parent.path().join("outside.txt");
        std::fs::write(&outside, "outside").unwrap();
        let registry = ToolRegistry::without_agent(
            Arc::new(SandboxPolicy::workspace_only(&workspace).unwrap()),
            Arc::new(CommandSandbox::unrestricted_for_tests()),
        );
        let cancel = CancellationToken::new();

        let inside_read = registry
            .execute(
                "Read",
                json!({"file_path": workspace.join("inside.txt")}),
                cancel.clone(),
            )
            .await;
        assert!(!inside_read.is_error);

        let outside_read = registry
            .execute("Read", json!({"file_path": &outside}), cancel.clone())
            .await;
        assert!(outside_read.is_error);
        assert!(outside_read.content.contains("sandbox denied read"));

        let outside_write = registry
            .execute(
                "Write",
                json!({
                    "file_path": parent.path().join("created-outside.txt"),
                    "content": "must not be written"
                }),
                cancel,
            )
            .await;
        assert!(outside_write.is_error);
        assert!(outside_write.content.contains("sandbox denied write"));
        assert!(!parent.path().join("created-outside.txt").exists());
    }

    #[tokio::test]
    async fn unrestricted_native_tools_allow_paths_outside_the_workspace() {
        let parent = tempfile::tempdir().unwrap();
        let workspace = parent.path().join("workspace");
        std::fs::create_dir(&workspace).unwrap();
        let outside = parent.path().join("outside.txt");
        std::fs::write(&outside, "outside").unwrap();
        let registry = ToolRegistry::without_agent(
            Arc::new(SandboxPolicy::unrestricted(&workspace).unwrap()),
            Arc::new(CommandSandbox::unrestricted_for_tests()),
        );

        let read = registry
            .execute(
                "Read",
                json!({"file_path": &outside}),
                CancellationToken::new(),
            )
            .await;
        assert!(!read.is_error);
        assert!(read.content.contains("outside"));

        let created = parent.path().join("created-outside.txt");
        let write = registry
            .execute(
                "Write",
                json!({"file_path": &created, "content": "created"}),
                CancellationToken::new(),
            )
            .await;
        assert!(!write.is_error);
        assert_eq!(std::fs::read_to_string(created).unwrap(), "created");
    }

    #[tokio::test]
    async fn glob_rejects_patterns_that_escape_the_workspace() {
        let workspace = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::without_agent(
            Arc::new(SandboxPolicy::workspace_only(workspace.path()).unwrap()),
            Arc::new(CommandSandbox::unrestricted_for_tests()),
        );

        let output = registry
            .execute(
                "Glob",
                json!({"pattern": "../**/*", "path": workspace.path()}),
                CancellationToken::new(),
            )
            .await;

        assert!(output.is_error);
        assert!(output
            .content
            .contains("search pattern escapes the authorized base"));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn native_tools_reject_symlink_escapes() {
        use std::os::unix::fs::symlink;

        let parent = tempfile::tempdir().unwrap();
        let workspace = parent.path().join("workspace");
        let outside = parent.path().join("outside");
        std::fs::create_dir(&workspace).unwrap();
        std::fs::create_dir(&outside).unwrap();
        std::fs::write(outside.join("secret.txt"), "secret").unwrap();
        symlink(&outside, workspace.join("escape")).unwrap();
        let registry = ToolRegistry::without_agent(
            Arc::new(SandboxPolicy::workspace_only(&workspace).unwrap()),
            Arc::new(CommandSandbox::unrestricted_for_tests()),
        );

        let read = registry
            .execute(
                "Read",
                json!({"file_path": workspace.join("escape/secret.txt")}),
                CancellationToken::new(),
            )
            .await;
        assert!(read.is_error);
        assert!(read.content.contains("sandbox denied read"));

        let write = registry
            .execute(
                "Write",
                json!({
                    "file_path": workspace.join("escape/new.txt"),
                    "content": "must not be written"
                }),
                CancellationToken::new(),
            )
            .await;
        assert!(write.is_error);
        assert!(write.content.contains("sandbox denied write"));
        assert!(!outside.join("new.txt").exists());
    }

    #[test]
    fn read_tools_are_read_only() {
        let reg = ToolRegistry::new();
        assert!(reg.is_read_only("Read"));
        assert!(reg.is_read_only("Glob"));
        assert!(reg.is_read_only("Grep"));
    }

    #[test]
    fn write_tools_are_not_read_only() {
        let reg = ToolRegistry::new();
        assert!(!reg.is_read_only("Write"));
        assert!(!reg.is_read_only("Edit"));
        assert!(!reg.is_read_only("Bash"));
    }

    #[test]
    fn unknown_tool_is_not_read_only() {
        let reg = ToolRegistry::new();
        assert!(!reg.is_read_only("NonexistentTool"));
    }

    #[tokio::test]
    async fn execute_unknown_tool_returns_error_output() {
        let reg = ToolRegistry::new();
        let output = reg
            .execute(
                "FakeTool",
                serde_json::json!({}),
                tokio_util::sync::CancellationToken::new(),
            )
            .await;
        assert!(output.is_error);
        assert!(output.content.contains("Unknown tool: FakeTool"));
        // The model should be able to self-correct from the message
        assert!(output.content.contains("Read"));
    }

    #[tokio::test]
    async fn execute_bad_params_returns_error_output() {
        let reg = ToolRegistry::new();
        // Read requires file_path; missing it must not abort the turn
        let output = reg
            .execute(
                "Read",
                serde_json::json!({}),
                tokio_util::sync::CancellationToken::new(),
            )
            .await;
        assert!(output.is_error);
        assert!(output.content.contains("Read"));
    }

    #[test]
    fn all_tools_have_valid_schemas() {
        let reg = ToolRegistry::new();
        for def in reg.definitions() {
            assert!(!def.name.is_empty());
            assert!(!def.description.is_empty());
            assert_eq!(def.input_schema["type"], "object");
            assert!(def.input_schema.get("properties").is_some());
        }
    }
}