Files
codex/codex-rs/tui/src/statusline_provider.rs
T
Joe Savona 59ac39addb Surface status line discovery failures and lookup timeouts (#61993)
## Why

Status line provider discovery failures were silently ignored, and startup lookup timeouts appeared as unavailable providers.

## What changed

- Show incomplete provider discovery in the warnings viewer, with sanitized server names, bounded summaries, and no raw RPC error details. Preserve the warning across canceled scans and clear it after a successful scan.
- Share a single five-second startup deadline across provider lookup retries and report timed-out lookups as `Status line timed out`.

## Testing

Add coverage for discovery warning rendering and lifecycle, and for a hung lookup retry exhausting the remaining startup deadline. Update the startup timeout expectation.

GitOrigin-RevId: 0403af2ed688c31c175d1b5c47d2cdd4cd355653
2026-11-17 08:08:51 +00:00

252 lines
8.0 KiB
Rust

//! Resolve globally advertised statusline tools and read them on the displayed thread.
mod descriptor;
pub(crate) mod discovery;
mod inventory;
#[cfg(test)]
pub(crate) mod test_support;
use crate::config_update::read_effective_config_with_layers;
use codex_app_server_client::AppServerRequestHandle;
use codex_app_server_protocol::ClientRequest;
use codex_app_server_protocol::McpResourceReadParams;
use codex_app_server_protocol::McpResourceReadResponse;
use codex_app_server_protocol::McpServerConnectionStatus;
use codex_app_server_protocol::RequestId;
use codex_protocol::ThreadId;
use codex_utils_path_uri::PathUri;
use color_eyre::Result;
use color_eyre::eyre::OptionExt as _;
use color_eyre::eyre::bail;
pub(crate) use descriptor::StatuslineProvider;
use serde_json::Value;
use std::collections::HashMap;
use tokio::time::Duration;
use tokio::time::Instant;
use uuid::Uuid;
pub(crate) const LOOKUP_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Debug, thiserror::Error)]
pub(crate) enum ProviderLookupError {
#[error("statusline provider lookup timed out after 5 seconds")]
TimedOut,
#[error(transparent)]
Request(#[from] color_eyre::Report),
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct McpServerName(String);
impl McpServerName {
pub(super) fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for McpServerName {
fn from(name: &str) -> Self {
Self(name.to_owned())
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct McpToolName(String);
impl McpToolName {
pub(super) fn as_str(&self) -> &str {
&self.0
}
}
impl From<&str> for McpToolName {
fn from(name: &str) -> Self {
Self(name.to_owned())
}
}
/// The saved identity is independent of the latest display title and resource URI.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ProviderId {
pub(crate) server: McpServerName,
pub(crate) tool: McpToolName,
}
impl From<&codex_config::types::StatusLineProviderId> for ProviderId {
fn from(id: &codex_config::types::StatusLineProviderId) -> Self {
Self {
server: id.server.as_str().into(),
tool: id.tool.as_str().into(),
}
}
}
impl From<&ProviderId> for codex_config::types::StatusLineProviderId {
fn from(id: &ProviderId) -> Self {
Self {
server: id.server.0.clone(),
tool: id.tool.0.clone(),
}
}
}
pub(crate) enum ProviderSelection<'a> {
Saved(&'a ProviderId),
/// A provider advertised during the current picker refresh.
Discovered(&'a StatuslineProvider),
}
pub(crate) struct ResolvedStatuslineProvider {
client: AppServerRequestHandle,
thread: ThreadId,
provider: StatuslineProvider,
}
impl ResolvedStatuslineProvider {
pub(crate) async fn lookup(
client: &AppServerRequestHandle,
thread: ThreadId,
cwd: &PathUri,
selection: ProviderSelection<'_>,
deadline: Instant,
) -> Result<Option<Self>, ProviderLookupError> {
tokio::select! {
biased;
() = tokio::time::sleep_until(deadline) => Err(ProviderLookupError::TimedOut),
result = Self::resolve(client, thread, cwd, selection) => {
if Instant::now() >= deadline {
return Err(ProviderLookupError::TimedOut);
}
Ok(result?)
}
}
}
async fn resolve(
client: &AppServerRequestHandle,
thread: ThreadId,
cwd: &PathUri,
selection: ProviderSelection<'_>,
) -> Result<Option<Self>> {
let config =
read_effective_config_with_layers(client, cwd.inferred_native_path_string()).await?;
let layers = config
.layers
.ok_or_eyre("config/read omitted the requested layers")?;
let project_sets = |section: &str, key: &str| {
let definitions: Vec<_> = layers
.iter()
.filter(|layer| layer["disabledReason"].is_null())
.filter_map(|layer| {
Some((
layer["name"]["type"] == "project",
layer["config"][section].get(key)?,
))
})
.collect();
has_project_field(&definitions)
};
let (id, advertisement) = match selection {
ProviderSelection::Saved(id) => (id, None),
ProviderSelection::Discovered(provider) => (&provider.id, Some(provider)),
};
if project_sets("mcp_servers", id.server.as_str()) {
return Ok(None);
}
let owner = if let Some(provider) = advertisement {
provider.plugin_id.clone()
} else {
let Some(global) = inventory::server(client, &id.server, /*thread*/ None).await? else {
return Ok(None);
};
if global.provider(&id.tool)?.is_none() {
return Ok(None);
}
global.plugin_id
};
if let Some(plugin) = &owner
&& (project_sets("plugins", &plugin.as_key())
|| project_sets("marketplaces", &plugin.marketplace_name))
{
return Ok(None);
}
let Some(live) = inventory::server(client, &id.server, Some(thread)).await? else {
return Ok(None);
};
if live.plugin_id != owner {
return Ok(None);
}
let provider = live.provider(&id.tool)?;
if live.runtime_status != Some(McpServerConnectionStatus::Connected) {
bail!("MCP server {} is not connected", id.server.as_str());
}
Ok(provider.map(|provider| Self {
client: client.clone(),
thread,
provider,
}))
}
pub(crate) fn provider(&self) -> &StatuslineProvider {
&self.provider
}
/// The host routes by name. A reload can replace that name between lookup and read.
/// A thread/start override can also replace a configured command before lookup. Both
/// registrations report the same name and no plugin ID. Same-owner plugin replacements
/// are indistinguishable for the same reason.
pub(crate) async fn read(
&self,
snapshot: Value,
) -> Result<McpResourceReadResponse, codex_app_server_client::TypedRequestError> {
let provider = &self.provider;
self.client
.request_typed(ClientRequest::McpResourceRead {
request_id: RequestId::String(Uuid::new_v4().to_string()),
params: McpResourceReadParams {
thread_id: Some(self.thread.to_string()),
server: provider.id.server.0.clone(),
environment_id: None,
uri: provider.uri.clone(),
connector_id: None,
origin_call_id: None,
target: None,
meta: Some(serde_json::Map::from_iter([(
"com.openai/codex-statusline-state".into(),
snapshot,
)])),
},
})
.await
}
}
/// Config layers arrive highest first. A project field only matters if a higher layer
/// leaves it in the effective registration. Arrays and scalars are replaced as a whole.
fn has_project_field(definitions: &[(bool, &Value)]) -> bool {
let Some((&(project, first), rest)) = definitions.split_first() else {
return false;
};
if !first.is_object() || rest.iter().all(|(origin, _)| *origin == project) {
return project;
}
let mut fields: HashMap<&str, Vec<_>> = HashMap::new();
for &(project, value) in definitions {
let Some(table) = value.as_object() else {
break;
};
for (key, value) in table {
fields.entry(key).or_default().push((project, value));
}
}
if fields.is_empty() {
project
} else {
fields.values().any(|values| has_project_field(values))
}
}
#[cfg(test)]
#[path = "statusline_provider_tests.rs"]
mod tests;