Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 92 additions & 53 deletions src/agent/agy/bridged_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,67 +436,19 @@ fn step(index: u64, kind: &str, extra: &serde_json::Value) -> serde_json::Value
/// handed, then ends the turn with a result line.
#[test]
fn fake_agy_child() {
use std::io::Write as _;
let Some(dir) = std::env::var_os(FAKE_AGY) else {
return;
};
let dir = PathBuf::from(dir);
let say = |line: serde_json::Value| println!("{line}");
say(
serde_json::json!({"event":"init","conversation_id":"fake-conv","init":{"tools":["run_command","invoke_subagent"]}}),
);
let hooks_by = std::env::var(HOOKS_BY_ENV).unwrap_or_default();
let result = serde_json::json!({"event":"result","result":{"conversation_id":"fake-conv","status":"SUCCESS","usage":{"input_tokens":1,"output_tokens":1}}});
if let Ok(how) = std::fs::read_to_string(dir.join(NO_SERVER)) {
if how == "error" {
// As agy refuses a model it does not know: at once, no server.
say(
serde_json::json!({"event":"result","result":{"conversation_id":"","status":"ERROR","error":"model x is not recognized"}}),
);
std::process::exit(1);
}
say(step(
1,
"agent_response",
&serde_json::json!({"text_delta": "no tools"}),
));
say(result);
std::process::exit(0);
}
// The one agent file in the folder it was pointed at: this binary's.
let agent = std::fs::read_dir(dir.join("agents"))
.expect("agents folder")
.filter_map(Result::ok)
.map(|e| e.path())
.find(|p| p.extension().is_some_and(|x| x == "md"))
.expect("an agent file");
let file = std::fs::read_to_string(agent).expect("agent file");
let (command, args) = server_in(&file);
let mut server = std::process::Command::new(&command)
.args(&args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("start the tool server");
let mut to = server.stdin.take().expect("stdin");
let mut from = BufReader::new(server.stdout.take().expect("stdout"));
let mut ask = |id: u64, method: &str, params: serde_json::Value| -> serde_json::Value {
let request = serde_json::json!({"jsonrpc":"2.0","id":id,"method":method,"params":params});
writeln!(to, "{request}").expect("send");
to.flush().expect("flush");
loop {
let mut line = String::new();
if from.read_line(&mut line).expect("read") == 0 {
panic!("the tool server went away");
}
// The test harness's own banner is not a reply.
if let Ok(reply) = serde_json::from_str::<serde_json::Value>(&line) {
if reply["id"] == id {
return reply;
}
}
}
};
answer_without_a_server(&dir, &result);
let mut server = start_agent_files_server(&dir);
let mut mcp = McpPipe::of(&mut server);
let mut ask = |id: u64, method: &str, params: serde_json::Value| mcp.ask(id, method, params);
ask(
0,
"initialize",
Expand Down Expand Up @@ -527,7 +479,8 @@ fn fake_agy_child() {
"output": reply["result"]["content"][0]["text"]}}),
));
}
drop(to);
// Its stdin closed, the server ends.
drop(mcp);
let _ = server.wait();
say(step(
9,
Expand All @@ -538,6 +491,92 @@ fn fake_agy_child() {
std::process::exit(0);
}

/// One line of [`fake_agy_child`]'s output, as agy prints them.
fn say(line: serde_json::Value) {
println!("{line}");
}

/// When the test asked [`fake_agy_child`] to start no server (a `NO_SERVER`
/// file in `dir`): it ends the turn without tools, or — the file saying
/// `error` — refuses at once as agy refuses a model it does not know. Returns
/// only when there is no such file.
fn answer_without_a_server(dir: &Path, result: &serde_json::Value) {
let Ok(how) = std::fs::read_to_string(dir.join(NO_SERVER)) else {
return;
};
if how == "error" {
// As agy refuses a model it does not know: at once, no server.
say(
serde_json::json!({"event":"result","result":{"conversation_id":"","status":"ERROR","error":"model x is not recognized"}}),
);
std::process::exit(1);
}
say(step(
1,
"agent_response",
&serde_json::json!({"text_delta": "no tools"}),
));
say(result.clone());
std::process::exit(0);
}

/// The MCP server named by the one agent file in `dir/agents` — this
/// binary's — started with [`fake_agy_child`]'s own environment, as agy
/// starts it.
fn start_agent_files_server(dir: &Path) -> std::process::Child {
// The one agent file in the folder it was pointed at: this binary's.
let agent = std::fs::read_dir(dir.join("agents"))
.expect("agents folder")
.filter_map(Result::ok)
.map(|e| e.path())
.find(|p| p.extension().is_some_and(|x| x == "md"))
.expect("an agent file");
let file = std::fs::read_to_string(agent).expect("agent file");
let (command, args) = server_in(&file);
std::process::Command::new(&command)
.args(&args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("start the tool server")
}

/// JSON-RPC over a started server's stdio, one request at a time, as
/// [`fake_agy_child`] talks to it. Dropped, it closes the server's stdin.
struct McpPipe {
to: std::process::ChildStdin,
from: BufReader<std::process::ChildStdout>,
}

impl McpPipe {
fn of(server: &mut std::process::Child) -> Self {
Self {
to: server.stdin.take().expect("stdin"),
from: BufReader::new(server.stdout.take().expect("stdout")),
}
}

/// Send request `id` and wait for the reply with that `id`.
fn ask(&mut self, id: u64, method: &str, params: serde_json::Value) -> serde_json::Value {
use std::io::Write as _;
let request = serde_json::json!({"jsonrpc":"2.0","id":id,"method":method,"params":params});
writeln!(self.to, "{request}").expect("send");
self.to.flush().expect("flush");
loop {
let mut line = String::new();
if self.from.read_line(&mut line).expect("read") == 0 {
panic!("the tool server went away");
}
// The test harness's own banner is not a reply.
if let Ok(reply) = serde_json::from_str::<serde_json::Value>(&line) {
if reply["id"] == id {
return reply;
}
}
}
}
}

/// A script at `path` that runs this test binary's `test` with `marker` set
/// to `value`, first recording its own arguments to `records_argv` when
/// given: NUL-separated on Unix (`printf '%s\0' "$@"`), as cmd's `%*` on
Expand Down
201 changes: 201 additions & 0 deletions src/toolbox/dispatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//! The tools whose answer is text alone, by family.
//!
//! Wired to: [`Toolbox::dispatch_text`] is reached from `Toolbox::dispatch`
//! (`toolbox/mod.rs`) once every gate has passed (`Toolbox::decide_and_run`,
//! the same file). Siblings that also run a tool, by other routes and from
//! the same `dispatch`, which tries them first: `Screenshot` and `RenderPdf`
//! through the headless browser (`toolbox/browser.rs`, `toolbox/pdf.rs`), the
//! desktop's tools (`Toolbox::run_desktop`) and the client's MCP servers'
//! (`Toolbox::run_client_tool`), both in `toolbox/mod.rs`.

use super::*;

impl Toolbox {
/// The tools whose answer is text alone, reached from `Toolbox::dispatch`.
///
/// Every such tool has one arm in one of three tables, by family — the
/// file tools ([`Self::file_tool`]), the session's own
/// ([`Self::session_tool`]) and the memory's ([`Self::memory_tool`]) —
/// and a tool none of them names is asked of the modules that own their
/// tools ([`Self::tool_elsewhere`]). The tables name distinct tools, so
/// their order decides nothing; `tests.rs` asserts every advertised tool
/// runs.
pub(super) fn dispatch_text(
&self,
call_id: &str,
name: &str,
input: &Value,
) -> Result<String, String> {
// The file and shell tools parse their own JSON from a string, which is
// the interface they were written to; re-serialising here keeps that
// contract rather than rewriting six working implementations.
let raw = input.to_string();
let ws = self.root_of_call(name, input);
self.file_tool(name, ws, &raw, input)
.or_else(|| self.session_tool(call_id, name, ws, &raw, input))
.or_else(|| self.memory_tool(call_id, name, ws, &raw, input))
.unwrap_or_else(|| self.tool_elsewhere(name, input))
}

/// The workspace, or the added directory the call names a path in —
/// and for `Read` alone, a folder only it may open.
fn root_of_call(&self, name: &str, input: &Value) -> &Path {
if name == READ {
self.read_root_for(input)
} else if matches!(
name,
WRITE | EDIT | GLOB | GREP | MULTI_EDIT | NOTEBOOK_EDIT | LS | OUTLINE
) {
self.root_for(input)
} else {
self.workspace.as_path()
}
}

/// [`Self::dispatch_text`]'s file tools; `None` for any other name.
fn file_tool(
&self,
name: &str,
ws: &Path,
raw: &str,
input: &Value,
) -> Option<Result<String, String>> {
Some(match name {
READ => crate::tools::file_ops::read_file(ws, raw).map_err(|e| e.to_string()),
WRITE => crate::tools::file_ops::write_file(ws, raw).map_err(|e| e.to_string()),
EDIT => crate::tools::file_ops::edit_file(ws, raw).map_err(|e| e.to_string()),
MULTI_EDIT => edits::multi_edit(ws, input),
NOTEBOOK_EDIT => edits::notebook_edit(ws, input),
APPLY_PATCH => edits::apply_patch(input, &|path| self.resolve(path)),
LS => listing::list(ws, input),
GLOB => crate::tools::search::glob_search(ws, input),
GREP => crate::tools::search::grep_search(ws, input),
OUTLINE => outline::run(ws, input),
CONVERT_DOCUMENT => self.convert_document(input),
DIAGNOSTICS => checks::diagnostics(ws, input, &|path| self.resolve(path)),
_ => return None,
})
}

/// [`Self::dispatch_text`]'s session tools — the shell, the todo list,
/// skills, subagents and questions; `None` for any other name.
fn session_tool(
&self,
call_id: &str,
name: &str,
ws: &Path,
raw: &str,
input: &Value,
) -> Option<Result<String, String>> {
Some(match name {
BASH => self.run_bash(call_id, ws, raw, input),
BASH_OUTPUT => shell::output(&self.state, input),
KILL_SHELL => shell::kill(&self.state, input),
TODO_WRITE => todo::write(&mut self.state.todos(), input),
SKILL => self.read_skill(input),
AGENT => self.run_subagent(input),
ASK_USER_QUESTION => self.ask_here(input),
_ => return None,
})
}

/// [`Self::dispatch_text`]'s memory tools — verify, recall, the recorder
/// and the brief; `None` for any other name.
fn memory_tool(
&self,
call_id: &str,
name: &str,
ws: &Path,
raw: &str,
input: &Value,
) -> Option<Result<String, String>> {
Some(match name {
crate::verify::VERIFY_TOOL => crate::verify::run_tool(ws),
crate::verify::PLAN_TOOL => Ok(crate::verify::run_plan_tool(ws)),
crate::recall::INDEX_TOOL => crate::recall::run_index_tool(raw, ws),
crate::recall::GET_TOOL => crate::recall::run_get_tool(raw),
crate::recall::TIMELINE_TOOL => crate::recall::run_timeline_tool(raw),
crate::recorder::LOOKUP_TOOL => self.lookup_recorded(call_id, ws, input),
crate::brief::SAVE_TOOL => self.save_brief(raw),
crate::brief_read::READ_TOOL => crate::brief_read::run(ws),
_ => return None,
})
}

/// A tool no table of [`Self::dispatch_text`] names, asked of the modules
/// that own their tools, in order: knowledge, the session record, the web
/// (with the headless browser when one is installed) and the team.
fn tool_elsewhere(&self, other: &str, input: &Value) -> Result<String, String> {
// The page's script, run by the headless browser, for a WebFetch
// that needs it — when the browser is installed.
let render = |url: &str| match self.browser() {
Some(bin) => browser::render_html(bin, url, self.filter_env),
None => Err("no headless browser is installed".to_string()),
};
let render: Option<crate::web::Render<'_>> = self.browser().is_some().then_some(&render);
crate::knowledge::handle_tool(
other,
input,
&crate::knowledge::CodeContext {
cwd: &self.workspace,
session: &self.session,
resolve: &|path| self.resolve(path),
},
)
.or_else(|| {
crate::session_record::handle_tool(
other,
input,
&crate::session_record::Record {
session: &self.session,
workspace: &self.workspace,
},
)
})
.or_else(|| crate::web::handle_tool(other, input, render))
.or_else(|| self.team.handle(other, input))
.unwrap_or_else(|| Err(format!("there is no tool called `{other}`")))
}

/// `ConvertDocument`, when LibreOffice was found.
fn convert_document(&self, input: &Value) -> Result<String, String> {
match self.soffice() {
Some(soffice) => {
convert::run(soffice, input, self.filter_env, &|path| self.resolve(path))
}
None => Err("LibreOffice was not found".to_string()),
}
}

/// `Bash`: started as a job when it asks to run in the background
/// (`toolbox/shell.rs`), otherwise run here (`Toolbox::run_shell`).
fn run_bash(
&self,
call_id: &str,
ws: &Path,
raw: &str,
input: &Value,
) -> Result<String, String> {
if input.get("run_in_background").and_then(Value::as_bool) == Some(true) {
return shell::start(&self.state, ws, input, &self.sandbox()?);
}
// A write through the shell is credited to this agent: the
// engine's window around the command (`shell_window`).
let command = input.get("command").and_then(Value::as_str).unwrap_or("");
self.shell_window("begin", command, call_id);
let ran = self.run_shell(call_id, ws, raw);
self.shell_window("end", command, call_id);
ran
}

/// The recorder's lookup tool; the watch, if there is one, is told
/// whether the recorder had the call.
fn lookup_recorded(&self, call_id: &str, ws: &Path, input: &Value) -> Result<String, String> {
crate::recorder::run_lookup_tool(ws, input).map(|found| {
if let Some(watch) = &self.watch {
watch.recorded(call_id, found.hit, found.observations);
}
found.text
})
}
}
Loading
Loading