# All files / src container.ts

3.12% Statements 1/32
0% Branches 0/4
33.33% Functions 1/3
3.12% Lines 1/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267

    80x

     /**
     * Dependency Container
     *
     * Factory functions for creating the full dependency graph.
     * Keeps startServer() thin and makes repo wiring reusable by the test harness.
     */

    import type { DrizzleDatabase } from './db/index.js';
    import {
      createDatabase,
      ensureSchema,
      DrizzleInitiativeRepository,
      DrizzlePhaseRepository,
      DrizzleTaskRepository,
      DrizzleMessageRepository,
      DrizzleAgentRepository,
      DrizzlePageRepository,
      DrizzleProjectRepository,
      DrizzleAccountRepository,
      DrizzleChangeSetRepository,
      DrizzleLogChunkRepository,
      DrizzleConversationRepository,
    } from './db/index.js';
    import type { InitiativeRepository } from './db/repositories/initiative-repository.js';
    import type { PhaseRepository } from './db/repositories/phase-repository.js';
    import type { TaskRepository } from './db/repositories/task-repository.js';
    import type { MessageRepository } from './db/repositories/message-repository.js';
    import type { AgentRepository } from './db/repositories/agent-repository.js';
    import type { PageRepository } from './db/repositories/page-repository.js';
    import type { ProjectRepository } from './db/repositories/project-repository.js';
    import type { AccountRepository } from './db/repositories/account-repository.js';
    import type { ChangeSetRepository } from './db/repositories/change-set-repository.js';
    import type { LogChunkRepository } from './db/repositories/log-chunk-repository.js';
    import type { ConversationRepository } from './db/repositories/conversation-repository.js';
    import type { EventBus } from './events/index.js';
    import { createEventBus } from './events/index.js';
    import { ProcessManager, ProcessRegistry } from './process/index.js';
    import { LogManager } from './logging/index.js';
    import { MultiProviderAgentManager } from './agent/index.js';
    import { DefaultAccountCredentialManager } from './agent/credentials/index.js';
    import type { AccountCredentialManager } from './agent/credentials/types.js';
    import { DefaultDispatchManager } from './dispatch/manager.js';
    import { DefaultPhaseDispatchManager } from './dispatch/phase-manager.js';
    import type { DispatchManager, PhaseDispatchManager } from './dispatch/types.js';
    import { SimpleGitBranchManager } from './git/simple-git-branch-manager.js';
    import type { BranchManager } from './git/branch-manager.js';
    import { ExecutionOrchestrator } from './execution/orchestrator.js';
    import { DefaultConflictResolutionService } from './coordination/conflict-resolution-service.js';
    import { PreviewManager } from './preview/index.js';
    import { findWorkspaceRoot } from './config/index.js';
    import { createModuleLogger } from './logger/index.js';
    import type { ServerContextDeps } from './server/index.js';

    // =============================================================================
    // Repositories
    // =============================================================================

    /**
     * All 11 repository ports.
     */
    export interface Repositories {
      initiativeRepository: InitiativeRepository;
      phaseRepository: PhaseRepository;
      taskRepository: TaskRepository;
      messageRepository: MessageRepository;
      agentRepository: AgentRepository;
      pageRepository: PageRepository;
      projectRepository: ProjectRepository;
      accountRepository: AccountRepository;
      changeSetRepository: ChangeSetRepository;
      logChunkRepository: LogChunkRepository;
      conversationRepository: ConversationRepository;
    }

    /**
     * Create all 11 Drizzle repository adapters from a database instance.
     * Reusable by both the production server and the test harness.
     */
    export function createRepositories(db: DrizzleDatabase): Repositories {
      return {
        initiativeRepository: new DrizzleInitiativeRepository(db),
        phaseRepository: new DrizzlePhaseRepository(db),
        taskRepository: new DrizzleTaskRepository(db),
        messageRepository: new DrizzleMessageRepository(db),
        agentRepository: new DrizzleAgentRepository(db),
        pageRepository: new DrizzlePageRepository(db),
        projectRepository: new DrizzleProjectRepository(db),
        accountRepository: new DrizzleAccountRepository(db),
        changeSetRepository: new DrizzleChangeSetRepository(db),
        logChunkRepository: new DrizzleLogChunkRepository(db),
        conversationRepository: new DrizzleConversationRepository(db),
      };
    }

    // =============================================================================
    // Container
    // =============================================================================

    /**
     * Full dependency graph for the coordination server.
     */
    export interface Container extends Repositories {
      db: DrizzleDatabase;
      eventBus: EventBus;
      processManager: ProcessManager;
      logManager: LogManager;
      workspaceRoot: string;
      credentialManager: AccountCredentialManager;
      agentManager: MultiProviderAgentManager;
      dispatchManager: DispatchManager;
      phaseDispatchManager: PhaseDispatchManager;
      branchManager: BranchManager;
      executionOrchestrator: ExecutionOrchestrator;
      previewManager: PreviewManager;

      /** Extract the subset of deps that CoordinationServer needs. */
      toContextDeps(): ServerContextDeps;
    }

    /**
     * Options for container creation.
     */
    export interface ContainerOptions {
      debug?: boolean;
    }

    /**
     * Create the full dependency container.
     *
     * Wires: ProcessRegistry → EventBus → ProcessManager → LogManager →
     *        Database → Repositories → CredentialManager → AgentManager.
     * Runs ensureSchema() and reconcileAfterRestart() before returning.
     */
    export async function createContainer(options?: ContainerOptions): Promise<Container> {
      const log = createModuleLogger('container');

      // Infrastructure
      const registry = new ProcessRegistry();
      const eventBus = createEventBus();
      const processManager = new ProcessManager(registry, eventBus);
      const logManager = new LogManager();

      // Database
      const db = createDatabase();
      ensureSchema(db);
      log.info('database initialized');

      // Repositories
      const repos = createRepositories(db);
      log.info('repositories created');

      // Workspace root
      const workspaceRoot = findWorkspaceRoot(process.cwd()) ?? process.cwd();
      log.info({ workspaceRoot }, 'workspace root resolved');

      // Credential manager
      const credentialManager = new DefaultAccountCredentialManager(eventBus);
      log.info('credential manager created');

      // Agent manager
      const agentManager = new MultiProviderAgentManager(
        repos.agentRepository,
        workspaceRoot,
        repos.projectRepository,
        repos.accountRepository,
        eventBus,
        credentialManager,
        repos.changeSetRepository,
        repos.phaseRepository,
        repos.taskRepository,
        repos.pageRepository,
        repos.logChunkRepository,
        options?.debug ?? false,
      );
      log.info('agent manager created');

      // Reconcile agent state from any previous server session
      await agentManager.reconcileAfterRestart();
      log.info('agent reconciliation complete');

      // Branch manager
      const branchManager = new SimpleGitBranchManager();
      log.info('branch manager created');

      // Dispatch managers
      const dispatchManager = new DefaultDispatchManager(
        repos.taskRepository,
        repos.messageRepository,
        agentManager,
        eventBus,
        repos.initiativeRepository,
        repos.phaseRepository,
      );
      const phaseDispatchManager = new DefaultPhaseDispatchManager(
        repos.phaseRepository,
        repos.taskRepository,
        dispatchManager,
        eventBus,
        repos.initiativeRepository,
        repos.projectRepository,
        branchManager,
        workspaceRoot,
      );
      log.info('dispatch managers created');

      // Conflict resolution service (for orchestrator)
      const conflictResolutionService = new DefaultConflictResolutionService(
        repos.taskRepository,
        repos.agentRepository,
        repos.messageRepository,
        eventBus,
      );

      // Execution orchestrator
      const executionOrchestrator = new ExecutionOrchestrator(
        branchManager,
        repos.phaseRepository,
        repos.taskRepository,
        repos.initiativeRepository,
        repos.projectRepository,
        phaseDispatchManager,
        conflictResolutionService,
        eventBus,
        workspaceRoot,
      );
      executionOrchestrator.start();
      log.info('execution orchestrator started');

      // Preview manager
      const previewManager = new PreviewManager(
        repos.projectRepository,
        eventBus,
        workspaceRoot,
      );
      log.info('preview manager created');

      return {
        db,
        eventBus,
        processManager,
        logManager,
        workspaceRoot,
        credentialManager,
        agentManager,
        dispatchManager,
        phaseDispatchManager,
        branchManager,
        executionOrchestrator,
        previewManager,
        ...repos,

        toContextDeps(): ServerContextDeps {
          return {
            agentManager,
            credentialManager,
            dispatchManager,
            phaseDispatchManager,
            branchManager,
            executionOrchestrator,
            previewManager,
            workspaceRoot,
            ...repos,
          };
        },
      };
    }