Skip to content

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

fix(claude-code): report the unparsable stream lines the parser keeps

Nguyen Thanh Dat314c0e85209a
diff --git a/src/openhuman/inference/provider/claude_code/driver.rs b/src/openhuman/inference/provider/claude_code/driver.rs
index 1141784371..ce016d5bd4 100644
--- a/src/openhuman/inference/provider/claude_code/driver.rs
+++ b/src/openhuman/inference/provider/claude_code/driver.rs
@@ -18,10 +18,34 @@ use tokio::sync::mpsc;
/// infinite loop, MCP deadlock) we kill the child and surface a timeout.
const TURN_TIMEOUT: Duration = Duration::from_secs(300);
+/// Characters of an unparsable stream line kept in the log: enough to recognise
+/// the shape of the line, not so much that a whole model reply lands in the log.
+const PARSE_ERROR_PREVIEW_CHARS: usize = 200;
+
+/// Render the log line for a `ParseError` event, or `None` for anything else.
+///
+/// The parser keeps `ParseError` precisely so an unparsable line is reported
+/// instead of vanishing, but the event mapper turns it into no deltas - so the
+/// driver loop is the only place left that can say anything about it.
+fn parse_error_log_line(ev: &ClaudeCodeEvent) -> Option<String> {
+ let ClaudeCodeEvent::ParseError { line, reason } = ev else {
+ return None;
+ };
+ let preview: String = line.chars().take(PARSE_ERROR_PREVIEW_CHARS).collect();
+ let ellipsis = if preview.chars().count() < line.chars().count() {
+ "..."
+ } else {
+ ""
+ };
+ Some(format!(
+ "[claude-code][driver] dropping unparsable stream line ({reason}): {preview}{ellipsis}"
+ ))
+}
+
use super::event_mapper::EventMapper;
use super::input_builder::build_stdin;
use super::session_store::{generate_uuid_v4, is_uuid_v4, SessionStore};
-use super::stream_parser::StreamJsonParser;
+use super::stream_parser::{ClaudeCodeEvent, StreamJsonParser};
use crate::openhuman::agent::messages::ChatMessage;
use crate::openhuman::inference::provider::types::{ChatResponse, ProviderDelta};
@@ -441,6 +465,9 @@ pub async fn run_turn(ctx: TurnContext<'_>) -> anyhow::Result<ChatResponse> {
break;
}
for ev in parser.feed_bytes(&buf[..n]) {
+ if let Some(msg) = parse_error_log_line(&ev) {
+ log::warn!("{msg}");
+ }
for delta in mapper.handle(ev) {
if let Some(tx) = ctx.stream {
let _ = tx.send(delta).await;
@@ -449,6 +476,9 @@ pub async fn run_turn(ctx: TurnContext<'_>) -> anyhow::Result<ChatResponse> {
}
}
for ev in parser.end() {
+ if let Some(msg) = parse_error_log_line(&ev) {
+ log::warn!("{msg}");
+ }
for delta in mapper.handle(ev) {
if let Some(tx) = ctx.stream {
let _ = tx.send(delta).await;
diff --git a/src/openhuman/inference/provider/claude_code/driver_tests.rs b/src/openhuman/inference/provider/claude_code/driver_tests.rs
index 081e03d29f..e86d633069 100644
--- a/src/openhuman/inference/provider/claude_code/driver_tests.rs
+++ b/src/openhuman/inference/provider/claude_code/driver_tests.rs
@@ -178,3 +178,56 @@ fn full_access_reads_persisted_toggle_when_env_unset() {
}
let _ = std::fs::remove_dir_all(&ws);
}
+
+#[test]
+fn parse_error_events_produce_a_log_line() {
+ let ev = ClaudeCodeEvent::ParseError {
+ line: "{not json".to_string(),
+ reason: "expected value at line 1 column 2".to_string(),
+ };
+ let msg = parse_error_log_line(&ev).expect("a ParseError must be reported");
+ assert!(msg.contains("{not json"), "{msg}");
+ assert!(msg.contains("expected value at line 1 column 2"), "{msg}");
+}
+
+#[test]
+fn other_events_produce_nothing() {
+ let ev = ClaudeCodeEvent::Error {
+ message: "boom".to_string(),
+ };
+ assert!(parse_error_log_line(&ev).is_none());
+}
+
+#[test]
+fn a_long_line_is_previewed_not_dumped() {
+ let ev = ClaudeCodeEvent::ParseError {
+ line: "x".repeat(5_000),
+ reason: "trailing characters".to_string(),
+ };
+ let msg = parse_error_log_line(&ev).unwrap();
+ assert!(msg.ends_with("..."), "a truncated line must say so: {msg}");
+ assert_eq!(msg.matches('x').count(), PARSE_ERROR_PREVIEW_CHARS);
+}
+
+#[test]
+fn a_short_line_is_not_marked_as_truncated() {
+ let ev = ClaudeCodeEvent::ParseError {
+ line: "oops".to_string(),
+ reason: "r".to_string(),
+ };
+ assert!(!parse_error_log_line(&ev).unwrap().ends_with("..."));
+}
+
+/// The preview counts characters, so a multi-byte line is not split
+/// mid-character the way byte slicing would split it.
+#[test]
+fn a_multibyte_line_is_previewed_on_character_boundaries() {
+ let ev = ClaudeCodeEvent::ParseError {
+ line: "\u{65e5}".repeat(5_000),
+ reason: "r".to_string(),
+ };
+ let msg = parse_error_log_line(&ev).unwrap();
+ assert_eq!(msg.matches('\u{65e5}').count(), PARSE_ERROR_PREVIEW_CHARS);
+}
+
+use super::*;