Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

client: retain remote agent statuses until their mirrors exist

RoversX0420714decf9
diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs
index 77c1aba2..b3d652ed 100644
--- a/wezterm-client/src/client.rs
+++ b/wezterm-client/src/client.rs
@@ -420,6 +420,24 @@ async fn process_unilateral_inner_async(
return Ok(());
}
+ // Agent statuses routinely arrive before their pane's local mirror
+ // exists (all through an attach). Record every one into the domain's
+ // remote-keyed snapshot first — a mirror materializing later seeds
+ // itself from it — and then still deliver live to a mapped pane below.
+ // PaneRemoved must prune that snapshot here: the server deliberately
+ // never publishes an eviction status (its PaneRemoved handler is
+ // evict-only), and a retained entry would seed a *reused* remote pane
+ // id with the dead pane's agent.
+ match &decoded.pdu {
+ Pdu::AgentStatusChanged(codec::AgentStatusChanged { pane_id, status }) => {
+ client_domain.record_remote_agent_status(*pane_id, status.clone());
+ }
+ Pdu::PaneRemoved(_) => {
+ client_domain.record_remote_agent_status(pane_id, None);
+ }
+ _ => {}
+ }
+
// If we get a push for a pane that we don't yet know about,
// it means that some other client has manipulated the mux
// topology; we need to re-sync.
@@ -427,11 +445,10 @@ async fn process_unilateral_inner_async(
Some(p) => p,
None => {
// Not for agent status though: the server publishes those for
- // every pane it owns (other workspaces included), and around a
- // pane's removal the eviction push can land on either side of
- // our own PaneRemoved handling — both orders are harmless.
- // Neither implies a topology change worth a full resync; the
- // cold-start fetch covers late mapping.
+ // every pane it owns, other workspaces included, so unmapped
+ // is routine and implies no topology change worth a resync.
+ // The status was recorded into the snapshot above; the mirror
+ // seeds from it whenever it materializes.
if matches!(decoded.pdu, Pdu::AgentStatusChanged(_)) {
return Ok(());
}
diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs
index a829d963..84c21ba4 100644
--- a/wezterm-client/src/domain.rs
+++ b/wezterm-client/src/domain.rs
@@ -460,6 +460,13 @@ pub struct ClientInner {
remote_to_local_window: Mutex<HashMap<WindowId, WindowId>>,
remote_to_local_tab: Mutex<HashMap<TabId, TabId>>,
remote_to_local_pane: Mutex<HashMap<PaneId, PaneId>>,
+ /// The latest agent status the server pushed or served for each
+ /// *remote* pane id, retained whether or not a local mirror exists
+ /// yet. A mirror that materializes later seeds itself from here
+ /// (`ClientPane::new`), which closes the attach-time hole where the
+ /// cold-start fetch ran before the panes existed and the push path
+ /// had nothing to deliver to — each side assumed the other covered it.
+ remote_agent_statuses: Mutex<HashMap<PaneId, thinkterm_proto::AgentStatus>>,
/// Authoritative per-remote-tab viewport ownership pushed by the server.
remote_viewports: Mutex<HashMap<TabId, codec::ClientViewportState>>,
/// Connection-wide A/B mode and exclusive handoff owner.
@@ -708,6 +715,39 @@ impl ClientInner {
None
}
+ /// Record one status into the remote-keyed snapshot (`None` clears).
+ /// Called for every AgentStatusChanged push, mapped or not, so a
+ /// mirror that materializes later can seed itself; the `None` path is
+ /// driven by PaneRemoved on the client side — the server never pushes
+ /// an eviction status of its own.
+ pub fn record_remote_agent_status(
+ &self,
+ remote_pane_id: PaneId,
+ status: Option<thinkterm_proto::AgentStatus>,
+ ) {
+ let mut map = self.remote_agent_statuses.lock().unwrap();
+ match status {
+ Some(status) => {
+ map.insert(remote_pane_id, status);
+ }
+ None => {
+ map.remove(&remote_pane_id);
+ }
+ }
+ }
+
+ /// The last status the server reported for a remote pane, if any.
+ pub fn remote_agent_status(
+ &self,
+ remote_pane_id: PaneId,
+ ) -> Option<thinkterm_proto::AgentStatus> {
+ self.remote_agent_statuses
+ .lock()
+ .unwrap()
+ .get(&remote_pane_id)
+ .cloned()
+ }
+
pub fn remote_to_local_pane_id(&self, remote_pane_id: PaneId) -> Option<TabId> {
let mut pane_map = self.remote_to_local_pane.lock().unwrap();
let remote_server_id = self.client.remote_server_id();
@@ -898,6 +938,11 @@ impl ClientInner {
self.remote_to_local_pane.lock().unwrap().clear();
self.remote_to_local_stack.lock().unwrap().clear();
self.reported_viewports.lock().unwrap().clear();
+ // The replacement server allocates pane ids from scratch, so the
+ // retained statuses describe panes that no longer exist. Left in
+ // place they would seed the replacement's mirrors (created before
+ // the post-replacement fetch) with the dead server's agents.
+ self.remote_agent_statuses.lock().unwrap().clear();
mirrors
}
@@ -1089,6 +1134,7 @@ impl ClientInner {
remote_to_local_window: Mutex::new(HashMap::new()),
remote_to_local_tab: Mutex::new(HashMap::new()),
remote_to_local_pane: Mutex::new(HashMap::new()),
+ remote_agent_statuses: Mutex::new(HashMap::new()),
remote_viewports: Mutex::new(HashMap::new()),
remote_access: Mutex::new(None),
reported_viewports: Mutex::new(HashMap::new()),
@@ -1583,6 +1629,17 @@ impl ClientDomain {
inner.remote_to_local_pane_id(remote_pane_id)
}
+ /// See [`ClientInner::record_remote_agent_status`].
+ pub fn record_remote_agent_status(
+ &self,
+ remote_pane_id: PaneId,
+ status: Option<thinkterm_proto::AgentStatus>,
+ ) {
+ if let Some(inner) = self.inner() {
+ inner.record_remote_agent_status(remote_pane_id, status);
+ }
+ }
+
pub fn remote_to_local_window_id(&self, remote_window_id: WindowId) -> Option<WindowId> {
let inner = self.inner()?;
inner.remote_to_local_window(remote_window_id)
@@ -2165,16 +2222,25 @@ impl ClientDomain {
.inner()
.ok_or_else(|| anyhow!("domain is not attached"))?;
let response = inner.client.get_agent_statuses().await?;
- let mut desired: std::collections::HashMap<PaneId, thinkterm_proto::AgentStatus> =
- response
- .statuses
- .into_iter()
- .filter_map(|entry| {
- inner
- .remote_to_local_pane_id(entry.pane_id)
- .map(|local| (local, entry.status))
- })
- .collect();
+ // Retain the snapshot keyed by *remote* pane id before any local
+ // mapping is attempted: on attach this fetch typically runs while
+ // the mirrors are still being materialized, and a status that
+ // cannot map yet must wait for its pane (`ClientPane::new` seeds
+ // from this map), not evaporate.
+ let remote: std::collections::HashMap<PaneId, thinkterm_proto::AgentStatus> = response
+ .statuses
+ .into_iter()
+ .map(|entry| (entry.pane_id, entry.status))
+ .collect();
+ *inner.remote_agent_statuses.lock().unwrap() = remote.clone();
+ let mut desired: std::collections::HashMap<PaneId, thinkterm_proto::AgentStatus> = remote
+ .into_iter()
+ .filter_map(|(remote_pane, status)| {
+ inner
+ .remote_to_local_pane_id(remote_pane)
+ .map(|local| (local, status))
+ })
+ .collect();
let mux = Mux::get();
let mut changed = Vec::new();
for pane in mux.iter_panes() {
diff --git a/wezterm-client/src/pane/clientpane.rs b/wezterm-client/src/pane/clientpane.rs
index 2246f0f1..9b357e9a 100644
--- a/wezterm-client/src/pane/clientpane.rs
+++ b/wezterm-client/src/pane/clientpane.rs
@@ -406,7 +406,11 @@ impl ClientPane {
user_vars: Mutex::new(HashMap::new()),
config: Mutex::new(None),
progress: Mutex::new(Progress::default()),
- agent_status: Mutex::new(None),
+ // Seed from the domain's remote-keyed snapshot: this pane's
+ // status may have been fetched or pushed before the mirror
+ // existed, and nothing re-delivers it until the agent next
+ // changes state.
+ agent_status: Mutex::new(client.remote_agent_status(remote_pane_id)),
}
}