mod workspace_fs

module workspace_fs

Sandboxed reads and edits for recipe workspaces.

eb_recipe_read, eb_recipe_list, eb_recipe_grep, eb_recipe_edit, and eb_recipe_write resolve every path against one workspace root. A .. component, an absolute path outside that root, or a symlink whose target leaves the root is rejected before the file is opened.

Variables

const MAX_GREP_MATCHES: usize

Default and upper bound for the number of lines grep_workspace returns.

const MAX_WORKSPACE_FILE_BYTES: u64

Largest recipe-workspace file the file tools will read or write.

Functions

fn apply_suggestion_workspace(root: &Path, requested: &Path, start: usize, end: usize, replacement: &str, expect: Option<&str>, first_seen: Option<&str>) -> Result<WorkspaceLineEdit, String>

replace_lines_workspace for a review suggestion, with two guards (proof v7: an empty suggestion applied by line number after an earlier edit had moved the lines deleted a closing ]):

  • without expect, when the file changed since first_seen (the file as the review showed it, whose numbering the suggestion uses), the anchored lines of first_seen are the expect, so moved lines are found where they are now and a stale range writes nothing;

  • a write that turns a valid file into invalid Python is undone and reported as an error.

fn configured_workspace_root() -> Result<PathBuf, String>

Workspace root for the recipe file tools.

EB_STACK_WORKSPACE wins when it is set and non-empty. Otherwise the server’s current directory is the root. eb-stack mcp --workspace sets the environment variable before serving.

fn edit_workspace(root: &Path, requested: &Path, old: &str, new: &str, replace_all: bool) -> Result<WorkspaceEdit, String>

Replace an exact old string in a workspace file.

old is literal text. When it matches more than once and replace_all is false, the file is left unchanged and the error tells the caller to pass replace_all or a longer unique string.

fn edit_workspace_checked(root: &Path, requested: &Path, old: &str, new: &str, replace_all: bool, within: Option<&str>, occurrence: Option<usize>) -> Result<WorkspaceScopedEdit, String>

edit_workspace_scoped for a Python module (.py) that refuses to turn valid Python into invalid Python: the file is written back as it was and the error names the line the edit broke. Other files, and a module that was already invalid, are edited as before.

fn edit_workspace_scoped(root: &Path, requested: &Path, old: &str, new: &str, replace_all: bool, within: Option<&str>, occurrence: Option<usize>) -> Result<WorkspaceScopedEdit, String>

Like edit_workspace, but able to address one list entry.

within names a ('name', ...) entry (a component, extension or dependency); only occurrences of old inside it are considered. When two entries share the name, occurrence (1-based) picks one of them, else the call fails and lists them. Without within, occurrence picks the n-th match of old. An ambiguous match fails with the line and entry of every occurrence, so near-identical components cannot be edited by accident; a replace_all that changes several different entries succeeds and says which ones in note.

fn exclusive_temp_path(prefix: &str) -> std::io::Result<PathBuf>

A new empty file under the temporary directory.

The name is not a function of the process id alone, so two calls in one process do not share a file. create_new refuses a path that already exists, including a symlink planted in a shared temporary directory.

fn grep_workspace(root: &Path, requested: &Path, pattern: &str, regex: bool, max_matches: Option<usize>) -> Result<WorkspaceGrep, String>

Search UTF-8 files under requested (a file or directory inside root).

pattern is literal text unless regex is true. Symlinks are not followed, .git is skipped, and files that are not UTF-8 or are larger than MAX_WORKSPACE_FILE_BYTES are skipped. At most max_matches lines are returned (default and cap MAX_GREP_MATCHES).

fn list_workspace(root: &Path, requested: &Path) -> Result<WorkspaceList, String>

List one directory inside root without following symlinks.

fn read_workspace(root: &Path, requested: &Path) -> Result<WorkspaceText, String>

Read a UTF-8 file that resolves inside root.

fn replace_lines_workspace(root: &Path, requested: &Path, start: usize, end: usize, replacement: &str, expect: Option<&str>) -> Result<WorkspaceLineEdit, String>

Replace lines start..=end (1-based) with replacement, the way GitHub applies a review suggestion to the lines its comment is anchored on: an empty replacement deletes them. With expect (the anchored lines as the review showed them), the lines must match it ignoring surrounding whitespace; when they do not, a unique place where they do is used instead, otherwise the call fails and nothing is written.

fn resolve_inside(root: &Path, requested: &Path) -> Result<(PathBuf, PathBuf), String>

Resolve requested inside root: the canonical root and the path, refusing .., absolute paths outside the root and symlinks that leave it.

fn write_workspace(root: &Path, requested: &Path, content: &str) -> Result<WorkspaceWrite, String>

Create or overwrite a UTF-8 file inside root.

Parent directories must already exist. The call does not follow a symlink that leaves the workspace.

Structs and Unions

struct WorkspaceEdit

Result of an exact string replacement.

path: String

Edited path, relative to the workspace root.

replaced: usize

Number of times old was replaced.

bytes: usize

Byte length after the edit.

struct WorkspaceEntry

One directory entry. Symlinks are not followed.

name: String

File name inside the listed directory.

kind: String

file, dir, symlink, or other.

escapes: bool

True when this entry is a symlink whose target leaves the workspace.

struct WorkspaceGrep

Result of searching the workspace.

pattern: String

The pattern searched for.

regex: bool

True when pattern was a regular expression, false for literal text.

matches: Vec<WorkspaceMatch>

Matching lines, in path then line order.

files_searched: usize

Number of UTF-8 text files read.

truncated: bool

True when the search stopped at the match limit.

struct WorkspaceLineEdit

Result of replace_lines_workspace.

path: String

Edited path, relative to the workspace root.

replaced_lines: (usize, usize)

1-based first and last line that were replaced (before the edit).

removed: Vec<String>

The lines that were removed.

added: usize

Number of lines written in their place (0 deletes them).

relocated: bool

True when expect was found at another place than the given lines (earlier edits moved them).

expect_from_first_seen: bool

True when no expect was passed and the anchored lines of the file as first seen were used as expect (the file had changed since).

bytes: usize

Byte length after the edit.

struct WorkspaceList

Directory listing.

path: String

Listed path, relative to the workspace root.

entries: Vec<WorkspaceEntry>

Entries sorted by name.

struct WorkspaceMatch

One matching line.

path: String

File path relative to the workspace root.

line: usize

1-based line number.

text: String

The line, without its newline.

struct WorkspaceScopedEdit

Result of edit_workspace_scoped.

path: String

Edited path, relative to the workspace root.

replaced: usize

Number of times old was replaced.

bytes: usize

Byte length after the edit.

lines: Vec<usize>

1-based line of each replaced occurrence, in the file before the edit.

entries: Vec<String>

Name of the list entry (component, extension, dependency tuple) each replaced occurrence sits in; empty for top-level text.

note: Option<String>

Set when one call changed several different entries.

struct WorkspaceText

UTF-8 file contents from the workspace.

path: String

Path relative to the workspace root when the request was inside it.

content: String

File contents.

bytes: usize

Byte length of content.

lines: usize

Number of \n-separated lines, including a trailing empty line.

struct WorkspaceWrite

Result of creating or overwriting a file.

path: String

Written path, relative to the workspace root.

created: bool

True when the file did not exist before this call.

bytes: usize

Byte length written.