v0.0.6.0 fix: make agent creation non-blocking by spawning run() as background task#127
Merged
Fullstop000 merged 7 commits intomainfrom Apr 30, 2026
Merged
Conversation
… task Move event forwarder spawn and agents-map insertion before handle.run() in start_agent(). Change ManagedAgent.handle to Arc<Mutex<Box<dyn Session>>> to allow shared ownership between the agents map and the background run task. Previously start_agent() awaited handle.run() before spawning the event forwarder and inserting into the map, gating the HTTP response on driver handshake round-trips (500ms-3s for Codex and ACP-native drivers). Add pre_starting flag to ManagedAgent to prevent a concurrent start_agent from evicting a handle during the bootstrap window. Background task cleans up on run() failure by removing the agent from the map. Closes #125
…top_agent The background task previously held Handle MutexGuard across the agents_ref lock call, creating a reverse lock order with stop_agent/sleep_agent which hold agents lock then acquire handle lock. Drop the handle guard immediately after run() completes, before touching agents_ref.
On failure: extract ManagedAgent from map, call close() on the handle, and abort _event_tasks — preventing resource leaks (child processes, bridge sessions, event forwarder tasks). On success and failure: verify handle identity via Arc::ptr_eq before mutating the map entry. Prevents a stale background task from clearing pre_starting or removing a newer agent that reused the same name. Batch both codex P1 fixes in one commit.
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.
Summary
Agent creation is now non-blocking. Previously
POST /api/agentsblocked until driver handshake round-trips completed (500ms–3s for Codex/ACP-native drivers). Now the agent appears in the sidebar immediately andrun()is spawned as a background task.Changes:
src/agent/manager.rs— Core restructuring:ManagedAgent.handlechanged toArc<Mutex<Box<dyn Session>>>, event forwarder spawned beforerun(),run()spawned as backgroundtokio::spawnsrc/agent/event_forwarder.rs— Mechanical test constructor updatepre_startingguard flag with eviction protectionArc::ptr_eqidentity verificationCommits:
Test Coverage
All 539 tests pass (328 lib + 22 main + 61 store + 76 server + 10 e2e + integration suites). Clippy clean. New test:
start_agent_respects_pre_starting_flagfor the race-condition guard.Pre-Landing Review
1 finding (P1 deadlock risk) fixed before landing. Lock ordering now consistent:
agents→handlefor all management paths; background task dropshandlelock before touchingagents.Codex Review
Codex found 2 P1 bugs (resource leak on run() failure, identity race in background task) — both fixed in
cf44a21. 3 P2 informational notes acknowledged.Closes #125
🤖 Generated with Claude Code