Skip to content

fix(terminal): skip redundant focus work and unchanged PTY resizes - #123

Merged
gsvprharsha merged 3 commits into
tempestai-dev:mainfrom
bferanmi806-sketch:fix/28-idle-focus-hitch
Sep 20, 2026
Merged

gsvprharsha merged 3 commits into
tempestai-dev:mainfrom
bferanmi806-sketch:fix/28-idle-focus-hitch

Conversation

@bferanmi806-sketch

Copy link
Copy Markdown
Contributor

Draft fix for #28 (idle refocus hitch on low-end systems); holding for review, not closing the issue yet.

Root cause: every window refocus on a visible terminal pane unconditionally scheduled fit plus a resize_pty IPC plus a full-row repaint, with zero change detection. The backend resizes unconditionally too, so each refocus cost an IPC round-trip plus ConPTY resize and shell repaint even when pixel-identical. Restore callbacks (focus, unhide, relayout) never coalesced, and N mounted panes each ran the sequence. webglPool.acquire was already idempotent and session timers already clear-then-set, so neither is the cause.

Implementation:

  • src/lib/terminalFocusPolicy.ts (new): pure dim-change, repaint-after-hidden, and coalescing policy.
  • src/lib/terminalRestore.ts (new): per-mount restore controller - last-sent PTY dim cache (null-forced on initial fit, session change, mobile handoff), one pending restore slot shared by focus/unhide/relayout, latched hidden state consumed once by recovery, rAF ids tracked and cancelled on dispose with stale callbacks neutered, failed resize_pty invalidates only its own cached request.
  • src/components/TerminalPane.tsx: all fit/resize/refresh paths route through the controller. Ordinary refocus now costs one idempotent WebGL acquire plus one coalesced frontend fit with no IPC and no repaint; full repaint happens only after the page was hidden. Mobile-driven dims, initial font fit, hidden/split panes and terminal content paths are unchanged. Reacquisition after WebGL context loss self-paints (verified: xterm setRenderer ends in _fullRefresh), so no extra refresh is needed there.

Test results:

  • New src/components/terminalFocus.check.ts drives the real controller with fakes: IPC counting over refocus sequences, latch survival, real-resize sends, sequential and deferred failure scope, mobile handoff, dispose/cancel/remount isolation, cross-source coalescing. Passes.
  • npm test 16/17 (sole failure is the pre-existing AgentQuotaStrip.check.ts extensionless-import error, identical on the base branch).
  • npx tsc --noEmit clean; npm run build:frontend succeeds.

Outstanding manual profiling: no live measurement of the 1-2s hitch and no interactive tauri dev run in this environment. Suggested: idle the app, Alt-Tab refocus (expect no resize_pty traffic, instant response) vs minimize-restore (expect one full repaint), rapid focus bouncing, split-pane restore, phone takeback sizing, and actual WebGL context-loss recovery on low-end hardware.

Ref: #28

Refocusing after idle always scheduled fit plus a resize_pty IPC plus a full-row repaint, even with identical dims, and restore callbacks never coalesced. Cache last-sent PTY dims per session and skip the IPC when unchanged, coalesce restore fits into one pending slot, and repaint fully only after the page was hidden. WebGL reacquire, mobile-driven dims, initial font fit and terminal content paths are unchanged.
… failures

Latch the hidden flag until focus consumes recovery so an early visible event cannot skip the restore repaint. Invalidate cached PTY dims on mobile-control acquisition so takeback always reasserts. Route all queued rAF fits/refreshes through a per-mount restore controller that cancels frames and neuters callbacks on dispose, so stale restores cannot run against a remounted session. Failed resize_pty invalidates only its own cached request, preserving newer updates. Reacquisition after WebGL context loss self-paints via xterm setRenderer, verified in the bundled addon source.
Retain only pending frame ids in the restore controller so cancellation touches live callbacks and the set cannot grow across the mount lifetime. Add a deferred-failure regression test where resize A stays pending while B succeeds: A's late rejection must not clobber B's cached dimensions, and newer changes still send.
@vercel

vercel Bot commented Sep 19, 2026

Copy link
Copy Markdown

@bferanmi806-sketch is attempting to deploy a commit to the Tempest OSS Team on Vercel.

A member of the Team first needs to authorize it.

@bferanmi806-sketch
bferanmi806-sketch marked this pull request as ready for review September 19, 2026 22:15

@gsvprharsha gsvprharsha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed: policy/controller split, lifecycle guards, IPC scoping and latch semantics all verified. CI green. LGTM.

@gsvprharsha
gsvprharsha merged commit 44186c6 into tempestai-dev:main Sep 20, 2026
5 of 6 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