| 1 | //! `/workflows` — the live workflow **run** dashboard. |
| 2 | //! |
| 3 | //! Lists every run this workspace's journal knows for the current session — |
| 4 | //! active and retained, not saved definitions — with the run's status, label, |
| 5 | //! elapsed time, child count, and latest progress. The detail pane adds the |
| 6 | //! phase order, the child-agent roster, the retained progress tail, and the |
| 7 | //! run id `/workflow cancel` accepts. `x` cancels the selected running run |
| 8 | //! through the same host path as `/workflow cancel` (no model turn, no |
| 9 | //! confirmation friction — matching the workflow panel's one-press cancel); |
| 10 | //! `r` re-reads the journal. |
| 11 | //! |
| 12 | //! This view never asks the model anything: it reads |
| 13 | //! [`codewhale_engine::tools::workflow::host_workflow_run_details`] and writes only |
| 14 | //! through [`codewhale_engine::tools::workflow::host_cancel_workflow`]. |
| 15 | |
| 16 | use std::cell::Cell; |
| 17 | use std::path::PathBuf; |
| 18 | use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; |
| 19 | |
| 20 | use crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind}; |
| 21 | use ratatui::{ |
| 22 | buffer::Buffer, |
| 23 | layout::{Constraint, Direction, Layout, Rect}, |
| 24 | style::{Modifier, Style}, |
| 25 | text::{Line, Span}, |
| 26 | widgets::{Paragraph, Widget, Wrap}, |
| 27 | }; |
| 28 | |
| 29 | use super::{ActionHint, ModalKind, ModalView, ViewAction, render_modal_footer}; |
| 30 | use crate::tui::app::App; |
| 31 | use codewhale_engine::tools::workflow::{ |
| 32 | HostWorkflowChildRow, HostWorkflowRunDetail, host_cancel_workflow, host_workflow_run_details, |
| 33 | }; |
| 34 | use codewhale_palette as palette; |
| 35 | use codewhale_tui_kit::list_nav::wrap_index; |
| 36 | |
| 37 | fn now_ms() -> u64 { |
| 38 | SystemTime::now() |
| 39 | .duration_since(UNIX_EPOCH) |
| 40 | .map(|d| d.as_millis() as u64) |
| 41 | .unwrap_or_default() |
| 42 | } |
| 43 | |
| 44 | /// Status ink follows the workflow panel's grammar: running is working ink, |
| 45 | /// completion is outcome ink, only failure (including budget and replay |
| 46 | /// failures) spends red. |
| 47 | fn status_style(status: &str) -> Style { |
| 48 | match status { |
| 49 | "queued" | "running" | "waiting" | "pending" => { |
| 50 | Style::default().fg(palette::STATUS_WARNING) |
| 51 | } |
| 52 | "completed" | "succeeded" => Style::default().fg(palette::STATUS_SUCCESS), |
| 53 | "degraded" => Style::default().fg(palette::STATUS_WARNING), |
| 54 | "failed" | "budget_exceeded" => Style::default().fg(palette::STATUS_ERROR), |
| 55 | _ => Style::default().fg(palette::TEXT_MUTED), |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// One glyph per projected child state. Only a real failure reads as `✗`: |
| 60 | /// a cancellation is not a failure, and a state this view does not know |
| 61 | /// must not borrow the failure mark (U09-m3). |
| 62 | fn child_state_glyph(state: &str) -> &'static str { |
| 63 | match state { |
| 64 | "running" | "pending" => "•", |
| 65 | "succeeded" => "✓", |
| 66 | "failed" | "budget_exceeded" => "✗", |
| 67 | "cancelled" => "–", |
| 68 | _ => "?", |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// A child with no completion receipt in a settled run did not finish as far |
| 73 | /// as this record shows; it is not still running (U09-m3). |
| 74 | fn child_display_state(child: &HostWorkflowChildRow, run_active: bool) -> &'static str { |
| 75 | if !run_active && matches!(child.state, "running" | "pending") { |
| 76 | "unknown" |
| 77 | } else { |
| 78 | child.state |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Open `/workflows` unless it is already on top. `/workflows`, the |
| 83 | /// workbar's `↓ to manage`, and a click on a workbar row all land here. |
| 84 | pub(crate) fn open(app: &mut App) { |
| 85 | if app.view_stack.top_kind() != Some(ModalKind::WorkflowsManager) { |
| 86 | let view = WorkflowsManagerView::new(app); |
| 87 | app.view_stack.push(view); |
| 88 | } |
| 89 | app.needs_redraw = true; |
| 90 | } |
| 91 | |
| 92 | pub struct WorkflowsManagerView { |
| 93 | /// Newest first, so the live run is the first thing read. |
| 94 | runs: Vec<HostWorkflowRunDetail>, |
| 95 | row: usize, |
| 96 | detail_open: bool, |
| 97 | detail_scroll: usize, |
| 98 | /// Largest useful detail scroll in rendered rows, from the last paint. |
| 99 | detail_max_scroll: Cell<usize>, |
| 100 | /// Receipt line for the last host action (cancel), shown under the header. |
| 101 | status: Option<String>, |
| 102 | workspace: PathBuf, |
| 103 | owner_session_id: Option<String>, |
| 104 | /// The manager follows owner-state changes while open without polling the |
| 105 | /// journal on every terminal frame. |
| 106 | last_refresh_at: Instant, |
| 107 | /// Screen rect of the run list body, recorded at render for mouse parity. |
| 108 | list_body: Cell<Rect>, |
| 109 | } |
| 110 | |
| 111 | impl WorkflowsManagerView { |
| 112 | #[must_use] |
| 113 | pub fn new(app: &App) -> Self { |
| 114 | let mut view = Self { |
| 115 | runs: Vec::new(), |
| 116 | row: 0, |
| 117 | detail_open: false, |
| 118 | detail_scroll: 0, |
| 119 | detail_max_scroll: Cell::new(usize::MAX), |
| 120 | status: None, |
| 121 | workspace: app.workspace.clone(), |
| 122 | owner_session_id: app.current_session_id.clone(), |
| 123 | last_refresh_at: Instant::now(), |
| 124 | list_body: Cell::new(Rect::ZERO), |
| 125 | }; |
| 126 | view.refresh(); |
| 127 | view |
| 128 | } |
| 129 | |
| 130 | /// Re-read the journal (newest first), preserving the selected run id when |
| 131 | /// a newer run arrives rather than silently moving focus to a different |
| 132 | /// row. |
| 133 | fn apply_runs(&mut self, runs: Vec<HostWorkflowRunDetail>) { |
| 134 | let selected_id = self.selected().map(|detail| detail.line.run_id.clone()); |
| 135 | self.runs = runs; |
| 136 | let preserved = selected_id.as_deref().and_then(|run_id| { |
| 137 | self.runs |
| 138 | .iter() |
| 139 | .position(|detail| detail.line.run_id == run_id) |
| 140 | }); |
| 141 | if let Some(index) = preserved { |
| 142 | self.row = index; |
| 143 | } else { |
| 144 | self.row = self.row.min(self.runs.len().saturating_sub(1)); |
| 145 | self.detail_scroll = 0; |
| 146 | } |
| 147 | if self.runs.is_empty() { |
| 148 | self.detail_open = false; |
| 149 | self.detail_scroll = 0; |
| 150 | } |
| 151 | self.last_refresh_at = Instant::now(); |
| 152 | } |
| 153 | |
| 154 | fn refresh(&mut self) { |
| 155 | let runs = host_workflow_run_details(&self.workspace, self.owner_session_id.as_deref()) |
| 156 | .into_iter() |
| 157 | .rev() |
| 158 | .collect(); |
| 159 | self.apply_runs(runs); |
| 160 | } |
| 161 | |
| 162 | fn selected(&self) -> Option<&HostWorkflowRunDetail> { |
| 163 | self.runs.get(self.row) |
| 164 | } |
| 165 | |
| 166 | fn move_row(&mut self, delta: isize) { |
| 167 | let rows = self.runs.len(); |
| 168 | if rows == 0 { |
| 169 | return; |
| 170 | } |
| 171 | self.row = wrap_index(self.row, rows, delta); |
| 172 | } |
| 173 | |
| 174 | /// Cancel the selected run through the host path — the same one |
| 175 | /// `/workflow cancel <run_id>` takes, so receipts and journal state are |
| 176 | /// identical. No model turn. |
| 177 | fn cancel_selected(&mut self) { |
| 178 | let Some(detail) = self.selected() else { |
| 179 | return; |
| 180 | }; |
| 181 | if !detail.line.active { |
| 182 | self.status = Some(format!( |
| 183 | "Run {} already {} — nothing to cancel.", |
| 184 | detail.line.run_id, detail.line.status |
| 185 | )); |
| 186 | return; |
| 187 | } |
| 188 | let run_id = detail.line.run_id.clone(); |
| 189 | match host_cancel_workflow(&self.workspace, &run_id, self.owner_session_id.as_deref()) { |
| 190 | Ok(line) => { |
| 191 | self.status = Some(format!( |
| 192 | "Workflow {} {} · {}", |
| 193 | line.run_id, line.status, line.label |
| 194 | )); |
| 195 | } |
| 196 | Err(reason) => { |
| 197 | self.status = Some(format!("Cancel failed: {reason}")); |
| 198 | } |
| 199 | } |
| 200 | self.refresh(); |
| 201 | } |
| 202 | |
| 203 | fn footer_hints(&self) -> Vec<ActionHint> { |
| 204 | let mut hints = vec![ActionHint::new("↑/↓", "move")]; |
| 205 | if self.detail_open { |
| 206 | hints.push(ActionHint::new("←", "runs")); |
| 207 | } else { |
| 208 | hints.push(ActionHint::new("Enter", "detail")); |
| 209 | } |
| 210 | if self.selected().is_some_and(|d| d.line.active) { |
| 211 | hints.push(ActionHint::new("x", "cancel")); |
| 212 | } |
| 213 | hints.push(ActionHint::new("r", "refresh")); |
| 214 | hints.push(ActionHint::new("Esc", "close")); |
| 215 | hints |
| 216 | } |
| 217 | |
| 218 | fn header_lines(&self) -> Vec<Line<'static>> { |
| 219 | let active = self.runs.iter().filter(|d| d.line.active).count(); |
| 220 | let finished = self.runs.len() - active; |
| 221 | let mut header = vec![ |
| 222 | Line::from(vec![ |
| 223 | Span::styled( |
| 224 | "─ Workflow runs ", |
| 225 | Style::default().fg(palette::WHALE_ACTION).bold(), |
| 226 | ), |
| 227 | Span::styled( |
| 228 | format!("· {active} active · {finished} finished"), |
| 229 | Style::default().fg(palette::TEXT_MUTED), |
| 230 | ), |
| 231 | ]), |
| 232 | Line::from(""), |
| 233 | ]; |
| 234 | if let Some(status) = &self.status { |
| 235 | header.push(Line::from(Span::styled( |
| 236 | format!(" {status}"), |
| 237 | Style::default().fg(palette::WHALE_HUMAN), |
| 238 | ))); |
| 239 | } |
| 240 | header |
| 241 | } |
| 242 | |
| 243 | fn render_list(&self, area: Rect, buf: &mut Buffer) { |
| 244 | self.list_body.set(area); |
| 245 | if self.runs.is_empty() { |
| 246 | Paragraph::new(Line::from(vec![ |
| 247 | Span::styled( |
| 248 | " No workflow runs in this workspace yet.", |
| 249 | Style::default().fg(palette::TEXT_MUTED), |
| 250 | ), |
| 251 | Span::styled( |
| 252 | " /workflow <objective> starts one.", |
| 253 | Style::default().fg(palette::TEXT_DIM), |
| 254 | ), |
| 255 | ])) |
| 256 | .render(area, buf); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | let now = now_ms(); |
| 261 | let rows_visible = usize::from(area.height).max(1); |
| 262 | let scroll = self.row.saturating_sub(rows_visible.saturating_sub(1)); |
| 263 | for (idx, detail) in self.runs.iter().enumerate() { |
| 264 | if idx < scroll || idx >= scroll + rows_visible { |
| 265 | continue; |
| 266 | } |
| 267 | let y = area.y + u16::try_from(idx - scroll).unwrap_or(u16::MAX); |
| 268 | if y >= area.y + area.height { |
| 269 | break; |
| 270 | } |
| 271 | let selected = idx == self.row; |
| 272 | let row_rect = Rect { |
| 273 | x: area.x, |
| 274 | y, |
| 275 | width: area.width, |
| 276 | height: 1, |
| 277 | }; |
| 278 | let elapsed = detail |
| 279 | .line |
| 280 | .completed_at_ms |
| 281 | .unwrap_or(now) |
| 282 | .saturating_sub(detail.line.started_at_ms) |
| 283 | / 1000; |
| 284 | let marker = if selected { "▸ " } else { " " }; |
| 285 | let base = if selected { |
| 286 | Style::default().fg(palette::WHALE_ACTION).bold() |
| 287 | } else { |
| 288 | Style::default().fg(palette::TEXT_SECONDARY) |
| 289 | }; |
| 290 | let mut spans = vec![ |
| 291 | Span::styled(marker, base), |
| 292 | Span::styled( |
| 293 | format!("{:<9}", detail.line.status), |
| 294 | status_style(detail.line.status).add_modifier(if selected { |
| 295 | Modifier::BOLD |
| 296 | } else { |
| 297 | Modifier::empty() |
| 298 | }), |
| 299 | ), |
| 300 | Span::styled(detail.line.label.clone(), base), |
| 301 | Span::styled( |
| 302 | format!( |
| 303 | " · {} · {} children", |
| 304 | codewhale_command_contract::elapsed::format_elapsed_secs(elapsed), |
| 305 | detail.line.child_count |
| 306 | ), |
| 307 | Style::default().fg(palette::TEXT_DIM), |
| 308 | ), |
| 309 | ]; |
| 310 | if let Some(progress) = detail.line.last_progress.as_deref() { |
| 311 | spans.push(Span::styled( |
| 312 | format!(" · {progress}"), |
| 313 | Style::default().fg(palette::TEXT_DIM), |
| 314 | )); |
| 315 | } |
| 316 | Line::from(spans).render(row_rect, buf); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | fn detail_lines(&self, detail: &HostWorkflowRunDetail) -> Vec<Line<'static>> { |
| 321 | let now = now_ms(); |
| 322 | let elapsed = detail |
| 323 | .line |
| 324 | .completed_at_ms |
| 325 | .unwrap_or(now) |
| 326 | .saturating_sub(detail.line.started_at_ms) |
| 327 | / 1000; |
| 328 | let mut lines = vec![ |
| 329 | Line::from(vec![ |
| 330 | Span::styled("─ ", Style::default().fg(palette::WHALE_ACTION).bold()), |
| 331 | Span::styled( |
| 332 | detail.line.label.clone(), |
| 333 | Style::default().fg(palette::TEXT_PRIMARY).bold(), |
| 334 | ), |
| 335 | Span::styled( |
| 336 | format!(" · {}", detail.line.status), |
| 337 | status_style(detail.line.status), |
| 338 | ), |
| 339 | ]), |
| 340 | Line::from(Span::styled( |
| 341 | format!( |
| 342 | " run {} · {} · {} children", |
| 343 | detail.line.run_id, |
| 344 | codewhale_command_contract::elapsed::format_elapsed_secs(elapsed), |
| 345 | detail.line.child_count |
| 346 | ), |
| 347 | Style::default().fg(palette::TEXT_DIM), |
| 348 | )), |
| 349 | Line::from(""), |
| 350 | ]; |
| 351 | if !detail.phases.is_empty() { |
| 352 | lines.push(Line::from(Span::styled( |
| 353 | " Phases", |
| 354 | Style::default().fg(palette::TEXT_PRIMARY).bold(), |
| 355 | ))); |
| 356 | lines.push(Line::from(Span::styled( |
| 357 | format!(" {}", detail.phases.join(" → ")), |
| 358 | Style::default().fg(palette::TEXT_SECONDARY), |
| 359 | ))); |
| 360 | lines.push(Line::from("")); |
| 361 | } |
| 362 | if !detail.children.is_empty() { |
| 363 | lines.push(Line::from(Span::styled( |
| 364 | format!(" Children ({})", detail.children.len()), |
| 365 | Style::default().fg(palette::TEXT_PRIMARY).bold(), |
| 366 | ))); |
| 367 | for child in &detail.children { |
| 368 | lines.push(child_row_line(child, detail.line.active)); |
| 369 | } |
| 370 | lines.push(Line::from("")); |
| 371 | } |
| 372 | if !detail.progress_tail.is_empty() { |
| 373 | lines.push(Line::from(Span::styled( |
| 374 | " Recent progress", |
| 375 | Style::default().fg(palette::TEXT_PRIMARY).bold(), |
| 376 | ))); |
| 377 | for progress in &detail.progress_tail { |
| 378 | lines.push(Line::from(Span::styled( |
| 379 | format!(" {progress}"), |
| 380 | Style::default().fg(palette::TEXT_SECONDARY), |
| 381 | ))); |
| 382 | } |
| 383 | lines.push(Line::from("")); |
| 384 | } |
| 385 | if let Some(error) = detail.line.error.as_deref() { |
| 386 | lines.push(Line::from(vec![ |
| 387 | Span::styled(" Error ", Style::default().fg(palette::STATUS_ERROR)), |
| 388 | Span::styled( |
| 389 | error.to_string(), |
| 390 | Style::default().fg(palette::TEXT_SECONDARY), |
| 391 | ), |
| 392 | ])); |
| 393 | } |
| 394 | lines.push(Line::from(Span::styled( |
| 395 | if detail.has_result { |
| 396 | " Result retained in the run journal (.codewhale/workflow-runs.jsonl)." |
| 397 | } else { |
| 398 | " No result recorded yet." |
| 399 | }, |
| 400 | Style::default().fg(palette::TEXT_DIM), |
| 401 | ))); |
| 402 | lines |
| 403 | } |
| 404 | |
| 405 | fn render_detail(&self, area: Rect, buf: &mut Buffer) { |
| 406 | let Some(detail) = self.selected() else { |
| 407 | self.render_list(area, buf); |
| 408 | return; |
| 409 | }; |
| 410 | let lines = self.detail_lines(detail); |
| 411 | self.detail_max_scroll.set(super::render_wrapped_detail( |
| 412 | lines, |
| 413 | area, |
| 414 | buf, |
| 415 | self.detail_scroll, |
| 416 | )); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | fn child_row_line(child: &HostWorkflowChildRow, run_active: bool) -> Line<'static> { |
| 421 | let name = child.label.clone().unwrap_or_else(|| child.task_id.clone()); |
| 422 | let state = child_display_state(child, run_active); |
| 423 | let mut spans = vec![ |
| 424 | Span::styled( |
| 425 | format!(" {} ", child_state_glyph(state)), |
| 426 | status_style(state), |
| 427 | ), |
| 428 | Span::styled(name, Style::default().fg(palette::TEXT_SECONDARY)), |
| 429 | Span::styled(format!(" · {state}"), status_style(state)), |
| 430 | ]; |
| 431 | let mut meta = Vec::new(); |
| 432 | if let Some(role) = child.role.as_deref() { |
| 433 | meta.push(role.to_string()); |
| 434 | } |
| 435 | if let Some(model) = child.model.as_deref() { |
| 436 | meta.push(model.to_string()); |
| 437 | } |
| 438 | if let Some(phase) = child.phase.as_deref() { |
| 439 | meta.push(phase.to_string()); |
| 440 | } |
| 441 | if !meta.is_empty() { |
| 442 | spans.push(Span::styled( |
| 443 | format!(" · {}", meta.join(" · ")), |
| 444 | Style::default().fg(palette::TEXT_DIM), |
| 445 | )); |
| 446 | } |
| 447 | Line::from(spans) |
| 448 | } |
| 449 | |
| 450 | impl ModalView for WorkflowsManagerView { |
| 451 | fn kind(&self) -> ModalKind { |
| 452 | ModalKind::WorkflowsManager |
| 453 | } |
| 454 | |
| 455 | fn as_any_mut(&mut self) -> &mut dyn std::any::Any { |
| 456 | self |
| 457 | } |
| 458 | |
| 459 | fn handle_key(&mut self, key: KeyEvent) -> ViewAction { |
| 460 | match key.code { |
| 461 | KeyCode::Esc => { |
| 462 | if self.detail_open { |
| 463 | self.detail_open = false; |
| 464 | self.detail_scroll = 0; |
| 465 | ViewAction::None |
| 466 | } else { |
| 467 | ViewAction::Close |
| 468 | } |
| 469 | } |
| 470 | KeyCode::Char('q') => ViewAction::Close, |
| 471 | KeyCode::Up | KeyCode::Char('k') => { |
| 472 | if self.detail_open { |
| 473 | self.detail_scroll = self.detail_scroll.saturating_sub(1); |
| 474 | } else { |
| 475 | self.move_row(-1); |
| 476 | } |
| 477 | ViewAction::None |
| 478 | } |
| 479 | KeyCode::Down | KeyCode::Char('j') => { |
| 480 | if self.detail_open { |
| 481 | // Stop at the last rendered (wrapped) row of the detail. |
| 482 | self.detail_scroll = self |
| 483 | .detail_scroll |
| 484 | .saturating_add(1) |
| 485 | .min(self.detail_max_scroll.get()); |
| 486 | } else { |
| 487 | self.move_row(1); |
| 488 | } |
| 489 | ViewAction::None |
| 490 | } |
| 491 | KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { |
| 492 | if !self.runs.is_empty() { |
| 493 | self.detail_open = true; |
| 494 | } |
| 495 | ViewAction::None |
| 496 | } |
| 497 | KeyCode::Left | KeyCode::Tab => { |
| 498 | self.detail_open = false; |
| 499 | self.detail_scroll = 0; |
| 500 | ViewAction::None |
| 501 | } |
| 502 | KeyCode::Char('x') | KeyCode::Char('c') => { |
| 503 | self.cancel_selected(); |
| 504 | ViewAction::None |
| 505 | } |
| 506 | KeyCode::Char('r') => { |
| 507 | self.refresh(); |
| 508 | ViewAction::None |
| 509 | } |
| 510 | KeyCode::Char('g') => ViewAction::Close, |
| 511 | _ => ViewAction::None, |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | fn handle_mouse(&mut self, mouse: MouseEvent) -> ViewAction { |
| 516 | if self.detail_open { |
| 517 | match mouse.kind { |
| 518 | MouseEventKind::ScrollUp => { |
| 519 | self.detail_scroll = self.detail_scroll.saturating_sub(1); |
| 520 | } |
| 521 | MouseEventKind::ScrollDown => { |
| 522 | self.detail_scroll = self |
| 523 | .detail_scroll |
| 524 | .saturating_add(1) |
| 525 | .min(self.detail_max_scroll.get()); |
| 526 | } |
| 527 | _ => {} |
| 528 | } |
| 529 | return ViewAction::None; |
| 530 | } |
| 531 | // The wheel moves this list, not the transcript behind it. |
| 532 | match mouse.kind { |
| 533 | MouseEventKind::ScrollUp => { |
| 534 | self.move_row(-1); |
| 535 | return ViewAction::None; |
| 536 | } |
| 537 | MouseEventKind::ScrollDown => { |
| 538 | self.move_row(1); |
| 539 | return ViewAction::None; |
| 540 | } |
| 541 | _ => {} |
| 542 | } |
| 543 | if let MouseEventKind::Down(MouseButton::Left) = mouse.kind { |
| 544 | let body = self.list_body.get(); |
| 545 | if body.width > 0 |
| 546 | && mouse.row >= body.y |
| 547 | && mouse.row < body.y + body.height |
| 548 | && mouse.column >= body.x |
| 549 | && mouse.column < body.x + body.width |
| 550 | { |
| 551 | let offset = usize::from(mouse.row - body.y); |
| 552 | let scroll = self |
| 553 | .row |
| 554 | .saturating_sub(usize::from(body.height).saturating_sub(1)); |
| 555 | let idx = scroll + offset; |
| 556 | if idx < self.runs.len() { |
| 557 | self.row = idx; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | ViewAction::None |
| 562 | } |
| 563 | |
| 564 | fn tick(&mut self) -> ViewAction { |
| 565 | let interval = if self.runs.iter().any(|detail| detail.line.active) { |
| 566 | Duration::from_millis(250) |
| 567 | } else { |
| 568 | Duration::from_secs(2) |
| 569 | }; |
| 570 | if self.last_refresh_at.elapsed() >= interval { |
| 571 | self.refresh(); |
| 572 | } |
| 573 | ViewAction::None |
| 574 | } |
| 575 | |
| 576 | fn render(&self, area: Rect, buf: &mut Buffer) { |
| 577 | let area = super::render_room_body(area, buf); |
| 578 | |
| 579 | let hints = self.footer_hints(); |
| 580 | let content = render_modal_footer(area, buf, &hints); |
| 581 | |
| 582 | let chunks = Layout::default() |
| 583 | .direction(Direction::Vertical) |
| 584 | .constraints([Constraint::Length(4), Constraint::Min(1)]) |
| 585 | .split(content); |
| 586 | |
| 587 | Paragraph::new(self.header_lines()) |
| 588 | .wrap(Wrap { trim: false }) |
| 589 | .render(chunks[0], buf); |
| 590 | |
| 591 | if self.detail_open { |
| 592 | self.render_detail(chunks[1], buf); |
| 593 | } else { |
| 594 | self.render_list(chunks[1], buf); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | #[cfg(test)] |
| 600 | mod tests { |
| 601 | use super::*; |
| 602 | |
| 603 | fn child(state: &'static str) -> HostWorkflowChildRow { |
| 604 | HostWorkflowChildRow { |
| 605 | task_id: "t1".to_string(), |
| 606 | label: None, |
| 607 | role: None, |
| 608 | model: None, |
| 609 | phase: None, |
| 610 | state, |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | /// U09-m3: a child with no completion receipt in a settled run is not |
| 615 | /// "running", a cancellation is not a failure, and an unknown state never |
| 616 | /// borrows the failure mark. |
| 617 | #[test] |
| 618 | fn child_rows_keep_their_source_of_truth_state() { |
| 619 | assert_eq!(child_display_state(&child("running"), true), "running"); |
| 620 | assert_eq!(child_display_state(&child("running"), false), "unknown"); |
| 621 | assert_eq!(child_display_state(&child("succeeded"), false), "succeeded"); |
| 622 | assert_eq!(child_state_glyph("unknown"), "?"); |
| 623 | assert_eq!(child_state_glyph("cancelled"), "–"); |
| 624 | assert_eq!(child_state_glyph("failed"), "✗"); |
| 625 | assert_eq!(child_state_glyph("budget_exceeded"), "✗"); |
| 626 | } |
| 627 | |
| 628 | fn run_detail(run_id: &str) -> HostWorkflowRunDetail { |
| 629 | HostWorkflowRunDetail { |
| 630 | line: codewhale_engine::tools::workflow::HostWorkflowRunLine { |
| 631 | run_id: run_id.to_string(), |
| 632 | label: run_id.to_string(), |
| 633 | status: "completed", |
| 634 | active: false, |
| 635 | started_at_ms: 0, |
| 636 | completed_at_ms: Some(1_000), |
| 637 | child_count: 0, |
| 638 | last_progress: None, |
| 639 | error: None, |
| 640 | }, |
| 641 | phases: Vec::new(), |
| 642 | children: Vec::new(), |
| 643 | progress_tail: Vec::new(), |
| 644 | has_result: false, |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | fn view_with_runs(runs: Vec<HostWorkflowRunDetail>) -> WorkflowsManagerView { |
| 649 | WorkflowsManagerView { |
| 650 | runs, |
| 651 | row: 0, |
| 652 | detail_open: false, |
| 653 | detail_scroll: 0, |
| 654 | detail_max_scroll: Cell::new(3), |
| 655 | status: None, |
| 656 | workspace: PathBuf::from("."), |
| 657 | owner_session_id: None, |
| 658 | last_refresh_at: Instant::now(), |
| 659 | list_body: Cell::new(Rect::ZERO), |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | #[test] |
| 664 | fn refresh_clears_stale_detail_state_when_selected_run_disappears_or_list_empties() { |
| 665 | let mut view = view_with_runs(vec![run_detail("r1"), run_detail("r2")]); |
| 666 | view.detail_open = true; |
| 667 | view.detail_scroll = 2; |
| 668 | |
| 669 | view.apply_runs(vec![run_detail("r2")]); |
| 670 | assert!(view.detail_open); |
| 671 | assert_eq!(view.row, 0); |
| 672 | assert_eq!(view.detail_scroll, 0); |
| 673 | |
| 674 | view.detail_scroll = 2; |
| 675 | view.apply_runs(Vec::new()); |
| 676 | assert!(!view.detail_open); |
| 677 | assert_eq!(view.detail_scroll, 0); |
| 678 | } |
| 679 | |
| 680 | #[test] |
| 681 | fn mouse_wheel_scrolls_detail_when_detail_is_open() { |
| 682 | let mut view = view_with_runs(vec![run_detail("r1"), run_detail("r2")]); |
| 683 | view.detail_open = true; |
| 684 | let wheel = |kind| MouseEvent { |
| 685 | kind, |
| 686 | column: 0, |
| 687 | row: 0, |
| 688 | modifiers: crossterm::event::KeyModifiers::NONE, |
| 689 | }; |
| 690 | for _ in 0..10 { |
| 691 | view.handle_mouse(wheel(MouseEventKind::ScrollDown)); |
| 692 | } |
| 693 | assert_eq!(view.row, 0); |
| 694 | assert_eq!(view.detail_scroll, 3); |
| 695 | view.handle_mouse(wheel(MouseEventKind::ScrollUp)); |
| 696 | assert_eq!(view.detail_scroll, 2); |
| 697 | } |
| 698 | } |
| 699 |