diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-delivery-lane.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-delivery-lane.ts index 42bdada2ff7..862bb8f20de 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-delivery-lane.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/agent-session-subscriber-delivery-lane.ts @@ -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 { diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/structured-agent-session-subscribers.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/structured-agent-session-subscribers.ts index 9ad0fd23b77..9f5037a38fa 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/structured-agent-session-subscribers.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/structured-agent-session-subscribers.ts @@ -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[] => [ diff --git a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-delivery-retention.test.ts b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-delivery-retention.test.ts index 5015c49ac61..41b17e0873d 100644 --- a/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-delivery-retention.test.ts +++ b/src/main/native-chat/agent-session-wire/publication/subscribers/testing/agent-session-subscriber-delivery-retention.test.ts @@ -26,7 +26,10 @@ async function collect(): Promise { } } -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() 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() + } +})