{"version":3,"file":"upgrade-source.cjs","names":["resolveRelease","readAuthoredConfigInput","getExtendsRef","walkExtendsChain","parseBosRef","fetchParentConfig"],"sources":["../../../src/services/scaffold/upgrade-source.ts"],"sourcesContent":["import { readAuthoredConfigInput } from \"../../config\";\nimport { getExtendsRef, parseBosRef } from \"../../utils/bos-ref\";\nimport type { ResolvedPlatformManifest } from \"../platform/manifest\";\nimport {\n  type ResolutionOutcome,\n  type ResolutionSeams,\n  resolveRelease,\n  walkExtendsChain,\n} from \"../registry/resolution\";\nimport { fetchParentConfig } from \"./init\";\n\nexport interface ExtendedRootSource {\n  catalog: Record<string, string>;\n  repository?: string;\n  extendsChain: string[];\n  /** The resolved platform release manifest when one publishes (ADR 0034). */\n  platform: ResolvedPlatformManifest;\n  /** The authored `release` as read from disk — the durable record of\n   * the release the tree is aligned to. The manifest above is resolved\n   * *ignoring* this field, so `authoredRelease !== platform.manifest.frameworkVersion`\n   * is the pending-delta fact the migration phase is derived from; it needs\n   * no flag carried across the re-exec boundary. */\n  authoredRelease?: string;\n}\n\n/** Injectable registry I/O — partial overrides over the resolution machine's\n * production seams (the test seam). Cache reads are never overridable: the\n * upgrade-of-record refuses cache stand-ins when choosing a release. */\nexport type ExtendedRootSourceOptions = {\n  fetchParent?: (account: string, gateway: string) => Promise<Record<string, unknown> | null>;\n  seams?: Omit<Partial<ResolutionSeams>, \"cacheRead\">;\n};\n\nconst neverCached = { state: \"miss\" as const };\n\nfunction unresolvableError(outcome: Extract<ResolutionOutcome, { state: \"Unresolved\" }>): Error {\n  const chain = outcome.context.chain.join(\" → \");\n  if (outcome.reason === \"unreachable\") {\n    return new Error(\n      `The registry is unreachable — cannot choose a release to stamp (${outcome.detail ?? \"read failed\"})` +\n        `${chain ? ` — chain: ${chain}` : \"\"}. Retry when the registry is reachable.`,\n    );\n  }\n  if (outcome.reason === \"integrityFailure\") {\n    return new Error(\n      `Refusing to choose a release: ${outcome.detail ?? \"integrity check failed\"}${chain ? ` — chain: ${chain}` : \"\"}`,\n    );\n  }\n  if (outcome.reason === \"absent\" && outcome.context.tagServes === \"thin\") {\n    // the tag home serves a thin tag naming a release whose record never\n    // published — the #568 race, not an unpublished chain\n    return new Error(\n      `The manifest tag names a release whose version record is not published (${outcome.detail ?? \"record absent\"})` +\n        `${chain ? ` — chain: ${chain}` : \"\"} — the release deploy is incomplete or still running; ` +\n        `retry once the release is fully published`,\n    );\n  }\n  return new Error(\n    `No platform release manifest resolves in the extends chain (${chain || \"empty\"}) — publish the platform manifest (bos publish on the parent) or run \\`bos platform manifest --check\\` to diagnose` +\n      (outcome.context.deadEnd\n        ? `; the chain terminates at ${outcome.context.deadEnd.ref}${\n            outcome.context.deadEnd.lastWrite\n              ? ` (last write ${outcome.context.deadEnd.lastWrite})`\n              : \" which publishes no manifest legs\"\n          }`\n        : \"\"),\n  );\n}\n\n/**\n * The upgrade-of-record's release resolution: `Latest` over the extends\n * chain, read fresh — the offline cache may never choose the release this\n * upgrade stamps (a stale stand-in would pin children to bytes nobody\n * serves). The chosen release's immutable version record is verified before\n * returning, so the train fails loud BEFORE any mutation when a release\n * deployed with a missing manifest leg (the #568 class) instead of stranding\n * the tree mid-upgrade. Version records are immutable, so one verification\n * covers the whole run.\n */\nexport async function resolveLatestReleaseForUpgrade(input: {\n  localConfig: Record<string, unknown>;\n  localRepository?: string;\n  configDir: string;\n  options?: ExtendedRootSourceOptions;\n}): Promise<ResolvedPlatformManifest> {\n  const seams: Partial<ResolutionSeams> = {\n    ...(input.options?.seams ?? {}),\n    cacheRead: () => neverCached,\n  };\n  const common = {\n    localConfig: input.localConfig,\n    ...(input.localRepository !== undefined ? { localRepository: input.localRepository } : {}),\n    configDir: input.configDir,\n    seams,\n    ...(input.options?.fetchParent ? { fetchParent: input.options.fetchParent } : {}),\n  };\n\n  const latest = await resolveRelease({ ...common, query: { kind: \"Latest\" } });\n  if (latest.state === \"Unresolved\") throw unresolvableError(latest);\n\n  // post-cutover the thin tag already carries (and verifies) the digest of\n  // the record it names; the explicit verification is what guards the\n  // pre-cutover full-manifest tag\n  const release = latest.manifest.frameworkVersion;\n  const record = await resolveRelease({ ...common, query: { kind: \"Pinned\", release } });\n  if (record.state === \"Unresolved\") {\n    const chain = record.context.chain.join(\" → \");\n    if (record.reason === \"unreachable\") {\n      throw new Error(\n        `The registry is unreachable — cannot verify release ${release}'s version record (${record.detail ?? \"read failed\"})` +\n          `${chain ? ` — chain: ${chain}` : \"\"}. Retry when the registry is reachable.`,\n      );\n    }\n    throw new Error(\n      `Release ${release}'s version record is not published in the extends chain` +\n        `${chain ? ` (${chain})` : \"\"} — the release deploy is incomplete or still running; ` +\n        `retry once the release is fully published`,\n    );\n  }\n\n  return {\n    manifest: latest.manifest,\n    source: latest.source,\n    fromCache: false,\n    provenance: latest.provenance,\n  };\n}\n\nexport async function readExtendedRootSource(\n  projectDir: string,\n  options: ExtendedRootSourceOptions = {},\n): Promise<ExtendedRootSource> {\n  const localConfig = (await readAuthoredConfigInput(projectDir)) as Record<string, unknown> | null;\n  if (!localConfig) {\n    throw new Error(\"No authored config found in current directory\");\n  }\n\n  const extendsRef = getExtendsRef(localConfig);\n  if (!extendsRef?.startsWith(\"bos://\")) {\n    throw new Error(\n      \"No bos:// extends field found in the authored config — upgrade moves children that extend a deployed runtime\",\n    );\n  }\n\n  const walk = await walkExtendsChain({\n    localConfig,\n    localRepository:\n      typeof localConfig.repository === \"string\" ? localConfig.repository : undefined,\n    ...(options.fetchParent ? { fetchParent: options.fetchParent } : {}),\n  });\n  const extendsChain = walk.chain.map((node) => `bos://${node.account}/${node.gateway}`);\n  if (walk.circularAt) {\n    throw new Error(`Circular extends detected while resolving upgrade source: ${walk.circularAt}`);\n  }\n\n  // ADR 0034: the platform release manifest is the single upgrade authority —\n  // its catalog and repository metadata. The upgrade-of-record resolves it\n  // *ignoring the child's own authored release*: the release freezes everyday\n  // resolution (sync, init, runtime); this is the pathway that moves it to\n  // the latest chain release. No manifest anywhere → fail loud.\n  const platform = await resolveLatestReleaseForUpgrade({\n    localConfig,\n    localRepository:\n      typeof localConfig.repository === \"string\" ? localConfig.repository : undefined,\n    configDir: projectDir,\n    options,\n  });\n\n  return {\n    catalog: platform.manifest.catalog,\n    repository: walk.repository,\n    extendsChain,\n    platform,\n    ...(typeof localConfig.release === \"string\" ? { authoredRelease: localConfig.release } : {}),\n  };\n}\n\nexport async function loadParentPluginOptions(projectDir: string): Promise<{\n  localConfig: Record<string, unknown>;\n  parentPlugins: Record<string, unknown>;\n  newPluginKeys: string[];\n} | null> {\n  const localConfig = (await readAuthoredConfigInput(projectDir)) as Record<string, unknown> | null;\n  if (!localConfig) {\n    return null;\n  }\n  const extendsRef = getExtendsRef(localConfig);\n  if (!extendsRef?.startsWith(\"bos://\")) {\n    return null;\n  }\n\n  const parsed = parseBosRef(extendsRef);\n  if (!parsed) {\n    return null;\n  }\n\n  let parentConfig: Record<string, unknown>;\n  try {\n    parentConfig = await fetchParentConfig(parsed.account, parsed.gateway);\n  } catch {\n    return null;\n  }\n\n  const parentPlugins =\n    parentConfig.plugins && typeof parentConfig.plugins === \"object\"\n      ? (parentConfig.plugins as Record<string, unknown>)\n      : {};\n  const localPlugins =\n    localConfig.plugins && typeof localConfig.plugins === \"object\"\n      ? (localConfig.plugins as Record<string, unknown>)\n      : {};\n\n  const newPluginKeys = Object.keys(parentPlugins).filter((key) => !(key in localPlugins));\n  return { localConfig, parentPlugins, newPluginKeys };\n}\n\nfunction extractSemver(value: string | undefined): string | null {\n  if (!value) return null;\n  const match = value.match(/\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?/);\n  return match?.[0] ?? null;\n}\n\nexport function buildChangelogUrl(\n  oldVersion: string | undefined,\n  newVersion: string,\n  repository: string | undefined,\n): string | undefined {\n  const fromVersion = extractSemver(oldVersion);\n  const toVersion = extractSemver(newVersion);\n  if (!fromVersion || !toVersion || fromVersion === toVersion) return undefined;\n\n  const repoUrl = repository;\n  if (!repoUrl) return undefined;\n\n  const githubMatch = repoUrl.match(/^https?:\\/\\/github\\.com\\/([^/]+)\\/([^/]+?)(?:\\.git)?$/);\n  if (!githubMatch) return undefined;\n\n  const [, owner, repo] = githubMatch;\n  return `https://github.com/${owner}/${repo}/compare/v${fromVersion}...v${toVersion}`;\n}\n"],"mappings":";;;;;;AAiCA,MAAM,cAAc,EAAE,OAAO,OAAgB;AAE7C,SAAS,kBAAkB,SAAqE;CAC9F,MAAM,QAAQ,QAAQ,QAAQ,MAAM,KAAK,KAAK;CAC9C,IAAI,QAAQ,WAAW,eACrB,uBAAO,IAAI,MACT,mEAAmE,QAAQ,UAAU,cAAc,GAC9F,QAAQ,aAAa,UAAU,GAAG,wCACzC;CAEF,IAAI,QAAQ,WAAW,oBACrB,uBAAO,IAAI,MACT,iCAAiC,QAAQ,UAAU,2BAA2B,QAAQ,aAAa,UAAU,IAC/G;CAEF,IAAI,QAAQ,WAAW,YAAY,QAAQ,QAAQ,cAAc,QAG/D,uBAAO,IAAI,MACT,2EAA2E,QAAQ,UAAU,gBAAgB,GACxG,QAAQ,aAAa,UAAU,GAAG,gGAEzC;CAEF,uBAAO,IAAI,MACT,+DAA+D,SAAS,QAAQ,uHAC7E,QAAQ,QAAQ,UACb,6BAA6B,QAAQ,QAAQ,QAAQ,MACnD,QAAQ,QAAQ,QAAQ,YACpB,gBAAgB,QAAQ,QAAQ,QAAQ,UAAU,KAClD,wCAEN,GACR;AACF;;;;;;;;;;;AAYA,eAAsB,+BAA+B,OAKf;CACpC,MAAM,QAAkC;EACtC,GAAI,MAAM,SAAS,SAAS,CAAC;EAC7B,iBAAiB;CACnB;CACA,MAAM,SAAS;EACb,aAAa,MAAM;EACnB,GAAI,MAAM,oBAAoB,SAAY,EAAE,iBAAiB,MAAM,gBAAgB,IAAI,CAAC;EACxF,WAAW,MAAM;EACjB;EACA,GAAI,MAAM,SAAS,cAAc,EAAE,aAAa,MAAM,QAAQ,YAAY,IAAI,CAAC;CACjF;CAEA,MAAM,SAAS,MAAMA,kCAAe;EAAE,GAAG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