Skip to content

fix(relay): attach a phone whose accept straddles a control rebind (desktop side) - #19238

Merged
Jinwoo-H merged 2 commits into
mainfrom
relay/pending-conns-replay
Sep 7, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
relay/pending-conns-replay

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​348 $\color{#cf222e}{\Huge{\mathbf{−}}}$​14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​334
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​116 $\color{#cf222e}{\Huge{\mathbf{−}}}$​15 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​101

Split: this is the desktop half only. The cell and contract half is #19266, and this PR is based on that branch. Held for the desktop review pass; depends on #19266 and must not land before it. After #19266 merges, retarget this to main and rebase.

ELI5

A phone connecting through the relay is announced to your desktop exactly once. If your desktop's control connection rotates at that moment, the announcement is lost and the phone sits there until it gives up, even though your desktop is online the whole time. The relay already re-lists those waiting connections when the desktop reconnects. Your desktop was reading that list and throwing it away. Now it picks them up.

What Changed

  • On activation, RelayControlOrigin dials a data socket for each pendingConns entry it is not already serving. Entries already spliced, or already holding a data socket, are skipped, so a rebind never double-dials.
  • The kind and relayDeviceId a dial needs come only from the relay, in this order: the ack entry when the cell states them (needs fix(relay): emit pending-conn details to hosts that advertise the capability (cell side) #19266), otherwise a conn-open this process already observed. observedOpens is capped at 16, evicted past the attach deadline, and cleared on close. An entry described by neither source is skipped with a warning, never dialed with guessed values, because kind decides pairing authority and relayDeviceId is the E2EE device binding.
  • conn-open is now forwarded while the control is draining. markDraining on the origin is removed; the pool's drain deadline still bounds the origin.
  • The desktop advertises x-orca-host-capabilities: pending-conn-details on the control WebSocket upgrade.
  • CloudRelayTransport.hasConnection exposes the already-open check the replay guard needs.

The header name and token are duplicated by hand because the desktop cannot import the relay contract. Both sides assert the literals, so drift fails a test instead of silently disabling the feature. The same is now true of RELAY_HOST_ATTACH_DEADLINE_MS, which mirrors RELAY_PROTOCOL_LIMITS.hostAttachDeadlineMs.

Why

The cell announces a connection with a single conn-open. When the desktop's control socket dies mid-accept, the phone waits out the 10s attach deadline and is closed HOST_OFFLINE even though the desktop is online. Desktop lease rotations happen fleet-wide in roughly 54-minute waves, so this recurs.

host-hello-ack already restates those connections in pendingConns. The desktop parsed the field and never acted on it, and separately dropped a conn-open that arrived while the control was draining. A draining cell cannot start a new accept, so such a frame predates the drain and only that cell holds the waiting phone.

Linked Issue

N/A. Found while investigating relay connection latency, not filed as an issue.

Visual Proof

N/A — transport-layer behavior with no rendered surface. The user-visible effect is a phone that attaches instead of timing out.

Testing

  • pnpm vitest run src/main/runtime/relay src/main/runtime/rpc — 278 files, 2384 passed, 1 skipped.

  • pnpm run check:code-quality:changed — 0 new findings across 7 changed files; type-aware 0; React Doctor 0.

  • Cell-side suites and typecheck are verified on fix(relay): emit pending-conn details to hosts that advertise the capability (cell side) #19266.

  • Mutation checks: removing the replay kills 5 tests; latching the capability per session instead of per socket fails the rebind-downgrade test on the cell half.

  • Platforms: the changed code is platform-independent Node in the main process, exercised by the unit suites above. Not separately smoke-tested on Windows or Linux.

  • I manually tested these changes locally

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

AI Disclosure

Internal contributor.

Review

Independent adversarial review, two rounds plus a nit round. Pullfrog: no new issues, one informational note that RELAY_HOST_ATTACH_DEADLINE_MS was not cross-pinned, now fixed with a test in this PR. CodeRabbit: no actionable comments.

Agent skill upstream boundary

  • Not applicable.

Notes

  • Backwards compatibility: all three version mixes are safe. Old desktop plus new cell gets the legacy ack. New desktop plus old cell gets the legacy ack and falls back to the observed conn-open, which works against every deployed cell. New plus new replays from the ack. Deploy the cell first.
  • Security: a replayed dial never invents kind or relayDeviceId; an entry the relay did not describe is skipped rather than dialed with a guess.
  • Cross-platform / SSH: no platform-dependent code paths touched.
  • Performance: observedOpens is bounded at 16 entries with deadline-based eviction and is cleared on origin close.
  • Not proven from the repo: that the production external load balancer forwards the x-orca-host-capabilities upgrade header. A stripped header degrades to the observed-conn-open path rather than failing. Worth one live probe against staging at deploy time.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • 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)

@coderabbitai

coderabbitai Bot commented Sep 7, 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: Team

Run ID: b58a9dfd-4aa4-49b6-a1c5-af641558e8dc

📥 Commits

Reviewing files that changed from the base of the PR and between 37279d4 and adcf659.

📒 Files selected for processing (1)
  • src/main/runtime/relay/relay-session-broker.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The relay control protocol now advertises pending connection details and supports optional connection metadata. The control client accepts connection-open messages during draining. Relay control origins retain observed opens, reconstruct pending connections after rebinds, avoid duplicate dials, enforce attach deadlines, and release failed connections. Tests cover handshake capabilities, drain handoff, replay behavior, duplicate prevention, closure, and broker rebinds.

Merge Risk: ⚪ Minimal · up to adcf6

This change preserves phone attachment across desktop control rebinds while avoiding duplicate data connections and cleaning up failed replay attempts. No concrete current-head merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description provides detailed ELI5, change, rationale, testing, compatibility, security, performance, and dependency information. However, the required Linked Issue section contains N/A, and the r… Provide the linked issue number in the Linked Issue section. Add and complete the checklist item confirming pnpm lint, pnpm typecheck, pnpm test, and pnpm build status, or explain which checks CI will cover.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 13 files. 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 identifies the relay fix for phone attachment when an accept spans a desktop control rebind.
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 provides detailed ELI5, change, rationale, testing, compatibility, security, performance, and dependency information. However, the required Linked Issue section contains N/A, and the required checklist item for lint, typecheck, test, and build status is missing.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 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.

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

Reviewed the accept-straddles-control-rebind fix across the cell (host-session-registry, relay-server, relay contract) and desktop (relay-control-client, relay-control-origin, protocol, transport, origin pool).

  • Cell emits pending kind/relayDeviceId — host-hello-ack now carries the reservation's pairing kind and device binding, but only when the control socket advertised pending-conn-details; legacy sockets get the byte-identical old shape, pinned with exact equality.
  • Desktop replays pending connections on activation — RelayControlOrigin dials each pendingConns entry it isn't already serving, sourcing kind/relayDeviceId from the ack or a remembered conn-open (observedOpens, capped at 16), and skips entries with neither source.
  • conn-open forwarded while draining — the client no longer drops conn-open in the draining state; markDraining/acceptingConnections on the origin is removed, with the pool's drain deadline still bounding the origin's lifetime.
  • Capability negotiated over the upgrade header — x-orca-host-capabilities is advertised on the control WebSocket upgrade and parsed server-side, keeping both HostHelloSchema and the legacy PendingConnectionSchema strict-compatible across all three version mixes.

I traced the ownership lifecycle (onConnectionOwned/onConnectionReleased through basisOrigins), the rebind and migrate drain paths, and the hasConnection/socketsByConnectionId replay guard and found no correctness issue: double-dials are prevented, the drain deadline still closes a draining origin even when it owns a straddling connection, and the per-socket WeakMap decays with the socket. The tests pin exact ack shapes and replayed messages and are capable of failing.

ℹ️ The mirrored attach-deadline literal is not cross-pinned

RELAY_HOST_ATTACH_DEADLINE_MS (src/main/runtime/relay/relay-control-protocol.ts:40) hand-mirrors the cell's RELAY_PROTOCOL_LIMITS.hostAttachDeadlineMs (10_000). The capability header and token are pinned on both sides, but this constant has no desktop-side assertion, so a future cell-side change would silently drift the desktop's observedOpens eviction window and replayed attachDeadlineMs. It is low-stakes today because the replayed deadline is already best-effort, so no change is required — flagging only so the decision to leave it unpinned is a conscious one.

Technical details
# Mirrored deadline literal is unpinned on the desktop side

## Affected sites
- src/main/runtime/relay/relay-control-protocol.ts:40 — `RELAY_HOST_ATTACH_DEADLINE_MS = 10_000` mirrors cloud/packages/relay-contract/src/protocol-limits.ts:16

## Required outcome
- None required; informational only.

## Open questions for the human
- Want the same both-sides-pinned guarantee applied to this constant (a desktop-side value assertion), or is the best-effort framing acceptable?

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@Jinwoo-H
Jinwoo-H force-pushed the relay/pending-conns-replay branch from 8eb6b7d to f0639a9 Compare September 7, 2026 08:37

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

Reviewed the two test-only commits added since the prior review: a contract test pinning the capability header/token literals, and a rebind suite proving the ack follows the successor control socket's capability rather than the session.

  • Pinned the capability header + token on the cloud side — contract.test.ts asserts RELAY_HOST_CAPABILITIES_HEADER and RELAY_HOST_CAPABILITY_PENDING_CONN_DETAILS equal the literals the desktop mirrors by hand, closing the drift that would silently disable the feature.
  • Covered a rebind that changes the host's stated capability — host-session-registry.test.ts adds downgrade/upgrade cases asserting the pendingConns ack follows each socket's own advertisement (legacy shape for a successor that drops the capability, detailed shape when it adds it), and refactors the ack/session fixtures into shared helpers.

The rebind tests exercise acceptControl/activate through the real rebind path and assert exact ack shapes, so a session-latched capability (the mutation the commit message calls out) would fail them. The pin test fails on literal drift on either side. No correctness concerns in the delta; the production code is unchanged from the previously reviewed version.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@Jinwoo-H
Jinwoo-H force-pushed the relay/pending-conns-replay branch from f0639a9 to 5ea2d46 Compare September 7, 2026 08:55
@Jinwoo-H Jinwoo-H changed the title fix(relay): attach a phone whose accept straddles a control rebind fix(relay): attach a phone whose accept straddles a control rebind (desktop side) Sep 7, 2026
@Jinwoo-H
Jinwoo-H changed the base branch from main to relay/pending-conns-replay-cell September 7, 2026 08:55
Jinwoo-H added a commit that referenced this pull request Sep 7, 2026
…pability (cell side) (#19266)

The cell announces a connection with a single conn-open. When the desktop's
control socket dies mid-accept the phone waited out the 10s attach deadline and
was closed HOST_OFFLINE, even though the desktop was online. host-hello-ack
already restates those connections in pendingConns, but only by connId and
connTicket, which is not enough for the desktop to dial: kind and relayDeviceId
decide the pairing authority a connection carries and the E2EE device binding,
so neither may be guessed.

The cell now states kind and relayDeviceId on each pending entry, but only to a
host that advertised it can read them: a shipped host parses those entries
strictly, so an unannounced key fails the whole ack parse and kills a working
control. The advertisement rides the control upgrade as
x-orca-host-capabilities, not host-hello, because HostHelloSchema is strict on
the cell too and any new hello key is refused by every already-deployed cell.

The capability is keyed by socket, not by session: a rebind can land a successor
whose decoder is older or newer than the one that opened the session, and the
ack must follow the socket that will actually read it.

With no capable host in the fleet the emitted ack is byte-identical to today's.
The desktop half that consumes the new fields is #19238.
@Jinwoo-H
Jinwoo-H changed the base branch from relay/pending-conns-replay-cell to main September 7, 2026 09:06

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

Reviewed the incremental delta since the prior Pullfrog review: the PR was split into its desktop half (cell + contract changes moved to #19266, base retargeted to relay/pending-conns-replay-cell) and one new test commit.

  • Split the PR into the desktop half — the cell/contract changes (host-session-registry, relay-server, relay contract) moved to #19266. The five desktop production files (relay-control-client, relay-control-origin, relay-control-protocol, relay-origin-pool, relay-transport) are byte-identical to the previously reviewed version, so no production behavior changed.
  • Pinned RELAY_HOST_ATTACH_DEADLINE_MS on the desktop side — a new test asserts the mirrored deadline literal is 10_000, closing the prior informational note that this mirror was not cross-pinned while the capability header/token were. The pin is a real assertion and fails on desktop-side drift; the cell side is already pinned in the contract suite, so drift in either direction now fails a test.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

Reviewed the incremental delta since the prior Pullfrog review: a single test commit, plus the PR base retarget from the cell branch to main.

  • Pinned RELAY_HOST_ATTACH_DEADLINE_MS on the desktop side — a new test asserts the mirrored deadline literal is 10_000, closing the prior informational note that this mirror was not cross-pinned while the capability header/token were. The pin is a real assertion and fails on desktop-side drift; the cell side is already pinned in the contract suite, so drift in either direction now fails a test.

No production code changed in this delta: the five desktop files and the cell-side files are byte-identical to the previously reviewed versions, and the retarget to main only re-lists the cell half (now also in #19266) in the diff without altering it.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@Jinwoo-H
Jinwoo-H force-pushed the relay/pending-conns-replay branch 2 times, most recently from 37279d4 to e909655 Compare September 7, 2026 09:31

@pullfrog pullfrog 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.

✅ No new issues found.

Reviewed changes

Reviewed the delta since the prior Pullfrog review: #19266 (the cell + contract half) merged to main, and this branch was rebased onto main, dropping its cell-side commits.

  • Cell half landed on main — #19266 (91d7783f2b) now carries host-session-registry/relay-server/relay-contract, so the cell changes are no longer part of this PR's diff against main.
  • Desktop half unchanged — the five production files (relay-control-client, relay-control-origin, relay-control-protocol, relay-origin-pool, relay-transport) and three test files are byte-identical (verified by blob hash) to the version reviewed and approved in the prior rounds. The rebase only re-hashed the two commits (fff8241c08 → 66eb1c6c50, 37279d4cad → e909655a2f).

The dependency is now satisfied, so the PR is clean and mergeable. No correctness, wire-compat, or rollout concern surfaced in this delta.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

ivanvykopal pushed a commit to ivanvykopal/orca that referenced this pull request Sep 7, 2026
…pability (cell side) (stablyai#19266)

The cell announces a connection with a single conn-open. When the desktop's
control socket dies mid-accept the phone waited out the 10s attach deadline and
was closed HOST_OFFLINE, even though the desktop was online. host-hello-ack
already restates those connections in pendingConns, but only by connId and
connTicket, which is not enough for the desktop to dial: kind and relayDeviceId
decide the pairing authority a connection carries and the E2EE device binding,
so neither may be guessed.

The cell now states kind and relayDeviceId on each pending entry, but only to a
host that advertised it can read them: a shipped host parses those entries
strictly, so an unannounced key fails the whole ack parse and kills a working
control. The advertisement rides the control upgrade as
x-orca-host-capabilities, not host-hello, because HostHelloSchema is strict on
the cell too and any new hello key is refused by every already-deployed cell.

The capability is keyed by socket, not by session: a rebind can land a successor
whose decoder is older or newer than the one that opened the session, and the
ack must follow the socket that will actually read it.

With no capable host in the fleet the emitted ack is byte-identical to today's.
The desktop half that consumes the new fields is stablyai#19238.
…esktop side)

The cell announces a connection with a single conn-open. When the desktop's
control socket dies mid-accept the phone waited out the 10s attach deadline and
was closed HOST_OFFLINE, even though the desktop was online. host-hello-ack
already restates those connections in pendingConns; the desktop parsed the field
and threw it away.

Desktop replays pendingConns on activation. It needs kind and relayDeviceId to
dial: they decide the pairing authority a connection carries and the E2EE device
binding, so neither may be guessed. Two sources, in order:

- the ack entry, when the cell states them. This is the case where the desktop
  never received the conn-open at all, i.e. the headline scenario, and it needs
  the cell change in #19266.
- the conn-open this process already saw, when the frame arrived but the data
  socket died with the control. Works against every deployed cell.

An entry described by neither is skipped with a warning rather than dialed.
Entries already spliced or already holding a data socket are skipped, so a
rebind never double-dials.

The desktop advertises x-orca-host-capabilities: pending-conn-details on the
control upgrade so a cell knows the ack entries will be read. The header name
and token are duplicated by hand because the desktop cannot import the relay
contract; both sides assert the literals so drift fails a test rather than
silently disabling the feature.

Also stop dropping a conn-open that lands while the control is draining. A
drain-only cell refuses new phones, so such a frame predates the drain and only
that cell holds the waiting phone.
RELAY_HOST_ATTACH_DEADLINE_MS duplicates RELAY_PROTOCOL_LIMITS.hostAttachDeadlineMs,
which the contract suite already pins to 10_000. Now that the cell and desktop
halves land as separate PRs the two can drift independently, and drift would
silently shorten both the observed-open eviction window and the deadline a
replayed dial states, with nothing failing.
@Jinwoo-H
Jinwoo-H force-pushed the relay/pending-conns-replay branch from e909655 to adcf659 Compare September 7, 2026 17:14
@Jinwoo-H
Jinwoo-H merged commit e60d9aa into main Sep 7, 2026
28 checks passed
@Jinwoo-H
Jinwoo-H deleted the relay/pending-conns-replay branch September 7, 2026 17:28
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