fix: move startup session preparation off the main thread

AU
24d ago
+402-110

Summary

Fixes the startup session persistence path behind Sentry incident CMUXTERM-MACOS-3ZVC (issue 7780739411). Session snapshot decoding, history/backup synchronization, crash-diagnostic pruning, and clean-launch scrollback checkpoint cleanup now run in a utility task. Initial window bootstrap and autosave/checkpoint services wait for the result, preserving restore ordering while removing file-backed work from the main actor.

Lifecycle saves join the same preparation barrier so explicit opens, power events, update relaunches, and termination cannot write or remove the snapshot while startup preparation owns it. A canceled quit resumes deferred persistence and window bootstrap.

The Sentry event set contains many post-launch AppKit/SwiftUI and LaunchServices stacks, so it does not independently prove that every grouped event came from this session-preparation path. The code fix addresses the app-owned synchronous startup I/O identified in the startup path; a tagged-build capture is still needed for quantitative attribution.

Testing

  • python3 scripts/verify-local.py — 17/17 selected checks passed.
  • ./scripts/sync-test-wiring --check — passed.
  • ./scripts/lint-pbxproj-test-wiring.sh — passed.
  • git diff --check — passed.
  • Added CmuxMainStartupTests.startupPreparationPreservesRestoreOrderingAndCleansCheckpoints using a temporary snapshot repository.
  • Native compilation and XCTest were not run locally: nx-remote cannot resolve the configured build host from this environment, and repository policy requires hosted XCTest.

Changelog

Fixed: Startup session preparation no longer performs snapshot file work on the main thread.

Proof

No GUI proof: this is a startup persistence scheduling change; native build and hosted XCTest are required for runtime proof.

Checklist

  • Behavior changes have added or updated tests
  • User-facing docs updated if needed
  • Reviewed with a correctness-first subagent before merge; findings resolved in the lifecycle barrier and backup cleanup ordering

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


Summary by cubic

Fixes a startup hang by moving session snapshot file work off the main thread and into the StartupSessionSnapshotPreparation actor. Snapshot decoding, history and backup sync, crash-diagnostic pruning, and scrollback checkpoint cleanup now run at utility priority.

Behavior changes

  • Window bootstrap and autosave/checkpoint services wait for the preparation task, preserving restore ordering.
  • Quit, power-event, and update-relaunch saves join the same barrier; a canceled quit resumes deferred persistence and window bootstrap.

Summary by CodeRabbit

  • Bug Fixes
    • Improved startup session recovery so saved windows and scrollback are prepared before restoration and persistence, reducing the risk of incomplete or conflicting session state.
    • Explicitly opening a session continues to take precedence over automatic startup restoration.
    • Improved save handling during app termination and power events, including cases where startup preparation is still underway.