All files / src/agent manager.ts

48.11% Statements 166/345
36.41% Branches 59/162
32.72% Functions 18/55
48.28% Lines 155/321

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 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948                                                                                                              15x     38x   38x 38x                 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x   23x 23x 23x 23x 23x 23x                 23x 23x                     6x 6x 6x 6x                       10x 10x                                                                                           9x               9x 9x 9x   9x 9x           9x 9x 9x 9x 1x         8x 8x     8x 8x   8x 8x                           8x     8x         8x                           8x 8x 8x       8x 8x               8x 8x         8x                     8x     8x     8x           8x 8x   9x                     9x   8x               8x           9x     8x                               9x           9x 9x 9x     9x 8x         8x       8x     8x   8x   8x                                                                                                                                                                                                                                                                   3x                                     4x 4x 3x   3x 3x 2x 2x   3x     3x   3x   3x 3x         3x               1x 1x             2x 2x             2x 2x                                               4x               4x 4x 4x 1x   3x 1x   2x   2x 2x 2x       2x 2x     2x 2x 2x           2x   2x 2x     2x     2x 2x 4x         2x 2x       2x           2x   2x 2x 2x   2x 2x         2x     2x     2x   2x             1x                           4x 4x 3x     3x 3x 2x 2x   3x     3x     3x     3x       3x           3x     3x 3x         3x   3x                                                                                                                                                                                                                                                                                                                                                                                                                   11x                                  
/**
 * Multi-Provider Agent Manager — Orchestrator
 *
 * Implementation of AgentManager port supporting multiple CLI providers.
 * Delegates to extracted helpers:
 * - ProcessManager: subprocess spawn/kill/poll, worktree creation, command building
 * - CredentialHandler: account selection, credential write/refresh, exhaustion handling
 * - OutputHandler: stream events, signal parsing, file reading, result capture
 * - CleanupManager: worktree/branch/log removal, orphan cleanup, reconciliation
 */
 
import type {
  AgentManager,
  AgentInfo,
  SpawnAgentOptions,
  AgentResult,
  AgentStatus,
  AgentMode,
  PendingQuestions,
} from './types.js';
import type { AgentRepository } from '../db/repositories/agent-repository.js';
import type { AccountRepository } from '../db/repositories/account-repository.js';
import type { ProjectRepository } from '../db/repositories/project-repository.js';
import type { ChangeSetRepository } 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 { LogChunkRepository } from '../db/repositories/log-chunk-repository.js';
import { generateUniqueAlias } from './alias.js';
import type {
  EventBus,
  AgentSpawnedEvent,
  AgentStoppedEvent,
  AgentResumedEvent,
  AgentDeletedEvent,
  ProcessCrashedEvent,
} from '../events/index.js';
import { writeInputFiles } from './file-io.js';
import { buildWorkspaceLayout, buildInterAgentCommunication } from './prompts/index.js';
import { getProvider } from './providers/registry.js';
import { createModuleLogger } from '../logger/index.js';
import { join } from 'node:path';
import { unlink, readFile } from 'node:fs/promises';
import { existsSync, writeFileSync } from 'node:fs';
import type { AccountCredentialManager } from './credentials/types.js';
import { ProcessManager } from './process-manager.js';
import { CredentialHandler } from './credential-handler.js';
import { OutputHandler, type ActiveAgent } from './output-handler.js';
import { CleanupManager } from './cleanup-manager.js';
import { createLifecycleController } from './lifecycle/factory.js';
import type { AgentLifecycleController } from './lifecycle/controller.js';
import { AgentExhaustedError, AgentFailureError } from './lifecycle/retry-policy.js';
import { FileSystemSignalManager } from './lifecycle/signal-manager.js';
import type { SignalManager } from './lifecycle/signal-manager.js';
 
const log = createModuleLogger('agent-manager');
 
export class MultiProviderAgentManager implements AgentManager {
  private static readonly MAX_COMMIT_RETRIES = 1;
 
  private activeAgents: Map<string, ActiveAgent> = new Map();
  private commitRetryCount: Map<string, number> = new Map();
  private processManager: ProcessManager;
  private credentialHandler: CredentialHandler;
  private outputHandler: OutputHandler;
  private cleanupManager: CleanupManager;
  private lifecycleController: AgentLifecycleController;
  private signalManager: SignalManager;
 
  constructor(
    private repository: AgentRepository,
    private workspaceRoot: string,
    private projectRepository: ProjectRepository,
    private accountRepository?: AccountRepository,
    private eventBus?: EventBus,
    private credentialManager?: AccountCredentialManager,
    private changeSetRepository?: ChangeSetRepository,
    private phaseRepository?: PhaseRepository,
    private taskRepository?: TaskRepository,
    private pageRepository?: PageRepository,
    private logChunkRepository?: LogChunkRepository,
    private debug: boolean = false,
  ) {
    this.signalManager = new FileSystemSignalManager();
    this.processManager = new ProcessManager(workspaceRoot, projectRepository);
    this.credentialHandler = new CredentialHandler(workspaceRoot, accountRepository, credentialManager);
    this.outputHandler = new OutputHandler(repository, eventBus, changeSetRepository, phaseRepository, taskRepository, pageRepository, this.signalManager);
    this.cleanupManager = new CleanupManager(workspaceRoot, repository, projectRepository, eventBus, debug, this.signalManager);
    this.lifecycleController = createLifecycleController({
      repository,
      processManager: this.processManager,
      cleanupManager: this.cleanupManager,
      accountRepository,
      debug,
    });
 
    // Listen for process crashed events to handle agents specially
    Eif (eventBus) {
      eventBus.on('process:crashed', async (event: ProcessCrashedEvent) => {
        await this.handleProcessCrashed(event.payload.processId, event.payload.exitCode, event.payload.signal);
      });
    }
  }
 
  /**
   * Centralized cleanup of all in-memory state for an agent.
   * Cancels polling timer, removes from activeAgents and commitRetryCount.
   */
  private cleanupAgentState(agentId: string): void {
    const active = this.activeAgents.get(agentId);
    if (active?.cancelPoll) active.cancelPoll();
    this.activeAgents.delete(agentId);
    this.commitRetryCount.delete(agentId);
  }
 
  /**
   * Create a fire-and-forget callback for persisting raw output chunks to the DB.
   * Returns undefined if no logChunkRepository is configured.
   */
  private createLogChunkCallback(
    agentId: string,
    agentName: string,
    sessionNumber: number,
  ): ((content: string) => void) | undefined {
    const repo = this.logChunkRepository;
    Eif (!repo) return undefined;
 
    return (content) => {
      repo.insertChunk({ agentId, agentName, sessionNumber, content })
        .then(() => {
          if (this.eventBus) {
            this.eventBus.emit({
              type: 'agent:output' as const,
              timestamp: new Date(),
              payload: { agentId, stream: 'stdout', data: content },
            });
          }
        })
        .catch(err => log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to persist log chunk'));
    };
  }
 
  /**
   * Spawn a new agent using the unified lifecycle controller.
   * Features comprehensive retry, error handling, and cleanup.
   */
  async spawnWithLifecycle(options: SpawnAgentOptions): Promise<AgentInfo> {
    log.info({
      taskId: options.taskId,
      provider: options.provider,
      initiativeId: options.initiativeId,
      mode: options.mode
    }, 'spawning agent with unified lifecycle management');
 
    let spawnedAgent: AgentInfo | undefined;
    await this.lifecycleController.spawnWithRetry(
      async (opts) => {
        const agent = await this.spawnInternal(opts);
        spawnedAgent = agent;
        return { id: agent.id, name: agent.name, status: agent.status, initiativeId: agent.initiativeId, worktreeId: agent.worktreeId };
      },
      options
    );
    return spawnedAgent!;
  }
 
  /**
   * Spawn a new agent to work on a task (legacy method).
   * Consider using spawnWithLifecycle for better error handling.
   */
  async spawn(options: SpawnAgentOptions): Promise<AgentInfo> {
    return this.spawnInternal(options);
  }
 
  /**
   * Internal spawn implementation without lifecycle management.
   * Used by both legacy spawn() and new lifecycle-managed spawn.
   */
  private async spawnInternal(options: SpawnAgentOptions): Promise<AgentInfo> {
    const { taskId, cwd, mode = 'execute', provider: providerName = 'claude', initiativeId, baseBranch, branchName } = options;
    let { prompt } = options;
    log.info({ taskId, provider: providerName, initiativeId, mode, baseBranch, branchName }, 'spawn requested');
 
    const provider = getProvider(providerName);
    Iif (!provider) {
      throw new Error(`Unknown provider: '${providerName}'. Available: claude, codex, gemini, cursor, auggie, amp, opencode`);
    }
 
    // Generate or validate name
    let name: string;
    if (options.name) {
      name = options.name;
      const existing = await this.repository.findByName(name);
      if (existing) {
        throw new Error(`Agent with name '${name}' already exists`);
      }
    } else E{
      name = await generateUniqueAlias(this.repository);
    }
    const alias = name;
    log.debug({ alias }, 'alias generated');
 
    // 1. Account selection
    let accountId: string | null = null;
    let accountConfigDir: string | null = null;
 
    const accountResult = await this.credentialHandler.selectAccount(providerName);
    Iif (accountResult) {
      accountId = accountResult.accountId;
      accountConfigDir = accountResult.configDir;
 
      this.credentialHandler.writeCredentialsToDisk(accountResult.account, accountConfigDir);
      const { valid, refreshed } = await this.credentialHandler.ensureCredentials(accountConfigDir, accountId);
      if (!valid) {
        log.warn({ alias, accountId }, 'failed to refresh account credentials, proceeding anyway');
      }
      if (refreshed) {
        await this.credentialHandler.persistRefreshedCredentials(accountId, accountConfigDir);
      }
    }
 
    Iif (accountId) {
      log.info({ alias, accountId }, 'account selected');
    } else {
      log.debug('no accounts available, spawning without account');
    }
 
    // 2. Create isolated worktrees
    let agentCwd: string;
    Iif (initiativeId) {
      log.debug({ alias, initiativeId, baseBranch, branchName }, 'creating initiative-based worktrees');
      agentCwd = await this.processManager.createProjectWorktrees(alias, initiativeId, baseBranch, branchName);
 
      // Log projects linked to the initiative
      const projects = await this.projectRepository.findProjectsByInitiativeId(initiativeId);
      log.info({
        alias,
        initiativeId,
        projectCount: projects.length,
        projects: projects.map(p => ({ name: p.name, url: p.url })),
        agentCwd
      }, 'initiative-based agent workdir created');
    } else {
      log.debug({ alias }, 'creating standalone worktree');
      agentCwd = await this.processManager.createStandaloneWorktree(alias);
      log.info({ alias, agentCwd }, 'standalone agent workdir created');
    }
 
    // Verify the final agentCwd exists
    const cwdVerified = existsSync(agentCwd);
    log.info({
      alias,
      agentCwd,
      cwdVerified,
      initiativeBasedAgent: !!initiativeId
    }, 'agent workdir setup completed');
 
    // 2b. Append workspace layout to prompt now that worktrees exist
    const workspaceSection = buildWorkspaceLayout(agentCwd);
    Iif (workspaceSection) {
      prompt = prompt + workspaceSection;
    }
 
    // 3. Create agent record
    const agent = await this.repository.create({
      name: alias,
      taskId: taskId ?? null,
      initiativeId: initiativeId ?? null,
      sessionId: null,
      worktreeId: alias,
      status: 'running',
      mode,
      provider: providerName,
      accountId,
    });
    const agentId = agent.id;
 
    // 3a. Append inter-agent communication instructions with actual agent ID
    prompt = prompt + buildInterAgentCommunication(agentId);
 
    // 3b. Write input files (after agent creation so we can include agentId/agentName)
    Iif (options.inputContext) {
      writeInputFiles({ agentWorkdir: agentCwd, ...options.inputContext, agentId, agentName: alias });
      log.debug({ alias }, 'input files written');
    }
 
    // 4. Build spawn command
    const { command, args, env: providerEnv } = this.processManager.buildSpawnCommand(provider, prompt);
    const finalCwd = cwd ?? agentCwd;
 
    log.info({
      agentId,
      alias,
      command,
      args: args.join(' '),
      finalCwd,
      customCwdProvided: !!cwd,
      providerEnv: Object.keys(providerEnv)
    }, 'spawn command built');
 
    // 5. Prepare process environment with credentials
    const { processEnv } = await this.credentialHandler.prepareProcessEnv(providerEnv, provider, accountId);
 
    log.debug({
      agentId,
      finalProcessEnv: Object.keys(processEnv),
      hasAccountConfig: !!accountId,
      hasOAuthToken: !!processEnv['CLAUDE_CODE_OAUTH_TOKEN'],
    }, 'process environment prepared');
 
    // 6. Spawn detached subprocess
    const { pid, outputFilePath, tailer } = this.processManager.spawnDetached(
      agentId, alias, command, args, cwd ?? agentCwd, processEnv, providerName, prompt,
      (event) => this.outputHandler.handleStreamEvent(agentId, event, this.activeAgents.get(agentId)),
      this.createLogChunkCallback(agentId, alias, 1),
    );
 
    await this.repository.update(agentId, { pid, outputFilePath });
 
    // Write spawn diagnostic file for post-execution verification
    const diagnostic = {
      timestamp: new Date().toISOString(),
      agentId,
      alias,
      intendedCwd: finalCwd,
      worktreeId: agent.worktreeId,
      provider: providerName,
      command,
      args,
      env: processEnv,
      cwdExistsAtSpawn: existsSync(finalCwd),
      initiativeId: initiativeId || null,
      customCwdProvided: !!cwd,
      accountId: accountId || null,
    };
 
    writeFileSync(
      join(finalCwd, '.cw', 'spawn-diagnostic.json'),
      JSON.stringify(diagnostic, null, 2),
      'utf-8'
    );
 
    const activeEntry: ActiveAgent = { agentId, pid, tailer, outputFilePath, agentCwd: finalCwd };
    this.activeAgents.set(agentId, activeEntry);
    log.info({ agentId, alias, pid, diagnosticWritten: true }, 'detached subprocess started with diagnostic');
 
    // Emit spawned event
    if (this.eventBus) {
      const event: AgentSpawnedEvent = {
        type: 'agent:spawned',
        timestamp: new Date(),
        payload: { agentId, name: alias, taskId: taskId ?? null, worktreeId: alias, provider: providerName },
      };
      this.eventBus.emit(event);
    }
 
    // Start polling for completion
    const { cancel } = this.processManager.pollForCompletion(
      agentId, pid,
      () => this.handleDetachedAgentCompletion(agentId),
      () => this.activeAgents.get(agentId)?.tailer,
    );
    activeEntry.cancelPoll = cancel;
 
    return this.toAgentInfo(agent);
  }
 
  /**
   * Handle completion of a detached agent.
   */
  private async handleDetachedAgentCompletion(agentId: string): Promise<void> {
    if (!this.activeAgents.has(agentId)) return;
 
    const active = this.activeAgents.get(agentId);
    await this.outputHandler.handleCompletion(
      agentId,
      active,
      (alias) => this.processManager.getAgentWorkdir(alias),
    );
 
    // Sync credentials back to DB if the agent had an account
    await this.syncCredentialsPostCompletion(agentId);
 
    this.cleanupAgentState(agentId);
 
    // Auto-cleanup workdir after completion
    await this.tryAutoCleanup(agentId);
  }
 
  /**
   * Attempt auto-cleanup of agent workdir after completion.
   * If dirty and retries remain, resumes the agent to commit changes.
   */
  private async tryAutoCleanup(agentId: string): Promise<void> {
    try {
      const agent = await this.repository.findById(agentId);
      if (!agent || agent.status !== 'idle') return;
 
      const { clean, removed } = await this.cleanupManager.autoCleanupAfterCompletion(
        agentId, agent.name, agent.initiativeId,
      );
 
      if (removed) {
        this.commitRetryCount.delete(agentId);
        log.info({ agentId, alias: agent.name }, 'auto-cleanup completed');
        return;
      }
 
      if (!clean) {
        const retries = this.commitRetryCount.get(agentId) ?? 0;
        if (retries < MultiProviderAgentManager.MAX_COMMIT_RETRIES) {
          this.commitRetryCount.set(agentId, retries + 1);
          const resumed = await this.resumeForCommit(agentId);
          if (resumed) {
            log.info({ agentId, alias: agent.name, retry: retries + 1 }, 'resumed agent to commit uncommitted changes');
            return;
          }
        }
        log.warn({ agentId, alias: agent.name }, 'agent workdir has uncommitted changes after max retries, leaving in place');
        this.commitRetryCount.delete(agentId);
      }
    } catch (err) {
      log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'auto-cleanup failed');
      this.commitRetryCount.delete(agentId);
    }
  }
 
  /**
   * Resume an agent's session with a prompt to commit uncommitted changes.
   * Returns false if the agent can't be resumed (no session, provider doesn't support resume).
   */
  private async resumeForCommit(agentId: string): Promise<boolean> {
    const agent = await this.repository.findById(agentId);
    if (!agent?.sessionId) return false;
 
    const provider = getProvider(agent.provider);
    if (!provider || provider.resumeStyle === 'none') return false;
 
    // Check which specific worktrees are dirty — skip resume if all clean
    const dirtyPaths = await this.cleanupManager.getDirtyWorktreePaths(agent.name, agent.initiativeId);
    if (dirtyPaths.length === 0) return false;
 
    const dirtyList = dirtyPaths.map(p => `- \`${p}/\``).join('\n');
    const commitPrompt =
      'You have uncommitted changes in the following project directories:\n' +
      dirtyList + '\n\n' +
      'For each directory listed above, `cd` into it, then run `git add -A && git commit -m "<message>"` ' +
      'with an appropriate commit message describing the work. Do not make any other changes.';
 
    await this.repository.update(agentId, { status: 'running', pendingQuestions: null, result: null });
 
    const agentCwd = this.processManager.getAgentWorkdir(agent.worktreeId);
    const { command, args, env: providerEnv } = this.processManager.buildResumeCommand(provider, agent.sessionId, commitPrompt);
 
    const { processEnv } = await this.credentialHandler.prepareProcessEnv(providerEnv, provider, agent.accountId);
 
    const prevActive = this.activeAgents.get(agentId);
    prevActive?.cancelPoll?.();
    if (prevActive?.tailer) {
      await prevActive.tailer.stop();
    }
 
    // Determine session number for commit retry
    let commitSessionNumber = 1;
    if (this.logChunkRepository) {
      commitSessionNumber = (await this.logChunkRepository.getSessionCount(agentId)) + 1;
    }
 
    const { pid, outputFilePath, tailer } = this.processManager.spawnDetached(
      agentId, agent.name, command, args, agentCwd, processEnv, provider.name, commitPrompt,
      (event) => this.outputHandler.handleStreamEvent(agentId, event, this.activeAgents.get(agentId)),
      this.createLogChunkCallback(agentId, agent.name, commitSessionNumber),
    );
 
    await this.repository.update(agentId, { pid, outputFilePath });
    const commitActiveEntry: ActiveAgent = { agentId, pid, tailer, outputFilePath };
    this.activeAgents.set(agentId, commitActiveEntry);
 
    const { cancel: commitCancel } = this.processManager.pollForCompletion(
      agentId, pid,
      () => this.handleDetachedAgentCompletion(agentId),
      () => this.activeAgents.get(agentId)?.tailer,
    );
    commitActiveEntry.cancelPoll = commitCancel;
 
    return true;
  }
 
  /**
   * Sync credentials from agent's config dir back to DB after completion.
   * The subprocess may have refreshed tokens mid-session; this ensures
   * the DB stays current and the next spawn uses fresh tokens.
   */
  private async syncCredentialsPostCompletion(agentId: string): Promise<void> {
    Eif (!this.accountRepository) return;
 
    try {
      const agent = await this.repository.findById(agentId);
      if (!agent?.accountId) return;
 
      const { getAccountConfigDir } = await import('./accounts/paths.js');
      const configDir = getAccountConfigDir(this.workspaceRoot, agent.accountId);
      await this.credentialHandler.persistRefreshedCredentials(agent.accountId, configDir);
      log.debug({ agentId, accountId: agent.accountId }, 'post-completion credential sync done');
    } catch (err) {
      log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'post-completion credential sync failed');
    }
  }
 
  /**
   * Stop a running agent.
   */
  async stop(agentId: string): Promise<void> {
    const agent = await this.repository.findById(agentId);
    if (!agent) throw new Error(`Agent '${agentId}' not found`);
    log.info({ agentId, name: agent.name }, 'stopping agent');
 
    const active = this.activeAgents.get(agentId);
    if (active) {
      try { process.kill(active.pid, 'SIGTERM'); } catch { /* already exited */ }
      await active.tailer.stop();
    }
    this.cleanupAgentState(agentId);
 
    // Sync credentials before marking stopped
    await this.syncCredentialsPostCompletion(agentId);
 
    await this.repository.update(agentId, { status: 'stopped', pendingQuestions: null });
 
    Eif (this.eventBus) {
      const event: AgentStoppedEvent = {
        type: 'agent:stopped',
        timestamp: new Date(),
        payload: { agentId, name: agent.name, taskId: agent.taskId ?? '', reason: 'user_requested' },
      };
      this.eventBus.emit(event);
    }
  }
 
  /**
   * List all agents with their current status.
   */
  async list(): Promise<AgentInfo[]> {
    const agents = await this.repository.findAll();
    return agents.map((a) => this.toAgentInfo(a));
  }
 
  /**
   * Get a specific agent by ID.
   */
  async get(agentId: string): Promise<AgentInfo | null> {
    const agent = await this.repository.findById(agentId);
    return agent ? this.toAgentInfo(agent) : null;
  }
 
  /**
   * Get a specific agent by name.
   */
  async getByName(name: string): Promise<AgentInfo | null> {
    const agent = await this.repository.findByName(name);
    return agent ? this.toAgentInfo(agent) : null;
  }
 
  /**
   * Resume an agent using the unified lifecycle controller.
   * Features comprehensive retry, error handling, and cleanup.
   */
  async resumeWithLifecycle(agentId: string, answers: Record<string, string>): Promise<void> {
    log.info({
      agentId,
      answerKeys: Object.keys(answers)
    }, 'resuming agent with unified lifecycle management');
 
    await this.lifecycleController.resumeWithRetry(
      (id, modifiedAnswers) => this.resumeInternal(id, modifiedAnswers),
      { agentId, answers }
    );
  }
 
  /**
   * Resume an agent that's waiting for input (legacy method).
   * Consider using resumeWithLifecycle for better error handling.
   */
  async resume(agentId: string, answers: Record<string, string>): Promise<void> {
    return this.resumeInternal(agentId, answers);
  }
 
  /**
   * Internal resume implementation without lifecycle management.
   * Used by both legacy resume() and new lifecycle-managed resume.
   */
  private async resumeInternal(agentId: string, answers: Record<string, string>): Promise<void> {
    const agent = await this.repository.findById(agentId);
    Iif (!agent) throw new Error(`Agent '${agentId}' not found`);
    if (agent.status !== 'waiting_for_input') {
      throw new Error(`Agent '${agent.name}' is not waiting for input (status: ${agent.status})`);
    }
    if (!agent.sessionId) {
      throw new Error(`Agent '${agent.name}' has no session to resume`);
    }
    log.info({ agentId, sessionId: agent.sessionId, provider: agent.provider }, 'resuming agent');
 
    const provider = getProvider(agent.provider);
    Iif (!provider) throw new Error(`Unknown provider: '${agent.provider}'`);
    Iif (provider.resumeStyle === 'none') {
      throw new Error(`Provider '${provider.name}' does not support resume`);
    }
 
    const agentCwd = this.processManager.getAgentWorkdir(agent.worktreeId);
    const prompt = this.outputHandler.formatAnswersAsPrompt(answers);
 
    // Clear previous signal.json to ensure clean completion detection
    const signalPath = join(agentCwd, '.cw/output/signal.json');
    try {
      await unlink(signalPath);
      log.debug({ agentId, signalPath }, 'cleared previous signal.json for resume');
    } catch {
      // File might not exist, which is fine
    }
 
    await this.repository.update(agentId, { status: 'running', pendingQuestions: null, result: null });
 
    const { command, args, env: providerEnv } = this.processManager.buildResumeCommand(provider, agent.sessionId, prompt);
    log.debug({ command, args: args.join(' ') }, 'resume command built');
 
    // Prepare process environment with credentials
    const { processEnv } = await this.credentialHandler.prepareProcessEnv(providerEnv, provider, agent.accountId);
 
    // Stop previous tailer and cancel previous poll
    const prevActive = this.activeAgents.get(agentId);
    prevActive?.cancelPoll?.();
    Iif (prevActive?.tailer) {
      await prevActive.tailer.stop();
    }
 
    // Determine session number for this resume
    let resumeSessionNumber = 1;
    Iif (this.logChunkRepository) {
      resumeSessionNumber = (await this.logChunkRepository.getSessionCount(agentId)) + 1;
    }
 
    const { pid, outputFilePath, tailer } = this.processManager.spawnDetached(
      agentId, agent.name, command, args, agentCwd, processEnv, provider.name, prompt,
      (event) => this.outputHandler.handleStreamEvent(agentId, event, this.activeAgents.get(agentId)),
      this.createLogChunkCallback(agentId, agent.name, resumeSessionNumber),
    );
 
    await this.repository.update(agentId, { pid, outputFilePath });
 
    const resumeActiveEntry: ActiveAgent = { agentId, pid, tailer, outputFilePath };
    this.activeAgents.set(agentId, resumeActiveEntry);
    log.info({ agentId, pid }, 'resume detached subprocess started');
 
    Eif (this.eventBus) {
      const event: AgentResumedEvent = {
        type: 'agent:resumed',
        timestamp: new Date(),
        payload: { agentId, name: agent.name, taskId: agent.taskId ?? '', sessionId: agent.sessionId },
      };
      this.eventBus.emit(event);
    }
 
    const { cancel: resumeCancel } = this.processManager.pollForCompletion(
      agentId, pid,
      () => this.handleDetachedAgentCompletion(agentId),
      () => this.activeAgents.get(agentId)?.tailer,
    );
    resumeActiveEntry.cancelPoll = resumeCancel;
  }
 
  /**
   * Get the result of an agent's work.
   */
  async getResult(agentId: string): Promise<AgentResult | null> {
    return this.outputHandler.getResult(agentId, this.activeAgents.get(agentId));
  }
 
  /**
   * Get pending questions for an agent waiting for input.
   */
  async getPendingQuestions(agentId: string): Promise<PendingQuestions | null> {
    return this.outputHandler.getPendingQuestions(agentId, this.activeAgents.get(agentId));
  }
 
  /**
   * Delete an agent and clean up all associated resources.
   */
  async delete(agentId: string): Promise<void> {
    const agent = await this.repository.findById(agentId);
    if (!agent) throw new Error(`Agent '${agentId}' not found`);
    log.info({ agentId, name: agent.name }, 'deleting agent');
 
    // 1. Kill process, stop tailer, clear all in-memory state
    const active = this.activeAgents.get(agentId);
    if (active) {
      try { process.kill(active.pid, 'SIGTERM'); } catch { /* already exited */ }
      await active.tailer.stop();
    }
    this.cleanupAgentState(agentId);
 
    // 2. Best-effort cleanup
    try { await this.cleanupManager.removeAgentWorktrees(agent.name, agent.initiativeId); }
    catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to remove worktrees'); }
 
    try { await this.cleanupManager.removeAgentBranches(agent.name, agent.initiativeId); }
    catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to remove branches'); }
 
    try { await this.cleanupManager.removeAgentLogs(agent.name); }
    catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to remove logs'); }
 
    // 3b. Delete log chunks from DB
    Iif (this.logChunkRepository) {
      try { await this.logChunkRepository.deleteByAgentId(agentId); }
      catch (err) { log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to delete log chunks'); }
    }
 
    // 4. Delete DB record
    await this.repository.delete(agentId);
 
    // 5. Emit deleted event
    Eif (this.eventBus) {
      const event: AgentDeletedEvent = {
        type: 'agent:deleted',
        timestamp: new Date(),
        payload: { agentId, name: agent.name },
      };
      this.eventBus.emit(event);
    }
    log.info({ agentId, name: agent.name }, 'agent deleted');
  }
 
  /**
   * Dismiss an agent.
   */
  async dismiss(agentId: string): Promise<void> {
    const agent = await this.repository.findById(agentId);
    if (!agent) throw new Error(`Agent '${agentId}' not found`);
    log.info({ agentId, name: agent.name }, 'dismissing agent');
 
    this.cleanupAgentState(agentId);
 
    await this.repository.update(agentId, {
      userDismissedAt: new Date(),
      updatedAt: new Date(),
    });
 
    log.info({ agentId, name: agent.name }, 'agent dismissed');
  }
 
  /**
   * Clean up orphaned agent workdirs.
   */
  async cleanupOrphanedWorkdirs(): Promise<void> {
    return this.cleanupManager.cleanupOrphanedWorkdirs();
  }
 
  /**
   * Clean up orphaned agent log directories.
   */
  async cleanupOrphanedLogs(): Promise<void> {
    return this.cleanupManager.cleanupOrphanedLogs();
  }
 
  /**
   * Reconcile agent state after server restart.
   */
  async reconcileAfterRestart(): Promise<void> {
    const reconcileLogChunkRepo = this.logChunkRepository;
    await this.cleanupManager.reconcileAfterRestart(
      this.activeAgents,
      (agentId, event) => this.outputHandler.handleStreamEvent(agentId, event, this.activeAgents.get(agentId)),
      (agentId, rawOutput, provider) => this.outputHandler.processAgentOutput(agentId, rawOutput, provider, (alias) => this.processManager.getAgentWorkdir(alias)),
      (agentId, pid) => {
        const { cancel } = this.processManager.pollForCompletion(
          agentId, pid,
          () => this.handleDetachedAgentCompletion(agentId),
          () => this.activeAgents.get(agentId)?.tailer,
        );
        const active = this.activeAgents.get(agentId);
        if (active) active.cancelPoll = cancel;
      },
      reconcileLogChunkRepo
        ? (agentId, agentName, content) => {
            // Determine session number asynchronously — use fire-and-forget
            reconcileLogChunkRepo.getSessionCount(agentId).then(count => {
              return reconcileLogChunkRepo.insertChunk({
                agentId,
                agentName,
                sessionNumber: count + 1,
                content,
              });
            }).catch(err => log.warn({ agentId, err: err instanceof Error ? err.message : String(err) }, 'failed to persist log chunk during reconciliation'));
          }
        : undefined,
    );
  }
 
  /**
   * Handle process crashed event specifically for agents.
   * Check if the agent actually completed successfully despite the non-zero exit code.
   */
  private async handleProcessCrashed(processId: string, exitCode: number | null, signal: string | null): Promise<void> {
    try {
      // Check if this is an agent process
      const agent = await this.repository.findById(processId);
      if (!agent) {
        return; // Not our agent
      }
 
      // Store exit code and signal for debugging
      await this.repository.update(processId, { exitCode });
 
      log.info({
        agentId: processId,
        name: agent.name,
        exitCode,
        signal,
        outputFilePath: agent.outputFilePath
      }, 'agent process crashed, analyzing completion status');
 
      // Check if the agent has output that indicates successful completion
      if (agent.outputFilePath) {
        const hasCompletion = await this.checkAgentCompletionResult(agent.worktreeId);
        if (hasCompletion) {
          log.info({
            agentId: processId,
            name: agent.name,
            exitCode,
            signal
          }, 'agent marked as crashed but completed successfully - completion already handled by polling');
 
          // Note: We don't call handleCompletion() here because the polling handler
          // (handleDetachedAgentCompletion) already processes completions. The mutex
          // in OutputHandler.handleCompletion() prevents duplicate processing.
 
          log.info({
            agentId: processId,
            name: agent.name,
            exitCode
          }, 'completion detection confirmed - deferring to polling handler');
        } else {
          log.warn({
            agentId: processId,
            name: agent.name,
            exitCode,
            signal,
            outputFilePath: agent.outputFilePath
          }, 'agent crashed and no successful completion detected - marking as truly crashed');
 
          // Only mark as crashed if agent truly crashed (no completion detected)
          await this.repository.update(processId, { status: 'crashed' });
        }
      } else {
        log.warn({
          agentId: processId,
          name: agent.name,
          exitCode,
          signal
        }, 'agent crashed with no output file path - marking as crashed');
 
        await this.repository.update(processId, { status: 'crashed' });
      }
    } catch (err) {
      log.error({
        processId,
        exitCode,
        signal,
        err: err instanceof Error ? err.message : String(err)
      }, 'failed to check agent completion after crash');
    }
  }
 
  /**
   * Check if agent completed successfully by reading signal.json file.
   * Probes the workspace/ subdirectory for standalone agents.
   */
  private async checkAgentCompletionResult(worktreeId: string): Promise<boolean> {
    try {
      // Resolve actual agent workdir — standalone agents have .cw inside workspace/ subdir
      let agentWorkdir = this.processManager.getAgentWorkdir(worktreeId);
      const workspaceSub = join(agentWorkdir, 'workspace');
      if (!existsSync(join(agentWorkdir, '.cw', 'output')) && existsSync(join(workspaceSub, '.cw'))) {
        agentWorkdir = workspaceSub;
      }
 
      const signalPath = join(agentWorkdir, '.cw/output/signal.json');
 
      if (!existsSync(signalPath)) {
        log.debug({ worktreeId, signalPath }, 'no signal.json found - agent not completed');
        return false;
      }
 
      const signalContent = await readFile(signalPath, 'utf-8');
      const signal = JSON.parse(signalContent);
 
      // Agent completed if status is done, questions, or error
      const completed = signal.status === 'done' || signal.status === 'questions' || signal.status === 'error';
 
      if (completed) {
        log.debug({ worktreeId, signal }, 'agent completion detected via signal.json');
      } else {
        log.debug({ worktreeId, signal }, 'signal.json found but status indicates incomplete');
      }
 
      return completed;
 
    } catch (err) {
      log.warn({ worktreeId, err: err instanceof Error ? err.message : String(err) }, 'failed to read or parse signal.json');
      return false;
    }
  }
 
  /**
   * Convert database agent record to AgentInfo.
   */
  private toAgentInfo(agent: {
    id: string;
    name: string;
    taskId: string | null;
    initiativeId: string | null;
    sessionId: string | null;
    worktreeId: string;
    status: string;
    mode: string;
    provider: string;
    accountId: string | null;
    createdAt: Date;
    updatedAt: Date;
    userDismissedAt?: Date | null;
  }): AgentInfo {
    return {
      id: agent.id,
      name: agent.name,
      taskId: agent.taskId ?? '',
      initiativeId: agent.initiativeId,
      sessionId: agent.sessionId,
      worktreeId: agent.worktreeId,
      status: agent.status as AgentStatus,
      mode: agent.mode as AgentMode,
      provider: agent.provider,
      accountId: agent.accountId,
      createdAt: agent.createdAt,
      updatedAt: agent.updatedAt,
      userDismissedAt: agent.userDismissedAt,
    };
  }
}