Repository navigation
fix(agents): resume an agent from the folder it ran in, not the pane's start folder - #33527
Conversation
…s start folder A sleeping/live resume record kept only the provider session id, so after a restart Orca typed `claude --resume <id>` into a shell at the worktree root. An agent started via `cd elsewhere && claude` was restored into the wrong working directory (Claude resolves --resume by id across projects, so it does not fail; it silently runs the conversation in the wrong folder). Record the hook-reported cwd on the provider session (first value per session, since Claude's hook cwd follows its Bash tool's cd), persist it, and prefix the cold-restore / wake / host-owned resume with `cd <cwd> &&` when it differs from the workspace root. Strip it from the strict ensureAgentSession wire so older hosts keep accepting resumes. Fixes #16960
Walkthrough
Priority: Severity of issue fixed: Medium Merge Risk:
|
There was a problem hiding this comment.
Actionable comments posted: 2

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
2d699e39-e68f-429a-b800-323fe9be8665
Files selected for processing: 12-
src/main/runtime/terminal/launch/mobile-terminal-startup.tssrc/main/ssh/orcad-migration-path-fields.tssrc/renderer/src/components/terminal-pane/pty-connection/cold-restore-resume-startup.tssrc/renderer/src/components/terminal-pane/remote-runtime-pty-transport.tssrc/renderer/src/lib/sleeping-agent-session-launch.tssrc/renderer/src/runtime/web-runtime-terminal-create-operation.tssrc/shared/agent-hook-listener-claude-turn-state.test.tssrc/shared/agent-hook-listener/pane-provider-session.tssrc/shared/agent-session-resume.tssrc/shared/tui-agent-resume-startup.tssrc/shared/tui-agent-startup.spec.tssrc/shared/workspace-session-sleeping-agents.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.
-
| const cwd = | ||
| previous?.id === providerSession.id && previous.cwd | ||
| ? previous.cwd | ||
| : readPathFromKeys(hookPayload, ['cwd']) |
There was a problem hiding this comment.
Preserve whitespace in the reported cwd.
readPathFromKeys returns raw.trim(). If the agent starts in a valid POSIX directory such as /repo/pkg , capture records /repo/pkg instead. If both directories exist, resume can silently use the wrong one. normalizeAgentProviderSession also calls this helper at src/shared/agent-session-resume.ts Line 182. Check for an empty path without changing the cwd’s bytes at either step.
| ? buildAiVaultResumeShellCommand({ | ||
| resumeCommand, | ||
| cwd: agentCwd, | ||
| platform: args.platform, | ||
| shell |
There was a problem hiding this comment.
🔎 Supported by static analysis
-
Script executed:
#!/bin/bash ast-grep outline src/shared/ai-vault-resume-command.ts --view expanded rg -n -C8 'Set-Location|buildResumeShellCommandForShell' src/shared
Repository: stablyai/orca
Length of output: 7730
Script executed:
sed -n '1,180p' src/shared/ai-vault-resume-command.ts sed -n '230,300p' src/shared/ai-vault-resume-command.test.ts rg -n -F -- 'function commandSeparator' src/shared
Repository: stablyai/orca
Length of output: 8501
Stop the PowerShell resume command when changing directories fails.
The PowerShell branch joins Set-Location and the resume command with ;. If the recorded folder was deleted, the resume command can run from the launch folder. Guard the resume command with the result of Set-Location.
Suggested fix
-
- if (codexHome) { - segments.push(`$env:CODEX_HOME=${quoteStartupArg(codexHome, shell)}`) - } - segments.push(args.resumeCommand) + const resumeCommand = codexHome + ? `$env:CODEX_HOME=${quoteStartupArg(codexHome, shell)}${separator}${args.resumeCommand}` + : args.resumeCommand + segments.push(cwd ? `if ($?) { ${resumeCommand} }` : resumeCommand)
…re only Claude/Codex cwd Claude's hook cwd follows its Bash tool's cd, so a new session id in the same pane keeps the previous folder unless Claude reports a fresh SessionStart startup. Only Claude and Codex hooks report the agent's own folder (Cursor's cwd is a shell command's), so other agents skip capture. Compare folders with the shared runtime path normalizer, mapping WSL UNC launch paths to their Linux path.
A retained same-agent row outlives exits and restarts, so reusing its folder for any new session id recorded the old folder after Codex /cd or /worktree forks, a relaunch from another folder, or Claude --resume from elsewhere. Keep it only for the same provider session id or a Claude /clear or compact restart; otherwise take the hook's cwd.
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

Major · Compare hook cwd when deciding whether a live recovery record… · agent-session-resume.ts:182
src/shared/agent-session-resume.ts:182
🎯 Functional Correctness | 🟠 Major | ⚡ Quick winCompare hook
cwdwhen deciding whether a live recovery record changed.A later status that adds only
providerSession.cwdis treated as unchanged. The sleeping record keeps its earlier provider metadata, so the newly suppliedcwdis not stored. A later resume then starts atlaunchCwdinstead of the recorded directory. This can persist until a later capture replaces the record.The live checkpoint path is:
- The live recovery record copies the current
providerSession, includingcwd.liveResumeRecordChangereturnsnullwhenrecoveryRecordMatchesis true, so the sleeping record is not replaced.recoveryRecordMatchesrelies onagentProviderSessionsEqual, which compares onlykey,id, andtranscriptPathfor Pi-family agents. It does not comparecwd.Keep the identity check unchanged. Compare
cwdonly when the new record supplies one. A later status withoutcwdmust not clear a stored value.Suggested fix
agentProviderSessionsEqual(existing.agent, existing.providerSession, next.providerSession) && + (next.providerSession.cwd === undefined || + existing.providerSession.cwd === next.providerSession.cwd) && launchConfigsEqual(existing.launchConfig, next.launchConfig)

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
5cff2338-a2aa-4b92-8d10-4ab76728f41e
Files selected for processing: 4-
src/shared/agent-hook-listener-claude-turn-state.test.tssrc/shared/agent-hook-listener/pane-provider-session.tssrc/shared/tui-agent-resume-startup.tssrc/shared/tui-agent-startup.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.
-
| const resumeCommand = buildAgentResumeLaunchCommand(args.agent, baseCommand.command, argv, shell) | ||
| const agentCwd = args.providerSession.cwd | ||
| const launchCommand = | ||
| agentCwd && args.launchCwd && !args.resumeInLaunchCwd && !isSameFolder(agentCwd, args.launchCwd) |
There was a problem hiding this comment.
Avoid changing directories twice for a cwd-changing command override.
If the launch directory is /repo and the configured command is cd pkg && claude, the hook records /repo/pkg. This branch then builds cd /repo/pkg && cd pkg && claude --resume …. The second cd targets /repo/pkg/pkg, so resume fails when that directory does not exist. Account for directory changes in the saved launch command before adding the cwd prefix. (raw.githubusercontent.com)
…uuid ids when following a Claude job - Carry providerSession.cwd into followed sessions so resume keeps the folder (#33527). - Like Claude Code's own reader, drop a continued-in pointer that a later prompt or finished reply overtook, and remember a pending pointer so an unchanged transcript is not re-read. - Require Claude's session-id shape for continuedInSessionId, resumeSessionId and the linkScanPath basename so ids cannot escape the project or jobs directory.
… it was started in (#33659) Claude account-switch restarts now pass the folder the restarted shell opens in (tab start folder, else workspace), so #33527's `cd <start folder> &&` resume prefix applies and a session started in a subfolder resumes there. Codex account restarts benefit the same way. Refs #31545 Supersedes #32033 Co-authored-by: ASH <[email protected]>
… target account in place Build the resume through the shared startup plan and replace the current pane through the runtime PTY controller on the named Claude account. Resolve the current PTY cwd before planning and retain it as the spawned shell folder, allowing the builder to honor providerSession.cwd and recorded effort. adapted from the plan: launchCwd from the recorded session folder, per stablyai#33527
ELI5
If you start an agent yourself from a different folder (
cd pkg && claude), Orca remembered which session it was but not where it was running. After a restart or wake, Orca typedclaude --resume <id>into a shell at the worktree root. Claude finds the session by id anyway, so nothing failed. The conversation just quietly carried on in the wrong folder. Now Orca records the folder the agent ran in and resumes from there.Root cause
AgentProviderSessionMetadata/SleepingAgentSessionRecordstored no cwd (src/shared/agent-session-resume.ts). Every resume builder (buildAgentResumeStartupPlan) emitted a bareclaude --resume <id>, which runs in whatever folder the pane's shell starts in. After a cold restore that is the pane's spawn folder (the worktree root). Orca's shell integration emits no OSC 7, and incd X && claudethecdnever reaches a prompt anyway.CLAUDE_CONFIG_DIRand a fake Anthropic endpoint):cwd.cwdfollows the Bash tool'scd: PostToolUse/Stop reported.../proj/subafter acd subin a session started in.../proj.claude -p --resume <id>launched from an unrelated folder resumes the session (SessionStartsource: resume,cwd= the unrelated folder) and keeps appending to the original project transcript. So the failure is silent, as [Bug]: Resume record binds the session to the pane's worktree, not the agent's cwd — the session is relaunched in the wrong directory (Claude and Codex) #16960 describes.What changed
Capture:
readPaneProviderSessionattaches the agent's starting folder. This is done only for Claude and Codex, whose hookcwdis the session's own folder:turn_context.cwd(codex-rs/core/src/hook_runtime.rs:159,434).cwdis only sent onbeforeShellExecutionand names the shell command's folder, so Cursor is skipped. Other agents are skipped until verified.cwdfollows its Bash tool'scd. So the recorded folder is reused only for the same provider session id, or for a ClaudeSessionStartwithsource: clearorcompact(same process). Any other new session id takes the hook'scwd: Codex/cdor/worktreeforks, a relaunch from another folder, andclaude --resumefrom elsewhere./clearresets Claude to its launch folder,SessionStart source: clearreportsprojafter the Bash tool had moved toproj/sub. In Orca, the record after/clearkeepsrepo/pkg.Carry/persist:
normalizeAgentProviderSession, the workspace-session zod schema, and the orcad export path-rewrite list now keepcwd.Resume:
buildAgentResumeStartupPlantakeslaunchCwd(the folder the resume shell starts in). When the recordedcwddiffers from it (compared with the sharednormalizeRuntimePathForComparison, mapping WSL UNC launch paths to their Linux path), the command is prefixed withcd <cwd> &&. This reusesbuildAiVaultResumeShellCommand, so posix, cmd and PowerShell are all quoted correctly. Callers that pass it:resolveMobileSessionResumeCommand, which also covers server-side restored panes and mobile)Agents started at the workspace root produce exactly the same command as before.
Mixed versions: the strict
terminal.ensureAgentSessionProviderSessionschema on older hosts would reject an unknown key. The two client send sites therefore stripcwd(agentProviderSessionForEnsure). Older desktops and relays already drop the field throughnormalizeAgentProviderSession.How verified
Real Orca dev build, real Claude Code 2.1.310, isolated
HOME/CLAUDE_CONFIG_DIR/userData, a fake local Anthropic server (no model spend), and a real restart (quit Orca, kill its terminal daemon, relaunch):cd pkg && claudein the repo-root pane, the cold restore ranclaude --resume <id>with process cwd/private/tmp/.../repo. Claude's header shows the repo root.cd submid-session:cwd: .../repo/pkg; the transcript shows Claude moved topkg/sub.claude --resume <id>process cwd was.../repo/pkg, and the header showsrepo/pkg.cd: ...: No such file or directoryand stays at a shell instead of resuming in the wrong folder.cdprefix.pnpm tc, targeted vitest suites (shared, terminal-pane, lib, runtime, ssh, rpc-contract), andcheck:code-quality:changedall pass. Two regression tests were added: hook cwd stickiness, and the resumecdprefix.Before (main):

After (this branch):

Risk / trade-offs
cderror) instead of silently resuming in the workspace root. This is the outcome [Bug]: Resume record binds the session to the pane's worktree, not the agent's cwd — the session is relaunched in the wrong directory (Claude and Codex) #16960 argues for. On PowerShell,Set-Locationfailing does not stop the next statement, so the resume falls back to the old wrong-folder behaviour there.-ErrorAction Stopwas not added because it changes the shared AI vault resume line and I could not verify it on a Windows shell here.cd'd, the first hook in the new Orca run sets the folder. That is a subfolder of the original, and Claude still resolves the id.ensureAgentSessiondo not carrycwdyet. They behave as on main until a capability-gated field is added. A server's own pane restores do use it.claude --resumewith nocdprefix.Fixes #16960
Refs #15485 (part 1, resume in a different cwd. Part 2, spinning subagent rows on relay, is separate.)