Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(ssh): a cancelled attempt must not defuse the key-forget it armed

parsa2221a6547e562e8
diff --git a/src/main/remote-ssh/ssh-project.test.ts b/src/main/remote-ssh/ssh-project.test.ts
index 6982b5759..24b1b3e6d 100644
--- a/src/main/remote-ssh/ssh-project.test.ts
+++ b/src/main/remote-ssh/ssh-project.test.ts
@@ -1,9 +1,10 @@
-import { promises as fs } from 'fs'
+import { promises as fs, writeFileSync } from 'fs'
import os from 'os'
import path from 'path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { SshProjectManager, lastSshErrorLine } from './ssh-project'
import { AskpassServer } from './ssh-askpass'
+import { AppSshAgent } from './ssh-agent'
import { controlPathFor } from '../../core/remote-ssh/control-master'
const conn = { host: 'h', user: 'u' }
@@ -1340,6 +1341,50 @@ describe('SshProjectManager', () => {
expect(onStatus.mock.calls.map((c) => c[0].status)).not.toContain('connected')
})
+ it('a cancel BEFORE ensureAgent cannot defuse the idle key-forget its own disconnect armed', async () => {
+ // The ordering the previous test does not cover, wired like production: disconnect lands
+ // while the attempt is still in fs.mkdir, BEFORE ensureAgent was ever invoked. onIdle arms
+ // the agent's scheduled stop; the doomed attempt then resumes, and start()'s first act is
+ // cancelScheduledStop() - without the pre-ensureAgent ticket check it would silently disarm
+ // the key-forget it triggered, and the unlocked key would survive to the 12h backstop.
+ const sockDir = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-agent-'))
+ const sockPath = path.join(sockDir, 'a.sock')
+ const agent = new AppSshAgent((args) => {
+ void args
+ writeFileSync(sockPath, '') // "bind" instantly so start() resolves without polling out
+ return { kill: vi.fn(), on: vi.fn() }
+ }, sockPath)
+ await agent.start() // the agent already holds a key from an earlier connection
+ expect(agent.isRunning()).toBe(true)
+
+ let releaseMkdir!: () => void
+ const mkdirGate = new Promise<void>((r) => {
+ releaseMkdir = r
+ })
+ vi.spyOn(fs, 'mkdir').mockImplementation(() => mkdirGate as Promise<undefined>)
+ vi.spyOn(fs, 'stat').mockRejectedValue(Object.assign(new Error('ENOENT'), { code: 'ENOENT' }))
+ const mgr = new SshProjectManager({
+ userDataDir: '/ud',
+ spawnMaster: vi.fn(() => ({ kill: vi.fn(), on: vi.fn() })),
+ run: vi.fn(async () => ({ code: 0, stdout: '' })),
+ runScp: vi.fn(async () => ({ code: 0 })),
+ getHook: () => ({ port: 1, token: 't', version: '1' }),
+ onStatus: vi.fn(),
+ ensureAgent: () => agent.start(),
+ onIdle: () => agent.scheduleStop(50)
+ })
+ const attempt = mgr.connect('p1', { host: 'h', user: 'u' })
+ attempt.catch(() => {})
+ await new Promise((r) => setImmediate(r)) // park inside fs.mkdir, before ensureAgent
+ await mgr.disconnect('p1', { final: true }) // arms the 50ms key-forget
+ releaseMkdir()
+ await expect(attempt).rejects.toThrow(/cancelled/)
+ await new Promise((r) => setTimeout(r, 200))
+ expect(agent.isRunning()).toBe(false) // the scheduled stop survived the doomed attempt
+ agent.stop()
+ await fs.rm(sockDir, { recursive: true, force: true }).catch(() => {})
+ })
+
it('reports idle ONLY on a user-facing disconnect that leaves nothing connected', async () => {
// `onIdle` is what forgets the unlocked key. Internal teardowns empty `conns` routinely (an
// endpoint edit, a failed connect, the watchdog dropping a stale master), and treating those
diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.ts
index e68723799..8ef2a276a 100644
--- a/src/main/remote-ssh/ssh-project.ts
+++ b/src/main/remote-ssh/ssh-project.ts
@@ -462,6 +462,14 @@ export class SshProjectManager {
}
} else {
if (leftover) await fs.rm(controlPath, { force: true }).catch(() => {})
+ // Ticket check BEFORE the agent is consulted, not only before registration below: start()'s
+ // first act is cancelScheduledStop(), so a doomed attempt reaching ensureAgent AFTER its own
+ // disconnect already fired onIdle would silently disarm the idle key-forget it triggered,
+ // and nothing would ever re-arm it - the unlocked key then survives to the 12h backstop.
+ // No await sits between this check and start()'s cancel, so no disconnect can interleave.
+ if (ticket && this.inFlight.get(projectId)?.ticket !== ticket) {
+ throw new Error('SSH connect cancelled')
+ }
// The app-private agent has to be listening BEFORE ssh authenticates, or `AddKeysToAgent`
// stores the unlocked key nowhere and every connect this run prompts again. Never fatal.
await this.r.ensureAgent?.().catch(() => {})