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) <noreply@anthropic.com>

* 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) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brennan Benson
2026-11-16 10:24:19 -08:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 83714e8863
commit 4ecea0a693
3 changed files with 27 additions and 0 deletions
@@ -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
@@ -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<SubscriberFrameCapture<unknown> | 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)
@@ -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')
})