From 9ed4bf4f522058e676b03dc503300259f7cbb2bb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 20 Jun 2026 20:12:14 -0700 Subject: [PATCH] fix(desktop): preserve structural SSH IPC errors Co-authored-by: codex --- .../src/ipc/methods/sshEnvironment.test.ts | 48 ++++++++++++------- .../desktop/src/ipc/methods/sshEnvironment.ts | 37 ++------------ 2 files changed, 35 insertions(+), 50 deletions(-) diff --git a/apps/desktop/src/ipc/methods/sshEnvironment.test.ts b/apps/desktop/src/ipc/methods/sshEnvironment.test.ts index ef4938db86..a9cba3b149 100644 --- a/apps/desktop/src/ipc/methods/sshEnvironment.test.ts +++ b/apps/desktop/src/ipc/methods/sshEnvironment.test.ts @@ -5,7 +5,6 @@ import { } from "@t3tools/contracts"; import { SshCommandSpawnError, - SshCommandTimeoutError, SshHttpBridgeError, SshPasswordPromptWindowClosedError, } from "@t3tools/ssh/errors"; @@ -21,9 +20,9 @@ import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse"; import { DesktopSshEnvironmentRequestError, + disconnectSshEnvironment, ensureSshEnvironment, fetchSshEnvironmentDescriptor, - toDesktopSshIpcPresentationError, } from "./sshEnvironment.ts"; import * as DesktopSshEnvironment from "../../ssh/DesktopSshEnvironment.ts"; import * as DesktopSshPasswordPrompts from "../../ssh/DesktopSshPasswordPrompts.ts"; @@ -95,7 +94,7 @@ describe("SSH environment IPC", () => { }).pipe(Effect.provide(layer)); }); - it("presents legacy process causes without weakening structured errors", () => { + it.effect("preserves structural SSH errors and exact causes at the IPC boundary", () => { const cause = new Error("ssh executable was not found"); const structured = new SshCommandSpawnError({ command: "ssh", @@ -103,22 +102,35 @@ describe("SSH environment IPC", () => { target: "devbox", cause, }); + const target = { + alias: "devbox", + hostname: "devbox.example.test", + username: "developer", + port: 22, + }; + const layer = Layer.succeed( + DesktopSshEnvironment.DesktopSshEnvironment, + DesktopSshEnvironment.DesktopSshEnvironment.of({ + discoverHosts: () => Effect.die("unexpected host discovery"), + ensureEnvironment: () => Effect.fail(structured), + disconnectEnvironment: () => Effect.fail(structured), + }), + ); - const presentation = toDesktopSshIpcPresentationError(structured); - assert.equal(structured.message, "Failed to spawn SSH command for devbox."); - assert.equal(presentation.message, cause.message); - assert.strictEqual(presentation.cause, structured); - }); - - it("passes structural SSH errors without process causes through the IPC boundary", () => { - const structured = new SshCommandTimeoutError({ - command: "ssh", - argumentCount: 0, - target: "devbox", - timeoutMs: 30_000, - }); - - assert.strictEqual(toDesktopSshIpcPresentationError(structured), structured); + return Effect.gen(function* () { + const exits = yield* Effect.all([ + Effect.exit(ensureSshEnvironment.handler({ target })), + Effect.exit(disconnectSshEnvironment.handler(target)), + ]); + for (const exit of exits) { + assert(Exit.isFailure(exit)); + const failure = Cause.findErrorOption(exit.cause); + assert(Option.isSome(failure)); + assert.strictEqual(failure.value, structured); + } + assert.equal(structured.message, "Failed to spawn SSH command for devbox."); + assert.strictEqual(structured.cause, cause); + }).pipe(Effect.provide(layer)); }); it.effect("fetches and decodes the remote environment descriptor", () => { diff --git a/apps/desktop/src/ipc/methods/sshEnvironment.ts b/apps/desktop/src/ipc/methods/sshEnvironment.ts index 12aeb8e7cd..f5c55d9b39 100644 --- a/apps/desktop/src/ipc/methods/sshEnvironment.ts +++ b/apps/desktop/src/ipc/methods/sshEnvironment.ts @@ -26,14 +26,7 @@ import { AuthSessionState, AuthWebSocketTicketResult, } from "@t3tools/contracts"; -import { - SshCommandExecutionError, - SshCommandSpawnError, - SshHttpBridgeError, - type SshPasswordPromptError, - SshTunnelMonitorError, - SshTunnelSpawnError, -} from "@t3tools/ssh/errors"; +import { SshHttpBridgeError, type SshPasswordPromptError } from "@t3tools/ssh/errors"; import { resolveLoopbackSshHttpBaseUrl } from "@t3tools/ssh/tunnel"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; @@ -83,26 +76,6 @@ const isEnvironmentOperationForbiddenError = Schema.is(EnvironmentOperationForbi const isEnvironmentRequestInvalidError = Schema.is(EnvironmentRequestInvalidError); const isEnvironmentScopeRequiredError = Schema.is(EnvironmentScopeRequiredError); const isSshHttpBridgeError = Schema.is(SshHttpBridgeError); -const isSshCausePresentationError = Schema.is( - Schema.Union([ - SshCommandSpawnError, - SshCommandExecutionError, - SshTunnelSpawnError, - SshTunnelMonitorError, - ]), -); - -// Electron only forwards the message from a rejected ipcRenderer.invoke promise. Keep the typed, -// structural SSH error intact as the cause for main-process diagnostics while preserving the -// actionable process message that the renderer historically presented to the user. -export function toDesktopSshIpcPresentationError( - error: DesktopSshEnvironment.DesktopSshEnvironmentOperationError, -): DesktopSshEnvironment.DesktopSshEnvironmentOperationError | Error { - if (isSshCausePresentationError(error) && error.cause instanceof Error) { - return new Error(error.cause.message, { cause: error }); - } - return error; -} function readSshHttpStatus(cause: DesktopSshEnvironmentRequestCause): number | null { if (isRemoteEnvironmentAuthUndeclaredStatusError(cause)) { @@ -169,6 +142,9 @@ export const discoverSshHosts = DesktopIpc.makeIpcMethod({ }), }); +// Electron forwards only the message from a rejected ipcRenderer.invoke promise to the renderer. +// Preserve typed SSH errors at this boundary so that message stays structural while main-process +// diagnostics retain the exact cause and error fields. export const ensureSshEnvironment = DesktopIpc.makeIpcMethod({ channel: IpcChannels.ENSURE_SSH_ENVIRONMENT_CHANNEL, payload: DesktopSshEnvironmentEnsureInputSchema, @@ -185,7 +161,6 @@ export const ensureSshEnvironment = DesktopIpc.makeIpcMethod({ SshPasswordPromptWindowClosedError: handleDesktopSshPasswordPromptCancellation, SshPasswordPromptServiceStoppedError: handleDesktopSshPasswordPromptCancellation, }), - Effect.mapError(toDesktopSshIpcPresentationError), ); }), }); @@ -196,9 +171,7 @@ export const disconnectSshEnvironment = DesktopIpc.makeIpcMethod({ result: Schema.Void, handler: Effect.fn("desktop.ipc.sshEnvironment.disconnectEnvironment")(function* (target) { const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment; - yield* sshEnvironment - .disconnectEnvironment(target) - .pipe(Effect.mapError(toDesktopSshIpcPresentationError)); + yield* sshEnvironment.disconnectEnvironment(target); }), });