opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Fix delegation race condition causing subagent corruption

Open arsham opened this issue 2 months ago • 7 comments

Subagents were randomly receiving primary agent configuration (including the task tool, wrong system prompts, and execution tools). This happened intermittently due to a race condition in how agent state is accessed during delegation.

Root Cause

Commit a1214fff2eaa removed a session-level mutex. After the refactor, parent and child sessions could access the shared Instance.state() concurrently, causing Agent.get() calls to race and subagents to pick up corrupted or partially-loaded agent configuration.

Solution

Restored the SessionLock mechanism with minimal changes:

  • Reinstated session/lock.ts with only the necessary functions (acquire, abort)
  • Wrapped the prompt loop with session-level locking to prevent concurrent agent state access

The fix preserves parallel execution - locks are per-session, so different subagents can still run concurrently.

Testing

I'll be testing this over the next couple of days since this issue occurs multiple times a day. That should give us a good indication of whether it's fully resolved.

Closes #4439

arsham avatar Nov 22 '25 02:11 arsham

I'd like to try this but get conflicts, could you please rebase this?

Nindaleth avatar Dec 30 '25 17:12 Nindaleth