fix: prevent infinite loop in pagination when storage returns phantom items

- Fix FileSystemStorage counting non-existent files in totalCount
- Add safety check in BaseStorage to prevent hasMore:true with empty items
- Ensure pagination terminates correctly even with corrupted storage state
This commit is contained in:
David Snelling 2025-09-16 10:35:07 -07:00
parent 8f7eed05e0
commit 5f10f8d9ab
10 changed files with 340 additions and 22 deletions

View file

@ -414,10 +414,15 @@ export abstract class BaseStorage extends BaseStorageAdapter {
// Apply offset if needed (some adapters might not support offset)
const items = result.items.slice(offset)
// CRITICAL SAFETY CHECK: Prevent infinite loops
// If we have no items but hasMore is true, force hasMore to false
// This prevents pagination bugs from causing infinite loops
const safeHasMore = items.length > 0 ? result.hasMore : false
return {
items,
totalCount: result.totalCount || totalCount,
hasMore: result.hasMore,
hasMore: safeHasMore,
nextCursor: result.nextCursor
}
}
@ -606,10 +611,15 @@ export abstract class BaseStorage extends BaseStorageAdapter {
// Apply offset if needed (some adapters might not support offset)
const items = result.items.slice(offset)
// CRITICAL SAFETY CHECK: Prevent infinite loops
// If we have no items but hasMore is true, force hasMore to false
// This prevents pagination bugs from causing infinite loops
const safeHasMore = items.length > 0 ? result.hasMore : false
return {
items,
totalCount: result.totalCount || totalCount,
hasMore: result.hasMore,
hasMore: safeHasMore,
nextCursor: result.nextCursor
}
}