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(ui): the panel's kill must reach the machine it is showing

enese09d07efaa99
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 9ed0b7742..48a435077 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -140,6 +140,7 @@ import {
viewportForRect,
type FocusableNode
} from '../lib/nodeFocus'
+import { planSessionKill } from '../lib/sessionKill'
import { RemoteAccessDialog } from '../components/RemoteAccessDialog'
import { SshProjectDialog } from '../components/SshProjectDialog'
import { SshPassphrasePrompt } from '../components/SshPassphrasePrompt'
@@ -6538,7 +6539,7 @@ export function Canvas() {
// Close (end) a session. tmux sessions are keyed by node id, so destroy works for an
// inactive project's node even though it isn't mounted; then drop it from the store.
const closeSession = useCallback(
- (projectId: string, id: string) => {
+ (projectId: string, id: string, alsoOnConfirm?: () => void) => {
setConfirm({
message: 'End this session? This stops its tmux session.',
confirmLabel: 'End session',
@@ -6553,6 +6554,10 @@ export function Canvas() {
useProjects.getState().removeNode(projectId, id)
void writeDisk()
}
+ // The session-memory panel's remote leg (see `killSessionById`): the local destroy above
+ // cannot reach a HOST's tmux session unless a live client carries `sshRemote`. Runs only
+ // after the user confirmed, which is why it is a callback and not done at the call site.
+ alsoOnConfirm?.()
setConfirm(null)
}
})
@@ -6561,22 +6566,36 @@ export function Canvas() {
)
/**
- * End a session picked from the session-memory panel, which lists tmux sessions rather than
- * canvas nodes — so a row may have no node behind it at all.
+ * End a session picked from the session-memory panel, which lists tmux SESSIONS rather than
+ * canvas nodes — so a row may have no node behind it at all, and (on an SSH project) may not even
+ * be on this machine.
*
- * The owner is resolved HERE, at click time, instead of being taken from the row's `orphan` flag:
- * the panel's rows are a snapshot of the last sweep, and a node created since would otherwise be
- * killed as an orphan — tmux dies, the canvas node stays, pointing at nothing. When there IS an
- * owner this goes through `closeSession`, i.e. the exact path the sessions sidebar and the node's
- * own × use (confirm → destroy → drop the node), so the panel can never invent a third one.
+ * Both halves of the plan are the pure `planSessionKill`:
+ *
+ * - **Who owns it** is resolved HERE, at click time, rather than taken from the row's `orphan`
+ * flag: the rows are a snapshot of the last sweep, and a node created since would otherwise be
+ * killed as an orphan. With an owner this goes through `closeSession` — the exact path the
+ * sessions sidebar and the node's own × use — so the panel never invents a third one.
+ * - **Which machine** is the ACTIVE project's, because that is the machine the panel is showing.
+ * `transport.destroy` reaches a REMOTE session only through a live client carrying `sshRemote`,
+ * which an orphan and an unmounted node both lack — so on an SSH scope the kill would have
+ * touched only the local socket while the host's `nt-<id>` kept running, after a confirm that
+ * said otherwise. `sshProject.killSessions` runs `tmux kill-session` over that project's own
+ * ControlMaster and needs no live session; it is best-effort per id, so running it for the
+ * mounted case too (where `destroy` already ended it) is a harmless miss.
*/
const killSessionById = useCallback(
(nodeId: string, orphan: boolean) => {
- const owner = useProjects
- .getState()
- .projects.find((p) => p.nodes.some((n) => n.id === nodeId))
- if (owner) {
- closeSession(owner.id, nodeId)
+ const store = useProjects.getState()
+ const plan = planSessionKill(nodeId, store.projects, store.activeProjectId)
+ const remoteKill = plan.remoteProjectId
+ ? () =>
+ void window.nodeTerminal.sshProject
+ .killSessions(plan.remoteProjectId!, [nodeId])
+ .catch(() => {})
+ : undefined
+ if (plan.ownerProjectId) {
+ closeSession(plan.ownerProjectId, nodeId, remoteKill)
return
}
setConfirm({
@@ -6587,6 +6606,7 @@ export function Canvas() {
danger: true,
onConfirm: () => {
transport.destroy(nodeId)
+ remoteKill?.()
// Nothing else to clean up: with no node anywhere, there is no canvas entry to remove and
// no parked terminal to dispose. Persisted agent status is dropped anyway, since a
// session id can outlive the node it belonged to.
diff --git a/src/renderer/components/SessionMemoryPanel.test.tsx b/src/renderer/components/SessionMemoryPanel.test.tsx
index 74fb18ec2..d7745d6ad 100644
--- a/src/renderer/components/SessionMemoryPanel.test.tsx
+++ b/src/renderer/components/SessionMemoryPanel.test.tsx
@@ -241,6 +241,34 @@ describe('SessionMemoryPanel', () => {
expect(text()).not.toContain('No sessions')
expect(text()).not.toContain('Could not measure')
expect(text()).toContain('Measuring')
+ // …and the header and footer have to stay silent too. `ok` is still TRUE in this state, so a
+ // total or a count gated on `ok` instead of on "did we measure THIS machine" prints `0 B` /
+ // `0 sessions` beside "Measuring…" — the same conflation, one state along.
+ expect(host.querySelector('.sessmem-panel__total')).toBeNull()
+ expect(text()).not.toContain('0 sessions')
+ expect(text()).not.toMatch(/\d/)
+ })
+
+ it('disables the refresh and spins it while a sweep is in flight', () => {
+ // A remote sweep is an ssh exec plus a `ps` on someone else's machine. Without this the user
+ // can queue several by clicking, and nothing on screen says one is already running.
+ mount({ loading: true })
+ const refresh = host.querySelector<HTMLButtonElement>('.sessmem-panel__refresh')!
+ expect(refresh.disabled).toBe(true)
+ expect(refresh.classList.contains('spin')).toBe(true)
+ act(() => refresh.click())
+ expect(refreshFull).toHaveBeenCalledTimes(1) // the mount sweep, and nothing the click added
+ })
+
+ it('refuses to sweep with no active project — never through the api fallback', () => {
+ // Unreachable from the pill today (it renders nothing without an active project), but this is
+ // the one call that could fall through to the store's `activeSessionApi()`, which is the single
+ // path able to address the wrong machine. The mount effect always guarded it; the ⟳ did not.
+ useProjects.setState({ projects: [], activeProjectId: '' })
+ mount()
+ expect(refreshFull).not.toHaveBeenCalled()
+ act(() => host.querySelector<HTMLButtonElement>('.sessmem-panel__refresh')!.click())
+ expect(refreshFull).not.toHaveBeenCalled()
})
it('tells a relay tab it is unsupported here, not that the measurement failed', () => {
diff --git a/src/renderer/components/SessionMemoryPanel.tsx b/src/renderer/components/SessionMemoryPanel.tsx
index 98e19b410..f2f4a8cfc 100644
--- a/src/renderer/components/SessionMemoryPanel.tsx
+++ b/src/renderer/components/SessionMemoryPanel.tsx
@@ -9,7 +9,7 @@
// never enters the agent-status map at all.
// 3. Every row is rendered. A cap would have to announce itself.
-import { useEffect, useMemo } from 'react'
+import { useCallback, useEffect, useMemo } from 'react'
import type { AgentState } from '@shared/agents/normalize'
import { formatBytes } from '@shared/fsLimits'
import { useAgentStatus } from '../state/agentStatus'
@@ -92,15 +92,17 @@ export function SessionMemoryPanel({
// The sweep runs on OPEN (the panel is unmounted while closed) and on ⟳ — never on a timer, and
// never from the pill: it walks the whole process table, and on an SSH scope that is an ssh exec
// plus a `ps` of somebody else's machine. The project id is explicit, because the store's
- // `activeSessionApi()` fallback is the one path that can address the wrong machine.
+ // `activeSessionApi()` fallback is the one path that can address the wrong machine — so BOTH
+ // callers go through this one guarded function instead of each repeating the condition.
//
// This component must NEVER call `startHostPoll` / `stopHostPoll`: the store's timer and its
// active-scope stamp are module singletons owned by the pill, and a `stopHostPoll` on unmount
// would clear the pill's interval with nothing left to restart it.
- useEffect(() => {
+ const sweep = useCallback(() => {
if (relay || !activeProjectId) return
void refreshFull(scopeKey, activeProjectId)
}, [relay, scopeKey, activeProjectId, refreshFull])
+ useEffect(() => sweep(), [sweep])
// Have we got an answer for THIS machine yet? A `loadedScope` from the machine we just left says
// nothing about this one, so it is compared, not merely checked for existence.
@@ -199,7 +201,7 @@ export function SessionMemoryPanel({
title="Re-measure"
aria-label="Re-measure"
disabled={loading}
- onClick={() => void refreshFull(scopeKey, activeProjectId)}
+ onClick={sweep}
>
⟳
</button>
diff --git a/src/renderer/lib/sessionKill.test.ts b/src/renderer/lib/sessionKill.test.ts
new file mode 100644
index 000000000..6490f2c37
--- /dev/null
+++ b/src/renderer/lib/sessionKill.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from 'vitest'
+import type { Project } from '@shared/types'
+import { planSessionKill } from './sessionKill'
+
+const project = (id: string, nodeIds: string[], over: Partial<Project> = {}): Project =>
+ ({
+ id,
+ name: id,
+ color: '#fff',
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: nodeIds.map((n) => ({ id: n, kind: 'terminal', title: n, position: { x: 0, y: 0 } })),
+ ...over
+ }) as unknown as Project
+
+const ssh = (id: string, nodeIds: string[], over: Partial<Project> = {}): Project =>
+ project(id, nodeIds, {
+ ssh: { server: { host: 'box', user: 'enes', port: 22 } as never, remoteCwd: '/srv' },
+ ...over
+ })
+
+describe('planSessionKill', () => {
+ it('finds the owner across every project, closed ones included', () => {
+ const plan = planSessionKill('c', [project('p1', ['a']), project('p2', ['c'], { closed: true })], 'p1')
+ expect(plan.ownerProjectId).toBe('p2')
+ })
+
+ it('reports no owner for an orphan', () => {
+ expect(planSessionKill('ghost', [project('p1', ['a'])], 'p1').ownerProjectId).toBeNull()
+ })
+
+ it('kills an ORPHAN on the host when the scope is an SSH project', () => {
+ // The whole point. `transport.destroy` reaches a remote session only through a LIVE client
+ // carrying `sshRemote`; an orphan has no node and therefore no client, so destroy alone would
+ // touch the local socket and leave the host's `nt-ghost` running — after a confirm that said
+ // otherwise.
+ const plan = planSessionKill('ghost', [ssh('s1', ['a'])], 's1')
+ expect(plan).toEqual({ ownerProjectId: null, remoteProjectId: 's1' })
+ })
+
+ it('kills a NON-ACTIVE project´s session on the host too', () => {
+ // Same trap one step along: an unmounted node has no live client either. This is the case the
+ // sessions sidebar has always got wrong; it never listed another machine's sessions, and this
+ // panel's whole purpose is to.
+ const plan = planSessionKill('b', [ssh('s1', ['a']), ssh('s2', ['b'], { closed: true })], 's1')
+ expect(plan).toEqual({ ownerProjectId: 's2', remoteProjectId: 's1' })
+ })
+
+ it('routes the remote kill through the ACTIVE project, never the owner', () => {
+ // The panel shows ONE machine — the active project's — and the sweep that produced these rows
+ // ran over that project's master. The owner's own connection may be down, or on another host.
+ expect(planSessionKill('b', [ssh('s1', ['a']), ssh('s2', ['b'])], 's1').remoteProjectId).toBe('s1')
+ })
+
+ it('never reaches for a host when the scope is this machine', () => {
+ // A local sweep lists LOCAL sessions. A local `nt-b` whose node belongs to an SSH project is
+ // the stranded local fallback `requireRemote` exists to prevent — killing it on the host would
+ // leave it running here.
+ const plan = planSessionKill('b', [project('p1', ['a']), ssh('s2', ['b'])], 'p1')
+ expect(plan).toEqual({ ownerProjectId: 's2', remoteProjectId: null })
+ })
+
+ it('survives an unknown active project', () => {
+ expect(planSessionKill('a', [project('p1', ['a'])], '').remoteProjectId).toBeNull()
+ })
+})
diff --git a/src/renderer/lib/sessionKill.ts b/src/renderer/lib/sessionKill.ts
new file mode 100644
index 000000000..572ebb3f5
--- /dev/null
+++ b/src/renderer/lib/sessionKill.ts
@@ -0,0 +1,50 @@
+// Where a session picked from the SESSION-MEMORY PANEL has to be killed.
+//
+// The panel is the first surface in the app that lists sessions on a machine other than the one
+// the renderer runs on: an SSH project's rows come from `ps` on the HOST. That makes the ordinary
+// kill path a lie in exactly the cases the panel adds.
+//
+// `transport.destroy(nodeId)` reaches a REMOTE tmux session only through a LIVE local client that
+// carries `sshRemote` (see `PtyManager.endSession`) — i.e. only for a node that is currently
+// mounted. An ORPHAN row has no node at all, and a row owned by a non-active project has no live
+// client either, so for both of those `destroy` touches only the local socket while the remote
+// `nt-<id>` keeps running. The confirm would promise a kill it cannot perform, and the row would
+// come back on the next refresh with no explanation.
+//
+// So the plan is decided by the SCOPE, not by the row: **the panel kills on the machine it is
+// showing you.** On an SSH scope every row is a session on that host, so the kill additionally runs
+// `tmux kill-session` over that project's own ControlMaster (`sshProject.killSessions`), which
+// needs no live session. It is idempotent — a session already ended by `destroy` (the mounted case)
+// is a best-effort miss on the host — so no case analysis is needed at the call site.
+
+import type { Project } from '@shared/types'
+
+export interface SessionKillPlan {
+ /**
+ * The project whose canvas node must go too, or `null` when no project owns this session (an
+ * orphan). Resolved against EVERY project, closed ones included — `closeProject` keeps its nodes.
+ */
+ ownerProjectId: string | null
+ /**
+ * The project whose ControlMaster must run the remote `tmux kill-session`, or `null` when the
+ * scope is this machine. This is the ACTIVE project, never the owner: the panel shows one
+ * machine at a time, and that machine is the active project's.
+ */
+ remoteProjectId: string | null
+}
+
+export function planSessionKill(
+ nodeId: string,
+ projects: readonly Project[],
+ activeProjectId: string
+): SessionKillPlan {
+ const owner = projects.find((p) => (p.nodes ?? []).some((n) => n.id === nodeId))
+ const active = projects.find((p) => p.id === activeProjectId)
+ return {
+ ownerProjectId: owner?.id ?? null,
+ // `active.ssh`, not `owner.ssh`: a local scope lists LOCAL sessions, and a local `nt-<id>`
+ // whose node belongs to an SSH project is precisely the stranded local fallback that
+ // `requireRemote` exists to prevent — killing it on the host would leave it running here.
+ remoteProjectId: active?.ssh ? active.id : null
+ }
+}