Skip to content

The agent that grows with you

fix(desktop): let history reads proceed when the replay epoch changes

teknium1bb7788c62660
diff --git a/apps/shared/src/json-rpc-gateway-replay.test.ts b/apps/shared/src/json-rpc-gateway-replay.test.ts
index c45e8e1e231..89316692ec2 100644
--- a/apps/shared/src/json-rpc-gateway-replay.test.ts
+++ b/apps/shared/src/json-rpc-gateway-replay.test.ts
@@ -368,6 +368,65 @@ describe('JsonRpcGatewayClient event-seq tracking + replay resume', () => {
client.close()
})
+ // A backend restart announces a new epoch over the still-open socket. No
+ // replay can cover the old numbering, so waiting history reads must proceed
+ // (the reconnect backstop read, #94779) instead of being silently dropped.
+ it.each(['ready', 'response'] as const)(
+ 'lets waiting history reads proceed when %s announces a new epoch, after parked frames',
+ async via => {
+ const client = makeClient()
+ const seen: number[] = []
+ client.on('message.delta', event => seen.push(event.seq!))
+
+ try {
+ const first = client.connect('ws://x')
+ sockets[0].open()
+ await first
+ sockets[0].serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'gateway.ready', payload: { replay_epoch: 'A' } } })
+
+ for (const sid of ['s1', 's2']) {
+ sockets[0].serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: sid, seq: 97 } })
+ }
+
+ client.invalidate()
+ const second = client.connect('ws://x')
+ sockets[1].open()
+ await second
+ const barriers = ['s1', 's2'].map(sid => client.sessionReplayBarrier(sid))
+ const requests = sockets[1].sent.map(text => JSON.parse(text))
+ // A fresh live frame parked behind the replay must survive the revoke.
+ sockets[1].serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: 's1', seq: 1 } })
+ expect(seen).toEqual([97, 97])
+
+ const seenAtResolve = Promise.all(barriers).then(valid => ({ valid, seen: [...seen] }))
+
+ if (via === 'ready') {
+ sockets[1].serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'gateway.ready', payload: { replay_epoch: 'B' } } })
+ } else {
+ sockets[1].serverFrame({ jsonrpc: '2.0', id: requests[0].id, result: { events: [], epoch: 'B' } })
+ }
+
+ await expect(seenAtResolve).resolves.toEqual({ valid: [true, true], seen: [97, 97, 1] })
+ expect(client.sessionReplayBarrier('s1')).toBeUndefined()
+ expect(client.getSeqWatermarks()).toEqual({ s1: 1 })
+
+ // A late old-epoch response cannot restore the revoked window.
+ for (const request of requests) {
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ id: request.id,
+ result: { events: [{ type: 'message.delta', session_id: request.params.session_id, seq: 98 }], epoch: 'A' }
+ })
+ }
+
+ await Promise.resolve()
+ expect(seen).toEqual([97, 97, 1])
+ } finally {
+ 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 730d5eec9a1..c355f12c52b 100644
--- a/apps/shared/src/json-rpc-gateway.ts
+++ b/apps/shared/src/json-rpc-gateway.ts
@@ -464,8 +464,10 @@ export class JsonRpcGatewayClient {
/**
* Wait for this session's reconnect replay AND parked live frames to dispatch.
- * True includes bounded timeout/unsupported-method fallback; false means the
- * socket/epoch owner was lost and a pending history read must be abandoned.
+ * True includes bounded timeout/unsupported-method fallback and an epoch
+ * change on the still-open socket (backend restart: nothing will replay, so
+ * REST is authoritative); false means the socket was lost and a pending
+ * history read must be abandoned (the next open re-reads).
* Unobserved sessions and replay-disabled feeds have no barrier.
*/
sessionReplayBarrier(sessionId: string): Promise<boolean> | undefined {
@@ -613,8 +615,11 @@ export class JsonRpcGatewayClient {
if (changed) {
this.lastSeenSeq.clear()
// Revoke requests/cursors from the old numbering, but retain live
- // frames already received on this still-open socket.
- const hold = this.cancelReplay()
+ // frames already received on this still-open socket. The socket is
+ // still ours and no replay can cover the old numbering, so waiting
+ // history reads proceed: REST is the only recovery left (#94779).
+ // Their continuations run after the parked frames below dispatch.
+ const hold = this.cancelReplay(true)
const generation = this.replayGeneration
for (const replay of hold?.values() ?? []) {
@@ -657,13 +662,13 @@ export class JsonRpcGatewayClient {
replay.resolve(true)
}
- private cancelReplay(): Map<string, SessionReplay> | null {
+ private cancelReplay(readsMayProceed: boolean): Map<string, SessionReplay> | null {
const hold = this.replayHold
this.replayGeneration += 1
this.replayHold = null
for (const replay of hold?.values() ?? []) {
- replay.resolve(false)
+ replay.resolve(readsMayProceed)
}
return hold
@@ -674,7 +679,7 @@ export class JsonRpcGatewayClient {
// 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.cancelReplay()
+ this.cancelReplay(false)
this.socket = null
this.channel.detach(error)
this.setState('closed')