From 8ae5bd48cf233315b5f36a7a6b39002a5e77f74b Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 18:00:12 +0100 Subject: 0.7.0: resume shows where you left off; no approval for unparsable calls - --continue prints the last answer, or the last reasoning block if there was none - tool arguments are validated before prompting, so an empty bash call becomes an error for the model instead of a blank approval - empty placeholder tool calls are dropped and reported - sessions tolerate invalid UTF-8 lines from a torn write - AGENT686_DEBUG_STREAM=1 dumps SSE frames --- tests/loop.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/mock.rs | 25 +++++++++++++++++++++++ tests/sessions.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/tools.rs | 15 ++++++++++++++ 4 files changed, 152 insertions(+) (limited to 'tests') diff --git a/tests/loop.rs b/tests/loop.rs index b7e56f9..f90d19d 100644 --- a/tests/loop.rs +++ b/tests/loop.rs @@ -334,3 +334,56 @@ fn opening_a_session_with_unanswered_calls_repairs_it() { }); assert!(repaired, "missing result for call_2 should be synthesised"); } + +#[test] +fn an_empty_bash_call_is_never_approved() { + let dir = temp_dir("empty-bash"); + let empty_call = sse(&format!( + "data: {}\n\ndata: [DONE]\n\n", + json!({ + "choices": [{ + "delta": { "tool_calls": [{ + "index": 0, + "id": "call_empty", + "function": { "name": "bash", "arguments": "{}" } + }]}, + "finish_reason": "tool_calls" + }] + }) + )); + let final_round = sse( + "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"},\"finish_reason\":\"stop\"}]}\n\n\ + data: [DONE]\n\n", + ); + + let base = serve_sequence(vec![empty_call, final_round]); + 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: false, + }; + let mut history = agent::new_history(system); + // Not a terminal: if this reaches the approval path it is denied, which the + // assertion below would catch. + let mut approver = Approver::new(false); + + agent::run_turn( + &cfg, + &mut session, + &mut history, + "run something", + &AtomicBool::new(false), + &mut approver, + ) + .expect("the turn should survive a malformed call"); + + let result = history[3]["content"].as_str().unwrap(); + assert!(result.contains("`cmd` is required"), "{result}"); + assert!(!result.contains("denied"), "must not reach an approval prompt: {result}"); +} diff --git a/tests/mock.rs b/tests/mock.rs index 9b9a56d..7e076e3 100644 --- a/tests/mock.rs +++ b/tests/mock.rs @@ -442,3 +442,28 @@ fn a_final_frame_without_a_newline_is_not_lost() { .expect("a trailing frame without a newline should still count"); assert_eq!(outcome.content, "last"); } + +#[test] +fn an_empty_placeholder_tool_call_is_dropped() { + const SSE: &str = concat!( + "HTTP/1.1 200 OK\r\n", + "Content-Type: text/event-stream\r\n", + "Connection: close\r\n", + "\r\n", + "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"read\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}},{\"index\":1}]},\"finish_reason\":\"tool_calls\"}]}\n\n", + "data: [DONE]\n\n", + ); + let (base, _rx) = serve_once(SSE); + let mut sink = Collector::default(); + let outcome = provider::chat( + &config(base), + &[json!({ "role": "user", "content": "hi" })], + &[], + &AtomicBool::new(false), + &mut sink, + ) + .expect("chat should succeed"); + + assert_eq!(outcome.tool_calls.len(), 1, "{:?}", outcome.tool_calls); + assert_eq!(outcome.tool_calls[0].name, "read"); +} diff --git a/tests/sessions.rs b/tests/sessions.rs index 1d40f64..df2f3b2 100644 --- a/tests/sessions.rs +++ b/tests/sessions.rs @@ -110,3 +110,62 @@ fn listing_reports_the_model_in_use_after_a_switch() { assert_eq!(listed.len(), 1); assert_eq!(listed[0].model, "model-b"); } + +#[test] +fn a_torn_utf8_line_does_not_brick_a_session() { + let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let dir = temp_dir("torn"); + std::env::set_var("AGENT686_DATA", &dir); + let sessions = dir.join("sessions"); + fs::create_dir_all(&sessions).unwrap(); + + let mut raw = Vec::new(); + raw.extend_from_slice( + br#"{"t":"header","session":"torn","model":"m","cwd":"/tmp","system":{"role":"system","content":"s"}} +"#, + ); + raw.extend_from_slice(br#"{"t":"msg","role":"user","content":"hello"} +"#); + // A killed write that split a multi-byte character. + raw.extend_from_slice(b"{\"t\":\"msg\",\"role\":\"user\",\"content\":\"caf\xc3\"}\n"); + fs::write(sessions.join("torn.jsonl"), raw).unwrap(); + + let (_session, _system, messages) = Session::open("torn").expect("torn line must not fail"); + // Lossy decoding keeps the message (with a replacement character) rather + // than dropping a valid JSON line; what matters is that open succeeds. + assert_eq!(messages.len(), 2); + assert_eq!(messages[0]["content"], "hello"); + assert!(messages[1]["content"].as_str().unwrap().starts_with("caf")); +} + +#[test] +fn last_reply_prefers_content_and_falls_back_to_reasoning() { + let _env = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let dir = temp_dir("last-reply"); + std::env::set_var("AGENT686_DATA", &dir); + + let system = json!({ "role": "system", "content": "x" }); + let mut session = Session::create_as("reply".into(), "m", &system).unwrap(); + session + .append( + &json!({ "role": "assistant", "content": "the answer" }), + Some("the thinking"), + ) + .unwrap(); + session + .append(&json!({ "role": "assistant", "content": null }), Some("only thinking")) + .unwrap(); + + let reply = session::last_reply("reply").expect("there is a reply"); + assert_eq!(reply.content, "the answer", "prefers a real block"); + assert_eq!(reply.reasoning, "the thinking", "and its own reasoning"); + + // With no content anywhere, reasoning is the fallback. + let mut session = Session::create_as("reasoning-only".into(), "m", &system).unwrap(); + session + .append(&json!({ "role": "assistant", "content": null }), Some("thoughts")) + .unwrap(); + let reply = session::last_reply("reasoning-only").unwrap(); + assert!(reply.content.is_empty()); + assert_eq!(reply.reasoning, "thoughts"); +} diff --git a/tests/tools.rs b/tests/tools.rs index 77c1698..a400df0 100644 --- a/tests/tools.rs +++ b/tests/tools.rs @@ -419,3 +419,18 @@ fn glob_follows_file_symlinks_but_not_directories() { assert!(text.contains("link.rs"), "{text}"); assert!(text.contains("inner.rs"), "{text}"); } + +#[test] +fn validate_rejects_calls_that_cannot_run() { + use agent686::tools::validate; + + assert!(validate("bash", &json!({ "cmd": "" })).is_err()); + assert!(validate("bash", &json!({})).is_err()); + assert!(validate("bash", &json!({ "cmd": "ls" })).is_ok()); + + assert!(validate("read", &json!({ "path": " " })).is_err()); + assert!(validate("edit", &json!({ "path": "a", "old": "" })).is_err()); + assert!(validate("grep", &json!({ "pattern": "" })).is_err()); + // Empty content is how you create an empty file. + assert!(validate("write", &json!({ "path": "a", "content": "" })).is_ok()); +} -- cgit v1.2.3