Skip to content

The agent that grows with you

fix(desktop): a removed gateway stays removed for its terminal tabs

Tekniume8639fd4312e
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts
index ed834882054..63a22e4462c 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts
@@ -5,7 +5,7 @@ import type { DesktopConnectionsRegistry, DesktopRegistryConnection } from '@/gl
import { $activeConnectionId, $connectionsRegistry } from '@/store/connections'
import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
-import { bindTerminalDefaultGateway, type TerminalEntry } from './terminals'
+import { bindTerminalDefaultGateway, forgetRemovedTerminalGateways, type TerminalEntry } from './terminals'
/** The gateway a terminal tab was opened on. Only This device and SSH gateways
* carry a shell; the SSH tunnel is keyed by (connection, profile). */
@@ -51,6 +51,15 @@ export const runsOverSsh = (gateway: TerminalGateway | undefined): boolean =>
// cwd only on the session's host.
bindTerminalDefaultGateway($defaultTerminalGateway)
+// A gateway that leaves the registry is gone for its tabs for good: ids are
+// reusable label slugs, so a later gateway with the same name is another
+// machine. An edit keeps the id registered and its tabs keep reconnecting.
+$connectionsRegistry.subscribe(registry => {
+ if (registry) {
+ forgetRemovedTerminalGateways(new Set(registry.connections.map(connection => connection.id)))
+ }
+})
+
export const terminalGatewayOf = (term: TerminalEntry | undefined): TerminalGateway | undefined =>
term?.connectionId ? { connectionId: term.connectionId, ...(term.profile ? { profile: term.profile } : {}) } : undefined
@@ -69,7 +78,15 @@ export const $localGateway = computed(
export type TerminalHost = { connection: Pick<DesktopRegistryConnection, 'id' | 'kind' | 'label'> } | 'removed' | null
export function terminalHost(term: TerminalEntry, registry: DesktopConnectionsRegistry | null): TerminalHost {
- if (term.kind !== 'user' || !term.connectionId || !registry) {
+ if (term.kind !== 'user' || !term.connectionId) {
+ return null
+ }
+
+ if (term.gatewayRemoved) {
+ return 'removed'
+ }
+
+ if (!registry) {
return null
}
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts
index de4f9b4e6ed..aba0ce870ae 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts
@@ -128,3 +128,48 @@ describe('a terminal tab owns the gateway it was opened on', () => {
expect(stores.$activeTerminalId.get()).toBe(local)
})
})
+
+describe('a removed gateway stays removed for its tabs', () => {
+ it('deleting NAS and adding another machine named NAS never reconnects the old tab, across relaunch', async () => {
+ let stores = await loadStores()
+ stores.$connectionsRegistry.set(registryWith({ host: 'old-nas.example', id: 'nas' }))
+ const id = stores.createTerminal('/repo', { connectionId: 'nas', profile: 'default' })
+
+ stores.$connectionsRegistry.set(registryWith())
+ stores.$connectionsRegistry.set(registryWith({ host: 'different-prod.example', id: 'nas' }))
+ const tab = () => stores.$terminals.get().find(term => term.id === id)!
+ expect(stores.terminalHost(tab(), stores.$connectionsRegistry.get())).toBe('removed')
+
+ stores = await loadStores() // relaunch
+ stores.$connectionsRegistry.set(registryWith({ host: 'different-prod.example', id: 'nas' }))
+ expect(stores.terminalHost(tab(), stores.$connectionsRegistry.get())).toBe('removed')
+
+ const start = vi.fn().mockResolvedValue(session('nas'))
+ const { controller, write } = controllerFor(stores, id, start)
+ controller.start()
+ await waitFor(() => expect(write).toHaveBeenCalled())
+ controller.release()
+
+ expect(start).not.toHaveBeenCalled()
+ })
+
+ it('editing NAS in place keeps its tabs reconnecting to it, across relaunch', async () => {
+ let stores = await loadStores()
+ stores.$connectionsRegistry.set(registryWith({ host: 'old-nas.example', id: 'nas' }))
+ const id = stores.createTerminal('/repo', { connectionId: 'nas', profile: 'default' })
+ stores.$connectionsRegistry.set(registryWith({ host: 'new-nas.example', id: 'nas' }))
+
+ stores = await loadStores() // relaunch
+ stores.$connectionsRegistry.set(registryWith({ host: 'new-nas.example', id: 'nas' }))
+ const tab = stores.$terminals.get().find(term => term.id === id)!
+ expect(stores.terminalHost(tab, stores.$connectionsRegistry.get())).toMatchObject({ connection: { id: 'nas' } })
+
+ const start = vi.fn().mockResolvedValue(session('nas'))
+ const { controller } = controllerFor(stores, id, start)
+ controller.start()
+ await waitFor(() => expect(start).toHaveBeenCalledTimes(1))
+ controller.release()
+
+ expect(start.mock.calls[0]![0]).toMatchObject({ connectionId: 'nas', profile: 'default' })
+ })
+})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts
index 14bd4afeb2e..d033ab65e7d 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts
@@ -146,6 +146,12 @@ export function createSessionAttemptController(
// and records it BEFORE the start, so a failed start retries on it too.
const tab = $terminals.get().find(entry => entry.id === id)
+ if (tab?.gatewayRemoved) {
+ showUnavailable('missing')
+
+ return
+ }
+
const gateway = terminalGatewayOf(tab) ?? defaultTerminalGateway()
if (gateway) {
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
index 66c0f5af335..fcd894cc793 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
@@ -141,11 +141,16 @@ export interface TerminalEntry {
connectionId?: string
/** Gateway profile the SSH tunnel is keyed by (with `connectionId`). */
profile?: string
+ /** `connectionId` left the registry. Ids are reusable label slugs, so a
+ * gateway added later under the same name is another machine: this tab
+ * never reconnects, even after relaunch. */
+ gatewayRemoved?: boolean
}
interface PersistedTerminalEntry {
auto: boolean
connectionId?: string
+ gatewayRemoved?: boolean
cwd: string
id: string
restoreCwd?: string
@@ -194,6 +199,7 @@ function sanitizePersistedTerminal(value: unknown): PersistedTerminalEntry | nul
return {
auto: typeof record.auto === 'boolean' ? record.auto : true,
...persistedGateway(record),
+ ...(record.gatewayRemoved === true ? { gatewayRemoved: true } : {}),
cwd,
id,
...(restoreCwd ? { restoreCwd } : {}),
@@ -244,6 +250,7 @@ function persistTerminals(list: readonly TerminalEntry[], activeTerminalId: null
.map(term => ({
auto: term.auto,
...(term.connectionId ? { connectionId: term.connectionId } : {}),
+ ...(term.gatewayRemoved ? { gatewayRemoved: true } : {}),
cwd: term.cwd,
id: term.id,
...(term.profile ? { profile: term.profile } : {}),
@@ -310,6 +317,18 @@ export function adoptTerminalGateway(id: string, gateway: TerminalGateway): void
)
}
+/** Mark tabs whose gateway is no longer registered. Sticky: see `gatewayRemoved`. */
+export function forgetRemovedTerminalGateways(registered: ReadonlySet<string>): void {
+ const list = $terminals.get()
+
+ const gone = (term: TerminalEntry) =>
+ term.kind === 'user' && Boolean(term.connectionId) && !term.gatewayRemoved && !registered.has(term.connectionId!)
+
+ if (list.some(gone)) {
+ $terminals.set(list.map(term => (gone(term) ? { ...term, gatewayRemoved: true } : term)))
+ }
+}
+
/** True once user tabs run on more than one host: only then do This device
* tabs need a label too. */
export const $terminalHostsMixed = computed(
@@ -422,6 +441,7 @@ function followSessionCwd(cwd: string) {
const linked = (term: TerminalEntry | undefined) =>
term?.kind === 'user' &&
terminalCwd(term) === target &&
+ !term.gatewayRemoved &&
(!host || !term.connectionId || term.connectionId === host)
const list = $terminals.get()