Skip to content

fix(mobile): try the desktop's other addresses when the nearest one doesn't answer - #51198

Merged
AmoabaKelvin merged 1 commit into
mainfrom
fix-endpoint-move-try-alternatives
Nov 27, 2026
Merged

AmoabaKelvin merged 1 commit into
mainfrom
fix-endpoint-move-try-alternatives

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

ELI5

When your desktop's local address changes, the phone asks the desktop for its new addresses and tries one. If the desktop has several addresses and the one the phone picked didn't answer, the phone gave up and stayed on Relay, even when another of the desktop's addresses would have worked. Now it tries the others too.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​70 $\color{#cf222e}{\Huge{\mathbf{−}}}$​8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​62
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​60 $\color{#cf222e}{\Huge{\mathbf{−}}}$​41 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​19

What Changed

Before: when the desktop stopped listing the phone's saved direct endpoint, the follower picked a single replacement (the address nearest the old one) and dialed only that one. If it missed, it dialed the same one again a minute later and never tried the desktop's other listed addresses of the same kind (a second interface, IPv6). The phone stayed on Relay until you re-paired or used Edit Host.

After:

  • directEndpointCandidates (was directEndpointCandidate) returns every listed same-kind address in the same order as before: nearest to the old address first, then the desktop's order on ties. LAN and Tailscale are still never mixed, and duplicates are dropped.
  • The follower dials them one at a time until one authenticates, at most three per read. A read where every dial missed starts the existing 60 s cooldown. The next read picks up at a cursor where the last one stopped, so capped reads work through the whole list rather than retrying the same dead addresses.
  • The cursor and cooldown reset on each new connection and after a success. As before, an address is saved only after this phone authenticates there, through the compare-and-swap in the host-list mutation queue.

Why

Root cause: the follower in host-direct-endpoint-follower.ts treated the nearest listed address as the only one the phone could reach. #50032 assumed a DHCP move stays in its subnet, and that held for the single-interface case it was built for. On a multi-homed desktop the nearest address can be one this phone can't reach while another listed address works.

The fix keeps candidate choice where it already lives (the ranking in host-direct-endpoint-refresh.ts and the follower's dial). The desktop RPC and the stored row shape don't change.

A cursor with one cooldown timestamp was chosen over a per-address miss map. Two numbers of state need no pruning or growth bound, and when there's nothing to do (saved address still listed, empty list, cooldown) the follower does no extra work.

Linked Issue

No separate issue. This follows up #50032: endpoint-move recovery could stop at the first advertised address and miss a reachable alternative.

Visual Proof

N/A. Nothing on screen changes. The phone just reaches a direct address in a case where it used to stay on Relay.

Testing

  • mobile/src/host-catalog/host-direct-endpoint-refresh.test.ts drives the real follower. The desktop lists three dead addresses and one live one. The first read dials the three dead ones. A read ten minutes later resumes at the live one and saves it.

    • It fails on main, which dials only the first dead address.
    • It also fails on an intermediate version that restarted from the top of the list on each read.
  • Focused test file, mobile typecheck (mobile/tsconfig.test.json) and pnpm check:changed pass on macOS.

  • 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

The extra dials happen only when the desktop no longer lists the saved endpoint: at most three sequential dials per foreground read, then a 60 s cooldown. The cooldown is checked before candidates are ranked. When the saved endpoint is still listed, the list is empty or the cooldown is active, the follower ranks nothing, allocates nothing and dials nothing. State per host is one cursor and one timestamp.

Risk

Level: Low

The phone may now dial up to three of the desktop's listed addresses where it used to dial one. That only happens while the saved address is gone from the desktop's list, and each dial must authenticate against the paired desktop key before anything is saved. Older desktops without pairing.getDirectEndpoints take the existing unsupported path and see no change. A plain revert backs this out, and no stored data changes shape.

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

Mobile-only change, no platform-specific code. Mixed versions: the desktop RPC contract is unchanged, and older desktops keep the pairing-time endpoint as before.

Follow-ups, not changed here:

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)

…t one does not answer

When a paired desktop stopped listing the phone's saved direct endpoint,
the phone dialed only the nearest listed replacement. If that one did not
answer, a reachable second address (another interface, IPv6) was never
tried and the phone stayed on relay.

The follower now works through the desktop's de-duplicated same-kind
addresses in nearness order, at most three dials per read, with a 60 s
cooldown after a read that missed and a cursor so the next read resumes
where the last stopped.
@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

The candidate helper now returns an ordered list of eligible replacement endpoints. The follower tries up to three candidates per read, advances after misses, and pauses for 60 seconds after a fully missed batch. A new connection resets candidate progress and the miss timer. Tests cover candidate filtering and ordering, and verify that the follower reaches a remaining replacement after a delay.

Merge Risk: Low · up to 5c85b

After a reconnect, a rare race can delay trying a newly listed desktop address by up to a minute. The behavior is otherwise a bounded improvement, and the change is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: when the desktop's nearest direct address does not answer, the mobile follower now tries the desktop's other listed addresses. It matches the raw summary, which descri…
Description check Passed The description follows the repository template. It has ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk with level, Agent skill boundary, Notes, and C…
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: when the desktop's nearest direct address does not answer, the mobile follower now tries the desktop's other listed addresses. It matches the raw summary, which describes trying multiple same-kind replacement endpoints with a cursor and cooldown. The title does not mention the cursor or cooldown, which is acceptable because it need not cover every detail.

Full details: Description check

Explanation

The description follows the repository template. It has ELI5, What Changed, Why, Linked Issue, Visual Proof (N/A with reason), Testing, Performance, Risk with level, Agent skill boundary, Notes, and Checklist sections. It explains the before and after behavior, the mechanism, and the rollback path. Gaps: the Linked Issue section names no issue number, only a follow-up reference to #50032, and the template requires an actual linked issue for outside contributors. The AI Disclosure section is empty. The Testing list leaves the manual testing box unchecked, and the final checklist item for lint, typecheck, test, and build is unchecked. The template's Review section is also empty.

Resolution

Consider filling the empty AI Disclosure section, and link an actual issue number in the Linked Issue section if one exists. Also confirm whether the lint, typecheck, test, and build commands were run, since the checklist item is unchecked.

  • 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

@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: 3e95ac17-900d-4bb9-9421-ad3b03c2bf68
Commits

Reviewing files that changed from the base of the PR and between 787c8b0 and 5c85b3b.

Files selected for processing: 3

  • mobile/src/host-catalog/host-direct-endpoint-refresh.test.ts
  • mobile/src/host-catalog/host-direct-endpoint-refresh.ts
  • mobile/src/transport/connection/host-direct-endpoint-follower.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.

}
nextReplacement = index + 1
}
missedAt = args.now()

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.

Minor · Stability & Availability · Quick win

Stop stale endpoint-list reads from restoring retry state after a reconnect.

If the endpoint-list response settles before a reconnect, the read can resume after onConnected() resets the retry state. This happens when the first read has no settled storage row and yields at markHostDirectEndpointListed. The stale read then dials under the new connection number. Its three misses set missedAt, and the queued fresh read returns at the cooldown guard. A newly listed live address can then go undialed for 60 seconds.

Capture the connection when read starts. Stop the replacement loop when that connection is no longer current, and set missedAt only for the current connection.

Suggested fix

-  const tryReplacements = async (candidates: readonly string[]): Promise<void> => {
+  const tryReplacements = async (candidates: readonly string[], epoch: number): Promise<void> => {
     const dials = Math.min(MAX_REPLACEMENT_DIALS_PER_READ, candidates.length)
     for (let dial = 0; dial < dials; dial += 1) {
-      if (stopped || !args.isForeground()) {
+      if (stopped || !args.isForeground() || epoch !== connection) {
         return
       }
       const index = nextReplacement % candidates.length
       const replacement = candidates[index]
       if (replacement === undefined || (await tryReplacement(replacement)) !== 'missed') {
         return
       }
       nextReplacement = index + 1
     }
-    missedAt = args.now()
+    if (epoch === connection) {
+      missedAt = args.now()
+    }
   }
   const read = async (): Promise<void> => {
+    const epoch = connection
     const reply = await pairingDirectEndpointsRead.request(client, {})
     if (candidates.length > 0) {
-      await tryReplacements(candidates)
+      await tryReplacements(candidates, epoch)
     }

Source: Learnings

@AmoabaKelvin
AmoabaKelvin merged commit 014b851 into main Nov 27, 2026
27 checks passed
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