Skip to content

Keep keyboard focus on the last-clicked pane until the core answers that click - #589

Merged
yansfil merged 2 commits into
mainfrom
fix/stale-snapshot-focus
Oct 6, 2026
Merged

yansfil merged 2 commits into
mainfrom
fix/stale-snapshot-focus

Conversation

@yansfil

@yansfil yansfil commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Refs #585

Summary

A snapshot that was already on its way when the operator's last click was sent still names the pane clicked before, and the page moved DOM focus to it, so keys typed right after landed in the wrong pane until the snapshot that includes the click arrived.
The page now numbers each operator focus_pane it sends (a per-page-load client_id and a sequence), the core records the last number it applied per page in the snapshot's focused.operator_focus, and a pane follows the snapshot's focused pane only once that number has caught up with the last one sent.
After it has caught up, a snapshot naming another pane is a real move (the core followed Herdr, or refused the click and kept its value) and is followed as before.
There is no time window, and no input is dropped or delayed.

Review

  • 판단해 주세요: A remote pane's focus (remote_control) is not numbered, because the local core never applies it; a stale device session can still pull focus there. Is that follow-up acceptable?
  • 판단해 주세요: For a moment after a click the header (data-focused) can still follow the older snapshot while the keys stay on the clicked pane; the header settles with the next snapshot.
  • 조심해서 볼 파일: herdr-core/src/model.rs and herdr-core/src/runtime/session.rs - snapshot wire: new focused.operator_focus list, skipped when empty, capped at 16 pages with a logged eviction.
  • 조심해서 볼 파일: herdr-core/src/runtime/events.rs - the ack is recorded for every operator focus before the core decides, so a refused or redundant click still answers the page.
  • 조심해서 볼 파일: web/src/ws.ts, web/src/PaneView.tsx - the number counts only once the frame has left the socket, and a new connection awaits nothing.
  • 비용 검토: per click one small struct update in the core and one {client_id, sequence} entry (about 60 bytes) in the next snapshot's rest section.
  • 비용 검토: no new notification, subprocess, lock or timer; the page adds one store write per click and one boolean selector per pane view. The list is capped at 16 entries.

Evidence

  • 확인한 것: web/e2e/stale-snapshot-focus.spec.ts holds the daemon's frames at the WebSocket: with the last click sent and only an older snapshot delivered the keys stay on the clicked pane, then the snapshot that includes the click keeps them. With the gate removed it fails 3 of 3 at that assertion; with it, 6 of 6.
  • 확인한 것: A move Herdr makes with nothing pending still takes the keys (second spec of the same file).
  • 확인한 것: operator_focus.rs (5 core tests) covers refused clicks, newest number kept, restore origin and unnumbered events not recorded, the cap and eviction, and the delta carrying the field; operatorFocus.test.ts (4 tests) covers the store rule and a lost socket. Core lib 1455 passed, web unit 1041 passed, clippy, fmt, eslint, typecheck and the e2e size budget pass.
  • 확인한 것: pane-focus-ordering "rapid pane clicks" as it is on main (without the wait for hided to accept every click from Wait for hided to accept every click of a burst before typing into the last pane #575): 40 of 40 passed locally with 4 yes processes saturating the CPU.
  • 확인한 것: Click-to-snapshot latency (focus_pane sent until a snapshot with its number arrives, macOS, 3 runs each). Idle one click: 19, 26, 22 ms. Idle 40 clicks queued back to back: p50 95-106 ms, p95 103-122 ms, max 105-124 ms. Four yes processes: one click 12-19 ms; 40 clicks p50 99-167 ms, p95 106-264 ms, max 109-267 ms.
  • 확인하지 못한 것: Linux and Windows were not repeated locally; the new specs run in CI.
  • 확인하지 못한 것: Wait for hided to accept every click of a burst before typing into the last pane #575's barrier in pane-focus-ordering.spec.ts is left alone; once this lands the test would pass without it, but removing the wait is a separate change after main's runs confirm it.

…hat click

The page numbers each operator focus_pane it sends, the core records the
last number it applied per page in the snapshot's focused section, and a
pane view follows the snapshot's focused pane only once that number has
caught up with the last one sent. A snapshot older than the click no longer
pulls the keys back; a move the core makes afterwards is still followed.

Refs #585
@yansfil
yansfil merged commit 0f3739a into main Oct 6, 2026
28 checks passed
@yansfil
yansfil deleted the fix/stale-snapshot-focus branch October 6, 2026 02:22
yansfil added a commit that referenced this pull request Oct 6, 2026
…up with the last click (#614)

Refs #608
Related: #589

## Summary

`overview-modal.spec.ts` "shared Overview preserves work, returns the
keyboard and has direct sidebar commands"가 Overview에서 Agents 사이드바 명령 뒤
사이드바가 키보드를 잡지 못하던 제품 원인을 고칩니다(`:101`).
#589의 회귀입니다: #589 이후 처음 나왔고(run 37406686557), 원인은 #589가 바꾼 pane의 따라가기
effect입니다.

- 사용자 영향: 터미널을 클릭한 직후 ⌘⇧A(Agents 사이드바)를 누르면, 클릭에 대한 core의 답이 도착하는 순간
키보드가 터미널로 돌아갑니다. 사이드바에서 화살표나 Enter가 터미널로 갑니다. Agent cycle이나 Overview를 거쳐
사이드바로 가도 같습니다.
- 원인: #589 이후 `PaneView`의 "스냅샷의 focused pane 따라가기" effect가 core가
operator focus 번호를 따라잡을 때(`caughtUp`)도 다시 돕니다. focused pane이 바뀌지 않았는데도 그
순간 `focusTerminal`을 불러, 그 사이 운영자가 옮긴 키보드를 되돌렸습니다.
test는 바로 앞 Agent cycle이 이미 focused인 pane에 `focus_pane`을 보내고 곧바로 사이드바로
가므로, 답이 늦게 오는 runner에서만 실패했습니다.
- 수정(web만, timer 없이 상태 전이로):
  - 스냅샷의 focus가 이 pane으로 옮겨 온 순간만 따라갑니다. 따라잡음 자체는 이동이 아닙니다.
  - 옮겨 온 순간 아직 따라잡지 못했으면 한 번 대기하고, 따라잡을 때 실행합니다.
- 그 대기는 focus가 다른 pane으로 가면 버리고, 대기를 세울 때와 실행할 때 키보드 위치가 다르면(운영자가 그 사이
옮김) 버립니다. palette guard는 그대로입니다.

## Review

- **판단해 주세요**: core가 거절한 클릭의 답이 클릭 전 pane을 그대로 가리키면, 이제 키보드를 옮기지 않습니다.
키는 클릭한 pane에, focus 테두리는 core의 pane에 남습니다.
#589 이전과 같은 동작이고, #589가 따라잡음에서 우연히 맞춰 주던 경우입니다. `docs/ARCHITECTURE.md`에
그렇게 적었습니다. 이 경우도 맞춰야 한다면 별도 issue로 다루는 게 맞다고 봅니다.
- **조심해서 볼 파일**:
- `web/src/PaneView.tsx` - snapshot을 따라 DOM focus를 옮기는 유일한 자리입니다. 대기 상태는
pane 하나에 ref 두 개이고, focus나 따라잡음이 바뀔 때만 계산합니다.
- `web/e2e/stale-snapshot-focus.spec.ts` - `holdSnapshots`가 page에 넘긴
frame 수를 세고, `read`는 page probe의 `arrivals`가 그 수에 닿을 때까지 기다립니다. 화면을 바꾸지
않는 답이 처리됐다는 것을 시간 대신 상태로 확인합니다.

## Evidence

- **확인한 것**:
  - `stale-snapshot-focus.spec.ts`에 새 test 2개(#589의 frame 보류 helper 사용):
    - "이미 focused인 pane 클릭 -> 답 보류 -> 사이드바로 키보드 -> 답 전달 -> 사이드바 유지"
    - "뒤처진 채 focus가 pane으로 옴 -> 사이드바로 키보드 -> 답 도착 -> 사이드바 유지"
  - 같은 spec을 세 build로 각 5회, `--retries=0`:

    | build | #589 test 2개 | 새 test 1 | 새 test 2 |
    |---|---|---|---|
    | main(수정 없음) | 통과 | 5/5 실패 | 5/5 실패 |
    | 수정, 키보드 위치 guard 없음 | 통과 | 통과 | 5/5 실패 |
    | 이 PR | 통과 | 통과 | 통과 |

  - 부하(`yes` 4개)에서 `--repeat-each` `--workers=8 --retries=0`:
    - `stale-snapshot-focus.spec.ts` 20회: 80/80.
- `overview-modal`, `recent`, `recent-agents-restart`,
`pane-focus-ordering`, `cmdk`, `palette`, `sidebar-shell`, `s2`,
`digit-hints` 10회: 249/250.
실패 1건은 `cmdk.spec.ts:215`의 ⌘K Recent에 checkout이 하나 더 있던 것입니다. Recent는
core가 기록하고(`track_recent_checkouts`), 이 PR은 page의 DOM focus만 바꾸므로 무관하다고
봅니다.
  - web typecheck, lint(e2e 크기 검사 포함), 단위 test 1050개 통과.
- **확인하지 못한 것**:
- CI 첫 시도의 hided log는 남지 않았습니다(artifact에는 통과한 retry의 log만 있음). 이 실패는
page 안의 DOM focus 문제라 hided log에는 원래 흔적이 없습니다.
- 원래 실패한 `overview-modal.spec.ts`의 수정 전 부하 반복은 하지 않았습니다. 원인 경로는 새 test가
결정적으로 재현합니다.
- desktop(Electron) 창에서 직접 확인하지 않았습니다. 바뀐 것은 web의 DOM focus 경로이고,
desktop은 같은 web shell을 씁니다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant