Skip to content

feat(agent): bound concurrent tool calls within a dependency wave - #147

Merged
hung12ct merged 1 commit into
mainfrom
feat/bounded-parallel-tool-calls
Aug 17, 2026
Merged

feat(agent): bound concurrent tool calls within a dependency wave#147
hung12ct merged 1 commit into
mainfrom
feat/bounded-parallel-tool-calls

Conversation

@hung12ct

Copy link
Copy Markdown
Owner

What & why

Tool calls inside a dependency wave were dispatched with one goroutine per call and no cap. A model returning a wide fan-out — 40 calls in one turn — opened 40 concurrent tool executions: 40 sockets, 40 subprocesses, or 40 database connections. There was no way for a host to bound it.

WithMaxParallelToolCalls(n) caps in-flight calls per wave. Nothing is dropped: a call over the cap waits for a slot, so the wave's result set is byte-identical either way and only peak resource use changes.

Changes

  • AgentLoop.MaxParallelToolCalls, defaulting to defaultMaxParallelToolCalls (8) from NewAgentLoop; 0 means unlimited.
  • WithMaxParallelToolCalls(n) option. Raise it for latency-bound tools (HTTP fan-out), lower it for tools each holding a scarce resource.
  • toolCallSemaphore(waveSize) returns nil when no cap can bind — unlimited setting, or a wave already at or below the cap — so the common small-wave path allocates nothing and pays no channel send/receive.

The dependency scheduler and <output_of:...> argument substitution are untouched; the cap applies within each wave the scheduler produces.

Behaviour change worth a look: the default is a real cap (8) rather than 0-preserves-today. Unbounded fan-out is a defect rather than a feature, so this follows the prefer-rebuild stance, but it is the one thing in this PR an existing adopter could notice. Waves are normally far smaller than 8, so the cap is invisible outside the pathological case.

Testing

  • TestMaxParallelToolCalls_BoundsWaveConcurrency — a probe tool records the high-water mark of overlapping Execute calls; a 20-call wave with a cap of 4 never exceeds 4, and all 20 still run.
  • TestMaxParallelToolCalls_DefaultApplies — the constructor default binds without an explicit option.
  • TestMaxParallelToolCalls_ZeroIsUnlimited — confirms the uncapped path genuinely exceeds the default, which is what makes the bounded assertion meaningful.
  • TestToolCallSemaphore_NilWhenNoCapBinds — the nil-return fast paths.
  • Every test asserts the executed-call count, so a regression that drops calls instead of delaying them fails loudly.
  • gofmt -l . empty, make lint 0 issues, make build, make test, make test-race all clean (concurrency change — race run is the relevant gate).

Checklist

  • Title is a Conventional Commit
  • gofmt -l . prints nothing
  • make lint is clean
  • make build passes
  • make test-race passes
  • Errors wrapped with a package prefix (n/a — no new error paths)
  • CHANGELOG.md updated if this PR is cut as a release tag
  • No changes to pkg/llm/, pkg/history/, or pkg/telemetry/

WithMaxParallelToolCalls caps wave concurrency (default 8, 0 = unlimited).
Calls over the cap wait for a slot rather than being dropped.
@hung12ct
hung12ct merged commit 9269d9a into main Aug 17, 2026
2 checks passed
@hung12ct
hung12ct deleted the feat/bounded-parallel-tool-calls branch August 17, 2026 16:22
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.

1 participant