---
name: "Auto-assign only triggers on agent completion, not on failure or periodically"
---

# Bug 161: Auto-assign only triggers on agent completion, not on failure or periodically

## Description

`auto_assign_available_work()` is only called in two places:
1. On server startup (`server/src/main.rs:128`)
2. Inside `run_pipeline_advance_for_completed_agent` (`server/src/agents.rs` lines 830, 883, 945)

This means when agents **fail** (e.g. after a server restart kills their PTY processes), work items sitting in `2_current/`, `3_qa/`, or `4_merge/` are never picked up by free agents. Auto-assign only fires when an agent completes successfully, not when one fails.

## How to Reproduce

1. Have agents running on stories
2. Restart the server — agents become Failed (orphaned PTY processes)
3. Move new stories into `2_current/`
4. Observe that no coder picks them up, even though coders are free (Failed status)

## Expected Behavior

Auto-assign should also trigger when:
- An agent transitions to Failed status
- A work item file appears in a pipeline stage directory (watcher event)

## Suggested Fix

Add an `auto_assign_available_work` call in the watcher event handler. When a `WatcherEvent::WorkItem` fires for `2_current/`, `3_qa/`, or `4_merge/`, trigger auto-assign. This way, moving a story file into a pipeline directory automatically tries to assign a free agent.

Alternatively (or additionally), trigger auto-assign when `check_orphaned_agents` marks an agent as Failed.

## Key Files

- `server/src/agents.rs:1276` — `auto_assign_available_work()`
- `server/src/agents.rs:830,883,945` — only call sites after startup
- `server/src/agents.rs:1828` — `check_orphaned_agents()` — marks agents Failed but doesn't trigger auto-assign
- `server/src/main.rs:128` — startup call

## Acceptance Criteria

- [ ] When an agent fails, auto-assign runs to pick up unassigned work
- [ ] When a story file is moved into `2_current/`, `3_qa/`, or `4_merge/`, auto-assign runs
- [ ] Free agents pick up waiting work items without manual intervention
