[Go SDK] OpenCode harness has no concurrency guard
Summary
OpenCodeProvider.Execute in the Go SDK harness spawns a new opencode subprocess on every call with no concurrency cap, allowing bursty callers to launch unbounded parallel processes and trigger rate-limit flapping.
Context
harness/opencode.go:29 calls RunCLI immediately for every Execute invocation. A fan-out reasoner or a parallel workflow with 20 branches will launch 20 concurrent opencode processes, each establishing its own LLM API session. This saturates the backend's rate limits, causes 429 cascades, and consumes system resources (file descriptors, process slots) proportionally to fan-out width. The Python SDK's OpenCode provider uses a class-level semaphore to cap concurrency — the Go SDK has no equivalent.
Scope
In Scope
- Add a
MaxConcurrent intfield onOpenCodeProviderwith a default value (e.g. 4, configurable viaOpenCodeConfig). - Use a buffered channel or
golang.org/x/sync/semaphoreto gate entry intoRunCLI. - If
MaxConcurrentis 0 or negative, use the default (do not disable limiting entirely without an explicitNoLimitsentinel). - Callers blocked on the semaphore should respect context cancellation.
Out of Scope
- Adding a global semaphore across multiple
OpenCodeProviderinstances — per-instance is sufficient. - Changing the
opencodeCLI invocation or argument handling. - Adding retry logic for 429 responses — that is a separate concern.
Files
sdk/go/harness/opencode.go:29— add semaphore acquisition beforeRunCLI, release in defersdk/go/harness/opencode.go— addMaxConcurrent inttoOpenCodeProviderstruct andOpenCodeConfig; initialize semaphore in constructorsdk/go/harness/opencode_test.go— test: withMaxConcurrent=2, a burst of 5 concurrentExecutecalls does not launch more than 2 subprocesses simultaneously; context cancellation unblocks a waiting caller
Acceptance Criteria
- [ ] At most
MaxConcurrentopencodesubprocesses run simultaneously across concurrentExecutecalls on the same provider instance - [ ] A caller blocked waiting for a slot is unblocked immediately when the context is cancelled (returns
context.Canceled) - [ ] Default behavior (
MaxConcurrentnot set) limits to 4 concurrent executions - [ ] Tests pass (
go test ./sdk/go/...) - [ ] Linting passes (
make lint)
Notes for Contributors
Severity: MEDIUM
golang.org/x/sync/semaphore is cleaner than a buffered channel for this use case because it supports weighted acquisition (useful if future work adds priority levels). Check if golang.org/x/sync is already a dependency in sdk/go/go.mod; if not, a buffered channel semaphore is acceptable to avoid adding a new dependency.
shall i try to fix this ???
Yes please go ahead !
Thanks for picking this up, @dinzz005! :tada:
Here's what to expect:
- We'll check in after 7 days if we haven't heard an update
- After 14 days, we'll send a second reminder
- After 21 days without activity, we'll unassign to let others contribute
Tips for success:
- Drop a comment when you start working on it
- Share any blockers - we're here to help!
- Link your PR with
Fixes #438in the description
Happy coding! :rocket:
Hi — I couldn’t find OpenCodeConfig in the Go SDK; where is it defined, or should I create it for MaxConcurrent?
Ah, apologies — looks like there's a mistake in the issue. OpenCodeConfig doesn't exist, so don't create it. And the per-instance approach won't actually limit anything: Runner.buildProvider (runner.go:99-105) constructs a fresh OpenCodeProvider on every Run() call, so a field on the struct resets each time.
The Python SDK we're mirroring actually uses a ClassVar semaphore — process-global, not per-instance (opencode.py#L26-L36), default 3, env var OPENCODE_MAX_CONCURRENT.
Easiest fix: a package-level semaphore in opencode.go (a buffered chan struct{} is fine — no need to pull golang.org/x/sync), lazy-init from OPENCODE_MAX_CONCURRENT, acquire before the RunCLI call and release in defer. Use a select on ctx.Done() so a blocked caller respects cancellation.
Hey @dinzz005! :wave:
Just a friendly reminder that you're assigned to this issue. It's been about a week since assignment.
How's it going? We'd love to hear:
- Any progress updates?
- Running into any blockers we can help with?
- Need more time? (Totally fine!)
A quick comment helps us know you're still interested. If your plans have changed, no worries at all - just let us know and we can find another contributor.
Thanks for contributing to AgentField! :rocket:
Hi @dinzz005,
Just checking in again! It's been 14 days since we last heard from you on this issue.
Quick options:
- :speech_balloon: Drop a comment with a progress update
- :link: Link a draft PR if you've started working on it
- :wave: Let us know if you'd like to be unassigned (no judgment!)
If we don't hear back in 7 days, we'll unassign this issue to give other contributors a chance to help.
Thanks for understanding!
Hi @dinzz005,
It's been 21 days since the last update on this issue. To keep the project moving, we've unassigned this issue so other contributors can pick it up.
No worries at all! Life happens, and we totally understand. If you'd like to continue working on this, just comment here and we'll reassign it to you.
This issue is now available for anyone who'd like to contribute. Check out our contribution guidelines if you're interested!
Thanks for your interest in AgentField! :heart:
This was already fixed in PR #505 (commit 994e3bc2). The package-level semaphore with OPENCODE_MAX_CONCURRENT env var and context-aware acquire is live in main. This issue can be closed.
Confirmed on main — thanks @7vignesh.
994e3bc2 / #505 is there and does the job. opencode.go:160-165 acquires from a package-level buffered channel before the subprocess spawns, releases in a defer, and bails with ctx.Err() if the caller is cancelled while waiting. Default cap is 4, overridable via OPENCODE_MAX_CONCURRENT. Four tests cover it and all pass, including TestOpenCodeConcurrencyLimit_RealSubprocess, and I confirmed the default-of-4 empirically with 20 concurrent calls.
For the record: the fix is package-global, not the per-instance field the issue's Scope section originally described — that was my own correction on this thread, since a per-instance field resets on every Run(). Closing on behavior. Two small things if anyone revisits: semOnce caches the size on first Execute, so changing OPENCODE_MAX_CONCURRENT mid-process does nothing; and Go defaults to 4 where Python defaults to 10 — we'll sort out that parity separately.