@@ -180,6 +180,27 @@ function compareSessionCandidatesByUpdatedAt(left: SessionCandidate, right: Sess
180180 return ( right . updatedAt ?? 0 ) - ( left . updatedAt ?? 0 ) ;
181181}
182182
183+ function selectRecentSessionCandidates (
184+ candidates : SessionCandidate [ ] ,
185+ limit : number ,
186+ ) : SessionCandidate [ ] {
187+ const selected : SessionCandidate [ ] = [ ] ;
188+ for ( const candidate of candidates ) {
189+ const insertAt = selected . findIndex (
190+ ( selectedCandidate ) => compareSessionCandidatesByUpdatedAt ( candidate , selectedCandidate ) < 0 ,
191+ ) ;
192+ if ( insertAt >= 0 ) {
193+ selected . splice ( insertAt , 0 , candidate ) ;
194+ if ( selected . length > limit ) {
195+ selected . pop ( ) ;
196+ }
197+ } else if ( selected . length < limit ) {
198+ selected . push ( candidate ) ;
199+ }
200+ }
201+ return selected ;
202+ }
203+
183204function listSessionCandidates ( storePath : string , agentId ?: string ) {
184205 return (
185206 listSessionEntries ( {
@@ -193,7 +214,6 @@ function listSessionCandidates(storePath: string, agentId?: string) {
193214 entry,
194215 updatedAt : entry ?. updatedAt ?? null ,
195216 } ) )
196- . toSorted ( compareSessionCandidatesByUpdatedAt )
197217 ) ;
198218}
199219
@@ -471,9 +491,10 @@ export async function getStatusSummary(
471491 agentList . agents . map ( async ( agent ) => {
472492 const storePath = resolveStorePath ( cfg . session ?. store , { agentId : agent . id } ) ;
473493 const candidates = loadSessionCandidates ( storePath , agent . id ) ;
474- const sessions = await buildSessionRows ( candidates . slice ( 0 , RECENT_SESSION_LIMIT ) , {
475- agentIdOverride : agent . id ,
476- } ) ;
494+ const sessions = await buildSessionRows (
495+ selectRecentSessionCandidates ( candidates , RECENT_SESSION_LIMIT ) ,
496+ { agentIdOverride : agent . id } ,
497+ ) ;
477498 return {
478499 agentId : agent . id ,
479500 path : storePath ,
@@ -492,9 +513,10 @@ export async function getStatusSummary(
492513 source . storePath ,
493514 pathCounts . get ( source . storePath ) === 1 ? source . agentId : undefined ,
494515 ) ,
495- )
496- . toSorted ( ( a , b ) => ( b . updatedAt ?? 0 ) - ( a . updatedAt ?? 0 ) ) ;
497- const recent = await buildSessionRows ( allSessions . slice ( 0 , RECENT_SESSION_LIMIT ) ) ;
516+ ) ;
517+ const recent = await buildSessionRows (
518+ selectRecentSessionCandidates ( allSessions , RECENT_SESSION_LIMIT ) ,
519+ ) ;
498520 const totalSessions = allSessions . length ;
499521
500522 const summary : StatusSummary = {
0 commit comments