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(server): refuse --run-now with --after; run resolves the owned project (#925)

review1dc7c6aaa12e
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 0ecf2cb5f..6caca75b1 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -574,12 +574,16 @@ import { chordHeld, isHoldChord, isModifierEventKey, matchesShortcut } from '@sh
// The dispatch below is the CONSUMER of the confirm-gated set. Before this import the set named
// write/close as "the confirm-gated pair" from inside `src/main` — which this project cannot see —
// while the gating lived in two hand-written blocks here, so the set decided nothing.
-import { isDestructiveVerb, dryRunRequested, runNowRequested } from '@shared/control-verbs'
+import {
+ isDestructiveVerb,
+ dryRunRequested,
+ runNowRequested,
+ RUN_NOW_AFTER_REFUSAL
+} from '@shared/control-verbs'
import {
headlessStartNoticeText,
mergeRunNow,
planRunVerb,
- RUN_NOW_AFTER_REFUSAL,
savePendingAnywhere,
startHeadless,
type HeadlessStartOutcome
diff --git a/src/renderer/lib/headlessRun.ts b/src/renderer/lib/headlessRun.ts
index cfd60de3d..42952e9a7 100644
--- a/src/renderer/lib/headlessRun.ts
+++ b/src/renderer/lib/headlessRun.ts
@@ -6,9 +6,6 @@ import type { CanvasNodeState, PendingLaunch, Project, PtyCreateOptions } from '
import type { HeadlessLaunchFailure, HeadlessLaunchResult } from '@shared/headless-launch'
import { HEADLESS_COLS, HEADLESS_ROWS, localNodePtyOptions } from '@shared/node-pty-options'
-export const RUN_NOW_AFTER_REFUSAL =
- 'run-now-after-unsupported: --run-now cannot be combined with --after'
-
export type RunVerbPlan =
| 'nothing-queued'
| 'already-starting'
diff --git a/src/server/headless-node-factory.test.ts b/src/server/headless-node-factory.test.ts
index 6ed5cb0fb..d17f7c9cd 100644
--- a/src/server/headless-node-factory.test.ts
+++ b/src/server/headless-node-factory.test.ts
@@ -8,6 +8,7 @@ import { fakePlatform } from '../core/platform-fake'
import { initPlatform, resetPlatformForTests } from '../core/platform'
import { WorkspaceStore } from '../core/workspace-store'
import type { AgentState } from '../shared/agents/normalize'
+import { RUN_NOW_AFTER_REFUSAL } from '../shared/control-verbs'
import {
DEFAULT_SETTINGS,
type CanvasNodeState,
@@ -1326,6 +1327,51 @@ describe('HeadlessNodeFactory', () => {
expect(pty.sends).toEqual([{ nodeId: id, text: "claude 'brief'" }])
})
+ it('refuses --run-now with --after on open-terminal before anything is created, saved or sent (#925)', async () => {
+ states['term-upstream'] = 'working'
+ const save = vi.spyOn(store, 'save')
+ const reply = await factory.openTerminal(
+ 'term-source',
+ { cwd: projectDir, cmd: 'printf hi', after: 'term-upstream', 'run-now': '' },
+ true
+ )
+ expect(reply).toEqual({ ok: false, error: RUN_NOW_AFTER_REFUSAL })
+ expect(save).not.toHaveBeenCalled()
+ expect(pty.creates).toEqual([])
+ expect(pty.sends).toEqual([])
+ expect(published).toEqual([])
+ expect((await store.load({ sideline: false })).projects[0].nodes).toHaveLength(3)
+ })
+
+ it('refuses --run-now with --after on open-agent in the same words (#925)', async () => {
+ states['term-upstream'] = 'working'
+ const save = vi.spyOn(store, 'save')
+ const reply = await factory.openAgent(
+ 'term-source',
+ { agent: 'claude', prompt: 'brief', after: 'term-upstream', 'run-now': '1' },
+ true
+ )
+ expect(reply).toEqual({ ok: false, error: RUN_NOW_AFTER_REFUSAL })
+ expect(save).not.toHaveBeenCalled()
+ expect(pty.creates).toEqual([])
+ expect(pty.sends).toEqual([])
+ })
+
+ it('an explicit --run-now 0 is off, so --after still arms (#925)', async () => {
+ states['term-upstream'] = 'working'
+ const reply = await factory.openAgent(
+ 'term-source',
+ { agent: 'claude', prompt: 'consume result', after: 'term-upstream', 'run-now': '0' },
+ true
+ )
+ expect(reply.ok).toBe(true)
+ const id = (reply.result as { id: string }).id
+ expect(reply.result).toMatchObject({ queued: true, queuedIds: [id], deliveredIds: [] })
+ expect(pty.sends).toEqual([])
+ const node = (await store.load({ sideline: false })).projects[0].nodes.find((n) => n.id === id)
+ expect(node?.pendingLaunch).toMatchObject({ after: ['term-upstream'], attempted: false })
+ })
+
it('run delivers a retained launch for the node owner (#925)', async () => {
const spy = vi.spyOn(pty, 'writeHeadless').mockReturnValue(false)
const opened = await factory.openTerminal('term-source', { cwd: projectDir, cmd: 'printf hi' }, true)
@@ -1410,6 +1456,64 @@ describe('HeadlessNodeFactory', () => {
expect(pty.sends).toEqual([{ nodeId: depId, text: 'printf after' }])
})
+ describe('run with a node id that two projects share (#925)', () => {
+ // A committed `.nodeterm/project.json` opened from a second folder carries the same node ids.
+ // The copy this caller spawned is in `project-1`; the stranger sorts FIRST.
+ const held = { after: [], command: 'printf dup', executor: 'server' as const, attempted: false }
+ let otherDir = ''
+
+ beforeEach(async () => {
+ otherDir = path.join(dataDir, 'other')
+ fs.mkdirSync(otherDir, { recursive: true })
+ const workspace = await store.load({ sideline: false })
+ workspace.projects[0].nodes.push({ ...terminal('term-dup', 'Owned copy'), pendingLaunch: held })
+ workspace.projects.unshift({
+ id: 'project-0',
+ name: 'Other checkout',
+ color: '#0a84ff',
+ cwd: otherDir,
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: [terminal('term-dup', 'Stranger copy')],
+ bridges: [],
+ ropes: []
+ })
+ await store.save(workspace)
+ ownership.record('term-dup', { sourceNodeId: 'term-source', projectId: 'project-1' })
+ const ordered = (await store.load({ sideline: false })).projects.map((p) => p.id)
+ expect(ordered).toEqual(['project-0', 'project-1'])
+ })
+
+ const copies = async (): Promise<Record<string, CanvasNodeState | undefined>> => {
+ const workspace = await store.load({ sideline: false })
+ return Object.fromEntries(
+ workspace.projects.map((p) => [p.id, p.nodes.find((n) => n.id === 'term-dup')])
+ )
+ }
+
+ it('delivers and clears the OWNED copy, leaving the other project untouched', async () => {
+ const before = (await copies())['project-0']
+ const reply = await factory.run('term-source', { node: 'term-dup' }, true)
+ expect(reply).toMatchObject({ ok: true, result: { started: true, startedIds: ['term-dup'] } })
+ expect(pty.creates).toEqual([
+ expect.objectContaining({ persistKey: 'term-dup', ownerProjectId: 'project-1' })
+ ])
+ expect(pty.sends).toEqual([{ nodeId: 'term-dup', text: 'printf dup' }])
+ const after = await copies()
+ expect(after['project-1']?.pendingLaunch).toBeUndefined()
+ expect(after['project-0']).toEqual(before)
+ })
+
+ it('refuses a --project that is not the owned node\'s project, as the desktop does', async () => {
+ expect(await factory.run('term-source', { node: 'term-dup', project: 'project-0' }, true))
+ .toEqual({ ok: false, error: 'run: no node with id term-dup' })
+ expect(pty.creates).toEqual([])
+ expect((await copies())['project-1']?.pendingLaunch).toEqual(held)
+ // The owned project named explicitly is the same as naming none.
+ expect(await factory.run('term-source', { node: 'term-dup', project: 'project-1' }, true))
+ .toMatchObject({ ok: true, result: { started: true } })
+ })
+ })
+
it('run refuses an SSH node before any claim (#925)', async () => {
const held = { after: [], command: 'printf hi', executor: 'server' as const, attempted: false }
const workspace = await store.load({ sideline: false })
diff --git a/src/server/headless-node-factory.ts b/src/server/headless-node-factory.ts
index ba470c2de..10ae6690a 100644
--- a/src/server/headless-node-factory.ts
+++ b/src/server/headless-node-factory.ts
@@ -36,6 +36,7 @@ import {
import { assembleLaunchCommand } from '../shared/agents/launch'
import type { AgentState, NormalizedAgentEvent } from '../shared/agents/normalize'
import { oneLine } from '../shared/one-line'
+import { RUN_NOW_AFTER_REFUSAL, runNowRequested } from '../shared/control-verbs'
import { isRemoteSessionNode } from '../shared/worktree'
import { UNKNOWN_CODEX_CLI_CAPS } from '../shared/types'
import type {
@@ -1053,10 +1054,18 @@ export class HeadlessNodeFactory {
if (flagError) return { ok: false, error: `run: ${flagError}` }
const id = (args.node ?? '').trim()
if (!this.ownsSpawn(sourceNodeId, id)) return this.ownershipRefusal('run', sourceNodeId, id)
+ // Resolve through the ownership record, never by first id match: node ids repeat across
+ // projects (a committed project.json opened from a second folder), and `attach()` is keyed
+ // by id alone, so a stranger copy sorting first would be claimed while the owned session is
+ // typed into. `close` resolves the same way. A `--project` naming any other project is the
+ // desktop's lookup inside the named project coming up empty.
+ const owner = this.ownership.ownerOf(id)!
+ const noNode: ServerControlReply = { ok: false, error: `run: no node with id ${id}` }
+ if (args.project !== undefined && args.project !== owner.projectId) return noNode
const workspace = await this.deps.workspaceStore.load({ sideline: false })
- const project = workspace.projects.find((p) => p.nodes.some((n) => n.id === id))
+ const project = workspace.projects.find((p) => p.id === owner.projectId)
const node = project?.nodes.find((n) => n.id === id)
- if (!project || !node) return { ok: false, error: `run: no node with id ${id}` }
+ if (!project || !node) return noNode
const held = node.pendingLaunch
if (!held?.command) return { ok: false, error: `run-nothing-queued: ${id} has no queued launch` }
// A remote node is NEVER spawned locally, and this launcher only spawns locally. Refuse
@@ -1138,12 +1147,16 @@ export class HeadlessNodeFactory {
return this.runExclusive(async () => {
const flagError = unsupportedFlags(
args,
- // `run-now` (#925) is accepted and ignored: a server open already delivers immediately.
+ // `run-now` (#925) is a no-op, since a server open already delivers immediately; only its
+ // pairing with `--after` is refused, just below.
verb === 'open-terminal'
? new Set(['count', 'cwd', 'cmd', 'after', 'project', 'run-now'])
: new Set(['agent', 'count', 'cwd', 'prompt', 'after', 'project', 'model', 'run-now'])
)
if (flagError) return { ok: false, error: `${verb}: ${flagError}` }
+ // "Start now" and "start when X is done" contradict each other: refused in the desktop's
+ // words, before anything is created, rather than silently opening an armed node.
+ if (runNowRequested(args) && args.after) return { ok: false, error: RUN_NOW_AFTER_REFUSAL }
if (!verified) {
return {
ok: false,
diff --git a/src/shared/control-verbs.ts b/src/shared/control-verbs.ts
index a39136c78..4c68dff9b 100644
--- a/src/shared/control-verbs.ts
+++ b/src/shared/control-verbs.ts
@@ -117,6 +117,11 @@ export function runNowRequested(args: Record<string, string | undefined>): boole
return !/^(false|no|0)$/i.test(v.trim())
}
+/** "Start now" and "start when X is done" contradict each other, so `--run-now` with `--after` is
+ * refused on every open verb. Shared so the desktop and the Server Edition refuse in one sentence. */
+export const RUN_NOW_AFTER_REFUSAL =
+ 'run-now-after-unsupported: --run-now cannot be combined with --after'
+
/** The refusal for `--dry-run` on a verb outside DRY_RUN_VERBS — derived from the set so the
* sentence can never name a verb the gate does not honour. */
export function dryRunRefusal(verb: string): string {