fix(delegate-task): pause timeouts for active sessions

This commit is contained in:
YeonGyu-Kim
2026-05-10 13:52:22 +09:00
parent 5a4127cc8a
commit 862a2df0db
6 changed files with 132 additions and 7 deletions
@@ -364,6 +364,29 @@ describe("BackgroundManager pollRunningTasks", () => {
//#then
expect(task.status).toBe("running")
})
test("#when progress is older than prune TTL #then active status still keeps the task running", async () => {
//#given
const manager = createManagerWithClient({
status: async () => ({ data: { "ses-busy-stale": { type: "busy" } } }),
})
const task = createRunningTask("ses-busy-stale")
task.startedAt = new Date(Date.now() - 60 * 60 * 1000)
task.progress = {
toolCalls: 4,
lastUpdate: new Date(Date.now() - 35 * 60 * 1000),
}
injectTask(manager, task)
//#when
const poll = manager["pollRunningTasks"]
await poll.call(manager)
manager.shutdown()
//#then
expect(task.status).toBe("running")
expect(task.error).toBeUndefined()
})
})
describe("#given a running task whose session has terminal non-idle status", () => {
+4 -3
View File
@@ -2285,11 +2285,12 @@ The task was re-queued on a fallback model after a retryable failure.
}
}
private pruneStaleTasksAndNotifications(): void {
private pruneStaleTasksAndNotifications(allStatuses?: SessionStatusMap): void {
pruneStaleTasksAndNotifications({
tasks: this.tasks,
notifications: this.notifications,
taskTtlMs: this.config?.taskTtlMs,
sessionStatuses: allStatuses,
onTaskPruned: (taskId, task, errorMessage) => {
const wasPending = task.status === "pending"
log("[background-agent] Pruning stale task:", { taskId, status: task.status, age: Math.round(((wasPending ? task.queuedAt?.getTime() : task.startedAt?.getTime()) ? (Date.now() - (wasPending ? task.queuedAt!.getTime() : task.startedAt!.getTime())) : 0) / 1000) + "s" })
@@ -2412,8 +2413,6 @@ The task was re-queued on a fallback model after a retryable failure.
if (this.pollingInFlight) return
this.pollingInFlight = true
try {
this.pruneStaleTasksAndNotifications()
let allStatuses: SessionStatusMap | undefined
const sessionStatusMethod = this.client?.session?.status
if (typeof sessionStatusMethod !== "function") {
@@ -2435,6 +2434,8 @@ The task was re-queued on a fallback model after a retryable failure.
}
}
this.pruneStaleTasksAndNotifications(allStatuses)
await this.checkAndInterruptStaleTasks(allStatuses)
for (const task of this.tasks.values()) {
@@ -903,6 +903,42 @@ describe("pruneStaleTasksAndNotifications", () => {
expect(pruned).toContain("stale-task")
})
it("#given running task with stale progress and active session #when lastUpdate exceeds TTL #then should NOT prune", () => {
//#given
const tasks = new Map<string, BackgroundTask>()
const activeTask: BackgroundTask = {
id: "active-status-task",
sessionId: "ses-active-status",
parentSessionId: "parent",
parentMessageId: "msg",
description: "active status",
prompt: "active status",
agent: "oracle",
status: "running",
startedAt: new Date(Date.now() - 60 * 60 * 1000),
progress: {
toolCalls: 10,
lastUpdate: new Date(Date.now() - 35 * 60 * 1000),
},
}
tasks.set("active-status-task", activeTask)
const pruned: string[] = []
const notifications = new Map<string, BackgroundTask[]>()
//#when
pruneStaleTasksAndNotifications({
tasks,
notifications,
sessionStatuses: { "ses-active-status": { type: "busy" } },
onTaskPruned: (taskId) => pruned.push(taskId),
})
//#then
expect(pruned).toEqual([])
expect(tasks.has("active-status-task")).toBe(true)
})
it("#given custom taskTtlMs #when task exceeds custom TTL #then should prune", () => {
//#given
const tasks = new Map<string, BackgroundTask>()
@@ -31,6 +31,7 @@ export function pruneStaleTasksAndNotifications(args: {
notifications: Map<string, BackgroundTask[]>
onTaskPruned: (taskId: string, task: BackgroundTask, errorMessage: string) => void
taskTtlMs?: number
sessionStatuses?: SessionStatusMap
}): void {
const { tasks, notifications, onTaskPruned } = args
const effectiveTtl = args.taskTtlMs ?? TASK_TTL_MS
@@ -62,6 +63,11 @@ export function pruneStaleTasksAndNotifications(args: {
continue
}
const sessionStatus = task.sessionId ? args.sessionStatuses?.[task.sessionId]?.type : undefined
if (task.status === "running" && sessionStatus !== undefined && isActiveSessionStatus(sessionStatus)) {
continue
}
const lastActivity = task.status === "running" && task.progress?.lastUpdate
? task.progress.lastUpdate.getTime()
: undefined