Repository navigation
fix(terminal): poll across frames in post-spawn PTY reconcile (follow-up to #6644) - #6649
Conversation
Follow-up to #6644. The merged fix used a single post-spawn requestAnimationFrame, which did not fully close the first-mount column race: the pane's real layout can keep changing for several frames (split equalize, sidebar/title reflow), so a one-shot re-fit could still measure a stale width and leave the PTY pinned. The golden 'during initial mount' e2e test failed ~1 in 9 runs with the single-frame version. Poll for up to 12 frames, forwarding the settled size to the PTY only when it differs from what the PTY was last told (mirroring the existing ResizeObserver stability loop). Each resize is gated on an actual change, so a TUI sees at most a couple of SIGWINCH during startup, not a loop. Verified: golden test passes 6/6 with repeat-each + retries; full spec 15/15. Without this change the single-frame version is intermittently flaky. Co-authored-by: Orca <[email protected]>
📝 WalkthroughWalkthrough
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 44dc0ef3-a89d-4daf-af02-98e3d0e4c4f3
📒 Files selected for processing (1)
src/renderer/src/components/terminal-pane/pty-connection.ts
…unt column desync Follow-up to #6644/#6649. Those added a post-spawn PTY reconcile but capped it at a FIXED 12 requestAnimationFrames whose counter advanced even on frames where the pane was unmeasurable or the split layout had not yet equalized. When a tab MOUNTS with a split layout already present (a new worktree opened with the side split panel on), the real narrow split width settles AFTER frame 12, so the reconcile gave up while xterm had reflowed narrow and the PTY stayed pinned at the wide spawn width. The corrective xterm onResize is dropped during the hidden mount window (isRendererPtyResizeAuthoritative() is false), so process.stdout.columns stayed wide and interactive TUIs (Claude Code) rendered garbled until a manual resize. Extract the reconcile into pty-size-reconcile.ts with an authoritative-gated convergence loop instead of a fixed frame budget: - While the pane is hidden (onResize dropped), the reconcile is the SOLE corrector: it keeps polling and forwarding every grid change (its transport.resize bypasses the visibility gate). Hidden frames never advance the settle counter. - Once visible AND stable for SETTLE_FRAMES, it hands off to the live onResize/ResizeObserver path, which reliably catches any later reflow. - Hard cap (MAX_FRAMES) guarantees termination; mobile-fit/locked frames are skipped; the reconcile handle is cancelled on dispose. Harness: pty-size-reconcile.test.ts (14 tests) drives the loop with a deterministic frame scheduler; the desync-repro tests fail against the old 12-frame logic and pass on the fix. Adds an e2e "MOUNTS with a split layout present" test. Caveat: headless Electron does not reproduce this layout-settle-after-rAF race (the existing golden e2e passes even against the old buggy logic), which is why #6644/#6649 merged with green e2e yet the bug persisted. The unit test is the real regression harness; the e2e tests are integration smoke. Made with [Orca](https://github.com/stablyai/orca) Co-authored-by: Orca <[email protected]>
…unt column desync (#6725) * fix(terminal): converge post-spawn PTY size reconcile to fix split-mount column desync Follow-up to #6644/#6649. Those added a post-spawn PTY reconcile but capped it at a FIXED 12 requestAnimationFrames whose counter advanced even on frames where the pane was unmeasurable or the split layout had not yet equalized. When a tab MOUNTS with a split layout already present (a new worktree opened with the side split panel on), the real narrow split width settles AFTER frame 12, so the reconcile gave up while xterm had reflowed narrow and the PTY stayed pinned at the wide spawn width. The corrective xterm onResize is dropped during the hidden mount window (isRendererPtyResizeAuthoritative() is false), so process.stdout.columns stayed wide and interactive TUIs (Claude Code) rendered garbled until a manual resize. Extract the reconcile into pty-size-reconcile.ts with an authoritative-gated convergence loop instead of a fixed frame budget: - While the pane is hidden (onResize dropped), the reconcile is the SOLE corrector: it keeps polling and forwarding every grid change (its transport.resize bypasses the visibility gate). Hidden frames never advance the settle counter. - Once visible AND stable for SETTLE_FRAMES, it hands off to the live onResize/ResizeObserver path, which reliably catches any later reflow. - Hard cap (MAX_FRAMES) guarantees termination; mobile-fit/locked frames are skipped; the reconcile handle is cancelled on dispose. Harness: pty-size-reconcile.test.ts (14 tests) drives the loop with a deterministic frame scheduler; the desync-repro tests fail against the old 12-frame logic and pass on the fix. Adds an e2e "MOUNTS with a split layout present" test. Caveat: headless Electron does not reproduce this layout-settle-after-rAF race (the existing golden e2e passes even against the old buggy logic), which is why #6644/#6649 merged with green e2e yet the bug persisted. The unit test is the real regression harness; the e2e tests are integration smoke. Made with [Orca](https://github.com/stablyai/orca) Co-authored-by: Orca <[email protected]> * fix(terminal): re-assert PTY size on visibility resume to heal stubborn column desync Follow-up within the same fix: the user reported "sometimes even resizing doesn't fix it." Root cause beyond the mount-timing race — the renderer forwards resizes fire-and-forget and dedupes on the size it THINKS it sent, but never learns the PTY's actual size. A resize dropped main-side (the pane was hidden, a mobile take-back resize-suppression window, or a provider no-op) leaves xterm and the PTY silently diverged; a later same-cols layout fires no onResize, so it never self-corrects. Expose the PTY's last-APPLIED size to the renderer and re-assert on show: - New read-only IPC pty:getSize -> ptySizes.get(id) (the size written only when a resize actually lands or at spawn — the authoritative "what the PTY believes it is"). Wired through preload (window.api.pty.getSize) + api-types. - On visibility resume (noteVisibilityResume), the pane re-fits, reads the PTY's real size, and re-asserts via forwardPtyResize ONLY on genuine drift — so no spurious SIGWINCH on an already-synced resume (which would jar alt-screen TUIs). Routed through forwardPtyResize so the authoritative/mobile gates are re-checked at send time; remote-runtime PTYs (separate viewport channel) are skipped; overlapping resumes coalesce to one query. Also register pty:getSize in the registerPtyHandlers removeHandler cleanup block so re-registration (macOS re-activate / new window) doesn't throw on a duplicate ipcMain.handle, and make the pty IPC test mock throw on duplicate channels like real Electron so this class of omission is caught going forward. Tests: 7 resume-reassert cases (drift / match / null / remote-skip / mobile-fit-skip / hide-during-hop / overlap-coalesce), all non-vacuous. Full terminal-pane + pty IPC suites green (1494 tests); typecheck (web+node) + oxlint clean; e2e desync specs pass against a fresh build. Made with [Orca](https://github.com/stablyai/orca) Co-authored-by: Orca <[email protected]> * Stub PTY getSize API and skip redundant Wayland GPU sandbox tests - Implement PTY `getSize` stub in `web-preload-api.ts` to satisfy API requirements for the web-preload environment. - Skip the unfixed Wayland GPU sandbox negative control test if the target base branch already contains the sandbox workaround. --------- Co-authored-by: Orca <[email protected]> Co-authored-by: Jinjing <[email protected]>
…ai#6649) Follow-up to stablyai#6644. The merged fix used a single post-spawn requestAnimationFrame, which did not fully close the first-mount column race: the pane's real layout can keep changing for several frames (split equalize, sidebar/title reflow), so a one-shot re-fit could still measure a stale width and leave the PTY pinned. The golden 'during initial mount' e2e test failed ~1 in 9 runs with the single-frame version. Poll for up to 12 frames, forwarding the settled size to the PTY only when it differs from what the PTY was last told (mirroring the existing ResizeObserver stability loop). Each resize is gated on an actual change, so a TUI sees at most a couple of SIGWINCH during startup, not a loop. Verified: golden test passes 6/6 with repeat-each + retries; full spec 15/15. Without this change the single-frame version is intermittently flaky. Co-authored-by: Orca <[email protected]>
…unt column desync (stablyai#6725) * fix(terminal): converge post-spawn PTY size reconcile to fix split-mount column desync Follow-up to stablyai#6644/stablyai#6649. Those added a post-spawn PTY reconcile but capped it at a FIXED 12 requestAnimationFrames whose counter advanced even on frames where the pane was unmeasurable or the split layout had not yet equalized. When a tab MOUNTS with a split layout already present (a new worktree opened with the side split panel on), the real narrow split width settles AFTER frame 12, so the reconcile gave up while xterm had reflowed narrow and the PTY stayed pinned at the wide spawn width. The corrective xterm onResize is dropped during the hidden mount window (isRendererPtyResizeAuthoritative() is false), so process.stdout.columns stayed wide and interactive TUIs (Claude Code) rendered garbled until a manual resize. Extract the reconcile into pty-size-reconcile.ts with an authoritative-gated convergence loop instead of a fixed frame budget: - While the pane is hidden (onResize dropped), the reconcile is the SOLE corrector: it keeps polling and forwarding every grid change (its transport.resize bypasses the visibility gate). Hidden frames never advance the settle counter. - Once visible AND stable for SETTLE_FRAMES, it hands off to the live onResize/ResizeObserver path, which reliably catches any later reflow. - Hard cap (MAX_FRAMES) guarantees termination; mobile-fit/locked frames are skipped; the reconcile handle is cancelled on dispose. Harness: pty-size-reconcile.test.ts (14 tests) drives the loop with a deterministic frame scheduler; the desync-repro tests fail against the old 12-frame logic and pass on the fix. Adds an e2e "MOUNTS with a split layout present" test. Caveat: headless Electron does not reproduce this layout-settle-after-rAF race (the existing golden e2e passes even against the old buggy logic), which is why stablyai#6644/stablyai#6649 merged with green e2e yet the bug persisted. The unit test is the real regression harness; the e2e tests are integration smoke. Made with [Orca](https://github.com/stablyai/orca) Co-authored-by: Orca <[email protected]> * fix(terminal): re-assert PTY size on visibility resume to heal stubborn column desync Follow-up within the same fix: the user reported "sometimes even resizing doesn't fix it." Root cause beyond the mount-timing race — the renderer forwards resizes fire-and-forget and dedupes on the size it THINKS it sent, but never learns the PTY's actual size. A resize dropped main-side (the pane was hidden, a mobile take-back resize-suppression window, or a provider no-op) leaves xterm and the PTY silently diverged; a later same-cols layout fires no onResize, so it never self-corrects. Expose the PTY's last-APPLIED size to the renderer and re-assert on show: - New read-only IPC pty:getSize -> ptySizes.get(id) (the size written only when a resize actually lands or at spawn — the authoritative "what the PTY believes it is"). Wired through preload (window.api.pty.getSize) + api-types. - On visibility resume (noteVisibilityResume), the pane re-fits, reads the PTY's real size, and re-asserts via forwardPtyResize ONLY on genuine drift — so no spurious SIGWINCH on an already-synced resume (which would jar alt-screen TUIs). Routed through forwardPtyResize so the authoritative/mobile gates are re-checked at send time; remote-runtime PTYs (separate viewport channel) are skipped; overlapping resumes coalesce to one query. Also register pty:getSize in the registerPtyHandlers removeHandler cleanup block so re-registration (macOS re-activate / new window) doesn't throw on a duplicate ipcMain.handle, and make the pty IPC test mock throw on duplicate channels like real Electron so this class of omission is caught going forward. Tests: 7 resume-reassert cases (drift / match / null / remote-skip / mobile-fit-skip / hide-during-hop / overlap-coalesce), all non-vacuous. Full terminal-pane + pty IPC suites green (1494 tests); typecheck (web+node) + oxlint clean; e2e desync specs pass against a fresh build. Made with [Orca](https://github.com/stablyai/orca) Co-authored-by: Orca <[email protected]> * Stub PTY getSize API and skip redundant Wayland GPU sandbox tests - Implement PTY `getSize` stub in `web-preload-api.ts` to satisfy API requirements for the web-preload environment. - Skip the unfixed Wayland GPU sandbox negative control test if the target base branch already contains the sandbox workaround. --------- Co-authored-by: Orca <[email protected]> Co-authored-by: Jinjing <[email protected]>
Follow-up to #6644
#6644 fixed the first-mount xterm↔PTY column desync (TUIs rendering ~1 char per line) by reconciling the PTY size after spawn. But it used a single post-spawn
requestAnimationFrame, which does not fully close the race.Why the single frame is not enough
The pane's real layout can keep changing for several frames after spawn (split equalize, sidebar/title reflow). A one-shot re-fit one frame later can still measure a stale width and leave the PTY pinned — so the desync recurs intermittently.
This was caught by running the golden repro repeatedly:
tests/e2e/terminal-column-desync-repro.spec.ts -g "during initial mount"failed ~1 in 9 runs with the single-frame version (the failure is the real desync assertion, not harness flake — verified by separating the two).Fix
Poll for up to 12 frames, calling
safeFiteach frame and forwarding the settled size to the PTY only when it differs from what the PTY was last told (mirroring the existingResizeObserverstability loop inpane-fit-resize-observer.ts). Because eachtransport.resizeis gated on an actual change, a TUI sees at most a couple of SIGWINCH during its own startup, not a loop. The mobile-fit override / driver lock guards are preserved.Verification
--repeat-each=6 --retries=2(was intermittently failing before).--repeat-each=3 --retries=2.pnpm typecheckclean.Made with Orca 🐋