fix(worktrees): a worktree whose folder was deleted can still be removed

When a managed worktree's folder is deleted by hand (a user in a file
manager, an agent's rm -rf), git keeps its record and lists it as
prunable. Codeman then had no way out: the dialog showed the row as
"missing" with no action, DELETE answered "No worktree named ...", and
the name could never be used again (the branch still existed, and with
another branch git refused "a missing but already registered
worktree"). Reproduced on git 2.43; git's own `git worktree remove` on
the missing path drops the record with exit 0.

Removal (only removal, never a session start) now accepts a worktree
whose folder is gone when git lists it at exactly the managed path, as
prunable or locked, and no component on the way is a symlink or a file.
The live-session and lock refusals still apply. The dialog offers
Remove... (not Open) on such a row, with a note saying what it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-10-14 05:34:49 +02:00
1 parent 0ce6e09725
commit 64fea4ff84
10 files changed
+150 -18

No files matched your search

+46
View File
@@ -380,4 +380,50 @@ describe('git worktrees on a real repository', () => {
expect(r).toMatchObject({ ok: false, code: 'CONFLICT' });
if (!r.ok) expect(r.message).toContain('agent busy');
});
it('removes a worktree whose folder was deleted by hand, so its name can be used again', async () => {
const made = await createCaseWorktree(repo, { name: 'gone' });
if (!made.ok) throw new Error(made.message);
rmSync(made.worktree.path, { recursive: true, force: true });
const listed = await listCaseWorktrees(repo);
expect(listed.ok && listed.worktrees.find((w) => w.name === 'gone')).toMatchObject({
managed: true,
prunable: true,
});
// A session never starts in it; only removal accepts a missing folder.
expect(await resolveManagedWorktree(repo, 'gone')).toMatchObject({ ok: false, code: 'NOT_FOUND' });
// Before the fix this was NOT_FOUND, and both ways of reusing the name failed.
const r = await removeCaseWorktree(repo, { name: 'gone', deleteBranch: true, isInUse: () => false });
expect(r).toMatchObject({ ok: true, removed: { name: 'gone', branch: 'worktree-gone', branchDeleted: true } });
expect(git(repoDir, 'worktree', 'list', '--porcelain')).not.toContain('gone');
expect((await createCaseWorktree(repo, { name: 'gone' })).ok).toBe(true);
});
it('refuses a missing worktree that a live session still names, or that is locked', async () => {
const made = await createCaseWorktree(repo, { name: 'held' });
if (!made.ok) throw new Error(made.message);
git(repoDir, 'worktree', 'lock', '--reason', 'on a removable disk', made.worktree.path);
rmSync(made.worktree.path, { recursive: true, force: true });
expect(await removeCaseWorktree(repo, { name: 'held', isInUse: () => true })).toMatchObject({
ok: false,
code: 'CONFLICT',
});
const locked = await removeCaseWorktree(repo, { name: 'held', isInUse: () => false });
expect(locked).toMatchObject({ ok: false, code: 'CONFLICT' });
if (!locked.ok) expect(locked.message).toContain('removable disk');
});
it('never follows a symlink on the way to a missing worktree', async () => {
const made = await createCaseWorktree(repo, { name: 'via' });
if (!made.ok) throw new Error(made.message);
// The whole worktrees folder replaced by a link to somewhere else.
const elsewhere = join(dir, 'elsewhere');
mkdirSync(elsewhere);
rmSync(join(repoDir, '.claude', 'worktrees'), { recursive: true, force: true });
symlinkSync(elsewhere, join(repoDir, '.claude', 'worktrees'));
expect(await removeCaseWorktree(repo, { name: 'via', isInUse: () => false })).toMatchObject({
ok: false,
code: 'INVALID_INPUT',
});
});
});
+20
View File
@@ -182,6 +182,26 @@ describe('worktree routes: happy path', () => {
expect(readFileSync(join(wtPath, 'wip.txt'), 'utf-8')).toBe('work');
});
it('removes a worktree whose folder was deleted by hand and frees its name', async () => {
await create({ name: 'gone' });
const wtPath = join(repo, '.claude', 'worktrees', 'gone');
await rm(wtPath, { recursive: true, force: true });
const row = (await list()).json().data.worktrees.find((w: { name?: string }) => w.name === 'gone');
expect(row).toMatchObject({ managed: true, prunable: true });
// A session cannot start in it.
const qs = await h.app.inject({
method: 'POST',
url: '/api/quick-start',
payload: { caseName: 'wtcase', mode: 'shell', worktree: 'gone' },
});
expect(qs.statusCode).toBe(404);
const res = await remove('gone', '?deleteBranch=true');
expect(res.statusCode).toBe(200);
expect(res.json().data.removed).toMatchObject({ name: 'gone', branchDeleted: true });
expect(git(repo, 'worktree', 'list')).not.toContain('gone');
expect((await create({ name: 'gone' })).statusCode).toBe(201);
});
it('lets a POST /api/sessions create path use the worktree as workingDir', async () => {
await create({ name: 'viasessions' });
const wtPath = join(repo, '.claude', 'worktrees', 'viasessions');
+1
View File
@@ -118,6 +118,7 @@ describe('the git worktree UI in zh-CN', () => {
'Start a session in this worktree',
'Remove…',
'git worktree remove refuses while the worktree has uncommitted or untracked files. Codeman never forces it.',
'Its folder is gone. Removing it clears the record git keeps of it, so the name can be used again.',
'Also delete its branch (git branch -d keeps a branch that is not merged)',
'Remove worktree',
'Cancel',
+30
View File
@@ -13,6 +13,7 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import vm from 'node:vm';
import { JSDOM } from 'jsdom';
import { describe, expect, it, vi } from 'vitest';
const SESSION_UI = readFileSync(resolve(import.meta.dirname, '../src/web/public/session-ui.js'), 'utf8');
@@ -149,6 +150,35 @@ describe('launching in a worktree', () => {
expect(app._runMode).toBe('claude');
});
it('a worktree whose folder is gone offers Remove but not Open, with a note saying what Remove does', () => {
const dom = new JSDOM('<!doctype html><body></body>');
const CodemanApp = function CodemanApp(this: any) {};
const context = vm.createContext({
CodemanApp,
localStorage: { getItem: () => null, setItem: () => {} },
document: dom.window.document,
console,
setTimeout,
});
vm.runInContext(SESSION_UI, context, { filename: 'session-ui.js' });
const app = new (CodemanApp as any)();
app._shortenHomePath = (p: string) => p;
const row = (wt: Record<string, unknown>) =>
app._buildWorktreeRow({ branch: 'worktree-x', path: '/r/.claude/worktrees/x', name: 'x', ...wt });
const buttons = (el: HTMLElement) => [...el.querySelectorAll('button')].map((b) => b.textContent);
expect(buttons(row({ managed: true, prunable: false }))).toEqual(['Open', 'Remove…']);
expect(buttons(row({ managed: true, prunable: true }))).toEqual(['Remove…']);
expect(buttons(row({ managed: false, prunable: true }))).toEqual([]);
app._worktreeRemoveConfirm = 'x';
const missingNote = row({ managed: true, prunable: true }).querySelector('.worktree-remove-note')!.textContent;
expect(missingNote).toContain('folder is gone');
const liveNote = row({ managed: true, prunable: false }).querySelector('.worktree-remove-note')!.textContent;
expect(liveNote).toContain('never forces');
dom.window.close();
});
it('closing offers worktree removal only for a session in a managed worktree', () => {
const { app } = load(async () => json({}));
expect(app._managedWorktreeOfSession({ workingDir: WT.path })).toEqual(WT);