@@ -123,54 +123,47 @@ export async function collectPersistenceDiagnostics(options: {
123123 }
124124 const indexIds = new Set ( indexEntries ?. filter ( isHistoryItem ) . map ( ( item ) => item . id ) ?? [ ] )
125125
126- const tasks = await Promise . all (
127- relevant . map ( async ( item ) => {
128- const taskPath = path . join ( tasksPath , item . id )
129- const [ historyItemResult , uiMessagesResult ] = await Promise . all ( [
130- inspectJson ( path . join ( taskPath , GlobalFileNames . historyItem ) ) ,
131- inspectJson ( path . join ( taskPath , GlobalFileNames . uiMessages ) ) ,
132- ] )
133- const messages = Array . isArray ( uiMessagesResult . value ) ? uiMessagesResult . value : undefined
134- const timestamps = ( messages ?? [ ] )
135- . map ( ( message ) =>
136- message !== null && typeof message === "object" && "ts" in message && typeof message . ts === "number"
137- ? message . ts
138- : undefined ,
139- )
140- . filter ( ( timestamp ) : timestamp is number => timestamp !== undefined )
141- const findings : string [ ] = [ ]
142- for ( const related of [
143- item . rootTaskId ,
144- item . parentTaskId ,
145- item . awaitingChildId ,
146- ...( item . childIds ?? [ ] ) ,
147- ] ) {
148- if ( related && ! byId . has ( related ) ) findings . push ( "missingReferencedTask" )
149- }
150- if ( item . awaitingChildId && item . status !== "delegated" ) findings . push ( "awaitingChildStatusMismatch" )
151- if ( item . parentTaskId && ! byId . get ( item . parentTaskId ) ?. childIds ?. includes ( item . id ) ) {
152- findings . push ( "parentChildMismatch" )
153- }
154- if ( hasParentCycle ( item , byId ) ) findings . push ( "parentCycle" )
155-
156- return {
157- id : pseudonymize ( item . id ) ,
158- rootTask : item . rootTaskId ? pseudonymize ( item . rootTaskId ) : undefined ,
159- parentTask : item . parentTaskId ? pseudonymize ( item . parentTaskId ) : undefined ,
160- children : ( item . childIds ?? [ ] ) . slice ( 0 , MAX_TASKS ) . map ( pseudonymize ) ,
161- status : item . status ,
162- number : item . number ,
163- historyItem : historyItemResult . inspection ,
164- uiMessages : {
165- ...uiMessagesResult . inspection ,
166- messageCount : messages ?. length ,
167- firstTimestamp : timestamps [ 0 ] ,
168- lastTimestamp : timestamps . at ( - 1 ) ,
169- } ,
170- integrityFindings : Array . from ( new Set ( findings ) ) ,
171- }
172- } ) ,
173- )
126+ // Inspect sequentially to avoid retaining parsed histories for multiple tasks.
127+ const tasks : DiagnosticsPersistenceReport [ "tasks" ] = [ ]
128+ for ( const item of relevant ) {
129+ const taskPath = path . join ( tasksPath , item . id )
130+ const historyItemResult = await inspectJson ( path . join ( taskPath , GlobalFileNames . historyItem ) )
131+ const uiMessagesResult = await inspectJson ( path . join ( taskPath , GlobalFileNames . uiMessages ) )
132+ const messages = Array . isArray ( uiMessagesResult . value ) ? uiMessagesResult . value : undefined
133+ const timestamps = ( messages ?? [ ] )
134+ . map ( ( message ) =>
135+ message !== null && typeof message === "object" && "ts" in message && typeof message . ts === "number"
136+ ? message . ts
137+ : undefined ,
138+ )
139+ . filter ( ( timestamp ) : timestamp is number => timestamp !== undefined )
140+ const findings : string [ ] = [ ]
141+ for ( const related of [ item . rootTaskId , item . parentTaskId , item . awaitingChildId , ...( item . childIds ?? [ ] ) ] ) {
142+ if ( related && ! byId . has ( related ) ) findings . push ( "missingReferencedTask" )
143+ }
144+ if ( item . awaitingChildId && item . status !== "delegated" ) findings . push ( "awaitingChildStatusMismatch" )
145+ if ( item . parentTaskId && ! byId . get ( item . parentTaskId ) ?. childIds ?. includes ( item . id ) ) {
146+ findings . push ( "parentChildMismatch" )
147+ }
148+ if ( hasParentCycle ( item , byId ) ) findings . push ( "parentCycle" )
149+
150+ tasks . push ( {
151+ id : pseudonymize ( item . id ) ,
152+ rootTask : item . rootTaskId ? pseudonymize ( item . rootTaskId ) : undefined ,
153+ parentTask : item . parentTaskId ? pseudonymize ( item . parentTaskId ) : undefined ,
154+ children : ( item . childIds ?? [ ] ) . slice ( 0 , MAX_TASKS ) . map ( pseudonymize ) ,
155+ status : item . status ,
156+ number : item . number ,
157+ historyItem : historyItemResult . inspection ,
158+ uiMessages : {
159+ ...uiMessagesResult . inspection ,
160+ messageCount : messages ?. length ,
161+ firstTimestamp : timestamps [ 0 ] ,
162+ lastTimestamp : timestamps . at ( - 1 ) ,
163+ } ,
164+ integrityFindings : Array . from ( new Set ( findings ) ) ,
165+ } )
166+ }
174167
175168 return {
176169 cacheEntryCount : history . length ,
0 commit comments