mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Classify status line provider failures and shorten error messages (#61990)
## What changed Replace the generic provider error with concise status line messages for unavailable providers, timed-out reads, and other failures. Each message includes `/statusline` to help users reselect a provider. Classify lookup, snapshot, transport, RPC, and resource validation failures, and log the failure category without carrying raw RPC errors or resource content. ## Testing Update tests to assert specific resource validation errors and the displayed messages for invalid content, RPC failures, read timeouts, and provider lookup timeouts. GitOrigin-RevId: 4ae4953cf2552c1c146d77463680655697d0d444
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
//! Poll the displayed thread’s statusline resource until a read fails or the reader is retired.
|
||||
|
||||
mod resource;
|
||||
|
||||
use self::resource::Failure;
|
||||
use self::resource::ReadIssue;
|
||||
use self::resource::decode_tree;
|
||||
use super::ChatWidget;
|
||||
use super::statusline_state::StatuslineState;
|
||||
use crate::app_event::AppEvent;
|
||||
@@ -12,16 +17,11 @@ use crate::statusline_provider::ResolvedStatuslineProvider;
|
||||
use crate::statusline_provider::StatuslineProvider;
|
||||
use crate::ui_tree::UiNode;
|
||||
use crate::ui_tree::UiTree;
|
||||
use codex_app_server_protocol::McpResourceContent;
|
||||
use codex_config::types::StatusLineProviderId;
|
||||
use codex_config::types::StatusLineSelection;
|
||||
use codex_features::Feature;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_utils_path_uri::LegacyAppPathString;
|
||||
use color_eyre::eyre::Result;
|
||||
use color_eyre::eyre::WrapErr;
|
||||
use color_eyre::eyre::bail;
|
||||
use color_eyre::eyre::eyre;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::oneshot;
|
||||
@@ -61,7 +61,7 @@ enum ReadStep {
|
||||
#[debug("Snapshot(..)")]
|
||||
Snapshot(BackendId, oneshot::Sender<StatuslineState>),
|
||||
#[debug("Finished(..)")]
|
||||
Finished(BackendId, Result<UiTree, String>),
|
||||
Finished(BackendId, Result<UiTree, Failure>),
|
||||
}
|
||||
|
||||
impl StatuslineReadEvent {
|
||||
@@ -171,12 +171,13 @@ impl ChatWidget {
|
||||
event: StatuslineReadEvent,
|
||||
owner: &AppServerSession,
|
||||
) {
|
||||
let Some(footer) = self.statusline_read.as_ref() else {
|
||||
let Some(footer) = self
|
||||
.statusline_read
|
||||
.as_ref()
|
||||
.filter(|read| read.token == event.token)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if event.token != footer.token {
|
||||
return;
|
||||
}
|
||||
if !footer.is_current(self) {
|
||||
self.refresh_status_line();
|
||||
return;
|
||||
@@ -194,12 +195,19 @@ impl ChatWidget {
|
||||
if backend != owner.backend_id() {
|
||||
return;
|
||||
}
|
||||
let tree = result.unwrap_or_else(|name| {
|
||||
footer.cancel.cancel();
|
||||
UiTree(Some(UiNode::String(format!(
|
||||
"Error loading statusline contents from {name}. Use /statusline to (re)select your provider and try again."
|
||||
))))
|
||||
});
|
||||
let tree = match result {
|
||||
Ok(tree) => tree,
|
||||
Err(failure) => {
|
||||
footer.cancel.cancel();
|
||||
tracing::warn!(?failure, "Status line provider stopped");
|
||||
let message = match failure {
|
||||
Failure::Read(ReadIssue::TimedOut) => "Status line timed out",
|
||||
Failure::Lookup => "Status line unavailable",
|
||||
Failure::Snapshot | Failure::Read(_) => "Status line failed",
|
||||
};
|
||||
UiTree(Some(UiNode::String(format!("{message} · /statusline"))))
|
||||
}
|
||||
};
|
||||
self.set_status_line(Some(tree.into()));
|
||||
}
|
||||
}
|
||||
@@ -231,10 +239,6 @@ impl ActiveStatuslineRead {
|
||||
let Some(thread) = self.thread else { return };
|
||||
let id = self.selection.clone();
|
||||
let provider_from_picker = self.provider_from_picker.clone();
|
||||
let mut name = provider_from_picker.as_ref().map_or_else(
|
||||
|| id.tool.as_str().to_owned(),
|
||||
|provider| provider.title.clone(),
|
||||
);
|
||||
let token = self.token;
|
||||
let events = events.clone();
|
||||
let client = owner.request_handle();
|
||||
@@ -242,9 +246,9 @@ impl ActiveStatuslineRead {
|
||||
let cwd = LegacyAppPathString::from_path(&self.host_cwd).to_inferred_path_uri();
|
||||
let cancel = self.cancel.clone();
|
||||
tokio::spawn(async move {
|
||||
let result: Option<Result<()>> = cancel
|
||||
let result: Option<Result<(), Failure>> = cancel
|
||||
.run_until_cancelled(async {
|
||||
let cwd = cwd.ok_or_else(|| eyre!("invalid app-server working directory"))?;
|
||||
let cwd = cwd.ok_or(Failure::Lookup)?;
|
||||
let provider = timeout(Duration::from_secs(/*secs*/ 5), async {
|
||||
loop {
|
||||
if let Ok(Some(provider)) = ResolvedStatuslineProvider::lookup(
|
||||
@@ -258,14 +262,14 @@ impl ActiveStatuslineRead {
|
||||
)
|
||||
.await
|
||||
{
|
||||
return Ok::<_, watch::error::RecvError>(provider);
|
||||
return Ok::<_, Failure>(provider);
|
||||
}
|
||||
// A cold catalog may arrive before the selected MCP server finishes starting.
|
||||
ready.changed().await?;
|
||||
ready.changed().await.map_err(|_| Failure::Lookup)?;
|
||||
}
|
||||
})
|
||||
.await??;
|
||||
name.clone_from(&provider.provider().title);
|
||||
.await
|
||||
.map_err(|_| Failure::Lookup)??;
|
||||
loop {
|
||||
let (reply, snapshot) = oneshot::channel();
|
||||
StatuslineReadEvent::send(
|
||||
@@ -273,14 +277,16 @@ impl ActiveStatuslineRead {
|
||||
token,
|
||||
ReadStep::Snapshot(backend, reply),
|
||||
);
|
||||
let snapshot = serde_json::to_value(snapshot.await?)?;
|
||||
let deadline = Instant::now() + Duration::from_secs(1);
|
||||
let state = snapshot.await.map_err(|_| Failure::Snapshot)?;
|
||||
let snapshot =
|
||||
serde_json::to_value(state).map_err(|_| Failure::Snapshot)?;
|
||||
let deadline = Instant::now() + Duration::from_secs(/*secs*/ 1);
|
||||
// A ready reply can win the final poll of timeout_at after the deadline.
|
||||
let response = match timeout_at(deadline, provider.read(snapshot)).await {
|
||||
Ok(response) if Instant::now() < deadline => response?,
|
||||
Ok(_) | Err(_) => {
|
||||
bail!("resource read timed out");
|
||||
Ok(response) if Instant::now() < deadline => {
|
||||
response.map_err(ReadIssue::from)?
|
||||
}
|
||||
Ok(_) | Err(_) => return Err(ReadIssue::TimedOut.into()),
|
||||
};
|
||||
let tree = decode_tree(&provider.provider().uri, &response.contents)?;
|
||||
StatuslineReadEvent::send(
|
||||
@@ -292,32 +298,17 @@ impl ActiveStatuslineRead {
|
||||
}
|
||||
})
|
||||
.await;
|
||||
if let Some(Err(_)) = result {
|
||||
StatuslineReadEvent::send(&events, token, ReadStep::Finished(backend, Err(name)));
|
||||
if let Some(Err(failure)) = result {
|
||||
StatuslineReadEvent::send(
|
||||
&events,
|
||||
token,
|
||||
ReadStep::Finished(backend, Err(failure)),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_tree(uri: &str, contents: &[McpResourceContent]) -> Result<UiTree> {
|
||||
let [
|
||||
McpResourceContent::Text {
|
||||
uri: returned_uri,
|
||||
mime_type: Some(mime),
|
||||
text,
|
||||
..
|
||||
},
|
||||
] = contents
|
||||
else {
|
||||
bail!("expected one text resource");
|
||||
};
|
||||
let media_type = mime.split(';').next().unwrap_or_default().trim();
|
||||
if uri != returned_uri || !media_type.eq_ignore_ascii_case("application/json") {
|
||||
bail!("expected application/json at the advertised resource URI");
|
||||
}
|
||||
text.parse().wrap_err("invalid UI tree")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "statusline_reader_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
//! Classify provider failures without carrying raw RPC errors or resource content.
|
||||
|
||||
use super::UiTree;
|
||||
use codex_app_server_client::TypedRequestError;
|
||||
use codex_app_server_protocol::McpResourceContent;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, thiserror::Error)]
|
||||
pub(super) enum ReadIssue {
|
||||
#[error("timed out after 1 second")]
|
||||
TimedOut,
|
||||
#[error("failed in transport")]
|
||||
Transport,
|
||||
#[error("was rejected with code {0}")]
|
||||
Rejected(i64),
|
||||
#[error("returned an invalid app-server response")]
|
||||
Response,
|
||||
#[error("expected exactly one text resource")]
|
||||
Envelope,
|
||||
#[error("resource URI differs from the advertised URI")]
|
||||
Uri,
|
||||
#[error("expected application/json on the resource")]
|
||||
Mime,
|
||||
#[error("resource text is not a valid, bounded UI tree")]
|
||||
Tree,
|
||||
}
|
||||
|
||||
impl From<TypedRequestError> for ReadIssue {
|
||||
fn from(error: TypedRequestError) -> Self {
|
||||
match error {
|
||||
TypedRequestError::Transport { .. } => Self::Transport,
|
||||
TypedRequestError::Server { source, .. } => Self::Rejected(source.code),
|
||||
TypedRequestError::Deserialize { .. } => Self::Response,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub(super) enum Failure {
|
||||
#[error("The selected provider could not be resolved for this thread.")]
|
||||
Lookup,
|
||||
#[error("The displayed thread could not supply its current status line state.")]
|
||||
Snapshot,
|
||||
#[error("Resource read: {0}.")]
|
||||
Read(#[from] ReadIssue),
|
||||
}
|
||||
|
||||
pub(super) fn decode_tree(uri: &str, contents: &[McpResourceContent]) -> Result<UiTree, ReadIssue> {
|
||||
let [
|
||||
McpResourceContent::Text {
|
||||
uri: returned_uri,
|
||||
mime_type,
|
||||
text,
|
||||
..
|
||||
},
|
||||
] = contents
|
||||
else {
|
||||
return Err(ReadIssue::Envelope);
|
||||
};
|
||||
if uri != returned_uri {
|
||||
return Err(ReadIssue::Uri);
|
||||
}
|
||||
let media_type = mime_type
|
||||
.as_deref()
|
||||
.unwrap_or_default()
|
||||
.split(';')
|
||||
.next()
|
||||
.unwrap_or_default();
|
||||
if !media_type.trim().eq_ignore_ascii_case("application/json") {
|
||||
return Err(ReadIssue::Mime);
|
||||
}
|
||||
text.parse().map_err(|_| ReadIssue::Tree)
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use codex_app_server_client::CloudCommand;
|
||||
use codex_app_server_client::CloudOperation;
|
||||
use codex_app_server_client::RequestResult;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::McpResourceContent;
|
||||
use codex_config::types::BuiltinStatusLine;
|
||||
use pretty_assertions::assert_eq;
|
||||
use ratatui::buffer::Buffer;
|
||||
@@ -31,20 +32,22 @@ fn statusline_resource_requires_one_matching_json_text_item() {
|
||||
decode_tree(uri, std::slice::from_ref(&item)).unwrap(),
|
||||
UiTree(None)
|
||||
);
|
||||
for contents in [
|
||||
vec![],
|
||||
vec![item.clone(), item],
|
||||
vec![content(
|
||||
json!({"uri":"ui://wrong","mimeType":"application/json","text":"null"}),
|
||||
)],
|
||||
vec![content(json!({"uri":uri,"text":"null"}))],
|
||||
vec![content(
|
||||
json!({"uri":uri,"mimeType":"application/json","blob":"bnVsbA=="}),
|
||||
)],
|
||||
vec![text("text/plain", "null")],
|
||||
vec![text("application/json", "{")],
|
||||
let wrong_uri =
|
||||
content(json!({"uri":"ui://wrong","mimeType":"application/json","text":"null"}));
|
||||
let blob = content(json!({"uri":uri,"mimeType":"application/json","blob":"bnVsbA=="}));
|
||||
for (contents, expected) in [
|
||||
(vec![], ReadIssue::Envelope),
|
||||
(vec![item.clone(), item], ReadIssue::Envelope),
|
||||
(vec![wrong_uri], ReadIssue::Uri),
|
||||
(
|
||||
vec![content(json!({"uri":uri,"text":"null"}))],
|
||||
ReadIssue::Mime,
|
||||
),
|
||||
(vec![blob], ReadIssue::Envelope),
|
||||
(vec![text("text/plain", "null")], ReadIssue::Mime),
|
||||
(vec![text("application/json", "{")], ReadIssue::Tree),
|
||||
] {
|
||||
assert!(decode_tree(uri, &contents).is_err());
|
||||
assert_eq!(decode_tree(uri, &contents).unwrap_err(), expected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +156,7 @@ async fn statusline_reader_waits_for_the_displayed_thread_and_keeps_its_result()
|
||||
chat.read_only_history = false;
|
||||
chat.refresh_status_line();
|
||||
let [resumed] = read_events(&mut rx).try_into().unwrap();
|
||||
chat.handle_statusline_read(finish(token, None), &server);
|
||||
chat.handle_statusline_read(finish(token, /*root*/ None), &server);
|
||||
assert_eq!(footer(&chat).as_deref(), Some("Initializing…"));
|
||||
let token = resumed.token;
|
||||
chat.primary_environment_cwd = Some(crate::session_state::EnvironmentCwd {
|
||||
@@ -361,7 +364,7 @@ async fn statusline_invalid_content_stops_polling_immediately() {
|
||||
respond(invalid, "{");
|
||||
reader.dispatch().await;
|
||||
assert_eq!(failed.elapsed(), Duration::ZERO);
|
||||
insta::assert_snapshot!(footer(&reader.chat).unwrap(), @"Error loading statusline contents from Calendar. Use /statusline to (re)select your provider and try again.");
|
||||
insta::assert_snapshot!(footer(&reader.chat).unwrap(), @"Status line failed · /statusline");
|
||||
assert!(!reader.chat.provider_needs_snapshot());
|
||||
reader.chat.refresh_status_line();
|
||||
advance(/*millis*/ 60_000).await;
|
||||
@@ -377,14 +380,15 @@ async fn statusline_rpc_error_stops_polling_immediately() {
|
||||
assert_eq!(resolved.elapsed(), Duration::ZERO);
|
||||
let failed = Instant::now();
|
||||
reply
|
||||
.send(Err(std::io::Error::other("provider disconnected")))
|
||||
.send(Err(std::io::Error::other(
|
||||
"PRIVATE provider error and account data",
|
||||
)))
|
||||
.unwrap();
|
||||
reader.dispatch().await;
|
||||
assert_eq!(failed.elapsed(), Duration::ZERO);
|
||||
assert!(
|
||||
footer(&reader.chat)
|
||||
.unwrap()
|
||||
.contains("Error loading statusline contents from Calendar")
|
||||
assert_eq!(
|
||||
footer(&reader.chat).as_deref(),
|
||||
Some("Status line failed · /statusline")
|
||||
);
|
||||
advance(/*millis*/ 60_000).await;
|
||||
reader.assert_idle();
|
||||
@@ -436,10 +440,9 @@ async fn statusline_deadline_wins_when_a_late_reply_is_already_queued() {
|
||||
assert!(futures::poll!(&mut advance).is_pending());
|
||||
respond(reply, r#""too late""#);
|
||||
reader.dispatch().await;
|
||||
assert!(
|
||||
footer(&reader.chat)
|
||||
.unwrap()
|
||||
.contains("Error loading statusline contents from Calendar")
|
||||
assert_eq!(
|
||||
footer(&reader.chat).as_deref(),
|
||||
Some("Status line timed out · /statusline")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -454,12 +457,13 @@ async fn statusline_hung_read_stops_polling_after_one_second() {
|
||||
advance(/*millis*/ 1).await;
|
||||
reader.dispatch().await;
|
||||
assert_eq!(started.elapsed(), Duration::from_secs(1));
|
||||
assert!(
|
||||
footer(&reader.chat)
|
||||
.unwrap()
|
||||
.contains("Error loading statusline contents from Calendar")
|
||||
assert_eq!(
|
||||
footer(&reader.chat).as_deref(),
|
||||
Some("Status line timed out · /statusline")
|
||||
);
|
||||
assert!(held.is_closed());
|
||||
reader.chat.local_settings.tui.status_line_provider = None;
|
||||
reader.chat.refresh_status_line();
|
||||
advance(/*millis*/ 60_000).await;
|
||||
reader.assert_idle();
|
||||
}
|
||||
@@ -476,10 +480,9 @@ async fn statusline_startup_timeout_stops_before_the_first_read() {
|
||||
advance(/*millis*/ 1).await;
|
||||
reader.dispatch().await;
|
||||
assert_eq!(started.elapsed(), Duration::from_secs(5));
|
||||
assert!(
|
||||
footer(&reader.chat)
|
||||
.unwrap()
|
||||
.contains("Error loading statusline contents from line")
|
||||
assert_eq!(
|
||||
footer(&reader.chat).as_deref(),
|
||||
Some("Status line unavailable · /statusline")
|
||||
);
|
||||
assert!(held.is_closed());
|
||||
reader.chat.refresh_status_line();
|
||||
@@ -530,7 +533,7 @@ async fn statusline_changing_provider_or_reconnecting_discards_the_previous_read
|
||||
reader.resolve().await;
|
||||
let next = reader.read(URI).await;
|
||||
held.closed().await;
|
||||
for result in [Ok(UiTree(None)), Err("retired".into())] {
|
||||
for result in [Ok(UiTree(None)), Err(Failure::Snapshot)] {
|
||||
let step = ReadStep::Finished(reader.owner.backend_id(), result);
|
||||
reader
|
||||
.chat
|
||||
@@ -550,10 +553,9 @@ async fn statusline_explicit_reselection_restarts_a_failed_reader() {
|
||||
reader.dispatch().await;
|
||||
reader.chat.refresh_status_line();
|
||||
reader.assert_idle();
|
||||
assert!(
|
||||
footer(&reader.chat)
|
||||
.unwrap()
|
||||
.contains("Error loading statusline contents from Calendar")
|
||||
assert_eq!(
|
||||
footer(&reader.chat).as_deref(),
|
||||
Some("Status line timed out · /statusline")
|
||||
);
|
||||
|
||||
// An explicit choice can arrive while the displayed thread is starting.
|
||||
|
||||
@@ -188,10 +188,12 @@ impl ResolvedStatuslineProvider {
|
||||
/// 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> {
|
||||
pub(crate) async fn read(
|
||||
&self,
|
||||
snapshot: Value,
|
||||
) -> Result<McpResourceReadResponse, codex_app_server_client::TypedRequestError> {
|
||||
let provider = &self.provider;
|
||||
Ok(self
|
||||
.client
|
||||
self.client
|
||||
.request_typed(ClientRequest::McpResourceRead {
|
||||
request_id: RequestId::String(Uuid::new_v4().to_string()),
|
||||
params: McpResourceReadParams {
|
||||
@@ -208,7 +210,7 @@ impl ResolvedStatuslineProvider {
|
||||
)])),
|
||||
},
|
||||
})
|
||||
.await?)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -130,7 +130,10 @@ async fn picker_choice_reads_the_displayed_threads_uri_with_the_snapshot() -> Re
|
||||
.await?
|
||||
.unwrap();
|
||||
assert_eq!(provider.provider().title, "Next event");
|
||||
provider.read(snapshot.clone()).await
|
||||
provider
|
||||
.read(snapshot.clone())
|
||||
.await
|
||||
.map_err(color_eyre::Report::from)
|
||||
};
|
||||
let (result, ()) = tokio::join!(read, host);
|
||||
result?;
|
||||
|
||||
Reference in New Issue
Block a user