artifactize-tools 0.13.1

Portable scoped tools and safe file access for artifactize.
Documentation
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
464
465
466
467
468
469
470
471
472
//! Scoped read-only Agent tools.
mod fixed;
mod input;
pub use fixed::{
    Context, call_fixed, fixed_description, fixed_schema, is_url, validate_args, validate_target,
};
pub use input::Input;

mod output;
mod scan;
use output::{Entry, EntryKind, Output};

/// Bound the compiled regex automaton to limit search memory.
const REGEX_BYTES: usize = 2 * 1024 * 1024;

use std::{
    collections::{BTreeMap, BTreeSet},
    fs::File,
    io::{BufRead, BufReader, Read},
    path::Path,
};

use globset::{GlobBuilder, GlobMatcher};
use serde_json::{Value, json};
use tokio_util::sync::CancellationToken;

use crate::{
    Builtin,
    files::{self as platform, FileKind},
    scope::{self, ArtifactId, OpenError, Scope, ScopedPath},
};

use crate::{Content, ToolResult, image};

/// Bound one read's contribution to the Agent context while preserving complete lines.
pub const READ_BYTES: usize = 64 * 1024;
/// Cap serialized tool output even when match text or logical paths are very long.
pub const RESULT_BYTES: usize = 512 * 1024;
/// Reserve 1 KiB for serialized list/glob/grep keys, brackets and page metadata before
/// accumulating entries; the final serialized-result check still enforces RESULT_BYTES.
const RESULT_ENVELOPE_BYTES: usize = 1024;
/// Keep list/search pages small enough for a review; pagination exposes later entries.
const MAX_RESULTS: usize = 200;
/// Bound directory fan-out and traversal work regardless of the requested result count.
const MAX_ENTRIES: usize = 10_000;
/// Skip individual oversized files so one input cannot monopolize a search.
pub const SEARCH_FILE_BYTES: usize = 8 * 1024 * 1024;
/// Bound total search I/O across files; exhausting the budget reports truncation.
const SEARCH_BYTES: usize = 64 * 1024 * 1024;
/// Patterns are user input rather than paths, but share their bounded schema size.
const MAX_PATTERN_UNITS: usize = scope::MAX_PATH_UNITS;
/// An ordinary read starts with eighty lines; owners can page further or request
/// up to five hundred without scanning an unbounded number of lines in one call.
const DEFAULT_READ_LINES: usize = 80;
const MAX_READ_LINES: usize = 500;

pub fn description(builtin: Builtin) -> &'static str {
    match builtin {
        Builtin::Graph => {
            "Show the current project Artifact graph, including evals and input-to-consumer relations."
        }
        Builtin::Section => "Read a Markdown section in {artifactName} by its exact heading.",
        Builtin::Help => "Read a program's --help output.",
        Builtin::Open => "Open a scoped file or URL in its default application.",
        Builtin::Read => {
            "Read UTF-8 complete lines in {artifactName}. path is a relative logical file path, including child paths. Paths are relative to {artifactName} itself: use \"notes.md\", not \"{artifactName}/notes.md\". offset is 1-based (default 1); limit defaults to 80, maximum 500. Returns numbered lines preserving LF/CRLF/BOM, up to 64 KiB, startLine/endLine/lineCount, totalLines when known, truncated and nextOffset. No symlinks or binary text."
        }
        Builtin::List => {
            "List {artifactName} at a relative logical path (default root). Paths are relative to {artifactName} itself: use \"notes.md\", not \"{artifactName}/notes.md\". Sorted entries include name, path and kind. offset is 0-based; limit defaults to and cannot exceed 200. Returns totalEntries, truncated and nextOffset. Symlinks and special files are listed but never followed. Directories are limited to 10,000 entries."
        }
        Builtin::Glob => {
            "Find files in {artifactName} with a relative glob pattern (* within a path component, ** across directories). path is a relative logical directory, default root; patterns are relative to it. Paths are relative to {artifactName} itself: use \"notes.md\", not \"{artifactName}/notes.md\". Returns up to 200 sorted logical paths and truncated. Includes hidden files; ignores no files by git rules. Symlinks/special files are skipped, and traversal is limited to 10,000 entries."
        }
        Builtin::Grep => {
            "Search UTF-8 files in {artifactName} with a Rust regex, one match per matching line. path is a relative logical file or directory (default root); glob filters paths relative to it. Paths are relative to {artifactName} itself: use \"notes.md\", not \"{artifactName}/notes.md\". caseInsensitive defaults to false; maxResults defaults to and cannot exceed 200. Returns matches with path, line and text, plus truncated. Skips binary/invalid UTF-8 and symlinks; includes hidden files. Caps: 10,000 traversed entries, 8 MiB per file, 64 MiB searched, 512 KiB result. Skipped oversized files or bounded results set truncated."
        }
        Builtin::ViewImage => {
            "View one image in {artifactName} at a relative logical file path, including child paths. Paths are relative to {artifactName} itself: use \"image.png\", not \"{artifactName}/image.png\". Detects PNG, JPEG or WebP by bytes, not extension; returns an embedded image block. Requires a nonempty regular file up to 4 MiB; no symlinks, GIF, BMP or animated PNG."
        }
    }
}

pub fn input_schema(builtin: Builtin) -> Value {
    let path = json!({
        "type":"string",
        "maxLength":scope::MAX_PATH_UNITS,
        "description":"Logical path relative to the tool's Artifact, without the Artifact's name; no absolute paths, dot components, backslashes or symlinks. Empty means the root.",
    });
    let pattern = json!({"type":"string","minLength":1,"maxLength":MAX_PATTERN_UNITS});
    let integer =
        |min, max, default| json!({"type":"integer","minimum":min,"maximum":max,"default":default});
    let (properties, required) = match builtin {
        Builtin::Read => (
            json!({
                "path":path,
                "offset":integer(1, crate::MAX_SAFE_JSON_INTEGER, 1),
                "limit":integer(1, MAX_READ_LINES as u64, DEFAULT_READ_LINES as u64),
            }),
            vec!["path"],
        ),
        Builtin::List => (
            json!({
                "path":path,
                "offset":integer(0, crate::MAX_SAFE_JSON_INTEGER, 0),
                "limit":integer(1, MAX_RESULTS as u64, MAX_RESULTS as u64),
            }),
            vec![],
        ),
        Builtin::Graph => (json!({}), vec![]),
        Builtin::Glob => (json!({"pattern":pattern,"path":path}), vec!["pattern"]),
        Builtin::Grep => (
            json!({
                "pattern":pattern,
                "path":path,
                "glob":pattern,
                "caseInsensitive":{"type":"boolean","default":false},
                "maxResults":integer(1, MAX_RESULTS as u64, MAX_RESULTS as u64),
            }),
            vec!["pattern"],
        ),
        Builtin::ViewImage => (json!({"path":path}), vec!["path"]),
        Builtin::Section => (json!({"heading":pattern}), vec!["heading"]),
        Builtin::Help | Builtin::Open => (json!({}), vec![]),
    };
    json!({
        "type":"object",
        "properties":properties,
        "required":required,
        "additionalProperties":false,
    })
}

pub fn call(
    input: Input,
    root: &Path,
    scope: &Scope,
    owner: &ArtifactId,
    cancellation: &CancellationToken,
) -> ToolResult {
    let reader = Reader {
        root,
        scope,
        owner,
        cancellation,
    };
    let result = (|| {
        reader.check_cancelled()?;
        let data = match input {
            Input::Read(input) => reader.read(&input.path, input.offset, input.limit),
            Input::List(input) => {
                reader.list(reader.root_path(&input.path), input.offset, input.limit)
            }
            Input::Glob(input) => reader.glob(reader.root_path(&input.path), &input.pattern),
            Input::Grep(input) => reader.grep(reader.root_path(&input.path), &input),
            Input::ViewImage(input) => return reader.view_image(&input.path),
        }?;
        if serde_json::to_vec(&data).unwrap().len() > RESULT_BYTES {
            return Err("Built-in result exceeds 512 KiB; narrow the path or range.".into());
        }
        Ok(Content::Json {
            data: serde_json::to_value(data).expect("builtin output is JSON"),
        })
    })();
    match result {
        Ok(content) => ToolResult::success(content),
        Err(message) => ToolResult::error(message),
    }
}

struct Reader<'a> {
    root: &'a Path,
    scope: &'a Scope,
    owner: &'a ArtifactId,
    cancellation: &'a CancellationToken,
}

impl Reader<'_> {
    fn root_path<'a>(&self, path: &'a str) -> &'a str {
        if path == "." && self.scope.artifacts[self.owner].file_name().is_some() {
            ""
        } else {
            path
        }
    }

    fn check_cancelled(&self) -> Result<(), String> {
        if self.cancellation.is_cancelled() {
            Err("Agent tool call was cancelled.".into())
        } else {
            Ok(())
        }
    }

    fn location(&self, path: &str) -> Result<ScopedPath, String> {
        self.scope
            .resolve_path(self.owner, path)
            .map_err(|e| e.to_string())
    }

    /// Open the resolved `location` of the logical `path`, which a missing entry names.
    fn open(&self, path: &str, location: &ScopedPath) -> Result<File, String> {
        let artifact = &self.scope.artifacts[&location.artifact_id];
        scope::open_input(self.root, artifact, &location.path).map_err(|error| match error {
            OpenError::NotFound => missing(self.owner, path),
            error => error.to_string(),
        })
    }

    fn view_image(&self, path: &str) -> Result<Content, String> {
        let location = self.location(path)?;
        let bytes = image::read(self.open(path, &location)?)?;
        self.check_cancelled()?;
        image::normalize(&bytes, None)
    }

    fn read(&self, path: &str, offset: usize, limit: usize) -> Result<Output, String> {
        let location = self.location(path)?;
        let file = self.open(path, &location)?;
        if !file.metadata().map_err(|e| e.to_string())?.is_file() {
            return Err("Reading requires a regular file; list the directory first.".into());
        }
        let mut reader = BufReader::new(file);
        let mut scan = scan::Read::new(offset, limit);
        let end = loop {
            self.check_cancelled()?;
            let bytes = reader.fill_buf().map_err(|error| error.to_string())?;
            if bytes.is_empty() {
                break scan::ReadEnd::Complete;
            }
            match scan.push(bytes)? {
                scan::ReadStep::More(consumed) => reader.consume(consumed),
                scan::ReadStep::Full(consumed) => {
                    reader.consume(consumed);
                    break if reader
                        .fill_buf()
                        .map_err(|error| error.to_string())?
                        .is_empty()
                    {
                        scan::ReadEnd::Complete
                    } else {
                        scan::ReadEnd::Bounded
                    };
                }
                scan::ReadStep::Bounded => break scan::ReadEnd::Bounded,
            }
        };
        Ok(Output::Read(output::Read {
            artifact_id: self.owner.clone(),
            resolved_artifact_id: location.artifact_id,
            path: path.to_owned(),
            rows: scan.finish(end)?,
        }))
    }

    fn entries(&self, path: &str) -> Result<Vec<Entry>, String> {
        self.check_cancelled()?;
        let location = self.location(path)?;
        let file = self.open(path, &location)?;
        if !file.metadata().map_err(|e| e.to_string())?.is_dir() {
            return Err("Listing requires a directory.".into());
        }
        let owner = &self.scope.artifacts[&location.artifact_id];
        let mut entries = BTreeMap::new();
        if let Some(name) = owner.file_name() {
            let target = scope::open_child(&file, std::ffi::OsStr::new(name))
                .map_err(|error| error.to_string())?;
            if !target.metadata().map_err(|e| e.to_string())?.is_file() {
                return Err("File Artifact target must remain a regular file.".into());
            }
            entries.insert(name.to_owned(), EntryKind::File);
        } else {
            // Enumerate the pinned directory, not a path that could have been replaced by a link.
            let directory =
                platform::read_dir(&file).map_err(|_| "Cannot list Artifact directory.")?;
            for entry in directory {
                self.check_cancelled()?;
                if entries.len() >= MAX_ENTRIES {
                    return Err(
                        "Directory exceeds the 10,000-entry listing limit; choose a narrower path."
                            .into(),
                    );
                }
                let entry = entry.map_err(|_| "Cannot list Artifact entry.")?;
                let name = entry
                    .file_name()
                    .into_string()
                    .map_err(|_| "Artifact paths must be UTF-8.")?;
                let kind = entry
                    .file_type()
                    .map_err(|_| "Cannot inspect Artifact entry.")?;
                let kind = match kind {
                    FileKind::File => EntryKind::File,
                    FileKind::Directory => EntryKind::Directory,
                    FileKind::Symlink => EntryKind::Symlink,
                    FileKind::Other => EntryKind::Other,
                };
                entries.insert(name, kind);
            }
        }
        Ok(entries
            .into_iter()
            .map(|(name, kind)| Entry {
                kind,
                path: join(path, &name),
                name,
            })
            .collect())
    }

    fn list(&self, path: &str, offset: usize, limit: usize) -> Result<Output, String> {
        let entries = self.entries(path)?;
        scan::list(self.owner.clone(), path, entries, offset, limit).map(Output::List)
    }

    fn files(&self, path: &str) -> Result<(BTreeSet<String>, bool), String> {
        let location = self.location(path)?;
        if self
            .open(path, &location)?
            .metadata()
            .map_err(|e| e.to_string())?
            .is_file()
        {
            return Ok((BTreeSet::from([path.into()]), false));
        }
        let mut pending = vec![path.to_owned()];
        let mut files = BTreeSet::new();
        let mut seen = 0;
        let mut truncated = false;
        while let Some(path) = pending.pop() {
            self.check_cancelled()?;
            for entry in self.entries(&path)? {
                if seen == MAX_ENTRIES {
                    truncated = true;
                    break;
                }
                seen += 1;
                match entry.kind {
                    EntryKind::File => {
                        files.insert(entry.path);
                    }
                    EntryKind::Directory => pending.push(entry.path),
                    EntryKind::Symlink | EntryKind::Other => {}
                }
            }
            if truncated {
                break;
            }
        }
        Ok((files, truncated))
    }

    fn glob(&self, path: &str, matcher: &GlobMatcher) -> Result<Output, String> {
        let location = self.location(path)?;
        if !self
            .open(path, &location)?
            .metadata()
            .map_err(|error| error.to_string())?
            .is_dir()
        {
            return Err("Glob path must be a directory.".into());
        }
        let (files, truncated) = self.files(path)?;
        let mut scan = scan::Glob::new(path, matcher, truncated);
        for file in files {
            self.check_cancelled()?;
            match scan.push(&file) {
                scan::Selection::Skip => continue,
                scan::Selection::Full => break,
                scan::Selection::Added => {}
            }
            // The selected file can have changed since directory enumeration.
            if !self
                .open(&file, &self.location(&file)?)?
                .metadata()
                .map_err(|error| error.to_string())?
                .is_file()
            {
                return Err("Glob target is no longer a regular file.".into());
            }
        }
        Ok(Output::Glob(scan.finish()))
    }

    fn grep(&self, path: &str, input: &input::Grep) -> Result<Output, String> {
        let (files, truncated) = self.files(path)?;
        let mut scan = scan::Grep::new(path, input, truncated);
        'files: for file in files {
            self.check_cancelled()?;
            if !scan.allows(&file) {
                continue;
            }
            let input = self.open(&file, &self.location(&file)?)?;
            let metadata = input.metadata().map_err(|error| error.to_string())?;
            if !metadata.is_file() {
                return Err("Grep requires regular files.".into());
            }
            let limit = match scan.file(metadata.len()) {
                scan::FileRead::Read(limit) => limit,
                scan::FileRead::Skip => continue,
                scan::FileRead::Full => break,
            };
            let mut contents = Vec::new();
            input
                .take(limit)
                .read_to_end(&mut contents)
                .map_err(|_| "Cannot read Artifact file.")?;
            match scan.received(contents.len()) {
                scan::Received::Skip => continue,
                scan::Received::Full => break,
                scan::Received::Text => {}
            }
            let Some(text) = scan::text(&contents) else {
                continue;
            };
            for (index, line) in text.split_inclusive('\n').enumerate() {
                self.check_cancelled()?;
                if !scan.line(&file, index, line) {
                    break 'files;
                }
            }
        }
        Ok(Output::Grep(scan.finish()))
    }
}

/// A missing path, with a hint when a model prefixed it with the Artifact's own name.
fn missing(owner: &ArtifactId, path: &str) -> String {
    match path.strip_prefix(owner.as_str()) {
        Some("") => format!(
            "No {path:?} in Artifact {owner}; paths are relative to the Artifact, without its name (its root is \"\")."
        ),
        Some(rest) if rest.starts_with('/') => format!(
            "No {path:?} in Artifact {owner}; paths are relative to the Artifact (for example {:?}), without its name.",
            &rest[1..]
        ),
        _ => format!("No {path:?} in Artifact {owner}."),
    }
}

fn join(base: &str, name: &str) -> String {
    if base.is_empty() {
        name.into()
    } else {
        format!("{base}/{name}")
    }
}

fn relative<'a>(base: &str, file: &'a str) -> &'a str {
    if file == base {
        file.rsplit('/').next().unwrap()
    } else if base.is_empty() {
        file
    } else {
        file.strip_prefix(base).unwrap().strip_prefix('/').unwrap()
    }
}

fn glob(pattern: &str) -> Result<GlobMatcher, String> {
    if pattern.starts_with('/')
        || pattern.contains(['\\', '\0', ':'])
        || pattern
            .split('/')
            .any(|part| part == "." || part == ".." || part.is_empty())
    {
        return Err("Glob pattern must be relative without traversal components.".into());
    }
    GlobBuilder::new(pattern)
        .literal_separator(true)
        .build()
        .map(|glob| glob.compile_matcher())
        .map_err(|_| "Invalid glob pattern.".into())
}