Skip to content

Commit 51477c1

Browse files
authored
fix(terminal): pop all entries from a single execution when the limit is exceeded (#2815)
1 parent a353563 commit 51477c1

File tree

1 file changed

+51
-7
lines changed
  • apps/sim/stores/terminal/console

1 file changed

+51
-7
lines changed

apps/sim/stores/terminal/console/store.ts

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2020
const 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

Comments
 (0)