tokens: rolling cache breakpoints, real-usage compact trigger, smaller read/bash limits with head+tail
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-01 21:00:47 +00:00
1 parent d332cf4cc4
commit c5420026b8
3 files changed
+109 -9

No files matched your search

+31 -7
View File
@@ -7,8 +7,10 @@ use serde_json::{Value, json};
use std::path::{Path, PathBuf};
use std::time::Duration;
const MAX_LINES: usize = 2000;
const MAX_BYTES: usize = 50_000;
const MAX_LINES: usize = 600;
const MAX_BYTES: usize = 20_000;
/// bash keeps this much of the start (command echo, first errors) and the rest from the end.
const HEAD_BYTES: usize = 4_000;
fn spec(name: &str, desc: &str, schema: Value) -> ToolSpec {
ToolSpec {
@@ -28,6 +30,20 @@ fn resolve(cx: &ToolCtx, p: &str) -> PathBuf {
}
/// Keep the tail of `s` within `max` bytes on a char boundary.
/// First `head` bytes + last `max - head` bytes, char-boundary safe. Returns (text, cut bytes).
fn head_tail(s: &str, max: usize, head: usize) -> (String, usize) {
if s.len() <= max {
return (s.to_string(), 0);
}
let mut h = head;
while !s.is_char_boundary(h) {
h -= 1;
}
let t = tail(s, max - head);
let cut = s.len() - h - t.len();
(format!("{}\n[... {cut} bytes cut ...]\n{t}", &s[..h]), cut)
}
fn tail(s: &str, max: usize) -> &str {
if s.len() <= max {
return s;
@@ -247,7 +263,7 @@ impl Tool for Bash {
fn spec(&self) -> ToolSpec {
spec(
"bash",
"Run a shell command in the working directory. stdout and stderr are merged. Long output is truncated to the tail.",
"Run a shell command in the working directory. stdout and stderr are merged. Long output keeps the start and the end; the middle is cut and the full output saved to a file.",
json!({"type":"object","properties":{
"command":{"type":"string"},
"timeout":{"type":"integer","description":"seconds, default 120"}},
@@ -287,8 +303,8 @@ impl Tool for Bash {
}
};
let s = String::from_utf8_lossy(&out);
let mut body = tail(&s, MAX_BYTES).to_string();
if body.len() < s.len() {
let (mut body, cut) = head_tail(&s, MAX_BYTES, HEAD_BYTES);
if cut > 0 {
let f = std::env::temp_dir().join(format!(
"hx-bash-{}.log",
std::process::id() as u128 * 1000 + pid.unwrap_or(0) as u128
@@ -296,7 +312,7 @@ impl Tool for Bash {
let _ = std::fs::write(&f, s.as_bytes());
body = format!(
"[truncated {} bytes; full output: {}]\n{body}",
s.len() - body.len(),
cut,
f.display()
);
}
@@ -353,6 +369,14 @@ mod tests {
assert!(r.content.len() < MAX_BYTES + 200 && r.content.contains("use offset="));
}
#[test]
fn head_tail_keeps_both_ends() {
let s = format!("HEAD{}TAIL", "é".repeat(5000));
let (o, cut) = head_tail(&s, 1000, 200);
assert!(o.starts_with("HEAD") && o.ends_with("TAIL") && cut > 0 && o.len() < 1100);
assert_eq!(head_tail("short", 1000, 200), ("short".to_string(), 0));
}
#[tokio::test]
async fn write_edit_guard_crlf() {
let d = tempfile::tempdir().unwrap();
@@ -444,6 +468,6 @@ mod tests {
let r = Bash
.call(json!({"command":"yes | head -c 200000"}), &c)
.await;
assert!(r.content.contains("[truncated") && r.content.len() < MAX_BYTES + 300);
assert!(r.content.contains("[truncated") && r.content.len() < MAX_BYTES + 400);
}
}