1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Tool Failures dashboard page — browse flattened tool call errors from the logs store.
//!
//! Two-line row layout with role badges and HH:MM:SS timestamps, matching the
//! Logs page style. No live streaming — data refreshes on search changes or
//! tab switch. Pagination and search controls live in the Logs page's shared
//! bottom bar (see [`super::logs`]), so this page only renders the entries.
use crate::stats::{ToolErrorEntry, ToolErrorQuery};
use iced::widget::{Column, Space, column, container, row, scrollable, text};
use iced::{Alignment, Element, Length, Task};
use iced_fonts::lucide;
use super::theme;
use super::widgets;
use super::widgets::selectable_text;
#[derive(Debug, Clone)]
pub enum ToolFailuresMessage {
/// Data refreshed from the store. Carries the refresh generation (stale
/// responses are dropped), the entries, and the total count.
Refreshed(u64, Vec<ToolErrorEntry>, usize),
/// Refresh query failed. Carries the generation so stale errors are dropped.
RefreshError(u64, String),
}
pub struct ToolFailuresState {
entries: Vec<ToolErrorEntry>,
load_state: super::common::AsyncLoadState,
// Pagination
pagination: super::common::PaginationState,
/// This tab's own search query (preserved across tab switches).
search: String,
/// Monotonic guard: refresh responses carry the generation they were
/// issued under; stale responses (issued before a newer refresh) are
/// dropped so an older query can never overwrite newer data.
refresh_generation: u64,
}
impl ToolFailuresState {
#[must_use]
pub fn new() -> Self {
Self {
entries: Vec::new(),
load_state: super::common::AsyncLoadState::new(),
pagination: super::common::PaginationState::new(50),
search: String::new(),
refresh_generation: 0,
}
}
fn build_query(search: &str) -> ToolErrorQuery {
ToolErrorQuery {
search: crate::util::none_if_empty(search),
}
}
/// Request a refresh from the logs store.
///
/// Delegates to `AsyncLoadState::start_loading`.
pub fn refresh(&mut self) -> Task<ToolFailuresMessage> {
self.load_state.start_loading();
self.refresh_generation = self.refresh_generation.wrapping_add(1);
let generation = self.refresh_generation;
let query = Self::build_query(&self.search);
let page = self.pagination.page;
let page_size = self.pagination.page_size;
Task::perform(
async move {
// Fail-open: no logs store → empty result, not a panic.
let Some(store) = crate::logs::LOG_STORE.get() else {
return Ok((generation, Vec::new(), 0));
};
store
.query_tool_errors(&query, page_size, page * page_size)
.await
.map(|(entries, total)| (generation, entries, total))
.map_err(|e| (generation, e.to_string()))
},
|res| match res {
Ok((generation, entries, total)) => {
ToolFailuresMessage::Refreshed(generation, entries, total)
}
Err((generation, e)) => ToolFailuresMessage::RefreshError(generation, e),
},
)
}
pub fn update(&mut self, message: ToolFailuresMessage) -> Task<ToolFailuresMessage> {
match message {
ToolFailuresMessage::Refreshed(generation, entries, total) => {
if generation != self.refresh_generation {
return Task::none();
}
// Page clamp against the fresh `total` from the response:
// if the total shrank between refreshes a previously valid
// page may now be past the end — clamp and re-query so the
// "Page X of Y" indicator stays valid.
if self.pagination.clamp_page(total) {
// Adopt the fresh total immediately so the shared bottom
// bar's "Page X of Y" indicator stays consistent with the
// clamped page during the re-query window; the old
// entries stay on screen.
self.pagination.total = total;
return self.refresh();
}
self.entries = entries;
self.pagination.total = total;
self.load_state.finish_loading();
Task::none()
}
ToolFailuresMessage::RefreshError(generation, e) => {
if generation != self.refresh_generation {
return Task::none();
}
self.load_state.fail(e);
// ToolFailures shows "empty state" instead of "Loading…" after
// the first attempt, even if it failed, so mark has_loaded=true.
self.load_state.set_has_loaded();
Task::none()
}
}
}
/// Reset pagination to the first page.
pub fn reset_pagination(&mut self) {
self.pagination.reset();
}
/// Go to the previous page and refresh.
pub fn prev_page(&mut self) -> Task<ToolFailuresMessage> {
if self.pagination.prev_page() {
self.refresh()
} else {
Task::none()
}
}
/// Go to the next page and refresh.
pub fn next_page(&mut self) -> Task<ToolFailuresMessage> {
if self.pagination.next_page() {
self.refresh()
} else {
Task::none()
}
}
/// Current page index (rendered by the Logs page's shared bottom bar).
pub(crate) fn page(&self) -> usize {
self.pagination.page
}
/// Total number of pages given the current total (rendered by the Logs
/// page's shared bottom bar).
pub(crate) fn total_pages(&self) -> usize {
self.pagination.total_pages()
}
/// This tab's search query (bound to the Logs page's shared search input).
pub(crate) fn search(&self) -> &str {
&self.search
}
/// Replace this tab's search query (from the Logs page's shared search input).
pub(crate) fn set_search(&mut self, search: String) {
self.search = search;
}
pub fn view(&self) -> Element<'_, ToolFailuresMessage> {
let mut content = Column::new();
// Error display
content = widgets::push_error_banner(content, self.load_state.error());
// Entries or empty state
if self.load_state.loading() && !self.load_state.has_loaded() {
content = content.push(widgets::loading_text());
} else if self.entries.is_empty() && self.load_state.has_loaded() {
content = content.push(widgets::empty_state_placeholder(
lucide::bug::<iced::Theme, iced::Renderer>(),
"No tool failures",
));
} else if !self.entries.is_empty() {
let entries_view = {
scrollable(
Column::with_children(
self.entries
.iter()
.map(Self::render_error_row)
.collect::<Vec<_>>(),
)
.spacing(2),
)
.height(Length::Fill)
.direction(theme::vertical_scrollbar())
.style(theme::scrollbar_style)
};
content = content.push(entries_view);
}
container(content)
.width(Length::Fill)
.height(Length::Fill)
.padding(24)
.style(theme::base_container_style)
.into()
}
/// Render a single error row with two-line layout:
/// Line 1: HH:MM:SS timestamp | tool name badge | role badge | workspace
/// Line 2: error message (selectable monospace text)
/// Build the metadata badge row for a tool error entry.
fn render_metadata_row(entry: &ToolErrorEntry) -> iced::widget::Row<'_, ToolFailuresMessage> {
let role_colors = theme::role_badge_color(&entry.role);
let timestamp = theme::format_hhmmss(&entry.recorded_at);
let duration_label = format!("{}ms", entry.duration_ms);
row![
text(timestamp).size(10).color(theme::TEXT_MUTED),
Space::new().width(8),
widgets::badge_pill(
entry.tool_name.clone(),
(theme::HOVER, theme::TEXT_SECONDARY),
10,
[1, 6],
),
Space::new().width(4),
widgets::badge_pill(
duration_label,
(theme::HOVER, theme::TEXT_MUTED),
10,
[1, 6]
),
Space::new().width(4),
widgets::role_badge(entry.role.clone(), role_colors, 10, [1, 6], false),
Space::new().width(Length::Fill),
if !entry.workspace.is_empty() {
text(&entry.workspace).size(10).color(theme::TEXT_MUTED)
} else {
text("")
},
]
.align_y(Alignment::Center)
.spacing(2)
}
/// Compute an optional arguments preview string, truncated to 200 chars.
fn compute_args_preview(entry: &ToolErrorEntry) -> Option<String> {
if entry.arguments.is_empty() || entry.arguments == "{}" {
return None;
}
if entry.arguments.len() > 200 {
Some(format!(
"{}…",
crate::util::truncate_bytes(&entry.arguments, 200)
))
} else {
Some(entry.arguments.clone())
}
}
fn render_error_row(entry: &ToolErrorEntry) -> iced::Element<'_, ToolFailuresMessage> {
let metadata_row = Self::render_metadata_row(entry);
let args_preview = Self::compute_args_preview(entry);
let row_content = column![
metadata_row,
Space::new().height(2),
if let Some(ref preview) = args_preview {
iced::Element::new(
selectable_text(preview.clone(), theme::TEXT_MUTED)
.size(11)
.font(super::JETBRAINS_MONO)
.width(Length::Fill),
)
} else {
iced::Element::new(iced::widget::Space::new().height(0))
},
if !entry.error_message.is_empty() {
let mut parts = column![].spacing(0);
if args_preview.is_some() {
parts = parts.push(Space::new().height(2));
}
parts = parts.push(
selectable_text(&entry.error_message, theme::TEXT_PRIMARY)
.size(13)
.font(super::JETBRAINS_MONO)
.width(Length::Fill),
);
iced::Element::new(parts)
} else {
iced::Element::new(iced::widget::Space::new().height(0))
},
]
.spacing(0);
container(row_content)
.padding(6)
.style(theme::surface_card_style)
.into()
}
}