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
//! Tool error types
use thiserror::Error;
/// Tool error types
#[derive(Debug, Error)]
pub enum ToolError {
/// Tool not found
#[error("Tool not found: {0}")]
NotFound(String),
/// Invalid input
#[error("Invalid input: {0}")]
InvalidInput(String),
/// Execution error
#[error("Execution error: {0}")]
Execution(String),
/// Permission denied
#[error("Permission denied: {0}")]
PermissionDenied(String),
/// Approval required
#[error("Tool requires approval: {0}")]
ApprovalRequired(String),
/// File not found
#[error("File not found: {0}")]
FileNotFound(String),
/// I/O error
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// JSON error
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
/// Timeout
#[error("Tool execution timed out after {0}s")]
Timeout(u64),
/// Internal error
#[error("Internal error: {0}")]
Internal(String),
}
impl ToolError {
/// True when the tool never actually ran: the call was rejected before
/// execution because the name didn't resolve (`NotFound`) or the arguments
/// failed validation (`InvalidInput`). These are model tool-USE mistakes,
/// not reliability failures of the tool itself, so the feedback ledger
/// records them as `discovery_miss` rather than `tool_failure` and they're
/// kept out of a tool's success rate (#214).
pub fn is_pre_execution_miss(&self) -> bool {
matches!(self, ToolError::NotFound(_) | ToolError::InvalidInput(_))
}
}
/// Result type for tool operations
pub type Result<T> = std::result::Result<T, ToolError>;
/// Expand a leading `~` or `~/` in a user-provided path into the current
/// user's home directory. Everything else passes through unchanged.
///
/// Models routinely paste tilde paths (`~/.opencrabs/logs`) and without
/// expansion `PathBuf::is_absolute()` returns false, so the path gets
/// joined to the process working directory as literal `~` — which never
/// exists. This helper normalizes that so tools don't all have to
/// reinvent the wheel.
pub fn expand_tilde(path: &str) -> std::path::PathBuf {
use std::path::PathBuf;
if let Some(rest) = path.strip_prefix("~/")
&& let Some(home) = dirs::home_dir()
{
return home.join(rest);
}
if path == "~"
&& let Some(home) = dirs::home_dir()
{
return home;
}
PathBuf::from(path)
}
/// Inverse of `expand_tilde`: replace a leading home-dir prefix in an
/// absolute path with `~`. Falls back to the original path string when
/// no replacement applies.
///
/// Used everywhere a path lands in user-visible output OR in the
/// model's system prompt — the goal is twofold:
/// 1. Don't leak the local username (`/Users/$you/srv/...`) into
/// every prompt; that's a privacy/identity leak that also varies
/// between machines, hurting prompt-cache hit rates.
/// 2. Save tokens — `~/srv/myapp/...` is consistently shorter
/// than `/Users/alice/srv/myapp/...`.
pub fn collapse_home(path: &std::path::Path) -> String {
if let Some(home) = dirs::home_dir()
&& let Ok(rest) = path.strip_prefix(&home)
{
if rest.as_os_str().is_empty() {
return "~".to_string();
}
let suffix = rest.display().to_string();
if let Some(stripped) = suffix.strip_prefix('/') {
return format!("~/{}", stripped);
}
return format!("~/{}", suffix);
}
path.display().to_string()
}
/// Resolve a user-provided path into an absolute `PathBuf`.
///
/// 1. Leading `~` / `~/` is expanded to the user's home directory.
/// 2. Absolute paths pass through.
/// 3. Relative paths are joined to the supplied working directory.
///
/// This is the single source of truth for path resolution across all
/// path-taking tools so they stay consistent.
pub fn resolve_tool_path(
requested_path: &str,
working_directory: &std::path::Path,
) -> std::path::PathBuf {
let expanded = expand_tilde(requested_path);
if expanded.is_absolute() {
expanded
} else {
working_directory.join(expanded)
}
}
/// Resolve a path relative to the working directory.
///
/// Absolute paths pass through as-is. Relative paths are joined to the
/// working directory. For new files the parent directory must exist.
///
/// Security is enforced at the tool level via `requires_approval` and
/// capability flags — not by restricting paths to a single directory.
pub fn validate_path_safety(
requested_path: &str,
working_directory: &std::path::Path,
) -> Result<std::path::PathBuf> {
let path = resolve_tool_path(requested_path, working_directory);
// For new files, verify the parent directory exists
if !path.exists() {
let parent = path
.parent()
.ok_or_else(|| ToolError::InvalidInput("Invalid path: no parent directory".into()))?;
if !parent.exists() {
return Err(ToolError::InvalidInput(format!(
"Parent directory does not exist: {}",
parent.display()
)));
}
}
Ok(path)
}
/// Resolve a path, check it exists, and confirm it's a file.
///
/// Returns a user-friendly error message suitable for ToolResult::error()
pub fn validate_file_path(
requested_path: &str,
working_directory: &std::path::Path,
) -> std::result::Result<std::path::PathBuf, String> {
let path = match validate_path_safety(requested_path, working_directory) {
Ok(p) => p,
Err(ToolError::InvalidInput(msg)) => {
return Err(format!("Invalid path: {}", msg));
}
Err(e) => {
return Err(format!("Path validation failed: {}", e));
}
};
if !path.exists() {
return Err(format!("File not found: {}", path.display()));
}
if !path.is_file() {
return Err(format!("Path is not a file: {}", path.display()));
}
Ok(path)
}
/// Resolve a path, check it exists, and confirm it's a directory.
///
/// Similar to validate_file_path but checks for directories instead of files.
pub fn validate_directory_path(
requested_path: &str,
working_directory: &std::path::Path,
) -> std::result::Result<std::path::PathBuf, String> {
let path = match validate_path_safety(requested_path, working_directory) {
Ok(p) => p,
Err(ToolError::InvalidInput(msg)) => {
return Err(format!("Invalid path: {}", msg));
}
Err(e) => {
return Err(format!("Path validation failed: {}", e));
}
};
if !path.exists() {
return Err(format!("Directory not found: {}", path.display()));
}
if !path.is_dir() {
return Err(format!("Path is not a directory: {}", path.display()));
}
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tool_error_display() {
let err = ToolError::NotFound("test_tool".to_string());
assert_eq!(err.to_string(), "Tool not found: test_tool");
let err = ToolError::PermissionDenied("dangerous_operation".to_string());
assert_eq!(err.to_string(), "Permission denied: dangerous_operation");
}
#[test]
fn test_is_pre_execution_miss() {
// The tool never ran: name didn't resolve, or args failed validation.
assert!(ToolError::NotFound("x".into()).is_pre_execution_miss());
assert!(ToolError::InvalidInput("x".into()).is_pre_execution_miss());
// The tool ran (or tried) and genuinely errored: real reliability signal.
assert!(!ToolError::Execution("x".into()).is_pre_execution_miss());
assert!(!ToolError::PermissionDenied("x".into()).is_pre_execution_miss());
assert!(!ToolError::ApprovalRequired("x".into()).is_pre_execution_miss());
assert!(!ToolError::Timeout(5).is_pre_execution_miss());
}
#[test]
fn test_expand_tilde_prefix() {
let home = dirs::home_dir().expect("home dir required for this test");
assert_eq!(expand_tilde("~/foo/bar"), home.join("foo/bar"));
assert_eq!(expand_tilde("~"), home);
}
#[test]
fn test_expand_tilde_passthrough() {
// Tilde in the middle of a path is NOT a home reference — leave it alone.
assert_eq!(
expand_tilde("/tmp/~backup").to_string_lossy(),
"/tmp/~backup"
);
assert_eq!(expand_tilde("foo/bar").to_string_lossy(), "foo/bar");
assert_eq!(expand_tilde("/abs/path").to_string_lossy(), "/abs/path");
}
#[test]
fn test_resolve_tool_path_tilde_becomes_absolute() {
// The classic custom-provider bug: model sends `~/.opencrabs/logs`,
// cwd is `/Users/adolfo/srv/rs/opencrabs`. Before the fix this
// produced `/Users/adolfo/srv/rs/opencrabs/~/.opencrabs/logs`.
let cwd = std::path::Path::new("/Users/adolfo/srv/rs/opencrabs");
let resolved = resolve_tool_path("~/.opencrabs/logs", cwd);
let home = dirs::home_dir().expect("home dir required");
assert_eq!(resolved, home.join(".opencrabs/logs"));
assert!(resolved.is_absolute());
}
#[test]
fn test_resolve_tool_path_relative_joins_cwd() {
let cwd = std::path::Path::new("/tmp/project");
assert_eq!(
resolve_tool_path("src/main.rs", cwd),
std::path::PathBuf::from("/tmp/project/src/main.rs"),
);
}
#[test]
fn test_resolve_tool_path_absolute_passthrough() {
let cwd = std::path::Path::new("/tmp/project");
assert_eq!(
resolve_tool_path("/etc/hosts", cwd),
std::path::PathBuf::from("/etc/hosts"),
);
}
}