Skip to content

Commit 168a692

Browse files
author
viethx
committed
fix: contain pdf-extract panics and multibyte truncation panic
- jcode-pdf: pdf_extract panics on malformed PDFs ('unexpected entry in unicode map', 'explicit panic'), killing the read tool task with 'Tool task panicked'. Wrap extraction in catch_unwind and return a normal error instead. - comm_format: format_comm_context_history byte-sliced content at index 500, panicking mid-code-point on multibyte text (observed with Vietnamese 'ủ' via the swarm tool). Truncate on a char boundary. Both observed as real 'Tool task panicked' failures in session logs.
1 parent 44440cc commit 168a692

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

‎crates/jcode-pdf/src/lib.rs‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
use anyhow::Result;
22
use std::path::Path;
33

4+
/// Extract text from a PDF.
5+
///
6+
/// `pdf_extract` panics on malformed or exotic PDFs (e.g.
7+
/// "unexpected entry in unicode map", observed crashing the read tool with
8+
/// "Tool task panicked"). Contain those panics here and surface them as
9+
/// ordinary errors so a bad PDF cannot take down the calling tool task.
410
pub fn extract_text(path: &Path) -> Result<String> {
5-
Ok(pdf_extract::extract_text(path)?)
11+
let path = path.to_path_buf();
12+
match std::panic::catch_unwind(move || pdf_extract::extract_text(&path)) {
13+
Ok(result) => Ok(result?),
14+
Err(panic) => {
15+
let message = panic
16+
.downcast_ref::<&str>()
17+
.map(|s| s.to_string())
18+
.or_else(|| panic.downcast_ref::<String>().cloned())
19+
.unwrap_or_else(|| "unknown panic".to_string());
20+
Err(anyhow::anyhow!(
21+
"PDF text extraction failed (parser panic: {message}). The file may be malformed or use unsupported encoding."
22+
))
23+
}
24+
}
625
}

‎crates/jcode-protocol/src/comm_format.rs‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,11 @@ pub fn format_comm_context_history(target: &str, messages: &[HistoryMessage]) ->
518518
messages.len()
519519
);
520520
for msg in messages {
521-
let truncated = if msg.content.len() > 500 {
522-
format!("{}...", &msg.content[..500])
523-
} else {
524-
msg.content.clone()
521+
// Truncate on a char boundary: byte-slicing multibyte text (e.g.
522+
// Vietnamese) at a fixed index panics inside a code point.
523+
let truncated = match msg.content.char_indices().nth(500) {
524+
Some((byte_idx, _)) => format!("{}...", &msg.content[..byte_idx]),
525+
None => msg.content.clone(),
525526
};
526527
output.push_str(&format!("[{}] {}\n\n", msg.role, truncated));
527528
}
@@ -644,3 +645,38 @@ pub fn format_comm_channels(channels: &[SwarmChannelInfo]) -> String {
644645
output
645646
}
646647
}
648+
649+
#[cfg(test)]
650+
mod comm_format_truncation_tests {
651+
use super::*;
652+
653+
#[test]
654+
fn context_history_truncates_multibyte_content_without_panicking() {
655+
// 600 Vietnamese chars: byte index 500 falls inside a code point, which
656+
// used to panic ("end byte index 500 is not a char boundary").
657+
let content = "ủ".repeat(600);
658+
let messages = vec![HistoryMessage {
659+
role: "user".to_string(),
660+
content,
661+
tool_calls: None,
662+
tool_data: None,
663+
}];
664+
let output = format_comm_context_history("worker", &messages);
665+
assert!(output.contains("..."), "long content should be truncated");
666+
// 500 chars survive the cut.
667+
assert!(output.matches('ủ').count() == 500);
668+
}
669+
670+
#[test]
671+
fn context_history_keeps_short_multibyte_content_intact() {
672+
let messages = vec![HistoryMessage {
673+
role: "user".to_string(),
674+
content: "xin chào đội ngũ".to_string(),
675+
tool_calls: None,
676+
tool_data: None,
677+
}];
678+
let output = format_comm_context_history("worker", &messages);
679+
assert!(output.contains("xin chào đội ngũ"));
680+
assert!(!output.contains("..."));
681+
}
682+
}

0 commit comments

Comments
 (0)