---
name: Worktree-Based Agent Orchestration
---
# Story 30: Worktree-Based Agent Orchestration

## User Story
As a user, I want to press a button in the frontend to spin up an agent that works on a story in its own git worktree, so that multiple stories can be worked on concurrently without branch conflicts.

## Acceptance Criteria
- [ ] The Rust binary can create a git worktree for a given story, on a new feature branch.
- [ ] The Rust binary can remove a git worktree and clean up the feature branch after story completion.
- [ ] Project-specific setup commands (e.g., dependency install) are configurable per-project, not hardcoded to any language or framework.
- [ ] The Rust binary can spawn an agent process (e.g., Claude Code CLI) pointed at the worktree directory.
- [ ] The backend exposes an API endpoint to start an agent for a given story (creates worktree, runs setup, spawns agent).
- [ ] The backend exposes an API endpoint to stop a running agent and optionally tear down its worktree.
- [ ] The backend tracks running agents and their status (idle/running/done/error).
- [ ] The frontend displays a "Run" button on stories that are ready to be worked on.
- [ ] The frontend shows agent status (running/done/error) for active stories.
- [ ] Agent stdout/stderr is streamed to the frontend in real time (via WebSocket or SSE).

## Configuration
Agent and worktree behavior is driven by a project-level config file (e.g., `.story_kit/config.toml`), keeping the Rust binary language-agnostic. Projects can define multiple components, each with their own working directory and setup/teardown commands:

```toml
[[component]]
name = "server"
path = "."                          # relative to worktree root
setup = ["cargo check"]
teardown = []

[[component]]
name = "frontend"
path = "frontend"
setup = ["pnpm install"]
teardown = []

[agent]
command = "claude"
args = ["--print", "--directory", "{{worktree_path}}"]
prompt = "Read .story_kit/README.md, then pick up story {{story_id}}"
```

Components are set up in order. Each `path` is relative to the worktree root.

## Out of Scope
- Coordinating merges to master when multiple agents finish simultaneously (see Story 29).
- Lock file implementation for multi-agent conflict prevention (see Story 29).
- Port management for dev servers within worktrees.
- Agent-to-agent communication.
