Skip to content

The agent that grows with you

test: restore replay-barrier fallback and background-sync coverage

teknium1716db852fe31
diff --git a/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx b/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx
index db3e3c56d40..9857fbc9b77 100644
--- a/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx
+++ b/apps/desktop/src/app/session/hooks/warm-resume-replay-barrier.test.tsx
@@ -4,6 +4,7 @@ import { act, cleanup, renderHook } from '@testing-library/react'
import { useRef } from 'react'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+import { reconcileActiveTranscript } from '@/app/contrib/hooks/use-background-sync'
import { getLatestSessionMessages } from '@/hermes'
import { chatMessageText, toChatMessages } from '@/lib/chat-messages'
import { resetInFlightTurnJournalStateForTests } from '@/lib/inflight-turn-journal'
@@ -361,3 +362,39 @@ it('does not paint a cold re-resume read ahead of a replay redialed while REST w
client.close()
}
})
+
+it('holds a background active-transcript refresh behind the reconnect replay', async () => {
+ const { result, client, second, request } = await mountWithPendingReplay()
+
+ const { activeSessionIdRef, selectedStoredSessionIdRef, sessionStateByRuntimeIdRef, updateSessionState } =
+ result.current.cache
+
+ selectedStoredSessionIdRef.current = storedId
+ activeSessionIdRef.current = runtimeId
+
+ try {
+ const refresh = reconcileActiveTranscript({
+ activeSessionIdRef,
+ busyRef: { current: false },
+ requestSequenceRef: { current: 0 },
+ resolveSession: () => ({ profile: 'default' }),
+ selectedStoredSessionIdRef,
+ signatureRef: { current: new Map() },
+ updateSessionState
+ })
+
+ await new Promise(resolve => setTimeout(resolve, 0))
+ expect(getLatestSessionMessages).not.toHaveBeenCalled()
+
+ await act(async () => {
+ second.frame({ id: request.id, jsonrpc: '2.0', result: { events: replay } })
+ await refresh
+ })
+
+ expect(getLatestSessionMessages).toHaveBeenCalledTimes(1)
+ const text = sessionStateByRuntimeIdRef.current.get(runtimeId)!.messages.map(chatMessageText).join('\n')
+ expect(text.match(/Finished result\./g)).toHaveLength(1)
+ } finally {
+ client.close()
+ }
+})
diff --git a/apps/shared/src/json-rpc-gateway-replay.test.ts b/apps/shared/src/json-rpc-gateway-replay.test.ts
index 89316692ec2..884bf37b269 100644
--- a/apps/shared/src/json-rpc-gateway-replay.test.ts
+++ b/apps/shared/src/json-rpc-gateway-replay.test.ts
@@ -368,6 +368,124 @@ describe('JsonRpcGatewayClient event-seq tracking + replay resume', () => {
client.close()
})
+ it.each(['timeout', 'unsupported'] as const)(
+ 'settles sessions independently and releases fresh frames on replay %s',
+ async fallback => {
+ vi.useFakeTimers()
+ const client = makeClient()
+ const seen: string[] = []
+ client.onEvent(event => seen.push(`${event.session_id}:${event.type}:${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: 'stable' } }
+ })
+
+ for (const sid of ['slow', 'fast']) {
+ sockets[0].serverFrame({
+ jsonrpc: '2.0',
+ method: 'event',
+ params: { type: 'session.info', session_id: sid, seq: 1 }
+ })
+ }
+
+ seen.length = 0
+ client.invalidate()
+ const second = client.connect('ws://x')
+ sockets[1].open()
+ await second
+ const slow = client.sessionReplayBarrier('slow')!
+ const fast = client.sessionReplayBarrier('fast')!
+ expect(slow).toBeInstanceOf(Promise)
+ expect(fast).toBeInstanceOf(Promise)
+ const requests = sockets[1].sent.map(text => JSON.parse(text))
+ const slowRequest = requests.find(request => request.params.session_id === 'slow')
+ const fastRequest = requests.find(request => request.params.session_id === 'fast')
+ const slowSettled = vi.fn()
+ void slow.then(slowSettled)
+
+ // Both ordinary parked starts and wire-marked replayed starts are fresh.
+ for (const [sid, seq] of [
+ ['slow', 2],
+ ['fast', 3]
+ ] as const) {
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ method: 'event',
+ params: { type: 'message.start', session_id: sid, seq, replayed: sid === 'fast' }
+ })
+ }
+
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ method: 'event',
+ params: { type: 'message.start', session_id: 'new', seq: 1 }
+ })
+ expect(seen).toEqual(['new:message.start:1'])
+
+ const fastSettled = fast.then(valid => {
+ expect(valid).toBe(true)
+ expect(seen).toEqual(['new:message.start:1', 'fast:message.complete:2', 'fast:message.start:3'])
+ })
+
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ id: fastRequest.id,
+ result: {
+ epoch: 'stable',
+ events: [{ type: 'message.complete', session_id: 'fast', seq: 2 }]
+ }
+ })
+ await fastSettled
+ expect(client.sessionReplayBarrier('fast')).toBeUndefined()
+ expect(client.sessionReplayBarrier('slow')).toBe(slow)
+ expect(slowSettled).not.toHaveBeenCalled()
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ method: 'event',
+ params: { type: 'message.delta', session_id: 'fast', seq: 4 }
+ })
+ expect(seen.at(-1)).toBe('fast:message.delta:4')
+
+ if (fallback === 'timeout') {
+ // The replay deadline is bounded independently of the 120s RPC default.
+ await vi.advanceTimersByTimeAsync(9_999)
+ expect(slowSettled).not.toHaveBeenCalled()
+ await vi.advanceTimersByTimeAsync(1)
+ } else {
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ id: slowRequest.id,
+ error: { code: -32601, message: 'method not found' }
+ })
+ }
+
+ await expect(slow).resolves.toBe(true)
+ expect(seen.at(-1)).toBe('slow:message.start:2')
+ expect(client.sessionReplayBarrier('slow')).toBeUndefined()
+ // Late replies after fallback cannot resurrect the old replay window.
+ sockets[1].serverFrame({
+ jsonrpc: '2.0',
+ id: slowRequest.id,
+ result: {
+ events: [{ type: 'message.delta', session_id: 'slow', seq: 99 }]
+ }
+ })
+ await Promise.resolve()
+ expect(client.getSeqWatermarks()).toEqual({ slow: 2, fast: 4, new: 1 })
+ } finally {
+ client.close()
+ vi.useRealTimers()
+ }
+ }
+ )
+
// 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.