diff --git a/apps/sim/lib/desktop/application/executor.integration.ts b/apps/sim/lib/desktop/application/executor.integration.ts index a292d738d2f..174a9d924d2 100644 --- a/apps/sim/lib/desktop/application/executor.integration.ts +++ b/apps/sim/lib/desktop/application/executor.integration.ts @@ -883,6 +883,27 @@ describe.runIf(Boolean(redisUrl))('desktop background executor protocol', () => ).toEqual([first, second, vfs]) }) + it('lists calls persisted within the same millisecond in persistence order', async () => { + const desktop = await signedInDesktop() + const run = await boundRun(desktop) + /** Ids sorted against persistence order, so an id tiebreak would reverse them. */ + const persisted = [generateId(), generateId(), generateId()].sort().reverse() + await db.insert(copilotAsyncToolCalls).values( + persisted.map((toolCallId, index) => ({ + runId: run.runId, + toolCallId, + toolName: 'browser_click', + args: { ref: 'e1' }, + pickupDeadlineAt: sql`now() + interval '1 minute'`, + createdAt: sql`date_trunc('milliseconds', localtimestamp) + ${index + 1} * interval '1 microsecond'`, + })) + ) + + expect( + (await inbox(desktop)).items.map((item) => item.kind === 'call' && item.toolCallId) + ).toEqual(persisted) + }) + it('counts the device online after a pull or a stream open, and keeps it online when the stream closes', async () => { const desktop = await signedInDesktop() const presenceKey = `desktop:presence:${desktop.deviceId}` diff --git a/apps/sim/lib/desktop/executor/repository.ts b/apps/sim/lib/desktop/executor/repository.ts index 94496ce84ae..77851d529fe 100644 --- a/apps/sim/lib/desktop/executor/repository.ts +++ b/apps/sim/lib/desktop/executor/repository.ts @@ -171,6 +171,11 @@ export async function listDesktopInboxRows(identity: Omit`${copilotAsyncToolCalls.executionOwnerToken} IS NOT NULL`, createdAt: copilotAsyncToolCalls.createdAt, + // A Date's milliseconds would tie calls written within the same millisecond. + persistedAtMicros: + sql`(extract(epoch from ${copilotAsyncToolCalls.createdAt}) * 1000000)::bigint`.mapWith( + Number + ), chatId: copilotRuns.chatId, chatTitle: copilotChats.title, workspaceId: copilotRuns.workspaceId, @@ -208,8 +213,7 @@ export async function listDesktopInboxRows(identity: Omit - a.createdAt.getTime() - b.createdAt.getTime() || a.toolCallId.localeCompare(b.toolCallId) + (a, b) => a.persistedAtMicros - b.persistedAtMicros || a.toolCallId.localeCompare(b.toolCallId) ) }