Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ The Plugin inserts one Pack, `runvil/std`, which gives a run the standard file t

| Lua id | Name the model calls | In this build |
|---|---|---|
| `std/read` | `Read` | Returns a window of a text file's lines, each with its line number |
| `std/read` | `Read` | Returns a window of a text file's lines, numbering every 10th line |
| `std/dir` | `ListDir` | Returns a bullet tree of a directory, listed breadth first |
| `std/grep` | `Grep` | Searches the files under a path with a Rust regular expression |
| `std/write` | `Write` | Creates a file, or replaces the whole contents of one |
| `std/replace` | `StrReplace` | Replaces a text that occurs once in a file, or every occurrence of it |
| `std/rm` | `Delete` | Deletes a file or a link, and refuses a directory |
| `std/glob` | `Glob` | A stub: answers that the tool is not implemented |
| `std/glob` | `Glob` | Finds files matching a glob pattern under a directory |

- Each tool's wire name, description, and parameter schema is a JSON file under `crates/plugin-std/schemas`, and `EDITS.md` beside them lists how each differs from Cursor's definition.
- Every file operation of a tool goes through the Host, so the run's policy applies to each one. An operation the policy denies fails the call with the policy's own reason. The one exception is an entry below the path a Grep call names, which Grep skips and counts.
- File text and file names come back as untrusted output. The Host marks them before a model reads them. The answer to a change holds fixed text and at most a count, so it is trusted output.
- Every tool in `std` returns trusted output (`ToolOutput::trusted`). The guard-nonce envelope is optional in the API and currently unused by `std`.
- Error handling splits by caller: on `Caller::Model`, domain and argument errors return plain-text `ToolOutput::trusted` so the model can recover without failing the call, while `Caller::Script` receives structured `ToolError` values.
- Write, StrReplace, and Delete say to the Host that they change files. The Host runs a call of such a tool by itself among the calls of one model reply, so two changes of one reply happen in the order the reply lists them.
- StrReplace edits a file with a read and then a write, so a change that another task makes to the file between the two is lost.
- Delete asks what a path names and then asks for its removal, so a directory with no entries that another task puts at the path between the two is removed.
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin-std/schemas/EDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Each file in this directory except `dir.json` is Cursor's definition of the tool
- `Assume this tool is able to read all files on the machine.` became `Assume this tool is able to read every file the run can see.`, for the same reason.
- `If the User provides a path to a file` became `If the prompt provides a path to a file`, because a Runvil run has a prompt rather than a chat user.
- Removed: `Image Support`, `PDF Support`, and the paragraph explaining how images and PDFs are returned. This build reads text only, so a model told otherwise would send a PDF and get an error.
- Removed `Results are returned using cat -n format, with line numbers starting at 1`. The description now states the format Read really uses: a six-wide right-aligned number, a pipe, and the line's text, where the number and the pipe are not part of the line.
- Removed `Results are returned using cat -n format, with line numbers starting at 1`. The description now states the format Read really uses: a six-wide right-aligned number and a pipe on every 10th line and on the starting line when `offset` is past line 1 (with no prefix on lines in between), where the number and the pipe are metadata and not part of the file.
- Removed `Any lines longer than 2000 characters will be truncated`. Read cuts a line only when that line alone passes the byte cap, and the description says so.
- Added lines that state what Read does: it opens any path the call names inside what the run has mounted, and reads through a link unless the link leaves the run's filesystem; a result holds at most `{byte_cap}` bytes of numbered lines, and a read that stops early names the offset to continue from; only text is read, so a file over `{file_ceiling}` bytes and a binary file are refused. The two placeholders are filled when the definition is built, from the constants the code enforces, so the text and the limits cannot drift apart.
- The `offset` and `limit` types are `integer`, where Cursor's were `number`, because a line count is whole.
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin-std/schemas/read.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"wire_name": "Read",
"description": "Reads a file from the run's filesystem. You can access any file directly by using this tool.\nAssume this tool is able to read every file the run can see. If the prompt provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.\n\nUsage:\n- Read opens any path the call names inside what the run has mounted. A link is read through unless it leaves the run's filesystem.\n- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters\n- Each line comes back as a six-wide right-aligned line number, a pipe, and the line's text, like ` 1|first line`. The number and the pipe are not part of the line.\n- A result holds at most {byte_cap} bytes of numbered lines. When a read stops before the last line, its last line names the offset to pass to continue. A single line longer than that is cut, and no parameter reads the rest of it.\n- Only text is read. A file over {file_ceiling} bytes is refused, and so is a binary file.\n- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.\n- If you read a file that exists but has empty contents you will receive 'File is empty.'",
"description": "Reads a file from the run's filesystem. You can access any file directly by using this tool.\nAssume this tool is able to read every file the run can see. If the prompt provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.\n\nUsage:\n- Read opens any path the call names inside what the run has mounted. A link is read through unless it leaves the run's filesystem.\n- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters\n- Line numbers appear as a six-wide right-aligned number and a pipe (like ` 10|tenth line`) on every 10th line and on the starting line when `offset` is past line 1; lines in between have no prefix. Treat the `LINE_NUMBER|` prefix as metadata, not part of the file.\n- A result holds at most {byte_cap} bytes of rendered lines. When a read stops before the last line, its last line names the offset to pass to continue. A single line longer than that is cut, and no parameter reads the rest of it.\n- Only text is read. A file over {file_ceiling} bytes is refused, and so is a binary file.\n- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.\n- If you read a file that exists but has empty contents you will receive 'File is empty.'",
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
"parameters": {
"type": "object",
"properties": {
Expand Down
4 changes: 2 additions & 2 deletions crates/plugin-std/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! directories share one byte-ordered list, entries named `.git` are
//! skipped, and a link is listed and never entered. An entry whose name
//! holds a control character is left out and counted in a note. The
//! names are untrusted output.
//! names are trusted output.

mod args;
mod render;
Expand Down Expand Up @@ -64,7 +64,7 @@ async fn list_directory(
let tree = walk::walk(source, target, &request.ignore, cap)
.await
.map_err(|failure| message(refusal(tool, &failure.path, &failure.error)))?;
Ok(ToolOutput::untrusted(render::render(&tree)))
Ok(ToolOutput::trusted(render::render(&tree)))
}

/// Decides from the kind of node at `path` whether ListDir goes on: a
Expand Down
4 changes: 2 additions & 2 deletions crates/plugin-std/src/dir/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ fn walk_listed(
block_on(walk(&Lister(list), target, ignore, cap))
}

/// The text of a successful listing, which must be untrusted output.
/// The text of a successful listing, which must be trusted output.
fn text_of(result: Result<ToolOutput, ToolError>) -> String {
let output = result.expect("the listing succeeds");
assert!(!output.is_trusted(), "{}", output.text());
assert!(output.is_trusted(), "{}", output.text());
output.text().to_owned()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/plugin-std/src/dir/tests/cap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ fn the_note_and_the_dots_do_not_count_against_the_cap() {
}

#[test]
fn a_truncated_listing_is_a_successful_untrusted_answer() {
fn a_truncated_listing_is_a_successful_trusted_answer() {
let files = small();
let output = list_capped(&files, "/p", &[], 21).expect("a truncated listing succeeds");
assert!(!output.is_trusted());
assert!(output.is_trusted());
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/plugin-std/src/dir/tests/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ fn a_target_that_is_a_pipe_is_not_a_directory() {
}

#[test]
fn every_successful_listing_is_untrusted_output() {
fn every_successful_listing_is_trusted_output() {
let files = seeded(&["/p/a.md", "/p/.git/HEAD"]).dir("/empty");
for args in [
json!({ "target_directory": "/p" }),
Expand All @@ -563,6 +563,6 @@ fn every_successful_listing_is_untrusted_output() {
json!({ "target_directory": "/p", "ignore_globs": ["*.md"] }),
] {
let output = list(&files, &args).expect("the listing succeeds");
assert!(!output.is_trusted(), "{args}");
assert!(output.is_trusted(), "{args}");
}
}
100 changes: 100 additions & 0 deletions crates/plugin-std/src/glob-tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Unit tests for the `Glob` tool.

use runvil_plugin_sdk::testing::Files;
use runvil_plugin_sdk::{ToolError, ToolErrorKind, ToolOutput};
use serde_json::{Value, json};

use crate::fake::block_on;
use crate::source::Source;
use crate::spec::{self, Definition};

/// The `glob` definition the Pack builds the tool from.
fn definition() -> &'static Definition {
spec::all()
.iter()
.find(|definition| definition.segment == "glob")
.expect("the glob definition exists")
}

/// Calls Glob over `files` with `args` as the Pack does.
fn glob(files: &impl Source, args: &Value) -> Result<ToolOutput, ToolError> {
let definition = definition();
crate::check::check(&definition.wire_name, &definition.parameters, args)?;
block_on(super::call(files, definition, args))
}

#[test]
fn matches_files_at_any_depth_and_returns_trusted_output() {
let files = Files::new()
.file("/a.rs", "")
.file("/src/main.rs", "")
.file("/src/lib.ts", "")
.file("/.git/config.rs", "");
let output = glob(&files, &json!({ "glob_pattern": "*.rs" })).expect("glob succeeds");
assert!(output.is_trusted());
assert_eq!(output.text(), "/a.rs\n/src/main.rs");
}

#[test]
fn respects_target_directory_and_anchored_patterns() {
let files = Files::new()
.file("/a.md", "")
.file("/docs/guide.md", "")
.file("/docs/nested/ref.md", "");
let scoped = glob(
&files,
&json!({ "glob_pattern": "*.md", "target_directory": "/docs" }),
)
.expect("scoped glob succeeds");
assert_eq!(scoped.text(), "/docs/guide.md\n/docs/nested/ref.md");

let anchored = glob(&files, &json!({ "glob_pattern": "/*.md" })).expect("anchored succeeds");
assert_eq!(anchored.text(), "/a.md");
}

#[test]
fn reports_no_files_matched_when_empty() {
let files = Files::new().file("/a.txt", "");
let output = glob(&files, &json!({ "glob_pattern": "*.rs" })).expect("glob succeeds");
assert!(output.is_trusted());
assert_eq!(output.text(), "No files matched.");
}

#[test]
fn refuses_invalid_pattern_and_missing_target_directory() {
let files = Files::new();
let bad_pattern =
glob(&files, &json!({ "glob_pattern": "a***" })).expect_err("invalid pattern fails");
assert_eq!(bad_pattern.kind(), ToolErrorKind::InvalidArguments);

let missing = glob(
&files,
&json!({ "glob_pattern": "*.rs", "target_directory": "/nope" }),
)
.expect_err("missing target fails");
assert_eq!(missing.kind(), ToolErrorKind::Other);
assert_eq!(missing.message(), "Path does not exist: /nope");
}

#[test]
fn visits_subdirectories_in_sorted_path_order() {
let files = Files::new().file("/a/z.rs", "").file("/b.rs", "");
let output = glob(&files, &json!({ "glob_pattern": "*.rs" })).expect("glob succeeds");
assert_eq!(output.text(), "/a/z.rs\n/b.rs");
}

#[test]
fn truncates_when_output_exceeds_cap() {
let files = Files::new()
.file("/a.rs", "")
.file("/b.rs", "")
.file("/c.rs", "");
let args = json!({ "glob_pattern": "*.rs" });
let req = super::parse(definition(), &args).expect("parses");
let output = block_on(super::glob_files(&files, "Glob", &req, 12)).expect("glob succeeds");
assert!(
output
.text()
.starts_with("/a.rs\n/b.rs\n\n[Results truncated at 12 bytes.")
);
}
Loading
Loading