diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-18 12:28:29 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-18 12:28:29 +0100 |
| commit | 84e70c0e73460e5db1297c47d02d9b76802bfdf7 (patch) | |
| tree | ff0cd75141a28d7112b4f82b2c74bdba91b332a3 /src/tools | |
| parent | 0.9.0: distinguishable tool errors, separated stderr, configurable limits (diff) | |
| download | agent686-84e70c0e73460e5db1297c47d02d9b76802bfdf7.tar.gz agent686-84e70c0e73460e5db1297c47d02d9b76802bfdf7.zip | |
0.9.1: correctness fixes from the audit
- 64 MiB limit detected by file length, so pipelines and redirects are reported
- bash temp files removed on every path via a scope guard
- validate requires content/new as the schemas declare (present, may be empty)
- Session::open folds in model-switch records; resume matches --list
- combining mark at a wrap boundary no longer counts as a new row
- --flag=value accepted; flag-shaped values rejected
- dead code removed; last_reply folded into open (one read per resume)
Diffstat (limited to '')
| -rw-r--r-- | src/tools/mod.rs | 58 | ||||
| -rw-r--r-- | src/tools/shell.rs | 28 |
2 files changed, 56 insertions, 30 deletions
diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 27840e9..7c6a921 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs | |||
| @@ -24,13 +24,6 @@ pub fn approval(name: &str) -> Approval { | |||
| 24 | } | 24 | } |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | /// True when the call cannot be replayed as a real tool call at all: no name. | ||
| 28 | /// (Named calls with no arguments are kept so the model can be told what went | ||
| 29 | /// wrong and retry.) | ||
| 30 | pub fn is_placeholder(arguments: &str) -> bool { | ||
| 31 | arguments.trim().is_empty() | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Returns the keys the model actually sent, quoted and bounded. | 27 | /// Returns the keys the model actually sent, quoted and bounded. |
| 35 | fn received_keys(args: &Value) -> String { | 28 | fn received_keys(args: &Value) -> String { |
| 36 | let Some(object) = args.as_object() else { | 29 | let Some(object) = args.as_object() else { |
| @@ -55,32 +48,45 @@ fn received_keys(args: &Value) -> String { | |||
| 55 | /// arrived with no arguments at all (usually argument loss in transit). They | 48 | /// arrived with no arguments at all (usually argument loss in transit). They |
| 56 | /// used to share one message, which pointed callers at the wrong fix. | 49 | /// used to share one message, which pointed callers at the wrong fix. |
| 57 | pub fn validate(name: &str, args: &Value) -> Result<(), String> { | 50 | pub fn validate(name: &str, args: &Value) -> Result<(), String> { |
| 58 | let required = |key: &str| match args.get(key).and_then(|value| value.as_str()) { | 51 | let missing = |key: &str| { |
| 52 | if args.as_object().map(|o| o.is_empty()).unwrap_or(true) { | ||
| 53 | format!( | ||
| 54 | "the call arrived with no arguments at all, so `{key}` is missing. \ | ||
| 55 | If you sent one, it was lost in transit — retry the call" | ||
| 56 | ) | ||
| 57 | } else { | ||
| 58 | format!( | ||
| 59 | "missing `{key}`; received {}. The parameter is `{key}`", | ||
| 60 | received_keys(args) | ||
| 61 | ) | ||
| 62 | } | ||
| 63 | }; | ||
| 64 | // Must be present and non-blank. | ||
| 65 | let required_text = |key: &str| match args.get(key).and_then(|value| value.as_str()) { | ||
| 59 | Some(value) if !value.trim().is_empty() => Ok(()), | 66 | Some(value) if !value.trim().is_empty() => Ok(()), |
| 60 | Some(_) => Err(format!("`{key}` is present but empty")), | 67 | Some(_) => Err(format!("`{key}` is present but empty")), |
| 61 | None => { | 68 | None => Err(missing(key)), |
| 62 | if args.as_object().map(|o| o.is_empty()).unwrap_or(true) { | 69 | }; |
| 63 | Err(format!( | 70 | // Must be present, but may be the empty string (an empty file, a deletion). |
| 64 | "the call arrived with no arguments at all, so `{key}` is missing. \ | 71 | let required_string = |key: &str| match args.get(key) { |
| 65 | If you sent one, it was lost in transit — retry the call" | 72 | Some(Value::String(_)) => Ok(()), |
| 66 | )) | 73 | Some(_) => Err(format!("`{key}` must be a string")), |
| 67 | } else { | 74 | None => Err(missing(key)), |
| 68 | Err(format!( | ||
| 69 | "missing `{key}`; received {}. The parameter is `{key}`", | ||
| 70 | received_keys(args) | ||
| 71 | )) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | }; | 75 | }; |
| 75 | 76 | ||
| 76 | match name { | 77 | match name { |
| 77 | "bash" => required("cmd"), | 78 | "bash" => required_text("cmd"), |
| 78 | "read" | "write" => required("path"), | 79 | "read" => required_text("path"), |
| 80 | "write" => { | ||
| 81 | required_text("path")?; | ||
| 82 | required_string("content") | ||
| 83 | } | ||
| 79 | "edit" => { | 84 | "edit" => { |
| 80 | required("path")?; | 85 | required_text("path")?; |
| 81 | required("old") | 86 | required_text("old")?; |
| 87 | required_string("new") | ||
| 82 | } | 88 | } |
| 83 | "grep" | "glob" => required("pattern"), | 89 | "grep" | "glob" => required_text("pattern"), |
| 84 | "" => Err("the tool call arrived without a tool name; it was probably truncated in transit — retry the call".into()), | 90 | "" => Err("the tool call arrived without a tool name; it was probably truncated in transit — retry the call".into()), |
| 85 | _ => Ok(()), | 91 | _ => Ok(()), |
| 86 | } | 92 | } |
diff --git a/src/tools/shell.rs b/src/tools/shell.rs index 91f2dd9..c38f032 100644 --- a/src/tools/shell.rs +++ b/src/tools/shell.rs | |||
| @@ -18,6 +18,20 @@ const MAX_FILE_BYTES: u64 = 64 * 1024 * 1024; | |||
| 18 | 18 | ||
| 19 | static COUNTER: AtomicU64 = AtomicU64::new(0); | 19 | static COUNTER: AtomicU64 = AtomicU64::new(0); |
| 20 | 20 | ||
| 21 | /// Removes both temp files when dropped, so an early `?` on the spawn/read paths | ||
| 22 | /// cannot leak `agent686-…-{out,err}.log` files into $TMPDIR. | ||
| 23 | struct TempGuard { | ||
| 24 | out: PathBuf, | ||
| 25 | err: PathBuf, | ||
| 26 | } | ||
| 27 | |||
| 28 | impl Drop for TempGuard { | ||
| 29 | fn drop(&mut self) { | ||
| 30 | let _ = std::fs::remove_file(&self.out); | ||
| 31 | let _ = std::fs::remove_file(&self.err); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 21 | pub fn bash( | 35 | pub fn bash( |
| 22 | args: &Value, | 36 | args: &Value, |
| 23 | timeout: Duration, | 37 | timeout: Duration, |
| @@ -44,6 +58,10 @@ pub fn bash( | |||
| 44 | let serial = COUNTER.fetch_add(1, Ordering::Relaxed); | 58 | let serial = COUNTER.fetch_add(1, Ordering::Relaxed); |
| 45 | let out_path = temp_path("out", serial); | 59 | let out_path = temp_path("out", serial); |
| 46 | let err_path = temp_path("err", serial); | 60 | let err_path = temp_path("err", serial); |
| 61 | let _guard = TempGuard { | ||
| 62 | out: out_path.clone(), | ||
| 63 | err: err_path.clone(), | ||
| 64 | }; | ||
| 47 | let mut out_file = create(&out_path)?; | 65 | let mut out_file = create(&out_path)?; |
| 48 | let mut err_file = create(&err_path)?; | 66 | let mut err_file = create(&err_path)?; |
| 49 | let stdout = out_file.try_clone().map_err(|e| e.to_string())?; | 67 | let stdout = out_file.try_clone().map_err(|e| e.to_string())?; |
| @@ -104,15 +122,17 @@ pub fn bash( | |||
| 104 | } | 122 | } |
| 105 | }; | 123 | }; |
| 106 | 124 | ||
| 107 | // A child that blew the file-size limit was killed by SIGXFSZ. | 125 | // SIGXFSZ lands on whichever process crossed the limit — usually a |
| 108 | let over_limit = status.signal() == Some(libc::SIGXFSZ); | 126 | // grandchild of `sh` — so `sh` exits 128+25 rather than being signalled. |
| 127 | // The reliable signal is the file itself having reached the limit. | ||
| 128 | let over_limit = status.signal() == Some(libc::SIGXFSZ) | ||
| 129 | || out_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES | ||
| 130 | || err_file.metadata().map(|m| m.len()).unwrap_or(0) >= MAX_FILE_BYTES; | ||
| 109 | 131 | ||
| 110 | let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?; | 132 | let (out_text, out_cut) = read_capped(&mut out_file, head_budget, tail_budget)?; |
| 111 | let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?; | 133 | let (err_text, err_cut) = read_capped(&mut err_file, head_budget, tail_budget)?; |
| 112 | drop(out_file); | 134 | drop(out_file); |
| 113 | drop(err_file); | 135 | drop(err_file); |
| 114 | let _ = std::fs::remove_file(&out_path); | ||
| 115 | let _ = std::fs::remove_file(&err_path); | ||
| 116 | 136 | ||
| 117 | let marker = if over_limit { | 137 | let marker = if over_limit { |
| 118 | format!( | 138 | format!( |
