diff --git a/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.test.tsx b/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.test.tsx index 8ba8dbf77f3..268595f5108 100644 --- a/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.test.tsx +++ b/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.test.tsx @@ -167,6 +167,14 @@ describe('the paired cloud workspace surface', () => { expect(wakePaired).toHaveBeenCalledWith(ID, { source: 'open' }) }) + it('a just-created paired row with no status read and no pane yet is not shown as asleep', async () => { + seedCloudWorkspaceSnapshot({ paired: { [ID]: 'env-paired' }, idsLoaded: true }) + const { container } = render() + await flush() + expect(container.querySelector('[data-cloud-cover="asleep"]')).toBeNull() + expect(container.textContent).not.toContain('Wake to continue') + }) + it('after a paired Sleep: saving (never the empty states) until a new-enough sealed copy is read', async () => { // The old copy, then nothing new, then the one this Sleep sealed. reads = [ diff --git a/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.tsx b/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.tsx index f4831d6675d..cdd536defaf 100644 --- a/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.tsx +++ b/src/renderer/src/components/orca-cloud/PairedCloudWorkspaceSurface.tsx @@ -134,10 +134,13 @@ export function PairedCloudWorkspaceSurface({ isWakeActive(view.wake, Math.max(now, view.wake.at)) ) // Sleep pressed here: its status read can still say ready, but it is going to sleep. + // Before the first status read "sleeping" only means no pane has opened yet (a workspace that was + // just created and is about to start its agent): never show it as asleep on that alone. const asleep = !wakeInFlight && view.state !== 'archived' && - (view.state === 'sleeping' || copySaveEntry?.state === 'saving') + ((view.state === 'sleeping' && Boolean(view.entry?.status)) || + copySaveEntry?.state === 'saving') const layer: 'waking' | 'asleep' | null = overlay.wake ? 'waking' : asleep ? 'asleep' : null const sandbox = usePairedSandboxId(workspaceId, layer === 'asleep') diff --git a/src/renderer/src/components/terminal-pane/controller/TerminalPaneSurface.tsx b/src/renderer/src/components/terminal-pane/controller/TerminalPaneSurface.tsx index 5c8f4a7d47b..86f70bb8fcc 100644 --- a/src/renderer/src/components/terminal-pane/controller/TerminalPaneSurface.tsx +++ b/src/renderer/src/components/terminal-pane/controller/TerminalPaneSurface.tsx @@ -13,6 +13,7 @@ import { isPaneOwnerRecoveryError } from '../errors/terminal-error-display' import { TerminalErrorToast } from '../errors/TerminalErrorToast' import { openTerminalBesideTab } from '../context-menu/open-terminal-beside-tab' import { AgentLaunchPaneNoticePortal } from '../launch-outcome/AgentLaunchPaneNotice' +import { AgentStartingPortals } from '../launch-outcome/AgentStartingPortals' import { TerminalSessionStateSaveFailureDialog } from '../layout/persistence/TerminalSessionStateSaveFailureDialog' import { LinkActionPopover } from '@/components/link-actions/LinkActionPopover' import { TerminalAgentSessionForkDialog } from '../session-fork/TerminalAgentSessionForkDialog' @@ -233,6 +234,7 @@ export function TerminalPaneSurface({ `terminal-error-${activePane.id}` ) : null} + diff --git a/src/renderer/src/components/terminal-pane/launch-outcome/AgentStartingPortals.tsx b/src/renderer/src/components/terminal-pane/launch-outcome/AgentStartingPortals.tsx new file mode 100644 index 00000000000..dc0b0d7eabf --- /dev/null +++ b/src/renderer/src/components/terminal-pane/launch-outcome/AgentStartingPortals.tsx @@ -0,0 +1,141 @@ +import { useEffect, useState } from 'react' +import { createPortal } from 'react-dom' +import { Loader2 } from 'lucide-react' +import { useTranslate } from '@/i18n/use-translate' +import { useNow } from '@/hooks/use-now' +import type { Terminal } from '@xterm/xterm' +import { TUI_AGENT_DISPLAY_NAMES } from '../../../../../shared/agent-launch/catalog/tui-agent-display-names' +import type { TuiAgent } from '../../../../../shared/agent-launch/catalog/tui-agent' +import type { TerminalPaneController } from '../controller/use-terminal-pane-controller' +import { + AGENT_STARTING_DELAY_MS, + AGENT_STARTING_SLOW_MS, + isAgentLaunchLeaf, + terminalHasVisibleContent +} from './agent-starting-state' + +/** Terminals that have drawn something: once they have, the status never comes back. */ +const drawn = new WeakSet() + +function useTerminalStillBlank(terminal: Terminal): boolean { + const [blank, setBlank] = useState( + () => !drawn.has(terminal) && !terminalHasVisibleContent(terminal.buffer) + ) + useEffect(() => { + if (!blank) { + drawn.add(terminal) + return + } + const check = (): void => { + if (terminalHasVisibleContent(terminal.buffer)) { + drawn.add(terminal) + setBlank(false) + } + } + check() + const subscription = terminal.onWriteParsed(check) + return () => subscription.dispose() + }, [terminal, blank]) + return blank +} + +export function AgentStartingStatus({ + agent, + startedAt +}: { + agent: TuiAgent + startedAt: number +}): React.JSX.Element | null { + const t = useTranslate() + const now = useNow(1_000) + const elapsed = Math.max(0, now - startedAt) + if (elapsed < AGENT_STARTING_DELAY_MS) { + return null + } + const name = TUI_AGENT_DISPLAY_NAMES[agent] ?? agent + return ( +
+
+ + + {elapsed >= AGENT_STARTING_SLOW_MS ? ( + + {t( + 'terminalPane.agentStarting.slow', + 'Taking longer than usual. It opens here as soon as it’s ready.' + )} + + ) : null} +
+
+ ) +} + +function PaneAgentStarting({ + terminal, + agent, + container, + paneId +}: { + terminal: Terminal + agent: TuiAgent + container: HTMLElement + paneId: number +}): React.JSX.Element | null { + const blank = useTerminalStillBlank(terminal) + // Counted from when this pane began waiting; the clock stops mattering once it draws. + const [startedAt] = useState(() => Date.now()) + if (!blank) { + return null + } + return createPortal( + , + container, + `agent-starting-${paneId}` + ) +} + +/** "Starting Claude…" over a launched agent's pane until the agent draws its first frame. */ +export function AgentStartingPortals({ + controller +}: { + controller: TerminalPaneController +}): React.JSX.Element | null { + const { managedPanes, terminalTab, paneProcessExitsByPaneId } = controller + const agent = terminalTab?.launchAgent + // Read once per render: a launch older than the window never shows the status. + const [mountedAt] = useState(() => Date.now()) + if (!agent) { + return null + } + return ( + <> + {managedPanes.map((pane) => + // An exited agent shows its exit overlay (the error), never "Starting". + isAgentLaunchLeaf(terminalTab, pane.leafId, managedPanes.length, mountedAt) && + !paneProcessExitsByPaneId[pane.id] ? ( + + ) : null + )} + + ) +} diff --git a/src/renderer/src/components/terminal-pane/launch-outcome/AgentStartingStatus.fixture.tsx b/src/renderer/src/components/terminal-pane/launch-outcome/AgentStartingStatus.fixture.tsx new file mode 100644 index 00000000000..78702b1597e --- /dev/null +++ b/src/renderer/src/components/terminal-pane/launch-outcome/AgentStartingStatus.fixture.tsx @@ -0,0 +1,18 @@ +import { useState } from 'react' +import { defineFixture } from '../../../component-fixtures/component-fixture' +import { AgentStartingStatus } from './AgentStartingPortals' + +function Pane({ elapsedMs }: { elapsedMs: number }): React.JSX.Element { + const [startedAt] = useState(() => Date.now() - elapsedMs) + return ( +
+ + +
+ ) +} + +/** A cloud Claude pane before Claude's first frame: says what is happening, never a bare cursor. */ +export const starting = defineFixture({ width: 720, render: () => }) + +export const startingSlow = defineFixture({ width: 720, render: () => }) diff --git a/src/renderer/src/components/terminal-pane/launch-outcome/agent-starting-state.test.ts b/src/renderer/src/components/terminal-pane/launch-outcome/agent-starting-state.test.ts new file mode 100644 index 00000000000..9063f488453 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/launch-outcome/agent-starting-state.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from 'vitest' +import { + AGENT_STARTING_WINDOW_MS, + isAgentLaunchLeaf, + terminalHasVisibleContent +} from './agent-starting-state' + +function buffer(lines: string[]) { + return { + active: { + length: lines.length, + getLine: (y: number) => (y < lines.length ? { translateToString: () => lines[y] } : undefined) + } + } +} + +describe('agent starting status', () => { + it('a pane with only a cursor (escape setup, blank rows) is still starting; any text ends it', () => { + expect(terminalHasVisibleContent(buffer(['', ' ', '']))).toBe(false) + expect(terminalHasVisibleContent(buffer(['', ' ╭─ Claude Code']))).toBe(true) + }) + + it('shows only on the launched agent’s pane, and only for a fresh launch', () => { + const now = 1_000_000 + const tab = { launchAgent: 'claude', createdAt: now - 1_000 } + expect(isAgentLaunchLeaf(tab, 'leaf-1', 1, now)).toBe(true) + // A split beside it is a plain terminal. + expect(isAgentLaunchLeaf(tab, 'leaf-2', 2, now)).toBe(false) + expect( + isAgentLaunchLeaf({ ...tab, agentLaunchPane: { leafId: 'leaf-2' } }, 'leaf-2', 2, now) + ).toBe(true) + // A plain terminal tab, or a restored launch from long ago (a reattach, not a start). + expect(isAgentLaunchLeaf({ createdAt: now }, 'leaf-1', 1, now)).toBe(false) + expect( + isAgentLaunchLeaf({ ...tab, createdAt: now - AGENT_STARTING_WINDOW_MS - 1 }, 'leaf-1', 1, now) + ).toBe(false) + }) +}) diff --git a/src/renderer/src/components/terminal-pane/launch-outcome/agent-starting-state.ts b/src/renderer/src/components/terminal-pane/launch-outcome/agent-starting-state.ts new file mode 100644 index 00000000000..7e3325302e0 --- /dev/null +++ b/src/renderer/src/components/terminal-pane/launch-outcome/agent-starting-state.ts @@ -0,0 +1,47 @@ +// "Starting …" while a launched agent's pane is still blank. A cloud agent starts behind +// a shell whose echo is erased, so between the pane attaching and the agent's first frame there is +// only a cursor; this says what is happening instead (Neil, n70: a blank cursor for ~10 s). + +/** Below this, the agent usually draws first: no status flashes by. */ +export const AGENT_STARTING_DELAY_MS = 600 +/** Past this, the status says it's taking longer than usual. */ +export const AGENT_STARTING_SLOW_MS = 20_000 + +type BufferLike = { + active: { + length: number + getLine: (y: number) => { translateToString: (trimRight?: boolean) => string } | undefined + } +} + +/** Whether anything visible has been drawn yet (escape sequences and cursor moves don't count). */ +export function terminalHasVisibleContent(buffer: BufferLike): boolean { + const { active } = buffer + for (let y = 0; y < active.length; y++) { + if (active.getLine(y)?.translateToString(true).trim()) { + return true + } + } + return false +} + +/** Only a launch this recent can still be starting; an older blank pane is a reattach, not a start. */ +export const AGENT_STARTING_WINDOW_MS = 3 * 60_000 + +/** + * The pane that runs the tab's freshly launched agent: its recorded leaf, else the tab's only pane. + */ +export function isAgentLaunchLeaf( + tab: + | { launchAgent?: string; agentLaunchPane?: { leafId: string }; createdAt: number } + | null + | undefined, + leafId: string, + paneCount: number, + now: number +): boolean { + if (!tab?.launchAgent || now - tab.createdAt > AGENT_STARTING_WINDOW_MS) { + return false + } + return tab.agentLaunchPane ? tab.agentLaunchPane.leafId === leafId : paneCount === 1 +} diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index b687aa55b74..0fda31ea1e6 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -29269,5 +29269,11 @@ "tryAgain": "Try again", "goneTitle": "Couldn’t find {{name}}", "goneDescription": "It may have been deleted." + }, + "terminalPane": { + "agentStarting": { + "title": "Starting {{agent}}…", + "slow": "Taking longer than usual. It opens here as soon as it’s ready." + } } }