Skip to content

fix: connect mobile emulator in folder workspaces - #14009

Merged
Jinwoo-H merged 2 commits into
mainfrom
bug-mobile-emulator-pane-never-connects-in-a-fol
Aug 12, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
bug-mobile-emulator-pane-never-connects-in-a-fol

Conversation

@Jinwoo-H

Copy link
Copy Markdown
Contributor

Closes #13831

Problem and invariant

Successful orca emulator attach scoped to folder:<id> must register under that exact workspace identity and publish renderer auto-attach (plus optional focus) notifications for it.

On latest main (346e59c879adfa7c55fd22485a221a42116de1d2), the CLI discarded the folder identity and scanned for a Git worktree. Direct runtime calls then sent folder selectors through the worktree-only resolver, failing with selector_not_found before the emulator helper could register.

The authoritative boundary is the main-process runtime's workspace resolver, with the terminal-exported workspace identity as the CLI authority before cwd scanning.

Fix

  • Prefer the exact terminal-exported worktree identity, then a canonical folder workspace identity, before cwd-based discovery. Remote clients still omit local scope.
  • Resolve folder selectors through the folder-workspace store and register the canonical folder:<id> key.
  • Allow cleanup-only commands to address a deleted folder's canonical key while normal attach still requires the folder to exist.
  • Revalidate the workspace after slow emulator startup so a deleted folder cannot receive a late registration.
  • Coordinate in-flight starts and cleanup through backend leases, preserving Android cold boot and preventing one workspace from stopping a device another workspace is attaching to or already shares.

This keeps truth at the existing runtime/session owners rather than adding renderer fallback logic or broadening the RPC wire contract.

Deterministic evidence

Byte-identical decisive oracle:

pnpm exec vitest run --config config/vitest.config.ts \
  src/main/runtime/orca-runtime-emulator-folder-workspace.test.ts \
  src/cli/handlers/emulator.test.ts \
  --reporter=verbose
  • Latest main 346e59c879ad: red, 7 failed / 7 passed. Folder attach and cleanup threw selector_not_found; CLI attach made an extra worktree.list call and lost terminal scope.
  • Candidate 26b2ca5fb85e: green, 14/14.
  • Candidate with only the folder resolver and CLI environment lookup disabled: red, 5 failed / 9 passed with the same attach-routing and CLI-scope failures.

Expanded lifecycle oracle: 44/44 green across folder routing, CLI scoping, shared-device ownership, Android cold boot, failed-attach cleanup, and pane-close/in-flight-attach races.

Additional validation:

  • pnpm typecheck
  • full pnpm lint (including type-aware checks and max-lines ratchet)
  • git diff --check
  • three fresh architecture/adversarial, resource/security/platform, and readiness review lanes: clean

Live proof

An isolated macOS Orca profile opened the real Mobile Emulator pane for a plain folder workspace and connected Pixel_9_Pro_API_36. The folder-scoped runtime command returned sdk_gphone64_arm64, and the pane showed the canonical “Plain folder” workspace as Connected with the live Android screen.

folder-emulator-android-connected.png

Scope and remaining topology gaps

No RPC schema/opcode, persisted mobile state, Git/provider, or UI-style contract changed. This was not freshly exercised on physical Windows/WSL, direct SSH/paired runtimes, mixed client/server versions, or a live iOS simulator. Those paths do not own the reproduced selector failure; the live proof is macOS host + Android emulator.

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 368271a3-f06c-4cdd-a0b0-a4937afe924e

📥 Commits

Reviewing files that changed from the base of the PR and between 26b2ca5 and f0c3aa5.

📒 Files selected for processing (3)
  • src/cli/handlers/emulator.test.ts
  • src/main/runtime/orca-runtime-emulator-folder-workspace.test.ts
  • src/main/runtime/orca-runtime-emulator.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/cli/handlers/emulator.test.ts
  • src/main/runtime/orca-runtime-emulator.ts

📝 Walkthrough

Walkthrough

The change adds environment-based emulator workspace selection and separate workspace and cleanup resolution. It introduces lease-based helper acquisition with coordinated concurrent cleanup and session retention. EmulatorBridge uses centralized device inventory and lease-aware lifecycle operations. Runtime emulator commands validate workspace identity, release leases across attachment outcomes, and support folder-workspace cleanup. Tests cover routing, concurrent claims, cleanup, failed attachment, and folder-workspace behavior.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address issue #13831 by aligning folder workspace scope across CLI, runtime, and emulator pane attachment and cleanup.
Out of Scope Changes check ✅ Passed The changes remain focused on folder workspace emulator routing, lifecycle cleanup, concurrency, and related test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title clearly and concisely describes the primary change: enabling mobile emulator attachment in folder workspaces.
Description check ✅ Passed The description clearly explains the problem, fix, validation, linked issue, and live proof, although it does not follow every template heading.
✨ 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: 2

🧹 Nitpick comments (4)
src/main/runtime/orca-runtime.ts (1)

28380-28409: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Extract the shared selector-normalization logic to avoid a third divergent copy.

resolveFolderWorkspaceSelector (line 28381-28382) and resolveEmulatorCleanupWorkspaceId (line 28403-28404) both re-implement the same selector.startsWith('id:') ? selector.slice(3) : selector followed by parseWorkspaceKey(...). The same pattern already exists unchanged in resolveTerminalWorkspaceLaunchScope. resolveEmulatorCleanupWorkspaceId cannot simply call resolveFolderWorkspaceSelector, because it must skip the store lookup and the selector_not_found throw so cleanup works for a folder workspace that no longer exists. Extract only the normalization + parse step into a small private helper (for example parseFolderWorkspaceSelectorId(selector): string | null), and build both resolveFolderWorkspaceSelector (add the store lookup and throw) and resolveEmulatorCleanupWorkspaceId (skip the lookup) on top of it. This keeps the "id: stripping + parseWorkspaceKey" rule in one place so a future change to selector normalization cannot silently diverge between the workspace-resolution and cleanup-resolution paths.

♻️ Proposed refactor
+  private parseFolderWorkspaceSelectorId(selector: string): string | null {
+    const workspaceSelector = selector.startsWith('id:') ? selector.slice(3) : selector
+    const parsed = parseWorkspaceKey(workspaceSelector)
+    return parsed?.type === 'folder' ? parsed.folderWorkspaceId : null
+  }
+
   private resolveFolderWorkspaceSelector(selector: string): FolderWorkspace | null {
-    const workspaceSelector = selector.startsWith('id:') ? selector.slice(3) : selector
-    const parsed = parseWorkspaceKey(workspaceSelector)
-    if (parsed?.type !== 'folder') {
+    const folderWorkspaceId = this.parseFolderWorkspaceSelectorId(selector)
+    if (folderWorkspaceId === null) {
       return null
     }
     const workspace = this.store
       ?.getFolderWorkspaces?.()
-      .find((entry) => entry.id === parsed.folderWorkspaceId)
+      .find((entry) => entry.id === folderWorkspaceId)
     if (!workspace) {
       throw new Error('selector_not_found')
     }
     return workspace
   }

   private async resolveEmulatorCleanupWorkspaceId(selector: string): Promise<string> {
-    const workspaceSelector = selector.startsWith('id:') ? selector.slice(3) : selector
-    const parsed = parseWorkspaceKey(workspaceSelector)
-    return parsed?.type === 'folder'
-      ? folderWorkspaceKey(parsed.folderWorkspaceId)
+    const folderWorkspaceId = this.parseFolderWorkspaceSelectorId(selector)
+    return folderWorkspaceId !== null
+      ? folderWorkspaceKey(folderWorkspaceId)
       : this.resolveEmulatorWorkspaceId(selector)
   }
src/cli/selectors.ts (1)

240-247: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Normalize ORCA_WORKTREE_ID the same way as ORCA_WORKSPACE_ID.

Line 241 tests the trimmed value but line 242 returns the raw value. A value with surrounding whitespace is forwarded to the runtime selector unchanged. ORCA_WORKSPACE_ID is trimmed on line 244. Use one normalization rule for both variables.

♻️ Proposed change
-  const terminalWorktreeId = process.env.ORCA_WORKTREE_ID
-  if (terminalWorktreeId?.trim()) {
-    return terminalWorktreeId
-  }
+  const terminalWorktreeId = process.env.ORCA_WORKTREE_ID?.trim()
+  if (terminalWorktreeId) {
+    return terminalWorktreeId
+  }

Note: src/cli/handlers/emulator.test.ts line 114 asserts the untrimmed value, so update that expectation together with this change.

src/main/emulator/emulator-bridge.test.ts (1)

460-461: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Extract the repeated host resolver fixture.

The same two resolver mocks repeat in ten host fixtures (lines 460-461, 494-495, 529-530, 560-561, 595-596, 627-628, 649-650, 674-675, 699-700, 742-743). A small factory keeps future host-contract changes to one edit site.

♻️ Example factory
function workspaceResolvers(worktreeId = 'wt-1') {
  return {
    resolveEmulatorWorkspaceId: vi.fn(async () => worktreeId),
    resolveEmulatorCleanupWorkspaceId: vi.fn(async () => worktreeId)
  }
}
src/main/runtime/orca-runtime-emulator-folder-workspace.test.ts (1)

38-46: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider a narrower cast than as never for the stubs.

as never disables all structural checking. This PR splits the host contract into resolveEmulatorWorkspaceId and resolveEmulatorCleanupWorkspaceId. A later rename of a bridge method such as acquireHelperForDevice will not fail typechecking in this file. A Partial<EmulatorBridge> as EmulatorBridge style cast keeps property-name checking on the members the test does define.

Also applies to: 58-59


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: c107fbd6-fa1d-4382-ae09-39a03b66e2ca

📥 Commits

Reviewing files that changed from the base of the PR and between 346e59c and 26b2ca5.

📒 Files selected for processing (11)
  • src/cli/handlers/emulator.test.ts
  • src/cli/selectors.ts
  • src/main/emulator/emulator-bridge.test.ts
  • src/main/emulator/emulator-bridge.ts
  • src/main/emulator/emulator-device-inventory.ts
  • src/main/emulator/emulator-session-registry.ts
  • src/main/emulator/emulator-start-lease-registry.test.ts
  • src/main/emulator/emulator-start-lease-registry.ts
  • src/main/runtime/orca-runtime-emulator-folder-workspace.test.ts
  • src/main/runtime/orca-runtime-emulator.ts
  • src/main/runtime/orca-runtime.ts

Comment thread src/cli/handlers/emulator.test.ts Resolved
@Jinwoo-H

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 12, 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.

@Jinwoo-H
Jinwoo-H merged commit a868b09 into main Aug 12, 2026
50 checks passed
@Jinwoo-H
Jinwoo-H deleted the bug-mobile-emulator-pane-never-connects-in-a-fol branch August 12, 2026 20:02
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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]: Mobile Emulator pane never connects in a folder workspace — raw selector_not_found; CLI attach/list/pane disagree on scope

1 participant