fix(background-agent): decrement spawn budget on task completion, cancellation, error, and interrupt#2725
Merged
code-yeongyu merged 2 commits intocode-yeongyu:devfrom Mar 25, 2026
Conversation
…cellation, error, and interrupt rootDescendantCounts was incremented on every spawn but never decremented when tasks reached terminal states (completed, cancelled, error, interrupt, stale-pruned). This made maxDescendants=50 a session-lifetime quota instead of its intended semantics as a concurrent-active agent cap. Fix: add unregisterRootDescendant() in five terminal-state handlers: - tryCompleteTask(): task completes successfully - cancelTask(): running task cancelled (wasRunning guard prevents double-decrement for pending tasks already handled by rollbackPreStartDescendantReservation) - session.error handler: task errors - promptAsync catch (startTask): task interrupted on launch - promptAsync catch (resume): task interrupted on resume - onTaskPruned callback: stale task pruned (wasPending guard) Fixes: code-yeongyu#2700
…nt on task completion Tests prove rootDescendantCounts is never decremented on task completion, cancellation, or error — making maxDescendants a lifetime quota instead of a concurrent-active cap. All 4 tests fail (RED phase) before the fix. Refs: code-yeongyu#2700
There was a problem hiding this comment.
No issues found across 2 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Auto-approved: Fixes a significant resource leak in the subagent spawn budget. Includes defensive guards against double-decrementing for pending tasks and adds comprehensive regression tests.
4 tasks
|
@code-yeongyu please merge/add this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2700 — subagents stop spawning after 50 total per session.
Root Cause
rootDescendantCountsinBackgroundManagerincrements on every spawn but only decrements on pre-start rollback or root session deletion. Task completion, cancellation, error, and interrupt never decrement the counter. This makesbackground_task.maxDescendants(default: 50) a session-lifetime quota instead of its intended semantics as a concurrent-active agent cap.After 50 total spawns across a session (completed + cancelled + errored), no new subagents can be created — even if all 50 have finished.
Fix
Add
this.unregisterRootDescendant(task.rootSessionID)in five terminal-state handlers:tryCompleteTask()— task completes successfullycancelTask()— running task cancelled (withwasRunningguard: pending tasks already decrement viarollbackPreStartDescendantReservationto prevent double-decrement)session.errorhandler — task errorspromptAsynccatch instartTask— task interrupted on launchpromptAsynccatch inresume— task interrupted on resumeonTaskPrunedcallback — stale task pruned (withwasPendingguard)Tests
Four regression tests added to
manager.test.ts:Verification
Before fix: spawn 51st agent after 1st completes → blocked with maxDescendants=50 error
After fix: spawn 51st agent after 1st completes → succeeds
Full test suite: 121 pass, 3 fail (3 pre-existing failures in unrelated
session.error > retry pathtests, present before this change)Summary by cubic
Fixes spawn budget leaks in
BackgroundManagerby decrementing root descendant counts when tasks end. Restoresbackground_task.maxDescendantsto act as a concurrent cap so new subagents can spawn after others finish.Written for commit 031503b. Summary will update on new commits.