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): attach remote terminals on sight, and never fail silently

amaudruz5bfe2ba7fa19
diff --git a/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.test.ts b/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.test.ts
index 67d785415..c615ee64b 100644
--- a/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.test.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.test.ts
@@ -798,6 +798,31 @@ describe('SshAgentRuntime', () => {
expect(openSsh2Pty).toHaveBeenCalledTimes(2);
});
+ // A runtime joins the attachment pool at provision time but learns its session
+ // only from `ensureAttachable`. Returning quietly here is indistinguishable to
+ // the pool from a successful attach: it logs `remote_attach`, reports the
+ // session attached, and the user stares at an empty pane with nothing in the
+ // log. Observed on a real host — every click logged an attach and
+ // `attachedCount` never left 0.
+ it('fails loudly when asked to attach before it knows its session', async () => {
+ mockSpawn([]);
+ const provider = sshProvider({ tmux: true });
+
+ await expect(provider.attach()).rejects.toThrow(/never made attachable/);
+ expect(openSsh2Pty).not.toHaveBeenCalled();
+ });
+
+ it('attaches once ensureAttachable has told it which session it serves', async () => {
+ mockSpawn([]);
+ const provider = sshProvider({ tmux: true });
+
+ await provider.ensureAttachable(session());
+ await provider.attach();
+
+ expect(openSsh2Pty).toHaveBeenCalledTimes(1);
+ expect(provider.isAttached()).toBe(true);
+ });
+
it('does not report the agent as exited when a session is evicted', async () => {
// The sidebar derives status from hook events; a deliberate detach must not
// look like the agent stopping, or every eviction would flip it to idle.
diff --git a/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.ts b/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.ts
index 279d17a1a..abd233da5 100644
--- a/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/agent-runtime/impl/ssh-agent-runtime.ts
@@ -223,9 +223,18 @@ export class SshAgentRuntime implements AgentRuntimeProvider, AttachableRuntime
* in flight, so repeated calls remain safe.
*/
async attach(): Promise<void> {
- if (!this.known || this.pty) return;
+ if (this.pty) return;
+ // Loud, because the pool has no way to see the difference: a runtime that
+ // silently declines looks exactly like one that attached, and the terminal
+ // stays blank with nothing in the log to explain it. A runtime reaches the
+ // pool at provision time but only learns its session from `ensureAttachable`,
+ // so this fires when provisioning skipped that step.
+ if (!this.known || !this.session) {
+ throw new Error(
+ `SshAgentRuntime: cannot attach session ${this.sessionId} — the runtime was never made attachable (known=${this.known}, session=${this.session !== null})`
+ );
+ }
const session = this.session;
- if (!session) return;
return this.withLogScope(() => this.attachInternal(session));
}
diff --git a/dash/apps/switchdash-desktop/src/main/core/sessions/session-service.ts b/dash/apps/switchdash-desktop/src/main/core/sessions/session-service.ts
index 6f79d9794..b93e93174 100644
--- a/dash/apps/switchdash-desktop/src/main/core/sessions/session-service.ts
+++ b/dash/apps/switchdash-desktop/src/main/core/sessions/session-service.ts
@@ -22,6 +22,7 @@ import type {
import { archiveSession } from './operations/archiveSession';
import { createSession } from './operations/createSession';
import { deleteSession } from './operations/deleteSession';
+import { ensureSessionAttachable } from './operations/ensureSessionAttachable';
import { getSessions } from './operations/getSessions';
import { renameSession } from './operations/renameSession';
import { restoreSession } from './operations/restoreSession';
@@ -77,6 +78,27 @@ export class SessionService implements Hookable<SessionLifecycleHooks> {
* already live. Fires the `session:runtime-ready` hook and
* emits the `session:provisioned` IPC event on success.
*/
+ /**
+ * Bring a remote session's sidecar and relay up as part of provisioning.
+ *
+ * A remote runtime joins the attachment pool the moment it is registered, but
+ * it learns which session it serves only from `ensureAttachable`. Without this
+ * the pool holds a runtime that refuses every attach, so opening the session
+ * shows an empty terminal for good. Local sessions have no sidecar and return
+ * false. Best-effort: an unreachable host must not fail provisioning, and the
+ * next attach reports the real error.
+ */
+ private async _makeAttachable(sessionId: string): Promise<void> {
+ try {
+ await ensureSessionAttachable(sessionId);
+ } catch (error) {
+ log.warn('SessionService: could not make session attachable', {
+ sessionId,
+ error: String(error),
+ });
+ }
+ }
+
async provisionSession(
sessionId: string
): Promise<Result<ProvisionResult, TeardownSessionError>> {
@@ -90,6 +112,7 @@ export class SessionService implements Hookable<SessionLifecycleHooks> {
path: location.dir,
locationId: sessionRuntimeManager.getLocationId(sessionId) ?? location.locationId,
};
+ await this._makeAttachable(sessionId);
this._hooks.callHookBackground('session:runtime-ready', sessionId, provisionResult);
events.emit(sessionProvisionedChannel, { sessionId, ...provisionResult });
return ok(provisionResult);
@@ -97,6 +120,7 @@ export class SessionService implements Hookable<SessionLifecycleHooks> {
const built = await provisionSessionRuntime(session.session, location);
await this._registerAndPersist(sessionId, built);
+ await this._makeAttachable(sessionId);
const provisionResult: ProvisionResult = { path: built.path, locationId: built.locationId };
this._hooks.callHookBackground('session:runtime-ready', sessionId, provisionResult);
diff --git a/dash/apps/switchdash-desktop/src/renderer/features/sessions/session-attachment-state.tsx b/dash/apps/switchdash-desktop/src/renderer/features/sessions/session-attachment-state.tsx
index 7f984fac5..f93ab1960 100644
--- a/dash/apps/switchdash-desktop/src/renderer/features/sessions/session-attachment-state.tsx
+++ b/dash/apps/switchdash-desktop/src/renderer/features/sessions/session-attachment-state.tsx
@@ -1,16 +1,22 @@
-import { Loader2, MonitorPlay, RefreshCw, TriangleAlert } from 'lucide-react';
+import { Loader2, RefreshCw, TriangleAlert } from 'lucide-react';
+import { useCallback, useEffect } from 'react';
import { rpc } from '@renderer/lib/ipc';
import { log } from '@renderer/utils/logger';
import type { AttachState } from './stores/session-agent-store';
/**
- * What a remote session shows instead of a terminal.
+ * What a remote session shows on the way to its terminal.
*
* Only so many sessions per host keep a terminal open at once — they share one
- * SSH connection, and past a handful the slower tunnels stop answering. A
- * detached session is not stopped: its agent is running on the VM and its
- * status in the sidebar stays live. The copy has to say that, or an empty pane
- * reads as a broken session.
+ * SSH connection, and past a handful the slower tunnels stop answering. Which
+ * ones those are is not the user's problem: opening a session attaches it and
+ * evicts the least-recently-viewed one on that host, so `detached` is a state
+ * this pane passes through, never one it settles in. It therefore asks for the
+ * attach itself rather than offering a button — a session you are looking at
+ * and cannot see is a bug, not a choice to present.
+ *
+ * A detached session is not a stopped one: its agent keeps running on the VM
+ * and its status in the sidebar stays live.
*/
export function SessionAttachmentState({
state,
@@ -21,13 +27,21 @@ export function SessionAttachmentState({
sessionId: string;
host: string | null;
}) {
- const attach = () => {
+ const attach = useCallback(() => {
void rpc.sessions.attachSession(sessionId).catch((error: unknown) => {
log.warn('SessionAttachmentState: attach request failed', { sessionId, error });
});
- };
+ }, [sessionId]);
+
+ // Focus reporting normally attaches before this pane renders. This covers the
+ // orders it cannot: a session evicted while its view stays mounted, and a
+ // reconnect that replays fewer sessions than are open.
+ const shouldSelfAttach = state === 'detached';
+ useEffect(() => {
+ if (shouldSelfAttach) attach();
+ }, [shouldSelfAttach, attach]);
- if (state === 'attaching') {
+ if (state === 'attaching' || state === 'detached') {
return (
<Centered>
<Loader2 className="h-5 w-5 animate-spin text-foreground-muted" />
@@ -53,24 +67,7 @@ export function SessionAttachmentState({
);
}
- return (
- <Centered>
- <MonitorPlay className="h-6 w-6 text-foreground-muted" />
- <p className="font-mono text-sm font-medium">Terminal not attached</p>
- <p className="font-mono text-xs text-foreground-muted">
- This agent is running{host ? ` on ${host}` : ''} and its status stays live. Attach to watch
- it or type to it.
- </p>
- <ActionButton
- onClick={attach}
- icon={<MonitorPlay className="h-3 w-3" />}
- label="Attach terminal"
- />
- <p className="font-mono text-[11px] text-foreground-muted">
- Scrollback from before attaching is not restored.
- </p>
- </Centered>
- );
+ return null;
}
function Centered({ children }: { children: React.ReactNode }) {