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(agents): thread pane ownership through the kanban modal + pin the fresh-gate

Claude Codef2a990561678
diff --git a/src/core/agents/pane-ownership.test.ts b/src/core/agents/pane-ownership.test.ts
index 93944fe46..d21326ff0 100644
--- a/src/core/agents/pane-ownership.test.ts
+++ b/src/core/agents/pane-ownership.test.ts
@@ -3,7 +3,8 @@ import {
recordFreshSpawnOwner,
paneOwnerProject,
forgetPaneOwner,
- resetPaneOwnershipForTests
+ resetPaneOwnershipForTests,
+ shouldRecordOwnership
} from './pane-ownership'
beforeEach(() => resetPaneOwnershipForTests())
@@ -47,3 +48,21 @@ describe('pane-ownership ledger', () => {
expect(paneOwnerProject('n2')).toBe('proj-a')
})
})
+
+describe('shouldRecordOwnership — the load-bearing fresh-gate, pure', () => {
+ it('records ONLY a genuine fresh spawn with a persistKey and a known owner', () => {
+ expect(shouldRecordOwnership(true, 'n1', 'proj-a')).toBe(true)
+ })
+ it('an ATTACH (fresh=false) never records — the confused-deputy hole this gate closes', () => {
+ // Recording on attach would let a project claim a pane it merely re-opened after a restart.
+ expect(shouldRecordOwnership(false, 'n1', 'proj-a')).toBe(false)
+ })
+ it('a spawn with no persistKey (a plain, non-persistent pty) does not record', () => {
+ expect(shouldRecordOwnership(true, undefined, 'proj-a')).toBe(false)
+ expect(shouldRecordOwnership(true, '', 'proj-a')).toBe(false)
+ })
+ it('a spawn with no known owner (old caller / relay) does not record — stays unproven, fails closed', () => {
+ expect(shouldRecordOwnership(true, 'n1', undefined)).toBe(false)
+ expect(shouldRecordOwnership(true, 'n1', '')).toBe(false)
+ })
+})
diff --git a/src/core/agents/pane-ownership.ts b/src/core/agents/pane-ownership.ts
index b8b4f975b..6f02db67d 100644
--- a/src/core/agents/pane-ownership.ts
+++ b/src/core/agents/pane-ownership.ts
@@ -41,6 +41,22 @@
/** nodeId → owning projectId (machine-local entry id), for panes freshly spawned THIS run. */
const owners = new Map<string, string>()
+/**
+ * THE FRESH-GATE, pure and pinned: may this create() record pane ownership? True ONLY for a
+ * genuine fresh spawn (`fresh === true`) of a persistent node (`persistKey`) whose owner is known
+ * (`ownerProjectId`). This is the load-bearing security property — recording on an ATTACH
+ * (`fresh === false`) would let a project claim a pane it merely re-opened after a restart, which
+ * is exactly the confused deputy this ledger closes. Extracted so the gate has its own unit test
+ * (`pane-ownership.test.ts`) rather than living only inside `spawnNew`.
+ */
+export function shouldRecordOwnership(
+ fresh: boolean,
+ persistKey: string | undefined,
+ ownerProjectId: string | undefined
+): boolean {
+ return fresh === true && !!persistKey && !!ownerProjectId
+}
+
/**
* Record the owner of a node whose pane was just GENUINELY spawned. Call site: `spawnNew`, guarded
* by `fresh === true` and a present `persistKey` + `ownerProjectId`. A fresh spawn for an id that
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 97a9ec81b..d6d02d650 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -35,7 +35,11 @@ import {
remotePaneCursorArgs
} from './remote-ssh/control-master'
import { parsePaneCursor } from './pane-cursor'
-import { recordFreshSpawnOwner, forgetPaneOwner } from './agents/pane-ownership'
+import {
+ recordFreshSpawnOwner,
+ forgetPaneOwner,
+ shouldRecordOwnership
+} from './agents/pane-ownership'
import { PANE_OWNER_FMT, foregroundArgvArgs, paneOwnerFrom, parsePaneOwner } from './agents/pane-owner'
import type { PaneOwner } from '../shared/agents/pane-owner-predicate'
import { readSpawnResources, spawnResourceNote } from './spawn-resources'
@@ -1654,7 +1658,8 @@ export class PtyManager {
// someone else spawned (incl. an app-restart re-attach) leaves the pane UNPROVEN, so a second
// project that merely opens another's node id cannot claim it. The owner is the renderer's
// machine-local project id, never the git-shared file id. See `agents/pane-ownership.ts`.
- if (fresh && options.persistKey) recordFreshSpawnOwner(options.persistKey, options.ownerProjectId)
+ if (shouldRecordOwnership(fresh, options.persistKey, options.ownerProjectId))
+ recordFreshSpawnOwner(options.persistKey as string, options.ownerProjectId)
// Surface a missing-account-dir fallback so the renderer can flag the node's account chip.
const accountFallback = spawned?.accountFallback
// The session's `persistKey` is set iff the spawn actually landed on a tmux, local or remote
diff --git a/src/renderer/components/kanban/ModalTerminal.tsx b/src/renderer/components/kanban/ModalTerminal.tsx
index 3f167cb7b..4c58bacb2 100644
--- a/src/renderer/components/kanban/ModalTerminal.tsx
+++ b/src/renderer/components/kanban/ModalTerminal.tsx
@@ -238,6 +238,12 @@ export function ModalTerminal({ nodeId, spawn, searchOpen, onCloseSearch }: Moda
shellArgs: localSsh ? buildSshArgs(spawn.ssh!) : undefined,
cwd: spawn.cwd,
persistKey: nodeId,
+ // Pane-ownership ledger parity with TerminalNode (agent messaging, PR #237 fix round 2):
+ // if this modal is ever the FIRST/sole fresh spawner of the node (a card for a node whose
+ // project is not the active canvas), the pane must still record its true owner. Use the
+ // CARD's project resolved above, NOT the active canvas id — the modal may be for a
+ // non-active project. Recorded main-side only on a genuine fresh spawn.
+ ownerProjectId: projectId,
agentId: spawn.agentId,
accountId: spawn.accountId,
sshRemote,