Skip to content

The agent that grows with you

fix(shared): forget a session's replay epoch with the cursor it numbers

Tekniuma4593d8d6d4f
diff --git a/apps/shared/src/json-rpc-gateway-replay.test.ts b/apps/shared/src/json-rpc-gateway-replay.test.ts
index 345ea3b15cc..0740cb2cdbf 100644
--- a/apps/shared/src/json-rpc-gateway-replay.test.ts
+++ b/apps/shared/src/json-rpc-gateway-replay.test.ts
@@ -797,3 +797,39 @@ describe('JsonRpcGatewayClient event-seq tracking + replay resume', () => {
client.close()
})
})
+
+describe('JsonRpcGatewayClient per-session replay epochs', () => {
+ beforeEach(() => {
+ FakeWebSocket.instances = []
+ sockets = FakeWebSocket.instances as unknown as FakeWebSocket[]
+ })
+
+ it('forgets a session epoch together with the cursor it numbers', async () => {
+ const client = makeClient()
+ const first = client.connect('ws://x')
+ let sock = sockets[sockets.length - 1]
+ sock.open()
+ await first
+
+ for (let index = 0; index < 50; index += 1) {
+ sock.serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: `s${index}`, seq: 1, replay_epoch: `e${index}` } })
+ }
+
+ // Every replay window is gone (restarted authority): each cursor is dropped for a snapshot.
+ client.invalidate('drop')
+ const second = client.connect('ws://x')
+ sock = sockets[sockets.length - 1]
+ sock.open()
+ await second
+ await vi.waitFor(() => expect(sock.sent.length).toBe(50))
+
+ for (const raw of sock.sent) {
+ const req = JSON.parse(raw) as ReturnType<FakeWebSocket['lastRequest']>
+ sock.serverFrame({ jsonrpc: '2.0', id: req.id, result: { events: [], latest_seq: 9, epoch: 'new', replay_epoch: 'new', truncated: true, snapshot_required: true } })
+ }
+
+ await vi.waitFor(() => expect(client.getSeqWatermarks()).toEqual({}))
+ expect((client as unknown as { replayEpochBySession: Map<string, string> }).replayEpochBySession.size).toBe(0)
+ client.close()
+ })
+})
diff --git a/apps/shared/src/json-rpc-gateway.ts b/apps/shared/src/json-rpc-gateway.ts
index d0b1bd0e9cf..be95b34a334 100644
--- a/apps/shared/src/json-rpc-gateway.ts
+++ b/apps/shared/src/json-rpc-gateway.ts
@@ -618,8 +618,11 @@ export class JsonRpcGatewayClient {
// next reconnect believe nothing was missed; drop it and tell the
// consumer to re-resume for a snapshot. A legacy backend reports a
// bare `truncated` instead; that skips the window below (#100122).
- this.adoptSessionReplayEpoch(sid, sessionEpoch)
+ // The session's epoch goes with its cursor: it only ever qualifies that cursor, and the
+ // re-resumed session's next live frame re-establishes both. Keeping it leaked one entry per
+ // session ever observed on this client.
this.lastSeenSeq.delete(sid)
+ this.replayEpochBySession.delete(sid)
this.dispatchEvent({ type: 'session.replay_gap', session_id: sid, payload: { replay_epoch: sessionEpoch, latest_seq: result.latest_seq } })
return true
@@ -695,6 +698,7 @@ export class JsonRpcGatewayClient {
for (const sid of this.lastSeenSeq.keys()) {
if (!this.replayEpochBySession.has(sid)) { this.lastSeenSeq.delete(sid) }
}
+
// Revoke requests/cursors from the old numbering, but retain live
// frames already received on this still-open socket. The socket is
// still ours and no replay can cover the old numbering, so waiting