---
status: resolved
trigger: "TRPCError: Initiative repository not available at requireInitiativeRepository (router.ts:247) on listInitiatives endpoint"
created: 2026-02-04T00:00:00Z
updated: 2026-02-04T00:00:00Z
resolved: 2026-02-04T00:00:00Z
---

## Current Focus

hypothesis: CONFIRMED - Server startup never creates database or repositories, so ctx.initiativeRepository is always undefined
test: traced full call chain from CLI -> CoordinationServer -> trpc-adapter -> createContext
expecting: N/A - confirmed
next_action: Fix by wiring database and repositories into server startup

## Symptoms

expected: Initiative page loads and displays list of initiatives via listInitiatives tRPC endpoint
actual: 500 Internal Server Error - "Initiative repository not available" thrown by requireInitiativeRepository at router.ts:247
errors: TRPCError: Initiative repository not available (code: INTERNAL_SERVER_ERROR, httpStatus: 500, path: listInitiatives)
reproduction: Load the initiative page in the browser
started: Never worked

## Eliminated

[none yet]

## Evidence

1. [2026-02-04 E1] `requireInitiativeRepository` at router.ts:245-253 throws when `ctx.initiativeRepository` is falsy.
2. [2026-02-04 E2] `TRPCContext.initiativeRepository` is typed as optional (`?`), set via `createContext()` in context.ts:85.
3. [2026-02-04 E3] `createContext` is called in production at `src/server/trpc-adapter.ts:82-88` -- only passes `eventBus`, `serverStartedAt`, `processCount`, `agentManager`. Does NOT pass `initiativeRepository`, `phaseRepository`, `planRepository`, `dispatchManager`, `coordinationManager`, or `phaseDispatchManager`.
4. [2026-02-04 E4] `TrpcAdapterOptions` interface (trpc-adapter.ts:17-26) only defines `eventBus`, `serverStartedAt`, `processCount`, `agentManager`. Missing all repository fields.
5. [2026-02-04 E5] `CoordinationServer.handleTrpc()` at server/index.ts:221-225 calls `createTrpcHandler` with only `eventBus`, `serverStartedAt`, `processCount`. No repositories passed.
6. [2026-02-04 E6] `CoordinationServer` class has no database or repository references at all.
7. [2026-02-04 E7] CLI `startServer()` at cli/index.ts:24-53 creates CoordinationServer with no database.
8. [2026-02-04 E8] `createDatabase()` exists at db/index.ts:20 but is never called in production server startup path.
9. [2026-02-04 E9] Test harness (test/harness.ts:408-450) shows correct wiring: creates db, all repositories, all managers, passes everything to createContext.

## Resolution

root_cause: Server startup path never created a database or repositories. The CLI startServer() created a CoordinationServer with only processManager, logManager, and eventBus. The tRPC adapter (trpc-adapter.ts) only passed eventBus, serverStartedAt, processCount, and agentManager to createContext(). The initiativeRepository (and all other repositories) were never instantiated or injected, so ctx.initiativeRepository was always undefined, causing requireInitiativeRepository() to throw on every call.

fix: |
  1. Created src/db/ensure-schema.ts - shared schema initialization with individual CREATE TABLE IF NOT EXISTS statements
  2. Extended TrpcAdapterOptions in src/server/trpc-adapter.ts to accept all repository/manager types and pass them to createContext()
  3. Added ServerContextDeps type and contextDeps parameter to CoordinationServer constructor, spreads into createTrpcHandler() options
  4. Updated CLI startServer() in src/cli/index.ts to create database, ensure schema, instantiate repositories, and pass them to CoordinationServer
  5. Refactored src/db/repositories/drizzle/test-helpers.ts to use shared ensureSchema() instead of duplicating SQL

verification: |
  - TypeScript compilation: zero errors (npx tsc --noEmit)
  - All 452 tests pass, 0 failures (npx vitest run)
  - ensureSchema correctly works with drizzle-orm by executing individual statements (fixed multi-statement SQL error)

files_changed:
  - src/db/ensure-schema.ts (NEW - shared schema initialization)
  - src/db/index.ts (added ensureSchema re-export)
  - src/db/repositories/drizzle/test-helpers.ts (refactored to use shared ensureSchema)
  - src/server/trpc-adapter.ts (extended options and context creation)
  - src/server/index.ts (added ServerContextDeps, contextDeps to constructor, spread into handler)
  - src/cli/index.ts (wired database, schema, and repositories into server startup)
