---
phase: 01.1-hexagonal-architecture
plan: 06
type: execute
wave: 3
depends_on: ["01.1-02", "01.1-05"]
files_modified: [src/cli/trpc-client.ts, src/cli/index.ts, src/server/trpc-adapter.ts, src/server/index.ts, tests/integration/cli-server.test.ts]
autonomous: true
---

<objective>
Integrate tRPC client into CLI and add HTTP adapter to server.

Purpose: Complete the tRPC contract by connecting CLI to server. This establishes the pattern for all future client-server communication.
Output: CLI communicates with server via tRPC, with integration tests proving the full flow.
</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/01.1-hexagonal-architecture/01.1-02-SUMMARY.md
@.planning/phases/01.1-hexagonal-architecture/01.1-05-SUMMARY.md

@src/cli/index.ts
@src/server/index.ts
@src/trpc/index.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Add tRPC HTTP adapter to server and create CLI client</name>
  <files>src/server/trpc-adapter.ts, src/server/index.ts, src/cli/trpc-client.ts, package.json</files>
  <action>
    Install additional dependency if needed:
    - npm install @trpc/client (should already be dev dep, move to regular deps)

    Create src/server/trpc-adapter.ts:
    - Create function to handle tRPC requests via node:http
    - Use @trpc/server/adapters/fetch or manual handling
    - Route /trpc/* requests to tRPC router
    - Create context with eventBus, serverStartedAt, processCount

    Modify CoordinationServer:
    - In handleRequest(), add routing for /trpc/* paths
    - Delegate to tRPC adapter
    - Keep existing /health and /status routes for backwards compatibility
    - tRPC endpoints: /trpc/health, /trpc/status

    Create src/cli/trpc-client.ts:
    - Create tRPC client using @trpc/client
    - Configure with httpBatchLink pointing to http://127.0.0.1:{port}/trpc
    - Export typed client
    - Export helper function: createTrpcClient(port: number)

    Key: The existing HTTP endpoints (/health, /status) remain for curl/debugging.
    CLI will use tRPC endpoints for type safety.
  </action>
  <verify>npm run build succeeds</verify>
  <done>tRPC adapter integrated into server, CLI client created</done>
</task>

<task type="auto">
  <name>Task 2: Update CLI status command and write integration tests</name>
  <files>src/cli/index.ts, tests/integration/cli-server.test.ts</files>
  <action>
    Update src/cli/index.ts:
    - Modify status command to use tRPC client instead of placeholder
    - Create tRPC client with default port (3847) or env var
    - Call health procedure and display result
    - Handle connection errors gracefully (server not running)

    Keep the placeholder message if tRPC call fails:
    ```
    Server not running or unreachable. Start with: cw --server
    ```

    Create tests/integration/cli-server.test.ts:
    - Test full flow: start server, call tRPC from client, verify response
    - Test health procedure returns correct data
    - Test status procedure returns correct data
    - Test client handles server not running

    Integration test approach:
    - Start CoordinationServer on random port in beforeAll
    - Create tRPC client pointing to that port
    - Run procedures, verify responses
    - Stop server in afterAll

    These are INTEGRATION tests, not unit tests - they test the full stack.
  </action>
  <verify>npm test passes with all tests including integration tests</verify>
  <done>CLI uses tRPC for status, integration tests prove full flow works</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds
- [ ] npm test passes (all tests including integration)
- [ ] cw status connects to server via tRPC
- [ ] tRPC endpoints work alongside HTTP endpoints
- [ ] Full flow tested: server start → tRPC call → response
</verification>

<success_criteria>

- All tasks completed
- All verification checks pass
- CLI communicates with server via type-safe tRPC
- Integration tests prove the architecture works end-to-end
</success_criteria>

<output>
After completion, create `.planning/phases/01.1-hexagonal-architecture/01.1-06-SUMMARY.md`
</output>
