Skip to content

Commit 2b6e35e

Browse files
committed
fix(repository): use tuple comparison for deterministic file pagination
Use (update_time, uid) tuple comparison instead of just update_time to ensure deterministic pagination. This prevents infinite loops when paginating through files with identical timestamps.
1 parent 291c964 commit 2b6e35e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pkg/repository/file.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,12 +580,16 @@ func (r *repository) ListFiles(ctx context.Context, params KnowledgeBaseFileList
580580
return nil, fmt.Errorf("invalid next page token")
581581
}
582582

583-
q = q.Where("file.update_time <= ?", kbfs[0].UpdateTime)
583+
// Use tuple comparison for deterministic pagination.
584+
// This ensures files with the same update_time are ordered by UID,
585+
// preventing infinite loops when paginating through files with identical timestamps.
586+
q = q.Where("(file.update_time, file.uid) <= (?, ?)", kbfs[0].UpdateTime, kbfs[0].UID)
584587
}
585588

586589
// TODO INS-8162: the repository method (and the upstream handler) should
587590
// take an `ordering` parameter so clients can choose the sorting.
588-
q = q.Order("file.update_time DESC")
591+
// Secondary sort by UID ensures deterministic ordering for pagination.
592+
q = q.Order("file.update_time DESC, file.uid DESC")
589593

590594
// Fetch the records
591595
if err := q.Find(&files).Error; err != nil {

0 commit comments

Comments
 (0)