Skip to content

fix(graphai): preserve nesting headroom (regression from #1314 — nested mapAgent fails with "Concurrency is too low") - #1326

Merged
isamu merged 1 commit into
mainfrom
fix/nesting-headroom-drain
Jun 8, 2026
Merged

isamu merged 1 commit into
mainfrom
fix/nesting-headroom-drain

Conversation

@snakajima

@snakajima snakajima commented Jun 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes a regression introduced by #1314 (feat: concurrency per label with head-of-line skip). A graph with a mapAgent whose nested graph contains another mapAgent (e.g. mulmocast's translate action) now fails with:

mapAgent: Concurrency is too low: <n>

…even when per-label concurrency is not used at all. It affects the plain concurrency: number path.

Root cause

prepareForNesting() bumps concurrency to reserve a slot so the nested graph's own children have headroom to run — this is the deadlock-avoidance invariant that mapAgent asserts via concurrency > running.

#1314 added a drain loop at the end of prepareForNesting():

this.concurrency++;
...
let progressed = true;
while (progressed) {
  const before = this.runningNodes.size;
  this.dequeueTaskIfPossible();
  progressed = this.runningNodes.size > before;
}

When a sibling task is already queued (common under a low global limit), this drain immediately dispatches it into the just-reserved slot, pushing running up to concurrency. The next nested mapAgent then sees running == concurrency (no headroom) and throws.

At prepareForNesting time the nested children are not queued yet — the nested graph is built and run after prepareForNesting returns — so the drain can only consume the reservation with unrelated sibling work.

Fix

Remove the drain loop from prepareForNesting(). Nested children and any newly-eligible queued tasks are dispatched when they call addTask() or when a slot frees up in onComplete() (the onComplete drain loop is kept). This restores the pre-#1314 behavior on the global path while leaving the per-label feature intact.

Tests

  • unit (test_task_manager.ts): prepareForNesting must not dispatch a queued sibling — headroom preserved (running < concurrency after the bump). The pre-existing nesting test had no queued sibling, which is why the regression slipped through.
  • integration (test_nested_concurrency.ts, new): a mapAgent whose nested graph contains another mapAgent runs to completion under concurrency: 1. Verified to fail (Concurrency is too low: 5) on the unfixed code and pass with the fix.

npx tsc --noEmit clean. The 8 pre-existing Cannot find module './array_find_first_exists' failures in the suite are unrelated (present on a clean checkout of main).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved concurrency handling for nested graph execution to prevent task queue overflow and ensure correct task ordering in scenarios with concurrency constraints. This resolves an issue where nested operations could trigger spurious concurrency-related guard conditions, enhancing the reliability of concurrent nested graph processing.

…to siblings

prepareForNesting() bumps `concurrency` to reserve a slot so a nested graph's
own children have headroom to run (deadlock avoidance, which mapAgent asserts
via `concurrency > running`). PR #1314 added a drain loop at the end of
prepareForNesting that immediately dispatched already-queued sibling tasks into
that reserved slot, pushing `running` up to `concurrency`. The next nested
mapAgent then saw no headroom and threw "mapAgent: Concurrency is too low".

At prepareForNesting time the nested children are not queued yet (the nested
graph is built and run *after* it returns), so the drain can only consume the
reservation with unrelated work. Remove the drain loop: nested children and any
newly-eligible queued tasks are dispatched when they call addTask() or when a
slot frees up in onComplete() (the onComplete drain is kept). This restores the
pre-#1314 behavior for the global path while leaving the per-label feature
intact.

Reproduces with a mapAgent whose nested graph contains another mapAgent (e.g.
mulmocast's translate action) under a low global concurrency.

Tests:
- unit: prepareForNesting must not dispatch a queued sibling (headroom preserved)
- integration: nested mapAgent-within-mapAgent runs without tripping the guard

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dd4778d7-f7f5-445d-99a3-85a25b84d3d0

📥 Commits

Reviewing files that changed from the base of the PR and between 2730e21 and f07b4c8.

📒 Files selected for processing (3)
  • packages/graphai/src/task_manager.ts
  • packages/graphai/tests/units/test_nested_concurrency.ts
  • packages/graphai/tests/units/test_task_manager.ts

📝 Walkthrough

Walkthrough

TaskManager.prepareForNesting() no longer drains the queued task execution queue after incrementing concurrency for nesting. Instead, it explicitly preserves the reserved headroom slot to prevent queued sibling tasks from consuming capacity needed by nested graph execution. Unit and integration tests validate the fix.

Changes

Nested graph concurrency headroom preservation

Layer / File(s) Summary
TaskManager.prepareForNesting() queue preservation
packages/graphai/src/task_manager.ts
Removes the queue-draining while loop from prepareForNesting() after incrementing global concurrency, ensuring reserved capacity is not consumed by queued sibling tasks during nesting setup.
TaskManager unit test for headroom preservation
packages/graphai/tests/units/test_task_manager.ts
New regression test verifies that prepareForNesting() preserves reserved headroom by keeping a queued sibling task unexecuted when concurrency is bumped, while running remains below the new limit.
Nested mapAgent integration test
packages/graphai/tests/units/test_nested_concurrency.ts
New regression test constructs a nested mapAgent with concurrency: 1 and verifies GraphAI.run() produces correct output ordering without triggering concurrency guard violations during nested execution.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 A queue that drains too eager, tasks collide,
But nesting now preserves its rightful tide,
The headroom slot stands guard with watchful care,
No sibling jumps—the nested task gets its fair share! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: fixing a regression where nested mapAgent fails due to nesting headroom not being preserved, directly referencing the problematic PR #1314.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/nesting-headroom-drain

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f07b4c87e5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +184 to +192
// IMPORTANT: do NOT drain the queue here. The concurrency++ above reserves a
// slot so the nested graph's own children have headroom to run (deadlock
// avoidance, which mapAgent asserts via `concurrency > running`). At this
// point the nested children are not queued yet -- the nested graph is built
// and run *after* prepareForNesting returns -- so draining now can only
// dispatch unrelated already-queued sibling tasks into the reserved slot,
// immediately consuming the headroom and tripping mapAgent's guard. The
// nested children, and any newly-eligible queued tasks, are dispatched when
// they call addTask() or when a slot frees up in onComplete().

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Update the compiled TaskManager output

This changes only src/task_manager.ts, but packages/graphai/package.json points runtime consumers at lib/index.js and publishes only ./lib; packages/graphai/lib/task_manager.js still contains the old prepareForNesting() drain loop. In any install or workspace import that resolves the package entrypoint instead of the TS source, the nested mapAgent regression remains unfixed despite these source tests passing. Please regenerate/commit the lib output for this TaskManager change.

Useful? React with 👍 / 👎.

@isamu
isamu merged commit 5003180 into main Jun 8, 2026
11 checks passed
@isamu
isamu deleted the fix/nesting-headroom-drain branch June 8, 2026 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants