Skip to content

fix(runtime): preserve terminals during headless desktop activation - #8480

Closed
bbingz wants to merge 6 commits into
stablyai:mainfrom
bbingz:fix/headless-serve-gui-activation
Closed

bbingz wants to merge 6 commits into
stablyai:mainfrom
bbingz:fix/headless-serve-gui-activation

Conversation

@bbingz

@bbingz bbingz commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fixes [Bug]: headless serve hijacks GUI relaunch; forced reopen interrupts and duplicates live agents #8457.
  • Let a headless orca serve process remain the single Electron owner and promote that same process to a desktop window when Orca is opened normally.
  • Preserve the existing runtime, detached daemon, and live local/SSH terminal bindings during promotion instead of starting a competing GUI owner that interrupts agents.
  • Make orca open request desktop activation even when the local runtime is already reachable, while keeping remote-paired opens from launching a local desktop.
  • Report explicit desktop activation states (available, openable, initializing, blocked) so the CLI can wait or fail safely instead of recommending open -n.
  • Preserve a true Cmd+Q quit after a headless owner has been promoted.

Screenshots

No visual change.

Testing

Final pushed HEAD: 6e3d103c73a5760b7dd885aff91b56c581199a79

  • pnpm lint
  • pnpm typecheck
  • pnpm test
  • pnpm build
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

Focused and lifecycle coverage:

  • pnpm exec vitest run --config config/vitest.config.ts src/cli/runtime-client.test.ts src/cli/runtime/websocket-transport.test.ts src/main/runtime/orca-runtime.test.ts src/main/startup/serve-desktop-activation-wiring.test.ts src/main/startup/serve-desktop-activation.test.ts src/main/startup/single-instance-lock.test.ts src/main/startup/window-all-closed-quit-policy.test.ts — 7 files, 719 tests passed.
  • ORCA_E2E_FORWARD_APP_LOGS=1 pnpm exec playwright test tests/e2e/headless-serve-desktop-activation.spec.ts --config tests/playwright.config.ts --project electron-headless --workers=1 — passed in 1.0m. The test uses one isolated profile and proves: desktop quit -> detached daemon and PTY survive -> headless serve adopts the original PTY -> a normal second launch promotes the same serve owner -> runtime and daemon inventories both remain exactly one original PTY -> the restored terminal still accepts commands.
  • pnpm run build:desktop — passed, including typecheck, relay, CLI, Electron, and web builds.
  • pnpm run build:native — both macOS architecture builds completed, then the existing packaging script failed because lipo could not find its expected architecture output paths. No native files are changed by this PR.
  • A full pnpm test attempt under local Node 26 entered unrelated renderer tests and hit the known missing --localstorage-file environment failure; the repository declares Node 24. The affected feature suites above pass directly, and CI remains authoritative for the supported environment.

AI Review Report

Reviewed the final diff against current main and checked the failure modes behind the incident:

  • single-instance ownership and concurrent second-instance / activate requests;
  • provider and agent-hook readiness before headless PTY registration;
  • atomic promotion without replacing the runtime, daemon, PTY, or terminal graph;
  • renderer hydration of windowless local and SSH terminal bindings;
  • CLI behavior for local versus remote-paired runtimes and explicit blocked states;
  • true quit behavior after promotion;
  • isolated E2E cleanup and duplicate/orphan-session detection.

The review caught a stale startup call after merging current main; a source-wiring regression test was added first, then the duplicate hook-server startup call was removed. It also expanded the original headless-only E2E into the exact desktop-quit -> headless-adopt -> GUI-promote lifecycle and asserts that both runtime and daemon inventories contain exactly the original PTY.

CodeRabbit's current findings were adjudicated against the final source. The valid items were fixed: the dropped startup promise is explicitly voided, wiring-test anchor failures can no longer degrade into false passes, and the activating process no longer owns unread stdout/stderr pipes. A subsequent E2E precondition failure exposed another test false positive: the old PTY probe could accept echoed input before the shell executed it. The lifecycle now waits for a split-marker command round trip whose complete marker only exists in executed output; the final E2E passed with that gate.

Cross-platform review explicitly covered macOS, Linux, and Windows. The Electron single-instance API remains platform-neutral; no shortcut labels or hard-coded separators were added; local executable launch continues through Electron/Node process APIs; SSH activation remains remote-scoped; and platform-dependent quit/window behavior stays behind the existing Electron lifecycle checks.

Security Audit

  • No dependency, secret, auth-token, or permission-model changes.
  • The new runtime field is a closed status enum and does not expose process credentials or additional IPC authority.
  • Desktop activation reuses the authenticated local runtime and Electron single-instance channel; it does not add a shell command or interpolate user input into a command line.
  • Existing path/provider validation remains in force for local, WSL, and SSH terminal restoration.
  • Headless promotion waits for the authoritative daemon provider and explicitly blocks fallback-provider promotion, avoiding a split-brain path that could spawn or replace sessions.

No security follow-up identified.

Notes

@bbingz

bbingz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

CI verification for the exact upstream PR commit a1f8a8a against base a5faf19:

The executable workflows ran in temporary fork PR bbingz#2 because untrusted-fork workflows on the upstream PR require maintainer approval. The fork-only track-community-pr job lacks the upstream project token and is not a code gate; the same upstream tracking job passed here: https://github.com/stablyai/orca/actions/runs/29225117929/job/86737537620.

@bbingz
bbingz force-pushed the fix/headless-serve-gui-activation branch from a1f8a8a to 0eabed9 Compare July 13, 2026 05:15
@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a desktop-window status protocol and activation gate for headless serve runtimes. Electron startup synchronizes a headless sentinel, manages activation readiness, and preserves single-instance and quit behavior. Runtime promotion persists eligible PTY bindings, retains SSH session identities, and enables terminal side effects after desktop availability. CLI status and open commands expose and use desktop status. Unit, wiring, reliability configuration, and Electron end-to-end tests cover the lifecycle.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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 accurately describes the main change: preserving terminals during headless-to-desktop activation.
Description check ✅ Passed The description follows the required template and includes complete Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes sections.

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 (2)
src/main/index.ts (1)

2070-2072: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Void the dropped startup-services promise for consistency.

The rest of this function explicitly voids unawaited promises (void firstWindowStartupServicesReady.then(...)), but the top-level call here doesn't. Likely harmless today (the underlying promise is designed to resolve, not reject) but inconsistent with the file's own pattern and could trip a no-floating-promises-style lint rule if enforced.

♻️ Proposed fix
-  startTerminalRuntimeStartupServices()
+  void startTerminalRuntimeStartupServices()
src/main/startup/serve-desktop-activation-wiring.test.ts (1)

15-32: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Assert anchor indices are found before using them as search baselines.

appReadyIndex/serveIndex are used as fromIndex for later indexOf calls but are never asserted to be >= 0 themselves. Since indexOf treats a negative fromIndex as 0, and toBeGreaterThan(-1) is true for any found index, a broken anchor (e.g., if (serveOptions) { reformatted) silently degrades these checks to no-ops instead of failing the test — defeating the purpose of the ordering guard on this critical activation wiring.

♻️ Proposed fix
     const serveIndex = source.indexOf('if (serveOptions) {', appReadyIndex)
     const ptyReadyIndex = source.indexOf('await localPtyStartupReady', serveIndex)
     const headlessRegistrationIndex = source.indexOf('registerHeadlessPtyRuntime(', serveIndex)

     expect(startupIndex).toBeGreaterThanOrEqual(0)
+    expect(serveIndex).toBeGreaterThanOrEqual(0)
     expect(startupIndex).toBeLessThan(serveIndex)

Apply the analogous expect(serveIndex).toBeGreaterThanOrEqual(0) guard before the sentinelIndex/rpcIndex/settleIndex comparisons in the third test as well.

Separately, the exact-whitespace literal '\n startTerminalRuntimeStartupServices()\n' (test 2) is brittle to harmless reformatting (e.g., prefixing the call with void) — consider matching on the bare call ('startTerminalRuntimeStartupServices()') without the surrounding indentation/newlines if precise ordering is already covered by the serveIndex comparison.

Also applies to: 34-47


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 17a8d217-0d6c-463d-8e92-1f67f7ed0b17

📥 Commits

Reviewing files that changed from the base of the PR and between 81ed573 and a1f8a8a.

📒 Files selected for processing (22)
  • config/reliability-gates.jsonc
  • docs/superpowers/plans/2026-07-13-headless-serve-gui-activation.md
  • docs/superpowers/specs/2026-07-13-headless-serve-gui-activation-design.md
  • src/cli/format.ts
  • src/cli/runtime-client.test.ts
  • src/cli/runtime/client.ts
  • src/cli/runtime/status.ts
  • src/cli/runtime/websocket-transport.test.ts
  • src/main/index.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/ssh/ssh-remote-cli-format.ts
  • src/main/ssh/ssh-remote-orca-cli.ts
  • src/main/startup/serve-desktop-activation-wiring.test.ts
  • src/main/startup/serve-desktop-activation.test.ts
  • src/main/startup/serve-desktop-activation.ts
  • src/main/startup/single-instance-lock.test.ts
  • src/main/startup/single-instance-lock.ts
  • src/main/startup/window-all-closed-quit-policy.test.ts
  • src/main/startup/window-all-closed-quit-policy.ts
  • src/shared/runtime-types.ts
  • tests/e2e/headless-serve-desktop-activation.spec.ts

@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.

♻️ Duplicate comments (1)
tests/e2e/headless-serve-desktop-activation.spec.ts (1)

145-148: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Drain spawned process pipes to prevent potential test hang.

stdio: 'pipe' without consuming stdout/stderr can block the activating Electron process if startup output fills the OS pipe buffer (~64 KB on Linux), causing waitForProcessExit to time out. This was previously flagged and remains unaddressed.

🔧 Proposed fix
 activatingProcess = spawn(electronPath, getOrcaElectronLaunchArgs(mainPath, false), {
   env,
-  stdio: 'pipe'
+  stdio: 'pipe'
 })
+activatingProcess.stdout?.resume()
+activatingProcess.stderr?.resume()

Alternatively, use stdio: 'ignore' if the output is not needed:

 activatingProcess = spawn(electronPath, getOrcaElectronLaunchArgs(mainPath, false), {
   env,
-  stdio: 'pipe'
+  stdio: 'ignore'
 })

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bddf8717-a6af-43c2-a735-1bdb6bfc0cd2

📥 Commits

Reviewing files that changed from the base of the PR and between a1f8a8a and 0eabed9.

📒 Files selected for processing (22)
  • config/reliability-gates.jsonc
  • docs/superpowers/plans/2026-07-13-headless-serve-gui-activation.md
  • docs/superpowers/specs/2026-07-13-headless-serve-gui-activation-design.md
  • src/cli/format.ts
  • src/cli/runtime-client.test.ts
  • src/cli/runtime/client.ts
  • src/cli/runtime/status.ts
  • src/cli/runtime/websocket-transport.test.ts
  • src/main/index.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/ssh/ssh-remote-cli-format.ts
  • src/main/ssh/ssh-remote-orca-cli.ts
  • src/main/startup/serve-desktop-activation-wiring.test.ts
  • src/main/startup/serve-desktop-activation.test.ts
  • src/main/startup/serve-desktop-activation.ts
  • src/main/startup/single-instance-lock.test.ts
  • src/main/startup/single-instance-lock.ts
  • src/main/startup/window-all-closed-quit-policy.test.ts
  • src/main/startup/window-all-closed-quit-policy.ts
  • src/shared/runtime-types.ts
  • tests/e2e/headless-serve-desktop-activation.spec.ts
🚧 Files skipped from review as they are similar to previous changes (18)
  • src/main/startup/window-all-closed-quit-policy.ts
  • src/cli/format.ts
  • src/main/ssh/ssh-remote-orca-cli.ts
  • src/main/startup/window-all-closed-quit-policy.test.ts
  • src/main/startup/single-instance-lock.ts
  • config/reliability-gates.jsonc
  • src/main/startup/serve-desktop-activation.test.ts
  • src/main/startup/serve-desktop-activation.ts
  • src/main/startup/single-instance-lock.test.ts
  • src/main/startup/serve-desktop-activation-wiring.test.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/cli/runtime/client.ts
  • src/cli/runtime-client.test.ts
  • src/cli/runtime/status.ts
  • src/main/ssh/ssh-remote-cli-format.ts
  • src/cli/runtime/websocket-transport.test.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/index.ts

@bbingz

bbingz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Superseding the earlier CI note after rebasing the PR onto the then-current main.

Exact tested pair:

Executable code gates:

The workflows ran in temporary fork PR bbingz#3 because upstream untrusted-fork workflows require maintainer approval. The fork-only track-community-pr job is not a code gate and lacks the upstream project token.

After these gates completed, main advanced to 5856a9f via unrelated #8483 (three agent-hook files only). A non-mutating git merge-tree check confirms the current main and this PR head still merge without conflicts.

@bbingz

bbingz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review\n\nPlease review the current head, including the desktop-quit → headless-adopt → GUI-promote E2E hardening and the fixes for the prior process-pipe and wiring-anchor findings.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@bbingz

bbingz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

Exact-head CI is green for current head 6e3d103c73a5760b7dd885aff91b56c581199a79 against base 0b65d725c98d6552f08fb512f4026b350b7fbf0b:

The first Windows attempt timed out only in the unrelated build-windows-cli-launcher test's 5-second test budget; the unchanged-head rerun passed that test and all subsequent build, daemon, workspace-close, and Windows E2E steps. The fork-only track-community-pr metadata job cannot map this temporary validation PR to an upstream PR and is not a code/product gate.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

@bbingz Thanks for the detailed CI status update. I'll review the current head.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@bbingz

bbingz commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmethystLiang
AmethystLiang requested a review from Jinwoo-H July 13, 2026 16:48
@bbingz
bbingz force-pushed the fix/headless-serve-gui-activation branch from 6e3d103 to f243745 Compare July 14, 2026 00:19
@bbingz
bbingz force-pushed the fix/headless-serve-gui-activation branch from f243745 to 18eaace Compare July 14, 2026 00:38
# Conflicts:
#	config/reliability-gates.jsonc
#	src/cli/runtime-client.test.ts
#	src/cli/runtime/client.ts
#	src/cli/runtime/status.ts
#	src/main/index.ts
#	src/main/runtime/orca-runtime.ts
#	src/main/startup/serve-desktop-activation-wiring.test.ts
#	tests/e2e/headless-serve-desktop-activation.spec.ts
@Jinwoo-H

Copy link
Copy Markdown
Contributor

Thanks for the detailed report and implementation. The core change from this PR was incorporated and hardened through #8646 (1d2aaf1b), with the original authorship preserved. After current main was merged back into this branch, the PR base and head resolved to identical file trees, and GitHub now reports zero changed files. I independently validated the desktop → quit → headless serve adoption → desktop activation lifecycle on current main; the original daemon, runtime, and PTY identities remained continuous without duplicate sessions. Closing this PR as superseded by #8646.

@Jinwoo-H Jinwoo-H closed this Jul 15, 2026
@AmethystLiang AmethystLiang added the orca-remote-server Remote Orca Server / orca serve / paired remote runtime label Jul 22, 2026
@bbingz
bbingz deleted the fix/headless-serve-gui-activation branch August 1, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

orca-remote-server Remote Orca Server / orca serve / paired remote runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: headless serve hijacks GUI relaunch; forced reopen interrupts and duplicates live agents

3 participants