| 1 | //! Session-page MCP + plugin boot surface. |
| 2 | //! |
| 3 | //! Plugin discovery and every enabled MCP server boot as a **set**, not a |
| 4 | //! toast per name. The Tideline footer carries the compact pulse |
| 5 | //! (`MCP · 4 connecting` or `Plugins · Problems: 2 · /plugins`); detailed |
| 6 | //! diagnosis and actions belong in `/mcp` or `/plugins`, never as multi-row |
| 7 | //! boot output between the transcript and composer. |
| 8 | //! |
| 9 | //! One exception, and it is a place rather than a second source: the launch |
| 10 | //! screen has the vertical room the footer does not, so |
| 11 | //! [`crate::tui::underwater::launch_empty_state`] projects the *same* |
| 12 | //! [`SessionBootSurface`] into a block under the recent-work list that names |
| 13 | //! the servers that failed or need a login. Nothing here computes MCP state |
| 14 | //! twice; that renderer reads [`SessionBootSurface::servers`]. |
| 15 | |
| 16 | use unicode_width::UnicodeWidthStr; |
| 17 | |
| 18 | use crate::tui::app::App; |
| 19 | use codewhale_engine::mcp::{McpManagerSnapshot, McpServerSnapshot}; |
| 20 | use codewhale_localization::{Locale, MessageId, tr}; |
| 21 | use codewhale_plugins::PluginRegistry; |
| 22 | use codewhale_plugins::types::{PluginDiagnosticLevel, PluginTrustStatus}; |
| 23 | |
| 24 | pub(crate) const ITEM_SEPARATOR: &str = " · "; |
| 25 | const MAX_NAMED_CHIPS: usize = 4; |
| 26 | |
| 27 | /// How long a boot *prediction* may stand in for the boot pass's receipt (#7035). |
| 28 | /// |
| 29 | /// Configured rows paint as connecting because the engine's pass is expected to |
| 30 | /// name them. That is a guess about work in flight, so it needs a horizon: the |
| 31 | /// pass connects eight servers at a time with a 30 s default connect timeout, |
| 32 | /// which is ten waves — far more than any real workspace — plus room for a |
| 33 | /// first-run `npx`/`uvx` package fetch. Past this, no receipt has arrived, and |
| 34 | /// the honest surface is the last known snapshot rather than an eternal |
| 35 | /// "connecting". |
| 36 | pub const MCP_BOOT_PREDICTION_HORIZON: std::time::Duration = std::time::Duration::from_secs(300); |
| 37 | |
| 38 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 39 | pub enum SessionBootPhase { |
| 40 | Hidden, |
| 41 | Booting, |
| 42 | Settled, |
| 43 | } |
| 44 | |
| 45 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 46 | pub enum McpServerBootState { |
| 47 | Connecting, |
| 48 | Connected, |
| 49 | Failed, |
| 50 | NeedsLogin, |
| 51 | Disabled, |
| 52 | } |
| 53 | |
| 54 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 55 | pub struct McpServerBootRow { |
| 56 | pub name: String, |
| 57 | pub state: McpServerBootState, |
| 58 | } |
| 59 | |
| 60 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 61 | pub struct PluginBootSummary { |
| 62 | pub loaded: usize, |
| 63 | pub invalid: usize, |
| 64 | pub duplicate: usize, |
| 65 | pub needs_setup: usize, |
| 66 | } |
| 67 | |
| 68 | impl PluginBootSummary { |
| 69 | #[must_use] |
| 70 | pub fn is_quiet(self) -> bool { |
| 71 | self.problem_count() == 0 |
| 72 | } |
| 73 | |
| 74 | #[must_use] |
| 75 | pub fn problem_count(self) -> usize { |
| 76 | self.invalid + self.duplicate + self.needs_setup |
| 77 | } |
| 78 | |
| 79 | #[must_use] |
| 80 | pub fn from_registry(registry: &PluginRegistry) -> Self { |
| 81 | let loaded = registry.list().len(); |
| 82 | let mut invalid = 0usize; |
| 83 | let mut duplicate = 0usize; |
| 84 | let mut needs_setup = 0usize; |
| 85 | for diagnostic in registry.diagnostics() { |
| 86 | match diagnostic.code { |
| 87 | "duplicate-root" | "name-conflict" => duplicate += 1, |
| 88 | _ if diagnostic.level == PluginDiagnosticLevel::Error => invalid += 1, |
| 89 | _ => {} |
| 90 | } |
| 91 | } |
| 92 | for plugin in registry.list() { |
| 93 | if plugin |
| 94 | .diagnostics |
| 95 | .iter() |
| 96 | .any(|diagnostic| diagnostic.level == PluginDiagnosticLevel::Error) |
| 97 | { |
| 98 | invalid += 1; |
| 99 | } else if plugin.enabled |
| 100 | && (plugin_trust_needs_setup(plugin.trust_status) |
| 101 | || plugin.state_label() == "unstaged") |
| 102 | { |
| 103 | needs_setup += 1; |
| 104 | } |
| 105 | } |
| 106 | Self { |
| 107 | loaded, |
| 108 | invalid, |
| 109 | duplicate, |
| 110 | needs_setup, |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// Warm render-path caches (syntax highlighting) off the UI thread, once per |
| 116 | /// process. Only the live TUI reads [`SessionBootSurface::from_app`], so |
| 117 | /// headless and one-shot paths never pay for the load. |
| 118 | fn spawn_render_prewarm_once() { |
| 119 | static PREWARM: std::sync::Once = std::sync::Once::new(); |
| 120 | PREWARM.call_once(|| { |
| 121 | // Best effort: if the thread cannot start, the first code block |
| 122 | // loads the sets lazily exactly as before. |
| 123 | let _ = std::thread::Builder::new() |
| 124 | .name("cw-syntax-prewarm".to_string()) |
| 125 | .spawn(codewhale_tui_kit::markdown_render::prewarm_syntax_highlighting); |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | fn plugin_trust_needs_setup(status: PluginTrustStatus) -> bool { |
| 130 | matches!( |
| 131 | status, |
| 132 | PluginTrustStatus::NeverReviewed |
| 133 | | PluginTrustStatus::ContentChanged |
| 134 | | PluginTrustStatus::CapabilitiesChanged |
| 135 | ) |
| 136 | } |
| 137 | |
| 138 | /// Semantic severity for the compact boot activity notice. |
| 139 | /// |
| 140 | /// The text carries no color names or inferred state. Its consumer maps this |
| 141 | /// closed state into the Tideline palette, so a plugin warning cannot inherit |
| 142 | /// an unrelated MCP color merely because both use the same footer slot. |
| 143 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 144 | pub enum SessionBootActivityLevel { |
| 145 | Active, |
| 146 | Attention, |
| 147 | Failure, |
| 148 | } |
| 149 | |
| 150 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 151 | pub struct SessionBootActivityChip { |
| 152 | pub text: String, |
| 153 | pub level: SessionBootActivityLevel, |
| 154 | } |
| 155 | |
| 156 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 157 | pub struct SessionBootSurface { |
| 158 | pub phase: SessionBootPhase, |
| 159 | pub servers: Vec<McpServerBootRow>, |
| 160 | pub plugins: PluginBootSummary, |
| 161 | /// Enabled-server count used when names have not arrived yet, so the |
| 162 | /// first frame can still say `MCP · N connecting` instead of hiding. |
| 163 | unnamed_connecting: usize, |
| 164 | } |
| 165 | |
| 166 | impl SessionBootSurface { |
| 167 | #[must_use] |
| 168 | pub fn from_app(app: &App) -> Self { |
| 169 | spawn_render_prewarm_once(); |
| 170 | Self::from_parts( |
| 171 | app.mcp_snapshot.as_ref(), |
| 172 | app.mcp_boot_initializing(), |
| 173 | &app.mcp_connecting, |
| 174 | app.mcp_configured_count, |
| 175 | PluginBootSummary::from_registry(app.plugin_registry.as_ref()), |
| 176 | ) |
| 177 | } |
| 178 | |
| 179 | #[must_use] |
| 180 | pub fn from_parts( |
| 181 | snapshot: Option<&McpManagerSnapshot>, |
| 182 | initializing: bool, |
| 183 | connecting: &[String], |
| 184 | configured_count: usize, |
| 185 | plugins: PluginBootSummary, |
| 186 | ) -> Self { |
| 187 | let servers = if let Some(snapshot) = snapshot { |
| 188 | snapshot |
| 189 | .servers |
| 190 | .iter() |
| 191 | .filter_map(|server| row_from_snapshot(server, connecting)) |
| 192 | .collect() |
| 193 | } else if initializing { |
| 194 | let mut names = connecting.to_vec(); |
| 195 | names.sort(); |
| 196 | names |
| 197 | .into_iter() |
| 198 | .map(|name| McpServerBootRow { |
| 199 | name, |
| 200 | state: McpServerBootState::Connecting, |
| 201 | }) |
| 202 | .collect() |
| 203 | } else { |
| 204 | Vec::new() |
| 205 | }; |
| 206 | |
| 207 | let connecting_count = servers |
| 208 | .iter() |
| 209 | .filter(|row| row.state == McpServerBootState::Connecting) |
| 210 | .count(); |
| 211 | // The pre-event gap is the only moment the in-flight names are |
| 212 | // genuinely unknown: once `connecting` arrives it is the engine's |
| 213 | // real in-flight set (#6033), and an empty set under lazy boot means |
| 214 | // nothing is connecting — not "names have not arrived yet". |
| 215 | let unnamed_connecting = |
| 216 | if connecting_count == 0 && initializing && connecting.is_empty() && snapshot.is_none() |
| 217 | { |
| 218 | configured_count |
| 219 | } else { |
| 220 | 0 |
| 221 | }; |
| 222 | let phase = if servers.is_empty() && plugins.is_quiet() && unnamed_connecting == 0 { |
| 223 | SessionBootPhase::Hidden |
| 224 | } else if initializing || connecting_count > 0 || unnamed_connecting > 0 { |
| 225 | SessionBootPhase::Booting |
| 226 | } else { |
| 227 | SessionBootPhase::Settled |
| 228 | }; |
| 229 | |
| 230 | Self { |
| 231 | phase, |
| 232 | servers, |
| 233 | plugins, |
| 234 | unnamed_connecting, |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #[must_use] |
| 239 | pub fn quiet_announced( |
| 240 | mut self, |
| 241 | announced: &std::collections::BTreeMap<String, McpServerBootState>, |
| 242 | ) -> Self { |
| 243 | if announced.is_empty() { |
| 244 | return self; |
| 245 | } |
| 246 | self.servers.retain(|row| { |
| 247 | row.state != McpServerBootState::Connecting || !announced.contains_key(&row.name) |
| 248 | }); |
| 249 | if self.phase == SessionBootPhase::Booting |
| 250 | && self.unnamed_connecting == 0 |
| 251 | && !self |
| 252 | .servers |
| 253 | .iter() |
| 254 | .any(|row| row.state == McpServerBootState::Connecting) |
| 255 | { |
| 256 | self.phase = SessionBootPhase::Settled; |
| 257 | } |
| 258 | self |
| 259 | } |
| 260 | |
| 261 | #[cfg(test)] |
| 262 | pub fn announce_problem_changes( |
| 263 | &self, |
| 264 | locale: Locale, |
| 265 | announced: &mut std::collections::BTreeMap<String, McpServerBootState>, |
| 266 | ) -> Option<SessionBootActivityChip> { |
| 267 | self.announce_problem_changes_with_fallbacks(locale, announced) |
| 268 | .map(|(chip, _)| chip) |
| 269 | } |
| 270 | |
| 271 | pub(crate) fn announce_problem_changes_with_fallbacks( |
| 272 | &self, |
| 273 | locale: Locale, |
| 274 | announced: &mut std::collections::BTreeMap<String, McpServerBootState>, |
| 275 | ) -> Option<(SessionBootActivityChip, Vec<String>)> { |
| 276 | let mut needs_login = Vec::new(); |
| 277 | let mut failed = Vec::new(); |
| 278 | let mut recovered = Vec::new(); |
| 279 | for row in &self.servers { |
| 280 | match row.state { |
| 281 | McpServerBootState::NeedsLogin | McpServerBootState::Failed => { |
| 282 | if !announced.contains_key(&row.name) { |
| 283 | announced.insert(row.name.clone(), row.state); |
| 284 | if row.state == McpServerBootState::NeedsLogin { |
| 285 | needs_login.push(row.name.as_str()); |
| 286 | } else { |
| 287 | failed.push(row.name.as_str()); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | McpServerBootState::Connected => { |
| 292 | if let Some(prev) = announced.get_mut(&row.name) |
| 293 | && matches!( |
| 294 | *prev, |
| 295 | McpServerBootState::NeedsLogin | McpServerBootState::Failed |
| 296 | ) |
| 297 | { |
| 298 | *prev = McpServerBootState::Connected; |
| 299 | recovered.push(row.name.as_str()); |
| 300 | } |
| 301 | } |
| 302 | McpServerBootState::Disabled | McpServerBootState::Connecting => {} |
| 303 | } |
| 304 | } |
| 305 | if needs_login.is_empty() && failed.is_empty() && recovered.is_empty() { |
| 306 | return None; |
| 307 | } |
| 308 | let mut line = String::from("MCP"); |
| 309 | if !recovered.is_empty() { |
| 310 | line.push_str(&format!( |
| 311 | "{ITEM_SEPARATOR}{} {}", |
| 312 | named_list(&recovered), |
| 313 | tr(locale, MessageId::ExtensionsStateConnected) |
| 314 | )); |
| 315 | } |
| 316 | if !needs_login.is_empty() { |
| 317 | line.push_str(&format!( |
| 318 | "{ITEM_SEPARATOR}{}: {}", |
| 319 | mcp_auth_required_state_label(locale), |
| 320 | named_list(&needs_login) |
| 321 | )); |
| 322 | } |
| 323 | if !failed.is_empty() { |
| 324 | line.push_str(&format!( |
| 325 | "{ITEM_SEPARATOR}{}: {}", |
| 326 | tr(locale, MessageId::PhaseFailed), |
| 327 | named_list(&failed) |
| 328 | )); |
| 329 | } |
| 330 | let level = if !failed.is_empty() { |
| 331 | SessionBootActivityLevel::Failure |
| 332 | } else if !needs_login.is_empty() { |
| 333 | SessionBootActivityLevel::Attention |
| 334 | } else { |
| 335 | SessionBootActivityLevel::Active |
| 336 | }; |
| 337 | if level != SessionBootActivityLevel::Active { |
| 338 | line.push_str(&format!("{ITEM_SEPARATOR}/mcp")); |
| 339 | } |
| 340 | let mut fallbacks = Vec::new(); |
| 341 | if level != SessionBootActivityLevel::Active { |
| 342 | let mut counted = String::from("MCP"); |
| 343 | if !needs_login.is_empty() { |
| 344 | counted.push_str(&format!("{ITEM_SEPARATOR}{} auth", needs_login.len())); |
| 345 | } |
| 346 | if !failed.is_empty() { |
| 347 | counted.push_str(&format!( |
| 348 | "{ITEM_SEPARATOR}{} {}", |
| 349 | failed.len(), |
| 350 | tr(locale, MessageId::PhaseFailed) |
| 351 | )); |
| 352 | } |
| 353 | counted.push_str(&format!("{ITEM_SEPARATOR}/mcp")); |
| 354 | fallbacks.push(counted); |
| 355 | let total = needs_login.len() + failed.len(); |
| 356 | let noun = if total == 1 { "problem" } else { "problems" }; |
| 357 | fallbacks.push(format!( |
| 358 | "MCP{ITEM_SEPARATOR}{total} {noun}{ITEM_SEPARATOR}/mcp" |
| 359 | )); |
| 360 | } else if !recovered.is_empty() { |
| 361 | fallbacks.push(format!( |
| 362 | "MCP{ITEM_SEPARATOR}{} {}", |
| 363 | recovered.len(), |
| 364 | tr(locale, MessageId::ExtensionsStateConnected) |
| 365 | )); |
| 366 | } |
| 367 | Some((SessionBootActivityChip { text: line, level }, fallbacks)) |
| 368 | } |
| 369 | |
| 370 | #[must_use] |
| 371 | pub fn activity_notice(&self, budget: usize) -> Option<SessionBootActivityChip> { |
| 372 | if self.phase == SessionBootPhase::Hidden || budget == 0 { |
| 373 | return None; |
| 374 | } |
| 375 | let connecting: Vec<&str> = self |
| 376 | .servers |
| 377 | .iter() |
| 378 | .filter(|row| row.state == McpServerBootState::Connecting) |
| 379 | .map(|row| row.name.as_str()) |
| 380 | .collect(); |
| 381 | if !connecting.is_empty() { |
| 382 | let count = connecting.len(); |
| 383 | let named = named_chip_line("MCP", count, "connecting", &connecting); |
| 384 | return activity_notice_from_candidates( |
| 385 | SessionBootActivityLevel::Active, |
| 386 | vec![named, format!("MCP{ITEM_SEPARATOR}{count} connecting")], |
| 387 | budget, |
| 388 | ); |
| 389 | } |
| 390 | if self.phase == SessionBootPhase::Booting { |
| 391 | let count = self.unnamed_connecting; |
| 392 | if count > 0 { |
| 393 | return activity_notice_from_candidates( |
| 394 | SessionBootActivityLevel::Active, |
| 395 | vec![format!("MCP{ITEM_SEPARATOR}{count} connecting")], |
| 396 | budget, |
| 397 | ); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Plugins contribute no footer chip. |
| 402 | // |
| 403 | // "Plugins · Problems: N" sat in the footer of every session for as |
| 404 | // long as the condition held, and a person could do nothing about it |
| 405 | // from there. Worse, its number and the Plugins tab disagreed by |
| 406 | // construction: the chip counted enabled-but-unreviewed bundles, |
| 407 | // while the tab's Problems group lists only registry-level |
| 408 | // diagnostics, so the rows that made the count were somewhere else |
| 409 | // entirely — "it says 2 problems ... it's not even clear what the |
| 410 | // problems are because there are actually 3". |
| 411 | // |
| 412 | // A bundle waiting on a review is not an incident; it is a row with |
| 413 | // an action. `/plugins` states it per bundle, next to the key that |
| 414 | // resolves it. `self.plugins` is still computed: it decides whether |
| 415 | // this surface is `Hidden`, and the launch block reads the same |
| 416 | // summary. |
| 417 | None |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | fn named_list(names: &[&str]) -> String { |
| 422 | let shown = names |
| 423 | .iter() |
| 424 | .take(MAX_NAMED_CHIPS) |
| 425 | .copied() |
| 426 | .collect::<Vec<_>>(); |
| 427 | let mut list = shown.join(", "); |
| 428 | if names.len() > shown.len() { |
| 429 | list.push_str(&format!(" +{}", names.len() - shown.len())); |
| 430 | } |
| 431 | list |
| 432 | } |
| 433 | |
| 434 | fn activity_notice_from_candidates( |
| 435 | level: SessionBootActivityLevel, |
| 436 | candidates: Vec<String>, |
| 437 | budget: usize, |
| 438 | ) -> Option<SessionBootActivityChip> { |
| 439 | candidates |
| 440 | .into_iter() |
| 441 | .find(|line| line.width() <= budget) |
| 442 | .map(|text| SessionBootActivityChip { text, level }) |
| 443 | } |
| 444 | |
| 445 | fn row_from_snapshot( |
| 446 | server: &McpServerSnapshot, |
| 447 | connecting: &[String], |
| 448 | ) -> Option<McpServerBootRow> { |
| 449 | if !server.enabled { |
| 450 | return Some(McpServerBootRow { |
| 451 | name: server.name.clone(), |
| 452 | state: McpServerBootState::Disabled, |
| 453 | }); |
| 454 | } |
| 455 | if server.connected { |
| 456 | return Some(McpServerBootRow { |
| 457 | name: server.name.clone(), |
| 458 | state: McpServerBootState::Connected, |
| 459 | }); |
| 460 | } |
| 461 | if let Some(error) = server.error.as_deref() { |
| 462 | let state = if server.auth_required || mcp_error_requires_login(error) { |
| 463 | McpServerBootState::NeedsLogin |
| 464 | } else { |
| 465 | McpServerBootState::Failed |
| 466 | }; |
| 467 | return Some(McpServerBootRow { |
| 468 | name: server.name.clone(), |
| 469 | state, |
| 470 | }); |
| 471 | } |
| 472 | if connecting.iter().any(|name| name == &server.name) { |
| 473 | return Some(McpServerBootRow { |
| 474 | name: server.name.clone(), |
| 475 | state: McpServerBootState::Connecting, |
| 476 | }); |
| 477 | } |
| 478 | // Enabled, unconnected, no diagnosis, not in flight: a lazy server |
| 479 | // nobody has asked for yet (#6033). It is not boot activity, so it gets |
| 480 | // no row — calling it Failed or Connecting would both be lies. |
| 481 | None |
| 482 | } |
| 483 | |
| 484 | /// Text fallback for the typed [`McpServerSnapshot::auth_required`] state: |
| 485 | /// the one shared auth-required classifier, plus the shape the session-boot |
| 486 | /// receipt itself prints. |
| 487 | #[must_use] |
| 488 | pub fn mcp_error_requires_login(error: &str) -> bool { |
| 489 | let lowered = error.to_ascii_lowercase(); |
| 490 | codewhale_engine::mcp::oauth::error_text_looks_auth_required(error) |
| 491 | || (lowered.contains("oauth") && lowered.contains("authenticat")) |
| 492 | } |
| 493 | |
| 494 | /// The `◆ auth required` state label every TUI MCP surface prints for a |
| 495 | /// server whose login is missing, expired, or revoked. |
| 496 | #[must_use] |
| 497 | pub fn mcp_auth_required_state_label(locale: Locale) -> String { |
| 498 | format!( |
| 499 | "{} {}", |
| 500 | codewhale_tui_kit::glyphs::ATTENTION, |
| 501 | tr(locale, MessageId::McpAuthRequiredState) |
| 502 | ) |
| 503 | } |
| 504 | |
| 505 | fn named_chip_line(kind: &str, count: usize, verb: &str, names: &[&str]) -> String { |
| 506 | let chips = names |
| 507 | .iter() |
| 508 | .take(MAX_NAMED_CHIPS) |
| 509 | .copied() |
| 510 | .collect::<Vec<_>>(); |
| 511 | let extra = names.len().saturating_sub(chips.len()); |
| 512 | let mut line = format!("{kind}{ITEM_SEPARATOR}{count} {verb}"); |
| 513 | if !chips.is_empty() { |
| 514 | line.push_str(ITEM_SEPARATOR); |
| 515 | line.push_str(&chips.join(ITEM_SEPARATOR)); |
| 516 | if extra > 0 { |
| 517 | line.push_str(&format!("{ITEM_SEPARATOR}+{extra}")); |
| 518 | } |
| 519 | } |
| 520 | line |
| 521 | } |
| 522 | |
| 523 | #[cfg(test)] |
| 524 | mod tests { |
| 525 | use super::*; |
| 526 | use codewhale_engine::mcp::{ |
| 527 | McpManagerSnapshot, McpServerCapabilityMetadata, McpServerSnapshot, |
| 528 | }; |
| 529 | use std::path::PathBuf; |
| 530 | |
| 531 | fn server( |
| 532 | name: &str, |
| 533 | enabled: bool, |
| 534 | connected: bool, |
| 535 | error: Option<&str>, |
| 536 | ) -> McpServerSnapshot { |
| 537 | McpServerSnapshot { |
| 538 | name: name.to_string(), |
| 539 | enabled, |
| 540 | required: false, |
| 541 | transport: "stdio".to_string(), |
| 542 | command_or_url: format!("cmd-{name}"), |
| 543 | connect_timeout: 5, |
| 544 | execute_timeout: 5, |
| 545 | read_timeout: 5, |
| 546 | connected, |
| 547 | error: error.map(str::to_string), |
| 548 | auth_required: false, |
| 549 | capability_metadata: McpServerCapabilityMetadata::NotObserved, |
| 550 | tools: Vec::new(), |
| 551 | resources: Vec::new(), |
| 552 | prompts: Vec::new(), |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | fn snapshot(servers: Vec<McpServerSnapshot>) -> McpManagerSnapshot { |
| 557 | McpManagerSnapshot { |
| 558 | config_path: PathBuf::from("mcp.json"), |
| 559 | config_exists: true, |
| 560 | reload_required: false, |
| 561 | servers, |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn zero_servers_and_quiet_plugins_hide() { |
| 567 | let surface = |
| 568 | SessionBootSurface::from_parts(None, false, &[], 0, PluginBootSummary::default()); |
| 569 | assert_eq!(surface.phase, SessionBootPhase::Hidden); |
| 570 | assert!( |
| 571 | surface |
| 572 | .activity_notice(80) |
| 573 | .map(|notice| notice.text) |
| 574 | .is_none() |
| 575 | ); |
| 576 | } |
| 577 | |
| 578 | #[test] |
| 579 | fn healthy_loaded_plugins_do_not_claim_the_boot_surface() { |
| 580 | let surface = SessionBootSurface::from_parts( |
| 581 | None, |
| 582 | false, |
| 583 | &[], |
| 584 | 0, |
| 585 | PluginBootSummary { |
| 586 | loaded: 2, |
| 587 | ..PluginBootSummary::default() |
| 588 | }, |
| 589 | ); |
| 590 | assert_eq!(surface.phase, SessionBootPhase::Hidden); |
| 591 | assert!(surface.activity_notice(80).is_none()); |
| 592 | } |
| 593 | |
| 594 | #[test] |
| 595 | fn changed_plugin_content_requires_setup() { |
| 596 | assert!(plugin_trust_needs_setup(PluginTrustStatus::ContentChanged)); |
| 597 | assert!(plugin_trust_needs_setup(PluginTrustStatus::NeverReviewed)); |
| 598 | assert!(plugin_trust_needs_setup( |
| 599 | PluginTrustStatus::CapabilitiesChanged |
| 600 | )); |
| 601 | assert!(!plugin_trust_needs_setup(PluginTrustStatus::Trusted)); |
| 602 | } |
| 603 | |
| 604 | /// A fresh install ships every built-in bundle disabled and never |
| 605 | /// reviewed. That is the shipped resting state, not a problem: counting |
| 606 | /// it kept "Plugins · Problems" in the footer of every new install until |
| 607 | /// each built-in had been reviewed. |
| 608 | #[test] |
| 609 | fn fresh_install_built_ins_keep_the_plugin_chip_quiet() { |
| 610 | let _lock = crate::test_support::lock_test_env(); |
| 611 | let tmp = tempfile::tempdir().unwrap(); |
| 612 | let home = tmp.path().join("home"); |
| 613 | let workspace = tmp.path().join("workspace"); |
| 614 | std::fs::create_dir_all(&workspace).unwrap(); |
| 615 | std::fs::create_dir_all(&home).unwrap(); |
| 616 | let _home = crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", &home); |
| 617 | |
| 618 | let registry = codewhale_plugins::PluginDiscoveryContext::capture_pre_dotenv() |
| 619 | .registry_for_workspace(&workspace); |
| 620 | let summary = PluginBootSummary::from_registry(®istry); |
| 621 | assert_eq!(summary.loaded, registry.list().len()); |
| 622 | assert!( |
| 623 | summary.is_quiet(), |
| 624 | "a fresh install must not report plugin problems: {summary:?}" |
| 625 | ); |
| 626 | } |
| 627 | |
| 628 | /// Plugin state never reaches the footer, at any width and at any |
| 629 | /// severity. The chip it replaced was permanent, unactionable, and |
| 630 | /// counted a different set than the tab it pointed at. |
| 631 | #[test] |
| 632 | fn plugin_problems_never_produce_a_footer_chip() { |
| 633 | for summary in [ |
| 634 | PluginBootSummary { |
| 635 | loaded: 3, |
| 636 | invalid: 1, |
| 637 | duplicate: 1, |
| 638 | needs_setup: 1, |
| 639 | }, |
| 640 | PluginBootSummary { |
| 641 | loaded: 1, |
| 642 | needs_setup: 1, |
| 643 | ..PluginBootSummary::default() |
| 644 | }, |
| 645 | ] { |
| 646 | let surface = SessionBootSurface::from_parts(None, false, &[], 0, summary); |
| 647 | assert_eq!(surface.phase, SessionBootPhase::Settled); |
| 648 | for budget in [12, 22, 40, 80] { |
| 649 | assert_eq!( |
| 650 | surface.activity_notice(budget), |
| 651 | None, |
| 652 | "plugin summary {summary:?} produced a chip at width {budget}" |
| 653 | ); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | #[test] |
| 659 | fn mcp_activity_outranks_plugin_problems() { |
| 660 | let snap = snapshot(vec![server("alpha", true, false, None)]); |
| 661 | let surface = SessionBootSurface::from_parts( |
| 662 | Some(&snap), |
| 663 | true, |
| 664 | &["alpha".to_string()], |
| 665 | 1, |
| 666 | PluginBootSummary { |
| 667 | invalid: 1, |
| 668 | ..PluginBootSummary::default() |
| 669 | }, |
| 670 | ); |
| 671 | assert_eq!( |
| 672 | surface.activity_notice(80), |
| 673 | Some(SessionBootActivityChip { |
| 674 | text: "MCP · 1 connecting · alpha".to_string(), |
| 675 | level: SessionBootActivityLevel::Active, |
| 676 | }) |
| 677 | ); |
| 678 | } |
| 679 | |
| 680 | #[test] |
| 681 | fn one_connecting_server_names_itself() { |
| 682 | let snap = snapshot(vec![server("alpha", true, false, None)]); |
| 683 | let surface = SessionBootSurface::from_parts( |
| 684 | Some(&snap), |
| 685 | true, |
| 686 | &["alpha".to_string()], |
| 687 | 1, |
| 688 | PluginBootSummary::default(), |
| 689 | ); |
| 690 | assert_eq!(surface.phase, SessionBootPhase::Booting); |
| 691 | assert_eq!(surface.servers.len(), 1); |
| 692 | assert_eq!(surface.servers[0].state, McpServerBootState::Connecting); |
| 693 | let chip = surface |
| 694 | .activity_notice(80) |
| 695 | .map(|notice| notice.text) |
| 696 | .expect("chip"); |
| 697 | assert!(chip.contains("alpha"), "{chip}"); |
| 698 | assert!(!chip.to_ascii_lowercase().contains("slack"), "{chip}"); |
| 699 | } |
| 700 | |
| 701 | #[test] |
| 702 | fn many_connecting_servers_use_count_and_named_chips() { |
| 703 | let snap = snapshot(vec![ |
| 704 | server("alpha", true, false, None), |
| 705 | server("beta", true, false, None), |
| 706 | server("gamma", true, false, None), |
| 707 | server("docs", true, false, None), |
| 708 | ]); |
| 709 | let connecting = ["alpha", "beta", "gamma", "docs"] |
| 710 | .into_iter() |
| 711 | .map(str::to_string) |
| 712 | .collect::<Vec<_>>(); |
| 713 | let surface = SessionBootSurface::from_parts( |
| 714 | Some(&snap), |
| 715 | true, |
| 716 | &connecting, |
| 717 | 4, |
| 718 | PluginBootSummary::default(), |
| 719 | ); |
| 720 | assert_eq!(surface.phase, SessionBootPhase::Booting); |
| 721 | let chip = surface |
| 722 | .activity_notice(80) |
| 723 | .map(|notice| notice.text) |
| 724 | .expect("chip"); |
| 725 | assert!(chip.contains("4 connecting"), "{chip}"); |
| 726 | assert!(chip.contains("alpha"), "{chip}"); |
| 727 | assert!(chip.contains("docs"), "{chip}"); |
| 728 | assert!(!chip.to_ascii_lowercase().contains("slack"), "{chip}"); |
| 729 | } |
| 730 | |
| 731 | #[test] |
| 732 | fn settled_failures_remain_classified() { |
| 733 | let snap = snapshot(vec![ |
| 734 | server("alpha", true, true, None), |
| 735 | server("beta", true, false, Some("protocol negotiation timed out")), |
| 736 | server( |
| 737 | "gamma", |
| 738 | true, |
| 739 | false, |
| 740 | Some("MCP server 'gamma' requires OAuth authentication. Run `/mcp login gamma`"), |
| 741 | ), |
| 742 | server("docs", false, false, Some("disabled")), |
| 743 | ]); |
| 744 | let surface = SessionBootSurface::from_parts( |
| 745 | Some(&snap), |
| 746 | false, |
| 747 | &[], |
| 748 | 4, |
| 749 | PluginBootSummary::default(), |
| 750 | ); |
| 751 | assert_eq!(surface.phase, SessionBootPhase::Settled); |
| 752 | assert_eq!( |
| 753 | surface |
| 754 | .servers |
| 755 | .iter() |
| 756 | .find(|row| row.name == "beta") |
| 757 | .map(|row| row.state), |
| 758 | Some(McpServerBootState::Failed) |
| 759 | ); |
| 760 | assert_eq!( |
| 761 | surface |
| 762 | .servers |
| 763 | .iter() |
| 764 | .find(|row| row.name == "gamma") |
| 765 | .map(|row| row.state), |
| 766 | Some(McpServerBootState::NeedsLogin) |
| 767 | ); |
| 768 | } |
| 769 | |
| 770 | #[test] |
| 771 | fn typed_auth_required_state_routes_to_login_without_error_text_sniffing() { |
| 772 | let mut delta = server("delta", true, false, Some("request rejected")); |
| 773 | delta.auth_required = true; |
| 774 | let snap = snapshot(vec![delta]); |
| 775 | let surface = SessionBootSurface::from_parts( |
| 776 | Some(&snap), |
| 777 | false, |
| 778 | &[], |
| 779 | 1, |
| 780 | PluginBootSummary::default(), |
| 781 | ); |
| 782 | assert_eq!( |
| 783 | surface |
| 784 | .servers |
| 785 | .iter() |
| 786 | .find(|row| row.name == "delta") |
| 787 | .map(|row| row.state), |
| 788 | Some(McpServerBootState::NeedsLogin) |
| 789 | ); |
| 790 | assert_eq!(surface.servers.len(), 1); |
| 791 | assert_eq!(surface.activity_notice(100), None); |
| 792 | } |
| 793 | |
| 794 | #[test] |
| 795 | fn problems_are_announced_once_and_recoveries_once() { |
| 796 | let mut expired = server("slack", true, false, Some("401 Unauthorized")); |
| 797 | expired.auth_required = true; |
| 798 | let failing = server("beta", true, false, Some("Stdio transport closed")); |
| 799 | let snap = snapshot(vec![ |
| 800 | server("alpha", true, true, None), |
| 801 | expired.clone(), |
| 802 | failing.clone(), |
| 803 | ]); |
| 804 | let surface = SessionBootSurface::from_parts( |
| 805 | Some(&snap), |
| 806 | false, |
| 807 | &[], |
| 808 | 3, |
| 809 | PluginBootSummary::default(), |
| 810 | ); |
| 811 | assert_eq!(surface.activity_notice(100), None); |
| 812 | let mut announced = std::collections::BTreeMap::new(); |
| 813 | let chip = surface |
| 814 | .announce_problem_changes(Locale::En, &mut announced) |
| 815 | .expect("first sight is announced"); |
| 816 | assert_eq!(chip.level, SessionBootActivityLevel::Failure); |
| 817 | assert_eq!( |
| 818 | chip.text, |
| 819 | format!( |
| 820 | "MCP{ITEM_SEPARATOR}{}: slack{ITEM_SEPARATOR}failed: beta{ITEM_SEPARATOR}/mcp", |
| 821 | mcp_auth_required_state_label(Locale::En) |
| 822 | ) |
| 823 | ); |
| 824 | for _ in 0..10 { |
| 825 | assert_eq!( |
| 826 | surface.announce_problem_changes(Locale::En, &mut announced), |
| 827 | None |
| 828 | ); |
| 829 | } |
| 830 | |
| 831 | let retrying = SessionBootSurface::from_parts( |
| 832 | Some(&snap), |
| 833 | true, |
| 834 | &["beta".to_string()], |
| 835 | 3, |
| 836 | PluginBootSummary::default(), |
| 837 | ); |
| 838 | assert_eq!( |
| 839 | retrying.quiet_announced(&announced).activity_notice(100), |
| 840 | None, |
| 841 | "a retry of an announced server paints nothing" |
| 842 | ); |
| 843 | |
| 844 | let mut signed_in = expired; |
| 845 | signed_in.connected = true; |
| 846 | signed_in.error = None; |
| 847 | signed_in.auth_required = false; |
| 848 | let snap = snapshot(vec![ |
| 849 | server("alpha", true, true, None), |
| 850 | signed_in, |
| 851 | failing.clone(), |
| 852 | ]); |
| 853 | let surface = SessionBootSurface::from_parts( |
| 854 | Some(&snap), |
| 855 | false, |
| 856 | &[], |
| 857 | 3, |
| 858 | PluginBootSummary::default(), |
| 859 | ); |
| 860 | let chip = surface |
| 861 | .announce_problem_changes(Locale::En, &mut announced) |
| 862 | .expect("sign-in is announced"); |
| 863 | assert_eq!(chip.text, format!("MCP{ITEM_SEPARATOR}slack connected")); |
| 864 | assert_eq!( |
| 865 | surface.announce_problem_changes(Locale::En, &mut announced), |
| 866 | None |
| 867 | ); |
| 868 | |
| 869 | let flapped = snapshot(vec![ |
| 870 | server("alpha", true, true, None), |
| 871 | server("slack", true, false, Some("transport closed")), |
| 872 | failing, |
| 873 | ]); |
| 874 | let flapped_surface = SessionBootSurface::from_parts( |
| 875 | Some(&flapped), |
| 876 | false, |
| 877 | &[], |
| 878 | 3, |
| 879 | PluginBootSummary::default(), |
| 880 | ); |
| 881 | assert_eq!( |
| 882 | flapped_surface.announce_problem_changes(Locale::En, &mut announced), |
| 883 | None |
| 884 | ); |
| 885 | let recovered_again = SessionBootSurface::from_parts( |
| 886 | Some(&snap), |
| 887 | false, |
| 888 | &[], |
| 889 | 3, |
| 890 | PluginBootSummary::default(), |
| 891 | ); |
| 892 | assert_eq!( |
| 893 | recovered_again.announce_problem_changes(Locale::En, &mut announced), |
| 894 | None |
| 895 | ); |
| 896 | } |
| 897 | |
| 898 | #[test] |
| 899 | fn narrow_activity_budget_sheds_names_keeps_count() { |
| 900 | let snap = snapshot(vec![ |
| 901 | server("alpha", true, false, None), |
| 902 | server("beta", true, false, None), |
| 903 | server("gamma", true, false, None), |
| 904 | ]); |
| 905 | let connecting = ["alpha", "beta", "gamma"] |
| 906 | .into_iter() |
| 907 | .map(str::to_string) |
| 908 | .collect::<Vec<_>>(); |
| 909 | let surface = SessionBootSurface::from_parts( |
| 910 | Some(&snap), |
| 911 | true, |
| 912 | &connecting, |
| 913 | 3, |
| 914 | PluginBootSummary::default(), |
| 915 | ); |
| 916 | let chip = surface |
| 917 | .activity_notice(22) |
| 918 | .map(|notice| notice.text) |
| 919 | .expect("chip"); |
| 920 | assert_eq!(chip, "MCP · 3 connecting"); |
| 921 | } |
| 922 | |
| 923 | #[test] |
| 924 | fn first_frame_names_enabled_servers_before_a_snapshot_arrives() { |
| 925 | let connecting = ["gamma", "alpha", "docs"] |
| 926 | .into_iter() |
| 927 | .map(str::to_string) |
| 928 | .collect::<Vec<_>>(); |
| 929 | let surface = SessionBootSurface::from_parts( |
| 930 | None, |
| 931 | true, |
| 932 | &connecting, |
| 933 | 3, |
| 934 | PluginBootSummary::default(), |
| 935 | ); |
| 936 | assert_eq!(surface.phase, SessionBootPhase::Booting); |
| 937 | assert_eq!( |
| 938 | surface |
| 939 | .servers |
| 940 | .iter() |
| 941 | .map(|row| row.name.as_str()) |
| 942 | .collect::<Vec<_>>(), |
| 943 | vec!["alpha", "docs", "gamma"] |
| 944 | ); |
| 945 | let chip = surface |
| 946 | .activity_notice(80) |
| 947 | .map(|notice| notice.text) |
| 948 | .expect("chip"); |
| 949 | assert!(chip.contains("3 connecting"), "{chip}"); |
| 950 | assert!(chip.contains("alpha"), "{chip}"); |
| 951 | assert!(chip.contains("gamma"), "{chip}"); |
| 952 | assert!(!chip.to_ascii_lowercase().contains("slack"), "{chip}"); |
| 953 | } |
| 954 | |
| 955 | #[test] |
| 956 | fn initializing_without_names_still_shows_the_count() { |
| 957 | let surface = |
| 958 | SessionBootSurface::from_parts(None, true, &[], 4, PluginBootSummary::default()); |
| 959 | assert_eq!(surface.phase, SessionBootPhase::Booting); |
| 960 | assert!(surface.servers.is_empty()); |
| 961 | assert_eq!( |
| 962 | surface |
| 963 | .activity_notice(80) |
| 964 | .map(|notice| notice.text) |
| 965 | .as_deref(), |
| 966 | Some("MCP · 4 connecting") |
| 967 | ); |
| 968 | } |
| 969 | |
| 970 | #[test] |
| 971 | fn booting_snapshot_with_non_connecting_servers_reports_no_connecting_chip() { |
| 972 | let snap = snapshot(vec![ |
| 973 | server("alpha", true, true, None), |
| 974 | server("beta", false, false, None), |
| 975 | server("gamma", true, false, Some("connection refused")), |
| 976 | ]); |
| 977 | let surface = |
| 978 | SessionBootSurface::from_parts(Some(&snap), true, &[], 3, PluginBootSummary::default()); |
| 979 | assert_eq!(surface.phase, SessionBootPhase::Booting); |
| 980 | assert_eq!(surface.activity_notice(80), None); |
| 981 | } |
| 982 | |
| 983 | #[test] |
| 984 | fn mcp_auth_required_state_label_uses_locale() { |
| 985 | let label = mcp_auth_required_state_label(Locale::ZhHans); |
| 986 | let expected = tr(Locale::ZhHans, MessageId::McpAuthRequiredState); |
| 987 | assert!(label.contains(expected.as_ref()), "{label}"); |
| 988 | assert!(!label.contains("auth required"), "{label}"); |
| 989 | } |
| 990 | } |
| 991 |