---
phase: 01-core-infrastructure
plan: 05
type: execute
wave: 3
depends_on: ["01-02", "01-03", "01-04"]
files_modified: [src/server/index.ts, src/server/shutdown.ts, src/cli/commands/server.ts, src/bin/cw.ts]
autonomous: true
---

<objective>
Add server mode to CLI with HTTP health endpoint and graceful shutdown handling.

Purpose: `cw --server` runs the background coordination server that manages agents. Server must handle signals gracefully.
Output: Server mode with health endpoint, PID file, and clean shutdown on SIGTERM/SIGINT.
</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-core-infrastructure/01-02-SUMMARY.md
@.planning/phases/01-core-infrastructure/01-03-SUMMARY.md
@.planning/phases/01-core-infrastructure/01-04-SUMMARY.md
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create HTTP server with health endpoint and PID file</name>
  <files>src/server/index.ts, src/server/types.ts, src/cli/index.ts</files>
  <action>
    Create src/server/types.ts:
    - ServerConfig: { port: number, host: string, pidFile: string }
    - ServerState: { startedAt: Date, processCount: number }

    Create src/server/index.ts:
    - Use node:http (not Express - keep it minimal)
    - CoordinationServer class:
      - constructor(config: ServerConfig, processManager: ProcessManager, logManager: LogManager)
      - start(): Promise<void> - starts HTTP server, writes PID file
      - stop(): Promise<void> - stops server, removes PID file
      - isRunning(): boolean

    HTTP routes (simple path matching):
    - GET /health → { status: 'ok', uptime: number, processCount: number }
    - GET /status → { server: ServerState, processes: ProcessInfo[] }

    PID file: ~/.cw/server.pid - contains process ID, checked to prevent duplicate servers.
    Default port: 3847 (arbitrary, configurable via env CW_PORT).

    Update src/cli/index.ts:
    - Add --server flag that starts CoordinationServer instead of CLI commands
    - Add --port option for custom port
  </action>
  <verify>cw --server starts HTTP server, curl localhost:3847/health returns OK</verify>
  <done>Server mode works, health endpoint responds, PID file created</done>
</task>

<task type="auto">
  <name>Task 2: Implement graceful shutdown</name>
  <files>src/server/shutdown.ts, src/server/index.ts</files>
  <action>
    Create src/server/shutdown.ts:
    - GracefulShutdown class:
      - constructor(server: CoordinationServer, processManager: ProcessManager, logManager: LogManager)
      - install(): void - registers signal handlers for SIGTERM, SIGINT, SIGHUP
      - shutdown(signal: string): Promise<void> - orchestrates cleanup

    Shutdown sequence:
    1. Log "Received {signal}, shutting down..."
    2. Stop accepting new connections
    3. Stop all managed processes (ProcessManager.stopAll())
    4. Close all log file handles
    5. Remove PID file
    6. Exit with code 0

    Timeout: If cleanup takes >10s, force exit with code 1.

    Handle double-signal: If SIGINT received twice, force immediate exit.

    Integrate into CoordinationServer.start() - install shutdown handlers after server starts.
  </action>
  <verify>cw --server responds to Ctrl+C by logging shutdown and cleaning up</verify>
  <done>SIGTERM/SIGINT triggers graceful shutdown, all resources cleaned up</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] `npm run build` succeeds
- [ ] `cw --server` starts server on port 3847
- [ ] `curl localhost:3847/health` returns JSON with status: 'ok'
- [ ] PID file created at ~/.cw/server.pid
- [ ] Ctrl+C (SIGINT) triggers graceful shutdown message
- [ ] PID file removed after shutdown
- [ ] Cannot start second server (PID file check prevents it)
</verification>

<success_criteria>

- All tasks completed
- All verification checks pass
- Server mode is the foundation for agent coordination
- Graceful shutdown satisfies INFRA-04
- Health endpoint enables monitoring
</success_criteria>

<output>
After completion, create `.planning/phases/01-core-infrastructure/01-05-SUMMARY.md`
</output>
