From 0716db1ce22578e7a46601ecc1afd99619b88dfa Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Wed, 5 Aug 2026 12:52:32 -0700 Subject: [PATCH 1/3] Stop reporting recovered remote reconnects as errors Quitting or restarting cmux while a remote PTY is attached surfaced two failures for an outage that healed itself seconds later: the terminal printed a red "Error: ssh-pty-attach: bridge closed before remote PTY exit could be confirmed: remote daemon is not ready", and the sidebar kept a red "Remote daemon error" row on a workspace that was connected. The CLI now leaves reporting to the persistent attach wrapper whenever that wrapper will retry the failure itself; the wrapper already prints its own reattaching notice, so the error line was duplicate and made a successful reconnect look broken. Failures the wrapper will not retry still print. The sidebar now retracts every daemon error once the daemon reports ready, instead of only the proxy-only subset. Relay bounces publish details that say "retrying in 2 seconds" and always schedule a reconnect, but did not match the proxy-only substrings, so they stayed red forever. Errors the daemon never recovers from still persist, because retraction only runs on ready. Co-Authored-By: Claude Opus 5 --- CLI/CMUXCLI+SSHPTYAttachBridge.swift | 14 ++++ CLI/cmux.swift | 4 +- Sources/Workspace.swift | 22 +++++- .../WorkspaceRemoteDaemonRecoveryTests.swift | 78 +++++++++++++++++++ 4 files changed, 116 insertions(+), 2 deletions(-) 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..6aa88e073f0e 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -2750,6 +2750,18 @@ 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() { + 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 +6967,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..e70ed6540f6f 100644 --- a/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift +++ b/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift @@ -61,4 +61,82 @@ 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" + ) + } + + private func makeRemoteWorkspace() -> 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) + return workspace + } } From bc5d53aad5eb66212432fed886fe61d89fb24eaf Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Wed, 5 Aug 2026 14:01:03 -0700 Subject: [PATCH 2/3] Keep daemon errors visible when the terminal is not remote active_terminal_sessions=0 on a workspace that still reports Connected is the signature of a remote terminal that fell back to a local shell: the sidebar row, the tab title, and the connection state all still name the remote host while the prompt is local. Retracting daemon errors there would erase the only visible sign the workspace is not remote, so retraction now requires a live remote terminal session as well as a ready daemon. Co-Authored-By: Claude Opus 5 --- Sources/Workspace.swift | 6 ++ .../WorkspaceRemoteDaemonRecoveryTests.swift | 55 +++++++++++++------ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index 6aa88e073f0e..bcbcb4427f2a 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -2757,6 +2757,12 @@ final class Workspace: Identifiable, ObservableObject { /// 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 } diff --git a/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift b/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift index e70ed6540f6f..2c02e3617507 100644 --- a/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift +++ b/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift @@ -16,22 +16,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), @@ -120,7 +105,40 @@ struct WorkspaceRemoteDaemonRecoveryTests { ) } - private func makeRemoteWorkspace() -> Workspace { + /// 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", @@ -137,6 +155,9 @@ struct WorkspaceRemoteDaemonRecoveryTests { skipDaemonBootstrap: true ) workspace.configureRemoteConnection(config, autoConnect: false) + if withRemoteTerminal { + workspace.trackRemoteTerminalSurface(UUID()) + } return workspace } } From d3ebc23775f1777eded6216308e9354ef9642c01 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Wed, 5 Aug 2026 15:06:25 -0700 Subject: [PATCH 3/3] Document the daemon-error recovery suite --- cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift b/cmuxTests/WorkspaceRemoteDaemonRecoveryTests.swift index 2c02e3617507..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