feat: move sessions to new worktrees

Add a root-session action that creates a generated worktree from the session directory's current branch, transfers uncommitted changes, and moves the parent session plus its descendants through OpenCode's control-plane API.

Reuse existing project/worktree topology and quick-create behavior, keep the UI non-blocking, reconcile live and global session state across directories, and roll back partial moves and failed worktree creation safely.

Split worktree bootstrap readiness into directory-created, git-ready, and setup-ready phases across web and VS Code. Session moves wait for Git readiness while existing setup-aware flows continue waiting for full setup completion, and worktree removal is serialized with active bootstrap tasks.

Expose the move only for idle root sessions, show localized progress and explanatory tooltips in the sidebar, and keep pending/ready worktree metadata synchronized with authoritative session attachments to avoid stale setup indicators.

Add coverage for control-plane payloads, session-state migration, bootstrap phase ordering and compatibility, removal races, progress metadata, and fast-ready attachment races.
This commit is contained in:
Bohdan Triapitsyn
2026-07-19 00:00:31 +03:00
parent e9d93a6744
commit 3fd6627196
31 changed files with 1322 additions and 151 deletions
@@ -1,11 +1,13 @@
import { beforeEach, describe, expect, mock, test } from 'bun:test';
import type { GitWorktreeBootstrapStatus } from '@/lib/api/types';
const bootstrapStatusCalls: string[] = [];
let bootstrapStatusResult: { status: 'pending' | 'ready' | 'failed'; error: string | null; updatedAt: number } = {
let bootstrapStatusResult: GitWorktreeBootstrapStatus = {
status: 'ready',
error: null,
updatedAt: 1,
};
let getBootstrapStatus = (): Promise<GitWorktreeBootstrapStatus> => Promise.resolve(bootstrapStatusResult);
const toastErrors: Array<{ title: string; description?: string }> = [];
mock.module('@/components/ui', () => ({
@@ -29,7 +31,7 @@ mock.module('@/contexts/runtimeAPIRegistry', () => ({
worktree: {
bootstrapStatus: (directory: string) => {
bootstrapStatusCalls.push(directory);
return Promise.resolve(bootstrapStatusResult);
return getBootstrapStatus();
},
},
},
@@ -39,7 +41,7 @@ mock.module('@/contexts/runtimeAPIRegistry', () => ({
mock.module('@/lib/gitApiHttp', () => ({
getGitWorktreeBootstrapStatus: (directory: string) => {
bootstrapStatusCalls.push(directory);
return Promise.resolve(bootstrapStatusResult);
return getBootstrapStatus();
},
}));
@@ -47,8 +49,10 @@ const {
clearWorktreeBootstrapState,
getWorktreeBootstrapState,
markWorktreeBootstrapPending,
setWorktreeBootstrapState,
startWorktreeBootstrapWatcher,
waitForWorktreeBootstrap,
waitForWorktreeGitReady,
} = await import('./worktreeBootstrap');
const waitFor = async (predicate: () => boolean): Promise<void> => {
@@ -64,6 +68,7 @@ describe('worktreeBootstrap.waitForWorktreeBootstrap', () => {
bootstrapStatusCalls.length = 0;
toastErrors.length = 0;
bootstrapStatusResult = { status: 'ready', error: null, updatedAt: 1 };
getBootstrapStatus = () => Promise.resolve(bootstrapStatusResult);
clearWorktreeBootstrapState('/repo');
clearWorktreeBootstrapState('/repo-wt');
});
@@ -82,6 +87,108 @@ describe('worktreeBootstrap.waitForWorktreeBootstrap', () => {
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
});
test('git-ready wait does not wait for setup-ready', async () => {
setWorktreeBootstrapState('/repo-wt', {
status: 'pending',
phase: 'git-ready',
error: null,
updatedAt: 1,
});
await waitForWorktreeGitReady('/repo-wt');
expect(bootstrapStatusCalls).toEqual([]);
});
test('git-ready wait remains compatible with ready responses that omit phase', async () => {
markWorktreeBootstrapPending('/repo-wt');
await waitForWorktreeGitReady('/repo-wt');
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
expect(getWorktreeBootstrapState('/repo-wt')).toEqual(bootstrapStatusResult);
});
test('dedupes concurrent waiters for the same phase', async () => {
let resolveStatus!: (status: GitWorktreeBootstrapStatus) => void;
getBootstrapStatus = () => new Promise((resolve) => {
resolveStatus = resolve;
});
markWorktreeBootstrapPending('/repo-wt');
const first = waitForWorktreeGitReady('/repo-wt');
const second = waitForWorktreeGitReady('/repo-wt');
await waitFor(() => bootstrapStatusCalls.length === 1);
resolveStatus({ status: 'pending', phase: 'git-ready', error: null, updatedAt: 2 });
await Promise.all([first, second]);
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
});
test('does not let a cleared waiter restore stale state or remove a replacement waiter', async () => {
const statusResolvers: Array<(status: GitWorktreeBootstrapStatus) => void> = [];
getBootstrapStatus = () => new Promise((resolve) => {
statusResolvers.push(resolve);
});
markWorktreeBootstrapPending('/repo-wt');
const staleWaiter = waitForWorktreeGitReady('/repo-wt');
await waitFor(() => statusResolvers.length === 1);
clearWorktreeBootstrapState('/repo-wt');
markWorktreeBootstrapPending('/repo-wt');
const replacementWaiter = waitForWorktreeGitReady('/repo-wt');
await waitFor(() => statusResolvers.length === 2);
statusResolvers[0]({ status: 'pending', phase: 'git-ready', error: null, updatedAt: 2 });
await expect(staleWaiter).rejects.toThrow('cancelled');
expect(getWorktreeBootstrapState('/repo-wt')?.phase).toBe('directory-created');
statusResolvers[1]({ status: 'pending', phase: 'git-ready', error: null, updatedAt: 3 });
await replacementWaiter;
expect(getWorktreeBootstrapState('/repo-wt')?.phase).toBe('git-ready');
expect(bootstrapStatusCalls).toEqual(['/repo-wt', '/repo-wt']);
});
test('does not regress a newer phase when concurrent polls resolve out of order', async () => {
const statusResolvers: Array<(status: GitWorktreeBootstrapStatus) => void> = [];
getBootstrapStatus = () => new Promise((resolve) => {
statusResolvers.push(resolve);
});
markWorktreeBootstrapPending('/repo-wt');
startWorktreeBootstrapWatcher('/repo-wt', { pollIntervalMs: 1000 });
await waitFor(() => statusResolvers.length === 1);
const gitReadyWaiter = waitForWorktreeGitReady('/repo-wt');
await waitFor(() => statusResolvers.length === 2);
statusResolvers[1]({ status: 'pending', phase: 'git-ready', error: null, updatedAt: 3 });
await gitReadyWaiter;
statusResolvers[0]({ status: 'pending', phase: 'directory-created', error: null, updatedAt: 2 });
await Promise.resolve();
expect(getWorktreeBootstrapState('/repo-wt')?.phase).toBe('git-ready');
clearWorktreeBootstrapState('/repo-wt');
});
test('full bootstrap wait continues through git-ready until setup-ready', async () => {
setWorktreeBootstrapState('/repo-wt', {
status: 'pending',
phase: 'git-ready',
error: null,
updatedAt: 1,
});
bootstrapStatusResult = {
status: 'ready',
phase: 'setup-ready',
error: null,
updatedAt: 2,
};
await waitForWorktreeBootstrap('/repo-wt');
expect(bootstrapStatusCalls).toEqual(['/repo-wt']);
});
test('background watcher polls pending worktrees without blocking', async () => {
markWorktreeBootstrapPending('/repo-wt');
const readyStatuses: Array<{ status: 'pending' | 'ready' | 'failed'; error: string | null; updatedAt: number }> = [];