Skip to content

The agent that grows with you

fix(shared): a replay the server cannot honour drops the watermark and signals a gap

teknium1329a7906170e
diff --git a/apps/shared/src/json-rpc-gateway-replay.test.ts b/apps/shared/src/json-rpc-gateway-replay.test.ts
index 1211a3911b4..eeeebb2a822 100644
--- a/apps/shared/src/json-rpc-gateway-replay.test.ts
+++ b/apps/shared/src/json-rpc-gateway-replay.test.ts
@@ -448,4 +448,41 @@ describe('JsonRpcGatewayClient event-seq tracking + replay resume', () => {
expect(client.getSeqWatermarks()).toEqual({ s1: 3 })
client.close()
})
+
+ it('drops the watermark and signals a replay gap when the server cannot replay the window', async () => {
+ const client = makeClient()
+ const gaps: string[] = []
+ client.on('session.replay_gap', event => { gaps.push(String(event.session_id)) })
+ const first = client.connect('ws://x')
+ let sock = sockets[sockets.length - 1]
+ sock.open()
+ await first
+
+ sock.serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: 'gone', seq: 7, replay_epoch: 'e1' } })
+ sock.serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: 'fine', seq: 3, replay_epoch: 'f1' } })
+ expect(client.getSeqWatermarks()).toEqual({ gone: 7, fine: 3 })
+
+ client.invalidate('drop')
+ const second = client.connect('ws://x')
+ sock = sockets[sockets.length - 1]
+ sock.open()
+ await second
+
+ const requests = sock.sent.map(raw => JSON.parse(raw) as ReturnType<FakeWebSocket['lastRequest']>)
+ expect(requests.map(req => req.method)).toEqual(['session.events.since', 'session.events.since'])
+
+ for (const req of requests) {
+ const result = req.params.session_id === 'gone'
+ ? { events: [], latest_seq: 40, epoch: 'e2', replay_epoch: 'e2', truncated: true, snapshot_required: true, count: 0 }
+ : { events: [{ type: 'message.delta', session_id: 'fine', seq: 4 }], latest_seq: 4, epoch: 'f1', replay_epoch: 'f1', truncated: false, snapshot_required: false, count: 1 }
+
+ sock.serverFrame({ jsonrpc: '2.0', id: req.id, result })
+ }
+
+ await vi.waitFor(() => {
+ expect(client.getSeqWatermarks()).toEqual({ fine: 4 })
+ })
+ expect(gaps).toEqual(['gone'])
+ client.close()
+ })
})
diff --git a/apps/shared/src/json-rpc-gateway.ts b/apps/shared/src/json-rpc-gateway.ts
index ef002a29b1f..6eed34c81a8 100644
--- a/apps/shared/src/json-rpc-gateway.ts
+++ b/apps/shared/src/json-rpc-gateway.ts
@@ -22,6 +22,10 @@ export type GatewayEventName =
| 'background.complete'
| 'error'
| 'skin.changed'
+ /** Synthetic, client-side: a reconnect replay could not cover the gap for
+ * `session_id` (epoch changed / ring truncated); the watermark was dropped
+ * and consumers must re-resume the session for a snapshot. */
+ | 'session.replay_gap'
| (string & {})
export interface GatewayEvent<P = unknown> {
@@ -570,14 +574,27 @@ export class JsonRpcGatewayClient {
continue
}
- const response = result.value as { epoch?: unknown; replay_epoch?: unknown }
+ const response = result.value as { epoch?: unknown; replay_epoch?: unknown; truncated?: unknown; snapshot_required?: unknown; latest_seq?: unknown }
const sessionEpoch = response.replay_epoch
const epoch = response.epoch
+ const sid = entries[index][0]
+
+ if (response.snapshot_required === true || response.truncated === true) {
+ // The server could not replay our window (epoch changed, ring
+ // truncated, cursor ahead). Keeping the watermark would make the
+ // next reconnect believe nothing was missed; drop it and tell the
+ // consumer to re-resume for a snapshot.
+ this.adoptSessionReplayEpoch(sid, sessionEpoch)
+ this.lastSeenSeq.delete(sid)
+ this.dispatchEvent({ type: 'session.replay_gap', session_id: sid, payload: { replay_epoch: sessionEpoch, latest_seq: response.latest_seq } })
+
+ continue
+ }
if (typeof sessionEpoch === 'string' && sessionEpoch) {
// Canonical responses also include `epoch`, but it is session-local,
// not the legacy process identity. Never reset unrelated cursors.
- if (this.adoptSessionReplayEpoch(entries[index][0], sessionEpoch)) { continue }
+ if (this.adoptSessionReplayEpoch(sid, sessionEpoch)) { continue }
} else if (typeof epoch === 'string' && epoch && this.replayEpoch && epoch !== this.replayEpoch) {
// Backend restarted: its seq numbering reset, so our watermarks —
// and this replay window — are meaningless. Drop them and start