use std::hint::black_box;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Instant;
use io_harness::provider::{CompletionRequest, CompletionResponse, ToolCall};
use io_harness::tools::{Tool, ToolFuture, Toolbox};
use io_harness::{
resume_tree, run_tree, run_with, Act, ApproveAll, Containment, Effect, Policy, Provider, Store,
TaskContract, ToolSpec, Verification,
};
use serde_json::json;
struct Echo;
impl Tool for Echo {
fn spec(&self) -> ToolSpec {
ToolSpec {
name: "echo".into(),
description: "Echo the `text` argument back.".into(),
parameters: json!({
"type": "object",
"properties": { "text": { "type": "string" } },
"required": ["text"]
}),
}
}
fn invoke<'a>(&'a self, arguments: &'a serde_json::Value) -> ToolFuture<'a> {
let text = arguments
.get("text")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
Box::pin(async move { Ok(text) })
}
}
struct Ledger {
name: String,
calls: Arc<Mutex<Vec<String>>>,
answer: String,
}
impl Ledger {
fn new(name: &str, answer: &str) -> Self {
Self {
name: name.into(),
calls: Arc::new(Mutex::new(Vec::new())),
answer: answer.into(),
}
}
}
impl Tool for Ledger {
fn spec(&self) -> ToolSpec {
ToolSpec {
name: self.name.clone(),
description: "Look up an order the filesystem does not know about.".into(),
parameters: json!({
"type": "object",
"properties": { "id": { "type": "string" } },
"required": ["id"]
}),
}
}
fn invoke<'a>(&'a self, arguments: &'a serde_json::Value) -> ToolFuture<'a> {
Box::pin(async move {
let id = arguments
.get("id")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string();
tokio::task::yield_now().await;
self.calls.lock().unwrap().push(id.clone());
Ok(format!("{}={}", id, self.answer))
})
}
}
struct Broken;
impl Tool for Broken {
fn spec(&self) -> ToolSpec {
ToolSpec {
name: "broken".into(),
description: "Always fails.".into(),
parameters: json!({ "type": "object", "properties": {} }),
}
}
fn invoke<'a>(&'a self, _arguments: &'a serde_json::Value) -> ToolFuture<'a> {
Box::pin(async move {
Err(io_harness::Error::Config(
"the upstream service is down".into(),
))
})
}
}
struct Fixed {
name: String,
result: String,
}
impl Fixed {
fn new(name: &str, result: &str) -> Self {
Self {
name: name.into(),
result: result.into(),
}
}
}
impl Tool for Fixed {
fn spec(&self) -> ToolSpec {
ToolSpec {
name: self.name.clone(),
description: "Returns a fixed string.".into(),
parameters: json!({ "type": "object", "properties": {} }),
}
}
fn invoke<'a>(&'a self, _arguments: &'a serde_json::Value) -> ToolFuture<'a> {
let result = self.result.clone();
Box::pin(async move { Ok(result) })
}
}
struct MockScript {
steps: Vec<Vec<ToolCall>>,
at: AtomicUsize,
seen: Arc<Mutex<Vec<CompletionRequest>>>,
}
impl MockScript {
fn new(steps: Vec<Vec<ToolCall>>) -> Self {
Self {
steps,
at: AtomicUsize::new(0),
seen: Arc::new(Mutex::new(Vec::new())),
}
}
fn calls(&self) -> usize {
self.at.load(Ordering::SeqCst)
}
}
impl Provider for MockScript {
async fn complete(&self, req: CompletionRequest) -> io_harness::Result<CompletionResponse> {
let i = self.at.fetch_add(1, Ordering::SeqCst);
self.seen.lock().unwrap().push(req);
Ok(CompletionResponse {
tool_calls: self.steps.get(i).cloned().unwrap_or_default(),
..Default::default()
})
}
}
fn call(name: &str, args: serde_json::Value) -> ToolCall {
ToolCall {
name: name.into(),
arguments: args,
}
}
fn ws() -> tempfile::TempDir {
tempfile::tempdir().unwrap()
}
fn never_passes(root: &std::path::Path, steps: u32) -> TaskContract {
TaskContract::workspace(
"exercise the registered tools",
root,
Verification::WorkspaceFileContains {
file: "unreachable.txt".into(),
needle: "never".into(),
},
)
.with_max_steps(steps)
}
fn open_policy() -> Policy {
Policy::default()
.layer("test")
.allow_read("*")
.allow_write("*")
.allow_exec("*")
}
fn containment() -> Containment {
Containment::new(10, 4, 3, 1_000_000)
}
fn spawn(goal: &str, file: &str, needle: &str) -> ToolCall {
call(
"spawn_agent",
json!({ "goal": goal, "verify_file": file, "verify_contains": needle }),
)
}
const CHILD_GOAL: &str = "look the order up";
fn turns_of(seen: &Mutex<Vec<CompletionRequest>>, goal: &str) -> Vec<CompletionRequest> {
let prefix = format!("Goal: {goal}");
seen.lock()
.unwrap()
.iter()
.filter(|r| r.user.starts_with(&prefix))
.cloned()
.collect()
}
#[tokio::test]
async fn a_registered_tool_may_not_shadow_a_built_in() {
for reserved in [
"write_file",
"grep",
"find",
"read_file",
"spawn_agent",
"read_skill",
] {
let dir = ws();
let contract =
never_passes(dir.path(), 1).with_tools(Toolbox::new().with(Fixed::new(reserved, "x")));
let provider = MockScript::new(vec![]);
let err = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.expect_err("a tool shadowing a built-in must be rejected");
assert!(
matches!(err, io_harness::Error::Config(ref m) if m.contains(reserved)),
"expected a Config error naming {reserved}, got {err:?}"
);
assert_eq!(
provider.calls(),
0,
"arbitration must run before the provider is called"
);
}
}
const NAMES_0_16_2_MISSED: &[&str] = &[
"git_log",
"git_status",
"git_diff",
"git_add",
"git_commit",
"view_image",
"xlsx_read",
"xlsx_sheets",
"xlsx_write",
"xlsx_set_cell",
"docx_read",
"docx_write",
"pptx_read",
"pdf_read",
"pdf_write",
"pdf_watermark",
"pdf_fill_form",
"barcode_decode",
"edit_file",
"exec",
];
#[tokio::test]
async fn no_built_in_name_can_be_taken_by_a_registered_tool() {
for reserved in NAMES_0_16_2_MISSED {
let dir = ws();
let contract =
never_passes(dir.path(), 1).with_tools(Toolbox::new().with(Fixed::new(reserved, "x")));
let provider = MockScript::new(vec![]);
let err = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.expect_err("a tool named after a built-in must be rejected");
assert!(
matches!(err, io_harness::Error::Config(ref m) if m.contains(reserved)),
"expected a Config error naming {reserved}, got {err:?}"
);
assert_eq!(
provider.calls(),
0,
"arbitration must run before the provider is called"
);
}
}
#[test]
fn the_0_16_2_reserved_set_accepted_git_status() {
const RESERVED_IN_0_16_2: &[&str] = &[
"write_file",
"grep",
"find",
"read_file",
"read_skill",
"remember",
"spawn_agent",
];
for name in NAMES_0_16_2_MISSED {
assert!(
!RESERVED_IN_0_16_2.contains(name),
"{name} was already reserved in 0.16.2, so it does not belong in the gap list"
);
}
assert!(
RESERVED_IN_0_16_2.contains(&"write_file"),
"the control list is the real 0.16.2 set, not an empty one"
);
}
#[tokio::test]
async fn a_registered_tool_may_not_use_the_mcp_prefix() {
let dir = ws();
let contract = never_passes(dir.path(), 1)
.with_tools(Toolbox::new().with(Fixed::new("mcp__files__read", "x")));
let provider = MockScript::new(vec![]);
let err = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.expect_err("a tool using the mcp__ prefix must be rejected");
assert!(
matches!(err, io_harness::Error::Config(ref m) if m.contains("mcp__")),
"expected a Config error naming the prefix, got {err:?}"
);
assert_eq!(provider.calls(), 0);
}
#[tokio::test]
async fn two_registered_tools_may_not_share_a_name() {
let dir = ws();
let contract = never_passes(dir.path(), 1).with_tools(
Toolbox::new()
.with(Fixed::new("lookup", "a"))
.with(Fixed::new("lookup", "b")),
);
let provider = MockScript::new(vec![]);
let err = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.expect_err("duplicate tool names must be rejected");
assert!(
matches!(err, io_harness::Error::Config(ref m) if m.contains("lookup")),
"expected a Config error naming the duplicate, got {err:?}"
);
assert_eq!(provider.calls(), 0);
}
#[tokio::test]
async fn a_registered_tool_needs_a_name() {
let dir = ws();
let contract = never_passes(dir.path(), 1).with_tools(Toolbox::new().with(Fixed::new("", "x")));
let provider = MockScript::new(vec![]);
let err = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.expect_err("an unnamed tool must be rejected");
assert!(matches!(err, io_harness::Error::Config(_)), "got {err:?}");
assert_eq!(provider.calls(), 0);
}
#[tokio::test]
async fn a_legally_named_toolbox_runs() {
let dir = ws();
let contract = never_passes(dir.path(), 1).with_tools(
Toolbox::new()
.with(Echo)
.with(Ledger::new("lookup_order", "shipped"))
.with(Broken),
);
let provider = MockScript::new(vec![vec![]]);
let result = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.expect("a legal toolbox must not be rejected");
assert!(
provider.calls() >= 1,
"the loop must have reached the provider"
);
let _ = result;
}
#[tokio::test]
async fn a_registered_tool_is_offered_called_and_its_result_observed() {
let dir = ws();
let tool = Ledger::new("lookup_order", "shipped");
let calls = tool.calls.clone();
let contract = never_passes(dir.path(), 2).with_tools(Toolbox::new().with(tool));
let provider = MockScript::new(vec![vec![call("lookup_order", json!({ "id": "A-17" }))]]);
let seen = provider.seen.clone();
let store = Store::memory().unwrap();
let result = run_with(&contract, &provider, &store, &open_policy(), &ApproveAll)
.await
.unwrap();
let (first_tools, first_system, second_user) = {
let requests = seen.lock().unwrap();
let names: Vec<String> = requests[0].tools.iter().map(|t| t.name.clone()).collect();
(names, requests[0].system.clone(), requests[1].user.clone())
};
assert!(
first_tools.iter().any(|n| n == "lookup_order"),
"the registered tool must be in the request's tool list, got {first_tools:?}"
);
assert!(
first_system.contains("lookup_order"),
"the system prompt must name the registered tool, got: {first_system}"
);
assert_eq!(
calls.lock().unwrap().as_slice(),
["A-17"],
"invoke must run with the model's args"
);
assert!(
second_user.contains("A-17=shipped"),
"the tool's result must reach the next turn, got: {second_user}"
);
let steps = store.steps(result.run_id).unwrap();
let first_step = &steps[0];
assert!(
first_step.tool_call.contains("lookup_order"),
"the trace must record the tool call"
);
assert!(
first_step.tool_call.contains("A-17"),
"the trace must record the arguments"
);
assert!(
first_step.decision.contains("lookup_order"),
"the trace must record the decision, got {:?}",
first_step.decision
);
}
#[tokio::test]
async fn a_registered_tool_is_refused_by_the_policy_without_being_entered() {
let dir = ws();
let tool = Ledger::new("lookup_order", "shipped");
let calls = tool.calls.clone();
let contract = never_passes(dir.path(), 2).with_tools(Toolbox::new().with(tool));
let policy = Policy::default()
.layer("base")
.allow_read("*")
.allow_write("*")
.allow_exec("*")
.deny_exec("lookup_order");
let provider = MockScript::new(vec![vec![call("lookup_order", json!({ "id": "A-17" }))]]);
let seen = provider.seen.clone();
let store = Store::memory().unwrap();
let result = run_with(&contract, &provider, &store, &policy, &ApproveAll)
.await
.unwrap();
assert!(
calls.lock().unwrap().is_empty(),
"a refused call must never enter the tool's implementation"
);
let events = store.events(result.run_id).unwrap();
let refusal = events
.iter()
.find(|e| e.kind == "refusal" && e.target == "lookup_order")
.expect("the refusal must be in the trace");
assert_eq!(refusal.act, "exec");
assert_eq!(refusal.rule.as_deref(), Some("lookup_order"));
assert_eq!(refusal.layer.as_deref(), Some("base"));
let second = &seen.lock().unwrap()[1];
assert!(
second.user.contains("refused"),
"the model must see the refusal as an observation, got: {}",
second.user
);
assert!(
matches!(
result.outcome,
io_harness::RunOutcome::StepCapReached { .. }
),
"a refusal is not a failed run, got {:?}",
result.outcome
);
}
#[tokio::test]
async fn a_failing_tool_becomes_an_observation_not_a_failed_run() {
let dir = ws();
let contract = never_passes(dir.path(), 2).with_tools(Toolbox::new().with(Broken));
let provider = MockScript::new(vec![vec![call("broken", json!({}))]]);
let seen = provider.seen.clone();
let store = Store::memory().unwrap();
let result = run_with(&contract, &provider, &store, &open_policy(), &ApproveAll)
.await
.unwrap();
let second = &seen.lock().unwrap()[1];
assert!(
second.user.contains("the upstream service is down"),
"the tool's own error text must reach the model, got: {}",
second.user
);
assert!(
matches!(
result.outcome,
io_harness::RunOutcome::StepCapReached { .. }
),
"a failing tool must not end the run, got {:?}",
result.outcome
);
assert_eq!(
store.steps(result.run_id).unwrap().len(),
2,
"the step must still be committed"
);
}
#[tokio::test]
async fn an_oversized_tool_result_is_truncated_before_it_enters_the_context() {
let dir = ws();
let huge = "x".repeat(200_000);
let contract =
never_passes(dir.path(), 2).with_tools(Toolbox::new().with(Fixed::new("firehose", &huge)));
let provider = MockScript::new(vec![vec![call("firehose", json!({}))]]);
let seen = provider.seen.clone();
let store = Store::memory().unwrap();
let result = run_with(&contract, &provider, &store, &open_policy(), &ApproveAll)
.await
.unwrap();
let second = &seen.lock().unwrap()[1];
assert!(
second.user.len() < huge.len(),
"the oversized result must not reach the model whole ({} chars)",
second.user.len()
);
assert!(
second.user.contains("truncated"),
"truncation must be visible to the model rather than silent"
);
let step = &store.steps(result.run_id).unwrap()[0];
assert!(
step.result.len() < huge.len(),
"the trace must record the truncated form, not the original"
);
}
#[tokio::test]
async fn a_spawned_child_inherits_the_toolbox_and_the_call_is_its_own() {
let dir = ws();
let tool = Ledger::new("lookup_order", "shipped");
let calls = tool.calls.clone();
let contract = never_passes(dir.path(), 2).with_tools(Toolbox::new().with(tool));
let provider = MockScript::new(vec![
vec![spawn(CHILD_GOAL, "child_done.txt", "OK")],
vec![call("lookup_order", json!({ "id": "A-17" }))],
vec![call(
"write_file",
json!({ "path": "child_done.txt", "content": "OK" }),
)],
]);
let seen = provider.seen.clone();
let store = Store::memory().unwrap();
let result = run_tree(
&contract,
&provider,
&store,
&Policy::permissive(),
&ApproveAll,
&containment(),
)
.await
.unwrap();
let child_turns = turns_of(&seen, CHILD_GOAL);
assert!(
child_turns.len() >= 2,
"the child must have taken its own turns, got {}",
child_turns.len()
);
let names: Vec<String> = child_turns[0]
.tools
.iter()
.map(|t| t.name.clone())
.collect();
assert!(
names.iter().any(|n| n == "lookup_order"),
"the child's request must carry the inherited tool, got {names:?}"
);
assert!(
child_turns[0].system.contains("lookup_order"),
"the child's system prompt must name it, got: {}",
child_turns[0].system
);
assert_eq!(
calls.lock().unwrap().as_slice(),
["A-17"],
"the child must run the parent's implementation, not a copy of it"
);
assert!(
child_turns[1].user.contains("A-17=shipped"),
"the child observes its own tool's result, got: {}",
child_turns[1].user
);
let child_run = store.children(result.run_id).unwrap()[0];
assert!(
store
.steps(child_run)
.unwrap()
.iter()
.any(|s| s.tool_call.contains("lookup_order") && s.tool_call.contains("A-17")),
"the child's trace must record the call under the child's run_id"
);
assert!(
!store
.steps(result.run_id)
.unwrap()
.iter()
.any(|s| s.tool_call.contains("lookup_order")),
"the call belongs to the child's run, not the parent's"
);
assert!(
matches!(
result.outcome,
io_harness::RunOutcome::StepCapReached { .. }
),
"got {:?}",
result.outcome
);
}
#[tokio::test]
async fn a_toolbox_survives_a_tree_resume_and_still_reaches_a_child() {
let dir = ws();
let tool = Arc::new(Ledger::new("lookup_order", "shipped"));
let calls = tool.calls.clone();
let toolbox = || Toolbox::new().with_arc(tool.clone() as Arc<dyn Tool>);
let store = Store::memory().unwrap();
let crashed = run_tree(
&never_passes(dir.path(), 1).with_tools(toolbox()),
&MockScript::new(vec![vec![]]),
&store,
&Policy::permissive(),
&ApproveAll,
&containment(),
)
.await
.unwrap();
assert!(
matches!(
crashed.outcome,
io_harness::RunOutcome::StepCapReached { .. }
),
"the first leg must stop mid-task, got {:?}",
crashed.outcome
);
assert!(calls.lock().unwrap().is_empty(), "nothing called yet");
let provider = MockScript::new(vec![
vec![spawn(CHILD_GOAL, "child_done.txt", "OK")],
vec![call("lookup_order", json!({ "id": "A-99" }))],
vec![call(
"write_file",
json!({ "path": "child_done.txt", "content": "OK" }),
)],
]);
let seen = provider.seen.clone();
let resumed = resume_tree(
&never_passes(dir.path(), 3).with_tools(toolbox()),
&provider,
&store,
crashed.run_id,
&Policy::permissive(),
&ApproveAll,
&containment(),
)
.await
.unwrap();
let child_turns = turns_of(&seen, CHILD_GOAL);
assert!(
!child_turns.is_empty(),
"the resumed tree must have spawned a child"
);
let names: Vec<String> = child_turns[0]
.tools
.iter()
.map(|t| t.name.clone())
.collect();
assert!(
names.iter().any(|n| n == "lookup_order"),
"the toolbox must survive the resume and reach the child, got {names:?}"
);
assert_eq!(
calls.lock().unwrap().as_slice(),
["A-99"],
"the resumed child must reach the same implementation"
);
let child_run = store.children(resumed.run_id).unwrap()[0];
assert!(
store
.steps(child_run)
.unwrap()
.iter()
.any(|s| s.tool_call.contains("lookup_order")),
"the resumed child's call is in its own trace"
);
}
#[tokio::test]
async fn dispatching_a_registered_tool_costs_under_a_millisecond_over_a_direct_call() {
const CALLS: u32 = 1000;
let args = json!({ "text": "ping" });
let direct = |a: &serde_json::Value| {
a.get("text")
.and_then(|v| v.as_str())
.unwrap_or("")
.to_string()
};
let toolbox = Toolbox::new()
.with(Ledger::new("lookup_order", "shipped"))
.with(Fixed::new("firehose", "x"))
.with(Echo);
let policy = open_policy();
assert!(toolbox.owns("echo"));
assert_eq!(policy.check(Act::Exec, "echo").effect, Effect::Allow);
assert_eq!(
toolbox.get("echo").unwrap().invoke(&args).await.unwrap(),
direct(&args)
);
let direct_elapsed = {
let start = Instant::now();
for _ in 0..CALLS {
black_box(direct(black_box(&args)));
}
start.elapsed()
};
let toolbox_elapsed = {
let start = Instant::now();
for _ in 0..CALLS {
let name = black_box("echo");
black_box(toolbox.owns(name));
let out = toolbox.get(name).unwrap().invoke(black_box(&args)).await;
black_box(out.unwrap());
}
start.elapsed()
};
let harness_elapsed = {
let start = Instant::now();
for _ in 0..CALLS {
let name = black_box("echo");
black_box(toolbox.owns(name));
black_box(policy.check(Act::Exec, name));
let out = toolbox.get(name).unwrap().invoke(black_box(&args)).await;
black_box(out.unwrap());
}
start.elapsed()
};
let overhead_ns = (harness_elapsed.as_nanos() as i128 - direct_elapsed.as_nanos() as i128)
/ i128::from(CALLS);
println!(
"NF3 dispatch: {CALLS} calls harness {harness_elapsed:?}, direct {direct_elapsed:?}, \
overhead {overhead_ns} ns/call (toolbox half alone {toolbox_elapsed:?}; the rest is the \
policy's Act::Exec check, which matches its globs against the tool name)"
);
assert!(
overhead_ns < 1_000_000,
"dispatch must add under 1 ms per call; added {overhead_ns} ns \
(harness {harness_elapsed:?} vs direct {direct_elapsed:?} over {CALLS} calls)"
);
}
#[tokio::test]
async fn a_contract_with_no_registered_tools_behaves_as_before() {
let dir = ws();
let contract = TaskContract::workspace(
"write the note",
dir.path(),
Verification::WorkspaceFileContains {
file: "NOTES.md".into(),
needle: "hello".into(),
},
)
.with_max_steps(2)
.with_constraint("keep it short");
let provider = MockScript::new(vec![vec![call(
"write_file",
json!({ "path": "NOTES.md", "content": "hello" }),
)]]);
let result = run_with(
&contract,
&provider,
&Store::memory().unwrap(),
&open_policy(),
&ApproveAll,
)
.await
.unwrap();
assert!(
matches!(result.outcome, io_harness::RunOutcome::Success { .. }),
"an unregistered contract must behave exactly as 0.8.1, got {:?}",
result.outcome
);
}