pub struct Workspace { /* private fields */ }Expand description
A workspace roots every file/search/shell operation at a directory and enforces a small capability policy.
All paths supplied to the tools are resolved relative to root
and rejected if they escape it. Mutating operations require
is_writable; bash requires bash_allowed.
The workspace also records the sha256 of every file read and of every file its tools
write, so that edit_file can require a prior read_file and refuse to edit a file
that changed since (spec 3.1 §7.26, §10 D7). The record is kept per session and agent,
taken from the tool call’s context, so one session or agent reading a file does not let
another edit it. Records live in a ReadLedger, in memory unless
with_read_ledger names a persistent one.
Workspace is cheap to clone. Clones share the read records, which stay
separated by session and agent; calls whose context names no session share one
record, kept in memory.
Implementations§
Source§impl Workspace
impl Workspace
Sourcepub fn new(root: impl Into<PathBuf>) -> Self
pub fn new(root: impl Into<PathBuf>) -> Self
Create a read-write workspace rooted at root, with bash disabled.
bash runs model-written commands on the host, outside the path containment the file
tools enforce, so it is off until allow_bash enables it. If root
exists it is canonicalized so containment checks are robust.
§Example
use adk_devtools::Workspace;
let files_only = Workspace::new("./my-repo");
assert!(!files_only.bash_allowed());
let with_shell = Workspace::new("./my-repo").allow_bash(true);
assert!(with_shell.bash_allowed());Sourcepub fn read_only(root: impl Into<PathBuf>) -> Self
pub fn read_only(root: impl Into<PathBuf>) -> Self
Create a read-only workspace (no writes, no bash) — useful for exploration / plan modes.
Sourcepub fn allow_bash(self, yes: bool) -> Self
pub fn allow_bash(self, yes: bool) -> Self
Set whether the bash tool is permitted. Off by default.
Commands run with the workspace root as their working directory, which is not a sandbox: they can reach absolute paths and the network.
Sourcepub fn bash_timeout(self, timeout: Duration) -> Self
pub fn bash_timeout(self, timeout: Duration) -> Self
Set the timeout applied to bash commands, and the most a call may request.
Sourcepub fn max_output_bytes(self, bytes: usize) -> Self
pub fn max_output_bytes(self, bytes: usize) -> Self
Set the most bytes of a bash command’s output an
OutputStore file keeps: the disk bound (default
DEFAULT_MAX_OUTPUT_BYTES, 64 MiB; spec 3.1 §10 D10). The result itself carries at
most INLINE_OUTPUT_CHARS characters whatever this
is, and memory holds only the start and end of each stream.
§Example
use adk_devtools::Workspace;
let ws = Workspace::new(".").allow_bash(true).max_output_bytes(8 * 1024 * 1024);
assert_eq!(ws.max_output(), 8 * 1024 * 1024);Sourcepub fn inherit_env(self, yes: bool) -> Self
pub fn inherit_env(self, yes: bool) -> Self
Pass the whole parent environment to bash.
Off by default, because the parent environment of an agent process routinely
holds provider API keys and a model-directed command can read them with env.
Enable it only when the commands you run genuinely need the caller’s environment
and you accept that exposure.
Sourcepub fn env_allowlist<I, S>(self, keys: I) -> Self
pub fn env_allowlist<I, S>(self, keys: I) -> Self
Replace the variables bash receives when the environment is not inherited.
Defaults to DEFAULT_ENV_ALLOWLIST. Adding a variable that holds a credential
re-exposes it.
Sourcepub fn bash_kill_group_on_exit(self, yes: bool) -> Self
pub fn bash_kill_group_on_exit(self, yes: bool) -> Self
Set whether bash kills the rest of the command’s process group once the
shell exits. On by default.
With it on, a process the command leaves running in the background — sleep 300 &,
a dev server — is killed when the call returns, and the call does not wait for
it to close its output. Turn it off only when commands must leave processes
running; a timeout or a cancelled call still kills the group. Implements spec 3.1
§10 D3.
§Example
use adk_devtools::Workspace;
let ws = Workspace::new("./my-repo").allow_bash(true);
assert!(ws.kills_bash_group_on_exit());
let keeps_servers = ws.bash_kill_group_on_exit(false);
assert!(!keeps_servers.kills_bash_group_on_exit());Sourcepub fn kills_bash_group_on_exit(&self) -> bool
pub fn kills_bash_group_on_exit(&self) -> bool
Whether bash kills the command’s process group once the shell exits.
Set with bash_kill_group_on_exit; spec 3.1 §10 D3.
Sourcepub fn with_read_ledger(self, ledger: Arc<dyn ReadLedger>) -> Self
pub fn with_read_ledger(self, ledger: Arc<dyn ReadLedger>) -> Self
Keep read records in ledger instead of process memory (spec 3.1 §10 D7).
A host that resumes a session in another process — a held edit approved from a new
process, a crash-resumed run, a kernel process moved between workers — persists the
records with the session, so the read that preceded an edit is still known. Clones
made after this call share ledger.
§Example
use std::sync::Arc;
use adk_devtools::{InMemoryReadLedger, Workspace};
let ws = Workspace::new("./my-repo").with_read_ledger(Arc::new(InMemoryReadLedger::new()));
assert!(ws.is_writable());Sourcepub fn exclude_globs(self, globs: impl IntoIterator<Item = String>) -> Self
pub fn exclude_globs(self, globs: impl IntoIterator<Item = String>) -> Self
Replace the file patterns grep and glob skip and read_file refuses.
Defaults to DEFAULT_EXCLUDE_GLOBS; an empty list turns exclusion off. Each
pattern is a glob matched, ignoring case, against the path relative to the
root and against each of its components, so .env also excludes
config/.env and secrets/** everything under secrets/. A pattern that is
not a valid glob matches its literal text. Implements spec 3.1 §10 D5.
§Example
use std::path::Path;
use adk_devtools::Workspace;
let ws = Workspace::new("./my-repo").exclude_globs(["secrets/**".to_string()]);
assert!(ws.is_excluded(Path::new("secrets/prod/token.txt")));
assert!(!ws.is_excluded(Path::new(".env")));
let everything = Workspace::new("./my-repo").exclude_globs([]);
assert!(!everything.is_excluded(Path::new(".env")));Sourcepub fn is_excluded(&self, path: &Path) -> bool
pub fn is_excluded(&self, path: &Path) -> bool
Whether path matches one of the exclusion patterns.
path is relative to the root, or an absolute path inside it. The check is
lexical; read_file also checks the path a symlink resolves to. A harness
uses it to require approval before a read. Implements spec 3.1 §10 D5.
§Example
use std::path::Path;
use adk_devtools::Workspace;
let ws = Workspace::new("./my-repo");
assert!(ws.is_excluded(Path::new("deploy/server.KEY")));
assert!(!ws.is_excluded(Path::new("src/keys.rs")));Sourcepub fn inherits_env(&self) -> bool
pub fn inherits_env(&self) -> bool
Whether bash inherits the parent environment.
Sourcepub fn bash_env(&self) -> Vec<(String, String)>
pub fn bash_env(&self) -> Vec<(String, String)>
The variables bash receives, resolved from the current process.
Empty when the environment is inherited, in which case the caller must not clear it.
Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Whether mutating file operations are permitted.
Sourcepub fn bash_allowed(&self) -> bool
pub fn bash_allowed(&self) -> bool
Whether the bash tool is permitted.
Sourcepub fn bash_timeout_value(&self) -> Duration
pub fn bash_timeout_value(&self) -> Duration
The default bash timeout.
Sourcepub fn max_output(&self) -> usize
pub fn max_output(&self) -> usize
The most bytes of a command’s output its output file keeps.
Sourcepub fn resolve(&self, path: &str) -> Result<PathBuf, DevToolError>
pub fn resolve(&self, path: &str) -> Result<PathBuf, DevToolError>
Resolve a user-supplied path against the root, rejecting any path that escapes it, lexically or through a symlink. The target need not exist yet.
The returned path is the lexical one. The file tools open the canonical target the same check finds, following no symlink on it, so a link swapped onto the path after the check fails the call (spec 3.1 §10 D4).
Sourcepub fn display(&self, path: &Path) -> String
pub fn display(&self, path: &Path) -> String
Render a path relative to the root for display (falls back to the full path).
Sourcepub fn record_path(&self, resolved: &Path) -> String
pub fn record_path(&self, resolved: &Path) -> String
The key a read record names resolved by: its path relative to the root with /
separators, so a record written on one platform matches on another.
Sourcepub fn record_key(&self, path: &str) -> Result<String, DevToolError>
pub fn record_key(&self, path: &str) -> Result<String, DevToolError>
The key a read record and an expect_sha256 map name path by, after the
containment check: its path relative to the root with / separators.
§Errors
Returns DevToolError::PathEscape for a path outside the root.
§Example
let dir = tempfile::tempdir().unwrap();
let ws = adk_devtools::Workspace::new(dir.path());
assert_eq!(ws.record_key("./src/../src/lib.rs").unwrap(), "src/lib.rs");
assert!(ws.record_key("../outside").is_err());Sourcepub async fn read_for_review(
&self,
path: &str,
) -> Result<Option<Vec<u8>>, DevToolError>
pub async fn read_for_review( &self, path: &str, ) -> Result<Option<Vec<u8>>, DevToolError>
Reads path for a reviewer under the file tools’ rules — containment, no symlink
followed, excluded secret files refused — returning None when it does not exist
(spec 3.1 §7.26: a change set is composed over what the tools would read).
§Errors
Returns DevToolError::PathEscape, DevToolError::Excluded, or the read error.
§Example
let ws = adk_devtools::Workspace::new("./my-repo");
let current = ws.read_for_review("src/lib.rs").await?;Sourcepub async fn record_current(
&self,
ctx: &dyn ToolContext,
path: &str,
) -> Result<(), DevToolError>
pub async fn record_current( &self, ctx: &dyn ToolContext, path: &str, ) -> Result<(), DevToolError>
Records the current content of path as what the session and agent behind ctx
last saw, for a host step that changes a file right after the agent wrote it — a
formatter hook — so the agent’s next edit is not refused as stale (spec 3.1 §7.22,
§7.26).
§Errors
Returns the containment or read error for path.
§Example
ws.record_current(ctx, "src/lib.rs").await.unwrap();