@@ -289,6 +289,7 @@ function rememberSingleRowChildSessionCandidateCacheEntry(
289289
290290function buildStoreChildSessionCandidateIndex (
291291 store : Record < string , SessionEntry > | null | undefined ,
292+ selectedParents ?: ReadonlySet < string > ,
292293) : Map < string , string [ ] > {
293294 const childSessionsByKey = new Map < string , string [ ] > ( ) ;
294295 if ( ! store ) {
@@ -303,7 +304,9 @@ function buildStoreChildSessionCandidateIndex(
303304 normalizeOptionalString ( entry . parentSessionKey ) ,
304305 ] . filter ( ( value ) : value is string => Boolean ( value ) && value !== key ) ;
305306 for ( const parentKey of parentKeys ) {
306- addChildSessionKey ( childSessionsByKey , parentKey , key ) ;
307+ if ( ! selectedParents || selectedParents . has ( parentKey ) ) {
308+ addChildSessionKey ( childSessionsByKey , parentKey , key ) ;
309+ }
307310 }
308311 }
309312 return childSessionsByKey ;
@@ -412,92 +415,72 @@ export function isCurrentSessionChildOwner(params: {
412415 ) ;
413416}
414417
415- export function buildStoreChildSessionIndex (
416- store : Record < string , SessionEntry > ,
417- now = Date . now ( ) ,
418- subagentRuns ?: SessionListRowContext [ "subagentRuns" ] ,
419- ) : Map < string , string [ ] > {
420- const childSessionsByKey = new Map < string , string [ ] > ( ) ;
421- for ( const [ key , entry ] of Object . entries ( store ) ) {
422- if ( ! entry ) {
423- continue ;
424- }
425- const parentKeys = [
426- normalizeOptionalString ( entry . spawnedBy ) ,
427- normalizeOptionalString ( entry . parentSessionKey ) ,
428- ] . filter ( ( value ) : value is string => Boolean ( value ) && value !== key ) ;
429- if ( parentKeys . length === 0 ) {
430- continue ;
431- }
432- const latest = subagentRuns
433- ? subagentRuns . getDisplaySubagentRun ( key )
434- : getSessionDisplaySubagentRunByChildSessionKey ( key ) ;
435- let latestControllerSessionKey : string | undefined ;
436- if ( latest ) {
437- latestControllerSessionKey =
438- normalizeOptionalString ( latest . controllerSessionKey ) ||
439- normalizeOptionalString ( latest . requesterSessionKey ) ;
440- if (
441- ! shouldKeepSubagentRunChildLink ( latest , {
442- activeDescendants : subagentRuns
443- ? subagentRuns . countActiveDescendantRuns ( key )
444- : countActiveDescendantRuns ( key ) ,
445- now,
446- } )
447- ) {
448- continue ;
449- }
450- } else if ( ! shouldKeepStoreOnlyChildLink ( entry , now ) ) {
451- continue ;
452- }
453- for ( const parentKey of parentKeys ) {
454- if (
455- latestControllerSessionKey &&
456- ! isCurrentSessionChildOwner ( {
457- entry,
458- ownerSessionKey : parentKey ,
459- controllerSessionKey : latestControllerSessionKey ,
460- } )
461- ) {
462- continue ;
463- }
464- addChildSessionKey ( childSessionsByKey , parentKey , key ) ;
418+ /** Prepare only selected parents; a supplied candidate map retains exact-row cache ownership. */
419+ export function buildStoreChildSessionIndex ( params : {
420+ store : Record < string , SessionEntry > ;
421+ keys : readonly string [ ] ;
422+ now : number ;
423+ subagentRuns ?: SessionListRowContext [ "subagentRuns" ] ;
424+ candidates ?: ReadonlyMap < string , readonly string [ ] > ;
425+ excludedChildKeys ?: ReadonlySet < string > ;
426+ requireCurrentController ?: boolean ;
427+ } ) : Map < string , string [ ] > {
428+ const children = new Map < string , string [ ] > ( ) ;
429+ if ( params . keys . length === 0 ) {
430+ return children ;
431+ }
432+ const candidates =
433+ params . candidates ?? buildStoreChildSessionCandidateIndex ( params . store , new Set ( params . keys ) ) ;
434+ for ( const key of params . keys ) {
435+ const childKeys = resolveStoreChildSessionKeysFromCandidates ( { ...params , key, candidates } ) ;
436+ if ( childKeys ) {
437+ children . set ( key , childKeys ) ;
465438 }
466439 }
467- return childSessionsByKey ;
440+ return children ;
468441}
469442
470- export function resolveStoreChildSessionKeysFromCandidates ( params : {
443+ function resolveStoreChildSessionKeysFromCandidates ( params : {
471444 store : Record < string , SessionEntry > ;
472445 key : string ;
473446 now : number ;
474447 candidates : ReadonlyMap < string , readonly string [ ] > ;
448+ subagentRuns ?: SessionListRowContext [ "subagentRuns" ] ;
449+ excludedChildKeys ?: ReadonlySet < string > ;
450+ requireCurrentController ?: boolean ;
475451} ) : string [ ] | undefined {
476452 const childSessionKeys : string [ ] = [ ] ;
477453 for ( const childKey of params . candidates . get ( params . key ) ?? [ ] ) {
454+ if ( params . excludedChildKeys ?. has ( childKey ) ) {
455+ continue ;
456+ }
478457 const entry = params . store [ childKey ] ;
479458 if ( ! entry ) {
480459 continue ;
481460 }
482- const latest = getSessionDisplaySubagentRunByChildSessionKey ( childKey ) ;
461+ const latest = params . subagentRuns
462+ ? params . subagentRuns . getDisplaySubagentRun ( childKey )
463+ : getSessionDisplaySubagentRunByChildSessionKey ( childKey ) ;
483464 if ( latest ) {
484465 const latestControllerSessionKey =
485466 normalizeOptionalString ( latest . controllerSessionKey ) ||
486467 normalizeOptionalString ( latest . requesterSessionKey ) ;
487- if (
488- ! isCurrentSessionChildOwner ( {
489- entry,
490- ownerSessionKey : params . key ,
491- controllerSessionKey : latestControllerSessionKey ,
492- } )
493- ) {
468+ const matchesOwner = isCurrentSessionChildOwner ( {
469+ entry,
470+ ownerSessionKey : params . key ,
471+ controllerSessionKey : latestControllerSessionKey ,
472+ } ) ;
473+ if ( params . requireCurrentController && ! matchesOwner ) {
494474 continue ;
495475 }
496476 if (
497477 ! shouldKeepSubagentRunChildLink ( latest , {
498- activeDescendants : countActiveDescendantRuns ( childKey ) ,
478+ activeDescendants : params . subagentRuns
479+ ? params . subagentRuns . countActiveDescendantRuns ( childKey )
480+ : countActiveDescendantRuns ( childKey ) ,
499481 now : params . now ,
500- } )
482+ } ) ||
483+ ( latestControllerSessionKey && ! matchesOwner )
501484 ) {
502485 continue ;
503486 }
0 commit comments