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
41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import type { BackgroundTask } from "./types"
|
|
import type { ResultHandlerContext } from "./result-handler-context"
|
|
import { log } from "../../shared"
|
|
import { notifyParentSession } from "./parent-session-notifier"
|
|
|
|
export async function tryCompleteTask(
|
|
task: BackgroundTask,
|
|
source: string,
|
|
ctx: ResultHandlerContext
|
|
): Promise<boolean> {
|
|
const { concurrencyManager, state } = ctx
|
|
|
|
if (task.status !== "running") {
|
|
log("[background-agent] Task already completed, skipping:", {
|
|
taskId: task.id,
|
|
status: task.status,
|
|
source,
|
|
})
|
|
return false
|
|
}
|
|
|
|
task.status = "completed"
|
|
task.completedAt = new Date()
|
|
|
|
if (task.concurrencyKey) {
|
|
concurrencyManager.release(task.concurrencyKey)
|
|
task.concurrencyKey = undefined
|
|
}
|
|
|
|
state.markForNotification(task)
|
|
|
|
try {
|
|
await notifyParentSession(task, ctx)
|
|
log(`[background-agent] Task completed via ${source}:`, task.id)
|
|
} catch (error) {
|
|
log("[background-agent] Error in notifyParentSession:", { taskId: task.id, error })
|
|
}
|
|
|
|
return true
|
|
}
|