---
phase: 05-task-dispatch
plan: 03
type: execute
wave: 1
depends_on: []
files_modified: [src/dispatch/types.ts, src/dispatch/index.ts, src/events/types.ts, src/events/index.ts]
autonomous: true
---

<objective>
Define DispatchManager port interface and dispatch events.

Purpose: TASK-04 and TASK-05 require dependency-ordered task dispatch and work queue. Define the port interface that the adapter will implement, plus domain events for dispatch lifecycle.
Output: DispatchManager port interface, dispatch domain events, exported from dispatch module.
</objective>

<execution_context>
@~/.claude/get-shit-done/workflows/execute-plan.md
@~/.claude/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md

@src/agent/types.ts
@src/git/types.ts
@src/events/types.ts
@src/db/schema.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create DispatchManager port interface</name>
  <files>src/dispatch/types.ts, src/dispatch/index.ts</files>
  <action>
Create src/dispatch/ directory with types.ts and index.ts.

In types.ts, define:

1. QueuedTask type:
   - taskId: string
   - priority: 'low' | 'medium' | 'high'
   - queuedAt: Date
   - dependsOn: string[] (task IDs that must complete first)

2. DispatchResult type:
   - success: boolean
   - taskId: string
   - agentId?: string (assigned agent if dispatched)
   - reason?: string (why dispatch failed if not success)

3. DispatchManager port interface:
   /**
    * DispatchManager Port Interface
    *
    * Manages task dispatch queue with dependency ordering.
    *
    * Covers requirements:
    * - TASK-04: Dependency-ordered dispatch
    * - TASK-05: Work queue for available agents
    */
   interface DispatchManager {
     /**
      * Queue a task for dispatch.
      * Task will be dispatched when all dependencies complete.
      */
     queue(taskId: string): Promise<void>;

     /**
      * Get next dispatchable task.
      * Returns task with all dependencies complete, highest priority first.
      * Returns null if no tasks ready.
      */
     getNextDispatchable(): Promise<QueuedTask | null>;

     /**
      * Dispatch next available task to an agent.
      * Finds available agent, assigns task, spawns agent.
      * Returns dispatch result.
      */
     dispatchNext(): Promise<DispatchResult>;

     /**
      * Mark a task as complete.
      * Triggers re-evaluation of dependent tasks.
      */
     completeTask(taskId: string): Promise<void>;

     /**
      * Mark a task as blocked.
      * Task will not be dispatched until unblocked.
      */
     blockTask(taskId: string, reason: string): Promise<void>;

     /**
      * Get current queue state.
      * Returns all queued tasks with their dispatch readiness.
      */
     getQueueState(): Promise<{
       queued: QueuedTask[];
       ready: QueuedTask[];
       blocked: Array<{ taskId: string; reason: string }>;
     }>;
   }

Export all types from index.ts.
  </action>
  <verify>npm run build passes, types importable from src/dispatch</verify>
  <done>DispatchManager port interface defined and exported</done>
</task>

<task type="auto">
  <name>Task 2: Add dispatch domain events</name>
  <files>src/events/types.ts, src/events/index.ts</files>
  <action>
Add dispatch events to events/types.ts, following existing patterns:

1. TaskQueuedEvent:
   type: 'task:queued'
   payload: { taskId: string; priority: string; dependsOn: string[] }

2. TaskDispatchedEvent:
   type: 'task:dispatched'
   payload: { taskId: string; agentId: string; agentName: string }

3. TaskCompletedEvent:
   type: 'task:completed'
   payload: { taskId: string; agentId: string; success: boolean; message: string }

4. TaskBlockedEvent:
   type: 'task:blocked'
   payload: { taskId: string; reason: string; blockedBy?: string[] }

Add all four to DomainEventMap union type.
No changes needed to index.ts (types auto-exported).
  </action>
  <verify>npm run build passes, events compile without errors</verify>
  <done>Dispatch events added to DomainEventMap</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds
- [ ] npm test passes
- [ ] DispatchManager interface importable from src/dispatch
- [ ] Dispatch events in DomainEventMap
</verification>

<success_criteria>

- src/dispatch/types.ts with DispatchManager interface
- QueuedTask, DispatchResult types defined
- Four dispatch events added to events/types.ts
- All types properly exported
  </success_criteria>

<output>
After completion, create `.planning/phases/05-task-dispatch/05-03-SUMMARY.md`
</output>
