Fix delegation race condition causing subagent corruption
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.tswith 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
I'd like to try this but get conflicts, could you please rebase this?