@@ -42,6 +42,12 @@ const LegacyNotificationStore = Persistence.struct({
4242 started : Schema . optional ( Schema . Finite ) ,
4343} )
4444
45+ // Sessions marked unread by hand. This is client-local on purpose: the server's viewed watermark is monotone, so a
46+ // manual mark is merged into the computed state instead of rewriting `time.viewed`.
47+ const ManualUnreadStore = Persistence . struct ( {
48+ sessions : Persistence . array ( Schema . String ) ,
49+ } )
50+
4551// The carry-over waits this long after connecting so it doesn't compete with startup requests.
4652const LEGACY_MIGRATION_DELAY = 5_000
4753
@@ -74,6 +80,8 @@ export function createServerNotificationState(input: {
7480
7581 const legacyTarget = Persist . serverGlobal ( input . sdk . scope , "notification" )
7682 const [ legacy , setLegacy , , legacyReady ] = persisted ( legacyTarget , LegacyNotificationStore , { } , platform )
83+ const manualTarget = Persist . serverGlobal ( input . sdk . scope , "notification.manual-unread" )
84+ const [ manual , setManual ] = persisted ( manualTarget , ManualUnreadStore , { sessions : [ ] } , platform )
7785 const started = Date . now ( )
7886 const legacyStarted = ( ) => legacy . started ?? started
7987
@@ -89,11 +97,17 @@ export function createServerNotificationState(input: {
8997 // Idle watermarks the carry-over reported, so rows drawn from an older fetched copy of a session stay read.
9098 const legacyReported = new Map < string , number > ( )
9199
100+ const manuallyUnread = ( sessionID : string ) => manual . sessions . includes ( sessionID )
101+
92102 const unread = ( session ?: Pick < SessionInfo , "id" | "time" > ) => {
93- const idle = session ?. time . idle
103+ if ( ! session ) return false
104+
105+ if ( manuallyUnread ( session . id ) ) return true
106+
107+ const idle = session . time . idle
94108
95109 // Until the old list is read, a session it had read could flash unread.
96- if ( ! legacyReady ( ) || ! session || idle === undefined || ! isSessionUnread ( session ) ) return false
110+ if ( ! legacyReady ( ) || idle === undefined || ! isSessionUnread ( session ) ) return false
97111
98112 if ( idle <= ( legacyReported . get ( session . id ) ?? - Infinity ) ) return false
99113 const unseen = legacyUnseen ( )
@@ -177,6 +191,10 @@ export function createServerNotificationState(input: {
177191 const view = ( sessionID : string ) => {
178192 void state . viewRetry
179193 const root = input . data . session . root ( sessionID )
194+
195+ // Opening a manually marked session clears the mark before the usual viewed-watermark logic runs.
196+ if ( manuallyUnread ( root ) ) setManual ( "sessions" , ( sessions ) => sessions . filter ( ( id ) => id !== root ) )
197+
180198 const session = input . data . session . get ( root )
181199 const idle = session ?. time . idle
182200
@@ -204,6 +222,12 @@ export function createServerNotificationState(input: {
204222 )
205223 }
206224
225+ // Only a confirmed deletion drops a mark. Rows disappearing from a filtered or paginated list are not deletions.
226+ const forget = ( sessionID : string ) => {
227+ if ( ! manual . sessions . includes ( sessionID ) ) return
228+ setManual ( "sessions" , ( sessions ) => sessions . filter ( ( id ) => id !== sessionID ) )
229+ }
230+
207231 // Loading the session keeps a background completion visible in the server-derived unread state.
208232 const lookup = async ( sessionID : string ) => {
209233 const session = input . data . session . get ( sessionID )
@@ -265,6 +289,12 @@ export function createServerNotificationState(input: {
265289 }
266290
267291 const unsub = input . sdk . event . listen ( ( event ) => {
292+ if ( event . type === "session.deleted" ) {
293+ forget ( event . data . sessionID )
294+
295+ return
296+ }
297+
268298 if ( event . type === "session.execution.failed" ) {
269299 handleSessionError ( event . data . sessionID , event . id )
270300
@@ -290,6 +320,13 @@ export function createServerNotificationState(input: {
290320 } ,
291321 /** Reports the session's latest idle transition to the server as viewed. */
292322 markViewed : view ,
323+ /** Marks the session's root unread until this client views it. */
324+ markUnread ( sessionID : string ) {
325+ const root = input . data . session . root ( sessionID )
326+
327+ if ( manuallyUnread ( root ) ) return
328+ setManual ( "sessions" , ( sessions ) => [ ...sessions , root ] )
329+ } ,
293330 } ,
294331 project : {
295332 hasUnread ( directory : string ) {
0 commit comments