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(worktree): a remote repo's worktrees are refused, never guessed at locally

enes2dffda2b2d9f
diff --git a/src/core/git-service.ts b/src/core/git-service.ts
index 43889379a..006854a02 100644
--- a/src/core/git-service.ts
+++ b/src/core/git-service.ts
@@ -118,9 +118,18 @@ async function git(cwd: string, args: string[]): Promise<Exec> {
*/
const isRemoteRepo = (repoPath: string): boolean => !!resolveGitRemote(repoPath)
-/** What the worktree ops answer for a repo that lives on an SSH host. See `isRemoteRepo`. */
-const REMOTE_WORKTREE_REFUSAL =
- 'Worktrees are not supported in SSH projects. Nothing was changed.'
+/**
+ * What the worktree ops answer for a repo that lives on an SSH host. See `isRemoteRepo`.
+ *
+ * A plain failed op — and NEVER `worktreeGone`, which is the one field the renderer reads as proof
+ * that a directory is gone (it destroys the descendant terminals' tmux sessions and rewrites their
+ * persisted cwds on it). A refusal knows nothing about the host's filesystem, so it must not claim
+ * to.
+ */
+const REMOTE_WORKTREE_REFUSAL = (): worktreeOps.WorktreeOpResult => ({
+ ok: false,
+ message: 'Worktrees are not supported in SSH projects. Nothing was changed.'
+})
function parseRepoName(url: string, fallback: string): string {
const m = url.match(/[:/]([^/:]+\/[^/]+?)(?:\.git)?$/)
@@ -270,29 +279,39 @@ export class GitService {
// the other side of this IPC would otherwise mark every bound group missing on one bad read.
return worktreeOps.listWorktrees(git, repoPath, async (p) => fs.existsSync(p))
}
- worktreeAdd(repoPath: string, wtPath: string, branch: string, baseRef: string, isNew: boolean) {
+ worktreeAdd(
+ repoPath: string,
+ wtPath: string,
+ branch: string,
+ baseRef: string,
+ isNew: boolean
+ ): Promise<worktreeOps.WorktreeOpResult> {
// `wtPath` is computed from the LOCAL data dir, so adding it through a remote git would create a
// worktree at a nonsense path on the host. Refuse (see `isRemoteRepo`).
- if (isRemoteRepo(repoPath)) {
- return Promise.resolve({ ok: false, message: REMOTE_WORKTREE_REFUSAL })
- }
+ if (isRemoteRepo(repoPath)) return Promise.resolve(REMOTE_WORKTREE_REFUSAL())
return worktreeOps.worktreeAdd(git, repoPath, wtPath, branch, baseRef, isNew)
}
- worktreeMerge(repoPath: string, branch: string, baseRef: string, push = false) {
- if (isRemoteRepo(repoPath)) {
- return Promise.resolve({ ok: false, message: REMOTE_WORKTREE_REFUSAL })
- }
+ worktreeMerge(
+ repoPath: string,
+ branch: string,
+ baseRef: string,
+ push = false
+ ): Promise<worktreeOps.WorktreeOpResult> {
+ if (isRemoteRepo(repoPath)) return Promise.resolve(REMOTE_WORKTREE_REFUSAL())
// `pathExists` is git's `prunable` flag's fallback for git < 2.36 (see worktree-ops) — and the
// only thing that stops a merge into a base checkout whose directory is gone. LOCAL-only: see
// `isRemoteRepo` for why a remote repo is refused above instead of stat'd here.
return worktreeOps.worktreeMerge(git, repoPath, branch, baseRef, push, async (p) => fs.existsSync(p))
}
- worktreeRemove(repoPath: string, wtPath: string, deleteBranch: boolean, pruneOnly = false) {
+ worktreeRemove(
+ repoPath: string,
+ wtPath: string,
+ deleteBranch: boolean,
+ pruneOnly = false
+ ): Promise<worktreeOps.WorktreeOpResult> {
// Refuse — and note what the refusal does NOT say: `worktreeGone`. A local stat of a remote
// worktree would claim exactly that, and the renderer treats it as proof the directory is gone.
- if (isRemoteRepo(repoPath)) {
- return Promise.resolve({ ok: false, message: REMOTE_WORKTREE_REFUSAL })
- }
+ if (isRemoteRepo(repoPath)) return Promise.resolve(REMOTE_WORKTREE_REFUSAL())
// `pathExists` is git's `prunable` flag's fallback for git < 2.36 (see worktree-ops). LOCAL-only
// (see `isRemoteRepo`).
return worktreeOps.worktreeRemove(git, repoPath, wtPath, os.homedir(), deleteBranch, pruneOnly, async (p) =>
diff --git a/src/core/git-service.worktree-remote.test.ts b/src/core/git-service.worktree-remote.test.ts
new file mode 100644
index 000000000..09285c8bc
--- /dev/null
+++ b/src/core/git-service.worktree-remote.test.ts
@@ -0,0 +1,109 @@
+// GitService's worktree ops hand the shared ops a `pathExists` — and the only one this process has
+// is `fs.existsSync`, which answers about THIS MACHINE and nothing else. But the `git()` executor
+// they run through transparently routes to a REMOTE git over the project's ControlMaster whenever
+// `resolveGitRemote` claims the cwd. Pairing the two would make every worktree the host lists stat
+// as missing here → `prunable: true` for all of them → every bound group struck as missing, and
+// `worktreeRemove` answering `worktreeGone` (the renderer's proof that a directory is gone: it
+// destroys the descendant terminals' tmux sessions and rewrites their persisted cwds) for a
+// worktree that is alive and well on the host.
+//
+// So the ops REFUSE for a remote repo. These tests pin the refusal, and — because a refusal that
+// leaked into the local path would silently kill the whole feature — pin that a LOCAL repo (no
+// resolver claiming it) still goes through to real git.
+import { afterEach, beforeAll, afterAll, describe, expect, it } from 'vitest'
+import { execFileSync } from 'child_process'
+import fs from 'fs'
+import os from 'os'
+import path from 'path'
+import { GitService } from './git-service'
+import { setGitRemoteResolver } from './remote-ssh/remote-git'
+
+const svc = new GitService()
+
+/** Make the resolver claim exactly `repoPath` — i.e. "this repo lives on an SSH host". */
+function claimAsRemote(repoPath: string): void {
+ setGitRemoteResolver((cwd) =>
+ cwd === repoPath ? { conn: { host: 'h', user: 'u' }, controlPath: '/tmp/cm.sock' } : undefined
+ )
+}
+
+let repo: string
+
+beforeAll(() => {
+ repo = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'nt-wt-remote-')))
+ const git = (...args: string[]): void => {
+ execFileSync('git', args, { cwd: repo, stdio: 'ignore' })
+ }
+ git('init', '-b', 'main')
+ git('config', 'user.email', '[email protected]')
+ git('config', 'user.name', 't')
+ git('commit', '--allow-empty', '-m', 'init')
+})
+
+afterAll(() => fs.rmSync(repo, { recursive: true, force: true }))
+afterEach(() => setGitRemoteResolver(null))
+
+describe('worktree ops on a LOCAL repo (no remote claims it)', () => {
+ it('still reach real git — the remote guard does not swallow the local path', async () => {
+ const listed = await svc.worktreeList(repo)
+ expect(listed.ok).toBe(true)
+ expect(listed.entries.map((e) => e.path)).toEqual([repo])
+ // The main checkout is on disk, so the `pathExists` fallback must NOT call it prunable.
+ expect(listed.entries[0].prunable).toBeFalsy()
+ })
+
+ it('a resolver that claims some OTHER cwd leaves this repo local', async () => {
+ claimAsRemote('/somewhere/else')
+ const listed = await svc.worktreeList(repo)
+ expect(listed.ok).toBe(true)
+ expect(listed.entries).toHaveLength(1)
+ })
+})
+
+describe('worktree ops on a REMOTE repo', () => {
+ it('worktreeList reports a FAILED READ, not an empty list', async () => {
+ claimAsRemote(repo)
+ const listed = await svc.worktreeList(repo)
+ // `{ ok: false }` is the whole point: an `entries: []` with `ok: true` is "there are no
+ // worktrees", which the store would act on by striking every bound group as missing.
+ expect(listed).toEqual({ ok: false, entries: [] })
+ })
+
+ it('worktreeAdd refuses (the path is computed from the LOCAL data dir)', async () => {
+ claimAsRemote(repo)
+ const r = await svc.worktreeAdd(repo, path.join(repo, '..', 'wt'), 'feat', 'main', true)
+ expect(r.ok).toBe(false)
+ expect(r.message).toMatch(/not supported in SSH projects/i)
+ })
+
+ it('worktreeMerge refuses', async () => {
+ claimAsRemote(repo)
+ const r = await svc.worktreeMerge(repo, 'feat', 'main', false)
+ expect(r.ok).toBe(false)
+ expect(r.message).toMatch(/not supported in SSH projects/i)
+ })
+
+ it('worktreeRemove refuses — and the refusal is NEVER dressed up as `worktreeGone`', async () => {
+ claimAsRemote(repo)
+ for (const pruneOnly of [false, true]) {
+ const r = await svc.worktreeRemove(repo, path.join(repo, 'wt'), true, pruneOnly)
+ expect(r.ok).toBe(false)
+ expect(r.message).toMatch(/not supported in SSH projects/i)
+ // The one field the renderer reads as proof a directory is gone (it then destroys the
+ // descendant terminals' tmux sessions and rewrites their persisted cwds). A guess about
+ // another machine's filesystem must never set it.
+ expect(r.worktreeGone).toBeUndefined()
+ }
+ })
+
+ it('refuses without touching git at all — nothing is created on disk', async () => {
+ const wt = path.join(repo, 'wt-should-not-exist')
+ claimAsRemote(repo)
+ await svc.worktreeAdd(repo, wt, 'feat', 'main', true)
+ expect(fs.existsSync(wt)).toBe(false)
+ // …and the repo is untouched: still exactly one (local) worktree once the claim is dropped.
+ setGitRemoteResolver(null)
+ const listed = await svc.worktreeList(repo)
+ expect(listed.entries.map((e) => e.path)).toEqual([repo])
+ })
+})