Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(switchdash): fail gateway calls with the host state, not a localhost error (CHOO-1780)

amaudruz8c8ab775c110
diff --git a/dash/apps/switchdash-desktop/src/main/core/agents/remote-session-reconciler.ts b/dash/apps/switchdash-desktop/src/main/core/agents/remote-session-reconciler.ts
index 1670c2922..9de7ab3b0 100644
--- a/dash/apps/switchdash-desktop/src/main/core/agents/remote-session-reconciler.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/agents/remote-session-reconciler.ts
@@ -5,7 +5,6 @@ import type { SidecarEndpoint } from '@main/core/agent-runtime/impl/remote-sidec
import { httpGetJsonOverChannel } from '@main/core/agent-runtime/impl/sidecar-http';
import { sshConnectionIdForHost } from '@main/core/locations/location-transport';
import type { HostReachabilityChange } from '@main/core/remote-hosts/host-reachability-service';
-import { HostUnreachableError } from '@main/core/remote-hosts/host-reachability-service';
import { hostReachabilityService } from '@main/core/remote-hosts/production-host-reachability';
import { sessionHooks } from '@main/core/sessions/session-hooks';
import { sessionService } from '@main/core/sessions/session-service';
@@ -18,6 +17,7 @@ import { log } from '@main/lib/logger';
import { toSwitchSpecialization } from '@shared/core/agents/agent-provider-config';
import type { Agent } from '@shared/core/agents/agents';
import { makePtyId } from '@shared/core/pty/ptyId';
+import { HostUnreachableError } from '@shared/core/remote-hosts/reachability';
import { sessionDeletedChannel } from '@shared/core/sessions/sessionEvents';
import { getRemoteAgentLocation } from './agent-location';
import { connectRemoteAgent } from './connect-remote-agent';
diff --git a/dash/apps/switchdash-desktop/src/main/core/locations/location-manager.ts b/dash/apps/switchdash-desktop/src/main/core/locations/location-manager.ts
index a64fb7ad0..c85e2ffdb 100644
--- a/dash/apps/switchdash-desktop/src/main/core/locations/location-manager.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/locations/location-manager.ts
@@ -1,10 +1,10 @@
import { err, ok, type Result } from '@switchdash/shared';
import type { IDisposable } from '@switchdash/shared';
-import { HostUnreachableError } from '@main/core/remote-hosts/host-reachability-service';
import { HookCore, type Hookable } from '@main/lib/hookable';
import { LifecycleMap } from '@main/lib/lifecycle-map';
import { log } from '@main/lib/logger';
import type { Location } from '@shared/core/locations/locations';
+import { HostUnreachableError } from '@shared/core/remote-hosts/reachability';
import { createProvider } from './create-location-provider';
import type { LocationProvider } from './location-provider';
import { TimeoutSignal, withTimeout } from './utils';
diff --git a/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.test.ts b/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.test.ts
index 63384d7f4..1f3d86836 100644
--- a/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.test.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.test.ts
@@ -1,10 +1,9 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import type { HostReachability } from '@shared/core/remote-hosts/reachability';
import {
- HostReachabilityService,
+ type HostReachability,
HostUnreachableError,
- probeDelayFor,
-} from './host-reachability-service';
+} from '@shared/core/remote-hosts/reachability';
+import { HostReachabilityService, probeDelayFor } from './host-reachability-service';
vi.mock('./reachability-store', () => ({
listPersistedReachability: vi.fn(async () => []),
diff --git a/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.ts b/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.ts
index 1689a48c9..971aead58 100644
--- a/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/remote-hosts/host-reachability-service.ts
@@ -1,8 +1,8 @@
import { EventEmitter } from 'node:events';
import {
- hostBlockedReason,
type HostReachability,
type HostReachabilityStatus,
+ HostUnreachableError,
isHostBlocked,
unknownHostReachability,
} from '@shared/core/remote-hosts/reachability';
@@ -60,21 +60,6 @@ type HostEntry = {
inFlight?: Promise<HostReachability>;
};
-/**
- * Raised when host-dependent work is attempted against a host known to be
- * unreachable. Carries the reachability record so callers (and the UI) can
- * render the modeled state rather than re-deriving it from a transport error.
- */
-export class HostUnreachableError extends Error {
- readonly reachability: HostReachability;
-
- constructor(reachability: HostReachability) {
- super(hostBlockedReason(reachability));
- this.name = 'HostUnreachableError';
- this.reachability = reachability;
- }
-}
-
export type HostReachabilityChange = {
previous: HostReachabilityStatus;
current: HostReachability;
diff --git a/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.test.ts b/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.test.ts
index d29df6408..ded02f55f 100644
--- a/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.test.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.test.ts
@@ -23,9 +23,6 @@ vi.mock('@main/core/managed-switch-server/managed-server-status', () => ({
isManagedServerRunning,
managedServerHostBlocked,
}));
-vi.mock('@main/core/remote-hosts/host-reachability-service', () => ({
- HostUnreachableError: class HostUnreachableError extends Error {},
-}));
vi.mock('./auth', () => ({ oidcLogin: vi.fn(), passwordLogin: vi.fn() }));
vi.mock('./gateway-web', () => ({ openAuthenticatedGatewayPage: vi.fn() }));
vi.mock('./gateway-client', () => ({
diff --git a/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.ts b/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.ts
index f5e01161a..32ce7ee1f 100644
--- a/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/switch-servers/controller.ts
@@ -13,9 +13,9 @@ import {
isManagedServerRunning,
managedServerHostBlocked,
} from '@main/core/managed-switch-server/managed-server-status';
-import { HostUnreachableError } from '@main/core/remote-hosts/host-reachability-service';
import { ensureSshConnected } from '@main/core/ssh/connect/connect-agent-ssh';
import type { AgentProviderId } from '@shared/core/providers/agent-provider-registry';
+import { HostUnreachableError } from '@shared/core/remote-hosts/reachability';
import type {
AddressingPolicy,
AddServerParams,
@@ -92,10 +92,10 @@ async function requireServer(serverId: string): Promise<SwitchServer> {
/**
* Resolve a server and refuse to touch its gateway while the host it is managed
- * on is unreachable. Everything the gateway serves — auth config, sign-in,
- * dashboard pages — rides the SSH forward, so a fetch here can only produce a
- * misleading `Could not reach http://localhost:<port>` (CHOO-1780). Fail with
- * the modeled host state instead, which the UI already knows how to render.
+ * on is unreachable (CHOO-1780). `gatewayFetch` enforces the same rule at the
+ * transport, so this is for the paths that reach the gateway some other way —
+ * sign-in and the dashboard window — and for failing before the side effects a
+ * write would otherwise start.
*/
async function requireReachableServer(serverId: string): Promise<SwitchServer> {
const server = await requireServer(serverId);
diff --git a/dash/apps/switchdash-desktop/src/main/core/switch-servers/create-room.test.ts b/dash/apps/switchdash-desktop/src/main/core/switch-servers/create-room.test.ts
index ffd351df9..c4c42468e 100644
--- a/dash/apps/switchdash-desktop/src/main/core/switch-servers/create-room.test.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/switch-servers/create-room.test.ts
@@ -4,6 +4,12 @@ const getSessionCookie = vi.hoisted(() => vi.fn());
const refreshSession = vi.hoisted(() => vi.fn());
const reauthenticateManagedServer = vi.hoisted(() => vi.fn());
+const managedServerHostBlocked = vi.hoisted(() => vi.fn(() => null));
+
+vi.mock('@main/core/managed-switch-server/managed-server-status', () => ({
+ managedServerHostBlocked,
+}));
+
vi.mock('./servers-store', () => ({ getSessionCookie }));
vi.mock('./auth', () => ({ refreshSession, reauthenticateManagedServer }));
diff --git a/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.test.ts b/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.test.ts
index add094dae..dcf4ae85b 100644
--- a/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.test.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.test.ts
@@ -1,9 +1,20 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import {
+ type HostReachability,
+ HostUnreachableError,
+ unknownHostReachability,
+} from '@shared/core/remote-hosts/reachability';
const getSessionCookie = vi.hoisted(() => vi.fn());
const refreshSession = vi.hoisted(() => vi.fn());
const reauthenticateManagedServer = vi.hoisted(() => vi.fn());
+const managedServerHostBlocked = vi.hoisted(() => vi.fn<() => HostReachability | null>(() => null));
+
+vi.mock('@main/core/managed-switch-server/managed-server-status', () => ({
+ managedServerHostBlocked,
+}));
+
vi.mock('./servers-store', () => ({ getSessionCookie }));
vi.mock('./auth', () => ({ refreshSession, reauthenticateManagedServer }));
@@ -197,6 +208,30 @@ describe('gatewayFetch managed-server silent re-auth', () => {
});
});
+describe('gatewayFetch host reachability gate', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.stubGlobal('fetch', fetchMock);
+ });
+
+ afterEach(() => {
+ managedServerHostBlocked.mockReturnValue(null);
+ vi.unstubAllGlobals();
+ });
+
+ it('fails with the host state, without touching the network or the session', async () => {
+ managedServerHostBlocked.mockReturnValue({
+ ...unknownHostReachability('vm'),
+ status: 'unreachable',
+ lastError: 'connect ETIMEDOUT',
+ });
+
+ await expect(fetchMe(MANAGED)).rejects.toBeInstanceOf(HostUnreachableError);
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(getSessionCookie).not.toHaveBeenCalled();
+ });
+});
+
function jsonResponse(body: unknown): Response {
return {
status: 200,
diff --git a/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.ts b/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.ts
index dafe6de3c..1b843ab22 100644
--- a/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/switch-servers/gateway-client.ts
@@ -1,4 +1,6 @@
import type { KnownAgentType } from '@main/core/agents/known-agent-type';
+import { managedServerHostBlocked } from '@main/core/managed-switch-server/managed-server-status';
+import { HostUnreachableError } from '@shared/core/remote-hosts/reachability';
import type {
AddressingPolicy,
BridgeConfigField,
@@ -138,6 +140,14 @@ async function gatewayFetch(
path: string,
options: FetchOptions
): Promise<Response> {
+ // A remote-managed server's gateway is only reachable through the SSH forward.
+ // Once the host is known unreachable the forward is dead, so a fetch can only
+ // hang for its timeout and then report `Could not reach http://localhost:<port>`
+ // — a local address that was never the problem. Fail with the modeled host
+ // state instead, at the one point every gateway call passes through.
+ const blocked = managedServerHostBlocked(server);
+ if (blocked) throw new HostUnreachableError(blocked);
+
const sendOnce = async (cookie: string | null): Promise<Response> => {
const headers: Record<string, string> = { Accept: 'application/json' };
if (options.body !== undefined) {
diff --git a/dash/apps/switchdash-desktop/src/main/lib/rpc-logging.ts b/dash/apps/switchdash-desktop/src/main/lib/rpc-logging.ts
index 573a42a1e..677601b7e 100644
--- a/dash/apps/switchdash-desktop/src/main/lib/rpc-logging.ts
+++ b/dash/apps/switchdash-desktop/src/main/lib/rpc-logging.ts
@@ -1,3 +1,4 @@
+import { HostUnreachableError } from '@shared/core/remote-hosts/reachability';
import type { RPCInvocationWrapper } from '@shared/lib/ipc/rpc';
import { serializeLogValue, type LogContext } from '@shared/logger';
import { runWithLogContext } from './log-context';
@@ -46,7 +47,12 @@ export const withRPCLogContext: RPCInvocationWrapper = (channel, args, invoke) =
});
function logRPCFailure(channel: string, error: unknown) {
- log.error('RPC handler failed', {
+ // An unreachable host is a modeled, displayed state, not a fault of the call:
+ // every view scoped to that host refuses in the same way, so logging each one
+ // as an error buries real failures under a repeating wall of the same fact.
+ // The host's own transition to `unreachable` is logged once, where it belongs.
+ const level = error instanceof HostUnreachableError ? 'debug' : 'error';
+ log[level]('RPC handler failed', {
event: 'rpc_failed',
component: `rpc:${channel}`,
error: serializeLogValue(error),
diff --git a/dash/apps/switchdash-desktop/src/shared/core/remote-hosts/reachability.ts b/dash/apps/switchdash-desktop/src/shared/core/remote-hosts/reachability.ts
index f6bf5edeb..83eecb43d 100644
--- a/dash/apps/switchdash-desktop/src/shared/core/remote-hosts/reachability.ts
+++ b/dash/apps/switchdash-desktop/src/shared/core/remote-hosts/reachability.ts
@@ -68,6 +68,25 @@ export function hostBlockedReason(reachability: HostReachability): string {
}`;
}
+/**
+ * Raised when host-dependent work is attempted against a host known to be
+ * unreachable. Carries the reachability record so callers (and the UI) can
+ * render the modeled state rather than re-deriving it from a transport error.
+ *
+ * It lives with the model rather than with the service that throws it so that
+ * anything holding an error — including the RPC logging chokepoint — can
+ * recognise it without pulling in the service and its persistence.
+ */
+export class HostUnreachableError extends Error {
+ readonly reachability: HostReachability;
+
+ constructor(reachability: HostReachability) {
+ super(hostBlockedReason(reachability));
+ this.name = 'HostUnreachableError';
+ this.reachability = reachability;
+ }
+}
+
export function unknownHostReachability(sshHost: string): HostReachability {
return {
sshHost,