{"version":3,"file":"snapshot-source.cjs","names":["verifiedBootSnapshot"],"sources":["../../../src/services/start/snapshot-source.ts"],"sourcesContent":["/**\n * The degraded boot rung (ADR 0041, ticket 02): the registry identity is set,\n * the registry is unreachable, and an approved boot snapshot exists — boot\n * from the snapshot instead of failing, loudly and visibly. The rung applies\n * ONLY to transport failures; a definitive negative (the runtime has no\n * published config) is a state change, not an outage, and fails loud without\n * consulting the snapshot. The snapshot's own integrity is checked without\n * the network (fingerprint recomputation — `verifiedBootSnapshot`), so a\n * snapshot boot never serves bytes nobody verified.\n */\nimport type { BosConfig } from \"../../types\";\nimport { type BootSnapshot, verifiedBootSnapshot } from \"../snapshot/schema\";\n\nexport interface RegistryResolution {\n  config: BosConfig;\n  source: \"registry\" | \"snapshot\";\n  /** Degradation and persist notes — the boot surfaces them in the start\n   * summary's warnings (and the renderer prints them). */\n  warnings: string[];\n}\n\nexport interface RegistryConfigSourceDeps {\n  /** The registry fetch. Throws on transport failure; returns null for the\n   * definitive \"no config published\" negative. */\n  fetchPublishedConfig: (\n    account: string,\n    gateway: string,\n    registry?: string,\n  ) => Promise<BosConfig | null>;\n  loadSnapshot: (account: string, gateway: string, dir?: string) => Promise<BootSnapshot | null>;\n  /** The writer seam: called on every registry resolution so the first\n   * restart after a cold boot is covered even before any watch adopt. */\n  persistSnapshot: (account: string, gateway: string, config: BosConfig) => Promise<void>;\n}\n\nconst describe = (error: unknown): string =>\n  error instanceof Error ? error.message : String(error);\n\n/** The degraded boot's marker (ADR 0041): the host process was started from\n * the approved boot snapshot, so /health carries composition provenance until\n * the watch adopts a published generation. */\nexport const BOOT_SNAPSHOT_ENV_KEY = \"BOS_BOOT_SNAPSHOT\";\n\nexport function bootSnapshotEnv(\n  source: \"registry\" | \"snapshot\" | null | undefined,\n): Record<string, string> {\n  return source === \"snapshot\" ? { [BOOT_SNAPSHOT_ENV_KEY]: \"1\" } : {};\n}\n\nexport function createRegistryConfigSource(deps: RegistryConfigSourceDeps) {\n  return {\n    resolve: async (input: {\n      account: string;\n      gateway: string;\n      registry?: string;\n      allowSnapshot: boolean;\n      dir?: string;\n    }): Promise<RegistryResolution | null> => {\n      let fetched: BosConfig | null;\n      try {\n        fetched = await deps.fetchPublishedConfig(input.account, input.gateway, input.registry);\n      } catch (fetchError) {\n        if (!input.allowSnapshot) throw fetchError;\n        const snapshot = await usableSnapshot(input, fetchError);\n        if (!snapshot) throw fetchError;\n        const tag = snapshot.manifestTag ? `, ${snapshot.manifestTag.version}` : \"\";\n        return {\n          config: snapshot.config,\n          source: \"snapshot\",\n          warnings: [\n            `registry unreachable — serving the approved boot snapshot (written ${snapshot.writtenAt}${tag}); the pointer watch adopts the published config when the registry returns`,\n          ],\n        };\n      }\n      if (!fetched) return null;\n      const warnings: string[] = [];\n      try {\n        await deps.persistSnapshot(input.account, input.gateway, fetched);\n      } catch (error) {\n        warnings.push(`approved-snapshot persist failed (non-fatal): ${describe(error)}`);\n      }\n      return { config: fetched, source: \"registry\", warnings };\n    },\n  };\n\n  /** Absent snapshot → null (the original transport error is rethrown by the\n   * caller). Corrupt or unverifiable → a combined error naming both causes. */\n  async function usableSnapshot(\n    input: { account: string; gateway: string; dir?: string },\n    fetchError: unknown,\n  ): Promise<BootSnapshot | null> {\n    let loaded: BootSnapshot | null;\n    try {\n      loaded = await deps.loadSnapshot(input.account, input.gateway, input.dir);\n    } catch (snapshotError) {\n      throw new Error(bothBroken(fetchError, snapshotError));\n    }\n    if (!loaded) return null;\n    try {\n      return verifiedBootSnapshot(loaded);\n    } catch (snapshotError) {\n      throw new Error(bothBroken(fetchError, snapshotError));\n    }\n  }\n\n  function bothBroken(fetchError: unknown, snapshotError: unknown): string {\n    return `the registry is unreachable (${describe(fetchError)}), and the approved boot snapshot is unusable (${describe(snapshotError)}) — refusing to serve an unverified composition (ADR 0041; BOS_SNAPSHOT_BOOT=never disables the snapshot rung)`;\n  }\n}\n"],"mappings":";;;AAmCA,MAAM,YAAY,UAChB,iBAAiB,QAAQ,MAAM,UAA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