fix(background-agent): add TTL for terminal task retention to prevent unbounded growth

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-03-11 21:52:48 +09:00
parent d4232c9eac
commit 0d9f001c11
2 changed files with 61 additions and 11 deletions
+22 -1
View File
@@ -13,6 +13,8 @@ import {
} from "./constants"
import { removeTaskToastTracking } from "./remove-task-toast-tracking"
const TERMINAL_TASK_TTL_MS = 30 * 60 * 1000
const TERMINAL_TASK_STATUSES = new Set<BackgroundTask["status"]>([
"completed",
"error",
@@ -27,9 +29,28 @@ export function pruneStaleTasksAndNotifications(args: {
}): void {
const { tasks, notifications, onTaskPruned } = args
const now = Date.now()
const tasksWithPendingNotifications = new Set<string>()
for (const queued of notifications.values()) {
for (const task of queued) {
tasksWithPendingNotifications.add(task.id)
}
}
for (const [taskId, task] of tasks.entries()) {
if (TERMINAL_TASK_STATUSES.has(task.status)) continue
if (TERMINAL_TASK_STATUSES.has(task.status)) {
if (tasksWithPendingNotifications.has(taskId)) continue
const completedAt = task.completedAt?.getTime()
if (!completedAt) continue
const age = now - completedAt
if (age <= TERMINAL_TASK_TTL_MS) continue
removeTaskToastTracking(taskId)
tasks.delete(taskId)
continue
}
const timestamp = task.status === "pending"
? task.queuedAt?.getTime()