Skip to content

fix(relay): desktop control lifecycle — truthful registration, silence watchdog, named-check host proof - #12076

Merged
Jinwoo-H merged 1 commit into
mainfrom
fix/relay-desktop-lifecycle
Aug 2, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
fix/relay-desktop-lifecycle

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Clean, relay-only replacement for #12060 (that branch's inherited history reverted unrelated main files; this branch is cut from main and contains exactly the relay changes).

Fixes the desktop half of the 2026-08-01 sustained Relay-only HOST_OFFLINE incident:

  • Dead-control promotion: hello-ack + close in one ws parser turn fired onClose before activate(); the dead control was promoted with no recovery path. RelayControlOrigin.activate now rejects controls that closed before activation; failures flow into existing retry paths.
  • No liveness detection: new 75s inbound-silence watchdog mirroring the relay's 15s ping contract; silent/zombie sockets terminate and trigger recovery.
  • Registered-without-proof: RelayAuthCoordinator requires broker.isLive() (plumbed client → origin → pool → broker) before republishing registered; dead brokers are replaced.
  • Silent failures: coordinator reconcile failures and pool recovery-attempt failures are logged; host-proof validation reports the failing check by name with relative deltas only (never values) — this instrumentation is how the live C15 +100ms clock-skew trigger was isolated. Keeps main's 30s skew bounds from fix(relay): tolerate wall-clock skew in host-proof validation #10474.

Evidence: red-before/green-after tests at each seam; 66 relay tests pass; live Relay-only iOS repro went from a sustained relay_outer_4404 loop with a lying registered badge (zero sockets) to truthful status, autonomous recovery, and sustained real terminal round-trips. Cloud counterparts: orca-cloud#207/#208/#209 (activation fence + fleet image), #210 (clock-skew monitor), #211 (activation hardening).

- RelayControlOrigin.activate rejects controls whose socket closed before
  activation (hello-ack and close in one ws parser turn previously published
  a dead control with no recovery path)
- RelayControlClient gains a 75s inbound-silence watchdog mirroring the
  relay's ping contract, so dead or server-side-unindexed sockets terminate
  and trigger origin recovery
- RelayAuthCoordinator only republishes 'registered' when the owned broker
  proves a live control, and logs reconcile failures instead of swallowing
  them; the origin pool logs recovery-attempt failures
- Host-proof validation reports the failing check by name (never values),
  keeping main's 30s skew bounds
- isLive() plumbed client -> origin -> pool -> broker
@coderabbitai

coderabbitai Bot commented Aug 2, 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: af20ed7a-6ef8-4d2b-b0dd-fea3e2232a85

📥 Commits

Reviewing files that changed from the base of the PR and between 1e9963d and 217cb99.

📒 Files selected for processing (11)
  • src/main/runtime/relay/relay-auth-coordinator.test.ts
  • src/main/runtime/relay/relay-auth-coordinator.ts
  • src/main/runtime/relay/relay-control-client.test.ts
  • src/main/runtime/relay/relay-control-client.ts
  • src/main/runtime/relay/relay-control-origin.ts
  • src/main/runtime/relay/relay-control-silence-watchdog.ts
  • src/main/runtime/relay/relay-control-url.ts
  • src/main/runtime/relay/relay-host-proof.ts
  • src/main/runtime/relay/relay-origin-pool.ts
  • src/main/runtime/relay/relay-session-broker.test.ts
  • src/main/runtime/relay/relay-session-broker.ts

📝 Walkthrough

Walkthrough

The relay control client now validates control URLs, reports named host-proof failures, and monitors inbound silence with a watchdog. Relay origins and session brokers expose live-control state and reject controls that close before activation. The authentication coordinator detects non-live registered brokers, replaces them, and logs reconciliation failures. Tests cover handshake closure, challenge validation, silence timeouts, ping activity, activation failure, and dead-broker replacement.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the changes and testing evidence but omits the required Screenshots, AI Review Report, Security Audit, and Notes sections. Add all required template sections, including the visual-change statement, test checklist, AI review and cross-platform confirmation, security audit, and notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 summarizes the relay lifecycle, registration, silence watchdog, and host-proof changes.
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.
✨ 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.

@greptile-apps

greptile-apps Bot commented Aug 2, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes three interconnected relay reliability bugs that caused a sustained HOST_OFFLINE incident on 2026-08-01: (1) a race where hello-ack and WebSocket close arriving in the same parser turn promoted a dead control with no recovery path, (2) zombie sockets with no server-side pings staying "active" indefinitely, and (3) the auth coordinator re-emitting registered for brokers whose underlying control had already died.

  • Activation fence (relay-control-origin.ts): activate() now guards against controls that were removed from the live set by a close event that fired synchronously before the connect-promise continuation ran, throwing relay_control_closed_before_activation and flowing into the existing retry path.
  • Silence watchdog (relay-control-silence-watchdog.ts + relay-control-client.ts): a new 75 s inbound-silence watchdog (checked every 15 s) mirrors the relay's own ping contract; silent sockets are terminated and trigger recovery.
  • Truthful liveness propagation (relay-session-broker.ts → relay-origin-pool.ts → relay-control-origin.ts → relay-control-client.ts): a new isLive() chain from client through broker lets the auth coordinator replace dead brokers instead of republishing registered for them.

Confidence Score: 4/5

Safe to merge. All three incident-root-cause paths are directly addressed with targeted, well-scoped changes and red-before/green-after test coverage at every seam.

The activation fence, silence watchdog, and isLive chain are all correctly implemented. The optional isLive field on CoordinatedRelayBroker defaults to 'live' for unknown implementations, which won't cause a regression but does leave the dead-broker replacement path silently disabled for any future broker that omits the method. The watchdog start() has no double-call guard. Both are style concerns with no current failure path, but they are worth addressing before the pattern proliferates.

Files Needing Attention: relay-auth-coordinator.ts (optional isLive contract) and relay-control-silence-watchdog.ts (unguarded start()) would benefit from a follow-up tightening.

Important Files Changed

Filename Overview
src/main/runtime/relay/relay-control-origin.ts Adds activation guard that rejects controls removed from the live set by a same-turn close event, and a hasLiveControl() delegation method. The fix is correct: the guard fires on !controls.has(control) (deleted by close handler) before activeControl is ever set, and the thrown error flows cleanly into existing retry paths.
src/main/runtime/relay/relay-control-silence-watchdog.ts New file implementing a 75 s inbound-silence watchdog with 15 s check cadence. start()/stop() are correctly idempotent-safe; the timer is unref'd; onSilence calls terminate() which flows through handleClose into the standard recovery path.
src/main/runtime/relay/relay-control-client.ts Integrates silence watchdog (start on activation, stop on close/closeNow), adds isLive() method, and enriches the challenge failure message with the named failing check. The watchdog stop in both closeNow() and handleClose() is intentional and idempotent; the double onClose() via deferred terminate() is pre-existing behaviour unchanged by this PR.
src/main/runtime/relay/relay-auth-coordinator.ts Adds isLive? liveness gate before re-emitting registered, and adds console.warn on broker-reconcile failures. The optional isLive with ?? true default is safe (this.ownership.broker is never null when this.ownership is set), but the optional field makes the contract implicit.
src/main/runtime/relay/relay-host-proof.ts Refactors the transcript validation from a short-circuit boolean chain to a named-check array that collects all failing checks. Semantically equivalent: null-guards on issuedAt are preserved per-check, the field count (16) pre-check is unchanged, and the named check strings include only relative time deltas, not absolute field values.
src/main/runtime/relay/relay-session-broker.ts Adds isLive() delegating to pool.hasLiveControl(). Simple one-liner; correctly guards with !this.closed first.
src/main/runtime/relay/relay-control-url.ts Pure extraction of controlWebSocketUrl from relay-control-client.ts into its own file. Zero logic changes.
src/main/runtime/relay/relay-control-client.test.ts Adds a scripted-socket test suite covering close-before-activation, watchdog termination, clock-skew tolerance, and ping keep-alive with accurate fake-timer modelling.
src/main/runtime/relay/relay-session-broker.test.ts Adds mock isLive and a test verifying close-before-activation propagates the correct error, never emits registered, emits offline, and stops the transport.
src/main/runtime/relay/relay-auth-coordinator.test.ts Adds test verifying dead-broker replacement on reconcile rather than re-emitting registered.

Reviews (1): Last reviewed commit: "fix(relay): make desktop control lifecyc..." | Re-trigger Greptile

this.ownership.identityKey === nextIdentityKey &&
// Why: registered must be provable; a broker whose control died without
// recovering falls through and is replaced instead of republished.
(this.ownership.broker?.isLive?.() ?? true)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 isLive is optional but effectively required for correct behaviour

broker?.isLive?.() ?? true silently treats any broker that omits isLive as live, so the dead-broker replacement path is never triggered for those implementations. This is safe for RelaySessionBroker (which always defines isLive), but it means a future CoordinatedRelayBroker implementor who forgets the method will see registered emitted indefinitely with no visible failure. Upgrading isLive to a required field on CoordinatedRelayBroker would make the contract explicit and catch omissions at compile time.

Comment on lines +20 to +28
start(): void {
this.lastInboundAt = Date.now()
this.timer = setInterval(() => {
if (Date.now() - this.lastInboundAt > this.limitMs) {
this.stop()
this.onSilence()
}
}, CHECK_INTERVAL_MS)
this.timer.unref?.()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 start() does not guard against double-calls

If start() were called a second time (e.g. via a future code path that forgets state-machine invariants), it would silently overwrite this.timer and orphan the previous interval, causing two concurrent watchdog timers. The current state machine in RelayControlClient makes this unreachable today, but a one-line guard would be cheap insurance: if (this.timer) return.

Comment on lines +111 to +125
const checks: [string, boolean][] = [
['issuedAt-readable', issuedAt !== null],
[
`issuedAt-not-future(d=${issuedAt === null ? 'n/a' : issuedAt - now}ms)`,
issuedAt === null || issuedAt - RELAY_HOST_PROOF_CLOCK_SKEW_MS <= now
],
[
`not-expired(d=${challenge.expiresAt - now}ms)`,
now - RELAY_HOST_PROOF_CLOCK_SKEW_MS <= challenge.expiresAt
],
['issuedAt-before-expiry', issuedAt === null || issuedAt <= challenge.expiresAt],
[
`window(w=${issuedAt === null ? 'n/a' : challenge.expiresAt - issuedAt}ms)`,
issuedAt === null || challenge.expiresAt - issuedAt <= MAX_HOST_PROOF_CHALLENGE_WINDOW_MS
],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Check-name deltas are embedded in the WS close-frame reason

failProtocol(...) passes the reason string to socket.close(code, reason), which sends it to the relay server in the WebSocket close frame. Strings like issuedAt-not-future(d=1500ms) and not-expired(d=75000ms) therefore reach the server. This is intentional for incident diagnostics as documented in the PR, but it's worth confirming the relay server does not log or forward these reasons to any client-visible surface — if it does, the relative delta could help an attacker infer the server's clock.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@Jinwoo-H
Jinwoo-H merged commit 714bcbe into main Aug 2, 2026
84 of 86 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#12076)

- RelayControlOrigin.activate rejects controls whose socket closed before
  activation (hello-ack and close in one ws parser turn previously published
  a dead control with no recovery path)
- RelayControlClient gains a 75s inbound-silence watchdog mirroring the
  relay's ping contract, so dead or server-side-unindexed sockets terminate
  and trigger origin recovery
- RelayAuthCoordinator only republishes 'registered' when the owned broker
  proves a live control, and logs reconcile failures instead of swallowing
  them; the origin pool logs recovery-attempt failures
- Host-proof validation reports the failing check by name (never values),
  keeping main's 30s skew bounds
- isLive() plumbed client -> origin -> pool -> broker

Co-authored-by: OrcaWin <[email protected]>
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.

2 participants