@@ -105,21 +105,6 @@ const upsertPartInfo = (list: Part[], next: Part) => {
105105
106106const removePartInfo = ( list : Part [ ] , partID : string ) => list . filter ( ( part ) => part . id !== partID ) ;
107107
108- const pruneRecordKeys = < T > ( record : Record < string , T > , keep : Set < string > ) => {
109- let changed = false ;
110- const next : Record < string , T > = { } ;
111-
112- for ( const [ key , value ] of Object . entries ( record ) ) {
113- if ( ! keep . has ( key ) ) {
114- changed = true ;
115- continue ;
116- }
117- next [ key ] = value ;
118- }
119-
120- return changed ? next : record ;
121- } ;
122-
123108export function createSessionStore ( options : {
124109 client : ( ) => Client | null ;
125110 activeWorkspaceRoot : ( ) => string ;
@@ -163,45 +148,6 @@ export function createSessionStore(options: {
163148 const [ permissionReplyBusy , setPermissionReplyBusy ] = createSignal ( false ) ;
164149 const reloadDetectionSet = new Set < string > ( ) ;
165150
166- const messageIDFromReloadKey = ( key : string ) => {
167- const separator = key . indexOf ( ":" ) ;
168- if ( separator < 0 ) return "" ;
169- return key . slice ( 0 , separator ) ;
170- } ;
171-
172- const trimReloadDetectionSet = ( ) => {
173- if ( reloadDetectionSet . size <= MAX_RELOAD_DETECTION_KEYS ) return ;
174- const targetSize = Math . floor ( MAX_RELOAD_DETECTION_KEYS * 0.8 ) ;
175- const overflow = reloadDetectionSet . size - targetSize ;
176- if ( overflow <= 0 ) return ;
177-
178- const iterator = reloadDetectionSet . values ( ) ;
179- for ( let i = 0 ; i < overflow ; i += 1 ) {
180- const next = iterator . next ( ) ;
181- if ( next . done ) break ;
182- reloadDetectionSet . delete ( next . value ) ;
183- }
184- } ;
185-
186- const pruneReloadDetectionSetByMessageIDs = ( keepMessageIDs : Set < string > ) => {
187- if ( reloadDetectionSet . size === 0 ) return ;
188- for ( const key of reloadDetectionSet ) {
189- const messageID = messageIDFromReloadKey ( key ) ;
190- if ( ! messageID || keepMessageIDs . has ( messageID ) ) continue ;
191- reloadDetectionSet . delete ( key ) ;
192- }
193- } ;
194-
195- const removeReloadDetectionKeysForMessageIDs = ( removedMessageIDs : Set < string > ) => {
196- if ( reloadDetectionSet . size === 0 || removedMessageIDs . size === 0 ) return ;
197- for ( const key of reloadDetectionSet ) {
198- const messageID = messageIDFromReloadKey ( key ) ;
199- if ( messageID && removedMessageIDs . has ( messageID ) ) {
200- reloadDetectionSet . delete ( key ) ;
201- }
202- }
203- } ;
204-
205151 const skillPathPattern = / [ \\ / ] \. o p e n c o d e [ \\ / ] ( s k i l l | s k i l l s ) [ \\ / ] / i;
206152 const skillNamePattern = / [ \\ / ] \. o p e n c o d e [ \\ / ] (?: s k i l l | s k i l l s ) [ \\ / ] + ( [ ^ \\ / ] + ) / i;
207153 const commandPathPattern = / [ \\ / ] \. o p e n c o d e [ \\ / ] ( c o m m a n d | c o m m a n d s ) [ \\ / ] / i;
@@ -213,16 +159,15 @@ export function createSessionStore(options: {
213159 const openworkConfigPattern = / [ \\ / ] \. o p e n c o d e [ \\ / ] o p e n w o r k \. j s o n \b / i;
214160 const mutatingTools = new Set ( [ "write" , "edit" , "apply_patch" ] ) ;
215161
216- const MAX_SEARCH_TEXT_LENGTH = 20_000 ;
217-
218162 const extractSearchText = ( value : unknown ) => {
219- if ( value === null || value === undefined ) return "" ;
220- if ( typeof value === "string" ) return value . slice ( 0 , MAX_SEARCH_TEXT_LENGTH ) ;
221- if ( typeof value === "number" || typeof value === "boolean" ) return String ( value ) ;
222- return safeStringify ( value ) . slice ( 0 , MAX_SEARCH_TEXT_LENGTH ) ;
163+ if ( ! value ) return "" ;
164+ if ( typeof value === "string" ) return value ;
165+ if ( typeof value === "number" ) return String ( value ) ;
166+ return safeStringify ( value ) ;
223167 } ;
224168
225- const detectReloadReasonFromText = ( text : string ) : ReloadReason | null => {
169+ const detectReloadReason = ( value : unknown ) : ReloadReason | null => {
170+ const text = extractSearchText ( value ) ;
226171 if ( ! text ) return null ;
227172 if ( openworkConfigPattern . test ( text ) ) return null ;
228173 if ( skillPathPattern . test ( text ) ) return "skills" ;
@@ -278,27 +223,63 @@ export function createSessionStore(options: {
278223 return null ;
279224 } ;
280225
226+ const detectReloadReasonDeep = ( value : unknown ) : ReloadReason | null => {
227+ if ( ! value ) return null ;
228+ if ( typeof value === "string" || typeof value === "number" ) {
229+ return detectReloadReason ( value ) ;
230+ }
231+ if ( Array . isArray ( value ) ) {
232+ for ( const entry of value ) {
233+ const reason = detectReloadReasonDeep ( entry ) ;
234+ if ( reason ) return reason ;
235+ }
236+ return null ;
237+ }
238+ if ( typeof value === "object" ) {
239+ for ( const entry of Object . values ( value as Record < string , unknown > ) ) {
240+ const reason = detectReloadReasonDeep ( entry ) ;
241+ if ( reason ) return reason ;
242+ }
243+ }
244+ return null ;
245+ } ;
246+
247+ const detectReloadTriggerDeep = ( value : unknown ) : ReloadTrigger | null => {
248+ if ( ! value ) return null ;
249+ if ( typeof value === "string" || typeof value === "number" ) {
250+ return detectReloadTriggerFromText ( String ( value ) ) ;
251+ }
252+ if ( Array . isArray ( value ) ) {
253+ for ( const entry of value ) {
254+ const trigger = detectReloadTriggerDeep ( entry ) ;
255+ if ( trigger ) return trigger ;
256+ }
257+ return null ;
258+ }
259+ if ( typeof value === "object" ) {
260+ for ( const entry of Object . values ( value as Record < string , unknown > ) ) {
261+ const trigger = detectReloadTriggerDeep ( entry ) ;
262+ if ( trigger ) return trigger ;
263+ }
264+ }
265+ return null ;
266+ } ;
267+
281268 const detectReloadFromPart = ( part : Part ) : { reason : ReloadReason ; trigger ?: ReloadTrigger } | null => {
282269 if ( part . type !== "tool" ) return null ;
283270 const record = part as Record < string , unknown > ;
284271 const toolName = typeof record . tool === "string" ? record . tool : "" ;
285272 if ( ! mutatingTools . has ( toolName ) ) return null ;
286273 const state = ( record . state ?? { } ) as Record < string , unknown > ;
287-
288- const inputText = extractSearchText ( state . input ) ;
289- const patchText = extractSearchText ( state . patch ) ;
290- const diffText = extractSearchText ( state . diff ) ;
291-
292274 const reason =
293- detectReloadReasonFromText ( inputText ) ||
294- detectReloadReasonFromText ( patchText ) ||
295- detectReloadReasonFromText ( diffText ) ;
275+ detectReloadReasonDeep ( state . input ) ||
276+ detectReloadReasonDeep ( state . patch ) ||
277+ detectReloadReasonDeep ( state . diff ) ;
296278 if ( ! reason ) return null ;
297-
298279 const trigger =
299- detectReloadTriggerFromText ( inputText ) ||
300- detectReloadTriggerFromText ( patchText ) ||
301- detectReloadTriggerFromText ( diffText ) ;
280+ detectReloadTriggerDeep ( state . input ) ||
281+ detectReloadTriggerDeep ( state . patch ) ||
282+ detectReloadTriggerDeep ( state . diff ) ;
302283 return { reason, trigger : trigger ?? undefined } ;
303284 } ;
304285
@@ -315,13 +296,11 @@ export function createSessionStore(options: {
315296 }
316297 }
317298
318- const detection = detectReloadFromPart ( part ) ;
319- if ( ! detection ) return ;
320-
321299 const key = `${ part . messageID } :${ part . id } ` ;
322300 if ( reloadDetectionSet . has ( key ) ) return ;
301+ const detection = detectReloadFromPart ( part ) ;
302+ if ( ! detection ) return ;
323303 reloadDetectionSet . add ( key ) ;
324- trimReloadDetectionSet ( ) ;
325304 options . markReloadRequired ( detection . reason , detection . trigger ) ;
326305 } ;
327306
@@ -376,51 +355,6 @@ export function createSessionStore(options: {
376355 return store . todos [ id ] ?? [ ] ;
377356 } ) ;
378357
379- const pruneSessionCaches = ( sessionIDs : Set < string > ) => {
380- const keepMessageIDs = new Set < string > ( ) ;
381-
382- setStore (
383- produce ( ( draft : StoreState ) => {
384- draft . sessionStatus = pruneRecordKeys ( draft . sessionStatus , sessionIDs ) ;
385- draft . messages = pruneRecordKeys ( draft . messages , sessionIDs ) ;
386- draft . todos = pruneRecordKeys ( draft . todos , sessionIDs ) ;
387-
388- for ( const messages of Object . values ( draft . messages ) ) {
389- for ( const info of messages ?? [ ] ) {
390- if ( info ?. id ) keepMessageIDs . add ( info . id ) ;
391- }
392- }
393-
394- draft . parts = pruneRecordKeys ( draft . parts , keepMessageIDs ) ;
395- } ) ,
396- ) ;
397-
398- pruneReloadDetectionSetByMessageIDs ( keepMessageIDs ) ;
399- trimReloadDetectionSet ( ) ;
400- } ;
401-
402- const removeSessionCaches = ( sessionID : string ) => {
403- const removedMessageIDs = new Set < string > ( ) ;
404-
405- setStore (
406- produce ( ( draft : StoreState ) => {
407- const messageIDs = ( draft . messages [ sessionID ] ?? [ ] ) . map ( ( message ) => message . id ) ;
408- for ( const messageID of messageIDs ) {
409- removedMessageIDs . add ( messageID ) ;
410- }
411- delete draft . sessionStatus [ sessionID ] ;
412- delete draft . messages [ sessionID ] ;
413- delete draft . todos [ sessionID ] ;
414- for ( const messageID of messageIDs ) {
415- delete draft . parts [ messageID ] ;
416- }
417- } ) ,
418- ) ;
419-
420- removeReloadDetectionKeysForMessageIDs ( removedMessageIDs ) ;
421- trimReloadDetectionSet ( ) ;
422- } ;
423-
424358 async function loadSessions ( scopeRoot ?: string ) {
425359 const c = options . client ( ) ;
426360 if ( ! c ) return ;
@@ -452,7 +386,6 @@ export function createSessionStore(options: {
452386 : list ;
453387 sessionDebug ( "sessions:load:filtered" , { root : root || null , count : filtered . length } ) ;
454388 setStore ( "sessions" , reconcile ( sortSessionsByActivity ( filtered ) , { key : "id" } ) ) ;
455- pruneSessionCaches ( new Set ( filtered . map ( ( session ) => session . id ) ) ) ;
456389 }
457390
458391 async function renameSession ( sessionID : string , title : string ) {
@@ -639,7 +572,6 @@ export function createSessionStore(options: {
639572
640573 const setSessions = ( next : Session [ ] ) => {
641574 setStore ( "sessions" , reconcile ( sortSessionsByActivity ( next ) , { key : "id" } ) ) ;
642- pruneSessionCaches ( new Set ( next . map ( ( session ) => session . id ) ) ) ;
643575 } ;
644576
645577 const setSessionStatusById = ( next : Record < string , string > ) => {
@@ -658,14 +590,6 @@ export function createSessionStore(options: {
658590 setStore ( "todos" , id , next ) ;
659591 } ;
660592
661- const clearSessionCaches = ( ) => {
662- setStore ( "sessionStatus" , { } ) ;
663- setStore ( "messages" , { } ) ;
664- setStore ( "parts" , { } ) ;
665- setStore ( "todos" , { } ) ;
666- reloadDetectionSet . clear ( ) ;
667- } ;
668-
669593 const setPendingPermissions = ( next : PendingPermission [ ] ) => {
670594 setStore ( "pendingPermissions" , next ) ;
671595 } ;
@@ -720,10 +644,6 @@ export function createSessionStore(options: {
720644 const info = record . info as Session | undefined ;
721645 if ( info ?. id ) {
722646 setStore ( "sessions" , ( current ) => removeSession ( current , info . id ) ) ;
723- removeSessionCaches ( info . id ) ;
724- if ( options . selectedSessionId ( ) === info . id ) {
725- options . setSelectedSessionId ( null ) ;
726- }
727647 }
728648 }
729649 }
@@ -1061,7 +981,6 @@ export function createSessionStore(options: {
1061981 setSessionStatusById,
1062982 setMessages,
1063983 setTodos,
1064- clearSessionCaches,
1065984 setPendingPermissions,
1066985 setPendingQuestions,
1067986 } ;
0 commit comments