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 9535065c7f6..a3c38777f0d 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 @@ -375,7 +375,7 @@ describe('RA-11 Restart on a paired (remote) pane', () => { }) await vi.waitFor(() => expect(cloudAgentRestartStartedFresh(PANE_KEY, 0)).toBe(true)) expect(rpc.sends).toEqual([ - { terminal: 'term_new', text: 'claude --dangerously-skip-permissions', enter: true } + { terminal: 'term_new', text: 'claude --dangerously-skip-permissions\r' } ]) vi.advanceTimersByTime(CLOUD_AGENT_FRESH_NOTICE_MS) expect(cloudAgentRestartStartedFresh(PANE_KEY, 0)).toBe(false) @@ -406,6 +406,38 @@ describe('RA-11 Restart on a paired (remote) pane', () => { expect(cloudAgentRestartStartedFresh(PANE_KEY, 0)).toBe(false) }) + it('a fresh start that could not be typed shows Couldn’t restart (never a quiet bare shell)', async () => { + clickRemoteRestart() + const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) + // The pane settles on its new PTY first; the failed fallback must bring the strip back. + const handleRestartPane = vi.fn(() => { + settleCloudAgentRestart(PANE_KEY) + }) + runReadyPaneAccountRestarts({ + panes: [{ id: 7, leafId: LEAF }], + ptyIdOf: () => REMOTE, + readyRestartPtyIds: selectReadyAccountRestartPtyIds(useAppStore.getState(), TAB), + tabId: TAB, + consumePendingPaneAccountRestart: useAppStore.getState().consumePendingPaneAccountRestart, + buildAccountRestartStartup: () => ({ + command: 'claude', + resumeProviderSession: { key: 'session_id' as const, id: 'sess-tab-1' } + }), + handleRestartPane, + stopRemotePane: () => Promise.resolve(), + resumeRemotePane: () => Promise.resolve(), + freshIfResumeFoundNothing: () => Promise.reject(new Error('ra11e_fresh_send_failed')), + buildFreshCommand: () => 'claude' + }) + await vi.waitFor(() => expect(cloudAgentRestartAttempt(PANE_KEY, 0)?.phase).toBe('failed')) + expect(cloudAgentRestartAttempt(PANE_KEY, 0)).toMatchObject({ + failure: 'hostUnresponsive', + resumable: false + }) + expect(cloudAgentRestartStartedFresh(PANE_KEY, 0)).toBe(false) + warn.mockRestore() + }) + it('a failed resume on the host shows the failure and never reconnects', async () => { clickRemoteRestart() const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined) diff --git a/src/renderer/src/components/terminal-pane/account-restart/use-ready-pane-account-restarts.ts b/src/renderer/src/components/terminal-pane/account-restart/use-ready-pane-account-restarts.ts index adecb8a76ff..c98b91f098f 100644 --- a/src/renderer/src/components/terminal-pane/account-restart/use-ready-pane-account-restarts.ts +++ b/src/renderer/src/components/terminal-pane/account-restart/use-ready-pane-account-restarts.ts @@ -8,6 +8,7 @@ import { import { beginCloudAgentRestart, failCloudAgentRestart, + failCloudAgentRestartFresh, noteCloudAgentRestartFresh, isForcedCloudAgentRestart, noteCloudAgentRestartLaunched @@ -234,7 +235,19 @@ export function runReadyPaneAccountRestarts< noteCloudAgentRestartFresh(makePaneKey(args.tabId, pane.leafId)) } }) - .catch((err: unknown) => console.warn('[orca-cloud] fresh fallback failed:', err)) + .catch((err: unknown) => { + console.warn('[orca-cloud] fresh fallback failed:', err) + if (tracked) { + // The pane is a bare shell now: say so with Try again, never leave it quiet. + failCloudAgentRestartFresh(makePaneKey(args.tabId, pane.leafId), { + agent, + ptyId: + useAppStore.getState().terminalLayoutsByTabId[args.tabId]?.ptyIdsByLeafId?.[ + pane.leafId + ] ?? ptyId + }) + } + }) } }, (err: unknown) => { diff --git a/src/renderer/src/lib/orca-cloud/cloud-agent-restart-attempt.ts b/src/renderer/src/lib/orca-cloud/cloud-agent-restart-attempt.ts index d0b8f8c788d..56168718aff 100644 --- a/src/renderer/src/lib/orca-cloud/cloud-agent-restart-attempt.ts +++ b/src/renderer/src/lib/orca-cloud/cloud-agent-restart-attempt.ts @@ -179,6 +179,27 @@ export function cloudAgentRestartStartedFresh(paneKey: string, _revision: number return freshNotices.has(paneKey) } +/** + * RA-11e: the resume found no conversation and the fresh start could not be typed, so the pane is a + * bare shell. The attempt already settled on the new PTY; show "Couldn't restart" again so the + * person can Try again, never a quiet shell. + */ +export function failCloudAgentRestartFresh( + paneKey: string, + args: { agent: HostAgentSignInAgent; ptyId: string } +): void { + stopRestartTimer(paneKey) + restarts.set(paneKey, { + phase: 'failed', + agent: args.agent, + resumable: false, + ptyId: args.ptyId, + launchedAt: null, + failure: 'hostUnresponsive' + }) + notifyCloudRunningStrips() +} + /** Tests only. */ export function resetCloudAgentRestartsForTest(): void { forcedRestarts.clear() 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 index 3f8e809e56f..53dad184a32 100644 --- 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 @@ -4,6 +4,10 @@ const calls: { method: string; params: unknown; environmentId: string }[] = [] let showPtyId: string | null = 'host-pty-7' let stopResult: Record = {} let readTail: string[] = [] +/** How each terminal.send answers, in order ('ok' once the list runs out). */ +let sendAnswers: ('ok' | 'timeout' | 'refused')[] = [] +/** Tail the host shows after the nth send (the fresh agent came up), if set. */ +let tailAfterSend: string[] | null = null vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ callRuntimeRpc: async (target: { environmentId: string }, method: string, params: unknown) => { @@ -11,6 +15,16 @@ vi.mock('@/runtime/rpc/calls/runtime-rpc-call', () => ({ if (method === 'terminal.read') { return { terminal: { tail: readTail } } } + if (method === 'terminal.send') { + if (tailAfterSend) { + readTail = tailAfterSend + } + const answer = sendAnswers.shift() ?? 'ok' + if (answer === 'timeout') { + throw new Error('Timed out waiting for the remote Orca runtime to respond.') + } + return { send: { handle: 'term_new', accepted: answer === 'ok', bytesWritten: 0 } } + } return method === 'terminal.show' ? { terminal: { ptyId: showPtyId } } : stopResult } })) @@ -93,6 +107,8 @@ describe('freshIfResumeFoundNothing (RA-11e)', () => { beforeEach(() => { calls.length = 0 clock = 0 + sendAnswers = [] + tailAfterSend = null }) it('a resume that found no conversation starts the agent fresh in the same pane, once', async () => { @@ -109,8 +125,69 @@ describe('freshIfResumeFoundNothing (RA-11e)', () => { expect(sends).toHaveLength(1) expect(sends[0]).toMatchObject({ environmentId: 'env-1', - params: { terminal: 'term_new', text: 'claude --dangerously-skip-permissions', enter: true } + // Enter rides in the text: `enter: true` makes the host wait for the exited Claude's composer. + params: { terminal: 'term_new', text: 'claude --dangerously-skip-permissions\r' } }) + expect(sends[0]?.params).not.toHaveProperty('enter') + }) + + it('a send that times out once is tried again and the fresh start lands', async () => { + readTail = ['No conversation found with session ID: s1', '$ '] + sendAnswers = ['timeout', 'ok'] + const relaunched = await freshIfResumeFoundNothing({ + currentPtyId: () => 'remote:env-1@@term_new', + replacedPtyId: 'remote:env-1@@term_old', + freshCommand: 'claude', + sleep, + now + }) + expect(relaunched).toBe(true) + expect(calls.filter((c) => c.method === 'terminal.send')).toHaveLength(2) + }) + + it('a refused send counts as a failed try', async () => { + readTail = ['No conversation found with session ID: s1', '$ '] + sendAnswers = ['refused', 'ok'] + await expect( + freshIfResumeFoundNothing({ + currentPtyId: () => 'remote:env-1@@term_new', + replacedPtyId: 'remote:env-1@@term_old', + freshCommand: 'claude', + sleep, + now + }) + ).resolves.toBe(true) + expect(calls.filter((c) => c.method === 'terminal.send')).toHaveLength(2) + }) + + it('never types twice: a timed-out send whose Claude came up anyway is not sent again', async () => { + readTail = ['No conversation found with session ID: s1', '$ '] + sendAnswers = ['timeout'] + tailAfterSend = ['No conversation found with session ID: s1', '$ claude', 'Claude Code v2.1.319'] + const relaunched = await freshIfResumeFoundNothing({ + currentPtyId: () => 'remote:env-1@@term_new', + replacedPtyId: 'remote:env-1@@term_old', + freshCommand: 'claude', + sleep, + now + }) + expect(relaunched).toBe(true) + expect(calls.filter((c) => c.method === 'terminal.send')).toHaveLength(1) + }) + + it('every try failing throws, so the caller shows Couldn’t restart instead of a quiet shell', async () => { + readTail = ['No conversation found with session ID: s1', '$ '] + sendAnswers = ['timeout', 'timeout', 'timeout'] + await expect( + freshIfResumeFoundNothing({ + currentPtyId: () => 'remote:env-1@@term_new', + replacedPtyId: 'remote:env-1@@term_old', + freshCommand: 'claude', + sleep, + now + }) + ).rejects.toThrow('ra11e_fresh_send_failed') + expect(calls.filter((c) => c.method === 'terminal.send')).toHaveLength(3) }) it('ANSI-coloured output and a late message (after 15 s) are still caught', async () => { 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 f969a741036..3a21d9932d8 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 @@ -106,11 +106,88 @@ function hostTerminalOf(ptyId: string | null): { environmentId: string; handle: : null } +// A just-relaunched host can be slow to take input; never leave the pane a bare shell for that. +const FRESH_SEND_TIMEOUT_MS = 20_000 +const FRESH_SEND_ATTEMPTS = 3 +const FRESH_SEND_RETRY_MS = 1_000 +/** The fresh start landed: the command echoed, or the agent's own screen is up after the miss. */ +const AGENT_UP = /Claude Code|OpenAI Codex|^\s*[❯›>]\s*$/ + +/** Whether the fresh command (or the agent it starts) shows after the last "no conversation" line. */ +function freshStartLanded(lines: readonly string[], freshCommand: string): boolean { + let miss = -1 + lines.forEach((line, index) => { + if (NO_CONVERSATION.test(line)) { + miss = index + } + }) + const echo = freshCommand.trim().slice(-24) + return lines + .slice(miss + 1) + .some((line) => AGENT_UP.test(line) || (echo.length > 0 && line.includes(echo))) +} + +async function readTail( + target: { kind: 'environment'; environmentId: string }, + handle: string +): Promise { + const read = await callRuntimeRpc<{ terminal?: { tail?: string[] } }>( + target, + 'terminal.read', + { terminal: handle, limit: 80 }, + { timeoutMs: 5_000 } + ).catch(() => null) + const tail = read?.terminal?.tail + return Array.isArray(tail) ? tail.map((line) => line.replace(ANSI, '')) : null +} + +/** + * Type the fresh command once. A send that times out may still have landed, so before each retry + * the tail is read again and nothing is typed a second time if the command or the agent shows. + * Throws when every try failed: the caller reports it rather than leave a quiet bare shell. + */ +async function sendFreshOnce( + target: { kind: 'environment'; environmentId: string }, + handle: string, + freshCommand: string, + sleep: (ms: number) => Promise +): Promise { + let lastError: unknown = null + for (let attempt = 1; attempt <= FRESH_SEND_ATTEMPTS; attempt += 1) { + if (attempt > 1) { + await sleep(FRESH_SEND_RETRY_MS) + const tail = await readTail(target, handle) + if (tail && freshStartLanded(tail, freshCommand)) { + return + } + } + try { + // The Enter rides in the text, not `enter: true`: the host holds an Enter send for the launched + // Claude's composer (8 s), which never comes once it exited to the shell, then refuses it. + const sent = await callRuntimeRpc<{ send?: { accepted?: boolean; refusedReason?: string } }>( + target, + 'terminal.send', + { terminal: handle, text: `${freshCommand}\r` }, + { timeoutMs: FRESH_SEND_TIMEOUT_MS } + ) + if (sent?.send?.accepted === false) { + throw new Error(`terminal_send_refused:${sent.send.refusedReason ?? 'unknown'}`) + } + return + } catch (err) { + lastError = err + console.warn(`[orca-cloud] RA-11e fresh start send failed (try ${attempt}):`, err) + } + } + throw new Error('ra11e_fresh_send_failed', { cause: lastError }) +} + /** * RA-11e backstop: a recorded session can have no conversation on disk (Claude started, never ran a * turn). Its `--resume` prints "No conversation found…" and exits, leaving a bare shell. Watch the * relaunched pane's terminal (its new handle) for up to 25 s; if that happened, start the agent fresh - * in the same shell, once. Returns whether it relaunched fresh. + * in the same shell, once. Returns whether it relaunched fresh; throws when the fresh start could not + * be typed (the pane is left a bare shell, so the caller must say so). */ export async function freshIfResumeFoundNothing(args: { /** The pane's PTY id now (the relaunched terminal), or null while it is still binding. */ @@ -156,12 +233,7 @@ export async function freshIfResumeFoundNothing(args: { '[orca-cloud] RA-11e resume found no conversation; starting fresh in', current.handle ) - await callRuntimeRpc( - target, - 'terminal.send', - { terminal: current.handle, text: args.freshCommand, enter: true }, - { timeoutMs: 5_000 } - ) + await sendFreshOnce(target, current.handle, args.freshCommand, sleep) return true } console.info('[orca-cloud] RA-11e watch ended without a "no conversation" line', lastSeen)