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: Anchor Background Results Onto Tool Calls a Resumed Turn Saved Without Step IDs (#16353)
Danny Avila8183959f1981
diff --git a/packages/data-schemas/src/methods/message.spec.ts b/packages/data-schemas/src/methods/message.spec.tsindex 8460b85dcf..85353eb6e3 100644--- a/packages/data-schemas/src/methods/message.spec.ts+++ b/packages/data-schemas/src/methods/message.spec.ts@@ -862,6 +862,410 @@ describe('Message Operations', () => {expect(saved?.attachments).toEqual([{ file_id: 'f1', toolCallId: 'call_bg' }]);});+ describe('a step id the persisted part lost', () => {+ const conversationId = () => mockMessageData.conversationId as string;+ const partOutputs = async () => {+ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ return (+ saved?.content as Array<{ tool_call?: { id: string; stepId?: string; output?: string } }>+ )+ .filter((part) => part.tool_call != null)+ .map((part) => [+ part.tool_call?.id,+ part.tool_call?.stepId ?? null,+ part.tool_call?.output,+ ]);+ };++ it('patches the one part a resumed turn saved without its step id', async () => {+ await saveMessage(mockCtx, { ...mockMessageData, content: toolCallContent() });++ const result = await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ stepId: 'step_before_pause',+ output: 'stdout:\nfinished during the pause',+ attachments: [{ file_id: 'report', toolCallId: 'call_bg' }],+ });++ expect(result).toEqual({ matched: true, unfinished: false });+ expect(await partOutputs()).toEqual([+ ['call_bg', null, 'stdout:\nfinished during the pause'],+ ['call_other', null, 'untouched'],+ ]);+ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ expect(saved?.attachments).toEqual([{ file_id: 'report', toolCallId: 'call_bg' }]);+ });++ it('still prefers the part whose step id matches exactly', async () => {+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: [+ {+ type: 'tool_call',+ tool_call: { id: 'call_0', name: 'execute_code', output: 'stepless' },+ },+ {+ type: 'tool_call',+ tool_call: { id: 'call_0', name: 'execute_code', stepId: 'step_b', output: 'handle' },+ },+ ],+ });++ await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_0',+ stepId: 'step_b',+ output: 'settled',+ });++ expect(await partOutputs()).toEqual([+ ['call_0', null, 'stepless'],+ ['call_0', 'step_b', 'settled'],+ ]);+ });++ it.each([+ { file_id: 'shared-file' },+ { filepath: '/api/files/code/download/session/shared-file' },+ ])('preserves sibling step attachments when merging %j', async (fileKey) => {+ const siblingAttachment = {+ ...fileKey,+ toolCallId: 'call_bg',+ agentId: 'agent-a',+ stepId: 'step_b',+ };+ const attachment = { ...siblingAttachment, stepId: 'step_a' };+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: [+ ...toolCallContent(),+ {+ type: 'tool_call',+ agentId: 'agent-a',+ tool_call: { id: 'call_bg', stepId: 'step_b', output: 'sibling' },+ },+ ],+ attachments: [siblingAttachment],+ });+ const patch = {+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ agentId: 'agent-a',+ stepId: 'step_a',+ output: 'settled',+ attachments: [attachment],+ };++ expect(await updateToolCallResult(patch)).toEqual({ matched: true, unfinished: false });+ expect(await updateToolCallResult(patch)).toEqual({ matched: true, unfinished: false });++ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ expect(saved?.attachments).toEqual([siblingAttachment, attachment]);+ expect((await partOutputs())[2]).toEqual(['call_bg', 'step_b', 'sibling']);+ });++ it('retries the exact part after attachment contention without patching its stepless sibling', async () => {+ const stepless = {+ type: 'tool_call',+ agentId: 'agent-a',+ tool_call: { id: 'call_bg', output: 'stepless' },+ };+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: [+ stepless,+ {+ type: 'tool_call',+ agentId: 'agent-a',+ tool_call: { id: 'call_bg', stepId: 'step_b', output: 'handle' },+ },+ ],+ attachments: [],+ });+ const patch = {+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ stepId: 'step_b',+ agentId: 'agent-a',+ output: 'settled',+ attachments: [{ file_id: 'result', toolCallId: 'call_bg', stepId: 'step_b' }],+ backgroundTask: {+ taskId: 'task-b',+ toolName: 'execute_code',+ status: 'completed' as const,+ settledAt: new Date(),+ resultClaim: {+ kind: 'manual' as const,+ claimId: 'poll-b',+ claimedAt: new Date(),+ },+ },+ };+ const original = Message.collection.findOneAndUpdate.bind(Message.collection);+ let collisions = 0;+ const spy = jest+ .spyOn(Message.collection, 'findOneAndUpdate')+ .mockImplementation(async (...args) => {+ if (Object.prototype.hasOwnProperty.call(args[0], 'attachments') && collisions < 8) {+ collisions += 1;+ await Message.updateOne(+ { messageId: 'msg123', user: 'user123' },+ { $push: { attachments: { file_id: `concurrent-${collisions}` } } },+ );+ }+ return original(...args);+ });+ try {+ expect(await updateToolCallResult(patch)).toEqual({ matched: false, unfinished: false });+ expect(collisions).toBe(8);+ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ expect(saved?.content?.[0]).toEqual(stepless);+ expect(saved?.attachments).toHaveLength(8);+ } finally {+ spy.mockRestore();+ }++ expect(await updateToolCallResult(patch)).toEqual({ matched: true, unfinished: false });+ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ expect(saved?.content?.[0]).toEqual(stepless);+ expect(saved?.content?.[1]).toMatchObject({+ tool_call: { output: 'settled', backgroundTask: patch.backgroundTask },+ });+ expect(saved?.attachments).toHaveLength(9);+ });++ describe.each([false, true])('fallback with attachments: %s', (withAttachments) => {+ it.each(['exact', 'ambiguous', 'unfinished', 'claimed'])(+ 'leaves a concurrent %s change intact and retryable',+ async (change) => {+ await saveMessage(mockCtx, { ...mockMessageData, content: toolCallContent() });+ const claim = { kind: 'manual', claimId: 'concurrent-poll', claimedAt: new Date() };+ const original = Message.collection.findOneAndUpdate.bind(Message.collection);+ let raced = false;+ let concurrentContent: IMessage['content'];+ const spy = jest+ .spyOn(Message.collection, 'findOneAndUpdate')+ .mockImplementation(async (...args) => {+ if (+ !raced &&+ (Array.isArray(args[0].content) ||+ args[0].content?.$elemMatch?.['tool_call.stepId'] === null)+ ) {+ raced = true;+ const filter = { messageId: 'msg123', user: 'user123' };+ if (change === 'unfinished') {+ await Message.updateOne(filter, { $set: { unfinished: true } });+ } else if (change === 'claimed') {+ await Message.updateOne(filter, {+ $set: { 'content.1.tool_call.backgroundTask.resultClaim': claim },+ });+ } else {+ await Message.updateOne(filter, {+ $push: {+ content: {+ type: 'tool_call',+ tool_call: {+ id: 'call_bg',+ output: 'concurrent',+ ...(change === 'exact' ? { stepId: 'step_a' } : {}),+ },+ },+ },+ });+ }+ concurrentContent = (+ await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean()+ )?.content;+ }+ return original(...args);+ });+ try {+ const result = await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ stepId: 'step_a',+ output: 'settled',+ ...(withAttachments+ ? { attachments: [{ file_id: 'result', toolCallId: 'call_bg' }] }+ : {}),+ backgroundTask: {+ taskId: 'task-a',+ toolName: 'execute_code',+ status: 'completed',+ settledAt: new Date(),+ resultClaim: { kind: 'wakeup', claimId: 'wakeup-a', claimedAt: new Date() },+ },+ });+ expect(raced).toBe(true);+ expect(result).toEqual({ matched: false, unfinished: false });+ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ expect(saved?.content).toEqual(concurrentContent);+ expect(saved?.attachments ?? []).toEqual([]);+ } finally {+ spy.mockRestore();+ }+ },+ );+ });++ it.each([undefined, null, { kind: 'manual', claimId: 'existing', claimedAt: new Date() }])(+ 'atomically settles a fallback while preserving claim ownership: %j',+ async (resultClaim) => {+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: [+ {+ type: 'tool_call',+ tool_call: {+ id: 'call_bg',+ stepId: null,+ output: 'handle',+ backgroundTask: { resultClaim },+ },+ },+ ],+ });+ const backgroundTask = {+ taskId: 'task-a',+ toolName: 'execute_code',+ status: 'completed' as const,+ settledAt: new Date(),+ resultClaim: { kind: 'wakeup' as const, claimId: 'new', claimedAt: new Date() },+ };+ expect(+ await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ stepId: 'step_a',+ output: 'settled',+ backgroundTask,+ }),+ ).toEqual({ matched: true, unfinished: false });+ const saved = await Message.findOne({ messageId: 'msg123', user: 'user123' }).lean();+ expect(saved?.content?.[0]).toMatchObject({+ tool_call: {+ stepId: null,+ output: 'settled',+ backgroundTask: {+ ...backgroundTask,+ resultClaim: resultClaim ?? backgroundTask.resultClaim,+ },+ },+ });+ },+ );++ it.each([+ { partAgent: 'agent-a', callAgent: 'agent-b', matched: true },+ { partAgent: 'agent-b', callAgent: 'agent-a', matched: false },+ { partAgent: null, callAgent: 'agent-a', matched: true },+ { partAgent: null, callAgent: 'agent-b', matched: false },+ { partAgent: null, callAgent: null, matched: true },+ ])(+ 'keeps agent ownership when falling back: %j',+ async ({ partAgent, callAgent, matched }) => {+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: [+ {+ type: 'tool_call',+ agentId: partAgent,+ tool_call: { id: 'call_bg', agentId: callAgent, stepId: null, output: 'handle' },+ },+ {+ type: 'tool_call',+ agentId: 'agent-b',+ tool_call: { id: 'call_bg', stepId: 'step_a', output: 'sibling' },+ },+ ],+ });+ expect(+ await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ agentId: 'agent-a',+ stepId: 'step_a',+ output: 'settled',+ }),+ ).toEqual({ matched, unfinished: false });+ expect(await partOutputs()).toEqual([+ ['call_bg', null, matched ? 'settled' : 'handle'],+ ['call_bg', 'step_a', 'sibling'],+ ]);+ },+ );++ it('does not guess on a row the turn is still writing', async () => {+ await saveMessage(mockCtx, {+ ...mockMessageData,+ unfinished: true,+ content: toolCallContent(),+ });++ const result = await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_bg',+ stepId: 'step_not_saved_yet',+ output: 'settled',+ });++ expect(result).toEqual({ matched: false, unfinished: false });+ expect((await partOutputs())[0]).toEqual([+ 'call_bg',+ null,+ '{"background_task_id":"task-1"}',+ ]);+ });++ it('leaves an ambiguous repeated call id unmatched rather than guessing', async () => {+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: [+ {+ type: 'tool_call',+ tool_call: { id: 'call_0', name: 'execute_code', output: 'first' },+ },+ {+ type: 'tool_call',+ tool_call: { id: 'call_0', name: 'execute_code', output: 'second' },+ },+ ],+ });++ const result = await updateToolCallResult({+ userId: 'user123',+ messageId: 'msg123',+ conversationId: conversationId(),+ toolCallId: 'call_0',+ stepId: 'step_lost',+ output: 'settled',+ });++ expect(result).toEqual({ matched: false, unfinished: false });+ expect(await partOutputs()).toEqual([+ ['call_0', null, 'first'],+ ['call_0', null, 'second'],+ ]);+ });+ });+it('appends to existing attachments instead of replacing them', async () => {await saveMessage(mockCtx, {...mockMessageData,@@ -1025,7 +1429,16 @@ describe('Message Operations', () => {});it('reports no match when the targeted tool-call part is absent', async () => {- await saveMessage(mockCtx, { ...mockMessageData, content: toolCallContent() });+ /** The call was saved under a different, stamped step: a real mismatch, not+ * a part that lost its step id. */+ await saveMessage(mockCtx, {+ ...mockMessageData,+ content: toolCallContent().map((part) =>+ part.tool_call?.id === 'call_bg'+ ? { ...part, tool_call: { ...part.tool_call, stepId: 'other-step' } }+ : part,+ ),+ });await expect(updateToolCallResult({diff --git a/packages/data-schemas/src/methods/message.ts b/packages/data-schemas/src/methods/message.tsindex 8b85d82d61..1799b90e57 100644--- a/packages/data-schemas/src/methods/message.ts+++ b/packages/data-schemas/src/methods/message.ts@@ -860,6 +860,48 @@ function agentOwnershipFilter(prefix: string, agentId: string): Record<string, u};}+type UpdateToolCallResultInput = {+ userId: string;+ messageId: string;+ conversationId: string;+ toolCallId: string;+ stepId?: string;+ /** Scopes the part match when provider tool-call ids repeat across+ * agents in one response message (e.g. `call_0` per response); a part+ * without agent identity matches any caller (single-agent runs). */+ agentId?: string;+ output?: string;+ attachments?: unknown[];+ /**+ * Stamps `backgrounded: true` onto the patched tool call. Replacing the+ * dispatch-handle output with the settled task's stdout destroys the only+ * signal renderers had that this call ran detached (the handle JSON and+ * the live status-marker attachment are both transient), so the patch+ * that erases it must persist a durable one alongside.+ */+ markBackgrounded?: boolean;+ backgroundTask?: {+ taskId: string;+ toolName: string;+ status: 'completed' | 'error';+ cancelled?: true;+ settledAt: Date;+ completionWakeup?: true;+ completionReceipt?: true;+ resultClaim?: {+ kind: 'manual' | 'wakeup';+ claimId: string;+ claimedAt: Date;+ generationId?: string;+ };+ };+};++type SteplessToolCallFallback = {+ content: NonNullable<IMessage['content']>;+ hasResultClaim: boolean;+};+export function createMessageMethods(mongoose: typeof import('mongoose')): MessageMethods {/*** Saves a message in the database.@@ -1212,53 +1254,98 @@ export function createMessageMethods(mongoose: typeof import('mongoose')): Messa* finalize will overwrite the patch with in-memory content, so callers* should keep re-applying until a finalized row is patched.*/- async function updateToolCallResult({+ async function updateToolCallResult(+ input: UpdateToolCallResultInput,+ ): Promise<{ matched: boolean; unfinished: boolean }> {+ const exact = await settleToolCallResult(input, input.stepId);+ if (exact.matched || input.stepId == null) {+ return exact;+ }+ /** A turn resumed after an approval pause persists the tool calls it made+ * before the pause without their step ids, so an exact step match can never+ * land and the settled result would be given up. On a finished row, fall back+ * to the one part with this call id (and agent scope) that has no stored step.+ * An unfinished row may not hold the real part yet, and more than one+ * candidate means a repeated provider id: both stay unmatched rather than+ * patching the wrong part. */+ const fallback = await getSteplessToolCallFallback(input);+ if (fallback == null) {+ return exact;+ }+ return settleToolCallResult(input, null, fallback);+ }++ async function getSteplessToolCallFallback({userId,messageId,conversationId,toolCallId,stepId,agentId,- output,- attachments,- markBackgrounded,- backgroundTask,- }: {- userId: string;- messageId: string;- conversationId: string;- toolCallId: string;- stepId?: string;- /** Scopes the part match when provider tool-call ids repeat across- * agents in one response message (e.g. `call_0` per response); a part- * without agent identity matches any caller (single-agent runs). */- agentId?: string;- output?: string;- attachments?: unknown[];- /**- * Stamps `backgrounded: true` onto the patched tool call. Replacing the- * dispatch-handle output with the settled task's stdout destroys the only- * signal renderers had that this call ran detached (the handle JSON and- * the live status-marker attachment are both transient), so the patch- * that erases it must persist a durable one alongside.- */- markBackgrounded?: boolean;- backgroundTask?: {- taskId: string;- toolName: string;- status: 'completed' | 'error';- cancelled?: true;- settledAt: Date;- completionWakeup?: true;- completionReceipt?: true;- resultClaim?: {- kind: 'manual' | 'wakeup';- claimId: string;- claimedAt: Date;- generationId?: string;- };- };- }): Promise<{ matched: boolean; unfinished: boolean }> {+ }: UpdateToolCallResultInput): Promise<SteplessToolCallFallback | undefined> {+ const Message = mongoose.models.Message as Model<IMessage>;+ const row = await Message.findOne({ messageId, user: userId, conversationId })+ .select({ content: 1, unfinished: 1 })+ .lean<Pick<IMessage, 'content' | 'unfinished'> | null>();+ if (row == null || row.unfinished === true) {+ return;+ }+ let candidates = 0;+ let hasResultClaim = false;+ const content = row.content ?? [];+ for (const part of content) {+ const entry = part as {+ type?: unknown;+ agentId?: unknown;+ tool_call?: {+ id?: unknown;+ stepId?: unknown;+ agentId?: unknown;+ backgroundTask?: { resultClaim?: unknown };+ };+ } | null;+ if (entry?.type !== 'tool_call' || entry.tool_call?.id !== toolCallId) {+ continue;+ }+ const owner = entry.agentId ?? entry.tool_call.agentId;+ if (agentId != null && owner != null && owner !== agentId) {+ continue;+ }+ /** An unmatched write can mean attachment contention, not a missing+ * identity. Never substitute a sibling for an existing exact part. */+ if (entry.tool_call.stepId === stepId) {+ return;+ }+ if (entry.tool_call.stepId != null) {+ continue;+ }+ candidates += 1;+ if (candidates > 1) {+ return;+ }+ hasResultClaim = entry.tool_call.backgroundTask?.resultClaim != null;+ }+ return candidates === 1 ? { content, hasResultClaim } : undefined;+ }++ async function settleToolCallResult(+ {+ userId,+ messageId,+ conversationId,+ toolCallId,+ stepId,+ agentId,+ output,+ attachments,+ markBackgrounded,+ backgroundTask,+ }: UpdateToolCallResultInput,+ /** A step id to match exactly, `null` to match a part with no stored step,+ * or `undefined` to ignore steps. */+ stepMatch: string | null | undefined,+ fallback?: SteplessToolCallFallback,+ ): Promise<{ matched: boolean; unfinished: boolean }> {/** One source of truth for which content part this settle may touch:* `prefix: ''` yields the `$elemMatch` document filter, `prefix: 'part.'`* the arrayFilters element filter — the same predicate in one dialect,@@ -1268,14 +1355,16 @@ export function createMessageMethods(mongoose: typeof import('mongoose')): Messaconst partScope = (prefix: string): Record<string, unknown> => ({[`${prefix}type`]: 'tool_call',[`${prefix}tool_call.id`]: toolCallId,- ...(stepId != null ? { [`${prefix}tool_call.stepId`]: stepId } : {}),+ ...(stepMatch !== undefined ? { [`${prefix}tool_call.stepId`]: stepMatch } : {}),...(agentId != null ? agentOwnershipFilter(prefix, agentId) : {}),});const messageFilter = {messageId,user: userId,conversationId,- content: { $elemMatch: partScope('') },+ ...(fallback == null+ ? { content: { $elemMatch: partScope('') } }+ : { content: fallback.content, unfinished: { $ne: true } }),};const partIdentityFilter = partScope('part.');/** Amazon DocumentDB rejects aggregation-pipeline updates, so the part@@ -1315,8 +1404,18 @@ export function createMessageMethods(mongoose: typeof import('mongoose')): Messaif (Object.keys(partPatch).length === 0 && !mergingAttachments) {return { matched: false, unfinished: false };}+ /** Fence the fallback on the content used to establish its unique identity.+ * A concurrent save or claim must invalidate the whole write, including any+ * supplied claim. The snapshot lets us preserve an existing claim and stamp+ * a missing one atomically with settlement, rather than before this fence. */+ const settlePatch = {+ ...partPatch,+ ...(fallback != null && !fallback.hasResultClaim && backgroundTask?.resultClaim != null+ ? { 'content.$[part].tool_call.backgroundTask.resultClaim': backgroundTask.resultClaim }+ : {}),+ };const settleUpdate = {- ...(Object.keys(partPatch).length > 0 ? { $set: partPatch } : {}),+ ...(Object.keys(settlePatch).length > 0 ? { $set: settlePatch } : {}),...(disarmWakeup? { $unset: { 'content.$[part].tool_call.backgroundTask.completionWakeup': 1 } }: {}),@@ -1339,7 +1438,7 @@ export function createMessageMethods(mongoose: typeof import('mongoose')): Messa* instead expose a claimable terminal part that lets a second consumer* deliver the same result. A crash between the writes is healed by the* settle retry, whose claim write no-ops against its own stamp. */- if (backgroundTask?.resultClaim != null) {+ if (fallback == null && backgroundTask?.resultClaim != null) {await Message.updateOne(messageFilter,{@@ -1423,7 +1522,7 @@ export function createMessageMethods(mongoose: typeof import('mongoose')): Messa},{...settleUpdate,- $set: { ...partPatch, attachments: merged },+ $set: { ...settlePatch, attachments: merged },},settleOptions,).lean<{ unfinished?: boolean } | null>();