From 4ecea0a693c2deb543ef36facdf7aea012d28c56 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:24:19 -0800 Subject: [PATCH] fix(native-chat): resend a subscriber's snapshot when its capture was abandoned (#43020) * fix(agent-session): resend a chat's first snapshot when its capture is abandoned A subscriber opened without a cursor gets a snapshot first. When live authority moved during that read (a hand-off finishing, a start going live), the capture was abandoned and the following delivery sent batches from sequence 0 instead, so the client never received the conversation and stayed empty. The subscriber now owes that snapshot until one is emitted, and the next delivery sends it. Co-Authored-By: Claude Opus 5.5 (1M context) * test(native-chat): pin the owed snapshot after an abandoned capture The opening snapshot's authority changes mid-read, so its capture is abandoned; the next delivery must send that snapshot before any other frame. Without the owed-snapshot check the client's first frame is a reset. Co-Authored-By: Claude Opus 5.5 (1M context) --------- Co-authored-by: Claude Opus 5.5 (1M context) --- .../agent-session-subscribe-input.ts | 2 ++ .../agent-session-subscriber-catch-up.ts | 6 ++++++ .../agent-session-subscriber-catch-up.test.ts | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscribe-input.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscribe-input.ts index 50f3c6b6764..adfc559267b 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscribe-input.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscribe-input.ts @@ -43,6 +43,8 @@ export type Subscriber = { readHistorySnapshot?: HistoryReader['snapshot'] readQueueSourceIds?: HistoryReader['queueSourceIds'] snapshotCatchUp?: boolean + /** The fence of a snapshot whose capture was abandoned; the next delivery sends it, not a batch. */ + owedSnapshotFence?: number /** The current-work view the last frame's fields were read under * (`StructuredAgentSessionCurrentWork.viewKey`); a change with no row still owes a frame. */ workView?: string diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-catch-up.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-catch-up.ts index 900e38cfaba..4a10986fcb2 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-catch-up.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-catch-up.ts @@ -101,6 +101,9 @@ async function deliver( hostNow, ...(publishedActivity !== undefined ? { activity: publishedActivity } : {}) } + if (subscriber.owedSnapshotFence !== undefined) { + return publishSnapshotFrame(port, subscriber, journal, subscriber.owedSnapshotFence, shared) + } const reader = subscriberHistoryReader(subscriber) const readPage = createAgentSessionCatchUpReader(journal, reader) while (true) { @@ -269,6 +272,8 @@ async function publishSnapshotFrame( fence: number, shared: { hostNow: number; activity?: AgentSessionTurnActivity | null } ): Promise | undefined> { + // Owed until emitted: batches onto a view the client never received would leave it empty. + subscriber.owedSnapshotFence = fence const reader = subscriberHistoryReader(subscriber) const frame = await captureSubscriberFrame(port, subscriber, journal, (work) => readAgentSessionHydrationFrame(journal, fence, reader, work, true) @@ -291,6 +296,7 @@ async function publishSnapshotFrame( } subscriber.cursor = page.liveCursor ?? page.window.nextCursor subscriber.fence = fence + subscriber.owedSnapshotFence = undefined // The replacement dropped client restatements; owe them until every follow-up lands. subscriber.workView = undefined await refreshPublishedWork(delivery, subscriber, journal, frame.work, shared) diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-catch-up.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-catch-up.test.ts index dde06a5788a..dfe1844b8b3 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-catch-up.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-catch-up.test.ts @@ -481,3 +481,22 @@ it('a send restated off a snapshot page survives a follow-up abandoned by a newe expect(chat.sendState('send')).toBe('pending') }) + +it('a first snapshot abandoned mid-read is still sent before any other frame', async () => { + const held = new Set(['send']) + const chat = await openChat('owed-snapshot', held) + await chat.submit() + // The opening snapshot's authority changes during its read, so that capture is abandoned. + chat.historyReads.mockImplementationOnce(async (...args) => { + const read = chat.journal.history(...args) + held.clear() + return read + }) + chat.open() + await chat.laneDrained() + chat.subscribers.publish(SESSION, chat.journal) + await vi.waitFor(() => expect(chat.events.length).toBeGreaterThan(0)) + + expect(chat.events[0]).toMatchObject({ type: 'snapshot', sessionId: SESSION, fence: 1 }) + expect(chat.sendState('send')).toBe('unknown') +})