Skip to content

Let a remote agent ring the Mac to ask for the relay - #40

Merged
gshost1 merged 8 commits into
mainfrom
feat/relay-doorbell
Nov 6, 2026
Merged

gshost1 merged 8 commits into
mainfrom
feat/relay-doorbell

Conversation

@gshost1

@gshost1 gshost1 commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

What and why

Adds an opt-in relay doorbell so an agent whose relay is offline can ask the person at the Mac to turn it on. The Worker accepts a separate doorbell socket using a key derived from the relay secret, forwards only hashes of the ringer's client token or pairing, throttles rings, and maps the Mac's answer to an HTTP response. The Mac validates the live credential, screen state and prompt limits before asking for Touch ID naming the ringer. CLI on/off/status, app launch/lifecycle handling and API status are included; the local MCP still cannot turn the relay on.

Fixes the two confirmed blockers in the reviewed head:

  • A completed shutdown could be followed by a stale Touch ID approval reopening the relay. Final doorbell validity and start now share the relay toggle lock with shutdown. A generation invalidates pending approvals on relay off, doorbell off, reset and shutdown, including off/on while a prompt is pending. Touch ID remains outside the lock. A catalog transaction serializes the final live-credential check/start with all credential revocations, including share/unwrap and other catalog writers. This fixes relay reopening; revoked credentials were never resurrected.
  • Worker updates retain old hibernating relay sockets accepted without tags and with only { connectedAt } attached. Discovery and reconnect replacement now recognize that legacy shape. Doorbells require their explicit kind, so relay and doorbell sockets neither count as nor replace one another.

Merged current main 9ca4b392c35e47299d3c7f44efff52026716b8c6, including the other 11 reviewed PRs, before final verification. Fix commit: 574121c; integrated head: 9ab440feb9893c9a5c2840d2335cde2417b7eed5.

How tested

Final runs on the integrated head, all synthetic/offline with KEYS_TEST_ISOLATION=1 and GROK_HOME, CODEX_HOME, KEYS_DROID_HOME pointing at task temp directories:

  • swift test --filter 'Gateway|Grant|Relay|Revoke|Share|Unwrap|HeldClientToken': 446 executed, 3 skipped, 0 failures; 82.64 s wall. Includes all 34 doorbell tests, including 8 new deterministic regressions for the original kill race, doorbell off/off-on, relay off, process shutdown, revoke-all, token revoke and pairing revoke. Shutdown callbacks run both during the prompt and at the former check/start boundary, without timing sleeps.
  • swift test --sanitize=thread --filter 'Gateway|Grant|Relay|Revoke': 396 executed, 3 skipped, 0 failures, no race reports; 109.72 s wall.
  • npm --prefix Relay test: 68 passed, including 2 new workerd regressions for legacy discovery and reconnect replacement with a doorbell present; 8.51 s wall. No Uncaught (in promise) failures. The existing workerd reader-canceled diagnostic appeared without failing the suite.
  • swift build: passed; 3.47 s wall.
  • git diff --check: passed.

Before the fix, the original deterministic Swift reproducer failed both assertions (47.20 s including initial build):

XCTAssertFalse failed - kill completed before start, so relay must remain off
XCTAssertNotEqual failed: ("on") is equal to ("on")

Both Worker regressions also failed on the reviewed implementation (1.72 s): legacy discovery returned null, and reconnect left the legacy socket open. An initial test-fixture attempt returned a WebSocket through a non-upgrade test-runner request; the fixture was corrected to keep sockets inside the Durable Object callback before reproducing either production failure.

Full Swift suite and UI suites are left to CI on the exact pushed commit. Parent will independently review the final code/security diff and handle CI; this implementation run does not watch or wait for CI.

Risks

  • Final start briefly holds a catalog writer transaction as well as the relay toggle lock. The unattended Touch ID prompt holds neither.
  • The opt-in doorbell adds an outbound connection while enabled and can cause bounded Touch ID prompts if the user's Worker is compromised. Approving a ring only starts the relay; gateway credentials are still required for calls.
  • Users must redeploy their Worker to get the doorbell. Existing legacy relay sockets remain usable through the update and are replaced on reconnect.
  • Known relay/menu recovery flakes remain; no final-run retries were needed. npm ci reported 5 existing dependency advisories (2 moderate, 3 high); dependencies were not changed.
  • No installed app, real credentials, provider services or deployment were used for verification.

What's left

  • Parent's independent code/security review, CI on the pushed head, and merge.
  • Existing follow-ups: dashboard/menu doorbell controls, printing a ring command from share output, sharing the duplicated relay/doorbell connection loop, and a release note telling users to redeploy their Worker.

A Linux box or cloud agent whose calls get 503 mac_offline had no way to
ask for the relay: the Mac only dials the Worker while the relay is on.
The Worker now takes a second, low-privilege Mac socket, the doorbell,
authenticated with an HMAC of the relay secret so no new Worker secret is
needed, and a POST /.keysrs/doorbell from the agent's own client token or
pairing. Only SHA-256 hashes of those reach the Mac, one ring at a time and
at most every ten seconds; the Mac's answer becomes the agent's status.
The Worker can now ring, but the Mac had nothing listening while the relay
was off. Keysrs.app keeps the doorbell socket open from launch once the
person turns it on (keys relay doorbell on, one Touch ID to derive the
key), checks each ring's hash against live client tokens and pairings,
answers away, pending or not_now without a prompt where nobody should be
asked, and otherwise asks for Touch ID naming the token that rang before
turning the relay on as keys relay on would. Setup re-derives the key;
reset, purge and the kill switch turn the doorbell off and delete it.
Code review found that a ring's Touch ID prompt ran inside the lock that
keys relay off and revoke --all take, so a prompt nobody answered left the
kill switch hanging, and an approval that landed after the doorbell was
turned off or the token revoked still turned the relay on. The prompt now
runs outside that lock and both are checked again after it. Also: rings
are spaced per caller address so a made-up token cannot lock out real
agents, the Mac caps rings it handles, refusals that never showed a
prompt no longer count against the hour, a doorbell loop that ended by
itself reads as off, and a restart no longer races the old socket.
ThreadSanitizer on CI caught the doorbell test polling FakeRelayDialer's
attempts while the loop under test appended to them under the dialer's
lock; the read now takes the same lock.
The full suite pins which commands may launch the app and which read it
gracefully; the doorbell verbs follow keys relay on/off and keys relay
status, so they join those two sets.
Two RelayHostTests turned the relay off right after turning it on and then
asserted the fake socket was closed. When the stop landed before the loop
had dialed, there was no socket to close and the test failed on timing
alone (seen on this PR's ThreadSanitizer runs and on main). Both now wait
for the connection first.
A completed shutdown could be followed by a pending approval starting the relay. Serialize final validity and start with shutdown, invalidate outstanding approvals on off, and serialize credential revocation through the catalog transaction while keeping Touch ID outside the toggle lock.

Workers retain pre-update hibernating sockets without doorbell tags. Discover and replace those legacy relay sockets by their attachment without treating doorbells as relays; cover both blockers with deterministic offline regressions.
@gshost1
gshost1 merged commit 4f9f39d into main Nov 6, 2026
8 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