{"version":3,"file":"cliApp.mjs","names":[],"sources":["../../src/cli/cliApp.ts"],"sourcesContent":["import type { HarnessTelemetry } from '@agimon-ai/doompi-core/runtime-log-sink-telemetry';\n\nimport type { HarnessOptions } from '../composition/types/harness';\nimport type { BaseCommand } from './commands/baseCommand';\nimport { HARNESS_VERSION, printHelp } from './help';\nimport { parseHarnessArgs } from './options';\n\n/**\n * Main CLI Application\n *\n * Owns command registration and dispatch. Preparation is shared: the context\n * is built once and handed to whichever command claims the run.\n */\nasync function selectHarnessCommand(\n  options: HarnessOptions,\n): Promise<\n  (context: import('../builders/cli/harnessContext').HarnessContext, telemetry: HarnessTelemetry) => Promise<number>\n> {\n  if (options.emitMcp) return (await import('./commands/emit-mcp')).runEmitMcp;\n  if (options.explain) return (await import('./commands/explain')).runExplain;\n  if (options.sandbox) return (await import('./commands/sandbox')).runSandbox;\n  return (await import('./commands/launch')).runLaunch;\n}\nasync function selectLegacyCommand(options: HarnessOptions): Promise<BaseCommand> {\n  if (options.emitMcp) {\n    const { EmitMcpCommand } = await import('./commands/emit-mcp');\n    return new EmitMcpCommand();\n  }\n  if (options.explain) {\n    const { ExplainCommand } = await import('./commands/explain');\n    return new ExplainCommand();\n  }\n  if (options.sandbox) {\n    const { SandboxLaunchCommand } = await import('./commands/sandbox');\n    return new SandboxLaunchCommand();\n  }\n  const { LaunchCommand } = await import('./commands/launch');\n  return new LaunchCommand();\n}\nfunction createCliApplication(initialTelemetry?: HarnessTelemetry) {\n  let telemetry = initialTelemetry;\n  let ownsTelemetry = false;\n  async function getTelemetry(): Promise<HarnessTelemetry> {\n    if (telemetry) return telemetry;\n    const { createHarnessTelemetry } = await import('@agimon-ai/doompi-core/runtime-log-sink-telemetry');\n    // Telemetry's transport graph costs roughly one launcher budget slice by\n    // itself. Startup callbacks and lifecycle events buffer until shutdown, so\n    // transport initialization cannot compete with the Pi child for input.\n    telemetry = createHarnessTelemetry({ deferSpans: true });\n    ownsTelemetry = true;\n    return telemetry;\n  }\n\n  async function runHarness(options: HarnessOptions): Promise<number> {\n    const telemetry = await getTelemetry();\n    const [{ buildHarnessContext }, { ensureLayerPackages }, { HARNESS_EVENT }] = await Promise.all([\n      import('../builders/cli/harnessContext'),\n      import('../composition/layerPackageInstaller'),\n      import('@agimon-ai/doompi-core/runtime-log-sink-telemetry'),\n    ]);\n    return telemetry.runInSpan(\n      'doom_pi.run',\n      {\n        'harness.major_mode': options.majorMode,\n        'harness.domain_count': options.domains.length,\n        'harness.agents': options.agents,\n        'harness.mcp': options.mcp,\n        'harness.hooks': options.hooks,\n        ...(options.profile ? { 'harness.profile': options.profile } : {}),\n        ...(options.preset ? { 'harness.preset': options.preset } : {}),\n      },\n      async () => {\n        const context = await buildHarnessContext(options, telemetry);\n        try {\n          await ensureLayerPackages({\n            repoRoot: options.repoRoot,\n            config: context.majorModesConfig,\n            layers: context.selectedLayers,\n            environment: context.environment,\n          });\n          const command = await selectHarnessCommand(context.options);\n          // Selection uses the same resolved options used to build the context.\n          return await command(context, telemetry);\n        } catch (error) {\n          await telemetry.recordError(HARNESS_EVENT.cliFailed, error);\n          throw error;\n        } finally {\n          await context.cleanup();\n        }\n      },\n    );\n  }\n\n  async function run(args: string[]): Promise<number> {\n    if (args[0] === 'init') {\n      const { runInit } = await import('./commands/init');\n      return runInit(args);\n    }\n    // Sync owns the private cache build and then commits the resolved matrix for\n    // plain Pi. Keeping both phases behind one command avoids stale partial setup.\n    if (args[0] === 'sync') {\n      const [{ runSync }, telemetry] = await Promise.all([import('./commands/sync/workflow'), getTelemetry()]);\n      return runSync(args, undefined, undefined, undefined, { telemetry });\n    }\n    if (args[0] === 'api-export') {\n      const { runApiExport } = await import('./commands/api-export');\n      return runApiExport(args);\n    }\n    if (args[0] === 'doctor') {\n      const { runDoctor } = await import('./commands/doctor');\n      return runDoctor(args);\n    }\n    if (args[0] === 'compat') {\n      const [{ runCompatibility }, telemetry] = await Promise.all([import('./commands/compat'), getTelemetry()]);\n      return runCompatibility(args, undefined, undefined, telemetry);\n    }\n    if (args[0] === 'history-export') {\n      const { runHistoryExport } = await import('./commands/history-export');\n      return runHistoryExport(args);\n    }\n    if (args[0] === 'history-import') {\n      const { runHistoryImport } = await import('./commands/history-import');\n      return runHistoryImport(args);\n    }\n\n    // The first pass resolves --cwd without touching repository configuration,\n    // which keeps help and version available even when modes.yaml is malformed.\n    const initial = parseHarnessArgs(args);\n    if (initial.help) {\n      printHelp();\n      return 0;\n    }\n    if (initial.version) {\n      process.stdout.write(`${HARNESS_VERSION}\\n`);\n      return 0;\n    }\n    const telemetry = await getTelemetry();\n    const [{ resolveHarnessOptions }, { toFailureReporter }] = await Promise.all([\n      import('./harnessOptions'),\n      import('@agimon-ai/doompi-core/runtime-log-sink-telemetry'),\n    ]);\n    return runHarness(resolveHarnessOptions({ args, report: toFailureReporter(telemetry) }));\n  }\n\n  async function shutdown(): Promise<void> {\n    if (ownsTelemetry) await telemetry?.shutdown();\n  }\n  return { run, runHarness, shutdown };\n}\n/** Public compatibility API; execution is implemented by the application functions. */\nexport class CliApp {\n  private readonly app: ReturnType<typeof createCliApplication>;\n  constructor(telemetry?: HarnessTelemetry) {\n    this.app = createCliApplication(telemetry);\n  }\n  selectCommand(options: HarnessOptions): Promise<BaseCommand> {\n    return selectLegacyCommand(options);\n  }\n  runHarness(options: HarnessOptions): Promise<number> {\n    return this.app.runHarness(options);\n  }\n  run(args: string[]): Promise<number> {\n    return this.app.run(args);\n  }\n  shutdown(): Promise<void> {\n    return this.app.shutdown();\n  }\n}\n\n/**\n * Convenience wrapper kept so the binary and tests need no CliApp knowledge.\n *\n * Telemetry is shut down here rather than inside runHarness, because this is\n * the only scope that owns the process: flushing earlier would drop the records\n * a later command still emits.\n */\nexport async function runCli(args: string[]): Promise<number> {\n  const app = createCliApplication();\n  try {\n    return await app.run(args);\n  } finally {\n    await app.shutdown();\n  }\n}\n\n/** Runs an already-resolved option set, skipping argument parsing. */\nexport async function runHarness(options: HarnessOptions): Promise<number> {\n  const app = createCliApplication();\n  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