fix: address review feedback — UTF-8 safe truncation, saturating_sub, lock poison logging, cleanup

Co-Authored-By: Claude Opus 4.6 <[email protected]>
This commit is contained in:
Zaki Manian
2026-03-21 08:13:26 +00:00
committed by Claude
co-authored by Claude Opus 4.6
parent 9cffb1d6b7
commit 786df99dc7
3 changed files with 26 additions and 2 deletions
+23 -1
View File
@@ -118,9 +118,13 @@ impl PtcScriptTool {
if output.len() <= MAX_OUTPUT_SIZE {
output.to_string()
} else {
let mut i = MAX_OUTPUT_SIZE;
while i > 0 && !output.is_char_boundary(i) {
i -= 1;
}
format!(
"{}\n\n[Output truncated at {} bytes]",
&output[..MAX_OUTPUT_SIZE],
&output[..i],
MAX_OUTPUT_SIZE
)
}
@@ -335,6 +339,24 @@ mod tests {
assert!(truncated.contains("[Output truncated"));
}
#[test]
fn test_truncate_output_multibyte_boundary() {
// Build a string of multi-byte chars (emoji = 4 bytes each) that crosses MAX_OUTPUT_SIZE
let emoji = "\u{1F600}"; // 4 bytes
let count = MAX_OUTPUT_SIZE / emoji.len() + 10;
let long: String = emoji.repeat(count);
assert!(long.len() > MAX_OUTPUT_SIZE);
let truncated = PtcScriptTool::truncate_output(&long);
// Must not panic and must contain valid UTF-8
assert!(truncated.contains("[Output truncated"));
// The kept portion must end on a char boundary (valid UTF-8 guaranteed by compilation)
let kept = truncated.split("\n\n[Output truncated").next().unwrap();
assert!(kept.len() <= MAX_OUTPUT_SIZE);
// Every char should be complete (no partial emoji)
assert!(kept.chars().all(|c| c == '\u{1F600}'));
}
#[test]
fn test_tool_metadata() {
let tool = PtcScriptTool::new();
+2
View File
@@ -157,6 +157,8 @@ impl ToolRegistry {
pub fn set_tool_executor(&self, executor: Arc<ToolExecutor>) {
if let Ok(mut guard) = self.tool_executor_slot.write() {
*guard = Some(executor);
} else {
tracing::error!("tool_executor_slot RwLock is poisoned; PTC will be unavailable");
}
}
+1 -1
View File
@@ -46,7 +46,7 @@ struct NestingGuard<'a> {
impl Drop for NestingGuard<'_> {
fn drop(&mut self) {
*self.depth -= 1;
*self.depth = self.depth.saturating_sub(1);
}
}