diff --git a/frontend/src/lib/partsBatcher.test.ts b/frontend/src/lib/partsBatcher.test.ts index 3f355c1c..f34c4cf4 100644 --- a/frontend/src/lib/partsBatcher.test.ts +++ b/frontend/src/lib/partsBatcher.test.ts @@ -28,7 +28,7 @@ function textPart(sessionID: string, messageID: string, partID: string, text: st } describe('createPartsBatcher', () => { - it('keeps part deltas queued until message cache exists', () => { + it('invalidates when part deltas arrive before message cache exists and applies a later authoritative upsert', () => { const queryClient = new QueryClient() const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries') const batcher = createPartsBatcher(queryClient, 'http://localhost:5551') @@ -88,6 +88,36 @@ describe('createPartsBatcher', () => { expect(data2![0].parts[0]).toHaveProperty('text', 'authoritative text') }) + it('does not replay unapplied deltas onto refetched authoritative data', () => { + const queryClient = new QueryClient() + const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries') + const batcher = createPartsBatcher(queryClient, 'http://localhost:5551') + + queryClient.setQueryData( + ['opencode', 'messages', 'http://localhost:5551', 'session-1', '/repo'], + [assistantMessage('session-1', 'message-1')], + ) + + batcher.queuePartDelta('session-1', 'message-1', 'part-1', 'text', ' stale', '/repo') + batcher.flush() + + expect(invalidateSpy).toHaveBeenCalledWith({ + queryKey: ['opencode', 'messages', 'http://localhost:5551', 'session-1', '/repo'], + }) + + queryClient.setQueryData( + ['opencode', 'messages', 'http://localhost:5551', 'session-1', '/repo'], + [{ ...assistantMessage('session-1', 'message-1'), parts: [textPart('session-1', 'message-1', 'part-1', 'fresh')] }], + ) + + batcher.flush() + + const data = queryClient.getQueryData([ + 'opencode', 'messages', 'http://localhost:5551', 'session-1', '/repo', + ]) + expect(data![0].parts[0]).toHaveProperty('text', 'fresh') + }) + it('applies deltas queued after an authoritative upsert in the same batch', () => { const queryClient = new QueryClient() const batcher = createPartsBatcher(queryClient, 'http://localhost:5551') diff --git a/frontend/src/lib/partsBatcher.ts b/frontend/src/lib/partsBatcher.ts index d50a9285..dfe1967f 100644 --- a/frontend/src/lib/partsBatcher.ts +++ b/frontend/src/lib/partsBatcher.ts @@ -64,6 +64,7 @@ export function createPartsBatcher( if (!currentData) { invalidateOnce(queryKey) + groupsToDelete.push(key) continue } @@ -141,11 +142,10 @@ export function createPartsBatcher( }) if (filteredUnapplied.length > 0) { - group.operations = filteredUnapplied invalidateOnce(queryKey) - } else { - groupsToDelete.push(key) } + + groupsToDelete.push(key) } for (const key of groupsToDelete) {