Skip to content

fix(linux): commit the Ctrl-Tab session switch on Ctrl release - #37

Merged
melonamin merged 2 commits into
melonamin:linux-portfrom
n2tr2:linux-ctrl-tab-commit
Sep 3, 2026
Merged

melonamin merged 2 commits into
melonamin:linux-portfrom
n2tr2:linux-ctrl-tab-commit

Conversation

@n2tr2

@n2tr2 n2tr2 commented Aug 30, 2026

Copy link
Copy Markdown

Linux Ctrl+Tab cycling feels random because quickSwitchSession calls selectSession on every begin/advance step. Each step pushes the session onto RecencyStack, so a cycle longer than two reorders the MRU differently from macOS:

MRU [A,B,C], cycle to C resulting MRU next Ctrl+Tab lands on
macOS [C,A,B] A (toggles back)
Linux, before [C,B,A] B

macOS (agterm/Views/SessionSwitcher.swift) moves only an index during the cycle and selects once on Ctrl release. That was already the documented contract in .claude/rules/menu-actions.md — "Ctrl-Tab snapshots sessionRecency and cycles without reordering until commit" — so this is the Linux side catching up to a rule the repo already states.

What changed

Cycling now moves the overlay highlight only. commitSessionSwitch selects once when the Ctrl modifier clears, so a cycle pushes recency exactly once and a second Ctrl+Tab toggles back.

Ending the model before selecting is load-bearing: selectSession grabs focus, and that blur reaches cancelSessionSwitch, which must find the cycle already over. macOS gets this from commit()'s defer { reset() }.

Three things macOS gets free from app-wide monitors

The fork dispatches through the focused surface's key controller instead, which needs:

  • HeldControlKeys — the commit waits for the last held Ctrl keycode, matching macOS's .control-cleared test. A GDK release carries the modifier state from before it, so the event alone cannot tell whether the other Ctrl key is still down. Keycodes rather than keyvals, since a Caps-remapped Control_L reports the same keyval. Any press arriving with Ctrl clear resyncs the set, so a release lost to a blur or a keyboard grab heals on the next keystroke instead of stranding a phantom that would block every later commit.
  • A window-level Ctrl-release handler for the sessionless case. A restore that drops a dangling selected id leaves activeSession == nil over live sessions, so a cycle could begin there with no surface focused and nothing would ever commit it.
  • Blur cancels the cycle, since the release reaches only the surface holding the keyboard. Deliberately broader than that reason — a focus move to a sibling surface of the same window cancels a cycle macOS would still commit — and it is why the overlay card is never focusable.

Esc cancels ahead of the leader abort, mirroring macOS. Opening the dashboard cancels rather than commits.

Companion change

The Recent Sessions popover and its button move from whole-tree recentSessions to navigableRecentSessions, matching agterm/Views/WindowContentView+RecentSessions.swift. User-visible in flagged/filtered mode: a shorter list, and the button disables when the scope is empty. The rules file claimed the button "enables only with at least two sessions", which was wrong for macOS too — both sides enable on a non-empty navigable list.

File move

AppController.swift hit 1011 lines with the new code, over the 1000-line SwiftLint limit. Rather than raise the limit, the switcher block moved verbatim into AppControllerSessionPicker.swift, which already owned the switcher's scroller helper and is documented as the switcher's mouse twin. AppController.swift is now 947 lines.

Tests

AppController is not constructible in unit tests, so the commit decision is extracted into the pure SessionSwitcherModel.commitTarget(liveIDs:) and HeldControlKeys and unit-tested there. The wiring is covered end to end by a new AT-SPI session-switch-commit scenario:

  • mid-cycle non-selection (selection unchanged through a negative settle while Ctrl is held)
  • the [C,A,B] toggle-back
  • reverse cycling under a held Ctrl
  • Esc abort
  • blur cancel
  • dashboard-open cancel
  • a two-Ctrl hold — releasing one commits nothing, releasing the second commits
  • a plain Ctrl+C negative, pinning that a release with no cycle in flight selects nothing

The existing session-pickers scenario gains a flagged-mode leg for the scope change.

HeldControlKeys was added in review after an external reviewer found that releasing one of two held Ctrl keys committed early. It is genuine scope growth beyond the original plan, but on-subject: macOS parity is the point of the change.

Docs

The Linux-specific contract lives in a new agterm-linux/docs/menu-actions.md, next to main-loop.md / sidebar.md / x11-wayland.md, rather than in the shared rules file alongside the macOS monitors it replaces. .claude/rules/main-loop.md's blur bullet points at it.

Verification

  • swift test in agterm-linux/: 335 tests, one pre-existing unrelated failure (Flatpak process environments do not offer a host launcher, IntegrationServiceTests.swift:765 — it trips on a real deployed agtermctl on the host's PATH)
  • AgtermLinux and agtermctl-linux both build
  • swiftlint lint --strict: 0 violations in 485 files
  • Full scripts/test-linux-ui.sh: 28 scenarios green

No files under agterm/, agtermCore/, or agtermUITests/ are touched — navigableRecentSessions(limit:) already existed in core.

🤖 Generated with Claude Code

https://claude.ai/code/session_011GdLJpdGdL4xqQeWoFj7v3

@n2tr2
n2tr2 requested a review from melonamin as a code owner August 30, 2026 18:25
Linux Ctrl+Tab cycling felt random because `quickSwitchSession` called
`selectSession` on every begin/advance step. Each step pushed the session
onto `RecencyStack`, so a cycle longer than two reordered the MRU
differently from macOS: with MRU [A,B,C], cycling to C left macOS at
[C,A,B] — the next Ctrl+Tab toggles back to A — but Linux at [C,B,A],
where the next Ctrl+Tab lands on B instead.

macOS (`agterm/Views/SessionSwitcher.swift`) moves only an index during
the cycle and selects once on Ctrl release. That was already the
documented contract in `.claude/rules/menu-actions.md` ("Ctrl-Tab
snapshots `sessionRecency` and cycles without reordering until commit");
the Linux implementation violated it.

Cycling now moves the overlay highlight only. `commitSessionSwitch`
selects once when the Ctrl modifier clears, so a cycle pushes recency
exactly once and a second Ctrl+Tab toggles back. Ending the model before
selecting is load-bearing: `selectSession` grabs focus, and that blur
reaches `cancelSessionSwitch`, which must find the cycle already over.

Where macOS uses app-wide event monitors, the fork relies on the focused
surface's key controller, which needs three things macOS gets for free:

- `HeldControlKeys` tracks held Ctrl keycodes so the commit waits for the
  last one, matching macOS's `.control`-cleared test. A GDK release
  carries the modifier state from before it, so the event alone cannot
  tell whether the other Ctrl key is still down. Keycodes rather than
  keyvals, since a Caps-remapped Control_L reports the same keyval. Any
  press arriving with Ctrl clear resyncs the set, so a release lost to a
  blur or a grab heals instead of stranding a phantom.
- The window's own controller carries a Ctrl-release handler for the
  sessionless case. A restore that drops a dangling selected id leaves
  `activeSession == nil` over live sessions, so a cycle could begin there
  with no surface focused and nothing would ever commit it.
- A terminal blur cancels the cycle, since the release reaches only the
  surface holding the keyboard. This is deliberately broader than that
  reason and is why the overlay card is never focusable.

Esc cancels ahead of the leader abort, mirroring macOS. Opening the
dashboard cancels rather than commits.

Companion change: the Recent Sessions popover and its button move from
whole-tree `recentSessions` to `navigableRecentSessions`, matching
`agterm/Views/WindowContentView+RecentSessions.swift`. This is visible in
flagged/filtered mode — a shorter list, and the button disables when the
scope is empty. The rules file said the button "enables only with at
least two sessions", which was wrong for both platforms.

`AppController` was at 1011 lines with the new code, over the 1000-line
SwiftLint limit, so the switcher block moved verbatim into
`AppControllerSessionPicker.swift`, which already owned the switcher's
scroller helper and is documented as the switcher's mouse twin.

Tests: `AppController` is not constructible in unit tests, so the commit
decision is extracted into the pure `SessionSwitcherModel.commitTarget`
and `HeldControlKeys`, and the wiring is covered end to end by a new
AT-SPI `session-switch-commit` scenario — mid-cycle non-selection, the
[C,A,B] toggle-back, reverse cycling, Esc abort, blur cancel, dashboard
cancel, a two-Ctrl hold, and a plain Ctrl+C negative for the invariant
that a release with no cycle in flight selects nothing. The existing
`session-pickers` scenario gains a flagged-mode leg for the scope change.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011GdLJpdGdL4xqQeWoFj7v3
@melonamin
melonamin force-pushed the linux-ctrl-tab-commit branch from c6baedc to e4bb1c4 Compare September 3, 2026 13:14
@melonamin
melonamin force-pushed the linux-ctrl-tab-commit branch from e4bb1c4 to 68fc611 Compare September 3, 2026 13:50
@melonamin
melonamin merged commit 0e4fff0 into melonamin:linux-port Sep 3, 2026
5 checks passed
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.

2 participants