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 --- src/agent.rs | 13 ++++++++++- src/main.rs | 43 ++++++++++++++++++++++++++++++++++++ src/provider.rs | 15 ++++++++++++- src/session.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++------ src/tools/mod.rs | 24 +++++++++++++++++++++ 5 files changed, 152 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/agent.rs b/src/agent.rs index 4bf19ed..f0e1292 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -342,8 +342,19 @@ fn execute( cancel: &AtomicBool, step: usize, ) -> ToolOutcome { - let args: Value = serde_json::from_str(&call.arguments).unwrap_or_else(|_| json!({})); + let args: Value = match serde_json::from_str(&call.arguments) { + Ok(args) => args, + Err(error) => { + return ToolOutcome::Failed(format!( + "arguments were not valid JSON ({error}); issue the call again with the \ + complete arguments" + )) + } + }; ui::tool_line(step, &call.name, &args); + if let Err(error) = tools::validate(&call.name, &args) { + return ToolOutcome::Failed(format!("{error}; the call was not run")); + } match tools::approval(&call.name) { Approval::Never => match tools::run(&call.name, &args, cancel) { diff --git a/src/main.rs b/src/main.rs index 91d54da..45ca8c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,6 +195,7 @@ fn run(args: Args) { } }; + let resuming = args.session.is_some() || args.continue_last; let (mut session, mut history) = match open_session(&cfg, args.session, args.continue_last) { Ok(pair) => pair, Err(message) => { @@ -205,6 +206,9 @@ fn run(args: Args) { cfg.session = session.id.clone(); warn_if_moved(&session); + if resuming { + print_last_reply(&session.id); + } if !session.version.is_empty() && session.version != env!("CARGO_PKG_VERSION") { eprintln!( "agent686: this session was written by {}; the prompt and tool schemas may \ @@ -647,3 +651,42 @@ fn render_message(message: &Value, color: bool) -> String { other => format!("[{other}]\n"), } } + +/// On resume, show where the conversation left off: the last answer, or the last +/// reasoning when the model never produced content. Only on a terminal, so piped +/// runs stay clean. +fn print_last_reply(id: &str) { + if !std::io::stdout().is_terminal() { + return; + } + let Some(reply) = session::last_reply(id) else { + return; + }; + let text = if reply.content.trim().is_empty() { + reply.reasoning.trim() + } else { + reply.content.trim() + }; + if text.is_empty() { + return; + } + + let lines: Vec<&str> = text.lines().collect(); + let limit = 20; + println!(); + println!("{}", style("2", "── where you left off ──", true)); + for line in lines.iter().take(limit) { + println!("{line}"); + } + if lines.len() > limit { + println!( + "{}", + style( + "2", + &format!("… {} more line(s); `agent686 --show` for the transcript", lines.len() - limit), + true + ) + ); + } + println!(); +} diff --git a/src/provider.rs b/src/provider.rs index 1b9e220..0973e8c 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -358,7 +358,17 @@ pub fn chat_with( } } - outcome.tool_calls = calls.into_values().collect(); + let before = calls.len(); + outcome.tool_calls = calls + .into_values() + // Some backends emit an empty placeholder alongside a real call; asking + // a human to approve a blank command is worse than dropping it. + .filter(|call| !(call.name.is_empty() && call.arguments.trim().is_empty())) + .collect(); + let dropped_calls = before - outcome.tool_calls.len(); + if dropped_calls > 0 { + eprintln!("agent686: ignored {dropped_calls} empty tool call(s)"); + } if outcome.usage.as_ref().is_some_and(|u| u.is_null()) { outcome.usage = None; } @@ -393,6 +403,9 @@ fn handle_event( if data.is_empty() { return false; } + if std::env::var_os("AGENT686_DEBUG_STREAM").is_some() { + eprintln!("sse: {}", truncate(data, 400)); + } if data.trim() == "[DONE]" { *completed = true; return true; diff --git a/src/session.rs b/src/session.rs index d017eda..8e0ef02 100644 --- a/src/session.rs +++ b/src/session.rs @@ -130,8 +130,8 @@ impl Session { /// that was frozen when the session started. pub fn open(id: &str) -> Result<(Session, Value, Vec), String> { let path = sessions_dir().join(format!("{id}.jsonl")); - let file = File::open(&path).map_err(|e| format!("{}: {e}", path.display()))?; - let reader = BufReader::new(file.try_clone().map_err(|e| e.to_string())?); + let raw = std::fs::read(&path).map_err(|e| format!("{}: {e}", path.display()))?; + let _ = File::open(&path); let mut model = String::new(); let mut system = Value::Null; @@ -140,14 +140,15 @@ impl Session { let mut version = String::new(); let mut messages = Vec::new(); - for line in reader.lines() { - let line = line.map_err(|e| e.to_string())?; - if line.trim().is_empty() { + for line in raw.split(|&byte| byte == b'\n') { + if line.is_empty() { continue; } - let value: Value = match serde_json::from_str(&line) { + // Lossy decoding: a torn write can split a multi-byte character, and + // one bad byte must not make the whole transcript unopenable. + let text = String::from_utf8_lossy(line); + let value: Value = match serde_json::from_str(&text) { Ok(v) => v, - // A torn last line is expected after kill -9; ignore it. Err(_) => continue, }; match value.get("t").and_then(|t| t.as_str()) { @@ -232,6 +233,57 @@ impl Session { } } +pub struct LastReply { + pub content: String, + pub reasoning: String, +} + +/// The last thing the agent said, for the resume banner. Prefers a real answer; +/// falls back to reasoning when the last block had no content. +pub fn last_reply(id: &str) -> Option { + let raw = std::fs::read(sessions_dir().join(format!("{id}.jsonl"))).ok()?; + let mut with_content: Option = None; + let mut with_reasoning: Option = None; + + for line in raw.split(|&byte| byte == b'\n') { + if line.is_empty() { + continue; + } + let text = String::from_utf8_lossy(line); + let Ok(value) = serde_json::from_str::(&text) else { + continue; + }; + if value.get("role").and_then(|role| role.as_str()) != Some("assistant") { + continue; + } + let content = value + .get("content") + .and_then(|c| c.as_str()) + .unwrap_or("") + .to_string(); + let reasoning = value + .get("_reasoning") + .and_then(|r| r.as_str()) + .unwrap_or("") + .to_string(); + let reply = LastReply { + content, + reasoning, + }; + if !reply.content.trim().is_empty() { + with_content = Some(LastReply { + content: reply.content.clone(), + reasoning: reply.reasoning.clone(), + }); + } + if !reply.reasoning.trim().is_empty() { + with_reasoning = Some(reply); + } + } + + with_content.or(with_reasoning) +} + pub fn exists(id: &str) -> bool { sessions_dir().join(format!("{id}.jsonl")).is_file() } diff --git a/src/tools/mod.rs b/src/tools/mod.rs index a8851ab..060dd7b 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -24,6 +24,30 @@ pub fn approval(name: &str) -> Approval { } } +/// Rejects a call that cannot possibly run, before a human is asked to approve +/// it. An empty `cmd` used to mean an approval prompt for `run: `. +pub fn validate(name: &str, args: &Value) -> Result<(), String> { + let text = |key: &str| args.get(key).and_then(|value| value.as_str()).unwrap_or(""); + let required = |key: &str| { + if text(key).trim().is_empty() { + Err(format!("`{key}` is required and must not be empty")) + } else { + Ok(()) + } + }; + + match name { + "bash" => required("cmd"), + "read" | "write" => required("path"), + "edit" => { + required("path")?; + required("old") + } + "grep" | "glob" => required("pattern"), + _ => Ok(()), + } +} + /// Tools that execute immediately. pub fn run(name: &str, args: &Value, cancel: &std::sync::atomic::AtomicBool) -> Result { match name { -- cgit v1.2.3