{"version":3,"file":"harness.cjs","names":["isLocalPackageSpecifier","localPackageExport","SANDBOX_HARNESS_EXPORT_SUBPATH","splitPackageSpecifier","consumerPackageEntry","pathToFileURL","isSandboxHarnessModule"],"sources":["../../../../src/cli/commands/sandbox/harness.ts"],"sourcesContent":["import { pathToFileURL } from 'node:url';\n\nimport { isLocalPackageSpecifier, type MajorModesConfig } from '@agimon-ai/doompi-config/majorModes';\nimport {\n  consumerPackageEntry,\n  localPackageExport,\n  splitPackageSpecifier,\n} from '@agimon-ai/doompi-core/module-resolution';\nimport {\n  isSandboxHarnessModule,\n  SANDBOX_HARNESS_EXPORT_SUBPATH,\n  type SandboxHarnessModule,\n} from '@agimon-ai/doompi-core/sandbox-harness';\n\nexport interface SandboxHarnessResolution {\n  /** Configured package specifier that answered for the sandbox subpath. */\n  specifier: string;\n  /** Absolute path of the module the harness imports. */\n  entry: string;\n}\n\ninterface ConfiguredPackage {\n  specifier: string;\n  /** Root the specifier resolves against when it is a local path. */\n  baseDirectory: string;\n}\n\nfunction configuredPackages(config: MajorModesConfig, layers: readonly string[]): ConfiguredPackage[] {\n  const definitions = [\n    ...(config.default ? [config.default] : []),\n    ...layers.map((layerName) => {\n      const layer = config.layers[layerName];\n      if (!layer) throw new Error(`Unknown layer: ${layerName}`);\n      return layer;\n    }),\n  ];\n  const packages: ConfiguredPackage[] = [];\n  for (const definition of definitions) {\n    for (const configured of definition.packages ?? []) {\n      const specifier = typeof configured === 'string' ? configured : configured.name;\n      const candidate = { specifier, baseDirectory: definition.baseDirectory };\n      if (!packages.some((entry) => entry.specifier === specifier && entry.baseDirectory === candidate.baseDirectory)) {\n        packages.push(candidate);\n      }\n    }\n  }\n  return packages;\n}\n\nfunction sandboxEntryFor(configured: ConfiguredPackage, repoRoot: string): string | undefined {\n  if (isLocalPackageSpecifier(configured.specifier)) {\n    return localPackageExport(configured.specifier, configured.baseDirectory, SANDBOX_HARNESS_EXPORT_SUBPATH);\n  }\n  const { name } = splitPackageSpecifier(configured.specifier);\n  return consumerPackageEntry(`${name}${SANDBOX_HARNESS_EXPORT_SUBPATH.slice(1)}`, repoRoot);\n}\n\n/**\n * Finds the single sandbox harness the selected composition provides.\n *\n * Exactly one package may answer: two sandbox providers cannot both own the\n * launch, and which one wins would otherwise depend on declaration order.\n */\nexport function resolveSandboxHarnessEntry(\n  config: MajorModesConfig,\n  layers: readonly string[],\n  repoRoot: string,\n): SandboxHarnessResolution | undefined {\n  const resolutions: SandboxHarnessResolution[] = [];\n  for (const configured of configuredPackages(config, layers)) {\n    const entry = sandboxEntryFor(configured, repoRoot);\n    if (entry && !resolutions.some((resolution) => resolution.entry === entry)) {\n      resolutions.push({ specifier: configured.specifier, entry });\n    }\n  }\n  if (resolutions.length > 1) {\n    const names = resolutions.map((resolution) => resolution.specifier).join(', ');\n    throw new Error(\n      `Multiple sandbox harnesses in the selected composition: ${names}. Keep exactly one sandbox layer in the major mode.`,\n    );\n  }\n  return resolutions[0];\n}\n\n/** Imports and validates a sandbox harness entry file. */\nexport async function loadSandboxHarness(entry: string): Promise<SandboxHarnessModule> {\n  const imported: unknown = await import(pathToFileURL(entry).href);\n  if (isSandboxHarnessModule(imported)) return imported;\n  const fallback = (imported as { default?: unknown } | null)?.default;\n  if (isSandboxHarnessModule(fallback)) return fallback;\n  throw new Error(`Sandbox harness at ${entry} does not export launchSandbox().`);\n}\n"],"mappings":";;;;;AA2BA,SAAS,mBAAmB,QAA0B,QAAgD;CACpG,MAAM,cAAc,CAClB,GAAI,OAAO,UAAU,CAAC,OAAO,OAAO,IAAI,CAAC,GACzC,GAAG,OAAO,KAAK,cAAc;EAC3B,MAAM,QAAQ,OAAO,OAAO;EAC5B,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,kBAAkB,WAAW;EACzD,OAAO;CACT,CAAC,CACH;CACA,MAAM,WAAgC,CAAC;CACvC,KAAK,MAAM,cAAc,aACvB,KAAK,MAAM,cAAc,WAAW,YAAY,CAAC,GAAG;EAClD,MAAM,YAAY,OAAO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