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:
@@ -192,6 +192,115 @@ export function mirrorSessionIntoLiveStores(session: Session, directory?: string
|
||||
updateLiveSession(session)
|
||||
}
|
||||
|
||||
function moveRecordEntries<T>(
|
||||
source: Record<string, T>,
|
||||
destination: Record<string, T>,
|
||||
keys: Iterable<string>,
|
||||
): { source: Record<string, T>; destination: Record<string, T> } {
|
||||
let nextSource = source
|
||||
let nextDestination = destination
|
||||
|
||||
for (const key of keys) {
|
||||
if (!Object.prototype.hasOwnProperty.call(source, key)) continue
|
||||
if (nextSource === source) nextSource = { ...source }
|
||||
if (nextDestination === destination) nextDestination = { ...destination }
|
||||
nextDestination[key] = source[key]
|
||||
delete nextSource[key]
|
||||
}
|
||||
|
||||
return { source: nextSource, destination: nextDestination }
|
||||
}
|
||||
|
||||
function reconcileSessionMove(
|
||||
session: Session,
|
||||
sourceDirectory: string,
|
||||
destinationDirectory: string,
|
||||
): Session {
|
||||
const stores = _childStores
|
||||
const sourceStore = stores?.getChild(sourceDirectory)
|
||||
const destinationStore = stores?.ensureChild(destinationDirectory, { bootstrap: false })
|
||||
const sourceState = sourceStore?.getState()
|
||||
const destinationState = destinationStore?.getState()
|
||||
const liveSession = sourceState?.session.find((candidate) => candidate.id === session.id) ?? session
|
||||
const movedSession = { ...liveSession, directory: destinationDirectory } as Session
|
||||
|
||||
if (!destinationStore || !destinationState || sourceStore === destinationStore) {
|
||||
return movedSession
|
||||
}
|
||||
|
||||
const destinationSessionIndex = destinationState.session.findIndex((candidate) => candidate.id === session.id)
|
||||
const destinationSessions = [...destinationState.session]
|
||||
if (destinationSessionIndex === -1) destinationSessions.push(movedSession)
|
||||
else destinationSessions[destinationSessionIndex] = movedSession
|
||||
|
||||
if (!sourceStore || !sourceState) {
|
||||
destinationStore.setState({
|
||||
session: destinationSessions,
|
||||
sessionTotal: destinationSessionIndex === -1
|
||||
? destinationState.sessionTotal + 1
|
||||
: destinationState.sessionTotal,
|
||||
})
|
||||
return movedSession
|
||||
}
|
||||
|
||||
const sourceContainsSession = sourceState.session.some((candidate) => candidate.id === session.id)
|
||||
const status = moveRecordEntries(sourceState.session_status, destinationState.session_status, [session.id])
|
||||
const diffs = moveRecordEntries(sourceState.session_diff, destinationState.session_diff, [session.id])
|
||||
const todos = moveRecordEntries(sourceState.todo, destinationState.todo, [session.id])
|
||||
const permissions = moveRecordEntries(sourceState.permission, destinationState.permission, [session.id])
|
||||
const questions = moveRecordEntries(sourceState.question, destinationState.question, [session.id])
|
||||
const messages = moveRecordEntries(sourceState.message, destinationState.message, [session.id])
|
||||
const messageIds = sourceState.message[session.id]?.map((message) => message.id) ?? []
|
||||
const parts = moveRecordEntries(sourceState.part, destinationState.part, messageIds)
|
||||
|
||||
sourceStore.setState({
|
||||
session: sourceState.session.filter((candidate) => candidate.id !== session.id),
|
||||
sessionTotal: sourceContainsSession ? Math.max(0, sourceState.sessionTotal - 1) : sourceState.sessionTotal,
|
||||
session_status: status.source,
|
||||
session_diff: diffs.source,
|
||||
todo: todos.source,
|
||||
permission: permissions.source,
|
||||
question: questions.source,
|
||||
message: messages.source,
|
||||
part: parts.source,
|
||||
})
|
||||
destinationStore.setState({
|
||||
session: destinationSessions,
|
||||
sessionTotal: destinationSessionIndex === -1
|
||||
? destinationState.sessionTotal + 1
|
||||
: destinationState.sessionTotal,
|
||||
session_status: status.destination,
|
||||
session_diff: diffs.destination,
|
||||
todo: todos.destination,
|
||||
permission: permissions.destination,
|
||||
question: questions.destination,
|
||||
message: messages.destination,
|
||||
part: parts.destination,
|
||||
})
|
||||
|
||||
return movedSession
|
||||
}
|
||||
|
||||
export async function moveSessionToDirectory(
|
||||
session: Session,
|
||||
sourceDirectory: string,
|
||||
destinationDirectory: string,
|
||||
moveChanges = true,
|
||||
): Promise<void> {
|
||||
const result = await opencodeClient.getSdkClient().experimental.controlPlane.moveSession({
|
||||
sessionID: session.id,
|
||||
destination: { directory: destinationDirectory },
|
||||
moveChanges,
|
||||
})
|
||||
assertSdkSuccess(result, "Move session")
|
||||
|
||||
const moved = reconcileSessionMove(session, sourceDirectory, destinationDirectory)
|
||||
|
||||
registerSessionDirectory(session.id, destinationDirectory)
|
||||
useGlobalSessionsStore.getState().upsertSession(moved)
|
||||
useSessionUIStore.getState().setSessionDirectory(session.id, destinationDirectory)
|
||||
}
|
||||
|
||||
function dir() {
|
||||
return _getDirectory() || undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user