diff --git a/src/features/background-agent/manager.ts b/src/features/background-agent/manager.ts index aa492278..28b5b6e4 100644 --- a/src/features/background-agent/manager.ts +++ b/src/features/background-agent/manager.ts @@ -200,6 +200,11 @@ export class BackgroundManager { await this.startTask(item) } catch (error) { log("[background-agent] Error starting task:", error) + // Release concurrency slot if startTask failed and didn't release it itself + // This prevents slot leaks when errors occur after acquire but before task.concurrencyKey is set + if (!item.task.concurrencyKey) { + this.concurrencyManager.release(key) + } } queue.shift() @@ -240,16 +245,16 @@ export class BackgroundManager { query: { directory: parentDirectory, }, - }).catch((error) => { - this.concurrencyManager.release(concurrencyKey) - throw error }) if (createResult.error) { - this.concurrencyManager.release(concurrencyKey) throw new Error(`Failed to create background session: ${createResult.error}`) } + if (!createResult.data?.id) { + throw new Error("Failed to create background session: API returned no session ID") + } + const sessionID = createResult.data.id subagentSessions.add(sessionID)