Skip to content

fix(mobile): send phone unpairing to servers the desktop reaches later - #51227

Merged
AmoabaKelvin merged 3 commits into
mainfrom
fix/relay-resync-revocations
Nov 28, 2026
Merged

AmoabaKelvin merged 3 commits into
mainfrom
fix/relay-resync-revocations

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

ELI5

When you unpair a phone on your Mac, the Mac has to tell every Orca server it relays that phone to, so each server drops the phone too. It used to tell only servers it had talked to since it last started, and only right away: a server that was offline, or any server after a Mac restart, kept the phone. Now the Mac remembers which servers hold its phones and tells each of them as soon as it reaches them again.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​165 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​165
Prod 11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​340 $\color{#cf222e}{\Huge{\mathbf{−}}}$​43 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​297

What Changed

What triggers a re-sync

  • Any change to the phone list. The device registry's change listener now drives phonesChanged(), so cascade revokes and edits made on disk count too. The explicit call in revokeMobileDevice is gone. The listener is deferred one microtask, because a save announces before the registry holds the saved list, and it is merged so a cascade runs once.
  • A server coming back into contact. The desktop watches the existing hosts.onStatusChanged and re-syncs a server when it goes from out of contact to in contact. WSL's bulk call, which carries no contact, is ignored.
  • The registry becoming ready. This catches servers that came into contact before the phone list could be read.

What gets sent

  • A server gets the new list only if it may still hold a dropped phone. Added phones still sync on their first relayed request, as before. A server that never held this desktop's phones is never contacted.
  • relayed-phone-servers.json in userData records which servers may hold this desktop's phones, so this survives restarts:
    • The record is written durably after the server says it can mint phones and before the sync that may mint one. If the write fails, the sync doesn't go out.
    • It is cleared after an empty sync is answered.
    • At launch, servers no longer saved on this desktop are pruned from it.
  • A null or unreadable registry is never sent as an empty list, which would revoke every phone on every server. The sync is refused instead.

Ordering

  • Syncs to one server go out one at a time, so an older list can never land after a newer one.
  • One check runs per server. A trigger that arrives during a check runs it once more afterwards.
  • A phone paired while a sync is in flight re-checks once that sync answers, instead of being refused.

Why

The next PRs in this stack let a phone hold its own pairing with a server, chained to its delegated row there. Unpairing the phone on the desktop then has to reach every server that holds it, including servers that were offline at the time and servers the desktop next reaches after a restart. Otherwise the phone keeps direct access.

Re-syncing every server on each launch would also mint the desktop's phones on servers nobody uses from the phone, such as a teammate's server. It would also wake idle desktops. Recording only the servers that actually hold phones keeps the nothing-to-do case at zero calls and zero writes.

Linked Issue

Part of #40079, #27988, #26291 and #10426. This is the revocation half; the phone's own server pairing comes later in this stack.

Visual Proof

N/A: no UI changes. The proof is a real three-process run, described under Testing.

Testing

Real run on macOS with two worktree-built orca serve processes: a server, and a desktop paired to it. A phone-protocol client was paired to the desktop.

  • The phone's relayed status.get reached the server. The server minted the delegated row, and the desktop wrote relayed-phone-servers.json with the server's id.
  • I stopped the server, removed the phone's row from the desktop's registry on disk, then restarted the server. Within 10 seconds the desktop saw the server back in contact, re-synced, and the server's delegated row count went from 1 to 0.

delegated-phone-grant-cache.test.ts runs the real cache over a real record file. It pins four cases:

  • A phone dropped while the server was unreachable is sent after a restart.

  • Contact flaps and added phones send nothing.

  • A phone dropped while the previous removal is still in flight is sent.

  • An unreadable phone list is never sent, and the check runs once the list is readable.

  • I manually tested these changes locally

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

  • Every added test fails without this change and avoids the test anti-patterns

Performance

  • No phones ever relayed: the desktop does no host lists, server resolves, RPCs, writes or timers. It reads one small file at launch, and status events cost one map update each.
  • Desktop with phones: each server holding its phones gets at most one extra sync per phone removal, and one after a reconnect when a removal is still pending.
  • Cascades: a cascade of registry saves runs one check.
  • The record file: it is written only when a server starts or stops holding phones. An unchanged set copies nothing.

Risk

The riskiest parts are the record file and the ordering, because they decide whether a revocation reaches a server.

  • If the record is lost (deleted or unreadable), a phone unpaired while its server was unreachable stays on that server until the next relayed request or the next phone change. That is today's behaviour.
  • A failed record write fails that relayed request; the phone retries it.

A plain revert restores the old behaviour. It leaves relayed-phone-servers.json behind, and nothing older reads it.

Level: Medium

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

On Windows, WSL hosts that this desktop hasn't started stay recorded: the launch prune compares against every saved server, not just the ones a phone can reach now. Builds on #51226 (merged).

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@AmoabaKelvin
AmoabaKelvin force-pushed the fix/relay-resync-revocations branch from 41b3e3d to 8c2abb9 Compare November 27, 2026 23:26
@AmoabaKelvin
AmoabaKelvin changed the base branch from fix/delegated-sync-origin to main November 27, 2026 23:31
Unpairing a phone re-synced only servers already in the relay's memory, and a
failed sync was never retried, so a server offline at the time, or any server
after a desktop restart, kept the phone's grant. The relay now re-syncs a
server when it comes back into contact, reacts to every registry change
(cascades and disk edits too), sends only when a phone was dropped, and never
sends an unreadable registry's empty list.
@AmoabaKelvin
AmoabaKelvin force-pushed the fix/relay-resync-revocations branch from 8c2abb9 to af96e02 Compare November 27, 2026 23:32
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The relay cache now tracks phone keys sent to each server and persists server IDs that may hold relayed phones. It checks for dropped phones after contact or phone-list changes, and serializes syncs. Phone-list reads distinguish an unreadable registry from an empty list. RPC paths queue relay refreshes, and tests cover restart recovery, sequential removals, and unreadable phone lists.

Merge Risk: Low · up to 54901

Phone unpairing now reaches servers that were offline or contacted after a restart, and unreadable phone lists are refused rather than sent as empty. One narrow ordering gap remains: if a server is retired while a sync is in flight, an older request could arrive after a newer revocation. It is low impact and bounded, so this is mergeable with owner awareness.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: phone unpairing is now sent to servers the desktop reaches later. It uses the fix(mobile) convention. The changeset matches: servers that were offline or reached after…
Description check Passed The description follows the template. It has ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Performance, Risk, Level, AI Disclosure, Review, Agent skill boundary, Notes, and Checklist s…
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: Title check

Explanation

The title names the main change: phone unpairing is now sent to servers the desktop reaches later. It uses the fix(mobile) convention. The changeset matches: servers that were offline or reached after restart now receive revocations via a persisted record of relayed-phone servers and a re-sync on contact.

Full details: Description check

Explanation

The description follows the template. It has ELI5, What Changed, Why, Linked Issue, Visual Proof, Testing, Performance, Risk, Level, AI Disclosure, Review, Agent skill boundary, Notes, and Checklist sections. The What Changed, Why, Performance, and Risk sections are detailed and match the code changes. The Linked Issue section cites related issues but uses 'Part of' rather than the template's 'Fixes #', which is acceptable for a stacked PR. The AI Disclosure and Review sections are empty, and the final pnpm checklist item is unchecked. These are minor gaps.

Resolution

Fill in the empty AI Disclosure and Review sections, or mark them N/A. Confirm the unchecked pnpm lint, typecheck, test, and build item before merge.

  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 814aca0a-f523-44e4-9d71-11720fbe27b6
Commits

Reviewing files that changed from the base of the PR and between 4b67e30 and 549010f.

Files selected for processing: 4

  • src/main/runtime/mobile-desktop-relay/delegated-phone-grant-cache.ts
  • src/main/runtime/mobile-desktop-relay/relayed-phone-servers.ts
  • src/main/runtime/rpc/server/pairing/device-registry-file-read.ts
  • src/main/runtime/rpc/server/pairing/device-registry-store.ts

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

@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: 4

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Minor · Keep per-server sync ordering after a server is retired. · delegated-phone-grant-cache.ts:54

src/main/runtime/mobile-desktop-relay/delegated-phone-grant-cache.ts:54
Minor · Security & Privacy · Quick win

Keep per-server sync ordering after a server is retired.

delete() removes the only record of the last sync to a server. After retirement, sync() starts a new sync without waiting for an older sync that is still in flight. If the older sync sends phones and the newer sync sends an empty list, the older request can arrive last. The server then keeps a phone the desktop has dropped. The newer sync has already cleared the durable record, so a restart does not retry the revocation. This can happen when the environment is retired by transport invalidation or same-ID re-pairing and the server remains configured.

Keep the last sync for each server in a map that delete() does not clear. Chain each new sync behind that entry.

Suggested fix

   private readonly entries = new Map<string, GrantsEntry>()
+  // Last sync per server. Kept past delete(), so a later sync still waits for an in-flight one.
+  private readonly tails = new Map<string, Promise<unknown>>()
-    const previous = this.entries.get(environmentId)?.promise.catch(() => undefined)
+    const previous = this.tails.get(environmentId)
     this.entries.set(environmentId, entry)
+    this.tails.set(environmentId, entry.promise.catch(() => undefined))
   dispose(): void {
     this.stopContactWatch()
     this.entries.clear()
     this.inContact.clear()
+    this.tails.clear()
   }
Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 739608ea-82ef-4aff-92e3-c5b269e446fb
Commits

Reviewing files that changed from the base of the PR and between 3e4a1d5 and af96e02.

Files selected for processing: 11

  • src/main/ipc/runtime-environments/mobile-relay/runtime-environment-mobile-relay-hosts.ts
  • src/main/runtime/mobile-desktop-relay/delegated-phone-grant-cache.test.ts
  • src/main/runtime/mobile-desktop-relay/delegated-phone-grant-cache.ts
  • src/main/runtime/mobile-desktop-relay/delegated-phone-grants.ts
  • src/main/runtime/mobile-desktop-relay/mobile-desktop-relay-hosts.ts
  • src/main/runtime/mobile-desktop-relay/mobile-desktop-relay.ts
  • src/main/runtime/mobile-desktop-relay/relayed-phone-servers.ts
  • src/main/runtime/rpc/server/pairing/device-registry-store.ts
  • src/main/runtime/rpc/server/runtime-rpc-lifecycle.ts
  • src/main/runtime/rpc/server/runtime-rpc-mobile-devices.ts
  • src/main/runtime/rpc/server/runtime-rpc-pairing.ts

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

@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

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 81d991b1-7631-44f1-8763-b2a249946407
Commits

Reviewing files that changed from the base of the PR and between af96e02 and 4b67e30.

Files selected for processing: 1

  • src/main/runtime/mobile-desktop-relay/delegated-phone-grant-cache.ts

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

…ed servers once, prune in the record's queue
@AmoabaKelvin
AmoabaKelvin merged commit 0ae0650 into main Nov 28, 2026
57 checks passed
@AmoabaKelvin
AmoabaKelvin deleted the fix/relay-resync-revocations branch November 28, 2026 00:11
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