From 1077ccea93ccc7d10cc8f8610db7e7075751b4c3 Mon Sep 17 00:00:00 2001 From: LHagfoss Date: Sun, 22 Nov 2026 20:10:06 +0100 Subject: [PATCH] refactor(tools): move ToolExecutionOutput into rustcode-tools Orchestration, Smart UI, preview and desktop control build tool results from the core crate instead of the agent's tools module. --- rustcode/core/rustcode-tools/src/lib.rs | 4 ++ rustcode/core/rustcode-tools/src/output.rs | 68 +++++++++++++++++++ rustcode/engine/src/desktop_control.rs | 3 +- rustcode/engine/src/lib.rs | 10 +-- rustcode/engine/src/orchestration.rs | 19 +++--- rustcode/engine/src/orchestration/local.rs | 4 +- .../engine/src/orchestration/local_threads.rs | 4 +- rustcode/engine/src/orchestration/outside.rs | 2 +- rustcode/engine/src/orchestration/secret.rs | 11 +-- rustcode/engine/src/preview.rs | 3 +- rustcode/engine/src/smart_ui.rs | 3 +- rustcode/engine/src/tools/mod.rs | 67 +----------------- 12 files changed, 105 insertions(+), 93 deletions(-) create mode 100644 rustcode/core/rustcode-tools/src/output.rs diff --git a/rustcode/core/rustcode-tools/src/lib.rs b/rustcode/core/rustcode-tools/src/lib.rs index bbe907ada..a4f91a16b 100644 --- a/rustcode/core/rustcode-tools/src/lib.rs +++ b/rustcode/core/rustcode-tools/src/lib.rs @@ -7,6 +7,10 @@ //! now because their ignore/walk policy is coupled to the search orchestration; //! directory listing is intentionally small enough to share this boundary. +mod output; + +pub use output::ToolExecutionOutput; + use std::cell::RefCell; use std::path::{Path, PathBuf}; diff --git a/rustcode/core/rustcode-tools/src/output.rs b/rustcode/core/rustcode-tools/src/output.rs new file mode 100644 index 000000000..70b59b37f --- /dev/null +++ b/rustcode/core/rustcode-tools/src/output.rs @@ -0,0 +1,68 @@ +//! What a tool call returns: its display text and the facts beside it. + +/// Authoritative facts returned by a tool invocation alongside its display +/// text. Consumers must not reconstruct these fields from `content`. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ToolExecutionOutput { + pub content: String, + pub success: bool, + pub pending: bool, + pub command: Option, + pub exit_code: Option, + pub truncated: bool, + /// Machine-readable completeness of the output delivered by the tool. + /// This must be set by the execution layer, never inferred from display text. + pub completeness: rustcode_core::ToolResultCompleteness, + /// True when the harness served a bounded cached read instead of running + /// the tool again. This is execution state, not display prose. + pub replayed: bool, + pub error_kind: Option, + pub retryable: bool, + pub command_status: Option, +} + +impl ToolExecutionOutput { + pub fn success(content: String) -> Self { + Self { + content, + success: true, + pending: false, + command: None, + exit_code: None, + truncated: false, + completeness: rustcode_core::ToolResultCompleteness::Complete, + replayed: false, + error_kind: None, + retryable: false, + command_status: None, + } + } + + pub fn failure(content: String) -> Self { + Self { + content, + success: false, + pending: false, + command: None, + exit_code: None, + truncated: false, + completeness: rustcode_core::ToolResultCompleteness::Complete, + replayed: false, + error_kind: Some(rustcode_core::ToolErrorKind::Internal), + retryable: false, + command_status: None, + } + } + + pub fn failure_with_kind( + content: String, + error_kind: rustcode_core::ToolErrorKind, + retryable: bool, + ) -> Self { + Self { + error_kind: Some(error_kind), + retryable, + ..Self::failure(content) + } + } +} diff --git a/rustcode/engine/src/desktop_control.rs b/rustcode/engine/src/desktop_control.rs index f3fb23699..ab573990d 100644 --- a/rustcode/engine/src/desktop_control.rs +++ b/rustcode/engine/src/desktop_control.rs @@ -1,6 +1,7 @@ //! Client-owned native desktop automation. The client enforces user consent //! and snapshot provenance; the engine never executes desktop commands itself. -use crate::tools::{ToolErrorKind, ToolExecutionOutput}; +use rustcode_core::ToolErrorKind; +use rustcode_tools::ToolExecutionOutput; use serde_json::{Value, json}; use std::{sync::Arc, time::Duration}; use tokio::sync::Mutex; diff --git a/rustcode/engine/src/lib.rs b/rustcode/engine/src/lib.rs index 8c333d628..9bce40b65 100644 --- a/rustcode/engine/src/lib.rs +++ b/rustcode/engine/src/lib.rs @@ -89,7 +89,7 @@ pub mod remote_gateway; pub(crate) fn background_task_history_message( task_id: &str, call_id: Option<&str>, - mut output: crate::tools::ToolExecutionOutput, + mut output: rustcode_tools::ToolExecutionOutput, ) -> ChatMessage { // A background command can print a secret it was given as `env`. crate::orchestration::secret::redact_output(&mut output); @@ -129,7 +129,7 @@ pub(crate) fn background_task_history_message( pub struct PendingBackgroundOutput { pub task_id: String, pub call_id: Option, - pub output: crate::tools::ToolExecutionOutput, + pub output: rustcode_tools::ToolExecutionOutput, } pub(crate) fn queue_background_wakeup(state: &mut AppState, task_id: &str) { @@ -210,7 +210,7 @@ mod background_history_tests { #[test] fn background_completion_is_one_tool_message_answering_a_call() { - let output = || crate::tools::ToolExecutionOutput::success("done".to_owned()); + let output = || rustcode_tools::ToolExecutionOutput::success("done".to_owned()); let message = background_task_history_message("task_7", Some("call_bg"), output()); assert_eq!(message.role, "tool"); @@ -231,7 +231,7 @@ mod background_history_tests { let message = background_task_history_message( "task_42", None, - crate::tools::ToolExecutionOutput { + rustcode_tools::ToolExecutionOutput { content: raw.clone(), success: false, pending: false, @@ -275,7 +275,7 @@ mod background_history_tests { let message = background_task_history_message( "task_43", None, - crate::tools::ToolExecutionOutput { + rustcode_tools::ToolExecutionOutput { content: "exit code: 0\n[Output truncated:]\nFull output saved to: /tmp/spoof" .to_string(), success: false, diff --git a/rustcode/engine/src/orchestration.rs b/rustcode/engine/src/orchestration.rs index 23f61838e..344c2697f 100644 --- a/rustcode/engine/src/orchestration.rs +++ b/rustcode/engine/src/orchestration.rs @@ -20,7 +20,8 @@ use serde_json::{Value, json}; use tokio::sync::Mutex; use crate::preview::PreviewClient; -use crate::tools::{ToolErrorKind, ToolExecutionOutput}; +use rustcode_core::ToolErrorKind; +use rustcode_tools::ToolExecutionOutput; pub mod ask_user; pub mod local; @@ -2346,23 +2347,23 @@ mod tests { // activity read, v7 the queue control, v8 that native subagents go // through RustCode's tools, v9 a delegate's pull request. - let tokens = crate::network::compaction::estimate_tokens(PROMPT_HINT_V11); + let tokens = rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V11); println!( "prompt hints: v3 {} tokens, v4 {} tokens, v5 {} tokens, v6 {} tokens, v9 {tokens} tokens", - crate::network::compaction::estimate_tokens(PROMPT_HINT_V3), - crate::network::compaction::estimate_tokens(PROMPT_HINT_V4), - crate::network::compaction::estimate_tokens(PROMPT_HINT_V5), - crate::network::compaction::estimate_tokens(PROMPT_HINT_V6) + rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V3), + rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V4), + rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V5), + rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V6) ); // v9 adds the three delegate pull-request tools, all named with their use, // and how an orchestrator keeps its own context small: those few lines // save far more than they cost on each request. assert!(tokens < 1000, "{tokens}"); // v12 is the v11 hint plus the machine sentences, still bounded. - let v12 = crate::network::compaction::estimate_tokens(PROMPT_HINT_V12); + let v12 = rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V12); assert!(v12 < 1150, "{v12}"); // v13 adds the worktree pair, and the seconds each call saves. - let v13 = crate::network::compaction::estimate_tokens(PROMPT_HINT_V13); + let v13 = rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V13); assert!(v13 < 1200, "{v13}"); } @@ -3417,7 +3418,7 @@ mod tests { #[test] fn the_version_two_hint_stays_short() { - let tokens = crate::network::compaction::estimate_tokens(PROMPT_HINT_V2); + let tokens = rustcode_core::tokens::estimate_tokens(PROMPT_HINT_V2); assert!(tokens < 115, "{tokens}"); for name in tool_names(2) { assert!(PROMPT_HINT_V2.contains(name), "{name}"); diff --git a/rustcode/engine/src/orchestration/local.rs b/rustcode/engine/src/orchestration/local.rs index f6b185e92..1291be15c 100644 --- a/rustcode/engine/src/orchestration/local.rs +++ b/rustcode/engine/src/orchestration/local.rs @@ -24,7 +24,7 @@ use std::time::Duration; use serde_json::{Value, json}; use tokio_util::sync::CancellationToken; -use crate::app::ChatMessage; +use rustcode_core::ChatMessage; /// The most threads one read returns: what the app's `list_sessions` lists. const MAX_SESSIONS: usize = 50; @@ -47,7 +47,7 @@ pub(crate) async fn answer( cancel: &CancellationToken, name: &str, args: &Value, -) -> crate::tools::ToolExecutionOutput { +) -> rustcode_tools::ToolExecutionOutput { super::output(name, answer_json(session_id, cancel, name, args).await) } diff --git a/rustcode/engine/src/orchestration/local_threads.rs b/rustcode/engine/src/orchestration/local_threads.rs index 923bb3534..173cfc57d 100644 --- a/rustcode/engine/src/orchestration/local_threads.rs +++ b/rustcode/engine/src/orchestration/local_threads.rs @@ -2797,8 +2797,8 @@ pub(super) fn test_thread(dir: &std::path::Path) -> String { crate::sessions::save_session_history( &id, &vec![ - crate::app::ChatMessage::new("user", "a test thread"), - crate::app::ChatMessage::new("assistant", "ok"), + rustcode_core::ChatMessage::new("user", "a test thread"), + rustcode_core::ChatMessage::new("assistant", "ok"), ], ); crate::sessions::flush_history(); diff --git a/rustcode/engine/src/orchestration/outside.rs b/rustcode/engine/src/orchestration/outside.rs index c15a9ffaa..2243bc7e8 100644 --- a/rustcode/engine/src/orchestration/outside.rs +++ b/rustcode/engine/src/orchestration/outside.rs @@ -901,7 +901,7 @@ fn json_of(answer: CallAnswer) -> Option { /// `thread_usage`, `query_data` and `read_activity` read the local data store. /// They cannot be narrowed to a project, so they follow the same switch as /// reading threads across projects. -fn data_tool(tool: &str, args: &Value) -> crate::tools::ToolExecutionOutput { +fn data_tool(tool: &str, args: &Value) -> rustcode_tools::ToolExecutionOutput { use crate::acp::data; let text = |key: &str| args.get(key).and_then(Value::as_str).map(str::to_owned); let number = |key: &str| { diff --git a/rustcode/engine/src/orchestration/secret.rs b/rustcode/engine/src/orchestration/secret.rs index 31e9195e1..04cfe6b31 100644 --- a/rustcode/engine/src/orchestration/secret.rs +++ b/rustcode/engine/src/orchestration/secret.rs @@ -79,7 +79,7 @@ pub fn redact(text: &str) -> Option { } /// Mask held secrets in a tool's result before it reaches the model, the /// transcript or history. -pub fn redact_output(output: &mut crate::tools::ToolExecutionOutput) { +pub fn redact_output(output: &mut rustcode_tools::ToolExecutionOutput) { if let Some(content) = redact(&output.content) { output.content = content; } @@ -453,7 +453,8 @@ mod tests { let value = "history-canary-31415"; set_thread_env(thread, "DEPLOY_TOKEN", value); let echoed = || { - let mut output = crate::tools::ToolExecutionOutput::success(format!("token {value}\n")); + let mut output = + rustcode_tools::ToolExecutionOutput::success(format!("token {value}\n")); output.command = Some(format!("echo {value}")); output }; @@ -507,7 +508,7 @@ pub async fn execute( state: &std::sync::Arc>, cancel: &tokio_util::sync::CancellationToken, args: &Value, -) -> crate::tools::ToolExecutionOutput { +) -> rustcode_tools::ToolExecutionOutput { let (root, asker, id) = { let state = state.lock().await; ( @@ -531,8 +532,8 @@ pub async fn execute( ) .await; if result.success { - crate::tools::ToolExecutionOutput::success(result.content) + rustcode_tools::ToolExecutionOutput::success(result.content) } else { - crate::tools::ToolExecutionOutput::failure(result.content) + rustcode_tools::ToolExecutionOutput::failure(result.content) } } diff --git a/rustcode/engine/src/preview.rs b/rustcode/engine/src/preview.rs index e1b027212..9649673ac 100644 --- a/rustcode/engine/src/preview.rs +++ b/rustcode/engine/src/preview.rs @@ -30,7 +30,8 @@ use std::sync::Arc; use std::time::Duration; use tokio::sync::Mutex; -use crate::tools::{ToolErrorKind, ToolExecutionOutput}; +use rustcode_core::ToolErrorKind; +use rustcode_tools::ToolExecutionOutput; pub const PREVIEW_TOOL_NAME: &str = "localhost_preview"; /// The `clientCapabilities._meta.rustcode.preview` version this engine diff --git a/rustcode/engine/src/smart_ui.rs b/rustcode/engine/src/smart_ui.rs index d14ec0c33..d166b4415 100644 --- a/rustcode/engine/src/smart_ui.rs +++ b/rustcode/engine/src/smart_ui.rs @@ -20,7 +20,8 @@ use std::time::Duration; use tokio::sync::Mutex; use crate::preview::{PreviewClient, decode_screenshot, one_line, page_errors}; -use crate::tools::{ToolErrorKind, ToolExecutionOutput}; +use rustcode_core::ToolErrorKind; +use rustcode_tools::ToolExecutionOutput; pub const PREVIEW_TOOL_NAME: &str = "smart_ui_preview"; /// The extension request the app answers. diff --git a/rustcode/engine/src/tools/mod.rs b/rustcode/engine/src/tools/mod.rs index 0ddf9c005..c16584bbe 100644 --- a/rustcode/engine/src/tools/mod.rs +++ b/rustcode/engine/src/tools/mod.rs @@ -259,72 +259,7 @@ pub fn filter_tools_by_query(query: &str, limit: usize) -> Vec<&'static str> { scored.into_iter().map(|(name, _)| name).collect() } -/// Authoritative facts returned by a tool invocation alongside its display -/// text. Consumers must not reconstruct these fields from `content`. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ToolExecutionOutput { - pub(crate) content: String, - pub(crate) success: bool, - pub(crate) pending: bool, - pub(crate) command: Option, - pub(crate) exit_code: Option, - pub(crate) truncated: bool, - /// Machine-readable completeness of the output delivered by the tool. - /// This must be set by the execution layer, never inferred from display text. - pub(crate) completeness: rustcode_core::ToolResultCompleteness, - /// True when the harness served a bounded cached read instead of running - /// the tool again. This is execution state, not display prose. - pub(crate) replayed: bool, - pub(crate) error_kind: Option, - pub(crate) retryable: bool, - pub(crate) command_status: Option, -} - -impl ToolExecutionOutput { - pub(crate) fn success(content: String) -> Self { - Self { - content, - success: true, - pending: false, - command: None, - exit_code: None, - truncated: false, - completeness: rustcode_core::ToolResultCompleteness::Complete, - replayed: false, - error_kind: None, - retryable: false, - command_status: None, - } - } - - pub(crate) fn failure(content: String) -> Self { - Self { - content, - success: false, - pending: false, - command: None, - exit_code: None, - truncated: false, - completeness: rustcode_core::ToolResultCompleteness::Complete, - replayed: false, - error_kind: Some(ToolErrorKind::Internal), - retryable: false, - command_status: None, - } - } - - pub(crate) fn failure_with_kind( - content: String, - error_kind: ToolErrorKind, - retryable: bool, - ) -> Self { - Self { - error_kind: Some(error_kind), - retryable, - ..Self::failure(content) - } - } -} +pub use rustcode_tools::ToolExecutionOutput; /// How many calls that can change the workspace may run from one response. ///