diff options
Diffstat (limited to 'tests/loop.rs')
| -rw-r--r-- | tests/loop.rs | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/tests/loop.rs b/tests/loop.rs index d77d890..ac137da 100644 --- a/tests/loop.rs +++ b/tests/loop.rs | |||
| @@ -190,3 +190,138 @@ fn a_denied_tool_call_is_returned_as_data() { | |||
| 190 | ); | 190 | ); |
| 191 | assert_eq!(history[4]["content"], "understood"); | 191 | assert_eq!(history[4]["content"], "understood"); |
| 192 | } | 192 | } |
| 193 | |||
| 194 | fn tool_call(index: usize, id: &str, name: &str, arguments: serde_json::Value) -> serde_json::Value { | ||
| 195 | json!({ | ||
| 196 | "index": index, | ||
| 197 | "id": id, | ||
| 198 | "function": { "name": name, "arguments": arguments.to_string() } | ||
| 199 | }) | ||
| 200 | } | ||
| 201 | |||
| 202 | #[test] | ||
| 203 | fn aborting_between_tool_calls_answers_every_call() { | ||
| 204 | let dir = temp_dir("abort-mid-calls"); | ||
| 205 | let slow = sse(&format!( | ||
| 206 | "data: {}\n\ndata: [DONE]\n\n", | ||
| 207 | json!({ | ||
| 208 | "choices": [{ | ||
| 209 | "delta": { "tool_calls": [ | ||
| 210 | tool_call(0, "call_a", "bash", json!({ "cmd": "sleep 0.5" })), | ||
| 211 | tool_call(1, "call_b", "write", json!({ "path": dir.join("b.txt"), "content": "b" })), | ||
| 212 | tool_call(2, "call_c", "write", json!({ "path": dir.join("c.txt"), "content": "c" })) | ||
| 213 | ]}, | ||
| 214 | "finish_reason": "tool_calls" | ||
| 215 | }] | ||
| 216 | }) | ||
| 217 | )); | ||
| 218 | // If the harness wrongly continues after the abort, it would consume this. | ||
| 219 | let after = sse( | ||
| 220 | "data: {\"choices\":[{\"delta\":{\"content\":\"should not be reached\"},\"finish_reason\":\"stop\"}]}\n\n\ | ||
| 221 | data: [DONE]\n\n", | ||
| 222 | ); | ||
| 223 | |||
| 224 | let base = serve_sequence(vec![slow, after]); | ||
| 225 | std::env::set_var("AGENT686_DATA", dir.join("data")); | ||
| 226 | |||
| 227 | let system = agent::system_prompt(); | ||
| 228 | let mut session = Session::create("test-model", &system).unwrap(); | ||
| 229 | let cfg = Config { | ||
| 230 | base_url: base, | ||
| 231 | api_key: "test-key".into(), | ||
| 232 | model: "test-model".into(), | ||
| 233 | session: session.id.clone(), | ||
| 234 | yolo: true, | ||
| 235 | }; | ||
| 236 | let mut history = agent::new_history(system); | ||
| 237 | let mut approver = Approver::new(true); | ||
| 238 | let cancel = std::sync::Arc::new(AtomicBool::new(false)); | ||
| 239 | |||
| 240 | let flag = cancel.clone(); | ||
| 241 | thread::spawn(move || { | ||
| 242 | thread::sleep(std::time::Duration::from_millis(150)); | ||
| 243 | flag.store(true, std::sync::atomic::Ordering::SeqCst); | ||
| 244 | }); | ||
| 245 | |||
| 246 | let report = agent::run_turn( | ||
| 247 | &cfg, | ||
| 248 | &mut session, | ||
| 249 | &mut history, | ||
| 250 | "do three things", | ||
| 251 | &cancel, | ||
| 252 | &mut approver, | ||
| 253 | ) | ||
| 254 | .expect("an abort is not an error"); | ||
| 255 | |||
| 256 | assert!(report.aborted); | ||
| 257 | |||
| 258 | // Every announced tool call must have exactly one result, or the provider | ||
| 259 | // rejects the session forever and the transcript cannot be rewritten. | ||
| 260 | let answered: Vec<String> = history | ||
| 261 | .iter() | ||
| 262 | .filter(|message| message.get("role").and_then(|r| r.as_str()) == Some("tool")) | ||
| 263 | .filter_map(|message| message.get("tool_call_id").and_then(|id| id.as_str()).map(|s| s.to_string())) | ||
| 264 | .collect(); | ||
| 265 | assert_eq!(answered, vec!["call_a", "call_b", "call_c"], "{answered:?}"); | ||
| 266 | |||
| 267 | // The loop stopped: no assistant text was ever appended. | ||
| 268 | assert_eq!(history.last().unwrap()["role"], "tool"); | ||
| 269 | assert!(fs::read_to_string(session.path()).unwrap().lines().count() >= 5); | ||
| 270 | } | ||
| 271 | |||
| 272 | #[test] | ||
| 273 | fn opening_a_session_with_unanswered_calls_repairs_it() { | ||
| 274 | let dir = temp_dir("repair"); | ||
| 275 | std::env::set_var("AGENT686_DATA", dir.join("data")); | ||
| 276 | |||
| 277 | let base = serve_sequence(vec![sse( | ||
| 278 | "data: {\"choices\":[{\"delta\":{\"content\":\"carrying on\"},\"finish_reason\":\"stop\"}]}\n\n\ | ||
| 279 | data: [DONE]\n\n", | ||
| 280 | )]); | ||
| 281 | |||
| 282 | let system = agent::system_prompt(); | ||
| 283 | let mut session = Session::create("test-model", &system).unwrap(); | ||
| 284 | let mut history = agent::new_history(system); | ||
| 285 | |||
| 286 | // A transcript shape left behind by an older build: two calls, one result. | ||
| 287 | let assistant = json!({ | ||
| 288 | "role": "assistant", | ||
| 289 | "content": null, | ||
| 290 | "tool_calls": [ | ||
| 291 | { "id": "call_1", "type": "function", "function": { "name": "read", "arguments": "{}" } }, | ||
| 292 | { "id": "call_2", "type": "function", "function": { "name": "read", "arguments": "{}" } } | ||
| 293 | ] | ||
| 294 | }); | ||
| 295 | session.append(&assistant, None).unwrap(); | ||
| 296 | history.push(assistant); | ||
| 297 | let partial = json!({ "role": "tool", "tool_call_id": "call_1", "content": "ok" }); | ||
| 298 | session.append(&partial, None).unwrap(); | ||
| 299 | history.push(partial); | ||
| 300 | |||
| 301 | let cfg = Config { | ||
| 302 | base_url: base, | ||
| 303 | api_key: "test-key".into(), | ||
| 304 | model: "test-model".into(), | ||
| 305 | session: session.id.clone(), | ||
| 306 | yolo: true, | ||
| 307 | }; | ||
| 308 | let mut approver = Approver::new(true); | ||
| 309 | agent::run_turn( | ||
| 310 | &cfg, | ||
| 311 | &mut session, | ||
| 312 | &mut history, | ||
| 313 | "go on", | ||
| 314 | &AtomicBool::new(false), | ||
| 315 | &mut approver, | ||
| 316 | ) | ||
| 317 | .expect("repair should let the turn run"); | ||
| 318 | |||
| 319 | let repaired = history.iter().any(|message| { | ||
| 320 | message.get("tool_call_id").and_then(|id| id.as_str()) == Some("call_2") | ||
| 321 | && message | ||
| 322 | .get("content") | ||
| 323 | .and_then(|c| c.as_str()) | ||
| 324 | .is_some_and(|c| c.contains("interrupted")) | ||
| 325 | }); | ||
| 326 | assert!(repaired, "missing result for call_2 should be synthesised"); | ||
| 327 | } | ||
