Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(phone): advertise relay identity for late adoption; surface pairing relay result

enes38c3001f58d3
diff --git a/src/main/pairing-service.ts b/src/main/pairing-service.ts
index 425170685..3722e342c 100644
--- a/src/main/pairing-service.ts
+++ b/src/main/pairing-service.ts
@@ -139,7 +139,13 @@ export interface PairingStartResult {
}
/** Fired once when pairing finishes: ok=true → a key was installed, ok=false → timeout/cancel. */
-export type PairingDone = { ok: boolean }
+export type PairingDone = {
+ ok: boolean
+ /** Only on ok=true: did the pairing come with a relay leg? 'off' = toggle disabled,
+ * 'failed' = enabled but the mint failed (the SILENT LAN-only degrade that cost a
+ * field debugging session — surface it, never swallow it). */
+ relay?: 'ok' | 'off' | 'failed'
+}
export interface PairingService {
/** Begin pairing; resolves once the listener is up. `onDone` fires exactly once later. */
@@ -491,7 +497,10 @@ export function createPairingService(relayDeps?: PairingRelayDeps): PairingServi
} else {
send(res, 200, JSON.stringify(responseObj), 'application/json')
}
- finish({ ok: true })
+ finish({
+ ok: true,
+ relay: !relayCtx ? 'off' : relayFields.relayDeviceToken ? 'ok' : 'failed'
+ })
} catch (err) {
send(res, 500, 'pairing failed')
console.warn('[pairing] request failed:', err)
diff --git a/src/main/remote/relay-advertise.ts b/src/main/remote/relay-advertise.ts
new file mode 100644
index 000000000..db74fa76f
--- /dev/null
+++ b/src/main/remote/relay-advertise.ts
@@ -0,0 +1,46 @@
+// Relay advertisement — the cure for the LAN-only-pairing trap without re-pairing.
+//
+// `connection.relay` on the phone is otherwise written at exactly ONE moment (the pairing
+// exchange), so a phone paired while "Reach this Mac from anywhere" was OFF stayed LAN-only
+// forever: flipping the toggle later changed nothing, and off-LAN every open died with a raw
+// connection error (field report). While the standing phone host is UP, we now advertise the
+// relay identity in ~/.nodeterm/relay.json. The phone reads it over the SAME TOFU-verified SSH
+// bootstrap it already uses for agent.json, then mints its own device token against the API
+// (the free-tier TOFU mint). The file carries ONLY public material — the same block every
+// pairing QR embeds, plus this desktop's device id for that mint; relay ACCESS remains gated
+// host-side by the pin-once SAS approval, exactly as for a pairing-minted identity.
+
+import { promises as fs } from 'fs'
+import os from 'os'
+import path from 'path'
+
+const FILE = path.join(os.homedir(), '.nodeterm', 'relay.json')
+
+export interface RelayAdvertisement {
+ v: 1
+ hostId: string
+ hostPublicKeyB64: string
+ relayEndpoint: string
+ hostDeviceId: string
+}
+
+/** Best-effort atomic write — a failed advertisement only means late adoption is unavailable. */
+export async function writeRelayAdvertisement(ad: RelayAdvertisement): Promise<void> {
+ try {
+ await fs.mkdir(path.dirname(FILE), { recursive: true, mode: 0o700 })
+ const tmp = `${FILE}.tmp`
+ await fs.writeFile(tmp, JSON.stringify(ad, null, 2) + '\n', { mode: 0o600 })
+ await fs.rename(tmp, FILE)
+ } catch {
+ // Advertisement is opportunistic; pairing-time provisioning still works.
+ }
+}
+
+/** Remove the advertisement (host stopped / toggle off) so phones stop offering adoption. */
+export async function removeRelayAdvertisement(): Promise<void> {
+ try {
+ await fs.unlink(FILE)
+ } catch {
+ // Already absent — fine.
+ }
+}
diff --git a/src/main/remote/standing-host.ts b/src/main/remote/standing-host.ts
index d17973bb4..8f3300b5c 100644
--- a/src/main/remote/standing-host.ts
+++ b/src/main/remote/standing-host.ts
@@ -36,6 +36,8 @@ import {
type HostSession
} from './host-service'
import { currentCanvas, initHostCanvasHub, subscribeCanvas } from './host-canvas-hub'
+import { hostIdFromPublicKeyB64 } from './relay-id'
+import { removeRelayAdvertisement, writeRelayAdvertisement } from './relay-advertise'
import { isPinned, pinDevice } from './approved-devices-core'
import { loadApprovedDevices, saveApprovedDevices } from './approved-devices'
@@ -340,6 +342,18 @@ export function initStandingHost(
})
pool.add(pooled)
scheduleRefreshFor(pooled, token.exp)
+ // A listener is registered at the relay → advertise the identity for LATE ADOPTION
+ // (~/.nodeterm/relay.json — see relay-advertise.ts): a phone whose pairing predates the
+ // toggle reads it over its SSH bootstrap and gains a relay leg without re-pairing.
+ // Written here (not in start()) so it only exists while the host is genuinely reachable.
+ const pub = publicKeyToB64(keys.publicKey)
+ void writeRelayAdvertisement({
+ v: 1,
+ hostId: hostIdFromPublicKeyB64(pub),
+ hostPublicKeyB64: pub,
+ relayEndpoint: RELAY_URL,
+ hostDeviceId: getDeviceId()
+ })
} finally {
opening = false
// If we're still short (e.g. TARGET_PENDING > 1, or one was consumed while minting), continue.
@@ -361,6 +375,9 @@ export function initStandingHost(
reconnectTimer = null
}
for (const p of [...pool]) removeFromPool(p)
+ // Host gone from the relay → stop advertising, so phones don't mint tokens against a
+ // host that will never answer.
+ void removeRelayAdvertisement()
}
function reconcile(): void {
diff --git a/src/renderer/components/PhonePairPopover.tsx b/src/renderer/components/PhonePairPopover.tsx
index 7ce20a1a2..0f9c454f7 100644
--- a/src/renderer/components/PhonePairPopover.tsx
+++ b/src/renderer/components/PhonePairPopover.tsx
@@ -23,7 +23,7 @@ export function PhonePairPopover({
onClose: () => void
onOpenSettings: () => void
}): React.JSX.Element {
- const { phase, qr, sshOpen, sshHealed, error, busy, start } = usePhonePairing()
+ const { phase, qr, sshOpen, sshHealed, relayResult, error, busy, start } = usePhonePairing()
const phoneAccessEnabled = useSettings((s) => s.settings.phoneAccessEnabled)
const updateSettings = useSettings((s) => s.update)
@@ -100,7 +100,19 @@ export function PhonePairPopover({
</>
)
) : phase === 'paired' ? (
- <div className="phone-pair__ok">✓ Paired — your phone can now connect.</div>
+ <>
+ <div className="phone-pair__ok">✓ Paired — your phone can now connect.</div>
+ {relayResult === 'ok' ? (
+ <div className="phone-pair__ok">Remote access is set up — reachable from anywhere.</div>
+ ) : relayResult === 'failed' ? (
+ <div className="phone-pair__warn">
+ ⚠ Remote-access setup failed — this pairing is LAN-only for now. Check the
+ internet connection and pair again to retry.
+ </div>
+ ) : relayResult === 'off' ? (
+ <div className="phone-pair__hint">LAN-only pairing — remote access is off.</div>
+ ) : null}
+ </>
) : phase === 'timeout' ? (
<>
<div className="phone-pair__hint">Pairing timed out.</div>
diff --git a/src/renderer/components/settings/sections/PhoneSection.tsx b/src/renderer/components/settings/sections/PhoneSection.tsx
index ab241742b..e454920ce 100644
--- a/src/renderer/components/settings/sections/PhoneSection.tsx
+++ b/src/renderer/components/settings/sections/PhoneSection.tsx
@@ -55,7 +55,7 @@ export function PhoneSection({ isActive }: { isActive: boolean }): React.JSX.Ele
// The shared pairing machine (also behind the top-right quick-pair popover); a completed
// pairing refreshes the device list below.
- const { phase, qr, sshOpen, sshHealed, error, busy, start, stop, reset } = usePhonePairing(
+ const { phase, qr, sshOpen, sshHealed, relayResult, error, busy, start, stop, reset } = usePhonePairing(
() => void refreshDevices()
)
@@ -196,6 +196,21 @@ export function PhoneSection({ isActive }: { isActive: boolean }): React.JSX.Ele
<p className="text-sm font-medium" style={{ color: '#30d158' }}>
✓ Paired. Your phone can now connect with its own key.
</p>
+ {relayResult === 'ok' ? (
+ <p className="text-sm" style={{ color: '#30d158' }}>
+ Remote access is set up — the phone can reach this machine from anywhere.
+ </p>
+ ) : relayResult === 'failed' ? (
+ <p className="text-sm" style={{ color: '#ff9f0a' }}>
+ ⚠ Remote-access setup failed, so this pairing is LAN-only for now. Check this
+ machine&apos;s internet connection and pair again to retry — or the phone will
+ pick it up by itself next time it connects on this network.
+ </p>
+ ) : relayResult === 'off' ? (
+ <p className="text-sm text-muted">
+ LAN-only pairing — remote access is switched off above.
+ </p>
+ ) : null}
<Button onClick={reset}>Pair another phone</Button>
</div>
) : null}
diff --git a/src/renderer/components/settings/usePhonePairing.ts b/src/renderer/components/settings/usePhonePairing.ts
index 4b6f21e69..af383bca5 100644
--- a/src/renderer/components/settings/usePhonePairing.ts
+++ b/src/renderer/components/settings/usePhonePairing.ts
@@ -18,6 +18,10 @@ export function usePhonePairing(onPaired?: () => void): {
qr: string
sshOpen: boolean
sshHealed: boolean
+ /** On phase 'paired': whether the pairing came with a relay leg ('off' = toggle disabled,
+ * 'failed' = mint failed → LAN-only). Surfaced so the silent degrade is visible at the one
+ * moment the user is looking. */
+ relayResult: 'ok' | 'off' | 'failed' | null
error: string
busy: boolean
start: () => Promise<void>
@@ -30,6 +34,7 @@ export function usePhonePairing(onPaired?: () => void): {
// Went from unreachable → reachable while the warning was showing: show a green confirmation
// instead of silently dropping the warning (the user just flipped a toggle; acknowledge it).
const [sshHealed, setSshHealed] = useState(false)
+ const [relayResult, setRelayResult] = useState<'ok' | 'off' | 'failed' | null>(null)
const [error, setError] = useState('')
const [busy, setBusy] = useState(false)
// Track whether a pairing listener is currently running so unmount can stop it.
@@ -70,6 +75,7 @@ export function usePhonePairing(onPaired?: () => void): {
setQr(dataUrl)
setSshOpen(open)
setSshHealed(false)
+ setRelayResult(null)
setPhase('waiting')
runningRef.current = true
} catch (err) {
@@ -97,6 +103,7 @@ export function usePhonePairing(onPaired?: () => void): {
runningRef.current = false
setQr('')
setPhase(result.ok ? 'paired' : 'timeout')
+ setRelayResult(result.ok ? (result.relay ?? null) : null)
if (result.ok) onPairedRef.current?.()
})
}, [])
@@ -111,5 +118,5 @@ export function usePhonePairing(onPaired?: () => void): {
}
}, [])
- return { phase, qr, sshOpen, sshHealed, error, busy, start, stop, reset: () => setPhase('idle') }
+ return { phase, qr, sshOpen, sshHealed, relayResult, error, busy, start, stop, reset: () => setPhase('idle') }
}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 54522440a..61f474a1b 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -1875,7 +1875,7 @@ export interface PairingApi {
/** Cancel an in-flight pairing (e.g. when the settings section unmounts). */
stop(): Promise<void>
/** Fires once when pairing finishes (ok=true paired, ok=false timeout). Returns unsubscribe. */
- onDone(cb: (result: { ok: boolean }) => void): () => void
+ onDone(cb: (result: { ok: boolean; relay?: 'ok' | 'off' | 'failed' }) => void): () => void
/** Live re-probe of 127.0.0.1:22, so the Remote Login warning can clear the moment the user
* flips the toggle in System Settings (polled by the UI only while the warning is showing). */
probeSsh(): Promise<boolean>