(entries: ConsoleEntry[])
| 245 | * Returns a workflow's entries trimmed to the configured cap. |
| 246 | */ |
| 247 | export function trimWorkflowConsoleEntries(entries: ConsoleEntry[]): ConsoleEntry[] { |
| 248 | if (entries.length <= TERMINAL_CONSOLE_LIMITS.MAX_ENTRIES_PER_WORKFLOW) { |
| 249 | return entries |
| 250 | } |
| 251 | |
| 252 | const executionGroups = new Map<string, ConsoleEntry[]>() |
| 253 | |
| 254 | for (const entry of entries) { |
| 255 | const executionId = entry.executionId ?? entry.id |
| 256 | const group = executionGroups.get(executionId) |
| 257 | if (group) { |
| 258 | group.push(entry) |
| 259 | } else { |
| 260 | executionGroups.set(executionId, [entry]) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | const executionIds = [...executionGroups.keys()] |
| 265 | const newestExecutionId = executionIds[0] |
| 266 | |
| 267 | if (!newestExecutionId) { |
| 268 | return entries.slice(0, TERMINAL_CONSOLE_LIMITS.MAX_ENTRIES_PER_WORKFLOW) |
| 269 | } |
| 270 | |
| 271 | const keptEntryIds = new Set<string>() |
| 272 | let remainingSlots = TERMINAL_CONSOLE_LIMITS.MAX_ENTRIES_PER_WORKFLOW |
| 273 | |
| 274 | const newestExecutionEntries = executionGroups.get(newestExecutionId) ?? [] |
| 275 | const newestExecutionToKeep = newestExecutionEntries.slice(0, remainingSlots) |
| 276 | newestExecutionToKeep.forEach((entry) => keptEntryIds.add(entry.id)) |
| 277 | remainingSlots -= newestExecutionToKeep.length |
| 278 | |
| 279 | for (const executionId of executionIds.slice(1)) { |
| 280 | const executionEntries = executionGroups.get(executionId) ?? [] |
| 281 | |
| 282 | if (executionEntries.length > remainingSlots) { |
| 283 | continue |
| 284 | } |
| 285 | |
| 286 | executionEntries.forEach((entry) => keptEntryIds.add(entry.id)) |
| 287 | remainingSlots -= executionEntries.length |
| 288 | |
| 289 | if (remainingSlots === 0) { |
| 290 | break |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return entries.filter((entry) => keptEntryIds.has(entry.id)) |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Applies workflow-level trimming while preserving newest-first order. |
no test coverage detected