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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(canvas): one --project belt; run refuses an unmounted held launch (#925)
review4c265078d871
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex 8e8e746d1..0ecf2cb5f 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -314,6 +314,7 @@ import {nextFreePosition,armForColdOpen,projectTargetFlagRefusal,+ resolveProjectTarget,clearAttachConsent,folderName} from '../lib/projectOpen'@@ -10366,42 +10367,23 @@ export function Canvas() {return}const tgStore = useProjects.getState()- const tgLiveSrc = nodesRef.current.find((n) => n.id === sourceNodeId)- const tgStoredSrc = tgStore.projects- .flatMap((p) => p.nodes.map((n) => ({ node: n, projectId: p.id })))- .find((x) => x.node.id === sourceNodeId)- const callerProjectId = tgLiveSrc ? tgStore.activeProjectId : tgStoredSrc?.projectId- if (targetId !== callerProjectId) {- if (!tgLiveSrc && !tgStoredSrc) {- reply({ ok: false, error: 'source node is not in any open project' })- return- }- if (!sourceIsControlCapable(tgLiveSrc?.data.agentId ?? tgStoredSrc?.node.agentId)) {- reply({ ok: false, error: 'source node is not a control-capable agent' })- return- }- // Belt only — main already refused every stranger id with one byte-identical sentence- // (no existence oracle). This fires for a target main authorized that this renderer's- // store cannot see yet (mid-hydration), so it is worded transient.- const target = tgStore.getProject(targetId)- if (!target) {- reply({- ok: false,- error: 'project-target-refused: the target project is not available here — try again'- })- return- }- // Belt for the SSH invariant: a granted SSH id cannot exist (grants are minted only by- // local open-project) and main refuses ungranted ones — but if that ever breaks, the- // target is refused, not opened. A relay tab is another machine's project entirely.- if (target.ssh || target.remote) {- reply({- ok: false,- error:- 'project-target-ssh-unsupported: opening sessions into an SSH project is not supported — do not retry'- })- return- }+ // The source and target belt is ONE definition, shared with `run --project`+ // (resolveProjectTarget, lib/projectOpen — the order and every sentence are pinned there).+ // `own` falls through to the legacy path below.+ const tgResolved = resolveProjectTarget({+ targetId,+ sourceNodeId,+ liveNodes: nodesRef.current,+ projects: tgStore.projects,+ activeProjectId: tgStore.activeProjectId,+ sshVerbWord: 'opening'+ })+ if (tgResolved.kind === 'refused') {+ reply({ ok: false, error: tgResolved.error })+ return+ }+ if (tgResolved.kind === 'target') {+ const target = tgResolved.projectconst tgAgentId = (verb === 'open-agent' ? args.agent : 'claude') as AgentIdconst tgIsTerminal = verb === 'open-terminal'const tgCount = Math.max(@@ -10521,38 +10503,21 @@ export function Canvas() {// project falls through to the normal routing and the `case 'run'` below.if (verb === 'run' && args.project !== undefined) {const runStore = useProjects.getState()- const runLiveSrc = nodesRef.current.find((n) => n.id === sourceNodeId)- const runStoredSrc = runStore.projects- .flatMap((p) => p.nodes.map((n) => ({ node: n, projectId: p.id })))- .find((x) => x.node.id === sourceNodeId)- const runCallerProjectId = runLiveSrc ? runStore.activeProjectId : runStoredSrc?.projectId- if (args.project !== runCallerProjectId) {- if (!runLiveSrc && !runStoredSrc) {- reply({ ok: false, error: 'source node is not in any open project' })- return- }- if (!sourceIsControlCapable(runLiveSrc?.data.agentId ?? runStoredSrc?.node.agentId)) {- reply({ ok: false, error: 'source node is not a control-capable agent' })- return- }- // Belt only, worded like the `--project` open block's: main refused every stranger id.- const runTarget = runStore.getProject(args.project)- if (!runTarget) {- reply({- ok: false,- error: 'project-target-refused: the target project is not available here — try again'- })- return- }- if (runTarget.ssh || runTarget.remote) {- reply({- ok: false,- error:- 'project-target-ssh-unsupported: starting sessions in an SSH project is not supported — do not retry'- })- return- }- reply(await runQueuedNodeRef.current(runTarget, args.node ?? ''))+ // The SAME belt the `--project` open block runs (resolveProjectTarget), in `run`'s words.+ const runResolved = resolveProjectTarget({+ targetId: args.project,+ sourceNodeId,+ liveNodes: nodesRef.current,+ projects: runStore.projects,+ activeProjectId: runStore.activeProjectId,+ sshVerbWord: 'starting'+ })+ if (runResolved.kind === 'refused') {+ reply({ ok: false, error: runResolved.error })+ return+ }+ if (runResolved.kind === 'target') {+ reply(await runQueuedNodeRef.current(runResolved.project, args.node ?? ''))return}}@@ -14375,9 +14340,15 @@ export function Canvas() {// A closed project gets its tab back, never the focus (#925 spec §2.5): unhide BEFORE the// claim's disk write, so the same write persists it. Not while no project is active (the// welcome screen): un-closing one there would flip `hasProjects` and render a canvas with no- // active project. The session still starts; the project stays in Recently closed.+ // active project. The session still starts; the project stays in Recently closed. Nor for+ // an SSH project: `startHeadless` refuses every one of its nodes (`remote-unsupported`)+ // before any claim, so nothing would start and no claim write would persist the tab.const projectsNow = useProjects.getState()- if (projectsNow.getProject(project.id)?.closed && projectsNow.activeProjectId !== '') {+ if (+ projectsNow.getProject(project.id)?.closed &&+ !project.ssh &&+ projectsNow.activeProjectId !== ''+ ) {projectsNow.unhideProject(project.id)}const outcomes = await Promise.all(nodes.map((node) => startHeadless(deps, { project, node })))@@ -14429,6 +14400,13 @@ export function Canvas() {return { ok: false, error: `run-already-starting: ${nodeId} is already starting` }case 'wait-for-mount':return one(false, 'starts-when-mounted')+ case 'refuse-not-mounted':+ // The mount will not fire a manualOnly or `--after` launch, so "starts when mounted"+ // would be a promise nobody keeps. Starting it NOW takes ▶ on the mounted node.+ return {+ ok: false,+ error: `run-not-mounted: ${nodeId} is on screen but its terminal is not mounted — bring it into view and use Run now`+ }case 'mounted': {// The ▶ path verbatim (TerminalNode's QUEUED button): disarm only on a landed delivery.const outcome = await launchCommand(nodeId, pending!.command, true, api)diff --git a/src/renderer/canvas/control-open-project.source.test.ts b/src/renderer/canvas/control-open-project.source.test.tsindex a2de4190e..d43936db1 100644--- a/src/renderer/canvas/control-open-project.source.test.ts+++ b/src/renderer/canvas/control-open-project.source.test.ts@@ -99,9 +99,11 @@ describe('the --project targeted-opens block (source pins)', () => {})it('the gate order is refusal-before-write: every refusal precedes every store/canvas write', () => {- // A refused target must write NOTHING. The flag refusal, the source checks, the- // unknown-target belt and the SSH belt all return before the first applyNodeMutation or- // setNodes — moving a write above any of them is the gate-before-write mutation.+ // A refused target must write NOTHING. The flag refusal and the source/target belt both+ // return before the first applyNodeMutation or setNodes — moving a write above either is the+ // gate-before-write mutation. Both are decided by pure helpers whose logic (and every refusal+ // sentence) is red-capable in projectOpen.test.ts; what only the source can show is that the+ // dispatch relays their answer before it writes.const body = targetedOpensBody()const firstWrite = Math.min(...['applyNodeMutation', 'setNodes'].map((s) => {@@ -110,18 +112,36 @@ describe('the --project targeted-opens block (source pins)', () => {}))for (const refusal of [- // The flag exclusion is decided by the pure projectTargetFlagRefusal (its logic is- // red-capable in projectOpen.test.ts — review #363 I-2); here we pin its CALL SITE's- // position, like the inline refusals below.'projectTargetFlagRefusal(args)',- 'project-target-refused',- 'project-target-ssh-unsupported',- 'source node is not a control-capable agent'+ 'resolveProjectTarget(',+ "if (tgResolved.kind === 'refused') {"]) {const at = body.indexOf(refusal)expect(at, refusal).toBeGreaterThan(-1)expect(at, `${refusal} after a write`).toBeLessThan(firstWrite)}+ expect(body).toMatch(+ /if \(tgResolved\.kind === 'refused'\) \{\s+reply\(\{ ok: false, error: tgResolved\.error \}\)\s+return/+ )+ })++ it('the --project belt is ONE definition: open-* and run both call resolveProjectTarget (#925)', () => {+ // Two hand-copies of the caller-project resolution + source + target checks are how one of+ // them drifts (CLAUDE.md "Adding a new agent" rule 10). Neither block may re-grow an inline+ // copy of the belt; each passes only the word its refusal differs in.+ const body = targetedOpensBody()+ expect(body.match(/resolveProjectTarget\(/g)?.length).toBe(2)+ expect(body).toMatch(/resolveProjectTarget\(\{[^}]*sshVerbWord: 'opening'/)+ expect(body).toMatch(/resolveProjectTarget\(\{[^}]*sshVerbWord: 'starting'/)+ for (const inline of [+ 'project-target-refused',+ 'project-target-ssh-unsupported',+ 'source node is not in any open project',+ 'source node is not a control-capable agent',+ 'callerProjectId'+ ]) {+ expect(body, inline).not.toContain(inline)+ }})it('the flag-exclusion guard FIRES on a truthy refusal — polarity pinned (review #363 I-2)', () => {@@ -152,8 +172,10 @@ describe('the --project targeted-opens block (source pins)', () => {})it('the caller’s OWN project id falls through to the legacy path (B3a) — no return on that leg', () => {+ // `own` is the one resolution with no branch of its own: only `refused` and `target` return.const body = targetedOpensBody()- expect(body).toContain('if (targetId !== callerProjectId)')+ expect(body).toContain("if (tgResolved.kind === 'target') {")+ expect(body).not.toMatch(/kind === 'own'/)expect(body).toContain('fall through to the legacy path unchanged')})diff --git a/src/renderer/lib/headlessRun.test.ts b/src/renderer/lib/headlessRun.test.tsindex a5a55657f..e4d792d02 100644--- a/src/renderer/lib/headlessRun.test.ts+++ b/src/renderer/lib/headlessRun.test.ts@@ -43,6 +43,24 @@ describe('planRunVerb', () => {expect(planRunVerb({ ...base, projectActive: true })).toBe('wait-for-mount')expect(planRunVerb(base)).toBe('headless')})++ it('refuses an on-screen, unmounted node whose launch the mount will not fire', () => {+ const onScreen = { inFlight: false, projectActive: true, hasWriter: false }+ // Amber (manualOnly): only an explicit Run now delivers it.+ expect(planRunVerb({ ...onScreen, pending: { ...pending, attempted: true, manualOnly: true } })).toBe(+ 'refuse-not-mounted'+ )+ // Armed with --after: the canvas fires it when its deps report done, not on mount.+ expect(planRunVerb({ ...onScreen, pending: { ...pending, after: ['dep-1'] } })).toBe('refuse-not-mounted')+ // The plain, never-attempted, dependency-free launch does start on mount.+ expect(planRunVerb({ ...onScreen, pending })).toBe('wait-for-mount')+ })++ it('a mounted writer or an off-screen project is unaffected by manualOnly / --after', () => {+ const held = { ...pending, attempted: true, manualOnly: true, after: ['dep-1'] }+ expect(planRunVerb({ pending: held, inFlight: false, projectActive: true, hasWriter: true })).toBe('mounted')+ expect(planRunVerb({ pending: held, inFlight: false, projectActive: false, hasWriter: false })).toBe('headless')+ })})describe('claimForHeadless', () => {diff --git a/src/renderer/lib/headlessRun.ts b/src/renderer/lib/headlessRun.tsindex e0f32bd7b..cfd60de3d 100644--- a/src/renderer/lib/headlessRun.ts+++ b/src/renderer/lib/headlessRun.ts@@ -9,10 +9,20 @@ import { HEADLESS_COLS, HEADLESS_ROWS, localNodePtyOptions } from '@shared/node-export const RUN_NOW_AFTER_REFUSAL ='run-now-after-unsupported: --run-now cannot be combined with --after'-export type RunVerbPlan = 'nothing-queued' | 'already-starting' | 'mounted' | 'wait-for-mount' | 'headless'+export type RunVerbPlan =+ | 'nothing-queued'+ | 'already-starting'+ | 'mounted'+ | 'wait-for-mount'+ | 'refuse-not-mounted'+ | 'headless'/** What `run --node` does. A project on screen never starts headless: its node either has a- * mounted writer (the ▶ path) or starts through the ordinary path when its terminal next mounts. */+ * mounted writer (the ▶ path) or starts through the ordinary path when its terminal next mounts.+ * That ordinary path only fires a plain, never-attempted launch with no dependencies: a+ * `manualOnly` launch waits for an explicit Run now and an `--after` launch waits for its deps.+ * Telling the caller either one "starts when mounted" would promise a start that never comes, so+ * those are refused instead (`refuse-not-mounted`). */export function planRunVerb(input: {pending?: PendingLaunchinFlight: boolean@@ -21,7 +31,12 @@ export function planRunVerb(input: {}): RunVerbPlan {if (!input.pending?.command) return 'nothing-queued'if (input.inFlight) return 'already-starting'- if (input.projectActive) return input.hasWriter ? 'mounted' : 'wait-for-mount'+ if (input.projectActive) {+ if (input.hasWriter) return 'mounted'+ // `after` is required by the type, but the launch comes out of hand-editable project JSON.+ const waitsOnDeps = (input.pending.after?.length ?? 0) > 0+ return input.pending.manualOnly || waitsOnDeps ? 'refuse-not-mounted' : 'wait-for-mount'+ }return 'headless'}diff --git a/src/renderer/lib/projectOpen.test.ts b/src/renderer/lib/projectOpen.test.tsindex da8d9a535..58ee51775 100644--- a/src/renderer/lib/projectOpen.test.ts+++ b/src/renderer/lib/projectOpen.test.ts@@ -11,6 +11,7 @@ import {nextFreePosition,armForColdOpen,projectTargetFlagRefusal,+ resolveProjectTarget,clearAttachConsent,type PlacedNode} from './projectOpen'@@ -238,6 +239,102 @@ describe('projectTargetFlagRefusal — the v1 flag exclusion fires (review I-2)'})})+describe('resolveProjectTarget — the ONE --project belt (open-* and run, #925)', () => {+ // `claude` is control-capable; a node with no agent id (a plain terminal) is not.+ const tgProjects = [+ { id: 'own', nodes: [{ id: 'src-stored', agentId: 'claude' }, { id: 'plain', agentId: undefined }] },+ { id: 'other', nodes: [] },+ { id: 'ssh', ssh: { remoteCwd: '~' }, nodes: [] },+ { id: 'relay', remote: true, nodes: [] }+ ]+ const base = {+ sourceNodeId: 'src-stored',+ liveNodes: [] as { id: string; data: { agentId?: unknown } }[],+ projects: tgProjects,+ activeProjectId: 'other',+ sshVerbWord: 'opening' as const+ }++ it("the caller's OWN project is `own` — nothing below applies to it (B3a)", () => {+ // Off canvas: the source's project is the one whose serialized nodes hold it.+ expect(resolveProjectTarget({ ...base, targetId: 'own' })).toEqual({ kind: 'own' })+ // On canvas: a source on the live canvas belongs to the ACTIVE project.+ expect(+ resolveProjectTarget({+ ...base,+ sourceNodeId: 'src-live',+ liveNodes: [{ id: 'src-live', data: { agentId: 'claude' } }],+ targetId: 'other'+ })+ ).toEqual({ kind: 'own' })+ })++ it('refuses a source found nowhere', () => {+ expect(resolveProjectTarget({ ...base, sourceNodeId: 'ghost', targetId: 'other' })).toEqual({+ kind: 'refused',+ error: 'source node is not in any open project'+ })+ })++ it('refuses a source that is not a control-capable agent (live or stored)', () => {+ const refused = { kind: 'refused', error: 'source node is not a control-capable agent' }+ expect(resolveProjectTarget({ ...base, sourceNodeId: 'plain', targetId: 'other' })).toEqual(refused)+ expect(+ resolveProjectTarget({+ ...base,+ sourceNodeId: 'live-plain',+ liveNodes: [{ id: 'live-plain', data: {} }],+ activeProjectId: 'own',+ targetId: 'other'+ })+ ).toEqual(refused)+ })++ it('refuses a target this store cannot see, worded transient', () => {+ expect(resolveProjectTarget({ ...base, targetId: 'missing' })).toEqual({+ kind: 'refused',+ error: 'project-target-refused: the target project is not available here — try again'+ })+ })++ it('refuses an SSH target and a relay target — byte-identical to core for the open verbs', () => {+ // The `opening` sentence is core's PROJECT_TARGET_SSH_UNSUPPORTED verbatim+ // (src/core/project-grants.ts); only the verb word differs for `run`.+ const opening = {+ kind: 'refused',+ error: 'project-target-ssh-unsupported: opening sessions into an SSH project is not supported — do not retry'+ }+ expect(resolveProjectTarget({ ...base, targetId: 'ssh' })).toEqual(opening)+ expect(resolveProjectTarget({ ...base, targetId: 'relay' })).toEqual(opening)+ })++ it("run's wording: `starting sessions`, same refusal otherwise", () => {+ const starting = {+ kind: 'refused',+ error: 'project-target-ssh-unsupported: starting sessions into an SSH project is not supported — do not retry'+ }+ expect(resolveProjectTarget({ ...base, targetId: 'ssh', sshVerbWord: 'starting' })).toEqual(starting)+ expect(resolveProjectTarget({ ...base, targetId: 'relay', sshVerbWord: 'starting' })).toEqual(starting)+ })++ it('a valid local target resolves to that project object', () => {+ const r = resolveProjectTarget({ ...base, targetId: 'other' })+ expect(r).toEqual({ kind: 'target', project: tgProjects[1] })+ if (r.kind === 'target') expect(r.project).toBe(tgProjects[1])+ })++ it('checks the source before the target: an unknown source never learns the target exists', () => {+ expect(resolveProjectTarget({ ...base, sourceNodeId: 'ghost', targetId: 'ssh' })).toEqual({+ kind: 'refused',+ error: 'source node is not in any open project'+ })+ expect(resolveProjectTarget({ ...base, sourceNodeId: 'plain', targetId: 'missing' })).toEqual({+ kind: 'refused',+ error: 'source node is not a control-capable agent'+ })+ })+})+describe('clearAttachConsent — the mirror dies with its caller (review M-1)', () => {it('a cleared caller confirms again; other callers are untouched', () => {recordAttachConsent('term-caller', 'project-a')diff --git a/src/renderer/lib/projectOpen.ts b/src/renderer/lib/projectOpen.tsindex 2f186c150..3144a149b 100644--- a/src/renderer/lib/projectOpen.ts+++ b/src/renderer/lib/projectOpen.ts@@ -3,6 +3,7 @@// calls and the reply — so every decision that does not need React is unit-testable here, the// same reasoning as controlRouting.ts / pendingLaunch.ts.import { oneLine } from '@shared/one-line'+import { sourceIsControlCapable } from './controlRouting'/** The little these helpers need to know about a project. */export interface ProjectForOpen {@@ -89,6 +90,77 @@ export function projectTargetFlagRefusal(args: { group?: string; after?: stringreturn null}+/** What `resolveProjectTarget` needs to know about a project (a structural subset of `Project`). */+export interface ProjectTargetProject {+ id: string+ ssh?: unknown+ remote?: boolean+ nodes: readonly { id: string; agentId?: unknown }[]+}++export type ProjectTargetResolution<P> =+ /** The target IS the caller's own project: fall through to the legacy path (B3a). */+ | { kind: 'own' }+ | { kind: 'refused'; error: string }+ | { kind: 'target'; project: P }++/**+ * The renderer's `--project` belt, ONE definition for every verb that takes the flag+ * (`open-terminal`/`open-claude`/`open-agent` and `run`, #925). Main's `gateProjectTarget` already+ * enforced own-or-granted before forwarding, so none of this is the boundary — but it is the+ * cross-project reach's second line, and two hand-copies of it are how one of them drifts.+ *+ * Order matters and is the dispatch's historical one: the caller's own project first (it is not a+ * cross-project call at all, so nothing below applies to it), then the source checks, then the+ * target checks.+ * - The caller's project is the ACTIVE one when the source is on the live canvas, else the one+ * whose serialized nodes hold it.+ * - The unknown-target refusal is worded TRANSIENT: main refused every stranger id with one+ * byte-identical sentence (no existence oracle), so this only fires for a target main+ * authorized that the renderer's store cannot see yet (mid-hydration).+ * - The SSH/relay refusal is belt for an invariant main already holds: grants are minted only by+ * local open-project, so a granted SSH id cannot exist; if that ever breaks, the target is still+ * refused, never acted on. A relay tab is another machine's project entirely. `sshVerbWord` is+ * the one word the verbs differ in; with `opening` the sentence is byte-identical to core's+ * `PROJECT_TARGET_SSH_UNSUPPORTED`.+ */+export function resolveProjectTarget<P extends ProjectTargetProject>(input: {+ targetId: string+ sourceNodeId: string+ /** React Flow's nodes — the ACTIVE project's. */+ liveNodes: readonly { id: string; data: { agentId?: unknown } }[]+ projects: readonly P[]+ activeProjectId: string+ sshVerbWord: 'opening' | 'starting'+}): ProjectTargetResolution<P> {+ const liveSrc = input.liveNodes.find((n) => n.id === input.sourceNodeId)+ const storedSrc = input.projects+ .flatMap((p) => p.nodes.map((n) => ({ node: n, projectId: p.id })))+ .find((x) => x.node.id === input.sourceNodeId)+ const callerProjectId = liveSrc ? input.activeProjectId : storedSrc?.projectId+ if (input.targetId === callerProjectId) return { kind: 'own' }+ if (!liveSrc && !storedSrc) {+ return { kind: 'refused', error: 'source node is not in any open project' }+ }+ if (!sourceIsControlCapable(liveSrc?.data.agentId ?? storedSrc?.node.agentId)) {+ return { kind: 'refused', error: 'source node is not a control-capable agent' }+ }+ const project = input.projects.find((p) => p.id === input.targetId)+ if (!project) {+ return {+ kind: 'refused',+ error: 'project-target-refused: the target project is not available here — try again'+ }+ }+ if (project.ssh || project.remote) {+ return {+ kind: 'refused',+ error: `project-target-ssh-unsupported: ${input.sshVerbWord} sessions into an SSH project is not supported — do not retry`+ }+ }+ return { kind: 'target', project }+}+export type OpenProjectPlan =| { kind: 'silent'; project: ProjectForOpen }| {