---
phase: 05-task-dispatch
plan: 05
type: execute
wave: 3
depends_on: ["05-02", "05-04"]
files_modified: [src/trpc/router.ts, src/trpc/context.ts, src/cli/index.ts]
autonomous: true
---

<objective>
Add message and dispatch tRPC procedures and CLI commands.

Purpose: Complete AGENT-06 (message visibility) and TASK-04/TASK-05 (dispatch control) with user-facing interfaces. Users can view pending agent questions and control task dispatch.
Output: tRPC message and dispatch procedures, CLI commands for messages and dispatch.
</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

@.planning/phases/05-task-dispatch/05-01-SUMMARY.md
@.planning/phases/05-task-dispatch/05-02-SUMMARY.md
@.planning/phases/05-task-dispatch/05-04-SUMMARY.md

@src/trpc/router.ts
@src/trpc/context.ts
@src/cli/index.ts
@src/db/repositories/message-repository.ts
@src/dispatch/types.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Add message and dispatch tRPC procedures</name>
  <files>src/trpc/router.ts, src/trpc/context.ts</files>
  <action>
1. Add to TRPCContext:
   - messageRepository?: MessageRepository
   - dispatchManager?: DispatchManager
   - Add requireMessageRepository and requireDispatchManager helpers

2. Add message procedures to appRouter:

   listMessages: publicProcedure
     .input(z.object({ agentId: z.string().optional(), status: z.enum(['pending', 'read', 'responded']).optional() }))
     .query(...)
   - If agentId, filter by agent
   - If status, filter by status
   - Return array of Message objects

   getMessage: publicProcedure
     .input(z.object({ id: z.string() }))
     .query(...)
   - Return single message or throw NOT_FOUND

   respondToMessage: publicProcedure
     .input(z.object({ id: z.string(), response: z.string() }))
     .mutation(...)
   - Update message with response and status='responded'
   - Return updated message

3. Add dispatch procedures:

   queueTask: publicProcedure
     .input(z.object({ taskId: z.string() }))
     .mutation(...)
   - Call dispatchManager.queue(taskId)
   - Return { success: true }

   dispatchNext: publicProcedure
     .mutation(...)
   - Call dispatchManager.dispatchNext()
   - Return DispatchResult

   getQueueState: publicProcedure
     .query(...)
   - Call dispatchManager.getQueueState()
   - Return queue state object

   completeTask: publicProcedure
     .input(z.object({ taskId: z.string() }))
     .mutation(...)
   - Call dispatchManager.completeTask(taskId)
   - Return { success: true }
  </action>
  <verify>npm run build passes</verify>
  <done>Message and dispatch tRPC procedures added</done>
</task>

<task type="auto">
  <name>Task 2: Add message and dispatch CLI commands</name>
  <files>src/cli/index.ts</files>
  <action>
1. Add message command group:

   const messageCommand = program
     .command('message')
     .description('View agent messages and questions');

   cw message list [--agent <agentId>] [--status <status>]
   - Call listMessages tRPC
   - Display as table: id (short), agent, type, content (truncated), status, createdAt
   - Show "(pending)" count prominently

   cw message read <messageId>
   - Call getMessage tRPC
   - Display full message content
   - If pending, show prompt to respond

   cw message respond <messageId> <response>
   - Call respondToMessage tRPC
   - Confirm response recorded
   - Suggest resuming agent if appropriate

2. Add dispatch command group:

   const dispatchCommand = program
     .command('dispatch')
     .description('Control task dispatch queue');

   cw dispatch queue <taskId>
   - Call queueTask tRPC
   - Confirm task queued

   cw dispatch next
   - Call dispatchNext tRPC
   - Show result: which task dispatched to which agent, or why it failed

   cw dispatch status
   - Call getQueueState tRPC
   - Show: queued count, ready count, blocked count
   - List ready tasks with their priority

   cw dispatch complete <taskId>
   - Call completeTask tRPC
   - Confirm task completed

Follow existing CLI patterns for error handling and output formatting.
  </action>
  <verify>cw message --help and cw dispatch --help show commands</verify>
  <done>Message and dispatch CLI commands functional</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds
- [ ] npm test passes
- [ ] cw message --help shows list, read, respond
- [ ] cw dispatch --help shows queue, next, status, complete
- [ ] All tRPC procedures added
</verification>

<success_criteria>

- Message tRPC procedures: listMessages, getMessage, respondToMessage
- Dispatch tRPC procedures: queueTask, dispatchNext, getQueueState, completeTask
- CLI message commands: list, read, respond
- CLI dispatch commands: queue, next, status, complete
- All requirements (AGENT-06, TASK-01, TASK-04, TASK-05) have user-facing interfaces
  </success_criteria>

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