LibreChat/api/server/controllers/agents/callbacks.background.spec.js
Danny Avila 00c5a747e9
🧵 feat: Native Background Execution for Code Interpreter Tools (#14386)
* 🧵 feat: Native Background Execution for Code Interpreter Tools

* 🩹 fix: Address Codex Round 1 (fallback dedupe, harvest failure, handle parsing)

* 🩹 fix: Live Completion Marker + Unkeyed Attachment Dedupe (Codex Round 2)

* 🎨 chore: Sort Imports + Widen Marker Type Comparison (CI)

* 🩹 fix: Stale-Harvest Guard, Error Marker Status, Faster Anchor Retry (Codex Round 3)

* 🧹 refactor: TS Harvest Module, Claim-Neutral Timestamps, Error Parity (Codex Round 4)

* 🩹 fix: Dispatch-Ordered Stale Guard, Foreground Downgrade, Error Wrapper Parity (Codex Round 5)

* 🩹 fix: Retry Past Unfinished Rows + Per-Call Attachment Dedupe (Codex Round 6)

* 🩹 fix: Writer-Dispatch Ordering, Scoped Live Upserts, Reaped-Task Wrapper (Codex Round 7)

* 🩹 fix: Wildcard toolCallId Matching for Bare Attachment Updates (CI)

* 🩹 fix: Claim-Insert Dispatch Stamp (Schema-Backed) + Scoped Status Markers (Codex Round 8)

* 🩹 fix: Pre-Write Ownership CAS + Agent-Scoped Part Patching (Codex Round 9)

* 🩹 fix: Insert-Path Ownership CAS + Agent-Routed Attachments (Codex Round 10)

* 🩹 fix: Agent-Scoped Marker Ids and Attachment Dedupe (Codex Round 11)

* 🩹 fix: Atomic File Commit and Sibling Preview Fan-Out (Codex Round 12)

- Replace the two-step claim-confirm CAS with an atomic conditional updateFile: the ownership predicate (no sourceDispatchedAt, or <= this write's dispatch order) moves into the update filter, removing confirmCodeFileOwnership and the lost-update window between check and write
- Thread agentId through createDownloadFallback so fallback download rows scope to the emitting agent like primary rows
- Fan terminal preview overlays out to every live attachment sharing the file_id in useAttachmentPreviewSync (sibling tool calls no longer stick on pending)
- Restore background artifacts through toStoredArtifact so the size bound applies on re-anchor
- Apply filterAttachmentsForPart to grouped tool-call attachments in ContentParts so handoff agents with colliding provider call ids do not cross-contaminate groups

* 🩹 fix: Agent-Scoped Live Upserts and Monotonic Dispatch Stamps (Codex Round 13)

- Scope the SSE attachment upsert and the useAttachments DB/live merge by agentId with the same wildcard semantics as toolCallId: distinct non-null agentIds stay separate entries, so handoff agents sharing a claimed file_id and a repeated provider tool id (call_0) no longer merge over each other's cards
- Extend the attachment identity key to fileKey::toolCallId::agentId and register less-specific key variants so bare and agent-less live records still dedupe after overlay
- Stamp background task createdAt from a strictly-increasing per-process dispatch counter: raw Date.now() can tie for same-millisecond dispatches and the stale-output guard accepts equal stamps (needed for idempotent re-commits), which would let an older task overwrite a newer task's committed file
2026-07-22 22:13:15 -04:00

194 lines
7 KiB
JavaScript

jest.mock('~/server/services/Files/Code/process', () => ({
processCodeOutput: jest.fn(),
runPreviewFinalize: jest.fn(),
}));
jest.mock('~/server/services/Files/Citations', () => ({ processFileCitations: jest.fn() }));
jest.mock('~/server/services/Files/process', () => ({ saveBase64Image: jest.fn() }));
const { processCodeOutput, runPreviewFinalize } = require('~/server/services/Files/Code/process');
const { createBackgroundCodeResultHandler } = require('./callbacks');
const req = { user: { id: 'user-1' } };
const baseParams = {
toolName: 'execute_code',
toolCallId: 'call_code',
messageId: 'msg-dispatch',
conversationId: 'convo-1',
agentId: 'agent_a',
output: 'stdout:\nhello',
artifact: {
session_id: 'exec-sess',
files: [
{ id: 'f1', name: 'plot.png', storage_session_id: 'store-1' },
{ id: 'f2', name: 'input.csv', inherited: true },
],
},
};
describe('createBackgroundCodeResultHandler', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('persists non-inherited files with the original identity and patches the message row', async () => {
processCodeOutput.mockResolvedValue({
file: { file_id: 'f1', filename: 'plot.png', toolCallId: 'call_code' },
finalize: undefined,
});
const updateToolCallResult = jest.fn().mockResolvedValue({ matched: true, unfinished: false });
const handler = createBackgroundCodeResultHandler({ req, updateToolCallResult });
const result = await handler(baseParams);
expect(processCodeOutput).toHaveBeenCalledTimes(1);
expect(processCodeOutput).toHaveBeenCalledWith(
expect.objectContaining({
req,
id: 'f1',
name: 'plot.png',
messageId: 'msg-dispatch',
toolCallId: 'call_code',
conversationId: 'convo-1',
agentId: 'agent_a',
session_id: 'store-1',
freshClaimAfter: expect.any(Number),
}),
);
expect(updateToolCallResult).toHaveBeenCalledTimes(1);
expect(updateToolCallResult).toHaveBeenCalledWith({
userId: 'user-1',
messageId: 'msg-dispatch',
conversationId: 'convo-1',
toolCallId: 'call_code',
agentId: 'agent_a',
output: 'stdout:\nhello',
attachments: [{ file_id: 'f1', filename: 'plot.png', toolCallId: 'call_code' }],
});
expect(result).toEqual({
attachments: [{ file_id: 'f1', filename: 'plot.png', toolCallId: 'call_code' }],
});
});
it('anchors the stale-output guard to dispatch time when provided', async () => {
processCodeOutput.mockResolvedValue({ file: { file_id: 'f1' } });
const handler = createBackgroundCodeResultHandler({
req,
updateToolCallResult: jest.fn().mockResolvedValue({ matched: true, unfinished: false }),
});
await handler({ ...baseParams, dispatchedAt: 12345 });
expect(processCodeOutput).toHaveBeenCalledWith(
expect.objectContaining({ freshClaimAfter: 12345 }),
);
});
it('runs deferred preview finalization without a live stream callback', async () => {
const finalize = jest.fn();
processCodeOutput.mockResolvedValue({
file: { file_id: 'f1' },
finalize,
previewRevision: 3,
});
const handler = createBackgroundCodeResultHandler({
req,
updateToolCallResult: jest.fn().mockResolvedValue({ matched: true, unfinished: false }),
});
await handler(baseParams);
expect(runPreviewFinalize).toHaveBeenCalledWith({ finalize, fileId: 'f1', previewRevision: 3 });
});
it('retries the row patch until the dispatch turn persists', async () => {
jest.useFakeTimers();
try {
processCodeOutput.mockResolvedValue({ file: { file_id: 'f1' } });
const updateToolCallResult = jest
.fn()
.mockResolvedValueOnce({ matched: false, unfinished: false })
.mockResolvedValueOnce({ matched: false, unfinished: false })
.mockResolvedValue({ matched: true, unfinished: false });
const handler = createBackgroundCodeResultHandler({ req, updateToolCallResult });
const promise = handler(baseParams);
await jest.advanceTimersByTimeAsync(250);
await jest.advanceTimersByTimeAsync(500);
const result = await promise;
expect(updateToolCallResult).toHaveBeenCalledTimes(3);
expect(result?.attachments).toHaveLength(1);
} finally {
jest.useRealTimers();
}
});
it('keeps re-applying past unfinished partial rows until a finalized row is patched', async () => {
jest.useFakeTimers();
try {
processCodeOutput.mockResolvedValue({ file: { file_id: 'f1' } });
/* A disconnect mid-turn persists an unfinished partial row; the later
* finalize save overwrites it with the in-memory handle JSON, so a
* patch that settled on the partial row must not stop the loop. */
const updateToolCallResult = jest
.fn()
.mockResolvedValueOnce({ matched: true, unfinished: true })
.mockResolvedValue({ matched: true, unfinished: false });
const handler = createBackgroundCodeResultHandler({ req, updateToolCallResult });
const promise = handler(baseParams);
await jest.advanceTimersByTimeAsync(250);
const result = await promise;
expect(updateToolCallResult).toHaveBeenCalledTimes(2);
expect(result?.attachments).toHaveLength(1);
} finally {
jest.useRealTimers();
}
});
it('still patches output when a file download fails (files are best-effort)', async () => {
processCodeOutput.mockRejectedValue(new Error('download failed'));
const updateToolCallResult = jest.fn().mockResolvedValue({ matched: true, unfinished: false });
const handler = createBackgroundCodeResultHandler({ req, updateToolCallResult });
const result = await handler(baseParams);
expect(updateToolCallResult).toHaveBeenCalledWith(
expect.objectContaining({ output: 'stdout:\nhello', attachments: [] }),
);
expect(result).toEqual({ attachments: [] });
});
it('reapply mode re-applies the row patch without reprocessing files', async () => {
const updateToolCallResult = jest.fn().mockResolvedValue({ matched: true, unfinished: false });
const handler = createBackgroundCodeResultHandler({ req, updateToolCallResult });
const result = await handler({
...baseParams,
artifact: undefined,
attachments: [{ file_id: 'f1' }],
reapply: true,
});
expect(processCodeOutput).not.toHaveBeenCalled();
expect(updateToolCallResult).toHaveBeenCalledTimes(1);
expect(updateToolCallResult).toHaveBeenCalledWith(
expect.objectContaining({
messageId: 'msg-dispatch',
toolCallId: 'call_code',
output: 'stdout:\nhello',
attachments: [{ file_id: 'f1' }],
}),
);
expect(result).toEqual({ attachments: [{ file_id: 'f1' }] });
});
it('returns null without identity to anchor to', async () => {
const updateToolCallResult = jest.fn();
const handler = createBackgroundCodeResultHandler({ req, updateToolCallResult });
expect(await handler({ ...baseParams, messageId: undefined })).toBeNull();
expect(updateToolCallResult).not.toHaveBeenCalled();
});
});