# All files / src/agent output-handler.ts

49.56% Statements 169/341
30.85% Branches 79/256
75% Functions 15/20
50.76% Lines 166/327

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
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929

    20x

    51x
    51x

    51x
    51x
    51x
    51x
    51x
    51x
    51x

    2x
    2x
    2x
    2x

    2x

    2x
    2x

    2x
    2x

    2x
    2x

    2x

    18x
    2x
    2x

    16x

    16x
    16x
    14x

    8x
    8x

    8x

    8x
    18x
    18x
    18x

    18x
    18x
    18x

    18x

    18x

    8x

    18x

    8x
    8x
    8x
    8x

    8x

    8x
    8x

    8x
    6x
    6x
    6x
    6x
    6x

    2x

    2x
    8x

    2x

    2x

    2x
    2x
    2x

    16x
    16x

    8x
    8x

    8x
    8x
    8x

    8x
    8x
    8x

    8x

    3x
    3x

    5x
    5x

    3x
    3x
    3x
    3x

    3x
    3x

    3x
    3x

    3x

    3x

    3x

    3x

    3x
    3x
    1x

    1x

    3x

    5x

    5x

    5x
    1x

    1x

    3x

    3x

    3x
    3x

    3x
    3x
    3x
    3x

    3x
    3x
    3x
    3x
    3x

    3x

    3x

    3x

    3x
    3x
    3x
    3x
    3x
    3x
    3x

    3x

    3x

    3x
    3x
    3x

    1x
    1x
    1x

    2x

    3x
    3x
    3x

    3x

    3x

    3x

    3x
    1x

    1x

    7x
    7x
    7x

    7x
    7x

    7x

    7x

    1x
    1x
    1x

    2x
    2x
    2x

    14x

    14x
    14x
    14x

    11x
    11x

    11x
    9x

    1x

    1x
    1x

     /**
     * OutputHandler — Stream event processing, signal parsing, file reading, result capture.
     *
     * Extracted from MultiProviderAgentManager. Processes all output from agent
     * subprocesses: stream events, agent signals, output files, and result/question
     * retrieval.
     */

    import { readFile } from 'node:fs/promises';
    import { existsSync } from 'node:fs';
    import { join } from 'node:path';
    import type { AgentRepository } from '../db/repositories/agent-repository.js';
    import type { ChangeSetRepository, CreateChangeSetEntryData } from '../db/repositories/change-set-repository.js';
    import type { PhaseRepository } from '../db/repositories/phase-repository.js';
    import type { TaskRepository } from '../db/repositories/task-repository.js';
    import type { PageRepository } from '../db/repositories/page-repository.js';
    import type {
      EventBus,
      AgentStoppedEvent,
      AgentCrashedEvent,
      AgentWaitingEvent,
    } from '../events/index.js';
    import type {
      AgentResult,
      AgentMode,
      PendingQuestions,
      QuestionItem,
    } from './types.js';
    import type { StreamEvent } from './providers/parsers/index.js';
    import type { AgentProviderConfig } from './providers/types.js';
    import { agentSignalSchema } from './schema.js';
    import {
      readSummary,
      readPhaseFiles,
      readTaskFiles,
      readDecisionFiles,
      readPageFiles,
      readFrontmatterFile,
    } from './file-io.js';
    import { getProvider } from './providers/registry.js';
    import { markdownToTiptapJson } from './markdown-to-tiptap.js';
    import type { SignalManager } from './lifecycle/signal-manager.js';
    import { createModuleLogger } from '../logger/index.js';

    const log = createModuleLogger('output-handler');

    /**
     * Tracks an active agent with its PID and file tailer.
     */
    export interface ActiveAgent {
      agentId: string;
      pid: number;
      tailer: import('./file-tailer.js').FileTailer;
      outputFilePath: string;
      /** Actual working directory the agent process runs in (may differ from getAgentWorkdir for standalone agents) */
      agentCwd?: string;
      result?: AgentResult;
      pendingQuestions?: PendingQuestions;
      streamResultText?: string;
      streamSessionId?: string;
      streamCostUsd?: number;
      /** True when the stream result indicated an error (e.g. auth failure) */
      streamIsError?: boolean;
      /** Cancel handle for polling timer — call to stop polling on cleanup */
      cancelPoll?: () => void;
    }

    /**
     * Result structure from Claude CLI with --output-format json.
     */
    interface ClaudeCliResult {
      type: 'result';
      subtype: 'success' | 'error';
      is_error: boolean;
      session_id: string;
      result: string;
      structured_output?: unknown;
      total_cost_usd?: number;
    }

    export class OutputHandler {
      private filePositions = new Map<string, number>();
      private completionLocks = new Set<string>(); // Track agents currently being processed

      constructor(
        private repository: AgentRepository,
        private eventBus?: EventBus,
        private changeSetRepository?: ChangeSetRepository,
        private phaseRepository?: PhaseRepository,
        private taskRepository?: TaskRepository,
        private pageRepository?: PageRepository,
        private signalManager?: SignalManager,
      ) {}

      /**
       * Validate that a signal file is complete and properly formatted.
       */
      private async validateSignalFile(filePath: string): Promise<boolean> {
        try {
          const content = await readFile(filePath, 'utf-8');
          const trimmed = content.trim();
          Eif (!trimmed) return false;

          // Check if JSON is complete (ends with } or ])
          const endsCorrectly = trimmed.endsWith('}') || trimmed.endsWith(']');
          Iif (!endsCorrectly) return false;

          // Try to parse as JSON to ensure it's valid
          JSON.parse(trimmed);
          return true;
        } catch {
          return false;
        }
      }

      /**
       * Read complete lines from a file, avoiding partial lines that might still be writing.
       * This eliminates race conditions when agents are still writing output.
       */
      private async readCompleteLines(filePath: string, fromPosition: number = 0): Promise<{ content: string; lastPosition: number }> {
        try {
          const content = await readFile(filePath, 'utf-8');

          Eif (fromPosition >= content.length) {
            return { content: '', lastPosition: fromPosition };
          }

          // Get content from our last read position
          const newContent = content.slice(fromPosition);

          // Split into lines
          const lines = newContent.split('\n');

          // If file doesn't end with newline, last element is potentially incomplete
          // Only process complete lines (all but the last, unless file ends with \n)
          const hasTrailingNewline = newContent.endsWith('\n');
          const completeLines = hasTrailingNewline ? lines : lines.slice(0, -1);

          // Calculate new position (only count complete lines)
          const completeLinesContent = completeLines.join('\n') + (completeLines.length > 0 && hasTrailingNewline ? '\n' : '');
          const newPosition = fromPosition + Buffer.byteLength(completeLinesContent, 'utf-8');

          return {
            content: completeLinesContent,
            lastPosition: newPosition
          };
        } catch (err) {
          log.debug({ filePath, err: err instanceof Error ? err.message : String(err) }, 'failed to read output file lines');
          return { content: '', lastPosition: fromPosition };
        }
      }

      /**
       * Handle a standardized stream event from a parser.
       */
      handleStreamEvent(
        agentId: string,
        event: StreamEvent,
        active: ActiveAgent | undefined,
      ): void {
        switch (event.type) {
          case 'init':
            if (active && event.sessionId) {
              active.streamSessionId = event.sessionId;
              this.repository.update(agentId, { sessionId: event.sessionId }).catch((err) => {
                log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to update session ID');
              });
            }
            break;

          case 'text_delta':
            // Text deltas are now streamed via DB log chunks + EventBus in manager.createLogChunkCallback
            break;

          case 'tool_use_start':
            log.debug({ agentId, tool: event.name, toolId: event.id }, 'tool use started');
            break;

          case 'result':
            if (active) {
              active.streamResultText = event.text;
              active.streamCostUsd = event.costUsd;
              active.streamIsError = event.isError === true;
              if (!active.streamSessionId && event.sessionId) {
                active.streamSessionId = event.sessionId;
              }
            }
            break;

          case 'error':
            log.error({ agentId, error: event.message }, 'stream error event');
            break;

          case 'turn_end':
            log.debug({ agentId, stopReason: event.stopReason }, 'turn ended');
            break;
        }
      }

      /**
       * Handle completion of a detached agent.
       * Processes the final result from the stream data captured by the tailer.
       *
       * RACE CONDITION FIX: Uses a completion lock to prevent duplicate processing.
       * Both the polling handler (handleDetachedAgentCompletion) and crash handler
       * (handleProcessCrashed) can call this method when a process exits with non-zero code.
       * The mutex ensures only one handler processes the completion per agent.
       */
      async handleCompletion(
        agentId: string,
        active: ActiveAgent | undefined,
        getAgentWorkdir: (alias: string) => string,
      ): Promise<void> {
        // CRITICAL: Prevent race condition - only one completion handler per agent
        if (this.completionLocks.has(agentId)) {
          log.debug({ agentId }, 'completion already being processed - skipping duplicate');
          return;
        }

        this.completionLocks.add(agentId);

        try {
          const agent = await this.repository.findById(agentId);
          if (!agent) return;

          const provider = getProvider(agent.provider);
          Iif (!provider) return;

          log.debug({ agentId }, 'detached agent completed');

          // Resolve actual agent working directory — standalone agents run in a
          // "workspace/" subdirectory inside getAgentWorkdir, so prefer agentCwd
          // recorded at spawn time when available.
          const agentWorkdir = active?.agentCwd ?? getAgentWorkdir(agent.worktreeId);
          const outputDir = join(agentWorkdir, '.cw', 'output');
          const expectedPwdFile = join(agentWorkdir, '.cw', 'expected-pwd.txt');
          const diagnosticFile = join(agentWorkdir, '.cw', 'spawn-diagnostic.json');

          const outputDirExists = existsSync(outputDir);
          const expectedPwdExists = existsSync(expectedPwdFile);
          const diagnosticExists = existsSync(diagnosticFile);

          log.info({
            agentId,
            agentWorkdir,
            outputDirExists,
            expectedPwdExists,
            diagnosticExists,
            verification: outputDirExists ? 'PASS' : 'FAIL'
          }, 'agent workdir verification completed');

          Iif (!outputDirExists) {
            log.warn({
              agentId,
              agentWorkdir
            }, 'No output files found in agent workdir! Agent may have run in wrong location.');
          }

          let signalText = active?.streamResultText;

          // If the stream result indicated an error (e.g. auth failure, usage limit),
          // route directly to error handling instead of trying to parse as signal JSON
          Iif (signalText && active?.streamIsError) {
            log.warn({ agentId, error: signalText }, 'agent returned error result');
            await this.handleAgentError(agentId, new Error(signalText), provider, getAgentWorkdir);
            return;
          }

          Eif (!signalText) {
            try {
              const outputFilePath = active?.outputFilePath ?? '';
              Eif (outputFilePath) {
                // First, check for robust signal.json completion before attempting incremental reading
                log.debug({ agentId, worktreeId: agent.worktreeId, agentWorkdir }, 'checking signal completion');

                const hasSignalCompletion = await this.readSignalCompletion(agentWorkdir);
                log.debug({ agentId, agentWorkdir, hasSignalCompletion }, 'signal completion check result');

                if (hasSignalCompletion) {
                  const signalPath = join(agentWorkdir, '.cw/output/signal.json');
                  const signalContent = await readFile(signalPath, 'utf-8');
                  log.debug({ agentId, signalPath }, 'detected completion via signal.json, processing');
                  await this.processSignalAndFiles(agentId, signalContent, agent.mode as AgentMode, getAgentWorkdir, active?.streamSessionId);
                  return;
                } else {
                  log.debug({ agentId, agentWorkdir }, 'no signal completion found, proceeding with raw output');
                }

                // Read only complete lines from the file, avoiding race conditions
                const lastPosition = this.filePositions.get(agentId) || 0;
                const { content: fileContent, lastPosition: newPosition } = await this.readCompleteLines(outputFilePath, lastPosition);

                Iif (fileContent.trim()) {
                  this.filePositions.set(agentId, newPosition);
                  await this.processAgentOutput(agentId, fileContent, provider, getAgentWorkdir);
                  return;
                }

                // If no new complete lines, but file might still be writing, try again with validation
                Iif (await this.validateSignalFile(outputFilePath)) {
                  const fullContent = await readFile(outputFilePath, 'utf-8');
                  if (fullContent.trim() && fullContent.length > newPosition) {
                    // File is complete and has content beyond what we've read
                    await this.processAgentOutput(agentId, fullContent, provider, getAgentWorkdir);
                    return;
                  }
                }
              }
            } catch { /* file empty or missing */ }

            log.debug({ agentId }, 'no result from stream or file, marking as error');
            await this.handleAgentError(agentId, new Error('No output received'), provider, getAgentWorkdir);
            return;
          }

          // Check for signal.json file first, then fall back to stream text
          if (await this.readSignalCompletion(agentWorkdir)) {
            const signalPath = join(agentWorkdir, '.cw/output/signal.json');
            const signalContent = await readFile(signalPath, 'utf-8');
            log.debug({ agentId, signalPath }, 'using signal.json content for completion');
            await this.processSignalAndFiles(agentId, signalContent, agent.mode as AgentMode, getAgentWorkdir, active?.streamSessionId);
          } else {
            log.debug({ agentId }, 'using stream text for completion (no signal.json found)');
            await this.processSignalAndFiles(
              agentId,
              signalText,
              agent.mode as AgentMode,
              getAgentWorkdir,
              active?.streamSessionId,
            );
          }
        } finally {
          this.completionLocks.delete(agentId);
          this.filePositions.delete(agentId);
        }
      }

      /**
       * Process agent signal JSON and read output files.
       * Universal handler for all providers and modes.
       */
      async processSignalAndFiles(
        agentId: string,
        signalText: string,
        mode: AgentMode,
        getAgentWorkdir: (alias: string) => string,
        sessionId?: string,
      ): Promise<void> {
        const agent = await this.repository.findById(agentId);
        Iif (!agent) return;

        let signal;
        let parsed;

        // Step 1: JSON parsing
        try {
          parsed = JSON.parse(signalText.trim());
          log.debug({ agentId }, 'signal JSON parsing successful');
        } catch (jsonError) {
          log.error({
            agentId,
            signalText: signalText.trim(),
            error: jsonError instanceof Error ? jsonError.message : String(jsonError),
            stack: jsonError instanceof Error ? jsonError.stack : undefined
          }, 'signal JSON parsing failed');
          await this.repository.update(agentId, { status: 'crashed' });
          this.emitCrashed(agent, 'Failed to parse agent signal JSON');
          return;
        }

        // Step 2: Schema validation
        try {
          signal = agentSignalSchema.parse(parsed);
          log.debug({ agentId, signalStatus: signal.status }, 'signal schema validation passed');
        } catch (schemaError) {
          log.error({
            agentId,
            signalText: signalText.trim(),
            parsed,
            error: schemaError instanceof Error ? schemaError.message : String(schemaError),
            stack: schemaError instanceof Error ? schemaError.stack : undefined
          }, 'signal schema validation failed');
          await this.repository.update(agentId, { status: 'crashed' });
          this.emitCrashed(agent, 'Failed to validate agent signal schema');
          return;
        }

        switch (signal.status) {
          case 'done':
            await this.processOutputFiles(agentId, agent, mode, getAgentWorkdir);
            break;
          case 'questions':
            await this.handleQuestions(agentId, agent, signal.questions, sessionId);
            break;
          case 'error':
            await this.handleSignalError(agentId, agent, signal.error);
            break;
        }
      }

      /**
       * Process output files from agent workdir after successful completion.
       * Performs direct writes to entities and records change sets.
       */
      private async processOutputFiles(
        agentId: string,
        agent: { id: string; name: string; taskId: string | null; worktreeId: string; mode: string; initiativeId?: string | null },
        mode: AgentMode,
        getAgentWorkdir: (alias: string) => string,
      ): Promise<void> {
        const agentWorkdir = getAgentWorkdir(agent.worktreeId);
        const summary = readSummary(agentWorkdir);
        const initiativeId = agent.initiativeId;
        const canWriteChangeSets = this.changeSetRepository && initiativeId;

        let resultMessage = summary?.body ?? 'Task completed';
        switch (mode) {
          case 'plan': {
            const phases = readPhaseFiles(agentWorkdir);
            if (canWriteChangeSets && this.phaseRepository && phases.length > 0) {
              const entries: CreateChangeSetEntryData[] = [];

              // First pass: create phases
              for (const [i, p] of phases.entries()) {
                try {
                  const tiptapContent = p.body ? JSON.stringify(markdownToTiptapJson(p.body)) : undefined;
                  const created = await this.phaseRepository.create({
                    id: p.id ?? undefined,
                    initiativeId,
                    name: p.title,
                    content: tiptapContent,
                  });
                  entries.push({
                    entityType: 'phase',
                    entityId: created.id,
                    action: 'create',
                    newState: JSON.stringify(created),
                    sortOrder: i,
                  });
                  this.eventBus?.emit({
                    type: 'phase:started' as const,
                    timestamp: new Date(),
                    payload: { phaseId: created.id, initiativeId },
                  });
                } catch (err) {
                  log.warn({ agentId, phase: p.title, err: err instanceof Error ? err.message : String(err) }, 'failed to create phase');
                }
              }

              // Second pass: create phase dependencies
              let depSortOrder = entries.length;
              for (const p of phases) {
                const phaseId = p.id;
                if (!phaseId || !Array.isArray(p.dependencies)) continue;
                for (const depFileId of p.dependencies) {
                  try {
                    await this.phaseRepository.createDependency(phaseId, depFileId);
                    entries.push({
                      entityType: 'phase_dependency',
                      entityId: `${phaseId}:${depFileId}`,
                      action: 'create',
                      newState: JSON.stringify({ phaseId, dependsOnPhaseId: depFileId }),
                      sortOrder: depSortOrder++,
                    });
                  } catch (err) {
                    log.warn({ agentId, phaseId, depFileId, err: err instanceof Error ? err.message : String(err) }, 'failed to create phase dependency');
                  }
                }
              }

              if (entries.length > 0) {
                try {
                  const cs = await this.changeSetRepository!.createWithEntries({
                    agentId,
                    agentName: agent.name,
                    initiativeId,
                    mode: 'plan',
                    summary: summary?.body ?? `Created ${phases.length} phases`,
                  }, entries);
                  this.eventBus?.emit({
                    type: 'changeset:created' as const,
                    timestamp: new Date(),
                    payload: { changeSetId: cs.id, initiativeId, agentId, mode: 'plan', entryCount: entries.length },
                  });
                } catch (err) {
                  log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to record change set after successful writes');
                }
              }
              resultMessage = summary?.body ?? `${phases.length} phases created`;
            } else {
              resultMessage = JSON.stringify({ summary: summary?.body, phases });
            }
            break;
          }
          case 'detail': {
            const tasks = readTaskFiles(agentWorkdir);
            if (canWriteChangeSets && this.taskRepository && tasks.length > 0) {
              const phaseInput = readFrontmatterFile(join(agentWorkdir, '.cw', 'input', 'phase.md'));
              const phaseId = (phaseInput?.data?.id as string) ?? null;
              const entries: CreateChangeSetEntryData[] = [];

              // Load existing tasks for dedup — prevents duplicates when multiple agents finish concurrently
              const existingTasks = phaseId ? await this.taskRepository.findByPhaseId(phaseId) : [];
              const existingNames = new Set(existingTasks.map(t => t.name));

              for (const [i, t] of tasks.entries()) {
                if (existingNames.has(t.title)) {
                  log.info({ agentId, task: t.title, phaseId }, 'skipped duplicate task');
                  continue;
                }
                try {
                  const created = await this.taskRepository.create({
                    initiativeId,
                    phaseId,
                    parentTaskId: agent.taskId ?? null,
                    name: t.title,
                    description: t.body ?? undefined,
                    category: (t.category as any) ?? 'execute',
                    type: (t.type as any) ?? 'auto',
                  });
                  existingNames.add(t.title); // prevent dupes within same agent output
                  entries.push({
                    entityType: 'task',
                    entityId: created.id,
                    action: 'create',
                    newState: JSON.stringify(created),
                    sortOrder: i,
                  });
                  this.eventBus?.emit({
                    type: 'task:completed' as const,
                    timestamp: new Date(),
                    payload: { taskId: created.id, agentId, success: true, message: 'Task created by detail' },
                  });
                } catch (err) {
                  log.warn({ agentId, task: t.title, err: err instanceof Error ? err.message : String(err) }, 'failed to create task');
                }
              }

              if (entries.length > 0) {
                try {
                  const cs = await this.changeSetRepository!.createWithEntries({
                    agentId,
                    agentName: agent.name,
                    initiativeId,
                    mode: 'detail',
                    summary: summary?.body ?? `Created ${tasks.length} tasks`,
                  }, entries);
                  this.eventBus?.emit({
                    type: 'changeset:created' as const,
                    timestamp: new Date(),
                    payload: { changeSetId: cs.id, initiativeId, agentId, mode: 'detail', entryCount: entries.length },
                  });
                } catch (err) {
                  log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to record change set after successful writes');
                }
              }
              resultMessage = summary?.body ?? `${tasks.length} tasks created`;
            } else {
              resultMessage = JSON.stringify({ summary: summary?.body, tasks });
            }
            break;
          }
          case 'discuss': {
            const decisions = readDecisionFiles(agentWorkdir);
            resultMessage = JSON.stringify({ summary: summary?.body, decisions });
            break;
          }
          case 'refine': {
            const pages = readPageFiles(agentWorkdir);
            Iif (canWriteChangeSets && this.pageRepository && pages.length > 0) {
              const entries: CreateChangeSetEntryData[] = [];

              for (const [i, p] of pages.entries()) {
                try {
                  if (!p.pageId) continue;
                  const existing = await this.pageRepository.findById(p.pageId);
                  if (!existing) {
                    log.warn({ agentId, pageId: p.pageId }, 'page not found for refine update');
                    continue;
                  }
                  const previousState = JSON.stringify(existing);
                  const tiptapJson = markdownToTiptapJson(p.body || '');
                  await this.pageRepository.update(p.pageId, {
                    content: JSON.stringify(tiptapJson),
                    title: p.title,
                  });
                  const updated = await this.pageRepository.findById(p.pageId);
                  entries.push({
                    entityType: 'page',
                    entityId: p.pageId,
                    action: 'update',
                    previousState,
                    newState: JSON.stringify(updated),
                    sortOrder: i,
                  });
                  this.eventBus?.emit({
                    type: 'page:updated' as const,
                    timestamp: new Date(),
                    payload: { pageId: p.pageId, initiativeId, title: p.title },
                  });
                } catch (err) {
                  log.warn({ agentId, pageId: p.pageId, err: err instanceof Error ? err.message : String(err) }, 'failed to update page');
                }
              }

              if (entries.length > 0) {
                try {
                  const cs = await this.changeSetRepository!.createWithEntries({
                    agentId,
                    agentName: agent.name,
                    initiativeId,
                    mode: 'refine',
                    summary: summary?.body ?? `Updated ${entries.length} pages`,
                  }, entries);
                  this.eventBus?.emit({
                    type: 'changeset:created' as const,
                    timestamp: new Date(),
                    payload: { changeSetId: cs.id, initiativeId, agentId, mode: 'refine', entryCount: entries.length },
                  });
                } catch (err) {
                  log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to record change set after successful writes');
                }
              }
              resultMessage = summary?.body ?? `${entries.length} pages updated`;
            } else {
              resultMessage = JSON.stringify({ summary: summary?.body, pages });
            }
            break;
          }
        }

        const resultPayload: AgentResult = {
          success: true,
          message: resultMessage,
          filesModified: summary?.filesModified,
        };
        await this.repository.update(agentId, { result: JSON.stringify(resultPayload), status: 'idle' });

        const reason = this.getStoppedReason(mode);
        if (this.eventBus) {
          const event: AgentStoppedEvent = {
            type: 'agent:stopped',
            timestamp: new Date(),
            payload: {
              agentId,
              name: agent.name,
              taskId: agent.taskId ?? '',
              reason,
            },
          };
          this.eventBus.emit(event);
        }

        return;
      }

      /**
       * Handle questions signal from agent.
       */
      async handleQuestions(
        agentId: string,
        agent: { id: string; name: string; taskId: string | null; sessionId: string | null },
        questions: QuestionItem[],
        sessionId?: string,
      ): Promise<void> {
        const questionsPayload: PendingQuestions = { questions };

        await this.repository.update(agentId, { pendingQuestions: JSON.stringify(questionsPayload), status: 'waiting_for_input' });

        if (this.eventBus) {
          const event: AgentWaitingEvent = {
            type: 'agent:waiting',
            timestamp: new Date(),
            payload: {
              agentId,
              name: agent.name,
              taskId: agent.taskId ?? '',
              sessionId: sessionId ?? agent.sessionId ?? '',
              questions,
            },
          };
          this.eventBus.emit(event);
        }
      }

      /**
       * Handle error signal from agent.
       */
      async handleSignalError(
        agentId: string,
        agent: { id: string; name: string; taskId: string | null },
        error: string,
      ): Promise<void> {
        const errorResult: AgentResult = { success: false, message: error };

        await this.repository.update(agentId, {
          result: JSON.stringify(errorResult),
          status: 'crashed'
        });

        this.emitCrashed(agent, error);
      }

      /**
       * Map agent mode to stopped event reason.
       */
      getStoppedReason(mode: AgentMode): AgentStoppedEvent['payload']['reason'] {
        switch (mode) {
          case 'discuss': return 'context_complete';
          case 'plan': return 'plan_complete';
          case 'detail': return 'detail_complete';
          case 'refine': return 'refine_complete';
          default: return 'task_complete';
        }
      }

      /**
       * Process raw output from an agent (from file or direct).
       */
      async processAgentOutput(
        agentId: string,
        rawOutput: string,
        provider: AgentProviderConfig,
        getAgentWorkdir: (alias: string) => string,
      ): Promise<void> {
        const agent = await this.repository.findById(agentId);
        Iif (!agent) return;

        // Extract session ID using provider's extraction config
        let sessionId: string | null = null;
        Eif (provider.sessionId) {
          const outputLines = rawOutput.trim().split('\n');
          Iif (provider.sessionId.extractFrom === 'result') {
            // Find the result line in JSONL output
            for (const line of outputLines) {
              try {
                const parsed = JSON.parse(line);
                if (parsed.type === 'result' || parsed[provider.sessionId.field]) {
                  sessionId = parsed[provider.sessionId.field] ?? null;
                  if (sessionId) break;
                }
              } catch { /* intentional: skip non-JSON JSONL lines */ }
            }
          E} else if (provider.sessionId.extractFrom === 'event') {
            for (const line of outputLines) {
              try {
                const event = JSON.parse(line);
                Iif (event.type === provider.sessionId.eventType) {
                  sessionId = event[provider.sessionId.field] ?? null;
                }
              } catch { /* intentional: skip non-JSON JSONL lines */ }
            }
          }
        }

        Iif (sessionId) {
          await this.repository.update(agentId, { sessionId });
        }
        log.debug({ agentId, provider: provider.name, hasSessionId: !!sessionId }, 'processing agent output');

        if (provider.name === 'claude') {
          // rawOutput may be a single JSON object or multi-line JSONL — find the result line
          let cliResult: ClaudeCliResult | null = null;
          const lines = rawOutput.trim().split('\n');
          for (const line of lines) {
            try {
              const parsed = JSON.parse(line);
              Eif (parsed.type === 'result') {
                cliResult = parsed;
              }
            } catch { /* intentional: skip non-JSON JSONL lines */ }
          }

          Iif (!cliResult) {
            log.error({ agentId }, 'no result event found in agent output');
            await this.handleAgentError(agentId, new Error('No result event in output'), provider, getAgentWorkdir);
            return;
          }

          // Handle error results (auth failure, usage limits, etc.)
          Iif (cliResult.is_error) {
            log.warn({ agentId, error: cliResult.result }, 'agent returned error result from file');
            await this.handleAgentError(agentId, new Error(cliResult.result), provider, getAgentWorkdir);
            return;
          }

          let signalText: string;
          try {
            const signal = cliResult.structured_output ?? JSON.parse(cliResult.result);
            signalText = JSON.stringify(signal);
          } catch (parseErr) {
            log.error({ agentId, err: parseErr instanceof Error ? parseErr.message : String(parseErr) }, 'failed to parse agent signal from result');
            await this.handleAgentError(agentId, new Error('Failed to parse agent signal'), provider, getAgentWorkdir);
            return;
          }

          await this.processSignalAndFiles(agentId, signalText, agent.mode as AgentMode, getAgentWorkdir, sessionId ?? undefined);
        } else E{
          await this.processSignalAndFiles(agentId, rawOutput, agent.mode as AgentMode, getAgentWorkdir, sessionId ?? undefined);
        }
      }

      /**
       * Handle agent errors. Detects usage limit exhaustion patterns.
       * Returns true if error was an exhaustion error (caller should attempt failover).
       */
      async handleAgentError(
        agentId: string,
        error: unknown,
        provider: AgentProviderConfig,
        _getAgentWorkdir: (alias: string) => string,
      ): Promise<void> {
        const errorMessage = error instanceof Error ? error.message : String(error);
        const agent = await this.repository.findById(agentId);
        Iif (!agent) return;

        log.error({ agentId, err: errorMessage }, 'agent error');

        const errorResult: AgentResult = {
          success: false,
          message: errorMessage,
        };

        await this.repository.update(agentId, {
          status: 'crashed',
          result: JSON.stringify(errorResult)
        });

        if (this.eventBus) {
          const event: AgentCrashedEvent = {
            type: 'agent:crashed',
            timestamp: new Date(),
            payload: {
              agentId,
              name: agent.name,
              taskId: agent.taskId ?? '',
              error: errorMessage,
            },
          };
          this.eventBus.emit(event);
        }
      }

      /**
       * Format answers map as structured prompt.
       * Handles special __instruction__ key for retry scenarios.
       */
      formatAnswersAsPrompt(answers: Record<string, string>): string {
        const instruction = answers['__instruction__'];
        const realAnswers = { ...answers };
        delete realAnswers['__instruction__'];

        const lines = Object.entries(realAnswers).map(
          ([questionId, answer]) => `[${questionId}]: ${answer}`,
        );
        const basePrompt = `Here are my answers to your questions:\n${lines.join('\n')}`;

        return instruction ? `${instruction.trim()}\n\n${basePrompt}` : basePrompt;
      }

      /**
       * Get the result of an agent's work.
       */
      async getResult(agentId: string, active?: ActiveAgent): Promise<AgentResult | null> {
        Iif (active?.result) return active.result;
        const agent = await this.repository.findById(agentId);
        return agent?.result ? JSON.parse(agent.result) : null;
      }

      /**
       * Get pending questions for an agent waiting for input.
       */
      async getPendingQuestions(agentId: string, active?: ActiveAgent): Promise<PendingQuestions | null> {
        Iif (active?.pendingQuestions) return active.pendingQuestions;
        const agent = await this.repository.findById(agentId);
        return agent?.pendingQuestions ? JSON.parse(agent.pendingQuestions) : null;
      }

      // =========================================================================
      // Private Helpers
      // =========================================================================

      /**
       * Read signal.json and return its content if the agent completed successfully.
       * Uses SignalManager for atomic read-and-validate when available.
       * Returns the raw JSON string on success, null if missing/invalid.
       */
      private async readSignalCompletion(agentWorkdir: string): Promise<string | null> {
        // Prefer SignalManager (unified implementation with proper validation)
        Iif (this.signalManager) {
          const signal = await this.signalManager.readSignal(agentWorkdir);
          return signal ? JSON.stringify(signal) : null;
        }

        // Fallback: inline read (for tests that don't inject SignalManager)
        try {
          const signalPath = join(agentWorkdir, '.cw/output/signal.json');
          if (!existsSync(signalPath)) return null;

          const signalContent = await readFile(signalPath, 'utf-8');
          const signal = JSON.parse(signalContent);

          if (signal.status === 'done' || signal.status === 'questions' || signal.status === 'error') {
            return signalContent;
          }
          return null;
        } catch (err) {
          log.debug({ agentWorkdir, err: err instanceof Error ? err.message : String(err) }, 'failed to read or parse signal.json');
          return null;
        }
      }

      private emitCrashed(agent: { id: string; name: string; taskId: string | null }, error: string): void {
        if (this.eventBus) {
          const event: AgentCrashedEvent = {
            type: 'agent:crashed',
            timestamp: new Date(),
            payload: {
              agentId: agent.id,
              name: agent.name,
              taskId: agent.taskId ?? '',
              error,
            },
          };
          this.eventBus.emit(event);
        }
      }
    }