Skip to content

fix(automations): bind agent terminal output before publishing, and launch SSH folder workspaces on their own host - #10818

Merged
nwparker merged 6 commits into
mainfrom
nwparker/fix-2989-blank-agent-terminal
Jul 28, 2026
Merged

nwparker merged 6 commits into
mainfrom
nwparker/fix-2989-blank-agent-terminal

Conversation

@nwparker

@nwparker nwparker commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. Orca put the terminal on screen before connecting its output cable. If the agent spoke during that tiny gap, its words were lost. Now Orca connects the cable first, then shows the terminal.
  2. For a folder stored on another computer over SSH, Orca could send the agent to your local computer instead. The folder was not there, so the launch went nowhere useful. Now Orca sends the agent to the computer that actually owns the folder.

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

  • Bind the agent PTY before publishing its run tab.
  • Resolve already-persisted modern folder: automation scopes and launch them on the folder's owning local, SSH, or WSL host.
  • Validate that folder ownership matches the automation run host and fail closed for ambiguous or mismatched ownership.
  • Authenticate and reconnect SSH using the folder owner, and scope host-trust writes to that connection.
  • Preserve shell-output readiness for SSH startup commands while adding a 1.5-second fallback for quiet shells and output that arrived before subscription.
  • Read fresh tab state after PTY spawn so a concurrent tab-ID collision fails closed and retires the unowned PTY.
  • Preserve Windows/WSL quoting and shell selection for local folder launches.

Verification

  • pnpm typecheck
  • pnpm lint
  • pnpm 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.ts
    • 344 files passed
    • 3,591 tests passed, 1 skipped
  • git diff --check

Regression 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 🐋

nwparker and others added 4 commits July 26, 2026 15:23
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]>
…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.
@coderabbitai

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Background 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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 35.71% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning It describes the change, but it misses most required template sections, including Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes. Add the missing template sections: Summary, Screenshots (or 'No visual change'), Testing checklist, AI Review Report, Security Audit, and Notes.
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and matches the two main changes: binding terminal output before publishing and launching SSH folder workspaces on their host.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 value

Trim this rationale block.

Eight lines of narrative here restate the launch-site history; a one or two line "why" pointing at #2989 conveys 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

📥 Commits

Reviewing files that changed from the base of the PR and between 29c40e3 and 233a03f.

📒 Files selected for processing (9)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/lib/adopt-agent-background-session-tab.ts
  • src/renderer/src/lib/agent-background-session-launch-host.ts
  • src/renderer/src/lib/agent-background-session-test-state.ts
  • src/renderer/src/lib/launch-agent-background-session-remote.test.ts
  • src/renderer/src/lib/launch-agent-background-session.test.ts
  • src/renderer/src/lib/launch-agent-background-session.ts
  • src/renderer/src/lib/launch-worktree-background-terminals.ts
  • src/renderer/src/lib/retire-unowned-background-terminal.ts

Comment on lines +112 to +121
if (isTerminalTabPresent(store, reservedTabId)) {
store.clearAgentLaunchConfig(args.paneKey)
args.onRetire()
await retireProvider({
ptyId,
runtimeTarget: args.runtimeTarget,
runtimeTerminalHandle: args.runtimeTerminalHandle
})
return null
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 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).

nwparker added 2 commits July 27, 2026 16:56
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.
@nwparker
nwparker merged commit 1fd0f73 into main Jul 28, 2026
23 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Automations still having blank terminal

1 participant