{"version":3,"file":"dpi.cjs","names":["path","loadPiSettingsRuntime","installDpiSettingsOverlay","initializeRepositoryDoomConfig"],"sources":["../../src/cli/dpi.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { initializeRepositoryDoomConfig, type RepositoryDoomInitResult } from '@agimon-ai/doompi-config';\n\nimport { installDpiSettingsOverlay, loadPiSettingsRuntime, type PiSettingsRuntime } from '../builders/cli/dpiSettings';\n\nconst INIT_COMMAND = 'init';\nconst SYNC_COMMAND = 'sync';\nconst SANDBOX_OPTION = '--sandbox';\nconst FORCE_FLAG = '--force';\nconst PI_SUBAGENT_BINARY_ENV = 'PI_SUBAGENT_PI_BINARY';\nconst DPI_ENTRY_PATTERN = /^dpi(?:\\.[cm]?js|\\.ts)?$/u;\n\ntype PiModule = typeof import('@earendil-works/pi-coding-agent');\n\nexport interface DpiRunnerDependencies {\n  init(args: string[]): number | Promise<number>;\n  launchPi(args: string[]): Promise<number>;\n  sync(args: string[]): Promise<number>;\n  launchSandbox(args: string[]): Promise<number>;\n}\n\nasync function loadPiModule(settingsRuntime: PiSettingsRuntime): Promise<PiModule> {\n  const loaded = await import('@earendil-works/pi-coding-agent');\n  if (loaded.SettingsManager !== settingsRuntime.SettingsManager) {\n    throw new Error('DPI resolved more than one Pi settings runtime');\n  }\n  return loaded;\n}\n\n/** Marks the process as Pi and keeps any CLI-based nested fallback on DPI. */\nexport function configureDpiEnvironment(\n  environment: NodeJS.ProcessEnv = process.env,\n  entryPoint: string | undefined = process.argv[1],\n): void {\n  environment.PI_CODING_AGENT = 'true';\n  environment.AI_AGENT = 'pi';\n  if (!environment[PI_SUBAGENT_BINARY_ENV]?.trim() && entryPoint && DPI_ENTRY_PATTERN.test(path.basename(entryPoint))) {\n    environment[PI_SUBAGENT_BINARY_ENV] = path.resolve(entryPoint);\n  }\n}\n\n/** Runs the pinned upstream Pi entry point after installing DPI's settings view. */\nexport async function launchDpiPi(args: string[]): Promise<number> {\n  const settingsRuntime = await loadPiSettingsRuntime();\n  const pi = await loadPiModule(settingsRuntime);\n  const restoreSettings = installDpiSettingsOverlay(settingsRuntime);\n  process.title = 'dpi';\n  configureDpiEnvironment();\n  process.emitWarning = (() => undefined) as typeof process.emitWarning;\n\n  try {\n    await pi.main(args);\n    const exitCode = process.exitCode;\n    return typeof exitCode === 'number' ? exitCode : Number.parseInt(exitCode ?? '0', 10);\n  } finally {\n    restoreSettings();\n  }\n}\n\nfunction formatDpiInitResult(result: RepositoryDoomInitResult): string {\n  return [\n    `DoomPi repository configuration: ${result.directory}`,\n    ...(result.created.length > 0 ? [`created:  ${result.created.join(', ')}`] : []),\n    ...(result.preserved.length > 0 ? [`kept:     ${result.preserved.join(', ')}`] : []),\n    ...(result.replaced.length > 0 ? [`replaced: ${result.replaced.join(', ')}`] : []),\n    '',\n    'Run `dpi sync` next.',\n    '',\n  ].join('\\n');\n}\n\n/** Seeds repository-local DoomPi configuration without registering anything with Pi. */\nexport function runDpiInit(\n  args: string[],\n  currentDirectory = process.cwd(),\n  output: Pick<NodeJS.WritableStream, 'write'> = process.stdout,\n): number {\n  const flags = args.slice(1);\n  const unknown = flags.find((flag) => flag !== FORCE_FLAG);\n  if (unknown !== undefined) throw new Error(`dpi init does not accept ${unknown}`);\n\n  const result = initializeRepositoryDoomConfig(currentDirectory, { force: flags.includes(FORCE_FLAG) });\n  output.write(formatDpiInitResult(result));\n  return 0;\n}\n\n/** Runs DPI's private build → sync pipeline without persisting the settings overlay. */\nexport async function runDpiSync(args: string[]): Promise<number> {\n  const { runSync } = await import('./commands/sync/workflow');\n  return runSync(args, undefined, undefined, undefined, { settingsMode: 'embedded' });\n}\n\n/**\n * Hands a sandboxed run to the harness instead of this fast path.\n *\n * The fast path exists to load Pi in-process against already synchronized\n * settings. A sandboxed run is a container launch, and a fresh container has\n * no synchronized state to load, so the harness composes it from the\n * repository the way a first run does.\n */\nexport async function runDpiSandbox(args: string[]): Promise<number> {\n  const { runCli } = await import('./cliApp');\n  return runCli(args);\n}\n\nconst DEFAULT_DEPENDENCIES: DpiRunnerDependencies = {\n  init: runDpiInit,\n  launchPi: launchDpiPi,\n  sync: runDpiSync,\n  launchSandbox: runDpiSandbox,\n};\n\n/** Dispatches DPI-owned setup commands and forwards every other argument to Pi. */\nexport async function runDpi(\n  args: string[],\n  dependencies: DpiRunnerDependencies = DEFAULT_DEPENDENCIES,\n): Promise<number> {\n  if (args[0] === INIT_COMMAND) return dependencies.init(args);\n  if (args[0] === SYNC_COMMAND) return dependencies.sync(args);\n  // Pi owns every other flag, so the sandbox option has to be claimed here or\n  // it reaches Pi as an unknown one.\n  if (args.includes(SANDBOX_OPTION)) return dependencies.launchSandbox(args);\n  return dependencies.launchPi(args);\n}\n"],"mappings":";;;;;;AAMA,MAAM,eAAe;AACrB,MAAM,eAAe;AACrB,MAAM,iBAAiB;AACvB,MAAM,aAAa;AACnB,MAAM,yBAAyB;AAC/B,MAAM,oBAAoB;AAW1B,eAAe,aAAa,iBAAuD;CACjF,MAAM,SAAS,MAAM,OAAO;CAC5B,IAAI,OAAO,oBAAoB,gBAAgB,iBAC7C,MAAM,IAAI,MAAM,gDAAgD;CAElE,OAAO;AACT;;AAGA,SAAgB,wBACd,cAAiC,QAAQ,KACzC,aAAiC,QAAQ,KAAK,IACxC;CACN,YAAY,kBAAkB;CAC9B,YAAY,WAAW;CACvB,IAAI,CAAC,YAAY,uBAAuB,EAAE,KAAK,KAAK,cAAc,kBAAkB,KAAKA,UAAAA,QAAK,SAAS,UAAU,CAAC,GAChH,YAAY,0BAA0BA,UAAAA,QAAK,QAAQ,UAAU;AAEjE;;AAGA,eAAsB,YAAY,MAAiC;CACjE,MAAM,kBAAkB,MAAMC,oBAAAA,sBAAsB;CACpD,MAAM,KAAK,MAAM,aAAa,eAAe;CAC7C,MAAM,kBAAkBC,oBAAAA,0BAA0B,eAAe;CACjE,QAAQ,QAAQ;CAChB,wBAAwB;CACxB,QAAQ,qBAAqB,KAAA;CAE7B,IAAI;EACF,MAAM,GAAG,KAAK,IAAI;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