From 810fa9c41e5095af0241f9128708fe2e28b6e31c Mon Sep 17 00:00:00 2001 From: yansfil Date: Tue, 6 Oct 2026 19:20:18 +0900 Subject: [PATCH] Remember a tab focus that leaves after its wait was replaced A tab focus still waiting on the control lane was forgotten when a pane focus's tab wait replaced it, but a pane focus does not take a tab focus off the lane, so it still reached Herdr and its answer was followed as Herdr's own move. The tab focus is now recorded as superseded when it leaves, if it is no longer the one awaited. Refs #629 --- docs/ARCHITECTURE.md | 5 +- herdr-core/src/runtime.rs | 4 +- herdr-core/src/runtime/control_lane.rs | 25 +++++++-- herdr-core/src/runtime/session.rs | 31 ++++++++-- .../src/runtime/tests/session_navigation.rs | 56 +++++++++++++++++++ 5 files changed, 106 insertions(+), 15 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 22122daf48..5f917acd45 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -158,7 +158,7 @@ Selecting a pane first moves the core-owned project, checkout and visible tab to The shell sends physical terminal-focus events even when its latest snapshot already names that pane; core coalescing decides whether Herdr needs another operation, while focus applied from a snapshot sends no operator echo. Every control Hide sends on this machine's Herdr socket for a tab or a pane focus travels one ordered lane (`runtime/control_lane.rs`): tab focus, tab creation and owner open, tab move, tab rename and pane focus. Each request is queued, in flight, superseded or settled. -A queued request has not left, so a newer one replaces it and nothing of it is remembered; an in-flight request is applied by Herdr whatever Hide decides next, so one replaced after it left is superseded and its answer is Hide's own. +A queued request has not left, so a newer one that replaces it on the lane leaves nothing of it to remember; an in-flight request is applied by Herdr whatever Hide decides next, so one that is replaced by the time it leaves is superseded and its answer is Hide's own. One socket operation runs at a time and the rest wait in arrival order, at most 16 (`CONTROL_LANE_LIMIT`; a request past it is refused where it was asked and logged as `control.lane_full`), so a tab focus, a creation and a pane focus sent moments apart reach Herdr in the order they were asked, where a thread per request let them land in either order. Only a focus coalesces: a queued tab focus is replaced by the newer one, and a pane focus keeps one turn on the lane whose target is the latest intent, so rapid clicks cannot leave older requests running after the last click has been confirmed. Creation, move and rename are distinct intents and are never replaced. @@ -174,7 +174,8 @@ If the mutation answer is lost or malformed, its external effect is unknown: the Hide keeps the latest local selection at that outcome, but the pinned API supplies no mutation cancellation or fence: a late Herdr focus may subsequently be followed with no owned request pending, and a later user selection or Retry starts a new burst rather than resuming the aborted queue. For tab notifications, the Herdr workspace that owns the target showing it is read as confirmation, whichever workspace holds Herdr's keyboard, because a checkout is keyed by path and can hold tabs from several Herdr workspaces. A tab notification replaced after it was sent but before Herdr answered it stays remembered as superseded (`superseded_tab_focus`, at most 16, each for the same three seconds, dropped when Herdr definitely refuses it but kept when its result was lost, since Herdr may still have applied it): a session from before both requests can confirm the newer one, and Herdr's answer to the older one then arrives naming a tab Hide has left, so a move onto a superseded tab is consumed as that answer (`tab.focus.late_answer`) rather than followed; following it moved the keyboard to the other Agent area in the middle of typed input (#413). -A tab focus that was replaced while it still waited on the lane never left, so it is not remembered, and its three-second wait starts when it is sent rather than when it was asked. +Whether a tab focus is superseded is decided when it leaves the lane: one a newer tab focus replaced there never leaves and is not remembered, and one whose wait was replaced or settled while it waited, as a pane focus's tab wait does without taking it off the lane, is superseded as it leaves; its three-second wait starts when it is sent rather than when it was asked. +Remembering only what had left by the time it was replaced let that tab focus leave unremembered, and its answer was followed as Herdr's own after a slow Herdr held the lane (#629). A pane focus in a tab Herdr is not showing moves Herdr's tab as well, and Herdr reports that tab move on its session stream after it has answered the focus, so the tab is awaited in the same tab wait: sent with its pane focus, held while that pane control runs, given its three seconds from Herdr's answer, superseded by the next move, and dropped when the focus is refused or never leaves. Without it each late tab move of a quick run of Agent cycle commits was followed as Herdr's own, replaying every tab passed through and leaving the screen on an older one (#594); a pane in the tab Herdr shows, or will show once Hide's outstanding tab request lands, arms nothing. After a tab focus or pane focus whose answer was lost, the focus waiting behind it is not sent, because the lost one may still land after it and put Herdr back; Hide keeps the tab it shows, records `tab.focus.unknown`, and the next click starts a new request. diff --git a/herdr-core/src/runtime.rs b/herdr-core/src/runtime.rs index a506c823d8..254af4e390 100644 --- a/herdr-core/src/runtime.rs +++ b/herdr-core/src/runtime.rs @@ -394,8 +394,8 @@ struct PendingViewFocus { /// Local pane controls settle by operation identity, never by target alone. pane_control_serial: Option, /// Whether the request has left for Herdr. A request still waiting its - /// turn on the control lane cannot be answered, so replacing it leaves no - /// late answer to explain, and its wait does not run out. + /// turn on the control lane cannot be answered yet, so replacing it + /// leaves no late answer until it leaves, and its wait does not run out. sent: bool, requested_at_unix_ms: u64, } diff --git a/herdr-core/src/runtime/control_lane.rs b/herdr-core/src/runtime/control_lane.rs index 37f2e52271..50e0e6d47b 100644 --- a/herdr-core/src/runtime/control_lane.rs +++ b/herdr-core/src/runtime/control_lane.rs @@ -530,15 +530,30 @@ impl Runtime { } /// Records that the tab focus for `tab_id` has left for Herdr: from now - /// on its answer can arrive after something newer replaced it. + /// on its answer can arrive after something newer replaced it. One whose + /// wait was replaced or settled while it waited here still leaves (a + /// pane focus's tab wait does not take it off the lane), so its answer + /// is remembered as superseded from now (#629). A tab no checkout lists + /// has nothing to follow, so nothing of it is remembered. fn mark_tab_focus_sent(&mut self, tab_id: &str) { - if let Some(pending) = self + match self .pending_tab_focus .as_mut() - .filter(|pending| pending.target_id == tab_id && !pending.sent) + .filter(|pending| pending.target_id == tab_id) { - pending.sent = true; - pending.requested_at_unix_ms = unix_milliseconds(); + Some(pending) => { + if !pending.sent { + pending.sent = true; + pending.requested_at_unix_ms = unix_milliseconds(); + } + } + None => { + if let Some(checkout_id) = self.checkout_holding_tab(tab_id) { + self.supersede_tab_focus( + PendingViewFocus::new(checkout_id, tab_id).already_sent(), + ); + } + } } } } diff --git a/herdr-core/src/runtime/session.rs b/herdr-core/src/runtime/session.rs index f1fa8d52a1..6cd1bc948c 100644 --- a/herdr-core/src/runtime/session.rs +++ b/herdr-core/src/runtime/session.rs @@ -3379,17 +3379,22 @@ impl Runtime { pub(super) fn await_tab_focus(&mut self, next: PendingViewFocus) { let next_target_id = next.target_id.clone(); // Only a request that already left can be answered late. One still - // waiting on the control lane is replaced there and never reaches - // Herdr, so nothing of it is remembered. + // waiting on the control lane is remembered when it leaves, if it + // does (`mark_tab_focus_sent`); a newer tab focus replaces it there, + // so it never does. if let Some(previous) = self.pending_tab_focus.replace(next) && previous.target_id != next_target_id && previous.sent { - self.superseded_tab_focus.retain(|held| { - held.scope_id != previous.scope_id || held.target_id != previous.target_id - }); - self.superseded_tab_focus.push(previous); + self.supersede_tab_focus(previous); } + } + /// Remembers a tab notification Herdr will answer after Hide has moved + /// on, so the answer is consumed rather than followed. + pub(super) fn supersede_tab_focus(&mut self, held: PendingViewFocus) { + self.superseded_tab_focus + .retain(|kept| kept.scope_id != held.scope_id || kept.target_id != held.target_id); + self.superseded_tab_focus.push(held); if self.superseded_tab_focus.len() > SUPERSEDED_TAB_FOCUS_LIMIT { let evicted = self.superseded_tab_focus.remove(0); crate::diagnostic!(serde_json::json!({ @@ -3441,6 +3446,20 @@ impl Runtime { self.pending_tab_focus = None; } } + pub(super) fn checkout_holding_tab(&self, tab_id: &str) -> Option { + self.snapshot + .navigator + .workspaces + .iter() + .flat_map(|workspace| &workspace.checkouts) + .find(|checkout| { + checkout + .tabs + .iter() + .any(|tab| tab.id.as_deref() == Some(tab_id)) + }) + .map(|checkout| checkout.id.clone()) + } fn checkout_tab_holding_pane(&self, pane_id: &str) -> Option<(String, String)> { self.snapshot .navigator diff --git a/herdr-core/src/runtime/tests/session_navigation.rs b/herdr-core/src/runtime/tests/session_navigation.rs index 852fba5aa1..76b13ef8c6 100644 --- a/herdr-core/src/runtime/tests/session_navigation.rs +++ b/herdr-core/src/runtime/tests/session_navigation.rs @@ -1101,6 +1101,62 @@ fn view_authority_a_pane_focus_s_late_tab_move_is_its_answer_not_an_outside_focu assert_eq!(diagnostic_count(&runtime, "tab.focus.followed"), 1); } +/// Issue #629. Two tab clicks and an Agent cycle commit meet a slow Herdr: +/// t2 is on the wire, t1 waits on the lane behind it, and the commit's pane +/// focus replaces t1's wait with its own. A session from before the clicks, +/// still on t3, settles that wait, and t1 leaves anyway, because a pane +/// focus does not take a tab focus off the lane. Herdr's answer to t1 is +/// Hide's own: following it put the screen back on t1 and the shell +/// recorded t1's pane as a visit, so the next cycle started from it. +#[test] +fn view_authority_a_tab_focus_that_leaves_after_its_wait_was_replaced_is_superseded() { + let checkout_path = "/private/tmp/hide-view-authority-left-replaced"; + let (mut runtime, checkout_id) = live_tab_order_runtime(checkout_path); + let tabs = ["w-order:t1", "w-order:t2", "w-order:t3"]; + let herdr_on = |tab: &str| Ok(tab_order_payload(checkout_path, &tabs, &tabs, tab)); + runtime.ingest_session(herdr_on("w-order:t3")); + let focus = |tab_id: &str| RemoteControlAction::FocusTab { + tab_id: tab_id.to_owned(), + }; + let acknowledged = || { + Ok(RemoteControlOutcome::Acknowledged { + created_tab_id: None, + created_pane_id: None, + }) + }; + + assert!(runtime.dispatch_json(&focus_tab_event(&checkout_id, "w-order:t2"))); + assert!(runtime.dispatch_json(&focus_tab_event(&checkout_id, "w-order:t1"))); + assert!(runtime.dispatch_json(&operator_focus_event("w-order:t3:p"))); + runtime.ingest_session(herdr_on("w-order:t3")); + // The lane sends t2, then t1. + runtime.complete_lane_tab(focus("w-order:t2"), acknowledged(), 3); + runtime.complete_lane_tab(focus("w-order:t1"), acknowledged(), 3); + + for answer in ["w-order:t2", "w-order:t1"] { + runtime.ingest_session(herdr_on(answer)); + assert_eq!( + checkout_active_tab_id(&runtime, &checkout_id).as_deref(), + Some("w-order:t3"), + "Herdr naming {answer} does not move the tab the operator chose last" + ); + assert_eq!( + runtime.snapshot().terminal.pane_id.as_deref(), + Some("w-order:t3:p") + ); + } + assert_eq!(diagnostic_count(&runtime, "tab.focus.followed"), 0); + assert!(runtime.superseded_tab_focus.is_empty()); + + // Each answer was consumed, so a tab Herdr focuses on its own is followed. + runtime.ingest_session(herdr_on("w-order:t2")); + assert_eq!( + checkout_active_tab_id(&runtime, &checkout_id).as_deref(), + Some("w-order:t2") + ); + assert_eq!(diagnostic_count(&runtime, "tab.focus.followed"), 1); +} + /// The tab wait a pane focus arms always ends: Herdr refusing the focus /// moves no tab, a focus in the tab Herdr already shows arms nothing, and a /// tab move Herdr never reports runs out with a tab focus's deadline. After