Repository navigation
fix(remote): the Node remote client sends a heartbeat and treats 45 s of silence as a drop (#973) - #995
Merged
Conversation
… of silence as a drop (#973) connectRemoteSandbox, and the lazy remoteSandbox handle that @pyric/cli/register installs, now follow ADR-0019's heartbeat amendment. After attach-ack the client pings at once, which starts the host's watch, then every HOSTED_HEARTBEAT_INTERVAL_MS. Before each ping it checks how long the host has been silent; after 45 s with no frame of any kind it terminates the socket, so the existing close path runs: the eager handle ends its listeners with unavailable, and the lazy handle reconnects on the ADR backoff and resubscribes them. The watch lives in remote/host-liveness.ts. It belongs to one socket and stops on close and on client close, pings bypass the operation budget, and every timer is unref'd so an idle script still exits. The 45 s value is local until the shared HOSTED_CLIENT_LIVENESS_TIMEOUT_MS lands in bridge/protocol.ts. Tests: host-liveness.test.ts (fake timers); reconnect.test.ts over a real ws server that stops answering without closing (fails on main: no loss is reported and the listener never resubscribes); loop-hold.test.ts spawns a real Node process on the built client that stays idle through several heartbeats and exits. Refs #973
…ption (#973) ADR-0019 fixes the 15 s heartbeat and the 45 s silence window in each client, as it fixes the hold deadline. Drop heartbeatIntervalMs and hostSilenceTimeoutMs from ConnectRemoteSandboxOptions, which @pyric/cli/remote exports, and let tests shorten the timing through setHeartbeatForTesting in remote/host-liveness.ts, which no package entry exports, as the web client's setHoldDeadlineForTesting does. Refs #973
This was referenced Oct 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes the Node remote client (
connectRemoteSandbox, and the lazyremoteSandbox()handle that@pyric/cli/registerinstalls forpyric-admin) send a heartbeat, and treat 45 s with no frame from the host as a dropped connection, as ADR-0019's heartbeat amendment requires.A silent host is now a dropped connection
Before this change, the remote client sent no
ping, so the host never watched it, and a host that stopped answering without closing the socket left the client waiting with no loss reported. Now:attach-ack, the client sends{ type: 'ping', id }at once, which starts the host's existing watch (it watches a socket from its first ping), then everyHOSTED_HEARTBEAT_INTERVAL_MS.pong. After 45 s it callsws.terminate(), so the existing close path runs with the reason "the remote sandbox sent nothing for 45s". The lazy handle reconnects on the ADR backoff and resubscribes; the eager handle ends its listeners withunavailable. An op in flight on the silent socket fails once and is not replayed; an op issued while reconnecting waits for the existingensure()attempt (no decision-7 hold, as the amendment states).close(), and never pings an old socket. Pings go straight to the socket, outside the operation budget, and neverref()it. Every timer is unref'd, so an idle script still exits.The watch is
packages/cli/src/remote/host-liveness.ts. The 15 s and 45 s are fixed, as ADR-0019 rules; there is no public option. Tests shorten them withsetHeartbeatForTesting, which no package entry exports, like the web client'ssetHoldDeadlineForTesting.Risk
pingwould now be dropped after 45 s idle. The consumer session the client attaches to (ConsumerSessioninbridge/server/peer.ts, used by the Node host and the Vite bridge) already answerspingwithpong, so this only changes behavior for a host that is truly silent.HOSTED_CLIENT_LIVENESS_TIMEOUT_MSthat fix(bridge): the host runs a native or remote client's onDisconnect when its socket closes (#973) #996 added tobridge/protocol.ts.Integration evidence
The app scenario that drives a paused
node --import @pyric/cli/registerprocess and a paused host (test/apps/host-closes-silent-consumer, lane E of the #973 plan) is a later PR, because it needs both this change and the host change. Until then, the evidence ispackages/cli/test/remote/reconnect.test.ts("remoteSandbox when its host stops answering without closing the socket"), which runs the real client over a realwsserver whose sockets go silent without closing, andloop-hold.test.ts, which spawns a real Node process on the built client.Refs #973
Implementation notes
test/remote/host-liveness.test.ts(fake timers): ping at start and on each interval with distinct ids; silence reported once, at the first check after the timeout, with no ping on the dead socket; any frame resets the window; nothing afterstop(); a ping the socket refuses counts as a drop.test/remote/reconnect.test.ts, new describe block over a realwsserver: a host that answers pings keeps the connection past the timeout; a muted socket gives one loss, a reconnect and a resubscribed listener that delivers the new value; an op on the silent socket fails once withunavailableand an op issued after the loss runs once on the new socket; the eager handle ends its listener withunavailable. With the heartbeat start commented out, the last three fail (no loss is ever reported).test/remote/loop-hold.test.ts: spawnsnodeondist/remote/index.js, attaches, runs one op, stays idle through several 50 ms heartbeats, and exits with code 0 within the deadline; the host counted at least 3 pings. Withtimer.unref()removed from the built file, the process hangs and the test fails.bun test test/remote(48 pass), plustest/bridge,test/functions-rtdb,test/register,test/serve/hosted-sqlite.test.ts,test/serve/vite-plugin-restart.test.ts, andpackages/pyric-admintest/remoteandtest/surface(141 pass). Twoproject-host-selectiontests that connect to127.0.0.1:1time out on my machine because that connect hangs locally; they do not reach the heartbeat.ping/pongalready exist and the hosted bridge-mount's consumer session answersping.