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.

feat(relay): route worktree path + clone picker through the session core (Stage 4 Task 9)

enes85df3556a5ae
diff --git a/src/renderer/bridge/relay-api.test.ts b/src/renderer/bridge/relay-api.test.ts
index 5e83b6929..014b549e8 100644
--- a/src/renderer/bridge/relay-api.test.ts
+++ b/src/renderer/bridge/relay-api.test.ts
@@ -86,14 +86,30 @@ describe('buildRelayApi', () => {
const t = new FakeTransport()
const { api } = buildRelayApi('conn-1', t)
- // Your update banner, clipboard, dialog picker, settings and license are YOURS, not the host's.
+ // Your update banner, clipboard, settings and license are YOURS, not the host's.
expect(api.updates).toBe(local.updates)
expect(api.clipboard).toBe(local.clipboard)
expect(api.settings).toBe(local.settings)
- expect(api.dialog).toBe(local.dialog)
expect(api.license).toBe(local.license)
})
+ it('routes the folder/file picker to the HOST fs, not the local native dialog', () => {
+ // Task 9 refines Task 5's coarse "dialog → local": selectFolder/selectFile are host-path
+ // pickers in a remote tab (the chosen path feeds api.git.clone / the host fs), so they must
+ // browse the HOST filesystem via the in-app directory browser, NOT this client's native dialog.
+ const { local } = fakeLocalApi()
+ ;(globalThis as Record<string, unknown>).window = { nodeTerminal: local }
+ const t = new FakeTransport()
+ const { api } = buildRelayApi('conn-1', t)
+
+ expect(api.dialog).not.toBe(local.dialog) // overridden — no longer the local native dialog
+ expect(typeof api.dialog.selectFolder).toBe('function')
+ expect(api.dialog.selectFolder).not.toBe(
+ (local.dialog as unknown as { selectFolder?: unknown }).selectFolder
+ )
+ expect(typeof api.dialog.selectFile).toBe('function')
+ })
+
it('delegates pty.onData to the LOCAL per-session channel, not the RpcClient', () => {
const { local, ptyOnData } = fakeLocalApi()
;(globalThis as Record<string, unknown>).window = { nodeTerminal: local }
diff --git a/src/renderer/bridge/relay-api.ts b/src/renderer/bridge/relay-api.ts
index 273c5474f..a9ec33760 100644
--- a/src/renderer/bridge/relay-api.ts
+++ b/src/renderer/bridge/relay-api.ts
@@ -42,6 +42,7 @@ import {
buildClaudeApi
} from './ws-bridge'
import { buildStubApi } from './stubs'
+import { mountPickerRoot, openDirectoryPicker } from './dialog-picker'
/** What Task 6 consumes: the bridged api for `createSession`, an approval gate to await, and a
* teardown hook to run on disconnect/revoke. */
@@ -102,6 +103,23 @@ export function buildRelayApi(connectionId: string, transport?: FrameTransport):
// `settings` stays LOCAL (font/cursor/theme render in YOUR window). It came in via `...local`;
// `real.settings` is deliberately left unused so a remote tab never adopts the host's prefs.
+ // `dialog` REFINES Task 5's coarse "dialog → local". `selectFolder`/`selectFile` are the only
+ // members `DialogApi` exposes, and in a remote tab BOTH are host-path pickers, not local ones:
+ // the chosen path is fed to the SESSION core (a clone destination for `api.git.clone`, an
+ // "open folder/file" target on the host fs), so a native LOCAL picker would land the op on the
+ // wrong machine (obligation d). Route both to the SAME in-app directory browser the Server
+ // Edition uses, over the HOST's `fs.list` (`files.fs`, already core-bound). There is no other,
+ // genuinely-local `dialog.*` method that would want to stay on `...local`. Desktop-only path, so
+ // `document` exists for `mountPickerRoot`.
+ dialog: (() => {
+ mountPickerRoot()
+ const startDir = '/' // navigable up/down from the host root; no cross-call memory in v1
+ return {
+ selectFolder: () => openDirectoryPicker({ mode: 'folder', startDir, list: files.fs.list }),
+ selectFile: () => openDirectoryPicker({ mode: 'file', startDir, list: files.fs.list })
+ }
+ })(),
+
// ── Deferred over the relay in v1 — documented degrades (a clean refusal, not a wrong-machine
// silent no-op): ──
// The SDK chat node has no relay chat builder yet (the Server Edition defers it too). Routing to
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 3824131a3..50f92c539 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -101,6 +101,7 @@ import { LoopNode } from '../nodes/LoopNode'
import type { NormalizedAgentEvent } from '@shared/agents/normalize'
import {
computeWorktreePath,
+ resolveWorktreePath,
displacedByWorktree,
isRemoteSessionNode,
resolveBaseRef,
@@ -501,8 +502,12 @@ export function Canvas() {
// resolves must re-render with the real base, or it would keep suggesting nothing.
const [userDataDir, setUserDataDir] = useState('')
useEffect(() => {
- void window.nodeTerminal.userDataDir().then(setUserDataDir)
- }, [])
+ // The SESSION core's writable base, not this client's: a remote tab's worktree default path
+ // must live on the machine `git worktree add` runs on (the host — obligation c), so it comes
+ // from the session api (`api.userDataDir()`), re-resolved when the active session changes.
+ // For the local session `api` IS window.nodeTerminal, so this stays byte-identical.
+ void api.userDataDir().then(setUserDataDir)
+ }, [api])
// Worktrees already bound to a group on THIS canvas. The store's orphan list is refreshed after
// every mutation, but it is also filled asynchronously — filtering against the live nodes is the
// guard that stops the dialog from offering a worktree a second group could bind to.
@@ -4193,13 +4198,14 @@ export function Canvas() {
bindGroupId = g.id
}
const baseRef = args.base?.trim() || resolveBaseRef(entries)
- const wtPath =
- args.path?.trim() ||
- computeWorktreePath(
- await window.nodeTerminal.userDataDir(),
- repoRoot.split('/').pop() || 'repo',
- branch
- )
+ // Path from the SESSION core's userData (the HOST for a remote tab), not the local
+ // client's — the git worktree op below runs on `api.git`, so the path must live there.
+ const wtPath = await resolveWorktreePath({
+ explicitPath: args.path,
+ userDataDir: api.userDataDir,
+ repoRoot,
+ branch
+ })
if (!wtPath) {
reply({ ok: false, error: 'open-worktree: could not derive a worktree path — pass --path' })
return
diff --git a/src/renderer/components/CloneRepoDialog.tsx b/src/renderer/components/CloneRepoDialog.tsx
index f3fadc781..efbfad116 100644
--- a/src/renderer/components/CloneRepoDialog.tsx
+++ b/src/renderer/components/CloneRepoDialog.tsx
@@ -19,7 +19,7 @@ interface CloneRepoDialogProps {
/**
* Clone dialog: URL (with owner/repo GitHub shorthand preview) + parent
- * folder (native picker, last-used remembered) + live progress + inline error.
+ * folder (session picker — host fs on a relay tab, last-used remembered) + live progress + inline error.
* Cancel — or closing the dialog — aborts the in-flight clone; main cleans up the
* half-cloned directory it claimed.
*/
@@ -27,7 +27,10 @@ export function CloneRepoDialog({ open, onClose, onCloned }: CloneRepoDialogProp
// This dialog's core api (a stable context read): the clone runs on the session's core. The
// effects below capture it in their CLOSURES and keep their `[open]` dep arrays — one of them
// subscribes (onCloneProgress), and re-keying a resource effect on `api` is the 4c bomb the
- // sub-stage rules forbid. (The native folder picker stays on the app-global `dialog`.)
+ // sub-stage rules forbid. The folder picker ALSO goes through `api.dialog` (not the global): the
+ // clone lands on the machine the git op runs on, so for a relay tab the picker must browse the
+ // HOST fs (obligation d — `buildRelayApi` overrides `dialog.selectFolder`). Local session → the
+ // native local dialog, byte-identical.
const { api } = useSession()
const [url, setUrl] = useState('')
const [parent, setParent] = useState('')
@@ -144,7 +147,7 @@ export function CloneRepoDialog({ open, onClose, onCloned }: CloneRepoDialogProp
title="Choose folder"
disabled={cloning}
onClick={() => {
- void window.nodeTerminal.dialog.selectFolder().then((f) => {
+ void api.dialog.selectFolder().then((f) => {
if (f) setParent(f)
})
}}
diff --git a/src/shared/worktree.test.ts b/src/shared/worktree.test.ts
index 903df55c0..b55457f97 100644
--- a/src/shared/worktree.test.ts
+++ b/src/shared/worktree.test.ts
@@ -5,6 +5,7 @@ import {
isInsideDir,
sanitizeWorktreeBranch,
computeWorktreePath,
+ resolveWorktreePath,
parseWorktreePorcelain,
isDangerousWorktreeRemovalPath,
decideMergeStrategy,
@@ -92,6 +93,36 @@ describe('computeWorktreePath', () => {
})
})
+describe('resolveWorktreePath', () => {
+ it('derives the default UNDER the session core userData (the host, for a remote tab)', async () => {
+ // The provider is the SESSION core's `userDataDir()` — a remote tab hands the HOST's base, so
+ // the worktree lands on the machine `git worktree add` runs on, not this client.
+ const p = await resolveWorktreePath({
+ userDataDir: () => Promise.resolve('/home/host/.config/nodeterm'),
+ repoRoot: '/srv/repos/myapp',
+ branch: 'feature/x'
+ })
+ expect(p).toBe('/home/host/.config/nodeterm/worktrees/myapp/feature-x')
+ })
+ it('honors an explicit --path and never reads the userData provider', async () => {
+ const p = await resolveWorktreePath({
+ explicitPath: ' /custom/wt ',
+ userDataDir: () => Promise.reject(new Error('provider must not be read when --path is given')),
+ repoRoot: '/srv/repos/x',
+ branch: 'b'
+ })
+ expect(p).toBe('/custom/wt')
+ })
+ it('yields "" when the base dir is unknown and no --path was given', async () => {
+ const p = await resolveWorktreePath({
+ userDataDir: () => Promise.resolve(''),
+ repoRoot: '/srv/repos/x',
+ branch: 'b'
+ })
+ expect(p).toBe('')
+ })
+})
+
describe('resolveBaseRef', () => {
it("uses the main checkout's branch — git prints it first", () => {
expect(resolveBaseRef([entry('/repo', 'trunk'), entry('/wt/x', 'feature/x')])).toBe('trunk')
diff --git a/src/shared/worktree.ts b/src/shared/worktree.ts
index 378c3c0ba..e3f6931bd 100644
--- a/src/shared/worktree.ts
+++ b/src/shared/worktree.ts
@@ -91,6 +91,27 @@ export function computeWorktreePath(userDataDir: string, repoName: string, branc
return `${base}/worktrees/${repoName}/${flat}`
}
+/**
+ * Resolve the on-disk worktree directory for a create: an explicit `--path` wins; otherwise the
+ * default under the SESSION CORE's writable base (`userDataDir()` — the HOST's userData for a
+ * remote tab, so the worktree lands on the machine `git worktree add` runs on, not this client).
+ *
+ * `userDataDir` is injected (not read off any global) precisely so the path follows the session
+ * that runs the git op — the obligation-c fix. Async because the base dir is fetched from the core;
+ * a given `--path` short-circuits it, so the provider is never touched when the caller already knows
+ * the location. Returns '' when nothing can be derived (an unknown base and no `--path`).
+ */
+export async function resolveWorktreePath(args: {
+ explicitPath?: string
+ userDataDir: () => Promise<string>
+ repoRoot: string
+ branch: string
+}): Promise<string> {
+ const explicit = args.explicitPath?.trim()
+ if (explicit) return explicit
+ return computeWorktreePath(await args.userDataDir(), args.repoRoot.split('/').pop() || 'repo', args.branch)
+}
+
/** Values the worktree dialog collects. Mapped to a `GroupWorktree` by `worktreeFromCreate`. */
export interface WorktreeCreateValue {
repoPath: string