Skip to content

The agent that grows with you

fix(desktop): restart replay after interrupted reconnect

KoNit-K93b3aa0674a3
diff --git a/apps/shared/src/json-rpc-gateway-replay.test.ts b/apps/shared/src/json-rpc-gateway-replay.test.ts
index a5875738308..d34164335e1 100644
--- a/apps/shared/src/json-rpc-gateway-replay.test.ts
+++ b/apps/shared/src/json-rpc-gateway-replay.test.ts
@@ -289,6 +289,40 @@ describe('JsonRpcGatewayClient event-seq tracking + replay resume', () => {
client.close()
})
+ it('restarts replay after its socket is invalidated before rejected cleanup runs', async () => {
+ const client = makeClient()
+ const seen: number[] = []
+ client.on('message.delta', e => seen.push((e as unknown as { seq: number }).seq))
+
+ const first = client.connect('ws://x')
+ sockets[0].open()
+ await first
+ sockets[0].serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: 's1', seq: 1 } })
+
+ client.invalidate('first drop')
+ const second = client.connect('ws://x')
+ sockets[1].open()
+ await second
+ await vi.waitFor(() => expect(sockets[1].lastRequest().method).toBe('session.events.since'))
+
+ // The old request rejects asynchronously after detach. Open the
+ // replacement before its cleanup runs; it must own a fresh replay.
+ client.invalidate('second drop')
+ const third = client.connect('ws://x')
+ sockets[2].open()
+ await third
+ await vi.waitFor(() => expect(sockets[2].lastRequest().method).toBe('session.events.since'))
+
+ const request = sockets[2].lastRequest()
+ sockets[2].serverFrame({
+ jsonrpc: '2.0', id: request.id,
+ result: { events: [{ type: 'message.delta', session_id: 's1', seq: 2 }], latest_seq: 2, truncated: false, count: 1 }
+ })
+
+ await vi.waitFor(() => expect(seen).toEqual([1, 2]))
+ client.close()
+ })
+
it('clears stale watermarks when the backend epoch changes (restart poisoning)', async () => {
const client = makeClient()
diff --git a/apps/shared/src/json-rpc-gateway.ts b/apps/shared/src/json-rpc-gateway.ts
index 2c546e8f7a0..365e6d0feb9 100644
--- a/apps/shared/src/json-rpc-gateway.ts
+++ b/apps/shared/src/json-rpc-gateway.ts
@@ -114,6 +114,8 @@ export class JsonRpcGatewayClient {
private lastSeenSeq = new Map<string, number>()
/** Set while a post-reconnect replay fetch is in flight (dedup guard). */
private replayInFlight = false
+ /** Invalidates an interrupted replay so its async cleanup cannot own a replacement socket. */
+ private replayGeneration = 0
/**
* While a replay fetch is in flight, live seq'd frames for the sessions
* being replayed are parked here instead of dispatching immediately.
@@ -449,6 +451,7 @@ export class JsonRpcGatewayClient {
}
this.replayInFlight = true
+ const replayGeneration = ++this.replayGeneration
// Park live frames for the sessions we're about to replay so a frame
// racing the replay response can't dispatch ahead of (or duplicate) the
// gap events. Sessions without watermarks are unaffected.
@@ -475,6 +478,13 @@ export class JsonRpcGatewayClient {
)
)
+ // The socket that owned this replay was dropped while its requests were
+ // settling. Its results and cleanup must not consume the replacement
+ // socket's replay window.
+ if (this.replayGeneration !== replayGeneration) {
+ return
+ }
+
for (const result of results) {
if (result.status !== 'fulfilled' || !Array.isArray(result.value?.events)) {
continue
@@ -506,8 +516,10 @@ export class JsonRpcGatewayClient {
} catch {
// Replay is an optimization over lossy-reconnect; never surface errors.
} finally {
- this.flushReplayHold()
- this.replayInFlight = false
+ if (this.replayGeneration === replayGeneration) {
+ this.flushReplayHold()
+ this.replayInFlight = false
+ }
}
}
@@ -567,6 +579,12 @@ export class JsonRpcGatewayClient {
/** Forget the current socket generation, fail its calls, and go 'closed'. */
private dropSocket(error: Error): void {
+ // A replay belongs to the socket that started it. Detaching that socket
+ // rejects its requests asynchronously, so clear its ownership now; the
+ // next open can immediately schedule a replay of its own.
+ this.replayGeneration += 1
+ this.replayInFlight = false
+ this.replayHold = null
this.socket = null
this.channel.detach(error)
this.setState('closed')