Skip to content

Commit 6fbb1bc

Browse files
jidongclaude
authored andcommitted
fix: improve Rust panic (2%→80%) and Java error (-12%→20%) compression
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 08fc7b3 commit 6fbb1bc

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

src/error_cmd.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,19 @@ fn compress_rust(input: &str) -> String {
246246
let mut result = Vec::new();
247247
let mut hidden_count = 0;
248248
let mut i = 0;
249+
let mut skip_next_at = false;
250+
251+
fn is_rust_framework_line(line: &str) -> bool {
252+
RUST_FRAMEWORK_RE.is_match(line)
253+
|| RUST_RUSTC_PATH_RE.is_match(line)
254+
|| line.contains(".cargo/registry/")
255+
}
249256

250257
while i < lines.len() {
251258
let line = lines[i];
252259

253260
// Match numbered backtrace frame line (e.g. " 3: myapp::handler::process")
254261
if RUST_BACKTRACE_FRAME_RE.is_match(line) {
255-
// Extract function name from this line
256262
let func_name = RUST_FRAME_EXTRACT_RE
257263
.captures(line)
258264
.and_then(|c| c.get(1))
@@ -266,19 +272,21 @@ fn compress_rust(input: &str) -> String {
266272
None
267273
};
268274

269-
// Determine if this is a framework frame
270-
let is_framework = RUST_FRAMEWORK_RE.is_match(line)
271-
|| at_line.is_some_and(|l| RUST_RUSTC_PATH_RE.is_match(l))
272-
|| at_line.is_some_and(|l| RUST_FRAMEWORK_RE.is_match(l));
275+
// Framework if function name or at-path matches
276+
let is_framework = is_rust_framework_line(line)
277+
|| at_line.is_some_and(|l| is_rust_framework_line(l));
273278

274279
if is_framework {
275280
hidden_count += 1;
276-
// Skip the "at" continuation line too
277281
if at_line.is_some() {
278-
i += 1;
282+
skip_next_at = true;
279283
}
280284
} else {
281-
// User code frame -- format nicely
285+
// Flush hidden count before user frame
286+
if hidden_count > 0 {
287+
result.push(format!(" (+ {} framework frames hidden)", hidden_count));
288+
hidden_count = 0;
289+
}
282290
let location = at_line.and_then(|l| {
283291
RUST_LOCATION_RE.captures(l).map(|c| {
284292
let file = c.get(1).map(|m| m.as_str()).unwrap_or("?");
@@ -288,30 +296,29 @@ fn compress_rust(input: &str) -> String {
288296
});
289297

290298
if let Some(loc) = location {
291-
result.push(format!(" \u{2192} {:<16} {}()", loc, func_name));
299+
result.push(format!(" \u{2192} {} {}()", loc, func_name));
300+
skip_next_at = true;
292301
} else {
293302
result.push(format!(" \u{2192} {}", func_name));
294303
}
295-
296-
// Skip the "at" continuation line
297-
if at_line.is_some() {
298-
i += 1;
299-
}
300304
}
301305
} else if RUST_BACKTRACE_AT_RE.is_match(line) {
302-
// Orphan "at" line (shouldn't happen normally, but handle gracefully)
303-
// If it's a rustc internal path, hide it
304-
if RUST_RUSTC_PATH_RE.is_match(line) {
306+
if skip_next_at {
307+
skip_next_at = false;
308+
} else if is_rust_framework_line(line) {
305309
hidden_count += 1;
306310
} else {
307311
result.push(line.to_string());
308312
}
313+
} else if line.trim() == "stack backtrace:" {
314+
// Keep but don't flush hidden count (backtrace hasn't started)
315+
result.push(line.to_string());
309316
} else {
310-
// Non-frame line (panic message, "stack backtrace:", etc.)
311317
if hidden_count > 0 {
312318
result.push(format!(" (+ {} framework frames hidden)", hidden_count));
313319
hidden_count = 0;
314320
}
321+
skip_next_at = false;
315322
result.push(line.to_string());
316323
}
317324

@@ -322,7 +329,12 @@ fn compress_rust(input: &str) -> String {
322329
result.push(format!(" (+ {} framework frames hidden)", hidden_count));
323330
}
324331

325-
result.join("\n")
332+
let compressed = result.join("\n");
333+
// Guard: if compression made it larger, return original
334+
if compressed.len() >= input.len() {
335+
return input.to_string();
336+
}
337+
compressed
326338
}
327339

328340
fn compress_go(input: &str) -> String {

0 commit comments

Comments
 (0)