@@ -13,6 +13,7 @@ import type {
1313} from "@opencode-ai/sdk/v2/client"
1414import type { State , VcsCache } from "./types"
1515import { trimSessions } from "./session-trim"
16+ import { dropSessionCaches } from "./session-cache"
1617
1718export function applyGlobalEvent ( input : {
1819 event : { type : string ; properties ?: unknown }
@@ -40,37 +41,44 @@ export function applyGlobalEvent(input: {
4041}
4142
4243function cleanupSessionCaches (
43- store : Store < State > ,
4444 setStore : SetStoreFunction < State > ,
4545 sessionID : string ,
4646 setSessionTodo ?: ( sessionID : string , todos : Todo [ ] | undefined ) => void ,
4747) {
4848 if ( ! sessionID ) return
49- const hasAny =
50- store . message [ sessionID ] !== undefined ||
51- store . session_diff [ sessionID ] !== undefined ||
52- store . todo [ sessionID ] !== undefined ||
53- store . permission [ sessionID ] !== undefined ||
54- store . question [ sessionID ] !== undefined ||
55- store . session_status [ sessionID ] !== undefined
5649 setSessionTodo ?.( sessionID , undefined )
57- if ( ! hasAny ) return
5850 setStore (
5951 produce ( ( draft ) => {
60- const messages = draft . message [ sessionID ]
61- if ( messages ) {
62- for ( const message of messages ) {
63- const id = message ?. id
64- if ( ! id ) continue
65- delete draft . part [ id ]
66- }
67- }
68- delete draft . message [ sessionID ]
69- delete draft . session_diff [ sessionID ]
70- delete draft . todo [ sessionID ]
71- delete draft . permission [ sessionID ]
72- delete draft . question [ sessionID ]
73- delete draft . session_status [ sessionID ]
52+ dropSessionCaches ( draft , [ sessionID ] )
53+ } ) ,
54+ )
55+ }
56+
57+ export function cleanupDroppedSessionCaches (
58+ store : Store < State > ,
59+ setStore : SetStoreFunction < State > ,
60+ next : Session [ ] ,
61+ setSessionTodo ?: ( sessionID : string , todos : Todo [ ] | undefined ) => void ,
62+ ) {
63+ const keep = new Set ( next . map ( ( item ) => item . id ) )
64+ const stale = [
65+ ...Object . keys ( store . message ) ,
66+ ...Object . keys ( store . session_diff ) ,
67+ ...Object . keys ( store . todo ) ,
68+ ...Object . keys ( store . permission ) ,
69+ ...Object . keys ( store . question ) ,
70+ ...Object . keys ( store . session_status ) ,
71+ ...Object . values ( store . part )
72+ . map ( ( parts ) => parts ?. find ( ( part ) => ! ! part ?. sessionID ) ?. sessionID )
73+ . filter ( ( sessionID ) : sessionID is string => ! ! sessionID ) ,
74+ ] . filter ( ( sessionID , index , list ) => ! keep . has ( sessionID ) && list . indexOf ( sessionID ) === index )
75+ if ( stale . length === 0 ) return
76+ for ( const sessionID of stale ) {
77+ setSessionTodo ?.( sessionID , undefined )
78+ }
79+ setStore (
80+ produce ( ( draft ) => {
81+ dropSessionCaches ( draft , stale )
7482 } ) ,
7583 )
7684}
@@ -102,6 +110,7 @@ export function applyDirectoryEvent(input: {
102110 next . splice ( result . index , 0 , info )
103111 const trimmed = trimSessions ( next , { limit : input . store . limit , permission : input . store . permission } )
104112 input . setStore ( "session" , reconcile ( trimmed , { key : "id" } ) )
113+ cleanupDroppedSessionCaches ( input . store , input . setStore , trimmed , input . setSessionTodo )
105114 if ( ! info . parentID ) input . setStore ( "sessionTotal" , ( value ) => value + 1 )
106115 break
107116 }
@@ -117,7 +126,7 @@ export function applyDirectoryEvent(input: {
117126 } ) ,
118127 )
119128 }
120- cleanupSessionCaches ( input . store , input . setStore , info . id , input . setSessionTodo )
129+ cleanupSessionCaches ( input . setStore , info . id , input . setSessionTodo )
121130 if ( info . parentID ) break
122131 input . setStore ( "sessionTotal" , ( value ) => Math . max ( 0 , value - 1 ) )
123132 break
@@ -130,6 +139,7 @@ export function applyDirectoryEvent(input: {
130139 next . splice ( result . index , 0 , info )
131140 const trimmed = trimSessions ( next , { limit : input . store . limit , permission : input . store . permission } )
132141 input . setStore ( "session" , reconcile ( trimmed , { key : "id" } ) )
142+ cleanupDroppedSessionCaches ( input . store , input . setStore , trimmed , input . setSessionTodo )
133143 break
134144 }
135145 case "session.deleted" : {
@@ -143,7 +153,7 @@ export function applyDirectoryEvent(input: {
143153 } ) ,
144154 )
145155 }
146- cleanupSessionCaches ( input . store , input . setStore , info . id , input . setSessionTodo )
156+ cleanupSessionCaches ( input . setStore , info . id , input . setSessionTodo )
147157 if ( info . parentID ) break
148158 input . setStore ( "sessionTotal" , ( value ) => Math . max ( 0 , value - 1 ) )
149159 break
0 commit comments