Skip to content

Commit ebffcbe

Browse files
rainmanaclaude
andcommitted
fix: Stable newest-first ordering for created_at-sorted lists
created_at timestamps can tie under Windows' coarse clock when rows are inserted in quick succession, making 'ORDER BY created_at DESC' ordering arbitrary; rowid DESC breaks ties by insertion order. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 9fb7fef commit ebffcbe

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

internal/repository/memory_repository.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (r *MemoryRepository) ListSessions(ctx context.Context, taskType string, st
120120
args = append(args, status)
121121
}
122122

123-
query += " ORDER BY created_at DESC LIMIT ? OFFSET ?"
123+
query += " ORDER BY created_at DESC, rowid DESC LIMIT ? OFFSET ?"
124124
args = append(args, limit, offset)
125125

126126
rows, err := r.db.QueryContext(ctx, query, args...)
@@ -662,7 +662,7 @@ func (r *MemoryRepository) ListContextSnapshots(ctx context.Context, sessionID s
662662
SELECT id, session_id, name, description, context_data, memory_summary, created_at
663663
FROM context_snapshots
664664
WHERE session_id = ?
665-
ORDER BY created_at DESC
665+
ORDER BY created_at DESC, rowid DESC
666666
LIMIT ? OFFSET ?
667667
`
668668

@@ -770,7 +770,7 @@ func (r *MemoryRepository) CheckForDuplicates(ctx context.Context, sessionID, ti
770770
)
771771
)
772772
)
773-
ORDER BY created_at DESC
773+
ORDER BY created_at DESC, rowid DESC
774774
LIMIT 5
775775
`
776776

@@ -2435,7 +2435,7 @@ func (r *MemoryRepository) ListTaskProgress(ctx context.Context, sessionID strin
24352435
args = append(args, status)
24362436
}
24372437

2438-
query += " ORDER BY created_at DESC LIMIT ? OFFSET ?"
2438+
query += " ORDER BY created_at DESC, rowid DESC LIMIT ? OFFSET ?"
24392439
args = append(args, limit, offset)
24402440

24412441
rows, err := r.db.QueryContext(ctx, query, args...)
@@ -2551,7 +2551,7 @@ func (r *MemoryRepository) GetCVEMapping(ctx context.Context, sessionID, cweID s
25512551
SELECT id, session_id, cwe_id, cve_list, last_updated, confidence, source, created_at, updated_at
25522552
FROM cve_mappings
25532553
WHERE session_id = ? AND cwe_id = ?
2554-
ORDER BY created_at DESC
2554+
ORDER BY created_at DESC, rowid DESC
25552555
LIMIT 1
25562556
`
25572557

0 commit comments

Comments
 (0)