Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🔁 feat: Reattach to a Running Generation and Keep a Dropped One Stoppable (#17584)
Marco Berettaaae1a6ec1643
diff --git a/packages/chat/src/core/__tests__/chat.spec.ts b/packages/chat/src/core/__tests__/chat.spec.tsindex 8ec339ab84..a26431dd15 100644--- a/packages/chat/src/core/__tests__/chat.spec.ts+++ b/packages/chat/src/core/__tests__/chat.spec.ts@@ -7,7 +7,7 @@ import type {ChatConnection,ChatAbortResult,} from '../types';-import type { ChatState, ChatInit } from '../chat';+import type { ChatState, ChatInit, ChatPreparedRequest } from '../chat';import { Chat } from '../chat';/** A stream the test feeds event by event; closing it ends the iteration. */@@ -209,6 +209,25 @@ describe('Chat', () => {expect(chat.connection).toBe('idle');});+ it('on the reconnecting state still attaches the reattachment', async () => {+ const resumed = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ const { chat, transport } = setup();+ transport.reconnectToStream.mockResolvedValueOnce(resumed);+ chat.subscribe(() => {+ if (chat.connection === 'reconnecting') {+ throw new Error('listener failed');+ }+ });++ const attached = chat.resumeStream({ streamId: 'convo-1' });+ await flush();++ expect(chat.connection).toBe('live');+ resumed.push(finalEvent());+ await attached;+ expect(chat.connection).toBe('idle');+ });+it('on dispose still releases the chat from its state', async () => {const { chat, state } = setup();const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });@@ -318,7 +337,7 @@ describe('Chat', () => {});it('releases a stop still waiting for the server, and the turn it stops', async () => {- const { chat, transport } = setup({});+ const { chat, transport } = setup({ resumeSelection: () => ({ endpoint: 'agents' }) });transport.abort.mockImplementationOnce(() => new Promise<never>(() => undefined));const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });await flush();@@ -466,6 +485,21 @@ describe('Chat', () => {expect(ended.onFinish).toHaveBeenCalledWith(expect.objectContaining({ isDisconnect: true }));});+ it('clears a dropped connection error once a reconnect finishes the turn', async () => {+ const stream = createStream({ streamId: 'convo-1' });+ const { chat, state } = setup({}, { reconnectToStream: jest.fn(async () => stream) });+ state.error = new Error('dropped');+ state.status = 'error';++ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ stream.push(finalEvent());+ await resumed;++ expect(chat.status).toBe('ready');+ expect(chat.error).toBeUndefined();+ });+it('reports a failed start as an error and frees the chat for the next turn', async () => {const failure = new Error('start failed');const { chat, transport } = setup(@@ -561,25 +595,31 @@ describe('Chat', () => {},);- it.each(['sendMessages'] as const)(+ it.each(['sendMessages', 'reconnectToStream'] as const)('stops while %s ignores the abort signal, closing the stream it opens later',async (opener) => {let open: (() => void) | undefined;const stream = createStream({ streamId: 'convo-1' });const pending = jest.fn(() =>- new Promise<Awaited<ReturnType<ChatTransport['sendMessages']>>>((resolve) => {- open = () => resolve({ status: 'stream', stream });+ new Promise((resolve) => {+ open = () => resolve(opener === 'sendMessages' ? { status: 'stream', stream } : stream);}),);- const { chat, onFinish, transport } = setup({}, { [opener]: pending });+ const { chat, onFinish, transport } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ { [opener]: pending },+ );- const started = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ const started =+ opener === 'sendMessages'+ ? chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] })+ : chat.resumeStream({ streamId: 'convo-1' });await flush();await chat.stop();await started;- /** A cancelled start may still run on the server. */- expect(chat.status).toBe('error');+ /** A cancelled start may still run on the server; a reattachment had nothing to start. */+ expect(chat.status).toBe(opener === 'sendMessages' ? 'error' : 'ready');open?.();await flush();@@ -589,7 +629,11 @@ describe('Chat', () => {expect.objectContaining({ endpoint: 'agents', streamId: 'convo-1' }),);expect(onFinish).toHaveBeenCalledTimes(1);- expect(onFinish).toHaveBeenCalledWith(expect.objectContaining({ isDisconnect: true }));+ expect(onFinish).toHaveBeenCalledWith(+ expect.objectContaining(+ opener === 'sendMessages' ? { isDisconnect: true } : { isAbort: true },+ ),+ );},);@@ -911,11 +955,19 @@ describe('Chat', () => {await sent;});- it('returns a live connection to idle on dispose', async () => {- const { chat } = setup();- void chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ it.each([+ ['live', 'attached'],+ ['reconnecting', 'reattaching'],+ ])('returns a %s connection to idle on dispose', async (connection, phase) => {+ const { chat, transport } = setup({ resumeSelection: () => ({ endpoint: 'agents' }) });+ if (phase === 'attached') {+ void chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ } else {+ transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ void chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ }await flush();- expect(chat.connection).toBe('live');+ expect(chat.connection).toBe(connection);chat.dispose();@@ -1060,6 +1112,33 @@ describe('Chat', () => {);});+ it('does not carry a finished generation timestamp into a fresh reattachment', async () => {+ const resumed = createStream({ streamId: 'convo-1' });+ const { chat, stream, transport } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ { reconnectToStream: jest.fn(async () => resumed) },+ );+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push(finalEvent());+ await sent;++ const attached = chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ resumed.push(stepEvent);+ await flush();+ transport.abort.mockImplementation(async () => {+ resumed.push(finalEvent({ terminalStatus: 'aborted' }));+ return { success: true };+ });+ await chat.stop();+ await attached;++ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ streamId: 'convo-1', generationCreatedAt: undefined }),+ );+ });+it('lets a completion callback start the next turn', async () => {let next: Promise<void> | undefined;const { chat, stream, transport } = setup({@@ -1230,34 +1309,44 @@ describe('Chat', () => {expect(onFinish).toHaveBeenCalledWith(expect.objectContaining({ isAbort: true }));});- it('settles an attached Assistants run a stop found already gone for the host to reconcile', async () => {- const stream = createStream();- const { chat, transport, onFinish } = setup(- {- prepareRequest: jest.fn(() => ({- payload: { endpoint: 'assistants', conversationId: 'convo-1', text: 'Hi' } as TPayload,- messages: [userMessage, placeholder],- responseMessageId: 'response-1',- })),- },- { sendMessages: jest.fn(async () => ({ status: 'stream' as const, stream })) },- );- transport.abort.mockResolvedValue(null);+ it.each([+ ['an attached', false],+ ['a dropped', true],+ ])(+ 'settles %s Assistants run a stop found already gone for the host to reconcile',+ async (_label, dropped) => {+ const stream = createStream();+ const { chat, transport, onFinish } = setup(+ {+ prepareRequest: jest.fn(() => ({+ payload: { endpoint: 'assistants', conversationId: 'convo-1', text: 'Hi' } as TPayload,+ messages: [userMessage, placeholder],+ responseMessageId: 'response-1',+ })),+ },+ { sendMessages: jest.fn(async () => ({ status: 'stream' as const, stream })) },+ );+ transport.abort.mockResolvedValue(null);- const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });- await flush();- await expect(chat.stop()).resolves.toEqual({ status: 'stopped' });- await sent;+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ if (dropped) {+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ }+ await expect(chat.stop()).resolves.toEqual({ status: 'stopped' });+ await sent;- expect(transport.abort).toHaveBeenCalledWith(- expect.objectContaining({ endpoint: 'assistants', abortKey: 'convo-1:response-1' }),- );- expect(stream.closed()).toBe(true);- expect(chat.status).toBe('ready');- expect(onFinish).toHaveBeenLastCalledWith(- expect.objectContaining({ isAbort: false, isSettled: true }),- );- });+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ endpoint: 'assistants', abortKey: 'convo-1:response-1' }),+ );+ expect(stream.closed()).toBe(true);+ expect(chat.status).toBe('ready');+ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isAbort: false, isSettled: true }),+ );+ },+ );it('returns the conversation an Assistants stop created for a first turn under `new`', async () => {const stream = createStream();@@ -1347,6 +1436,108 @@ describe('Chat', () => {);});+ it('merges the messages a stop returns for an Assistants run whose stream dropped', async () => {+ const stream = createStream();+ const { chat, transport } = setup(+ {+ prepareRequest: jest.fn(() => ({+ payload: { endpoint: 'assistants', conversationId: 'convo-1', text: 'Hi' } as TPayload,+ messages: [userMessage, placeholder],+ responseMessageId: 'response-1',+ })),+ },+ { sendMessages: jest.fn(async () => ({ status: 'stream' as const, stream })) },+ );+ transport.abort.mockResolvedValue({+ final: true,+ conversation: {},+ runMessages: [userMessage, { ...placeholder, text: 'Partial' }],+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ await chat.stop();++ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ abortKey: 'convo-1:response-1' }),+ );+ expect(chat.messages[1].text).toBe('Partial');+ expect(chat.status).toBe('ready');+ });++ it('keeps the reattachment target of a reconnect that failed on the network', async () => {+ const failure = Object.assign(new Error('Network Error'), { code: 'ERR_NETWORK' });+ const { chat, transport } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ { reconnectToStream: jest.fn(async () => Promise.reject(failure)) },+ );+ await chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ expect(chat.status).toBe('error');++ await chat.stop();++ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({+ endpoint: 'agents',+ streamId: 'convo-1',+ generationCreatedAt: 300,+ }),+ );+ });++ it('keeps a dropped generation stoppable when reattaching to it fails on the server', async () => {+ const { chat, stream, transport, onFinish } = setup({+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ transport.reconnectToStream.mockRejectedValueOnce(new Error('Service Unavailable'));++ await chat.resumeStream();+ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isError: true, isDisconnect: true }),+ );++ await expect(chat.stop()).resolves.toEqual({ status: 'stopped' });+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({+ endpoint: 'agents',+ streamId: 'convo-1',+ generationCreatedAt: 100,+ }),+ );+ });++ it.each([+ ['404', { response: { status: 404, data: null, headers: {} } }],+ ['410', { response: { status: 410, data: null, headers: {} } }],+ ['NO_ACTIVE_RUN', { code: 'NO_ACTIVE_RUN' }],+ ])(+ 'settles a reattachment the server answered %s as an error, not a disconnect',+ async (_label, gone) => {+ const { chat, stream, transport, onFinish } = setup({+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ transport.reconnectToStream.mockRejectedValueOnce(Object.assign(new Error('Gone'), gone));++ await chat.resumeStream();++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isError: true, isDisconnect: false }),+ );+ await expect(chat.stop()).resolves.toEqual({ status: 'idle' });+ expect(transport.abort).not.toHaveBeenCalled();+ },+ );+it('does not merge a delayed stop of a dropped run into a newer turn', async () => {const first = createStream();const second = createStream();@@ -1442,6 +1633,27 @@ describe('Chat', () => {expect(onError).toHaveBeenCalled();});+ it('reports reconnecting while a reattachment opens', async () => {+ let attach: ((stream: null) => void) | undefined;+ const { chat } = setup(+ {},+ {+ reconnectToStream: jest.fn(+ () =>+ new Promise<null>((resolve) => {+ attach = resolve;+ }),+ ),+ },+ );++ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ expect(chat.connection).toBe('reconnecting');+ attach?.(null);+ await resumed;+ expect(chat.connection).toBe('idle');+ });+it('settles a final frame that asks for reconciliation instead of completing it', async () => {const { chat, stream, onFinish } = setup();const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });@@ -1484,6 +1696,67 @@ describe('Chat', () => {expect(chat.status).toBe('ready');});+ it('reports a reattachment the stop found already finished as settled', async () => {+ const { chat, transport, onFinish } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ { reconnectToStream: jest.fn(() => new Promise<null>(() => undefined)) },+ );+ transport.abort.mockResolvedValue({+ success: false,+ settled: true,+ terminalStatus: 'complete',+ });++ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ await chat.stop();+ await resumed;++ expect(onFinish).toHaveBeenCalledWith(+ expect.objectContaining({ isAbort: false, isSettled: true }),+ );+ });++ it('settles a reattachment the stop cancelled as it opened by the stop answer', async () => {+ let open: ((stream: ChatStream) => void) | undefined;+ let answer: ((result: ChatAbortResult) => void) | undefined;+ const opened = createStream({ streamId: 'convo-1' });+ const { chat, onFinish } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ {+ reconnectToStream: jest.fn(+ () =>+ new Promise<ChatStream>((resolve) => {+ open = resolve;+ }),+ ),+ abort: jest.fn(+ () =>+ new Promise<ChatAbortResult>((resolve) => {+ answer = resolve;+ }),+ ),+ },+ );+ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ const stopped = chat.stop();+ await flush();++ /** The answer lands just before the stream opens, so the stop cancels the open. */+ answer?.({ success: false, settled: true, terminalStatus: 'complete' });+ await flush();+ open?.(opened);+ await stopped;+ await resumed;++ expect(opened.closed()).toBe(true);+ expect(onFinish).toHaveBeenCalledTimes(1);+ expect(onFinish).toHaveBeenCalledWith(+ expect.objectContaining({ isAbort: false, isSettled: true }),+ );+ });+it('accepts a stop the run had already settled', async () => {const { chat, stream, transport } = setup();transport.abort.mockImplementation(async () => {@@ -1581,6 +1854,28 @@ describe('Chat', () => {expect(onFinish).toHaveBeenCalledWith(expect.objectContaining({ message: undefined }));});+ it('stops a generation whose stream dropped, which the server kept running', async () => {+ const { chat, stream, transport } = setup();+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push(stepEvent);+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ expect(chat.status).toBe('error');++ await chat.stop();++ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({+ endpoint: 'agents',+ streamId: 'convo-1',+ generationCreatedAt: 100,+ }),+ );+ expect(chat.status).toBe('ready');+ expect(chat.error).toBeUndefined();+ });+it('continues a response with the continue trigger', async () => {const { chat, stream, transport } = setup();const sent = chat.continue({ messageId: 'response-1' });@@ -1616,15 +1911,83 @@ describe('Chat', () => {expect(regenerating.status).toBe('ready');});- describe('stop answers and reattachment targets', () => {- const steer = { steerId: 'steer-1', text: 'Shorter' };+ it('resumes without flashing submitted, and stays ready when nothing is running', async () => {+ const idle = setup();+ await idle.chat.resumeStream();+ expect(idle.state.statuses).toEqual([]);- it('hands back the steers a stop found uninjected, under the conversation it stopped', async () => {- const { chat, stream, transport } = setup();- transport.abort.mockImplementation(async () => {- stream.push(finalEvent({ terminalStatus: 'aborted' }));- return { success: true, aborted: 'stream-7', pendingSteers: [steer] };- });+ const stream = createStream({ generationCreatedAt: 300 });+ const running = setup({}, { reconnectToStream: jest.fn(async () => stream) });+ const resumed = running.chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ stream.push(stepEvent);+ stream.push(finalEvent());+ await resumed;++ expect(running.transport.reconnectToStream).toHaveBeenCalledWith(+ expect.objectContaining({ chatId: 'convo-1', streamId: 'convo-1', resume: true }),+ );+ expect(running.state.statuses).toEqual(['streaming', 'ready']);+ });++ it('stops a resumed stream on the server, at the endpoint the host selects', async () => {+ const stream = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ const { chat, transport } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ { reconnectToStream: jest.fn(async () => stream) },+ );+ transport.abort.mockImplementation(async () => {+ stream.push(finalEvent({ terminalStatus: 'aborted' }));+ return { success: true };+ });++ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ stream.push(stepEvent);+ await flush();+ await chat.stop();+ await resumed;++ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({+ endpoint: 'agents',+ streamId: 'convo-1',+ generationCreatedAt: 300,+ }),+ );+ expect(chat.status).toBe('ready');+ });++ it('refuses to stop a resumed stream whose endpoint the host does not select', async () => {+ const stream = createStream({ streamId: 'convo-1' });+ const { chat, transport, onError } = setup(+ {},+ { reconnectToStream: jest.fn(async () => stream) },+ );++ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ stream.push(stepEvent);+ await flush();++ await expect(chat.stop()).rejects.toThrow('without its endpoint');+ expect(transport.abort).not.toHaveBeenCalled();+ expect(stream.closed()).toBe(false);+ expect(chat.status).toBe('streaming');+ stream.push(finalEvent());+ await resumed;+ expect(onError).not.toHaveBeenCalled();+ });++ describe('stop answers and reattachment targets', () => {+ const steer = { steerId: 'steer-1', text: 'Shorter' };++ it('hands back the steers a stop found uninjected, under the conversation it stopped', async () => {+ const { chat, stream, transport } = setup();+ transport.abort.mockImplementation(async () => {+ stream.push(finalEvent({ terminalStatus: 'aborted' }));+ return { success: true, aborted: 'stream-7', pendingSteers: [steer] };+ });const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });await flush();@@ -1773,6 +2136,89 @@ describe('Chat', () => {);});+ it('reattaches a dropped first turn under the conversation its sync frame assigned', async () => {+ const { chat, stream, transport, onFinish } = setup({ id: 'new' });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({+ type: 'sync',+ data: { conversationId: 'convo-9', responseMessage: placeholder },+ } as ChatEvent);+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ transport.reconnectToStream.mockResolvedValueOnce(null);++ await chat.resumeStream({ streamId: 'convo-1' });++ expect(transport.reconnectToStream).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: 'convo-9' }),+ );+ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isSettled: true, conversationId: 'convo-9' }),+ );+ });++ it('stops a replacement from the reconnect notice and reattaches it under its conversation', async () => {+ const stream = createStream({ generationCreatedAt: 200 });+ const stops: Promise<unknown>[] = [];+ const { chat, transport, onFinish } = setup(+ { id: 'new' },+ {+ sendMessages: jest.fn(async () => ({+ status: 'replaced' as const,+ generationProtocolVersion: 2,+ conversationId: 'convo-9',+ streamId: 'stream-2',+ generationCreatedAt: 200,+ })),+ reconnectToStream: jest.fn(async () => stream),+ },+ );+ transport.abort.mockImplementation(async () => {+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ return { success: true };+ });+ const unsubscribe = chat.subscribe(() => {+ if (chat.connection === 'reconnecting' && stops.length === 0) {+ stops.push(chat.stop());+ }+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ unsubscribe();+ await expect(stops[0]).resolves.toEqual({ status: 'stopped' });+ await sent;+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ streamId: 'stream-2', generationCreatedAt: 200 }),+ );+ expect(onFinish).toHaveBeenCalledTimes(1);++ const dropped = setup(+ { id: 'new' },+ {+ sendMessages: jest.fn(async () => ({+ status: 'replaced' as const,+ generationProtocolVersion: 2,+ conversationId: 'convo-9',+ streamId: 'stream-2',+ generationCreatedAt: 200,+ })),+ },+ );+ const replacement = createStream({ generationCreatedAt: 200 });+ dropped.transport.reconnectToStream.mockResolvedValueOnce(replacement);+ const sentDropped = dropped.chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ replacement.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sentDropped;+ dropped.transport.reconnectToStream.mockResolvedValueOnce(null);+ await dropped.chat.resumeStream({ streamId: 'stream-2', generationCreatedAt: 200 });++ expect(dropped.transport.reconnectToStream).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: 'convo-9' }),+ );+ });+it('reports nothing to stop as idle', async () => {const { chat, transport } = setup();@@ -1780,6 +2226,232 @@ describe('Chat', () => {expect(transport.abort).not.toHaveBeenCalled();});+ it('keeps an unsaved stop attached, and reports it unsaved when nothing is left to attach to', async () => {+ let open: ((stream: ChatStream | null) => void) | undefined;+ const { chat, transport, onFinish } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ {+ reconnectToStream: jest.fn(+ () =>+ new Promise<ChatStream | null>((resolve) => {+ open = resolve;+ }),+ ),+ abort: jest.fn(async () => ({+ success: true,+ generationProtocolVersion: 2,+ aborted: 'convo-1',+ persistenceFailed: true,+ })),+ },+ );+ const resumed = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ await flush();++ const stopped = chat.stop();+ await flush();+ const [request] = transport.reconnectToStream.mock.calls[0];+ expect(request.abortSignal?.aborted).toBe(false);+ expect(onFinish).not.toHaveBeenCalled();++ open?.(null);+ await stopped;+ await resumed;+ expect(chat.status).toBe('ready');+ expect(onFinish).toHaveBeenCalledTimes(1);+ expect(onFinish).toHaveBeenCalledWith(+ expect.objectContaining({+ isAbort: true,+ isSettled: true,+ conversationId: 'convo-1',+ reconcileReason: 'abort_persistence_failed',+ }),+ );+ });++ it('settles a reconnect that finds nothing by the answer of a stop still in flight', async () => {+ let open: ((stream: ChatStream | null) => void) | undefined;+ let answer: ((result: ChatAbortResult) => void) | undefined;+ const { chat, onFinish } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ {+ reconnectToStream: jest.fn(+ () =>+ new Promise<ChatStream | null>((resolve) => {+ open = resolve;+ }),+ ),+ abort: jest.fn(+ () =>+ new Promise<ChatAbortResult>((resolve) => {+ answer = resolve;+ }),+ ),+ },+ );+ const resumed = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ await flush();++ const stopped = chat.stop();+ await flush();+ open?.(null);+ await flush();+ expect(onFinish).not.toHaveBeenCalled();++ answer?.({+ success: true,+ generationProtocolVersion: 2,+ aborted: 'convo-1',+ persistenceFailed: true,+ });+ await stopped;+ await resumed;+ expect(chat.status).toBe('ready');+ expect(onFinish).toHaveBeenCalledTimes(1);+ expect(onFinish).toHaveBeenCalledWith(+ expect.objectContaining({+ isAbort: true,+ isSettled: true,+ conversationId: 'convo-1',+ reconcileReason: 'abort_persistence_failed',+ }),+ );+ });++ it('names the conversation an unframed stop of a dropped first turn settled under', async () => {+ const { chat, stream, transport, onFinish } = setup({ id: 'new' });+ transport.abort.mockResolvedValue({+ success: true,+ generationProtocolVersion: 2,+ aborted: 'convo-9',+ persistenceFailed: true,+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ await chat.stop();++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({+ isSettled: true,+ conversationId: 'convo-9',+ reconcileReason: 'abort_persistence_failed',+ }),+ );+ });++ it('adopts the conversation a settled stop of a dropped first turn names by its stream', async () => {+ const { chat, stream, transport, onFinish } = setup({ id: 'new' });+ transport.abort.mockResolvedValue({+ success: false,+ settled: true,+ code: 'RUN_ALREADY_SETTLED',+ streamId: 'convo-9',+ generationProtocolVersion: 2,+ terminalStatus: 'complete',+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ await expect(chat.stop()).resolves.toMatchObject({+ status: 'stopped',+ conversationId: 'convo-9',+ });+ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isSettled: true, conversationId: 'convo-9' }),+ );++ void chat.sendMessage({ parts: [{ type: 'text', text: 'Next' }] });+ await flush();+ expect(transport.sendMessages).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: 'convo-9' }),+ );+ });++ it('stops a dropped turn it reattached to at the endpoint the turn started on', async () => {+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ const { chat, stream, transport } = setup();+ transport.abort.mockImplementation(async () => {+ reattached.push(finalEvent({ terminalStatus: 'aborted' }));+ return { success: true };+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ transport.reconnectToStream.mockResolvedValueOnce(reattached);+ const resumed = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ await flush();++ await chat.stop();+ await resumed;++ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ endpoint: 'agents', streamId: 'convo-1' }),+ );+ });++ it('reports the response of a dropped turn it reattached to, not the list tail', async () => {+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ const sibling: TMessage = { ...placeholder, messageId: 'sibling-1', text: 'Other branch' };+ const { chat, stream, transport, state, onFinish } = setup({+ prepareRequest: jest.fn(() => ({+ payload,+ messages: [userMessage, placeholder],+ responseMessageId: placeholder.messageId,+ })),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ state.replaceMessages([...chat.messages, sibling]);+ transport.reconnectToStream.mockResolvedValueOnce(reattached);+ const resumed = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ await flush();++ reattached.push({ type: 'abort', data: {} } as ChatEvent);+ await resumed;++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({+ isAbort: true,+ message: expect.objectContaining({ messageId: placeholder.messageId }),+ }),+ );+ });++ it('reports the response of a reattachment to another generation as unknown, not the dropped turn', async () => {+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 200 });+ const { chat, stream, transport, state, onFinish } = setup({+ prepareRequest: jest.fn(() => ({+ payload,+ messages: [userMessage, placeholder],+ responseMessageId: placeholder.messageId,+ })),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ const newer: TMessage = { ...placeholder, messageId: 'response-2', text: 'Newer' };+ state.replaceMessages([...chat.messages, newer]);+ transport.reconnectToStream.mockResolvedValueOnce(reattached);+ const resumed = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 200 });+ await flush();++ reattached.push({ type: 'abort', data: {} } as ChatEvent);+ await resumed;++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isAbort: true, isResponseUnknown: true, message: undefined }),+ );+ });+it('reports the response of a replacement that ended before any frame named it as unknown', async () => {const replacing = () => {const stream = createStream({ streamId: 'stream-2', generationCreatedAt: 200 });@@ -1833,30 +2505,196 @@ describe('Chat', () => {);});- it('sends the request options of a turn to the generation that replaced it', async () => {+ it('reports the response unknown when a reattachment without a timestamp finds another generation', async () => {+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 200 });+ const { chat, stream, transport, onFinish } = setup({+ prepareRequest: jest.fn(() => ({+ payload,+ messages: [userMessage, placeholder],+ responseMessageId: placeholder.messageId,+ })),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ transport.reconnectToStream.mockResolvedValueOnce(reattached);+ const resumed = chat.resumeStream({ streamId: 'convo-1' });+ await flush();++ reattached.push({ type: 'abort', data: {} } as ChatEvent);+ await resumed;++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isAbort: true, isResponseUnknown: true, message: undefined }),+ );+ });++ it('sends the request options of a turn to the generation that replaced it', async () => {+ const stream = createStream();+ const { chat, transport } = setup(+ {},+ {+ sendMessages: jest.fn(async () => ({+ status: 'replaced' as const,+ generationProtocolVersion: 2,+ conversationId: 'convo-1',+ streamId: 'stream-2',+ generationCreatedAt: 200,+ })),+ reconnectToStream: jest.fn(async () => stream),+ },+ );+ const options = { headers: { 'x-tenant': 'acme' }, body: { spec: 'a' } };+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] }, options);+ await flush();+ stream.push(finalEvent());+ await sent;++ expect(transport.reconnectToStream).toHaveBeenCalledWith(+ expect.objectContaining({ ...options, chatId: 'convo-1', streamId: 'stream-2' }),+ );+ });++ it.each([+ ['its preparation fails', 'prepare'],+ ['the server refuses its start', 'start'],+ ])('still stops a dropped generation after a retry where %s', async (_label, failure) => {+ const prepareRequest = jest.fn(() => ({ payload, messages: [userMessage, placeholder] }));+ const { chat, stream, transport } = setup({+ prepareRequest,+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ if (failure === 'prepare') {+ prepareRequest.mockImplementationOnce(() => {+ throw new Error('Preparation failed');+ });+ } else {+ transport.sendMessages.mockRejectedValueOnce(new Error('A run is already in progress'));+ }+ await chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ expect(chat.status).toBe('error');++ await expect(chat.stop()).resolves.toEqual({ status: 'stopped' });+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({+ endpoint: 'agents',+ streamId: 'convo-1',+ generationCreatedAt: 100,+ }),+ );+ });++ it.each([+ ['still preparing', 'prepare', { status: 'stopped' }],+ ['sent but unanswered', 'start', { status: 'unaddressable' }],+ ])(+ 'also stops a dropped generation when stopping its retry %s',+ async (_label, phase, expected) => {+ const prepareRequest = jest.fn((): ChatPreparedRequest | Promise<ChatPreparedRequest> => ({+ payload,+ messages: [userMessage, placeholder],+ }));+ const { chat, stream, transport } = setup({+ prepareRequest,+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ const pending = () => new Promise<never>(() => undefined);+ if (phase === 'prepare') {+ prepareRequest.mockImplementationOnce(pending);+ } else {+ transport.sendMessages.mockImplementationOnce(pending);+ }+ const retry = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();++ await expect(chat.stop()).resolves.toEqual(expected);+ await retry;+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({+ endpoint: 'agents',+ streamId: 'convo-1',+ generationCreatedAt: 100,+ }),+ );+ },+ );++ it('still stops a dropped generation after a retry rejected as an unnegotiated replacement', async () => {+ const { chat, stream, transport } = setup({+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ transport.sendMessages.mockResolvedValueOnce({+ status: 'replaced',+ streamId: 'stream-2',+ conversationId: 'convo-1',+ } as Awaited<ReturnType<ChatTransport['sendMessages']>>);++ await chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ expect(chat.status).toBe('error');++ await expect(chat.stop()).resolves.toEqual({ status: 'stopped' });+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ streamId: 'convo-1', generationCreatedAt: 100 }),+ );+ });++ it('does not let an older stop of a dropped Assistants run overwrite a retry in flight', async () => {const stream = createStream();+ const prepareRequest = jest.fn(+ (): ChatPreparedRequest => ({+ payload: { endpoint: 'assistants', conversationId: 'convo-1', text: 'Hi' } as TPayload,+ messages: [userMessage, placeholder],+ responseMessageId: 'response-1',+ }),+ );const { chat, transport } = setup(- {},- {- sendMessages: jest.fn(async () => ({- status: 'replaced' as const,- generationProtocolVersion: 2,- conversationId: 'convo-1',- streamId: 'stream-2',- generationCreatedAt: 200,- })),- reconnectToStream: jest.fn(async () => stream),- },+ { prepareRequest },+ { sendMessages: jest.fn(async () => ({ status: 'stream' as const, stream })) },);- const options = { headers: { 'x-tenant': 'acme' }, body: { spec: 'a' } };- const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] }, options);+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });await flush();- stream.push(finalEvent());+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);await sent;- expect(transport.reconnectToStream).toHaveBeenCalledWith(- expect.objectContaining({ ...options, chatId: 'convo-1', streamId: 'stream-2' }),+ let answer: (result: ChatAbortResult) => void = () => undefined;+ transport.abort.mockImplementationOnce(+ () =>+ new Promise<ChatAbortResult>((resolve) => {+ answer = resolve;+ }),);+ const stopped = chat.stop();+ await flush();+ const retry: TMessage = { ...placeholder, messageId: 'response-2' };+ transport.sendMessages.mockImplementationOnce(() => new Promise(() => undefined));+ prepareRequest.mockImplementationOnce(() => ({+ payload: { endpoint: 'assistants', conversationId: 'convo-1', text: 'Again' } as TPayload,+ messages: [userMessage, retry],+ responseMessageId: 'response-2',+ }));+ void chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();++ answer({ final: true, conversation: {}, runMessages: [userMessage, placeholder] });+ await stopped;++ expect(chat.messages).toEqual([userMessage, retry]);+ expect(chat.status).toBe('submitted');});it('stops a legacy first turn by the conversation its created frame named', async () => {@@ -1891,6 +2729,109 @@ describe('Chat', () => {);});+ it('reports the response of a resumed generation no frame named as unknown', async () => {+ const resumed = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ const { chat, state, transport, onFinish } = setup({+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ state.replaceMessages([userMessage, { ...placeholder, messageId: 'other-branch' }]);+ transport.reconnectToStream.mockResolvedValueOnce(resumed);++ const attached = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ await flush();+ resumed.push({ type: 'abort', data: {} } as ChatEvent);+ await attached;++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isAbort: true, isResponseUnknown: true, message: undefined }),+ );+ });++ it('sends one stop for a generation stopped again once a reattachment took it over', async () => {+ const { chat, stream, transport } = setup({+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ let answer: (result: ChatAbortResult) => void = () => undefined;+ transport.abort.mockImplementationOnce(+ () =>+ new Promise<ChatAbortResult>((resolve) => {+ answer = resolve;+ }),+ );+ const first = chat.stop();+ await flush();+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ transport.reconnectToStream.mockResolvedValueOnce(reattached);+ const resumed = chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ await flush();+ const second = chat.stop();+ await flush();++ answer({ success: true });+ await flush();+ reattached.push({ type: 'abort', data: {} } as ChatEvent);+ await resumed;++ await expect(Promise.all([first, second])).resolves.toEqual([+ { status: 'stopped' },+ { status: 'stopped' },+ ]);+ expect(transport.abort).toHaveBeenCalledTimes(1);+ expect(chat.status).toBe('ready');+ });++ it('ends a retry that replaced a dropped generation with the unsaved stop sent for it', async () => {+ let answer: ((result: ChatAbortResult) => void) | undefined;+ let reconnect: ((stream: ChatStream | null) => void) | undefined;+ const { chat, stream, transport, onFinish } = setup(+ {},+ {+ abort: jest.fn(+ () =>+ new Promise<ChatAbortResult>((resolve) => {+ answer = resolve;+ }),+ ),+ reconnectToStream: jest.fn(+ () =>+ new Promise<ChatStream | null>((resolve) => {+ reconnect = resolve;+ }),+ ),+ },+ );+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ const stopped = chat.stop();+ await flush();+ transport.sendMessages.mockResolvedValueOnce({+ status: 'replaced',+ generationProtocolVersion: 2,+ conversationId: 'convo-1',+ streamId: 'convo-1',+ generationCreatedAt: 100,+ });+ const retry = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ answer?.({ success: true, persistenceFailed: true });+ await flush();+ reconnect?.(null);+ await Promise.all([stopped, retry]);++ expect(transport.abort).toHaveBeenCalledTimes(1);+ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isAbort: true, reconcileReason: 'abort_persistence_failed' }),+ );+ });+it('reports a cancelled start the server may still run as a disconnect, not an abort', async () => {const { chat, onFinish } = setup({},@@ -1910,6 +2851,87 @@ describe('Chat', () => {await expect(chat.stop()).resolves.toEqual({ status: 'unaddressable' });});+ it('reports the stored tail when a local turn without a named response reattaches', async () => {+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ const { chat, stream, onFinish } = setup(+ { resumeSelection: () => ({ endpoint: 'agents' }) },+ { reconnectToStream: jest.fn(async () => reattached) },+ );+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ const resumed = chat.resumeStream();+ await flush();+ reattached.push({ type: 'abort', data: {} } as ChatEvent);+ await resumed;++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({+ isAbort: true,+ isResponseUnknown: false,+ message: expect.objectContaining({ messageId: 'response-1' }),+ }),+ );+ });++ it('keeps a lost start unaddressable when a retry fails before the server takes it', async () => {+ const failure = Object.assign(new Error('Network Error'), { code: 'ERR_NETWORK' });+ const prepareRequest = jest.fn(() => ({ payload, messages: [userMessage, placeholder] }));+ const { chat, transport } = setup({ prepareRequest });+ transport.sendMessages.mockRejectedValueOnce(failure);+ await chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });++ prepareRequest.mockImplementationOnce(() => {+ throw new Error('prepare failed');+ });+ await chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });++ await expect(chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ });++ it('keeps a lost start unaddressable when its retry is stopped while preparing', async () => {+ const failure = Object.assign(new Error('Network Error'), { code: 'ERR_NETWORK' });+ const prepareRequest = jest.fn((): ChatPreparedRequest | Promise<ChatPreparedRequest> => ({+ payload,+ messages: [userMessage, placeholder],+ }));+ const { chat, transport } = setup({ prepareRequest });+ transport.sendMessages.mockRejectedValueOnce(failure);+ await chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });++ prepareRequest.mockImplementationOnce(() => new Promise<never>(() => undefined));+ const retry = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();++ await expect(chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ await retry;+ });++ it('keeps a generation stoppable when the host fails to apply one of its frames', async () => {+ const applyEvent = jest.fn((event: ChatEvent) => {+ if (event.type === 'step') {+ throw new Error('apply failed');+ }+ });+ const { chat, stream, transport, onFinish } = setup({+ applyEvent,+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push(stepEvent);+ await sent;++ expect(stream.closed()).toBe(true);+ expect(onFinish).toHaveBeenLastCalledWith(expect.objectContaining({ isDisconnect: true }));+ await expect(chat.stop()).resolves.toEqual({ status: 'stopped' });+ expect(transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ streamId: 'convo-1', generationCreatedAt: 100 }),+ );+ });+it('ends a turn by its terminal frame when the host fails to apply it', async () => {const applyEvent = jest.fn((event: ChatEvent) => {if (event.type === 'final') {@@ -1918,6 +2940,7 @@ describe('Chat', () => {});const { chat, stream, transport, onFinish } = setup({applyEvent,+ resumeSelection: () => ({ endpoint: 'agents' }),});const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });await flush();@@ -1931,6 +2954,87 @@ describe('Chat', () => {expect(transport.abort).not.toHaveBeenCalled();});+ it('keeps a lost start unaddressable when a reattachment fails before the server answers', async () => {+ const failure = () => Object.assign(new Error('Network Error'), { code: 'ERR_NETWORK' });+ const { chat, transport } = setup();+ transport.sendMessages.mockRejectedValueOnce(failure());+ await chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });++ transport.reconnectToStream.mockRejectedValueOnce(failure());+ await chat.resumeStream();++ await expect(chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ });++ it('ends a retry the server attached to the dropped generation by the stop in flight for it', async () => {+ let answer: ((result: ChatAbortResult) => void) | undefined;+ const { chat, stream, transport, onFinish } = setup();+ transport.abort.mockImplementation(+ () =>+ new Promise<ChatAbortResult>((resolve) => {+ answer = resolve;+ }),+ );+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ const stopped = chat.stop();+ await flush();++ const attached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ transport.sendMessages.mockResolvedValueOnce({ status: 'stream', stream: attached });+ const retry = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();+ answer?.({ success: true });+ await flush();+ attached.close();+ await expect(stopped).resolves.toEqual({ status: 'stopped' });+ await retry;++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({ isAbort: true, isDisconnect: false }),+ );+ });++ it('undoes the optimistic rows of a retry the server attached to the dropped generation', async () => {+ const retryUser: TMessage = { ...userMessage, messageId: 'user-2', text: 'Again' };+ const retryResponse: TMessage = {+ ...placeholder,+ messageId: 'response-2',+ parentMessageId: 'user-2',+ };+ const prepareRequest = jest.fn(() => ({+ payload,+ messages: (current: TMessage[]) => [...current, userMessage, placeholder],+ }));+ const { chat, stream, transport } = setup({ prepareRequest });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ const attached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ let attachedWith: string[] = [];+ transport.sendMessages.mockImplementationOnce(async () => {+ await flush();+ return { status: 'stream', stream: attached };+ });+ prepareRequest.mockImplementationOnce(() => ({+ payload,+ messages: (current: TMessage[]) => [...current, retryUser, retryResponse],+ }));+ const retry = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();+ await flush();+ attachedWith = chat.messages.map((message) => message.messageId);+ attached.push(finalEvent());+ await retry;++ expect(attachedWith).toEqual(['user-1', 'response-1']);+ expect(chat.messages.map((message) => message.messageId)).toEqual(['user-1', 'response-1']);+ });+it('reports a lost start stopped once its late stream was stopped on the server', async () => {let deliver: ((result: { status: 'stream'; stream: ChatStream }) => void) | undefined;const late = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });@@ -1954,9 +3058,36 @@ describe('Chat', () => {);await expect(chat.stop()).resolves.toEqual({ status: 'idle' });});++ it('settles a named reattachment that finds its generation already finished', async () => {+ const { chat, transport, onFinish } = setup({+ resumeSelection: () => ({ endpoint: 'agents' }),+ });+ transport.reconnectToStream.mockResolvedValue(null);++ await chat.resumeStream();+ expect(onFinish).not.toHaveBeenCalled();++ await chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ expect(onFinish).toHaveBeenCalledTimes(1);+ expect(onFinish).toHaveBeenCalledWith(expect.objectContaining({ isSettled: true }));+ });});describe('ids a turn reports', () => {+ it('opens no reattachment a listener disposed the chat from on its reconnecting notice', async () => {+ const { chat, transport } = setup();+ chat.subscribe(() => {+ if (chat.connection === 'reconnecting') {+ chat.dispose();+ }+ });++ await chat.resumeStream({ streamId: 'convo-1' }).catch(() => undefined);++ expect(transport.reconnectToStream).not.toHaveBeenCalled();+ });+it('opens no replacement a listener disposed the chat from on its reconnecting notice', async () => {const { chat, transport } = setup({},@@ -2070,6 +3201,30 @@ describe('Chat', () => {},);+ it('settles an unframed stop under the chat, not under the stream id the answer names', async () => {+ const { chat, stream, transport, onFinish } = setup();+ transport.abort.mockResolvedValue({+ success: true,+ generationProtocolVersion: 2,+ aborted: 'stream-9',+ persistenceFailed: true,+ });+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ stream.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;++ await expect(chat.stop()).resolves.toEqual({ status: 'stopped', conversationId: 'convo-1' });++ expect(onFinish).toHaveBeenLastCalledWith(+ expect.objectContaining({+ isSettled: true,+ conversationId: 'convo-1',+ reconcileReason: 'abort_persistence_failed',+ }),+ );+ });+it('reports no response for a turn whose optimistic write failed', async () => {const earlier: TMessage = {...placeholder,@@ -2695,6 +3850,64 @@ describe('Chat attachments and reported conversations', () => {expect(attachedWith).toEqual(['earlier-1:Earlier', 'earlier-2:Answer', 'user-1:Edited']);});+ it.each([+ ['over a store that reports writes synchronously', false],+ ['over a structurally shared store', true],+ ])(+ 'keeps a row a store subscriber appended inside the write of a retry the server answered with the dropped generation, %s',+ async (_label, shared) => {+ const state = createStore(shared);+ const retryUser: TMessage = { ...userMessage, messageId: 'user-2', text: 'Again' };+ const retryResponse: TMessage = {+ ...placeholder,+ messageId: 'response-2',+ parentMessageId: 'user-2',+ };+ state.subscribe?.(() => {+ const current = state.messages;+ if (+ current.some((m) => m.messageId === retryResponse.messageId) &&+ !current.some((m) => m.messageId === external.messageId)+ ) {+ state.replaceMessages([...current, external]);+ }+ });+ const first = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ const reattached = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ let attachedWith: string[] = [];+ const prepareRequest = jest+ .fn()+ .mockImplementationOnce(() => ({ payload, messages: optimistic }))+ .mockImplementationOnce(() => ({+ payload,+ messages: (current: TMessage[]) => [...current, retryUser, retryResponse],+ }));+ const { chat, transport } = setup(+ { state, prepareRequest },+ {+ sendMessages: jest.fn(async () => ({ status: 'stream' as const, stream: first })),+ reconnectToStream: jest.fn(async () => {+ attachedWith = rows(chat.messages);+ return reattached;+ }),+ },+ );+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ await flush();+ first.push({ type: 'error', status: 0, data: 'offline' } as ChatEvent);+ await sent;+ const before = rows(state.messages);++ transport.sendMessages.mockResolvedValueOnce({ ...replaced, generationCreatedAt: 100 });+ const retry = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();+ reattached.push(finalEvent({ requestMessage: null, responseMessage: null }));+ await retry;++ expect(attachedWith).toEqual([...before, 'external-1:External']);+ },+ );+/** The rows an Assistants stop answer carries, which replace the list when the run ends. */const stored: TMessage[] = [userMessage, { ...placeholder, text: 'Stored' }];const storedAnswer = { final: true, conversation: {}, runMessages: stored };diff --git a/packages/chat/src/core/__tests__/generations.spec.ts b/packages/chat/src/core/__tests__/generations.spec.tsindex e81b91a32a..b925be5d70 100644--- a/packages/chat/src/core/__tests__/generations.spec.ts+++ b/packages/chat/src/core/__tests__/generations.spec.ts@@ -160,6 +160,7 @@ const createHarness = (init: Partial<ChatInit> & { messages?: TMessage[] } = {})state,transport: harness.transport,prepareRequest: (...args) => harness.prepare(...args),+ resumeSelection: () => ({ endpoint: 'agents' }),onFinish: (result) => finishes.push(result),...overrides,});@@ -224,6 +225,32 @@ const states: Record<string, (h: Harness) => Promise<void>> = {h.stream?.push(stepEvent);await flush();},+ /** The stream dropped; the server keeps the generation running. */+ detached: async (h) => {+ await states.attached(h);+ h.stream?.push(dropEvent);+ await flush();+ h.stream = undefined;+ },+ /** Reattaching to a running generation; the reconnect is in flight. */+ reattaching: async (h) => {+ h.transport.reconnectToStream.mockImplementation(+ (request) =>+ new Promise((resolve, reject) => {+ h.open = () => {+ h.stream = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ resolve(h.stream);+ };+ h.fail = () => reject(networkError());+ request.abortSignal?.addEventListener('abort', () => reject(new Error('aborted')));+ }),+ );+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ },/** The server finished the turn before the chat attached. */settled: async (h) => {h.transport.sendMessages.mockResolvedValue({@@ -299,6 +326,11 @@ const events: Record<string, (h: Harness) => Promise<void>> = {}await flush();},+ dropThenStop: async (h) => {+ await events.networkDrop(h);+ h.stream = undefined;+ await events.stop(h);+ },final: async (h) => {h.open?.();await flush();@@ -362,6 +394,12 @@ const events: Record<string, (h: Harness) => Promise<void>> = {);await events.stop(h);},+ /** A reconnect that finds nothing running: the generation ended while nothing was attached. */+ reconnectIdle: async (h) => {+ h.transport.reconnectToStream.mockResolvedValue(null);+ await record(h, h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 }));+ await flush();+ },dispose: async (h) => {h.chat.dispose();await flush();@@ -443,13 +481,14 @@ const unsaved = {};const table: Array<[keyof typeof states, keyof typeof events, Expected]> = [- ['starting', 'final', { status: 'ready', finishes: 1, last: done, abort: NONE }],- ['starting', 'newTurn', { status: 'submitted', finishes: 0, outcome: 'in flight' }],+ /** The start reached the transport, so the server may run it with nothing this chat can name. */['starting','stop',{ status: 'error', finishes: 1, last: dropped, abort: NONE, outcome: 'unaddressable' },],+ ['starting', 'final', { status: 'ready', finishes: 1, last: done, abort: NONE }],+ ['starting', 'newTurn', { status: 'submitted', finishes: 0, outcome: 'in flight' }],['attached',@@ -475,6 +514,37 @@ const table: Array<[keyof typeof states, keyof typeof events, Expected]> = [['attached', 'reconcile', { status: 'ready', finishes: 1, last: settled }],['attached', 'newTurn', { status: 'streaming', finishes: 0, outcome: 'in flight' }],+ [+ 'detached',+ 'stop',+ {+ status: 'ready',+ finishes: 2,+ last: stoppedUnframed,+ abort: { streamId: 'convo-1', generationCreatedAt: 100 },+ },+ ],+ ['detached', 'newTurn', { status: 'ready', finishes: 2, last: settled, outcome: 'sent:ok' }],++ [+ 'reattaching',+ 'stop',+ {+ status: 'ready',+ connection: 'idle',+ finishes: 1,+ last: stoppedUnframed,+ abort: { streamId: 'convo-1', generationCreatedAt: 300 },+ },+ ],+ ['reattaching', 'final', { status: 'ready', connection: 'idle', finishes: 1, last: done }],+ ['reattaching', 'reconcile', { status: 'ready', finishes: 1, last: settled }],+ [+ 'reattaching',+ 'newTurn',+ { status: 'ready', connection: 'reconnecting', finishes: 0, outcome: 'in flight' },+ ],+['settled', 'stop', { status: 'ready', finishes: 1, last: settled, abort: NONE, outcome: 'ok' }],['settled', 'newTurn', { status: 'ready', finishes: 2, outcome: 'sent:ok' }],@@ -498,7 +568,53 @@ const table: Array<[keyof typeof states, keyof typeof events, Expected]> = [/** Network failures before a stream, and the stop that follows a drop or a restore. */['starting', 'streamEnd', { status: 'error', finishes: 1, last: dropped, abort: NONE }],['starting', 'networkDrop', { status: 'error', finishes: 1, last: dropped, abort: NONE }],+ /** A start whose response was lost left nothing to address; the caller stops it its own way. */+ [+ 'starting',+ 'dropThenStop',+ { status: 'error', finishes: 1, last: dropped, abort: NONE, outcome: 'unaddressable' },+ ],+ [+ 'attached',+ 'dropThenStop',+ {+ status: 'ready',+ finishes: 2,+ last: stoppedUnframed,+ abort: { streamId: 'convo-1', generationCreatedAt: 100 },+ },+ ],+ [+ 'reattaching',+ 'streamEnd',+ { status: 'error', connection: 'idle', finishes: 1, last: dropped, abort: NONE },+ ],+ [+ 'reattaching',+ 'networkDrop',+ { status: 'error', connection: 'idle', finishes: 1, last: dropped, abort: NONE },+ ],+ [+ 'reattaching',+ 'dropThenStop',+ {+ status: 'ready',+ finishes: 2,+ last: stoppedUnframed,+ abort: { streamId: 'convo-1', generationCreatedAt: 300 },+ },+ ],['replaced', 'streamEnd', { status: 'error', finishes: 1, last: dropped }],+ [+ 'replaced',+ 'dropThenStop',+ {+ status: 'ready',+ finishes: 2,+ last: stoppedUnframed,+ abort: { streamId: 'stream-2', generationCreatedAt: 200 },+ },+ ],/** Stops the server answered as unsaved or as already settled; neither is a plain abort. */[@@ -512,6 +628,42 @@ const table: Array<[keyof typeof states, keyof typeof events, Expected]> = [outcome: 'ok',},],+ [+ 'reattaching',+ 'unsavedStop',+ {+ status: 'ready',+ connection: 'idle',+ finishes: 1,+ last: unsaved,+ abort: { streamId: 'convo-1', generationCreatedAt: 300 },+ outcome: 'ok',+ },+ ],+ [+ 'detached',+ 'unsavedStop',+ {+ status: 'ready',+ finishes: 2,+ last: unsaved,+ abort: { streamId: 'convo-1', generationCreatedAt: 100 },+ outcome: 'ok',+ },+ ],+ ['detached', 'reconnectIdle', { status: 'ready', finishes: 2, last: settled, abort: NONE }],+ ['aborted', 'reconnectIdle', { status: 'ready', finishes: 1, last: aborted, abort: NONE }],+ [+ 'detached',+ 'settledStop',+ {+ status: 'ready',+ finishes: 2,+ last: settled,+ abort: { streamId: 'convo-1', generationCreatedAt: 100 },+ outcome: 'ok',+ },+ ],];const live = { streamId: 'convo-1', generationCreatedAt: 100 };@@ -565,6 +717,7 @@ const teardown: Array<[keyof typeof states, keyof typeof events, Expected]> = ['dispose',{ status: 'submitted', connection: 'idle', finishes: 0, turn: 'settled', abort: NONE },],+ /** The late start is stopped on the server, which resolves the failure its lost answer reported. */['starting','lateAnswer',@@ -578,6 +731,7 @@ const teardown: Array<[keyof typeof states, keyof typeof events, Expected]> = [after: 'idle',},],+ /** The late settled answer says the lost start finished, which resolves the failure it reported. */['starting','lateSettled',@@ -645,6 +799,11 @@ const gone: Array<[keyof typeof states, keyof typeof events, Expected]> = [after: undefined,},],+ [+ 'detached',+ 'goneStop',+ { status: 'ready', finishes: 2, last: settled, abort: live, outcome: 'ok' },+ ],];describe('Chat teardown', () => {@@ -715,8 +874,15 @@ describe('Chat lifecycle', () => {* (3) a disposed chat reaches no transport method;* (4) a generation record carries the response its source named, or counts it unknown.*/+const lost = async (h: Harness) => {+ await states.starting(h);+ h.fail?.();+ await flush();+};+const disposedCalls: Record<string, (h: Harness) => Promise<unknown>> = {stop: (h) => h.chat.stop(),+ resumeStream: (h) => h.chat.resumeStream({ streamId: 'convo-1' }),sendMessage: (h) => h.chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] }),};@@ -726,14 +892,18 @@ const transportCalls = (h: Harness) =>h.transport.abort.mock.calls.length +h.transport.resume.mock.calls.length;-const disposedTable = Object.keys(states).flatMap((state) =>+const withLost: Record<string, (h: Harness) => Promise<void>> = { ...states, lost };++const disposedTable = Object.keys(withLost).flatMap((state) =>Object.keys(disposedCalls).map((call) => [state, call] as const),);+const laterMessage: TMessage = { ...placeholder, messageId: 'response-10', text: 'Later' };+describe('Chat invariants', () => {it.each(disposedTable)('(3) %s, disposed, then %s reaches no transport', async (state, call) => {const h = createHarness();- await states[state](h);+ await withLost[state](h);h.chat.dispose();await flush();const before = transportCalls(h);@@ -748,7 +918,97 @@ describe('Chat invariants', () => {expect(settled).toBe(true);});+ it.each([+ ['a stream answer', { success: true }],+ ['an Assistants answer', { final: true, conversation: {}, runMessages: [] }],+ ['an Assistants answer finding no run', null],+ ['a gone answer', Object.assign(new Error('Not Found'), { response: { status: 404 } })],+ ])(+ '(1) a stop answered with %s after its reattachment moved to another generation leaves that one running',+ async (_label, answer) => {+ const h = createHarness();+ await states.detached(h);+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementation(+ () =>+ new Promise((resolve, reject) => {+ answerStop = () =>+ answer instanceof Error ? reject(answer) : resolve(answer as ChatAbortResult);+ }),+ );+ const other = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ let openOther: (() => void) | undefined;+ h.transport.reconnectToStream.mockImplementation(+ () => new Promise((resolve) => (openOther = () => resolve(other))),+ );+ h.chat.resumeStream({ streamId: 'convo-1' }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ let stopped = false;+ h.chat.stop().then(+ () => (stopped = true),+ () => (stopped = true),+ );+ await flush();+ openOther?.();+ await flush();+ answerStop?.();+ await flush();+ other.push(stepEvent);+ await flush();++ expect(stopped).toBe(true);+ expect(other.closed()).toBe(false);+ expect(h.chat.status).toBe('streaming');+ expect(h.chat.connection).toBe('live');+ },+ );++ it("(2) a lost start stays the caller's through a reattachment that failed before any answer", async () => {+ const h = createHarness({ resumeSelection: () => ({}) });+ await lost(h);+ h.transport.reconnectToStream.mockRejectedValueOnce(networkError());+ await h.chat.resumeStream({ streamId: 'convo-1' }).catch(() => undefined);++ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ });++ it.each([+ ['whose preparation failed', () => Promise.reject(new Error('Upload failed'))],+ ['stopped before it was sent', () => new Promise<never>(() => undefined)],+ ])("(2) a lost start stays the caller's through a retry %s", async (_label, retry) => {+ let prepared = 0;+ const h = createHarness({+ prepareRequest: () => {+ prepared += 1;+ if (prepared > 1) {+ return retry() as never;+ }+ return { payload, messages: [userMessage, placeholder], responseMessageId: 'response-1' };+ },+ });+ await lost(h);+ h.chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] }).then(+ () => undefined,+ () => undefined,+ );+ await flush();++ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ });+it.each([+ [+ 'a named reattachment',+ (h: Harness) =>+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 300 }).then(+ () => undefined,+ () => undefined,+ ),+ ],['a replacement',(h: Harness) => {@@ -831,6 +1091,76 @@ describe('Chat invariants', () => {expect(h.transport.reconnectToStream).not.toHaveBeenCalled();},);++ it.each([['a streamed frame', stepEvent]])(+ "(4) a reattachment that recovers with %s ends the dropped connection's error",+ async (_label, event) => {+ const h = createHarness();+ await states.detached(h);+ expect(h.chat.status).toBe('error');+ const resumed = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ h.transport.reconnectToStream.mockResolvedValueOnce(resumed);+ h.chat.resumeStream({ streamId: 'convo-1' }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ resumed.push(event);+ await flush();++ expect(h.chat.status).toBe('streaming');+ expect(h.chat.error).toBeUndefined();+ },+ );++ it.each([+ ['with no turn in flight', false],+ ['while a later retry is still preparing', true],+ ])(+ '(2) a stop of a dropped generation still owes the abort of a lost retry %s',+ async (_label, preparing) => {+ let prepared = 0;+ const h = createHarness({+ prepareRequest: () => {+ prepared += 1;+ if (prepared > 2) {+ return new Promise<never>(() => undefined);+ }+ return { payload, messages: [userMessage, placeholder], responseMessageId: 'response-1' };+ },+ });+ await states.detached(h);+ h.transport.sendMessages.mockRejectedValueOnce(networkError());+ send(h);+ await flush();+ if (preparing) {+ send(h);+ await flush();+ }++ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ expect(h.transport.abort).toHaveBeenCalledTimes(1);+ expect(h.transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ generationCreatedAt: 100 }),+ );+ },+ );++ it('(4) a fresh reattachment that drops before a frame names its response reports none', async () => {+ const h = createHarness({ messages: [userMessage, laterMessage] });+ const stream = createStream({ streamId: 'convo-1', generationCreatedAt: 700 });+ h.transport.reconnectToStream.mockResolvedValueOnce(stream);+ h.chat.resumeStream({ streamId: 'convo-1' }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ stream.push(dropEvent);+ await flush();++ expect(h.finishes).toHaveLength(1);+ expect(h.finishes[0]).toMatchObject({ isResponseUnknown: true, message: undefined });+ });});/** Where a host callback or transport method can throw once a request owns the chat. */@@ -893,6 +1223,53 @@ describe('Chat exception safety', () => {expect(await freeAfter(h)).toBe(true);});+ it('(5) a reattachment whose resume selection throws keeps the dropped generation stoppable', async () => {+ const f: Fault = { fault: false };+ const h = createHarness({+ resumeSelection: () => {+ throwing(f, 'selection');+ return { endpoint: 'agents' };+ },+ });+ await states.detached(h);+ f.fault = true;++ expect(await settles(h.chat.resumeStream({ streamId: 'convo-1' }))).toBe(true);+ await h.chat.stop();+ expect(h.transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ generationCreatedAt: 100 }),+ );+ });++ it('(5) a reattachment whose transport.reconnectToStream throws settles and frees the chat', async () => {+ const h = createHarness();+ h.transport.reconnectToStream.mockImplementationOnce(() => {+ throw new Error('reconnect failed');+ });++ expect(await settles(h.chat.resumeStream({ streamId: 'convo-1' }))).toBe(true);+ expect(await freeAfter(h)).toBe(true);+ });++ it('(5) a frame whose applyEvent throws keeps its generation stoppable', async () => {+ const f: Fault = { fault: false };+ const h = createHarness({+ applyEvent: () => {+ throwing(f, 'apply');+ },+ });+ await states.attached(h);+ f.fault = true;+ h.stream?.push(stepEvent);+ await flush();++ expect(h.chat.status).toBe('error');+ await h.chat.stop();+ expect(h.transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ generationCreatedAt: 100 }),+ );+ });+it('(5) a stop whose transport.abort throws leaves the stream attached and a later stop sends anew', async () => {const h = createHarness();await states.attached(h);@@ -964,63 +1341,523 @@ describe('Chat final frames', () => {});const createdUnder = 'convo-9';-describe('Chat late answers and lost starts', () => {- it.each([- ['settled', { status: 'settled', conversationId: createdUnder, generationProtocolVersion: 2 }],- [- 'replaced',- {- status: 'replaced',- conversationId: createdUnder,- streamId: 'stream-2',- generationCreatedAt: 200,- generationProtocolVersion: 2,- },- ],- ])(- '(7) a cancelled first turn answered late as %s carries the conversation into the next turn',- async (_label, late) => {- const h = createHarness({- id: 'new',- prepareRequest: () => ({- payload: { ...payload, conversationId: null } as unknown as TPayload,- messages: [userMessage, placeholder],- responseMessageId: 'response-1',- }),- });- let answer: (() => void) | undefined;- h.transport.sendMessages.mockImplementationOnce(- () => new Promise((resolve) => (answer = () => resolve(late as never))),- );+const learns: Array<[string, (h: Harness) => Promise<void>]> = [+ [+ 'a sync frame',+ async (h) => {+ const stream = createStream({ streamId: createdUnder, generationCreatedAt: 100 });+ attach(h, stream);send(h);await flush();- await h.chat.stop();- answer?.();- await flush();-- attach(h, createStream({ streamId: createdUnder, generationCreatedAt: 900 }));+ stream.push({+ type: 'sync',+ data: { conversationId: createdUnder, responseMessage: placeholder },+ } as ChatEvent);+ stream.push(finalEvent());+ await flush();+ },+ ],+ [+ 'a created frame',+ async (h) => {+ const stream = createStream({ streamId: createdUnder, generationCreatedAt: 100 });+ attach(h, stream);+ send(h);+ await flush();+ stream.push({+ type: 'created',+ data: { message: { ...userMessage, conversationId: createdUnder }, created: true },+ } as ChatEvent);+ stream.push(finalEvent());+ await flush();+ },+ ],+ [+ 'a settled start',+ async (h) => {+ h.transport.sendMessages.mockResolvedValueOnce({+ status: 'settled',+ conversationId: createdUnder,+ generationProtocolVersion: 2,+ });send(h);await flush();+ },+ ],+];++describe('Chat conversation identity', () => {+ it.each(learns)(+ '(7) after %s names the conversation, later turns, stops and reattachments use it',+ async (_label, learn) => {+ const prepared: string[] = [];+ const h = createHarness({+ id: 'new',+ prepareRequest: (request) => {+ prepared.push(request.chatId);+ return {+ payload: { ...payload, conversationId: null } as unknown as TPayload,+ messages: [userMessage, placeholder],+ responseMessageId: 'response-1',+ };+ },+ });+ await learn(h);++ const next = createStream({ streamId: createdUnder, generationCreatedAt: 700 });+ attach(h, next);+ send(h);+ await flush();+ expect(prepared[prepared.length - 1]).toBe(createdUnder);expect(h.transport.sendMessages).toHaveBeenLastCalledWith(expect.objectContaining({ chatId: createdUnder }),);+ h.transport.abort.mockResolvedValueOnce({ success: true });+ next.push(dropEvent);+ await flush();+ await h.chat.stop();+ expect(h.transport.abort).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: createdUnder }),+ );+ await h.chat.resumeStream({ streamId: createdUnder }).catch(() => undefined);+ expect(h.transport.reconnectToStream).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: createdUnder }),+ );+ },+ );+});++describe('Chat status reads out of order', () => {+ it.each([['the dropped generation', states.detached, 50, 100]])(+ '(2) a reattachment naming a generation older than %s changes nothing',+ async (_label, state, named, kept) => {+ const h = createHarness();+ await state(h);+ const reconnects = h.transport.reconnectToStream.mock.calls.length;+ await h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: named });++ expect(h.transport.reconnectToStream.mock.calls.length).toBe(reconnects);+ h.transport.abort.mockResolvedValueOnce({ success: true });+ await h.chat.stop();+ expect(h.transport.abort).toHaveBeenLastCalledWith(+ expect.objectContaining({ generationCreatedAt: kept }),+ );+ },+ );++ it('(5) an idle stop answers idle without reading a resume selection that throws', async () => {+ const h = createHarness({+ resumeSelection: () => {+ throw new Error('no selection');+ },+ });++ await expect(h.chat.stop()).resolves.toEqual({ status: 'idle' });+ });+});++describe('Chat answers, successors and pause order', () => {+ it('(2) a stop of an unsent retry still ends the dropped generation when its callback starts a successor', async () => {+ let prepared = 0;+ let started = false;+ const h = createHarness({+ prepareRequest: () => {+ prepared += 1;+ if (prepared > 1) {+ return new Promise<never>(() => undefined);+ }+ return { payload, messages: [userMessage, placeholder], responseMessageId: 'response-1' };+ },+ });+ await states.detached(h);+ const chat = h.chat;+ const unsubscribe = chat.subscribe(() => {+ if (!started && prepared === 2 && chat.status === 'ready') {+ started = true;+ chat.sendMessage({ parts: [{ type: 'text', text: 'Successor' }] }).then(+ () => undefined,+ () => undefined,+ );+ }+ });+ send(h);+ await flush();++ await h.chat.stop();+ unsubscribe();+ expect(started).toBe(true);+ expect(h.transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ generationCreatedAt: 100 }),+ );+ });+});++describe('Chat identity every frame carries', () => {+ const ids = { conversationId: createdUnder, responseMessageId: 'response-9' };+ const frames: Array<[string, ChatEvent, { response?: boolean; conversation?: boolean }]> = [+ [+ 'activity_label',+ { type: 'activity_label', data: { index: 0, part: {}, ...ids } } as unknown as ChatEvent,+ { response: true, conversation: true },+ ],+ [+ 'reasoning_label',+ { type: 'reasoning_label', data: { index: 0, ...ids } } as unknown as ChatEvent,+ { response: true, conversation: true },+ ],+ [+ 'steer_applied',+ {+ type: 'steer_applied',+ data: { steerId: 's', index: 0, part: {}, ...ids },+ } as unknown as ChatEvent,+ { response: true, conversation: true },+ ],+ [+ 'content',+ {+ type: 'content',+ data: {+ index: 0,+ type: 'text',+ text: 'x',+ messageId: 'response-9',+ conversationId: createdUnder,+ },+ } as unknown as ChatEvent,+ { response: true, conversation: true },+ ],+ [+ 'text',+ { type: 'text', data: { text: 'x', messageId: 'response-9' } } as unknown as ChatEvent,+ { response: true },+ ],+ [+ 'title',+ {+ type: 'title',+ data: { event: 'title', data: { conversationId: createdUnder, title: 'T' } },+ } as ChatEvent,+ { conversation: true },+ ],+ [+ 'steer_updated',+ { type: 'steer_updated', data: { conversationId: createdUnder } } as unknown as ChatEvent,+ { conversation: true },+ ],+ ];++ it.each(frames)('(4)(7) the %s frame names what it carries', async (_label, frame, names) => {+ const named: TMessage = { ...placeholder, messageId: 'response-9', text: 'Named' };+ const h = createHarness({+ id: 'new',+ messages: [userMessage, named, { ...placeholder, messageId: 'other' }],+ });+ const stream = createStream({ streamId: createdUnder, generationCreatedAt: 700 });+ h.transport.reconnectToStream.mockResolvedValueOnce(stream);+ h.chat.resumeStream({ streamId: createdUnder }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ stream.push(frame);+ await flush();+ stream.push({ type: 'error', data: 'failed' } as ChatEvent);+ await flush();++ const last = h.finishes[h.finishes.length - 1];+ if (names.response) {+ expect(last).toMatchObject({ isResponseUnknown: false });+ expect(last.message?.messageId).toBe('response-9');+ }+ if (names.conversation) {+ attach(h, createStream({ streamId: createdUnder, generationCreatedAt: 800 }));+ send(h);+ await flush();+ expect(h.transport.sendMessages).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: createdUnder }),+ );+ }+ });+});++describe('Chat late answers and lost starts', () => {+ it('(2) a stop of an unsent retry still owes the lost start when a callback starts another turn', async () => {+ let prepared = 0;+ let started = false;+ const h = createHarness({+ prepareRequest: () => {+ prepared += 1;+ if (prepared > 1) {+ return new Promise<never>(() => undefined);+ }+ return { payload, messages: [userMessage, placeholder], responseMessageId: 'response-1' };+ },+ });+ await lost(h);+ const chat = h.chat;+ const unsubscribe = chat.subscribe(() => {+ if (!started && prepared === 2 && chat.status !== 'submitted') {+ started = true;+ chat.sendMessage({ parts: [{ type: 'text', text: 'Successor' }] }).then(+ () => undefined,+ () => undefined,+ );+ }+ });+ send(h);+ await flush();++ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ unsubscribe();+ expect(started).toBe(true);+ });++ it.each([+ ['settled', { status: 'settled', conversationId: createdUnder, generationProtocolVersion: 2 }],+ [+ 'replaced',+ {+ status: 'replaced',+ conversationId: createdUnder,+ streamId: 'stream-2',+ generationCreatedAt: 200,+ generationProtocolVersion: 2,+ },+ ],+ ])(+ '(7) a cancelled first turn answered late as %s carries the conversation into the next turn',+ async (_label, late) => {+ const h = createHarness({+ id: 'new',+ prepareRequest: () => ({+ payload: { ...payload, conversationId: null } as unknown as TPayload,+ messages: [userMessage, placeholder],+ responseMessageId: 'response-1',+ }),+ });+ let answer: (() => void) | undefined;+ h.transport.sendMessages.mockImplementationOnce(+ () => new Promise((resolve) => (answer = () => resolve(late as never))),+ );+ send(h);+ await flush();+ await h.chat.stop();+ answer?.();+ await flush();++ attach(h, createStream({ streamId: createdUnder, generationCreatedAt: 900 }));+ send(h);+ await flush();+ expect(h.transport.sendMessages).toHaveBeenLastCalledWith(+ expect.objectContaining({ chatId: createdUnder }),+ );+ },+ );+});++describe('Chat stops while a lost start is owed', () => {+ it('(2) a stop of a reattachment still owes the lost start', async () => {+ const h = createHarness();+ await lost(h);+ h.transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 500 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ h.transport.abort.mockResolvedValueOnce({ success: true });++ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ });++ it('(2) an Assistants stop answer without a conversation still resolves the stop', async () => {+ const h = createHarness();+ await states.attached(h);+ h.transport.abort.mockResolvedValueOnce({+ final: true,+ runMessages: [],+ } as unknown as ChatAbortResult);++ await expect(h.chat.stop()).resolves.toEqual({ status: 'stopped' });+ });+});++describe('Chat reattachments that fail over HTTP', () => {+ it('(2) a reattachment that fails with an HTTP status keeps its generation stoppable', async () => {+ const h = createHarness();+ const stream = createStream({ streamId: 'convo-1', generationCreatedAt: 700 });+ h.transport.reconnectToStream.mockResolvedValueOnce(stream);+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 700 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ stream.push({ type: 'error', status: 502, data: 'Bad gateway' } as ChatEvent);+ await flush();++ expect(h.finishes[h.finishes.length - 1]).toMatchObject({ isDisconnect: true });+ h.transport.abort.mockResolvedValueOnce({ success: true });+ await h.chat.stop();+ expect(h.transport.abort).toHaveBeenCalledWith(+ expect.objectContaining({ generationCreatedAt: 700 }),+ );+ });+});++describe('Chat lost starts and conflict probes', () => {+ it('(2) a retry stopped before it is sent keeps the lost start failure', async () => {+ let prepared = 0;+ const h = createHarness({+ prepareRequest: () => {+ prepared += 1;+ if (prepared > 1) {+ return new Promise<never>(() => undefined);+ }+ return { payload, messages: [userMessage, placeholder], responseMessageId: 'response-1' };+ },+ });+ await lost(h);+ send(h);+ await flush();++ await expect(h.chat.stop()).resolves.toEqual({ status: 'unaddressable' });+ expect(h.chat.status).toBe('error');+ expect(h.chat.error).toBeDefined();+ });+});++describe('Chat disposed while a confirmed stop ends its request', () => {+ it.each([0, 1, 2, 3, 4, 6, 8, 12])(+ '(3) a chat disposed %i microtasks into the answer to a stop of a failed reattachment writes no state',+ async (ticks) => {+ const h = createHarness();+ await states.reattaching(h);+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementation(+ () =>+ new Promise((resolve) => {+ answerStop = () =>+ resolve({+ final: true,+ conversation: {},+ runMessages: [userMessage],+ } as unknown as ChatAbortResult);+ }),+ );+ h.chat.stop().then(+ () => undefined,+ () => undefined,+ );+ await flush();+ h.fail?.();+ await flush();+ expect(h.chat.status).toBe('error');++ answerStop?.();+ for (let i = 0; i < ticks; i++) {+ await Promise.resolve();+ }+ const { status, error } = h.chat;+ h.chat.dispose();+ await flush();++ expect(h.chat.status).toBe(status);+ expect(h.chat.error).toBe(error);+ },+ );++ it.each([0, 1, 2, 3, 4, 6, 8, 12])(+ '(3) a chat disposed %i microtasks into the Assistants answer to a stop of a dropped generation writes no state',+ async (ticks) => {+ const h = createHarness();+ await states.detached(h);+ expect(h.chat.status).toBe('error');+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementation(+ () =>+ new Promise((resolve) => {+ answerStop = () =>+ resolve({+ final: true,+ conversation: {},+ runMessages: [userMessage],+ } as unknown as ChatAbortResult);+ }),+ );+ h.chat.stop().then(+ () => undefined,+ () => undefined,+ );+ await flush();++ answerStop?.();+ for (let i = 0; i < ticks; i++) {+ await Promise.resolve();+ }+ const { status, error, messages } = h.chat;+ h.chat.dispose();+ await flush();++ expect(h.chat.status).toBe(status);+ expect(h.chat.error).toBe(error);+ expect(h.chat.messages).toBe(messages);+ },+ );++ /**+ * The reconnect opens a newer generation up to four microtasks after the answer, while the merge+ * of its run messages waits its turn; later than that, the stop has already ended the request.+ */+ it.each([0, 1, 2, 3, 4])(+ '(1) a stop answer with run messages leaves a generation its reattachment opened %i microtasks later running',+ async (ticks) => {+ const h = createHarness();+ await states.detached(h);+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementation(+ () =>+ new Promise((resolve) => {+ answerStop = () =>+ resolve({+ final: true,+ conversation: {},+ runMessages: [userMessage],+ } as unknown as ChatAbortResult);+ }),+ );+ const other = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ let openOther: (() => void) | undefined;+ h.transport.reconnectToStream.mockImplementation(+ () => new Promise((resolve) => (openOther = () => resolve(other))),+ );+ h.chat.resumeStream({ streamId: 'convo-1' }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ h.chat.stop().then(+ () => undefined,+ () => undefined,+ );+ await flush();++ answerStop?.();+ for (let i = 0; i < ticks; i++) {+ await Promise.resolve();+ }+ openOther?.();+ await flush();+ other.push(stepEvent);+ await flush();++ expect(other.closed()).toBe(false);+ expect(h.chat.status).toBe('streaming');+ expect(h.chat.connection).toBe('live');},);});-describe('Chat stops while a lost start is owed', () => {- it('(2) an Assistants stop answer without a conversation still resolves the stop', async () => {- const h = createHarness();- await states.attached(h);- h.transport.abort.mockResolvedValueOnce({- final: true,- runMessages: [],- } as unknown as ChatAbortResult);-- await expect(h.chat.stop()).resolves.toEqual({ status: 'stopped' });- });-});-/*** Every way a stop ends a request, and whether the host is asked to reload (`isSettled`). The rule:* a stop the server confirmed reloads whenever no terminal frame delivered what it saved; a frame@@ -1080,6 +1917,61 @@ const stopExits: Array<[string, (h: Harness) => Promise<void>, StopExit]> = [},{ last: settled, outcome: 'ok' },],+ [+ 'reattaching: the stop is confirmed before the stream opens',+ async (h) => {+ await states.reattaching(h);+ await events.stop(h);+ },+ { last: stoppedUnframed, outcome: 'ok' },+ ],+ [+ 'empty reconnect: the reconnect finds nothing after the stop was confirmed',+ async (h) => {+ await states.detached(h);+ let opened: ((stream: null) => void) | undefined;+ h.transport.reconnectToStream.mockImplementation(+ () =>+ new Promise((resolve) => {+ opened = resolve;+ }),+ );+ let answer: (() => void) | undefined;+ h.transport.abort.mockImplementation(+ () =>+ new Promise((resolve) => {+ answer = () => resolve({ success: true });+ }),+ );+ const resumed = h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ await flush();+ const stopping = record(h, h.chat.stop());+ await flush();+ opened?.(null);+ await flush();+ answer?.();+ await stopping;+ await resumed.catch(() => undefined);+ await flush();+ },+ { last: stoppedUnframed, outcome: 'ok' },+ ],+ [+ 'detached: the dropped generation is stopped',+ async (h) => {+ await states.detached(h);+ await events.stop(h);+ },+ { last: stoppedUnframed, outcome: 'ok' },+ ],+ [+ 'detached: the dropped generation had already settled',+ async (h) => {+ await states.detached(h);+ await events.settledStop(h);+ },+ { last: settled, outcome: 'ok' },+ ],['never answered: the start lost its response, so the stop cannot name it',async (h) => {@@ -1132,6 +2024,46 @@ describe('Chat stop exits and the reload signal', () => {});});+/** Table 2: every path a lost start can take, and what a stop answers after it. */+describe('Chat lost-start resolution', () => {+ const lose = async (h: Harness) => {+ h.transport.sendMessages.mockRejectedValueOnce(networkError());+ await h.chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ };++ const rows: Array<[string, string, (h: Harness) => Promise<void>, ChatStopResult['status']]> = [+ [+ 'a reconnect the server answers for its conversation',+ 'convo-1',+ async (h) => {+ h.transport.reconnectToStream.mockResolvedValueOnce(null);+ await h.chat.resumeStream({ streamId: 'convo-1' });+ await flush();+ },+ 'idle',+ ],+ [+ 'a reconnect while it was sent under `new`',+ 'new',+ async (h) => {+ h.transport.reconnectToStream.mockResolvedValueOnce(null);+ await h.chat.resumeStream({ streamId: 'convo-9' });+ await flush();+ },+ 'unaddressable',+ ],+ ['the chat is disposed', 'convo-1', async (h) => h.chat.dispose(), 'idle'],+ ];++ it.each(rows)('after %s', async (_path, id, drive, expected) => {+ const h = createHarness({ id });+ await lose(h);+ await drive(h);++ expect((await h.chat.stop()).status).toBe(expected);+ });+});+/*** Table 3: every place the core notifies subscribers. The rule: a request installs its ownership* (the active record, or the pause it is answering) before any notify, and an entry point that@@ -1167,6 +2099,25 @@ describe('Chat notify ordering and request ownership', () => {expect(h.transport.sendMessages).toHaveBeenCalledTimes(1);});+ it('a reattachment yields to a turn the host starts while it selects the endpoint', async () => {+ let started = false;+ const h = createHarness({+ resumeSelection: () => {+ if (!started) {+ started = true;+ send(h);+ }+ return { endpoint: 'agents' };+ },+ });+ attach(h, createStream({ streamId: 'convo-1', generationCreatedAt: 100 }));+ await h.chat.resumeStream({ streamId: 'convo-1' });+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ expect(h.transport.sendMessages).toHaveBeenCalledTimes(1);+ });+it('a streamed frame notifies while the request still owns the chat', async () => {const h = createHarness();attach(h, createStream({ streamId: 'convo-1', generationCreatedAt: 100 }));@@ -1196,6 +2147,20 @@ describe('Chat notify ordering and request ownership', () => {expect(h.transport.sendMessages).toHaveBeenCalledTimes(2);});+ it('a stop of a dropped generation notifies after it let the chat go, so the next turn owns it', async () => {+ const h = createHarness();+ await states.detached(h);+ const next = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ h.transport.sendMessages.mockResolvedValueOnce({ status: 'stream', stream: next });+ const { attempt, unsubscribe } = startOnNotify(h, () => h.chat.status === 'ready');+ await h.chat.stop();+ await flush();+ unsubscribe();++ expect(attempt.error).toBeUndefined();+ expect(h.chat.status).toBe('submitted');+ });+it('a dispose notifies after the chat is disposed, so a turn a subscriber starts is refused', async () => {const h = createHarness();await states.attached(h);@@ -1207,9 +2172,29 @@ describe('Chat notify ordering and request ownership', () => {expect(attempt.error ?? 'none').not.toBe('none');expect(h.transport.sendMessages).toHaveBeenCalledTimes(1);});++ it('leaves the chat to a turn the host starts while it selects the endpoint of a reattachment', async () => {+ let started: Promise<void> | undefined;+ const h = createHarness({+ resumeSelection: () => {+ started ??= h.chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ return { endpoint: 'agents' };+ },+ });+ attach(h, createStream({ streamId: 'convo-1', generationCreatedAt: 100 }));++ await h.chat.resumeStream({ streamId: 'convo-1' });+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ expect(h.transport.sendMessages).toHaveBeenCalledTimes(1);+ h.stream?.push(finalEvent());+ await started;+ expect(h.finishes).toHaveLength(1);+ });});-const learns: Array<[string, (h: Harness) => Promise<void>]> = [+const liveLearns: Array<[string, (h: Harness) => Promise<void>]> = [['a sync frame',async (h) => {@@ -1254,8 +2239,8 @@ const learns: Array<[string, (h: Harness) => Promise<void>]> = [],];-describe('Chat conversation identity', () => {- it.each(learns)(+describe('Chat conversation identity on a live turn', () => {+ it.each(liveLearns)('(7) after %s names the conversation, later turns and stops use it',async (_label, learn) => {const prepared: string[] = [];@@ -1288,7 +2273,7 @@ describe('Chat conversation identity', () => {);});-describe('Chat identity every frame carries', () => {+describe('Chat identity every frame carries on a live turn', () => {const ids = { conversationId: createdUnder, responseMessageId: 'response-9' };const frames: Array<[string, ChatEvent, { response?: boolean; conversation?: boolean }]> = [[@@ -1380,7 +2365,7 @@ describe('Chat identity every frame carries', () => {);});-describe('Chat disposed while a confirmed stop ends its request', () => {+describe('Chat disposed while a confirmed stop of a live turn ends its request', () => {it.each([0, 1, 2, 3, 4, 6, 8, 12])('(3) a chat disposed %i microtasks into the Assistants answer to a stop of a live turn writes no state',async (ticks) => {@@ -1486,3 +2471,294 @@ describe('Chat replacements and the notifications their rollback sends', () => {);});});++describe('Chat reattachment endpoints', () => {+ it('a reattachment to a dropped generation keeps its endpoint when the host selection changed', async () => {+ let endpoint = 'agents';+ const h = createHarness({ resumeSelection: () => ({ endpoint }) });+ await states.detached(h);+ endpoint = 'assistants';+ h.transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ h.transport.abort.mockResolvedValueOnce({ success: true, aborted: 'convo-1' });+ await h.chat.stop();++ expect(h.transport.abort).toHaveBeenLastCalledWith(+ expect.objectContaining({ endpoint: 'agents' }),+ );+ });++ it('a reattachment to a generation this chat did not start uses the host selection', async () => {+ const h = createHarness({ resumeSelection: () => ({ endpoint: 'assistants' }) });+ h.transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ h.transport.abort.mockResolvedValueOnce({ success: true, aborted: 'convo-1' });+ await h.chat.stop();++ expect(h.transport.abort).toHaveBeenLastCalledWith(+ expect.objectContaining({ endpoint: 'assistants' }),+ );+ });+});++describe('Chat dropped generations a retry, a stop or a status read settles', () => {+ it('a stop a subscriber sends while a retry continuing the dropped generation is undone reaches it', async () => {+ const h = createHarness();+ await states.detached(h);+ const retry = createStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ let answer: ((result: ChatSendResult) => void) | undefined;+ h.transport.sendMessages.mockImplementationOnce(+ () =>+ new Promise<ChatSendResult>((resolve) => {+ answer = resolve;+ }),+ );+ const sent = h.chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();+ h.stream = retry;+ let stopping: Promise<ChatStopResult> | undefined;+ const unsubscribe = h.chat.subscribe(() => {+ stopping ??= h.chat.stop();+ });+ answer?.({ status: 'stream', stream: retry });+ await flush();+ unsubscribe();+ await sent;++ expect(stopping).toBeDefined();+ expect((await stopping)?.status).toBe('stopped');+ expect(h.transport.abort).toHaveBeenLastCalledWith(+ expect.objectContaining({ streamId: 'convo-1', generationCreatedAt: 100 }),+ );+ });++ it('a retry the server settles keeps the dropped generation in the stale-read history', async () => {+ const h = createHarness();+ const stream = createStream({ streamId: 'convo-1', generationCreatedAt: 300 });+ attach(h, stream);+ send(h);+ await flush();+ stream.push(stepEvent);+ await flush();+ stream.push(dropEvent);+ await flush();+ h.transport.sendMessages.mockResolvedValueOnce({+ status: 'settled',+ conversationId: 'convo-1',+ generationProtocolVersion: 2,+ });+ await h.chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });+ await flush();+ const finishes = h.finishes.length;+ h.transport.reconnectToStream.mockClear();+ h.transport.reconnectToStream.mockResolvedValueOnce(null);+ await h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 200 });+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ expect(h.finishes).toHaveLength(finishes);+ });++ it('(7) a stop the server finds gone reports the conversation the dropped generation named', async () => {+ const h = createHarness({+ id: 'new',+ prepareRequest: () => ({+ payload: { ...payload, conversationId: null } as unknown as TPayload,+ messages: [userMessage, placeholder],+ responseMessageId: 'response-1',+ }),+ });+ const stream = createStream({ streamId: createdUnder, generationCreatedAt: 100 });+ attach(h, stream);+ send(h);+ await flush();+ stream.push({+ type: 'sync',+ data: { conversationId: createdUnder, responseMessage: placeholder },+ } as ChatEvent);+ await flush();+ stream.push(dropEvent);+ await flush();+ h.transport.abort.mockRejectedValueOnce(+ Object.assign(new Error('gone'), { response: { status: 404 } }),+ );+ await h.chat.stop();++ expect(h.finishes[h.finishes.length - 1]).toMatchObject({+ isSettled: true,+ conversationId: createdUnder,+ });+ });++ it('a stop that outlives a failed reconnect reports what the server saved, and leaves nothing to stop', async () => {+ const h = createHarness();+ await states.reattaching(h);+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answerStop = () =>+ resolve({ success: true, aborted: 'convo-1', persistenceFailed: true });+ }),+ );+ const stopping = h.chat.stop();+ await flush();+ h.fail?.();+ await flush();+ answerStop?.();+ await stopping;+ await flush();++ expect(h.finishes[h.finishes.length - 1]).toMatchObject({+ reconcileReason: 'abort_persistence_failed',+ });+ h.transport.abort.mockClear();+ expect((await h.chat.stop()).status).toBe('idle');+ expect(h.transport.abort).not.toHaveBeenCalled();+ });+});++/** A store that reports each field it is written, as a cache another hook also writes does. */+const createReportingState = (): ChatState => {+ const listeners = new Set<() => void>();+ const tell = () => listeners.forEach((listener) => listener());+ let messages: TMessage[] = [];+ let status: ChatStatus = 'ready';+ let error: Error | undefined;+ let connection: ChatConnection = 'idle';+ return {+ get status() {+ return status;+ },+ set status(next) {+ status = next;+ tell();+ },+ get error() {+ return error;+ },+ set error(next) {+ error = next;+ tell();+ },+ get connection() {+ return connection;+ },+ set connection(next) {+ connection = next;+ tell();+ },+ get messages() {+ return messages;+ },+ replaceMessages(next) {+ messages = next;+ tell();+ },+ subscribe(listener) {+ listeners.add(listener);+ return () => listeners.delete(listener);+ },+ };+};++describe('Chat reconnects that settle a dropped generation or a lost start', () => {+ it('a stop of a dropped generation still ends it when a reattachment to it fails before the answer', async () => {+ const h = createHarness();+ await states.detached(h);+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answerStop = () =>+ resolve({ success: true, aborted: 'convo-1', persistenceFailed: true });+ }),+ );+ const stopping = h.chat.stop();+ await flush();+ h.transport.reconnectToStream.mockRejectedValueOnce(networkError());+ await h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 100 });+ await flush();+ answerStop?.();+ await stopping;+ await flush();++ expect(h.finishes[h.finishes.length - 1]).toMatchObject({+ reconcileReason: 'abort_persistence_failed',+ });+ expect(h.chat.status).not.toBe('error');+ h.transport.abort.mockClear();+ expect((await h.chat.stop()).status).toBe('idle');+ expect(h.transport.abort).not.toHaveBeenCalled();+ });++ it('an empty reconnect lets go of the chat before a state that reports its writes is told', async () => {+ const state = createReportingState();+ const h = createHarness({ state });+ h.transport.reconnectToStream.mockResolvedValueOnce(null);+ attach(h, createStream({ streamId: 'convo-1', generationCreatedAt: 200 }));+ let reconnecting = false;+ let sent = false;+ let refused: unknown;+ const unsubscribe = h.chat.subscribe(() => {+ reconnecting ||= h.chat.connection === 'reconnecting';+ if (reconnecting && h.chat.connection === 'idle' && !sent) {+ sent = true;+ h.chat.sendMessage({ parts: [{ type: 'text', text: 'Next' }] }).catch((error: unknown) => {+ refused = error;+ });+ }+ });+ await h.chat.resumeStream();+ await flush();+ unsubscribe();++ expect(sent).toBe(true);+ expect(refused).toBeUndefined();+ expect(h.transport.sendMessages).toHaveBeenCalledTimes(1);+ });++ it('an unnamed reconnect that releases a lost start settles it for the host to reconcile', async () => {+ const h = createHarness();+ h.transport.sendMessages.mockRejectedValueOnce(networkError());+ await h.chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });+ expect(h.chat.status).toBe('error');+ const finishes = h.finishes.length;+ h.transport.reconnectToStream.mockResolvedValueOnce(null);+ await h.chat.resumeStream({ streamId: 'convo-1' });+ await flush();++ expect(h.finishes).toHaveLength(finishes + 1);+ expect(h.finishes[h.finishes.length - 1]).toMatchObject({ isSettled: true });+ expect(h.chat.status).toBe('ready');+ expect((await h.chat.stop()).status).toBe('idle');+ });++ it('a stop that ends a dropped generation never shows a store a failed status without its error', async () => {+ const h = createHarness({ state: createReportingState() });+ await states.detached(h);+ expect(h.chat.status).toBe('error');+ let torn = false;+ const unsubscribe = h.chat.subscribe(() => {+ torn ||= h.chat.status === 'error' && h.chat.error === undefined;+ });+ h.transport.abort.mockResolvedValueOnce({+ final: true,+ conversation: {},+ } as unknown as ChatAbortResult);+ await h.chat.stop();+ await flush();+ unsubscribe();++ expect(torn).toBe(false);+ expect(h.chat.status).toBe('ready');+ });+});diff --git a/packages/chat/src/core/chat.ts b/packages/chat/src/core/chat.tsindex ceb9dd6c2a..dfb883a699 100644--- a/packages/chat/src/core/chat.ts+++ b/packages/chat/src/core/chat.ts@@ -20,6 +20,7 @@ import type {ChatAbortResult,ChatAbortRequest,ChatStreamAbortResult,+ ChatResumeSelection,ChatTransportError,} from './types';import { isRetryableNetworkError } from './resumable/start';@@ -147,6 +148,8 @@ export type ChatInit = {* response messages are merged into the list by id and every other event only moves the status.*/applyEvent?(event: ChatEvent, context: ChatEventContext): void;+ /** The agent selection a generation this chat did not start runs on, which a reattachment reads. */+ resumeSelection?(): Omit<ChatResumeSelection, 'conversationId' | 'generationCreatedAt'>;onError?(error: Error): void;onFinish?(result: ChatFinishResult): void;/** Every event, after it was applied: the side channels (usage, title, attachments, steering). */@@ -171,7 +174,7 @@ type Outcome = {/*** A generation on the server as this chat knows it. It outlives the requests attached to it, so a- * stop and the request it ends read and write one record instead of copying its fields across.+ * drop, a reattachment and a stop read and write one record instead of copying its fields across.*/type Generation = {endpoint?: string;@@ -180,8 +183,8 @@ type Generation = {/** The response this generation produces: the placeholder, then the one a frame names. */responseId?: string;/**- * The generation is not the one this chat prepared (a replacement) and no frame has named its- * response yet.+ * The generation is not the one this chat prepared (a replacement, or a reattachment that found+ * another generation) and no frame has named its response yet.*/responseUnknown?: boolean;/** Set once the server confirmed a stop, so a stream that drops instead of ending aborts. */@@ -221,6 +224,12 @@ type OptimisticWrite = {removed: Set<string>;};+type DetachedGeneration = {+ error: Error;+ target: Omit<ChatAbortRequest, 'chatId'>;+ generation: Generation;+};+type ActiveRequest = {controller: AbortController;/** The optimistic write, undone when the server never got the turn or ran another in its place. */@@ -229,7 +238,7 @@ type ActiveRequest = {/** The generation this request runs or attaches to. */generation: Generation;/**- * Set while attaching to the generation the server answered the start with, before it attaches,+ * Set when reattaching to a generation already running on the server, before it attaches,* naming the stream a stop addresses meanwhile.*/reattach?: { streamId?: string };@@ -238,7 +247,7 @@ type ActiveRequest = {* attachment unless the server says the generation is gone.*/attached?: boolean;- /** The generation the start was answered with; a stream of another one starts a new record. */+ /** The generation a reattachment continued; a stream of another one starts a new record. */inheritedGeneration?: number;/** The error the request settled with when its connection dropped. */disconnected?: Error;@@ -675,12 +684,17 @@ export class Chat {private readonly listeners = new Set<() => void>();private releaseState?: () => void;private active?: ActiveRequest;+ private generationCreatedAt?: number;private disposed = false;/** Set while the chat writes the state, whose own report of that write the chat announces itself. */private writing = false;private revision = 0;/** Aborted on dispose, which ends every wait on the server so no caller outlives the chat. */private readonly lifetime = new AbortController();+ /** A generation whose stream dropped while the server kept it running. */+ private detached?: DetachedGeneration;+ /** The timestamps of the generations a stop or their own stream ended. */+ private readonly endedGenerations = new Set<number>();/** The newest start handed to the transport, whose conversation a late answer must not replace. */private latestStart?: ActiveRequest;/**@@ -690,7 +704,7 @@ export class Chat {private lostStart?: ActiveRequest;/*** The conversation this chat runs under: its id, until the server names the one it created for- * a chat started under `new`. Every later start and stop addresses that one.+ * a chat started under `new`. Every later start, stop and reattachment addresses that one.*/private conversation: string;@@ -760,6 +774,125 @@ export class Chat {return this.request({ trigger: 'continue-message', messageId }, requestOptions);}+ /**+ * Reattaches to the generation the server is still running. The status stays as it is until a+ * stream is returned, so a chat with nothing running never flashes `submitted`.+ */+ async resumeStream(+ options: { streamId?: string; generationCreatedAt?: number } & ChatRequestOptions = {},+ ): Promise<void> {+ if (this.active || this.disposed) {+ return;+ }+ if (options.generationCreatedAt != null && this.isStaleRead(options.generationCreatedAt)) {+ /** A status read that named an older generation than this chat holds changes nothing. */+ return;+ }+ /** The host's selection is read before any marker changes, so a throw leaves them all as they were. */+ const selected = this.resumeEndpoint();+ if (this.active || this.disposed) {+ /** The host's selection may have started a turn or disposed the chat, which then owns it. */+ return;+ }+ /** A turn whose stream dropped: finding nothing running means it ended without this chat. */+ const dropped = this.detached;+ this.detached = undefined;+ /**+ * Reattaching to a dropped turn's own generation continues its record, so the turn's identity+ * and a stop still in flight for it carry over.+ */+ const continued =+ options.generationCreatedAt == null ||+ options.generationCreatedAt === dropped?.target.generationCreatedAt+ ? dropped+ : undefined;+ const active = createActiveRequest(continued?.generation);+ const { generation } = active;+ if (!continued && generation.responseId === undefined) {+ /**+ * A reattachment this chat did not prepare has no response until a frame names one. A+ * continued local turn keeps the list-tail fallback its own request had.+ */+ generation.responseUnknown = true;+ }+ /** A continued generation runs where it started; the host's selection names any other. */+ generation.endpoint = continued?.target.endpoint ?? selected ?? dropped?.target.endpoint;+ /** The generation the caller named, or the record continued, so a stream of another is told apart. */+ active.inheritedGeneration =+ options.generationCreatedAt ?? continued?.target.generationCreatedAt;+ active.reattach = { streamId: options.streamId ?? continued?.target.streamId };+ active.attached = true;+ this.active = active;+ /** Only the generation this continues vouches for a timestamp, so a stop made now addresses it. */+ this.generationCreatedAt = options.generationCreatedAt ?? active.inheritedGeneration;+ const conversationId = generation.conversationId ?? this.conversationId;+ try {+ this.setConnection('reconnecting');+ if (active.controller.signal.aborted) {+ /** A listener of that notification stopped or disposed the chat; nothing is opened. */+ throw active.controller.signal.reason;+ }+ const stream = await openUntilAborted(+ this.init.transport.reconnectToStream({+ ...options,+ chatId: conversationId,+ resume: true,+ abortSignal: active.controller.signal,+ }),+ active.controller.signal,+ (opened) => opened?.close(),+ );+ /** The server answered for the conversation, so a lost start in it is no longer the caller's. */+ const released = this.releaseLostStart();+ if (!stream && generation.stopAnswered) {+ /** A stop still in flight decides how an empty reconnect ended. */+ await generation.stopAnswered;+ }+ if (!this.owns(active)) {+ return;+ }+ if (!stream && generation.stopped) {+ this.settle(getStopOutcome(generation), active);+ return;+ }+ /**+ * A caller that named the generation, as a status read does, knew it running; an unnamed+ * probe finding nothing only means nothing runs. A generation already seen ending is done.+ */+ const named =+ options.generationCreatedAt != null &&+ !this.endedGenerations.has(options.generationCreatedAt);+ /** A lost start the answer released may have finished on the server, which the host reloads. */+ if (!stream && (named || dropped != null || released)) {+ this.settle({ kind: 'complete', settled: { conversationId } }, active);+ return;+ }+ if (!stream) {+ /**+ * Nothing is running and nothing said it was. The chat is let go first and the state+ * published as one change, so a subscriber a store tells of it can start the next turn.+ */+ this.release(active);+ this.writing = true;+ try {+ if (this.status === 'submitted' || this.status === 'streaming') {+ this.init.state.status = 'ready';+ }+ this.init.state.connection = 'idle';+ } finally {+ this.writing = false;+ }+ this.notify();+ return;+ }+ await this.consume(stream, active);+ } catch (error) {+ this.settle(this.toOutcome(error, active), active);+ } finally {+ this.release(active);+ }+ }+/*** Stops the turn in flight. A request still starting is cancelled; an attached stream is* stopped on the server, and this resolves once the stream reported its end. Rejects when the@@ -773,11 +906,15 @@ export class Chat {return { status: 'idle' };}const active = this.active;+ if (!active && this.detached) {+ return this.orUnaddressed(await this.stopDetached(this.detached));+ }if (!active) {/** The lost start is still the caller's. */return this.lostStart ? unaddressable : { status: 'idle' };}if (!active.stream && !active.reattach) {+ const { detached } = this;active.controller.abort();await active.finished;/**@@ -786,6 +923,14 @@ export class Chat {*/const own: ChatStopResult =active.sent && !active.resolved ? unaddressable : { status: 'stopped' };+ if (detached && this.detached === detached) {+ /**+ * The cancelled retry never took over the dropped generation, which this stop also ends,+ * even when a turn its completion callback started is preparing meanwhile.+ */+ const result = await this.stopDetached(detached);+ return active.sent ? own : this.orUnaddressed(result);+ }return this.orUnaddressed(own);}const { generation } = active;@@ -796,7 +941,10 @@ export class Chat {const target = {endpoint: generation.endpoint,streamId,- generationCreatedAt: active.stream?.generationCreatedAt ?? active.inheritedGeneration,+ generationCreatedAt:+ active.stream?.generationCreatedAt ??+ this.generationCreatedAt ??+ active.inheritedGeneration,abortKey: streamId == null ? this.abortKey(generation) : undefined,};if (!this.isAddressable(target)) {@@ -827,6 +975,9 @@ export class Chat {const since = this.latestStart;const answer = this.abortOnServer(target).then((result) => {generation.stopped = true;+ if (target.generationCreatedAt != null) {+ this.endedGenerations.add(target.generationCreatedAt);+ }this.adoptStopAnswer(generation, result, since);generation.settledAs = getSettledAs(result);if (!generation.settledAs && isStreamAbort(result) && result.persistenceFailed) {@@ -861,7 +1012,7 @@ export class Chat {if (result && 'runMessages' in result) {await this.mergeRunMessages(result.runMessages, () => this.active === active && owns());if (!owns()) {- /** The request opened another generation while the merge waited its turn. */+ /** The reattachment opened another generation while the merge waited its turn. */return getStopResult(result, generation.stoppedConversationId);}active.controller.abort();@@ -875,7 +1026,7 @@ export class Chat {active.stream?.close();} else if (!active.stream && !generation.persistenceFailed) {/**- * Still attaching: nothing will report the end, so the stop ends the request here. An+ * Still reattaching: nothing will report the end, so the stop ends the request here. An* unsaved stop stays attached for the reconcile frame that reports it.*/active.controller.abort();@@ -885,6 +1036,17 @@ export class Chat {/** A chat disposed while the stop ended its request writes no state. */return getStopResult(result, generation.stoppedConversationId);}+ if (!this.active && this.detached?.generation === generation) {+ /** The request dropped before the answer arrived, so the answer ends what it left running. */+ this.detached = undefined;+ if (this.publishUnframedStop(result, generation)) {+ return getStopResult(result, generation.stoppedConversationId);+ }+ }+ /** The server confirmed the stop, so this generation is over however its stream ended. */+ if (active.disconnected && this.error === active.disconnected) {+ this.clearFailure();+ }this.notify();return getStopResult(result, generation.stoppedConversationId);}@@ -927,6 +1089,10 @@ export class Chat {}void this.abortOnServer(target).then((result) => {+ /** Like any confirmed stop, so a late status read of its generation is ignored. */+ if (generationCreatedAt != null) {+ this.endedGenerations.add(generationCreatedAt);+ }/** The answer names the conversation the start created, unless a newer start owns the chat. */this.adoptStopAnswer(active.generation, result, active);this.resolveLostStart(active);@@ -977,11 +1143,24 @@ export class Chat {if (this.disposed || this.active || !failed || this.error !== failed) {return;}- this.init.state.error = undefined;- this.init.state.status = 'ready';+ this.clearFailure();this.notify();}+ /**+ * Clears a lost start the server's answer for its conversation covers: one generation runs per+ * conversation, so the answer owns or superseded it. A start sent under `new` created a+ * conversation of its own, which no other answer covers, so it stays the caller's to stop.+ * Returns whether it released one.+ */+ private releaseLostStart(): boolean {+ if (!this.lostStart || this.lostStart.sentUnder === Constants.NEW_CONVO) {+ return false;+ }+ this.lostStart = undefined;+ return true;+ }+/*** Whether a start sent after `since` is still running or lost, so it may own a conversation of its own* that a late answer about an older generation must not replace.@@ -1071,6 +1250,97 @@ export class Chat {return result;}+ /** Stops a generation whose stream dropped, which the server keeps running. */+ private async stopDetached(detached: DetachedGeneration): Promise<ChatStopResult> {+ if (!this.isAddressable(detached.target)) {+ return unaddressable;+ }+ const { generation } = detached;+ const result = await this.stopGeneration(generation, detached.target);+ const active = this.active;+ if (!this.disposed && active?.generation === generation) {+ /** A reattachment took the generation over while the stop was in flight; it ends with it. */+ return this.endStopped(active, generation, result);+ }+ /**+ * The dropped generation owns the chat only while no newer request does, such as a retry. A+ * reattachment to it that failed drops it again under a new record of the same generation.+ */+ const owns = () => !this.disposed && this.detached?.generation === generation && !this.active;+ if (!owns()) {+ return getStopResult(result, generation.stoppedConversationId);+ }+ if (result && 'runMessages' in result) {+ await this.mergeRunMessages(result.runMessages, owns);+ if (!owns()) {+ /** A newer generation took over while the merge waited its turn. */+ return getStopResult(result, generation.stoppedConversationId);+ }+ }+ const failed = this.detached?.error ?? detached.error;+ this.detached = undefined;+ if (this.publishUnframedStop(result, generation)) {+ return getStopResult(result, generation.stoppedConversationId);+ }+ if (this.error === failed) {+ this.clearFailure();+ }+ this.notify();+ return getStopResult(result, generation.stoppedConversationId);+ }++ /**+ * Reports a stop whose end no attached stream will frame: one the run had already settled, one+ * the server could not save, or one it stopped and saved, whose abort frame no stream is left to+ * carry. The host reloads what the server kept. Returns whether it did.+ */+ private publishUnframedStop(result: ChatAbortResult, generation: Generation): boolean {+ const settledAs = getSettledAs(result);+ const persistenceFailed = isStreamAbort(result) && result.persistenceFailed;+ const stopFramed = isStreamAbort(result) && !settledAs && !persistenceFailed;+ if (!settledAs && !persistenceFailed && !stopFramed) {+ return false;+ }+ const outcome = getStopOutcome({+ settledAs,+ persistenceFailed,+ stopFramed,+ stoppedConversationId: generation.stoppedConversationId,+ });+ /** An answer that names no stream leaves the conversation the generation's frames named. */+ const { conversationId } = generation;+ const named = conversationId !== Constants.NEW_CONVO ? conversationId : undefined;+ /** A lost start still owed its own abort keeps the failure it reported. */+ const owed = this.lostStart && this.error ? this.error : undefined;+ this.publish(+ { ...outcome, conversationId: named },+ generation.responseId,+ generation.responseUnknown,+ false,+ owed,+ );+ return true;+ }++ /** The endpoint a generation this chat did not start runs on, as the host selects it. */+ private resumeEndpoint(): string | undefined {+ return this.init.resumeSelection?.().endpoint ?? undefined;+ }++ /**+ * Returns a failed chat to `ready` as one write, which the caller announces, so a store that+ * reports each field never shows a failed status without its error.+ */+ private clearFailure(): void {+ this.writing = true;+ try {+ this.init.state.error = undefined;+ this.init.state.status = 'ready';+ } finally {+ this.writing = false;+ }+ }+/** Returns a failed chat to `ready`. AI SDK: `clearError`. */clearError(): void {if (this.status !== 'error') {@@ -1111,7 +1381,7 @@ export class Chat {active?.controller.abort();active?.stream?.close();const { state } = this.init;- const unsent = active != null && !active.sent;+ const unsent = active != null && !active.sent && !active.reattach;const rollback = unsent ? active.optimistic : undefined;const status = unsent && state.status === 'submitted' ? 'ready' : state.status;if (rollback || status !== state.status || state.connection !== 'idle') {@@ -1220,29 +1490,79 @@ export class Chat {if (result.status !== 'stream') {this.learnConversation(result.conversationId);}- if (this.lostStart?.sentUnder !== Constants.NEW_CONVO) {+ /**+ * The server took the start, so it owns or superseded a generation whose stream dropped.+ * Until then that generation stays the stop target, in case the start fails.+ */+ const dropped = this.detached;+ this.detached = undefined;+ /** Likewise a lost start stays the caller's to stop until the server takes one in its conversation. */+ this.releaseLostStart();+ let answered: number | undefined;+ if (result.status === 'stream') {+ answered = result.stream.generationCreatedAt;+ } else if (result.status === 'replaced') {+ answered = result.generationCreatedAt ?? undefined;+ }+ if (answered == null || answered !== dropped?.target.generationCreatedAt) {/**- * The server took a start in the lost start's conversation, where one generation runs, so it- * owns or superseded it. One sent under `new` created a conversation no other start covers.+ * A start the server attached to the dropped generation continues it; any other supersedes+ * it, which stays in the stale-read history so a late read of it changes nothing.*/- this.lostStart = undefined;+ for (const superseded of [this.generationCreatedAt, dropped?.target.generationCreatedAt]) {+ if (superseded != null) {+ this.endedGenerations.add(superseded);+ }+ }+ this.generationCreatedAt = undefined;}let stream: ChatStream | null = null;if (result.status === 'stream') {stream = result.stream;/** A start the server attached to a generation already running is an attachment too. */active.attached = result.stream.resumed === true;+ const { generationCreatedAt } = stream;+ if (+ generationCreatedAt != null &&+ generationCreatedAt === dropped?.target.generationCreatedAt+ ) {+ /**+ * The server attached the start to the dropped generation, whose record it continues. The+ * stream is installed before the rollback notifies, so a stop made then addresses it.+ */+ active.generation = dropped.generation;+ active.inheritedGeneration = generationCreatedAt;+ active.stream = stream;+ const rollback = active.optimistic;+ if (rollback) {+ /** That generation is not this turn, so this turn's optimistic write is undone. */+ await this.executor.run(() => {+ this.write(undoOptimisticWrite(rollback, this.messages));+ });+ }+ }} else if (result.status === 'replaced') {+ const generationCreatedAt = result.generationCreatedAt ?? undefined;+ /** The target is recorded before anyone hears of the reconnect, so a stop can reach it. */+ this.generationCreatedAt = generationCreatedAt;/*** The replacement it names, so a stream of yet another generation gets its own record.* Recorded before the rollback notifies, so a stop made then addresses the replacement.*/- active.inheritedGeneration = result.generationCreatedAt ?? undefined;+ active.inheritedGeneration = generationCreatedAt;active.reattach = { streamId: result.streamId };active.attached = true;- /** The replacement's response is not this turn's placeholder; its first frame names it. */- active.generation.responseId = undefined;- active.generation.responseUnknown = true;+ if (+ generationCreatedAt != null &&+ generationCreatedAt === dropped?.target.generationCreatedAt+ ) {+ /** The dropped generation itself: its record carries the stop still in flight for it. */+ active.generation = dropped.generation;+ } else {+ /** The replacement's response is not this turn's placeholder; its first frame names it. */+ active.generation.responseId = undefined;+ active.generation.responseUnknown = true;+ }active.generation.conversationId = result.conversationId;const rollback = active.optimistic;if (rollback) {@@ -1302,18 +1622,22 @@ export class Chat {return;}active.stream = stream;+ this.detached = undefined;if (active.inheritedGeneration != null &&stream.generationCreatedAt != null &&stream.generationCreatedAt !== active.inheritedGeneration) {/**- * The attachment found another generation, whose response no frame has named yet. It gets- * its own record, so a stop of the one it was answered with does not end it.+ * The reattachment found another generation, whose response no frame has named yet. It gets+ * its own record, so a stop of the one it continued does not end it.*/const { endpoint, conversationId } = active.generation;active.generation = { endpoint, conversationId, responseUnknown: true };}+ if (stream.generationCreatedAt != null) {+ this.generationCreatedAt = stream.generationCreatedAt;+ }let outcome: Outcome | undefined;/** The event this chat is applying, whose failure is the chat's, not the generation's. */let handling: ChatEvent | undefined;@@ -1454,7 +1778,27 @@ export class Chat {}if (outcome.kind === 'error' && outcome.disconnect) {active.disconnected = outcome.error;- if (active.sent && !active.stream && !active.reattach && !active.resolved) {+ const streamId = active.stream?.streamId ?? active.reattach?.streamId;+ if (+ (active.stream || active.reattach) &&+ active.generation.endpoint &&+ !active.generation.stopped+ ) {+ /** The server keeps the generation running, so a later stop still reaches it. */+ this.detached = {+ error: outcome.error,+ target: {+ endpoint: active.generation.endpoint,+ streamId,+ generationCreatedAt:+ active.stream?.generationCreatedAt ??+ this.generationCreatedAt ??+ active.inheritedGeneration,+ abortKey: streamId == null ? this.abortKey(active.generation) : undefined,+ },+ generation: active.generation,+ };+ } else if (active.sent && !active.stream && !active.reattach && !active.resolved) {/** The start may have reached the server, which answered nothing this chat can address. */this.lostStart = active;}@@ -1464,6 +1808,16 @@ export class Chat {* that starts the next turn is not overwritten by this one's terminal state.*/this.active = undefined;+ if (outcome.kind !== 'error' || !outcome.disconnect) {+ /** Including an attachment that found its generation already finished. */+ const ended =+ active.stream?.generationCreatedAt ??+ active.inheritedGeneration ??+ (active.reattach ? this.generationCreatedAt : undefined);+ if ((active.stream || active.reattach) && ended != null) {+ this.endedGenerations.add(ended);+ }+ }const { responseId, responseUnknown, responseless, conversationId } = active.generation;const named = conversationId !== Constants.NEW_CONVO ? conversationId : undefined;/** A lost start still owed its own abort keeps the failure it reported. */@@ -1493,6 +1847,23 @@ export class Chat {}}+ /**+ * Whether a status read names a generation this chat already knows is over or superseded: one+ * that ended, or one older than any generation the chat has seen, including one that since ended.+ */+ private isStaleRead(generationCreatedAt: number): boolean {+ if (this.endedGenerations.has(generationCreatedAt)) {+ return true;+ }+ /** Every generation this chat saw, running or over, so an older read stays stale after it ends. */+ const seen = [+ ...this.endedGenerations,+ this.detached?.target.generationCreatedAt,+ this.generationCreatedAt,+ ];+ return seen.some((known) => known != null && generationCreatedAt < known);+ }+/** Adopts the conversation the server created for a chat started under `new`; a real id stays. */private learnConversation(conversationId: string | null | undefined): void {if (@@ -1560,6 +1931,11 @@ export class Chat {});}+ /** Whether `active` still holds this chat, which an async continuation checks before acting. */+ private owns(active: ActiveRequest): boolean {+ return !this.disposed && this.active === active;+ }+private release(active: ActiveRequest): void {if (this.active === active) {this.active = undefined;