Skip to content

Commit 9a0b6ab

Browse files
committed
Unify tool failures with normal rows and compact connected reasons
1 parent 70e50cc commit 9a0b6ab

5 files changed

Lines changed: 351 additions & 44 deletions

File tree

‎crates/jcode-tui/src/tui/ui_messages.rs‎

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,12 +2790,18 @@ pub(crate) fn render_background_task_message(
27902790
parsed.display_name.as_deref(),
27912791
);
27922792
let is_swarm = parsed.tool_name == "swarm";
2793-
if is_swarm {
2793+
if is_swarm && !parsed.status.starts_with('✗') {
27942794
return render_compact_swarm_background_completion(&parsed, width);
27952795
}
27962796
if parsed.status.starts_with('✗') {
27972797
let muted = Style::default().fg(dim_color());
27982798
let failure = Style::default().fg(rgb(220, 100, 100));
2799+
let block_width = if centered {
2800+
super::centered_content_block_width(width, 96)
2801+
} else {
2802+
width as usize
2803+
};
2804+
let row_width = block_width.saturating_sub(1);
27992805
let timing = msg
28002806
.content
28012807
.lines()
@@ -2843,32 +2849,31 @@ pub(crate) fn render_background_task_message(
28432849
})
28442850
.unwrap_or_else(|| format!("{} · no diagnostic captured", parsed.exit_label));
28452851
let mut header = Line::from(vec![
2846-
Span::styled("✗ ", failure),
2852+
Span::styled(" ✗ ", failure),
28472853
Span::styled(
2848-
if task_label == parsed.tool_name {
2849-
parsed.tool_name.clone()
2850-
} else {
2851-
format!("{} {}", parsed.tool_name, task_label)
2852-
},
2853-
Style::default(),
2854+
tools_ui::resolve_display_tool_name(&parsed.tool_name).to_string(),
2855+
Style::default().fg(tool_color()),
28542856
),
28552857
]);
2856-
let timing = format!(" · {timing}");
2857-
header = super::truncate_line_with_ellipsis_to_width(
2858-
&header,
2859-
(width as usize).saturating_sub(UnicodeWidthStr::width(timing.as_str())),
2860-
);
2861-
header.spans.push(Span::styled(timing, muted));
2858+
if task_label != parsed.tool_name {
2859+
header.spans.push(Span::styled(" · ", muted));
2860+
header.spans.push(Span::styled(
2861+
task_label.clone(),
2862+
Style::default().fg(tool_color()),
2863+
));
2864+
}
2865+
let timing = Line::from(Span::styled(format!(" · {timing}"), muted));
2866+
header = super::truncate_line_preserving_suffix_to_width(&header, &timing, row_width);
28622867
let reason = Line::from(vec![
2863-
Span::styled("╰─ ", muted),
2868+
Span::styled(" ╰─ ", muted),
28642869
Span::styled(detail, muted),
28652870
]);
28662871
let mut lines = vec![
2867-
super::truncate_line_with_ellipsis_to_width(&header, width as usize),
2868-
super::truncate_line_with_ellipsis_to_width(&reason, width as usize),
2872+
header,
2873+
super::truncate_line_with_ellipsis_to_width(&reason, row_width),
28692874
];
28702875
if centered {
2871-
left_pad_lines_for_centered_mode(&mut lines, width);
2876+
super::left_pad_lines_to_block_width(&mut lines, width, block_width);
28722877
}
28732878
return lines;
28742879
}
@@ -4099,6 +4104,42 @@ fn inline_diff_header(
40994104
.alignment(ratatui::layout::Alignment::Left)
41004105
}
41014106

4107+
fn render_tool_failure_reason(content: &str, width: usize, indent: usize) -> Line<'static> {
4108+
let clean = strip_ansi_escape_sequences(content);
4109+
let normalized = clean
4110+
.lines()
4111+
.map(str::trim)
4112+
.filter(|line| !line.starts_with("[tool timing:") && !line.starts_with("[tool duration:"))
4113+
.map(|line| {
4114+
line.strip_prefix("[stderr] ")
4115+
.or_else(|| line.strip_prefix("[stdout] "))
4116+
.unwrap_or(line)
4117+
})
4118+
.collect::<Vec<_>>()
4119+
.join("\n");
4120+
let diagnostic = normalized
4121+
.lines()
4122+
.find(|line| line.starts_with("error[") || line.contains("panicked at"));
4123+
let summary = diagnostic
4124+
.map(str::to_owned)
4125+
.or_else(|| tools_ui::concise_tool_error_summary(&normalized))
4126+
.unwrap_or_else(|| {
4127+
normalized
4128+
.lines()
4129+
.find(|line| !line.is_empty())
4130+
.unwrap_or("Tool call failed")
4131+
.to_string()
4132+
});
4133+
let summary = summary.split_whitespace().collect::<Vec<_>>().join(" ");
4134+
super::truncate_line_with_ellipsis_to_width(
4135+
&Line::from(Span::styled(
4136+
format!("{}╰─ {}", " ".repeat(indent), summary),
4137+
Style::default().fg(dim_color()),
4138+
)),
4139+
width,
4140+
)
4141+
}
4142+
41024143
pub(crate) fn render_tool_message(
41034144
msg: &DisplayMessage,
41044145
width: u16,
@@ -4160,7 +4201,7 @@ pub(crate) fn render_tool_message(
41604201
return lines;
41614202
};
41624203

4163-
if tools_ui::is_memory_store_tool(tc) && !msg.content.starts_with("Error:") {
4204+
if tools_ui::is_memory_store_tool(tc) && !tools_ui::tool_output_looks_failed(&msg.content) {
41644205
let content = tc
41654206
.input
41664207
.get("content")
@@ -4198,7 +4239,7 @@ pub(crate) fn render_tool_message(
41984239
return lines;
41994240
}
42004241

4201-
if tools_ui::is_memory_recall_tool(tc) && !msg.content.starts_with("Error:") {
4242+
if tools_ui::is_memory_recall_tool(tc) && !tools_ui::tool_output_looks_failed(&msg.content) {
42024243
let border_style = Style::default().fg(rgb(150, 180, 255));
42034244
let text_style = Style::default().fg(dim_color());
42044245

@@ -4319,8 +4360,6 @@ pub(crate) fn render_tool_message(
43194360
} else {
43204361
format!("{} calls", counts.total())
43214362
}
4322-
} else if let Some(error_summary) = tools_ui::concise_tool_error_summary(&msg.content) {
4323-
error_summary
43244363
} else if tc.name == "subagent" {
43254364
msg.title
43264365
.as_deref()
@@ -4339,7 +4378,7 @@ pub(crate) fn render_tool_message(
43394378
};
43404379
// Edit rows read like every other tool row: the intent. The file path is
43414380
// already shown on the inline diff header right below, so repeating it on
4342-
// the row is noise. Errors keep their summary so failures stay diagnosable.
4381+
// the row is noise. Failures keep their call description on the header.
43434382
let edit_path_on_diff_header = is_edit_tool
43444383
&& !is_error
43454384
&& diff_mode.is_inline()
@@ -4360,9 +4399,7 @@ pub(crate) fn render_tool_message(
43604399
intent.to_string(),
43614400
Style::default().fg(tool_color()),
43624401
));
4363-
// Error summaries always render so failures stay diagnosable even
4364-
// when technical details are hidden.
4365-
let show_detail = tools_ui::show_tool_call_details() || is_error || is_partial_batch;
4402+
let show_detail = tools_ui::show_tool_call_details() || is_partial_batch;
43664403
if show_detail && !summary.is_empty() && summary != intent {
43674404
tool_line.push(Span::styled(" · ", Style::default().fg(dim_color())));
43684405
tool_line.push(Span::styled(summary, Style::default().fg(dim_color())));
@@ -4398,6 +4435,13 @@ pub(crate) fn render_tool_message(
43984435
);
43994436
let rendered_tool_line_text = super::line_plain_text(&rendered_tool_line);
44004437
lines.push(rendered_tool_line);
4438+
if is_error && (tc.name != "batch" || batch_counts.is_none()) {
4439+
lines.push(render_tool_failure_reason(&msg.content, row_width, 4));
4440+
if centered {
4441+
super::left_pad_lines_to_block_width(&mut lines, width, block_width);
4442+
}
4443+
return lines;
4444+
}
44014445
if let Some(draft_lines) = render_gmail_draft_card(tc, &msg.content, is_error, row_width) {
44024446
lines.extend(draft_lines);
44034447
}
@@ -4512,6 +4556,17 @@ pub(crate) fn render_tool_message(
45124556
sub_result.map(|result| result.content.as_str()),
45134557
));
45144558

4559+
if sub_errored {
4560+
lines.push(render_tool_failure_reason(
4561+
sub_result
4562+
.map(|result| result.content.as_str())
4563+
.unwrap_or("Error: Tool call failed"),
4564+
row_width,
4565+
6,
4566+
));
4567+
continue;
4568+
}
4569+
45154570
if let Some(result) = sub_result
45164571
&& let Some(mut draft_lines) = render_gmail_draft_card(
45174572
&sub_tc,

‎crates/jcode-tui/src/tui/ui_messages/gmail_drafts_tests.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn render_tool_message_shows_gmail_draft_card() {
5252
}
5353

5454
#[test]
55-
fn render_gmail_draft_card_marks_failures_and_empty_fields() {
55+
fn render_gmail_draft_failure_keeps_normal_header_without_redundant_card() {
5656
let msg = gmail_draft_message(
5757
"Error: Gmail draft creation failed",
5858
serde_json::json!({ "action": "draft", "body": "" }),
@@ -65,10 +65,12 @@ fn render_gmail_draft_card_marks_failures_and_empty_fields() {
6565
.collect::<Vec<_>>()
6666
.join("\n");
6767

68-
assert!(plain.contains("Gmail draft failed"), "{plain}");
69-
assert!(plain.contains("(recipient missing)"), "{plain}");
70-
assert!(plain.contains("(no subject)"), "{plain}");
71-
assert!(plain.contains("(empty body)"), "{plain}");
68+
assert_eq!(lines.len(), 2, "{plain}");
69+
assert!(plain.contains("✗ gmail draft"), "{plain}");
70+
assert!(plain.contains("╰─ error: Gmail draft creation failed"), "{plain}");
71+
assert!(!plain.contains("(recipient missing)"), "{plain}");
72+
assert!(!plain.contains("(no subject)"), "{plain}");
73+
assert!(!plain.contains("(empty body)"), "{plain}");
7274
}
7375

7476
#[test]

0 commit comments

Comments
 (0)