zeph_core/
overflow_tools.rs1use std::sync::Arc;
5
6use zeph_memory::store::SqliteStore;
7use zeph_tools::executor::{ToolCall, ToolError, ToolExecutor, ToolOutput, deserialize_params};
8use zeph_tools::registry::{InvocationHint, ToolDef};
9
10#[derive(Debug, Clone, serde::Deserialize, schemars::JsonSchema)]
11struct ReadOverflowParams {
12 id: String,
14}
15
16pub struct OverflowToolExecutor {
17 sqlite: Arc<SqliteStore>,
18 conversation_id: Option<i64>,
19}
20
21impl OverflowToolExecutor {
22 pub const TOOL_NAME: &'static str = "read_overflow";
23
24 #[must_use]
25 pub fn new(sqlite: Arc<SqliteStore>) -> Self {
26 Self {
27 sqlite,
28 conversation_id: None,
29 }
30 }
31
32 #[must_use]
33 pub fn with_conversation(mut self, conversation_id: i64) -> Self {
34 self.conversation_id = Some(conversation_id);
35 self
36 }
37}
38
39impl ToolExecutor for OverflowToolExecutor {
40 fn tool_definitions(&self) -> Vec<ToolDef> {
41 vec![ToolDef {
42 id: Self::TOOL_NAME.into(),
43 description: "Retrieve the full content of a tool output that was truncated due to \
44 size. Use when a previous tool result contains an overflow notice. \
45 Parameters: id (string, required) — the bare UUID from the notice \
46 (e.g. '550e8400-e29b-41d4-a716-446655440000'). \
47 Returns: full original tool output text. Errors: NotFound if the \
48 overflow entry has expired or does not exist."
49 .into(),
50 schema: schemars::schema_for!(ReadOverflowParams),
51 invocation: InvocationHint::ToolCall,
52 output_schema: None,
53 }]
54 }
55
56 async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
57 Ok(None)
58 }
59
60 async fn execute_tool_call(&self, call: &ToolCall) -> Result<Option<ToolOutput>, ToolError> {
61 if call.tool_id != Self::TOOL_NAME {
62 return Ok(None);
63 }
64 let params: ReadOverflowParams = deserialize_params(&call.params)?;
65
66 let id = params.id.strip_prefix("overflow:").unwrap_or(¶ms.id);
67
68 if uuid::Uuid::parse_str(id).is_err() {
69 return Err(ToolError::InvalidParams {
70 message: "id must be a valid UUID".to_owned(),
71 });
72 }
73
74 let Some(conv_id) = self.conversation_id else {
75 return Err(ToolError::Execution(std::io::Error::other(
76 "overflow entry not found or expired",
77 )));
78 };
79
80 match self.sqlite.load_overflow(id, conv_id).await {
81 Ok(Some(bytes)) => {
82 let text = String::from_utf8_lossy(&bytes).into_owned();
83 Ok(Some(ToolOutput {
84 tool_name: zeph_common::ToolName::new(Self::TOOL_NAME),
85 summary: text,
86 blocks_executed: 1,
87 filter_stats: None,
88 diff: None,
89 streamed: false,
90 terminal_id: None,
91 locations: None,
92 raw_response: None,
93 claim_source: None,
94 }))
95 }
96 Ok(None) => Err(ToolError::Execution(std::io::Error::other(
97 "overflow entry not found or expired",
98 ))),
99 Err(e) => Err(ToolError::Execution(std::io::Error::other(format!(
100 "failed to load overflow: {e}"
101 )))),
102 }
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109 use zeph_memory::store::SqliteStore;
110
111 async fn make_store_with_conv() -> (Arc<SqliteStore>, i64) {
112 let store = SqliteStore::new(":memory:")
113 .await
114 .expect("SqliteStore::new");
115 let cid = store
116 .create_conversation()
117 .await
118 .expect("create_conversation");
119 (Arc::new(store), cid.0)
120 }
121
122 fn make_call(id: &str) -> ToolCall {
123 let mut params = serde_json::Map::new();
124 params.insert("id".into(), serde_json::Value::String(id.to_owned()));
125 ToolCall {
126 tool_id: zeph_common::ToolName::new("read_overflow"),
127 params,
128 caller_id: None,
129 context: None,
130 }
131 }
132
133 #[tokio::test]
134 async fn tool_definitions_returns_one_tool() {
135 let (store, _) = make_store_with_conv().await;
136 let exec = OverflowToolExecutor::new(store);
137 let defs = exec.tool_definitions();
138 assert_eq!(defs.len(), 1);
139 assert_eq!(defs[0].id.as_ref(), OverflowToolExecutor::TOOL_NAME);
140 }
141
142 #[tokio::test]
143 async fn execute_always_returns_none() {
144 let (store, _) = make_store_with_conv().await;
145 let exec = OverflowToolExecutor::new(store);
146 let result = exec.execute("anything").await.unwrap();
147 assert!(result.is_none());
148 }
149
150 #[tokio::test]
151 async fn unknown_tool_returns_none() {
152 let (store, _) = make_store_with_conv().await;
153 let exec = OverflowToolExecutor::new(store);
154 let call = ToolCall {
155 tool_id: zeph_common::ToolName::new("other_tool"),
156 params: serde_json::Map::new(),
157 caller_id: None,
158 context: None,
159 };
160 let result = exec.execute_tool_call(&call).await.unwrap();
161 assert!(result.is_none());
162 }
163
164 #[tokio::test]
165 async fn invalid_uuid_returns_error() {
166 let (store, cid) = make_store_with_conv().await;
167 let exec = OverflowToolExecutor::new(store).with_conversation(cid);
168 let call = make_call("not-a-uuid");
169 let err = exec.execute_tool_call(&call).await.unwrap_err();
170 assert!(matches!(err, ToolError::InvalidParams { .. }));
171 }
172
173 #[tokio::test]
174 async fn overflow_prefix_accepted_and_stripped() {
175 let (store, cid) = make_store_with_conv().await;
176 let content = b"prefixed overflow content";
177 let uuid = store
178 .save_overflow(cid, content)
179 .await
180 .expect("save_overflow");
181
182 let exec = OverflowToolExecutor::new(Arc::clone(&store)).with_conversation(cid);
183 let call = make_call(&format!("overflow:{uuid}"));
184 let result = exec.execute_tool_call(&call).await.unwrap().unwrap();
185 assert_eq!(result.summary.as_bytes(), content);
186 }
187
188 #[tokio::test]
189 async fn bare_uuid_still_accepted() {
190 let (store, cid) = make_store_with_conv().await;
191 let content = b"bare uuid content";
192 let uuid = store
193 .save_overflow(cid, content)
194 .await
195 .expect("save_overflow");
196
197 let exec = OverflowToolExecutor::new(Arc::clone(&store)).with_conversation(cid);
198 let call = make_call(&uuid);
199 let result = exec.execute_tool_call(&call).await.unwrap().unwrap();
200 assert_eq!(result.summary.as_bytes(), content);
201 }
202
203 #[tokio::test]
204 async fn invalid_uuid_with_overflow_prefix_returns_error() {
205 let (store, cid) = make_store_with_conv().await;
206 let exec = OverflowToolExecutor::new(store).with_conversation(cid);
207 let call = make_call("overflow:not-a-uuid");
208 let err = exec.execute_tool_call(&call).await.unwrap_err();
209 assert!(matches!(err, ToolError::InvalidParams { .. }));
210 }
211
212 #[tokio::test]
213 async fn missing_entry_returns_error() {
214 let (store, cid) = make_store_with_conv().await;
215 let exec = OverflowToolExecutor::new(store).with_conversation(cid);
216 let call = make_call("00000000-0000-0000-0000-000000000000");
217 let err = exec.execute_tool_call(&call).await.unwrap_err();
218 assert!(matches!(err, ToolError::Execution(_)));
219 }
220
221 #[tokio::test]
222 async fn no_conversation_returns_error() {
223 let (store, cid) = make_store_with_conv().await;
224 let uuid = store.save_overflow(cid, b"data").await.expect("save");
225 let exec = OverflowToolExecutor::new(store);
227 let call = make_call(&uuid);
228 let err = exec.execute_tool_call(&call).await.unwrap_err();
229 assert!(matches!(err, ToolError::Execution(_)));
230 }
231
232 #[tokio::test]
233 async fn valid_entry_returns_content() {
234 let (store, cid) = make_store_with_conv().await;
235 let content = b"full tool output content";
236 let uuid = store
237 .save_overflow(cid, content)
238 .await
239 .expect("save_overflow");
240
241 let exec = OverflowToolExecutor::new(Arc::clone(&store)).with_conversation(cid);
242 let call = make_call(&uuid);
243 let result = exec.execute_tool_call(&call).await.unwrap().unwrap();
244 assert_eq!(result.tool_name, OverflowToolExecutor::TOOL_NAME);
245 assert_eq!(result.summary.as_bytes(), content);
246 }
247
248 #[tokio::test]
249 async fn cross_conversation_access_denied() {
250 let (store, cid1) = make_store_with_conv().await;
251 let cid2 = store
252 .create_conversation()
253 .await
254 .expect("create_conversation")
255 .0;
256 let uuid = store.save_overflow(cid1, b"secret").await.expect("save");
257 let exec = OverflowToolExecutor::new(Arc::clone(&store)).with_conversation(cid2);
259 let call = make_call(&uuid);
260 let err = exec.execute_tool_call(&call).await.unwrap_err();
261 assert!(
262 matches!(err, ToolError::Execution(_)),
263 "must not access overflow from a different conversation"
264 );
265 }
266
267 #[tokio::test]
268 async fn read_overflow_output_is_not_reoverflowed() {
269 let (store, cid) = make_store_with_conv().await;
272 let big_content = "x".repeat(100_000).into_bytes();
273 let uuid = store
274 .save_overflow(cid, &big_content)
275 .await
276 .expect("save_overflow");
277
278 let exec = OverflowToolExecutor::new(Arc::clone(&store)).with_conversation(cid);
279 let call = make_call(&uuid);
280 let result = exec.execute_tool_call(&call).await.unwrap().unwrap();
281 assert_eq!(
282 result.summary.len(),
283 100_000,
284 "full content must be returned"
285 );
286 }
287}