Hold workspace handoff until incoming terminals are presentable (#1291)

Closed
LA
2mo ago
+251-12

Fixes https://github.com/manaflow-ai/cmux/issues/1291.

What

Switching workspaces intermittently painted one or more frames with neither the old nor the new workspace's terminal content. Bonsplit tab switches never flickered.

Root cause: the workspace handoff hid the retiring workspace's terminals as soon as the target workspace's surfaces merely existed (hasLoadedTerminalSurface), but a sidebar switch remounts the whole workspace (maxMountedWorkspaces = 1), so the incoming portals reveal one or more main-queue turns later, and a reclaimed renderer republishes pixels later still. Between the hide and the first publish, the window backdrop showed through. Tab switches are immune because they flip isHidden on both surfaces synchronously in one CATransaction with no remount.

How

  • Handoff readiness now means presentable, not pointer-exists: every rendered-visible incoming terminal must be unhidden, in a window, and its terminal layer must hold pixels (presentation-layer contents, the same idiom as the debug present-stats reader). The retiring workspace stays mounted and visible (the existing pin) until then.
  • WorkspaceHandoffFrameWatcher observes portal visibility notifications, isHidden KVO, and a bounded 32ms recheck (CALayer contents bypasses KVO for the core-owned layer), completing the handoff the moment the incoming content is real. Focus-driven completions defer to it, and the liveness timeout (150ms → 500ms) remains the ceiling: a dead surface holds the old content for at most half a second instead of flashing blank.
  • terminal.rendererRealization.maxWarmRenderers default raised 1 → 4 so switching among recently used workspaces presents retained pixels instantly. Hidden windows still release everything via window occlusion (#10815), and the idle threshold bounds the rest.

Verification

  • Pixel harness (12 scripted switches, in-app window screenshots at ~10fps, 3 seeded workspaces): before the fix, blank terminal-area frames reproduced on switches into never-shown or reclaimed workspaces; after, 0 blank frames across 500+ captured frames per run.
  • RendererRealizationPlannerTests updated for the new warm-cap default (cap+1 surfaces reclaim exactly the excess).
  • Caveat: the fast completion path (presentable before timeout) could not be timing-verified on the verify fleet — the minis are headless, so after #10815 their windows are permanently occluded and terminals never render there. It needs a lit display: dogfood covers it.

Notes

  • The ws.handoff.frameWatch.* DEBUG events (begin/presentable/state) make the handoff decision observable in the debug log.
  • Known residual (follow-up material, from the #1291 mechanism audit): retaining layer contents across renderer derealization in the ghostty fork would remove the rebuild gap entirely; the portal z-raise remove/re-add can still cause an in-window bounce on bind.

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes #1291: workspace switches intermittently painted frames with neither the old nor the new workspace's terminals, because the handoff hid the retiring workspace as soon as the target's surfaces merely existed. The handoff now holds the retiring workspace mounted and visible until every incoming visible terminal is presentable on screen (unhidden, in a window, holding pixels in its layer).

Bug Fixes

  • WorkspaceHandoffFrameWatcher observes portal visibility notifications, isHidden KVO, and a bounded 32ms recheck (CALayer contents bypasses KVO), logging ws.handoff.frameWatch.* DEBUG events for observability.
  • Focus-driven handoff completions defer to the watcher; the liveness timeout rose from 150ms to 500ms as the ceiling.
  • terminal.rendererRealization.maxWarmRenderers default raised from 1 to 4 so recently used workspaces switch with retained pixels instantly; hidden windows still release renderers via occlusion and idle thresholds bound the rest.
  • The fast completion path couldn't be timing-verified on the headless verify fleet; it needs dogfooding on a lit display.
  • Known residual: portal z-raise remove/re-add can cause an in-window bounce on bind.

Summary by CodeRabbit

  • Improvements
    • Workspace switching now completes when incoming terminal views are visibly rendered, reducing premature handoffs and visual glitches.
    • Added safeguards for terminals that are slow or unable to render, while preserving responsive switching.
    • Immediate workspace handoffs now occur only when all visible terminal views are ready.
    • Increased the default number of retained warm renderers from 1 to 4 to improve transitions between recently used terminals.