# Agent Dispatch Rules — Automated Orchestrator **Purpose:** Given the `ProjectState` from the Jira State Reader, determine which BMAD agent and workflow to invoke next. Implements the automated polling mode where the orchestrator drives the project forward based on Jira state. --- ## Dispatch Priority Rules are evaluated in order. The first matching rule fires. If no rule matches, the project is either complete or blocked. --- ## Rule Definitions project_state.pending_handoffs is not empty Dispatch the agent indicated by the handoff label 1. Take the first pending handoff from the list 2. Read the handoff comment on the issue for context (look for "## Agent Handoff:" comments) 3. Dispatch the target agent with the recommended workflow from the handoff comment 4. After dispatching, remove the `bmad-handoff-{agent}` label from the issue via Update Issue Handoff detected: {target_agent} should work on {issue_key}. Dispatching. Handoff labels take absolute priority over state-based rules below. This ensures agents respond to explicit signals from the previous agent rather than re-inferring state. project_state.locked_issues is not empty (after stale lock clearing) WAIT — another agent is currently working Agent is active on: {locked_issue_keys}. Waiting for completion. Poll again in 30 seconds project_state.artefacts.product_brief.exists == false Analyst (Mary) Create Brief [CB] None (user provides initial input) No product brief found. Starting with Analyst to create the product brief. This is typically the entry point for a new project. The user should have an idea to discuss. project_state.artefacts.product_brief.exists == true AND project_state.artefacts.prd.exists == false PM (John) Create PRD [CP] - Product Brief from Confluence (page_id from key map) - Any research reports from Confluence Product brief exists. Invoking PM to create the PRD. project_state.artefacts.prd.exists == true AND project_state.artefacts.ux_design.exists == false UX Designer (Sally) Create UX Design [CU] - PRD from Confluence - Product Brief from Confluence PRD exists. Invoking UX Designer for UX design. UX Design is recommended but optional. If user wants to skip, proceed to epics. project_state.artefacts.prd.exists == true AND project_state.epics.total == 0 PM (John) Create Epics and Stories [CE] — with Jira output override - PRD from Confluence - UX Design from Confluence (if exists) PRD exists but no epics in Jira. Invoking PM to create epics and stories. project_state.epics.total > 0 AND project_state.artefacts.architecture.exists == false Architect (Winston) Create Architecture [CA] - PRD from Confluence - Epic summaries from Jira (Search Issues: issuetype = Epic) - UX Design from Confluence (if exists) Epics exist but no architecture document. Invoking Architect. project_state.artefacts.architecture.exists == true AND project_state.stories.by_status.ready_for_dev == 0 AND project_state.stories.by_status.in_progress == 0 AND project_state.stories.by_status.backlog > 0 AND project_state.active_sprint.exists == false PM (John) or Architect (Winston) Implementation Readiness [IR] - PRD, UX Design, Architecture from Confluence - All Epics and Stories from Jira Architecture exists. Running implementation readiness check before sprint planning. This is an optional quality gate. Can be skipped if user wants to proceed directly. project_state.artefacts.architecture.exists == true AND project_state.active_sprint.exists == false AND project_state.stories.by_status.backlog > 0 SM (Bob) Sprint Planning [SP] — Jira override - All Epics and Stories from Jira No active sprint. Invoking SM for sprint planning. project_state.stories.by_status.backlog > 0 AND project_state.stories.by_status.ready_for_dev == 0 AND project_state.stories.by_status.in_progress == 0 SM (Bob) Create Story [CS] — Jira override - Next backlog story from Jira - Architecture from Confluence - Previous story learnings from Jira comments Stories in backlog need dev context. Invoking SM to prepare the next story. project_state.stories.by_status.ready_for_dev > 0 Dev (Amelia) Dev Story [DS] — Jira override - Next "Ready for Dev" story from Jira - Architecture from Confluence Stories ready for development. Invoking Dev agent. project_state.stories.by_status.review > 0 Dev (Amelia) Code Review [CR] — Jira override - Story in "Review" status from Jira - Dev agent record from Jira comments Stories awaiting review. Invoking code review. Recommended: use a fresh context window and different LLM for adversarial review project_state.stories.by_status.backlog > 0 AND (project_state.stories.by_status.in_progress > 0 OR project_state.stories.by_status.review > 0) SM (Bob) Create Story [CS] — Jira override - Next backlog story - Previous story learnings Dev is busy. Preparing the next story in parallel. Only if team capacity allows parallel work. Default: sequential story prep. Any epic where all stories are "done" AND epic status is "in_progress" SM (Bob) Epic Retrospective [ER] - All done stories in the epic from Jira - Dev agent records from Jira comments All stories complete for Epic {epic_key}. Running retrospective. Transition Epic to Done via transition-jira-issue task project_state.epics.by_status.done == project_state.epics.total AND project_state.epics.total > 0 COMPLETE All epics are done. Project implementation is complete! 🎉 No other rule matched ASK_USER Unable to determine next action automatically. Current state: {state_summary}. What would you like to do? --- ## Orchestrator Loop When running in automated mode, the orchestrator repeats: ``` 1. Run jira-state-reader to poll project state 2. Evaluate dispatch rules against state 3. If agent should be invoked: a. Load the specified context b. Invoke the agent with its workflow c. Wait for agent completion d. Return to step 1 4. If WAIT: pause and re-poll after 30 seconds 5. If COMPLETE or ASK_USER: stop and report to user ``` The user can interrupt at any time to override the automated dispatch or switch to manual mode.