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') +})