From 714534475818e6e5dced91ecad100ca9e286ccce Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:37:56 +0100 Subject: 0.4.1 + 0.4.2: abort integrity, stream completeness, tool-call indexing - an aborted multi-call step now answers every announced tool call - sessions with unanswered calls are repaired by appending results - a stream without [DONE]/finish_reason is an error, not an answer - tool call deltas keyed by index can no longer vanish or invent empty calls - one-shot exit codes: 130 aborted, 3 denied --- tests/loop.rs | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) (limited to 'tests/loop.rs') 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() { ); assert_eq!(history[4]["content"], "understood"); } + +fn tool_call(index: usize, id: &str, name: &str, arguments: serde_json::Value) -> serde_json::Value { + json!({ + "index": index, + "id": id, + "function": { "name": name, "arguments": arguments.to_string() } + }) +} + +#[test] +fn aborting_between_tool_calls_answers_every_call() { + let dir = temp_dir("abort-mid-calls"); + let slow = sse(&format!( + "data: {}\n\ndata: [DONE]\n\n", + json!({ + "choices": [{ + "delta": { "tool_calls": [ + tool_call(0, "call_a", "bash", json!({ "cmd": "sleep 0.5" })), + tool_call(1, "call_b", "write", json!({ "path": dir.join("b.txt"), "content": "b" })), + tool_call(2, "call_c", "write", json!({ "path": dir.join("c.txt"), "content": "c" })) + ]}, + "finish_reason": "tool_calls" + }] + }) + )); + // If the harness wrongly continues after the abort, it would consume this. + let after = sse( + "data: {\"choices\":[{\"delta\":{\"content\":\"should not be reached\"},\"finish_reason\":\"stop\"}]}\n\n\ + data: [DONE]\n\n", + ); + + let base = serve_sequence(vec![slow, after]); + std::env::set_var("AGENT686_DATA", dir.join("data")); + + let system = agent::system_prompt(); + let mut session = Session::create("test-model", &system).unwrap(); + let cfg = Config { + base_url: base, + api_key: "test-key".into(), + model: "test-model".into(), + session: session.id.clone(), + yolo: true, + }; + let mut history = agent::new_history(system); + let mut approver = Approver::new(true); + let cancel = std::sync::Arc::new(AtomicBool::new(false)); + + let flag = cancel.clone(); + thread::spawn(move || { + thread::sleep(std::time::Duration::from_millis(150)); + flag.store(true, std::sync::atomic::Ordering::SeqCst); + }); + + let report = agent::run_turn( + &cfg, + &mut session, + &mut history, + "do three things", + &cancel, + &mut approver, + ) + .expect("an abort is not an error"); + + assert!(report.aborted); + + // Every announced tool call must have exactly one result, or the provider + // rejects the session forever and the transcript cannot be rewritten. + let answered: Vec = history + .iter() + .filter(|message| message.get("role").and_then(|r| r.as_str()) == Some("tool")) + .filter_map(|message| message.get("tool_call_id").and_then(|id| id.as_str()).map(|s| s.to_string())) + .collect(); + assert_eq!(answered, vec!["call_a", "call_b", "call_c"], "{answered:?}"); + + // The loop stopped: no assistant text was ever appended. + assert_eq!(history.last().unwrap()["role"], "tool"); + assert!(fs::read_to_string(session.path()).unwrap().lines().count() >= 5); +} + +#[test] +fn opening_a_session_with_unanswered_calls_repairs_it() { + let dir = temp_dir("repair"); + std::env::set_var("AGENT686_DATA", dir.join("data")); + + let base = serve_sequence(vec![sse( + "data: {\"choices\":[{\"delta\":{\"content\":\"carrying on\"},\"finish_reason\":\"stop\"}]}\n\n\ + data: [DONE]\n\n", + )]); + + let system = agent::system_prompt(); + let mut session = Session::create("test-model", &system).unwrap(); + let mut history = agent::new_history(system); + + // A transcript shape left behind by an older build: two calls, one result. + let assistant = json!({ + "role": "assistant", + "content": null, + "tool_calls": [ + { "id": "call_1", "type": "function", "function": { "name": "read", "arguments": "{}" } }, + { "id": "call_2", "type": "function", "function": { "name": "read", "arguments": "{}" } } + ] + }); + session.append(&assistant, None).unwrap(); + history.push(assistant); + let partial = json!({ "role": "tool", "tool_call_id": "call_1", "content": "ok" }); + session.append(&partial, None).unwrap(); + history.push(partial); + + let cfg = Config { + base_url: base, + api_key: "test-key".into(), + model: "test-model".into(), + session: session.id.clone(), + yolo: true, + }; + let mut approver = Approver::new(true); + agent::run_turn( + &cfg, + &mut session, + &mut history, + "go on", + &AtomicBool::new(false), + &mut approver, + ) + .expect("repair should let the turn run"); + + let repaired = history.iter().any(|message| { + message.get("tool_call_id").and_then(|id| id.as_str()) == Some("call_2") + && message + .get("content") + .and_then(|c| c.as_str()) + .is_some_and(|c| c.contains("interrupted")) + }); + assert!(repaired, "missing result for call_2 should be synthesised"); +} -- cgit v1.2.3