Skip to content

fix(pairing): adding a server you already saved updates it instead of saving it twice - #31080

Merged
nwparker merged 7 commits into
mainfrom
nwparker/dedupe-paired-servers
Oct 20, 2026
Merged

nwparker merged 7 commits into
mainfrom
nwparker/dedupe-paired-servers

Conversation

@nwparker

@nwparker nwparker commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

If you add a server you already have (for example over a new network address, or with a fresh pairing code), Orca now updates the saved server instead of saving it a second time.

What Changed

Before: Orca only refused a second server with the same name. Pairing the same machine again under a new name (LAN plus a different address, or a re-pair) saved two servers. Both then listed the same workspaces, so every edit, delete or terminal write to those workspaces failed with "Workspace identity is ambiguous across hosts. Refresh projects and try again." Refreshing never fixed it.

After:

  • Settings > Remote Orca Servers and the sidebar's Add remote server: a code for a server you already saved updates that server in place. It keeps its name, id, projects and open tabs, takes the new address and token, and Orca says "This server is already paired as ; updated its connection." The name you typed is ignored, and you can now re-enter the saved server's own name, too.
  • orca environment add, cloud workspaces and the unverified add channel refuse instead, naming the saved server: "This server is already paired as "". To update its connection, add it again under Settings > Remote Orca Servers."
  • Servers Orca manages over SSH, WSL hosts, servers with SSH access links or an unfinished SSH access change, and cloud-workspace servers are never replaced this way; the refusal says which server and why.

Mechanism:

  • One choke point in the pairing store (saveEnvironmentFromPairingCode) looks the code's host public key up in every saved server's endpoints, including servers hidden because this build can't read their extra settings. Unpadded and URL-safe Base64 spellings of a key count as the same key, and a refresh keeps the saved spelling, so the host-key fingerprint doesn't change. A match is the same machine: the key is the server's persisted identity, which the pairing handshake proves.
  • Refreshing reuses the existing re-pair code path (replaceEnvironmentPairing, shared with updateEnvironmentFromPairingCode), so the pairing revision advances exactly as a repair does.
  • The verified-add handler then retires the connections still using the old pairing (invalidateTransport). A new publishVerifiedPairing helper, now shared by add and repair, publishes the verified status only if the saved pairing revision is still the one that was verified. This closes a window where an overlapping save could be stamped with stale status.
  • Renderer: the peer-replacement classifier used to treat any re-pair of a hand-paired server as a different machine, so its workspaces and tabs were dropped. It now keeps them when the old and new pairings carry the same host-key fingerprint. Without a fingerprint, with a different one, or when switching to or from a managed server, they are dropped as before.
  • If the connection fails right after a refresh, Settings no longer deletes the server. It only deletes a server it just created.

Why

  • Why not merge the new address as a second endpoint? Orca stores an endpoints list, but nothing ever fails over between endpoints. Every reader uses the preferred endpoint or the first one, and each endpoint has its own device token. A second endpoint would never be used.
  • Why the host key and not the name or address? The same machine changes address (LAN, VPN, tunnel), and different machines can share a name.
  • Why refuse in the CLI? Only the running app holds the open connections, which still authenticate with the replaced pairing. A CLI write would leave the app stuck on the old token until the next manual connect.
  • Why refuse on the raw add channel? Nothing on that channel checks the code before it would replace a working one.
  • Mixed versions: this is client-side only. Nothing on the wire changes. updatedExisting is an optional field on the desktop's own main-to-renderer IPC, which always ships in the same build. The paired host never sees it.
  • Existing duplicates: this PR stops new duplicates. A follow-up names both servers in the "ambiguous" error and marks them in Settings.

ref-oss check (after 3 review rounds): a comparable remote-host registry keys saved hosts by server identity and updates the existing entry in place, keeping its label. It merges addresses only because it fails over between them, which Orca does not. The approach stayed the same.

Linked Issue

N/A (internal follow-up from the SSH reliability review)

Visual Proof

The only visible change is the success toast text after re-adding a saved server. There is no layout change. The behavior is covered by renderer tests (toast text, no removal on failed connect, same-name entry reaches main).

Testing

  • New tests (each fails on origin/main):

    • src/shared/runtime-environment-store-same-server.test.ts: refresh in place; refuse where it can't switch connections; same-name refresh; tunnel dependency follows the new code; managed and cloud refusals.
    • src/shared/runtime-environment-store-unreadable-records.test.ts: a hidden server still refuses a second entry.
    • src/main/ipc/runtime-environments/runtime-environments-pairing.test.ts: the verified add refreshes and retires the old connection; an overtaken refresh is not stamped with stale status.
    • src/renderer/src/components/settings/use-runtime-environment-mutation-actions.test.tsx: no removal of a refreshed server, the toast, and the saved server's own name reaching main.
    • src/renderer/src/store/slices/runtime-managed-repair-keeps-workspaces.test.ts: a same-key refresh keeps workspaces and tabs; a different key drops them.
    • src/cli/runtime/environments.test.ts: the CLI refuses the duplicate.
  • Fixtures that paired "different" servers with one shared test key now use distinct keys.

  • Locally:

    • pnpm tc.
    • The static-analysis job steps (lint, format, code-quality, ratchets, localization).
    • These test folders, single worker: src/shared/runtime-environment*, src/shared/wsl-host*, src/main/ipc/runtime-environments, src/cli/runtime, src/renderer/src/components/settings, src/renderer/src/runtime, src/renderer/src/store/slices.
  • Verified live: none. Covered by unit and integration tests only. The pairing integration test runs a real runtime server over WebSocket.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

Notes

  • Follow-up: the existing repair flow (verifyAndRepairRuntimeEnvironmentFromPairingCode) still compares keys as raw strings, so a repair link with a differently spelled key reads as a different server. That predates this PR.

  • Managed-server and WSL-host registration (addManagedOrcadEnvironment, the WSL host store) can still save an entry whose key matches a hand-paired one. That is out of scope here.

  • Records this build cannot parse at all are still invisible to the check. This predates the change.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

Pairing offers now match saved servers by normalized host key. Eligible pairings update the saved connection while retaining the server’s existing identity; replacement guards reject unsupported cases. Verification reports whether it updated an existing entry and publishes status only when the saved pairing revision still matches. The renderer displays update feedback and retains refreshed entries if reconnection fails. Peer replacement checks use host-key fingerprints to classify hand-paired servers.

Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 8d77b

A pairing code with an unusual but valid host-key spelling can add a duplicate server instead of updating the saved one. Fix the key comparison before merge if avoiding duplicates for these codes is required.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 41.18% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 34 functions across 26 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: re-adding a saved server updates it instead of creating a duplicate.
Description check Passed The description is comprehensive and covers the user impact, implementation, rationale, testing, limitations, and visual impact. It does not provide a linked issue and does not use the exact required …
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.
Full details: Description check

Explanation

The description is comprehensive and covers the user impact, implementation, rationale, testing, limitations, and visual impact. It does not provide a linked issue and does not use the exact required N/A wording for visual proof, but it is otherwise mostly complete.

  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4ffba11b-f54f-4a2d-b7a2-7fa5f87c9c11
📥 Commits

Reviewing files that changed from the base of the PR and between 43b2d95 and 5af0c4c.

📒 Files selected for processing: 32
  • src/cli/runtime/environments.test.ts
  • src/main/ipc/runtime-environments/runtime-environment-connectivity-handlers.ts
  • src/main/ipc/runtime-environments/runtime-environment-pairing-verification.ts
  • src/main/ipc/runtime-environments/runtime-environment-preference.test.ts
  • src/main/ipc/runtime-environments/runtime-environment-repair-handler.ts
  • src/main/ipc/runtime-environments/runtime-environment-session-tabs-delta.integration.test.ts
  • src/main/ipc/runtime-environments/runtime-environment-verified-pairing-publish.ts
  • src/main/ipc/runtime-environments/runtime-environments-pairing.test.ts
  • src/main/ipc/runtime-environments/runtime-environments-repair.test.ts
  • src/renderer/src/components/settings/RuntimeEnvironmentsPane.tsx
  • src/renderer/src/components/settings/use-runtime-environment-mutation-actions.test.tsx
  • src/renderer/src/components/settings/use-runtime-environment-mutation-actions.ts
  • src/renderer/src/components/sidebar/AddRemoteHostDialog.tsx
  • src/renderer/src/i18n/locales/en.json
  • src/renderer/src/i18n/locales/es.json
  • src/renderer/src/i18n/locales/fr.json
  • src/renderer/src/i18n/locales/ja.json
  • src/renderer/src/i18n/locales/ko.json
  • src/renderer/src/i18n/locales/zh.json
  • src/renderer/src/store/slices/runtime-environment-peer-replacement.test.ts
  • src/renderer/src/store/slices/runtime-environment-peer-replacement.ts
  • src/renderer/src/store/slices/runtime-managed-repair-keeps-workspaces.test.ts
  • src/shared/remote-pairing-verification.ts
  • src/shared/runtime-environment-pairing-replace-guards.ts
  • src/shared/runtime-environment-sidecar.test.ts
  • src/shared/runtime-environment-ssh-access-store.test.ts
  • src/shared/runtime-environment-store-same-server.test.ts
  • src/shared/runtime-environment-store-unreadable-records.test.ts
  • src/shared/runtime-environment-store.test.ts
  • src/shared/runtime-environment-store.ts
  • src/shared/runtime-environments.ts
  • src/shared/wsl-host-deployment-store.test.ts
💤 Files with no reviewable changes: 1
  • src/renderer/src/components/settings/RuntimeEnvironmentsPane.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

Comment thread src/shared/runtime-environments.ts Resolved

@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


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bf891e04-dcc9-465e-b15a-1c517ef4dd5c
📥 Commits

Reviewing files that changed from the base of the PR and between 5af0c4c and 8d77bd9.

📒 Files selected for processing: 3
  • src/shared/runtime-environment-host-key-match.ts
  • src/shared/runtime-environment-store-same-server.test.ts
  • src/shared/runtime-environment-store.ts
🚧 Files skipped from review as they are similar to previous changes: 2
  • src/shared/runtime-environment-store-same-server.test.ts
  • src/shared/runtime-environment-store.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.


/** Why: pairing links accept unpadded and URL-safe Base64, which decode to the same key. */
function canonicalHostKeyB64(publicKeyB64: string): string {
return publicKeyB64.trim().replace(/-/g, '+').replace(/_/g, '/').replace(/=+$/, '')

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

🔎 Supported by static analysis

🏁 Script executed:

#!/bin/bash
# Inspect whether pairing-code parsing rejects noncanonical host-key Base64.
ast-grep outline src/shared/pairing.ts --match 'parseRemotePairingCode|PairingOffer' --view expanded
ast-grep outline src/shared/runtime-environments.ts --match 'KnownRuntimeEnvironmentSchema' --view expanded
rg -n -C 5 'publicKeyB64|parseRemotePairingCode|base64url|base64' src/shared/pairing.ts src/shared/runtime-environments.ts

Repository: stablyai/orca

Length of output: 8027


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- pairing.ts ---'
nl -ba src/shared/pairing.ts | sed -n '1,115p'
printf '%s\n' '--- runtime-environment-host-key-match.ts ---'
nl -ba src/shared/runtime-environment-host-key-match.ts | sed -n '1,180p'
printf '%s\n' '--- direct consumers ---'
rg -n -F --glob '*.ts' -e 'findEnvironmentWithHostKey' -e 'savedHostKeySpelling' -e 'parsePairingCode' src

Repository: stablyai/orca

Length of output: 22112


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- PairingOfferSchema ---'
rg -n -C 12 -F -- 'PairingOfferSchema' src/shared/mobile-relay-pairing-offer.ts
printf '%s\n' '--- runtime-environment-store pairing branch ---'
nl -ba src/shared/runtime-environment-store.ts | sed -n '65,125p'

Repository: stablyai/orca

Length of output: 4599


🏁 Script executed:

nl -ba src/shared/mobile-relay-pairing-offer.ts | sed -n '1,112p'

Repository: stablyai/orca

Length of output: 4992


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- parseRemotePairingCode binding ---'
rg -n -C 8 -F -- 'parseRemotePairingCode' src/shared src/main src/cli
printf '%s\n' '--- runtime-environment-store imports ---'
nl -ba src/shared/runtime-environment-store.ts | sed -n '1,35p'

Repository: stablyai/orca

Length of output: 5946


🏁 Script executed:

nl -ba src/shared/runtime-environment-store.ts | sed -n '137,164p'

Repository: stablyai/orca

Length of output: 1754


Match decoded host keys for non-relay pairing offers.

parseRemotePairingCode accepts noncanonical publicKeyB64 values for non-relay offers. The current matcher only normalizes the string, so equivalent decoded keys with different unused Base64 padding bits can compare differently. The update path can then create a duplicate saved server instead of refreshing the existing entry. Compare validated decoded keys while preserving savedHostKeySpelling for storage.

@nwparker
nwparker merged commit 63246eb into main Oct 20, 2026
50 of 51 checks passed
nwparker added a commit that referenced this pull request Nov 6, 2026
…39198)

Since #31080 a client keeps one saved entry per server, so pairing the same
host a second time under another name is refused. The steady 'Direct host' is
now a different server than the interrupted one. Tooltip checks name the host
so a closing tooltip from the previous card cannot match. Route the spec from
its pairing-store and runtime-status sources.
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.

1 participant