diff --git a/CLI/CMUXCLI+SSHPTYAttachBridge.swift b/CLI/CMUXCLI+SSHPTYAttachBridge.swift index a5e55a3c0d6d..9376e5755cb2 100644 --- a/CLI/CMUXCLI+SSHPTYAttachBridge.swift +++ b/CLI/CMUXCLI+SSHPTYAttachBridge.swift @@ -35,6 +35,20 @@ extension CMUXCLI { return exitCode.isWrapperRetryable } + /// True when the persistent attach wrapper will retry this failure itself. + /// + /// The wrapper prints its own "reattaching (attempt n/limit)" notice, so + /// also printing `Error: ...` renders a recovered reconnect as a hard + /// failure in the user's terminal. Failures the wrapper will not retry + /// still print normally, and so does every non-attach command. + func attachWrapperReportsFailureItself(_ error: Error) -> Bool { + guard let cliError = error as? CLIError, + let exitCode = SSHPTYAttachExitCode(rawValue: cliError.exitCode) else { + return false + } + return sshPTYAttachWrapperWillRetry(exitCode) + } + func sshPTYAttachBridgeClosedExitCode( receivedLiveOutput: Bool, readyUptime: TimeInterval diff --git a/CLI/cmux.swift b/CLI/cmux.swift index ba2b99dea761..baea5f1142e0 100644 --- a/CLI/cmux.swift +++ b/CLI/cmux.swift @@ -36367,7 +36367,9 @@ struct CMUXTermMain { do { try cli.run() } catch { - CMUXCLIOutput.writeStandardError("Error: \(error)\n") + if !cli.attachWrapperReportsFailureItself(error) { + CMUXCLIOutput.writeStandardError("Error: \(error)\n") + } let exitCode = (error as? CLIError)?.exitCode ?? 1 exit(exitCode) } diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index c0127e06ca21..bcbcb4427f2a 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -2750,6 +2750,24 @@ final class Workspace: Identifiable, ObservableObject { } } + /// Retracts daemon errors that a later ready daemon resolved. + /// + /// Call only once the daemon reports `.ready`: every daemon error recorded + /// before that point describes an outage the reconnect state machine has + /// since healed, so keeping it would leave a red sidebar entry as the last + /// word on a working workspace. + func clearResolvedRemoteDaemonSidebarArtifacts() { + // A ready daemon only proves the management lane recovered. When the + // workspace has no remote terminal session, its terminal has fallen + // back to a local shell while the row still reads "Connected", and + // retracting here would erase the only sign that anything went wrong. + // Keep the errors visible until a remote terminal is actually alive. + guard hasActiveRemoteTerminalSessions else { return } + logEntries.removeAll { entry in + entry.source == "remote-daemon" && entry.level == .error + } + } + private func remoteNotificationCooldownKey(target: String) -> String? { let rawTarget = (remoteConfiguration?.destination ?? target) .trimmingCharacters(in: .whitespacesAndNewlines) @@ -6955,7 +6973,15 @@ final class Workspace: Identifiable, ObservableObject { // #8917: a transport bounce that re-bootstrapped successfully is // not a workspace failure, so retract its sidebar error the same // way the connection-state path retracts on `.connected`. - clearProxyOnlyRemoteDaemonSidebarArtifacts() + // + // A ready daemon has resolved every daemon error recorded before + // it, so retract all of them rather than only the proxy-only + // subset. Relay bounces publish `.error` details that already say + // "retrying in 2 seconds" and always schedule a reconnect; leaving + // those behind made a fully recovered reconnect read as a red + // failure in the sidebar. Errors the daemon never recovers from + // still persist, because this only runs on `.ready`. + clearResolvedRemoteDaemonSidebarArtifacts() } return } diff --git a/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift b/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift index 0653c5eb346b..f5de1416d5c4 100644 --- a/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift +++ b/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift @@ -9,6 +9,9 @@ import Testing @testable import cmux #endif +/// Covers which remote-daemon errors survive on a workspace's sidebar row: +/// errors a recovered daemon resolved are retracted, errors that are still +/// true are not. @MainActor struct WorkspaceRemoteDaemonRecoveryTests { /// https://github.com/manaflow-ai/cmux/issues/8917: a daemon transport @@ -16,22 +19,7 @@ struct WorkspaceRemoteDaemonRecoveryTests { /// on the workspace's sidebar row. @Test func recoveredDaemonTransportBounceClearsSidebarDaemonError() { - let workspace = Workspace() - let config = WorkspaceRemoteConfiguration( - destination: "dev@example.com", - port: 22, - identityFile: nil, - sshOptions: [], - localProxyPort: nil, - relayPort: 64_021, - relayID: String(repeating: "c", count: 16), - relayToken: String(repeating: "d", count: 64), - localSocketPath: "/tmp/cmux-debug-test.sock", - terminalStartupCommand: "ssh dev@example.com", - preserveAfterTerminalExit: true, - skipDaemonBootstrap: true - ) - workspace.configureRemoteConnection(config, autoConnect: false) + let workspace = makeRemoteWorkspace() workspace.applyRemoteDaemonStatusUpdate( WorkspaceRemoteDaemonStatus(state: .ready), @@ -61,4 +49,118 @@ struct WorkspaceRemoteDaemonRecoveryTests { "The workspace row must not keep rendering the recovered daemon error" ) } + + /// A reverse-relay bounce publishes a daemon error whose own detail says it + /// is retrying, and the reconnect state machine always schedules that retry. + /// Once the daemon reports ready again the workspace is healthy, so the + /// sidebar must not keep a red row for the outage that already healed. + @Test + func recoveredRelayBounceClearsSidebarDaemonError() { + let workspace = makeRemoteWorkspace() + + for detail in [ + "Remote SSH relay unavailable; retrying in 2 seconds", + "Remote SSH relay port unavailable; retrying in 2 seconds", + ] { + workspace.applyRemoteDaemonStatusUpdate( + WorkspaceRemoteDaemonStatus(state: .error, detail: detail), + target: "dev@example.com" + ) + } + + #expect( + workspace.logEntries.contains { $0.source == "remote-daemon" && $0.level == .error }, + "The relay bounce should be recorded while the daemon is still down" + ) + + workspace.applyRemoteDaemonStatusUpdate( + WorkspaceRemoteDaemonStatus(state: .ready), + target: "dev@example.com" + ) + + #expect( + workspace.logEntries.contains { $0.source == "remote-daemon" && $0.level == .error } == false, + "A relay bounce the daemon recovered from must not stay a sidebar error" + ) + } + + /// The retraction is tied to the daemon actually recovering, so an outage + /// that never reaches ready keeps its sidebar error. + @Test + func daemonErrorWithoutRecoveryKeepsSidebarEntry() { + let workspace = makeRemoteWorkspace() + + workspace.applyRemoteDaemonStatusUpdate( + WorkspaceRemoteDaemonStatus( + state: .error, + detail: "Remote SSH relay unavailable; retrying in 2 seconds" + ), + target: "dev@example.com" + ) + workspace.applyRemoteDaemonStatusUpdate( + WorkspaceRemoteDaemonStatus(state: .bootstrapping, detail: "Reconnecting"), + target: "dev@example.com" + ) + + #expect( + workspace.logEntries.contains { $0.source == "remote-daemon" && $0.level == .error }, + "An unrecovered daemon outage must keep reporting itself" + ) + } + + /// Retraction must not paper over a workspace whose remote terminal died: + /// the daemon can be ready while the surface has fallen back to a local + /// shell and the row still reads "Connected". The daemon error is then the + /// only visible sign that the workspace is not really remote. + @Test + func daemonReadyWithoutRemoteTerminalKeepsSidebarDaemonError() { + let workspace = makeRemoteWorkspace(withRemoteTerminal: false) + + workspace.applyRemoteDaemonStatusUpdate( + WorkspaceRemoteDaemonStatus( + state: .error, + detail: "Remote SSH relay unavailable; retrying in 2 seconds" + ), + target: "dev@example.com" + ) + workspace.applyRemoteDaemonStatusUpdate( + WorkspaceRemoteDaemonStatus(state: .ready), + target: "dev@example.com" + ) + + #expect( + workspace.hasActiveRemoteTerminalSessions == false, + "This case is about a workspace with no live remote terminal" + ) + #expect( + workspace.logEntries.contains { $0.source == "remote-daemon" && $0.level == .error }, + "A ready daemon with no remote terminal session must not clear the error" + ) + } + + /// Builds a remote workspace. `withRemoteTerminal` mirrors a workspace whose + /// surface is actually attached to the remote host; without it the workspace + /// models the fallback case where the terminal is no longer remote. + private func makeRemoteWorkspace(withRemoteTerminal: Bool = true) -> Workspace { + let workspace = Workspace() + let config = WorkspaceRemoteConfiguration( + destination: "dev@example.com", + port: 22, + identityFile: nil, + sshOptions: [], + localProxyPort: nil, + relayPort: 64_021, + relayID: String(repeating: "c", count: 16), + relayToken: String(repeating: "d", count: 64), + localSocketPath: "/tmp/cmux-debug-test.sock", + terminalStartupCommand: "ssh dev@example.com", + preserveAfterTerminalExit: true, + skipDaemonBootstrap: true + ) + workspace.configureRemoteConnection(config, autoConnect: false) + if withRemoteTerminal { + workspace.trackRemoteTerminalSurface(UUID()) + } + return workspace + } }