The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(shared): probe once more when the heartbeat tick itself ran late
686f6c6159c4570c8a5e
diff --git a/apps/shared/src/json-rpc-channel.test.ts b/apps/shared/src/json-rpc-channel.test.tsindex 2fd66ea1f40..0e426a59d1a 100644--- a/apps/shared/src/json-rpc-channel.test.ts+++ b/apps/shared/src/json-rpc-channel.test.ts@@ -118,6 +118,50 @@ describe('JsonRpcRequestChannel', () => {}})+ // A renderer stall (frozen window, intensive throttling, OS suspend) delays+ // both the tick and the pong it is waiting for. The first tick after the+ // stall must probe again instead of reading the local gap as peer silence.+ it('a tick that ran a stall late gives the peer one probe before failing', async () => {+ vi.useFakeTimers()++ try {+ const failures: string[] = []++ const channel = new JsonRpcRequestChannel({+ heartbeatDeadlineMs: 300,+ heartbeatIntervalMs: 100,+ heartbeatLiveness: 'any-inbound',+ onHeartbeatFailure: e => void failures.push(e.message)+ })++ const { sent, transport } = spyTransport()++ channel.attach(transport)+ channel.startHeartbeat()++ const pings = () =>+ sent.map(f => JSON.parse(f) as { id: string; method: string }).filter(f => f.method === 'gateway.ping')++ // One tick on schedule: the ping goes out and its pong is never read.+ await vi.advanceTimersByTimeAsync(100)+ expect(pings()).toHaveLength(1)++ // The renderer stalls 500 ms: the callback runs once with a stale clock,+ // past the 300 ms deadline. That gap is local, so it re-probes.+ vi.setSystemTime(Date.now() + 500)+ await vi.advanceTimersByTimeAsync(100)+ expect(pings()).toHaveLength(2)+ expect(failures).toEqual([])++ // The fresh probe goes unanswered on schedule: one window, then the+ // transport is dead. The grace is not renewable.+ await vi.advanceTimersByTimeAsync(100)+ expect(failures).toHaveLength(1)+ } finally {+ vi.useRealTimers()+ }+ })+// 'response' mode itself stays available (explicit opt-in): a caller that// wants only a pong (or a response to its own request) to prove the// backend can answer keeps that stricter contract.diff --git a/apps/shared/src/json-rpc-channel.ts b/apps/shared/src/json-rpc-channel.tsindex 4a753c86958..3e045f0b1e1 100644--- a/apps/shared/src/json-rpc-channel.ts+++ b/apps/shared/src/json-rpc-channel.ts@@ -170,6 +170,10 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 120_000export const DEFAULT_HEARTBEAT_INTERVAL_MS = 15_000export const DEFAULT_HEARTBEAT_DEADLINE_MS = 45_000const MAX_OUTSTANDING_PINGS = 8+// A tick that lands this many intervals late ran behind a local stall (frozen+// renderer, intensive throttling, OS suspend): the gap says nothing about the+// peer, so the deadline is not judged on it alone.+const LATE_TICK_INTERVALS = 1.5// Hoisted decoder: attach mode can drive high-frequency binary frames (tool// deltas, reasoning streams) and a fresh TextDecoder per message is avoidable@@ -562,6 +566,11 @@ export class JsonRpcRequestChannel {// throttling), so the previous ping's pong is ~60 s old at every tick;// a deadline on its age alone dropped every idle hidden socket.let lastPingAt = 0+ let lastTickAt = Date.now()+ // One probe per silence episode: a tick that ran late cannot tell local+ // suspension from a dead peer, so it re-probes once instead of dropping a+ // socket whose pong is still queued behind the stall.+ let graceProbeSent = falsethis.heartbeatTimer = setInterval(() => {if (this.transport !== transport) {@@ -569,11 +578,24 @@ export class JsonRpcRequestChannel {}const now = Date.now()+ const late = now - lastTickAt >= this.options.heartbeatIntervalMs * LATE_TICK_INTERVALS- if (lastPingAt > this.lastLivenessAt && now - this.lastLivenessAt >= this.options.heartbeatDeadlineMs) {- this.failHeartbeat(new Error('WebSocket heartbeat acknowledgement timed out'))+ lastTickAt = now- return+ const unanswered = lastPingAt > this.lastLivenessAt++ if (!unanswered) {+ graceProbeSent = false+ }++ if (unanswered && now - this.lastLivenessAt >= this.options.heartbeatDeadlineMs) {+ if (!late || graceProbeSent) {+ this.failHeartbeat(new Error('WebSocket heartbeat acknowledgement timed out'))++ return+ }++ graceProbeSent = true}const id = `heartbeat-${++this.heartbeatSequence}`