From 4c7ea19b46665d25a86f695f01f22d2dd71c469e Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:46:43 -0700 Subject: [PATCH 1/5] test: preserve iOS viewport lease across backgrounding --- .../TerminalSurfaceMountOwnershipTests.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift index c0a809a0644f..cb793ad69f87 100644 --- a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift +++ b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift @@ -150,12 +150,13 @@ struct TerminalSurfaceMountOwnershipTests { } #expect(remounted) - // Presentation ownership, unlike temporary window attachment, releases - // the sticky viewport lease and its generation-fenced Mac report. + // Backgrounding must keep the sticky viewport lease and its + // generation-fenced Mac report so resume cannot flash the wrong grid. coordinator.setTerminalPresentationActive(false) - #expect(!store.reportedViewportSizesByTerminalKey.values.contains( + #expect(store.reportedViewportSizesByTerminalKey.values.contains( MobileTerminalViewportSize(columns: 72, rows: 61) )) + #expect(store.terminalViewportGeneration(for: surfaceID) == 1) } @MainActor From dbcd2eb3bb11f7adef23fffdd91b01dfe8345c6c Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:49:17 -0700 Subject: [PATCH 2/5] fix: persist iOS terminal sizing handoff preference --- ...faceRepresentable+CoordinatorFactory.swift | 1 + .../GhosttySurfaceRepresentable.swift | 24 ++++++- .../MobileDisplaySettings.swift | 16 +++++ .../MobileSettingsView.swift | 16 +++++ .../Resources/Localizable.xcstrings | 28 ++++++++ ...orkspaceDetailView+TerminalArtifacts.swift | 1 + .../MobileDisplaySettingsTests.swift | 16 +++++ .../TerminalSurfaceMountOwnershipTests.swift | 65 ++++++++++++++++++- 8 files changed, 164 insertions(+), 3 deletions(-) diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable+CoordinatorFactory.swift b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable+CoordinatorFactory.swift index 9a69d3feff19..9616fcd517dd 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable+CoordinatorFactory.swift +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable+CoordinatorFactory.swift @@ -8,6 +8,7 @@ extension GhosttySurfaceRepresentable { surfaceID: surfaceID, store: store, terminalPresentationIsActive: terminalPresentationIsActive, + handoffTerminalSizingWhenInactive: handoffTerminalSizingWhenInactive, artifactFilesEnabled: artifactFilesEnabled, terminalFolderTapEnabled: terminalFolderTapEnabled, terminalFilesChipEnabled: terminalFilesChipEnabled, diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift index 308195294a46..d9ed99e5f773 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift @@ -58,6 +58,11 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { /// screens. The default uses the settled visible-height grid for /// alternate-screen apps. var useLegacyTerminalSizing: Bool = false + /// When enabled, backgrounding releases this phone's viewport so the Mac + /// can take the terminal grid. The default preserves the viewport lease + /// across a background/foreground transition to keep the host policy and + /// rendered grid stable. + var handoffTerminalSizingWhenInactive: Bool = false var sessionArtifactCountEnabled: Bool = false var visibleArtifactCount: Int = 0 /// SSH terminals: show the Files chip regardless of paths on screen; it @@ -163,6 +168,9 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { // composer band. This is a UIKit-internal mutation, not a sibling-observed // state write, so it is safe in `updateUIViewController`. context.coordinator.setTerminalPresentationActive(terminalPresentationIsActive) + context.coordinator.setHandoffTerminalSizingWhenInactive( + handoffTerminalSizingWhenInactive + ) context.coordinator.attemptPendingOutputConsumerRecoveryPresentation() guard let surfaceView = (uiView as? GhosttySurfaceHostView)?.surfaceView else { return } surfaceView.terminalWorkPopulation = terminalWorkPopulation @@ -317,6 +325,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { /// will never be emitted again. var outputStartReady = false var terminalPresentationIsActive: Bool + var handoffTerminalSizingWhenInactive: Bool var outputStartContinuation: AsyncStream.Continuation? var outputStartViewportTimeouts = 0 var outputStartMinimumViewportReportID: UInt64? @@ -393,6 +402,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { surfaceID: String, store: CMUXMobileShellStore, terminalPresentationIsActive: Bool = true, + handoffTerminalSizingWhenInactive: Bool = false, artifactFilesEnabled: Bool, terminalFolderTapEnabled: Bool, terminalFilesChipEnabled: Bool, @@ -412,6 +422,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { self.surfaceID = surfaceID self.store = store self.terminalPresentationIsActive = terminalPresentationIsActive + self.handoffTerminalSizingWhenInactive = handoffTerminalSizingWhenInactive self.artifactFilesEnabled = artifactFilesEnabled self.terminalFolderTapEnabled = terminalFolderTapEnabled self.artifactChipGate = TerminalArtifactChipFeatureGate( @@ -1243,10 +1254,21 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { attemptPendingOutputConsumerRecoveryPresentation() } else { outputConsumerRecoveryAlertPending = outputConsumerRestartBlocked - stopMountedTasks(releaseViewport: true) + stopMountedTasks(releaseViewport: handoffTerminalSizingWhenInactive) } } + /// Updates the persisted handoff choice while keeping the current + /// inactive surface consistent. Enabling handoff while the terminal is + /// already backgrounded releases the lease immediately; disabling it + /// preserves any lease until the next foreground mount. + func setHandoffTerminalSizingWhenInactive(_ enabled: Bool) { + guard handoffTerminalSizingWhenInactive != enabled else { return } + handoffTerminalSizingWhenInactive = enabled + guard enabled, !terminalPresentationIsActive else { return } + stopMountedTasks(releaseViewport: true) + } + func detach() { outputConsumerRecoveryAlertPending = false stopMountedTasks(releaseViewport: true) diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileDisplaySettings.swift b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileDisplaySettings.swift index 7010c14f530e..41e7ec9c04a3 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileDisplaySettings.swift +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileDisplaySettings.swift @@ -29,6 +29,7 @@ public final class MobileDisplaySettings { private static let showMissingFilesKey = "cmux.mobile.showMissingFiles" private static let terminalFolderTapEnabledKey = "cmux.mobile.terminalFolderTapEnabled" private static let useLegacyTerminalSizingKey = "cmux.mobile.useLegacyTerminalSizing" + private static let handoffTerminalSizingWhenInactiveKey = "cmux.mobile.handoffTerminalSizingWhenInactive" private static let workspacePreviewLineCountKey = "cmux.mobile.workspacePreviewLineCount" private static let unreadIndicatorLeftShiftKey = "cmux.mobile.debug.unreadIndicatorLeftShift.v2" private static let unreadBadgeDiameterKey = "cmux.mobile.debug.unreadBadgeDiameter.v1" @@ -115,6 +116,18 @@ public final class MobileDisplaySettings { didSet { defaults.set(useLegacyTerminalSizing, forKey: Self.useLegacyTerminalSizingKey) } } + /// Whether an inactive iPhone releases its terminal viewport so a Mac can + /// take the grid. Defaults to `false`, preserving the host policy and the + /// last confirmed grid across a background/foreground transition. + public var handoffTerminalSizingWhenInactive: Bool { + didSet { + defaults.set( + handoffTerminalSizingWhenInactive, + forKey: Self.handoffTerminalSizingWhenInactiveKey + ) + } + } + /// Whether cmux emits app-owned haptic feedback. Defaults to `true`. /// This is the sole observed writer for the persisted preference; haptic /// emitters read the same defaults store through ``haptics``. @@ -238,6 +251,9 @@ public final class MobileDisplaySettings { self.showMissingFiles = defaults.bool(forKey: Self.showMissingFilesKey) self.terminalFolderTapEnabled = defaults.object(forKey: Self.terminalFolderTapEnabledKey) as? Bool ?? true self.useLegacyTerminalSizing = defaults.object(forKey: Self.useLegacyTerminalSizingKey) as? Bool ?? false + self.handoffTerminalSizingWhenInactive = defaults.object( + forKey: Self.handoffTerminalSizingWhenInactiveKey + ) as? Bool ?? false self.hapticFeedbackEnabled = haptics.isEnabled self.terminalScrollbackRows = MobileTerminalScrollbackPreference.resolve(from: defaults) let storedPreviewLines = defaults.object(forKey: Self.workspacePreviewLineCountKey) as? Int diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileSettingsView.swift b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileSettingsView.swift index 7b6f676304e4..3c1d5b97acaf 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileSettingsView.swift +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/MobileSettingsView.swift @@ -307,6 +307,22 @@ struct MobileSettingsView: View { } .accessibilityIdentifier("MobileSettingsLegacyTerminalSizingToggle") + Toggle(isOn: $displaySettings.handoffTerminalSizingWhenInactive) { + VStack(alignment: .leading, spacing: 4) { + Text(L10n.string( + "mobile.settings.handoffTerminalSizing", + defaultValue: "Hand Off Terminal Sizing When Inactive" + )) + Text(L10n.string( + "mobile.settings.handoffTerminalSizing.description", + defaultValue: "Let the Mac choose the terminal size while this app is in the background." + )) + .font(.caption) + .foregroundStyle(.secondary) + } + } + .accessibilityIdentifier("MobileSettingsHandoffTerminalSizingToggle") + Button { showingShortcuts = true } label: { diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/Resources/Localizable.xcstrings b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/Resources/Localizable.xcstrings index a0421859bcf1..c289bd0eb45c 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/Resources/Localizable.xcstrings +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/Resources/Localizable.xcstrings @@ -22580,6 +22580,34 @@ } } }, + "mobile.settings.handoffTerminalSizing": { + "extractionState": "manual", + "localizations": { + "en": {"stringUnit": {"state": "translated", "value": "Hand Off Terminal Sizing When Inactive"}}, + "ja": {"stringUnit": {"state": "translated", "value": "非アクティブ時にターミナルサイズを引き継ぐ"}}, + "de": {"stringUnit": {"state": "translated", "value": "Terminalgröße bei Inaktivität übergeben"}}, + "fr": {"stringUnit": {"state": "translated", "value": "Transférer le dimensionnement du terminal en cas d’inactivité"}}, + "ar": {"stringUnit": {"state": "translated", "value": "تسليم تحجيم الطرفية عند عدم النشاط"}}, + "es": {"stringUnit": {"state": "translated", "value": "Ceder el dimensionamiento del terminal cuando está inactivo"}}, + "zh-Hant": {"stringUnit": {"state": "translated", "value": "非使用中時交接終端機大小"}}, + "zh-Hans": {"stringUnit": {"state": "translated", "value": "不活跃时移交终端大小"}}, + "ko": {"stringUnit": {"state": "translated", "value": "비활성 상태일 때 터미널 크기 넘기기"}} + } + }, + "mobile.settings.handoffTerminalSizing.description": { + "extractionState": "manual", + "localizations": { + "en": {"stringUnit": {"state": "translated", "value": "Let the Mac choose the terminal size while this app is in the background."}}, + "ja": {"stringUnit": {"state": "translated", "value": "このアプリがバックグラウンドにある間は、Macにターミナルサイズを任せます。"}}, + "de": {"stringUnit": {"state": "translated", "value": "Der Mac wählt die Terminalgröße, während diese App im Hintergrund ist."}}, + "fr": {"stringUnit": {"state": "translated", "value": "Laisser le Mac choisir la taille du terminal lorsque l’app est en arrière-plan."}}, + "ar": {"stringUnit": {"state": "translated", "value": "السماح لجهاز Mac باختيار حجم الطرفية عندما يكون هذا التطبيق في الخلفية."}}, + "es": {"stringUnit": {"state": "translated", "value": "Dejar que el Mac elija el tamaño del terminal mientras esta app está en segundo plano."}}, + "zh-Hant": {"stringUnit": {"state": "translated", "value": "此 App 在背景執行時,讓 Mac 選擇終端機大小。"}}, + "zh-Hans": {"stringUnit": {"state": "translated", "value": "此 App 在后台运行时,让 Mac 选择终端大小。"}}, + "ko": {"stringUnit": {"state": "translated", "value": "이 앱이 백그라운드에 있을 때 Mac이 터미널 크기를 선택하도록 합니다."}} + } + }, "mobile.ios106PairingUpdate.releaseLabel": { "extractionState": "manual", "localizations": { diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceDetailView+TerminalArtifacts.swift b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceDetailView+TerminalArtifacts.swift index fb9e2de032e5..42d8fc77cb15 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceDetailView+TerminalArtifacts.swift +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/WorkspaceDetailView+TerminalArtifacts.swift @@ -49,6 +49,7 @@ extension WorkspaceDetailView { terminalFilesChipEnabled: isTerminalFilesChipEnabled, showMissingFiles: showMissingFiles, useLegacyTerminalSizing: displaySettings.useLegacyTerminalSizing, + handoffTerminalSizingWhenInactive: displaySettings.handoffTerminalSizingWhenInactive, sessionArtifactCountEnabled: !isSSH && store.supportsChatArtifactGallery, visibleArtifactCount: visibleArtifactCount, sshFilesChipEnabled: isSSH, diff --git a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/MobileDisplaySettingsTests.swift b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/MobileDisplaySettingsTests.swift index 6a8fbb365a89..917d1f9395d2 100644 --- a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/MobileDisplaySettingsTests.swift +++ b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/MobileDisplaySettingsTests.swift @@ -115,6 +115,22 @@ import Testing #expect(!MobileDisplaySettings(defaults: defaults).useLegacyTerminalSizing) } + @Test func handoffTerminalSizingDefaultsToOffWithoutAWrite() throws { + let defaults = try makeDefaults("handoffTerminalSizingDefaults") + let settings = MobileDisplaySettings(defaults: defaults) + #expect(!settings.handoffTerminalSizingWhenInactive) + #expect(defaults.object(forKey: "cmux.mobile.handoffTerminalSizingWhenInactive") == nil) + } + + @Test func handoffTerminalSizingPersistsAcrossInstances() throws { + let defaults = try makeDefaults("handoffTerminalSizingPersists") + let settings = MobileDisplaySettings(defaults: defaults) + settings.handoffTerminalSizingWhenInactive = true + #expect(MobileDisplaySettings(defaults: defaults).handoffTerminalSizingWhenInactive) + settings.handoffTerminalSizingWhenInactive = false + #expect(!MobileDisplaySettings(defaults: defaults).handoffTerminalSizingWhenInactive) + } + @Test func hapticFeedbackDefaultsToEnabledWithoutAWrite() throws { let defaults = try makeDefaults("hapticFeedbackDefaults") let settings = MobileDisplaySettings(defaults: defaults) diff --git a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift index cb793ad69f87..a5f978286a92 100644 --- a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift +++ b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift @@ -58,6 +58,7 @@ struct TerminalSurfaceMountOwnershipTests { workspaceID: workspace.id.rawValue, surfaceID: surfaceID, store: store, + handoffTerminalSizingWhenInactive: true, artifactFilesEnabled: false, terminalFolderTapEnabled: false, terminalFilesChipEnabled: false, @@ -150,9 +151,69 @@ struct TerminalSurfaceMountOwnershipTests { } #expect(remounted) - // Backgrounding must keep the sticky viewport lease and its - // generation-fenced Mac report so resume cannot flash the wrong grid. + // Presentation ownership releases the sticky viewport lease only when + // the explicit handoff preference is enabled. coordinator.setTerminalPresentationActive(false) + #expect(!store.reportedViewportSizesByTerminalKey.values.contains( + MobileTerminalViewportSize(columns: 72, rows: 61) + )) + } + + @MainActor + @Test("backgrounding preserves the viewport lease by default") + func backgroundingPreservesViewportLeaseByDefault() async throws { + let store = MobileShellComposite.preview() + let workspace = try #require(store.workspaces.first { !$0.terminals.isEmpty }) + let terminal = try #require(workspace.terminals.first) + let surfaceID = terminal.id.rawValue + let coordinator = GhosttySurfaceRepresentable.Coordinator( + workspaceID: workspace.id.rawValue, + surfaceID: surfaceID, + store: store, + artifactFilesEnabled: false, + terminalFolderTapEnabled: false, + terminalFilesChipEnabled: false, + sessionArtifactCountEnabled: false, + visibleArtifactCount: 0, + onArtifactFilesRequested: { _ in }, + onArtifactPathTapped: { _ in }, + onVisibleArtifactCountChanged: { _ in }, + onArtifactGalleryRefreshSignal: { _ in } + ) + let surfaceView = GhosttySurfaceView( + runtime: try GhosttyRuntime.shared(), + delegate: coordinator + ) + surfaceView.stopDisplayLink() + let host = UIViewController() + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844)) + window.rootViewController = host + window.makeKeyAndVisible() + coordinator.attach(surfaceView: surfaceView) + defer { + surfaceView.removeFromSuperview() + coordinator.detach() + surfaceView.prepareForDismantle() + window.isHidden = true + } + surfaceView.frame = host.view.bounds + host.view.addSubview(surfaceView) + coordinator.ghosttySurfaceView( + surfaceView, + didResize: TerminalGridSize( + columns: 72, + rows: 61, + pixelWidth: 1_296, + pixelHeight: 2_135 + ), + reportID: 1 + ) + #expect(await waitUntil { + store.terminalOutputStreamTokensBySurfaceID[surfaceID] != nil + }) + + coordinator.setTerminalPresentationActive(false) + #expect(store.reportedViewportSizesByTerminalKey.values.contains( MobileTerminalViewportSize(columns: 72, rows: 61) )) From 6a9daba99545fb3bc116f98b64e2db9014475848 Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 8 Oct 2026 13:58:17 -0700 Subject: [PATCH 3/5] test: cover iOS terminal resume lease --- .../TerminalSurfaceMountOwnershipTests.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift index a5f978286a92..91ecc99d99e9 100644 --- a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift +++ b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift @@ -218,6 +218,15 @@ struct TerminalSurfaceMountOwnershipTests { MobileTerminalViewportSize(columns: 72, rows: 61) )) #expect(store.terminalViewportGeneration(for: surfaceID) == 1) + + coordinator.setTerminalPresentationActive(true) + #expect(await waitUntil { + store.terminalOutputStreamTokensBySurfaceID[surfaceID] != nil + }) + #expect(store.reportedViewportSizesByTerminalKey.values.contains( + MobileTerminalViewportSize(columns: 72, rows: 61) + )) + #expect(store.terminalViewportGeneration(for: surfaceID) == 1) } @MainActor From 4e3dafcb783d0f0e241f77c2e2e2c17fcdcaf4f9 Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:46:56 +0000 Subject: [PATCH 4/5] test: preserve iOS viewport during foreground recovery --- .../TerminalSurfaceMountOwnershipTests.swift | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift index 91ecc99d99e9..6a34bb2dae39 100644 --- a/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift +++ b/Packages/iOS/CmuxMobileShellUI/Tests/CmuxMobileShellUITests/TerminalSurfaceMountOwnershipTests.swift @@ -229,6 +229,78 @@ struct TerminalSurfaceMountOwnershipTests { #expect(store.terminalViewportGeneration(for: surfaceID) == 1) } + @MainActor + @Test("foreground recovery preserves the viewport lease by default") + func foregroundRecoveryPreservesViewportLeaseByDefault() async throws { + let store = MobileShellComposite.preview() + let workspace = try #require(store.workspaces.first { !$0.terminals.isEmpty }) + let terminal = try #require(workspace.terminals.first) + let surfaceID = terminal.id.rawValue + let coordinator = GhosttySurfaceRepresentable.Coordinator( + workspaceID: workspace.id.rawValue, + surfaceID: surfaceID, + store: store, + artifactFilesEnabled: false, + terminalFolderTapEnabled: false, + terminalFilesChipEnabled: false, + sessionArtifactCountEnabled: false, + visibleArtifactCount: 0, + onArtifactFilesRequested: { _ in }, + onArtifactPathTapped: { _ in }, + onVisibleArtifactCountChanged: { _ in }, + onArtifactGalleryRefreshSignal: { _ in } + ) + let surfaceView = GhosttySurfaceView( + runtime: try GhosttyRuntime.shared(), + delegate: coordinator + ) + surfaceView.stopDisplayLink() + let host = UIViewController() + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844)) + window.rootViewController = host + window.makeKeyAndVisible() + coordinator.attach(surfaceView: surfaceView) + defer { + surfaceView.removeFromSuperview() + coordinator.detach() + surfaceView.prepareForDismantle() + window.isHidden = true + } + + surfaceView.frame = host.view.bounds + host.view.addSubview(surfaceView) + coordinator.ghosttySurfaceView( + surfaceView, + didResize: TerminalGridSize( + columns: 72, + rows: 61, + pixelWidth: 1_296, + pixelHeight: 2_135 + ), + reportID: 1 + ) + #expect(await waitUntil { + store.terminalOutputStreamTokensBySurfaceID[surfaceID] != nil + }) + + // Simulate an inactive cleanup that preserved the lease, followed by + // the app-active callback after the output task disappeared. The + // callback must not turn this transient stream recovery into a real + // viewport handoff. + coordinator.setTerminalPresentationActive(false) + #expect(store.terminalViewportGeneration(for: surfaceID) == 1) + coordinator.terminalPresentationIsActive = true + coordinator.ghosttySurfaceViewDidBecomeActive(surfaceView) + + #expect(await waitUntil { + store.terminalOutputStreamTokensBySurfaceID[surfaceID] != nil + }) + #expect(store.reportedViewportSizesByTerminalKey.values.contains( + MobileTerminalViewportSize(columns: 72, rows: 61) + )) + #expect(store.terminalViewportGeneration(for: surfaceID) == 1) + } + @MainActor @Test("a terminated output stream is reclaimed while the surface remains mounted") func terminatedOutputStreamIsReclaimedWhileSurfaceRemainsMounted() async throws { From d308b17b44c91d750ca1b3214a6b1c21fd48111f Mon Sep 17 00:00:00 2001 From: Austin Wang <38676809+austinywang@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:47:13 +0000 Subject: [PATCH 5/5] fix: preserve viewport during iOS foreground recovery --- .../Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift index d9ed99e5f773..67c5ff4ac3a0 100644 --- a/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift +++ b/Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/GhosttySurfaceRepresentable.swift @@ -1337,7 +1337,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable { "terminal.output.foreground_reconcile surface=\(surfaceID) " + "task=\(outputTask != nil) owner=\(ownsCurrentStream)" ) - stopMountedTasks(releaseViewport: true) + stopMountedTasks(releaseViewport: handoffTerminalSizingWhenInactive) startMountedTasks( surfaceView: surfaceView, resetRestartFailure: true