From bf0a962b014f6ad3247e5279c6c399b70bd47669 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 22:45:12 -0800 Subject: [PATCH 1/2] fix(cloud): RA-11 Restart on a paired pane sleeps that pane, then wakes it; never closes a tab m4air run 2: a plain stop (keepHistory false) is a close, and the host dropped the whole Claude tab, so the reconnect had nowhere to relaunch. The stop is now the reversible one the pane-sleep path uses (terminal.stopExact, keepHistory + targetOnly): the leaf stays as a sleeping surface and the reconnect wakes it with --resume. If the pane is gone anyway, the attempt ends with a toast naming the workspace, 'Couldn't restart Claude in {workspace}.' with Open workspace, instead of vanishing with the strip. Tests assert the same tab and leaf before and after, the reversible single-target stop, and the toast. --- .../CloudRunningStripPortal.tsx | 2 + .../cloud-strip-restart-run.test.ts | 5 + src/renderer/src/i18n/locales/en.json | 4 +- .../cloud-agent-restart-pane-watch.test.ts | 101 ++++++++++++++++++ .../cloud-agent-restart-pane-watch.ts | 74 +++++++++++++ .../cloud-pane-restart-stop.test.ts | 49 +++++++++ .../lib/orca-cloud/cloud-pane-restart-stop.ts | 6 +- 7 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.test.ts create mode 100644 src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.ts create mode 100644 src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.test.ts diff --git a/src/renderer/src/components/agent-sign-in-strip/CloudRunningStripPortal.tsx b/src/renderer/src/components/agent-sign-in-strip/CloudRunningStripPortal.tsx index c7330e102f8..cd5f4174f43 100644 --- a/src/renderer/src/components/agent-sign-in-strip/CloudRunningStripPortal.tsx +++ b/src/renderer/src/components/agent-sign-in-strip/CloudRunningStripPortal.tsx @@ -37,6 +37,7 @@ import { cloudStripWidth, type CloudStripActions } from './CloudAgentSignInStrip' +import { watchCloudAgentRestartPane } from '@/lib/orca-cloud/cloud-agent-restart-pane-watch' import { cloudRestartAttemptModel, cloudRunningStripModel } from './cloud-running-strip-model' function openAccounts(): void { @@ -109,6 +110,7 @@ export function CloudRunningStripPortal({ beginCloudAgentRestart(paneKey, { ...args, ptyId }, () => useAppStore.getState().queuePaneAccountRestarts([ptyId], args.agent) ) + watchCloudAgentRestartPane(paneKey, worktreeId, args.agent) } // A Restart in flight (or failed) owns the strip even while the pane has no agent status. if (attempt && cloud) { diff --git a/src/renderer/src/components/terminal-pane/account-restart/cloud-strip-restart-run.test.ts b/src/renderer/src/components/terminal-pane/account-restart/cloud-strip-restart-run.test.ts index d3a29a1ce6e..f104d815097 100644 --- a/src/renderer/src/components/terminal-pane/account-restart/cloud-strip-restart-run.test.ts +++ b/src/renderer/src/components/terminal-pane/account-restart/cloud-strip-restart-run.test.ts @@ -242,6 +242,11 @@ describe('RA-11 Restart on a paired (remote) pane', () => { { focus: false } ) expect(cloudAgentRestartAttempt(PANE_KEY, 0)?.launchedAt).not.toBeNull() + // Never closes a tab or pane: the same tab and leaf are there after the restart. + const state = useAppStore.getState() + expect(state.tabsByWorktree['wt-1']?.map((t) => t.id)).toEqual([TAB, OTHER_TAB]) + expect(state.terminalLayoutsByTabId[TAB]?.root).toEqual({ type: 'leaf', leafId: LEAF }) + expect(state.terminalLayoutsByTabId[OTHER_TAB]?.ptyIdsByLeafId).toEqual({ [OTHER_LEAF]: OTHER }) }) it('shows the failure and never reconnects when the host terminal could not be stopped', async () => { diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 1a4c6bca5ec..272fde1bc26 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -28062,7 +28062,9 @@ "restartingResume": "Picking up where you left off.", "restartFailed": "Couldn’t restart {{agent}}", "tryAgainDetail": "Try again.", - "restartFailedAnnounce": "Couldn’t restart {{agent}}. Try again." + "restartFailedAnnounce": "Couldn’t restart {{agent}}. Try again.", + "restartFailedIn": "Couldn’t restart {{agent}} in {{workspace}}.", + "openWorkspace": "Open workspace" }, "cloudAgentCheck": { "keyAgentSignedOut": "{{agent}} isn’t signed in.", diff --git a/src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.test.ts b/src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.test.ts new file mode 100644 index 00000000000..65b60ea7353 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.test.ts @@ -0,0 +1,101 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const toastError = vi.fn() +vi.mock('sonner', () => ({ toast: { error: (...args: unknown[]) => toastError(...args) } })) +const activate = vi.fn() +vi.mock('@/lib/workspace/activation/worktree-activation', () => ({ + activateAndRevealWorktree: (id: string) => activate(id) +})) + +const { useAppStore } = await import('@/store') +const { makePaneKey } = await import('../../../../shared/workspace-layout/stable-pane-id') +const attempts = await import('./cloud-agent-restart-attempt') +const { restartPaneExists, watchCloudAgentRestartPane } = + await import('./cloud-agent-restart-pane-watch') + +const TAB = 'tab-1' +const LEAF = '11111111-1111-4111-8111-111111111111' +const PANE = makePaneKey(TAB, LEAF) +const PTY = 'remote:env-1@@term_a' + +function seed(): void { + useAppStore.setState({ + tabsByWorktree: { + 'wt-1': [ + { + id: TAB, + ptyId: PTY, + worktreeId: 'wt-1', + title: 'claude', + customTitle: null, + color: null, + sortOrder: 0, + createdAt: 1 + } + ] + }, + terminalLayoutsByTabId: { + [TAB]: { + root: { type: 'leaf', leafId: LEAF }, + activeLeafId: LEAF, + expandedLeafId: null, + ptyIdsByLeafId: { [LEAF]: PTY } + } + } + }) +} + +describe('RA-11 Restart when its pane goes away', () => { + beforeEach(() => { + vi.useFakeTimers() + toastError.mockClear() + activate.mockClear() + attempts.resetCloudAgentRestartsForTest() + seed() + attempts.beginCloudAgentRestart( + PANE, + { agent: 'claude', resumable: true, ptyId: PTY }, + () => undefined + ) + }) + afterEach(() => { + attempts.resetCloudAgentRestartsForTest() + vi.useRealTimers() + }) + + it('a restart rebinding the leaf’s PTY keeps the pane: same tab, same leaf, no toast', () => { + watchCloudAgentRestartPane(PANE, 'wt-1', 'claude') + useAppStore.setState({ + terminalLayoutsByTabId: { + [TAB]: { + root: { type: 'leaf', leafId: LEAF }, + activeLeafId: LEAF, + expandedLeafId: null, + ptyIdsByLeafId: {} + } + } + }) + expect(restartPaneExists(useAppStore.getState(), PANE)).toBe(true) + expect(toastError).not.toHaveBeenCalled() + expect(attempts.cloudAgentRestartAttempt(PANE, 0)?.phase).toBe('restarting') + }) + + it('the tab closing mid-restart ends the attempt with a toast naming the workspace', () => { + watchCloudAgentRestartPane(PANE, 'wt-1', 'claude') + useAppStore.setState({ tabsByWorktree: { 'wt-1': [] }, terminalLayoutsByTabId: {} }) + expect(attempts.cloudAgentRestartAttempt(PANE, 0)).toBeNull() + expect(toastError).toHaveBeenCalledTimes(1) + const [title, options] = toastError.mock.calls[0] + expect(title).toBe('Couldn’t restart Claude in wt-1.') + options.action.onClick() + expect(activate).toHaveBeenCalledWith('wt-1') + }) + + it('stops watching once the attempt settled', () => { + const stop = watchCloudAgentRestartPane(PANE, 'wt-1', 'claude') + attempts.dismissCloudAgentRestart(PANE) + useAppStore.setState({ tabsByWorktree: { 'wt-1': [] } }) + expect(toastError).not.toHaveBeenCalled() + stop() + }) +}) diff --git a/src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.ts b/src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.ts new file mode 100644 index 00000000000..a6fd9b15521 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/cloud-agent-restart-pane-watch.ts @@ -0,0 +1,74 @@ +// RA-11: a Restart must never vanish without a result. If its pane goes away while the restart runs +// (or after it failed), the strip has nowhere to show; say so in a toast that names the workspace. +import { toast } from 'sonner' +import { translate } from '@/i18n/i18n' +import { useAppStore } from '@/store' +import type { AppState } from '@/store/types' +import { activateAndRevealWorktree } from '@/lib/workspace/activation/worktree-activation' +import { resolveWorktreeDisplayName } from '@/lib/worktree-default-display-name' +import { parsePaneKey } from '../../../../shared/workspace-layout/stable-pane-id' +import type { TerminalPaneLayoutNode } from '../../../../shared/workspace-layout/terminal-tab-types' +import { + HOST_AGENT_SIGN_IN_AGENT_NAME, + type HostAgentSignInAgent +} from '../../../../shared/host-agent-sign-in-status' +import { cloudAgentRestartAttempt, dismissCloudAgentRestart } from './cloud-agent-restart-attempt' + +function hasLeaf(node: TerminalPaneLayoutNode | null | undefined, leafId: string): boolean { + if (!node) { + return false + } + return node.type === 'leaf' + ? node.leafId === leafId + : hasLeaf(node.first, leafId) || hasLeaf(node.second, leafId) +} + +/** Whether the pane (its tab and its leaf) still exists. A restart only rebinds the leaf's PTY. */ +export function restartPaneExists(state: AppState, paneKey: string): boolean { + const parsed = parsePaneKey(paneKey) + if (!parsed) { + return false + } + const tabExists = Object.values(state.tabsByWorktree).some((tabs) => + tabs.some((tab) => tab.id === parsed.tabId) + ) + return tabExists && hasLeaf(state.terminalLayoutsByTabId[parsed.tabId]?.root, parsed.leafId) +} + +/** Watch the pane for the attempt's life; returns the unsubscribe. */ +export function watchCloudAgentRestartPane( + paneKey: string, + worktreeId: string, + agent: HostAgentSignInAgent +): () => void { + const unsubscribe = useAppStore.subscribe((state) => { + if (!cloudAgentRestartAttempt(paneKey, 0)) { + unsubscribe() + return + } + if (restartPaneExists(state, paneKey)) { + return + } + unsubscribe() + dismissCloudAgentRestart(paneKey) + const worktree = state.getKnownWorktreeById(worktreeId) + const workspace = worktree ? resolveWorktreeDisplayName(worktree) : worktreeId + toast.error( + translate( + 'agentSignInStrip.restartFailedIn', + 'Couldn’t restart {{agent}} in {{workspace}}.', + { + agent: HOST_AGENT_SIGN_IN_AGENT_NAME[agent], + workspace + } + ), + { + action: { + label: translate('agentSignInStrip.openWorkspace', 'Open workspace'), + onClick: () => activateAndRevealWorktree(worktreeId) + } + } + ) + }) + return unsubscribe +} diff --git a/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.test.ts b/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.test.ts new file mode 100644 index 00000000000..f497653a6a8 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.test.ts @@ -0,0 +1,49 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const calls: { method: string; params: unknown; environmentId: string }[] = [] +let showPtyId: string | null = 'host-pty-7' +let stopResult: Record = {} + +vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ + callRuntimeRpc: async (target: { environmentId: string }, method: string, params: unknown) => { + calls.push({ method, params, environmentId: target.environmentId }) + return method === 'terminal.show' ? { terminal: { ptyId: showPtyId } } : stopResult + } +})) + +const { stopRemotePaneTerminalForRestart } = await import('./cloud-pane-restart-stop') + +describe('stopRemotePaneTerminalForRestart (RA-11 paired pane)', () => { + beforeEach(() => { + calls.length = 0 + showPtyId = 'host-pty-7' + stopResult = { stoppedPtyIds: ['host-pty-7'], postStopVerified: true } + }) + + it('puts exactly that one host terminal to sleep (reversible, target only), never a close', async () => { + await stopRemotePaneTerminalForRestart('remote:env-1@@term_a', 'wt-1') + expect(calls.map((c) => c.method)).toEqual(['terminal.show', 'terminal.stopExact']) + expect(calls[0]).toMatchObject({ environmentId: 'env-1', params: { terminal: 'term_a' } }) + expect(calls[1]).toMatchObject({ + environmentId: 'env-1', + params: { expectedPtyIds: ['host-pty-7'], keepHistory: true, targetOnly: true } + }) + }) + + it('throws when the host has no live terminal for the pane, or does not confirm the stop', async () => { + showPtyId = null + await expect(stopRemotePaneTerminalForRestart('remote:env-1@@term_a', 'wt-1')).rejects.toThrow( + 'restart_remote_pane_not_live' + ) + showPtyId = 'host-pty-7' + stopResult = { stoppedPtyIds: ['host-pty-7'], postStopVerified: false } + await expect(stopRemotePaneTerminalForRestart('remote:env-1@@term_a', 'wt-1')).rejects.toThrow() + }) + + it('refuses a pane id without an owning environment', async () => { + await expect(stopRemotePaneTerminalForRestart('remote:term_a', 'wt-1')).rejects.toThrow( + 'restart_remote_pane_unowned' + ) + expect(calls).toEqual([]) + }) +}) diff --git a/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.ts b/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.ts index 80f1b04009d..9aa1e92d7f4 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-pane-restart-stop.ts @@ -1,6 +1,8 @@ // RA-11: Restart on a paired (web-host) pane. The host already runs a terminal in that pane, so a // desktop reconnect reattaches to it (ensureAgentSession hands back the live session) and nothing -// relaunches. Stop exactly that one host terminal first; the reconnect then starts the agent anew. +// relaunches. Put exactly that one host terminal to sleep first (the reversible stop the pane-sleep +// path uses: the leaf stays, as a sleeping surface); the reconnect then wakes it with --resume. +// A plain stop is a close: the host drops the tab and there is nothing left to relaunch into. import { parseRemoteRuntimePtyId } from '../../../../shared/remote-runtime/remote-runtime-pty-id' import type { RuntimeTerminalShow } from '../../../../shared/runtime-terminal-contracts' import { callRuntimeRpc } from '@/runtime/rpc/calls/runtime-rpc-call' @@ -39,6 +41,8 @@ export async function stopRemotePaneTerminalForRestart( { worktree: toRuntimeWorktreeSelector(worktreeId), expectedPtyIds: [hostPtyId], + // Reversible: the host keeps the pane (and its scrollback) for the wake. + keepHistory: true, // Only this pane: every other terminal in the workspace keeps running. targetOnly: true }, From 71b219cbafeb0e0f69ea69ffefb3ff9499bc916a Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 10 Nov 2026 22:59:59 -0800 Subject: [PATCH 2/2] fix(cloud): RA-11 Restart resumes the pane's own conversation on the host; quieter restarting/failed strip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit m4air run 3: same tab, same leaf, new PTY, and state 1 after, but the relaunch was a new conversation (no --resume; new session id). Orca Cloud tabs are host-surface tabs: the desktop reconnect only attaches, and the host woke the slept leaf with its original launch. After the reversible stop, the desktop now asks the host to ensure the agent on the pane's own provider session, placed in the same host tab and leaf, then reconnects. A pane with no session keeps the host's fresh launch ('Starts a new conversation'). A host that does not stop or resume shows the failure with the reason. Strip (design-squad): 'Restarting Claude…' is the spinner and words only, with no button and no ×. The failure says it once: the body names the reason when known ('The workspace didn't respond.') and is otherwise empty; Try again and × stay. --- .../CloudAgentSignInStrip.fixture.tsx | 9 ++- .../CloudAgentSignInStrip.tsx | 9 +-- .../cloud-agent-strip-model.ts | 2 + .../cloud-running-strip-model.test.ts | 20 +++++-- .../cloud-running-strip-model.ts | 15 ++--- .../cloud-strip-restart-run.test.ts | 59 +++++++++++++++++++ .../use-ready-pane-account-restarts.ts | 43 +++++++++++--- src/renderer/src/i18n/locales/en.json | 2 +- .../orca-cloud/cloud-agent-restart-attempt.ts | 9 ++- .../cloud-pane-restart-stop.test.ts | 36 ++++++++++- .../lib/orca-cloud/cloud-pane-restart-stop.ts | 37 ++++++++++++ .../cloud-agent-sign-in-strip.spec.ts | 12 ++-- 12 files changed, 219 insertions(+), 34 deletions(-) diff --git a/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.fixture.tsx b/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.fixture.tsx index d48d5908c7a..0040d2396f9 100644 --- a/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.fixture.tsx +++ b/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.fixture.tsx @@ -173,5 +173,12 @@ export const runningRestarting = fixture({ }) /** RA-11: nothing relaunched in time. */ export const runningRestartFailed = fixture({ - restart: { phase: 'failed', agent: 'claude', resumable: true, ptyId: 'p', launchedAt: null } + restart: { + phase: 'failed', + agent: 'claude', + resumable: true, + ptyId: 'p', + launchedAt: null, + failure: 'hostUnresponsive' + } }) diff --git a/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx b/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx index a5fc7c3f061..6773c654d35 100644 --- a/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx +++ b/src/renderer/src/components/agent-sign-in-strip/CloudAgentSignInStrip.tsx @@ -291,10 +291,11 @@ export function CloudAgentSignInStrip({ ) : null} {/* Renew and Restart are the whole action and the strip leaves on its own; Escape still cancels. */} - {!model.dismiss && - (model.status === 'done' || - primary?.action.kind === 'renew' || - primary?.action.kind === 'restart') ? null : ( + {model.noClose || + (!model.dismiss && + (model.status === 'done' || + primary?.action.kind === 'renew' || + primary?.action.kind === 'restart')) ? null : (