---
name: "Agent silently disappears when worktree creation fails"
---

# Bug 231: Agent silently disappears when worktree creation fails

## Description

When `start_agent` spawns its background task and worktree creation fails (e.g. stale branch conflict from a previous worktree at a different path), the agent entry is silently removed from the pool. No ERROR log is written, the UI just shows the agent as idle, and the story sits in current with no agent. The failure is only observable by noticing the agent never appeared.

## How to Reproduce

1. Have a story (e.g. 218) with a branch that's already checked out in a worktree outside .story_kit/worktrees/ (e.g. /private/tmp/story-218-worktree)
2. Call start_agent for that story
3. start_agent returns pending successfully
4. Background task tries to create worktree, git fails with "branch already used by worktree at ..."
5. PendingGuard or the error handler removes the agent from the pool

## Actual Result

Agent silently disappears from the pool. No ERROR log line. UI shows agent as idle. Story stuck in current with no agent.

## Expected Result

Worktree creation failure should: 1) Log an ERROR with the git error message, 2) Surface the failure in the UI (agent card shows error state), 3) Ideally attempt to recover (e.g. prune stale worktrees or force-checkout)

## Acceptance Criteria

- [ ] Worktree creation failures are logged at ERROR level with the full error message
- [ ] The agent shows a failed/error state in the UI instead of disappearing
- [ ] The AgentEvent::Error broadcast is persisted in the agent's event_log so the UI can display it
