From 3fa4f63ae593eb2015328f0ef2a2fa5673d88bf9 Mon Sep 17 00:00:00 2001
From: Luke Parker <10430890+Hona@users.noreply.github.com>
Date: Wed, 4 Nov 2026 23:18:11 -0600
Subject: [PATCH] fix(app): move sessions to new worktrees through one state
machine and recover orphan worktrees (#61948)
---
.../e2e/regression/session-summary.spec.ts | 124 ++++
packages/gui-plugins/src/details/i18n/en.ts | 6 +
.../src/details/location-workspace.tsx | 14 +-
packages/gui-plugins/src/details/paths.ts | 16 +-
packages/gui-plugins/src/details/renderer.tsx | 5 +-
.../src/details/workspace-menu.tsx | 499 +++++++++++---
.../details/workspace-move-machine.test.ts | 498 ++++++++++++++
.../src/details/workspace-move-machine.ts | 643 ++++++++++++++++++
8 files changed, 1712 insertions(+), 93 deletions(-)
create mode 100644 packages/gui-plugins/src/details/workspace-move-machine.test.ts
create mode 100644 packages/gui-plugins/src/details/workspace-move-machine.ts
diff --git a/packages/app/e2e/regression/session-summary.spec.ts b/packages/app/e2e/regression/session-summary.spec.ts
index 55a909baf42..b9b25f978b4 100644
--- a/packages/app/e2e/regression/session-summary.spec.ts
+++ b/packages/app/e2e/regression/session-summary.spec.ts
@@ -261,6 +261,115 @@ test("the details follow the routed session when it moves to another worktree",
await expect(location("feature")).toHaveCount(0)
})
+test("a new worktree that does not receive the session offers Retry and Delete worktree after the menu closes", async ({
+ page,
+}) => {
+ const created = "C:/OpenCode/worktrees/quiet-river"
+ const creates: unknown[] = []
+ const removes: unknown[] = []
+ const moves: unknown[] = []
+ const answers = ["fail", "ok", "fail"]
+
+ const view = await openSession(page, {
+ name: "WorktreeRecovery",
+ onWorktreeCreate: (input) => {
+ creates.push(input)
+
+ return { status: 200, body: { directory: created } }
+ },
+ onWorktreeRemove: (input) => void removes.push(input),
+ })
+
+ await page.route(`**/api/session/${view.session.id}/move`, (route) => {
+ if (route.request().method() !== "POST") return route.fallback()
+ moves.push(route.request().postDataJSON())
+
+ if (answers.shift() === "fail")
+ return route.fulfill({ status: 400, headers: cors, json: { message: "Session is locked" } })
+
+ return route.fulfill({ status: 204, headers: cors })
+ })
+ const dialog = page.getByRole("dialog", { name: "Session not moved", exact: true })
+ const retry = dialog.getByRole("button", { name: "Retry", exact: true })
+
+ // The location menu closes as the item is picked; the move and its recovery outlive it.
+ await moveToNewWorktree(page)
+ await expect(dialog).toContainText("The new worktree quiet-river is ready")
+ await expect(page.locator('[data-slot="session-location-card"]')).toBeHidden()
+ await expect(retry).toBeFocused()
+ expect(moves).toEqual([{ directory: created }])
+ await page.screenshot({ path: test.info().outputPath("worktree-recovery.png"), animations: "disabled" })
+
+ // Retry moves into the worktree already created, without creating another.
+ await retry.click()
+ await expect(dialog).toBeHidden()
+ expect(moves).toEqual([{ directory: created }, { directory: created }])
+ expect(creates).toHaveLength(1)
+
+ await moveToNewWorktree(page)
+ await expect(dialog).toBeVisible()
+ await dialog.getByRole("button", { name: "Delete worktree", exact: true }).click()
+ await expect(dialog).toBeHidden()
+ expect(creates).toHaveLength(2)
+ expect(removes).toEqual([expect.objectContaining({ directory: created, force: true })])
+})
+
+test("a session that starts running while its new worktree is created stays put until Retry", async ({ page }) => {
+ const created = "C:/OpenCode/worktrees/calm-lake"
+ const moves: unknown[] = []
+
+ const view = await openSession(page, {
+ name: "WorktreeRunning",
+ onWorktreeCreate: () => ({ status: 200, body: { directory: created } }),
+ })
+
+ await page.route(`**/api/session/${view.session.id}/move`, (route) => {
+ if (route.request().method() !== "POST") return route.fallback()
+ moves.push(route.request().postDataJSON())
+
+ return route.fulfill({ status: 204, headers: cors })
+ })
+ const create = await holdRoute(page, /\/api\/worktree(?:\?|$)/, { method: "POST" })
+ const dialog = page.getByRole("dialog", { name: "Session not moved", exact: true })
+ const retry = dialog.getByRole("button", { name: "Retry", exact: true })
+ const composer = page.locator('[data-component="session-composer-dock"]')
+
+ await moveToNewWorktree(page)
+ await create.arrived
+ await view.push([
+ {
+ id: "evt_worktree_running",
+ created: 1700000002000,
+ type: "session.execution.started",
+ durable: { aggregateID: view.session.id, seq: 1, version: 1 },
+ data: { sessionID: view.session.id },
+ },
+ ])
+ await expect(composer.getByRole("button", { name: "Stop", exact: true })).toBeEnabled()
+ create.release()
+
+ // The run keeps its checkout: nothing moves, and Retry waits for the run to end.
+ await expect(dialog).toContainText("The session is running. Try again once it finishes.")
+ await expect(retry).toBeDisabled()
+ await expect(dialog.getByRole("button", { name: "Keep worktree", exact: true })).toBeFocused()
+ expect(moves).toEqual([])
+ await page.screenshot({ path: test.info().outputPath("worktree-running.png"), animations: "disabled" })
+
+ await view.push([
+ {
+ id: "evt_worktree_idle",
+ created: 1700000003000,
+ type: "session.execution.succeeded",
+ durable: { aggregateID: view.session.id, seq: 2, version: 1 },
+ data: { sessionID: view.session.id },
+ },
+ ])
+ await expect(retry).toBeEnabled()
+ await retry.click()
+ await expect(dialog).toBeHidden()
+ expect(moves).toEqual([{ directory: created }])
+})
+
for (const direction of ["ltr", "rtl"] as const) {
test(`summary overlays the view and submenus follow ${direction}`, async ({ page }) => {
await page.setViewportSize({ width: 1440, height: 900 })
@@ -867,6 +976,21 @@ test("long MCP lists stay in the viewport in a real RTL locale", async ({ page }
await expect(menu).toBeHidden()
})
+// Picks New worktree from the location menu of a session in its project's root checkout.
+async function moveToNewWorktree(page: Page) {
+ const location = page.locator('[data-slot="session-location-card"]').getByRole("menuitem", {
+ name: "Local repository",
+ exact: true,
+ })
+
+ await page.locator('[data-slot="session-location-trigger"]').click()
+ await location.click()
+ await page
+ .getByRole("menu", { name: "Local repository", exact: true })
+ .getByRole("menuitem", { name: "New worktree", exact: true })
+ .click()
+}
+
// Moves the pointer straight across from a submenu's trigger into the open submenu, so it never crosses sibling items.
async function enter(page: Page, trigger: Locator, menu: Locator) {
await expect(menu).toBeVisible()
diff --git a/packages/gui-plugins/src/details/i18n/en.ts b/packages/gui-plugins/src/details/i18n/en.ts
index 9b2969a4746..e69e53dc20b 100644
--- a/packages/gui-plugins/src/details/i18n/en.ts
+++ b/packages/gui-plugins/src/details/i18n/en.ts
@@ -6,6 +6,12 @@ export default {
"move.title": "Move to worktree",
"move.menu": "Move session to",
"move.failed": "Failed to move session",
+ "move.running": "The session is running. Try again once it finishes.",
+ "move.removeFailed": "Failed to delete worktree",
+ "move.recovery.title": "Session not moved",
+ "move.recovery.description": "The new worktree {{name}} is ready, but the session is still in its current location.",
+ "move.recovery.keep": "Keep worktree",
+ "move.recovery.remove": "Delete worktree",
server: "Extensions",
mcp: "MCP",
"mcp.empty": "No MCP servers configured",
diff --git a/packages/gui-plugins/src/details/location-workspace.tsx b/packages/gui-plugins/src/details/location-workspace.tsx
index 7f079197e02..29df5ae64ad 100644
--- a/packages/gui-plugins/src/details/location-workspace.tsx
+++ b/packages/gui-plugins/src/details/location-workspace.tsx
@@ -2,9 +2,11 @@ import { Icon } from "@opencode/ui/icon"
import { Menu } from "@opencode/ui/menu"
import { Show } from "solid-js"
import type { BuiltinSlotMap } from "@opencode/plugin/gui/builtin"
-import { SessionWorkspaceMenu } from "./workspace-menu"
+import { SessionWorkspaceMenu, type WorkspaceMoveControllers } from "./workspace-menu"
-export default function SessionLocationWorkspace(props: BuiltinSlotMap["session.location.workspace"]) {
+export default function SessionLocationWorkspace(
+ props: BuiltinSlotMap["session.location.workspace"] & { moves: WorkspaceMoveControllers },
+) {
const location = () => (
<>