fix(native-chat): check an opening's or snapshot's own live text against the stream's share

Ordered work recorded the live text it sends without checking it, so an opening could hold a
snapshot past the share. It now counts that text plus any waiting snapshot, and past the share
ends the stream and sends nothing.
This commit is contained in:
Brennan Benson
2026-11-18 10:36:30 -08:00
parent da0c2b7449
commit 23b3b2e6bc
3 changed files with 36 additions and 6 deletions
@@ -136,11 +136,18 @@ export class SubscriberDeliveryLane {
this.add({ kind: 'live-text', since, liveText, release })
}
/** Ordered work (an opening or snapshot) that sends live text itself says what it holds. */
sendsLiveText(items: object): void {
/** Ordered work (an opening or snapshot) sending live text itself; false past the stream's share. */
sendsLiveText(items: object): boolean {
const waiting = this.pending.find((entry) => entry.kind === 'live-text')
const queued = waiting?.kind === 'live-text' ? heldBytesOf(waiting.liveText.items) : 0
if (heldBytesOf(items) + queued > CHAT_LIVE_TEXT_SUBSCRIBER_BYTES) {
this.refuse()
return false
}
if (this.active?.kind === 'ordered') {
this.delivering = items
}
return true
}
private refuse(): void {
@@ -250,8 +250,7 @@ export class AgentSessionSubscribers {
/** Live text an opening or snapshot sends itself counts against its subscriber's share. */
private sentLiveText(subscriber: Subscriber, sessionId: string) {
const items = this.hooks.readLiveText?.(sessionId) ?? []
this.lanes.of(subscriber).sendsLiveText(items)
return items
return this.lanes.of(subscriber).sendsLiveText(items) ? items : []
}
private subscribers = (sessionId: string): Subscriber[] => [
@@ -26,7 +26,10 @@ async function collect(): Promise<void> {
}
}
async function blockedSubscriber(sessionId = SESSION) {
async function blockedSubscriber(
sessionId = SESSION,
readLiveText?: () => readonly AgentJournalRenderItem[]
) {
const journal = await journals.open({
identity: {
sessionId,
@@ -37,7 +40,7 @@ async function blockedSubscriber(sessionId = SESSION) {
},
stateDirectory: join(fixture.root, sessionId)
})
const subscribers = new AgentSessionSubscribers()
const subscribers = new AgentSessionSubscribers(readLiveText ? { readLiveText } : {})
const ends: unknown[] = []
const transport = Promise.withResolvers<void>()
let blocked = false
@@ -161,3 +164,24 @@ it('ends a stream whose one live text alone outgrows its share', async () => {
pane.release()
}
})
it('ends a stream whose opening would send live text past its share', async () => {
const text: AgentJournalRenderItem = {
itemId: 'orca:text',
revision: 1,
sequence: 0,
observedAt: 0,
turnScope: { kind: 'thread' },
body: {
kind: 'message',
role: 'assistant',
blocks: [{ type: 'text', text: 'x'.repeat(5_000_000) }]
}
}
const pane = await blockedSubscriber('opening-session', () => [text])
try {
await vi.waitFor(() => expect(pane.ends).toHaveLength(1))
} finally {
pane.release()
}
})