agentfield icon indicating copy to clipboard operation
agentfield copied to clipboard

[Go SDK] OpenCode harness has no concurrency guard

Open santoshkumarradha opened this issue 7 months ago • 10 comments

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 int field on OpenCodeProvider with a default value (e.g. 4, configurable via OpenCodeConfig).
  • Use a buffered channel or golang.org/x/sync/semaphore to gate entry into RunCLI.
  • If MaxConcurrent is 0 or negative, use the default (do not disable limiting entirely without an explicit NoLimit sentinel).
  • Callers blocked on the semaphore should respect context cancellation.

Out of Scope

  • Adding a global semaphore across multiple OpenCodeProvider instances — per-instance is sufficient.
  • Changing the opencode CLI 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 before RunCLI, release in defer
  • sdk/go/harness/opencode.go — add MaxConcurrent int to OpenCodeProvider struct and OpenCodeConfig; initialize semaphore in constructor
  • sdk/go/harness/opencode_test.go — test: with MaxConcurrent=2, a burst of 5 concurrent Execute calls does not launch more than 2 subprocesses simultaneously; context cancellation unblocks a waiting caller

Acceptance Criteria

  • [ ] At most MaxConcurrent opencode subprocesses run simultaneously across concurrent Execute calls 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 (MaxConcurrent not 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.

santoshkumarradha avatar Apr 12 '26 03:04 santoshkumarradha

shall i try to fix this ???

dinzz005 avatar Apr 24 '26 11:04 dinzz005

Yes please go ahead !

santoshkumarradha avatar Apr 24 '26 15:04 santoshkumarradha

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 #438 in the description

Happy coding! :rocket:

github-actions[bot] avatar Apr 24 '26 15:04 github-actions[bot]

Hi — I couldn’t find OpenCodeConfig in the Go SDK; where is it defined, or should I create it for MaxConcurrent?

dinzz005 avatar Apr 26 '26 13:04 dinzz005

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.

AbirAbbas avatar Apr 27 '26 00:04 AbirAbbas

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:

github-actions[bot] avatar May 04 '26 10:05 github-actions[bot]

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!

github-actions[bot] avatar May 11 '26 11:05 github-actions[bot]

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:

github-actions[bot] avatar May 18 '26 12:05 github-actions[bot]

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.

7vignesh avatar Aug 25 '26 19:08 7vignesh

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.

AbirAbbas avatar Aug 26 '26 14:08 AbirAbbas