From 6df7a6403d88a4492ab6ea1c519dcae3a202be96 Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 18 Nov 2026 12:01:38 +0100 Subject: 0.9.0: distinguishable tool errors, separated stderr, configurable limits From a field report porting a Neovim config to Helix: - validate() now separates 'empty value', 'wrong key (here is what you sent)' and 'no arguments at all, retry' instead of one false message for all four causes - named calls with no arguments are answered rather than dropped silently - bash keeps stdout and stderr in labelled sections when both produce output - bash gains timeout_seconds (1-600) and max_bytes, and says when output was cut - tool lines/results carry [step.call] labels when a step has several calls - invalid JSON errors include a preview of what arrived --- src/tools/mod.rs | 74 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 20 deletions(-) (limited to 'src/tools/mod.rs') diff --git a/src/tools/mod.rs b/src/tools/mod.rs index ddb1004..27840e9 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs @@ -24,28 +24,52 @@ pub fn approval(name: &str) -> Approval { } } -/// A call with no arguments at all. Every tool here requires at least one, so -/// this is a placeholder some backends emit next to a real call, not an attempt. +/// True when the call cannot be replayed as a real tool call at all: no name. +/// (Named calls with no arguments are kept so the model can be told what went +/// wrong and retry.) pub fn is_placeholder(arguments: &str) -> bool { - let trimmed = arguments.trim(); - if trimmed.is_empty() { - return true; + arguments.trim().is_empty() +} + +/// Returns the keys the model actually sent, quoted and bounded. +fn received_keys(args: &Value) -> String { + let Some(object) = args.as_object() else { + return "no arguments".to_string(); + }; + if object.is_empty() { + return "no arguments".to_string(); } - matches!( - serde_json::from_str::(trimmed), - Ok(Value::Object(map)) if map.is_empty() - ) + let mut keys: Vec = object.keys().map(|key| format!("`{key}`")).collect(); + keys.sort(); + if keys.len() > 6 { + keys.truncate(6); + keys.push("…".to_string()); + } + keys.join(", ") } -/// 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: `. +/// Rejects a call that cannot run, before a human is asked to approve it. +/// +/// The message has to distinguish three different failures: the argument is +/// empty, the argument is missing because the key is wrong, and the call +/// arrived with no arguments at all (usually argument loss in transit). They +/// used to share one message, which pointed callers at the wrong fix. 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(()) + let required = |key: &str| match args.get(key).and_then(|value| value.as_str()) { + Some(value) if !value.trim().is_empty() => Ok(()), + Some(_) => Err(format!("`{key}` is present but empty")), + None => { + if args.as_object().map(|o| o.is_empty()).unwrap_or(true) { + Err(format!( + "the call arrived with no arguments at all, so `{key}` is missing. \ + If you sent one, it was lost in transit — retry the call" + )) + } else { + Err(format!( + "missing `{key}`; received {}. The parameter is `{key}`", + received_keys(args) + )) + } } }; @@ -57,6 +81,7 @@ pub fn validate(name: &str, args: &Value) -> Result<(), String> { required("old") } "grep" | "glob" => required("pattern"), + "" => Err("the tool call arrived without a tool name; it was probably truncated in transit — retry the call".into()), _ => Ok(()), } } @@ -67,7 +92,14 @@ pub fn run(name: &str, args: &Value, cancel: &std::sync::atomic::AtomicBool) -> "read" => fs::read(args), "grep" => fs::grep(args), "glob" => fs::glob(args), - "bash" => shell::bash(args, std::time::Duration::from_secs(120), cancel), + "bash" => { + let seconds = args + .get("timeout_seconds") + .and_then(|value| value.as_u64()) + .unwrap_or(120) + .clamp(1, 600); + shell::bash(args, std::time::Duration::from_secs(seconds), cancel) + } other => Err(unknown(other)), } } @@ -139,12 +171,14 @@ pub fn schemas() -> Vec { "type": "function", "function": { "name": "bash", - "description": "Run a shell command. Each call is a fresh shell at the working directory, so `cd` does not persist; pass cwd instead. Times out after 120s. Returns combined output and the exit code.", + "description": "Run a shell command. Each call is a fresh shell at the working directory, so `cd` does not persist; pass cwd instead. stdout and stderr are reported in separate labelled sections when both produce output. Times out after timeout_seconds (default 120, maximum 600). Large output is truncated head+tail with an explicit marker; raise max_bytes if the match you need is in the middle.", "parameters": { "type": "object", "properties": { "cmd": { "type": "string" }, - "cwd": { "type": "string", "description": "working directory (default: project root)" } + "cwd": { "type": "string", "description": "working directory (default: project root)" }, + "timeout_seconds": { "type": "integer", "description": "1-600, default 120" }, + "max_bytes": { "type": "integer", "description": "per-stream output budget, 1024-16777216, default 81920" } }, "required": ["cmd"] } -- cgit v1.2.3