Fix workspace switch latency from hibernation portal reconcile

Merged
AU
4mo ago
+30-1

Summary

Fixes #7226.

Switching into an already-open workspace was running setAgentHibernationAutoResumePresentationVisible(true), which immediately performed a full terminal portal visibility reconcile. That path is meant to auto-resume hibernated agents, but for ordinary non-hibernated terminal panes it restored every terminal portal from inside SwiftUI visibility updates. Each restore calls GhosttySurfaceScrollView.setVisibleInUI(true) and refreshSurfaceNow, which forced AppKit layout once per visible terminal.

This narrows the auto-resume visibility gate so it only resumes visible hibernated panels. Normal terminal portal visibility remains owned by the existing SwiftUI/portal layout paths.

Regression coverage

Two-commit structure:

  1. test: cover hibernation gate portal restore adds a focused Swift Testing regression that fails on main: toggling the hibernation presentation gate must not restore a non-hibernated terminal portal.
  2. fix: avoid portal reconcile on workspace auto-resume gate changes the production call to resume hibernated panels only.

Evidence

Baseline debug log from current main on macOS 15.7.4 showed repeated workspace toggles where ws.handoff.complete finished around 105-113 ms, but selected workspace terminal visibility then advanced per surface around 193 ms through 648 ms, with ws.select.asyncDone around 1.2-1.35 s. The sample attributed the slow block to WorkspaceContentView visibility onChange -> GhosttySurfaceScrollView.setVisibleInUI -> refreshSurfaceNow -> AppKit layoutSubtreeIfNeeded.

Local verification

No local build, local tests, or bare xcodebuild were run per handoff constraint. Non-build guards run:

  • python3 scripts/swift_file_length_budget.py --budget .github/swift-file-length-budget.tsv
  • ./scripts/lint-pbxproj-test-wiring.sh
  • ./scripts/check-pbxproj.sh
  • git diff --check

Dogfood build

Do not launch until explicitly requested. After CI is green, use:

CMUX_SKIP_ZIG_BUILD=1 /Users/austinwang/manaflow/cmuxterm-hq/scripts/reload-cloud.sh --tag issue-7226-workspace-switch-latency --launch

View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.


Summary by cubic

Fixes workspace switch latency by stopping the full terminal portal reconcile when showing the hibernation auto‑resume presentation. Only visible hibernated panels resume; non‑hibernated terminals keep their visibility (fixes #7226).

  • Bug Fixes
    • Use resumeVisibleAgentHibernationPanels(panelIds: agentHibernationVisiblePanelIdsForCurrentLayout()) instead of reconcileTerminalPortalVisibilityForCurrentRenderedLayout() when enabling auto‑resume.
    • Add and wire AgentHibernationPortalVisibilityTests to verify toggling the auto‑resume presentation does not restore a non‑hibernated terminal portal.

Summary by CodeRabbit

  • Bug Fixes

    • Improved behavior when enabling auto-resume presentations so only eligible agent hibernations are handled.
    • Prevented non-hibernated terminal portals from becoming visible when auto-resume presentation is toggled.
  • Tests

    • Added UI-focused test coverage to verify auto-resume visibility behavior keeps non-hibernated terminal portals hidden.