Files
codex/codex-rs/tui/src/app/dynamic_tool_tasks.rs
T
Benjamin Carlsson ed3ccd4c21 Scope TUI reconnect recovery to the affected backend (#65154)
## Why

When the active backend reconnects, the TUI can discard pending work on
other connected backends. Completions queued before the reconnect can
also arrive after the replacement connection is active.

## What changed

- Preserve pending requests and thread state for other connected backends.
  Track asynchronous completions by connection so retired connections
  cannot update the TUI.
- Apply request resolution from background backends to pending prompts
  and the agents overview. Require users to end a voice call before
  switching sessions across backends.
- Close the verification prompt when verification finishes and suppress
  queued proofs when the user cancels the prompt.

## Testing

Add regression coverage for preserving another backend's pending work
during reconnect, dropping stale completions, resolving background
requests, and cancelling verification after a proof has been queued.

GitOrigin-RevId: fc932b0d13fd1cf04404bd5ccfce71ff776e259f
2026-12-02 03:33:04 +00:00

89 lines
2.7 KiB
Rust

//! Owns dynamic tool jobs and their replies on the originating backend.
use super::App;
use crate::backend_id::BackendId;
use codex_app_server_client::AppServerReplyHandle;
use codex_app_server_protocol::DynamicToolCallResponse;
use tokio::task::JoinHandle;
pub(super) struct DynamicToolTask {
pub(super) source: String,
pub(super) generation: uuid::Uuid,
pub(super) reply: AppServerReplyHandle,
pub(super) task: Option<JoinHandle<()>>,
}
impl App {
pub(super) fn backend_completion_sender(
&mut self,
owner: BackendId,
) -> crate::app_event_sender::AppEventSender {
#[cfg(test)]
if self.backend_event_generations.is_empty() {
return self.app_event_tx.for_backend(owner);
}
let generation = *self
.backend_event_generations
.entry(owner)
.or_insert_with(uuid::Uuid::new_v4);
self.saved_openings.tx.for_connection(owner, generation)
}
pub(super) fn accept_backend_completion(
&self,
event: crate::app_event::AppEvent,
) -> Option<crate::app_event::AppEvent> {
match event {
crate::app_event::AppEvent::BackendCompletion {
backend_id,
generation,
event,
} if self.backend_event_generations.get(&backend_id) == Some(&generation) => {
Some(*event)
}
crate::app_event::AppEvent::BackendCompletion { .. } => None,
event => Some(event),
}
}
pub(super) fn cancel_dynamic_tools_for_backend(&mut self, owner: BackendId) {
self.backend_event_generations.remove(&owner);
for (_, task) in self
.dynamic_tool_tasks
.extract_if(|(backend, _), _| *backend == owner)
{
if let Some(job) = task.task {
job.abort();
}
}
}
pub(super) fn queue_tool_reply(
&self,
task: DynamicToolTask,
response: DynamicToolCallResponse,
) {
if let Some(job) = task.task {
job.abort();
}
let pending = self.cloud_title_shutdown.subscribe();
tokio::spawn(async move {
resolve_dynamic_tool_reply(task.reply, response).await;
drop(pending);
});
}
}
pub(super) async fn resolve_dynamic_tool_reply(
reply: AppServerReplyHandle,
response: DynamicToolCallResponse,
) {
match serde_json::to_value(response) {
Ok(result) => {
if let Err(error) = reply.resolve(result).await {
tracing::warn!(%error, "failed to reply to dynamic tool call");
}
}
Err(error) => tracing::warn!(%error, "failed to serialize dynamic tool response"),
}
}