| 1 | //! Background work that finished since the last notice (#6565). |
| 2 | //! |
| 3 | //! Every terminal transition of background work (a sub-agent, a background |
| 4 | //! shell, a durable task) lands here with the name every other surface shows. |
| 5 | //! The notice for a batch covers all of it, not only the last child to finish. |
| 6 | //! Agent notices use display names. Shells and tasks use generic labels on |
| 7 | //! lock-screen-capable transports; their commands, prompts and errors remain |
| 8 | //! in the in-app rows. Mixed batches also omit previews. |
| 9 | //! |
| 10 | //! When the notice fires is decided by [`ready_to_flush`]. In `final-only` |
| 11 | //! mode it waits only for *finite* work: running agents and queued or running |
| 12 | //! durable tasks. A background shell can run forever (a dev server, a |
| 13 | //! watcher), so a running shell never holds a notice back. |
| 14 | |
| 15 | use std::time::Duration; |
| 16 | |
| 17 | use codewhale_localization::{Locale, MessageId, tr}; |
| 18 | |
| 19 | use codewhale_config::notifications::SubagentCompletionNotification; |
| 20 | use codewhale_engine::tools::subagent::SubAgentStatus; |
| 21 | use codewhale_notify::payload::{NotificationPayload, PREVIEW_MAX_CHARS, sanitize_field}; |
| 22 | |
| 23 | /// Finished background shells kept listed (muted) in the dock. |
| 24 | pub const MAX_FINISHED_SHELLS: usize = 8; |
| 25 | /// Names listed in a batch notice before the rest are counted. |
| 26 | const MAX_NAMED: usize = 4; |
| 27 | /// Characters of a shell command kept as its name. |
| 28 | const SHELL_NAME_CHARS: usize = 48; |
| 29 | |
| 30 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 31 | pub enum FinishedKind { |
| 32 | Agent, |
| 33 | Shell, |
| 34 | Task, |
| 35 | } |
| 36 | |
| 37 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 38 | pub enum FinishedOutcome { |
| 39 | Done, |
| 40 | Failed, |
| 41 | Stopped, |
| 42 | } |
| 43 | |
| 44 | impl FinishedOutcome { |
| 45 | #[must_use] |
| 46 | pub fn from_agent(status: &SubAgentStatus) -> Self { |
| 47 | match status { |
| 48 | SubAgentStatus::Completed | SubAgentStatus::Running => Self::Done, |
| 49 | SubAgentStatus::Failed(_) | SubAgentStatus::BudgetExhausted => Self::Failed, |
| 50 | SubAgentStatus::Cancelled | SubAgentStatus::Interrupted(_) => Self::Stopped, |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// One piece of background work that reached a terminal state. |
| 56 | #[derive(Debug, Clone, PartialEq)] |
| 57 | pub struct FinishedWork { |
| 58 | pub kind: FinishedKind, |
| 59 | /// The name every other surface shows: the agent's label, the shell's |
| 60 | /// command, the task's summary. |
| 61 | pub name: String, |
| 62 | pub outcome: FinishedOutcome, |
| 63 | /// The agent's exact status, so a single-agent notice keeps its |
| 64 | /// status-specific headline ("Agent cancelled", "Budget exhausted"). |
| 65 | pub agent_status: Option<SubAgentStatus>, |
| 66 | /// The agent's result as reported, for the preview headline. |
| 67 | pub result: Option<String>, |
| 68 | /// One line of facts for a shell or task ("exit 0 · 12s", "killed"). |
| 69 | pub summary: Option<String>, |
| 70 | pub elapsed: Duration, |
| 71 | /// Whether the parent turn was running when this finished. A shell or |
| 72 | /// task that finished while the model was working is reported by that |
| 73 | /// turn's own notice, so [`drop_reported_by_turn`] removes it when the |
| 74 | /// turn completes instead of sending a second notice. |
| 75 | pub during_turn: bool, |
| 76 | } |
| 77 | |
| 78 | impl FinishedWork { |
| 79 | #[must_use] |
| 80 | pub fn agent(name: &str, status: &SubAgentStatus, result: &str, elapsed: Duration) -> Self { |
| 81 | Self { |
| 82 | kind: FinishedKind::Agent, |
| 83 | name: name.to_string(), |
| 84 | outcome: FinishedOutcome::from_agent(status), |
| 85 | agent_status: Some(status.clone()), |
| 86 | result: Some(result.to_string()), |
| 87 | summary: None, |
| 88 | elapsed, |
| 89 | during_turn: false, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | pub(crate) fn shell_display_label(command: &str, description: Option<&str>) -> String { |
| 94 | let normalized_description = description |
| 95 | .map(|value| value.split_whitespace().collect::<Vec<_>>().join(" ")) |
| 96 | .filter(|value| !value.is_empty()); |
| 97 | if let Some(description) = normalized_description { |
| 98 | description |
| 99 | } else { |
| 100 | command.split_whitespace().collect::<Vec<_>>().join(" ") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #[must_use] |
| 105 | pub fn shell( |
| 106 | command: &str, |
| 107 | description: Option<&str>, |
| 108 | outcome: FinishedOutcome, |
| 109 | summary: String, |
| 110 | elapsed: Duration, |
| 111 | ) -> Self { |
| 112 | let label = Self::shell_display_label(command, description); |
| 113 | let name = if label.chars().count() > SHELL_NAME_CHARS { |
| 114 | let kept: String = label.chars().take(SHELL_NAME_CHARS - 1).collect(); |
| 115 | format!("{kept}…") |
| 116 | } else { |
| 117 | label |
| 118 | }; |
| 119 | Self { |
| 120 | kind: FinishedKind::Shell, |
| 121 | name, |
| 122 | outcome, |
| 123 | agent_status: None, |
| 124 | result: None, |
| 125 | summary: Some(summary), |
| 126 | elapsed, |
| 127 | during_turn: false, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #[must_use] |
| 132 | pub fn task(name: &str, outcome: FinishedOutcome, summary: String, elapsed: Duration) -> Self { |
| 133 | Self { |
| 134 | kind: FinishedKind::Task, |
| 135 | name: name.trim().to_string(), |
| 136 | outcome, |
| 137 | agent_status: None, |
| 138 | result: None, |
| 139 | summary: Some(summary), |
| 140 | elapsed, |
| 141 | during_turn: false, |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /// Marks this item as finished while the parent turn was running. |
| 146 | #[must_use] |
| 147 | pub fn in_turn(mut self, parent_busy: bool) -> Self { |
| 148 | self.during_turn = parent_busy; |
| 149 | self |
| 150 | } |
| 151 | |
| 152 | /// Agent result headline, reserving room for the full-result pointer |
| 153 | /// after sanitization and the optional batch label have consumed space. |
| 154 | fn preview(&self, locale: Locale, named: bool) -> Option<String> { |
| 155 | if self.kind != FinishedKind::Agent { |
| 156 | return None; |
| 157 | } |
| 158 | let result = self.result.as_deref()?; |
| 159 | let headline = codewhale_engine::agent_roster::result_headline(result)?; |
| 160 | let bounded = crate::tui::notifications::text_summary(&headline)?; |
| 161 | let whole = result |
| 162 | .lines() |
| 163 | .map(str::trim) |
| 164 | .filter(|line| !line.is_empty() && !line.starts_with("<codewhale:")) |
| 165 | .collect::<Vec<_>>() |
| 166 | .join(" "); |
| 167 | let preview = sanitize_field(&if named { |
| 168 | format!("{}: {bounded}", self.name) |
| 169 | } else { |
| 170 | bounded.clone() |
| 171 | }); |
| 172 | if bounded == whole && preview.chars().count() <= PREVIEW_MAX_CHARS { |
| 173 | return Some(preview); |
| 174 | } |
| 175 | let suffix = format!( |
| 176 | " … {}", |
| 177 | tr(locale, MessageId::NotificationFullResultPointer) |
| 178 | ); |
| 179 | let budget = PREVIEW_MAX_CHARS.saturating_sub(suffix.chars().count()); |
| 180 | let kept: String = preview.chars().take(budget).collect(); |
| 181 | Some(format!( |
| 182 | "{}{suffix}", |
| 183 | kept.trim_end_matches("...").trim_end() |
| 184 | )) |
| 185 | } |
| 186 | |
| 187 | fn headline_id(&self) -> MessageId { |
| 188 | match (self.kind, self.outcome) { |
| 189 | (FinishedKind::Agent, _) => crate::tui::notifications::subagent_terminal_label( |
| 190 | self.agent_status |
| 191 | .as_ref() |
| 192 | .unwrap_or(&SubAgentStatus::Completed), |
| 193 | ), |
| 194 | (FinishedKind::Shell, FinishedOutcome::Done) => MessageId::NotificationShellFinished, |
| 195 | (FinishedKind::Shell, FinishedOutcome::Failed) => MessageId::NotificationShellFailed, |
| 196 | (FinishedKind::Shell, FinishedOutcome::Stopped) => MessageId::NotificationShellStopped, |
| 197 | (FinishedKind::Task, FinishedOutcome::Done) => MessageId::NotificationTaskFinished, |
| 198 | (FinishedKind::Task, FinishedOutcome::Failed) => MessageId::NotificationTaskFailed, |
| 199 | (FinishedKind::Task, FinishedOutcome::Stopped) => MessageId::NotificationTaskStopped, |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// Drop the shells and tasks that finished while the parent turn was running. |
| 205 | /// |
| 206 | /// Called when that turn completes: its own notice covers them, so they must |
| 207 | /// not go out as a second notice. Agents keep their own notice. |
| 208 | pub fn drop_reported_by_turn(batch: &mut Vec<FinishedWork>) { |
| 209 | batch.retain(|item| item.kind == FinishedKind::Agent || !item.during_turn); |
| 210 | } |
| 211 | |
| 212 | /// Whether the pending batch should be announced now. |
| 213 | /// |
| 214 | /// - `off` drains the batch without a notice. |
| 215 | /// - `always` announces every item as it arrives. |
| 216 | /// - `final-only` waits while finite work is still live (running agents, a |
| 217 | /// running workflow, queued or running durable tasks). Running shells are |
| 218 | /// open-ended and never count. A batch with no agent in it also waits for |
| 219 | /// the parent turn to go idle; when that turn completes, |
| 220 | /// [`drop_reported_by_turn`] removes the shells and tasks that finished |
| 221 | /// during it, so they are reported by the turn, not by a second notice. |
| 222 | #[must_use] |
| 223 | pub fn ready_to_flush( |
| 224 | mode: SubagentCompletionNotification, |
| 225 | batch: &[FinishedWork], |
| 226 | finite_work_live: bool, |
| 227 | parent_busy: bool, |
| 228 | ) -> bool { |
| 229 | if batch.is_empty() { |
| 230 | return false; |
| 231 | } |
| 232 | match mode { |
| 233 | SubagentCompletionNotification::Off | SubagentCompletionNotification::Always => true, |
| 234 | SubagentCompletionNotification::FinalOnly => { |
| 235 | !finite_work_live |
| 236 | && (!parent_busy || batch.iter().any(|item| item.kind == FinishedKind::Agent)) |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// One notice for a batch of finished background work. |
| 242 | /// |
| 243 | /// A single item keeps its own headline ("Agent complete", "Shell failed"), |
| 244 | /// names itself in the detail and previews its result. A batch counts what |
| 245 | /// finished ("3 finished", or "2 done · 1 failed" when something did not |
| 246 | /// succeed), names each item (the first few, then `+N`), and previews the |
| 247 | /// item most worth reading: the first that did not succeed, else the first. |
| 248 | #[must_use] |
| 249 | pub fn background_finished_payload( |
| 250 | locale: Locale, |
| 251 | batch: &[FinishedWork], |
| 252 | include_summary: bool, |
| 253 | ) -> Option<NotificationPayload> { |
| 254 | let elapsed = batch.iter().map(|item| item.elapsed).max()?; |
| 255 | let headline = match batch { |
| 256 | [single] => tr(locale, single.headline_id()).into_owned(), |
| 257 | many => { |
| 258 | let done = many |
| 259 | .iter() |
| 260 | .filter(|item| item.outcome == FinishedOutcome::Done) |
| 261 | .count(); |
| 262 | let failed = many |
| 263 | .iter() |
| 264 | .filter(|item| item.outcome == FinishedOutcome::Failed) |
| 265 | .count(); |
| 266 | let stopped = many |
| 267 | .iter() |
| 268 | .filter(|item| item.outcome == FinishedOutcome::Stopped) |
| 269 | .count(); |
| 270 | if stopped > 0 { |
| 271 | tr(locale, MessageId::NotificationBackgroundStopped) |
| 272 | .replace("{done}", &done.to_string()) |
| 273 | .replace("{failed}", &failed.to_string()) |
| 274 | .replace("{stopped}", &stopped.to_string()) |
| 275 | } else if done == many.len() { |
| 276 | tr(locale, MessageId::NotificationBackgroundFinished) |
| 277 | .replace("{count}", &many.len().to_string()) |
| 278 | } else { |
| 279 | tr(locale, MessageId::NotificationBackgroundMixed) |
| 280 | .replace("{done}", &done.to_string()) |
| 281 | .replace("{failed}", &failed.to_string()) |
| 282 | } |
| 283 | } |
| 284 | }; |
| 285 | let headline = |
| 286 | crate::tui::notifications::completion_status(&headline, include_summary, elapsed, None); |
| 287 | let mut names = batch |
| 288 | .iter() |
| 289 | .take(MAX_NAMED) |
| 290 | .map(|item| match item.kind { |
| 291 | FinishedKind::Agent => std::borrow::Cow::Borrowed(item.name.as_str()), |
| 292 | FinishedKind::Shell => tr(locale, MessageId::AgentFocusPostureShellFull), |
| 293 | FinishedKind::Task => tr(locale, MessageId::WorkflowDispatchFallbackTask), |
| 294 | }) |
| 295 | .collect::<Vec<_>>() |
| 296 | .join(" · "); |
| 297 | if batch.len() > MAX_NAMED { |
| 298 | names.push_str(&format!(" · +{}", batch.len() - MAX_NAMED)); |
| 299 | } |
| 300 | if batch.iter().any(|item| item.kind != FinishedKind::Agent) { |
| 301 | return Some(NotificationPayload::background_terminal(&headline, &names)); |
| 302 | } |
| 303 | let featured = batch |
| 304 | .iter() |
| 305 | .find(|item| item.outcome != FinishedOutcome::Done) |
| 306 | .unwrap_or(&batch[0]); |
| 307 | let preview = featured.preview(locale, batch.len() > 1); |
| 308 | Some(NotificationPayload::subagent_terminal(&headline, &names).with_preview(preview.as_deref())) |
| 309 | } |
| 310 | |
| 311 | #[cfg(test)] |
| 312 | mod tests { |
| 313 | use super::*; |
| 314 | use codewhale_config::notifications::SubagentCompletionNotification as Mode; |
| 315 | |
| 316 | fn agent(name: &str, status: SubAgentStatus, result: &str) -> FinishedWork { |
| 317 | FinishedWork::agent(name, &status, result, Duration::from_secs(30)) |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn final_only_waits_for_finite_work_but_never_for_a_running_shell() { |
| 322 | let batch = vec![agent("explore", SubAgentStatus::Completed, "Done.")]; |
| 323 | // Another agent is still running: hold. |
| 324 | assert!(!ready_to_flush(Mode::FinalOnly, &batch, true, false)); |
| 325 | // Only an `npm run dev` shell is still running: shells are not finite |
| 326 | // work, so the caller reports no finite work and the notice fires. |
| 327 | assert!(ready_to_flush(Mode::FinalOnly, &batch, false, false)); |
| 328 | // An agent batch does not wait for the parent turn. |
| 329 | assert!(ready_to_flush(Mode::FinalOnly, &batch, false, true)); |
| 330 | // A shell-only batch waits for the parent turn to go idle. |
| 331 | let shells = vec![FinishedWork::shell( |
| 332 | "cargo build", |
| 333 | None, |
| 334 | FinishedOutcome::Done, |
| 335 | "exit 0 · 12s".to_string(), |
| 336 | Duration::from_secs(12), |
| 337 | )]; |
| 338 | assert!(!ready_to_flush(Mode::FinalOnly, &shells, false, true)); |
| 339 | assert!(ready_to_flush(Mode::FinalOnly, &shells, false, false)); |
| 340 | assert!(ready_to_flush(Mode::Always, &batch, true, true)); |
| 341 | assert!(ready_to_flush(Mode::Off, &batch, true, true)); |
| 342 | assert!(!ready_to_flush(Mode::Always, &[], false, false)); |
| 343 | } |
| 344 | |
| 345 | #[test] |
| 346 | fn a_batch_names_everything_that_finished_and_counts_failures() { |
| 347 | let batch = vec![ |
| 348 | agent("explore", SubAgentStatus::Completed, "Found 3 call sites."), |
| 349 | agent( |
| 350 | "review", |
| 351 | SubAgentStatus::Failed("boom".to_string()), |
| 352 | "provider returned 429", |
| 353 | ), |
| 354 | agent( |
| 355 | "audit docs", |
| 356 | SubAgentStatus::Completed, |
| 357 | "All links resolve.", |
| 358 | ), |
| 359 | ]; |
| 360 | let payload = background_finished_payload(Locale::En, &batch, false).expect("payload"); |
| 361 | assert_eq!(payload.headline(), "2 done · 1 failed"); |
| 362 | assert_eq!(payload.detail(), Some("explore · review · audit docs")); |
| 363 | assert_eq!(payload.preview(), Some("review: provider returned 429")); |
| 364 | |
| 365 | let done = vec![ |
| 366 | agent("explore", SubAgentStatus::Completed, "One."), |
| 367 | agent("audit docs", SubAgentStatus::Completed, "Two."), |
| 368 | agent("review", SubAgentStatus::Completed, "Three."), |
| 369 | ]; |
| 370 | let payload = background_finished_payload(Locale::En, &done, false).expect("payload"); |
| 371 | assert_eq!(payload.headline(), "3 finished"); |
| 372 | assert_eq!(payload.preview(), Some("explore: One.")); |
| 373 | |
| 374 | let many = (0..6) |
| 375 | .map(|n| agent(&format!("lane {n}"), SubAgentStatus::Completed, "ok.")) |
| 376 | .collect::<Vec<_>>(); |
| 377 | let payload = background_finished_payload(Locale::En, &many, false).expect("payload"); |
| 378 | assert_eq!( |
| 379 | payload.detail(), |
| 380 | Some("lane 0 · lane 1 · lane 2 · lane 3 · +2") |
| 381 | ); |
| 382 | assert!(background_finished_payload(Locale::En, &[], false).is_none()); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn a_cut_preview_says_so_and_points_at_the_full_result() { |
| 387 | let single = vec![agent( |
| 388 | "audit docs", |
| 389 | SubAgentStatus::Completed, |
| 390 | "## Summary\n\nThree links are stale. Two are in README.md.", |
| 391 | )]; |
| 392 | let payload = background_finished_payload(Locale::En, &single, false).expect("payload"); |
| 393 | assert_eq!(payload.headline(), "Agent complete"); |
| 394 | assert_eq!(payload.detail(), Some("audit docs")); |
| 395 | assert_eq!( |
| 396 | payload.preview(), |
| 397 | Some("Three links are stale. … open Codewhale for the full result") |
| 398 | ); |
| 399 | // A result that is only its headline is shown as it is. |
| 400 | let whole = vec![agent("explore", SubAgentStatus::Completed, "Found it.")]; |
| 401 | let payload = background_finished_payload(Locale::En, &whole, false).expect("payload"); |
| 402 | assert_eq!(payload.preview(), Some("Found it.")); |
| 403 | } |
| 404 | |
| 405 | #[test] |
| 406 | fn shell_notices_keep_outcomes_but_commands_and_output_stay_in_app() { |
| 407 | for (outcome, summary, headline) in [ |
| 408 | (FinishedOutcome::Done, "exit 0 · 12s", "Shell finished"), |
| 409 | (FinishedOutcome::Failed, "failed · exit 2", "Shell failed"), |
| 410 | (FinishedOutcome::Stopped, "killed", "Shell stopped"), |
| 411 | (FinishedOutcome::Stopped, "timed out", "Shell stopped"), |
| 412 | ] { |
| 413 | let batch = vec![FinishedWork::shell( |
| 414 | "npm test --\n --watch=false", |
| 415 | None, |
| 416 | outcome, |
| 417 | summary.to_string(), |
| 418 | Duration::from_secs(12), |
| 419 | )]; |
| 420 | let payload = background_finished_payload(Locale::En, &batch, false).expect("payload"); |
| 421 | assert_eq!(payload.headline(), headline); |
| 422 | assert_eq!(payload.detail(), Some("shell")); |
| 423 | assert_eq!(payload.preview(), None); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | #[test] |
| 428 | fn background_review_stopped_work_is_never_counted_as_failed() { |
| 429 | let stopped = agent("cancelled", SubAgentStatus::Cancelled, "Stopped."); |
| 430 | let stopped_shell = FinishedWork::shell( |
| 431 | "sleep 99", |
| 432 | None, |
| 433 | FinishedOutcome::Stopped, |
| 434 | "killed".into(), |
| 435 | Duration::ZERO, |
| 436 | ); |
| 437 | let cases = [ |
| 438 | ( |
| 439 | vec![stopped.clone(), stopped_shell.clone()], |
| 440 | "0 done · 0 failed · 2 stopped", |
| 441 | ), |
| 442 | ( |
| 443 | vec![ |
| 444 | agent("done", SubAgentStatus::Completed, "Done."), |
| 445 | stopped.clone(), |
| 446 | ], |
| 447 | "1 done · 0 failed · 1 stopped", |
| 448 | ), |
| 449 | ( |
| 450 | vec![ |
| 451 | agent("failed", SubAgentStatus::Failed("failed".into()), "Failed."), |
| 452 | stopped_shell, |
| 453 | ], |
| 454 | "0 done · 1 failed · 1 stopped", |
| 455 | ), |
| 456 | ( |
| 457 | vec![ |
| 458 | agent("done", SubAgentStatus::Completed, "Done."), |
| 459 | agent("failed", SubAgentStatus::Failed("failed".into()), "Failed."), |
| 460 | stopped, |
| 461 | ], |
| 462 | "1 done · 1 failed · 1 stopped", |
| 463 | ), |
| 464 | ]; |
| 465 | for (batch, expected) in cases { |
| 466 | assert_eq!( |
| 467 | background_finished_payload(Locale::En, &batch, false) |
| 468 | .unwrap() |
| 469 | .headline(), |
| 470 | expected |
| 471 | ); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn background_review_notifications_hide_commands_and_task_errors() { |
| 477 | let shell = FinishedWork::shell( |
| 478 | "mysql -psecret123 private_customer", |
| 479 | None, |
| 480 | FinishedOutcome::Failed, |
| 481 | "private output".into(), |
| 482 | Duration::ZERO, |
| 483 | ); |
| 484 | let task = FinishedWork::task( |
| 485 | "private account", |
| 486 | FinishedOutcome::Failed, |
| 487 | "private customer error".into(), |
| 488 | Duration::ZERO, |
| 489 | ); |
| 490 | for batch in [ |
| 491 | vec![shell.clone()], |
| 492 | vec![task.clone()], |
| 493 | vec![ |
| 494 | agent("review", SubAgentStatus::Completed, "Done."), |
| 495 | shell, |
| 496 | task, |
| 497 | ], |
| 498 | ] { |
| 499 | let payload = background_finished_payload(Locale::En, &batch, false).unwrap(); |
| 500 | assert_eq!( |
| 501 | payload.kind(), |
| 502 | codewhale_notify::payload::NotificationKind::BackgroundTerminal |
| 503 | ); |
| 504 | assert_eq!( |
| 505 | payload |
| 506 | .clone() |
| 507 | .with_preview(Some("mysql -psecret123")) |
| 508 | .preview(), |
| 509 | None |
| 510 | ); |
| 511 | for private in ["mysql", "secret123", "private", "customer", "account"] { |
| 512 | assert!( |
| 513 | !payload.render_inline().contains(private), |
| 514 | "{}", |
| 515 | payload.render_inline() |
| 516 | ); |
| 517 | } |
| 518 | } |
| 519 | let batch = [agent( |
| 520 | "review https://user:shortpw@example.test", |
| 521 | SubAgentStatus::Completed, |
| 522 | "Done.", |
| 523 | )]; |
| 524 | let payload = background_finished_payload(Locale::En, &batch, false).unwrap(); |
| 525 | assert!(!payload.render_inline().contains("shortpw")); |
| 526 | assert!(!payload.detail().unwrap().contains("user:")); |
| 527 | assert!(payload.detail().unwrap().contains("example.test")); |
| 528 | } |
| 529 | |
| 530 | #[test] |
| 531 | fn shell_prefers_normalized_description_and_preserves_lock_privacy() { |
| 532 | let described = FinishedWork::shell( |
| 533 | "mysql -psecret123 private_customer", |
| 534 | Some(" run\t workspace diagnostics "), |
| 535 | FinishedOutcome::Done, |
| 536 | "exit 0 · 2s".into(), |
| 537 | Duration::from_secs(2), |
| 538 | ); |
| 539 | assert_eq!(described.name, "run workspace diagnostics"); |
| 540 | |
| 541 | let fallback = FinishedWork::shell( |
| 542 | " cargo test\n -p codewhale-tui ", |
| 543 | Some(" \t\n "), |
| 544 | FinishedOutcome::Done, |
| 545 | "exit 0 · 2s".into(), |
| 546 | Duration::from_secs(2), |
| 547 | ); |
| 548 | assert_eq!(fallback.name, "cargo test -p codewhale-tui"); |
| 549 | |
| 550 | let payload = background_finished_payload(Locale::En, &[described], false).unwrap(); |
| 551 | assert!(!payload.render_inline().contains("diagnostics")); |
| 552 | assert!(!payload.render_inline().contains("secret123")); |
| 553 | } |
| 554 | |
| 555 | #[test] |
| 556 | fn background_review_long_previews_keep_the_localized_full_result_pointer() { |
| 557 | for &locale in Locale::shipped_complete() { |
| 558 | for text in [ |
| 559 | "A long first sentence ".repeat(30), |
| 560 | "詳しい結果 ".repeat(90), |
| 561 | ] { |
| 562 | for named in [false, true] { |
| 563 | let mut batch = vec![agent( |
| 564 | &"review ".repeat(40), |
| 565 | SubAgentStatus::Completed, |
| 566 | &text, |
| 567 | )]; |
| 568 | if named { |
| 569 | batch.push(agent("other", SubAgentStatus::Completed, "Done.")); |
| 570 | } |
| 571 | let payload = background_finished_payload(locale, &batch, false).unwrap(); |
| 572 | let preview = payload.preview().unwrap(); |
| 573 | assert!(preview.chars().count() <= PREVIEW_MAX_CHARS); |
| 574 | assert!( |
| 575 | preview.ends_with( |
| 576 | tr(locale, MessageId::NotificationFullResultPointer).as_ref() |
| 577 | ), |
| 578 | "{preview}" |
| 579 | ); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 |