fix(core): order agent sessions by turn start

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Robin Braumann 2026-09-15 09:34:07 +02:00
parent bbcd70bad8
commit e5ac59adec
No known key found for this signature in database
3 changed files with 10 additions and 2 deletions

View File

@ -166,7 +166,7 @@ export class AgentExecutionThreadRepository extends Repository<AgentExecutionThr
.where('latestExecution.threadId = thread.id')
// A queued turn has not started; the thread's status is its last run.
.andWhere("latestExecution.status != 'queued'")
.orderBy('latestExecution.createdAt', 'DESC')
.orderBy('COALESCE(latestExecution.startedAt, latestExecution.createdAt)', 'DESC')
.addOrderBy('latestExecution.id', 'DESC')
.limit(1)
.getQuery()

View File

@ -329,7 +329,7 @@ export class AgentExecutionRepository extends BaseRepository<AgentExecution> {
.andWhere(
`e.id = (SELECT e2.id FROM ${tableName} e2 ` +
'WHERE e2."threadId" = e."threadId" AND e2."status" != \'queued\' ' +
'ORDER BY e2."createdAt" DESC, e2.id DESC LIMIT 1)',
'ORDER BY COALESCE(e2."startedAt", e2."createdAt") DESC, e2.id DESC LIMIT 1)',
)
.getRawMany<{ threadId: string; status: Exclude<AgentExecutionStatus, 'queued'> }>();

View File

@ -344,6 +344,14 @@ describe('AgentExecutionRepository', () => {
status: 'running',
failureSummary: null,
createdAt: new Date('2026-01-02T00:00:00Z'),
startedAt: new Date('2026-01-04T00:00:00Z'),
});
await createExecution({
threadId: running.id,
status: 'success',
failureSummary: null,
createdAt: new Date('2026-01-03T00:00:00Z'),
startedAt: new Date('2026-01-03T00:00:00Z'),
});
await createExecution({
threadId: succeeded.id,