YeonGyu-Kim e3bd43ff64 refactor(background-agent): split manager.ts into focused modules
Extract 30+ single-responsibility modules from manager.ts (1556 LOC):
- task lifecycle: task-starter, task-completer, task-canceller, task-resumer
- task queries: task-queries, task-poller, task-queue-processor
- notifications: notification-builder, notification-tracker, parent-session-notifier
- session handling: session-validator, session-output-validator, session-todo-checker
- spawner: spawner/ directory with focused spawn modules
- utilities: duration-formatter, error-classifier, message-storage-locator
- result handling: result-handler-context, background-task-completer
- shutdown: background-manager-shutdown, process-signal
2026-02-08 16:20:52 +09:00

58 lines
1.6 KiB
TypeScript

import { log } from "../../shared"
import { TASK_TTL_MS } from "./constants"
import { subagentSessions } from "../claude-code-session-state"
import { pruneStaleTasksAndNotifications } from "./task-poller"
import type { BackgroundTask } from "./types"
import type { ConcurrencyManager } from "./concurrency"
export function pruneStaleState(args: {
tasks: Map<string, BackgroundTask>
notifications: Map<string, BackgroundTask[]>
concurrencyManager: ConcurrencyManager
cleanupPendingByParent: (task: BackgroundTask) => void
clearNotificationsForTask: (taskId: string) => void
}): void {
const {
tasks,
notifications,
concurrencyManager,
cleanupPendingByParent,
clearNotificationsForTask,
} = args
pruneStaleTasksAndNotifications({
tasks,
notifications,
onTaskPruned: (taskId, task, errorMessage) => {
const now = Date.now()
const timestamp = task.status === "pending"
? task.queuedAt?.getTime()
: task.startedAt?.getTime()
const age = timestamp ? now - timestamp : TASK_TTL_MS
log("[background-agent] Pruning stale task:", {
taskId,
status: task.status,
age: Math.round(age / 1000) + "s",
})
task.status = "error"
task.error = errorMessage
task.completedAt = new Date()
if (task.concurrencyKey) {
concurrencyManager.release(task.concurrencyKey)
task.concurrencyKey = undefined
}
cleanupPendingByParent(task)
clearNotificationsForTask(taskId)
tasks.delete(taskId)
if (task.sessionID) {
subagentSessions.delete(task.sessionID)
}
},
})
}