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
🎟️ fix: End a Pause Answer's Claim Only When Its Generation Ends (#17636)
Marco Beretta859e9dddbbaf
diff --git a/packages/chat/src/core/__tests__/generations.spec.ts b/packages/chat/src/core/__tests__/generations.spec.tsindex f024e4cfd5..6347b8e83c 100644--- a/packages/chat/src/core/__tests__/generations.spec.ts+++ b/packages/chat/src/core/__tests__/generations.spec.ts@@ -4581,3 +4581,417 @@ describe('Chat pauses a sync frame carries or releases while an answer is in fliexpect(h.chat.pendingAction).toBeUndefined();});});++/**+ * Table 4: what may end the claim of an answer in flight. Only the end of its generation or a newer+ * pause does: a confirmed stop, a terminal frame, a pause that replaced it. Expiry, a snapshot that+ * releases the pause and a stale replay only hide it, so an accepted answer still follows its+ * continuation.+ */+describe('Chat answers in flight and what ends their claim', () => {+ const expired = { ...askAction, expiresAt: 1 } as Agents.PendingAction;+ const newer = { ...askAction, actionId: 'action-2', createdAt: 2 } as Agents.PendingAction;+ const reattached = async (h: Harness) => {+ const stream = createStream({ streamId: 'convo-1', generationCreatedAt: 500 });+ h.transport.reconnectToStream.mockResolvedValueOnce(stream);+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 500 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ return stream;+ };+ const dropped = async (h: Harness, stream: TestStream) => {+ stream.push(dropEvent);+ await flush();+ };++ const rows: Array<[string, (h: Harness) => Promise<void>, boolean]> = [+ [+ 'a reattached stream replays the pause after its expiry',+ async (h) => {+ const stream = await reattached(h);+ stream.push({ type: 'pending_action', data: expired } as unknown as ChatEvent);+ await flush();+ await dropped(h, stream);+ },+ true,+ ],+ [+ 'a snapshot carries the pause after its expiry',+ async (h) => {+ const stream = await reattached(h);+ stream.push({+ type: 'sync',+ data: { sync: true, resumeState: { runSteps: [], pendingAction: expired } },+ } as unknown as ChatEvent);+ await flush();+ await dropped(h, stream);+ },+ true,+ ],+ [+ 'a status read restores the pause after its expiry',+ async (h) => {+ h.chat.restorePendingAction(expired, 500);+ await flush();+ },+ true,+ ],+ [+ 'a snapshot releases the pause',+ async (h) => {+ const stream = await reattached(h);+ stream.push({+ type: 'sync',+ data: { sync: true, resumeState: { runSteps: [] } },+ } as unknown as ChatEvent);+ await flush();+ await dropped(h, stream);+ },+ true,+ ],+ [+ 'a snapshot releases it and a status read then reports a later pause that expired',+ async (h) => {+ const stream = await reattached(h);+ stream.push({+ type: 'sync',+ data: { sync: true, resumeState: { runSteps: [] } },+ } as unknown as ChatEvent);+ await flush();+ await dropped(h, stream);+ h.chat.restorePendingAction({ ...newer, expiresAt: 1 } as Agents.PendingAction, 500);+ await flush();+ },+ false,+ ],+ [+ 'a snapshot releases it and the stream then replays a later pause that expired',+ async (h) => {+ const stream = await reattached(h);+ stream.push({+ type: 'sync',+ data: { sync: true, resumeState: { runSteps: [] } },+ } as unknown as ChatEvent);+ await flush();+ stream.push({+ type: 'pending_action',+ data: { ...newer, expiresAt: 1 },+ } as unknown as ChatEvent);+ await flush();+ await dropped(h, stream);+ },+ false,+ ],+ [+ 'a newer pause replaces it',+ async (h) => {+ const stream = await reattached(h);+ stream.push({ type: 'pending_action', data: newer } as unknown as ChatEvent);+ await flush();+ await dropped(h, stream);+ },+ false,+ ],+ [+ 'its generation ends with a final frame',+ async (h) => {+ const stream = await reattached(h);+ stream.push(finalEvent());+ await flush();+ },+ false,+ ],+ [+ 'a stop the server confirms ends it',+ async (h) => {+ h.transport.abort.mockResolvedValueOnce({ success: true, aborted: 'convo-1' });+ await h.chat.stop().catch(() => undefined);+ await flush();+ },+ false,+ ],+ ];++ it.each(rows)('(5) an answer in flight when %s', async (_label, drive, follows) => {+ const h = createHarness();+ h.chat.restorePendingAction(askAction, 500);+ let answer: (() => void) | undefined;+ h.transport.resume.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answer = () =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' });+ }),+ );+ const answering = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ await drive(h);+ h.transport.reconnectToStream.mockClear();+ h.transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ answer?.();+ await answering.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).toHaveBeenCalledTimes(follows ? 1 : 0);+ });++ it('(5) a late stop answer for an earlier generation leaves the answer of a later one its claim', async () => {+ const h = createHarness();+ await states.attached(h);+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answerStop = () => resolve({ success: true, aborted: 'convo-1' });+ }),+ );+ const stopping = h.chat.stop().catch(() => undefined);+ await flush();+ h.stream?.push(finalEvent());+ await flush();+ h.chat.restorePendingAction(askAction, 500);+ let answer: (() => void) | undefined;+ h.transport.resume.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answer = () =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' });+ }),+ );+ const answering = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ answerStop?.();+ await stopping;+ await flush();+ h.transport.reconnectToStream.mockClear();+ h.transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ answer?.();+ await answering.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).toHaveBeenCalledTimes(1);+ });++ it('(5) an answer in flight still blocks a turn after a snapshot released its pause and the stream dropped', async () => {+ const h = createHarness();+ h.chat.restorePendingAction(askAction, 500);+ h.transport.resume.mockImplementationOnce(() => new Promise(() => undefined));+ void h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' }).catch(() => undefined);+ await flush();+ const stream = createStream({ streamId: 'convo-1', generationCreatedAt: 500 });+ h.transport.reconnectToStream.mockResolvedValueOnce(stream);+ h.chat.resumeStream({ streamId: 'convo-1', generationCreatedAt: 500 }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ stream.push({+ type: 'sync',+ data: { sync: true, resumeState: { runSteps: [] } },+ } as unknown as ChatEvent);+ await flush();+ stream.push(dropEvent);+ await flush();+ const before = h.transport.sendMessages.mock.calls.length;++ await expect(h.chat.sendMessage({ parts: [{ type: 'text', text: 'Next' }] })).rejects.toThrow();+ expect(h.transport.sendMessages).toHaveBeenCalledTimes(before);+ });++ it('(5) an answer a newer pause replaced stays ended when that pause is answered too', async () => {+ const h = createHarness();+ h.chat.restorePendingAction(askAction, 500);+ const answers: Array<() => void> = [];+ h.transport.resume.mockImplementation(+ () =>+ new Promise((resolve) => {+ answers.push(() =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' }),+ );+ }),+ );+ const first = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ const newer = { ...askAction, actionId: 'action-2', createdAt: 2 } as Agents.PendingAction;+ h.chat.restorePendingAction(newer, 500);+ void h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'B' }).catch(() => undefined);+ await flush();+ h.transport.reconnectToStream.mockClear();+ answers[0]?.();+ await first.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ expect(h.chat.pendingAction?.actionId).toBe('action-2');+ });++ it('(5) a late stop answer for an undated earlier generation leaves the answer of a later one its claim', async () => {+ const h = createHarness();+ attach(h, createStream({ streamId: 'convo-1' }));+ send(h);+ await flush();+ h.stream?.push(stepEvent);+ await flush();+ let answerStop: (() => void) | undefined;+ h.transport.abort.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answerStop = () => resolve({ success: true, aborted: 'convo-1' });+ }),+ );+ const stopping = h.chat.stop().catch(() => undefined);+ await flush();+ h.stream?.push(finalEvent());+ await flush();+ h.chat.restorePendingAction(askAction, 500);+ let answer: (() => void) | undefined;+ h.transport.resume.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answer = () =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' });+ }),+ );+ const answering = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ answerStop?.();+ await stopping;+ await flush();+ h.transport.reconnectToStream.mockClear();+ h.transport.reconnectToStream.mockImplementationOnce(() => new Promise(() => undefined));+ answer?.();+ await answering.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).toHaveBeenCalledTimes(1);+ });++ it('(5) an answer to an undated pause ends when a dated replay brings a later pause that expired', async () => {+ const h = createHarness();+ const undated = createStream({ streamId: 'convo-1' });+ attach(h, undated);+ send(h);+ await flush();+ undated.push({ type: 'pending_action', data: askAction } as unknown as ChatEvent);+ await flush();+ let answer: (() => void) | undefined;+ h.transport.resume.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answer = () =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' });+ }),+ );+ const answering = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ undated.push(dropEvent);+ await flush();+ const dated = createStream({ streamId: 'convo-1', generationCreatedAt: 700 });+ h.transport.reconnectToStream.mockResolvedValueOnce(dated);+ h.chat.resumeStream({ streamId: 'convo-1' }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ dated.push({+ type: 'pending_action',+ data: { ...askAction, actionId: 'action-2', createdAt: 2, expiresAt: 1 },+ } as unknown as ChatEvent);+ await flush();+ dated.push(dropEvent);+ await flush();+ h.transport.reconnectToStream.mockClear();+ answer?.();+ await answering.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ });++ it('(5) a stop of an undated pause a status read dated ends the answer in flight for it', async () => {+ const h = createHarness();+ const undated = createStream({ streamId: 'convo-1' });+ attach(h, undated);+ send(h);+ await flush();+ undated.push({ type: 'pending_action', data: askAction } as unknown as ChatEvent);+ await flush();+ let answer: (() => void) | undefined;+ h.transport.resume.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answer = () =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' });+ }),+ );+ const answering = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ undated.push(dropEvent);+ await flush();+ h.chat.restorePendingAction(askAction, 500);+ h.transport.abort.mockResolvedValueOnce({ success: true, aborted: 'convo-1' });+ await h.chat.stop();+ await flush();+ attach(h, createStream({ streamId: 'convo-2', generationCreatedAt: 900 }));+ let started: Error | undefined;+ const next = h.chat.sendMessage({ parts: [{ type: 'text', text: 'Next' }] });+ next.catch((error: Error) => {+ started = error;+ });+ await flush();++ expect(started).toBeUndefined();++ h.transport.reconnectToStream.mockClear();+ answer?.();+ await answering.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ });++ it('(5) a stop of an undated pause a dated replay names ends the answer in flight for it', async () => {+ const h = createHarness();+ const undated = createStream({ streamId: 'convo-1' });+ attach(h, undated);+ send(h);+ await flush();+ undated.push({ type: 'pending_action', data: askAction } as unknown as ChatEvent);+ await flush();+ let answer: (() => void) | undefined;+ h.transport.resume.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ answer = () =>+ resolve({ streamId: 'convo-1', conversationId: 'convo-1', status: 'resuming' });+ }),+ );+ const answering = h.chat.addToolOutput({ toolCallId: 'ask-1', output: 'A' });+ await flush();+ undated.push(dropEvent);+ await flush();+ const dated = createStream({ streamId: 'convo-1', generationCreatedAt: 700 });+ h.transport.reconnectToStream.mockResolvedValueOnce(dated);+ h.chat.resumeStream({ streamId: 'convo-1' }).then(+ () => undefined,+ () => undefined,+ );+ await flush();+ dated.push({ type: 'pending_action', data: askAction } as unknown as ChatEvent);+ await flush();+ h.transport.abort.mockResolvedValueOnce({ success: true, aborted: 'convo-1' });+ const stopping = h.chat.stop().catch(() => undefined);+ await flush();+ dated.close();+ await stopping;+ await flush();+ h.transport.reconnectToStream.mockClear();+ answer?.();+ await answering.catch(() => undefined);+ await flush();++ expect(h.transport.reconnectToStream).not.toHaveBeenCalled();+ });+});diff --git a/packages/chat/src/core/chat.ts b/packages/chat/src/core/chat.tsindex 230dcc7d03..79c2635de8 100644--- a/packages/chat/src/core/chat.ts+++ b/packages/chat/src/core/chat.ts@@ -248,6 +248,15 @@ type OptimisticWrite = {removed: Set<string>;};+/** An answer in flight, which a later pause or the end of its generation ends. */+type AnswerClaim = {+ action: Agents.PendingAction;+ generationCreatedAt?: number;+ /** The record of the paused generation, which identifies it when no timestamp does. */+ generation?: Generation;+ ended: boolean;+};+type DetachedGeneration = {error: Error;target: Omit<ChatAbortRequest, 'chatId'>;@@ -833,6 +842,13 @@ export class Chat {private generationCreatedAt?: number;/** The pause whose resume request is in flight, so a second submit cannot send it again. */private resuming?: string;+ /**+ * The claim of the newest answer in flight: its pause and generation, against which a later pause+ * is judged to replace it, and whether its generation ended or a newer pause replaced it. Expiry,+ * a snapshot that releases the pause and a stale replay only hide it, so they leave it live. Each+ * answer keeps its own claim, so a newer answer never revives an ended one.+ */+ private answering?: AnswerClaim;private disposed = false;/** Set while the chat writes the state, whose own report of that write the chat announces itself. */private writing = false;@@ -889,6 +905,10 @@ export class Chat {}clearTimeout(this.expiry);this.expiry = undefined;+ if (action && this.answering && action.actionId !== this.answering.action.actionId) {+ /** A newer pause replaced the one an answer is in flight for. */+ this.answering.ended = true;+ }this.held = action;if (action?.expiresAt != null && !this.disposed) {this.armExpiry(action);@@ -1082,6 +1102,9 @@ export class Chat {try {if (superseded || (retire != null && this.action?.actionId === retire)) {/** A restored pause belongs to the generation it was restored with, not this one. */+ if (superseded) {+ this.endAnswer();+ }this.clearAction();}this.setConnection('reconnecting');@@ -1246,6 +1269,10 @@ export class Chat {const since = this.latestStart;const answer = this.abortOnServer(target).then((result) => {generation.stopped = true;+ /** A confirmed stop ends its generation, and with it an answer in flight for that generation's pause. */+ if (this.isAnsweredGeneration(generation, target.generationCreatedAt)) {+ this.endAnswer();+ }if (target.generationCreatedAt != null) {this.endedGenerations.add(target.generationCreatedAt);}@@ -1680,11 +1707,36 @@ export class Chat {return getStopResult(result, generation.stoppedConversationId);}+ /** The record of the generation the held pause belongs to, as the request, drop or read keeps it. */+ private pausedGeneration(): Generation | undefined {+ return this.active?.generation ?? this.detached?.generation ?? this.restored?.generation;+ }+/** The conversation the paused generation's record names, which a frame may have assigned. */private pausedConversationId(): string | undefined {- const generation =- this.active?.generation ?? this.detached?.generation ?? this.restored?.generation;- return generation?.conversationId;+ return this.pausedGeneration()?.conversationId;+ }++ /**+ * Whether a stopped generation is the one the answer in flight belongs to: the same record, or+ * the same timestamp when both are known. An unknown timestamp alone matches nothing.+ */+ private isAnsweredGeneration(+ generation: Generation,+ generationCreatedAt: number | undefined,+ ): boolean {+ const answering = this.answering;+ if (!answering) {+ return false;+ }+ if (answering.generation === generation) {+ return true;+ }+ return (+ generationCreatedAt != null &&+ answering.generationCreatedAt != null &&+ generationCreatedAt === answering.generationCreatedAt+ );}/** The endpoint a generation this chat did not start runs on, as the host selects it. */@@ -1706,12 +1758,48 @@ export class Chat {}}- /** Whether `action` is the held pause, or the one a snapshot released while nothing replaced it. */- private holdsPause(action: Agents.PendingAction): boolean {- if (this.action) {- return this.action.actionId === action.actionId;+ /** Ends the claim of an answer in flight: its generation ended, so a continuation has nothing to follow. */+ private endAnswer(): void {+ if (this.answering) {+ this.answering.ended = true;+ }+ }++ /** A pause belongs to one generation, so the date a source reports for it is the answer's too. */+ private dateAnswer(action: Agents.PendingAction, generationCreatedAt: number | undefined): void {+ const answering = this.answering;+ if (+ answering &&+ !answering.ended &&+ answering.generationCreatedAt == null &&+ generationCreatedAt != null &&+ answering.action.actionId === action.actionId+ ) {+ answering.generationCreatedAt = generationCreatedAt;+ }+ }++ /**+ * Ends that claim when `next` replaces the pause being answered: a pause of a later generation,+ * or a later one of the same generation, whether or not the chat still shows the answered one.+ */+ private replaceAnswer(next: Agents.PendingAction, generationCreatedAt: number | undefined): void {+ const answering = this.answering;+ if (!answering || next.actionId === answering.action.actionId) {+ return;+ }+ const later =+ generationCreatedAt != null &&+ answering.generationCreatedAt != null &&+ generationCreatedAt > answering.generationCreatedAt;+ /** An undated answer cannot tell its generation apart, so the pauses' own order decides. */+ const sameGeneration =+ generationCreatedAt == null ||+ answering.generationCreatedAt == null ||+ generationCreatedAt === answering.generationCreatedAt;+ if (later || (sameGeneration && this.isSupersededBy(answering.action, next))) {+ answering.ended = true;}- return this.releasedPause === action.actionId;}private assertNotResuming(action: Agents.PendingAction): void {@@ -1782,6 +1870,8 @@ export class Chat {this.action != null &&(generationCreatedAt !== this.generationCreatedAt ||this.isSupersededBy(this.action, action));+ /** An expired pause of a later generation, or a later one of this generation, replaces it. */+ this.replaceAnswer(action, generationCreatedAt);if (this.action?.actionId === action.actionId || superseded) {this.clearAction();}@@ -1962,6 +2052,10 @@ export class Chat {if (this.action) {throw new Error('A paused generation is waiting for a decision; answer or stop it first');}+ if (this.answering && !this.answering.ended) {+ /** An answer the server may accept still owns the generation, even when its pause is hidden. */+ throw new Error('An answer to the paused generation is still in flight');+ }const active = createActiveRequest({ responseless: true });this.active = active;@@ -2190,6 +2284,7 @@ export class Chat {this.generationCreatedAt = stream.generationCreatedAt;if (this.action && previous != null && previous !== stream.generationCreatedAt) {/** A restored pause belongs to the generation it was restored with, not this one. */+ this.endAnswer();this.clearAction();}}@@ -2427,6 +2522,7 @@ export class Chat {this.action = undefined;this.decisions.clear();this.releasedPause = undefined;+ this.endAnswer();/** Including an attachment that found its generation already finished. */const ended =active.stream?.generationCreatedAt ??@@ -2547,6 +2643,8 @@ export class Chat {this.seePause(expired, generation);const held = this.action;const superseded = held != null && generation != null && this.isSupersededBy(held, expired);+ /** A later pause of the generation replaces the answered one, even though it already expired. */+ this.replaceAnswer(expired, generation);if (held?.actionId === expired.actionId || superseded) {this.clearAction();}@@ -2614,8 +2712,12 @@ export class Chat {);}- /** Records a pause of `generationCreatedAt` as the newest seen when no later one was. */+ /**+ * Records a pause of `generationCreatedAt` as the newest seen when no later one was, and dates an+ * undated answer in flight for that same pause, so a stop of the generation it names ends it.+ */private seePause(action: Agents.PendingAction, generationCreatedAt: number | undefined): void {+ this.dateAnswer(action, generationCreatedAt);if (generationCreatedAt == null || this.isOlderPause(action, generationCreatedAt)) {return;}@@ -2723,6 +2825,13 @@ export class Chat {return;}this.resuming = action.actionId;+ const claim: AnswerClaim = {+ action,+ generationCreatedAt,+ generation: this.pausedGeneration(),+ ended: false,+ };+ this.answering = claim;/** Read once: the answer and the reattachment that follows it run on the same selection. */let selection: ReturnType<NonNullable<ChatInit['resumeSelection']>> | undefined;try {@@ -2747,10 +2856,11 @@ export class Chat {/** The answer names the conversation, which a chat started under `new` adopts either way. */this.learnConversation(result.conversationId);/**- * A pause stopped or replaced while the request was in flight is no longer this one's. One a- * snapshot only released still is: the server took its answer, so its continuation runs.+ * A pause stopped, replaced or ended with its generation while the request was in flight is+ * no longer this one's. One that only expired or that a snapshot released still is: the server+ * took its answer, so its continuation runs.*/- if (!this.holdsPause(action)) {+ if (claim.ended) {return;}const reattach = this.active && !this.active.stream ? this.active.reattach : undefined;@@ -2814,7 +2924,7 @@ export class Chat {throw error;}const ambiguous = isRetryableNetworkError(failure);- if (ambiguous && !this.active && !this.disposed && this.holdsPause(action)) {+ if (ambiguous && !this.active && !this.disposed && !claim.ended) {/*** The answer may have reached the server, which then runs the generation again. It stays* addressable through a reattachment, which finds the continuation, the pause or the end.@@ -2836,6 +2946,9 @@ export class Chat {}throw error;} finally {+ if (this.answering === claim) {+ this.answering = undefined;+ }if (this.resuming === action.actionId) {this.resuming = undefined;const held = this.action;