Skip to content

fix(remote): the Node remote client sends a heartbeat and treats 45 s of silence as a drop (#973) - #995

Merged
davideast merged 4 commits into
mainfrom
fix/remote-client-heartbeat
Oct 15, 2026
Merged

davideast merged 4 commits into
mainfrom
fix/remote-client-heartbeat

Conversation

@davideast

@davideast davideast commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Makes the Node remote client (connectRemoteSandbox, and the lazy remoteSandbox() handle that @pyric/cli/register installs for pyric-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.

// server.mjs, run as: PYRIC_SANDBOX=remote:http://localhost:5173 node --import @pyric/cli/register server.mjs
import { remoteSandbox } from '@pyric/cli/remote';

const sandbox = remoteSandbox();
sandbox.rtdb.onValue('ticks', snapshot => console.log('tick', snapshot.value));

// The client pings at attach and every 15 s. If the host stops answering
// without closing the socket (a paused process, a half-open connection),
// the client notices at the first check after 45 s and logs once:
//   pyric: the remote sandbox sent nothing for 45s at http://localhost:5173;
//   the next op connects again, and live listeners reconnect when the host answers.
// The listener is then resubscribed on the new connection.

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:

  • After 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 every HOSTED_HEARTBEAT_INTERVAL_MS.
  • Before each ping it checks silence. Any frame counts, not only pong. After 45 s it calls ws.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 with unavailable. An op in flight on the silent socket fails once and is not replayed; an op issued while reconnecting waits for the existing ensure() attempt (no decision-7 hold, as the amendment states).
  • The watch belongs to one socket: it stops on close and on close(), and never pings an old socket. Pings go straight to the socket, outside the operation budget, and never ref() 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 with setHeartbeatForTesting, which no package entry exports, like the web client's setHoldDeadlineForTesting.

Risk

Integration evidence

The app scenario that drives a paused node --import @pyric/cli/register process 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 is packages/cli/test/remote/reconnect.test.ts ("remoteSandbox when its host stops answering without closing the socket"), which runs the real client over a real ws server whose sockets go silent without closing, and loop-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 after stop(); a ping the socket refuses counts as a drop.
  • test/remote/reconnect.test.ts, new describe block over a real ws server: 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 with unavailable and an op issued after the loss runs once on the new socket; the eager handle ends its listener with unavailable. With the heartbeat start commented out, the last three fail (no loss is ever reported).
  • test/remote/loop-hold.test.ts: spawns node on dist/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. With timer.unref() removed from the built file, the process hangs and the test fails.
  • Ran: bun test test/remote (48 pass), plus test/bridge, test/functions-rtdb, test/register, test/serve/hosted-sqlite.test.ts, test/serve/vite-plugin-restart.test.ts, and packages/pyric-admin test/remote and test/surface (141 pass). Two project-host-selection tests that connect to 127.0.0.1:1 time out on my machine because that connect hangs locally; they do not reach the heartbeat.
  • Worker protocol parity (AGENTS rule 3): no protocol change. ping/pong already exist and the hosted bridge-mount's consumer session answers ping.

… 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
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