fix(background-agent): release concurrency before prompt to unblock queued tasks

Previously, concurrency was released in finally block AFTER prompt completion.
This caused queued tasks to remain blocked while prompt hangs.

Now release happens BEFORE prompt, allowing next queued task to start immediately
when current task completes, regardless of prompt success/failure.

Also added early release on session creation error for proper cleanup.
This commit is contained in:
YeonGyu-Kim
2026-01-07 02:24:07 +09:00
parent b5c1cfb57f
commit 980b685393
2 changed files with 76 additions and 4 deletions
+8 -4
View File
@@ -86,9 +86,13 @@ export class BackgroundManager {
parentID: input.parentSessionID,
title: `Background: ${input.description}`,
},
}).catch((error) => {
this.concurrencyManager.release(model)
throw error
})
if (createResult.error) {
this.concurrencyManager.release(model)
throw new Error(`Failed to create background session: ${createResult.error}`)
}
@@ -345,6 +349,10 @@ export class BackgroundManager {
const taskId = task.id
setTimeout(async () => {
if (task.model) {
this.concurrencyManager.release(task.model)
}
try {
const messageDir = getMessageDir(task.parentSessionID)
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
@@ -367,10 +375,6 @@ export class BackgroundManager {
} catch (error) {
log("[background-agent] prompt failed:", String(error))
} finally {
if (task.model) {
this.concurrencyManager.release(task.model)
}
// Always clean up both maps to prevent memory leaks
this.clearNotificationsForTask(taskId)
this.tasks.delete(taskId)
log("[background-agent] Removed completed task from memory:", taskId)