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:
@@ -12,6 +12,7 @@ const execFileAsync = promisify(execFile);
|
||||
const gpgconfCandidates = ['gpgconf', '/opt/homebrew/bin/gpgconf', '/usr/local/bin/gpgconf'];
|
||||
let resolvedGitBinary = null;
|
||||
const worktreeBootstrapState = new Map();
|
||||
const activeWorktreeBootstrapTasks = new Map();
|
||||
const remoteExistenceCache = new Map();
|
||||
const SIMPLE_GIT_SAFE_BINARY_PATTERN = /^([a-z]:)?([a-z0-9/.\\_~-]+)$/i;
|
||||
const SIMPLE_GIT_UNSAFE_BINARY_WARNING = 'Invalid value supplied for custom binary, restricted characters must be removed';
|
||||
@@ -21,6 +22,9 @@ const gitIndexMutationQueues = new Map();
|
||||
const WORKTREE_BOOTSTRAP_PENDING = 'pending';
|
||||
const WORKTREE_BOOTSTRAP_READY = 'ready';
|
||||
const WORKTREE_BOOTSTRAP_FAILED = 'failed';
|
||||
const WORKTREE_BOOTSTRAP_PHASE_DIRECTORY_CREATED = 'directory-created';
|
||||
const WORKTREE_BOOTSTRAP_PHASE_GIT_READY = 'git-ready';
|
||||
const WORKTREE_BOOTSTRAP_PHASE_SETUP_READY = 'setup-ready';
|
||||
const WORKTREE_INDEX_LOCK_RETRY_DELAY_MS = 250;
|
||||
const WORKTREE_INDEX_LOCK_STALE_DELAY_MS = 750;
|
||||
|
||||
@@ -32,16 +36,21 @@ const toBootstrapStateKey = (directory) => {
|
||||
return path.resolve(normalized);
|
||||
};
|
||||
|
||||
const setWorktreeBootstrapState = (directory, status, error = null) => {
|
||||
const createWorktreeBootstrapState = (status, phase, error = null) => ({
|
||||
status,
|
||||
phase,
|
||||
error: typeof error === 'string' && error.trim().length > 0 ? error.trim() : null,
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
|
||||
const setWorktreeBootstrapState = (directory, status, phase, error = null) => {
|
||||
const key = toBootstrapStateKey(directory);
|
||||
if (!key) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
worktreeBootstrapState.set(key, {
|
||||
status,
|
||||
error: typeof error === 'string' && error.trim().length > 0 ? error.trim() : null,
|
||||
updatedAt: Date.now(),
|
||||
});
|
||||
const state = createWorktreeBootstrapState(status, phase, error);
|
||||
worktreeBootstrapState.set(key, state);
|
||||
return state;
|
||||
};
|
||||
|
||||
const clearWorktreeBootstrapState = (directory) => {
|
||||
@@ -52,6 +61,37 @@ const clearWorktreeBootstrapState = (directory) => {
|
||||
worktreeBootstrapState.delete(key);
|
||||
};
|
||||
|
||||
const trackWorktreeBootstrapTask = (directory, task) => {
|
||||
const key = toBootstrapStateKey(directory);
|
||||
if (!key) {
|
||||
return task;
|
||||
}
|
||||
|
||||
activeWorktreeBootstrapTasks.set(key, task);
|
||||
const clearTask = () => {
|
||||
if (activeWorktreeBootstrapTasks.get(key) === task) {
|
||||
activeWorktreeBootstrapTasks.delete(key);
|
||||
}
|
||||
};
|
||||
void task.then(clearTask, clearTask);
|
||||
return task;
|
||||
};
|
||||
|
||||
const waitForActiveWorktreeBootstrap = async (directory) => {
|
||||
const key = toBootstrapStateKey(directory);
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const task = activeWorktreeBootstrapTasks.get(key);
|
||||
if (!task) {
|
||||
return;
|
||||
}
|
||||
await task.catch(() => undefined);
|
||||
}
|
||||
};
|
||||
|
||||
const isExecutableFile = (candidate) => {
|
||||
if (typeof candidate !== 'string' || candidate.trim().length === 0) {
|
||||
return false;
|
||||
@@ -1730,8 +1770,8 @@ const queueWorktreeBootstrap = (args) => {
|
||||
ensureRemoteUrl,
|
||||
startCommand,
|
||||
} = args;
|
||||
setTimeout(() => {
|
||||
const run = async () => {
|
||||
const task = new Promise((resolve) => setTimeout(resolve, 0))
|
||||
.then(async () => {
|
||||
await populateWorktreeWithLockRecovery(directory);
|
||||
if (setUpstream) {
|
||||
await applyUpstreamConfiguration({
|
||||
@@ -1747,21 +1787,31 @@ const queueWorktreeBootstrap = (args) => {
|
||||
console.warn('Worktree upstream configuration failed:', error instanceof Error ? error.message : String(error));
|
||||
});
|
||||
}
|
||||
setWorktreeBootstrapState(
|
||||
directory,
|
||||
WORKTREE_BOOTSTRAP_PENDING,
|
||||
WORKTREE_BOOTSTRAP_PHASE_GIT_READY
|
||||
);
|
||||
await runWorktreeStartScripts(directory, projectID, startCommand).catch((error) => {
|
||||
console.warn('Worktree start script task failed:', error instanceof Error ? error.message : String(error));
|
||||
});
|
||||
setWorktreeBootstrapState(directory, WORKTREE_BOOTSTRAP_READY);
|
||||
};
|
||||
|
||||
void run().catch((error) => {
|
||||
setWorktreeBootstrapState(
|
||||
directory,
|
||||
WORKTREE_BOOTSTRAP_READY,
|
||||
WORKTREE_BOOTSTRAP_PHASE_SETUP_READY
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
setWorktreeBootstrapState(
|
||||
directory,
|
||||
WORKTREE_BOOTSTRAP_FAILED,
|
||||
WORKTREE_BOOTSTRAP_PHASE_DIRECTORY_CREATED,
|
||||
error instanceof Error ? error.message : String(error)
|
||||
);
|
||||
console.warn('Worktree bootstrap task failed:', error instanceof Error ? error.message : String(error));
|
||||
});
|
||||
}, 0);
|
||||
|
||||
trackWorktreeBootstrapTask(directory, task);
|
||||
};
|
||||
|
||||
const ensureRemoteWithUrl = async (primaryWorktree, remoteName, remoteUrl) => {
|
||||
@@ -3756,12 +3806,11 @@ async function attachGitWorktreeToCandidate(context, candidate, input = {}) {
|
||||
const upstreamRemote = String(input?.upstreamRemote || inferredUpstream?.remote || '').trim();
|
||||
const upstreamBranch = String(input?.upstreamBranch || inferredUpstream?.branch || '').trim();
|
||||
|
||||
setWorktreeBootstrapState(candidate.directory, WORKTREE_BOOTSTRAP_PENDING);
|
||||
const bootstrapStatus = worktreeBootstrapState.get(toBootstrapStateKey(candidate.directory)) ?? {
|
||||
status: WORKTREE_BOOTSTRAP_PENDING,
|
||||
error: null,
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
const bootstrapStatus = setWorktreeBootstrapState(
|
||||
candidate.directory,
|
||||
WORKTREE_BOOTSTRAP_PENDING,
|
||||
WORKTREE_BOOTSTRAP_PHASE_DIRECTORY_CREATED
|
||||
);
|
||||
|
||||
queueWorktreeBootstrap({
|
||||
directory: candidate.directory,
|
||||
@@ -3818,25 +3867,26 @@ export async function createWorktree(directory, input = {}) {
|
||||
console.warn('Failed to sync OpenCode sandbox metadata (add):', error instanceof Error ? error.message : String(error));
|
||||
}
|
||||
|
||||
setWorktreeBootstrapState(candidate.directory, WORKTREE_BOOTSTRAP_PENDING);
|
||||
const bootstrapStatus = worktreeBootstrapState.get(toBootstrapStateKey(candidate.directory)) ?? {
|
||||
status: WORKTREE_BOOTSTRAP_PENDING,
|
||||
error: null,
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
const bootstrapStatus = setWorktreeBootstrapState(
|
||||
candidate.directory,
|
||||
WORKTREE_BOOTSTRAP_PENDING,
|
||||
WORKTREE_BOOTSTRAP_PHASE_DIRECTORY_CREATED
|
||||
);
|
||||
const localBranch = mode === 'existing'
|
||||
? cleanBranchName(String(input?.branchName || input?.existingBranch || candidate.branch || '').trim())
|
||||
: candidate.branch;
|
||||
|
||||
void attachGitWorktreeToCandidate(context, candidate, input).catch((error) => {
|
||||
const task = attachGitWorktreeToCandidate(context, candidate, input).catch(async (error) => {
|
||||
setWorktreeBootstrapState(
|
||||
candidate.directory,
|
||||
WORKTREE_BOOTSTRAP_FAILED,
|
||||
WORKTREE_BOOTSTRAP_PHASE_DIRECTORY_CREATED,
|
||||
error instanceof Error ? error.message : String(error)
|
||||
);
|
||||
void cleanupFailedFastWorktreeCreate(context, candidate);
|
||||
await cleanupFailedFastWorktreeCreate(context, candidate);
|
||||
console.warn('Background worktree creation failed:', error instanceof Error ? error.message : String(error));
|
||||
});
|
||||
trackWorktreeBootstrapTask(candidate.directory, task);
|
||||
|
||||
return {
|
||||
head: '',
|
||||
@@ -3862,11 +3912,10 @@ export async function getWorktreeBootstrapStatus(directory) {
|
||||
return current;
|
||||
}
|
||||
|
||||
return {
|
||||
status: WORKTREE_BOOTSTRAP_READY,
|
||||
error: null,
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
return createWorktreeBootstrapState(
|
||||
WORKTREE_BOOTSTRAP_READY,
|
||||
WORKTREE_BOOTSTRAP_PHASE_SETUP_READY
|
||||
);
|
||||
}
|
||||
|
||||
export async function removeWorktree(directory, input = {}) {
|
||||
@@ -3875,6 +3924,8 @@ export async function removeWorktree(directory, input = {}) {
|
||||
throw new Error('Worktree directory is required');
|
||||
}
|
||||
|
||||
await waitForActiveWorktreeBootstrap(targetDirectory);
|
||||
|
||||
const context = await resolveWorktreeProjectContext(directory);
|
||||
const deleteLocalBranch = input?.deleteLocalBranch === true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user