Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(claude-code): bound the stderr buffer without splitting a character

Nguyen Thanh Datf3fefd8a9f57
diff --git a/src/openhuman/inference/provider/claude_code/driver.rs b/src/openhuman/inference/provider/claude_code/driver.rs
index 1141784371..4b9cb7e64a 100644
--- a/src/openhuman/inference/provider/claude_code/driver.rs
+++ b/src/openhuman/inference/provider/claude_code/driver.rs
@@ -18,6 +18,28 @@ use tokio::sync::mpsc;
/// infinite loop, MCP deadlock) we kill the child and surface a timeout.
const TURN_TIMEOUT: Duration = Duration::from_secs(300);
+/// How much of the child's stderr we keep for diagnostics.
+const STDERR_DIAGNOSTIC_CAP: usize = 16_384;
+
+/// Append `chunk` to `acc`, keeping at most `max_bytes` and never splitting a
+/// character.
+///
+/// `String::truncate` takes a *byte* index and panics when it is not a character
+/// boundary, so bounding this accumulator with `acc.truncate(16_384)` aborted the
+/// task the moment a multi-byte character straddled the cap. The panic happened
+/// inside `tokio::spawn`, and the join is `unwrap_or_default()`, so it surfaced as
+/// an empty stderr string: the operator lost the whole error output for that turn
+/// and saw `exit Some(1) stderr=`.
+fn push_bounded(acc: &mut String, chunk: &str, max_bytes: usize) {
+ acc.push_str(chunk);
+ if acc.len() > max_bytes {
+ let keep = utf8_safe_prefix_at_byte_boundary(acc, max_bytes).len();
+ acc.truncate(keep);
+ }
+}
+
+use crate::openhuman::util::text::utf8_safe_prefix_at_byte_boundary;
+
use super::event_mapper::EventMapper;
use super::input_builder::build_stdin;
use super::session_store::{generate_uuid_v4, is_uuid_v4, SessionStore};
@@ -421,10 +443,11 @@ pub async fn run_turn(ctx: TurnContext<'_>) -> anyhow::Result<ChatResponse> {
if n == 0 {
break;
}
- acc.push_str(&String::from_utf8_lossy(&tmp[..n]));
- if acc.len() > 16_384 {
- acc.truncate(16_384);
- }
+ push_bounded(
+ &mut acc,
+ &String::from_utf8_lossy(&tmp[..n]),
+ STDERR_DIAGNOSTIC_CAP,
+ );
}
acc
});
diff --git a/src/openhuman/inference/provider/claude_code/driver_tests.rs b/src/openhuman/inference/provider/claude_code/driver_tests.rs
index 081e03d29f..de3951a74d 100644
--- a/src/openhuman/inference/provider/claude_code/driver_tests.rs
+++ b/src/openhuman/inference/provider/claude_code/driver_tests.rs
@@ -178,3 +178,47 @@ fn full_access_reads_persisted_toggle_when_env_unset() {
}
let _ = std::fs::remove_dir_all(&ws);
}
+
+/// `String::truncate` takes a byte index and panics when it is not a character
+/// boundary, so bounding the stderr accumulator with `acc.truncate(16_384)`
+/// aborted the drain task whenever a multi-byte character straddled the cap.
+/// The join is `unwrap_or_default()`, so the operator saw `stderr=` and lost
+/// the whole error output for that turn.
+#[test]
+fn push_bounded_never_splits_a_character() {
+ // "aéb" is 4 bytes: a=0, é=1..2, b=3. A cap of 2 lands *inside* 'é', so the
+ // helper must back up to byte 1 -- `String::truncate(2)` would panic here.
+ let mut acc = String::new();
+ push_bounded(&mut acc, "aéb", 2);
+ assert_eq!(acc, "a", "must back up to the boundary, not split it");
+
+ // A cap of 3 lands exactly on a boundary, so nothing is given up needlessly.
+ let mut acc = String::new();
+ push_bounded(&mut acc, "aéb", 3);
+ assert_eq!(acc, "aé");
+ assert!(acc.len() <= 3);
+
+ // The same, driven the way the reader does it: many small chunks over the cap.
+ let mut acc = String::new();
+ for _ in 0..600 {
+ push_bounded(&mut acc, "日本語テキスト", 1024);
+ }
+ assert!(acc.len() <= 1024);
+ // The real assertion: it is still valid UTF-8 and did not panic getting here.
+ assert!(std::str::from_utf8(acc.as_bytes()).is_ok());
+}
+
+#[test]
+fn push_bounded_keeps_everything_below_the_cap() {
+ let mut acc = String::new();
+ push_bounded(&mut acc, "hello ", 64);
+ push_bounded(&mut acc, "world", 64);
+ assert_eq!(acc, "hello world");
+}
+
+#[test]
+fn push_bounded_handles_an_ascii_cap_exactly() {
+ let mut acc = String::new();
+ push_bounded(&mut acc, "abcdef", 3);
+ assert_eq!(acc, "abc");
+}