@@ -14,22 +14,22 @@ import {
1414 type CatalogSessionContinuedDetail ,
1515} from "../lib/sessions/catalog-key.ts" ;
1616import type { SessionCapability } from "../lib/sessions/index.ts" ;
17- import { normalizeAgentId } from "../lib/sessions/session-key.ts" ;
17+ import { areUiSessionKeysEquivalent , normalizeAgentId } from "../lib/sessions/session-key.ts" ;
1818import { SubscriptionsController } from "../lit/subscriptions-controller.ts" ;
1919import {
2020 collectKnownSessionRows ,
2121 evictArchivedSessionLineage ,
2222 fetchChildSessionRows ,
2323 fetchSessionLineage ,
24+ preserveActiveSessionLineageRows ,
2425 publishActiveSessionLineage ,
2526} from "./app-sidebar-child-session-data.ts" ;
2627import { SessionCatalogLiveState } from "./app-sidebar-session-catalog-live.ts" ;
2728import { bindAdoptedCatalogSession } from "./app-sidebar-session-catalogs.ts" ;
28- import {
29- resolveSidebarSessionsScrollState ,
30- type SidebarSessionMutationScope ,
31- type SidebarSessionStatusFilter ,
32- type SidebarSessionsScrollState ,
29+ import type {
30+ SidebarSessionMutationScope ,
31+ SidebarSessionStatusFilter ,
32+ SidebarSessionsScrollState ,
3333} from "./app-sidebar-session-types.ts" ;
3434import { createPanelRefreshStatus , type PanelRefreshStatus } from "./panel-refresh-status.ts" ;
3535import {
@@ -49,6 +49,7 @@ import {
4949 subscribeFilteredSidebarSessions ,
5050 subscribeSessionDataGatewayEvents ,
5151} from "./session-data-controller-events.ts" ;
52+ import { SessionDataScrollController } from "./session-data-scroll-controller.ts" ;
5253
5354/** Gateway-backed session-list and external-catalog data ownership. */
5455export class SessionDataController implements ReactiveController , SessionCatalogDataOwner {
@@ -64,7 +65,7 @@ export class SessionDataController implements ReactiveController, SessionCatalog
6465 failedChildSessionKeys : ReadonlySet < string > = new Set ( ) ;
6566 loadingChildSessionKeys : ReadonlySet < string > = new Set ( ) ;
6667 activeSessionLineageRoot : GatewaySessionRow | null = null ;
67- sessionsScrollState : SidebarSessionsScrollState = "none" ;
68+ activeSessionLineageSelectedRow : GatewaySessionRow | null = null ;
6869 sessionMutationError : string | null = null ;
6970 presencePayload : PresencePayload | undefined ;
7071 presenceInstanceId ?: string ;
@@ -96,9 +97,7 @@ export class SessionDataController implements ReactiveController, SessionCatalog
9697 private gatewayConnected = false ;
9798 // Bind mutation completions to one epoch so stale failures cannot cross reconnects.
9899 private sessionMutationEpoch = 0 ;
99- private sessionsScrollElement : HTMLElement | null = null ;
100- private sessionsScrollResizeObserver : ResizeObserver | null = null ;
101- private sessionsScrollStateFrame : number | null = null ;
100+ private readonly scroll = new SessionDataScrollController ( ( ) => this . notify ( ) ) ;
102101 private approvalBadgeQueue : ApplicationContext < RouteId > [ "overlays" ] [ "snapshot" ] [ "approvalQueue" ] =
103102 [ ] ;
104103 private approvalBadges : ApprovalBadgeSnapshot = deriveApprovalBadgeSnapshot ( [ ] ) ;
@@ -182,7 +181,7 @@ export class SessionDataController implements ReactiveController, SessionCatalog
182181
183182 hostUpdated ( ) : void {
184183 this . synchronizeSessionScope ( ) ;
185- this . syncSessionsScrollObserver ( ) ;
184+ this . scroll . synchronize ( this . host ) ;
186185 this . updateSessionCatalogData ( true ) ;
187186 }
188187
@@ -195,13 +194,7 @@ export class SessionDataController implements ReactiveController, SessionCatalog
195194 this . gatewayClient = null ;
196195 this . gatewayConnected = false ;
197196 this . retireSessionCatalogData ( ) ;
198- this . sessionsScrollResizeObserver ?. disconnect ( ) ;
199- this . sessionsScrollResizeObserver = null ;
200- this . sessionsScrollElement = null ;
201- if ( this . sessionsScrollStateFrame !== null ) {
202- cancelAnimationFrame ( this . sessionsScrollStateFrame ) ;
203- this . sessionsScrollStateFrame = null ;
204- }
197+ this . scroll . dispose ( ) ;
205198 if ( this . activeSessionLineageRetryTimer ) {
206199 globalThis . clearTimeout ( this . activeSessionLineageRetryTimer ) ;
207200 this . activeSessionLineageRetryTimer = null ;
@@ -357,54 +350,30 @@ export class SessionDataController implements ReactiveController, SessionCatalog
357350 return loadMoreSessionCatalogData ( this , catalogId ) ;
358351 }
359352
360- private syncSessionsScrollObserver ( ) : void {
361- const element = this . host . querySelector ( ".sidebar-shell__body" ) as HTMLElement | null ;
362- if ( element !== this . sessionsScrollElement ) {
363- this . sessionsScrollResizeObserver ?. disconnect ( ) ;
364- this . sessionsScrollElement = element ;
365- this . sessionsScrollResizeObserver = null ;
366- if ( element && typeof ResizeObserver === "function" ) {
367- this . sessionsScrollResizeObserver = new ResizeObserver ( ( ) =>
368- this . updateSessionsScrollState ( element ) ,
369- ) ;
370- this . sessionsScrollResizeObserver . observe ( element ) ;
371- }
372- }
373- if ( element ) {
374- this . scheduleSessionsScrollStateSync ( ) ;
375- }
376- }
377-
378- // One rAF-coalesced scroll read rides paint layout instead of flushing every update.
379- private scheduleSessionsScrollStateSync ( ) : void {
380- if ( this . sessionsScrollStateFrame !== null ) {
381- return ;
382- }
383- this . sessionsScrollStateFrame = requestAnimationFrame ( ( ) => {
384- this . sessionsScrollStateFrame = null ;
385- const element = this . sessionsScrollElement ;
386- if ( element ?. isConnected ) {
387- this . updateSessionsScrollState ( element ) ;
388- }
389- } ) ;
353+ get sessionsScrollState ( ) : SidebarSessionsScrollState {
354+ return this . scroll . state ;
390355 }
391356
392357 updateSessionsScrollState ( element : HTMLElement ) : void {
393- const nextState = resolveSidebarSessionsScrollState ( element ) ;
394- if ( nextState !== this . sessionsScrollState ) {
395- this . sessionsScrollState = nextState ;
396- this . notify ( ) ;
397- }
358+ this . scroll . update ( element ) ;
398359 }
399360
400- private resetChildSessionState ( ) : void {
361+ private resetChildSessionState ( options : { preserveActiveLineage ?: boolean } = { } ) : void {
401362 this . childSessionGeneration += 1 ;
402- this . childSessionRowsByParent = { } ;
363+ this . childSessionRowsByParent = options . preserveActiveLineage
364+ ? preserveActiveSessionLineageRows (
365+ this . activeSessionLineageRouteKey ,
366+ this . childSessionRowsByParent ,
367+ )
368+ : { } ;
403369 this . loadedChildSessionKeys = new Set ( ) ;
404370 this . failedChildSessionKeys = new Set ( ) ;
405371 this . loadingChildSessionKeys = new Set ( ) ;
406- this . activeSessionLineageRoot = null ;
407- this . activeSessionLineageRouteKey = null ;
372+ if ( options . preserveActiveLineage !== true ) {
373+ this . activeSessionLineageRoot = null ;
374+ this . activeSessionLineageSelectedRow = null ;
375+ this . activeSessionLineageRouteKey = null ;
376+ }
408377 this . activeSessionLineageLoaded = false ;
409378 this . activeSessionLineageRequestToken = null ;
410379 if ( this . activeSessionLineageRetryTimer ) {
@@ -416,9 +385,20 @@ export class SessionDataController implements ReactiveController, SessionCatalog
416385 private readonly updateSessions = ( sessions : SessionCapability ) => {
417386 if ( this . childSessionCanonicalListRevision !== sessions . canonicalListRevision ) {
418387 this . childSessionCanonicalListRevision = sessions . canonicalListRevision ;
388+ const routeKey = this . activeSessionLineageRouteKey ;
389+ const routedRow = routeKey
390+ ? this . sessionsResult ?. sessions . find ( ( row ) => areUiSessionKeysEquivalent ( row . key , routeKey ) )
391+ : undefined ;
392+ if ( routedRow ) {
393+ // A canonical active-list refresh can remove the selected row before
394+ // its archived descriptor is reloaded. Keep the enriched pre-refresh
395+ // row as the routed presentation baseline so no intermediate render
396+ // falls back to the raw session key or "New session".
397+ this . activeSessionLineageSelectedRow = routedRow ;
398+ }
419399 // The canonical root list advances after session events, but excludes hidden children.
420400 // Drop child snapshots so expanded parents refetch live terminal state.
421- this . resetChildSessionState ( ) ;
401+ this . resetChildSessionState ( { preserveActiveLineage : true } ) ;
422402 this . notify ( ) ;
423403 }
424404 const snapshot = sessions . state ;
@@ -619,12 +599,16 @@ export class SessionDataController implements ReactiveController, SessionCatalog
619599
620600 async loadActiveSessionLineage ( sessionKey : string ) : Promise < void > {
621601 const normalizedKey = sessionKey . trim ( ) ;
622- if ( normalizedKey !== this . activeSessionLineageRouteKey ) {
602+ if (
603+ ! this . activeSessionLineageRouteKey ||
604+ ! areUiSessionKeysEquivalent ( normalizedKey , this . activeSessionLineageRouteKey )
605+ ) {
623606 evictArchivedSessionLineage ( this , this . activeSessionLineageRouteKey ) ;
624607 this . activeSessionLineageRouteKey = normalizedKey ;
625608 this . activeSessionLineageLoaded = false ;
626609 this . activeSessionLineageRequestToken = null ;
627610 this . activeSessionLineageRoot = null ;
611+ this . activeSessionLineageSelectedRow = null ;
628612 if ( this . activeSessionLineageRetryTimer ) {
629613 globalThis . clearTimeout ( this . activeSessionLineageRetryTimer ) ;
630614 this . activeSessionLineageRetryTimer = null ;
0 commit comments