@@ -10,9 +10,14 @@ import {
1010 type SidebarSessionSortMode ,
1111 type SidebarSessionStatusFilter ,
1212} from "./app-sidebar-session-types.ts" ;
13+ import { sessionAttentionSubtitle } from "./session-attention-presentation.ts" ;
1314import { resolveSidebarSessionSubtitle } from "./session-row-subtitle.ts" ;
1415
1516const SIDEBAR_CREATED_ORDER_CAP = 1_000 ;
17+ // Ambient subtitle sources (observer digest, narration, work path) race at
18+ // event rate; without a floor the line swaps A->B->A within a second. Matches
19+ // the narration throttle so replacement cadence stays consistent.
20+ const SIDEBAR_SUBTITLE_MIN_DISPLAY_MS = 2_000 ;
1621
1722type SidebarExpansionMode = "collapsed-by-user" | "expanded" | "expanded-fully" ;
1823type SidebarSubtitleParams = Parameters < typeof resolveSidebarSessionSubtitle > [ 0 ] ;
@@ -68,18 +73,26 @@ function baselineSessionRows(rows: readonly SidebarRecentSession[], limit: numbe
6873 } ) ;
6974}
7075
71- function sidebarRunIdentity ( session : SidebarRecentSession ) : string {
72- return `${ session . sessionId ?? "" } \u0000${ session . activeRunIds ?. join ( "\u0000" ) ?? "" } ` ;
76+ /** Attention, agent-declared status, and the queued explanation are messages
77+ * the operator must act on; they replace a held subtitle immediately. */
78+ function isOperatorCriticalSubtitle ( session : SidebarRecentSession ) : boolean {
79+ return Boolean (
80+ sessionAttentionSubtitle ( session . attention ) ||
81+ session . agentStatusNote ||
82+ ( session . hasActiveRun && session . status === "queued" ) ,
83+ ) ;
7384}
7485
7586export class SidebarSessionProjection {
87+ constructor ( private readonly now : ( ) => number = ( ) => Date . now ( ) ) { }
88+
7689 private readonly observedOrder = new Map < string , number > ( ) ;
7790 private nextCreatedOrder = 0 ;
7891 private readonly stickySections = new Map < string , Set < string > > ( ) ;
7992 private readonly childModes = new Map < string , SidebarExpansionMode > ( ) ;
8093 private readonly heldSubtitles = new Map <
8194 string ,
82- { identity : string ; value : SidebarSubtitleValue ; catalogValue ?: SidebarSubtitleValue }
95+ { value : SidebarSubtitleValue ; catalogValue ?: SidebarSubtitleValue ; shownAt : number }
8396 > ( ) ;
8497 private previousInput : Pick <
8598 SidebarProjectionInput ,
@@ -307,15 +320,18 @@ export class SidebarSessionProjection {
307320 }
308321
309322 resolveSubtitle ( params : SidebarSubtitleParams ) : SidebarSubtitleValue {
310- const fresh = resolveSidebarSessionSubtitle ( params ) ;
311- if ( fresh . subtitle || ! params . session . hasActiveRun || ! params . showPreview ) {
312- return fresh ;
323+ if ( ! params . session . hasActiveRun || ! params . showPreview ) {
324+ return resolveSidebarSessionSubtitle ( params ) ;
313325 }
326+ // While a run is live the held value is the display: observeSubtitle
327+ // refreshed it this update pass, applying the minimum-display floor.
314328 const held = this . heldSubtitles . get ( params . session . key ) ;
315- if ( held ?. identity !== sidebarRunIdentity ( params . session ) ) {
316- return fresh ;
329+ if ( ! held ) {
330+ return resolveSidebarSessionSubtitle ( params ) ;
317331 }
318- return params . hasDisplay ? ( held . catalogValue ?? fresh ) : held . value ;
332+ return params . hasDisplay
333+ ? ( held . catalogValue ?? resolveSidebarSessionSubtitle ( params ) )
334+ : held . value ;
319335 }
320336
321337 private observeSubtitle (
@@ -326,10 +342,6 @@ export class SidebarSessionProjection {
326342 this . heldSubtitles . delete ( session . key ) ;
327343 return ;
328344 }
329- const identity = sidebarRunIdentity ( session ) ;
330- if ( this . heldSubtitles . get ( session . key ) ?. identity !== identity ) {
331- this . heldSubtitles . delete ( session . key ) ;
332- }
333345 if ( ! environment . sidebarLiveActivity && this . heldSubtitles . get ( session . key ) ?. value . narration ) {
334346 this . heldSubtitles . delete ( session . key ) ;
335347 }
@@ -343,13 +355,26 @@ export class SidebarSessionProjection {
343355 observerDigest : environment . observerDigests . get ( session . key ) ?? null ,
344356 } satisfies SidebarSubtitleParams ;
345357 const value = resolveSidebarSessionSubtitle ( params ) ;
346- if ( value . subtitle ) {
347- const catalogValue = resolveSidebarSessionSubtitle ( { ...params , hasDisplay : true } ) ;
348- this . heldSubtitles . set ( session . key , {
349- identity,
350- value,
351- ...( catalogValue . subtitle ? { catalogValue } : { } ) ,
352- } ) ;
358+ if ( ! value . subtitle ) {
359+ // Transient gaps between event updates keep the last shown line; the
360+ // hold dies with the run (the hasActiveRun branch above).
361+ return ;
362+ }
363+ const held = this . heldSubtitles . get ( session . key ) ;
364+ const now = this . now ( ) ;
365+ const replacing = held !== undefined && held . value . subtitle !== value . subtitle ;
366+ if (
367+ replacing &&
368+ now - held . shownAt < SIDEBAR_SUBTITLE_MIN_DISPLAY_MS &&
369+ ! isOperatorCriticalSubtitle ( session )
370+ ) {
371+ return ;
353372 }
373+ const catalogValue = resolveSidebarSessionSubtitle ( { ...params , hasDisplay : true } ) ;
374+ this . heldSubtitles . set ( session . key , {
375+ value,
376+ ...( catalogValue . subtitle ? { catalogValue } : { } ) ,
377+ shownAt : held !== undefined && ! replacing ? held . shownAt : now ,
378+ } ) ;
354379 }
355380}
0 commit comments