The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(shared): never dispatch a truncated reconnect replay tail
Austin Pickett666b9f04c422
diff --git a/apps/shared/src/json-rpc-gateway-replay.test.ts b/apps/shared/src/json-rpc-gateway-replay.test.tsindex 884bf37b269..11b01d38c12 100644--- a/apps/shared/src/json-rpc-gateway-replay.test.ts+++ b/apps/shared/src/json-rpc-gateway-replay.test.ts@@ -545,6 +545,61 @@ describe('JsonRpcGatewayClient event-seq tracking + replay resume', () => {})+ // The ring evicted part of the gap (#100122): the retained tail has a hole,+ // so dispatching it paints a response missing its prefix/tool state. The+ // client must skip the window and let waiting history reads resync instead.+ it('never dispatches a truncated replay tail; history reads proceed after fresh parked frames', async () => {+ 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: 'message.delta', session_id: 's1', seq: 3 } })++ client.invalidate('drop')+ const second = client.connect('ws://x')+ sockets[1].open()+ await second+ const barrier = client.sessionReplayBarrier('s1')!+ expect(barrier).toBeInstanceOf(Promise)+ await vi.waitFor(() => expect(sockets[1].lastRequest().method).toBe('session.events.since'))+ const req = sockets[1].lastRequest()++ // Live frames race the replay: 700 is inside the evicted window's+ // retained tail, 701 is newer than the server's head.+ for (const seq of [700, 701]) {+ sockets[1].serverFrame({ jsonrpc: '2.0', method: 'event', params: { type: 'message.delta', session_id: 's1', seq } })+ }++ const seenAtResolve = barrier.then(valid => ({ valid, seen: [...seen] }))++ sockets[1].serverFrame({+ jsonrpc: '2.0',+ id: req.id,+ result: {+ events: [+ { type: 'message.delta', session_id: 's1', seq: 699 },+ { type: 'message.delta', session_id: 's1', seq: 700 }+ ],+ latest_seq: 700,+ truncated: true,+ count: 2+ }+ })++ // No partial tail (699/700); the barrier resolves true so the reconnect+ // backstop's REST read is authoritative, and only the post-head live frame flows.+ await expect(seenAtResolve).resolves.toEqual({ valid: true, seen: [3, 701] })+ expect(client.sessionReplayBarrier('s1')).toBeUndefined()+ expect(client.getSeqWatermarks()).toEqual({ s1: 701 })+ } 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.tsindex 47de78caf8d..a628caf1868 100644--- a/apps/shared/src/json-rpc-gateway.ts+++ b/apps/shared/src/json-rpc-gateway.ts@@ -536,11 +536,12 @@ export class JsonRpcGatewayClient {try {// `open_requests` on the answer are re-delivered by the channel itself.- const result = await this.request<{ events?: GatewayEvent[]; epoch?: string }>(- 'session.events.since',- { session_id: sid, last_seen: lastSeen },- REPLAY_REQUEST_TIMEOUT_MS- )+ const result = await this.request<{+ epoch?: string+ events?: GatewayEvent[]+ latest_seq?: number+ truncated?: boolean+ }>('session.events.since', { session_id: sid, last_seen: lastSeen }, REPLAY_REQUEST_TIMEOUT_MS)// The socket that owned this replay was dropped while its requests were// settling. Its results and cleanup must not consume the replacement@@ -562,6 +563,19 @@ export class JsonRpcGatewayClient {this.replayEpoch = epoch}+ if (result?.truncated === true) {+ // The ring evicted events between our watermark and its oldest+ // retained seq: the tail has a hole (missing message prefix, tool+ // state, or terminal event), so never paint it as the gap. Skip the+ // whole window up to the server's head; the barrier then resolves+ // true, which lets the waiting history reads (the reconnect backstop,+ // #94779) resync from the authoritative transcript. Parked live+ // frames newer than that head still dispatch in the finally below.+ this.skipTruncatedWindow(sid, result)++ return+ }+if (!Array.isArray(result?.events)) {return}@@ -585,6 +599,18 @@ export class JsonRpcGatewayClient {}}+ /** Advance the watermark past a truncated replay window without dispatching any of it. */+ private skipTruncatedWindow(sid: string, result: { events?: GatewayEvent[]; latest_seq?: number }): void {+ const seqs = (Array.isArray(result.events) ? result.events : []).map(event => event?.seq)++ const head = Math.max(+ this.lastSeenSeq.get(sid) ?? 0,+ ...[result.latest_seq, ...seqs].filter((seq): seq is number => typeof seq === 'number' && Number.isFinite(seq))+ )++ this.lastSeenSeq.set(sid, head)+ }+/*** Dispatch an event only when its seq advances the session watermark.* Seq-less events always dispatch (no ordering contract to violate).