feat(sessions): move sessions to existing worktrees

This commit is contained in:
mattv8
2026-08-27 18:42:57 -06:00
parent b8465ae133
commit 9babd19d9b
29 changed files with 1967 additions and 98 deletions
@@ -0,0 +1,485 @@
import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test';
import type { Session, SessionStatus } from '@opencode-ai/sdk/v2';
import type { State } from '@/sync/types';
import type { WorktreeMetadata } from '@/types/worktree';
import type { ProjectRef } from '@/lib/worktrees/worktreeManager';
const moveCalls: Array<{
sessionId: string;
sourceDirectory: string;
destinationDirectory: string;
moveChanges: boolean;
}> = [];
const refreshCalls: string[][] = [];
type RemoveProjectWorktreeOptions = { deleteLocalBranch: boolean };
type RemoveProjectWorktreeCall = {
project: ProjectRef;
worktree: WorktreeMetadata;
options: RemoveProjectWorktreeOptions;
};
type MoveSessionImplementation = (
session: Session,
sourceDirectory: string,
destinationDirectory: string,
moveChanges: boolean,
) => Promise<void>;
type RefreshImplementation = (directories: string[]) => Promise<void>;
type CreateQuickWorktreeOptions = { preferredName?: string; startRef?: string };
type CreateQuickWorktreeImplementation = (
project: ProjectRef,
options: CreateQuickWorktreeOptions,
) => Promise<WorktreeMetadata>;
type ResolveProjectRefImplementation = (directory: string) => ProjectRef | null;
type WaitForWorktreeGitReadyImplementation = (directory: string) => Promise<void>;
type DirectoryState = Pick<State, 'session_status'>;
type DeferredVoid = {
promise: Promise<void>;
resolve: () => void;
reject: (error: Error) => void;
};
const removeWorktreeCalls: RemoveProjectWorktreeCall[] = [];
const metadataWrites: Array<{ sessionId: string; metadata: WorktreeMetadata | null }> = [];
const latestMetadataInputs: WorktreeMetadata[] = [];
const toastSuccesses: string[] = [];
const toastErrors: Array<{ title: string; description?: string }> = [];
const directoryStates = new Map<string, DirectoryState>();
const storedMetadata = new Map<string, WorktreeMetadata | null>();
const originalConsoleWarn = console.warn;
let moveSessionImplementation: MoveSessionImplementation = async () => {};
let refreshImplementation: RefreshImplementation = async () => {};
let latestMetadataResult: WorktreeMetadata;
let createQuickWorktreeImplementation: CreateQuickWorktreeImplementation = async () => ({
path: '/created-worktree',
projectDirectory: '/repo',
branch: 'feature',
label: 'Created worktree',
worktreeStatus: 'ready',
worktreeSource: 'created-for-session',
});
let resolveProjectRefImplementation: ResolveProjectRefImplementation = () => ({ id: 'project-1', path: '/repo' });
let waitForWorktreeGitReadyImplementation: WaitForWorktreeGitReadyImplementation = async () => {};
mock.module('@/components/ui', () => ({
toast: {
success: (message: string) => {
toastSuccesses.push(message);
},
error: (title: string, options?: { description?: string }) => {
toastErrors.push({ title, description: options?.description });
},
},
}));
mock.module('@/lib/gitApi', () => ({
getGitStatus: mock(() => Promise.resolve({ current: 'feature' })),
}));
mock.module('@/lib/worktreeSessionCreator', () => ({
createQuickWorktree: mock((project: ProjectRef, options: CreateQuickWorktreeOptions) => createQuickWorktreeImplementation(project, options)),
resolveProjectRef: mock((directory: string) => resolveProjectRefImplementation(directory)),
}));
mock.module('@/lib/worktrees/worktreeBootstrap', () => ({
waitForWorktreeGitReady: mock((directory: string) => waitForWorktreeGitReadyImplementation(directory)),
}));
mock.module('@/lib/worktrees/worktreeManager', () => ({
getLatestWorktreeMetadata: (metadata: WorktreeMetadata) => {
latestMetadataInputs.push(metadata);
return latestMetadataResult;
},
removeProjectWorktree: (project: ProjectRef, worktree: WorktreeMetadata, options: RemoveProjectWorktreeOptions) => {
removeWorktreeCalls.push({ project, worktree, options });
return Promise.resolve();
},
}));
mock.module('@/stores/useGlobalSessionsStore', () => ({
refreshGlobalSessionsForDirectories: (directories: string[]) => {
refreshCalls.push(directories);
return refreshImplementation(directories);
},
}));
mock.module('@/sync/session-actions', () => ({
moveSessionToDirectory: (session: Session, sourceDirectory: string, destinationDirectory: string, moveChanges = true) => {
moveCalls.push({ sessionId: session.id, sourceDirectory, destinationDirectory, moveChanges });
return moveSessionImplementation(session, sourceDirectory, destinationDirectory, moveChanges);
},
}));
mock.module('@/sync/session-ui-store', () => ({
useSessionUIStore: {
getState: () => ({
availableWorktrees: [],
availableWorktreesByProject: new Map<string, WorktreeMetadata[]>(),
getWorktreeMetadata: (sessionId: string) => storedMetadata.get(sessionId) ?? null,
setWorktreeMetadata: (sessionId: string, metadata: WorktreeMetadata | null) => {
storedMetadata.set(sessionId, metadata);
metadataWrites.push({ sessionId, metadata });
},
}),
},
}));
mock.module('@/sync/sync-refs', () => ({
getDirectoryState: (directory: string) => directoryStates.get(directory),
}));
const {
moveSessionTreeToExistingWorktree,
startSessionTreeWorktreeMove,
} = await import('./sessionWorktreeMove');
const makeSession = (id: string, directory = '/source'): Session => ({
id,
slug: id,
projectID: 'project-1',
directory,
title: id,
version: '1',
time: {
created: 0,
updated: 0,
},
});
const makeWorktreeMetadata = (overrides: Partial<WorktreeMetadata> = {}): WorktreeMetadata => ({
path: '/destination',
projectDirectory: '/repo',
branch: 'feature',
label: 'Destination',
worktreeStatus: 'ready',
worktreeSource: 'existing',
...overrides,
});
const makeSessionStatus = (type: SessionStatus['type']): SessionStatus => {
switch (type) {
case 'busy':
return { type: 'busy' };
case 'idle':
return { type: 'idle' };
case 'retry':
return { type: 'retry', attempt: 1, message: 'retry', next: 0 };
}
};
const setStatuses = (directory: string, statuses: Record<string, State['session_status'][string]['type']>): void => {
directoryStates.set(directory, {
session_status: Object.fromEntries(
Object.entries(statuses).map(([sessionId, type]) => [sessionId, makeSessionStatus(type)]),
),
});
};
const waitFor = async (predicate: () => boolean): Promise<void> => {
for (let attempt = 0; attempt < 20; attempt += 1) {
if (predicate()) return;
await Promise.resolve();
}
throw new Error('Timed out waiting for condition');
};
const deferred = (): DeferredVoid => {
let resolve!: () => void;
let reject!: (error: Error) => void;
const promise = new Promise<void>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
};
describe('moveSessionTreeToExistingWorktree', () => {
beforeEach(() => {
moveCalls.length = 0;
refreshCalls.length = 0;
removeWorktreeCalls.length = 0;
metadataWrites.length = 0;
latestMetadataInputs.length = 0;
toastSuccesses.length = 0;
toastErrors.length = 0;
directoryStates.clear();
storedMetadata.clear();
latestMetadataResult = makeWorktreeMetadata({ label: 'Latest destination' });
moveSessionImplementation = async () => {};
refreshImplementation = async () => {};
createQuickWorktreeImplementation = async () => makeWorktreeMetadata({ path: '/created-worktree', worktreeSource: 'created-for-session' });
resolveProjectRefImplementation = () => ({ id: 'project-1', path: '/repo' });
waitForWorktreeGitReadyImplementation = async () => {};
console.warn = () => {};
});
afterEach(() => {
console.warn = originalConsoleWarn;
});
test('moves the root before descendants, only transfers changes once, and refreshes both directories', async () => {
const root = makeSession('root');
const child = makeSession('child');
const previousRootMetadata = makeWorktreeMetadata({ path: '/old-root', label: 'Old root' });
const previousChildMetadata = makeWorktreeMetadata({ path: '/old-child', label: 'Old child' });
const destination = makeWorktreeMetadata();
setStatuses('/source', { root: 'idle', child: 'idle' });
storedMetadata.set(root.id, previousRootMetadata);
storedMetadata.set(child.id, previousChildMetadata);
const result = await moveSessionTreeToExistingWorktree({
root,
descendants: [child],
sourceDirectory: '/source',
destination,
});
expect(result).toBe('/destination');
expect(moveCalls).toEqual([
{ sessionId: 'root', sourceDirectory: '/source', destinationDirectory: '/destination', moveChanges: true },
{ sessionId: 'child', sourceDirectory: '/source', destinationDirectory: '/destination', moveChanges: false },
]);
expect(metadataWrites).toEqual([
{ sessionId: 'root', metadata: latestMetadataResult },
{ sessionId: 'child', metadata: latestMetadataResult },
]);
expect(latestMetadataInputs).toEqual([destination, destination]);
expect(refreshCalls).toEqual([['/source', '/destination']]);
expect(removeWorktreeCalls).toEqual([]);
});
test('rejects a destination that normalizes to the source directory', async () => {
setStatuses('/source', { root: 'idle' });
await expect(moveSessionTreeToExistingWorktree({
root: makeSession('root'),
descendants: [],
sourceDirectory: '/source/',
destination: makeWorktreeMetadata({ path: '/source' }),
})).rejects.toThrow('Source and destination are the same');
expect(moveCalls).toEqual([]);
expect(refreshCalls).toEqual([]);
});
test('rejects a destination worktree that is not ready', async () => {
setStatuses('/source', { root: 'idle' });
await expect(moveSessionTreeToExistingWorktree({
root: makeSession('root'),
descendants: [],
sourceDirectory: '/source',
destination: makeWorktreeMetadata({ worktreeStatus: 'pending' }),
})).rejects.toThrow('Destination worktree is not ready');
expect(moveCalls).toEqual([]);
});
test('rejects when the root session is busy before setup', async () => {
setStatuses('/source', { root: 'busy' });
await expect(moveSessionTreeToExistingWorktree({
root: makeSession('root'),
descendants: [],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
})).rejects.toThrow('Session is not idle');
expect(moveCalls).toEqual([]);
});
test('rejects when any descendant is busy before setup', async () => {
const root = makeSession('root');
const child = makeSession('child');
setStatuses('/source', { root: 'idle', child: 'retry' });
await expect(moveSessionTreeToExistingWorktree({
root,
descendants: [child],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
})).rejects.toThrow('Session is not idle');
expect(moveCalls).toEqual([]);
});
test('rejects a duplicate move request while the root move is pending', async () => {
const root = makeSession('root');
const rootMove = deferred();
setStatuses('/source', { root: 'idle' });
moveSessionImplementation = async (session, sourceDirectory) => {
if (session.id === 'root' && sourceDirectory === '/source') {
return rootMove.promise;
}
};
const firstMove = moveSessionTreeToExistingWorktree({
root,
descendants: [],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
});
await waitFor(() => moveCalls.length === 1);
await expect(moveSessionTreeToExistingWorktree({
root,
descendants: [],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
})).rejects.toThrow('Session move already in progress');
rootMove.resolve();
await firstMove;
expect(moveCalls).toHaveLength(1);
});
test('rolls back completed moves in reverse order, restores previous metadata, and never removes an existing destination', async () => {
const root = makeSession('root');
const childA = makeSession('child-a');
const childB = makeSession('child-b');
const previousRootMetadata = makeWorktreeMetadata({ path: '/old-root', label: 'Old root' });
const previousChildAMetadata = makeWorktreeMetadata({ path: '/old-child-a', label: 'Old child A' });
const previousChildBMetadata = makeWorktreeMetadata({ path: '/old-child-b', label: 'Old child B' });
setStatuses('/source', { root: 'idle', 'child-a': 'idle', 'child-b': 'idle' });
storedMetadata.set(root.id, previousRootMetadata);
storedMetadata.set(childA.id, previousChildAMetadata);
storedMetadata.set(childB.id, previousChildBMetadata);
moveSessionImplementation = async (session, sourceDirectory) => {
if (session.id === 'child-b' && sourceDirectory === '/source') {
throw new Error('child-b failed');
}
};
await expect(moveSessionTreeToExistingWorktree({
root,
descendants: [childA, childB],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
})).rejects.toThrow('child-b failed');
expect(moveCalls).toEqual([
{ sessionId: 'root', sourceDirectory: '/source', destinationDirectory: '/destination', moveChanges: true },
{ sessionId: 'child-a', sourceDirectory: '/source', destinationDirectory: '/destination', moveChanges: false },
{ sessionId: 'child-b', sourceDirectory: '/source', destinationDirectory: '/destination', moveChanges: false },
{ sessionId: 'child-a', sourceDirectory: '/destination', destinationDirectory: '/source', moveChanges: false },
{ sessionId: 'root', sourceDirectory: '/destination', destinationDirectory: '/source', moveChanges: true },
]);
expect(metadataWrites).toEqual([
{ sessionId: 'root', metadata: latestMetadataResult },
{ sessionId: 'child-a', metadata: latestMetadataResult },
{ sessionId: 'child-a', metadata: previousChildAMetadata },
{ sessionId: 'root', metadata: previousRootMetadata },
]);
expect(storedMetadata.get(root.id)).toBe(previousRootMetadata);
expect(storedMetadata.get(childA.id)).toBe(previousChildAMetadata);
expect(storedMetadata.get(childB.id)).toBe(previousChildBMetadata);
expect(removeWorktreeCalls).toEqual([]);
expect(refreshCalls).toEqual([]);
});
test('reports an incomplete rollback explicitly and still does not remove the existing destination', async () => {
const root = makeSession('root');
const child = makeSession('child');
setStatuses('/source', { root: 'idle', child: 'idle' });
moveSessionImplementation = async (session, sourceDirectory) => {
if (session.id === 'child' && sourceDirectory === '/source') {
throw new Error('child failed');
}
if (session.id === 'root' && sourceDirectory === '/destination') {
throw new Error('rollback failed');
}
};
await expect(moveSessionTreeToExistingWorktree({
root,
descendants: [child],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
})).rejects.toThrow('could not be fully rolled back');
expect(removeWorktreeCalls).toEqual([]);
});
test('keeps the move successful when the post-move refresh fails', async () => {
const root = makeSession('root');
setStatuses('/source', { root: 'idle' });
refreshImplementation = async () => {
throw new Error('refresh failed');
};
const result = await moveSessionTreeToExistingWorktree({
root,
descendants: [],
sourceDirectory: '/source',
destination: makeWorktreeMetadata(),
});
expect(result).toBe('/destination');
expect(refreshCalls).toEqual([['/source', '/destination']]);
});
test('removes a newly created worktree when git-ready setup fails', async () => {
setStatuses('/source', { root: 'idle' });
waitForWorktreeGitReadyImplementation = async () => {
throw new Error('git-ready failed');
};
startSessionTreeWorktreeMove({
root: makeSession('root'),
descendants: [],
sourceDirectory: '/source',
successMessage: 'success',
failureMessage: 'failed',
});
await waitFor(() => toastErrors.length === 1);
expect(toastErrors).toEqual([{ title: 'failed', description: 'git-ready failed' }]);
expect(removeWorktreeCalls).toEqual([{
project: { id: 'project-1', path: '/repo' },
worktree: makeWorktreeMetadata({ path: '/created-worktree', worktreeSource: 'created-for-session', label: 'Destination' }),
options: { deleteLocalBranch: true },
}]);
expect(moveCalls).toEqual([]);
});
test('removes a newly created worktree when a session becomes busy before the first move', async () => {
setStatuses('/source', { root: 'idle' });
waitForWorktreeGitReadyImplementation = async () => {
setStatuses('/source', { root: 'busy' });
};
startSessionTreeWorktreeMove({
root: makeSession('root'),
descendants: [],
sourceDirectory: '/source',
successMessage: 'success',
failureMessage: 'failed',
});
await waitFor(() => toastErrors.length === 1);
expect(removeWorktreeCalls).toEqual([{
project: { id: 'project-1', path: '/repo' },
worktree: makeWorktreeMetadata({ path: '/created-worktree', worktreeSource: 'created-for-session', label: 'Destination' }),
options: { deleteLocalBranch: true },
}]);
expect(moveCalls).toEqual([]);
});
test('surfaces a pre-destination preparation failure without attempting removal', async () => {
setStatuses('/source', { root: 'idle' });
resolveProjectRefImplementation = () => null;
startSessionTreeWorktreeMove({
root: makeSession('root'),
descendants: [],
sourceDirectory: '/source',
successMessage: 'success',
failureMessage: 'failed',
});
await waitFor(() => toastErrors.length === 1);
expect(toastErrors).toEqual([{ title: 'failed', description: 'Unable to find the project for this session' }]);
expect(removeWorktreeCalls).toEqual([]);
expect(moveCalls).toEqual([]);
});
});
@@ -88,31 +88,34 @@ const rollbackMovedSessions = async (
const removeFailedWorktree = async (
project: ProjectRef,
worktree: WorktreeMetadata,
moveError: unknown,
moveError: Error,
): Promise<never> => {
try {
await removeProjectWorktree(project, worktree, { deleteLocalBranch: true });
} catch {
const message = moveError instanceof Error ? moveError.message : String(moveError);
throw new Error(`Session move failed and the new worktree could not be removed: ${message}`);
throw new Error(`Session move failed and the new worktree could not be removed: ${moveError.message}`);
}
throw moveError;
};
const moveSessionTreeToQuickWorktree = async (input: {
root: Session;
descendants: Session[];
sourceDirectory: string;
}): Promise<string> => {
const moveSessionTreeTransaction = async (
input: {
root: Session;
descendants: Session[];
sourceDirectory: string;
},
prepareDestination: () => Promise<{
directory: string;
metadata: WorktreeMetadata;
onMoveFailure?: (error: Error) => Promise<never>;
}>,
): Promise<string> => {
if (useSessionMoveState.getState().pendingSessionIds.has(input.root.id)) {
throw new Error('Session move already in progress');
}
setSessionMovePending(input.root.id, true);
try {
const project = resolveProjectRef(input.sourceDirectory);
if (!project) throw new Error('Unable to find the project for this session');
const sessions = [input.root, ...input.descendants];
const previousMetadata = new Map(
sessions.map((session) => [
@@ -122,49 +125,112 @@ const moveSessionTreeToQuickWorktree = async (input: {
);
assertSessionsIdle(sessions, input.sourceDirectory);
const sourceBranch = await resolveSourceBranch(input.sourceDirectory, project.path);
const worktree = await createQuickWorktree(project, { startRef: sourceBranch });
let destination: Awaited<ReturnType<typeof prepareDestination>> | null = null;
const moved: Session[] = [];
try {
await waitForWorktreeGitReady(worktree.path);
// Branch/status discovery and worktree creation can take long enough for a
// session to start running, so verify the whole tree again before moving.
destination = await prepareDestination();
// Setup can take long enough for one of the sessions to start running, so
// verify the whole tree again immediately before the first move.
assertSessionsIdle(sessions, input.sourceDirectory);
for (const [index, session] of sessions.entries()) {
// Transfer the checkout changes once with the root. Descendants only
// need their execution location updated.
await moveSessionToDirectory(session, input.sourceDirectory, worktree.path, index === 0);
await moveSessionToDirectory(session, input.sourceDirectory, destination.directory, index === 0);
moved.push(session);
useSessionUIStore.getState().setWorktreeMetadata(session.id, getLatestWorktreeMetadata(worktree));
useSessionUIStore.getState().setWorktreeMetadata(session.id, getLatestWorktreeMetadata(destination.metadata));
}
} catch (error) {
const moveError = error instanceof Error ? error : new Error(String(error));
const rollbackFailures = await rollbackMovedSessions(
moved,
input.root.id,
input.sourceDirectory,
worktree.path,
destination?.directory ?? input.sourceDirectory,
previousMetadata,
);
if (rollbackFailures.length > 0) {
throw new Error(`Session move partially failed and could not be fully rolled back: ${error instanceof Error ? error.message : String(error)}`);
throw new Error(`Session move partially failed and could not be fully rolled back: ${moveError.message}`);
}
return removeFailedWorktree(project, worktree, error);
if (destination?.onMoveFailure) {
return destination.onMoveFailure(moveError);
}
throw moveError;
}
try {
await refreshGlobalSessionsForDirectories([input.sourceDirectory, worktree.path]);
await refreshGlobalSessionsForDirectories([input.sourceDirectory, destination.directory]);
} catch (error) {
// Direct action updates already reconciled both stores. Keep the move
// successful if this best-effort authoritative refresh is unavailable.
console.warn('[session-worktree-move] Failed to refresh moved sessions', error);
}
return worktree.path;
return destination.directory;
} finally {
setSessionMovePending(input.root.id, false);
}
};
export const moveSessionTreeToExistingWorktree = async (input: {
root: Session;
descendants: Session[];
sourceDirectory: string;
destination: WorktreeMetadata;
}): Promise<string> => {
const normalizedSourceDirectory = normalizePath(input.sourceDirectory) ?? input.sourceDirectory;
const normalizedDestinationDirectory = normalizePath(input.destination.path) ?? input.destination.path;
if (normalizedSourceDirectory === normalizedDestinationDirectory) {
throw new Error('Source and destination are the same');
}
if (input.destination.worktreeStatus !== 'ready') {
throw new Error('Destination worktree is not ready');
}
return moveSessionTreeTransaction(input, async () => ({
directory: input.destination.path,
metadata: input.destination,
}));
};
const moveSessionTreeToQuickWorktree = async (input: {
root: Session;
descendants: Session[];
sourceDirectory: string;
}): Promise<string> => {
return moveSessionTreeTransaction(input, async () => {
const project = resolveProjectRef(input.sourceDirectory);
if (!project) throw new Error('Unable to find the project for this session');
const sourceBranch = await resolveSourceBranch(input.sourceDirectory, project.path);
const worktree = await createQuickWorktree(project, { startRef: sourceBranch });
try {
await waitForWorktreeGitReady(worktree.path);
} catch (error) {
const setupError = error instanceof Error ? error : new Error(String(error));
return removeFailedWorktree(project, worktree, setupError);
}
return {
directory: worktree.path,
metadata: worktree,
onMoveFailure: (error) => removeFailedWorktree(project, worktree, error),
};
});
};
export const startSessionTreeExistingWorktreeMove = (input: {
root: Session;
descendants: Session[];
sourceDirectory: string;
destination: WorktreeMetadata;
successMessage: string;
failureMessage: string;
}): void => {
void moveSessionTreeToExistingWorktree(input)
.then(() => toast.success(input.successMessage))
.catch((error) => toast.error(input.failureMessage, {
description: error instanceof Error ? error.message : String(error),
}));
};
export const startSessionTreeWorktreeMove = (input: {
root: Session;
descendants: Session[];
@@ -11,6 +11,7 @@ type WorktreeListEntry = {
const listCalls: string[] = [];
const listResolvers: Array<(value: WorktreeListEntry[]) => void> = [];
const listRejecters: Array<(reason: Error) => void> = [];
const createPayloads: unknown[] = [];
const validatePayloads: unknown[] = [];
const createdWorktree = {
@@ -78,8 +79,9 @@ mock.module('@/lib/gitApi', () => ({
worktree: {
list: (directory: string) => {
listCalls.push(directory);
return new Promise<WorktreeListEntry[]>((resolve) => {
return new Promise<WorktreeListEntry[]>((resolve, reject) => {
listResolvers.push(resolve);
listRejecters.push((reason: Error) => reject(reason));
});
},
create: mock((_directory: string, payload: unknown) => {
@@ -118,6 +120,7 @@ describe('worktreeManager list invalidation', () => {
beforeEach(() => {
listCalls.length = 0;
listResolvers.length = 0;
listRejecters.length = 0;
createPayloads.length = 0;
validatePayloads.length = 0;
bootstrapWatcherCalls.length = 0;
@@ -152,6 +155,85 @@ describe('worktreeManager list invalidation', () => {
expect(result.map((entry) => entry.path)).toEqual(['/repo-feature']);
});
test('forced refresh bypasses a fresh cached result', async () => {
const project = { id: 'project-force-cache', path: '/repo-force-cache' };
const initialListing = listProjectWorktrees(project);
await waitForListCallCount(1);
listResolvers[0]([]);
const initialResult = await initialListing;
expect(initialResult).toEqual([]);
const cachedResult = await listProjectWorktrees(project);
expect(cachedResult).toEqual([]);
expect(listCalls).toEqual(['/repo-force-cache']);
const forcedListing = listProjectWorktrees(project, { force: true });
await waitForListCallCount(2);
listResolvers[1]([createdWorktree]);
const forcedResult = await forcedListing;
expect(forcedResult.map((entry) => entry.path)).toEqual(['/repo-feature']);
expect(listCalls).toEqual(['/repo-force-cache', '/repo-force-cache']);
const refreshedCachedResult = await listProjectWorktrees(project);
expect(refreshedCachedResult.map((entry) => entry.path)).toEqual(['/repo-feature']);
expect(listCalls).toEqual(['/repo-force-cache', '/repo-force-cache']);
});
test('forced refresh starts a new request instead of joining an older in-flight list', async () => {
const project = { id: 'project-force-inflight', path: '/repo-force-inflight' };
const initialListing = listProjectWorktrees(project);
await waitForListCallCount(1);
const forcedListing = listProjectWorktrees(project, { force: true });
await waitForListCallCount(2);
listResolvers[1]([createdWorktree]);
const forcedResult = await forcedListing;
expect(forcedResult.map((entry) => entry.path)).toEqual(['/repo-feature']);
listResolvers[0]([]);
await waitForListCallCount(3);
listResolvers[2]([createdWorktree]);
const initialResult = await initialListing;
expect(initialResult.map((entry) => entry.path)).toEqual(['/repo-feature']);
expect(listCalls).toEqual([
'/repo-force-inflight',
'/repo-force-inflight',
'/repo-force-inflight',
]);
});
test('older completions do not replace a forced refresh result with stale topology', async () => {
const project = { id: 'project-force-stale', path: '/repo-force-stale' };
void listProjectWorktrees(project);
await waitForListCallCount(1);
const forcedListing = listProjectWorktrees(project, { force: true });
await waitForListCallCount(2);
listResolvers[1]([createdWorktree]);
const forcedResult = await forcedListing;
expect(forcedResult.map((entry) => entry.path)).toEqual(['/repo-feature']);
listResolvers[0]([{ path: '/repo-stale', branch: 'stale', name: 'stale' }]);
await waitForListCallCount(3);
const cachedResult = await listProjectWorktrees(project);
expect(cachedResult.map((entry) => entry.path)).toEqual(['/repo-feature']);
});
test('rejects when git worktree listing fails', async () => {
const project = { id: 'project-force-failure', path: '/repo-force-failure' };
const listing = listProjectWorktrees(project);
await waitForListCallCount(1);
listRejecters[0](new Error('git failed'));
await expect(listing).rejects.toThrow('git failed');
});
test('marks fast-created worktrees pending until bootstrap settles', async () => {
const metadata = await createWorktree({ id: 'project-1', path: '/repo' }, {
preferredName: 'feature',
@@ -389,6 +471,7 @@ describe('worktreeManager fork remote payload wiring', () => {
beforeEach(() => {
listCalls.length = 0;
listResolvers.length = 0;
listRejecters.length = 0;
createPayloads.length = 0;
validatePayloads.length = 0;
bootstrapWatcherCalls.length = 0;
@@ -374,7 +374,7 @@ export const partitionWorktreesByRegisteredProject = (
// Cache worktree listings to avoid repeated git worktree list + rev-parse calls
const _worktreeListCache = new Map<string, { value: WorktreeMetadata[]; at: number }>();
const _worktreeListInflight = new Map<string, Promise<WorktreeMetadata[]>>();
const _worktreeListInflight = new Map<string, { generation: number; promise: Promise<WorktreeMetadata[]> }>();
const _worktreeListGeneration = new Map<string, number>();
const WORKTREE_LIST_CACHE_TTL = 30_000; // 30 seconds
@@ -391,7 +391,7 @@ const readProjectWorktrees = async (projectDirectory: string): Promise<WorktreeM
const metadataProjectDirectory = await resolveProjectRoot(projectDirectory).catch(() => projectDirectory);
const normalizedProjectDirectory = normalizePath(projectDirectory);
const worktrees = await git.worktree.list(projectDirectory).catch(() => []);
const worktrees = await git.worktree.list(projectDirectory);
const results: WorktreeMetadata[] = worktrees
.filter((entry) => typeof entry.path === 'string' && entry.path.trim().length > 0)
.map((entry) => {
@@ -424,38 +424,48 @@ const readProjectWorktrees = async (projectDirectory: string): Promise<WorktreeM
});
};
const readStableProjectWorktrees = async (projectDirectory: string): Promise<WorktreeMetadata[]> => {
const readStableProjectWorktrees = async (
projectDirectory: string,
minimumGeneration = getWorktreeListGeneration(projectDirectory),
): Promise<WorktreeMetadata[]> => {
while (true) {
const generation = getWorktreeListGeneration(projectDirectory);
const worktrees = await readProjectWorktrees(projectDirectory);
if (generation === getWorktreeListGeneration(projectDirectory)) {
if (generation >= minimumGeneration && generation === getWorktreeListGeneration(projectDirectory)) {
_worktreeListCache.set(projectDirectory, { value: worktrees, at: Date.now() });
return worktrees;
}
}
};
export async function listProjectWorktrees(project: ProjectRef): Promise<WorktreeMetadata[]> {
export async function listProjectWorktrees(project: ProjectRef, options?: { force?: boolean }): Promise<WorktreeMetadata[]> {
const projectDirectory = normalizePath(project.path);
const force = options?.force === true;
if (force) {
invalidateWorktreeList(projectDirectory);
}
const generation = getWorktreeListGeneration(projectDirectory);
// Return cached if fresh
const cached = _worktreeListCache.get(projectDirectory);
if (cached && Date.now() - cached.at < WORKTREE_LIST_CACHE_TTL) {
if (!force && cached && Date.now() - cached.at < WORKTREE_LIST_CACHE_TTL) {
return cached.value;
}
// Dedup in-flight requests
const inflight = _worktreeListInflight.get(projectDirectory);
if (inflight) return inflight;
if (inflight && inflight.generation === generation) return inflight.promise;
const promise = readStableProjectWorktrees(projectDirectory).finally(() => {
if (_worktreeListInflight.get(projectDirectory) === promise) {
const promise = readStableProjectWorktrees(projectDirectory, generation).finally(() => {
if (_worktreeListInflight.get(projectDirectory)?.promise === promise) {
_worktreeListInflight.delete(projectDirectory);
}
});
_worktreeListInflight.set(projectDirectory, promise);
_worktreeListInflight.set(projectDirectory, { generation, promise });
return promise;
}