use std::collections::HashMap;
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use adk_core::ToolContext;
#[cfg(not(any(unix, windows)))]
use tokio::io::AsyncWriteExt;
#[cfg(windows)]
use crate::beneath::{remove_beneath, replace_beneath, write_beneath};
use crate::error::DevToolError;
use crate::read_ledger::{InMemoryReadLedger, ReadLedger, ReadScope};
#[derive(Clone)]
pub struct Workspace {
root: PathBuf,
writable: bool,
allow_bash: bool,
bash_timeout: Duration,
max_output_bytes: usize,
read_ledger: Arc<dyn ReadLedger>,
unscoped_reads: Arc<Mutex<HashMap<String, String>>>,
inherit_env: bool,
env_allowlist: Vec<String>,
kill_group_on_exit: bool,
exclude: Vec<glob::Pattern>,
}
pub const DEFAULT_MAX_OUTPUT_BYTES: usize = 64 * 1024 * 1024;
pub const DEFAULT_ENV_ALLOWLIST: &[&str] =
&["PATH", "HOME", "LANG", "LC_ALL", "TMPDIR", "TERM", "USER", "SHELL"];
pub const DEFAULT_EXCLUDE_GLOBS: &[&str] =
&[".env", ".env.*", "*.pem", "*.key", "id_rsa*", "id_ed25519*", "*.p12", "*.pfx"];
const EXCLUDE_MATCH: glob::MatchOptions = glob::MatchOptions {
case_sensitive: false,
require_literal_separator: true,
require_literal_leading_dot: false,
};
impl Workspace {
pub fn new(root: impl Into<PathBuf>) -> Self {
let root = root.into();
let root = std::fs::canonicalize(&root).unwrap_or(root);
Self {
root,
writable: true,
allow_bash: false,
bash_timeout: Duration::from_secs(120),
max_output_bytes: DEFAULT_MAX_OUTPUT_BYTES,
read_ledger: Arc::new(InMemoryReadLedger::new()),
unscoped_reads: Arc::default(),
inherit_env: false,
env_allowlist: DEFAULT_ENV_ALLOWLIST.iter().map(|k| (*k).to_string()).collect(),
kill_group_on_exit: true,
exclude: compile_excludes(DEFAULT_EXCLUDE_GLOBS.iter().map(|glob| (*glob).to_string())),
}
}
pub fn read_only(root: impl Into<PathBuf>) -> Self {
let mut ws = Self::new(root);
ws.writable = false;
ws.allow_bash = false;
ws
}
pub fn writable(mut self, yes: bool) -> Self {
self.writable = yes;
self
}
pub fn allow_bash(mut self, yes: bool) -> Self {
self.allow_bash = yes;
self
}
pub fn bash_timeout(mut self, timeout: Duration) -> Self {
self.bash_timeout = timeout;
self
}
pub fn max_output_bytes(mut self, bytes: usize) -> Self {
self.max_output_bytes = bytes;
self
}
#[must_use]
pub fn inherit_env(mut self, yes: bool) -> Self {
self.inherit_env = yes;
self
}
#[must_use]
pub fn env_allowlist<I, S>(mut self, keys: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.env_allowlist = keys.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn bash_kill_group_on_exit(mut self, yes: bool) -> Self {
self.kill_group_on_exit = yes;
self
}
pub fn kills_bash_group_on_exit(&self) -> bool {
self.kill_group_on_exit
}
#[must_use]
pub fn with_read_ledger(mut self, ledger: Arc<dyn ReadLedger>) -> Self {
self.read_ledger = ledger;
self
}
#[must_use]
pub fn exclude_globs(mut self, globs: impl IntoIterator<Item = String>) -> Self {
self.exclude = compile_excludes(globs);
self
}
pub fn is_excluded(&self, path: &Path) -> bool {
if self.exclude.is_empty() {
return false;
}
let relative = path.strip_prefix(&self.root).unwrap_or(path);
self.exclude.iter().any(|pattern| {
pattern.matches_path_with(relative, EXCLUDE_MATCH)
|| relative.components().any(|component| match component {
Component::Normal(name) => {
pattern.matches_with(&name.to_string_lossy(), EXCLUDE_MATCH)
}
Component::Prefix(_)
| Component::RootDir
| Component::CurDir
| Component::ParentDir => false,
})
})
}
pub fn inherits_env(&self) -> bool {
self.inherit_env
}
pub fn bash_env(&self) -> Vec<(String, String)> {
if self.inherit_env {
return Vec::new();
}
self.env_allowlist
.iter()
.filter_map(|key| std::env::var(key).ok().map(|value| (key.clone(), value)))
.collect()
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn is_writable(&self) -> bool {
self.writable
}
pub fn bash_allowed(&self) -> bool {
self.allow_bash
}
pub fn bash_timeout_value(&self) -> Duration {
self.bash_timeout
}
pub fn max_output(&self) -> usize {
self.max_output_bytes
}
pub fn resolve(&self, path: &str) -> Result<PathBuf, DevToolError> {
self.resolve_target(path).map(|(normalized, _)| normalized)
}
pub(crate) fn resolve_target(&self, path: &str) -> Result<(PathBuf, PathBuf), DevToolError> {
let requested = Path::new(path);
let joined = if requested.is_absolute() {
requested.to_path_buf()
} else {
self.root.join(requested)
};
let normalized = normalize(&joined);
if !normalized.starts_with(&self.root) {
return Err(DevToolError::PathEscape(path.to_string()));
}
let target = self.contained_target(&normalized, path)?;
Ok((normalized, target))
}
fn contained_target(
&self,
normalized: &Path,
requested: &str,
) -> Result<PathBuf, DevToolError> {
let mut existing = normalized;
loop {
match std::fs::canonicalize(existing) {
Ok(canonical) => {
if !canonical.starts_with(&self.root) {
return Err(DevToolError::PathEscape(requested.to_string()));
}
let missing = normalized.strip_prefix(existing).unwrap_or(Path::new(""));
return Ok(if missing.as_os_str().is_empty() {
canonical
} else {
canonical.join(missing)
});
}
Err(_) => {
if std::fs::symlink_metadata(existing).is_ok() {
return Err(DevToolError::PathEscape(requested.to_string()));
}
match existing.parent() {
Some(parent) if parent.starts_with(&self.root) => existing = parent,
_ => return Ok(normalized.to_path_buf()),
}
}
}
}
}
pub(crate) async fn read_contained(&self, target: &Path) -> Result<String, DevToolError> {
let bytes = self.read_contained_bytes(target).await?;
String::from_utf8(bytes).map_err(|_| {
DevToolError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
))
})
}
pub(crate) async fn read_contained_bytes(
&self,
target: &Path,
) -> Result<Vec<u8>, DevToolError> {
#[cfg(any(unix, windows))]
{
#[cfg(unix)]
use std::io::Read as _;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt as _;
let relative = self.beneath_root(target)?;
let root = self.root.clone();
#[cfg(unix)]
let read =
tokio::task::spawn_blocking(move || -> std::io::Result<Result<Vec<u8>, u64>> {
let dir = crate::beneath::open_root(&root)?;
let opened = crate::beneath::open_beneath(&dir, &relative, libc::O_RDONLY, 0)?;
let mut file = std::fs::File::from(opened);
let meta = file.metadata()?;
if meta.is_file() && meta.nlink() > 1 {
return Ok(Err(meta.nlink()));
}
let mut contents = Vec::new();
file.read_to_end(&mut contents)?;
Ok(Ok(contents))
})
.await
.map_err(|error| {
DevToolError::Other(format!("the read did not complete: {error}"))
})?;
#[cfg(windows)]
let read =
tokio::task::spawn_blocking(move || crate::beneath::read_beneath(&root, &relative))
.await
.map_err(|error| {
DevToolError::Other(format!("the read did not complete: {error}"))
})?;
match read {
Ok(Ok(contents)) => Ok(contents),
Ok(Err(links)) => Err(DevToolError::Other(format!(
"'{}' has {links} hard links, and another link to it can lie outside the \
workspace, so the file tools do not read it; copy it to a new file to read it",
self.display(target)
))),
Err(error) => Err(self.open_error(target, error)),
}
}
#[cfg(not(any(unix, windows)))]
{
Ok(tokio::fs::read(target).await?)
}
}
pub(crate) async fn write_contained(
&self,
target: &Path,
contents: &[u8],
precondition: Precondition,
) -> Result<(), DevToolError> {
#[cfg(any(unix, windows))]
{
let relative = self.beneath_root(target)?;
let Some(name) = relative.file_name().map(std::ffi::OsStr::to_os_string) else {
return Err(std::io::Error::from(std::io::ErrorKind::IsADirectory).into());
};
let root = self.root.clone();
let contents = contents.to_vec();
tokio::task::spawn_blocking(move || {
write_beneath(&root, &relative, &name, &contents, &precondition)
})
.await
.map_err(|error| DevToolError::Other(format!("the write did not complete: {error}")))?
.map_err(|error| self.open_error(target, error))
}
#[cfg(not(any(unix, windows)))]
{
let escape = || DevToolError::PathEscape(self.display(target));
let (Some(parent), Some(name)) = (target.parent(), target.file_name()) else {
return Err(escape());
};
tokio::fs::create_dir_all(parent).await?;
if !tokio::fs::canonicalize(parent).await?.join(name).starts_with(&self.root) {
return Err(escape());
}
let current = tokio::fs::read(target).await;
precondition
.check(current.as_deref().ok())
.map_err(|error| self.open_error(target, error))?;
let mut file = tokio::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(target)
.await?;
file.write_all(contents).await?;
file.flush().await?;
Ok(())
}
}
pub(crate) async fn replace_contained(
&self,
target: &Path,
contents: &[u8],
precondition: Precondition,
) -> Result<(), DevToolError> {
#[cfg(any(unix, windows))]
{
let relative = self.beneath_root(target)?;
let Some(name) = relative.file_name().map(std::ffi::OsStr::to_os_string) else {
return Err(std::io::Error::from(std::io::ErrorKind::IsADirectory).into());
};
let root = self.root.clone();
let contents = contents.to_vec();
tokio::task::spawn_blocking(move || {
replace_beneath(&root, &relative, &name, &contents, &precondition)
})
.await
.map_err(|error| DevToolError::Other(format!("the write did not complete: {error}")))?
.map_err(|error| self.open_error(target, error))
}
#[cfg(not(any(unix, windows)))]
{
self.write_contained(target, contents, precondition).await
}
}
pub(crate) async fn remove_contained(
&self,
target: &Path,
precondition: Precondition,
) -> Result<(), DevToolError> {
#[cfg(any(unix, windows))]
{
let relative = self.beneath_root(target)?;
let Some(name) = relative.file_name().map(std::ffi::OsStr::to_os_string) else {
return Err(std::io::Error::from(std::io::ErrorKind::IsADirectory).into());
};
let root = self.root.clone();
tokio::task::spawn_blocking(move || {
remove_beneath(&root, &relative, &name, &precondition)
})
.await
.map_err(|error| DevToolError::Other(format!("the removal did not complete: {error}")))?
.map_err(|error| self.open_error(target, error))
}
#[cfg(not(any(unix, windows)))]
{
let current = tokio::fs::read(target).await;
precondition
.check(current.as_deref().ok())
.map_err(|error| self.open_error(target, error))?;
Ok(tokio::fs::remove_file(target).await?)
}
}
#[cfg(any(unix, windows))]
fn beneath_root(&self, target: &Path) -> Result<PathBuf, DevToolError> {
target
.strip_prefix(&self.root)
.map(Path::to_path_buf)
.map_err(|_| DevToolError::PathEscape(self.display(target)))
}
fn open_error(&self, target: &Path, error: std::io::Error) -> DevToolError {
if let Some(stale) = error.get_ref().and_then(|inner| inner.downcast_ref::<StaleFile>()) {
return DevToolError::Stale { path: self.display(target), detail: stale.to_string() };
}
#[cfg(any(unix, windows))]
if crate::beneath::is_symlink_refusal(&error) {
return DevToolError::PathEscape(self.display(target));
}
DevToolError::Io(error)
}
pub fn display(&self, path: &Path) -> String {
path.strip_prefix(&self.root).unwrap_or(path).display().to_string()
}
pub fn record_path(&self, resolved: &Path) -> String {
let relative = resolved.strip_prefix(&self.root).unwrap_or(resolved);
relative
.components()
.map(|component| component.as_os_str().to_string_lossy())
.collect::<Vec<_>>()
.join("/")
}
pub(crate) async fn record_digest(&self, ctx: &dyn ToolContext, resolved: &Path, sha256: &str) {
let path = self.record_path(resolved);
let Some(scope) = ReadScope::of(ctx) else {
if let Ok(mut reads) = self.unscoped_reads.lock() {
reads.insert(path, sha256.to_string());
}
return;
};
if let Err(error) = self.read_ledger.record(&scope, &path, sha256).await {
tracing::warn!(file.path = %path, error = %error, "read record not stored");
}
}
pub(crate) async fn read_digest(
&self,
ctx: &dyn ToolContext,
resolved: &Path,
) -> Result<Option<String>, DevToolError> {
let path = self.record_path(resolved);
let Some(scope) = ReadScope::of(ctx) else {
return Ok(self.unscoped_reads.lock().ok().and_then(|reads| reads.get(&path).cloned()));
};
self.read_ledger.digest(&scope, &path).await.map_err(|error| {
DevToolError::Other(format!(
"the read records are unavailable ({}), so '{path}' cannot be checked; try again",
error.message
))
})
}
pub fn record_key(&self, path: &str) -> Result<String, DevToolError> {
let (resolved, _) = self.resolve_target(path)?;
Ok(self.record_path(&resolved))
}
pub async fn read_for_review(&self, path: &str) -> Result<Option<Vec<u8>>, DevToolError> {
let (resolved, target) = self.resolve_target(path)?;
if self.is_excluded(&resolved) || self.is_excluded(&target) {
return Err(DevToolError::Excluded(self.display(&resolved)));
}
match self.read_contained_bytes(&target).await {
Ok(bytes) => Ok(Some(bytes)),
Err(DevToolError::Io(error)) if error.kind() == std::io::ErrorKind::NotFound => {
Ok(None)
}
Err(error) => Err(error),
}
}
pub async fn record_current(
&self,
ctx: &dyn ToolContext,
path: &str,
) -> Result<(), DevToolError> {
let (resolved, target) = self.resolve_target(path)?;
let bytes = self.read_contained_bytes(&target).await?;
self.record_digest(ctx, &resolved, &crate::read_ledger::sha256_hex(&bytes)).await;
Ok(())
}
pub(crate) async fn forget_read(&self, ctx: &dyn ToolContext, resolved: &Path) {
let path = self.record_path(resolved);
let Some(scope) = ReadScope::of(ctx) else {
if let Ok(mut reads) = self.unscoped_reads.lock() {
reads.remove(&path);
}
return;
};
if let Err(error) = self.read_ledger.forget(&scope, &path).await {
tracing::warn!(file.path = %path, error = %error, "read record not removed");
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Precondition {
Any,
Absent,
Sha256(String),
}
impl Precondition {
pub(crate) fn from_expect(expect: &str) -> Result<Self, DevToolError> {
let expect = expect.trim();
if expect.eq_ignore_ascii_case("absent") {
return Ok(Self::Absent);
}
let digest = expect.strip_prefix("sha256:").unwrap_or(expect).to_ascii_lowercase();
if digest.len() != 64 || !digest.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return Err(DevToolError::Other(format!(
"expect_sha256 must be a 64-character hex sha256 or \"absent\"; got \"{expect}\""
)));
}
Ok(Self::Sha256(digest))
}
pub(crate) fn check(&self, current: Option<&[u8]>) -> std::io::Result<()> {
match (self, current) {
(Self::Any, _) | (Self::Absent, None) => Ok(()),
(Self::Absent, Some(_)) => Err(StaleFile::Exists.into()),
(Self::Sha256(_), None) => Err(StaleFile::Missing.into()),
(Self::Sha256(expected), Some(bytes)) => {
let found = crate::read_ledger::sha256_hex(bytes);
if found == *expected {
Ok(())
} else {
Err(StaleFile::Changed { expected: expected.clone(), found }.into())
}
}
}
}
}
#[derive(Debug)]
pub(crate) enum StaleFile {
Missing,
Exists,
Changed { expected: String, found: String },
}
impl std::fmt::Display for StaleFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Missing => f.write_str("it no longer exists"),
Self::Exists => f.write_str("a file exists there now, and the call expected none"),
Self::Changed { expected, found } => write!(
f,
"its sha256 is now {}…, and the call expected {}…",
&found[..12.min(found.len())],
&expected[..12.min(expected.len())]
),
}
}
}
impl std::error::Error for StaleFile {}
impl From<StaleFile> for std::io::Error {
fn from(stale: StaleFile) -> Self {
std::io::Error::other(stale)
}
}
fn compile_excludes(globs: impl IntoIterator<Item = String>) -> Vec<glob::Pattern> {
globs
.into_iter()
.map(|glob| {
glob::Pattern::new(&glob).unwrap_or_else(|error| {
tracing::warn!(
exclude.pattern = %glob,
error = %error,
"exclusion pattern is not a valid glob; matching it literally"
);
glob::Pattern::new(&glob::Pattern::escape(&glob))
.expect("an escaped pattern is a valid glob")
})
})
.collect()
}
#[cfg(unix)]
fn open_parent_beneath(
root: &Path,
relative: &Path,
create: bool,
) -> std::io::Result<std::os::fd::OwnedFd> {
let dir = match crate::beneath::open_root(root) {
Err(error) if create && error.kind() == std::io::ErrorKind::NotFound => {
std::fs::create_dir_all(root)?;
crate::beneath::open_root(root)?
}
result => result?,
};
let parent_path = relative.parent().unwrap_or(Path::new(""));
let flags = libc::O_RDONLY | libc::O_DIRECTORY;
match crate::beneath::open_beneath(&dir, parent_path, flags, 0) {
Err(error) if create && error.kind() == std::io::ErrorKind::NotFound => {
crate::beneath::create_dirs_beneath(&dir, parent_path)
}
result => result,
}
}
#[cfg(unix)]
fn check_entry(
parent: &std::os::fd::OwnedFd,
name: &std::ffi::OsStr,
precondition: &Precondition,
) -> std::io::Result<Option<std::fs::Metadata>> {
use std::io::Read as _;
match crate::beneath::open_entry(parent, name, libc::O_RDONLY, 0) {
Ok(mut file) => {
let meta = file.metadata()?;
if !matches!(precondition, Precondition::Any) {
let mut current = Vec::new();
file.read_to_end(&mut current)?;
precondition.check(Some(¤t))?;
}
Ok(Some(meta))
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
precondition.check(None)?;
Ok(None)
}
Err(error) => Err(error),
}
}
#[cfg(unix)]
fn write_beneath(
root: &Path,
relative: &Path,
name: &std::ffi::OsStr,
contents: &[u8],
precondition: &Precondition,
) -> std::io::Result<()> {
use std::io::{Read as _, Seek as _, Write as _};
use std::os::unix::fs::MetadataExt as _;
let parent = open_parent_beneath(root, relative, true)?;
let opened = match precondition {
Precondition::Any => {
crate::beneath::open_entry(&parent, name, libc::O_WRONLY | libc::O_CREAT, 0o666)
}
Precondition::Absent => crate::beneath::open_entry(
&parent,
name,
libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL,
0o666,
),
Precondition::Sha256(_) => crate::beneath::open_entry(&parent, name, libc::O_RDWR, 0),
};
let mut file = match opened {
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
return Err(StaleFile::Exists.into());
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Err(StaleFile::Missing.into());
}
result => result?,
};
let meta = file.metadata()?;
if let Precondition::Sha256(_) = precondition {
let mut current = Vec::new();
file.read_to_end(&mut current)?;
precondition.check(Some(¤t))?;
file.rewind()?;
}
if meta.is_file() && meta.nlink() > 1 {
drop(file);
return replace_entry(&parent, name, Some(meta.permissions()), contents);
}
if meta.is_file() {
file.set_len(0)?;
}
file.write_all(contents)?;
file.flush()
}
#[cfg(unix)]
fn replace_beneath(
root: &Path,
relative: &Path,
name: &std::ffi::OsStr,
contents: &[u8],
precondition: &Precondition,
) -> std::io::Result<()> {
let parent = open_parent_beneath(root, relative, true)?;
let existing = check_entry(&parent, name, precondition)?;
replace_entry(&parent, name, existing.map(|meta| meta.permissions()), contents)
}
#[cfg(unix)]
fn remove_beneath(
root: &Path,
relative: &Path,
name: &std::ffi::OsStr,
precondition: &Precondition,
) -> std::io::Result<()> {
let parent = open_parent_beneath(root, relative, false)?;
if check_entry(&parent, name, precondition)?.is_none() {
return Err(std::io::Error::from(std::io::ErrorKind::NotFound));
}
crate::beneath::remove_entry(&parent, name)
}
#[cfg(unix)]
fn replace_entry(
parent: &std::os::fd::OwnedFd,
name: &std::ffi::OsStr,
permissions: Option<std::fs::Permissions>,
contents: &[u8],
) -> std::io::Result<()> {
use std::io::Write as _;
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|elapsed| elapsed.as_nanos())
.unwrap_or_default();
let temp = std::ffi::OsString::from(format!(
".{}.{}.{nanos}.tmp",
name.to_string_lossy(),
std::process::id()
));
let result = (|| {
let mode = if permissions.is_some() { 0o600 } else { 0o666 };
let mut file = crate::beneath::open_entry(
parent,
&temp,
libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL,
mode,
)?;
file.write_all(contents)?;
if let Some(permissions) = permissions {
file.set_permissions(permissions)?;
}
crate::beneath::rename_entry(parent, &temp, name)
})();
if result.is_err() {
let _ = crate::beneath::remove_entry(parent, &temp);
}
result
}
fn normalize(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
Component::ParentDir => {
out.pop();
}
Component::CurDir => {}
other => out.push(other.as_os_str()),
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_escape() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
assert!(ws.resolve("../etc/passwd").is_err());
assert!(ws.resolve("ok/file.rs").is_ok());
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_symlink_planted_after_resolve_is_not_followed() {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("workspace");
let outside = temp.path().join("outside");
std::fs::create_dir_all(&root).unwrap();
std::fs::create_dir_all(&outside).unwrap();
std::fs::write(outside.join("existing.txt"), "host").unwrap();
let ws = Workspace::new(&root);
for (name, target) in
[("dangling", outside.join("planted.txt")), ("existing", outside.join("existing.txt"))]
{
let resolved = ws.resolve(name).expect("the path does not exist yet");
let Some(()) = link(&target, &root.join(name)) else { return };
let result = ws.write_contained(&resolved, b"payload", Precondition::Any).await;
assert!(
matches!(result, Err(DevToolError::PathEscape(_))),
"a {name} link planted after resolve was followed: {result:?}"
);
}
assert!(!outside.join("planted.txt").exists());
assert_eq!(std::fs::read_to_string(outside.join("existing.txt")).unwrap(), "host");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn writing_a_hard_linked_file_leaves_the_other_link_unchanged() {
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("workspace");
let outside = temp.path().join("outside.txt");
std::fs::create_dir_all(&root).unwrap();
std::fs::write(&outside, "host").unwrap();
std::fs::hard_link(&outside, root.join("linked.txt")).unwrap();
#[cfg(unix)]
std::fs::set_permissions(root.join("linked.txt"), std::fs::Permissions::from_mode(0o640))
.unwrap();
let ws = Workspace::new(&root);
let resolved = ws.resolve("linked.txt").unwrap();
ws.write_contained(&resolved, b"rewritten", Precondition::Any).await.unwrap();
assert_eq!(std::fs::read_to_string(root.join("linked.txt")).unwrap(), "rewritten");
assert_eq!(std::fs::read_to_string(&outside).unwrap(), "host");
#[cfg(unix)]
{
let mode = std::fs::metadata(root.join("linked.txt")).unwrap().permissions().mode();
assert_eq!(mode & 0o7777, 0o640);
}
assert_eq!(std::fs::read_dir(&root).unwrap().count(), 1, "no temporary file is left");
}
#[cfg(any(unix, windows))]
struct SwapFixture {
_temp: tempfile::TempDir,
root: PathBuf,
outside: PathBuf,
}
#[cfg(any(unix, windows))]
fn swap_fixture() -> SwapFixture {
let temp = tempfile::tempdir().unwrap();
let root = temp.path().join("workspace");
let outside = temp.path().join("outside");
std::fs::create_dir_all(root.join("docs")).unwrap();
std::fs::create_dir_all(&outside).unwrap();
std::fs::write(root.join("notes.md"), "notes").unwrap();
std::fs::write(root.join("docs/guide.md"), "guide").unwrap();
std::fs::write(root.join("AGENTS.md"), "instructions").unwrap();
std::fs::write(outside.join("secret.txt"), "host secret").unwrap();
std::fs::write(outside.join("guide.md"), "host guide").unwrap();
SwapFixture { _temp: temp, root, outside }
}
#[cfg(any(unix, windows))]
fn swap_for_link(path: &Path, target: &Path) -> Option<()> {
let staged = path.with_extension("swap");
link(target, &staged)?;
if path.is_dir() {
std::fs::rename(path, path.with_extension("moved")).unwrap();
}
std::fs::rename(&staged, path).unwrap();
Some(())
}
#[cfg(any(unix, windows))]
fn link(target: &Path, at: &Path) -> Option<()> {
#[cfg(unix)]
std::os::unix::fs::symlink(target, at).unwrap();
#[cfg(windows)]
if target.is_dir() {
junction(target, at);
} else {
return privileged(std::os::windows::fs::symlink_file(target, at));
}
Some(())
}
#[cfg(windows)]
fn junction(target: &Path, at: &Path) {
let made = std::process::Command::new("cmd")
.args(["/C", "mklink", "/J"])
.arg(at)
.arg(target)
.output()
.unwrap();
assert!(
made.status.success(),
"mklink /J failed: {}",
String::from_utf8_lossy(&made.stdout)
);
}
#[cfg(windows)]
fn privileged(made: std::io::Result<()>) -> Option<()> {
let not_held = windows_sys::Win32::Foundation::ERROR_PRIVILEGE_NOT_HELD as i32;
match made {
Ok(()) => Some(()),
Err(error)
if error.raw_os_error() == Some(not_held) && std::env::var_os("CI").is_none() =>
{
eprintln!("skipped: creating a symlink needs Developer Mode or an elevated shell");
None
}
Err(error) => panic!("could not create a symlink: {error}"),
}
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_file_swapped_for_a_link_inside_the_root_after_the_check_is_not_written() {
let f = swap_fixture();
let ws = Workspace::new(&f.root);
let (_, target) = ws.resolve_target("notes.md").unwrap();
let Some(()) = swap_for_link(&f.root.join("notes.md"), &f.root.join("AGENTS.md")) else {
return;
};
let result = ws.write_contained(&target, b"rewritten", Precondition::Any).await;
assert!(matches!(result, Err(DevToolError::PathEscape(_))), "{result:?}");
assert_eq!(std::fs::read_to_string(f.root.join("AGENTS.md")).unwrap(), "instructions");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_file_swapped_for_a_link_outside_the_root_after_the_check_is_not_read() {
let f = swap_fixture();
let ws = Workspace::new(&f.root);
let (_, target) = ws.resolve_target("notes.md").unwrap();
let Some(()) = swap_for_link(&f.root.join("notes.md"), &f.outside.join("secret.txt"))
else {
return;
};
let result = ws.read_contained(&target).await;
assert!(matches!(result, Err(DevToolError::PathEscape(_))), "{result:?}");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_directory_swapped_for_a_link_after_the_check_is_not_followed() {
let f = swap_fixture();
let ws = Workspace::new(&f.root);
let (_, read_target) = ws.resolve_target("docs/guide.md").unwrap();
let (_, write_target) = ws.resolve_target("docs/new.md").unwrap();
swap_for_link(&f.root.join("docs"), &f.outside).unwrap();
let read = ws.read_contained(&read_target).await;
let write = ws.write_contained(&write_target, b"planted", Precondition::Any).await;
assert!(matches!(read, Err(DevToolError::PathEscape(_))), "{read:?}");
assert!(matches!(write, Err(DevToolError::PathEscape(_))), "{write:?}");
assert!(!f.outside.join("new.md").exists(), "the write landed outside the root");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_missing_directory_planted_as_a_link_after_the_check_is_not_followed() {
let f = swap_fixture();
let ws = Workspace::new(&f.root);
let (_, target) = ws.resolve_target("build/out/report.md").unwrap();
link(&f.outside, &f.root.join("build")).unwrap();
let result = ws.write_contained(&target, b"planted", Precondition::Any).await;
assert!(matches!(result, Err(DevToolError::PathEscape(_))), "{result:?}");
assert!(!f.outside.join("out").exists(), "a directory was created outside the root");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn the_checked_target_is_read_and_written_through_an_inside_link() {
let f = swap_fixture();
let Some(()) = link(&f.root.join("notes.md"), &f.root.join("alias.md")) else { return };
let ws = Workspace::new(&f.root);
let (lexical, target) = ws.resolve_target("alias.md").unwrap();
assert_eq!(lexical, ws.root().join("alias.md"));
assert_eq!(target, ws.root().join("notes.md"));
assert_eq!(ws.read_contained(&target).await.unwrap(), "notes");
ws.write_contained(&target, b"updated", Precondition::Any).await.unwrap();
assert_eq!(std::fs::read_to_string(f.root.join("notes.md")).unwrap(), "updated");
let (_, created) = ws.resolve_target("new/dir/file.md").unwrap();
ws.write_contained(&created, b"made", Precondition::Any).await.unwrap();
assert_eq!(std::fs::read_to_string(f.root.join("new/dir/file.md")).unwrap(), "made");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_file_with_more_than_one_hard_link_is_not_read() {
let f = swap_fixture();
std::fs::hard_link(f.outside.join("secret.txt"), f.root.join("linked.txt")).unwrap();
let ws = Workspace::new(&f.root);
let (_, target) = ws.resolve_target("linked.txt").unwrap();
let result = ws.read_contained(&target).await;
let Err(DevToolError::Other(message)) = result else {
panic!("a hard-linked file was read: {result:?}");
};
assert!(message.contains("2 hard links"), "{message}");
}
#[cfg(windows)]
#[tokio::test]
async fn a_directory_swapped_for_a_directory_symlink_after_the_check_is_not_followed() {
let f = swap_fixture();
let ws = Workspace::new(&f.root);
let (_, read_target) = ws.resolve_target("docs/guide.md").unwrap();
let (_, write_target) = ws.resolve_target("docs/new.md").unwrap();
let docs = f.root.join("docs");
let staged = docs.with_extension("swap");
let Some(()) = privileged(std::os::windows::fs::symlink_dir(&f.outside, &staged)) else {
return;
};
std::fs::rename(&docs, docs.with_extension("moved")).unwrap();
std::fs::rename(&staged, &docs).unwrap();
let read = ws.read_contained(&read_target).await;
let write = ws.write_contained(&write_target, b"planted", Precondition::Any).await;
assert!(matches!(read, Err(DevToolError::PathEscape(_))), "{read:?}");
assert!(matches!(write, Err(DevToolError::PathEscape(_))), "{write:?}");
assert!(!f.outside.join("new.md").exists(), "the write landed outside the root");
}
#[cfg(windows)]
#[tokio::test]
async fn an_in_place_reparse_point_is_read_and_written() {
use std::os::windows::ffi::OsStrExt as _;
use windows_sys::Win32::Storage::FileSystem::{WOF_PROVIDER_FILE, WofIsExternalFile};
let f = swap_fixture();
let packed = f.root.join("packed.txt");
let text = "an in-place reparse point keeps its data with its filter\n".repeat(32_768);
std::fs::write(&packed, &text).unwrap();
let compact = std::process::Command::new("compact.exe")
.args(["/C", "/EXE:XPRESS4K"])
.arg(&packed)
.output()
.unwrap();
let wide: Vec<u16> = packed.as_os_str().encode_wide().chain(Some(0)).collect();
let (mut external, mut provider) = (0, 0);
let asked = unsafe {
WofIsExternalFile(
wide.as_ptr(),
&raw mut external,
&raw mut provider,
std::ptr::null_mut(),
std::ptr::null_mut(),
)
};
assert!(
asked >= 0 && external != 0 && provider == WOF_PROVIDER_FILE,
"compact did not back the file with WOF: {}",
String::from_utf8_lossy(&compact.stdout)
);
let ws = Workspace::new(&f.root);
let (_, target) = ws.resolve_target("packed.txt").unwrap();
assert_eq!(ws.read_contained(&target).await.unwrap(), text);
ws.write_contained(&target, b"rewritten", Precondition::Any).await.unwrap();
assert_eq!(std::fs::read_to_string(&packed).unwrap(), "rewritten");
}
#[cfg(windows)]
fn set_unserved_reparse_point(path: &Path) -> u32 {
use std::os::windows::fs::OpenOptionsExt as _;
use std::os::windows::io::AsRawHandle as _;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
use windows_sys::Win32::System::IO::DeviceIoControl;
use windows_sys::Win32::System::Ioctl::FSCTL_SET_REPARSE_POINT;
const TAG: u32 = 0x0000_7ADC;
let file = std::fs::OpenOptions::new()
.write(true)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)
.unwrap();
let data = b"adk-devtools test placeholder";
let mut buffer = Vec::new();
buffer.extend_from_slice(&TAG.to_le_bytes());
buffer.extend_from_slice(&(data.len() as u16).to_le_bytes());
buffer.extend_from_slice(&[0, 0]);
buffer.extend_from_slice(&[0x41; 16]);
buffer.extend_from_slice(data);
let mut returned = 0u32;
let set = unsafe {
DeviceIoControl(
file.as_raw_handle(),
FSCTL_SET_REPARSE_POINT,
buffer.as_ptr().cast(),
buffer.len() as u32,
std::ptr::null_mut(),
0,
&raw mut returned,
std::ptr::null_mut(),
)
};
assert_ne!(set, 0, "FSCTL_SET_REPARSE_POINT failed: {}", std::io::Error::last_os_error());
TAG
}
#[cfg(windows)]
fn visible_reparse_tag(path: &Path) -> Option<u32> {
use std::os::windows::fs::OpenOptionsExt as _;
use std::os::windows::io::AsRawHandle as _;
use windows_sys::Win32::Storage::FileSystem::{
FILE_ATTRIBUTE_REPARSE_POINT, FILE_ATTRIBUTE_TAG_INFO, FILE_FLAG_OPEN_REPARSE_POINT,
FileAttributeTagInfo, GetFileInformationByHandleEx,
};
let file = std::fs::OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
.open(path)
.unwrap();
let mut tag = FILE_ATTRIBUTE_TAG_INFO::default();
let queried = unsafe {
GetFileInformationByHandleEx(
file.as_raw_handle(),
FileAttributeTagInfo,
(&raw mut tag).cast(),
size_of::<FILE_ATTRIBUTE_TAG_INFO>() as u32,
)
};
assert_ne!(queried, 0, "{}", std::io::Error::last_os_error());
(tag.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT != 0).then_some(tag.ReparseTag)
}
#[cfg(windows)]
#[tokio::test]
async fn an_unserved_in_place_reparse_point_fails_closed_at_the_reopen() {
use std::io::Read as _;
use std::os::windows::fs::OpenOptionsExt as _;
use windows_sys::Win32::Foundation::ERROR_STOPPED_ON_SYMLINK;
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OPEN_REPARSE_POINT;
let f = swap_fixture();
let path = f.root.join("placeholder.txt");
std::fs::write(&path, "placeholder content").unwrap();
let tag = set_unserved_reparse_point(&path);
assert_eq!(visible_reparse_tag(&path), Some(tag), "the handle shows the tag");
assert!(!crate::beneath::is_name_surrogate(tag));
let ws = Workspace::new(&f.root);
let target = ws.root().join("placeholder.txt");
let read = ws.read_contained(&target).await;
let write = ws.write_contained(&target, b"rewritten", Precondition::Any).await;
let stopped = Some(ERROR_STOPPED_ON_SYMLINK as i32);
assert!(
matches!(&read, Err(DevToolError::Io(error)) if error.raw_os_error() != stopped),
"{read:?}"
);
assert!(
matches!(&write, Err(DevToolError::Io(error)) if error.raw_os_error() != stopped),
"{write:?}"
);
let mut content = String::new();
std::fs::OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT)
.open(&path)
.unwrap()
.read_to_string(&mut content)
.unwrap();
assert_eq!(content, "placeholder content", "the refused write truncated the file");
}
struct ScopeCtx {
session: String,
agent: String,
}
impl ScopeCtx {
fn new(session: &str, agent: &str) -> Self {
Self { session: session.to_string(), agent: agent.to_string() }
}
}
#[async_trait::async_trait]
impl adk_core::ReadonlyContext for ScopeCtx {
fn invocation_id(&self) -> &str {
"invocation"
}
fn agent_name(&self) -> &str {
&self.agent
}
fn user_id(&self) -> &str {
"user"
}
fn app_name(&self) -> &str {
"app"
}
fn session_id(&self) -> &str {
&self.session
}
fn branch(&self) -> &str {
""
}
fn user_content(&self) -> &adk_core::Content {
static CONTENT: std::sync::OnceLock<adk_core::Content> = std::sync::OnceLock::new();
CONTENT.get_or_init(|| adk_core::Content::new("user").with_text("hi"))
}
}
#[async_trait::async_trait]
impl adk_core::CallbackContext for ScopeCtx {
fn artifacts(&self) -> Option<Arc<dyn adk_core::Artifacts>> {
None
}
}
#[async_trait::async_trait]
impl ToolContext for ScopeCtx {
fn function_call_id(&self) -> &str {
"call"
}
fn actions(&self) -> adk_core::EventActions {
adk_core::EventActions::default()
}
fn set_actions(&self, _actions: adk_core::EventActions) {}
async fn search_memory(
&self,
_query: &str,
) -> adk_core::Result<Vec<adk_core::MemoryEntry>> {
Ok(vec![])
}
}
#[tokio::test]
async fn read_tracking() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
let ctx = ScopeCtx::new("session", "agent");
let p = ws.resolve("a.txt").unwrap();
assert_eq!(ws.read_digest(&ctx, &p).await.unwrap(), None);
ws.record_digest(&ctx, &p, "d1").await;
assert_eq!(ws.read_digest(&ctx, &p).await.unwrap().as_deref(), Some("d1"));
ws.forget_read(&ctx, &p).await;
assert_eq!(ws.read_digest(&ctx, &p).await.unwrap(), None);
}
#[tokio::test]
async fn clones_keep_reads_apart_by_session_and_agent() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
let other = ws.clone();
let p = ws.resolve("a.txt").unwrap();
ws.record_digest(&ScopeCtx::new("one", "coder"), &p, "d1").await;
let seen = |session: &str, agent: &str| {
let (other, p, ctx) = (other.clone(), p.clone(), ScopeCtx::new(session, agent));
async move { other.read_digest(&ctx, &p).await.unwrap() }
};
assert_eq!(seen("one", "coder").await.as_deref(), Some("d1"), "clones share a scope");
assert_eq!(seen("two", "coder").await, None, "another session read it");
assert_eq!(seen("one", "reviewer").await, None, "another agent read it");
}
#[tokio::test]
async fn calls_without_a_session_share_one_record() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
let p = ws.resolve("a.txt").unwrap();
ws.record_digest(&ScopeCtx::new("", "first"), &p, "d1").await;
let unscoped = ws.clone().read_digest(&ScopeCtx::new("", "second"), &p).await.unwrap();
assert_eq!(unscoped.as_deref(), Some("d1"));
assert_eq!(ws.read_digest(&ScopeCtx::new("session", "first"), &p).await.unwrap(), None);
}
#[tokio::test]
async fn the_least_recently_used_scope_is_evicted_at_capacity() {
use crate::read_ledger::READ_SCOPE_CAPACITY;
let dir = tempfile::tempdir().unwrap();
let ledger = Arc::new(InMemoryReadLedger::new());
let ws = Workspace::new(dir.path()).with_read_ledger(ledger.clone());
let p = ws.resolve("a.txt").unwrap();
for session in 0..READ_SCOPE_CAPACITY {
ws.record_digest(&ScopeCtx::new(&format!("s{session}"), "agent"), &p, "d").await;
}
assert!(ws.read_digest(&ScopeCtx::new("s0", "agent"), &p).await.unwrap().is_some());
ws.record_digest(&ScopeCtx::new("newest", "agent"), &p, "d").await;
assert!(
ws.read_digest(&ScopeCtx::new("s0", "agent"), &p).await.unwrap().is_some(),
"a recently used scope was evicted"
);
assert!(
ws.read_digest(&ScopeCtx::new("s1", "agent"), &p).await.unwrap().is_none(),
"the oldest scope was kept"
);
assert_eq!(ledger.scopes(), READ_SCOPE_CAPACITY);
}
#[test]
fn record_paths_use_forward_slashes_relative_to_the_root() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
let p = ws.resolve("src/nested/a.rs").unwrap();
assert_eq!(ws.record_path(&p), "src/nested/a.rs");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn a_write_checks_its_precondition_on_the_file_it_writes() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
std::fs::write(dir.path().join("a.txt"), "one").unwrap();
let (_, target) = ws.resolve_target("a.txt").unwrap();
let one = crate::read_ledger::sha256_hex(b"one");
let stale = ws
.write_contained(
&target,
b"two",
Precondition::Sha256(crate::read_ledger::sha256_hex(b"x")),
)
.await;
assert!(matches!(&stale, Err(DevToolError::Stale { .. })), "{stale:?}");
assert_eq!(std::fs::read_to_string(dir.path().join("a.txt")).unwrap(), "one");
ws.write_contained(&target, b"two", Precondition::Sha256(one.clone())).await.unwrap();
assert_eq!(std::fs::read_to_string(dir.path().join("a.txt")).unwrap(), "two");
let exists = ws.write_contained(&target, b"three", Precondition::Absent).await;
assert!(matches!(&exists, Err(DevToolError::Stale { .. })), "{exists:?}");
let (_, fresh) = ws.resolve_target("fresh.txt").unwrap();
let missing = ws.write_contained(&fresh, b"x", Precondition::Sha256(one)).await;
assert!(matches!(&missing, Err(DevToolError::Stale { .. })), "{missing:?}");
assert!(!dir.path().join("fresh.txt").exists(), "a stale write creates nothing");
ws.write_contained(&fresh, b"x", Precondition::Absent).await.unwrap();
assert_eq!(std::fs::read_to_string(dir.path().join("fresh.txt")).unwrap(), "x");
}
#[cfg(any(unix, windows))]
#[tokio::test]
async fn replace_and_remove_check_their_precondition() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
std::fs::write(dir.path().join("a.txt"), "one").unwrap();
let (_, target) = ws.resolve_target("a.txt").unwrap();
let one = crate::read_ledger::sha256_hex(b"one");
let stale = ws.replace_contained(&target, b"two", Precondition::Absent).await;
assert!(matches!(&stale, Err(DevToolError::Stale { .. })), "{stale:?}");
ws.replace_contained(&target, b"two", Precondition::Sha256(one.clone())).await.unwrap();
assert_eq!(std::fs::read_to_string(dir.path().join("a.txt")).unwrap(), "two");
let stale = ws.remove_contained(&target, Precondition::Sha256(one)).await;
assert!(matches!(&stale, Err(DevToolError::Stale { .. })), "{stale:?}");
let two = crate::read_ledger::sha256_hex(b"two");
ws.remove_contained(&target, Precondition::Sha256(two)).await.unwrap();
assert!(!dir.path().join("a.txt").exists());
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0, "no temporary file is left");
}
#[test]
fn expect_sha256_takes_a_digest_or_absent() {
let digest = "A".repeat(64);
assert_eq!(
Precondition::from_expect(&digest).unwrap(),
Precondition::Sha256("a".repeat(64))
);
assert_eq!(
Precondition::from_expect(&format!("sha256:{digest}")).unwrap(),
Precondition::Sha256("a".repeat(64))
);
assert_eq!(Precondition::from_expect("absent").unwrap(), Precondition::Absent);
assert!(Precondition::from_expect("abc").is_err());
}
#[test]
fn secret_patterns_are_excluded_by_default() {
let dir = tempfile::tempdir().unwrap();
let ws = Workspace::new(dir.path());
for secret in [
".env",
"config/.env",
".env.production",
"certs/server.pem",
"tls.KEY",
".ssh/id_rsa",
"keys/id_rsa.pub",
"id_ed25519",
"bundle.p12",
"bundle.pfx",
".ENV",
] {
assert!(ws.is_excluded(Path::new(secret)), "{secret} was not excluded");
assert!(ws.is_excluded(&dir.path().join(secret)), "absolute {secret} was not excluded");
}
for ordinary in ["src/main.rs", "environment.md", "keys.rs", "README.md", ".envrc"] {
assert!(!ws.is_excluded(Path::new(ordinary)), "{ordinary} was excluded");
}
}
#[test]
fn exclusion_patterns_can_be_replaced_or_cleared() {
let dir = tempfile::tempdir().unwrap();
let custom = Workspace::new(dir.path()).exclude_globs(["secrets/**".to_string()]);
assert!(custom.is_excluded(Path::new("secrets/prod/token.txt")));
assert!(!custom.is_excluded(Path::new(".env")));
let cleared = Workspace::new(dir.path()).exclude_globs([]);
assert!(!cleared.is_excluded(Path::new(".env")));
let literal = Workspace::new(dir.path()).exclude_globs(["[unclosed".to_string()]);
assert!(literal.is_excluded(Path::new("dir/[unclosed")));
}
}