Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agent): cut oversized tool results for the model in agent sessions (#8871)

Eric Hayes680481de6e95
diff --git a/crates/agent/AGENTS.md b/crates/agent/AGENTS.md
index 062b4297cc..17c452c28e 100644
--- a/crates/agent/AGENTS.md
+++ b/crates/agent/AGENTS.md
@@ -18,6 +18,10 @@ and usage metering.
- `src/stream.rs`: `StreamPart`/`ChatCompletionStream`, the wire shape
callers consume.
- `src/completion.rs`: one-shot, non-looping completions (no tools).
+- `src/tool_result_limit.rs`: `ToolResultLimit` cuts tool results too long for
+ the model (in the hook and on replay through `to_rig_messages_limited`) and
+ keeps them for the `ReadToolResult` pager tool; opt in with
+ `AgentLoop::with_tool_result_limit`.
- `src/structured_output.rs`: prompted JSON generation with offline schema
preflight and result validation; external schema references are rejected.
- `src/tool_media.rs`: promotes explicit top-level `modelMedia` results to
diff --git a/crates/agent/src/agent_loop.rs b/crates/agent/src/agent_loop.rs
index 5c96c1d106..d7ba23c64e 100644
--- a/crates/agent/src/agent_loop.rs
+++ b/crates/agent/src/agent_loop.rs
@@ -9,6 +9,7 @@ use crate::model::{ModelSpeed, PredefinedModel, ReasoningEffort};
use crate::stream::ChatCompletionStream;
use crate::telemetry::GenAiContext;
use crate::tool_adapter::DynToolSetAdapter;
+use crate::tool_result_limit::ToolResultLimit;
use ai_toolset::{RequestContext, SearchableTool, ToolLoader, ToolSet as AiToolSet};
use ai_usage::{UsageContext, UsageRecorder};
use genai_telemetry::ContentPolicy;
@@ -110,6 +111,7 @@ pub struct AgentLoop {
max_tokens: u64,
recorder: Arc<dyn UsageRecorder>,
user_tool_finisher: Option<UserToolFinisher>,
+ result_limit: Option<ToolResultLimit>,
/// The conversation (session) sessions belong to, for telemetry.
conversation_id: Option<String>,
/// The agent name spans carry; defaults to the usage context's feature.
@@ -140,6 +142,7 @@ impl AgentLoop {
max_tokens: DEFAULT_MAX_TOKENS,
recorder,
user_tool_finisher: None,
+ result_limit: None,
conversation_id: None,
agent_name: None,
genai_telemetry: true,
@@ -188,6 +191,14 @@ impl AgentLoop {
self
}
+ /// Cut tool results too long for the model to read whole, keeping them in
+ /// `limit`. The host must offer [`crate::ReadToolResult`] over the same
+ /// `limit`, which the note on a cut result tells the model to call.
+ pub fn with_tool_result_limit(mut self, limit: ToolResultLimit) -> Self {
+ self.result_limit = Some(limit);
+ self
+ }
+
/// Override the model.
///
/// Accepts any stringifiable id — an [`AgentModel`] (backend) or a raw
@@ -467,6 +478,7 @@ impl AgentLoop {
register_loaded,
tool_loads: tool_search.loads.clone(),
user_tool_finisher: self.user_tool_finisher.clone(),
+ result_limit: self.result_limit.clone(),
},
recorder: self.recorder.clone(),
usage_ctx,
diff --git a/crates/agent/src/convert.rs b/crates/agent/src/convert.rs
index 32ab5cf241..77130348de 100644
--- a/crates/agent/src/convert.rs
+++ b/crates/agent/src/convert.rs
@@ -1,3 +1,4 @@
+use crate::tool_result_limit::ToolResultLimit;
/// Conversions between agent message types and `rig` message types.
use crate::types::{AssistantMessagePart, ChatMessage, ChatMessageContent, Role};
use attachment::image::ImageData;
@@ -76,14 +77,26 @@ impl ToUserContent for ImageData {
/// reconstructs the turn boundaries Anthropic expects:
/// `assistant(text + tool_calls)`, `user(tool_results)`, `assistant(text)`.
pub fn to_rig_messages(messages: &[ChatMessage]) -> Vec<Message> {
- messages.iter().flat_map(convert_one).collect()
+ messages
+ .iter()
+ .flat_map(|msg| convert_one(msg, None))
+ .collect()
}
-fn convert_one(msg: &ChatMessage) -> Vec<Message> {
+/// [`to_rig_messages`], with each tool result too long for the model cut as
+/// `limit` cut it when it arrived, so it reads the same in every turn.
+pub fn to_rig_messages_limited(messages: &[ChatMessage], limit: &ToolResultLimit) -> Vec<Message> {
+ messages
+ .iter()
+ .flat_map(|msg| convert_one(msg, Some(limit)))
+ .collect()
+}
+
+fn convert_one(msg: &ChatMessage, limit: Option<&ToolResultLimit>) -> Vec<Message> {
match msg.role {
Role::System => vec![],
Role::User => convert_user(msg),
- Role::Assistant => convert_assistant(msg),
+ Role::Assistant => convert_assistant(msg, limit),
}
}
@@ -116,7 +129,7 @@ fn convert_user(msg: &ChatMessage) -> Vec<Message> {
}
}
-fn convert_assistant(msg: &ChatMessage) -> Vec<Message> {
+fn convert_assistant(msg: &ChatMessage, limit: Option<&ToolResultLimit>) -> Vec<Message> {
let parts = match &msg.content {
ChatMessageContent::Text(text) => {
return vec![Message::assistant(text)];
@@ -168,9 +181,8 @@ fn convert_assistant(msg: &ChatMessage) -> Vec<Message> {
))
})
} else {
- OneOrMany::one(ToolResultContent::text(
- serde_json::to_string(json).unwrap_or_default(),
- ))
+ let text = serde_json::to_string(json).unwrap_or_default();
+ OneOrMany::one(ToolResultContent::text(model_view(limit, id, text)))
};
tool_results.push(UserContent::tool_result_with_call_id(
replay_item_id(id),
@@ -184,7 +196,11 @@ fn convert_assistant(msg: &ChatMessage) -> Vec<Message> {
tool_results.push(UserContent::tool_result_with_call_id(
replay_item_id(id),
replay_call_id(id),
- OneOrMany::one(ToolResultContent::text(description.clone())),
+ OneOrMany::one(ToolResultContent::text(model_view(
+ limit,
+ id,
+ description.clone(),
+ ))),
));
}
AssistantMessagePart::Thinking { .. } => {}
@@ -195,6 +211,14 @@ fn convert_assistant(msg: &ChatMessage) -> Vec<Message> {
out
}
+/// What the model reads of a replayed tool result: `text` itself, unless
+/// `limit` cuts it.
+fn model_view(limit: Option<&ToolResultLimit>, id: &str, text: String) -> String {
+ limit
+ .and_then(|limit| limit.view(id, &text))
+ .unwrap_or(text)
+}
+
/// `call_id` replayed to the provider for a persisted tool call or result.
///
/// The persisted id is the provider call id when one exists (OpenAI's
diff --git a/crates/agent/src/hook.rs b/crates/agent/src/hook.rs
index 86f7e80a75..3f344c879c 100644
--- a/crates/agent/src/hook.rs
+++ b/crates/agent/src/hook.rs
@@ -2,6 +2,7 @@
/// [`StreamPart`] items sent through a channel.
use crate::AgentError;
use crate::stream::{McpInfo, StreamPart, ToolCall, ToolResponse, Usage};
+use crate::tool_result_limit::ToolResultLimit;
use ai_toolset::{SearchableTool, ToolInfo};
use rig_agent::agent::hook::{
AgentHook, HookContext, InvalidToolCallAction, InvalidToolCallContext, ObservationAction,
@@ -142,6 +143,9 @@ pub struct BridgeInputs {
pub tool_loads: ToolLoads,
/// Finishes user tools mid-turn, when the host can.
pub user_tool_finisher: Option<UserToolFinisher>,
+ /// Cuts results too long for the model to read whole, when the host
+ /// offers [`crate::ReadToolResult`] to page through them.
+ pub result_limit: Option<ToolResultLimit>,
}
/// The answer a user tool gives when it has not been finished.
@@ -178,6 +182,8 @@ pub struct StreamBridge {
/// Finishes a user tool's pending answer before the model reads it, on
/// hosts that can reach the user mid-turn (see [`UserToolFinisher`]).
user_tool_finisher: Option<UserToolFinisher>,
+ /// Cuts results too long for the model to read whole.
+ result_limit: Option<ToolResultLimit>,
/// the user has requested the stream stop
cancel: CancellationToken,
}
@@ -204,6 +210,7 @@ impl StreamBridge {
register_loaded,
tool_loads,
user_tool_finisher,
+ result_limit,
} = inputs;
let (tx, rx) = mpsc::unbounded_channel();
(
@@ -215,6 +222,7 @@ impl StreamBridge {
tool_loads,
searchable_catalog,
user_tool_finisher,
+ result_limit,
cancel,
},
rx,
@@ -408,6 +416,16 @@ impl StreamBridge {
}
}
+ // The stream carries the whole result; only the model's copy is cut.
+ // Images reach the model as images and are never cut as text.
+ let view = self
+ .result_limit
+ .as_ref()
+ .filter(|_| {
+ json.as_ref()
+ .is_none_or(|json| json.get("modelMedia").is_none())
+ })
+ .and_then(|limit| limit.view(&id, &presentation.render()));
let response = if let Some(json) = json {
ToolResponse::Json {
id,
@@ -422,7 +440,7 @@ impl StreamBridge {
}
};
let _ = self.tx.send(Ok(StreamPart::ToolResponse(response)));
- ToolResultAction::Keep
+ view.map_or(ToolResultAction::Keep, ToolResultAction::rewrite)
}
pub(crate) fn handle_usage(&self, usage: rig_core::completion::Usage) -> ObservationAction {
diff --git a/crates/agent/src/lib.rs b/crates/agent/src/lib.rs
index f329f68121..b908b02574 100644
--- a/crates/agent/src/lib.rs
+++ b/crates/agent/src/lib.rs
@@ -15,6 +15,7 @@ pub mod structured_output;
mod telemetry;
mod tool_adapter;
mod tool_media;
+mod tool_result_limit;
#[cfg(test)]
mod test;
@@ -23,13 +24,14 @@ pub mod types;
pub use accumulator::StreamAccumulator;
pub use agent_loop::{AgentLoop, Session, SystemPrompt};
pub use completion::{complete, complete_about_image, complete_with_history};
-pub use convert::{merge_consecutive_parts, to_rig_messages};
+pub use convert::{merge_consecutive_parts, to_rig_messages, to_rig_messages_limited};
pub use error::AgentError;
pub use hook::{FinishedUserTool, PendingUserTool, UserToolFinisher};
pub use model::metering::{MeteringContext, MeteringError, ProviderSupport, WireProtocol};
pub use model::{ModelSpeed, PredefinedModel, ReasoningEffort};
pub use stream::{ChatCompletionStream, McpInfo, StreamPart, ToolCall, ToolResponse, Usage};
pub use tool_adapter::{DynToolSetAdapter, ToolsetToolAdapter, normalize_request_schema};
+pub use tool_result_limit::{ReadToolResult, ToolResultLimit};
pub use rig_core::completion::CompletionError;
pub use rig_core::message::Message;
diff --git a/crates/agent/src/test/mod.rs b/crates/agent/src/test/mod.rs
index f0a1a64f6e..025e5075a6 100644
--- a/crates/agent/src/test/mod.rs
+++ b/crates/agent/src/test/mod.rs
@@ -8,3 +8,4 @@ mod test_predefined_model;
mod test_rig_invariants;
mod test_router;
mod test_tool_adapter;
+mod test_tool_result_limit;
diff --git a/crates/agent/src/test/test_hook.rs b/crates/agent/src/test/test_hook.rs
index 4c68bc1e7a..433c648edb 100644
--- a/crates/agent/src/test/test_hook.rs
+++ b/crates/agent/src/test/test_hook.rs
@@ -45,6 +45,7 @@ fn inputs(
register_loaded: register,
tool_loads: ToolLoads::default(),
user_tool_finisher,
+ result_limit: None,
}
}
diff --git a/crates/agent/src/test/test_tool_result_limit.rs b/crates/agent/src/test/test_tool_result_limit.rs
new file mode 100644
index 0000000000..0e46508824
--- /dev/null
+++ b/crates/agent/src/test/test_tool_result_limit.rs
@@ -0,0 +1,194 @@
+use crate::convert::to_rig_messages_limited;
+use crate::hook::{BridgeInputs, StreamBridge, ToolLoads};
+use crate::stream::{StreamPart, ToolResponse};
+use crate::tool_result_limit::ToolResultLimit;
+use crate::types::{AssistantMessagePart, ChatMessage, ChatMessageContent, Role};
+use rig_agent::agent::ToolResultAction;
+use rig_agent::tool::ToolOutput;
+use rig_core::message::{Message, ToolResultContent, UserContent};
+use std::pin::Pin;
+use std::sync::{Arc, Mutex};
+use tokio_util::sync::CancellationToken;
+
+fn limited_bridge(
+ limit: &ToolResultLimit,
+) -> (
+ StreamBridge,
+ tokio::sync::mpsc::UnboundedReceiver<Result<StreamPart, crate::AgentError>>,
+) {
+ StreamBridge::channel(
+ BridgeInputs {
+ routing: Arc::new(|_| None),
+ loaded_buffer: Arc::new(Mutex::new(Vec::new())),
+ register_loaded: Arc::new(|_| {
+ Box::pin(async {}) as Pin<Box<dyn Future<Output = ()> + Send>>
+ }),
+ tool_loads: ToolLoads::default(),
+ user_tool_finisher: None,
+ result_limit: Some(limit.clone()),
+ },
+ Arc::new(vec![]),
+ CancellationToken::new(),
+ )
+}
+
+/// A JSON result of 100,000 characters once serialized: past the limit.
+fn huge_result() -> serde_json::Value {
+ serde_json::json!({ "logs": "é".repeat(99_989) })
+}
+
+#[tokio::test]
+async fn a_result_past_the_limit_reaches_the_model_cut_and_the_stream_whole() {
+ let limit = ToolResultLimit::default();
+ let (bridge, mut rx) = limited_bridge(&limit);
+ let result = huge_result();
+
+ let action = bridge
+ .handle_tool_result(
+ "SearchLogs",
+ Some("toolu_1"),
+ "internal-1",
+ "{}",
+ &ToolOutput::json(result.clone()),
+ true,
+ )
+ .await;
+
+ let ToolResultAction::Rewrite(shown) = action else {
+ panic!("the model's view is cut, got {action:?}");
+ };
+ let shown = shown.as_text().expect("the cut view is text");
+ let full = result.to_string();
+ let preview: String = full.chars().take(16_000).collect();
+ assert_eq!(
+ shown,
+ format!(
+ "{preview}\n\n[Truncated: this result is 100000 characters and only the first \
+ 16000 are shown. Call ReadToolResult with tool_call_id \"toolu_1\" and offset \
+ 16000 to read on.]"
+ )
+ );
+ let Ok(StreamPart::ToolResponse(ToolResponse::Json { json, .. })) = rx.try_recv().unwrap()
+ else {
+ panic!("the stream records the result");
+ };
+ assert_eq!(json, result);
+
+ // The rest reads back in pages to the result's last character.
+ let mut read = preview;
+ let mut offset = Some(16_000);
+ while let Some(from) = offset {
+ let page = limit
+ .read("toolu_1", from, 32_000)
+ .expect("the result is kept");
+ assert_eq!(page.total_chars, 100_000);
+ read.push_str(&page.content);
+ offset = page.next_offset;
+ }
+ assert_eq!(read, full);
+}
+
+#[tokio::test]
+async fn a_result_within_the_limit_reaches_the_model_as_it_is() {
+ let limit = ToolResultLimit::default();
+ let (bridge, _rx) = limited_bridge(&limit);
+
+ let action = bridge
+ .handle_tool_result(
+ "SearchLogs",
+ Some("toolu_2"),
+ "internal-2",
+ "{}",
+ &ToolOutput::json(serde_json::json!({ "logs": "x".repeat(79_000) })),
+ true,
+ )
+ .await;
+
+ assert!(matches!(action, ToolResultAction::Keep));
+ assert!(limit.read("toolu_2", 0, 10).is_none());
+}
+
+#[tokio::test]
+async fn a_replayed_result_reads_as_it_did_in_its_turn() {
+ let turn = ToolResultLimit::default();
+ let (bridge, _rx) = limited_bridge(&turn);
+ let result = huge_result();
+ let ToolResultAction::Rewrite(in_turn) = bridge
+ .handle_tool_result(
+ "SearchLogs",
+ Some("toolu_3"),
+ "internal-3",
+ "{}",
+ &ToolOutput::json(result.clone()),
+ true,
+ )
+ .await
+ else {
+ panic!("the model's view is cut");
+ };
+
+ // A later turn replays the logged result into a fresh limit.
+ let later = ToolResultLimit::default();
+ let history = [ChatMessage {
+ role: Role::Assistant,
+ content: ChatMessageContent::AssistantMessageParts(vec![
+ AssistantMessagePart::ToolCall {
+ name: "SearchLogs".to_owned(),
+ json: serde_json::json!({}),
+ id: "toolu_3".to_owned(),
+ },
+ AssistantMessagePart::ToolCallResponseJson {
+ name: "SearchLogs".to_owned(),
+ json: result,
+ id: "toolu_3".to_owned(),
+ },
+ ]),
+ attachments: None,
+ }];
+ let messages = to_rig_messages_limited(&history, &later);
+
+ let Some(Message::User { content }) = messages.last() else {
+ panic!("the result is replayed as a user message, got {messages:?}");
+ };
+ let UserContent::ToolResult(replayed) = content.first() else {
+ panic!("the user message carries the tool result");
+ };
+ let ToolResultContent::Text(replayed) = replayed.content.first() else {
+ panic!("the replayed result is text");
+ };
+ assert_eq!(Some(replayed.text.as_str()), in_turn.as_text());
+ assert_eq!(
+ later
+ .read("toolu_3", 99_990, 32_000)
+ .map(|page| page.content),
+ Some("éééééééé\"}".to_owned())
+ );
+}
+
+#[tokio::test]
+async fn an_image_result_reaches_the_model_uncut() {
+ let limit = ToolResultLimit::default();
+ let (bridge, _rx) = limited_bridge(&limit);
+ let image = crate::tool_media::model_output(serde_json::json!({
+ "revision": "r1",
+ "modelMedia": [{
+ "type": "image",
+ "mediaType": "image/png",
+ "data": format!("iVBORw0KGgo{}", "A".repeat(100_000)),
+ }],
+ }))
+ .expect("a bounded PNG");
+
+ let action = bridge
+ .handle_tool_result(
+ "RenderCanvas",
+ Some("toolu_4"),
+ "internal-4",
+ "{}",
+ &image,
+ true,
+ )
+ .await;
+
+ assert!(matches!(action, ToolResultAction::Keep));
+}
diff --git a/crates/agent/src/tool_result_limit.rs b/crates/agent/src/tool_result_limit.rs
new file mode 100644
index 0000000000..42ca433584
--- /dev/null
+++ b/crates/agent/src/tool_result_limit.rs
@@ -0,0 +1,151 @@
+//! Keeping one tool result from crowding the model out of its context.
+//!
+//! A tool can answer with megabytes: a search over logs, a downloaded file.
+//! The model reads every result of a turn on every later request, so a few of
+//! those overflow its context window and the provider refuses the turn.
+//!
+//! A loop given a [`ToolResultLimit`] shows the model at most
+//! [`MAX_RESULT_CHARS`] of any result. A longer one is cut to its first
+//! [`PREVIEW_CHARS`] and a note naming the [`ReadToolResult`] tool, which pages
+//! through the whole result the limit keeps. Only the model's view is cut: the
+//! stream, and so the host's log and history, carry the full result, and
+//! [`crate::to_rig_messages_limited`] cuts it the same way each time history is
+//! replayed, so a result reads the same in every turn.
+
+use std::collections::HashMap;
+use std::sync::{Arc, Mutex};
+
+use ai_toolset::{
+ AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolCallError,
+ ToolResult,
+};
+use async_trait::async_trait;
+use schemars::JsonSchema;
+use serde::{Deserialize, Serialize};
+
+/// The longest result the model reads whole: about 25k tokens, the limit
+/// Claude Code sets on an MCP tool's output.
+pub const MAX_RESULT_CHARS: usize = 80_000;
+
+/// How much of a longer result the model reads before the note.
+pub const PREVIEW_CHARS: usize = 16_000;
+
+/// The most one [`ReadToolResult`] call returns. Under [`MAX_RESULT_CHARS`]
+/// even once escaped as JSON, so a page is never cut itself.
+pub const MAX_PAGE_CHARS: usize = 32_000;
+
+/// The results too long for the model to read whole, by tool call id.
+///
+/// Cheap to clone; clones share the results. A host makes one per turn: the
+/// turn's own results are kept as they arrive, and earlier ones as history is
+/// replayed into the turn.
+#[derive(Clone, Default)]
+pub struct ToolResultLimit {
+ kept: Arc<Mutex<HashMap<String, Arc<str>>>>,
+}
+
+impl ToolResultLimit {
+ /// What the model reads of `text`, the result of call `id`, when it is
+ /// too long to read whole: its beginning and a note on reading the rest.
+ /// `None` when it fits.
+ pub(crate) fn view(&self, id: &str, text: &str) -> Option<String> {
+ text.char_indices().nth(MAX_RESULT_CHARS)?;
+ let total = text.chars().count();
+ let preview_end = text
+ .char_indices()
+ .nth(PREVIEW_CHARS)
+ .map_or(text.len(), |(index, _)| index);
+ tracing::info!(
+ tool_call_id = id,
+ chars = total,
+ "truncated a tool result for the model"
+ );
+ self.kept
+ .lock()
+ .expect("kept tool results poisoned")
+ .insert(id.to_owned(), Arc::from(text));
+ Some(format!(
+ "{}\n\n[Truncated: this result is {total} characters and only the first \
+ {PREVIEW_CHARS} are shown. Call ReadToolResult with tool_call_id \"{id}\" and \
+ offset {PREVIEW_CHARS} to read on.]",
+ &text[..preview_end]
+ ))
+ }
+
+ /// Up to `limit` characters of call `id`'s result from character
+ /// `offset`. `None` when no result of that call was cut.
+ pub(crate) fn read(&self, id: &str, offset: usize, limit: usize) -> Option<ToolResultPage> {
+ let text = Arc::clone(
+ self.kept
+ .lock()
+ .expect("kept tool results poisoned")
+ .get(id)?,
+ );
+ let total_chars = text.chars().count();
+ let content: String = text.chars().skip(offset).take(limit).collect();
+ let end = offset.saturating_add(content.chars().count());
+ Some(ToolResultPage {
+ content,
+ total_chars,
+ next_offset: (end < total_chars).then_some(end),
+ })
+ }
+}
+
+/// Read more of a tool result that was too long to show whole.
+#[derive(Debug, Deserialize, JsonSchema)]
+#[schemars(
+ title = "ReadToolResult",
+ description = "Read more of a tool result that was cut because it was too long. Use the tool_call_id and offset from the truncation note, then the returned next_offset to continue. Read only as much as the task needs."
+)]
+pub struct ReadToolResult {
+ /// The call whose result was cut, as the truncation note names it.
+ #[schemars(description = "The tool_call_id from the truncation note.")]
+ pub tool_call_id: String,
+ /// Where to start, in characters from the beginning of the result.
+ #[serde(default)]
+ #[schemars(description = "Character offset to start reading from; defaults to 0.")]
+ pub offset: usize,
+ /// How many characters to read.
+ #[serde(default)]
+ #[schemars(description = "Characters to read, at most 32000; defaults to 32000.")]
+ pub limit: Option<usize>,
+}
+
+/// One page of a cut tool result.
+#[derive(Debug, Serialize, JsonSchema)]
+pub struct ToolResultPage {
+ /// The result's characters from the requested offset.
+ pub content: String,
+ /// How long the whole result is, in characters.
+ pub total_chars: usize,
+ /// Where the next page starts; absent once the result is read to its end.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub next_offset: Option<usize>,
+}
+
+impl ToolAnnotated for ReadToolResult {
+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("Read tool result");
+}
+
+#[async_trait]
+impl AsyncTool<ToolResultLimit> for ReadToolResult {
+ type Output = ToolResultPage;
+
+ async fn call(
+ &self,
+ context: ServiceContext<ToolResultLimit>,
+ _request: RequestContext,
+ ) -> ToolResult<Self::Output> {
+ let limit = self.limit.unwrap_or(MAX_PAGE_CHARS).min(MAX_PAGE_CHARS);
+ context
+ .read(&self.tool_call_id, self.offset, limit)
+ .ok_or_else(|| ToolCallError {
+ description: format!(
+ "No cut result for tool call {}. Use the tool_call_id from a truncation note.",
+ self.tool_call_id
+ ),
+ internal_error: anyhow::anyhow!("no kept tool result"),
+ })
+ }
+}
diff --git a/crates/agent_inmem/AGENTS.md b/crates/agent_inmem/AGENTS.md
index cbf5039e34..c5a487b9cf 100644
--- a/crates/agent_inmem/AGENTS.md
+++ b/crates/agent_inmem/AGENTS.md
@@ -69,6 +69,10 @@ DecideReview are eager internal tools in both standard and code modes.
`models::resolve`.
- Keep session-aware `NameSearch`, `ContentSearch`, and `ListEntities` native in
code mode; legacy `ReadChat` must not appear in either runtime tool catalog.
+- Every turn cuts tool results over `agent::ToolResultLimit`'s limit (about
+ 25k tokens) for the model and offers `ReadToolResult` to page through them.
+ The stream, log and history keep the whole result; replay cuts it again, so
+ build turn messages with `to_rig_messages_limited`, not `to_rig_messages`.
- Replay tool calls using `_meta.macro.toolName`; ACP titles may be human-readable
MCP labels, so only use the title for older frames without tool-name metadata.
diff --git a/crates/agent_inmem/src/rig_engine.rs b/crates/agent_inmem/src/rig_engine.rs
index 534f0f043e..dcce9b7de8 100644
--- a/crates/agent_inmem/src/rig_engine.rs
+++ b/crates/agent_inmem/src/rig_engine.rs
@@ -23,7 +23,7 @@
use std::sync::Arc;
-use agent::{AgentError, AgentLoop, StreamPart, SystemPrompt};
+use agent::{AgentError, AgentLoop, ReadToolResult, StreamPart, SystemPrompt, ToolResultLimit};
use ai_tools::user_tool_review::{ReviewError, UserToolAuthorizer, authorized_user_tool_finisher};
use ai_tools::{AiHost, DeferredToolSet, ToolServiceContext, tools_for};
use ai_toolset::{AsyncToolCollection, ToolSet as AiToolSet};
@@ -126,8 +126,9 @@ impl RigTurnEngine {
}
/// Native tools never gated: asking the user something, and finding or
-/// loading a connected app's tools, spend nobody's access.
-const UNGATED_NATIVE_TOOLS: [&str; 3] = ["AskUser", "SearchTools", "LoadTools"];
+/// loading a connected app's tools, or reading the rest of a result the
+/// turn already got, spend nobody's access.
+const UNGATED_NATIVE_TOOLS: [&str; 4] = ["AskUser", "SearchTools", "LoadTools", "ReadToolResult"];
/// A turn's tools, each call to one of Macro's own asked of the gate first.
/// Remote MCP tools are left to the egress proxy, which gates them already.
@@ -230,6 +231,7 @@ fn add_native_actor_schema(name: &str, schema: &mut schemars::Schema) {
struct InMemToolContext {
base: ToolServiceContext,
ask_user: AskUserContext,
+ tool_results: ToolResultLimit,
}
impl FromRef<InMemToolContext> for ToolServiceContext {
@@ -244,11 +246,19 @@ impl FromRef<InMemToolContext> for AskUserContext {
}
}
+impl FromRef<InMemToolContext> for ToolResultLimit {
+ fn from_ref(context: &InMemToolContext) -> Self {
+ context.tool_results.clone()
+ }
+}
+
fn tools_for_turn(
base_tools: ai_tools::AiToolSet,
supports_user_input: bool,
) -> AsyncToolCollection<InMemToolContext> {
- let tools = AsyncToolCollection::<InMemToolContext>::new().add_subtoolset(base_tools);
+ let tools = AsyncToolCollection::<InMemToolContext>::new()
+ .add_subtoolset(base_tools)
+ .add_tool::<ReadToolResult, ToolResultLimit>();
if supports_user_input {
tools.add_tool::<AskUser, AskUserContext>()
} else {
@@ -579,11 +589,16 @@ async fn drive_turn(
.with_actor(identity.bot)
.with_actor_name(&identity.name);
}
+ // Results too long for the model to read whole are cut, the turn's as
+ // they arrive and earlier ones as history is replayed below, and kept
+ // here for `ReadToolResult`.
+ let tool_results = ToolResultLimit::default();
let tool_context = InMemToolContext {
base: tool_context,
ask_user: AskUserContext {
requester: user_input,
},
+ tool_results: tool_results.clone(),
};
// GenAI telemetry stays off here: this runtime's turns and tool calls
@@ -594,7 +609,8 @@ async fn drive_turn(
.with_model(&model)
.with_reasoning_effort(reasoning_effort)
.with_speed(speed)?
- .with_genai_telemetry(false);
+ .with_genai_telemetry(false)
+ .with_tool_result_limit(tool_results.clone());
// Only the loop's own model calls run on the agent's key, so only they are
// recorded against it; tools keep `usage_ctx`, since they call models on
// Macro's keys.
@@ -688,7 +704,7 @@ async fn drive_turn(
}
}));
- let rig_messages = agent::to_rig_messages(&messages);
+ let rig_messages = agent::to_rig_messages_limited(&messages, &tool_results);
let result = async {
let mut stream = session.send_message(rig_messages).await?;
let mut turn_spoke = false;