{"version":3,"file":"snapshot.mjs","names":[],"sources":["../../../src/services/scaffold/snapshot.ts"],"sourcesContent":["import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { readLockfileIfExists, writeLockfileSync } from \"../lockfile/schema\";\n\nexport interface SyncSnapshot {\n  timestamp: string;\n  files: Record<string, string>;\n}\n\nconst SNAPSHOT_DIR = \".bos\";\nconst SNAPSHOT_FILE = \"sync-snapshot.json\";\n\nfunction snapshotPath(projectDir: string): string {\n  return join(projectDir, SNAPSHOT_DIR, SNAPSHOT_FILE);\n}\n\nfunction legacySnapshot(projectDir: string): SyncSnapshot | null {\n  const path = snapshotPath(projectDir);\n  if (!existsSync(path)) {\n    return null;\n  }\n  try {\n    const content = readFileSync(path, \"utf-8\");\n    return JSON.parse(content) as SyncSnapshot;\n  } catch {\n    return null;\n  }\n}\n\n/**\n * The sync baseline (ADR 0047): the committed form is `bos.lock.json`'s\n * `template.sync`; the legacy `.bos/sync-snapshot.json` file is the fallback\n * while a repo still runs lock-less. Lock-first keeps the two in agreement\n * once the migration absorbs the file, and keeps lock-less children working\n * unchanged.\n */\nexport async function readSnapshot(projectDir: string): Promise<SyncSnapshot | null> {\n  const lock = await readLockfileIfExists(projectDir);\n  if (lock?.template) {\n    return { timestamp: lock.template.syncedAt, files: lock.template.files };\n  }\n  return legacySnapshot(projectDir);\n}\n\n/**\n * Record the baseline through the lock when one exists — splicing only the\n * `template` key, never recomputing the frozen composition (a sync has no\n * business touching remotes). The legacy file is deleted on the absorb path\n * so it stops regenerating beside the lock. Lock-less repos keep the legacy\n * file: it is still the only baseline, now local-only.\n */\nexport async function writeSnapshot(\n  projectDir: string,\n  data: { files: Record<string, string> },\n): Promise<void> {\n  const syncedAt = new Date().toISOString();\n  const lock = await readLockfileIfExists(projectDir);\n\n  if (lock) {\n    writeLockfileSync({ ...lock, template: { syncedAt, files: data.files } }, projectDir);\n    const legacyPath = snapshotPath(projectDir);\n    if (existsSync(legacyPath)) {\n      rmSync(legacyPath, { force: true });\n    }\n    return;\n  }\n\n  const dir = join(projectDir, SNAPSHOT_DIR);\n  if (!existsSync(dir)) {\n    mkdirSync(dir, { recursive: true });\n  }\n\n  writeFileSync(\n    snapshotPath(projectDir),\n    `${JSON.stringify({ timestamp: syncedAt, files: data.files }, null, 2)}\\n`,\n  );\n}\n\n/**\n * The upgrade migration leg: absorb a legacy `.bos/sync-snapshot.json` into\n * `bos.lock.json`'s `template.sync` and delete the file, so it stops\n * rewriting ~70 hash lines across long-lived branches and stops being\n * committed. \"absorbed\" — the lock gained the baseline (the repo's committed\n * artifact changes; `bos.lock.json` rides the migration report). \"removed\" —\n * a stale duplicate beside an already-absorbed lock, or an unparseable file.\n * undefined — nothing to do: no legacy file, or no lock (the legacy file\n * stays: it is still the only baseline).\n */\nexport async function absorbLegacySnapshot(\n  projectDir: string,\n): Promise<\"absorbed\" | \"removed\" | undefined> {\n  const legacyPath = snapshotPath(projectDir);\n  if (!existsSync(legacyPath)) return undefined;\n\n  let legacy: SyncSnapshot | null = null;\n  try {\n    legacy = JSON.parse(readFileSync(legacyPath, \"utf-8\")) as SyncSnapshot;\n  } catch {\n    legacy = null;\n  }\n\n  const lock = await readLockfileIfExists(projectDir);\n  if (!lock) return undefined;\n\n  if (legacy && !lock.template) {\n    writeLockfileSync(\n      { ...lock, template: { syncedAt: legacy.timestamp, files: legacy.files } },\n      projectDir,\n    );\n    rmSync(legacyPath, { force: true });\n    return \"absorbed\";\n  }\n\n  rmSync(legacyPath, { force: true });\n  return \"removed\";\n}\n"],"mappings":";;;;;AASA,MAAM,eAAe;AACrB,MAAM,gBAAgB;AAEtB,SAAS,aAAa,YAA4B;CAChD,OAAO,KAAK,YAAY,cAAc,aAAa;AACrD;AAEA,SAAS,eAAe,YAAyC;CAC/D,MAAM,OAAO,aAAa,UAAU;CACpC,IAAI,CAAC,WAAW,IAAI,GAClB,OAAO;CAET,IAAI;EACF,MAAM,UAAU,aAAa,MAAM,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