Skip to main content

Workspace

Struct Workspace 

Source
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

Source

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());
Source

pub fn read_only(root: impl Into<PathBuf>) -> Self

Create a read-only workspace (no writes, no bash) — useful for exploration / plan modes.

Source

pub fn writable(self, yes: bool) -> Self

Set whether mutating file operations are permitted.

Source

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.

Source

pub fn bash_timeout(self, timeout: Duration) -> Self

Set the timeout applied to bash commands, and the most a call may request.

Source

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);
Source

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.

Source

pub fn env_allowlist<I, S>(self, keys: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

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.

Source

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());
Source

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.

Source

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());
Source

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")));
Source

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")));
Source

pub fn inherits_env(&self) -> bool

Whether bash inherits the parent environment.

Source

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.

Source

pub fn root(&self) -> &Path

The workspace root.

Source

pub fn is_writable(&self) -> bool

Whether mutating file operations are permitted.

Source

pub fn bash_allowed(&self) -> bool

Whether the bash tool is permitted.

Source

pub fn bash_timeout_value(&self) -> Duration

The default bash timeout.

Source

pub fn max_output(&self) -> usize

The most bytes of a command’s output its output file keeps.

Source

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).

Source

pub fn display(&self, path: &Path) -> String

Render a path relative to the root for display (falls back to the full path).

Source

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.

Source

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());
Source

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?;
Source

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();

Trait Implementations§

Source§

impl Clone for Workspace

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more