diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 455c7750b9..72caeb8e63 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -894,7 +894,7 @@ The wheel has one owner: the capture listener stops the event before xterm sees Keyboard focus has one reporter: a pane's textarea gaining focus under the pointer is the operator moving focus and goes out as `focus_pane`, while the focus the shell itself moves to follow the snapshot's focused pane (`focusTerminal`) is never reported back, because that echo, arriving at the core while Herdr's confirmation of the previous move was still in flight, kept two panes trading focus on a slow runner. A snapshot older than the operator's last click must not pull the keys back, and the page tells such a snapshot from a real move by state, not by time: `web/src/ws.ts` numbers each operator `focus_pane` it sends (`client_id` for the page load, `sequence` per page, counted only once the frame has left the socket), the core records the last number it applied for each page in `focused.operator_focus` of the snapshot, in the same event that moves or refuses the focus, and `PaneView` follows the snapshot's focused pane only while the number applied has caught up with the number sent (`operatorFocusSent` and `operatorFocusApplied` in the store, `web/src/operatorFocus.ts`). Once caught up, a snapshot that names another pane is a real move (the core followed Herdr, or refused the click and kept its own value) and is followed as before. -The record is capped at 16 pages, oldest evicted with a `pane.focus.sequence_evicted` diagnostic, and a new connection starts with nothing awaited, because a number lost on a closed socket will never be answered. +The record is capped at 16 pages, oldest evicted with a `pane.focus.sequence_evicted` diagnostic (a page whose entry was seen and is gone takes everything it sent as applied, because the core reads it as having nothing in flight), and a new connection starts with nothing awaited, because a number lost on a closed socket will never be answered. A remote pane's focus goes to its host as `remote_control` and is not numbered: the local core never applies it, so it has nothing to acknowledge, and the same guard does not cover a stale device session. A pointer gesture follows one routing state in `web/src/terminals.ts` (PRD S2 B20): a drag selects locally in xterm, and a single primary click that never left its cell is replayed on release as one `terminal_click` with the pressed cell and the crossterm modifiers, after the `focus_pane` the press already sent; ⌥ + press and a multi-click stay local and send nothing. The cell is read against xterm's own screen element, which is exactly `cols` by `rows` cells; the pane host around it is larger by the fit's remainder, and a cell derived from the host drifts by up to one row and one column toward the far edge, which is where a full-screen program's one-row hint sits (the first real-session clicks on Claude Code's "Jump to bottom" missed for that reason). diff --git a/web/src/operatorFocus.test.ts b/web/src/operatorFocus.test.ts index 2a3f40b020..1fae15c1a7 100644 --- a/web/src/operatorFocus.test.ts +++ b/web/src/operatorFocus.test.ts @@ -44,4 +44,20 @@ describe("operator focus numbering", () => { releaseOperatorFocus(); expect(caughtUp(useShellStore.getState())).toBe(true); }); + + it("reads its own entry gone after it was seen as dropped by the core, not as still waiting", () => { + const { noteOperatorFocusSent, applyFrame } = useShellStore.getState(); + // Before any answer, an absent entry only means the core has not applied one. + noteOperatorFocusSent(1); + applyFrame(snapshotFrame([{ client_id: "someone-else", sequence: 4 }])); + expect(caughtUp(useShellStore.getState())).toBe(false); + applyFrame(snapshotFrame([{ client_id: operatorFocusClientId, sequence: 1 }])); + expect(caughtUp(useShellStore.getState())).toBe(true); + + // Later clicks are sent; the core then drops this page's entry (16 other pages spoke). + noteOperatorFocusSent(3); + expect(caughtUp(useShellStore.getState())).toBe(false); + applyFrame(snapshotFrame([{ client_id: "someone-else", sequence: 5 }])); + expect(caughtUp(useShellStore.getState())).toBe(true); + }); }); diff --git a/web/src/operatorFocus.ts b/web/src/operatorFocus.ts index a3cbb340c8..e78057ad0e 100644 --- a/web/src/operatorFocus.ts +++ b/web/src/operatorFocus.ts @@ -36,6 +36,22 @@ export function appliedIn(rest: SnapshotRest): number | null { return entry ? entry.sequence : null; } +/** + * What the page takes as applied once `rest` has arrived. + * + * The core keeps the newest number per page and drops the oldest page when 16 + * others have spoken since. A page whose entry was seen and is now gone has + * been dropped, and the core then reads it as having nothing in flight, so the + * page does the same: it takes everything it sent as applied rather than wait + * for an answer that cannot come. Before the first answer an absent entry + * means only that the core has not applied one yet. + */ +export function appliedAfter(rest: SnapshotRest, state: { operatorFocusSent: number; operatorFocusApplied: number }): number { + const applied = appliedIn(rest); + if (applied !== null) return Math.max(state.operatorFocusApplied, applied); + return state.operatorFocusApplied > 0 ? Math.max(state.operatorFocusApplied, state.operatorFocusSent) : 0; +} + /** Whether the snapshot includes every operator focus this page has sent on the open connection. */ export function caughtUp(state: { operatorFocusSent: number; operatorFocusApplied: number }): boolean { return state.operatorFocusApplied >= state.operatorFocusSent; diff --git a/web/src/store.ts b/web/src/store.ts index 8236c2e853..f6a09a64aa 100644 --- a/web/src/store.ts +++ b/web/src/store.ts @@ -1,7 +1,7 @@ import type { StoredBuffer } from "./buffers"; import { create } from "zustand"; import type { ConnectionState } from "./connection"; -import { appliedIn } from "./operatorFocus"; +import { appliedAfter } from "./operatorFocus"; import { remoteContext, remoteView } from "./remote"; import { share } from "./share"; import { @@ -534,7 +534,7 @@ export const useShellStore = create((set, get) => ({ viewGeneration: frame.type === "snapshot" ? get().viewGeneration + 1 : get().viewGeneration, ...cursors, focusedPaneId: focusedPaneOf(rest), - operatorFocusApplied: Math.max(get().operatorFocusApplied, appliedIn(rest) ?? 0), + operatorFocusApplied: appliedAfter(rest, get()), herdrState: rest.status?.herdr?.state ?? get().herdrState, }); } else {