@@ -15,7 +15,7 @@ const logger = createLogger('TerminalConsoleStore')
1515 * Maximum number of console entries to keep per workflow.
1616 * Keeps the stored data size reasonable and improves performance.
1717 */
18- const MAX_ENTRIES_PER_WORKFLOW = 500
18+ const MAX_ENTRIES_PER_WORKFLOW = 1000
1919
2020const updateBlockOutput = (
2121 existingOutput : NormalizedBlockOutput | undefined ,
@@ -96,13 +96,57 @@ export const useTerminalConsoleStore = create<ConsoleStore>()(
9696 }
9797
9898 const newEntries = [ newEntry , ...state . entries ]
99- const workflowCounts = new Map < string , number > ( )
100- const trimmedEntries = newEntries . filter ( ( entry ) => {
101- const count = workflowCounts . get ( entry . workflowId ) || 0
102- if ( count >= MAX_ENTRIES_PER_WORKFLOW ) return false
103- workflowCounts . set ( entry . workflowId , count + 1 )
104- return true
99+
100+ const executionsToRemove = new Set < string > ( )
101+
102+ const workflowGroups = new Map < string , ConsoleEntry [ ] > ( )
103+ for ( const e of newEntries ) {
104+ const group = workflowGroups . get ( e . workflowId ) || [ ]
105+ group . push ( e )
106+ workflowGroups . set ( e . workflowId , group )
107+ }
108+
109+ for ( const [ workflowId , entries ] of workflowGroups ) {
110+ if ( entries . length <= MAX_ENTRIES_PER_WORKFLOW ) continue
111+
112+ const execOrder : string [ ] = [ ]
113+ const seen = new Set < string > ( )
114+ for ( const e of entries ) {
115+ const execId = e . executionId ?? e . id
116+ if ( ! seen . has ( execId ) ) {
117+ execOrder . push ( execId )
118+ seen . add ( execId )
119+ }
120+ }
121+
122+ const counts = new Map < string , number > ( )
123+ for ( const e of entries ) {
124+ const execId = e . executionId ?? e . id
125+ counts . set ( execId , ( counts . get ( execId ) || 0 ) + 1 )
126+ }
127+
128+ let total = 0
129+ const toKeep = new Set < string > ( )
130+ for ( const execId of execOrder ) {
131+ const c = counts . get ( execId ) || 0
132+ if ( total + c <= MAX_ENTRIES_PER_WORKFLOW ) {
133+ toKeep . add ( execId )
134+ total += c
135+ }
136+ }
137+
138+ for ( const execId of execOrder ) {
139+ if ( ! toKeep . has ( execId ) ) {
140+ executionsToRemove . add ( `${ workflowId } :${ execId } ` )
141+ }
142+ }
143+ }
144+
145+ const trimmedEntries = newEntries . filter ( ( e ) => {
146+ const key = `${ e . workflowId } :${ e . executionId ?? e . id } `
147+ return ! executionsToRemove . has ( key )
105148 } )
149+
106150 return { entries : trimmedEntries }
107151 } )
108152
0 commit comments