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
feat(canvas-control): open-worktree --base accepts a station id (#530)
enes98f068e590fc
diff --git a/src/main/canvas-control-core.test.ts b/src/main/canvas-control-core.test.tsindex 33e3a25da..97fb97e41 100644--- a/src/main/canvas-control-core.test.ts+++ b/src/main/canvas-control-core.test.ts@@ -316,6 +316,18 @@ describe('parseControlRequest', () => {}})+ it('both agent-facing texts document --base accepting a station id (issue #530)', () => {+ for (const body of [buildCanvasSkillBody('/x/shim.sh'), buildCanvasControlInstructions('/tmp/nodeterm.sh')]) {+ // The flag surface must show the widened grammar…+ expect(body).toContain('--base <ref|stationId>')+ // …and both hard truths beside it: what a station id resolves to, and that the base is+ // captured at CREATION (the deferred-resolution half of #530 is not built — an agent that+ // reads this text and assumes lazy capture bases a wave on an empty branch).+ expect(body.toLowerCase()).toContain('station')+ expect(body).toMatch(/captured when the worktree is CREATED/i)+ }+ })+it('both agent-facing texts document --dry-run, derived from DRY_RUN_VERBS (issue #532)', () => {for (const body of [buildCanvasSkillBody('/x/shim.sh'), buildCanvasControlInstructions('/tmp/nodeterm.sh')]) {expect(body).toContain('--dry-run')diff --git a/src/main/canvas-control-core.ts b/src/main/canvas-control-core.tsindex 2c25a2238..da7fd8726 100644--- a/src/main/canvas-control-core.ts+++ b/src/main/canvas-control-core.ts@@ -367,8 +367,13 @@ export function buildCanvasControlInstructions(shimPath: string): string {' `model` is per role, so one team can mix tiers — give an expensive model to the role that needs it',' and a cheap one to the rest. Same rule as `--model` below. A role may carry `promptFile`',' (absolute path) instead of `prompt` — same multi-line-brief semantics as `--prompt-file`.',- '- `open-worktree --branch <name> [--base <ref>] [--path P] [--group <id>]` — create a git worktree',- ' wrapped in a bound group frame (terminals inside it run in the worktree). Local projects only.',+ '- `open-worktree --branch <name> [--base <ref|stationId>] [--path P] [--group <id>]` — create a git',+ ' worktree wrapped in a bound group frame (terminals inside it run in the worktree). `--base`',+ ' takes a git ref, or the id of a STATION — a node or group inside a worktree-bound frame — and',+ ' then resolves to that station\'s branch, so wave two branches off wave one by identity instead',+ ' of restating the branch name. The base is captured when the worktree is CREATED: to build on',+ ' a station\'s finished work, create the downstream worktree after that station has committed',+ ' (or have the downstream agent merge the branch first). Local projects only.','- `close-worktree --group <id> [--mode unbind|remove]` — unbind keeps the directory; remove asks',' the user to confirm deletion.','- `branch --node <id>` — branch a Claude node\'s conversation (Claude nodes only).',@@ -776,10 +781,20 @@ Verbs:run its heavy role on a large model and the rest on a cheap one. A role may carry\`promptFile\` (absolute path) instead of \`prompt\` — the \`--prompt-file\` semantics per role,for members whose brief is structured or multi-line.-- \`open-worktree --branch <name> [--base <ref>] [--path P] [--group <id>]\` — create a git+- \`open-worktree --branch <name> [--base <ref|stationId>] [--path P] [--group <id>]\` — create a gitworktree (new branch off base, default: the repo's default branch) and wrap it in a boundgroup frame (or bind it to an existing empty group). Terminals created inside the grouprun in the worktree. Local projects only.+ \`--base\` also takes the id of a STATION — a node or group inside a worktree-bound frame —+ and resolves to that station's branch, so "branch off what that station is working on" is+ said by identity: \`open-worktree --branch wave2 --base <wave1 node or group id>\`. The reply+ names the branch the id resolved to. Refused explicitly: an id outside any worktree frame, a+ base that resolves to the branch being created, and a value that is neither a node id nor a+ valid git ref. NOTE the timing: the base is captured when the worktree is CREATED, so a+ downstream worktree made at fan-out time starts from the upstream branch AS IT IS THEN+ (usually empty) — create it after the upstream station has committed (arm the downstream+ agent with \`--after\` and open its worktree when it fires), or have the downstream agent+ \`git merge\` the upstream branch as its first step.- \`close-worktree --group <id> [--mode unbind|remove]\` — unbind (default) drops the bindingand keeps the directory; remove asks the user to confirm deleting the worktree.- \`branch --node <id>\` — branch a Claude node's conversation: the node stays on the newdiff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex e47f02c3c..cef799706 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -329,6 +329,7 @@ import {worktreeFromCreate,worktreeFromEntry,worktreeRemoveMessage,+ resolveWorktreeBase,type GroupWorktree,type WorktreeCreateValue,type WorktreeEntry@@ -9342,7 +9343,35 @@ export function Canvas() {// reduce to entries/global exactly as before. An explicit `--base` still wins.const pw = projectLaunchInfoNow(project?.id ?? '')?.resolved.worktreeconst projectDefaults = { basePath: pw?.basePath?.value, baseRef: pw?.baseRef?.value }- const baseRef = args.base?.trim() || effectiveWorktreeBaseRef(projectDefaults, entries)+ // `--base` takes a git ref OR a station id (issue #530): an id naming a node/group on+ // the canvas resolves through its worktree-bound group to that station's BRANCH, so+ // "branch off what that station is working on" is expressed by identity. The pure+ // resolver owes the explicit refusals (unbound node, self-base, neither-node-nor-ref).+ const baseRes = resolveWorktreeBase(+ args.base,+ branch,+ nodesRef.current.map((nd) => ({+ id: nd.id,+ parentId: nd.parentId,+ worktree: nd.data.worktree as GroupWorktree | undefined+ }))+ )+ if (baseRes.kind === 'error') {+ reply({ ok: false, error: `open-worktree: ${baseRes.error}` })+ return+ }+ const baseRef =+ baseRes.kind === 'default'+ ? effectiveWorktreeBaseRef(projectDefaults, entries)+ : baseRes.ref+ // Said back in every reply so the resolution is transparent — the caller passed an id+ // and must see which branch it bought.+ const baseNote =+ baseRes.kind === 'station'+ ? ` (resolved from station ${baseRes.stationId}${+ baseRes.groupId !== baseRes.stationId ? ` in group ${baseRes.groupId}` : ''+ })`+ : ''// Resolve from this session's repo root, so a relay tab still produces a path on the// same host/filesystem where the `api.git` operation below runs.const wtPath = await resolveWorktreePath({@@ -9363,14 +9392,21 @@ export function Canvas() {ok: true,message: [DRY_RUN_PREFIX,- `open-worktree would create branch "${branch}" off ${baseRef || 'the repo default branch'} at ${wtPath}, ` ++ `open-worktree would create branch "${branch}" off ${baseRef || 'the repo default branch'}${baseNote} at ${wtPath}, ` +`bound to ${bindGroupId ? `group ${bindGroupId}` : 'a new group frame'}.`,- 'The base ref is not verified against git in a dry run — a bad ref still fails at create time.'+ // A station's branch comes from a live binding; a plain ref is only vetted by+ // git at create time — say which assurance the caller is getting.+ ...(baseRes.kind === 'station'+ ? []+ : [+ 'The base ref is not verified against git in a dry run — a bad ref still fails at create time.'+ ])].join('\n'),result: {dryRun: true,branch,baseRef: baseRef || null,+ ...(baseRes.kind === 'station' ? { baseStation: baseRes.stationId } : {}),path: wtPath,group: bindGroupId}@@ -9402,8 +9438,14 @@ export function Canvas() {)reply({ok: true,- message: `opened worktree ${branch} at ${wtPath} in group ${groupId}`,- result: { groupId, branch, path: wtPath, baseRef }+ message: `opened worktree ${branch} (off ${baseRef || 'the repo default branch'}${baseNote}) at ${wtPath} in group ${groupId}`,+ result: {+ groupId,+ branch,+ path: wtPath,+ baseRef,+ ...(baseRes.kind === 'station' ? { baseStation: baseRes.stationId } : {})+ }})return}diff --git a/src/shared/worktree.test.ts b/src/shared/worktree.test.tsindex 5c5d719ff..04ed2e74e 100644--- a/src/shared/worktree.test.ts+++ b/src/shared/worktree.test.ts@@ -12,6 +12,8 @@ import {isRemoteSessionNode,isValidGitRef,resolveBaseRef,+ resolveWorktreeBase,+ type WorktreeBaseNode,worktreeFromCreate,worktreeFromEntry,worktreeRemoveMessage,@@ -512,3 +514,86 @@ describe('effectiveWorktreeTemplate', () => {expect(computeWorktreePath('/src/repo', 'topic/a', template)).toBe('')})})++describe('resolveWorktreeBase (issue #530 — --base <ref|stationId>)', () => {+ // A wave-one worktree frame (g1, branch wave1) holding an agent node (a1), a nested inner+ // frame (g1a) holding a2; a loose node (n0); an unbound frame (g2) with a member (b1).+ const NODES: WorktreeBaseNode[] = [+ { id: 'g1', worktree: { branch: 'wave1' } },+ { id: 'a1', parentId: 'g1' },+ { id: 'g1a', parentId: 'g1' },+ { id: 'a2', parentId: 'g1a' },+ { id: 'n0' },+ { id: 'g2' },+ { id: 'b1', parentId: 'g2' }+ ]++ it('no --base means the default', () => {+ expect(resolveWorktreeBase(undefined, 'wave2', NODES)).toEqual({ kind: 'default' })+ expect(resolveWorktreeBase(' ', 'wave2', NODES)).toEqual({ kind: 'default' })+ })+ it('a member node resolves through its bound group to the branch', () => {+ expect(resolveWorktreeBase('a1', 'wave2', NODES)).toEqual({+ kind: 'station',+ ref: 'wave1',+ stationId: 'a1',+ groupId: 'g1'+ })+ })+ it('a node in a NESTED frame climbs to the nearest bound ancestor', () => {+ expect(resolveWorktreeBase('a2', 'wave2', NODES)).toEqual({+ kind: 'station',+ ref: 'wave1',+ stationId: 'a2',+ groupId: 'g1'+ })+ })+ it('the bound group id itself is a station', () => {+ expect(resolveWorktreeBase('g1', 'wave2', NODES)).toEqual({+ kind: 'station',+ ref: 'wave1',+ stationId: 'g1',+ groupId: 'g1'+ })+ })+ it('a node outside any worktree frame is an explicit refusal — never reinterpreted as a ref', () => {+ for (const id of ['n0', 'b1', 'g2']) {+ const r = resolveWorktreeBase(id, 'wave2', NODES)+ expect(r.kind).toBe('error')+ if (r.kind === 'error') expect(r.error).toContain('not inside a worktree-bound frame')+ }+ })+ it('a station whose branch IS the new branch is refused (self-base)', () => {+ const r = resolveWorktreeBase('a1', 'wave1', NODES)+ expect(r.kind).toBe('error')+ if (r.kind === 'error') expect(r.error).toContain('based on itself')+ })+ it('a plain ref self-base is refused too', () => {+ const r = resolveWorktreeBase('wave2', 'wave2', NODES)+ expect(r.kind).toBe('error')+ if (r.kind === 'error') expect(r.error).toContain('based on itself')+ })+ it('a value naming no node passes through as a validated git ref', () => {+ expect(resolveWorktreeBase('main', 'wave2', NODES)).toEqual({ kind: 'ref', ref: 'main' })+ expect(resolveWorktreeBase('origin/main', 'wave2', NODES)).toEqual({+ kind: 'ref',+ ref: 'origin/main'+ })+ })+ it('a value that is neither a node nor a valid ref is refused', () => {+ for (const bad of ['-rf', 'a..b', 'has space']) {+ const r = resolveWorktreeBase(bad, 'wave2', NODES)+ expect(r.kind).toBe('error')+ if (r.kind === 'error') expect(r.error).toContain('neither')+ }+ })+ it('a forged parentId cycle terminates as a refusal instead of hanging', () => {+ // project.json is hand-editable and git-shared; the walk is visited-set guarded.+ const cyc: WorktreeBaseNode[] = [+ { id: 'x', parentId: 'y' },+ { id: 'y', parentId: 'x' }+ ]+ const r = resolveWorktreeBase('x', 'wave2', cyc)+ expect(r.kind).toBe('error')+ })+})diff --git a/src/shared/worktree.ts b/src/shared/worktree.tsindex 0594d884b..b1e3b0799 100644--- a/src/shared/worktree.ts+++ b/src/shared/worktree.ts@@ -80,6 +80,89 @@ export function isValidGitRef(name: string): boolean {return !/[\s~^:?*[\\]|\.\.|^\/|\/$|@\{/.test(n)}+/** The node shape `resolveWorktreeBase` walks — id, containment, and (for groups) the binding. */+export interface WorktreeBaseNode {+ id: string+ parentId?: string+ /** The group's binding, when this node is a worktree-bound group frame. */+ worktree?: Pick<GroupWorktree, 'branch'>+}++export type WorktreeBaseResolution =+ /** No `--base` given — the caller falls back to its default base. */+ | { kind: 'default' }+ /** A plain git ref, validated by `isValidGitRef`. */+ | { kind: 'ref'; ref: string }+ /** A STATION: the id named a node/group on the canvas, and the base is the branch of the+ * worktree-bound group that contains it (or is it). */+ | { kind: 'station'; ref: string; stationId: string; groupId: string }+ | { kind: 'error'; error: string }++/**+ * Resolve `open-worktree --base <value>` (issue #530): the value may be a git ref, OR the id of a+ * station — a node or group frame inside a worktree-bound group — in which case the base is that+ * station's branch, resolved through the binding. This lets an orchestrator say "branch off what+ * that station is working on" by IDENTITY, instead of restating the branch name (and getting it+ * wrong) in every downstream call.+ *+ * Precedence: an id that names an EXISTING node is always read as a station — a node that is not+ * inside a worktree frame is an explicit refusal, never silently reinterpreted as a git ref (an+ * id that matched a node was clearly meant as one; falling through would base the worktree on a+ * ref that happens to parse). A value naming no node must be a valid git ref. Refusals are+ * explicit for: a station with no binding, a base that resolves to the branch being created+ * (a worktree cannot be based on itself), and a value that is neither a node nor a valid ref.+ *+ * Note the timing this deliberately does NOT change: the base is captured when the worktree is+ * CREATED, exactly like a plain ref — deferred (create-at-fire) worktrees are a separate design+ * (the rest of issue #530). The docs tell the orchestrator to create a downstream worktree after+ * the upstream station has committed.+ */+export function resolveWorktreeBase(+ baseArg: string | undefined,+ newBranch: string,+ nodes: WorktreeBaseNode[]+): WorktreeBaseResolution {+ const raw = (baseArg ?? '').trim()+ if (!raw) return { kind: 'default' }+ const byId = new Map(nodes.map((n) => [n.id, n]))+ const station = byId.get(raw)+ if (station) {+ // Climb from the station to the nearest worktree-bound group (the station may BE the group).+ // Visited-set guarded: parentId chains come from a hand-editable, git-shared project.json,+ // and a forged cycle must not hang the verb.+ const seen = new Set<string>()+ let cur: WorktreeBaseNode | undefined = station+ while (cur && !seen.has(cur.id)) {+ seen.add(cur.id)+ const branch = cur.worktree?.branch+ if (branch) {+ if (branch === newBranch) {+ return {+ kind: 'error',+ error: `--base ${raw} resolves to branch "${branch}", which is the branch being created — a worktree cannot be based on itself`+ }+ }+ return { kind: 'station', ref: branch, stationId: raw, groupId: cur.id }+ }+ cur = cur.parentId ? byId.get(cur.parentId) : undefined+ }+ return {+ kind: 'error',+ error: `--base ${raw} names a node that is not inside a worktree-bound frame — pass a git ref, or a station (node or group id) inside a worktree frame`+ }+ }+ if (!isValidGitRef(raw)) {+ return {+ kind: 'error',+ error: `--base "${raw}" is neither an existing node/group id nor a valid git ref`+ }+ }+ if (raw === newBranch) {+ return { kind: 'error', error: `--base "${raw}" is the branch being created — a worktree cannot be based on itself` }+ }+ return { kind: 'ref', ref: raw }+}+/** Flatten a branch name into a filesystem-safe, flag-safe slug. */export function sanitizeWorktreeBranch(input: string): string {return input