Repository navigation
fix(automations): bind agent terminal output before publishing, and launch SSH folder workspaces on their own host - #10818
Conversation
Why: launchAgentBackgroundSession created the hidden run tab synchronously and only then awaited the agent spawn, so the store briefly held a tab with ptyId: null. Terminal.tsx re-renders on that write, and for an already-visited worktree the tab can neither cold-park nor defer — a TerminalPane mounts, finds no adopt candidate, and starts a fresh default shell. When the agent PTY finally resolves it is rebound in state but the mounted pane still holds the shell, so the user sees a bare prompt and the agent PTY is orphaned (#2989). Reserve the tab id before the spawn and create the tab already bound to the live PTY, with no await in between.
… collision allWorktrees() reads only worktreesByRepo, so every folder workspace looked absent and its automation died at resolution. getKnownWorktreeById covers both. On a reserved-id collision, re-keying the tab could never work: ORCA_TAB_ID and ORCA_PANE_KEY are already baked into the spawned process, so routing and hook identity would permanently disagree. Retire the launch instead. Co-authored-by: Orca <[email protected]>
…er the 800-line cap Co-authored-by: Orca <[email protected]>
…ng SSH host Review of the bind-before-publish fix surfaced a second defect on the path it newly makes reachable. A folder workspace has no repo row — its synthetic repoId is `folder-workspace:<groupId>` — so `repos.find(...)` returns null and every repo-derived launch input silently degraded to a local default: connectionId null, platform CLIENT_PLATFORM, isRemote false. The automation then spawned on the user's machine with a cwd that only exists on the SSH host. Before this branch that path threw before reaching the spawn, so the bug was latent; making folder-workspace automations work is what exposes it. Host resolution now goes through resolveAgentBackgroundLaunchHost, which falls back to the workspace scope (the same getFolderWorkspaceConnectionId that ordinary terminal creation uses) when there is no repo. Ambiguous scopes — mixed local/remote children — still resolve to a local launch rather than guessing a host. Extracted to its own module rather than inlined: the added branch pushed launch-agent-background-session.ts over the 300-line oxlint cap, and a max-lines disable is forbidden. Tests: an SSH folder workspace must spawn with its connectionId and remote cwd; a local one must stay local. Reverting the fallback fails the first and leaves the rest green.
📝 WalkthroughWalkthroughBackground agent sessions now resolve repository or folder-workspace hosts, reserve tab and pane identities before spawning terminals, and adopt PTYs into inactive tabs after successful creation. Missing worktrees and reserved-tab collisions retire terminals without creating tabs. Launch cleanup tracks nullable adoption state, while terminal retirement accepts tab or worktree ownership. Tests cover local, SSH, remote runtime, legacy transport, startup delivery, folder routing, PTY-less tabs, and failure cleanup. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection.test.ts (1)
6275-6281: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueTrim this rationale block.
Eight lines of narrative here restate the launch-site history; a one or two line "why" pointing at
#2989conveys the same intent. As per coding guidelines, "Comments must be concise, limited to non-obvious information, and preferably one line".Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 4dc571ed-9962-4787-ab9e-e84efa2d61ae
📒 Files selected for processing (9)
src/renderer/src/components/terminal-pane/pty-connection.test.tssrc/renderer/src/lib/adopt-agent-background-session-tab.tssrc/renderer/src/lib/agent-background-session-launch-host.tssrc/renderer/src/lib/agent-background-session-test-state.tssrc/renderer/src/lib/launch-agent-background-session-remote.test.tssrc/renderer/src/lib/launch-agent-background-session.test.tssrc/renderer/src/lib/launch-agent-background-session.tssrc/renderer/src/lib/launch-worktree-background-terminals.tssrc/renderer/src/lib/retire-unowned-background-terminal.ts
| if (isTerminalTabPresent(store, reservedTabId)) { | ||
| store.clearAgentLaunchConfig(args.paneKey) | ||
| args.onRetire() | ||
| await retireProvider({ | ||
| ptyId, | ||
| runtimeTarget: args.runtimeTarget, | ||
| runtimeTerminalHandle: args.runtimeTerminalHandle | ||
| }) | ||
| return null | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Read fresh store state for the collision guard.
args.store is the snapshot captured before the spawn await (useAppStore.getState() at the top of launchAgentBackgroundSession), so isTerminalTabPresent(store, reservedTabId) inspects a stale tabsByWorktree and can miss a tab created during the await — the exact race this guard exists for. retireUnownedTerminal already re-reads state for this reason. Actions (createTab, clearAgentLaunchConfig) are stable and can stay on args.store.
🛡️ Proposed fix
- if (isTerminalTabPresent(store, reservedTabId)) {
+ if (isTerminalTabPresent(useAppStore.getState(), reservedTabId)) {Requires a value import: import { useAppStore } from '@/store' (currently import type).
Read live state before adopting a reserved tab so a collision that lands during the spawn is retired instead of re-keyed. Route persisted folder-workspace dispatch through its ambiguity-aware owner, including SSH auth, remote trust, quiet-shell fallback, and WSL shell selection.
…nk-agent-terminal
…aunch SSH folder workspaces on their own host (stablyai#10818) * fix(terminal): bind the agent PTY before the run tab is ever published Why: launchAgentBackgroundSession created the hidden run tab synchronously and only then awaited the agent spawn, so the store briefly held a tab with ptyId: null. Terminal.tsx re-renders on that write, and for an already-visited worktree the tab can neither cold-park nor defer — a TerminalPane mounts, finds no adopt candidate, and starts a fresh default shell. When the agent PTY finally resolves it is rebound in state but the mounted pane still holds the shell, so the user sees a bare prompt and the agent PTY is orphaned (stablyai#2989). Reserve the tab id before the spawn and create the tab already bound to the live PTY, with no await in between. * fix(terminal): resolve folder workspaces, and fail closed on a tab-id collision allWorktrees() reads only worktreesByRepo, so every folder workspace looked absent and its automation died at resolution. getKnownWorktreeById covers both. On a reserved-id collision, re-keying the tab could never work: ORCA_TAB_ID and ORCA_PANE_KEY are already baked into the spawned process, so routing and hook identity would permanently disagree. Retire the launch instead. Co-authored-by: Orca <[email protected]> * test(automations): split the background-session suite so it stays under the 800-line cap Co-authored-by: Orca <[email protected]> * fix(automations): route folder-workspace agent launches to their owning SSH host Review of the bind-before-publish fix surfaced a second defect on the path it newly makes reachable. A folder workspace has no repo row — its synthetic repoId is `folder-workspace:<groupId>` — so `repos.find(...)` returns null and every repo-derived launch input silently degraded to a local default: connectionId null, platform CLIENT_PLATFORM, isRemote false. The automation then spawned on the user's machine with a cwd that only exists on the SSH host. Before this branch that path threw before reaching the spawn, so the bug was latent; making folder-workspace automations work is what exposes it. Host resolution now goes through resolveAgentBackgroundLaunchHost, which falls back to the workspace scope (the same getFolderWorkspaceConnectionId that ordinary terminal creation uses) when there is no repo. Ambiguous scopes — mixed local/remote children — still resolve to a local launch rather than guessing a host. Extracted to its own module rather than inlined: the added branch pushed launch-agent-background-session.ts over the 300-line oxlint cap, and a max-lines disable is forbidden. Tests: an SSH folder workspace must spawn with its connectionId and remote cwd; a local one must stay local. Reverting the fallback fails the first and leaves the rest green. * fix(automations): close folder dispatch and adoption races Read live state before adopting a reserved tab so a collision that lands during the spawn is retired instead of re-keyed. Route persisted folder-workspace dispatch through its ambiguity-aware owner, including SSH auth, remote trust, quiet-shell fallback, and WSL shell selection. --------- Co-authored-by: Orca <[email protected]>
Closes #2989
Takeover of stale PR #8229. This is a rewrite, not a carry-over.
ELI5
Two bugs could leave you staring at an empty terminal:
What was broken
Automation terminals could render blank because Orca published the tab before the PTY output binding existed. Folder workspaces owned by an SSH connection could also be launched on the local host, where their remote path does not exist.
What changed
folder:automation scopes and launch them on the folder's owning local, SSH, or WSL host.Verification
pnpm typecheckpnpm lintpnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib src/renderer/src/hooks/useAutomationDispatchEvents.test.ts src/renderer/src/components/terminal-pane/pty-connection.test.tsgit diff --checkRegression coverage includes bind-before-publish ordering, eager SSH output, quiet-shell fallback, host validation, ambiguous ownership, local/SSH/WSL folder launch behavior, trust scoping, and concurrent tab collisions.
Folder-workspace scope
This PR supports dispatch for already-persisted modern
folder:automations. It does not add general user-facing folder-automation support: AutomationsPage creation/picker/Run Now/View Run, CLI workspace selection, legacy main-process dispatch, and legacy repo-based precheck cwd remain folder-blind. Those paths should be handled separately.Security and compatibility
The inherited PR was treated as untrusted and audited directly. The implementation fails closed on ownership ambiguity and tab collisions, adds no new network or filesystem reach, keeps SSH connection identity explicit, and covers macOS/Linux/Windows path and shell behavior.
Made with Orca 🐋