Skip to content

v0.0.6.0 fix: make agent creation non-blocking by spawning run() as background task#127

Merged
Fullstop000 merged 7 commits intomainfrom
fix/agent-creation-startup-delay
Apr 30, 2026
Merged

v0.0.6.0 fix: make agent creation non-blocking by spawning run() as background task#127
Fullstop000 merged 7 commits intomainfrom
fix/agent-creation-startup-delay

Conversation

@Fullstop000
Copy link
Copy Markdown
Owner

@Fullstop000 Fullstop000 commented Apr 30, 2026

Summary

Agent creation is now non-blocking. Previously POST /api/agents blocked until driver handshake round-trips completed (500ms–3s for Codex/ACP-native drivers). Now the agent appears in the sidebar immediately and run() is spawned as a background task.

Changes:

  • src/agent/manager.rs — Core restructuring: ManagedAgent.handle changed to Arc<Mutex<Box<dyn Session>>>, event forwarder spawned before run(), run() spawned as background tokio::spawn
  • src/agent/event_forwarder.rs — Mechanical test constructor update
  • Added pre_starting guard flag with eviction protection
  • Background task cleanup on failure with Arc::ptr_eq identity verification

Commits:

2143253 style: cargo fmt
cf44a21 fix: handle cleanup and identity verification in background run() task
aec49c3 refactor: simplify process_state with Option ? operator
7828c40 fix: scope handle lock to run() call only, preventing deadlock with stop_agent
db34c99 fix: make agent creation non-blocking by spawning run() as background task

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_flag for the race-condition guard.

Pre-Landing Review

1 finding (P1 deadlock risk) fixed before landing. Lock ordering now consistent: agentshandle for all management paths; background task drops handle lock before touching agents.

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

… 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.
@Fullstop000 Fullstop000 changed the title fix: make agent creation non-blocking by spawning run() as background task v0.0.6.0 fix: make agent creation non-blocking by spawning run() as background task Apr 30, 2026
@Fullstop000 Fullstop000 merged commit bd889cc into main Apr 30, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent creation has multi-second delay for Codex and ACP-native drivers

1 participant