diff --git a/docs/reference/agent-session-resume-cli-evidence.md b/docs/reference/agent-session-resume-cli-evidence.md new file mode 100644 index 000000000..5c26f9ecb --- /dev/null +++ b/docs/reference/agent-session-resume-cli-evidence.md @@ -0,0 +1,341 @@ +# Agent Session Resume Evidence for Sleep/Wake + +Date: June 5, 2026 + +Scope: issue [stablyai/orca#1796](https://github.com/stablyai/orca/issues/1796), current worktree code, installed agent CLIs on this machine, cloned open-source CLI repos under `/tmp/orca-agent-resume-research`, and official provider docs where available. + +## Executive conclusion + +Orca can implement one-click agent resume on wake, but it cannot use terminal PTY IDs, pane keys, tab IDs, or worktree IDs as the agent session ID. Those are Orca terminal identifiers. The required value is the provider's conversation/session/thread ID. + +For several first-tier providers, the exact provider ID is available in hook payloads today, but Orca drops it during normalization: + +- Claude: hook payloads include `session_id`; resume command is `claude --resume `. +- Codex: hook runtime sends `session_id`; resume command is `codex resume `. +- Gemini: hook payloads include `session_id`; resume command is `gemini --resume `. +- Antigravity: hook payloads include `conversationId`; resume command is `agy --conversation `. +- OpenCode: Orca's managed plugin already reads OpenCode `sessionID`; resume command is `opencode --session `. +- Droid: hook payloads include `session_id`; resume command is `droid --resume `. +- Grok: hook payloads include `sessionId`/`session_id`; resume command is `grok --resume `. + +The current blocker is not primarily "how do we resume?" The exact resume action is to spawn a new agent process on wake in the same workspace/pane context using the provider-specific resume command. The blocker is that Orca's current hook event and status types have no durable provider-session field, so the ID is discarded before sleep. + +## Current Orca behavior + +Issue #1796 is accurate about current behavior. The issue says sleep loses agent type, session ID, last prompt, and resume command because `dropAgentStatusByWorktree` wipes rows before wake can offer resume. + +Verified current code path: + +- Sleep calls terminal shutdown with identifiers preserved, not provider conversation metadata. See `src/renderer/src/components/sidebar/sleep-worktree-flow.ts`. +- `shutdownWorktreeTerminals(worktreeId, { keepIdentifiers: true })` preserves terminal IDs/layout wake hints, then calls `dropAgentStatusByWorktree(worktreeId)`. See `src/renderer/src/store/slices/terminals.ts`. +- `agentStatusByPaneKey` is explicitly documented as "Real-time only - lives in renderer memory, not persisted to disk." See `src/renderer/src/store/slices/agent-status.ts`. +- `WorkspaceSessionState` persists terminal/browser/editor/SSH state, including terminal layouts and remote relay PTY IDs, but no sleeping-agent records. See `src/shared/types.ts`, `src/renderer/src/lib/workspace-session.ts`, and `src/shared/workspace-session-schema.ts`. +- `AgentStatusEntry` has `state`, `prompt`, timestamps, `agentType`, pane/tab/worktree attribution, tool previews, assistant preview, and orchestration context. It has no provider session ID or resume command field. See `src/shared/agent-status-types.ts`. + +The existing sleep persistence is terminal-resume metadata. The feature needs provider-conversation-resume metadata. + +## Hook pipeline gap + +The current local and SSH hook wire shapes drop provider IDs: + +- `AgentHookEventPayload` in `src/shared/agent-hook-listener.ts` includes pane/tab/worktree, prompt cache fields, hook event name, Claude tool/subagent IDs, and normalized `payload`. It has no provider session ID field. +- `AgentHookRelayEnvelope` in `src/shared/agent-hook-relay.ts` mirrors that normalized shape for SSH. Its comment states the relay normalizes before sending the envelope. Therefore remote/SSH events lose provider IDs at the relay boundary too unless the new field is added to the shared envelope. +- `ParsedAgentStatusPayload` in `src/shared/agent-status-types.ts` includes only status fields: `state`, `prompt`, `agentType`, `toolName`, `toolInput`, `lastAssistantMessage`, `interrupted`. + +Implementation implication: provider-session metadata must be extracted before normalization completes and must be carried through both the local HTTP path and the SSH relay path. + +## Installed CLI versions checked + +These exact binaries were available on this machine: + +| Agent | Version checked | +| --- | --- | +| Claude Code | `2.1.165` | +| Codex | `codex-cli 0.137.0` | +| Gemini | `0.44.0` | +| OpenCode | `1.15.13` | +| Cursor Agent | `2025.09.18-7ae6800` | +| Antigravity | `1.0.5` | +| Droid | `0.122.0` | +| Grok | `grok 0.2.22 (967574cb117) [stable]` | +| Pi | `0.72.1` | +| Amp, Copilot, Command Code, Hermes | Not installed locally | + +## Provider matrix + +Legend: + +- `Exact supported`: exact resume command and exact ID source are both verified. +- `Resume verified, ID capture missing`: CLI can resume, but current Orca hook path does not expose a durable exact ID. +- `ID seen, resume not verified`: Orca/source exposes an ID, but no exact resume CLI command was verified. +- `Out of current hook scope`: Orca has no current hook source for this TUI agent. + +| Agent | Resume command evidence | Exact ID source evidence | Current Orca support status | +| --- | --- | --- | --- | +| Claude | `claude --resume ` verified by local `claude --help` and Anthropic CLI docs. | Claude hook docs show hook input includes `session_id` and `transcript_path`; current Orca normalizer drops `session_id`. | Exact supported after adding provider-session metadata extraction. | +| Codex | `codex resume ` verified by local help and cloned Codex source `codex-rs/utils/cli/src/resume_command.rs`. | Cloned Codex source `codex-rs/core/src/hook_runtime.rs` sends `session_id` on `SessionStart`, `PreToolUse`, `PermissionRequest`, and `PostToolUse` hook requests. | Exact supported after extraction. | +| Gemini | `gemini --resume ` verified by local help and Gemini docs. | Gemini hooks reference says all hooks receive `session_id` and `transcript_path`. | Exact supported after extraction. | +| Antigravity | `agy --conversation ` verified by local help and Antigravity docs. | Antigravity hook docs list `conversationId` as the active conversation UUID. | Exact supported after extraction. | +| OpenCode | `opencode --session ` verified by local help, official docs, and cloned OpenCode TUI source. | Orca's OpenCode plugin already reads `event.properties?.sessionID` before posting. | Exact supported after preserving `sessionID`. | +| Droid | `droid --resume ` verified by local help and Factory CLI docs. | Factory hooks reference includes `session_id`; Orca installs Droid hooks and already subscribes to `SessionStart`. | Exact supported after extraction. | +| Grok | `grok --resume ` verified by local help. | Orca already reads `sessionId`/`session_id` in `getGrokChatHistoryPath`, but only to locate chat history. | Exact supported after preserving that same ID. | +| Cursor | `cursor-agent --resume ` and `cursor-agent resume` verified by local help and Cursor docs. | Current Orca Cursor hook install deliberately omits `sessionStart`/`sessionEnd`; the subscribed event set does not currently prove a chat ID source. `cursor-agent create-chat` was locally verified to return a UUID, but preallocation for Orca launches still needs an end-to-end test before relying on it. | Resume verified, ID capture missing. | +| Pi | `pi --session `, `pi --resume`, and `pi --continue` verified by local help and Pi docs. | Orca's bundled Pi/OMP extension posts status events but does not include session file path, session UUID, or session name in the status payload. | Resume verified, ID capture missing. | +| OMP | Same launch family as Pi in Orca. | Orca reuses Pi extension API shape and does not include exact session pointer. | Resume verified by inheritance only from Pi-like CLI contract; exact OMP ID capture missing. | +| Amp | Orca's managed plugin posts `threadId: event.thread.id`. | Amp is not installed locally, and no exact Amp CLI resume command was verified in this investigation. | ID seen, resume not verified. | +| Hermes | Orca's managed Hermes plugin selects `session_id` for session/LLM/tool events. | Hermes is not installed locally, and no exact Hermes CLI resume command was verified in this investigation. | ID seen, resume not verified. | +| Copilot | Orca installs broad Copilot hooks. | Local Copilot CLI not installed; no exact resume command or session ID contract was verified. | Not supported for exact resume yet. | +| Command Code | Orca installs hooks for `PreToolUse`, `PostToolUse`, and `Stop`. | Local Command Code CLI not installed; no exact resume command or session ID contract was verified. | Not supported for exact resume yet. | +| openclaude, autohand, aider, goose, kilo, kiro, crush, aug, cline, codebuff, continue, kimi, mistral-vibe, qwen-code, rovo, openclaw | Not investigated to exact-resume standard. | Not in `HOOK_SOURCE_BY_PATHNAME` today. | Out of current hook scope. | + +## Exact resume behavior by provider + +### Claude + +Verified: + +- Local `claude --help` shows `--continue`, `--resume [value]`, `--session-id `, `--fork-session`, and `--no-session-persistence`. +- Anthropic CLI docs show `claude -r "" "query"` and document `--resume` as resuming a specific session by ID. +- Claude hooks docs show hook input includes `session_id` and `transcript_path`. + +Resume command: + +```sh +claude --resume +``` + +If Orca stores `lastPromptSummary` separately, it should not pass that summary as a new user prompt by default. The one-click resume action should reopen the prior conversation. Auto-submitting a summary would create a new turn. + +### Codex + +Verified: + +- Local `codex resume --help` accepts `[SESSION_ID] [PROMPT]`, where the session target is a UUID or session name. +- Cloned Codex source `codex-rs/utils/cli/src/resume_command.rs` formats `codex resume `. +- Cloned Codex source `codex-rs/core/src/hook_runtime.rs` sends `session_id` on hook requests. +- Cloned Codex source has tests asserting resumed root sessions use the thread ID as session ID. + +Resume command: + +```sh +codex resume +``` + +Avoid `codex resume --last` for this feature when an exact ID is available. `--last` can resume the wrong conversation if another Codex session ran after the slept workspace. + +### Gemini + +Verified: + +- Local `gemini --help` shows `--resume`, `--session-id`, `--session-file`, and `--list-sessions`. +- Gemini official hooks reference states all hook input includes `session_id` and `transcript_path`. +- Gemini official configuration docs describe `--resume [session_id]` and state that a UUID can be supplied. + +Resume command: + +```sh +gemini --resume +``` + +### Antigravity + +Verified: + +- Local `agy --help` shows `--continue` and `--conversation`. +- Antigravity conversation docs show `agy --continue` for most recent and `agy --conversation ` for a specific session. +- Antigravity hooks docs define common hook input `conversationId`. + +Resume command: + +```sh +agy --conversation +``` + +### OpenCode + +Verified: + +- Local `opencode --help` shows `--continue` and `--session`. +- Official OpenCode docs list `--session` as the session ID to continue. +- Cloned OpenCode source `packages/opencode/src/cli/cmd/tui/thread.ts` defines `--session` and passes it into TUI args as `sessionID`. +- Cloned OpenCode source `validate-session.ts` validates the supplied session ID through the OpenCode client. +- Orca's managed OpenCode plugin reads `event.properties?.sessionID`. + +Resume command: + +```sh +opencode --session +``` + +### Droid + +Verified: + +- Local `droid --help` shows `--resume [sessionId]`, `--fork `, and `--cwd`. +- Factory CLI docs list resume behavior and `droid exec -s ` for exec mode. +- Factory hooks reference says `SessionStart` has a `resume` matcher and hook input includes `session_id`. + +Resume command: + +```sh +droid --resume +``` + +### Grok + +Verified: + +- Local `grok --help` shows `--resume []`, `--continue`, `--cwd`, and `--restore-code`. +- Orca already reads `sessionId`/`session_id` in `getGrokChatHistoryPath`. + +Resume command: + +```sh +grok --resume +``` + +### Cursor + +Verified: + +- Local `cursor-agent --help` shows `--resume [chatId]`. +- Cursor docs show listing prior chats with `cursor-agent ls`, resuming latest with `cursor-agent resume`, and resuming a specific conversation with `cursor-agent --resume="chat-id-here"`. +- Local help shows `cursor-agent create-chat` creates an empty chat and returns its ID. +- Running `cursor-agent create-chat` locally returned UUID `1fff2e62-9dd0-4cf3-9d8c-3569fea0aff7`, proving the command can preallocate a chat ID without an initial prompt. + +Not verified: + +- Current Orca Cursor hook install subscribes to `beforeSubmitPrompt`, `stop`, tool events, approval events, and `afterAgentResponse`, but intentionally does not subscribe to `sessionStart`/`sessionEnd`. +- I did not verify a current subscribed Cursor hook payload containing `chatId`. +- I did not run `cursor-agent --resume ` because that would start a real agent turn and may make workspace changes. + +Conclusion: exact Cursor resume is CLI-supported, but Orca currently needs either a verified hook payload chat ID or a launch preallocation strategy that is tested end to end. + +### Pi and OMP + +Verified: + +- Local `pi --help` shows `--continue`, `--resume`, `--session `, `--fork `, `--session-dir`, and `--no-session`. +- Pi docs state `/resume` opens a picker and `pi -r` opens the same picker at startup. +- Orca's Pi/OMP extension posts status events from Pi's extension API, but the status events do not include session path, UUID, or session name. + +Resume command: + +```sh +pi --session +``` + +Conclusion: exact Pi resume is CLI-supported, but current Orca capture is missing the exact pointer. + +## What Orca should store + +Store a durable record per sleeping agent pane, not per worktree only: + +```ts +type SleepingAgentSessionRecord = { + id: string + worktreeId: string + tabId?: string + paneKey: string + connectionId: string | null + agentType: TuiAgent + providerSession: { + kind: 'session_id' | 'conversation_id' | 'thread_id' | 'chat_id' | 'path' + value: string + } + resumeCommand: { + argv: string[] + cwd?: string + } + lastPrompt: string + lastAssistantMessage?: string + capturedAt: number + retentionUntil?: number | null +} +``` + +Important constraints: + +- `resumeCommand.argv` should be structured argv, not a shell string. This avoids cross-platform quoting bugs. +- Do not persist a terminal PTY ID as `providerSession.value`. +- For SSH, `cwd` and the command execute on the remote host. The provider session ID also belongs to the remote CLI's session store. +- For local workspaces, execute locally in the worktree path. +- For auto-resume, launch exactly one process per sleeping agent record and guard against double-click/duplicate wake races. + +## Provider extraction map + +This is the extraction map that is supported by the evidence above: + +| Hook source | Provider session field(s) to extract | Resume argv | +| --- | --- | --- | +| `claude` | `session_id` | `['claude', '--resume', id]` | +| `codex` | `session_id` | `['codex', 'resume', id]` | +| `gemini` | `session_id` | `['gemini', '--resume', id]` | +| `antigravity` | `conversationId` | `['agy', '--conversation', id]` | +| `opencode` | `sessionID` | `['opencode', '--session', id]` | +| `droid` | `session_id` | `['droid', '--resume', id]` | +| `grok` | `sessionId`, fallback `session_id` | `['grok', '--resume', id]` | +| `amp` | `threadId` | not enabled until Amp resume command is verified | +| `hermes` | `session_id` | not enabled until Hermes resume command is verified | + +## Required implementation shape + +1. Add a provider-session metadata type to shared hook types. +2. Extract provider session evidence in `parseAgentHookEvent` from raw `hookPayloadRecord` before normalizing to `ParsedAgentStatusPayload`. +3. Carry it in `AgentHookEventPayload`. +4. Carry it in `AgentHookRelayEnvelope` so SSH does not lose it. +5. Store latest provider-session evidence by pane key alongside live agent status. +6. On sleep, snapshot the live agent status plus latest provider-session evidence before `dropAgentStatusByWorktree`. +7. Persist sleeping-agent records in `WorkspaceSessionState` and schema. +8. On wake, render sleeping resumable rows separately from active rows and launch the provider-specific structured argv when the user resumes. +9. Clear records on explicit worktree removal, not on sleep. +10. If the provider session no longer exists, let the provider CLI fail and surface that failure without deleting the record until the user dismisses it. + +## Open questions that remain genuinely unverified + +- Cursor: whether any currently subscribed Cursor hook payload carries `chatId`. If not, the only plausible exact strategy is preallocating a chat with `cursor-agent create-chat` at launch and starting/resuming that known chat. That needs an end-to-end test before productizing. +- Pi/OMP: whether the extension API can expose the current session file/path/UUID. Current Orca extension does not forward it. +- Amp: whether the Amp CLI has an exact thread resume command matching `threadId`. +- Hermes: whether Hermes has an exact session resume CLI command matching `session_id`. +- Copilot and Command Code: exact resume command and exact hook session field were not verified because the CLIs were not installed locally and no sufficient primary docs/source were found during this pass. +- Non-hook TUI agents: no exact support should be promised until Orca adds a hook/metadata capture path for each. + +## Sources + +Local Orca source: + +- `src/shared/agent-hook-listener.ts` +- `src/shared/agent-hook-relay.ts` +- `src/shared/agent-status-types.ts` +- `src/shared/types.ts` +- `src/shared/tui-agent-config.ts` +- `src/renderer/src/store/slices/terminals.ts` +- `src/renderer/src/store/slices/agent-status.ts` +- `src/renderer/src/lib/workspace-session.ts` +- `src/shared/workspace-session-schema.ts` +- `src/main/opencode/hook-service.ts` +- `src/main/amp/hook-service.ts` +- `src/main/hermes/hook-service.ts` +- `src/main/pi/agent-status-extension-source.ts` +- `src/main/cursor/hook-service.ts` + +Cloned source: + +- `/tmp/orca-agent-resume-research/codex` +- `/tmp/orca-agent-resume-research/gemini-cli` +- `/tmp/orca-agent-resume-research/opencode` + +Official docs: + +- GitHub issue: https://github.com/stablyai/orca/issues/1796 +- Claude CLI reference: https://docs.anthropic.com/en/docs/claude-code/cli-usage +- Claude hooks reference: https://code.claude.com/docs/en/hooks +- Gemini hooks reference: https://geminicli.com/docs/hooks/reference/ +- Cursor CLI parameters: https://docs.cursor.com/en/cli/reference/parameters +- Cursor CLI usage: https://docs.cursor.com/en/cli/using +- OpenCode CLI docs: https://opencode.ai/docs/cli/ +- Antigravity conversation docs: https://antigravity.google/docs/cli-conversations +- Antigravity hooks docs: https://www.antigravity.google/docs/hooks +- Factory Droid hooks reference: https://docs.factory.ai/cli/configuration/hooks-reference +- Factory Droid CLI reference: https://docs.factory.ai/cli/configuration/cli-reference +- Pi docs: https://pi.dev/docs/latest/tree diff --git a/src/main/agent-hooks/server.test.ts b/src/main/agent-hooks/server.test.ts index 313971bbc..c87c864b3 100644 --- a/src/main/agent-hooks/server.test.ts +++ b/src/main/agent-hooks/server.test.ts @@ -89,6 +89,7 @@ describe('AgentHookServer listener replay', () => { paneKey: PANE, tabId: 'tab-1', worktreeId: 'wt-1', + providerSession: { key: 'session_id', id: 'codex-interrupt-session-1' }, payload: { state: 'working', prompt: 'long task', agentType: 'codex' } }, 'conn-1' @@ -112,6 +113,7 @@ describe('AgentHookServer listener replay', () => { state: 'done', prompt: 'long task', agentType: 'codex', + providerSession: { key: 'session_id', id: 'codex-interrupt-session-1' }, interrupted: true, receivedAt: 1_500, stateStartedAt: 1_500 @@ -120,6 +122,7 @@ describe('AgentHookServer listener replay', () => { expect(listener).toHaveBeenLastCalledWith( expect.objectContaining({ paneKey: PANE, + providerSession: { key: 'session_id', id: 'codex-interrupt-session-1' }, payload: expect.objectContaining({ state: 'done', interrupted: true }) }) ) diff --git a/src/main/agent-hooks/server.ts b/src/main/agent-hooks/server.ts index 1ecbeb61d..f9cb59fec 100644 --- a/src/main/agent-hooks/server.ts +++ b/src/main/agent-hooks/server.ts @@ -55,6 +55,7 @@ import { } from '../../shared/agent-interrupt-intent' import { parseLegacyNumericPaneKey, parsePaneKey } from '../../shared/stable-pane-id' import type { LegacyPaneKeyAliasEntry } from '../../shared/types' +import { normalizeAgentProviderSession } from '../../shared/agent-session-resume' export type { AgentHookSource } @@ -222,6 +223,7 @@ function sanitizeHydratedEntry( toolUseId: typeof record.toolUseId === 'string' ? record.toolUseId : undefined, toolAgentId: typeof record.toolAgentId === 'string' ? record.toolAgentId : undefined, toolAgentType: typeof record.toolAgentType === 'string' ? record.toolAgentType : undefined, + providerSession: normalizeAgentProviderSession(record.providerSession) ?? undefined, payload, receivedAt, stateStartedAt @@ -236,6 +238,7 @@ function toAgentStatusIpcPayload(entry: EnrichedAgentHookEventPayload): AgentSta connectionId: entry.connectionId, receivedAt: entry.receivedAt, stateStartedAt: entry.stateStartedAt, + ...(entry.providerSession ? { providerSession: entry.providerSession } : {}), ...entry.payload } } @@ -525,6 +528,7 @@ export class AgentHookServer { tabId: existing.tabId, worktreeId: existing.worktreeId, connectionId: existing.connectionId, + providerSession: existing.providerSession, payload: { state: 'done', prompt: payload.prompt, @@ -982,6 +986,7 @@ export class AgentHookServer { toolUseId?: string toolAgentId?: string toolAgentType?: string + providerSession?: unknown isReplay?: boolean payload: unknown }, @@ -1056,6 +1061,7 @@ export class AgentHookServer { typeof envelope.toolAgentType === 'string' && envelope.toolAgentType.trim().length > 0 ? envelope.toolAgentType.trim() : undefined + const providerSession = normalizeAgentProviderSession(envelope.providerSession) ?? undefined // Why: the relay is across a trust boundary; re-run the canonical // normalizer on the inner payload so prompt/agentType/toolName/toolInput // length caps, embedded-newline collapse, and the `interrupted`-only-on- @@ -1085,6 +1091,7 @@ export class AgentHookServer { toolUseId, toolAgentId, toolAgentType, + providerSession, isReplay: envelope.isReplay === true ? true : undefined, payload: normalizedPayload } diff --git a/src/main/index.ts b/src/main/index.ts index 5bfd03371..6a6f1c5d9 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -607,6 +607,7 @@ function openMainWindow(): BrowserWindow { payload, receivedAt, stateStartedAt, + providerSession, isReplay }) => { if (mainWindow?.isDestroyed()) { @@ -624,6 +625,7 @@ function openMainWindow(): BrowserWindow { connectionId, receivedAt, stateStartedAt, + ...(providerSession ? { providerSession } : {}), ...(orchestration ? { orchestration } : {}) }) recordAgentStateCrashBreadcrumb(payload.agentType ?? 'unknown', payload.state) diff --git a/src/main/opencode/hook-service.test.ts b/src/main/opencode/hook-service.test.ts index a07963b67..add20ca79 100644 --- a/src/main/opencode/hook-service.test.ts +++ b/src/main/opencode/hook-service.test.ts @@ -110,6 +110,16 @@ describe('OpenCode hook plugin source', () => { expect(source).toContain('await post("AskUserQuestion", event.properties || {});') }) + it('forwards sessionID on status and message posts for resume metadata', () => { + const source = _internals.getOpenCodePluginSource() + + expect(source).toContain( + 'await post("MessagePart", { role, text: part.text, messageID: part.messageID, sessionID });' + ) + expect(source).toContain('await setStatus("busy", { sessionID });') + expect(source.match(/await setStatus\("idle", \{ sessionID \}\);/g) ?? []).toHaveLength(2) + }) + it('guards endpoint-file parse warnings with a process-lifetime latch', () => { // Why: ENOENT is the normal pre-install case and must stay silent, but a // malformed/unreadable file (EACCES, EIO, parse error) would otherwise diff --git a/src/main/opencode/hook-service.ts b/src/main/opencode/hook-service.ts index cfb723ede..ff0d9bde7 100644 --- a/src/main/opencode/hook-service.ts +++ b/src/main/opencode/hook-service.ts @@ -312,23 +312,23 @@ function getOpenCodePluginSource(): string { ' if (!part || part.type !== "text" || !part.text) return;', ' const role = messageRoleById.get(part.messageID);', ' if (!role) return;', - ' await post("MessagePart", { role, text: part.text, messageID: part.messageID });', + ' await post("MessagePart", { role, text: part.text, messageID: part.messageID, sessionID });', ' return;', ' }', '', ' if (event.type === "session.idle" || event.type === "session.error") {', - ' await setStatus("idle");', + ' await setStatus("idle", { sessionID });', ' return;', ' }', '', ' if (event.type === "session.status") {', ' const statusType = getStatusType(event);', ' if (statusType === "busy" || statusType === "retry") {', - ' await setStatus("busy");', + ' await setStatus("busy", { sessionID });', ' return;', ' }', ' if (statusType === "idle") {', - ' await setStatus("idle");', + ' await setStatus("idle", { sessionID });', ' }', ' }', ' },', diff --git a/src/main/ssh/ssh-relay-session-agent-hooks.integration.test.ts b/src/main/ssh/ssh-relay-session-agent-hooks.integration.test.ts index 057e095de..31b357ca0 100644 --- a/src/main/ssh/ssh-relay-session-agent-hooks.integration.test.ts +++ b/src/main/ssh/ssh-relay-session-agent-hooks.integration.test.ts @@ -423,6 +423,7 @@ describe('SshRelaySession agent hooks over a fake relay transport', () => { toolUseId: 'toolu-1', toolAgentId: 'agent-subagent-a', toolAgentType: 'Review', + providerSession: { key: 'session_id', id: 'ssh-relay-session-1' }, payload: { state: 'working', prompt: 'remote prompt', @@ -440,7 +441,8 @@ describe('SshRelaySession agent hooks over a fake relay transport', () => { promptInteractionKey: 'command-code-transcript-user-3', toolUseId: 'toolu-1', toolAgentId: 'agent-subagent-a', - toolAgentType: 'Review' + toolAgentType: 'Review', + providerSession: { key: 'session_id', id: 'ssh-relay-session-1' } }), 'conn-hook-metadata' ) diff --git a/src/main/ssh/ssh-relay-session.ts b/src/main/ssh/ssh-relay-session.ts index 19d94c357..d9568a333 100644 --- a/src/main/ssh/ssh-relay-session.ts +++ b/src/main/ssh/ssh-relay-session.ts @@ -738,6 +738,7 @@ export class SshRelaySession { toolAgentId?: unknown toolAgentType?: unknown isReplay?: unknown + providerSession?: unknown payload?: unknown } if (typeof envelope.paneKey !== 'string') { @@ -767,6 +768,7 @@ export class SshRelaySession { toolAgentType: typeof envelope.toolAgentType === 'string' ? envelope.toolAgentType : undefined, isReplay: envelope.isReplay === true ? true : undefined, + providerSession: envelope.providerSession, payload: envelope.payload }, this.targetId diff --git a/src/relay/agent-hook-server.ts b/src/relay/agent-hook-server.ts index 2dd9c23b0..fa404b88f 100644 --- a/src/relay/agent-hook-server.ts +++ b/src/relay/agent-hook-server.ts @@ -297,6 +297,7 @@ export class RelayAgentHookServer { toolUseId: event.toolUseId, toolAgentId: event.toolAgentId, toolAgentType: event.toolAgentType, + ...(event.providerSession ? { providerSession: event.providerSession } : {}), isReplay: options.isReplay === true ? true : undefined, env, version, diff --git a/src/renderer/src/components/sidebar/WorktreeCardAgents.tsx b/src/renderer/src/components/sidebar/WorktreeCardAgents.tsx index a97b1637c..2a941ed7f 100644 --- a/src/renderer/src/components/sidebar/WorktreeCardAgents.tsx +++ b/src/renderer/src/components/sidebar/WorktreeCardAgents.tsx @@ -396,7 +396,7 @@ const WorktreeCardAgentsBody = React.memo(function WorktreeCardAgentsBody({ aria-label="Agents" data-compact-agent-list="true" > - {shouldUseSummaryRow ? ( + {agents.length === 0 ? null : shouldUseSummaryRow ? ( // Why: the worktree card is already the surface. Expanded compact // agents stay a quiet tree; only the collapsed summary reads as a pill.
{ expect.objectContaining({ state: 'working', prompt: 'p', agentType: 'claude' }), 'Future Tab', { updatedAt: 1_700_000_000_000, stateStartedAt: 1_699_999_999_000 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -3519,7 +3520,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), 'Inactive Tab', { updatedAt: 1_700_000_000_200, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -3609,7 +3611,8 @@ describe('useIpcEvents agent status snapshot integration', () => { expect.objectContaining({ state: 'working', prompt: 'hidden worker', agentType: 'codex' }), undefined, { updatedAt: 1_700_000_000_200, stateStartedAt: 1_700_000_000_000 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) onClearListenerRef.current({ paneKey: FUTURE_PANE_KEY }) @@ -3752,7 +3755,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), 'Cursor ready', { updatedAt: 1_700_000_000_200, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -3836,7 +3840,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), 'Codex ready', { updatedAt: 1_700_000_000_200, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) expect(updateTabTitle).toHaveBeenCalledTimes(1) expect(updateTabTitle).toHaveBeenCalledWith('tab-future', 'Codex ready') @@ -4000,7 +4005,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), 'Terminal 2', { updatedAt: 1_700_000_000_200, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -4075,7 +4081,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), 'Inactive Tab', { updatedAt: 1_700_000_000_200, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -4178,7 +4185,8 @@ describe('useIpcEvents agent status snapshot integration', () => { expect.objectContaining({ state: 'working', prompt: 'queued prompt', agentType: 'codex' }), 'Future Tab', { updatedAt: 1_700_000_000_100, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) expect(setAgentStatus).toHaveBeenNthCalledWith( 2, @@ -4191,7 +4199,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), 'Future Tab', { updatedAt: 1_700_000_000_200, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -4256,7 +4265,8 @@ describe('useIpcEvents agent status snapshot integration', () => { expect.objectContaining({ state: 'working', prompt: 'remote p', agentType: 'codex' }), 'SSH Tab', { updatedAt: 1_700_000_000_000, stateStartedAt: 1_699_999_999_000 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) @@ -4703,7 +4713,8 @@ describe('useIpcEvents agent status snapshot integration', () => { }), undefined, { updatedAt: 1_700_000_000_000, stateStartedAt: 1_699_999_999_000 }, - expect.objectContaining({ worktreeId: 'wt-1', terminalHandle: 'term-child' }) + expect.objectContaining({ worktreeId: 'wt-1', terminalHandle: 'term-child' }), + undefined ) }) @@ -4818,7 +4829,8 @@ describe('useIpcEvents agent status snapshot integration', () => { expect.objectContaining({ state: 'working' }), 'Terminal 1', { updatedAt: 1_700_000_000_100, stateStartedAt: 1_699_999_999_100 }, - expectWorktreeRouting('wt-1') + expectWorktreeRouting('wt-1'), + undefined ) }) diff --git a/src/renderer/src/hooks/useIpcEvents.ts b/src/renderer/src/hooks/useIpcEvents.ts index a07fcb96c..42af0283e 100644 --- a/src/renderer/src/hooks/useIpcEvents.ts +++ b/src/renderer/src/hooks/useIpcEvents.ts @@ -2488,7 +2488,8 @@ export function useIpcEvents(): void { tabId: data.tabId, worktreeId: statusWorktreeId, terminalHandle: data.terminalHandle - } + }, + data.providerSession ? { providerSession: data.providerSession } : undefined ) applyResolvedAgentTerminalTitleToTab(store, data.paneKey, title, terminalTitle) if (options?.replay !== true && statusWorktreeId) { diff --git a/src/renderer/src/lib/resume-sleeping-agent-session.ts b/src/renderer/src/lib/resume-sleeping-agent-session.ts new file mode 100644 index 000000000..2b26d8d48 --- /dev/null +++ b/src/renderer/src/lib/resume-sleeping-agent-session.ts @@ -0,0 +1,80 @@ +import { toast } from 'sonner' +import { useAppStore } from '@/store' +import { CLIENT_PLATFORM } from '@/lib/new-workspace' +import { buildAgentResumeStartupPlan } from '@/lib/tui-agent-startup' +import { tuiAgentToAgentKind } from '@/lib/telemetry' +import { reconcileTabOrder } from '@/components/tab-bar/reconcile-order' +import { isWslUncPath } from '../../../shared/wsl-paths' +import type { SleepingAgentSessionRecord } from '../../../shared/agent-session-resume' + +function getResumeLaunchPlatform(worktreeId: string): NodeJS.Platform { + const state = useAppStore.getState() + const worktree = state.getKnownWorktreeById(worktreeId) + const repo = worktree ? state.repos.find((entry) => entry.id === worktree.repoId) : null + if (repo?.connectionId || (worktree?.path && isWslUncPath(worktree.path))) { + return 'linux' + } + return CLIENT_PLATFORM +} + +function appendTabToWorktreeOrder(worktreeId: string, tabId: string): void { + const state = useAppStore.getState() + const termIds = (state.tabsByWorktree[worktreeId] ?? []).map((tab) => tab.id) + const editorIds = state.openFiles + .filter((file) => file.worktreeId === worktreeId) + .map((f) => f.id) + const browserIds = (state.browserTabsByWorktree?.[worktreeId] ?? []).map((tab) => tab.id) + const base = reconcileTabOrder( + state.tabBarOrderByWorktree[worktreeId], + termIds, + editorIds, + browserIds + ) + const order = base.filter((id) => id !== tabId) + order.push(tabId) + state.setTabBarOrder(worktreeId, order) +} + +function launchSleepingAgentSession(record: SleepingAgentSessionRecord): boolean { + const state = useAppStore.getState() + const startupPlan = buildAgentResumeStartupPlan({ + agent: record.agent, + providerSession: record.providerSession, + cmdOverrides: state.settings?.agentCmdOverrides ?? {}, + platform: getResumeLaunchPlatform(record.worktreeId) + }) + if (!startupPlan) { + toast.error('This agent session cannot be resumed.') + return false + } + + const tab = state.createTab(record.worktreeId, undefined, undefined, { + launchAgent: record.agent + }) + state.queueTabStartupCommand(tab.id, { + command: startupPlan.launchCommand, + telemetry: { + agent_kind: tuiAgentToAgentKind(record.agent), + launch_source: 'sidebar', + request_kind: 'resume' + } + }) + state.clearSleepingAgentSession(record.paneKey) + state.setActiveTabType('terminal') + appendTabToWorktreeOrder(record.worktreeId, tab.id) + return true +} + +export function resumeSleepingAgentSessionsForWorktree(worktreeId: string): number { + const records = Object.values(useAppStore.getState().sleepingAgentSessionsByPaneKey) + .filter((record) => record.worktreeId === worktreeId) + .sort((a, b) => a.capturedAt - b.capturedAt || a.updatedAt - b.updatedAt) + + let launched = 0 + for (const record of records) { + if (launchSleepingAgentSession(record)) { + launched += 1 + } + } + return launched +} diff --git a/src/renderer/src/lib/tui-agent-startup.ts b/src/renderer/src/lib/tui-agent-startup.ts index 6427aa17d..478a9d9d0 100644 --- a/src/renderer/src/lib/tui-agent-startup.ts +++ b/src/renderer/src/lib/tui-agent-startup.ts @@ -1,4 +1,5 @@ export { + buildAgentResumeStartupPlan, buildAgentDraftLaunchPlan, buildAgentStartupPlan, isShellProcess diff --git a/src/renderer/src/lib/workspace-session-focus-recency.ts b/src/renderer/src/lib/workspace-session-focus-recency.ts new file mode 100644 index 000000000..2db182089 --- /dev/null +++ b/src/renderer/src/lib/workspace-session-focus-recency.ts @@ -0,0 +1,10 @@ +import type { WorkspaceSessionState } from '../../../shared/types' + +export function buildLastVisitedAtByWorktreeId(snapshot: { + lastVisitedAtByWorktreeId: WorkspaceSessionState['lastVisitedAtByWorktreeId'] +}): WorkspaceSessionState['lastVisitedAtByWorktreeId'] { + return snapshot.lastVisitedAtByWorktreeId && + Object.keys(snapshot.lastVisitedAtByWorktreeId).length > 0 + ? snapshot.lastVisitedAtByWorktreeId + : undefined +} diff --git a/src/renderer/src/lib/workspace-session-patch.test.ts b/src/renderer/src/lib/workspace-session-patch.test.ts index f36d10822..6d2cad8c2 100644 --- a/src/renderer/src/lib/workspace-session-patch.test.ts +++ b/src/renderer/src/lib/workspace-session-patch.test.ts @@ -245,4 +245,14 @@ describe('buildWorkspaceSessionPatch', () => { expect(Object.hasOwn(patch, 'defaultTerminalTabsAppliedByWorktreeId')).toBe(true) expect(patch.defaultTerminalTabsAppliedByWorktreeId).toBeUndefined() }) + + it('keeps sleeping agent session clearing keys in patches', () => { + const patch = buildWorkspaceSessionPatch( + createSnapshot({ sleepingAgentSessionsByPaneKey: {} }), + ['sleepingAgentSessionsByPaneKey'] + ) + + expect(Object.hasOwn(patch, 'sleepingAgentSessionsByPaneKey')).toBe(true) + expect(patch.sleepingAgentSessionsByPaneKey).toBeUndefined() + }) }) diff --git a/src/renderer/src/lib/workspace-session-patch.ts b/src/renderer/src/lib/workspace-session-patch.ts index 19f0edad7..b1b9e4d8c 100644 --- a/src/renderer/src/lib/workspace-session-patch.ts +++ b/src/renderer/src/lib/workspace-session-patch.ts @@ -4,13 +4,14 @@ import { normalizeBrowserHistoryEntries } from '../../../shared/workspace-sessio import { buildActiveConnectionIdsAtShutdown, buildEditorSessionData, - buildLastVisitedAtByWorktreeId, buildPersistedBrowserPagesByWorkspace, buildPersistedBrowserTabsByWorktree, buildSanitizedTabsByWorktree, buildTerminalSessionData, type WorkspaceSessionSnapshot } from './workspace-session' +import { buildLastVisitedAtByWorktreeId } from './workspace-session-focus-recency' +import { buildSleepingAgentSessionData } from './workspace-session-sleeping-agents' type SessionRelevantField = keyof WorkspaceSessionSnapshot @@ -133,6 +134,10 @@ export function buildWorkspaceSessionPatch( ? snapshot.defaultTerminalTabsAppliedByWorktreeId : undefined } + if (changed.has('sleepingAgentSessionsByPaneKey')) { + patch.sleepingAgentSessionsByPaneKey = + buildSleepingAgentSessionData(snapshot).sleepingAgentSessionsByPaneKey + } return patch } diff --git a/src/renderer/src/lib/workspace-session-relevant-fields.test.ts b/src/renderer/src/lib/workspace-session-relevant-fields.test.ts index df9794e4d..d9ab05f5b 100644 --- a/src/renderer/src/lib/workspace-session-relevant-fields.test.ts +++ b/src/renderer/src/lib/workspace-session-relevant-fields.test.ts @@ -30,7 +30,8 @@ describe('SESSION_RELEVANT_FIELDS', () => { worktreesByRepo: true, lastKnownRelayPtyIdByTabId: true, lastVisitedAtByWorktreeId: true, - defaultTerminalTabsAppliedByWorktreeId: true + defaultTerminalTabsAppliedByWorktreeId: true, + sleepingAgentSessionsByPaneKey: true } it('contains every key of WorkspaceSessionSnapshot', () => { diff --git a/src/renderer/src/lib/workspace-session-sleeping-agents.ts b/src/renderer/src/lib/workspace-session-sleeping-agents.ts new file mode 100644 index 000000000..fc54bc1c5 --- /dev/null +++ b/src/renderer/src/lib/workspace-session-sleeping-agents.ts @@ -0,0 +1,10 @@ +import type { WorkspaceSessionState } from '../../../shared/types' + +export function buildSleepingAgentSessionData(snapshot: { + sleepingAgentSessionsByPaneKey?: WorkspaceSessionState['sleepingAgentSessionsByPaneKey'] +}): Pick { + const records = snapshot.sleepingAgentSessionsByPaneKey + return records && Object.keys(records).length > 0 + ? { sleepingAgentSessionsByPaneKey: records } + : {} +} diff --git a/src/renderer/src/lib/workspace-session.ts b/src/renderer/src/lib/workspace-session.ts index 4ac7dbe11..7c48fadcb 100644 --- a/src/renderer/src/lib/workspace-session.ts +++ b/src/renderer/src/lib/workspace-session.ts @@ -9,6 +9,8 @@ import { pruneLocalTerminalScrollbackBuffers } from '../../../shared/workspace-s import { normalizeBrowserHistoryEntries } from '../../../shared/workspace-session-browser-history' import type { AppState } from '../store' import type { OpenFile } from '../store/slices/editor' +import { buildLastVisitedAtByWorktreeId } from './workspace-session-focus-recency' +import { buildSleepingAgentSessionData } from './workspace-session-sleeping-agents' /** Why (issue #1158): the debounced + shutdown session writers share this * gate so a hydration failure cannot overwrite orca-data.json with the @@ -55,7 +57,9 @@ export type WorkspaceSessionSnapshot = Pick< | 'lastKnownRelayPtyIdByTabId' | 'lastVisitedAtByWorktreeId' | 'defaultTerminalTabsAppliedByWorktreeId' -> +> & { + sleepingAgentSessionsByPaneKey?: AppState['sleepingAgentSessionsByPaneKey'] +} // Why: the App-level Zustand subscriber that debounces session writes uses // this list as a shallow-equality gate so it only resets the timer when a @@ -89,7 +93,8 @@ export const SESSION_RELEVANT_FIELDS = [ 'worktreesByRepo', 'lastKnownRelayPtyIdByTabId', 'lastVisitedAtByWorktreeId', - 'defaultTerminalTabsAppliedByWorktreeId' + 'defaultTerminalTabsAppliedByWorktreeId', + 'sleepingAgentSessionsByPaneKey' ] as const satisfies readonly (keyof WorkspaceSessionSnapshot)[] type _MissingSessionField = Exclude< @@ -320,15 +325,6 @@ export function buildActiveConnectionIdsAtShutdown( return connectedTargetIds.length > 0 ? connectedTargetIds : undefined } -export function buildLastVisitedAtByWorktreeId( - snapshot: WorkspaceSessionSnapshot -): WorkspaceSessionState['lastVisitedAtByWorktreeId'] { - return snapshot.lastVisitedAtByWorktreeId && - Object.keys(snapshot.lastVisitedAtByWorktreeId).length > 0 - ? snapshot.lastVisitedAtByWorktreeId - : undefined -} - export function buildWorkspaceSessionPayload( snapshot: WorkspaceSessionSnapshot ): WorkspaceSessionState { @@ -376,7 +372,8 @@ export function buildWorkspaceSessionPayload( snapshot.defaultTerminalTabsAppliedByWorktreeId && Object.keys(snapshot.defaultTerminalTabsAppliedByWorktreeId).length > 0 ? snapshot.defaultTerminalTabsAppliedByWorktreeId - : undefined + : undefined, + ...buildSleepingAgentSessionData(snapshot) } return pruneLocalTerminalScrollbackBuffers(payload, snapshot.repos) diff --git a/src/renderer/src/lib/worktree-activation-created-agent.test.ts b/src/renderer/src/lib/worktree-activation-created-agent.test.ts index 541c6d7af..d27f5970f 100644 --- a/src/renderer/src/lib/worktree-activation-created-agent.test.ts +++ b/src/renderer/src/lib/worktree-activation-created-agent.test.ts @@ -218,6 +218,92 @@ describe('activateAndRevealWorktree created agent reopen', () => { expect(revealWorktreeInSidebar).toHaveBeenCalledWith(worktree.id) }) + it('automatically resumes sleeping agent sessions when activating a slept worktree', () => { + const worktree = makeWorktree() + const revealWorktreeInSidebar = vi.fn() + + useAppStore.setState({ + repos: [ + { + id: 'repo-1', + path: '/workspace/repo', + displayName: 'repo', + badgeColor: '#000000', + addedAt: 0 + } + ], + worktreesByRepo: { 'repo-1': [worktree] }, + activeRepoId: 'repo-1', + activeView: 'terminal', + tabsByWorktree: { + [worktree.id]: [ + { + id: 'slept-tab', + ptyId: 'wake-hint', + worktreeId: worktree.id, + title: 'Codex', + customTitle: null, + color: null, + sortOrder: 0, + createdAt: 1 + } + ] + }, + ptyIdsByTabId: {}, + unifiedTabsByWorktree: {}, + groupsByWorktree: {}, + layoutByWorktree: {}, + activeGroupIdByWorktree: {}, + openFiles: [], + browserTabsByWorktree: {}, + activeFileIdByWorktree: {}, + activeBrowserTabIdByWorktree: {}, + activeTabTypeByWorktree: {}, + activeTabIdByWorktree: {}, + tabBarOrderByWorktree: {}, + pendingStartupByTabId: {}, + sleepingAgentSessionsByPaneKey: { + 'slept-tab:0': { + paneKey: 'slept-tab:0', + tabId: 'slept-tab', + worktreeId: worktree.id, + agent: 'codex', + providerSession: { key: 'session_id', id: 'codex-session-1' }, + prompt: 'resume prior task', + state: 'working', + capturedAt: 1000, + updatedAt: 1000, + terminalTitle: 'Codex' + } + }, + settings: { + agentCmdOverrides: {}, + setupScriptLaunchMode: 'new-tab' + } as unknown as ReturnType['settings'], + markWorktreeVisited: vi.fn(), + recordWorktreeVisit: vi.fn(), + refreshGitHubForWorktreeIfStale: vi.fn(), + revealWorktreeInSidebar + }) + + const result = activateAndRevealWorktree(worktree.id) + const state = useAppStore.getState() + const resumedTab = state.tabsByWorktree[worktree.id]?.find((tab) => tab.id !== 'slept-tab') + + expect(result).toEqual({ primaryTabId: null }) + expect(resumedTab?.launchAgent).toBe('codex') + expect(state.pendingStartupByTabId[resumedTab!.id]).toEqual({ + command: "codex 'resume' 'codex-session-1'", + telemetry: { + agent_kind: 'codex', + launch_source: 'sidebar', + request_kind: 'resume' + } + }) + expect(state.sleepingAgentSessionsByPaneKey['slept-tab:0']).toBeUndefined() + expect(revealWorktreeInSidebar).toHaveBeenCalledWith(worktree.id) + }) + it('forwards an explicit sidebar reveal behavior', () => { const worktree = makeWorktree() const revealWorktreeInSidebar = vi.fn() diff --git a/src/renderer/src/lib/worktree-activation.ts b/src/renderer/src/lib/worktree-activation.ts index bfbd370f3..38090b593 100644 --- a/src/renderer/src/lib/worktree-activation.ts +++ b/src/renderer/src/lib/worktree-activation.ts @@ -32,6 +32,7 @@ import { setWorktreeNavViewActivator } from '@/store/slices/worktree-nav-history' import { isTuiAgent } from '../../../shared/tui-agent-config' +import { resumeSleepingAgentSessionsForWorktree } from '@/lib/resume-sleeping-agent-session' /** Telemetry payload threaded from the launch site to `pty:spawn`. Main * fires `agent_started` only after the spawn succeeds — see @@ -216,6 +217,11 @@ export function activateAndRevealWorktree( state.recordWorktreeVisit(worktreeId) } + // Why: sleeping an agent destroys the local PTY but preserves the provider + // session id. Waking the workspace should restore those CLI sessions without + // making the user click a separate sidebar row. + resumeSleepingAgentSessionsForWorktree(worktreeId) + // 4. Ensure a focusable surface exists for externally-created worktrees const primaryTabId = ensureWorktreeHasInitialTerminal( useAppStore.getState(), diff --git a/src/renderer/src/store/slices/agent-status.ts b/src/renderer/src/store/slices/agent-status.ts index ad4cecd91..8cf381720 100644 --- a/src/renderer/src/store/slices/agent-status.ts +++ b/src/renderer/src/store/slices/agent-status.ts @@ -11,6 +11,12 @@ import { type MigrationUnsupportedPtyEntry, type ParsedAgentStatusPayload } from '../../../../shared/agent-status-types' +import { + getAgentResumeArgv, + isResumableTuiAgent, + type AgentProviderSessionMetadata, + type SleepingAgentSessionRecord +} from '../../../../shared/agent-session-resume' import { resolveAgentStatusIdentity, shouldSuppressInheritedTerminalStatus @@ -54,6 +60,10 @@ export type AgentStatusSlice = { * agent-status hover so the two surfaces display identical rows. */ retainedAgentsByPaneKey: Record + /** Durable agent sessions captured when a workspace sleeps. These are not + * live status rows; they power the one-click CLI resume action on wake. */ + sleepingAgentSessionsByPaneKey: Record + /** Pane keys explicitly torn down (pane close, tab close, PTY exit, manual * dismissal) and therefore forbidden from being re-retained on their next * disappearance. Consumed by the retention sync as a one-shot suppressor. */ @@ -65,7 +75,8 @@ export type AgentStatusSlice = { payload: ParsedAgentStatusPayload & { orchestration?: AgentStatusOrchestrationContext }, terminalTitle?: string, timing?: { updatedAt?: number; stateStartedAt?: number }, - routing?: { tabId?: string; worktreeId?: string; terminalHandle?: string } + routing?: { tabId?: string; worktreeId?: string; terminalHandle?: string }, + metadata?: { providerSession?: AgentProviderSessionMetadata } ) => void setRuntimeAgentOrchestrationByPaneKey: ( @@ -101,6 +112,11 @@ export type AgentStatusSlice = { * survive sleep/remove. */ dropAgentStatusByWorktree: (worktreeId: string) => void + captureSleepingAgentSessionsByWorktree: (worktreeId: string) => void + clearSleepingAgentSession: (paneKey: string) => void + clearSleepingAgentSessionsByWorktree: (worktreeId: string) => void + pruneSleepingAgentSessions: (validWorktreeIds: Set) => void + /** Retain agent snapshots (called by the top-level retention sync effect). * Accepts an array so multiple agents disappearing in the same frame * produce a single set(...) — avoids intermediate states visible @@ -155,6 +171,52 @@ function findAgentPaneWorktreeId(state: AppState, paneKey: string): string | nul return null } +function findTabForAgentEntry( + state: AppState, + worktreeId: string, + entry: AgentStatusEntry +): TerminalTab | undefined { + const tabId = entry.tabId ?? getTabIdFromPaneKey(entry.paneKey) + if (!tabId) { + return undefined + } + return (state.tabsByWorktree[worktreeId] ?? []).find((tab) => tab.id === tabId) +} + +function sleepingRecordFromEntry(args: { + state: AppState + entry: AgentStatusEntry + worktreeId: string + tab?: TerminalTab + capturedAt: number +}): SleepingAgentSessionRecord | null { + const agent = args.entry.agentType + if (!isResumableTuiAgent(agent) || !args.entry.providerSession) { + return null + } + if (!getAgentResumeArgv(agent, args.entry.providerSession)) { + return null + } + const tab = args.tab ?? findTabForAgentEntry(args.state, args.worktreeId, args.entry) + return { + paneKey: args.entry.paneKey, + ...(tab ? { tabId: tab.id } : {}), + worktreeId: args.worktreeId, + agent, + providerSession: args.entry.providerSession, + prompt: args.entry.prompt, + state: args.entry.state, + capturedAt: args.capturedAt, + updatedAt: args.entry.updatedAt, + ...((args.entry.terminalTitle ?? tab?.title) + ? { terminalTitle: (args.entry.terminalTitle ?? tab?.title)! } + : {}), + ...(args.entry.lastAssistantMessage + ? { lastAssistantMessage: args.entry.lastAssistantMessage } + : {}) + } +} + function pruneMigrationUnsupportedEntries( entries: Record, predicate: (entry: MigrationUnsupportedPtyEntry) => boolean @@ -243,6 +305,7 @@ export const createAgentStatusSlice: StateCreator { @@ -304,7 +367,7 @@ export const createAgentStatusSlice: StateCreator { + setAgentStatus: (paneKey, payload, terminalTitle, timing, routing, metadata) => { const updatedAt = timing?.updatedAt ?? Date.now() let completionRefreshWorktreeId: string | null = null let suppressedInheritedTerminalStatus = false @@ -399,6 +462,9 @@ export const createAgentStatusSlice: StateCreator entry.paneKey === paneKey ) + const hasSleepingRecord = paneKey in s.sleepingAgentSessionsByPaneKey + const nextSleepingAgentSessions = hasSleepingRecord + ? { ...s.sleepingAgentSessionsByPaneKey } + : s.sleepingAgentSessionsByPaneKey + if (hasSleepingRecord) { + delete nextSleepingAgentSessions[paneKey] + } return { agentStatusByPaneKey: { ...s.agentStatusByPaneKey, [paneKey]: entry }, + sleepingAgentSessionsByPaneKey: nextSleepingAgentSessions, migrationUnsupportedByPtyId: migrationUnsupported.next, retentionSuppressedPaneKeys: nextRetentionSuppressedPaneKeys, agentStatusEpoch: @@ -934,6 +1010,95 @@ export const createAgentStatusSlice: StateCreator { + set((s) => { + const capturedAt = Date.now() + const tabPrefixes = (s.tabsByWorktree[worktreeId] ?? []).map((tab) => `${tab.id}:`) + const next: Record = { + ...s.sleepingAgentSessionsByPaneKey + } + let changed = false + + for (const retained of Object.values(s.retainedAgentsByPaneKey)) { + if (retained.worktreeId !== worktreeId) { + continue + } + const record = sleepingRecordFromEntry({ + state: s, + entry: retained.entry, + worktreeId, + tab: retained.tab, + capturedAt + }) + if (record && next[record.paneKey] !== record) { + next[record.paneKey] = record + changed = true + } + } + + for (const [paneKey, entry] of Object.entries(s.agentStatusByPaneKey)) { + const belongsToWorktree = + entry.worktreeId === worktreeId || paneKeyMatchesAnyTabPrefix(paneKey, tabPrefixes) + if (!belongsToWorktree) { + continue + } + const record = sleepingRecordFromEntry({ + state: s, + entry, + worktreeId, + capturedAt + }) + if (record && next[record.paneKey] !== record) { + next[record.paneKey] = record + changed = true + } + } + + return changed ? { sleepingAgentSessionsByPaneKey: next } : s + }) + }, + + clearSleepingAgentSession: (paneKey) => { + set((s) => { + if (!(paneKey in s.sleepingAgentSessionsByPaneKey)) { + return s + } + const next = { ...s.sleepingAgentSessionsByPaneKey } + delete next[paneKey] + return { sleepingAgentSessionsByPaneKey: next } + }) + }, + + clearSleepingAgentSessionsByWorktree: (worktreeId) => { + set((s) => { + let changed = false + const next: Record = {} + for (const [paneKey, record] of Object.entries(s.sleepingAgentSessionsByPaneKey)) { + if (record.worktreeId === worktreeId) { + changed = true + continue + } + next[paneKey] = record + } + return changed ? { sleepingAgentSessionsByPaneKey: next } : s + }) + }, + + pruneSleepingAgentSessions: (validWorktreeIds) => { + set((s) => { + let changed = false + const next: Record = {} + for (const [paneKey, record] of Object.entries(s.sleepingAgentSessionsByPaneKey)) { + if (!validWorktreeIds.has(record.worktreeId)) { + changed = true + continue + } + next[paneKey] = record + } + return changed ? { sleepingAgentSessionsByPaneKey: next } : s + }) + }, + retainAgents: (entries) => { // Why: retained entries are a pure read-overlay — consumers read // retainedAgentsByPaneKey directly each render, so no sort/status epoch diff --git a/src/renderer/src/store/slices/store-cascades.test.ts b/src/renderer/src/store/slices/store-cascades.test.ts index 7186d3b0e..bedafb1be 100644 --- a/src/renderer/src/store/slices/store-cascades.test.ts +++ b/src/renderer/src/store/slices/store-cascades.test.ts @@ -2067,6 +2067,99 @@ describe('shutdownWorktreeTerminals (sleep) — agent status hygiene', () => { expect(s.agentStatusByPaneKey['tab-1:0']).toBeUndefined() }) + it('captures resumable provider session metadata before dropping sleep-time rows', async () => { + const store = createTestStore() + const wt = 'repo1::/path/wt1' + + seedStore(store, { + worktreesByRepo: { + repo1: [makeWorktree({ id: wt, repoId: 'repo1', path: '/path/wt1' })] + }, + tabsByWorktree: { + [wt]: [makeTab({ id: 'tab-1', worktreeId: wt, title: 'Codex' })] + }, + ptyIdsByTabId: { 'tab-1': ['pty-1'] } + }) + + store.getState().setAgentStatus( + 'tab-1:0', + { + state: 'working', + prompt: 'resume this', + agentType: 'codex' + }, + 'Codex', + { updatedAt: 1000, stateStartedAt: 1000 }, + { tabId: 'tab-1', worktreeId: wt }, + { providerSession: { key: 'session_id', id: 'codex-session-1' } } + ) + + await store.getState().shutdownWorktreeTerminals(wt, { keepIdentifiers: true }) + + const state = store.getState() + expect(state.agentStatusByPaneKey['tab-1:0']).toBeUndefined() + expect(state.sleepingAgentSessionsByPaneKey['tab-1:0']).toMatchObject({ + paneKey: 'tab-1:0', + tabId: 'tab-1', + worktreeId: wt, + agent: 'codex', + providerSession: { key: 'session_id', id: 'codex-session-1' }, + prompt: 'resume this', + terminalTitle: 'Codex' + }) + }) + + it('does not preserve provider session metadata when a pane switches agent type', async () => { + const store = createTestStore() + const wt = 'repo1::/path/wt1' + + seedStore(store, { + worktreesByRepo: { + repo1: [makeWorktree({ id: wt, repoId: 'repo1', path: '/path/wt1' })] + }, + tabsByWorktree: { + [wt]: [makeTab({ id: 'tab-1', worktreeId: wt, title: 'Claude' })] + }, + ptyIdsByTabId: { 'tab-1': ['pty-1'] } + }) + + store.getState().setAgentStatus( + 'tab-1:0', + { + state: 'done', + prompt: 'codex prompt', + agentType: 'codex', + interrupted: false + }, + 'Codex', + { updatedAt: 1000, stateStartedAt: 1000 }, + { tabId: 'tab-1', worktreeId: wt }, + { providerSession: { key: 'session_id', id: 'codex-session-1' } } + ) + + store.getState().setAgentStatus( + 'tab-1:0', + { + state: 'working', + prompt: 'claude prompt', + agentType: 'claude' + }, + 'Claude', + { updatedAt: 2000, stateStartedAt: 2000 }, + { tabId: 'tab-1', worktreeId: wt } + ) + + const liveEntry = store.getState().agentStatusByPaneKey['tab-1:0'] + expect(liveEntry?.agentType).toBe('claude') + expect(liveEntry?.providerSession).toBeUndefined() + + await store.getState().shutdownWorktreeTerminals(wt, { keepIdentifiers: true }) + + const state = store.getState() + expect(state.agentStatusByPaneKey['tab-1:0']).toBeUndefined() + expect(state.sleepingAgentSessionsByPaneKey['tab-1:0']).toBeUndefined() + }) + it('drops retainedAgentsByPaneKey entries for the slept worktree', async () => { const store = createTestStore() const wt = 'repo1::/path/wt1' diff --git a/src/renderer/src/store/slices/terminals.ts b/src/renderer/src/store/slices/terminals.ts index b7be29fdf..0dad6302d 100644 --- a/src/renderer/src/store/slices/terminals.ts +++ b/src/renderer/src/store/slices/terminals.ts @@ -1682,6 +1682,12 @@ export const createTerminalSlice: StateCreator } }) + if (keepIdentifiers) { + get().captureSleepingAgentSessionsByWorktree(worktreeId) + } else { + get().clearSleepingAgentSessionsByWorktree(worktreeId) + } + // Why: sleep/remove fold the whole worktree surface. The live PTY bindings // were cleared above and kill is about to run, so live rows are stale; // retained rows are folded too so a grey slept card does not keep a green @@ -2002,6 +2008,11 @@ export const createTerminalSlice: StateCreator .flat() .map((tab) => tab.id) ) + const sleepingAgentSessionsByPaneKey = Object.fromEntries( + Object.entries(session.sleepingAgentSessionsByPaneKey ?? {}).filter(([, record]) => + validWorktreeIds.has(record.worktreeId) + ) + ) const activeWorktreeId = session.activeWorktreeId && validWorktreeIds.has(session.activeWorktreeId) ? session.activeWorktreeId @@ -2169,6 +2180,7 @@ export const createTerminalSlice: StateCreator lastVisitedAtByWorktreeId: session.lastVisitedAtByWorktreeId ?? {}, defaultTerminalTabsAppliedByWorktreeId: session.defaultTerminalTabsAppliedByWorktreeId ?? {}, + sleepingAgentSessionsByPaneKey, pendingReconnectWorktreeIds, pendingReconnectTabByWorktree, pendingReconnectPtyIdByTabId, diff --git a/src/shared/agent-hook-listener.ts b/src/shared/agent-hook-listener.ts index cfc691644..1479e4412 100644 --- a/src/shared/agent-hook-listener.ts +++ b/src/shared/agent-hook-listener.ts @@ -30,6 +30,10 @@ import { join } from 'path' import { parseAgentStatusPayload, type ParsedAgentStatusPayload } from './agent-status-types' import { ORCA_HOOK_PROTOCOL_VERSION } from './agent-hook-types' import { REMOTE_AGENT_HOOK_ENV, type AgentHookSource } from './agent-hook-relay' +import { + extractAgentProviderSession, + type AgentProviderSessionMetadata +} from './agent-session-resume' import { parsePaneKey } from './stable-pane-id' /** Maximum request body size accepted by the listener (1 MB). */ @@ -177,6 +181,8 @@ export type AgentHookEventPayload = { toolAgentId?: string /** Agent/subagent type from the source hook payload, when present. */ toolAgentType?: string + /** Provider-owned conversation/session id needed to resume a sleeping agent. */ + providerSession?: AgentProviderSessionMetadata /** True when this event is a relay cache replay rather than a live hook. */ isReplay?: boolean payload: ParsedAgentStatusPayload @@ -2891,6 +2897,7 @@ export function normalizeHookPayload( // it null; the relay forwards null on the wire and Orca's `ingestRemote` // stamps the real value from `mux` identity on receive. See // docs/design/agent-status-over-ssh.md §5. + const providerSession = extractAgentProviderSession(source, hookPayloadRecord) return payload ? { paneKey, @@ -2914,6 +2921,7 @@ export function normalizeHookPayload( toolUseId: readFirstString(hookPayloadRecord, ['tool_use_id', 'toolUseId']), toolAgentId: readFirstString(hookPayloadRecord, ['agent_id', 'agentId']), toolAgentType: readString(hookPayloadRecord, 'agent_type'), + ...(providerSession ? { providerSession } : {}), payload } : null diff --git a/src/shared/agent-hook-relay.ts b/src/shared/agent-hook-relay.ts index 9c81a3e4b..8a1809648 100644 --- a/src/shared/agent-hook-relay.ts +++ b/src/shared/agent-hook-relay.ts @@ -23,6 +23,7 @@ // default env is `remote`, a location marker ignored by dev-vs-prod checks. import type { ParsedAgentStatusPayload } from './agent-status-types' +import type { AgentProviderSessionMetadata } from './agent-session-resume' // Why: the local hook server knows the discriminator from URL pathname routing // (`/hook/`); the relay equally must tag each forwarded notification @@ -72,6 +73,8 @@ export type AgentHookRelayEnvelope = { toolAgentId?: string /** Claude agent type, used only as a lower-confidence identity fallback. */ toolAgentType?: string + /** Provider-owned conversation/session id needed to resume a sleeping agent. */ + providerSession?: AgentProviderSessionMetadata /** True when the relay is replaying its cache after Orca reconnects. */ isReplay?: boolean /** Forwarded from the agent CLI POST body. The relay default is `remote`, diff --git a/src/shared/agent-session-resume.test.ts b/src/shared/agent-session-resume.test.ts new file mode 100644 index 000000000..18e768093 --- /dev/null +++ b/src/shared/agent-session-resume.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest' +import { + extractAgentProviderSession, + getAgentResumeArgv, + normalizeAgentProviderSession +} from './agent-session-resume' + +describe('agent session resume metadata', () => { + it.each([ + ['claude', { session_id: 'claude-session' }, { key: 'session_id', id: 'claude-session' }], + ['codex', { session_id: 'codex-session' }, { key: 'session_id', id: 'codex-session' }], + ['gemini', { session_id: 'gemini-session' }, { key: 'session_id', id: 'gemini-session' }], + [ + 'antigravity', + { conversationId: 'agy-conversation' }, + { key: 'conversation_id', id: 'agy-conversation' } + ], + ['opencode', { sessionID: 'opencode-session' }, { key: 'session_id', id: 'opencode-session' }], + ['droid', { session_id: 'droid-session' }, { key: 'session_id', id: 'droid-session' }], + ['grok', { sessionId: 'grok-session' }, { key: 'session_id', id: 'grok-session' }] + ] as const)('extracts %s provider session ids', (source, payload, expected) => { + expect(extractAgentProviderSession(source, payload)).toEqual(expected) + }) + + it.each([ + ['claude', { key: 'session_id', id: 's1' }, ['claude', '--resume', 's1']], + ['codex', { key: 'session_id', id: 's1' }, ['codex', 'resume', 's1']], + ['gemini', { key: 'session_id', id: 's1' }, ['gemini', '--resume', 's1']], + ['antigravity', { key: 'conversation_id', id: 's1' }, ['agy', '--conversation', 's1']], + ['opencode', { key: 'session_id', id: 's1' }, ['opencode', '--session', 's1']], + ['droid', { key: 'session_id', id: 's1' }, ['droid', '--resume', 's1']], + ['grok', { key: 'session_id', id: 's1' }, ['grok', '--resume', 's1']] + ] as const)('builds %s resume argv', (agent, providerSession, expected) => { + expect(getAgentResumeArgv(agent, providerSession)).toEqual(expected) + }) + + it('rejects unsupported sources and unsafe ids', () => { + expect(extractAgentProviderSession('pi', { session_id: 'pi-session' })).toBeNull() + expect(normalizeAgentProviderSession({ key: 'session_id', id: 'bad\nid' })).toBeNull() + expect(normalizeAgentProviderSession({ key: 'session_id', id: '--last' })).toBeNull() + expect(extractAgentProviderSession('codex', { session_id: '--last' })).toBeNull() + expect(normalizeAgentProviderSession({ key: 'session_id', id: 'ok' })).toEqual({ + key: 'session_id', + id: 'ok' + }) + }) +}) diff --git a/src/shared/agent-session-resume.ts b/src/shared/agent-session-resume.ts new file mode 100644 index 000000000..ccf174fcd --- /dev/null +++ b/src/shared/agent-session-resume.ts @@ -0,0 +1,151 @@ +import type { AgentHookSource } from './agent-hook-relay' +import type { AgentStatusState } from './agent-status-types' +import type { TuiAgent } from './types' + +export const RESUMABLE_TUI_AGENTS = [ + 'claude', + 'codex', + 'gemini', + 'antigravity', + 'opencode', + 'droid', + 'grok' +] as const satisfies readonly TuiAgent[] + +export type ResumableTuiAgent = (typeof RESUMABLE_TUI_AGENTS)[number] + +export type AgentProviderSessionKey = 'session_id' | 'conversation_id' + +export type AgentProviderSessionMetadata = { + key: AgentProviderSessionKey + id: string +} + +export type SleepingAgentSessionRecord = { + paneKey: string + tabId?: string + worktreeId: string + agent: ResumableTuiAgent + providerSession: AgentProviderSessionMetadata + prompt: string + state: AgentStatusState + capturedAt: number + updatedAt: number + terminalTitle?: string + lastAssistantMessage?: string + connectionId?: string | null +} + +const RESUMABLE_TUI_AGENT_SET: ReadonlySet = new Set(RESUMABLE_TUI_AGENTS) +const PROVIDER_SESSION_ID_MAX_LENGTH = 512 + +export function hasUnsafeProviderSessionIdChars(value: string): boolean { + for (let i = 0; i < value.length; i += 1) { + const code = value.charCodeAt(i) + if (code <= 0x1f || code === 0x7f) { + return true + } + } + return false +} + +function normalizeSessionId(value: unknown): string | null { + if (typeof value !== 'string') { + return null + } + const trimmed = value.trim() + if ( + trimmed.length === 0 || + trimmed.length > PROVIDER_SESSION_ID_MAX_LENGTH || + trimmed.startsWith('-') || + hasUnsafeProviderSessionIdChars(trimmed) + ) { + return null + } + return trimmed +} + +function readSessionId(record: Record, keys: readonly string[]): string | null { + for (const key of keys) { + const normalized = normalizeSessionId(record[key]) + if (normalized) { + return normalized + } + } + return null +} + +export function isResumableTuiAgent(value: unknown): value is ResumableTuiAgent { + return typeof value === 'string' && RESUMABLE_TUI_AGENT_SET.has(value) +} + +export function normalizeAgentProviderSession(raw: unknown): AgentProviderSessionMetadata | null { + if (typeof raw !== 'object' || raw === null) { + return null + } + const record = raw as Record + const key = record.key + if (key !== 'session_id' && key !== 'conversation_id') { + return null + } + const id = normalizeSessionId(record.id) + return id ? { key, id } : null +} + +export function extractAgentProviderSession( + source: AgentHookSource, + payload: Record +): AgentProviderSessionMetadata | null { + switch (source) { + case 'claude': + case 'codex': + case 'gemini': + case 'droid': { + const id = readSessionId(payload, ['session_id']) + return id ? { key: 'session_id', id } : null + } + case 'antigravity': { + const id = readSessionId(payload, ['conversationId']) + return id ? { key: 'conversation_id', id } : null + } + case 'opencode': { + const id = readSessionId(payload, ['sessionID']) + return id ? { key: 'session_id', id } : null + } + case 'grok': { + const id = readSessionId(payload, ['sessionId', 'session_id']) + return id ? { key: 'session_id', id } : null + } + case 'amp': + case 'cursor': + case 'pi': + case 'omp': + case 'command-code': + case 'copilot': + case 'hermes': + return null + } +} + +export function getAgentResumeArgv( + agent: ResumableTuiAgent, + providerSession: AgentProviderSessionMetadata +): string[] | null { + const id = providerSession.id + switch (agent) { + case 'claude': + return providerSession.key === 'session_id' ? ['claude', '--resume', id] : null + case 'codex': + return providerSession.key === 'session_id' ? ['codex', 'resume', id] : null + case 'gemini': + return providerSession.key === 'session_id' ? ['gemini', '--resume', id] : null + case 'antigravity': + return providerSession.key === 'conversation_id' ? ['agy', '--conversation', id] : null + case 'opencode': + return providerSession.key === 'session_id' ? ['opencode', '--session', id] : null + case 'droid': + return providerSession.key === 'session_id' ? ['droid', '--resume', id] : null + case 'grok': + return providerSession.key === 'session_id' ? ['grok', '--resume', id] : null + } +} diff --git a/src/shared/agent-status-types.ts b/src/shared/agent-status-types.ts index ac5ac573f..8f87bd44b 100644 --- a/src/shared/agent-status-types.ts +++ b/src/shared/agent-status-types.ts @@ -5,6 +5,8 @@ // agent misses its own cancellation hook. We still do not infer status from // terminal titles anywhere in the data flow. +import type { AgentProviderSessionMetadata } from './agent-session-resume' + export const AGENT_STATUS_STATES = ['working', 'blocked', 'waiting', 'done'] as const export type AgentStatusState = (typeof AGENT_STATUS_STATES)[number] // Why: agent types are not restricted to a fixed set — new agents appear @@ -113,6 +115,9 @@ export type AgentStatusEntry = { * Why: parent/child agent hierarchy is pane-level state, not worktree * lineage; workers often run in the same worktree as their coordinator. */ orchestration?: AgentStatusOrchestrationContext + /** Provider-owned conversation/session id captured from hook payloads. + * Used only for exact CLI resume; Orca terminal ids are not agent-session ids. */ + providerSession?: AgentProviderSessionMetadata } export type MigrationUnsupportedPtyEntry = { @@ -172,6 +177,7 @@ export type AgentStatusIpcPayload = ParsedAgentStatusPayload & { /** Timestamp (ms) when the current state first appeared for this pane. */ stateStartedAt: number orchestration?: AgentStatusOrchestrationContext + providerSession?: AgentProviderSessionMetadata } /** Maximum character length for the prompt field. Truncated on parse. */ diff --git a/src/shared/tui-agent-startup.test.ts b/src/shared/tui-agent-startup.test.ts index 06bb7ca33..62e6be5db 100644 --- a/src/shared/tui-agent-startup.test.ts +++ b/src/shared/tui-agent-startup.test.ts @@ -1,5 +1,10 @@ import { describe, expect, it } from 'vitest' -import { buildAgentDraftLaunchPlan, buildAgentStartupPlan } from './tui-agent-startup' +import { + buildAgentDraftLaunchPlan, + buildAgentResumeStartupPlan, + buildAgentStartupPlan, + buildShellCommandFromArgv +} from './tui-agent-startup' describe('tui agent startup plans', () => { it('uses POSIX quoting when the target shell is Linux', () => { @@ -24,6 +29,12 @@ describe('tui agent startup plans', () => { expect(plan?.launchCommand).toBe("claude 'fix Bob''s \"quoted\" branch'") }) + it('invokes fully quoted argv commands in PowerShell', () => { + expect(buildShellCommandFromArgv(['codex', 'resume', 's1'], 'powershell')).toBe( + "& 'codex' 'resume' 's1'" + ) + }) + it('uses cmd escaping when requested explicitly', () => { const plan = buildAgentStartupPlan({ agent: 'claude', @@ -113,6 +124,28 @@ describe('tui agent startup plans', () => { expect(plan?.launchCommand).toBe("codex --profile work 'fix it'") }) + it('builds Windows resume plans that PowerShell can invoke', () => { + const plan = buildAgentResumeStartupPlan({ + agent: 'codex', + providerSession: { key: 'session_id', id: 's1' }, + cmdOverrides: {}, + platform: 'win32' + }) + + expect(plan?.launchCommand).toBe("codex 'resume' 's1'") + }) + + it('honors command overrides when building POSIX resume plans', () => { + const plan = buildAgentResumeStartupPlan({ + agent: 'codex', + providerSession: { key: 'session_id', id: 's1' }, + cmdOverrides: { codex: 'codex --profile work' }, + platform: 'linux' + }) + + expect(plan?.launchCommand).toBe("codex --profile work 'resume' 's1'") + }) + it('clears draft environment variables with the target shell syntax', () => { expect( buildAgentDraftLaunchPlan({ diff --git a/src/shared/tui-agent-startup.ts b/src/shared/tui-agent-startup.ts index 56ce9b71b..c0147fe84 100644 --- a/src/shared/tui-agent-startup.ts +++ b/src/shared/tui-agent-startup.ts @@ -1,4 +1,9 @@ import { isShellProcess } from './agent-detection' +import { + getAgentResumeArgv, + type AgentProviderSessionMetadata, + type ResumableTuiAgent +} from './agent-session-resume' import { TUI_AGENT_CONFIG } from './tui-agent-config' import type { TuiAgent } from './types' @@ -13,14 +18,14 @@ export type AgentStartupPlan = { export type AgentStartupShell = 'posix' | 'powershell' | 'cmd' -function resolveStartupShell( +export function resolveStartupShell( platform: NodeJS.Platform, shell?: AgentStartupShell ): AgentStartupShell { return shell ?? (platform === 'win32' ? 'powershell' : 'posix') } -function quoteStartupArg(value: string, shell: AgentStartupShell): string { +export function quoteStartupArg(value: string, shell: AgentStartupShell): string { if (shell === 'powershell') { return `'${value.replace(/'/g, "''")}'` } @@ -30,6 +35,17 @@ function quoteStartupArg(value: string, shell: AgentStartupShell): string { return `'${value.replace(/'/g, `'\\''`)}'` } +export function buildShellCommandFromArgv( + args: readonly string[], + shell: AgentStartupShell +): string { + const command = args.map((arg) => quoteStartupArg(arg, shell)).join(' ') + if (shell === 'powershell' && command) { + return `& ${command}` + } + return command +} + function clearEnvCommand(name: string, shell: AgentStartupShell): string { if (shell === 'powershell') { return `Remove-Item Env:${name} -ErrorAction SilentlyContinue` @@ -135,6 +151,37 @@ export function buildAgentStartupPlan(args: { } } +export function buildAgentResumeStartupPlan(args: { + agent: ResumableTuiAgent + providerSession: AgentProviderSessionMetadata + cmdOverrides: Partial> + platform: NodeJS.Platform + shell?: AgentStartupShell +}): AgentStartupPlan | null { + const argv = getAgentResumeArgv(args.agent, args.providerSession) + if (!argv) { + return null + } + const shell = resolveStartupShell(args.platform, args.shell) + const config = TUI_AGENT_CONFIG[args.agent] + const baseCommand = resolveBaseCommand({ + agent: args.agent, + cmdOverrides: args.cmdOverrides, + shell + }) + const resumeArgs = argv + .slice(1) + .map((arg) => quoteStartupArg(arg, shell)) + .join(' ') + const launchCommand = resumeArgs ? `${baseCommand} ${resumeArgs}` : baseCommand + return { + agent: args.agent, + launchCommand, + expectedProcess: config.expectedProcess, + followupPrompt: null + } +} + export type AgentDraftLaunchPlan = { agent: TuiAgent launchCommand: string diff --git a/src/shared/types.ts b/src/shared/types.ts index 77cec28a8..871a8a490 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -24,6 +24,7 @@ import type { SourceControlAiSettings } from './source-control-ai-types' import type { AgentKind, LaunchSource, RequestKind } from './telemetry-events' +import type { SleepingAgentSessionRecord } from './agent-session-resume' // Re-exported for backward compat with renderer call sites that import // `WorkspaceCreateTelemetrySource` from '../../../shared/types'. @@ -726,6 +727,8 @@ export type WorkspaceSessionState = { * considered. Persisted so closing all tabs and re-opening the workspace * does not recreate the template. */ defaultTerminalTabsAppliedByWorktreeId?: Record + /** Provider-session resume records captured when workspaces sleep. */ + sleepingAgentSessionsByPaneKey?: Record } export type WorkspaceSessionPatch = Partial diff --git a/src/shared/workspace-session-schema.test.ts b/src/shared/workspace-session-schema.test.ts index 7b02044a6..d9609fd0b 100644 --- a/src/shared/workspace-session-schema.test.ts +++ b/src/shared/workspace-session-schema.test.ts @@ -107,6 +107,181 @@ describe('parseWorkspaceSession', () => { } }) + it('preserves valid sleeping agent resume records', () => { + const result = parseWorkspaceSession({ + activeRepoId: null, + activeWorktreeId: null, + activeTabId: null, + tabsByWorktree: {}, + terminalLayoutsByTabId: {}, + sleepingAgentSessionsByPaneKey: { + 'tab1:pane-1': { + paneKey: 'tab1:pane-1', + tabId: 'tab1', + worktreeId: 'wt', + agent: 'codex', + providerSession: { key: 'session_id', id: 'codex-session' }, + prompt: 'continue', + state: 'working', + capturedAt: 10, + updatedAt: 9, + terminalTitle: 'Codex', + lastAssistantMessage: 'done' + } + } + }) + expect(result.ok).toBe(true) + if (result.ok) { + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.agent).toBe('codex') + } + }) + + it('drops malformed sleeping agent resume records without failing the whole session', () => { + const result = parseWorkspaceSession({ + activeRepoId: null, + activeWorktreeId: null, + activeTabId: null, + tabsByWorktree: {}, + terminalLayoutsByTabId: {}, + sleepingAgentSessionsByPaneKey: { + 'tab1:pane-1': { + paneKey: 'tab1:pane-1', + worktreeId: 'wt', + agent: 'pi', + providerSession: { key: 'session_id', id: 'pi-session' }, + prompt: 'continue', + state: 'working', + capturedAt: 10, + updatedAt: 9 + } + } + }) + expect(result.ok).toBe(true) + if (result.ok) { + expect(result.value.sleepingAgentSessionsByPaneKey).toBeUndefined() + } + }) + + it('preserves valid sleeping agent resume records when sibling records are malformed', () => { + const result = parseWorkspaceSession({ + activeRepoId: null, + activeWorktreeId: null, + activeTabId: null, + tabsByWorktree: {}, + terminalLayoutsByTabId: {}, + sleepingAgentSessionsByPaneKey: { + 'tab1:pane-1': { + paneKey: 'tab1:pane-1', + tabId: 'tab1', + worktreeId: 'wt', + agent: 'codex', + providerSession: { key: 'session_id', id: 'codex-session' }, + prompt: 'continue', + state: 'working', + capturedAt: 10, + updatedAt: 9 + }, + 'tab2:pane-1': { + paneKey: 'tab2:pane-1', + worktreeId: 'wt', + agent: 'not-real', + providerSession: { key: 'session_id', id: 'bad-session' }, + prompt: 'ignore me', + state: 'working', + capturedAt: 10, + updatedAt: 9 + } + } + }) + expect(result.ok).toBe(true) + if (result.ok) { + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.agent).toBe('codex') + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab2:pane-1']).toBeUndefined() + } + }) + + it('drops sleeping agent records with unsafe provider session ids without dropping valid siblings', () => { + const result = parseWorkspaceSession({ + activeRepoId: null, + activeWorktreeId: null, + activeTabId: null, + tabsByWorktree: {}, + terminalLayoutsByTabId: {}, + sleepingAgentSessionsByPaneKey: { + 'tab1:pane-1': { + paneKey: 'tab1:pane-1', + tabId: 'tab1', + worktreeId: 'wt', + agent: 'codex', + providerSession: { key: 'session_id', id: 'codex-session' }, + prompt: 'continue', + state: 'working', + capturedAt: 10, + updatedAt: 9 + }, + 'tab2:pane-1': { + paneKey: 'tab2:pane-1', + tabId: 'tab2', + worktreeId: 'wt', + agent: 'codex', + providerSession: { key: 'session_id', id: '--last' }, + prompt: 'ignore me', + state: 'working', + capturedAt: 10, + updatedAt: 9 + } + } + }) + expect(result.ok).toBe(true) + if (result.ok) { + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.providerSession.id).toBe( + 'codex-session' + ) + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab2:pane-1']).toBeUndefined() + } + }) + + it('drops sleeping agent records whose embedded pane key differs from the map key', () => { + const result = parseWorkspaceSession({ + activeRepoId: null, + activeWorktreeId: null, + activeTabId: null, + tabsByWorktree: {}, + terminalLayoutsByTabId: {}, + sleepingAgentSessionsByPaneKey: { + 'tab1:pane-1': { + paneKey: 'tab1:pane-1', + tabId: 'tab1', + worktreeId: 'wt', + agent: 'codex', + providerSession: { key: 'session_id', id: 'codex-session' }, + prompt: 'continue', + state: 'working', + capturedAt: 10, + updatedAt: 9 + }, + 'tab2:pane-1': { + paneKey: 'other-tab:pane-1', + tabId: 'tab2', + worktreeId: 'wt', + agent: 'codex', + providerSession: { key: 'session_id', id: 'mismatched-session' }, + prompt: 'ignore me', + state: 'working', + capturedAt: 10, + updatedAt: 9 + } + } + }) + expect(result.ok).toBe(true) + if (result.ok) { + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.providerSession.id).toBe( + 'codex-session' + ) + expect(result.value.sleepingAgentSessionsByPaneKey?.['tab2:pane-1']).toBeUndefined() + } + }) + it('rejects a session where ptyId is a number (schema drift)', () => { const result = parseWorkspaceSession({ activeRepoId: null, diff --git a/src/shared/workspace-session-schema.ts b/src/shared/workspace-session-schema.ts index c8b1f42c1..a334cee2f 100644 --- a/src/shared/workspace-session-schema.ts +++ b/src/shared/workspace-session-schema.ts @@ -19,6 +19,7 @@ import type { import { isValidTerminalTabId } from './terminal-tab-id' import { isTuiAgent } from './tui-agent-config' import { normalizeBrowserHistoryEntries } from './workspace-session-browser-history' +import { normalizeAgentProviderSession, RESUMABLE_TUI_AGENTS } from './agent-session-resume' // ─── Terminal pane layout (recursive) ─────────────────────────────── @@ -80,6 +81,47 @@ const terminalTabSchema = z.object({ .catch(undefined) }) +// ─── Sleeping agent resume records ───────────────────────────────── + +const agentProviderSessionSchema = z.preprocess( + (raw) => normalizeAgentProviderSession(raw) ?? undefined, + z.object({ + key: z.enum(['session_id', 'conversation_id']), + id: z.string().min(1).max(512) + }) +) + +const sleepingAgentSessionRecordSchema = z.object({ + paneKey: z.string().refine((value) => value.length > 0), + tabId: terminalTabIdSchema.optional(), + worktreeId: z.string().min(1), + agent: z.enum(RESUMABLE_TUI_AGENTS), + providerSession: agentProviderSessionSchema, + prompt: z.string(), + state: z.enum(['working', 'blocked', 'waiting', 'done']), + capturedAt: z.number().finite().positive(), + updatedAt: z.number().finite().positive(), + terminalTitle: z.string().optional(), + lastAssistantMessage: z.string().optional(), + connectionId: z.string().nullable().optional() +}) + +const sleepingAgentSessionsByPaneKeySchema = z.preprocess((raw) => { + if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) { + return undefined + } + + const cleaned: Record> = {} + for (const [paneKey, value] of Object.entries(raw as Record)) { + const parsed = sleepingAgentSessionRecordSchema.safeParse(value) + if (parsed.success && parsed.data.paneKey === paneKey) { + cleaned[paneKey] = parsed.data + } + } + + return Object.keys(cleaned).length > 0 ? cleaned : undefined +}, z.record(z.string(), sleepingAgentSessionRecordSchema).optional()) + // ─── Unified tab model ────────────────────────────────────────────── const tabContentTypeSchema = z.enum(['terminal', 'editor', 'diff', 'conflict-review', 'browser']) @@ -256,7 +298,8 @@ export const workspaceSessionStateSchema: z.ZodType = z.o z.record(z.string(), z.number().finite().nonnegative()) ) .optional(), - defaultTerminalTabsAppliedByWorktreeId: z.record(z.string(), z.literal(true)).optional() + defaultTerminalTabsAppliedByWorktreeId: z.record(z.string(), z.literal(true)).optional(), + sleepingAgentSessionsByPaneKey: sleepingAgentSessionsByPaneKeySchema }) export type ParsedWorkspaceSession =