MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / applySortAndLimit

Function applySortAndLimit

source code/utils/listSessionsImpl.ts:237–273  ·  view source on GitHub ↗
(
  candidates: Candidate[],
  limit: number | undefined,
  offset: number,
)

Source from the content-addressed store, hash-verified

235}
236
237async function applySortAndLimit(
238 candidates: Candidate[],
239 limit: number | undefined,
240 offset: number,
241): Promise<SessionInfo[]> {
242 candidates.sort(compareDesc)
243
244 const sessions: SessionInfo[] = []
245 // limit: 0 means "no limit" (matches getSessionMessages semantics)
246 const want = limit && limit > 0 ? limit : Infinity
247 let skipped = 0
248 // Dedup post-filter: since candidates are sorted mtime-desc, the first
249 // non-null read per sessionId is naturally the newest valid copy.
250 // Pre-filter dedup would drop a session entirely if its newest-mtime
251 // copy is unreadable/empty, diverging from the no-stat readAllAndSort path.
252 const seen = new Set<string>()
253
254 for (let i = 0; i < candidates.length && sessions.length < want; ) {
255 const batchEnd = Math.min(i + READ_BATCH_SIZE, candidates.length)
256 const batch = candidates.slice(i, batchEnd)
257 const results = await Promise.all(batch.map(readCandidate))
258 for (let j = 0; j < results.length && sessions.length < want; j++) {
259 i++
260 const r = results[j]
261 if (!r) continue
262 if (seen.has(r.sessionId)) continue
263 seen.add(r.sessionId)
264 if (skipped < offset) {
265 skipped++
266 continue
267 }
268 sessions.push(r)
269 }
270 }
271
272 return sessions
273}
274
275/**
276 * Read-all path for when no limit/offset is set. Skips the stat pass

Callers 1

listSessionsImplFunction · 0.85

Calls 2

hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected