Skip to content

fix(worktree): prefer focused host when workspace identity is multi-host - #10607

Open
innocarpe wants to merge 2 commits into
stablyai:mainfrom
innocarpe:fix/workspace-identity-ambiguous-route
Open

innocarpe wants to merge 2 commits into
stablyai:mainfrom
innocarpe:fix/workspace-identity-ambiguous-route

Conversation

@innocarpe

@innocarpe innocarpe commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Multi-host projections of the same worktree/repo no longer hard-fail as ambiguous when the focused host uniquely selects one owner.
  • Still fail closed when focus does not discriminate.
  • Unblocks new terminal/agent in projects that appear on local + remote catalogs.

Fixes

Closes #10491

Test plan

  • terminal-worktree-route / worktree-operation-route / runtime-owner tests
  • Manual: open terminal/agent on multi-host-visible project with a focused host

ELI5

When the same project appears on local and remote hosts, creating a terminal or agent could fail as "ambiguous identity." If your focused host uniquely picks an owner, Orca uses that host instead of hard-failing.

@coderabbitai

coderabbitai Bot commented Jul 25, 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: 526ba0e0-3960-4463-8eea-8a78f9bbc0e3

📥 Commits

Reviewing files that changed from the base of the PR and between 81e7a2d and e2520e5.

📒 Files selected for processing (8)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/lib/terminal-worktree-route.test.ts
  • src/renderer/src/lib/worktree-operation-route-focus.ts
  • src/renderer/src/lib/worktree-operation-route.test.ts
  • src/renderer/src/lib/worktree-operation-route.ts
  • src/renderer/src/lib/worktree-runtime-owner.test.ts
  • src/renderer/src/runtime/file-explorer-delete-owner-provenance.test.ts
  • src/renderer/src/store/slices/worktrees.test.ts
🚧 Files skipped from review as they are similar to previous changes (6)
  • src/renderer/src/lib/worktree-operation-route.test.ts
  • src/renderer/src/lib/terminal-worktree-route.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/runtime/file-explorer-delete-owner-provenance.test.ts
  • src/renderer/src/lib/worktree-runtime-owner.test.ts
  • src/renderer/src/store/slices/worktrees.test.ts

📝 Walkthrough

Walkthrough

Worktree operation routing now collects exact and repository-derived route candidates and resolves them using the focused execution host. Local and runtime projections can be selected when focus uniquely matches one candidate, while unmatched duplicate projections remain ambiguous or missing. Terminal PTY routing now uses the focused HUB without IPC transport in that case and fails closed when focus matches neither HUB. Ownership, deletion, and branch-removal tests cover the updated outcomes.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fix and tests, but it omits the required Screenshots, AI Review Report, Security Audit, and Notes sections. Add all required template sections, record the mandated test checks, and include AI review, cross-platform, security, and platform-risk summaries.
Docstring Coverage ⚠️ Warning Docstring coverage is 8.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes preferring the focused host for multi-host worktree identity resolution.
Linked Issues check ✅ Passed The routing changes and tests address issue #10491 by selecting the focused host and preserving fail-closed behavior when ownership remains ambiguous.
Out of Scope Changes check ✅ Passed The production and test changes stay within focused-host worktree routing and the linked issue objectives.

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.

🧹 Nitpick comments (2)
src/renderer/src/lib/worktree-operation-route-focus.ts (1)

34-62: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider adding a dedicated unit test file for this module.

preferFocusedRoute/resolveFromCandidateRoutes/addOperationRoute are pure, reusable helpers, but coverage is only indirect via worktree-operation-route.test.ts/terminal-worktree-route.test.ts. A focused worktree-operation-route-focus.test.ts would pin edge cases (e.g., >2 candidates with multiple focus matches, settings null/undefined) more cheaply than exercising them through the full aggregation pipeline.

src/renderer/src/lib/worktree-operation-route.ts (1)

230-242: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Scope the projection scan to the worktree's own repo instead of all repos.

Both loops iterate every repo's worktree array (Object.values(state.worktreesByRepo ?? {}) / ...detectedWorktreesByRepo) and filter by id inside collectOwnerRoutes, even though getRepoIdFromWorktreeId(worktreeId) — already used a few lines below at line 241 and at line 169 — can derive the owning repo id directly. This turns an O(worktrees-in-one-repo) lookup into an O(total-worktrees-across-all-repos) scan on every call, on what is likely a fairly hot path (terminal connect / owner-routed operations).

⚡ Proposed fix to scope the scan by repo id
 export function resolveExplicitWorktreeOperationRouteResult(
   state: WorktreeOperationRouteState,
   worktreeId: string
 ): WorktreeOperationRouteResolution {
   const exactRoutes = new Map<string, WorktreeOperationRoute>()
   const exactRepoIds = new Set<string>()
+  const worktreeRepoId = getRepoIdFromWorktreeId(worktreeId)
   // Why: scan every projection so focus can select the unique active-host owner (`#10491`).
-  for (const worktrees of Object.values(state.worktreesByRepo ?? {})) {
-    collectOwnerRoutes(state, worktrees, worktreeId, exactRoutes, exactRepoIds)
-  }
-  for (const result of Object.values(state.detectedWorktreesByRepo ?? {})) {
-    collectOwnerRoutes(state, result.worktrees, worktreeId, exactRoutes, exactRepoIds)
-  }
+  collectOwnerRoutes(
+    state,
+    state.worktreesByRepo?.[worktreeRepoId] ?? [],
+    worktreeId,
+    exactRoutes,
+    exactRepoIds
+  )
+  collectOwnerRoutes(
+    state,
+    state.detectedWorktreesByRepo?.[worktreeRepoId]?.worktrees ?? [],
+    worktreeId,
+    exactRoutes,
+    exactRepoIds
+  )

If there's a legacy scenario where a worktree id can live under a different catalog key than its own repo-id prefix, please confirm before applying — otherwise this preserves identical behavior while avoiding the full scan.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dd4b9cdf-9483-45fe-831a-313bd5addfb8

📥 Commits

Reviewing files that changed from the base of the PR and between 9eff372 and 5c5af57.

📒 Files selected for processing (8)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/lib/terminal-worktree-route.test.ts
  • src/renderer/src/lib/worktree-operation-route-focus.ts
  • src/renderer/src/lib/worktree-operation-route.test.ts
  • src/renderer/src/lib/worktree-operation-route.ts
  • src/renderer/src/lib/worktree-runtime-owner.test.ts
  • src/renderer/src/runtime/file-explorer-delete-owner-provenance.test.ts
  • src/renderer/src/store/slices/worktrees.test.ts

@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
@innocarpe
innocarpe force-pushed the fix/workspace-identity-ambiguous-route branch from 5c5af57 to c370412 Compare July 28, 2026 13:13
…ost (stablyai#10491)

When the same worktree/repo id is projected on multiple hosts, route terminal
and owner operations to the unique owner matching the focused host instead of
always failing closed as ambiguous. Still fail closed when focus does not
discriminate among candidates.
@innocarpe
innocarpe force-pushed the fix/workspace-identity-ambiguous-route branch from c370412 to e2520e5 Compare August 6, 2026 03:55
@innocarpe

Copy link
Copy Markdown
Contributor Author

Sync update (e2520e510e)

Rebase onto latest upstream/main: multi-host worktree routes collapse via focused host preference (#10491).

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Workspace identity becomes ambiguous when opening a new terminal or agent in a specific project

4 participants