feat(agent): bound concurrent tool calls within a dependency wave - #147
Merged
Conversation
WithMaxParallelToolCalls caps wave concurrency (default 8, 0 = unlimited). Calls over the cap wait for a slot rather than being dropped.
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.
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 todefaultMaxParallelToolCalls(8) fromNewAgentLoop;0means 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 overlappingExecutecalls; 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.gofmt -l .empty,make lint0 issues,make build,make test,make test-raceall clean (concurrency change — race run is the relevant gate).Checklist
gofmt -l .prints nothingmake lintis cleanmake buildpassesmake test-racepassesCHANGELOG.mdupdated if this PR is cut as a release tagpkg/llm/,pkg/history/, orpkg/telemetry/