The server never created a database or instantiated repositories, so all tRPC procedures requiring initiativeRepository (and other repos) threw "Initiative repository not available" on every request. - Create ensureSchema() for shared database table initialization - Extend TrpcAdapterOptions to accept all repository/manager dependencies - Add ServerContextDeps to CoordinationServer for dependency injection - Wire database, schema, and repositories in CLI startServer() - Refactor test-helpers to use shared ensureSchema()
4.3 KiB
status, trigger, created, updated, resolved
| status | trigger | created | updated | resolved |
|---|---|---|---|---|
| resolved | TRPCError: Initiative repository not available at requireInitiativeRepository (router.ts:247) on listInitiatives endpoint | 2026-02-04T00:00:00Z | 2026-02-04T00:00:00Z | 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
- [2026-02-04 E1]
requireInitiativeRepositoryat router.ts:245-253 throws whenctx.initiativeRepositoryis falsy. - [2026-02-04 E2]
TRPCContext.initiativeRepositoryis typed as optional (?), set viacreateContext()in context.ts:85. - [2026-02-04 E3]
createContextis called in production atsrc/server/trpc-adapter.ts:82-88-- only passeseventBus,serverStartedAt,processCount,agentManager. Does NOT passinitiativeRepository,phaseRepository,planRepository,dispatchManager,coordinationManager, orphaseDispatchManager. - [2026-02-04 E4]
TrpcAdapterOptionsinterface (trpc-adapter.ts:17-26) only defineseventBus,serverStartedAt,processCount,agentManager. Missing all repository fields. - [2026-02-04 E5]
CoordinationServer.handleTrpc()at server/index.ts:221-225 callscreateTrpcHandlerwith onlyeventBus,serverStartedAt,processCount. No repositories passed. - [2026-02-04 E6]
CoordinationServerclass has no database or repository references at all. - [2026-02-04 E7] CLI
startServer()at cli/index.ts:24-53 creates CoordinationServer with no database. - [2026-02-04 E8]
createDatabase()exists at db/index.ts:20 but is never called in production server startup path. - [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: |
- Created src/db/ensure-schema.ts - shared schema initialization with individual CREATE TABLE IF NOT EXISTS statements
- Extended TrpcAdapterOptions in src/server/trpc-adapter.ts to accept all repository/manager types and pass them to createContext()
- Added ServerContextDeps type and contextDeps parameter to CoordinationServer constructor, spreads into createTrpcHandler() options
- Updated CLI startServer() in src/cli/index.ts to create database, ensure schema, instantiate repositories, and pass them to CoordinationServer
- 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)