| 1 | //! Ocean Work Graph surface ownership. |
| 2 | //! |
| 3 | //! This is called the "workbar" or the "work surface". Fresh settings default |
| 4 | //! to `Bottom` (round 3, 2026-09-01); `Top`, `Left`, and `Right` remain |
| 5 | //! supported and `Off` hides it. It is not the header |
| 6 | //! ([`crate::tui::underwater`]) and not the footer. |
| 7 | //! |
| 8 | //! Two settings are orthogonal and are routinely mixed up: |
| 9 | //! |
| 10 | //! - **placement** — where it renders. `Bottom` (fresh default) | `Top` | |
| 11 | //! `Left` | `Right` | `Off`. Drag-resizing the divider persists |
| 12 | //! `work_surface_top_height` (5..=16) or `work_surface_side_width` |
| 13 | //! (26..=80) to `settings.toml`. |
| 14 | //! - **panel** — what it shows. [`RailPanel`]: `Tasks` (default) | `Agents` | |
| 15 | //! `Background` | `Terminal` | `Files` | `Notepad` | `Context` | `Git` | `Price`, from |
| 16 | //! the `rail_panel` setting. The legacy `sidebar_focus` key migrates into |
| 17 | //! it. |
| 18 | //! |
| 19 | //! So the word "Pinned" on screen is a PANEL name, not a state. |
| 20 | //! |
| 21 | //! ## Auto-fit by placement |
| 22 | //! |
| 23 | //! Placement changes *which axis is the ceiling*, not the content rule: |
| 24 | //! |
| 25 | //! | Placement | Ceiling | Auto-fit | Empty | |
| 26 | //! |---|---|---|---| |
| 27 | //! | `Top` | `top_height` (rows) | content rows + divider, clamped to ceiling | `height() == 0` | |
| 28 | //! | `Left`/`Right` | `side_width` (cols) | full chat height at that width | no column reserved | |
| 29 | //! | `Off` | — | — | nothing | |
| 30 | //! |
| 31 | //! Shared rules: content drives size; the setting is a ceiling, never padding; |
| 32 | //! empty work is not a rail. Top never paints a chrome panel title (a checklist |
| 33 | //! reads as a checklist); side rails are named by their content's own heading |
| 34 | //! row (`Work · …`, `▾ Subagents N`, `Goal: …`) except Context, which keeps a |
| 35 | //! muted panel title over its fact list. Narrow hosts that cannot fit a side |
| 36 | //! column fall back to Top, where height auto-fit takes over. |
| 37 | //! |
| 38 | //! ## Row lifetime |
| 39 | //! |
| 40 | //! The strip is a standing register of this session's work, not a live-only |
| 41 | //! view. A to-do or sub-agent row appears when the work exists and stays for |
| 42 | //! the rest of the session after it settles — completion is quiet (glyph, |
| 43 | //! tone, frozen receipt), never an eviction, and the active goal title |
| 44 | //! outlives the work under it. Only transient receipts (aggregated file |
| 45 | //! activity, settled operations) expire on the #4688/#4690 lifetimes. |
| 46 | //! Auto-fit and the row budget decide how many rows are *visible* at once; |
| 47 | //! they never decide membership. |
| 48 | //! |
| 49 | //! ## Rows are objects — in every panel |
| 50 | //! |
| 51 | //! Tasks, Agents, and Pinned all render through one row/hitbox pipeline: |
| 52 | //! every visible work row is selectable, hoverable, and clickable, and its |
| 53 | //! primary action opens the row's world (agent transcript / work inspector). |
| 54 | //! Keyboard Enter and mouse click dispatch identically. Context is the one |
| 55 | //! line-list panel; it holds facts, not rows. |
| 56 | //! |
| 57 | //! Height is decided once per frame by [`render::height`]; the row budget it is |
| 58 | //! given comes from `crate::tui::ui::rail_row_budget`, which is its only |
| 59 | //! production caller. |
| 60 | //! |
| 61 | //! Placement, scrolling, selection, and pager ownership remain local to this |
| 62 | //! component. Every visible work row derives from the active-session graph. |
| 63 | |
| 64 | mod input; |
| 65 | mod interaction; |
| 66 | mod model; |
| 67 | mod render; |
| 68 | mod terminal; |
| 69 | mod views; |
| 70 | |
| 71 | pub use input::{cycle_view, enter_agents, handle_key, handle_mouse}; |
| 72 | pub(crate) use interaction::{agent_details_closed, release_focus, select_dock_panel}; |
| 73 | pub use model::{RailPanel, WorkSurfacePlacement, WorkSurfaceState}; |
| 74 | pub(crate) use render::collapse_strip; |
| 75 | pub use render::{height, render, split_chat}; |
| 76 | pub(crate) use terminal::{handle_paste as handle_terminal_paste, poll as poll_terminal}; |
| 77 | |
| 78 | #[cfg(test)] |
| 79 | mod tests { |
| 80 | use super::WorkSurfacePlacement; |
| 81 | use std::path::PathBuf; |
| 82 | use std::time::Instant; |
| 83 | |
| 84 | use crossterm::event::{ |
| 85 | KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind, |
| 86 | }; |
| 87 | use ratatui::{Terminal, backend::TestBackend}; |
| 88 | |
| 89 | use crate::tui::app::{ |
| 90 | AgentCurrentActivity, AgentCurrentActivityStatus, App, SidebarRowAction, ToolDetailRecord, |
| 91 | TuiOptions, |
| 92 | }; |
| 93 | use crate::tui::golden_harness::assert_matches_golden; |
| 94 | use crate::tui::history::{ |
| 95 | FileMutationReceipt, GenericToolCell, HistoryCell, PatchSummaryCell, ToolCell, ToolStatus, |
| 96 | }; |
| 97 | use codewhale_agent_contract::role::FleetRole; |
| 98 | use codewhale_config::ProviderKind; |
| 99 | use codewhale_engine::tools::subagent::{ |
| 100 | AgentWorkerStatus, MailboxMessage, SubAgentAssignment, SubAgentResult, SubAgentStatus, |
| 101 | }; |
| 102 | use codewhale_engine::tools::todo::TodoStatus; |
| 103 | use codewhale_engine::work_graph::{ |
| 104 | AcceptanceRequirement, ChangeCtx, EdgeKind, EvidenceKindTag, NodeKind, NodeState, |
| 105 | OperationBinding, OperationOwnerSnapshot, OwnerState, Provenance, WorkEdge, WorkEdgeId, |
| 106 | WorkGraph, WorkGraphChange, WorkNode, WorkNodeId, |
| 107 | }; |
| 108 | use codewhale_runtime::config::Config; |
| 109 | |
| 110 | const SESSION: &str = "work-surface-test"; |
| 111 | |
| 112 | fn app() -> App { |
| 113 | let options = TuiOptions { |
| 114 | use_mouse_capture: true, |
| 115 | max_subagents: 4, |
| 116 | ..crate::tui::test_support::test_tui_options(PathBuf::from(".")) |
| 117 | }; |
| 118 | let mut app = App::new(options, &Config::default()); |
| 119 | app.ui_locale = codewhale_localization::Locale::En; |
| 120 | // Dogfood guard: App::new reads the developer's real settings.toml, |
| 121 | // and the 0.9.4 migration maps a legacy sidebar_focus onto the rail |
| 122 | // panel. These tests exercise the Tasks panel's row machinery, so |
| 123 | // pin it rather than depend on the host file. |
| 124 | app.work_surface.panel = super::RailPanel::Tasks; |
| 125 | // Not an explicit pick: the auto rule opens the agents view when a |
| 126 | // fixture caches a running worker, exactly as the product does. |
| 127 | app.work_surface.explicit_view = false; |
| 128 | // Most tests in this module predate the fresh left-rail default and |
| 129 | // exercise the Top strip's height, divider, overflow, and row layout. |
| 130 | // Pin both requested and effective placement; dedicated placement |
| 131 | // tests override these fields explicitly. |
| 132 | app.work_surface.placement = WorkSurfacePlacement::Top; |
| 133 | app.work_surface.effective_placement = WorkSurfacePlacement::Top; |
| 134 | app |
| 135 | } |
| 136 | |
| 137 | /// The row budget `ui::render` would hand the rail on a terminal of this |
| 138 | /// height with real work on screen. Calls the production formula rather |
| 139 | /// than restating it, so a change to the chrome accounting shows up here |
| 140 | /// instead of silently diverging. The idle-empty budget (where the |
| 141 | /// ambient floor bites) is covered end-to-end in `ui::tests`. |
| 142 | fn working_budget(app: &App, terminal_height: u16) -> u16 { |
| 143 | crate::tui::ui::rail_row_budget(app, 80, terminal_height, false) |
| 144 | } |
| 145 | |
| 146 | /// A budget wide enough never to bind, for tests about something else. |
| 147 | const AMPLE_BUDGET: u16 = u16::MAX; |
| 148 | |
| 149 | fn add_todos(app: &mut App, count: usize) { |
| 150 | let mut todos = app.todos.try_lock().expect("todos"); |
| 151 | for index in 0..count { |
| 152 | todos.add( |
| 153 | format!("work item {index}"), |
| 154 | if index == 0 { |
| 155 | TodoStatus::InProgress |
| 156 | } else { |
| 157 | TodoStatus::Pending |
| 158 | }, |
| 159 | ); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | fn operation_graph(state: NodeState) -> codewhale_engine::work_graph::WorkGraphSnapshot { |
| 164 | let objective = WorkNodeId::derive(SESSION, "objective"); |
| 165 | let operation = WorkNodeId::derive(SESSION, "operation"); |
| 166 | let ctx = |now| ChangeCtx { |
| 167 | session_id: SESSION.to_string(), |
| 168 | now, |
| 169 | idempotency_key: None, |
| 170 | }; |
| 171 | let node = |id: WorkNodeId, kind, title: &str, now| WorkNode { |
| 172 | id, |
| 173 | kind, |
| 174 | title: title.to_string(), |
| 175 | state: NodeState::Ready, |
| 176 | acceptance: Vec::new(), |
| 177 | binding: None, |
| 178 | evidence: None, |
| 179 | provenance: Provenance::RuntimeReconcile { |
| 180 | source: "test-owner".to_string(), |
| 181 | observed_at: now, |
| 182 | }, |
| 183 | created_at: now, |
| 184 | updated_at: now, |
| 185 | }; |
| 186 | let mut graph = WorkGraph::new(); |
| 187 | graph |
| 188 | .apply( |
| 189 | WorkGraphChange::AddNode { |
| 190 | node: node(objective.clone(), NodeKind::Objective, "Ship v0.9.1", 1), |
| 191 | }, |
| 192 | ctx(1), |
| 193 | ) |
| 194 | .expect("objective"); |
| 195 | graph |
| 196 | .apply( |
| 197 | WorkGraphChange::AddNode { |
| 198 | node: node( |
| 199 | operation.clone(), |
| 200 | NodeKind::Operation, |
| 201 | "Verify installed build", |
| 202 | 2, |
| 203 | ), |
| 204 | }, |
| 205 | ctx(2), |
| 206 | ) |
| 207 | .expect("operation"); |
| 208 | graph |
| 209 | .apply( |
| 210 | WorkGraphChange::AddEdge { |
| 211 | edge: WorkEdge { |
| 212 | id: WorkEdgeId::derive(SESSION, "contains"), |
| 213 | kind: EdgeKind::Contains, |
| 214 | from: objective, |
| 215 | to: operation.clone(), |
| 216 | }, |
| 217 | }, |
| 218 | ctx(3), |
| 219 | ) |
| 220 | .expect("contains"); |
| 221 | graph |
| 222 | .apply( |
| 223 | WorkGraphChange::BindOperation { |
| 224 | node: operation.clone(), |
| 225 | binding: OperationBinding { |
| 226 | external: "shell:shell_1234abcd".to_string(), |
| 227 | durable: false, |
| 228 | last_observation: None, |
| 229 | }, |
| 230 | }, |
| 231 | ctx(4), |
| 232 | ) |
| 233 | .expect("binding"); |
| 234 | if state != NodeState::Ready { |
| 235 | graph |
| 236 | .apply( |
| 237 | WorkGraphChange::UpdateNode { |
| 238 | id: operation, |
| 239 | patch: codewhale_engine::work_graph::WorkNodePatch { |
| 240 | state: Some(state), |
| 241 | ..codewhale_engine::work_graph::WorkNodePatch::default() |
| 242 | }, |
| 243 | }, |
| 244 | ctx(5), |
| 245 | ) |
| 246 | .expect("state"); |
| 247 | } |
| 248 | graph.into_snapshot() |
| 249 | } |
| 250 | |
| 251 | fn restore_graph(app: &mut App, graph: &codewhale_engine::work_graph::WorkGraphSnapshot) { |
| 252 | app.current_session_id = Some(SESSION.to_string()); |
| 253 | app.runtime_services |
| 254 | .work |
| 255 | .as_ref() |
| 256 | .expect("Work Graph runtime") |
| 257 | .restore( |
| 258 | SESSION, |
| 259 | Some(graph), |
| 260 | &codewhale_engine::work_graph::project_todos(graph), |
| 261 | &codewhale_engine::work_graph::project_plan(graph), |
| 262 | ) |
| 263 | .expect("restore graph"); |
| 264 | } |
| 265 | |
| 266 | fn restore_saved_graph(app: &mut App, graph: &codewhale_engine::work_graph::WorkGraphSnapshot) { |
| 267 | app.current_session_id = Some(SESSION.to_string()); |
| 268 | let state = codewhale_engine::session_manager::SessionWorkState { |
| 269 | graph: Some(graph.clone()), |
| 270 | todos: codewhale_engine::work_graph::project_todos(graph), |
| 271 | plan: codewhale_engine::work_graph::project_plan(graph), |
| 272 | }; |
| 273 | app.restore_work_state(SESSION, std::path::Path::new("."), Some(&state)) |
| 274 | .expect("restore saved graph"); |
| 275 | } |
| 276 | |
| 277 | fn render_text(app: &mut App, width: u16, height: u16) -> String { |
| 278 | let backend = TestBackend::new(width, height); |
| 279 | let mut terminal = Terminal::new(backend).expect("terminal"); |
| 280 | terminal |
| 281 | .draw(|frame| super::render(frame, frame.area(), app)) |
| 282 | .expect("draw"); |
| 283 | terminal |
| 284 | .backend() |
| 285 | .buffer() |
| 286 | .content() |
| 287 | .iter() |
| 288 | .map(|cell| cell.symbol()) |
| 289 | .collect() |
| 290 | } |
| 291 | |
| 292 | fn render_golden_text(app: &mut App, width: u16, height: u16) -> String { |
| 293 | let backend = TestBackend::new(width, height); |
| 294 | let mut terminal = Terminal::new(backend).expect("terminal"); |
| 295 | terminal |
| 296 | .draw(|frame| super::render(frame, frame.area(), app)) |
| 297 | .expect("draw"); |
| 298 | format!("{}\n", terminal_text(&terminal)) |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn scheduled_automations_do_not_create_background_work() { |
| 303 | let mut app = crate::tui::test_support::test_app_with_options( |
| 304 | crate::tui::test_support::test_tui_options("."), |
| 305 | ); |
| 306 | app.automation_panel.active_automations = 2; |
| 307 | assert!(!super::model::background_has_live_work(&mut app)); |
| 308 | } |
| 309 | |
| 310 | #[test] |
| 311 | fn projection_keeps_every_legacy_todo_as_a_graph_row() { |
| 312 | let mut app = app(); |
| 313 | add_todos(&mut app, 4); |
| 314 | |
| 315 | let rows = super::model::project(&mut app); |
| 316 | |
| 317 | assert!( |
| 318 | rows[0].label.starts_with("Work · Running:") |
| 319 | || rows[0] |
| 320 | .label |
| 321 | .starts_with("Work · 1 active · 0 needs input · 3 ready"), |
| 322 | "unexpected heading {}", |
| 323 | rows[0].label |
| 324 | ); |
| 325 | for index in 0..4 { |
| 326 | assert!( |
| 327 | rows.iter() |
| 328 | .any(|row| row.label == format!("work item {index}")) |
| 329 | ); |
| 330 | } |
| 331 | assert!(rows.iter().all(|row| !row.id.0.starts_with("todo:"))); |
| 332 | } |
| 333 | |
| 334 | #[test] |
| 335 | fn coordination_projection_is_one_selectable_work_row_with_shared_details() { |
| 336 | use codewhale_engine::tools::subagent::CoordinationDetailProjection; |
| 337 | use codewhale_engine::tools::subagent::coord::{ |
| 338 | CoordinationDetailMetrics, DecisionRecord, DecisionStatus, |
| 339 | }; |
| 340 | |
| 341 | let mut app = app(); |
| 342 | app.coordination_detail = Some(CoordinationDetailProjection { |
| 343 | schema_version: 1, |
| 344 | sequence: 7, |
| 345 | decisions: vec![DecisionRecord { |
| 346 | decision_id: "decision-work".to_string(), |
| 347 | subject: "coordination row".to_string(), |
| 348 | status: DecisionStatus::Accepted, |
| 349 | owner: "release-owner".to_string(), |
| 350 | scope: Vec::new(), |
| 351 | constraints: vec!["PRIVATE-TRANSCRIPT-MARKER".to_string()], |
| 352 | evidence_handles: Vec::new(), |
| 353 | version: 2, |
| 354 | sequence: 7, |
| 355 | }], |
| 356 | write_claims: Vec::new(), |
| 357 | reconciliations: Vec::new(), |
| 358 | context_projections: Vec::new(), |
| 359 | contentions: Vec::new(), |
| 360 | metrics: CoordinationDetailMetrics { |
| 361 | hottest_paths: Vec::new(), |
| 362 | package_or_module_growth: None, |
| 363 | route_or_cost: None, |
| 364 | note: "No active claims".to_string(), |
| 365 | }, |
| 366 | bounded: true, |
| 367 | limit: 24, |
| 368 | process_lock_held: true, |
| 369 | process_lock_note: None, |
| 370 | }); |
| 371 | |
| 372 | let rows = super::model::project(&mut app); |
| 373 | assert_eq!( |
| 374 | rows[0].label, |
| 375 | "Work · 0 active · 0 needs input · 0 ready · 1 recent" |
| 376 | ); |
| 377 | let row = rows |
| 378 | .iter() |
| 379 | .find(|row| row.id.0 == "coordination") |
| 380 | .expect("coordination Work row"); |
| 381 | assert_eq!(row.label, "Coordination Work"); |
| 382 | assert_eq!(row.detail, "1 decisions · 0 contentions · 0 reconciled"); |
| 383 | let Some(SidebarRowAction::InspectWork { title, body, .. }) = row.primary_action.as_ref() |
| 384 | else { |
| 385 | panic!("coordination row must open the shared Work inspector"); |
| 386 | }; |
| 387 | assert_eq!(title, "Coordination Work"); |
| 388 | assert!(body.contains("decision-work · coordination row"), "{body}"); |
| 389 | assert!( |
| 390 | body.contains("status accepted · owner release-owner · version 2"), |
| 391 | "{body}" |
| 392 | ); |
| 393 | assert!(!body.contains("PRIVATE-TRANSCRIPT-MARKER"), "{body}"); |
| 394 | |
| 395 | app.work_surface.placement = WorkSurfacePlacement::Right; |
| 396 | app.work_surface.effective_placement = WorkSurfacePlacement::Right; |
| 397 | let narrow = render_text(&mut app, 32, 4); |
| 398 | assert!(narrow.contains("Coordination Work"), "{narrow}"); |
| 399 | let _ = super::handle_key( |
| 400 | &mut app, |
| 401 | KeyEvent::new(KeyCode::Char('w'), KeyModifiers::ALT), |
| 402 | ); |
| 403 | let action = super::handle_key(&mut app, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)) |
| 404 | .expect("Work surface handled Enter") |
| 405 | .expect("coordination inspector action"); |
| 406 | assert!(matches!(action, SidebarRowAction::InspectWork { .. })); |
| 407 | } |
| 408 | |
| 409 | #[test] |
| 410 | fn empty_coordination_projection_does_not_create_work_chrome() { |
| 411 | use codewhale_engine::tools::subagent::CoordinationDetailProjection; |
| 412 | use codewhale_engine::tools::subagent::coord::{ |
| 413 | ContextProjectionReceipt, CoordinationDetailMetrics, |
| 414 | }; |
| 415 | |
| 416 | let mut app = app(); |
| 417 | app.coordination_detail = Some(CoordinationDetailProjection { |
| 418 | schema_version: 1, |
| 419 | sequence: 3, |
| 420 | decisions: Vec::new(), |
| 421 | write_claims: Vec::new(), |
| 422 | reconciliations: Vec::new(), |
| 423 | context_projections: ["agent-a", "agent-b", "agent-c"] |
| 424 | .into_iter() |
| 425 | .enumerate() |
| 426 | .map(|(index, child_id)| ContextProjectionReceipt { |
| 427 | child_id: child_id.to_string(), |
| 428 | decision_ids: Vec::new(), |
| 429 | projected_bytes: 0, |
| 430 | deduplicated: 0, |
| 431 | omitted: 0, |
| 432 | sequence: u64::try_from(index + 1).expect("small fixture sequence"), |
| 433 | }) |
| 434 | .collect(), |
| 435 | contentions: Vec::new(), |
| 436 | metrics: CoordinationDetailMetrics { |
| 437 | hottest_paths: Vec::new(), |
| 438 | package_or_module_growth: None, |
| 439 | route_or_cost: None, |
| 440 | note: "growth and route/cost stay null when the coordination ledger has no authoritative source".to_string(), |
| 441 | }, |
| 442 | bounded: true, |
| 443 | limit: 24, |
| 444 | process_lock_held: true, |
| 445 | process_lock_note: None, |
| 446 | }); |
| 447 | |
| 448 | let rows = super::model::project(&mut app); |
| 449 | assert!( |
| 450 | rows.is_empty(), |
| 451 | "zero-byte, no-decision coordination receipts must not create Work chrome: {rows:?}" |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | #[test] |
| 456 | fn nonempty_context_projection_remains_inspectable_work() { |
| 457 | use codewhale_engine::tools::subagent::CoordinationDetailProjection; |
| 458 | use codewhale_engine::tools::subagent::coord::{ |
| 459 | ContextProjectionReceipt, CoordinationDetailMetrics, |
| 460 | }; |
| 461 | |
| 462 | let mut app = app(); |
| 463 | app.coordination_detail = Some(CoordinationDetailProjection { |
| 464 | schema_version: 1, |
| 465 | sequence: 1, |
| 466 | decisions: Vec::new(), |
| 467 | write_claims: Vec::new(), |
| 468 | reconciliations: Vec::new(), |
| 469 | context_projections: vec![ContextProjectionReceipt { |
| 470 | child_id: "agent-a".to_string(), |
| 471 | decision_ids: vec!["decision-a".to_string()], |
| 472 | projected_bytes: 32, |
| 473 | deduplicated: 0, |
| 474 | omitted: 0, |
| 475 | sequence: 1, |
| 476 | }], |
| 477 | contentions: Vec::new(), |
| 478 | metrics: CoordinationDetailMetrics { |
| 479 | hottest_paths: Vec::new(), |
| 480 | package_or_module_growth: None, |
| 481 | route_or_cost: None, |
| 482 | note: String::new(), |
| 483 | }, |
| 484 | bounded: true, |
| 485 | limit: 24, |
| 486 | process_lock_held: true, |
| 487 | process_lock_note: None, |
| 488 | }); |
| 489 | |
| 490 | let rows = super::model::project(&mut app); |
| 491 | assert!( |
| 492 | rows.iter().any(|row| row.id.0 == "coordination"), |
| 493 | "non-empty context projection must remain inspectable: {rows:?}" |
| 494 | ); |
| 495 | } |
| 496 | |
| 497 | #[test] |
| 498 | fn current_blocked_contention_uses_attention_bucket_mark_and_tone() { |
| 499 | use codewhale_engine::tools::subagent::CoordinationDetailProjection; |
| 500 | use codewhale_engine::tools::subagent::coord::{ |
| 501 | CoordinationDetailMetrics, PersistedWriteClaim, WriteContentionDisposition, |
| 502 | WriteContentionReceipt, WriteScopeClaim, |
| 503 | }; |
| 504 | |
| 505 | let mut app = app(); |
| 506 | app.coordination_detail = Some(CoordinationDetailProjection { |
| 507 | schema_version: 1, |
| 508 | sequence: 2, |
| 509 | decisions: Vec::new(), |
| 510 | write_claims: vec![PersistedWriteClaim { |
| 511 | claim: WriteScopeClaim { |
| 512 | owner: "worker-a".to_string(), |
| 513 | roots: vec!["crates/tui".to_string()], |
| 514 | exact_files: Vec::new(), |
| 515 | contracts: vec!["ui-contract".to_string()], |
| 516 | }, |
| 517 | sequence: 1, |
| 518 | isolated_worktree: false, |
| 519 | present_at_claim: Vec::new(), |
| 520 | }], |
| 521 | reconciliations: Vec::new(), |
| 522 | context_projections: Vec::new(), |
| 523 | contentions: vec![WriteContentionReceipt { |
| 524 | claimant: "worker-b".to_string(), |
| 525 | conflicting_owner: "worker-a".to_string(), |
| 526 | roots: vec!["crates/tui".to_string()], |
| 527 | exact_files: Vec::new(), |
| 528 | contracts: vec!["ui-contract".to_string()], |
| 529 | disposition: WriteContentionDisposition::BlockedPendingIsolationOrSerialization, |
| 530 | resolution_sequence: None, |
| 531 | sequence: 2, |
| 532 | }], |
| 533 | metrics: CoordinationDetailMetrics { |
| 534 | hottest_paths: Vec::new(), |
| 535 | package_or_module_growth: None, |
| 536 | route_or_cost: None, |
| 537 | note: "No authoritative metric source".to_string(), |
| 538 | }, |
| 539 | bounded: true, |
| 540 | limit: 24, |
| 541 | process_lock_held: true, |
| 542 | process_lock_note: None, |
| 543 | }); |
| 544 | |
| 545 | let rows = super::model::project(&mut app); |
| 546 | assert_eq!( |
| 547 | rows[0].label, |
| 548 | "Work · Needs input: Coordination Work · 1 blocked" |
| 549 | ); |
| 550 | let row = rows |
| 551 | .iter() |
| 552 | .find(|row| row.id.0 == "coordination") |
| 553 | .expect("blocked coordination Work row"); |
| 554 | assert_eq!(row.mark, codewhale_tui_kit::glyphs::ATTENTION); |
| 555 | assert_eq!(row.tone, super::model::WorkTone::Attention); |
| 556 | assert_eq!(row.detail, "0 decisions · 1 contentions · 0 reconciled"); |
| 557 | } |
| 558 | |
| 559 | #[test] |
| 560 | fn todos_share_one_canonical_work_projection_without_a_second_heading() { |
| 561 | let mut app = app(); |
| 562 | { |
| 563 | let mut todos = app.todos.try_lock().expect("todos"); |
| 564 | todos.add("finished".to_string(), TodoStatus::Completed); |
| 565 | todos.add("current".to_string(), TodoStatus::InProgress); |
| 566 | todos.add("next".to_string(), TodoStatus::Pending); |
| 567 | } |
| 568 | |
| 569 | let rows = super::model::project(&mut app); |
| 570 | |
| 571 | assert!( |
| 572 | rows[0].label.starts_with("Work · Running:") |
| 573 | || rows[0].label.starts_with("Work · Ready:"), |
| 574 | "expected actionable title heading, got {}", |
| 575 | rows[0].label |
| 576 | ); |
| 577 | assert_eq!( |
| 578 | rows.iter() |
| 579 | .skip(1) |
| 580 | .map(|row| row.label.as_str()) |
| 581 | .collect::<Vec<_>>(), |
| 582 | ["finished", "current", "next"] |
| 583 | ); |
| 584 | } |
| 585 | |
| 586 | #[test] |
| 587 | fn top_surface_pins_one_progress_receipt_and_numbers_canonical_rows() { |
| 588 | let mut app = app(); |
| 589 | { |
| 590 | let mut todos = app.todos.try_lock().expect("todos"); |
| 591 | todos.add("finished".to_string(), TodoStatus::Completed); |
| 592 | todos.add("current".to_string(), TodoStatus::InProgress); |
| 593 | todos.add("next".to_string(), TodoStatus::Pending); |
| 594 | } |
| 595 | |
| 596 | let text = render_text(&mut app, 80, 7); |
| 597 | let done = format!("1 · {} finished", codewhale_tui_kit::glyphs::DONE); |
| 598 | let current = format!("2 · {} current", codewhale_tui_kit::glyphs::SELECTION); |
| 599 | let next = format!("3 · {} next", codewhale_tui_kit::glyphs::READY); |
| 600 | |
| 601 | assert!(text.contains("To-do · 1/3 · 2 left"), "{text:?}"); |
| 602 | assert_eq!(text.matches("To-do ·").count(), 1, "{text:?}"); |
| 603 | assert!(text.contains(&done), "{text:?}"); |
| 604 | assert!(text.contains(¤t), "{text:?}"); |
| 605 | assert!(text.contains(&next), "{text:?}"); |
| 606 | assert!( |
| 607 | text.find(&done) < text.find(¤t) && text.find(¤t) < text.find(&next), |
| 608 | "canonical order drifted: {text:?}" |
| 609 | ); |
| 610 | assert_eq!(app.work_surface.hitboxes.len(), 3); |
| 611 | assert_eq!(app.work_surface.hitboxes[0].row_y, 2); |
| 612 | } |
| 613 | |
| 614 | #[test] |
| 615 | fn top_strip_auto_fits_step_count_up_to_caps() { |
| 616 | // Two steps need four literal lines, but the readable surface floor |
| 617 | // wins so the same saved size can also seat goal + Agent state. |
| 618 | let mut two_steps = app(); |
| 619 | two_steps.work_surface.top_height = 8; |
| 620 | add_todos(&mut two_steps, 2); |
| 621 | let budget = working_budget(&two_steps, 40); |
| 622 | assert_eq!( |
| 623 | super::height(&mut two_steps, 100, 40, budget), |
| 624 | super::model::TOP_HEIGHT_MIN |
| 625 | ); |
| 626 | |
| 627 | // Ten steps: content wants 12 lines, the default 8-line cap wins. |
| 628 | let mut ten_steps = app(); |
| 629 | ten_steps.work_surface.top_height = 8; |
| 630 | add_todos(&mut ten_steps, 10); |
| 631 | let budget = working_budget(&ten_steps, 40); |
| 632 | assert_eq!(super::height(&mut ten_steps, 100, 40, budget), 8); |
| 633 | |
| 634 | // Short terminal: the transcript's spare rows beat both content and |
| 635 | // the configured cap. A 12-row terminal spends 1 on the header, 1 on |
| 636 | // the phase strip and 3 on the bordered composer, and owes the |
| 637 | // transcript its 3-row floor — so only 4 rows are spare. That is below |
| 638 | // the readable floor, so the whole rail yields rather than painting a |
| 639 | // divider over clipped work. |
| 640 | let mut short_terminal = app(); |
| 641 | short_terminal.work_surface.top_height = 8; |
| 642 | add_todos(&mut short_terminal, 10); |
| 643 | let budget = working_budget(&short_terminal, 12); |
| 644 | assert_eq!(super::height(&mut short_terminal, 100, 12, budget), 0); |
| 645 | |
| 646 | // Nothing to show: no strip at all. |
| 647 | let mut empty = app(); |
| 648 | empty.work_surface.top_height = 8; |
| 649 | assert_eq!(super::height(&mut empty, 100, 40, AMPLE_BUDGET), 0); |
| 650 | } |
| 651 | |
| 652 | /// A strip that reports zero rows is not on screen, so the interaction |
| 653 | /// state describing it must go with it. Stale hitboxes outlive the rows |
| 654 | /// they described: the transcript rows that replaced the strip would keep |
| 655 | /// routing clicks into a panel that is not there. |
| 656 | #[test] |
| 657 | fn a_yielded_strip_drops_its_interaction_state() { |
| 658 | // Each case is a distinct zero-return inside `height`, and every one |
| 659 | // of them has to tear down. `starve` turns a rendered strip into a |
| 660 | // yielded one; the assertions are identical either way. The first two |
| 661 | // are the returns this yield rule introduced — the ones that had no |
| 662 | // teardown at all. |
| 663 | type Starve = fn(&mut App) -> (u16, u16, u16); |
| 664 | let cases: [(&str, Starve); 3] = [ |
| 665 | ("budget starves the Tasks strip", |_app| (100, 40, 0)), |
| 666 | ("budget starves a switched-to panel", |app| { |
| 667 | app.work_surface.panel = super::RailPanel::Tasks; |
| 668 | (100, 40, 0) |
| 669 | }), |
| 670 | ("placement off", |app| { |
| 671 | app.work_surface.placement = WorkSurfacePlacement::Off; |
| 672 | (100, 40, AMPLE_BUDGET) |
| 673 | }), |
| 674 | ]; |
| 675 | |
| 676 | for (label, starve) in cases { |
| 677 | let mut app = app(); |
| 678 | app.work_surface.placement = WorkSurfacePlacement::Top; |
| 679 | // `app()` reads the developer's real settings.toml. Pin the height |
| 680 | // too, or the strip this test renders to earn its hitboxes depends |
| 681 | // on whoever runs the suite. |
| 682 | app.work_surface.top_height = 8; |
| 683 | add_todos(&mut app, 4); |
| 684 | |
| 685 | // Earn a real strip, so the hitboxes under test are the ones the |
| 686 | // renderer actually produces rather than a fixture's guess. |
| 687 | render_text(&mut app, 100, 12); |
| 688 | assert!( |
| 689 | !app.work_surface.hitboxes.is_empty(), |
| 690 | "{label}: setup never rendered a strip to tear down" |
| 691 | ); |
| 692 | app.work_surface.focused = true; |
| 693 | app.work_surface.resizing = true; |
| 694 | app.work_surface.divider_hovered = true; |
| 695 | |
| 696 | let (width, height, budget) = starve(&mut app); |
| 697 | assert_eq!( |
| 698 | super::height(&mut app, width, height, budget), |
| 699 | 0, |
| 700 | "{label}: expected the strip to yield" |
| 701 | ); |
| 702 | assert!( |
| 703 | app.work_surface.hitboxes.is_empty(), |
| 704 | "{label}: left {} stale hitboxes behind", |
| 705 | app.work_surface.hitboxes.len() |
| 706 | ); |
| 707 | assert!( |
| 708 | app.work_surface.last_area.is_none(), |
| 709 | "{label}: stale last_area" |
| 710 | ); |
| 711 | assert!(!app.work_surface.focused, "{label}: focus survived"); |
| 712 | assert!(!app.work_surface.resizing, "{label}: resize drag survived"); |
| 713 | assert!( |
| 714 | !app.work_surface.divider_hovered, |
| 715 | "{label}: divider hover survived" |
| 716 | ); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /// `top_height` is a ceiling, not a fixed size. The compact floor must |
| 721 | /// still seat the goal, work progress, and actionable rows; content longer |
| 722 | /// than the ceiling is clamped rather than padded with blank water. |
| 723 | #[test] |
| 724 | fn a_short_top_height_caps_content_rather_than_collapsing() { |
| 725 | let mut capped = app(); |
| 726 | capped.work_surface.placement = WorkSurfacePlacement::Top; |
| 727 | capped.work_surface.panel = super::RailPanel::Tasks; |
| 728 | capped.work_surface.top_height = super::model::TOP_HEIGHT_MIN; |
| 729 | capped.composer_border = true; |
| 730 | // Goal + several checklist rows: content wants more than the readable |
| 731 | // floor, so the cap wins without hiding every actionable row. |
| 732 | capped.goal.objective = Some("ship the release".to_string()); |
| 733 | add_todos(&mut capped, 6); |
| 734 | let budget = working_budget(&capped, 40); |
| 735 | assert_eq!( |
| 736 | super::height(&mut capped, 100, 40, budget), |
| 737 | super::model::TOP_HEIGHT_MIN, |
| 738 | "short top_height is a cap the strip must fit under, not a cliff" |
| 739 | ); |
| 740 | |
| 741 | // Content shorter than the cap shrinks to the readable floor rather |
| 742 | // than padding all the way out to the saved 8-row cap. |
| 743 | let mut short = app(); |
| 744 | short.work_surface.placement = WorkSurfacePlacement::Top; |
| 745 | short.work_surface.panel = super::RailPanel::Tasks; |
| 746 | short.work_surface.top_height = 8; |
| 747 | short.goal.objective = Some("one goal only".to_string()); |
| 748 | let budget = working_budget(&short, 40); |
| 749 | let h = super::height(&mut short, 100, 40, budget); |
| 750 | assert_eq!(h, super::model::TOP_HEIGHT_MIN); |
| 751 | } |
| 752 | |
| 753 | /// Non-Tasks Top panels auto-fit the same way Tasks always did: content |
| 754 | /// rows + divider, never a fixed four-row chrome band. An active goal |
| 755 | /// adds exactly one title row (not a panel name). |
| 756 | #[test] |
| 757 | fn top_panel_auto_fits_content_like_tasks() { |
| 758 | let mut pinned = app(); |
| 759 | pinned.work_surface.placement = WorkSurfacePlacement::Top; |
| 760 | pinned.work_surface.panel = super::RailPanel::Tasks; |
| 761 | pinned.work_surface.top_height = 12; |
| 762 | pinned.goal.objective = Some("goal".to_string()); |
| 763 | add_todos(&mut pinned, 3); |
| 764 | let budget = working_budget(&pinned, 40); |
| 765 | let h = super::height(&mut pinned, 100, 40, budget); |
| 766 | // goal title + 3 checklist + divider ≈ 5; must not be the old fixed 4, |
| 767 | // and must not pad out to the 12-row cap. |
| 768 | assert!( |
| 769 | (4..=8).contains(&h), |
| 770 | "Pinned should auto-fit checklist content, got {h}" |
| 771 | ); |
| 772 | |
| 773 | // Empty Pinned collapses entirely. |
| 774 | let mut empty = app(); |
| 775 | empty.work_surface.placement = WorkSurfacePlacement::Top; |
| 776 | empty.work_surface.panel = super::RailPanel::Tasks; |
| 777 | empty.work_surface.top_height = 12; |
| 778 | assert_eq!( |
| 779 | super::height(&mut empty, 100, 40, AMPLE_BUDGET), |
| 780 | 0, |
| 781 | "empty Pinned is not a panel" |
| 782 | ); |
| 783 | |
| 784 | // Empty Agents collapses too (no "No agents" chrome strip). |
| 785 | let mut agents = app(); |
| 786 | agents.work_surface.placement = WorkSurfacePlacement::Top; |
| 787 | agents.work_surface.panel = super::RailPanel::Agents; |
| 788 | agents.work_surface.top_height = 12; |
| 789 | assert_eq!( |
| 790 | super::height(&mut agents, 100, 40, AMPLE_BUDGET), |
| 791 | 0, |
| 792 | "empty Agents is not a panel" |
| 793 | ); |
| 794 | } |
| 795 | |
| 796 | /// A chosen panel remains usable when only one content row fits. |
| 797 | #[test] |
| 798 | fn compact_explicit_view_keeps_content_ahead_of_goal_chrome() { |
| 799 | let mut app = app(); |
| 800 | app.goal.objective = Some("ship the release".to_string()); |
| 801 | super::select_dock_panel(&mut app, super::RailPanel::Agents); |
| 802 | let text = render_text(&mut app, 40, 3); |
| 803 | assert!(text.contains("no agents have run this session"), "{text:?}"); |
| 804 | assert!(app.work_surface.focused); |
| 805 | assert!( |
| 806 | super::handle_key(&mut app, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)).is_some() |
| 807 | ); |
| 808 | assert!(!app.work_surface.explicit_view); |
| 809 | } |
| 810 | |
| 811 | /// Top titles only when a live goal is set — never the panel name. |
| 812 | #[test] |
| 813 | fn top_title_is_goal_only_never_panel_chrome() { |
| 814 | // With a goal: title is "Goal: …". |
| 815 | let mut with_goal = app(); |
| 816 | with_goal.work_surface.placement = WorkSurfacePlacement::Top; |
| 817 | with_goal.work_surface.panel = super::RailPanel::Tasks; |
| 818 | with_goal.work_surface.top_height = 8; |
| 819 | with_goal.goal.objective = Some("ship 0.9.4".to_string()); |
| 820 | let text = render_text(&mut with_goal, 80, 8); |
| 821 | assert!( |
| 822 | text.contains("Goal: ship 0.9.4"), |
| 823 | "active goal must be the Top title: {text:?}" |
| 824 | ); |
| 825 | assert!( |
| 826 | !render_rows(&mut with_goal, 80, 8) |
| 827 | .iter() |
| 828 | .skip(1) |
| 829 | .any(|row| row.contains("Pinned")), |
| 830 | "panel name is not a Top title: {text:?}" |
| 831 | ); |
| 832 | |
| 833 | // Without a goal, only checklist: no Goal title, no Pinned chrome. |
| 834 | let mut no_goal = app(); |
| 835 | no_goal.work_surface.placement = WorkSurfacePlacement::Top; |
| 836 | no_goal.work_surface.panel = super::RailPanel::Tasks; |
| 837 | no_goal.work_surface.top_height = 8; |
| 838 | add_todos(&mut no_goal, 2); |
| 839 | let text = render_text(&mut no_goal, 80, 6); |
| 840 | assert!( |
| 841 | !text.contains("Goal:"), |
| 842 | "no live goal → no Goal title: {text:?}" |
| 843 | ); |
| 844 | assert!( |
| 845 | !render_rows(&mut no_goal, 80, 6) |
| 846 | .iter() |
| 847 | .skip(1) |
| 848 | .any(|row| row.contains("Pinned")), |
| 849 | "panel name is never a Top title: {text:?}" |
| 850 | ); |
| 851 | } |
| 852 | |
| 853 | /// Tasks with only a goal (no todos/agents) still shows a strip. |
| 854 | #[test] |
| 855 | fn top_tasks_goal_alone_still_renders_a_strip() { |
| 856 | let mut app = app(); |
| 857 | app.work_surface.placement = WorkSurfacePlacement::Top; |
| 858 | app.work_surface.panel = super::RailPanel::Tasks; |
| 859 | app.work_surface.top_height = 8; |
| 860 | app.goal.objective = Some("only a goal".to_string()); |
| 861 | let budget = working_budget(&app, 40); |
| 862 | let h = super::height(&mut app, 100, 40, budget); |
| 863 | assert!(h >= 2, "goal alone must reserve title + divider, got {h}"); |
| 864 | let text = render_text(&mut app, 80, h); |
| 865 | assert!( |
| 866 | text.contains("Goal: only a goal"), |
| 867 | "goal-alone strip must paint the title: {text:?}" |
| 868 | ); |
| 869 | } |
| 870 | |
| 871 | /// Side rails share the empty-collapse rule: no content → no column. |
| 872 | /// Width stays the configured ceiling when content exists. |
| 873 | #[test] |
| 874 | fn side_rail_collapses_when_empty_and_reserves_when_contentful() { |
| 875 | let area = ratatui::layout::Rect::new(0, 0, 120, 32); |
| 876 | |
| 877 | // Empty Pinned: no side column. |
| 878 | let mut empty = app(); |
| 879 | empty.work_surface.placement = WorkSurfacePlacement::Right; |
| 880 | empty.work_surface.panel = super::RailPanel::Tasks; |
| 881 | empty.work_surface.side_width = 30; |
| 882 | assert_eq!( |
| 883 | super::split_chat(&mut empty, area, 0), |
| 884 | (area, None), |
| 885 | "empty Pinned must not reserve a side column" |
| 886 | ); |
| 887 | |
| 888 | // Contentful Pinned: full-height column at configured width. |
| 889 | let mut full = app(); |
| 890 | full.work_surface.placement = WorkSurfacePlacement::Right; |
| 891 | full.work_surface.panel = super::RailPanel::Tasks; |
| 892 | full.work_surface.side_width = 30; |
| 893 | full.goal.objective = Some("ship it".to_string()); |
| 894 | let (chat, rail) = super::split_chat(&mut full, area, 0); |
| 895 | let rail = rail.expect("contentful Pinned reserves a side rail"); |
| 896 | assert_eq!(rail.width, 30); |
| 897 | assert_eq!(chat.width, area.width - 30); |
| 898 | assert_eq!(rail.height, area.height); |
| 899 | } |
| 900 | |
| 901 | #[test] |
| 902 | fn minimum_top_surface_keeps_a_numbered_todo_selectable() { |
| 903 | let mut app = app(); |
| 904 | add_todos(&mut app, 2); |
| 905 | |
| 906 | let text = render_text(&mut app, 40, 5); |
| 907 | |
| 908 | assert!(text.contains("1 ·"), "{text:?}"); |
| 909 | assert!(!app.work_surface.hitboxes.is_empty()); |
| 910 | assert_eq!(app.work_surface.hitboxes[0].row_y, 2); |
| 911 | } |
| 912 | |
| 913 | #[test] |
| 914 | fn compact_progress_window_reveals_current_without_reordering() { |
| 915 | let mut app = app(); |
| 916 | { |
| 917 | let mut todos = app.todos.try_lock().expect("todos"); |
| 918 | todos.add("finished".to_string(), TodoStatus::Completed); |
| 919 | todos.add("current".to_string(), TodoStatus::InProgress); |
| 920 | todos.add("next".to_string(), TodoStatus::Pending); |
| 921 | } |
| 922 | |
| 923 | // The current item must win the compact window while retaining its |
| 924 | // canonical ordinal. |
| 925 | let text = render_text(&mut app, 80, 6); |
| 926 | |
| 927 | assert!(text.contains("To-do · 1/3 · 2 left"), "{text:?}"); |
| 928 | assert!( |
| 929 | text.contains(&format!( |
| 930 | "2 · {} current", |
| 931 | codewhale_tui_kit::glyphs::SELECTION |
| 932 | )), |
| 933 | "{text:?}" |
| 934 | ); |
| 935 | assert_eq!(app.work_surface.hitboxes[0].row_y, 2); |
| 936 | } |
| 937 | |
| 938 | #[test] |
| 939 | fn settled_file_tools_aggregate_once_and_keep_only_safe_targets() { |
| 940 | let mut app = app(); |
| 941 | app.current_session_id = Some(SESSION.to_string()); |
| 942 | app.workspace = PathBuf::from("/workspace/project"); |
| 943 | for (id, name, input, status) in [ |
| 944 | ( |
| 945 | "read-1", |
| 946 | "read_file", |
| 947 | serde_json::json!({"path": "/workspace/project/src/lib.rs"}), |
| 948 | ToolStatus::Success, |
| 949 | ), |
| 950 | ( |
| 951 | "search-1", |
| 952 | "grep_files", |
| 953 | serde_json::json!({"pattern": "WorkSurfaceState"}), |
| 954 | ToolStatus::Success, |
| 955 | ), |
| 956 | ( |
| 957 | "write-1", |
| 958 | "edit_file", |
| 959 | serde_json::json!({"path": "src/lib.rs"}), |
| 960 | ToolStatus::Success, |
| 961 | ), |
| 962 | ( |
| 963 | "read-external", |
| 964 | "read_file", |
| 965 | serde_json::json!({"path": "/Users/alice/private.txt"}), |
| 966 | ToolStatus::Failed, |
| 967 | ), |
| 968 | ] { |
| 969 | app.add_message(HistoryCell::Tool(ToolCell::Generic(GenericToolCell { |
| 970 | name: name.to_string(), |
| 971 | status, |
| 972 | input_summary: None, |
| 973 | output: Some("done".to_string()), |
| 974 | prompts: None, |
| 975 | spillover_path: None, |
| 976 | output_summary: None, |
| 977 | is_diff: false, |
| 978 | }))); |
| 979 | let index = app.history.len() - 1; |
| 980 | app.tool_details_by_cell.insert( |
| 981 | index, |
| 982 | ToolDetailRecord { |
| 983 | tool_id: id.to_string(), |
| 984 | tool_name: name.to_string(), |
| 985 | input, |
| 986 | output: Some("done".to_string()), |
| 987 | }, |
| 988 | ); |
| 989 | } |
| 990 | |
| 991 | let rows = super::model::project(&mut app); |
| 992 | let activity = rows |
| 993 | .iter() |
| 994 | .find(|row| row.id.0 == "activity:aggregate") |
| 995 | .expect("aggregated activity row"); |
| 996 | assert!( |
| 997 | activity.label.contains("Read 1 file ·") |
| 998 | && activity.label.contains("Searched 1 pattern ·") |
| 999 | && activity.label.ends_with("Wrote 1 file"), |
| 1000 | "aggregated label: {}", |
| 1001 | activity.label |
| 1002 | ); |
| 1003 | assert!(!activity.detail.contains("/Users/alice")); |
| 1004 | assert!(!activity.label.contains("WorkSurfaceState")); |
| 1005 | } |
| 1006 | |
| 1007 | #[test] |
| 1008 | fn agent_rows_show_role_assignment_and_open_the_agent_transcript() { |
| 1009 | let mut app = app(); |
| 1010 | app.current_session_id = Some(SESSION.to_string()); |
| 1011 | app.subagent_cache.push(SubAgentResult { |
| 1012 | usage: None, |
| 1013 | name: "agent_worker".to_string(), |
| 1014 | agent_id: "agent_worker".to_string(), |
| 1015 | context_mode: "fresh".to_string(), |
| 1016 | fork_context: false, |
| 1017 | workspace: None, |
| 1018 | git_branch: None, |
| 1019 | agent_type: FleetRole::Builder, |
| 1020 | assignment: SubAgentAssignment { |
| 1021 | native_preset: None, |
| 1022 | objective: "Wire settled file activity".to_string(), |
| 1023 | role: Some("general".to_string()), |
| 1024 | }, |
| 1025 | model: "test-model".to_string(), |
| 1026 | nickname: Some( |
| 1027 | codewhale_engine::tools::subagent::whale_name_for_id_in_locale( |
| 1028 | "agent_worker", |
| 1029 | "en", |
| 1030 | ), |
| 1031 | ), |
| 1032 | status: SubAgentStatus::Running, |
| 1033 | worker_status: Some(AgentWorkerStatus::RunningTool), |
| 1034 | runtime_permissions: None, |
| 1035 | parent_run_id: None, |
| 1036 | spawn_depth: 1, |
| 1037 | child_route: None, |
| 1038 | result: None, |
| 1039 | steps_taken: 2, |
| 1040 | checkpoint: None, |
| 1041 | needs_input: None, |
| 1042 | duration_ms: 50, |
| 1043 | started_at: None, |
| 1044 | from_prior_session: false, |
| 1045 | idle_ms: None, |
| 1046 | heartbeat_timeout_ms: None, |
| 1047 | }); |
| 1048 | app.agent_progress_meta.insert( |
| 1049 | "agent_worker".to_string(), |
| 1050 | crate::tui::app::AgentProgressMeta { |
| 1051 | current_activity: Some(AgentCurrentActivity::bounded( |
| 1052 | AgentCurrentActivityStatus::RunningTool, |
| 1053 | None, |
| 1054 | Some("File.apply_patch".to_string()), |
| 1055 | Some(2), |
| 1056 | )), |
| 1057 | current_tool: Some("apply_patch".to_string()), |
| 1058 | files_touched: 2, |
| 1059 | ..crate::tui::app::AgentProgressMeta::default() |
| 1060 | }, |
| 1061 | ); |
| 1062 | |
| 1063 | let rows = super::model::project(&mut app); |
| 1064 | let row = rows |
| 1065 | .iter() |
| 1066 | .find(|row| row.id.0 == "worker:agent_worker") |
| 1067 | .expect("agent work row"); |
| 1068 | // The identity column leads with the agent's nickname and keeps the |
| 1069 | // fleet role as the fallback spelling. It is never the raw agent id |
| 1070 | // (#36), and carries no `(+N)` while the agent is childless. |
| 1071 | assert_eq!( |
| 1072 | row.label, |
| 1073 | codewhale_engine::tools::subagent::whale_name_for_id_in_locale("agent_worker", "en") |
| 1074 | ); |
| 1075 | let facts = row.agent.as_ref().expect("agent row facts"); |
| 1076 | assert_eq!(facts.role_label, "general"); |
| 1077 | assert_eq!(facts.objective, "Wire settled file activity"); |
| 1078 | assert_eq!(facts.elapsed_secs, Some(0)); |
| 1079 | // No usage envelope has been seen, so there is no token figure at all. |
| 1080 | assert_eq!(facts.tokens, None); |
| 1081 | assert!(row.detail.contains("Wire settled file activity")); |
| 1082 | assert!(row.detail.contains("using File.apply_patch")); |
| 1083 | assert!(row.detail.contains("step 2")); |
| 1084 | assert!(row.detail.contains("2 files changed")); |
| 1085 | // One agent, one destination (v0.9.7): activation opens the agent's |
| 1086 | // transcript directly; Agent Details is the secondary action. |
| 1087 | assert_eq!( |
| 1088 | row.primary_action, |
| 1089 | Some(SidebarRowAction::OpenAgentTranscript { |
| 1090 | agent_id: "agent_worker".to_string(), |
| 1091 | }) |
| 1092 | ); |
| 1093 | } |
| 1094 | |
| 1095 | fn cached_worker( |
| 1096 | id: &str, |
| 1097 | role: &str, |
| 1098 | nickname: Option<&str>, |
| 1099 | parent_run_id: Option<&str>, |
| 1100 | status: SubAgentStatus, |
| 1101 | ) -> SubAgentResult { |
| 1102 | SubAgentResult { |
| 1103 | // `name` is the raw session id in production snapshots — the |
| 1104 | // strip must never render it (#36). |
| 1105 | usage: None, |
| 1106 | name: id.to_string(), |
| 1107 | agent_id: id.to_string(), |
| 1108 | context_mode: "fresh".to_string(), |
| 1109 | fork_context: false, |
| 1110 | workspace: None, |
| 1111 | git_branch: None, |
| 1112 | agent_type: FleetRole::Builder, |
| 1113 | assignment: SubAgentAssignment { |
| 1114 | native_preset: None, |
| 1115 | objective: format!("objective for {id}"), |
| 1116 | role: Some(role.to_string()), |
| 1117 | }, |
| 1118 | model: "test-model".to_string(), |
| 1119 | nickname: nickname.map(str::to_string), |
| 1120 | status, |
| 1121 | worker_status: None, |
| 1122 | runtime_permissions: None, |
| 1123 | parent_run_id: parent_run_id.map(str::to_string), |
| 1124 | spawn_depth: u32::from(parent_run_id.is_some()) + 1, |
| 1125 | child_route: None, |
| 1126 | result: None, |
| 1127 | steps_taken: 1, |
| 1128 | checkpoint: None, |
| 1129 | needs_input: None, |
| 1130 | duration_ms: 50, |
| 1131 | started_at: None, |
| 1132 | from_prior_session: false, |
| 1133 | idle_ms: None, |
| 1134 | heartbeat_timeout_ms: None, |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | #[test] |
| 1139 | fn agent_rows_identify_by_fleet_role_and_never_leak_raw_ids() { |
| 1140 | // #36: the strip identifies an agent by its fleet role; the raw agent |
| 1141 | // id hash is noise and must never render as the "name". Flat fan-outs |
| 1142 | // carry no nesting chrome. |
| 1143 | let mut app = app(); |
| 1144 | app.current_session_id = Some(SESSION.to_string()); |
| 1145 | app.subagent_cache.push(cached_worker( |
| 1146 | "agent_e0b2dcf1", |
| 1147 | "builder", |
| 1148 | None, |
| 1149 | None, |
| 1150 | SubAgentStatus::Running, |
| 1151 | )); |
| 1152 | app.subagent_cache.push(cached_worker( |
| 1153 | "agent_99aa77bb", |
| 1154 | "scout", |
| 1155 | None, |
| 1156 | None, |
| 1157 | SubAgentStatus::Running, |
| 1158 | )); |
| 1159 | |
| 1160 | let rows = super::model::project(&mut app); |
| 1161 | let first = rows |
| 1162 | .iter() |
| 1163 | .find(|row| row.id.0 == "worker:agent_e0b2dcf1") |
| 1164 | .expect("first agent row"); |
| 1165 | let second = rows |
| 1166 | .iter() |
| 1167 | .find(|row| row.id.0 == "worker:agent_99aa77bb") |
| 1168 | .expect("second agent row"); |
| 1169 | assert_eq!(first.label, "builder"); |
| 1170 | assert_eq!(second.label, "scout"); |
| 1171 | assert!(first.detail.starts_with("running"), "{}", first.detail); |
| 1172 | for row in rows.iter().filter(|row| row.id.0.starts_with("worker:")) { |
| 1173 | assert!(!row.label.contains("agent_e0b2dcf1"), "{}", row.label); |
| 1174 | assert!(!row.label.contains("agent_99aa77bb"), "{}", row.label); |
| 1175 | assert!( |
| 1176 | !row.label.contains('↳'), |
| 1177 | "flat fan-out must not show nesting chrome: {}", |
| 1178 | row.label |
| 1179 | ); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | #[test] |
| 1184 | fn agent_rows_order_and_indent_nested_spawns_under_their_parent() { |
| 1185 | // #36: nesting is visible only when actually present — the child |
| 1186 | // renders directly under its parent with a `↳` indent, and the parent |
| 1187 | // advertises the child it spawned as `(+1)`. |
| 1188 | let mut app = app(); |
| 1189 | app.current_session_id = Some(SESSION.to_string()); |
| 1190 | app.subagent_cache.push(cached_worker( |
| 1191 | "agent_child", |
| 1192 | "scout", |
| 1193 | None, |
| 1194 | Some("agent_parent"), |
| 1195 | SubAgentStatus::Running, |
| 1196 | )); |
| 1197 | app.subagent_cache.push(cached_worker( |
| 1198 | "agent_parent", |
| 1199 | "builder", |
| 1200 | None, |
| 1201 | None, |
| 1202 | SubAgentStatus::Running, |
| 1203 | )); |
| 1204 | |
| 1205 | let rows = super::model::project(&mut app); |
| 1206 | let worker_labels = rows |
| 1207 | .iter() |
| 1208 | .filter(|row| row.id.0.starts_with("worker:")) |
| 1209 | .map(|row| row.label.as_str()) |
| 1210 | .collect::<Vec<_>>(); |
| 1211 | let parent_pos = worker_labels |
| 1212 | .iter() |
| 1213 | .position(|label| *label == "builder (+1)") |
| 1214 | .expect("parent row label with child count"); |
| 1215 | let child_pos = worker_labels |
| 1216 | .iter() |
| 1217 | .position(|label| *label == "↳ scout") |
| 1218 | .expect("indented child row label"); |
| 1219 | assert!( |
| 1220 | child_pos == parent_pos + 1, |
| 1221 | "child must render directly under its parent: {worker_labels:?}" |
| 1222 | ); |
| 1223 | } |
| 1224 | |
| 1225 | #[test] |
| 1226 | fn agent_rows_completed_agents_render_quietly_without_spawn_metadata() { |
| 1227 | // #36: quiet completion — a finished agent keeps status + what it |
| 1228 | // did; in-flight metadata (tool, step counters) must not linger as a |
| 1229 | // receipt dump. #6565: what it changed is the receipt, and once its |
| 1230 | // result is known the row says what it produced. |
| 1231 | let mut app = app(); |
| 1232 | app.current_session_id = Some(SESSION.to_string()); |
| 1233 | app.subagent_cache.push(cached_worker( |
| 1234 | "agent_done", |
| 1235 | "builder", |
| 1236 | None, |
| 1237 | None, |
| 1238 | SubAgentStatus::Completed, |
| 1239 | )); |
| 1240 | app.agent_progress_meta.insert( |
| 1241 | "agent_done".to_string(), |
| 1242 | crate::tui::app::AgentProgressMeta { |
| 1243 | current_activity: Some(AgentCurrentActivity::bounded( |
| 1244 | AgentCurrentActivityStatus::Done, |
| 1245 | Some("apply_patch finished".to_string()), |
| 1246 | Some("File.apply_patch".to_string()), |
| 1247 | Some(7), |
| 1248 | )), |
| 1249 | current_tool: Some("apply_patch".to_string()), |
| 1250 | files_touched: 4, |
| 1251 | ..crate::tui::app::AgentProgressMeta::default() |
| 1252 | }, |
| 1253 | ); |
| 1254 | |
| 1255 | let rows = super::model::project(&mut app); |
| 1256 | let row = rows |
| 1257 | .iter() |
| 1258 | .find(|row| row.id.0 == "worker:agent_done") |
| 1259 | .expect("completed agent row"); |
| 1260 | assert!(row.detail.contains("completed"), "{}", row.detail); |
| 1261 | assert!( |
| 1262 | row.detail.contains("objective for agent_done"), |
| 1263 | "{}", |
| 1264 | row.detail |
| 1265 | ); |
| 1266 | assert!(!row.detail.contains("using "), "{}", row.detail); |
| 1267 | assert!(!row.detail.contains("step 7"), "{}", row.detail); |
| 1268 | assert!(row.detail.contains("4 files changed"), "{}", row.detail); |
| 1269 | |
| 1270 | app.subagent_cache[0].result = |
| 1271 | Some("## Summary\n\nPatched the parser. Tests pass.".to_string()); |
| 1272 | let rows = super::model::project(&mut app); |
| 1273 | let row = rows |
| 1274 | .iter() |
| 1275 | .find(|row| row.id.0 == "worker:agent_done") |
| 1276 | .expect("completed agent row"); |
| 1277 | assert_eq!( |
| 1278 | row.detail, "completed · Patched the parser. · 4 files changed", |
| 1279 | "the headline replaces the assignment" |
| 1280 | ); |
| 1281 | assert_eq!( |
| 1282 | row.agent.as_ref().map(|facts| facts.objective.as_str()), |
| 1283 | Some("Patched the parser.") |
| 1284 | ); |
| 1285 | } |
| 1286 | |
| 1287 | // ---- Fleet row layout ------------------------------------------------- |
| 1288 | |
| 1289 | /// Painted lines, one per terminal row, trailing padding removed. |
| 1290 | fn render_rows(app: &mut App, width: u16, height: u16) -> Vec<String> { |
| 1291 | let backend = TestBackend::new(width, height); |
| 1292 | let mut terminal = Terminal::new(backend).expect("terminal"); |
| 1293 | terminal |
| 1294 | .draw(|frame| super::render(frame, frame.area(), app)) |
| 1295 | .expect("draw"); |
| 1296 | let buffer = terminal.backend().buffer().clone(); |
| 1297 | (0..height) |
| 1298 | .map(|y| { |
| 1299 | (0..width) |
| 1300 | .map(|x| buffer[(x, y)].symbol()) |
| 1301 | .collect::<String>() |
| 1302 | .trim_end() |
| 1303 | .to_string() |
| 1304 | }) |
| 1305 | .collect() |
| 1306 | } |
| 1307 | |
| 1308 | fn fleet_row(rows: &[String]) -> String { |
| 1309 | rows.iter() |
| 1310 | .find(|line| line.contains("Streaming")) |
| 1311 | .cloned() |
| 1312 | .unwrap_or_else(|| panic!("no fleet row in {rows:?}")) |
| 1313 | } |
| 1314 | |
| 1315 | fn fleet_worker( |
| 1316 | id: &str, |
| 1317 | role: &str, |
| 1318 | objective: &str, |
| 1319 | duration_ms: u64, |
| 1320 | status: SubAgentStatus, |
| 1321 | ) -> SubAgentResult { |
| 1322 | let mut agent = cached_worker(id, role, None, None, status); |
| 1323 | agent.assignment.objective = objective.to_string(); |
| 1324 | agent.duration_ms = duration_ms; |
| 1325 | agent |
| 1326 | } |
| 1327 | |
| 1328 | /// Seed a live fleet of one, with a reported token spend. |
| 1329 | fn fleet_app(tokens: Option<u64>) -> App { |
| 1330 | let mut app = app(); |
| 1331 | app.current_session_id = Some(SESSION.to_string()); |
| 1332 | app.subagent_cache.push(fleet_worker( |
| 1333 | "agent_stream", |
| 1334 | "general-purpose", |
| 1335 | "Streaming dead-code removal", |
| 1336 | 753_000, |
| 1337 | SubAgentStatus::Running, |
| 1338 | )); |
| 1339 | app.agent_progress_meta.insert( |
| 1340 | "agent_stream".to_string(), |
| 1341 | crate::tui::app::AgentProgressMeta { |
| 1342 | received_tokens: tokens, |
| 1343 | ..crate::tui::app::AgentProgressMeta::default() |
| 1344 | }, |
| 1345 | ); |
| 1346 | app |
| 1347 | } |
| 1348 | |
| 1349 | #[test] |
| 1350 | fn fleet_row_lays_out_type_objective_and_a_right_aligned_receipt() { |
| 1351 | let mut app = fleet_app(Some(111_900)); |
| 1352 | let rows = render_rows(&mut app, 100, 4); |
| 1353 | |
| 1354 | assert_eq!( |
| 1355 | fleet_row(&rows), |
| 1356 | "│ ▸ general-purpose running Streaming dead-code removal \ |
| 1357 | 12m 33s · ↓ 111.9k tokens │" |
| 1358 | ); |
| 1359 | // The group header the strip already had stays put. |
| 1360 | assert!( |
| 1361 | rows.iter().any(|line| line.contains("Subagents 1")), |
| 1362 | "{rows:?}" |
| 1363 | ); |
| 1364 | } |
| 1365 | |
| 1366 | #[test] |
| 1367 | fn focused_worker_row_carries_the_left_marker_and_queued_follow_ups() { |
| 1368 | let mut app = fleet_app(Some(111_900)); |
| 1369 | // No focus, nothing queued: the row is exactly as before. |
| 1370 | let plain = fleet_row(&render_rows(&mut app, 100, 4)); |
| 1371 | assert!(!plain.starts_with("❯"), "{plain}"); |
| 1372 | assert!(!plain.contains("queued"), "{plain}"); |
| 1373 | |
| 1374 | crate::tui::agent_focus::focus_agent(&mut app, "agent_stream"); |
| 1375 | app.agent_queued_follow_ups |
| 1376 | .insert("agent_stream".to_string(), 1); |
| 1377 | let focused = fleet_row(&render_rows(&mut app, 110, 4)); |
| 1378 | assert!( |
| 1379 | focused.starts_with("│ ❯ ▸ general-purpose"), |
| 1380 | "left-edge marker names the addressed fork: {focused}" |
| 1381 | ); |
| 1382 | assert!(focused.ends_with("· 1 queued │"), "{focused}"); |
| 1383 | |
| 1384 | // The counter is the runtime's truth: once the child takes the input |
| 1385 | // the next AgentList refresh clears it and the suffix disappears. |
| 1386 | app.agent_queued_follow_ups.clear(); |
| 1387 | let drained = fleet_row(&render_rows(&mut app, 110, 4)); |
| 1388 | assert!(!drained.contains("queued"), "{drained}"); |
| 1389 | // Leaving focus removes the gutter again. |
| 1390 | crate::tui::agent_focus::exit_focus(&mut app); |
| 1391 | let back = fleet_row(&render_rows(&mut app, 100, 4)); |
| 1392 | assert_eq!(back, plain); |
| 1393 | } |
| 1394 | |
| 1395 | #[test] |
| 1396 | fn fleet_row_repaints_resolved_model_and_each_distinct_usage_total() { |
| 1397 | let mut app = fleet_app(None); |
| 1398 | crate::tui::ui::record_agent_spawned_route(&mut app, "agent_stream", "deepseek-v4-pro"); |
| 1399 | let launched = fleet_row(&render_rows(&mut app, 120, 4)); |
| 1400 | assert!(launched.contains("deepseek-v4-pro"), "{launched}"); |
| 1401 | assert!(!launched.contains("tokens"), "{launched}"); |
| 1402 | |
| 1403 | let route = codewhale_engine::cost_status::EffectiveRouteEnvelope::capture( |
| 1404 | None, |
| 1405 | ProviderKind::Deepseek, |
| 1406 | ProviderKind::Deepseek.as_str(), |
| 1407 | "deepseek-v4-pro", |
| 1408 | Some(ProviderKind::Deepseek.provider().default_base_url()), |
| 1409 | chrono::Utc::now(), |
| 1410 | ); |
| 1411 | let usage = |source_id: &str, input_tokens, output_tokens| MailboxMessage::TokenUsage { |
| 1412 | record: None, |
| 1413 | agent_id: "agent_stream".to_string(), |
| 1414 | source_id: source_id.to_string(), |
| 1415 | route: Box::new(route.clone()), |
| 1416 | usage: codewhale_models::Usage { |
| 1417 | input_tokens, |
| 1418 | output_tokens, |
| 1419 | ..Default::default() |
| 1420 | }, |
| 1421 | }; |
| 1422 | |
| 1423 | crate::tui::subagent_routing::handle_subagent_mailbox( |
| 1424 | &mut app, |
| 1425 | 99, |
| 1426 | &usage("response-1", 10_000, 1_000), |
| 1427 | ); |
| 1428 | let first = fleet_row(&render_rows(&mut app, 120, 4)); |
| 1429 | assert!(first.contains("deepseek-v4-pro"), "{first}"); |
| 1430 | assert!(first.contains("11.0k tokens"), "{first}"); |
| 1431 | |
| 1432 | // Replaying the same mailbox envelope must not inflate the receipt. |
| 1433 | crate::tui::subagent_routing::handle_subagent_mailbox( |
| 1434 | &mut app, |
| 1435 | 1, |
| 1436 | &usage("response-1", 10_000, 1_000), |
| 1437 | ); |
| 1438 | let replay = fleet_row(&render_rows(&mut app, 120, 4)); |
| 1439 | assert!(replay.contains("11.0k tokens"), "{replay}"); |
| 1440 | |
| 1441 | crate::tui::subagent_routing::handle_subagent_mailbox( |
| 1442 | &mut app, |
| 1443 | 2, |
| 1444 | &usage("response-2", 20_000, 2_000), |
| 1445 | ); |
| 1446 | let second = fleet_row(&render_rows(&mut app, 120, 4)); |
| 1447 | assert!(second.contains("deepseek-v4-pro"), "{second}"); |
| 1448 | assert!(second.contains("33.0k tokens"), "{second}"); |
| 1449 | } |
| 1450 | |
| 1451 | #[test] |
| 1452 | fn fleet_row_shows_remaining_todos_only_when_the_ledger_has_unsettled_work() { |
| 1453 | let mut app = fleet_app(Some(1_200)); |
| 1454 | app.agent_progress_meta |
| 1455 | .get_mut("agent_stream") |
| 1456 | .expect("meta") |
| 1457 | .todos_remaining = Some(3); |
| 1458 | |
| 1459 | let with_left = fleet_row(&render_rows(&mut app, 100, 4)); |
| 1460 | assert!( |
| 1461 | with_left.contains("3 left"), |
| 1462 | "unsettled ledger must surface on the receipt: {with_left}" |
| 1463 | ); |
| 1464 | assert!( |
| 1465 | with_left.contains("↓") && with_left.contains("tokens"), |
| 1466 | "tokens stay alongside the remaining chip: {with_left}" |
| 1467 | ); |
| 1468 | |
| 1469 | // Fully settled list → quiet (no fabricated zero chip). |
| 1470 | app.agent_progress_meta |
| 1471 | .get_mut("agent_stream") |
| 1472 | .expect("meta") |
| 1473 | .todos_remaining = Some(0); |
| 1474 | let settled = fleet_row(&render_rows(&mut app, 100, 4)); |
| 1475 | assert!( |
| 1476 | !settled.contains("left"), |
| 1477 | "zero remaining must not paint a chip: {settled}" |
| 1478 | ); |
| 1479 | |
| 1480 | // No ledger published → quiet. |
| 1481 | app.agent_progress_meta |
| 1482 | .get_mut("agent_stream") |
| 1483 | .expect("meta") |
| 1484 | .todos_remaining = None; |
| 1485 | let absent = fleet_row(&render_rows(&mut app, 100, 4)); |
| 1486 | assert!( |
| 1487 | !absent.contains("left"), |
| 1488 | "missing ledger must not invent a chip: {absent}" |
| 1489 | ); |
| 1490 | } |
| 1491 | |
| 1492 | #[test] |
| 1493 | fn fleet_identity_prefers_the_nickname_and_falls_back_to_the_role() { |
| 1494 | // Nicknames are CodeWhale identity, so they lead. An agent that has |
| 1495 | // none falls back to its fleet role rather than showing a blank or a |
| 1496 | // fabricated name. |
| 1497 | let mut app = app(); |
| 1498 | app.current_session_id = Some(SESSION.to_string()); |
| 1499 | let mut named = fleet_worker( |
| 1500 | "agent_named", |
| 1501 | "general-purpose", |
| 1502 | "Streaming dead-code removal", |
| 1503 | 753_000, |
| 1504 | SubAgentStatus::Running, |
| 1505 | ); |
| 1506 | named.nickname = Some("Fluke".to_string()); |
| 1507 | app.subagent_cache.push(named); |
| 1508 | app.subagent_cache.push(fleet_worker( |
| 1509 | "agent_plain", |
| 1510 | "general-purpose", |
| 1511 | "Ambient visual calm-down", |
| 1512 | 741_000, |
| 1513 | SubAgentStatus::Running, |
| 1514 | )); |
| 1515 | |
| 1516 | let rows = super::model::project(&mut app); |
| 1517 | let row = |id: &str| { |
| 1518 | rows.iter() |
| 1519 | .find(|row| row.id.0 == format!("worker:{id}")) |
| 1520 | .unwrap_or_else(|| panic!("row for {id}")) |
| 1521 | }; |
| 1522 | assert_eq!(row("agent_named").label, "Fluke"); |
| 1523 | assert_eq!( |
| 1524 | row("agent_named").agent.as_ref().expect("facts").role_label, |
| 1525 | "general-purpose" |
| 1526 | ); |
| 1527 | // No nickname: the identity and its fallback are the same string. |
| 1528 | assert_eq!(row("agent_plain").label, "general-purpose"); |
| 1529 | |
| 1530 | // Both spellings share one column, so the objectives stay aligned. |
| 1531 | let painted = render_rows(&mut app, 100, 5); |
| 1532 | let named_line = painted |
| 1533 | .iter() |
| 1534 | .find(|line| line.contains("Fluke")) |
| 1535 | .expect("nicknamed row"); |
| 1536 | let plain_line = painted |
| 1537 | .iter() |
| 1538 | .find(|line| line.contains("general-purpose")) |
| 1539 | .expect("un-nicknamed row"); |
| 1540 | assert_eq!( |
| 1541 | named_line.find("Streaming"), |
| 1542 | plain_line.find("Ambient"), |
| 1543 | "objectives must share a column:\n{named_line}\n{plain_line}" |
| 1544 | ); |
| 1545 | } |
| 1546 | |
| 1547 | #[test] |
| 1548 | fn an_identity_too_wide_for_the_column_falls_back_without_widening_it() { |
| 1549 | // The identity column is shared, so one outlier must not starve every |
| 1550 | // other objective — and a name is shown whole or not at all. |
| 1551 | let mut app = app(); |
| 1552 | app.current_session_id = Some(SESSION.to_string()); |
| 1553 | let mut long = fleet_worker( |
| 1554 | "agent_long", |
| 1555 | "general-purpose", |
| 1556 | "Streaming dead-code removal", |
| 1557 | 753_000, |
| 1558 | SubAgentStatus::Running, |
| 1559 | ); |
| 1560 | long.nickname = Some("Bartholomew the Extremely Long-Winded Humpback".to_string()); |
| 1561 | app.subagent_cache.push(long); |
| 1562 | app.subagent_cache.push(fleet_worker( |
| 1563 | "agent_plain", |
| 1564 | "scout", |
| 1565 | "Ambient visual calm-down", |
| 1566 | 741_000, |
| 1567 | SubAgentStatus::Running, |
| 1568 | )); |
| 1569 | |
| 1570 | let painted = render_rows(&mut app, 100, 5); |
| 1571 | let joined = painted.join("\n"); |
| 1572 | // The oversized nickname never renders, whole or truncated. |
| 1573 | assert!(!joined.contains("Bartholomew"), "{joined}"); |
| 1574 | assert!(!joined.contains("Bartholom"), "{joined}"); |
| 1575 | // It falls back to its role, and the other row is untouched. |
| 1576 | assert!(joined.contains("general-purpose"), "{joined}"); |
| 1577 | assert!(joined.contains("scout"), "{joined}"); |
| 1578 | // Neither objective was starved by the outlier. |
| 1579 | assert!(joined.contains("Streaming dead-code removal"), "{joined}"); |
| 1580 | assert!(joined.contains("Ambient visual calm-down"), "{joined}"); |
| 1581 | } |
| 1582 | |
| 1583 | #[test] |
| 1584 | fn fleet_row_drops_tokens_then_elapsed_then_type_as_the_surface_narrows() { |
| 1585 | // Settled degradation order: tokens first, then elapsed, then the |
| 1586 | // type and status columns together. The objective is the last thing |
| 1587 | // to go and every column truncates rather than wrapping. The status |
| 1588 | // word outlives the whole receipt — a fleet row that cannot say its |
| 1589 | // state in words has lost the fact the strip exists to show. |
| 1590 | let mut app = fleet_app(Some(111_900)); |
| 1591 | let medium = fleet_row(&render_rows(&mut app, 72, 4)); |
| 1592 | assert!(medium.contains("12m 33s"), "{medium}"); |
| 1593 | assert!(!medium.contains("tokens"), "{medium}"); |
| 1594 | assert!(medium.contains("general-purpose"), "{medium}"); |
| 1595 | assert!(medium.contains("running"), "{medium}"); |
| 1596 | |
| 1597 | let narrow = fleet_row(&render_rows(&mut app, 56, 4)); |
| 1598 | assert!(!narrow.contains("tokens"), "{narrow}"); |
| 1599 | assert!(!narrow.contains("12m 33s"), "{narrow}"); |
| 1600 | assert!(narrow.contains("general-purpose"), "{narrow}"); |
| 1601 | assert!(narrow.contains("running"), "{narrow}"); |
| 1602 | |
| 1603 | let tight = fleet_row(&render_rows(&mut app, 28, 4)); |
| 1604 | assert!(!tight.contains("general-purpose"), "{tight}"); |
| 1605 | assert!(!tight.contains("running"), "{tight}"); |
| 1606 | assert!(tight.contains("Streaming"), "{tight}"); |
| 1607 | |
| 1608 | for line in [&medium, &narrow, &tight] { |
| 1609 | assert!(line.chars().all(|ch| ch != '\n'), "{line}"); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | #[test] |
| 1614 | fn fleet_row_elapsed_freezes_once_the_agent_is_finished() { |
| 1615 | // The manager recomputes `duration_ms` as `started_at.elapsed()` on |
| 1616 | // every snapshot, so a finished agent's raw duration keeps growing. |
| 1617 | // The row must latch the first terminal reading instead. |
| 1618 | let mut app = fleet_app(None); |
| 1619 | app.subagent_cache[0].status = SubAgentStatus::Completed; |
| 1620 | app.subagent_cache[0].duration_ms = 753_000; |
| 1621 | |
| 1622 | let first = super::model::project(&mut app); |
| 1623 | let finished = first |
| 1624 | .iter() |
| 1625 | .find(|row| row.id.0 == "worker:agent_stream") |
| 1626 | .and_then(|row| row.agent.as_ref()) |
| 1627 | .expect("finished agent facts"); |
| 1628 | assert_eq!(finished.elapsed_secs, Some(753)); |
| 1629 | |
| 1630 | // A later snapshot reports a larger duration for the same dead agent. |
| 1631 | app.subagent_cache[0].duration_ms = 999_000; |
| 1632 | let second = super::model::project(&mut app); |
| 1633 | let still = second |
| 1634 | .iter() |
| 1635 | .find(|row| row.id.0 == "worker:agent_stream") |
| 1636 | .and_then(|row| row.agent.as_ref()) |
| 1637 | .expect("finished agent facts"); |
| 1638 | assert_eq!( |
| 1639 | still.elapsed_secs, |
| 1640 | Some(753), |
| 1641 | "finished elapsed must freeze" |
| 1642 | ); |
| 1643 | } |
| 1644 | |
| 1645 | #[test] |
| 1646 | fn fleet_row_elapsed_still_advances_while_the_agent_runs() { |
| 1647 | let mut app = fleet_app(None); |
| 1648 | app.subagent_cache[0].duration_ms = 10_000; |
| 1649 | let early = super::model::project(&mut app); |
| 1650 | assert_eq!( |
| 1651 | early |
| 1652 | .iter() |
| 1653 | .find(|row| row.id.0 == "worker:agent_stream") |
| 1654 | .and_then(|row| row.agent.as_ref()) |
| 1655 | .expect("running agent facts") |
| 1656 | .elapsed_secs, |
| 1657 | Some(10) |
| 1658 | ); |
| 1659 | |
| 1660 | app.subagent_cache[0].duration_ms = 40_000; |
| 1661 | let later = super::model::project(&mut app); |
| 1662 | assert_eq!( |
| 1663 | later |
| 1664 | .iter() |
| 1665 | .find(|row| row.id.0 == "worker:agent_stream") |
| 1666 | .and_then(|row| row.agent.as_ref()) |
| 1667 | .expect("running agent facts") |
| 1668 | .elapsed_secs, |
| 1669 | Some(40) |
| 1670 | ); |
| 1671 | } |
| 1672 | |
| 1673 | #[test] |
| 1674 | fn fleet_row_with_no_reported_usage_shows_no_token_figure_at_all() { |
| 1675 | // An unknown number is rendered as nothing. Never `0`, which would |
| 1676 | // claim the agent spent nothing. |
| 1677 | let mut app = fleet_app(None); |
| 1678 | let row = fleet_row(&render_rows(&mut app, 100, 4)); |
| 1679 | assert!(!row.contains("tokens"), "{row}"); |
| 1680 | assert!(!row.contains('↓'), "{row}"); |
| 1681 | assert!(row.contains("12m 33s"), "{row}"); |
| 1682 | |
| 1683 | let mut spent = fleet_app(Some(0)); |
| 1684 | let zero = fleet_row(&render_rows(&mut spent, 100, 4)); |
| 1685 | // A *reported* zero is a fact and does render. |
| 1686 | assert!(zero.contains("↓ 0 tokens"), "{zero}"); |
| 1687 | } |
| 1688 | |
| 1689 | #[test] |
| 1690 | fn fleet_row_child_badge_counts_children_that_are_on_the_surface() { |
| 1691 | let mut app = app(); |
| 1692 | app.current_session_id = Some(SESSION.to_string()); |
| 1693 | app.subagent_cache.push(cached_worker( |
| 1694 | "agent_lead", |
| 1695 | "general-purpose", |
| 1696 | None, |
| 1697 | None, |
| 1698 | SubAgentStatus::Running, |
| 1699 | )); |
| 1700 | for child in ["agent_c1", "agent_c2", "agent_c3"] { |
| 1701 | app.subagent_cache.push(cached_worker( |
| 1702 | child, |
| 1703 | "scout", |
| 1704 | None, |
| 1705 | Some("agent_lead"), |
| 1706 | SubAgentStatus::Running, |
| 1707 | )); |
| 1708 | } |
| 1709 | // A child whose parent is not on the surface must not be counted for |
| 1710 | // anyone, and must not inflate the lead's badge. |
| 1711 | app.subagent_cache.push(cached_worker( |
| 1712 | "agent_orphan", |
| 1713 | "scout", |
| 1714 | None, |
| 1715 | Some("agent_missing"), |
| 1716 | SubAgentStatus::Running, |
| 1717 | )); |
| 1718 | |
| 1719 | let rows = super::model::project(&mut app); |
| 1720 | let label = |id: &str| { |
| 1721 | rows.iter() |
| 1722 | .find(|row| row.id.0 == format!("worker:{id}")) |
| 1723 | .map(|row| row.label.clone()) |
| 1724 | .unwrap_or_else(|| panic!("row for {id}")) |
| 1725 | }; |
| 1726 | assert_eq!(label("agent_lead"), "general-purpose (+3)"); |
| 1727 | assert_eq!(label("agent_c1"), "↳ scout"); |
| 1728 | assert_eq!(label("agent_orphan"), "scout"); |
| 1729 | } |
| 1730 | |
| 1731 | #[test] |
| 1732 | fn a_capped_fleet_list_announces_how_many_rows_it_is_hiding() { |
| 1733 | let mut app = app(); |
| 1734 | app.current_session_id = Some(SESSION.to_string()); |
| 1735 | for index in 0..8 { |
| 1736 | app.subagent_cache.push(cached_worker( |
| 1737 | &format!("agent_{index}"), |
| 1738 | "general-purpose", |
| 1739 | None, |
| 1740 | None, |
| 1741 | SubAgentStatus::Running, |
| 1742 | )); |
| 1743 | } |
| 1744 | |
| 1745 | // Four content rows for nine projected rows (header + eight workers). |
| 1746 | let rows = render_rows(&mut app, 100, 5); |
| 1747 | let more = rows |
| 1748 | .iter() |
| 1749 | .find(|line| line.contains("more")) |
| 1750 | .unwrap_or_else(|| panic!("no overflow line in {rows:?}")); |
| 1751 | // Nine projected rows (header + eight workers); two fit, seven do not. |
| 1752 | assert!(more.contains("↓ 7 more"), "{more}"); |
| 1753 | // Right-aligned against the content column, not the left margin. |
| 1754 | assert!(more.starts_with("│ "), "{more}"); |
| 1755 | } |
| 1756 | |
| 1757 | #[test] |
| 1758 | fn fleet_rows_render_in_top_left_and_right_placements() { |
| 1759 | for placement in [ |
| 1760 | super::WorkSurfacePlacement::Top, |
| 1761 | super::WorkSurfacePlacement::Left, |
| 1762 | super::WorkSurfacePlacement::Right, |
| 1763 | ] { |
| 1764 | let mut app = fleet_app(Some(111_900)); |
| 1765 | app.work_surface.placement = placement; |
| 1766 | app.work_surface.effective_placement = placement; |
| 1767 | let rows = render_rows(&mut app, 40, 8); |
| 1768 | let row = fleet_row(&rows); |
| 1769 | assert!( |
| 1770 | row.contains("Streaming"), |
| 1771 | "{placement:?} lost the objective: {rows:?}" |
| 1772 | ); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | #[test] |
| 1777 | fn progress_only_work_rows_use_typed_activity_not_display_substrings() { |
| 1778 | let mut app = app(); |
| 1779 | app.current_session_id = Some(SESSION.to_string()); |
| 1780 | app.agent_progress.insert( |
| 1781 | "agent_progress_only".to_string(), |
| 1782 | "queued waiting failed completed".to_string(), |
| 1783 | ); |
| 1784 | |
| 1785 | let rows = super::model::project(&mut app); |
| 1786 | let row = rows |
| 1787 | .iter() |
| 1788 | .find(|row| row.id.0 == "worker:agent_progress_only") |
| 1789 | .expect("progress-only work row"); |
| 1790 | assert_eq!(row.detail, "running"); |
| 1791 | |
| 1792 | app.agent_progress_meta.insert( |
| 1793 | "agent_progress_only".to_string(), |
| 1794 | crate::tui::app::AgentProgressMeta { |
| 1795 | current_activity: Some(AgentCurrentActivity::bounded( |
| 1796 | AgentCurrentActivityStatus::Waiting, |
| 1797 | Some("approval required".to_string()), |
| 1798 | None, |
| 1799 | Some(5), |
| 1800 | )), |
| 1801 | ..crate::tui::app::AgentProgressMeta::default() |
| 1802 | }, |
| 1803 | ); |
| 1804 | |
| 1805 | let rows = super::model::project(&mut app); |
| 1806 | let row = rows |
| 1807 | .iter() |
| 1808 | .find(|row| row.id.0 == "worker:agent_progress_only") |
| 1809 | .expect("typed progress-only work row"); |
| 1810 | assert!(row.detail.contains("waiting for input"), "{}", row.detail); |
| 1811 | assert!(row.detail.contains("approval required"), "{}", row.detail); |
| 1812 | assert!(row.detail.contains("step 5"), "{}", row.detail); |
| 1813 | } |
| 1814 | |
| 1815 | /// #6565: when the activity detail already names the step and the tool, |
| 1816 | /// the dock row says them once, not again as `using …` and `step N`. |
| 1817 | #[test] |
| 1818 | fn dock_agent_row_states_each_step_fact_once() { |
| 1819 | let mut app = app(); |
| 1820 | app.current_session_id = Some(SESSION.to_string()); |
| 1821 | app.agent_progress |
| 1822 | .insert("agent_reader".to_string(), String::new()); |
| 1823 | app.agent_progress_meta.insert( |
| 1824 | "agent_reader".to_string(), |
| 1825 | crate::tui::app::AgentProgressMeta { |
| 1826 | current_activity: Some(AgentCurrentActivity::bounded( |
| 1827 | AgentCurrentActivityStatus::RunningTool, |
| 1828 | Some("step 6: finished tool 'read_file'".to_string()), |
| 1829 | Some("read_file".to_string()), |
| 1830 | Some(6), |
| 1831 | )), |
| 1832 | ..crate::tui::app::AgentProgressMeta::default() |
| 1833 | }, |
| 1834 | ); |
| 1835 | |
| 1836 | let rows = super::model::project(&mut app); |
| 1837 | let row = rows |
| 1838 | .iter() |
| 1839 | .find(|row| row.id.0 == "worker:agent_reader") |
| 1840 | .expect("progress-only work row"); |
| 1841 | assert_eq!(row.detail.matches("step 6").count(), 1, "{}", row.detail); |
| 1842 | assert_eq!(row.detail.matches("read_file").count(), 1, "{}", row.detail); |
| 1843 | assert!(!row.detail.contains("using read_file"), "{}", row.detail); |
| 1844 | } |
| 1845 | |
| 1846 | // === #5906: a parked husk is not an agent waiting for input ========== |
| 1847 | |
| 1848 | /// Build a child exactly the way the turn-end parking projection does: |
| 1849 | /// `Interrupted` + `WaitingForUser` + a `needs_input` note phrased as a |
| 1850 | /// question, distinguished from a real question only by the checkpoint's |
| 1851 | /// `parked_at_turn_end` flag. |
| 1852 | fn parked_worker(id: &str, objective: &str) -> SubAgentResult { |
| 1853 | let mut agent = fleet_worker( |
| 1854 | id, |
| 1855 | "general-purpose", |
| 1856 | objective, |
| 1857 | 753_000, |
| 1858 | SubAgentStatus::Interrupted( |
| 1859 | "Parent turn ended before this turn-owned child settled.".to_string(), |
| 1860 | ), |
| 1861 | ); |
| 1862 | agent.worker_status = Some(AgentWorkerStatus::WaitingForUser); |
| 1863 | agent.needs_input = Some(codewhale_engine::tools::subagent::SubAgentNeedsInput { |
| 1864 | question: format!( |
| 1865 | "Resume this parked child with agent(action=\"start\", resume_from=\"{id}\")." |
| 1866 | ), |
| 1867 | }); |
| 1868 | agent.checkpoint = Some(codewhale_engine::tools::subagent::SubAgentCheckpoint { |
| 1869 | limit_kind: None, |
| 1870 | checkpoint_id: format!("{id}:step:2"), |
| 1871 | agent_id: id.to_string(), |
| 1872 | continuation_handle: format!("agent:{id}:checkpoint"), |
| 1873 | reason: "Parent turn ended before this turn-owned child settled.".to_string(), |
| 1874 | continuable: true, |
| 1875 | steps_taken: 2, |
| 1876 | message_count: 4, |
| 1877 | created_at_ms: 1_000, |
| 1878 | messages: Vec::new(), |
| 1879 | omitted_messages: 0, |
| 1880 | parked_at_turn_end: true, |
| 1881 | }); |
| 1882 | agent |
| 1883 | } |
| 1884 | |
| 1885 | fn asking_worker(id: &str, objective: &str) -> SubAgentResult { |
| 1886 | let mut agent = fleet_worker( |
| 1887 | id, |
| 1888 | "general-purpose", |
| 1889 | objective, |
| 1890 | 120_000, |
| 1891 | SubAgentStatus::Running, |
| 1892 | ); |
| 1893 | agent.worker_status = Some(AgentWorkerStatus::WaitingForUser); |
| 1894 | agent.needs_input = Some(codewhale_engine::tools::subagent::SubAgentNeedsInput { |
| 1895 | question: "Which path should I use?".to_string(), |
| 1896 | }); |
| 1897 | agent |
| 1898 | } |
| 1899 | |
| 1900 | fn parked_fixture() -> App { |
| 1901 | let mut app = app(); |
| 1902 | app.current_session_id = Some(SESSION.to_string()); |
| 1903 | app.subagent_cache |
| 1904 | .push(parked_worker("agent_parked", "Parked dead-code removal")); |
| 1905 | app.subagent_cache |
| 1906 | .push(asking_worker("agent_asking", "Asking about the path")); |
| 1907 | app.subagent_cache.push(fleet_worker( |
| 1908 | "agent_live", |
| 1909 | "general-purpose", |
| 1910 | "Streaming dead-code removal", |
| 1911 | 30_000, |
| 1912 | SubAgentStatus::Running, |
| 1913 | )); |
| 1914 | crate::tui::subagent_routing::reconcile_subagent_activity_state(&mut app); |
| 1915 | app |
| 1916 | } |
| 1917 | |
| 1918 | #[test] |
| 1919 | fn a_parked_work_row_says_parked_and_names_its_recovery() { |
| 1920 | let mut app = parked_fixture(); |
| 1921 | let rows = super::model::project(&mut app); |
| 1922 | |
| 1923 | let parked = rows |
| 1924 | .iter() |
| 1925 | .find(|row| row.id.0 == "worker:agent_parked") |
| 1926 | .expect("parked work row"); |
| 1927 | assert!(parked.detail.starts_with("parked"), "{}", parked.detail); |
| 1928 | assert!( |
| 1929 | !parked.detail.contains("waiting for input"), |
| 1930 | "a parked husk must not wear the answerable label: {}", |
| 1931 | parked.detail |
| 1932 | ); |
| 1933 | // The recovery names verbs the runtime actually exposes. |
| 1934 | assert!(parked.detail.contains("resume_from"), "{}", parked.detail); |
| 1935 | assert!(parked.detail.contains("cancel"), "{}", parked.detail); |
| 1936 | assert!( |
| 1937 | !parked.detail.contains("Resume this parked child"), |
| 1938 | "the parking note is not a question to replay at the operator: {}", |
| 1939 | parked.detail |
| 1940 | ); |
| 1941 | |
| 1942 | let asking = rows |
| 1943 | .iter() |
| 1944 | .find(|row| row.id.0 == "worker:agent_asking") |
| 1945 | .expect("asking work row"); |
| 1946 | assert!( |
| 1947 | asking.detail.contains("waiting for input"), |
| 1948 | "a child that really asked keeps the answerable label: {}", |
| 1949 | asking.detail |
| 1950 | ); |
| 1951 | assert!( |
| 1952 | asking.detail.contains("Which path should I use?"), |
| 1953 | "{}", |
| 1954 | asking.detail |
| 1955 | ); |
| 1956 | } |
| 1957 | |
| 1958 | #[test] |
| 1959 | fn parked_rows_sort_below_live_work_and_leave_the_needs_input_count_alone() { |
| 1960 | let mut app = parked_fixture(); |
| 1961 | let rows = super::model::project(&mut app); |
| 1962 | |
| 1963 | let heading = rows |
| 1964 | .iter() |
| 1965 | .find(|row| row.id.0 == "section:work") |
| 1966 | .expect("work heading"); |
| 1967 | // The attention chip counts children a person is actually blocking: |
| 1968 | // one, the child that asked. Two would mean the parked husk had been |
| 1969 | // counted as waiting for input all over again. |
| 1970 | assert!( |
| 1971 | heading.label.contains("1 blocked"), |
| 1972 | "only the child that actually asked is blocked on a person: {}", |
| 1973 | heading.label |
| 1974 | ); |
| 1975 | |
| 1976 | let position = |id: &str| { |
| 1977 | rows.iter() |
| 1978 | .position(|row| row.id.0 == id) |
| 1979 | .unwrap_or_else(|| panic!("{id} missing from {rows:?}")) |
| 1980 | }; |
| 1981 | assert!( |
| 1982 | position("worker:agent_parked") > position("worker:agent_live"), |
| 1983 | "a parked husk must not sort above live work" |
| 1984 | ); |
| 1985 | assert!( |
| 1986 | position("worker:agent_parked") > position("worker:agent_asking"), |
| 1987 | "a parked husk must not sort above a child a person can answer" |
| 1988 | ); |
| 1989 | } |
| 1990 | |
| 1991 | #[test] |
| 1992 | fn the_parked_status_word_survives_the_narrow_row_ladder() { |
| 1993 | // The status word outlives the whole receipt as the strip narrows |
| 1994 | // (see the degradation test above); `parked` is the fact the row |
| 1995 | // exists to carry, so it must survive the same ladder `running` does. |
| 1996 | let mut app = app(); |
| 1997 | app.current_session_id = Some(SESSION.to_string()); |
| 1998 | app.subagent_cache |
| 1999 | .push(parked_worker("agent_parked", "Parked dead-code removal")); |
| 2000 | crate::tui::subagent_routing::reconcile_subagent_activity_state(&mut app); |
| 2001 | |
| 2002 | for width in [96u16, 72, 56] { |
| 2003 | let rows = render_rows(&mut app, width, 6); |
| 2004 | let row = rows |
| 2005 | .iter() |
| 2006 | .find(|line| line.contains("Parked dead-code")) |
| 2007 | .unwrap_or_else(|| panic!("no parked row at width {width} in {rows:?}")); |
| 2008 | assert!(row.contains("parked"), "width {width}: {row}"); |
| 2009 | assert!(!row.contains("waiting for input"), "width {width}: {row}"); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | #[test] |
| 2014 | fn agent_transcript_keyboard_mouse_and_return_selection_converge() { |
| 2015 | fn add_worker(app: &mut App) { |
| 2016 | app.current_session_id = Some(SESSION.to_string()); |
| 2017 | app.subagent_cache.push(SubAgentResult { |
| 2018 | usage: None, |
| 2019 | name: "agent_converge".to_string(), |
| 2020 | agent_id: "agent_converge".to_string(), |
| 2021 | context_mode: "fresh".to_string(), |
| 2022 | fork_context: false, |
| 2023 | workspace: None, |
| 2024 | git_branch: Some("codex/details".to_string()), |
| 2025 | agent_type: FleetRole::Builder, |
| 2026 | assignment: SubAgentAssignment { |
| 2027 | native_preset: None, |
| 2028 | objective: "Verify keyboard and mouse convergence".to_string(), |
| 2029 | role: Some("worker".to_string()), |
| 2030 | }, |
| 2031 | model: "test-model".to_string(), |
| 2032 | nickname: Some( |
| 2033 | codewhale_engine::tools::subagent::whale_name_for_id_in_locale( |
| 2034 | "agent_converge", |
| 2035 | "en", |
| 2036 | ), |
| 2037 | ), |
| 2038 | status: SubAgentStatus::Running, |
| 2039 | worker_status: Some(AgentWorkerStatus::Running), |
| 2040 | runtime_permissions: None, |
| 2041 | parent_run_id: None, |
| 2042 | spawn_depth: 1, |
| 2043 | child_route: None, |
| 2044 | result: None, |
| 2045 | steps_taken: 1, |
| 2046 | checkpoint: None, |
| 2047 | needs_input: None, |
| 2048 | duration_ms: 100, |
| 2049 | started_at: None, |
| 2050 | from_prior_session: false, |
| 2051 | idle_ms: None, |
| 2052 | heartbeat_timeout_ms: None, |
| 2053 | }); |
| 2054 | } |
| 2055 | |
| 2056 | let mut keyboard = app(); |
| 2057 | add_worker(&mut keyboard); |
| 2058 | let _ = render_text(&mut keyboard, 100, 6); |
| 2059 | let _ = super::handle_key( |
| 2060 | &mut keyboard, |
| 2061 | KeyEvent::new(KeyCode::Char('w'), KeyModifiers::ALT), |
| 2062 | ); |
| 2063 | let keyboard_action = super::handle_key( |
| 2064 | &mut keyboard, |
| 2065 | KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE), |
| 2066 | ) |
| 2067 | .expect("Work key handled") |
| 2068 | .expect("agent transcript action"); |
| 2069 | let keyboard_selection = keyboard.work_surface.selected.clone(); |
| 2070 | |
| 2071 | let mut mouse = app(); |
| 2072 | add_worker(&mut mouse); |
| 2073 | let _ = render_text(&mut mouse, 100, 6); |
| 2074 | let row_y = mouse |
| 2075 | .work_surface |
| 2076 | .hitboxes |
| 2077 | .iter() |
| 2078 | .find(|hit| hit.id.0 == "worker:agent_converge") |
| 2079 | .expect("agent hitbox") |
| 2080 | .row_y; |
| 2081 | let mouse_action = super::handle_mouse( |
| 2082 | &mut mouse, |
| 2083 | MouseEvent { |
| 2084 | kind: MouseEventKind::Down(MouseButton::Left), |
| 2085 | column: 2, |
| 2086 | row: row_y, |
| 2087 | modifiers: KeyModifiers::NONE, |
| 2088 | }, |
| 2089 | ) |
| 2090 | .action |
| 2091 | .expect("mouse agent transcript action"); |
| 2092 | assert_eq!(mouse_action, keyboard_action); |
| 2093 | assert_eq!(mouse.work_surface.selected, keyboard_selection); |
| 2094 | |
| 2095 | crate::tui::mouse_ui::apply_sidebar_row_action(&mut mouse, mouse_action); |
| 2096 | // One agent, one destination: activation focuses the worker in place |
| 2097 | // (its full transcript owns the conversation area) instead of opening |
| 2098 | // a modal, and leaving focus keeps the rail selection where it was. |
| 2099 | assert!( |
| 2100 | mouse |
| 2101 | .agent_focus |
| 2102 | .as_ref() |
| 2103 | .is_some_and(|focus| focus.is("agent_converge")), |
| 2104 | "activation must focus the worker" |
| 2105 | ); |
| 2106 | let selected_before_close = mouse.work_surface.selected.clone(); |
| 2107 | assert!(crate::tui::agent_focus::exit_focus(&mut mouse)); |
| 2108 | assert_eq!(mouse.work_surface.selected, selected_before_close); |
| 2109 | assert!(mouse.work_surface.opened.is_none()); |
| 2110 | } |
| 2111 | |
| 2112 | #[test] |
| 2113 | fn active_session_without_work_keeps_surface_invisible() { |
| 2114 | let mut app = app(); |
| 2115 | app.current_session_id = Some(SESSION.to_string()); |
| 2116 | |
| 2117 | let rows = super::model::project(&mut app); |
| 2118 | |
| 2119 | assert!(rows.is_empty()); |
| 2120 | assert_eq!(super::height(&mut app, 120, 32, AMPLE_BUDGET), 0); |
| 2121 | } |
| 2122 | |
| 2123 | #[test] |
| 2124 | fn empty_work_stays_hidden_after_cached_session_state_is_cleared() { |
| 2125 | let mut app = app(); |
| 2126 | app.current_session_id = Some(SESSION.to_string()); |
| 2127 | app.work_surface.cached_graph = Some(operation_graph(NodeState::Active)); |
| 2128 | |
| 2129 | let rows = super::model::project(&mut app); |
| 2130 | |
| 2131 | assert!(rows.is_empty()); |
| 2132 | assert!(app.work_surface.cached_graph.is_none()); |
| 2133 | } |
| 2134 | |
| 2135 | #[test] |
| 2136 | fn empty_work_reserves_no_side_rail() { |
| 2137 | for placement in [ |
| 2138 | super::WorkSurfacePlacement::Left, |
| 2139 | super::WorkSurfacePlacement::Right, |
| 2140 | ] { |
| 2141 | let mut app = app(); |
| 2142 | app.current_session_id = Some(SESSION.to_string()); |
| 2143 | app.work_surface.placement = placement; |
| 2144 | let area = ratatui::layout::Rect::new(0, 0, 120, 32); |
| 2145 | |
| 2146 | assert_eq!( |
| 2147 | super::height(&mut app, area.width, area.height, AMPLE_BUDGET), |
| 2148 | 0 |
| 2149 | ); |
| 2150 | assert_eq!(super::split_chat(&mut app, area, 0), (area, None)); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | fn terminal_text(terminal: &Terminal<TestBackend>) -> String { |
| 2155 | let buf = terminal.backend().buffer(); |
| 2156 | (0..buf.area.height) |
| 2157 | .map(|y| { |
| 2158 | (0..buf.area.width) |
| 2159 | .map(|x| buf[(x, y)].symbol()) |
| 2160 | .collect::<String>() |
| 2161 | }) |
| 2162 | .collect::<Vec<_>>() |
| 2163 | .join("\n") |
| 2164 | } |
| 2165 | |
| 2166 | /// Render-level smoke coverage for the ported rail panels — reinstates |
| 2167 | /// the sidebar render smoke tests removed with the classic shell |
| 2168 | /// (739616787). Top never spends a row on panel chrome (content is |
| 2169 | /// self-evident). Side rails are named by their content's own heading |
| 2170 | /// row (`▾ Subagents N`, `Goal: …`); Context is the one line-list panel |
| 2171 | /// and keeps its muted panel title. |
| 2172 | #[test] |
| 2173 | fn rail_panels_render_in_all_placements() { |
| 2174 | for panel in [ |
| 2175 | super::RailPanel::Agents, |
| 2176 | super::RailPanel::Context, |
| 2177 | super::RailPanel::Tasks, |
| 2178 | ] { |
| 2179 | for placement in [ |
| 2180 | super::WorkSurfacePlacement::Bottom, |
| 2181 | super::WorkSurfacePlacement::Top, |
| 2182 | super::WorkSurfacePlacement::Left, |
| 2183 | super::WorkSurfacePlacement::Right, |
| 2184 | ] { |
| 2185 | let mut app = app(); |
| 2186 | app.work_surface.placement = placement; |
| 2187 | super::interaction::select_dock_panel(&mut app, panel); |
| 2188 | app.work_surface.focused = false; |
| 2189 | // Content so empty-collapse does not hide the panel. Agents |
| 2190 | // needs a cached worker; Tasks needs a goal; Context always |
| 2191 | // has a budget. |
| 2192 | app.goal.objective = Some("ship the release".to_string()); |
| 2193 | if panel == super::RailPanel::Agents { |
| 2194 | app.subagent_cache.push(cached_worker( |
| 2195 | "agent-a", |
| 2196 | "explore", |
| 2197 | Some("scout"), |
| 2198 | None, |
| 2199 | SubAgentStatus::Running, |
| 2200 | )); |
| 2201 | } |
| 2202 | let area = ratatui::layout::Rect::new(0, 0, 100, 24); |
| 2203 | |
| 2204 | // Render coverage, not yield coverage: a 24-row terminal with |
| 2205 | // work on screen has rows to spare, so the panel is expected |
| 2206 | // to draw. The idle-empty budget is exercised end-to-end in |
| 2207 | // `ui::tests::rail_strip_yields_the_ambient_floor_*`. |
| 2208 | let budget = working_budget(&app, area.height); |
| 2209 | let strip = super::height(&mut app, area.width, area.height, budget); |
| 2210 | let (_chat, rail) = super::split_chat(&mut app, area, 0); |
| 2211 | let backend = TestBackend::new(area.width, area.height); |
| 2212 | let mut terminal = Terminal::new(backend).expect("terminal"); |
| 2213 | terminal |
| 2214 | .draw(|frame| { |
| 2215 | if strip > 0 { |
| 2216 | super::render( |
| 2217 | frame, |
| 2218 | ratatui::layout::Rect::new(0, 0, area.width, strip), |
| 2219 | &mut app, |
| 2220 | ); |
| 2221 | } else if let Some(rail) = rail { |
| 2222 | super::render(frame, rail, &mut app); |
| 2223 | } |
| 2224 | }) |
| 2225 | .expect("draw"); |
| 2226 | let text = terminal_text(&terminal); |
| 2227 | match placement { |
| 2228 | super::WorkSurfacePlacement::Bottom => { |
| 2229 | assert!( |
| 2230 | strip > 0, |
| 2231 | "{panel:?} on Bottom should auto-fit a content strip; got height 0" |
| 2232 | ); |
| 2233 | } |
| 2234 | super::WorkSurfacePlacement::Top => { |
| 2235 | assert!( |
| 2236 | strip > 0, |
| 2237 | "{panel:?} on Top should auto-fit a content strip; got height 0" |
| 2238 | ); |
| 2239 | // Panel chrome ("Pinned"/"Agents") never on Top. |
| 2240 | // An active goal *is* a title — and this fixture sets one. |
| 2241 | // A chrome title would be a row saying only the |
| 2242 | // view's name; the tab row and `▾ Subagents N` both |
| 2243 | // legitimately contain the lowercase word. |
| 2244 | let strip_body = text.lines().skip(1).collect::<Vec<_>>().join("\n"); |
| 2245 | assert!( |
| 2246 | !strip_body.lines().any(|line| line.trim() == panel.title()), |
| 2247 | "{panel:?} on Top must not spend a row on panel chrome; got: {text}" |
| 2248 | ); |
| 2249 | // Goal title when a live goal is set. |
| 2250 | assert!( |
| 2251 | text.contains("Goal:") && text.contains("ship the release"), |
| 2252 | "Top with an active goal must title with Goal: …; got: {text}" |
| 2253 | ); |
| 2254 | } |
| 2255 | super::WorkSurfacePlacement::Left | super::WorkSurfacePlacement::Right => { |
| 2256 | assert!( |
| 2257 | rail.is_some() || strip > 0, |
| 2258 | "{panel:?} in {placement:?} should reserve a rail" |
| 2259 | ); |
| 2260 | // Work-row panels are named by their content heading; |
| 2261 | // only the Context fact list keeps a panel title. |
| 2262 | match panel { |
| 2263 | super::RailPanel::Agents => { |
| 2264 | assert!( |
| 2265 | text.contains("Subagents 1"), |
| 2266 | "{panel:?} in {placement:?} should render its \ |
| 2267 | Subagents heading; got: {text}" |
| 2268 | ); |
| 2269 | assert!( |
| 2270 | !app.work_surface.hitboxes.is_empty(), |
| 2271 | "{panel:?} in {placement:?} must record hitboxes — \ |
| 2272 | every work row is a door" |
| 2273 | ); |
| 2274 | } |
| 2275 | super::RailPanel::Tasks => { |
| 2276 | assert!( |
| 2277 | text.contains("Goal: ship the release"), |
| 2278 | "{panel:?} in {placement:?} should render the goal \ |
| 2279 | heading; got: {text}" |
| 2280 | ); |
| 2281 | } |
| 2282 | _ => { |
| 2283 | assert!( |
| 2284 | text.contains("compact now"), |
| 2285 | "{panel:?} in {placement:?} should render the budget \ |
| 2286 | rows; got: {text}" |
| 2287 | ); |
| 2288 | } |
| 2289 | } |
| 2290 | } |
| 2291 | super::WorkSurfacePlacement::Off => {} |
| 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | #[test] |
| 2298 | fn off_placement_reserves_no_rail_in_any_panel() { |
| 2299 | for panel in [ |
| 2300 | super::RailPanel::Tasks, |
| 2301 | super::RailPanel::Agents, |
| 2302 | super::RailPanel::Context, |
| 2303 | super::RailPanel::Tasks, |
| 2304 | ] { |
| 2305 | let mut app = app(); |
| 2306 | add_todos(&mut app, 2); |
| 2307 | app.work_surface.placement = super::WorkSurfacePlacement::Off; |
| 2308 | app.work_surface.panel = panel; |
| 2309 | let area = ratatui::layout::Rect::new(0, 0, 120, 32); |
| 2310 | |
| 2311 | assert_eq!( |
| 2312 | super::height(&mut app, area.width, area.height, AMPLE_BUDGET), |
| 2313 | 0 |
| 2314 | ); |
| 2315 | assert_eq!(super::split_chat(&mut app, area, 0), (area, None)); |
| 2316 | assert_eq!(app.work_surface.last_area, None); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | #[test] |
| 2321 | fn context_view_renders_the_budget_in_a_side_rail() { |
| 2322 | let mut app = app(); |
| 2323 | let (_, max, _) = crate::tui::ui::context_usage_snapshot(&app).expect("context budget"); |
| 2324 | app.auto_compact = true; |
| 2325 | app.auto_compact_threshold_percent = 80.0; |
| 2326 | app.compact_threshold = usize::try_from(max / 2).expect("context window"); |
| 2327 | app.work_surface.placement = super::WorkSurfacePlacement::Right; |
| 2328 | super::interaction::select_dock_panel(&mut app, super::RailPanel::Context); |
| 2329 | let area = ratatui::layout::Rect::new(0, 0, 100, 24); |
| 2330 | |
| 2331 | let budget = working_budget(&app, area.height); |
| 2332 | let strip = super::height(&mut app, area.width, area.height, budget); |
| 2333 | assert_eq!(strip, 0, "side placements take no top strip"); |
| 2334 | let (_chat, rail) = super::split_chat(&mut app, area, 0); |
| 2335 | let rail = rail.expect("context panel reserves a side rail"); |
| 2336 | |
| 2337 | let backend = TestBackend::new(area.width, area.height); |
| 2338 | let mut terminal = Terminal::new(backend).expect("terminal"); |
| 2339 | terminal |
| 2340 | .draw(|frame| super::render(frame, rail, &mut app)) |
| 2341 | .expect("draw"); |
| 2342 | let text = terminal_text(&terminal); |
| 2343 | assert!(text.contains(" of "), "budget row; got: {text}"); |
| 2344 | assert!( |
| 2345 | text.contains("auto ~50%"), |
| 2346 | "effective threshold; got: {text}" |
| 2347 | ); |
| 2348 | assert!(text.contains("compact now"), "compact door; got: {text}"); |
| 2349 | assert!( |
| 2350 | app.work_surface |
| 2351 | .hitboxes |
| 2352 | .iter() |
| 2353 | .any(|hit| hit.id.0 == "context:compact"), |
| 2354 | "the compact row is a hit target" |
| 2355 | ); |
| 2356 | |
| 2357 | let rows = super::views::context_rows(&mut app); |
| 2358 | let automatic = rows |
| 2359 | .iter() |
| 2360 | .find(|row| row.id.0 == "context:automatic") |
| 2361 | .unwrap(); |
| 2362 | assert_eq!(automatic.label, "auto ~50% · target 80%"); |
| 2363 | assert!(automatic.detail.contains("other routes may differ")); |
| 2364 | app.compact_threshold = usize::try_from(max * 4 / 5).expect("context window"); |
| 2365 | let rows = super::views::context_rows(&mut app); |
| 2366 | let automatic = rows |
| 2367 | .iter() |
| 2368 | .find(|row| row.id.0 == "context:automatic") |
| 2369 | .unwrap(); |
| 2370 | assert_eq!(automatic.label, "auto ~80% · target 80%"); |
| 2371 | app.auto_compact = false; |
| 2372 | let rows = super::views::context_rows(&mut app); |
| 2373 | let automatic = rows |
| 2374 | .iter() |
| 2375 | .find(|row| row.id.0 == "context:automatic") |
| 2376 | .unwrap(); |
| 2377 | assert_eq!(automatic.label, "automatic compaction off"); |
| 2378 | assert!(automatic.detail.contains("/compact")); |
| 2379 | } |
| 2380 | |
| 2381 | #[test] |
| 2382 | fn missing_runtime_renders_disconnected_state() { |
| 2383 | let mut app = app(); |
| 2384 | app.current_session_id = Some(SESSION.to_string()); |
| 2385 | app.runtime_services.work = None; |
| 2386 | |
| 2387 | let rows = super::model::project(&mut app); |
| 2388 | |
| 2389 | assert_eq!(rows[0].label, "Work · disconnected"); |
| 2390 | } |
| 2391 | |
| 2392 | #[test] |
| 2393 | fn busy_graph_authority_renders_truthful_error_without_leaking_it_into_header() { |
| 2394 | let mut app = app(); |
| 2395 | app.current_session_id = Some(SESSION.to_string()); |
| 2396 | let todos = app.todos.clone(); |
| 2397 | let _guard = todos.try_lock().expect("hold To-do authority lock"); |
| 2398 | |
| 2399 | let rows = super::model::project(&mut app); |
| 2400 | |
| 2401 | assert_eq!(rows.len(), 1); |
| 2402 | assert_eq!(rows[0].label, "Work · error"); |
| 2403 | assert!(rows[0].detail.contains("To-do state is busy")); |
| 2404 | assert!(!rows[0].label.contains("busy")); |
| 2405 | } |
| 2406 | |
| 2407 | #[test] |
| 2408 | fn graph_error_without_an_active_session_stays_suppressed() { |
| 2409 | let mut app = app(); |
| 2410 | let todos = app.todos.clone(); |
| 2411 | let _guard = todos.try_lock().expect("hold To-do authority lock"); |
| 2412 | |
| 2413 | let rows = super::model::project(&mut app); |
| 2414 | |
| 2415 | assert!(rows.is_empty()); |
| 2416 | } |
| 2417 | |
| 2418 | #[test] |
| 2419 | fn waiting_operation_is_not_counted_as_running() { |
| 2420 | let mut app = app(); |
| 2421 | let graph = operation_graph(NodeState::Waiting); |
| 2422 | restore_graph(&mut app, &graph); |
| 2423 | app.runtime_services |
| 2424 | .work |
| 2425 | .as_ref() |
| 2426 | .expect("Work Graph runtime") |
| 2427 | .reconcile_operation( |
| 2428 | SESSION, |
| 2429 | OperationOwnerSnapshot::new("shell:shell_1234abcd", OwnerState::Waiting, 1, 6), |
| 2430 | ) |
| 2431 | .expect("waiting shell owner"); |
| 2432 | |
| 2433 | let rows = super::model::project(&mut app); |
| 2434 | |
| 2435 | assert!( |
| 2436 | rows[0].label.starts_with("Work · Needs input:") |
| 2437 | || rows[0] |
| 2438 | .label |
| 2439 | .starts_with("Work · 0 active · 1 needs input · 0 ready · 0 recent"), |
| 2440 | "{}", |
| 2441 | rows[0].label |
| 2442 | ); |
| 2443 | assert!( |
| 2444 | rows[0].label.contains("blocked") || rows[0].label.contains("needs input"), |
| 2445 | "{}", |
| 2446 | rows[0].label |
| 2447 | ); |
| 2448 | } |
| 2449 | |
| 2450 | #[test] |
| 2451 | fn stale_operation_is_blocked_attention_with_bounded_output_section() { |
| 2452 | let mut app = app(); |
| 2453 | let graph = operation_graph(NodeState::Stale); |
| 2454 | restore_graph(&mut app, &graph); |
| 2455 | |
| 2456 | let rows = super::model::project(&mut app); |
| 2457 | assert!( |
| 2458 | rows[0].label.contains("Needs input") || rows[0].label.contains("1 needs input"), |
| 2459 | "{}", |
| 2460 | rows[0].label |
| 2461 | ); |
| 2462 | let row = rows.iter().find(|row| row.selectable).expect("stale row"); |
| 2463 | assert_eq!(row.mark, "?"); |
| 2464 | assert!(row.detail.starts_with("stale · operation")); |
| 2465 | let Some(SidebarRowAction::InspectWork { |
| 2466 | body, stop_action, .. |
| 2467 | }) = row.primary_action.as_ref() |
| 2468 | else { |
| 2469 | panic!("stale row must open inspector"); |
| 2470 | }; |
| 2471 | assert!( |
| 2472 | stop_action.is_none(), |
| 2473 | "a stale owner cannot truthfully expose a stop action" |
| 2474 | ); |
| 2475 | assert!( |
| 2476 | body.contains("Last bounded output\nNo output receipt"), |
| 2477 | "{body}" |
| 2478 | ); |
| 2479 | assert!(body.contains("Owner cannot confirm liveness"), "{body}"); |
| 2480 | } |
| 2481 | |
| 2482 | /// A durable failed operation, as a fleet agent task from a crashed or |
| 2483 | /// sibling instance leaves behind in the persisted graph (#4416). |
| 2484 | fn durable_failed_operation_graph() -> codewhale_engine::work_graph::WorkGraphSnapshot { |
| 2485 | let mut graph = WorkGraph::from_snapshot(operation_graph(NodeState::Failed)); |
| 2486 | let operation = WorkNodeId::derive(SESSION, "operation"); |
| 2487 | graph |
| 2488 | .apply( |
| 2489 | WorkGraphChange::BindOperation { |
| 2490 | node: operation, |
| 2491 | binding: OperationBinding { |
| 2492 | external: "fleet:run_1/task_1".to_string(), |
| 2493 | durable: true, |
| 2494 | last_observation: None, |
| 2495 | }, |
| 2496 | }, |
| 2497 | ChangeCtx { |
| 2498 | session_id: SESSION.to_string(), |
| 2499 | now: 6, |
| 2500 | idempotency_key: None, |
| 2501 | }, |
| 2502 | ) |
| 2503 | .expect("durable binding"); |
| 2504 | graph.into_snapshot() |
| 2505 | } |
| 2506 | |
| 2507 | // Regression for #4416: a persisted failed-agent record stamped by |
| 2508 | // another session instance (boot id) must not appear in the default |
| 2509 | // work listing of a fresh session in the same workspace. |
| 2510 | #[test] |
| 2511 | fn prior_instance_failed_rows_stay_out_of_the_default_listing() { |
| 2512 | let dir = tempfile::tempdir().expect("tempdir"); |
| 2513 | let manager = |
| 2514 | codewhale_engine::session_manager::SessionManager::new(dir.path().to_path_buf()) |
| 2515 | .expect("manager"); |
| 2516 | manager |
| 2517 | .record_session_boot_owner(SESSION, "boot_other_instance") |
| 2518 | .expect("stamp other instance"); |
| 2519 | |
| 2520 | let mut app = app(); |
| 2521 | app.work_surface.session_owner_probe_dir = Some(dir.path().to_path_buf()); |
| 2522 | let graph = durable_failed_operation_graph(); |
| 2523 | restore_saved_graph(&mut app, &graph); |
| 2524 | |
| 2525 | let rows = super::model::project(&mut app); |
| 2526 | assert!( |
| 2527 | rows.iter() |
| 2528 | .all(|row| !row.label.contains("Verify installed build")), |
| 2529 | "prior-instance failed row leaked into the default listing: {rows:#?}" |
| 2530 | ); |
| 2531 | assert!( |
| 2532 | rows.iter() |
| 2533 | .all(|row| !row.label.contains("needs input") && !row.label.contains("1 active")), |
| 2534 | "prior-instance residue must not count as live work: {rows:#?}" |
| 2535 | ); |
| 2536 | // The record stays reachable through the explicit catalog, clearly |
| 2537 | // marked historical. |
| 2538 | let historical = app |
| 2539 | .work_surface |
| 2540 | .catalog_rows |
| 2541 | .iter() |
| 2542 | .find(|row| row.label.contains("Verify installed build")) |
| 2543 | .expect("historical row remains in the catalog"); |
| 2544 | assert!( |
| 2545 | historical.detail.starts_with("prior session · "), |
| 2546 | "historical row must be labeled: {}", |
| 2547 | historical.detail |
| 2548 | ); |
| 2549 | } |
| 2550 | |
| 2551 | // Ownership control for #4416: the same failed record owned by this |
| 2552 | // session instance still renders as actionable work. |
| 2553 | #[test] |
| 2554 | fn current_instance_failed_rows_still_render_in_the_default_listing() { |
| 2555 | let dir = tempfile::tempdir().expect("tempdir"); |
| 2556 | let manager = |
| 2557 | codewhale_engine::session_manager::SessionManager::new(dir.path().to_path_buf()) |
| 2558 | .expect("manager"); |
| 2559 | manager |
| 2560 | .record_session_boot_owner( |
| 2561 | SESSION, |
| 2562 | codewhale_engine::session_manager::current_session_boot_id(), |
| 2563 | ) |
| 2564 | .expect("stamp current instance"); |
| 2565 | |
| 2566 | let mut app = app(); |
| 2567 | app.work_surface.session_owner_probe_dir = Some(dir.path().to_path_buf()); |
| 2568 | let graph = durable_failed_operation_graph(); |
| 2569 | restore_graph(&mut app, &graph); |
| 2570 | |
| 2571 | let rows = super::model::project(&mut app); |
| 2572 | assert!( |
| 2573 | rows.iter() |
| 2574 | .any(|row| row.label.contains("Verify installed build")), |
| 2575 | "this instance's own failed work must stay visible: {rows:#?}" |
| 2576 | ); |
| 2577 | } |
| 2578 | |
| 2579 | // Regression for review of #5063: if a prior session persisted no graph, |
| 2580 | // the first graph captured later belongs to this process and must not be |
| 2581 | // mistaken for restored residue. |
| 2582 | #[test] |
| 2583 | fn first_live_graph_after_empty_prior_restore_stays_visible() { |
| 2584 | let dir = tempfile::tempdir().expect("tempdir"); |
| 2585 | let manager = |
| 2586 | codewhale_engine::session_manager::SessionManager::new(dir.path().to_path_buf()) |
| 2587 | .expect("manager"); |
| 2588 | manager |
| 2589 | .record_session_boot_owner(SESSION, "boot_other_instance") |
| 2590 | .expect("stamp other instance"); |
| 2591 | |
| 2592 | let mut app = app(); |
| 2593 | app.work_surface.session_owner_probe_dir = Some(dir.path().to_path_buf()); |
| 2594 | app.current_session_id = Some(SESSION.to_string()); |
| 2595 | app.restore_work_state(SESSION, std::path::Path::new("."), None) |
| 2596 | .expect("restore empty prior session"); |
| 2597 | |
| 2598 | let graph = durable_failed_operation_graph(); |
| 2599 | restore_graph(&mut app, &graph); |
| 2600 | let rows = super::model::project(&mut app); |
| 2601 | assert!( |
| 2602 | rows.iter() |
| 2603 | .any(|row| row.label.contains("Verify installed build")), |
| 2604 | "this instance's first live graph must stay visible: {rows:#?}" |
| 2605 | ); |
| 2606 | } |
| 2607 | |
| 2608 | #[test] |
| 2609 | fn completed_operation_with_acceptance_is_not_rendered_done() { |
| 2610 | let mut graph = WorkGraph::from_snapshot(operation_graph(NodeState::Ready)); |
| 2611 | let operation = WorkNodeId::derive(SESSION, "operation"); |
| 2612 | graph |
| 2613 | .apply( |
| 2614 | WorkGraphChange::UpdateNode { |
| 2615 | id: operation, |
| 2616 | patch: codewhale_engine::work_graph::WorkNodePatch { |
| 2617 | state: Some(NodeState::Completed), |
| 2618 | acceptance: Some(vec![AcceptanceRequirement::EvidenceOfKind { |
| 2619 | kind: EvidenceKindTag::ToolRun, |
| 2620 | }]), |
| 2621 | ..codewhale_engine::work_graph::WorkNodePatch::default() |
| 2622 | }, |
| 2623 | }, |
| 2624 | ChangeCtx { |
| 2625 | session_id: SESSION.to_string(), |
| 2626 | now: 6, |
| 2627 | idempotency_key: None, |
| 2628 | }, |
| 2629 | ) |
| 2630 | .expect("completed pending evidence"); |
| 2631 | let graph = graph.into_snapshot(); |
| 2632 | let mut app = app(); |
| 2633 | restore_graph(&mut app, &graph); |
| 2634 | |
| 2635 | let rows = super::model::project(&mut app); |
| 2636 | assert!( |
| 2637 | rows[0].label.contains("Needs input") || rows[0].label.contains("1 needs input"), |
| 2638 | "{}", |
| 2639 | rows[0].label |
| 2640 | ); |
| 2641 | let row = rows |
| 2642 | .iter() |
| 2643 | .find(|row| row.selectable) |
| 2644 | .expect("operation row"); |
| 2645 | assert_eq!(row.mark, codewhale_tui_kit::glyphs::ATTENTION); |
| 2646 | assert!(row.detail.contains("completed · evidence pending")); |
| 2647 | assert_ne!(row.mark, "✓"); |
| 2648 | let Some(SidebarRowAction::InspectWork { body, .. }) = row.primary_action.as_ref() else { |
| 2649 | panic!("completed operation must remain inspectable"); |
| 2650 | }; |
| 2651 | assert!(body.contains("evidence of kind tool run"), "{body}"); |
| 2652 | assert!( |
| 2653 | body.contains("acceptance evidence is still missing"), |
| 2654 | "{body}" |
| 2655 | ); |
| 2656 | } |
| 2657 | |
| 2658 | #[test] |
| 2659 | fn work_rows_open_graph_inspector_without_inline_controls() { |
| 2660 | let mut app = app(); |
| 2661 | app.work_surface.placement = WorkSurfacePlacement::Right; |
| 2662 | app.work_surface.effective_placement = WorkSurfacePlacement::Right; |
| 2663 | let graph = operation_graph(NodeState::Active); |
| 2664 | restore_graph(&mut app, &graph); |
| 2665 | app.runtime_services |
| 2666 | .work |
| 2667 | .as_ref() |
| 2668 | .expect("Work Graph runtime") |
| 2669 | .reconcile_operation( |
| 2670 | SESSION, |
| 2671 | OperationOwnerSnapshot::new("shell:shell_1234abcd", OwnerState::Running, 1, 6), |
| 2672 | ) |
| 2673 | .expect("live shell owner"); |
| 2674 | |
| 2675 | let text = render_text(&mut app, 100, 6); |
| 2676 | assert!(!text.contains("[open]"), "{text}"); |
| 2677 | assert!(!text.contains("[stop]"), "{text}"); |
| 2678 | let row_y = app |
| 2679 | .work_surface |
| 2680 | .hitboxes |
| 2681 | .iter() |
| 2682 | .find(|hit| hit.id.0.starts_with("graph:")) |
| 2683 | .expect("graph hitbox") |
| 2684 | .row_y; |
| 2685 | let outcome = super::handle_mouse( |
| 2686 | &mut app, |
| 2687 | MouseEvent { |
| 2688 | kind: MouseEventKind::Down(MouseButton::Left), |
| 2689 | column: 2, |
| 2690 | row: row_y, |
| 2691 | modifiers: KeyModifiers::NONE, |
| 2692 | }, |
| 2693 | ); |
| 2694 | let action = outcome.action.expect("inspector action"); |
| 2695 | let SidebarRowAction::InspectWork { |
| 2696 | body, stop_action, .. |
| 2697 | } = &action |
| 2698 | else { |
| 2699 | panic!("expected Work inspector"); |
| 2700 | }; |
| 2701 | for section in [ |
| 2702 | "Objective", |
| 2703 | "Prerequisites", |
| 2704 | "Downstream impact", |
| 2705 | "Binding + lifecycle owner", |
| 2706 | "Evidence vs acceptance", |
| 2707 | "Blockers / approvals", |
| 2708 | "Why next", |
| 2709 | "Provenance + last reconcile", |
| 2710 | ] { |
| 2711 | assert!(body.contains(section), "missing {section}: {body}"); |
| 2712 | } |
| 2713 | assert!(matches!( |
| 2714 | stop_action.as_deref(), |
| 2715 | Some(SidebarRowAction::Command(command)) if command == "/jobs cancel shell_1234abcd" |
| 2716 | )); |
| 2717 | crate::tui::mouse_ui::apply_sidebar_row_action(&mut app, action); |
| 2718 | assert_eq!( |
| 2719 | app.view_stack.top_kind(), |
| 2720 | Some(crate::tui::views::ModalKind::Pager) |
| 2721 | ); |
| 2722 | } |
| 2723 | |
| 2724 | #[test] |
| 2725 | fn narrow_render_hover_keeps_full_untruncated_row() { |
| 2726 | let mut app = app(); |
| 2727 | app.todos.try_lock().expect("todos").add( |
| 2728 | "A deliberately long graph-owned work row".to_string(), |
| 2729 | TodoStatus::InProgress, |
| 2730 | ); |
| 2731 | |
| 2732 | let _ = render_text(&mut app, 24, 4); |
| 2733 | let hover = app |
| 2734 | .sidebar_hover |
| 2735 | .sections |
| 2736 | .last() |
| 2737 | .and_then(|section| section.rows.first()) |
| 2738 | .expect("hover row"); |
| 2739 | assert!(hover.is_truncated); |
| 2740 | assert!(hover.full_text.contains("deliberately long graph-owned")); |
| 2741 | assert!(hover.stop_action.is_none()); |
| 2742 | } |
| 2743 | |
| 2744 | #[test] |
| 2745 | fn narrow_file_activity_prioritizes_the_canonical_aggregate_label() { |
| 2746 | let mut app = app(); |
| 2747 | app.workspace = PathBuf::from("/workspace/project"); |
| 2748 | let result = codewhale_engine::tools::spec::ToolResult::success("ok").with_metadata( |
| 2749 | serde_json::json!({ |
| 2750 | "mutation": { |
| 2751 | "diff": "--- a/update.rs\n+++ b/update.rs\n@@ -1 +1 @@\n-old\n+new\n--- /dev/null\n+++ b/create.rs\n@@ -0,0 +1 @@\n+created\n--- a/delete.rs\n+++ /dev/null\n@@ -1 +0,0 @@\n-deleted\n", |
| 2752 | "files": [ |
| 2753 | { "path": "update.rs", "outcome": "updated" }, |
| 2754 | { "path": "create.rs", "outcome": "created" }, |
| 2755 | { "path": "delete.rs", "outcome": "deleted" } |
| 2756 | ], |
| 2757 | "renames": [{ "from": "old.rs", "to": "new.rs" }] |
| 2758 | } |
| 2759 | }), |
| 2760 | ); |
| 2761 | let receipt = FileMutationReceipt::from_success(&app.workspace, &result).expect("receipt"); |
| 2762 | app.add_message(HistoryCell::Tool(ToolCell::PatchSummary( |
| 2763 | PatchSummaryCell { |
| 2764 | path: "4 files".to_string(), |
| 2765 | summary: "ok".to_string(), |
| 2766 | status: ToolStatus::Success, |
| 2767 | error: None, |
| 2768 | receipt: Some(receipt), |
| 2769 | }, |
| 2770 | ))); |
| 2771 | app.tool_details_by_cell.insert( |
| 2772 | 0, |
| 2773 | ToolDetailRecord { |
| 2774 | tool_id: "file-multi".to_string(), |
| 2775 | tool_name: "File".to_string(), |
| 2776 | input: serde_json::json!({"action": "patch"}), |
| 2777 | output: Some("ok".to_string()), |
| 2778 | }, |
| 2779 | ); |
| 2780 | |
| 2781 | app.work_surface.placement = WorkSurfacePlacement::Right; |
| 2782 | app.work_surface.effective_placement = WorkSurfacePlacement::Right; |
| 2783 | let text = render_text(&mut app, 80, 6); |
| 2784 | assert!(text.contains("Wrote 4 files"), "{text}"); |
| 2785 | } |
| 2786 | |
| 2787 | #[test] |
| 2788 | fn overflow_scroll_and_selection_remain_panel_owned() { |
| 2789 | let mut app = app(); |
| 2790 | add_todos(&mut app, 8); |
| 2791 | let _ = render_text(&mut app, 80, 5); |
| 2792 | assert!(app.work_surface.total_rows > app.work_surface.visible_rows); |
| 2793 | |
| 2794 | let transcript_delta = app.viewport.pending_scroll_delta; |
| 2795 | let outcome = super::handle_mouse( |
| 2796 | &mut app, |
| 2797 | MouseEvent { |
| 2798 | kind: MouseEventKind::ScrollDown, |
| 2799 | column: 10, |
| 2800 | row: 2, |
| 2801 | modifiers: KeyModifiers::NONE, |
| 2802 | }, |
| 2803 | ); |
| 2804 | assert!(outcome.consumed); |
| 2805 | assert_eq!(app.viewport.pending_scroll_delta, transcript_delta); |
| 2806 | assert!(app.work_surface.scroll_offset > 0); |
| 2807 | } |
| 2808 | |
| 2809 | #[test] |
| 2810 | fn mouse_wheel_reaches_last_todo_across_top_surface_heights() { |
| 2811 | for height in [3, 5, 6, 8] { |
| 2812 | let mut app = app(); |
| 2813 | add_todos(&mut app, 10); |
| 2814 | let _ = render_text(&mut app, 80, height); |
| 2815 | assert!(app.work_surface.total_rows > app.work_surface.visible_rows); |
| 2816 | let transcript_delta = app.viewport.pending_scroll_delta; |
| 2817 | |
| 2818 | let mut text = String::new(); |
| 2819 | for _ in 0..16 { |
| 2820 | let outcome = super::handle_mouse( |
| 2821 | &mut app, |
| 2822 | MouseEvent { |
| 2823 | kind: MouseEventKind::ScrollDown, |
| 2824 | column: 10, |
| 2825 | row: 1, |
| 2826 | modifiers: KeyModifiers::NONE, |
| 2827 | }, |
| 2828 | ); |
| 2829 | assert!(outcome.consumed, "height {height}"); |
| 2830 | text = render_text(&mut app, 80, height); |
| 2831 | } |
| 2832 | |
| 2833 | assert!( |
| 2834 | text.contains("work item 9"), |
| 2835 | "last To-do was unreachable at surface height {height}: {text:?}" |
| 2836 | ); |
| 2837 | assert_eq!( |
| 2838 | app.work_surface.scroll_offset, |
| 2839 | app.work_surface |
| 2840 | .total_rows |
| 2841 | .saturating_sub(app.work_surface.visible_rows.max(1)), |
| 2842 | "wheel did not reach the legal tail at surface height {height}" |
| 2843 | ); |
| 2844 | assert_eq!(app.viewport.pending_scroll_delta, transcript_delta); |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | #[test] |
| 2849 | fn mouse_wheel_reaches_last_todo_in_side_rail_placements() { |
| 2850 | for placement in [ |
| 2851 | super::WorkSurfacePlacement::Left, |
| 2852 | super::WorkSurfacePlacement::Right, |
| 2853 | ] { |
| 2854 | let mut app = app(); |
| 2855 | add_todos(&mut app, 10); |
| 2856 | app.work_surface.placement = placement; |
| 2857 | app.work_surface.effective_placement = placement; |
| 2858 | let _ = render_text(&mut app, 30, 6); |
| 2859 | |
| 2860 | let mut text = String::new(); |
| 2861 | for _ in 0..16 { |
| 2862 | let outcome = super::handle_mouse( |
| 2863 | &mut app, |
| 2864 | MouseEvent { |
| 2865 | kind: MouseEventKind::ScrollDown, |
| 2866 | column: 10, |
| 2867 | row: 1, |
| 2868 | modifiers: KeyModifiers::NONE, |
| 2869 | }, |
| 2870 | ); |
| 2871 | assert!(outcome.consumed, "placement {placement:?}"); |
| 2872 | text = render_text(&mut app, 30, 6); |
| 2873 | } |
| 2874 | |
| 2875 | assert!( |
| 2876 | text.contains("work item 9"), |
| 2877 | "last To-do was unreachable in {placement:?}: {text:?}" |
| 2878 | ); |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | #[test] |
| 2883 | fn keyboard_end_reveals_last_todo_after_redraw() { |
| 2884 | let mut app = app(); |
| 2885 | add_todos(&mut app, 10); |
| 2886 | let _ = render_text(&mut app, 80, 5); |
| 2887 | let _ = super::handle_key( |
| 2888 | &mut app, |
| 2889 | KeyEvent::new(KeyCode::Char('w'), KeyModifiers::ALT), |
| 2890 | ); |
| 2891 | let _ = super::handle_key(&mut app, KeyEvent::new(KeyCode::End, KeyModifiers::NONE)); |
| 2892 | |
| 2893 | let text = render_text(&mut app, 80, 5); |
| 2894 | |
| 2895 | assert!(text.contains("work item 9"), "{text:?}"); |
| 2896 | assert_eq!( |
| 2897 | app.work_surface.scroll_offset, |
| 2898 | app.work_surface |
| 2899 | .total_rows |
| 2900 | .saturating_sub(app.work_surface.visible_rows.max(1)) |
| 2901 | ); |
| 2902 | } |
| 2903 | |
| 2904 | #[test] |
| 2905 | fn keyboard_navigation_is_panel_local_when_focused() { |
| 2906 | let mut app = app(); |
| 2907 | add_todos(&mut app, 3); |
| 2908 | let _ = render_text(&mut app, 80, super::model::TOP_HEIGHT_MIN); |
| 2909 | assert!( |
| 2910 | super::handle_key( |
| 2911 | &mut app, |
| 2912 | KeyEvent::new(KeyCode::Char('w'), KeyModifiers::ALT) |
| 2913 | ) |
| 2914 | .is_some() |
| 2915 | ); |
| 2916 | let first = app.work_surface.selected.clone(); |
| 2917 | let _ = super::handle_key(&mut app, KeyEvent::new(KeyCode::End, KeyModifiers::NONE)); |
| 2918 | assert_ne!(app.work_surface.selected, first); |
| 2919 | assert!(app.work_surface.focused); |
| 2920 | } |
| 2921 | |
| 2922 | #[test] |
| 2923 | fn clicking_agents_tab_switches_active_panel() { |
| 2924 | let mut app = app(); |
| 2925 | add_todos(&mut app, 1); |
| 2926 | app.subagent_cache.push(cached_worker( |
| 2927 | "agent-tab", |
| 2928 | "explore", |
| 2929 | Some("scout"), |
| 2930 | None, |
| 2931 | SubAgentStatus::Running, |
| 2932 | )); |
| 2933 | let _ = render_text(&mut app, 80, 8); |
| 2934 | // The to-do list opened first; the running worker is one tab over. |
| 2935 | assert_eq!(app.work_surface.panel, super::RailPanel::Tasks); |
| 2936 | let tab_area = app |
| 2937 | .work_surface |
| 2938 | .dock_tabs |
| 2939 | .iter() |
| 2940 | .find(|hitbox| { |
| 2941 | hitbox.target == super::model::DockTabTarget::Panel(super::RailPanel::Agents) |
| 2942 | }) |
| 2943 | .map(|hitbox| hitbox.area) |
| 2944 | .expect("Agents tab"); |
| 2945 | |
| 2946 | let down = super::handle_mouse( |
| 2947 | &mut app, |
| 2948 | MouseEvent { |
| 2949 | kind: MouseEventKind::Down(MouseButton::Left), |
| 2950 | column: tab_area.x, |
| 2951 | row: tab_area.y, |
| 2952 | modifiers: KeyModifiers::NONE, |
| 2953 | }, |
| 2954 | ); |
| 2955 | assert!(down.consumed); |
| 2956 | let up = super::handle_mouse( |
| 2957 | &mut app, |
| 2958 | MouseEvent { |
| 2959 | kind: MouseEventKind::Up(MouseButton::Left), |
| 2960 | column: tab_area.x, |
| 2961 | row: tab_area.y, |
| 2962 | modifiers: KeyModifiers::NONE, |
| 2963 | }, |
| 2964 | ); |
| 2965 | assert!(up.consumed); |
| 2966 | assert_eq!(app.work_surface.panel, super::RailPanel::Agents); |
| 2967 | assert!(app.work_surface.explicit_view); |
| 2968 | assert!(!app.work_surface.dismissed); |
| 2969 | } |
| 2970 | |
| 2971 | #[test] |
| 2972 | fn clicking_active_tab_dismisses_the_dock_until_new_work_arrives() { |
| 2973 | let mut app = app(); |
| 2974 | app.work_surface.top_height = 8; |
| 2975 | add_todos(&mut app, 2); |
| 2976 | let _ = render_text(&mut app, 80, 8); |
| 2977 | let tab_area = app |
| 2978 | .work_surface |
| 2979 | .dock_tabs |
| 2980 | .iter() |
| 2981 | .find(|hitbox| { |
| 2982 | hitbox.target == super::model::DockTabTarget::Panel(super::RailPanel::Tasks) |
| 2983 | }) |
| 2984 | .map(|hitbox| hitbox.area) |
| 2985 | .expect("Tasks tab"); |
| 2986 | |
| 2987 | super::handle_mouse( |
| 2988 | &mut app, |
| 2989 | MouseEvent { |
| 2990 | kind: MouseEventKind::Down(MouseButton::Left), |
| 2991 | column: tab_area.x, |
| 2992 | row: tab_area.y, |
| 2993 | modifiers: KeyModifiers::NONE, |
| 2994 | }, |
| 2995 | ); |
| 2996 | super::handle_mouse( |
| 2997 | &mut app, |
| 2998 | MouseEvent { |
| 2999 | kind: MouseEventKind::Up(MouseButton::Left), |
| 3000 | column: tab_area.x, |
| 3001 | row: tab_area.y, |
| 3002 | modifiers: KeyModifiers::NONE, |
| 3003 | }, |
| 3004 | ); |
| 3005 | assert!(app.work_surface.dismissed); |
| 3006 | assert_eq!( |
| 3007 | super::height(&mut app, 80, 24, AMPLE_BUDGET), |
| 3008 | 0, |
| 3009 | "dismissed dock remains collapsed" |
| 3010 | ); |
| 3011 | |
| 3012 | app.subagent_cache.push(cached_worker( |
| 3013 | "new-work", |
| 3014 | "explore", |
| 3015 | Some("new work"), |
| 3016 | None, |
| 3017 | SubAgentStatus::Running, |
| 3018 | )); |
| 3019 | assert!( |
| 3020 | super::height(&mut app, 80, 24, AMPLE_BUDGET) > 0, |
| 3021 | "new work re-shows dismissed dock" |
| 3022 | ); |
| 3023 | assert!(!app.work_surface.dismissed); |
| 3024 | } |
| 3025 | |
| 3026 | #[test] |
| 3027 | fn dock_tabs_match_80x24_golden() { |
| 3028 | let mut app = app(); |
| 3029 | add_todos(&mut app, 3); |
| 3030 | app.subagent_cache.push(cached_worker( |
| 3031 | "dock-golden-agent", |
| 3032 | "explore", |
| 3033 | Some("scout"), |
| 3034 | None, |
| 3035 | SubAgentStatus::Running, |
| 3036 | )); |
| 3037 | |
| 3038 | assert_matches_golden("dock_80x24", &render_golden_text(&mut app, 80, 24)); |
| 3039 | } |
| 3040 | |
| 3041 | #[test] |
| 3042 | fn dock_selection_is_readable_and_close_target_stays_inside_small_hosts() { |
| 3043 | use super::model::DockTabTarget; |
| 3044 | use ratatui::style::Modifier; |
| 3045 | for theme_id in codewhale_palette::SELECTABLE_THEMES { |
| 3046 | let mut app = app(); |
| 3047 | app.ui_theme = theme_id.ui_theme(); |
| 3048 | app.work_surface.explicit_view = true; |
| 3049 | add_todos(&mut app, 1); |
| 3050 | let mut terminal = Terminal::new(TestBackend::new(80, 8)).unwrap(); |
| 3051 | terminal |
| 3052 | .draw(|frame| super::render(frame, frame.area(), &mut app)) |
| 3053 | .unwrap(); |
| 3054 | let tab = app |
| 3055 | .work_surface |
| 3056 | .dock_tabs |
| 3057 | .iter() |
| 3058 | .find(|tab| tab.target == DockTabTarget::Panel(super::RailPanel::Tasks)) |
| 3059 | .unwrap(); |
| 3060 | let cell = &terminal.backend().buffer()[(tab.area.x + 1, tab.area.y)]; |
| 3061 | assert_eq!(cell.bg, app.ui_theme.composer_bg, "{theme_id:?}"); |
| 3062 | assert!(cell.modifier.contains(Modifier::BOLD), "{theme_id:?}"); |
| 3063 | assert!(!cell.modifier.contains(Modifier::REVERSED), "{theme_id:?}"); |
| 3064 | if let Some(ratio) = codewhale_palette::contrast_ratio(cell.fg, cell.bg) { |
| 3065 | assert!(ratio >= 4.5, "{theme_id:?}: {cell:?} ({ratio})"); |
| 3066 | } else { |
| 3067 | assert_eq!( |
| 3068 | app.ui_theme.composer_bg, |
| 3069 | ratatui::style::Color::Reset, |
| 3070 | "{theme_id:?}" |
| 3071 | ); |
| 3072 | } |
| 3073 | } |
| 3074 | for width in [1, 2, 3, 8, 16, 40, 60, 80] { |
| 3075 | for placement in [WorkSurfacePlacement::Top, WorkSurfacePlacement::Bottom] { |
| 3076 | let mut app = app(); |
| 3077 | app.work_surface.explicit_view = true; |
| 3078 | app.work_surface.effective_placement = placement; |
| 3079 | let mut terminal = Terminal::new(TestBackend::new(width, 8)).unwrap(); |
| 3080 | terminal |
| 3081 | .draw(|frame| super::render(frame, frame.area(), &mut app)) |
| 3082 | .unwrap(); |
| 3083 | let close = app |
| 3084 | .work_surface |
| 3085 | .dock_tabs |
| 3086 | .iter() |
| 3087 | .find(|tab| tab.target == DockTabTarget::Close) |
| 3088 | .unwrap(); |
| 3089 | assert!(close.area.right() <= width); |
| 3090 | assert!(close.area.width > 0); |
| 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | /// #6502: the close control names Esc only while Esc closes the dock. |
| 3096 | /// Unfocused, Esc stops the running turn, so the `×` stands alone and |
| 3097 | /// the Esc hint stays with the turn status. |
| 3098 | #[test] |
| 3099 | fn close_control_names_esc_only_while_esc_closes_the_dock() { |
| 3100 | let tab_row = |app: &mut App| { |
| 3101 | render_rows(app, 80, 8) |
| 3102 | .into_iter() |
| 3103 | .find(|row| row.contains("Tasks")) |
| 3104 | .expect("dock tab row") |
| 3105 | }; |
| 3106 | let mut app = app(); |
| 3107 | add_todos(&mut app, 3); |
| 3108 | app.is_loading = true; |
| 3109 | |
| 3110 | let row = tab_row(&mut app); |
| 3111 | assert!( |
| 3112 | !row.contains("Esc"), |
| 3113 | "unfocused dock must not claim Esc: {row:?}" |
| 3114 | ); |
| 3115 | |
| 3116 | app.work_surface.focused = true; |
| 3117 | let row = tab_row(&mut app); |
| 3118 | assert!( |
| 3119 | row.contains("Esc"), |
| 3120 | "focused dock names its close key: {row:?}" |
| 3121 | ); |
| 3122 | assert!( |
| 3123 | super::handle_key(&mut app, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)).is_some(), |
| 3124 | "the advertised Esc is the one that closes the dock" |
| 3125 | ); |
| 3126 | assert!(app.work_surface.dismissed); |
| 3127 | assert!(!app.work_surface.focused); |
| 3128 | } |
| 3129 | |
| 3130 | /// #6502 review: a row action that opens no view (the Context panel's |
| 3131 | /// `/compact` row) leaves a stale `opened` owner. The hint must follow |
| 3132 | /// what Esc will really do: close the dock when nothing is stacked above |
| 3133 | /// it, and stay quiet while a detail pager owns Esc. |
| 3134 | #[test] |
| 3135 | fn esc_hint_follows_the_pending_detail_state() { |
| 3136 | let tab_row = |app: &mut App| { |
| 3137 | render_rows(app, 80, 8) |
| 3138 | .into_iter() |
| 3139 | .find(|row| row.contains("Tasks")) |
| 3140 | .expect("dock tab row") |
| 3141 | }; |
| 3142 | let mut app = app(); |
| 3143 | add_todos(&mut app, 3); |
| 3144 | app.is_loading = true; |
| 3145 | app.work_surface.focused = true; |
| 3146 | let row = super::model::project(&mut app) |
| 3147 | .into_iter() |
| 3148 | .find(|row| row.selectable) |
| 3149 | .expect("work row"); |
| 3150 | |
| 3151 | // A detail pager is on screen: Esc closes it, not the dock. |
| 3152 | app.work_surface.opened = Some(row.id.clone()); |
| 3153 | app.view_stack.push(crate::tui::pager::PagerView::from_text( |
| 3154 | "Work · test".to_string(), |
| 3155 | "body", |
| 3156 | 40, |
| 3157 | )); |
| 3158 | let rendered = tab_row(&mut app); |
| 3159 | assert!( |
| 3160 | !rendered.contains("Esc"), |
| 3161 | "Esc belongs to the open detail: {rendered:?}" |
| 3162 | ); |
| 3163 | app.view_stack.pop(); |
| 3164 | |
| 3165 | // The row's command opened nothing: `opened` is stale, and the |
| 3166 | // advertised Esc closes the dock in one press. |
| 3167 | let rendered = tab_row(&mut app); |
| 3168 | assert!( |
| 3169 | rendered.contains("Esc"), |
| 3170 | "stale owner must not hide the dock's close key: {rendered:?}" |
| 3171 | ); |
| 3172 | assert!( |
| 3173 | super::handle_key(&mut app, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)).is_some() |
| 3174 | ); |
| 3175 | assert!(app.work_surface.dismissed, "one Esc closes the dock"); |
| 3176 | assert!(app.work_surface.opened.is_none()); |
| 3177 | } |
| 3178 | |
| 3179 | #[test] |
| 3180 | fn narrow_dock_drops_counts_before_optional_tabs() { |
| 3181 | let mut app = app(); |
| 3182 | add_todos(&mut app, 3); |
| 3183 | app.subagent_cache.push(cached_worker( |
| 3184 | "agent-count", |
| 3185 | "explore", |
| 3186 | Some("scout"), |
| 3187 | None, |
| 3188 | SubAgentStatus::Running, |
| 3189 | )); |
| 3190 | let first_row = render_rows(&mut app, 40, 8) |
| 3191 | .into_iter() |
| 3192 | .next() |
| 3193 | .expect("dock tab row"); |
| 3194 | |
| 3195 | assert!(first_row.contains("Tasks"), "{first_row:?}"); |
| 3196 | assert!(first_row.contains("Fleet"), "{first_row:?}"); |
| 3197 | assert!(!first_row.contains("Tasks 3"), "{first_row:?}"); |
| 3198 | assert!(!first_row.contains("Fleet 1"), "{first_row:?}"); |
| 3199 | assert!(first_row.contains("Terminal"), "{first_row:?}"); |
| 3200 | assert!(!first_row.contains("Context"), "{first_row:?}"); |
| 3201 | // Shed from the right: price goes before any work view. |
| 3202 | assert!(!first_row.contains("Cost"), "{first_row:?}"); |
| 3203 | } |
| 3204 | |
| 3205 | #[test] |
| 3206 | fn empty_panel_releases_plain_y_before_composer_dispatch() { |
| 3207 | let mut app = app(); |
| 3208 | app.work_surface.last_area = Some(ratatui::layout::Rect::new(0, 0, 80, 8)); |
| 3209 | app.work_surface.focused = true; |
| 3210 | app.work_surface.explicit_view = false; |
| 3211 | let outcome = super::handle_key( |
| 3212 | &mut app, |
| 3213 | KeyEvent::new(KeyCode::Char('y'), KeyModifiers::NONE), |
| 3214 | ); |
| 3215 | assert!(outcome.is_none()); |
| 3216 | assert!(!app.work_surface.focused); |
| 3217 | } |
| 3218 | |
| 3219 | #[test] |
| 3220 | fn printable_keys_release_panel_focus_for_composer() { |
| 3221 | let mut app = app(); |
| 3222 | add_todos(&mut app, 1); |
| 3223 | let _ = render_text(&mut app, 80, super::model::TOP_HEIGHT_MIN); |
| 3224 | assert!( |
| 3225 | super::handle_key( |
| 3226 | &mut app, |
| 3227 | KeyEvent::new(KeyCode::Char('w'), KeyModifiers::ALT), |
| 3228 | ) |
| 3229 | .is_some() |
| 3230 | ); |
| 3231 | |
| 3232 | let outcome = super::handle_key( |
| 3233 | &mut app, |
| 3234 | KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE), |
| 3235 | ); |
| 3236 | |
| 3237 | assert!(outcome.is_none()); |
| 3238 | assert!(!app.work_surface.focused); |
| 3239 | } |
| 3240 | |
| 3241 | #[test] |
| 3242 | fn side_placements_reuse_the_same_graph_rows() { |
| 3243 | for (placement, expected_chat_x, expected_rail_x) in [ |
| 3244 | (super::WorkSurfacePlacement::Left, 30, 0), |
| 3245 | (super::WorkSurfacePlacement::Right, 0, 70), |
| 3246 | ] { |
| 3247 | let mut app = app(); |
| 3248 | add_todos(&mut app, 2); |
| 3249 | app.work_surface.placement = placement; |
| 3250 | assert_eq!(super::height(&mut app, 100, 24, AMPLE_BUDGET), 0); |
| 3251 | let area = ratatui::layout::Rect::new(0, 0, 100, 12); |
| 3252 | let (chat, rail) = super::split_chat(&mut app, area, 0); |
| 3253 | let rail = rail.expect("side rail"); |
| 3254 | assert_eq!(chat.x, expected_chat_x); |
| 3255 | assert_eq!(rail.x, expected_rail_x); |
| 3256 | assert_eq!(rail.width, 30); |
| 3257 | assert!( |
| 3258 | app.work_surface |
| 3259 | .latest_rows |
| 3260 | .iter() |
| 3261 | .any(|row| row.label == "work item 1") |
| 3262 | ); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | #[test] |
| 3267 | fn divider_drag_resizes_top_left_and_right_surfaces() { |
| 3268 | let mut top = app(); |
| 3269 | add_todos(&mut top, 3); |
| 3270 | let _ = render_text(&mut top, 80, 3); |
| 3271 | let down = super::handle_mouse( |
| 3272 | &mut top, |
| 3273 | MouseEvent { |
| 3274 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3275 | column: 20, |
| 3276 | row: 2, |
| 3277 | modifiers: KeyModifiers::NONE, |
| 3278 | }, |
| 3279 | ); |
| 3280 | assert!(down.consumed); |
| 3281 | let _ = super::handle_mouse( |
| 3282 | &mut top, |
| 3283 | MouseEvent { |
| 3284 | kind: MouseEventKind::Drag(MouseButton::Left), |
| 3285 | column: 20, |
| 3286 | row: 7, |
| 3287 | modifiers: KeyModifiers::NONE, |
| 3288 | }, |
| 3289 | ); |
| 3290 | assert_eq!(top.work_surface.top_height, 8); |
| 3291 | |
| 3292 | for (placement, drag_column, expected_width) in [ |
| 3293 | (WorkSurfacePlacement::Left, 39, 40), |
| 3294 | (WorkSurfacePlacement::Right, 10, 26), |
| 3295 | ] { |
| 3296 | let mut side = app(); |
| 3297 | add_todos(&mut side, 2); |
| 3298 | side.work_surface.placement = placement; |
| 3299 | side.work_surface.effective_placement = placement; |
| 3300 | let _ = render_text(&mut side, 30, 8); |
| 3301 | let divider_column = if placement == WorkSurfacePlacement::Left { |
| 3302 | 29 |
| 3303 | } else { |
| 3304 | 0 |
| 3305 | }; |
| 3306 | let _ = super::handle_mouse( |
| 3307 | &mut side, |
| 3308 | MouseEvent { |
| 3309 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3310 | column: divider_column, |
| 3311 | row: 2, |
| 3312 | modifiers: KeyModifiers::NONE, |
| 3313 | }, |
| 3314 | ); |
| 3315 | let _ = super::handle_mouse( |
| 3316 | &mut side, |
| 3317 | MouseEvent { |
| 3318 | kind: MouseEventKind::Drag(MouseButton::Left), |
| 3319 | column: drag_column, |
| 3320 | row: 2, |
| 3321 | modifiers: KeyModifiers::NONE, |
| 3322 | }, |
| 3323 | ); |
| 3324 | assert_eq!( |
| 3325 | side.work_surface.side_width, expected_width, |
| 3326 | "{placement:?}" |
| 3327 | ); |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | #[test] |
| 3332 | fn divider_hover_and_drag_render_a_discoverable_handle() { |
| 3333 | let mut app = app(); |
| 3334 | add_todos(&mut app, 3); |
| 3335 | let resting = render_text(&mut app, 80, 3); |
| 3336 | assert!(resting.contains('─'), "{resting}"); |
| 3337 | |
| 3338 | let hover = super::handle_mouse( |
| 3339 | &mut app, |
| 3340 | MouseEvent { |
| 3341 | kind: MouseEventKind::Moved, |
| 3342 | column: 20, |
| 3343 | row: 2, |
| 3344 | modifiers: KeyModifiers::NONE, |
| 3345 | }, |
| 3346 | ); |
| 3347 | assert!(hover.consumed); |
| 3348 | assert!(app.work_surface.divider_hovered); |
| 3349 | let hovered = render_text(&mut app, 80, 3); |
| 3350 | assert!(hovered.contains('━'), "{hovered}"); |
| 3351 | |
| 3352 | let _ = super::handle_mouse( |
| 3353 | &mut app, |
| 3354 | MouseEvent { |
| 3355 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3356 | column: 20, |
| 3357 | row: 2, |
| 3358 | modifiers: KeyModifiers::NONE, |
| 3359 | }, |
| 3360 | ); |
| 3361 | let dragging = render_text(&mut app, 80, 3); |
| 3362 | assert!(dragging.contains('━'), "{dragging}"); |
| 3363 | } |
| 3364 | |
| 3365 | #[test] |
| 3366 | fn top_bar_excludes_generic_operations() { |
| 3367 | let mut operation_app = app(); |
| 3368 | let graph = operation_graph(NodeState::Failed); |
| 3369 | restore_graph(&mut operation_app, &graph); |
| 3370 | |
| 3371 | assert_eq!(super::height(&mut operation_app, 100, 24, AMPLE_BUDGET), 0); |
| 3372 | assert!(operation_app.work_surface.latest_rows.is_empty()); |
| 3373 | |
| 3374 | let mut todo_app = app(); |
| 3375 | add_todos(&mut todo_app, 2); |
| 3376 | assert!(super::height(&mut todo_app, 100, 24, AMPLE_BUDGET) > 0); |
| 3377 | assert!( |
| 3378 | todo_app |
| 3379 | .work_surface |
| 3380 | .latest_rows |
| 3381 | .iter() |
| 3382 | .all(|row| row.id.0.starts_with("graph:") || row.id.0.starts_with("worker:")) |
| 3383 | ); |
| 3384 | assert!( |
| 3385 | todo_app |
| 3386 | .work_surface |
| 3387 | .latest_rows |
| 3388 | .iter() |
| 3389 | .all(|row| !row.label.starts_with("Work ·")) |
| 3390 | ); |
| 3391 | } |
| 3392 | |
| 3393 | #[test] |
| 3394 | fn opened_row_toggles_closed_without_losing_selection() { |
| 3395 | let mut app = app(); |
| 3396 | add_todos(&mut app, 1); |
| 3397 | let row = super::model::project(&mut app) |
| 3398 | .into_iter() |
| 3399 | .find(|row| row.selectable) |
| 3400 | .expect("work row"); |
| 3401 | let open = row.primary_action.clone(); |
| 3402 | |
| 3403 | assert!(super::interaction::activate_primary(&mut app, &row.id, open.clone()).is_some()); |
| 3404 | // The action's pager is on screen, so the second activation is a |
| 3405 | // toggle-close. |
| 3406 | app.view_stack.push(crate::tui::pager::PagerView::from_text( |
| 3407 | "Work · test".to_string(), |
| 3408 | "body", |
| 3409 | 40, |
| 3410 | )); |
| 3411 | assert!(super::interaction::activate_primary(&mut app, &row.id, open).is_none()); |
| 3412 | assert!(app.work_surface.opened.is_none()); |
| 3413 | assert_eq!(app.work_surface.selected.as_ref(), Some(&row.id)); |
| 3414 | } |
| 3415 | |
| 3416 | #[test] |
| 3417 | fn a_click_after_the_pager_closed_itself_reopens_instead_of_going_dead() { |
| 3418 | // q/Esc inside the pager pops it without clearing `opened`. The next |
| 3419 | // click on that row must reopen its world, not be swallowed by a |
| 3420 | // stale toggle (owner regression report, 2026-08-04). |
| 3421 | let mut app = app(); |
| 3422 | add_todos(&mut app, 1); |
| 3423 | let row = super::model::project(&mut app) |
| 3424 | .into_iter() |
| 3425 | .find(|row| row.selectable) |
| 3426 | .expect("work row"); |
| 3427 | let open = row.primary_action.clone(); |
| 3428 | |
| 3429 | assert!(super::interaction::activate_primary(&mut app, &row.id, open.clone()).is_some()); |
| 3430 | // The pager was closed from inside itself; `opened` is now stale. |
| 3431 | assert_eq!(app.work_surface.opened.as_ref(), Some(&row.id)); |
| 3432 | assert!(app.view_stack.is_empty()); |
| 3433 | |
| 3434 | let reopened = super::interaction::activate_primary(&mut app, &row.id, open); |
| 3435 | assert!( |
| 3436 | reopened.is_some(), |
| 3437 | "a stale opened owner must not swallow the next activation" |
| 3438 | ); |
| 3439 | assert_eq!(app.work_surface.opened.as_ref(), Some(&row.id)); |
| 3440 | } |
| 3441 | |
| 3442 | /// Settled to-dos keep their rows across the recent-only TTL and new user |
| 3443 | /// turns. Finished sub-agents collapse into the Subagents Archived count |
| 3444 | /// (still reachable via the Agents panel) so fan-outs do not permanently |
| 3445 | /// eat the transcript. |
| 3446 | #[test] |
| 3447 | fn settled_todos_stay_and_finished_workers_collapse_after_ttl() { |
| 3448 | let mut app = app(); |
| 3449 | app.current_session_id = Some(SESSION.to_string()); |
| 3450 | { |
| 3451 | let mut todos = app.todos.try_lock().expect("todos"); |
| 3452 | todos.add("ship the fix".to_string(), TodoStatus::Completed); |
| 3453 | todos.add("verify the fix".to_string(), TodoStatus::Completed); |
| 3454 | } |
| 3455 | app.subagent_cache.push(cached_worker( |
| 3456 | "agent-settled", |
| 3457 | "builder", |
| 3458 | None, |
| 3459 | None, |
| 3460 | SubAgentStatus::Completed, |
| 3461 | )); |
| 3462 | |
| 3463 | app.work_surface.set_presentation_now_ms(0); |
| 3464 | let first = super::model::project_visible(&mut app); |
| 3465 | assert!( |
| 3466 | first.iter().any(|row| row.id.0.starts_with("graph:")), |
| 3467 | "settled to-dos must be listed: {first:?}" |
| 3468 | ); |
| 3469 | assert!( |
| 3470 | !first.iter().any(|row| row.id.0.starts_with("worker:")), |
| 3471 | "workers are the agents view's rows, never the tasks view's: {first:?}" |
| 3472 | ); |
| 3473 | let roster = super::model::visible_rows_for(&mut app, super::RailPanel::Agents); |
| 3474 | assert!( |
| 3475 | roster.iter().any(|row| row.id.0 == "worker:agent-settled"), |
| 3476 | "the roster retains a finished worker: {roster:?}" |
| 3477 | ); |
| 3478 | |
| 3479 | app.work_surface |
| 3480 | .set_presentation_now_ms(super::model::RECENT_ONLY_TTL_MS + 1); |
| 3481 | app.work_surface.note_user_turn_or_new_operation(); |
| 3482 | let later = super::model::project_visible(&mut app); |
| 3483 | assert!( |
| 3484 | later.iter().any(|row| row.id.0.starts_with("graph:")), |
| 3485 | "a settled to-do must survive the TTL and the next user turn: {later:?}" |
| 3486 | ); |
| 3487 | assert!( |
| 3488 | super::height(&mut app, 100, 40, AMPLE_BUDGET) > 0, |
| 3489 | "the strip must keep its height while it holds settled work" |
| 3490 | ); |
| 3491 | } |
| 3492 | |
| 3493 | /// A to-do row says its state in words, in the `/task digest` vocabulary. |
| 3494 | /// Dropping the words (2011b9b11 conflated them with the redundant kind |
| 3495 | /// label) was half of owner regression A1. |
| 3496 | #[test] |
| 3497 | fn todo_rows_carry_their_status_words() { |
| 3498 | let mut app = app(); |
| 3499 | add_todos(&mut app, 3); |
| 3500 | let rows = super::model::project(&mut app); |
| 3501 | let todo_details: Vec<&str> = rows |
| 3502 | .iter() |
| 3503 | .filter(|row| row.id.0.starts_with("graph:")) |
| 3504 | .map(|row| row.detail.as_str()) |
| 3505 | .collect(); |
| 3506 | assert!( |
| 3507 | todo_details.contains(&"in progress"), |
| 3508 | "the active step says so in words: {todo_details:?}" |
| 3509 | ); |
| 3510 | assert!( |
| 3511 | todo_details.contains(&"pending"), |
| 3512 | "a pending step is labeled, not blank: {todo_details:?}" |
| 3513 | ); |
| 3514 | |
| 3515 | // And the words are painted, not just projected. |
| 3516 | let text = render_text(&mut app, 100, 6); |
| 3517 | assert!(text.contains("in progress"), "{text}"); |
| 3518 | assert!(text.contains("pending"), "{text}"); |
| 3519 | } |
| 3520 | |
| 3521 | /// Top strip collapses completed/cancelled workers into an Archived count |
| 3522 | /// while keeping live (and failed) workers as rows. Agents panel still |
| 3523 | /// lists every worker — see the click test below. |
| 3524 | #[test] |
| 3525 | fn the_roster_keeps_live_failed_and_finished_workers() { |
| 3526 | let mut app = app(); |
| 3527 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3528 | app.work_surface.effective_placement = super::WorkSurfacePlacement::Top; |
| 3529 | app.current_session_id = Some(SESSION.to_string()); |
| 3530 | app.subagent_cache.push(cached_worker( |
| 3531 | "agent-live", |
| 3532 | "scout", |
| 3533 | None, |
| 3534 | None, |
| 3535 | SubAgentStatus::Running, |
| 3536 | )); |
| 3537 | app.subagent_cache.push(cached_worker( |
| 3538 | "agent-done", |
| 3539 | "builder", |
| 3540 | None, |
| 3541 | None, |
| 3542 | SubAgentStatus::Completed, |
| 3543 | )); |
| 3544 | app.subagent_cache.push(cached_worker( |
| 3545 | "agent-failed", |
| 3546 | "verifier", |
| 3547 | None, |
| 3548 | None, |
| 3549 | SubAgentStatus::Failed("boom".to_string()), |
| 3550 | )); |
| 3551 | |
| 3552 | // The roster is a history: every worker keeps its row, in every |
| 3553 | // state, and the tasks view never lists one. |
| 3554 | let rows = super::model::visible_rows_for(&mut app, super::RailPanel::Agents); |
| 3555 | let ids: Vec<&str> = rows.iter().map(|row| row.id.0.as_str()).collect(); |
| 3556 | assert!(ids.contains(&"section:agents"), "{ids:?}"); |
| 3557 | for id in [ |
| 3558 | "worker:agent-live", |
| 3559 | "worker:agent-failed", |
| 3560 | "worker:agent-done", |
| 3561 | ] { |
| 3562 | assert!(ids.contains(&id), "{id} in the roster: {ids:?}"); |
| 3563 | } |
| 3564 | let tasks = super::model::project_visible(&mut app); |
| 3565 | assert!( |
| 3566 | !tasks.iter().any(|row| row.id.0.starts_with("worker:")), |
| 3567 | "{tasks:?}" |
| 3568 | ); |
| 3569 | assert_eq!(super::model::live_agent_row_count(&mut app), 2); |
| 3570 | } |
| 3571 | |
| 3572 | #[test] |
| 3573 | fn subagent_header_opens_the_full_agents_register() { |
| 3574 | let mut app = app(); |
| 3575 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3576 | app.work_surface.effective_placement = super::WorkSurfacePlacement::Top; |
| 3577 | app.current_session_id = Some(SESSION.to_string()); |
| 3578 | app.subagent_cache.push(cached_worker( |
| 3579 | "agent-archived", |
| 3580 | "builder", |
| 3581 | None, |
| 3582 | None, |
| 3583 | SubAgentStatus::Completed, |
| 3584 | )); |
| 3585 | |
| 3586 | // Nothing live: the dock stays down until the user opens agents. |
| 3587 | assert_eq!(super::height(&mut app, 100, 24, AMPLE_BUDGET), 0); |
| 3588 | super::interaction::select_dock_panel(&mut app, super::RailPanel::Agents); |
| 3589 | let top = render_text(&mut app, 100, 4); |
| 3590 | assert!(top.contains("Subagents 1"), "{top}"); |
| 3591 | let header_y = app |
| 3592 | .work_surface |
| 3593 | .hitboxes |
| 3594 | .iter() |
| 3595 | .find(|hit| hit.id.0 == "section:agents") |
| 3596 | .expect("subagent header must be a real hit target") |
| 3597 | .row_y; |
| 3598 | let action = super::handle_mouse( |
| 3599 | &mut app, |
| 3600 | MouseEvent { |
| 3601 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3602 | column: 2, |
| 3603 | row: header_y, |
| 3604 | modifiers: KeyModifiers::NONE, |
| 3605 | }, |
| 3606 | ) |
| 3607 | .action |
| 3608 | .expect("subagent header must dispatch its primary action"); |
| 3609 | assert_eq!(action, SidebarRowAction::ShowSubagentsPanel); |
| 3610 | super::interaction::select_dock_panel(&mut app, super::RailPanel::Agents); |
| 3611 | |
| 3612 | let agents = render_text(&mut app, 100, 6); |
| 3613 | assert!( |
| 3614 | agents.contains("agent-archived") || agents.contains("builder"), |
| 3615 | "the full Agents register keeps the archived worker reachable: {agents}" |
| 3616 | ); |
| 3617 | } |
| 3618 | |
| 3619 | #[test] |
| 3620 | fn agent_entry_focuses_a_visible_row_and_esc_returns_to_composer() { |
| 3621 | let mut app = app(); |
| 3622 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3623 | app.work_surface.effective_placement = super::WorkSurfacePlacement::Top; |
| 3624 | app.current_session_id = Some(SESSION.to_string()); |
| 3625 | app.subagent_cache.push(cached_worker( |
| 3626 | "agent-live", |
| 3627 | "builder", |
| 3628 | None, |
| 3629 | None, |
| 3630 | SubAgentStatus::Running, |
| 3631 | )); |
| 3632 | |
| 3633 | let rendered = render_text(&mut app, 100, 8); |
| 3634 | assert!(rendered.contains("builder"), "{rendered}"); |
| 3635 | |
| 3636 | assert!(super::enter_agents(&mut app)); |
| 3637 | assert_eq!(app.work_surface.panel, super::RailPanel::Agents); |
| 3638 | assert!(app.work_surface.focused); |
| 3639 | assert_eq!( |
| 3640 | app.work_surface.selected.as_ref().map(|row| row.0.as_str()), |
| 3641 | Some("worker:agent-live") |
| 3642 | ); |
| 3643 | |
| 3644 | let handled = super::handle_key(&mut app, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); |
| 3645 | assert!(handled.is_some()); |
| 3646 | assert!( |
| 3647 | !app.work_surface.focused, |
| 3648 | "Esc returns ownership to composer" |
| 3649 | ); |
| 3650 | } |
| 3651 | |
| 3652 | #[test] |
| 3653 | fn agent_entry_rejects_a_surface_that_is_not_rendered() { |
| 3654 | let mut app = app(); |
| 3655 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3656 | app.work_surface.effective_placement = super::WorkSurfacePlacement::Top; |
| 3657 | app.current_session_id = Some(SESSION.to_string()); |
| 3658 | app.subagent_cache.push(cached_worker( |
| 3659 | "agent-live", |
| 3660 | "builder", |
| 3661 | None, |
| 3662 | None, |
| 3663 | SubAgentStatus::Running, |
| 3664 | )); |
| 3665 | // A previous frame's rectangle is not evidence that this Agent row |
| 3666 | // was painted. Only the renderer's current hitboxes may transfer |
| 3667 | // keyboard ownership away from the composer. |
| 3668 | app.work_surface.last_area = Some(ratatui::layout::Rect::new(0, 0, 100, 5)); |
| 3669 | assert!(app.work_surface.hitboxes.is_empty()); |
| 3670 | |
| 3671 | assert!(!super::enter_agents(&mut app)); |
| 3672 | assert!( |
| 3673 | !app.work_surface.focused, |
| 3674 | "hidden surface cannot own arrows" |
| 3675 | ); |
| 3676 | } |
| 3677 | |
| 3678 | #[test] |
| 3679 | fn compact_top_surface_keeps_goal_todos_and_named_agent_visible() { |
| 3680 | for (width, terminal_height) in [(160, 48), (120, 32), (80, 24)] { |
| 3681 | let mut app = app(); |
| 3682 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3683 | app.work_surface.panel = super::RailPanel::Tasks; |
| 3684 | app.work_surface.top_height = super::model::TOP_HEIGHT_MIN; |
| 3685 | app.goal.objective = Some("ship the release".to_string()); |
| 3686 | add_todos(&mut app, 3); |
| 3687 | app.current_session_id = Some(SESSION.to_string()); |
| 3688 | app.subagent_cache.push(cached_worker( |
| 3689 | "agent-harbor", |
| 3690 | "builder", |
| 3691 | Some("Harbor"), |
| 3692 | None, |
| 3693 | SubAgentStatus::Running, |
| 3694 | )); |
| 3695 | |
| 3696 | let budget = crate::tui::ui::rail_row_budget(&app, width, terminal_height, false); |
| 3697 | let height = super::height(&mut app, width, terminal_height, budget); |
| 3698 | assert_eq!( |
| 3699 | height, |
| 3700 | super::model::TOP_HEIGHT_MIN, |
| 3701 | "{width}x{terminal_height} must seat the readable compact surface" |
| 3702 | ); |
| 3703 | // The to-do list opens first: goal title + the progress |
| 3704 | // receipt. The named agent lives one view over. |
| 3705 | let rendered = render_text(&mut app, width, height); |
| 3706 | assert!( |
| 3707 | rendered.contains("ship the release"), |
| 3708 | "{width}x{terminal_height}: {rendered}" |
| 3709 | ); |
| 3710 | assert!( |
| 3711 | rendered.contains("3 left"), |
| 3712 | "{width}x{terminal_height}: {rendered}" |
| 3713 | ); |
| 3714 | super::interaction::select_dock_panel(&mut app, super::RailPanel::Agents); |
| 3715 | let height = super::height(&mut app, width, terminal_height, budget); |
| 3716 | let agents = render_text(&mut app, width, height); |
| 3717 | assert!( |
| 3718 | agents.contains("Harbor"), |
| 3719 | "{width}x{terminal_height}: {agents}" |
| 3720 | ); |
| 3721 | app.work_surface.explicit_view = false; |
| 3722 | let height = super::height(&mut app, width, terminal_height, budget); |
| 3723 | let _ = render_text(&mut app, width, height); |
| 3724 | |
| 3725 | assert!(super::enter_agents(&mut app)); |
| 3726 | assert_eq!( |
| 3727 | app.work_surface.selected.as_ref().map(|row| row.0.as_str()), |
| 3728 | Some("worker:agent-harbor"), |
| 3729 | "the advertised Left control must focus the named visible Agent" |
| 3730 | ); |
| 3731 | } |
| 3732 | } |
| 3733 | |
| 3734 | #[test] |
| 3735 | fn starved_surface_cannot_take_keyboard_focus() { |
| 3736 | let mut app = app(); |
| 3737 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3738 | app.work_surface.effective_placement = super::WorkSurfacePlacement::Top; |
| 3739 | app.current_session_id = Some(SESSION.to_string()); |
| 3740 | app.subagent_cache.push(cached_worker( |
| 3741 | "agent-hidden", |
| 3742 | "builder", |
| 3743 | Some("Harbor"), |
| 3744 | None, |
| 3745 | SubAgentStatus::Running, |
| 3746 | )); |
| 3747 | |
| 3748 | assert_eq!(super::height(&mut app, 80, 12, 0), 0); |
| 3749 | assert!(app.work_surface.last_area.is_none()); |
| 3750 | assert!(!super::enter_agents(&mut app)); |
| 3751 | assert!(!app.work_surface.focused); |
| 3752 | assert!( |
| 3753 | super::handle_key( |
| 3754 | &mut app, |
| 3755 | KeyEvent::new(KeyCode::Char('w'), KeyModifiers::ALT), |
| 3756 | ) |
| 3757 | .is_none() |
| 3758 | ); |
| 3759 | assert!(!app.work_surface.focused); |
| 3760 | } |
| 3761 | |
| 3762 | /// Acceptance for owner regression A2: an agent row is a door in the |
| 3763 | /// Agents panel too, and a FINISHED agent's world still opens — the |
| 3764 | /// panel is a standing register, not a live-only view. Since v0.9.7 the |
| 3765 | /// door leads to the agent's transcript (which explains itself when no |
| 3766 | /// capture exists yet), not to the details projection. |
| 3767 | #[test] |
| 3768 | fn agents_panel_click_opens_the_transcript_even_for_finished_agents() { |
| 3769 | let mut app = app(); |
| 3770 | app.work_surface.panel = super::RailPanel::Agents; |
| 3771 | app.current_session_id = Some(SESSION.to_string()); |
| 3772 | app.subagent_cache.push(cached_worker( |
| 3773 | "agent-finished", |
| 3774 | "builder", |
| 3775 | None, |
| 3776 | None, |
| 3777 | SubAgentStatus::Completed, |
| 3778 | )); |
| 3779 | |
| 3780 | let _ = render_text(&mut app, 100, 6); |
| 3781 | let row_y = app |
| 3782 | .work_surface |
| 3783 | .hitboxes |
| 3784 | .iter() |
| 3785 | .find(|hit| hit.id.0 == "worker:agent-finished") |
| 3786 | .expect("finished agent row must keep a hitbox in the Agents panel") |
| 3787 | .row_y; |
| 3788 | let action = super::handle_mouse( |
| 3789 | &mut app, |
| 3790 | MouseEvent { |
| 3791 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3792 | column: 2, |
| 3793 | row: row_y, |
| 3794 | modifiers: KeyModifiers::NONE, |
| 3795 | }, |
| 3796 | ) |
| 3797 | .action |
| 3798 | .expect("click on a finished agent row must dispatch its primary action"); |
| 3799 | assert_eq!( |
| 3800 | action, |
| 3801 | SidebarRowAction::OpenAgentTranscript { |
| 3802 | agent_id: "agent-finished".to_string() |
| 3803 | } |
| 3804 | ); |
| 3805 | crate::tui::mouse_ui::apply_sidebar_row_action(&mut app, action); |
| 3806 | assert!( |
| 3807 | app.agent_focus |
| 3808 | .as_ref() |
| 3809 | .is_some_and(|focus| focus.is("agent-finished")), |
| 3810 | "the finished agent's transcript must actually take focus" |
| 3811 | ); |
| 3812 | } |
| 3813 | |
| 3814 | /// Acceptance for owner regression A1: to-do rows are doors in the |
| 3815 | /// Pinned panel too — clicking one opens the work inspector. |
| 3816 | #[test] |
| 3817 | fn tasks_view_todo_rows_stay_clickable() { |
| 3818 | let mut app = app(); |
| 3819 | app.work_surface.panel = super::RailPanel::Tasks; |
| 3820 | add_todos(&mut app, 2); |
| 3821 | |
| 3822 | let _ = render_text(&mut app, 100, 6); |
| 3823 | let hit = app |
| 3824 | .work_surface |
| 3825 | .hitboxes |
| 3826 | .iter() |
| 3827 | .find(|hit| hit.id.0.starts_with("graph:")) |
| 3828 | .expect("tasks view to-do rows must keep hitboxes") |
| 3829 | .clone(); |
| 3830 | let action = super::handle_mouse( |
| 3831 | &mut app, |
| 3832 | MouseEvent { |
| 3833 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3834 | column: 2, |
| 3835 | row: hit.row_y, |
| 3836 | modifiers: KeyModifiers::NONE, |
| 3837 | }, |
| 3838 | ) |
| 3839 | .action |
| 3840 | .expect("click on a to-do row must dispatch its primary action"); |
| 3841 | assert!( |
| 3842 | matches!(action, SidebarRowAction::InspectWork { .. }), |
| 3843 | "a to-do row opens the work inspector: {action:?}" |
| 3844 | ); |
| 3845 | } |
| 3846 | |
| 3847 | /// Opening the sub-agent register must not hide the to-do list — both |
| 3848 | /// durable surfaces stay visible together (owner report, 0.9.6). The |
| 3849 | /// dock opens on TODO; AGENTS is the next tab (founder, 2026-09-03). |
| 3850 | #[test] |
| 3851 | fn agents_and_tasks_are_separate_views_and_the_dock_opens_on_todo() { |
| 3852 | let mut app = app(); |
| 3853 | app.current_session_id = Some(SESSION.to_string()); |
| 3854 | app.subagent_cache.push(cached_worker( |
| 3855 | "agent-live", |
| 3856 | "scout", |
| 3857 | None, |
| 3858 | None, |
| 3859 | SubAgentStatus::Running, |
| 3860 | )); |
| 3861 | add_todos(&mut app, 2); |
| 3862 | |
| 3863 | // The auto rule: the to-do list first, and only to-dos in it. |
| 3864 | super::model::resolve_view(&mut app); |
| 3865 | assert_eq!(app.work_surface.panel, super::RailPanel::Tasks); |
| 3866 | let ids: Vec<String> = super::model::visible_rows_for_panel(&mut app) |
| 3867 | .iter() |
| 3868 | .map(|row| row.id.0.clone()) |
| 3869 | .collect(); |
| 3870 | assert!(ids.iter().any(|id| id.starts_with("graph:")), "{ids:?}"); |
| 3871 | assert!(!ids.iter().any(|id| id.starts_with("worker:")), "{ids:?}"); |
| 3872 | |
| 3873 | // One key forward: the agents view, the roster only. |
| 3874 | super::cycle_view(&mut app, true); |
| 3875 | assert_eq!(app.work_surface.panel, super::RailPanel::Agents); |
| 3876 | assert!(app.work_surface.explicit_view); |
| 3877 | let ids: Vec<String> = super::model::visible_rows_for_panel(&mut app) |
| 3878 | .iter() |
| 3879 | .map(|row| row.id.0.clone()) |
| 3880 | .collect(); |
| 3881 | assert!(ids.iter().any(|id| id.starts_with("worker:")), "{ids:?}"); |
| 3882 | assert!( |
| 3883 | !ids.iter().any(|id| id.starts_with("graph:")), |
| 3884 | "the agents view is the roster, not the to-do list: {ids:?}" |
| 3885 | ); |
| 3886 | |
| 3887 | // Back, and Esc hands the choice back to the auto rule. |
| 3888 | super::cycle_view(&mut app, false); |
| 3889 | assert_eq!(app.work_surface.panel, super::RailPanel::Tasks); |
| 3890 | let _ = render_text(&mut app, 80, 8); |
| 3891 | assert!(app.work_surface.focused); |
| 3892 | let handled = super::handle_key(&mut app, KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); |
| 3893 | assert!(handled.is_some()); |
| 3894 | assert!(!app.work_surface.explicit_view); |
| 3895 | assert!(app.work_surface.dismissed); |
| 3896 | } |
| 3897 | |
| 3898 | #[test] |
| 3899 | fn cycling_visits_every_view_in_order_and_an_empty_view_still_paints() { |
| 3900 | let mut app = app(); |
| 3901 | add_todos(&mut app, 1); |
| 3902 | let mut seen = vec![]; |
| 3903 | for _ in 0..super::RailPanel::ORDER.len() { |
| 3904 | super::cycle_view(&mut app, true); |
| 3905 | seen.push(app.work_surface.panel); |
| 3906 | let height = super::height(&mut app, 80, 24, AMPLE_BUDGET); |
| 3907 | assert!( |
| 3908 | height > 0, |
| 3909 | "{:?} must keep a strip while explicitly open", |
| 3910 | app.work_surface.panel |
| 3911 | ); |
| 3912 | } |
| 3913 | let mut expected = super::RailPanel::ORDER.to_vec(); |
| 3914 | expected.rotate_left(1); // the fixture starts on tasks, the first tab |
| 3915 | assert_eq!(seen, expected); |
| 3916 | // An empty explicit view names itself instead of going blank. |
| 3917 | super::interaction::select_dock_panel(&mut app, super::RailPanel::Files); |
| 3918 | let text = render_text(&mut app, 80, 5); |
| 3919 | assert!(text.contains("no files touched this session"), "{text}"); |
| 3920 | } |
| 3921 | |
| 3922 | /// The register header is a two-way door: open the Agents panel, then the |
| 3923 | /// same click returns to Tasks, so the to-do list is never stranded. |
| 3924 | #[test] |
| 3925 | fn subagent_header_returns_to_tasks_from_the_agents_view() { |
| 3926 | let mut app = app(); |
| 3927 | app.work_surface.placement = super::WorkSurfacePlacement::Top; |
| 3928 | app.work_surface.effective_placement = super::WorkSurfacePlacement::Top; |
| 3929 | app.current_session_id = Some(SESSION.to_string()); |
| 3930 | app.subagent_cache.push(cached_worker( |
| 3931 | "agent-archived", |
| 3932 | "builder", |
| 3933 | None, |
| 3934 | None, |
| 3935 | SubAgentStatus::Completed, |
| 3936 | )); |
| 3937 | // A finished worker alone opens nothing; the user cycles to agents. |
| 3938 | assert_eq!(super::height(&mut app, 100, 24, AMPLE_BUDGET), 0); |
| 3939 | super::interaction::select_dock_panel(&mut app, super::RailPanel::Agents); |
| 3940 | |
| 3941 | let click_header = |app: &mut App| -> SidebarRowAction { |
| 3942 | let header_y = app |
| 3943 | .work_surface |
| 3944 | .hitboxes |
| 3945 | .iter() |
| 3946 | .find(|hit| hit.id.0 == "section:agents") |
| 3947 | .expect("subagent header is a real hit target") |
| 3948 | .row_y; |
| 3949 | super::handle_mouse( |
| 3950 | app, |
| 3951 | MouseEvent { |
| 3952 | kind: MouseEventKind::Down(MouseButton::Left), |
| 3953 | column: 2, |
| 3954 | row: header_y, |
| 3955 | modifiers: KeyModifiers::NONE, |
| 3956 | }, |
| 3957 | ) |
| 3958 | .action |
| 3959 | .expect("subagent header dispatches its primary action") |
| 3960 | }; |
| 3961 | |
| 3962 | let _ = render_text(&mut app, 100, 6); |
| 3963 | let action = click_header(&mut app); |
| 3964 | assert_eq!(action, SidebarRowAction::ShowSubagentsPanel); |
| 3965 | crate::tui::mouse_ui::apply_sidebar_row_action(&mut app, action); |
| 3966 | assert_eq!( |
| 3967 | app.work_surface.panel, |
| 3968 | super::RailPanel::Tasks, |
| 3969 | "clicking the header inside the register returns to Tasks" |
| 3970 | ); |
| 3971 | } |
| 3972 | |
| 3973 | /// ⌥V opens the selected work row's own details; the transcript pager is |
| 3974 | /// only the fallback when no row is selected (owner report, 0.9.6). |
| 3975 | #[test] |
| 3976 | fn details_chord_opens_the_selected_work_row() { |
| 3977 | let mut app = app(); |
| 3978 | app.current_session_id = Some(SESSION.to_string()); |
| 3979 | app.work_surface.panel = super::RailPanel::Agents; |
| 3980 | add_todos(&mut app, 2); |
| 3981 | let _ = render_text(&mut app, 100, 6); |
| 3982 | |
| 3983 | let rows = super::model::visible_rows_for_panel(&mut app); |
| 3984 | let todo_row = rows |
| 3985 | .iter() |
| 3986 | .find(|row| row.id.0.starts_with("graph:")) |
| 3987 | .expect("a to-do row projects") |
| 3988 | .clone(); |
| 3989 | app.work_surface.focused = true; |
| 3990 | app.work_surface.selected = Some(todo_row.id.clone()); |
| 3991 | |
| 3992 | let handled = super::handle_key( |
| 3993 | &mut app, |
| 3994 | KeyEvent::new(KeyCode::Char('v'), KeyModifiers::ALT), |
| 3995 | ); |
| 3996 | assert!( |
| 3997 | matches!(handled, Some(Some(SidebarRowAction::InspectWork { .. }))), |
| 3998 | "⌥V opens the selected row's own details: {handled:?}" |
| 3999 | ); |
| 4000 | } |
| 4001 | |
| 4002 | /// T1: a real right-click over an agent row opens that row's menu. The |
| 4003 | /// work surface's catch-all arm marked every event consumed, so |
| 4004 | /// `handle_mouse_event` returned before its right-click branch and no |
| 4005 | /// menu ever opened over the dock. |
| 4006 | #[test] |
| 4007 | fn right_click_on_an_agent_row_opens_its_menu() { |
| 4008 | use crate::tui::views::{ContextMenuAction, ModalKind, ViewEvent}; |
| 4009 | use ratatui::{buffer::Buffer, layout::Rect}; |
| 4010 | |
| 4011 | let mut app = app(); |
| 4012 | app.work_surface.panel = super::RailPanel::Agents; |
| 4013 | app.current_session_id = Some(SESSION.to_string()); |
| 4014 | app.subagent_cache.push(cached_worker( |
| 4015 | "agent-live", |
| 4016 | "builder", |
| 4017 | None, |
| 4018 | None, |
| 4019 | SubAgentStatus::Running, |
| 4020 | )); |
| 4021 | let _ = render_text(&mut app, 100, 6); |
| 4022 | let row_y = app |
| 4023 | .work_surface |
| 4024 | .hitboxes |
| 4025 | .iter() |
| 4026 | .find(|hit| hit.id.0 == "worker:agent-live") |
| 4027 | .expect("the live agent row is painted") |
| 4028 | .row_y; |
| 4029 | |
| 4030 | let events = crate::tui::mouse_ui::handle_mouse_event( |
| 4031 | &mut app, |
| 4032 | MouseEvent { |
| 4033 | kind: MouseEventKind::Down(MouseButton::Right), |
| 4034 | column: 2, |
| 4035 | row: row_y, |
| 4036 | modifiers: KeyModifiers::NONE, |
| 4037 | }, |
| 4038 | Instant::now(), |
| 4039 | ); |
| 4040 | assert!(events.is_empty()); |
| 4041 | assert_eq!(app.view_stack.top_kind(), Some(ModalKind::ContextMenu)); |
| 4042 | |
| 4043 | let area = Rect::new(0, 0, 100, 30); |
| 4044 | let mut buf = Buffer::empty(area); |
| 4045 | app.view_stack.render(area, &mut buf); |
| 4046 | let text: String = buf.content().iter().map(|cell| cell.symbol()).collect(); |
| 4047 | assert_eq!(text.matches("Focus agent").count(), 1, "{text}"); |
| 4048 | assert!( |
| 4049 | text.contains("Stop agent…"), |
| 4050 | "a running agent can be stopped" |
| 4051 | ); |
| 4052 | assert!(!text.contains("Message agent") && !text.contains("Open transcript")); |
| 4053 | assert!( |
| 4054 | !text.contains("Command palette"), |
| 4055 | "app chrome stays off a row's menu" |
| 4056 | ); |
| 4057 | |
| 4058 | // Enter runs the primary entry: the same focus a left click runs. |
| 4059 | let events = app |
| 4060 | .view_stack |
| 4061 | .handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); |
| 4062 | assert!( |
| 4063 | matches!( |
| 4064 | events.as_slice(), |
| 4065 | [ViewEvent::ContextMenuSelected { |
| 4066 | action: ContextMenuAction::Row(SidebarRowAction::OpenAgentTranscript { agent_id }), |
| 4067 | }] if agent_id == "agent-live" |
| 4068 | ), |
| 4069 | "{events:?}" |
| 4070 | ); |
| 4071 | } |
| 4072 | |
| 4073 | #[test] |
| 4074 | fn bottom_rounded_frame_separates_top_border_tabs_from_resize_drag() { |
| 4075 | let mut app = app(); |
| 4076 | app.onboarding_workspace_trust_gate = false; |
| 4077 | app.onboarding = crate::tui::app::OnboardingState::None; |
| 4078 | app.ui_theme = codewhale_palette::UNDERWATER_UI_THEME; |
| 4079 | app.viewport.ocean_caps = Some( |
| 4080 | crate::tui::color_compat::ColorCompatBackend::new( |
| 4081 | std::io::sink(), |
| 4082 | codewhale_palette::ColorDepth::TrueColor, |
| 4083 | codewhale_palette::PaletteMode::Dark, |
| 4084 | ) |
| 4085 | .native_ocean_caps(), |
| 4086 | ); |
| 4087 | app.work_surface.placement = WorkSurfacePlacement::Bottom; |
| 4088 | app.work_surface.effective_placement = WorkSurfacePlacement::Bottom; |
| 4089 | app.work_surface.top_height = 6; |
| 4090 | add_todos(&mut app, 4); |
| 4091 | for index in 0..24 { |
| 4092 | app.add_message(HistoryCell::User { |
| 4093 | content: format!("transcript line {index}"), |
| 4094 | }); |
| 4095 | } |
| 4096 | |
| 4097 | let config = Config::default(); |
| 4098 | let mut terminal = Terminal::new(TestBackend::new(100, 30)).expect("terminal"); |
| 4099 | terminal |
| 4100 | .draw(|frame| { |
| 4101 | let _ = crate::tui::ui::render(frame, &mut app, &config); |
| 4102 | }) |
| 4103 | .expect("draw"); |
| 4104 | let buf = terminal.backend().buffer(); |
| 4105 | let composer = app.viewport.last_composer_area.expect("composer area"); |
| 4106 | let strip = app.work_surface.last_area.expect("workbar area"); |
| 4107 | let transcript = app.viewport.last_transcript_area.expect("transcript area"); |
| 4108 | |
| 4109 | assert_eq!(transcript.right(), 100); |
| 4110 | assert_eq!(strip.x, composer.x); |
| 4111 | assert_eq!(strip.right(), composer.right()); |
| 4112 | assert_eq!(buf[(strip.x, strip.y)].symbol(), "╭"); |
| 4113 | assert_eq!(buf[(strip.right() - 1, strip.y)].symbol(), "╮"); |
| 4114 | assert_eq!(buf[(strip.x, strip.bottom() - 1)].symbol(), "╰"); |
| 4115 | assert_eq!(buf[(strip.right() - 1, strip.bottom() - 1)].symbol(), "╯"); |
| 4116 | assert_ne!( |
| 4117 | buf[(strip.x + 1, strip.y + 1)].bg, |
| 4118 | codewhale_palette::UNDERWATER_UI_THEME.composer_bg |
| 4119 | ); |
| 4120 | |
| 4121 | let terminal_tab = app |
| 4122 | .work_surface |
| 4123 | .dock_tabs |
| 4124 | .iter() |
| 4125 | .find(|hitbox| { |
| 4126 | hitbox.target == super::model::DockTabTarget::Panel(super::RailPanel::Terminal) |
| 4127 | }) |
| 4128 | .expect("Terminal tab hitbox") |
| 4129 | .area; |
| 4130 | assert_eq!(terminal_tab.y, strip.y); |
| 4131 | |
| 4132 | let close_tab = app |
| 4133 | .work_surface |
| 4134 | .dock_tabs |
| 4135 | .iter() |
| 4136 | .find(|hitbox| hitbox.target == super::model::DockTabTarget::Close) |
| 4137 | .expect("close hitbox") |
| 4138 | .area; |
| 4139 | assert_eq!(close_tab.y, strip.y); |
| 4140 | let border_col = close_tab.x.saturating_sub(2); |
| 4141 | assert_eq!(buf[(border_col, strip.y)].symbol(), "─"); |
| 4142 | |
| 4143 | let hover_tab = super::handle_mouse( |
| 4144 | &mut app, |
| 4145 | MouseEvent { |
| 4146 | kind: MouseEventKind::Moved, |
| 4147 | column: terminal_tab.x + 1, |
| 4148 | row: strip.y, |
| 4149 | modifiers: KeyModifiers::NONE, |
| 4150 | }, |
| 4151 | ); |
| 4152 | assert!(hover_tab.consumed); |
| 4153 | assert_eq!( |
| 4154 | app.work_surface.hovered_tab, |
| 4155 | Some(super::model::DockTabTarget::Panel( |
| 4156 | super::RailPanel::Terminal |
| 4157 | )) |
| 4158 | ); |
| 4159 | assert!(!app.work_surface.divider_hovered); |
| 4160 | |
| 4161 | let hover_border = super::handle_mouse( |
| 4162 | &mut app, |
| 4163 | MouseEvent { |
| 4164 | kind: MouseEventKind::Moved, |
| 4165 | column: border_col, |
| 4166 | row: strip.y, |
| 4167 | modifiers: KeyModifiers::NONE, |
| 4168 | }, |
| 4169 | ); |
| 4170 | assert!(hover_border.consumed); |
| 4171 | assert!(app.work_surface.divider_hovered); |
| 4172 | assert!(app.work_surface.hovered_tab.is_none()); |
| 4173 | |
| 4174 | let click_down = super::handle_mouse( |
| 4175 | &mut app, |
| 4176 | MouseEvent { |
| 4177 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4178 | column: terminal_tab.x + 1, |
| 4179 | row: strip.y, |
| 4180 | modifiers: KeyModifiers::NONE, |
| 4181 | }, |
| 4182 | ); |
| 4183 | assert!(click_down.consumed); |
| 4184 | assert!(!app.work_surface.resizing); |
| 4185 | let click_up = super::handle_mouse( |
| 4186 | &mut app, |
| 4187 | MouseEvent { |
| 4188 | kind: MouseEventKind::Up(MouseButton::Left), |
| 4189 | column: terminal_tab.x + 1, |
| 4190 | row: strip.y, |
| 4191 | modifiers: KeyModifiers::NONE, |
| 4192 | }, |
| 4193 | ); |
| 4194 | assert!(click_up.consumed); |
| 4195 | assert_eq!(app.work_surface.panel, super::RailPanel::Terminal); |
| 4196 | |
| 4197 | let start_height = strip.height; |
| 4198 | let drag_down = super::handle_mouse( |
| 4199 | &mut app, |
| 4200 | MouseEvent { |
| 4201 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4202 | column: border_col, |
| 4203 | row: strip.y, |
| 4204 | modifiers: KeyModifiers::NONE, |
| 4205 | }, |
| 4206 | ); |
| 4207 | assert!(drag_down.consumed); |
| 4208 | assert!(app.work_surface.resizing); |
| 4209 | let drag_move = super::handle_mouse( |
| 4210 | &mut app, |
| 4211 | MouseEvent { |
| 4212 | kind: MouseEventKind::Drag(MouseButton::Left), |
| 4213 | column: border_col, |
| 4214 | row: strip.y.saturating_sub(2), |
| 4215 | modifiers: KeyModifiers::NONE, |
| 4216 | }, |
| 4217 | ); |
| 4218 | assert!(drag_move.consumed); |
| 4219 | assert_eq!(app.work_surface.top_height, start_height + 2); |
| 4220 | let drag_up = super::handle_mouse( |
| 4221 | &mut app, |
| 4222 | MouseEvent { |
| 4223 | kind: MouseEventKind::Up(MouseButton::Left), |
| 4224 | column: border_col, |
| 4225 | row: strip.y.saturating_sub(2), |
| 4226 | modifiers: KeyModifiers::NONE, |
| 4227 | }, |
| 4228 | ); |
| 4229 | assert!(drag_up.consumed); |
| 4230 | assert!(!app.work_surface.resizing); |
| 4231 | } |
| 4232 | |
| 4233 | #[test] |
| 4234 | fn left_overflow_scrollbar_preserves_resize_divider() { |
| 4235 | let mut app = app(); |
| 4236 | app.work_surface.placement = WorkSurfacePlacement::Left; |
| 4237 | app.work_surface.effective_placement = WorkSurfacePlacement::Left; |
| 4238 | add_todos(&mut app, 10); |
| 4239 | |
| 4240 | let mut terminal = Terminal::new(TestBackend::new(30, 6)).expect("terminal"); |
| 4241 | terminal |
| 4242 | .draw(|frame| super::render(frame, frame.area(), &mut app)) |
| 4243 | .expect("draw"); |
| 4244 | assert!(app.work_surface.total_rows > app.work_surface.visible_rows); |
| 4245 | |
| 4246 | let row_y = app.work_surface.hitboxes[0].row_y; |
| 4247 | let hover_row = super::handle_mouse( |
| 4248 | &mut app, |
| 4249 | MouseEvent { |
| 4250 | kind: MouseEventKind::Moved, |
| 4251 | column: 2, |
| 4252 | row: row_y, |
| 4253 | modifiers: KeyModifiers::NONE, |
| 4254 | }, |
| 4255 | ); |
| 4256 | assert!(hover_row.consumed); |
| 4257 | assert!(app.work_surface.hovered.is_some()); |
| 4258 | |
| 4259 | let hover_scrollbar = super::handle_mouse( |
| 4260 | &mut app, |
| 4261 | MouseEvent { |
| 4262 | kind: MouseEventKind::Moved, |
| 4263 | column: 28, |
| 4264 | row: row_y, |
| 4265 | modifiers: KeyModifiers::NONE, |
| 4266 | }, |
| 4267 | ); |
| 4268 | assert!(hover_scrollbar.consumed); |
| 4269 | assert!(app.work_surface.hovered.is_none()); |
| 4270 | assert!(!app.work_surface.divider_hovered); |
| 4271 | |
| 4272 | let click_scrollbar = super::handle_mouse( |
| 4273 | &mut app, |
| 4274 | MouseEvent { |
| 4275 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4276 | column: 28, |
| 4277 | row: row_y, |
| 4278 | modifiers: KeyModifiers::NONE, |
| 4279 | }, |
| 4280 | ); |
| 4281 | assert!(click_scrollbar.consumed); |
| 4282 | assert!(click_scrollbar.action.is_none()); |
| 4283 | assert!(!app.work_surface.resizing); |
| 4284 | |
| 4285 | let hover_divider = super::handle_mouse( |
| 4286 | &mut app, |
| 4287 | MouseEvent { |
| 4288 | kind: MouseEventKind::Moved, |
| 4289 | column: 29, |
| 4290 | row: 2, |
| 4291 | modifiers: KeyModifiers::NONE, |
| 4292 | }, |
| 4293 | ); |
| 4294 | assert!(hover_divider.consumed); |
| 4295 | assert!(app.work_surface.divider_hovered); |
| 4296 | |
| 4297 | terminal |
| 4298 | .draw(|frame| super::render(frame, frame.area(), &mut app)) |
| 4299 | .expect("draw"); |
| 4300 | let buf = terminal.backend().buffer(); |
| 4301 | for y in 1..5 { |
| 4302 | assert_eq!(buf[(29, y)].symbol(), "┃", "divider cell at y={y}"); |
| 4303 | assert_eq!( |
| 4304 | buf[(29, y)].fg, |
| 4305 | app.ui_theme.accent_primary, |
| 4306 | "divider fg at y={y}" |
| 4307 | ); |
| 4308 | } |
| 4309 | let interior_symbols: Vec<&str> = (1..5).map(|y| buf[(28, y)].symbol()).collect(); |
| 4310 | assert!( |
| 4311 | interior_symbols.contains(&"┃") && interior_symbols.contains(&"│"), |
| 4312 | "{interior_symbols:?}" |
| 4313 | ); |
| 4314 | |
| 4315 | app.work_surface.hovered = Some(app.work_surface.hitboxes[0].id.clone()); |
| 4316 | let drag_start = super::handle_mouse( |
| 4317 | &mut app, |
| 4318 | MouseEvent { |
| 4319 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4320 | column: 29, |
| 4321 | row: 2, |
| 4322 | modifiers: KeyModifiers::NONE, |
| 4323 | }, |
| 4324 | ); |
| 4325 | assert!(drag_start.consumed); |
| 4326 | assert!(app.work_surface.resizing); |
| 4327 | assert!(app.work_surface.hovered.is_none()); |
| 4328 | } |
| 4329 | |
| 4330 | #[test] |
| 4331 | fn unframed_two_row_top_strip_activates_content_row_instead_of_resizing() { |
| 4332 | let mut app = app(); |
| 4333 | app.work_surface.placement = WorkSurfacePlacement::Top; |
| 4334 | app.work_surface.effective_placement = WorkSurfacePlacement::Top; |
| 4335 | add_todos(&mut app, 3); |
| 4336 | |
| 4337 | let _ = render_text(&mut app, 80, 2); |
| 4338 | let row_y = app.work_surface.hitboxes[0].row_y; |
| 4339 | assert_eq!(row_y, 1); |
| 4340 | |
| 4341 | let hover = super::handle_mouse( |
| 4342 | &mut app, |
| 4343 | MouseEvent { |
| 4344 | kind: MouseEventKind::Moved, |
| 4345 | column: 2, |
| 4346 | row: row_y, |
| 4347 | modifiers: KeyModifiers::NONE, |
| 4348 | }, |
| 4349 | ); |
| 4350 | assert!(hover.consumed); |
| 4351 | assert!(!app.work_surface.divider_hovered); |
| 4352 | assert!(app.work_surface.hovered.is_some()); |
| 4353 | |
| 4354 | let click = super::handle_mouse( |
| 4355 | &mut app, |
| 4356 | MouseEvent { |
| 4357 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4358 | column: 2, |
| 4359 | row: row_y, |
| 4360 | modifiers: KeyModifiers::NONE, |
| 4361 | }, |
| 4362 | ); |
| 4363 | assert!(click.consumed); |
| 4364 | assert!(!app.work_surface.resizing); |
| 4365 | assert!(matches!( |
| 4366 | click.action, |
| 4367 | Some(SidebarRowAction::InspectWork { .. }) |
| 4368 | )); |
| 4369 | } |
| 4370 | |
| 4371 | #[test] |
| 4372 | fn framed_vertical_borders_and_right_scrollbar_do_not_activate_rows() { |
| 4373 | let mut app = app(); |
| 4374 | app.work_surface.placement = WorkSurfacePlacement::Bottom; |
| 4375 | app.work_surface.effective_placement = WorkSurfacePlacement::Bottom; |
| 4376 | add_todos(&mut app, 8); |
| 4377 | |
| 4378 | let _ = render_text(&mut app, 80, 6); |
| 4379 | app.work_surface.selected = None; |
| 4380 | app.work_surface.opened = None; |
| 4381 | let row_y = app.work_surface.hitboxes[0].row_y; |
| 4382 | |
| 4383 | for column in [0u16, 79u16] { |
| 4384 | let hover = super::handle_mouse( |
| 4385 | &mut app, |
| 4386 | MouseEvent { |
| 4387 | kind: MouseEventKind::Moved, |
| 4388 | column, |
| 4389 | row: row_y, |
| 4390 | modifiers: KeyModifiers::NONE, |
| 4391 | }, |
| 4392 | ); |
| 4393 | assert!(hover.consumed); |
| 4394 | assert!(app.work_surface.hovered.is_none(), "col={column}"); |
| 4395 | |
| 4396 | let click = super::handle_mouse( |
| 4397 | &mut app, |
| 4398 | MouseEvent { |
| 4399 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4400 | column, |
| 4401 | row: row_y, |
| 4402 | modifiers: KeyModifiers::NONE, |
| 4403 | }, |
| 4404 | ); |
| 4405 | assert!(click.consumed); |
| 4406 | assert!(click.action.is_none(), "col={column}"); |
| 4407 | assert!(app.work_surface.selected.is_none(), "col={column}"); |
| 4408 | } |
| 4409 | |
| 4410 | let inside_hover = super::handle_mouse( |
| 4411 | &mut app, |
| 4412 | MouseEvent { |
| 4413 | kind: MouseEventKind::Moved, |
| 4414 | column: 2, |
| 4415 | row: row_y, |
| 4416 | modifiers: KeyModifiers::NONE, |
| 4417 | }, |
| 4418 | ); |
| 4419 | assert!(inside_hover.consumed); |
| 4420 | assert!(app.work_surface.hovered.is_some()); |
| 4421 | |
| 4422 | let inside_click = super::handle_mouse( |
| 4423 | &mut app, |
| 4424 | MouseEvent { |
| 4425 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4426 | column: 2, |
| 4427 | row: row_y, |
| 4428 | modifiers: KeyModifiers::NONE, |
| 4429 | }, |
| 4430 | ); |
| 4431 | assert!(inside_click.consumed); |
| 4432 | assert!(matches!( |
| 4433 | inside_click.action, |
| 4434 | Some(SidebarRowAction::InspectWork { .. }) |
| 4435 | )); |
| 4436 | |
| 4437 | app.work_surface.hovered_tab = Some(super::model::DockTabTarget::Close); |
| 4438 | let close_tab = app |
| 4439 | .work_surface |
| 4440 | .dock_tabs |
| 4441 | .iter() |
| 4442 | .find(|hitbox| hitbox.target == super::model::DockTabTarget::Close) |
| 4443 | .expect("close tab") |
| 4444 | .area; |
| 4445 | let direct_divider_down = super::handle_mouse( |
| 4446 | &mut app, |
| 4447 | MouseEvent { |
| 4448 | kind: MouseEventKind::Down(MouseButton::Left), |
| 4449 | column: close_tab.x.saturating_sub(2), |
| 4450 | row: 0, |
| 4451 | modifiers: KeyModifiers::NONE, |
| 4452 | }, |
| 4453 | ); |
| 4454 | assert!(direct_divider_down.consumed); |
| 4455 | assert!(app.work_surface.resizing); |
| 4456 | assert!(app.work_surface.hovered_tab.is_none()); |
| 4457 | assert!(app.work_surface.hovered.is_none()); |
| 4458 | } |
| 4459 | |
| 4460 | #[test] |
| 4461 | fn ascii_safe_framed_workbar_uses_ascii_scrollbar_and_border() { |
| 4462 | let mut app = app(); |
| 4463 | app.work_surface.placement = WorkSurfacePlacement::Bottom; |
| 4464 | app.work_surface.effective_placement = WorkSurfacePlacement::Bottom; |
| 4465 | add_todos(&mut app, 8); |
| 4466 | |
| 4467 | let mut terminal = Terminal::new(TestBackend::new(80, 6)).expect("terminal"); |
| 4468 | terminal |
| 4469 | .draw(|frame| super::render::render_with_ascii(frame, frame.area(), &mut app, true)) |
| 4470 | .expect("draw"); |
| 4471 | let buf = terminal.backend().buffer(); |
| 4472 | assert_eq!(buf[(0, 0)].symbol(), "+"); |
| 4473 | assert_eq!(buf[(79, 0)].symbol(), "+"); |
| 4474 | assert_eq!(buf[(0, 5)].symbol(), "+"); |
| 4475 | assert_eq!(buf[(79, 5)].symbol(), "+"); |
| 4476 | assert_eq!(buf[(1, 0)].symbol(), "-"); |
| 4477 | assert_eq!(buf[(1, 5)].symbol(), "-"); |
| 4478 | assert_eq!(buf[(0, 2)].symbol(), "|"); |
| 4479 | |
| 4480 | let close_tab = app |
| 4481 | .work_surface |
| 4482 | .dock_tabs |
| 4483 | .iter() |
| 4484 | .find(|hitbox| hitbox.target == super::model::DockTabTarget::Close) |
| 4485 | .expect("close tab") |
| 4486 | .area; |
| 4487 | let close_text: String = (close_tab.x..close_tab.right()) |
| 4488 | .map(|x| buf[(x, close_tab.y)].symbol()) |
| 4489 | .collect(); |
| 4490 | assert_eq!(close_text, " x "); |
| 4491 | |
| 4492 | let right_edge: Vec<&str> = (1..5).map(|y| buf[(79, y)].symbol()).collect(); |
| 4493 | assert!( |
| 4494 | right_edge.iter().all(|sym| matches!(*sym, "#" | "|")), |
| 4495 | "{right_edge:?}" |
| 4496 | ); |
| 4497 | assert!(right_edge.contains(&"#"), "{right_edge:?}"); |
| 4498 | } |
| 4499 | |
| 4500 | #[test] |
| 4501 | fn fleet_tab_badges_and_never_claims_empty_after_midturn_agent_completion() { |
| 4502 | use crate::tui::subagent_routing::{ |
| 4503 | apply_subagent_terminal_projection, upsert_roster_spawn, upsert_roster_terminal, |
| 4504 | }; |
| 4505 | |
| 4506 | let mut app = app(); |
| 4507 | app.work_surface.placement = WorkSurfacePlacement::Bottom; |
| 4508 | app.work_surface.effective_placement = WorkSurfacePlacement::Bottom; |
| 4509 | app.set_current_session_id(Some(SESSION.to_string())); |
| 4510 | |
| 4511 | for (id, name, status) in [ |
| 4512 | ("agent_one", "scout", SubAgentStatus::Completed), |
| 4513 | ("agent_two", "reviewer", SubAgentStatus::Cancelled), |
| 4514 | ] { |
| 4515 | app.agent_progress |
| 4516 | .insert(id.to_string(), "working".to_string()); |
| 4517 | upsert_roster_spawn( |
| 4518 | &mut app, |
| 4519 | SESSION, |
| 4520 | id, |
| 4521 | "inspect crate", |
| 4522 | Some(AgentWorkerStatus::Running), |
| 4523 | None, |
| 4524 | "gemini-4-hydrogen", |
| 4525 | Some(name), |
| 4526 | ); |
| 4527 | super::model::resolve_view(&mut app); |
| 4528 | assert_eq!(app.work_surface.panel, super::RailPanel::Agents); |
| 4529 | upsert_roster_terminal( |
| 4530 | &mut app, |
| 4531 | SESSION, |
| 4532 | id, |
| 4533 | Some(&status), |
| 4534 | "finished", |
| 4535 | None, |
| 4536 | None, |
| 4537 | Some(name), |
| 4538 | Some(5_000), |
| 4539 | ); |
| 4540 | apply_subagent_terminal_projection(&mut app, id, status, Some("finished".to_string())); |
| 4541 | } |
| 4542 | |
| 4543 | assert!(app.subagent_cache.is_empty()); |
| 4544 | assert!(app.agent_progress.is_empty()); |
| 4545 | assert!(super::height(&mut app, 100, 30, AMPLE_BUDGET) > 0); |
| 4546 | let text = render_text(&mut app, 100, 8); |
| 4547 | assert!(text.contains("Fleet 2"), "{text}"); |
| 4548 | assert!(text.contains("scout"), "{text}"); |
| 4549 | assert!(text.contains("reviewer"), "{text}"); |
| 4550 | assert!( |
| 4551 | !text.to_ascii_lowercase().contains("no agents have run"), |
| 4552 | "{text}" |
| 4553 | ); |
| 4554 | } |
| 4555 | } |
| 4556 |