@@ -205,7 +205,21 @@ export const {
205205 . then ( ( x ) => ( x . data ?? [ ] ) . toSorted ( ( a , b ) => a . id . localeCompare ( b . id ) ) )
206206 }
207207
208+ // Fork(lowmem): narrow the union payload without casting the event itself;
209+ // late events for a deleted session must not resurrect its buckets.
210+ const tombstoneSessionID = ( properties : unknown ) : string | undefined => {
211+ if ( typeof properties !== "object" || properties === null ) return undefined
212+ const record = properties as Record < string , unknown >
213+ if ( typeof record . sessionID === "string" ) return record . sessionID
214+ const info = record . info
215+ if ( typeof info === "object" && info !== null && typeof ( info as Record < string , unknown > ) . id === "string" ) {
216+ return ( info as Record < string , unknown > ) . id as string
217+ }
218+ return undefined
219+ }
208220 event . subscribe ( ( event , { directory, workspace } ) => {
221+ const tombstoneID = tombstoneSessionID ( event . properties )
222+ if ( tombstoneID !== undefined && payloadEviction . isDeleted ( tombstoneID ) ) return
209223 switch ( event . type ) {
210224 case "server.instance.disposed" :
211225 void bootstrap ( )
@@ -308,19 +322,15 @@ export const {
308322
309323 case "session.deleted" : {
310324 forgetInboundChild ( event . properties . info . id )
311- // Fork(lowmem): drop payload buckets too, or deleted sessions leak
312- // in the store (upstream #12351).
313- for ( const msg of store . message [ event . properties . info . id ] ?? [ ] ) {
314- partDeltaBuffer . dropMessage ( msg . id )
325+ // Fork(lowmem): full cleanup — payload and bookkeeping buckets die
326+ // with the session, and the ID is tombstoned so late events and
327+ // in-flight syncs cannot resurrect them (upstream #12351).
328+ for ( const msg of store . message [ event . properties . info . id ] ?? [ ] ) partDeltaBuffer . dropMessage ( msg . id )
329+ for ( const [ messageID , parts ] of Object . entries ( store . part ) ) {
330+ if ( parts . some ( ( part ) => part . sessionID === event . properties . info . id ) )
331+ partDeltaBuffer . dropMessage ( messageID )
315332 }
316- setStore (
317- produce ( ( draft ) => {
318- for ( const msg of draft . message [ event . properties . info . id ] ?? [ ] ) delete draft . part [ msg . id ]
319- delete draft . message [ event . properties . info . id ]
320- delete draft . session_diff [ event . properties . info . id ]
321- delete draft . session_status [ event . properties . info . id ]
322- } ) ,
323- )
333+ payloadEviction . forgetSession ( event . properties . info . id )
324334 const result = search ( store . session , event . properties . info . id , ( s ) => s . id )
325335 if ( result . found ) {
326336 setStore (
@@ -433,6 +443,14 @@ export const {
433443 case "message.removed" : {
434444 if ( payloadEviction . isEvicted ( event . properties . sessionID ) ) break
435445 touchMessage ( event . properties . sessionID , event . properties . messageID )
446+ // Fork(lowmem): the part bucket and pending deltas die with the
447+ // message, or removal orphans them forever.
448+ partDeltaBuffer . dropMessage ( event . properties . messageID )
449+ setStore (
450+ produce ( ( draft ) => {
451+ delete draft . part [ event . properties . messageID ]
452+ } ) ,
453+ )
436454 const messages = store . message [ event . properties . sessionID ]
437455 if ( ! messages ) break
438456 const index = messages . findIndex ( ( message ) => message . id === event . properties . messageID )
@@ -672,6 +690,9 @@ export const {
672690 return last . time . completed ? "idle" : "working"
673691 } ,
674692 async sync ( sessionID : string ) {
693+ // Fork(lowmem): deleted sessions stay deleted; viewed() would re-arm
694+ // the recency clock for a session whose payloads must not return.
695+ if ( payloadEviction . isDeleted ( sessionID ) ) return
675696 payloadEviction . viewed ( sessionID )
676697 if ( fullSyncedSessions . has ( sessionID ) ) return
677698 const syncing = syncingSessions . get ( sessionID )
@@ -685,6 +706,9 @@ export const {
685706 sdk . client . session . todo ( { sessionID } ) ,
686707 sdk . client . session . diff ( { sessionID } ) ,
687708 ] )
709+ // Fork(lowmem): the session may have been deleted while fetching;
710+ // writing would resurrect its payloads.
711+ if ( payloadEviction . isDeleted ( sessionID ) ) return
688712 setStore (
689713 produce ( ( draft ) => {
690714 const match = search ( draft . session , sessionID , ( s ) => s . id )
@@ -744,7 +768,9 @@ export const {
744768 hydratingSessions . delete ( sessionID )
745769 // Fork(lowmem): re-arm after tracker cleanup (protection checks the
746770 // tracker maps), so a failed hydration gates late events again.
747- if ( store . message [ sessionID ] === undefined ) payloadEviction . remarkEvicted ( sessionID )
771+ if ( ! payloadEviction . isDeleted ( sessionID ) && store . message [ sessionID ] === undefined ) {
772+ payloadEviction . remarkEvicted ( sessionID )
773+ }
748774 payloadEviction . compact ( )
749775 } )
750776 syncingSessions . set ( sessionID , task )
0 commit comments