@@ -89,22 +89,12 @@ const INBOX_ROW_LIMIT = 500
8989 * can share a millisecond, so the timestamp alone cannot order them. Rows persisted before the
9090 * sequence existed have none and come first, as they are the oldest.
9191 */
92- const persistOrder = [
93- sql `${ copilotAsyncToolCalls . persistSeq } ASC NULLS FIRST` ,
94- asc ( copilotAsyncToolCalls . createdAt ) ,
95- asc ( copilotAsyncToolCalls . toolCallId ) ,
96- ]
97-
98- function comparePersistOrder (
99- a : { persistSeq : number | null ; createdAt : Date ; toolCallId : string } ,
100- b : { persistSeq : number | null ; createdAt : Date ; toolCallId : string }
101- ) : number {
102- if ( a . persistSeq !== b . persistSeq ) {
103- if ( a . persistSeq === null ) return - 1
104- if ( b . persistSeq === null ) return 1
105- return a . persistSeq - b . persistSeq
106- }
107- return a . createdAt . getTime ( ) - b . createdAt . getTime ( ) || a . toolCallId . localeCompare ( b . toolCallId )
92+ function persistOrder ( ) {
93+ return [
94+ sql `${ copilotAsyncToolCalls . persistSeq } ASC NULLS FIRST` ,
95+ asc ( copilotAsyncToolCalls . createdAt ) ,
96+ asc ( copilotAsyncToolCalls . toolCallId ) ,
97+ ]
10898}
10999
110100export interface DesktopDeviceRegistration {
@@ -232,27 +222,27 @@ export async function listDesktopInboxRows(identity: Omit<DesktopDeviceIdentity,
232222 state
233223 )
234224 )
235- . orderBy ( ...persistOrder )
225+ . orderBy ( ...persistOrder ( ) )
236226 . limit ( INBOX_ROW_LIMIT )
237- const [ waiting , cancelled ] = await Promise . all ( [
238- rowsWhere (
239- and (
240- eq ( copilotAsyncToolCalls . status , ASYNC_TOOL_STATUS . pending ) ,
241- isNull ( copilotAsyncToolCalls . executionOwnerToken ) ,
242- or ( sql `${ copilotAsyncToolCalls . pickupDeadlineAt } > clock_timestamp()` , awaitingPermission ) ,
243- inArray ( copilotRuns . status , LIVE_RUN_STATUSES ) ,
244- isNull ( copilotRuns . toolAdmissionClosedAt )
245- )
246- ) ,
247- rowsWhere (
248- and (
249- isNotNull ( copilotAsyncToolCalls . executionOwnerToken ) ,
250- isNull ( copilotAsyncToolCalls . executionSettledAt ) ,
251- ne ( copilotAsyncToolCalls . status , ASYNC_TOOL_STATUS . running )
227+ return rowsWhere (
228+ and (
229+ eq ( copilotAsyncToolCalls . status , ASYNC_TOOL_STATUS . pending ) ,
230+ isNull ( copilotAsyncToolCalls . executionOwnerToken ) ,
231+ or ( sql `${ copilotAsyncToolCalls . pickupDeadlineAt } > clock_timestamp()` , awaitingPermission ) ,
232+ inArray ( copilotRuns . status , LIVE_RUN_STATUSES ) ,
233+ isNull ( copilotRuns . toolAdmissionClosedAt )
234+ )
235+ )
236+ . unionAll (
237+ rowsWhere (
238+ and (
239+ isNotNull ( copilotAsyncToolCalls . executionOwnerToken ) ,
240+ isNull ( copilotAsyncToolCalls . executionSettledAt ) ,
241+ ne ( copilotAsyncToolCalls . status , ASYNC_TOOL_STATUS . running )
242+ )
252243 )
253- ) ,
254- ] )
255- return [ ...waiting , ...cancelled ] . sort ( comparePersistOrder )
244+ )
245+ . orderBy ( ...persistOrder ( ) )
256246}
257247
258248export type DesktopInboxRow = Awaited < ReturnType < typeof listDesktopInboxRows > > [ number ]
@@ -448,7 +438,7 @@ export async function listOverdueDesktopToolCalls(input: { slackMs: number; limi
448438 )
449439 )
450440 )
451- . orderBy ( ...persistOrder )
441+ . orderBy ( ...persistOrder ( ) )
452442 . limit ( input . limit )
453443 return rows . map ( ( row ) => row . toolCallId )
454444}
0 commit comments