---
phase: 11-architect-agent
plan: 02
type: execute
wave: 1
depends_on: []
files_modified:
  - src/db/repositories/initiative-repository.ts
  - src/db/repositories/phase-repository.ts
  - src/db/repositories/index.ts
  - src/db/index.ts
autonomous: true
---

<objective>
Create Initiative and Phase repositories following the hexagonal port/adapter pattern.

Purpose: Enable CRUD operations for initiatives and phases, which are required for the Architect agent to persist its breakdown work.

Output: InitiativeRepository and PhaseRepository ports with Drizzle adapters.
</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

# Repository pattern reference
@src/db/repositories/task-repository.ts
@src/db/repositories/drizzle-task-repository.ts

# Schema reference
@src/db/schema.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create InitiativeRepository port and adapter</name>
  <files>src/db/repositories/initiative-repository.ts, src/db/repositories/drizzle-initiative-repository.ts</files>
  <action>
1. Create src/db/repositories/initiative-repository.ts (PORT):
   ```typescript
   import type { Initiative, NewInitiative } from '../schema.js';

   export type CreateInitiativeData = Omit<NewInitiative, 'id' | 'createdAt' | 'updatedAt'>;
   export type UpdateInitiativeData = Partial<CreateInitiativeData>;

   export interface InitiativeRepository {
     create(data: CreateInitiativeData): Promise<Initiative>;
     findById(id: string): Promise<Initiative | null>;
     findAll(): Promise<Initiative[]>;
     findByStatus(status: 'active' | 'completed' | 'archived'): Promise<Initiative[]>;
     update(id: string, data: UpdateInitiativeData): Promise<Initiative>;
     delete(id: string): Promise<void>;
   }
   ```

2. Create src/db/repositories/drizzle-initiative-repository.ts (ADAPTER):
   - Follow DrizzleTaskRepository pattern exactly
   - Use randomUUID() for id generation
   - Set createdAt/updatedAt to new Date() on create
   - Update updatedAt on update
   - Fetch after insert to get defaults
  </action>
  <verify>npm run build passes</verify>
  <done>InitiativeRepository port and Drizzle adapter created</done>
</task>

<task type="auto">
  <name>Task 2: Create PhaseRepository port and adapter</name>
  <files>src/db/repositories/phase-repository.ts, src/db/repositories/drizzle-phase-repository.ts</files>
  <action>
1. Create src/db/repositories/phase-repository.ts (PORT):
   ```typescript
   import type { Phase, NewPhase } from '../schema.js';

   export type CreatePhaseData = Omit<NewPhase, 'id' | 'createdAt' | 'updatedAt'>;
   export type UpdatePhaseData = Partial<CreatePhaseData>;

   export interface PhaseRepository {
     create(data: CreatePhaseData): Promise<Phase>;
     findById(id: string): Promise<Phase | null>;
     findByInitiativeId(initiativeId: string): Promise<Phase[]>;
     findByNumber(initiativeId: string, number: number): Promise<Phase | null>;
     update(id: string, data: UpdatePhaseData): Promise<Phase>;
     delete(id: string): Promise<void>;
     getNextNumber(initiativeId: string): Promise<number>;
   }
   ```

2. Create src/db/repositories/drizzle-phase-repository.ts (ADAPTER):
   - Follow DrizzleTaskRepository pattern
   - findByInitiativeId returns phases ordered by number ASC
   - getNextNumber returns MAX(number) + 1, or 1 if no phases exist
   - Foreign key to initiative enforced by database
  </action>
  <verify>npm run build passes</verify>
  <done>PhaseRepository port and Drizzle adapter created</done>
</task>

<task type="auto">
  <name>Task 3: Export repositories from index files</name>
  <files>src/db/repositories/index.ts, src/db/index.ts</files>
  <action>
1. In src/db/repositories/index.ts, add exports:
   ```typescript
   export type { InitiativeRepository, CreateInitiativeData, UpdateInitiativeData } from './initiative-repository.js';
   export { DrizzleInitiativeRepository } from './drizzle-initiative-repository.js';

   export type { PhaseRepository, CreatePhaseData, UpdatePhaseData } from './phase-repository.js';
   export { DrizzlePhaseRepository } from './drizzle-phase-repository.js';
   ```

2. In src/db/index.ts, re-export the new repositories if not already done via index.ts.
  </action>
  <verify>npm run build passes, repositories are importable from src/db</verify>
  <done>All repositories exported and importable</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm run build succeeds without errors
- [ ] npm test passes
- [ ] InitiativeRepository and PhaseRepository are importable from src/db
- [ ] Adapters follow existing patterns (randomUUID, timestamps, fetch after insert)
</verification>

<success_criteria>

- All tasks completed
- Both repositories have port interfaces and Drizzle adapters
- Pattern matches existing TaskRepository implementation
- TypeScript types are correct
</success_criteria>

<output>
After completion, create `.planning/phases/11-architect-agent/11-02-SUMMARY.md`
</output>
