Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(tools): share workspace scoping between system and filesystem tools

Steven Enamakeldc2f3caf1eed
diff --git a/crates/openhuman-core/src/tools/impl/README.md b/crates/openhuman-core/src/tools/impl/README.md
index 1479716b7a..7df42c77cd 100644
--- a/crates/openhuman-core/src/tools/impl/README.md
+++ b/crates/openhuman-core/src/tools/impl/README.md
@@ -39,11 +39,11 @@ tested but no production assembly site constructs it today.
input, allocate artifact, generate bytes off the async runtime, finalize or
fail the artifact) so further artifact-producing tools follow the same shape.
tool surface, not a host capability.
-- [`system/mod.rs`](./system/mod.rs) (`security_for_tool_context`) and [`filesystem/gate.rs`](./filesystem/gate.rs)
- (`security_scoped_to_root`, behind `FsGate::scoped_to_workspace`) each grant the run's TinyAgents workspace
- descriptor as `action_dir` plus a `ReadWrite` trusted root on a per-call
- clone of `SecurityPolicy`. They must stay in step; `is_always_forbidden` and
- `is_workspace_internal_path` are evaluated before any trusted-root shortcut.
+- [`filesystem/gate.rs`](./filesystem/gate.rs) owns workspace descriptor
+ scoping for both filesystem and system tools. It grants the descriptor root
+ as `action_dir` plus a `ReadWrite` trusted root on a per-call clone of
+ `SecurityPolicy`; `is_always_forbidden` and `is_workspace_internal_path` are
+ evaluated before any trusted-root shortcut.
## Further reading
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/gate.rs b/crates/openhuman-core/src/tools/impl/filesystem/gate.rs
index 0bfaca8dab..91e5c168bc 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/gate.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/gate.rs
@@ -34,7 +34,10 @@ use tinybox_bus::CommandClass;
/// The root always originates from trusted in-process code (the session
/// builder, the sub-agent runner, or the `cwd` RPC parameter), never from
/// model-supplied text.
-pub(super) fn security_scoped_to_root(security: &SecurityPolicy, root: &Path) -> SecurityPolicy {
+pub(in crate::tools::implementations) fn security_scoped_to_root(
+ security: &SecurityPolicy,
+ root: &Path,
+) -> SecurityPolicy {
let mut scoped = security.clone();
scoped.action_dir = root.to_path_buf();
scoped.trusted_roots.push(TrustedRoot {
diff --git a/crates/openhuman-core/src/tools/impl/filesystem/mod_tests.rs b/crates/openhuman-core/src/tools/impl/filesystem/mod_tests.rs
index 08e68815e0..1d90243e59 100644
--- a/crates/openhuman-core/src/tools/impl/filesystem/mod_tests.rs
+++ b/crates/openhuman-core/src/tools/impl/filesystem/mod_tests.rs
@@ -5,6 +5,7 @@
use super::gate::security_scoped_to_root;
use crate::security::policy::TrustedAccess;
use crate::security::SecurityPolicy;
+use crate::tools::implementations::system::security_for_tool_context;
use std::path::Path;
use tinyagents_harness::context::{RunConfig, RunContext};
use tinyagents_harness::tool::ToolExecutionContext;
@@ -19,20 +20,6 @@ fn tool_context_with_workspace(root: &Path) -> ToolExecutionContext {
ToolExecutionContext::from_run_context(&ctx, tinyagents_harness::ids::CallId::new("test-call"))
}
-/// The policy the tools validate against for a call: scoped to the run's
-/// workspace descriptor when there is one, otherwise untouched. This is what
-/// `tinytools_std::filesystem` does through `FsGate::scoped_to_workspace`.
-fn security_for_tool_context(
- security: &SecurityPolicy,
- context: Option<&dyn ToolRunContext>,
- _tool: &str,
-) -> SecurityPolicy {
- match context.and_then(|ctx| ctx.workspace()) {
- Some(workspace) => security_scoped_to_root(security, &workspace.root),
- None => security.clone(),
- }
-}
-
/// A policy whose `workspace_dir`/`action_dir` are the OpenHuman home — i.e. a
/// directory that does NOT contain the descriptor root. `workspace_only` is on,
/// matching the default tier, so an absolute path outside the workspace is
@@ -208,3 +195,18 @@ fn descriptor_grant_is_read_write_and_additive() {
assert_eq!(granted.path, project.path().to_string_lossy());
assert_eq!(granted.access, TrustedAccess::ReadWrite);
}
+
+#[test]
+fn system_and_filesystem_tools_share_workspace_scoped_policy() {
+ let home = tempfile::tempdir().expect("home");
+ let project = tempfile::tempdir().expect("project");
+ let base = policy_rooted_at(home.path());
+ let ctx = tool_context_with_workspace(project.path());
+
+ let system = security_for_tool_context(&base, Some(&ctx), "shell");
+ let filesystem = security_scoped_to_root(&base, project.path());
+
+ assert_eq!(system.action_dir, filesystem.action_dir);
+ assert_eq!(system.trusted_roots, filesystem.trusted_roots);
+ assert_eq!(system.workspace_dir, filesystem.workspace_dir);
+}
diff --git a/crates/openhuman-core/src/tools/impl/system/mod.rs b/crates/openhuman-core/src/tools/impl/system/mod.rs
index 83ea101862..5dca6c47b4 100644
--- a/crates/openhuman-core/src/tools/impl/system/mod.rs
+++ b/crates/openhuman-core/src/tools/impl/system/mod.rs
@@ -1,8 +1,7 @@
//! Cross-cutting system tools: shell, Node/npm/Python execution, tool
//! detection/install, time helpers, LSP, proxy config, update check/apply.
//! Each family, its tool structs, and its registration gates are catalogued in
-//! `tools/impl/README.md`; `security_for_tool_context` below is the shared
-//! `SecurityPolicy` resolver that must stay in step with the `filesystem` copy.
+//! `tools/impl/README.md`.
mod install_tool;
mod lsp;
@@ -16,7 +15,7 @@ mod shell_support;
mod update_apply;
mod update_check;
-use crate::security::policy::{TrustedAccess, TrustedRoot};
+use crate::tools::implementations::filesystem::gate::security_scoped_to_root;
use crate::security::SecurityPolicy;
use tinytools::ToolRunContext;
@@ -29,28 +28,8 @@ pub use shell::ShellTool;
pub use update_apply::UpdateApplyTool;
pub use update_check::UpdateCheckTool;
-/// Clone `security` and scope it to the run's workspace descriptor, if any.
-///
-/// The process-tool counterpart of
-/// `tools/impl/filesystem/gate.rs` (`security_scoped_to_root`), and it must stay in step
-/// with it: the descriptor's root becomes both the relative-path resolution
-/// root (`action_dir`) **and** a `ReadWrite` trusted root. The grant is the
-/// load-bearing half — `action_dir` only decides where a relative path lands,
-/// while the allow/deny decision reads `workspace_dir` + `trusted_roots`
-/// (`SecurityPolicy::is_resolved_path_allowed_for`). Granting it in the
-/// filesystem copy alone would let an agent read and edit a checkout it could
-/// not then build, test, or commit, because the shell tool resolves its path through here.
-///
-/// The grant is *additive and per-call*: it is pushed onto a clone, so nothing
-/// process-global is mutated and concurrent turns cannot race each other. It
-/// cannot widen the hard invariants either — `is_always_forbidden` and
-/// `is_workspace_internal_path` are both evaluated *before* any trusted-root
-/// shortcut.
-///
-/// The root always originates from trusted in-process code (the session
-/// builder, the sub-agent runner, or the `cwd` RPC parameter) — never from
-/// model-supplied text.
-pub(super) fn security_for_tool_context(
+/// Clone and scope `security` to the run's workspace descriptor, if any.
+pub(in crate::tools::implementations) fn security_for_tool_context(
security: &SecurityPolicy,
context: Option<&dyn ToolRunContext>,
tool: &str,
@@ -63,11 +42,7 @@ pub(super) fn security_for_tool_context(
policy_id = %workspace.policy_id,
"[tools:system] granting TinyAgents workspace descriptor as action dir + trusted root"
);
- scoped.action_dir = workspace.root.clone();
- scoped.trusted_roots.push(TrustedRoot {
- path: workspace.root.to_string_lossy().to_string(),
- access: TrustedAccess::ReadWrite,
- });
+ scoped = security_scoped_to_root(&scoped, &workspace.root);
}
scoped
}