{"version":3,"file":"atomicJson-D3MHqnLW.cjs","names":["createSerialRunner","path","getOrThrow","nodeHost","writeFileAtomic"],"sources":["../src/services/atomicJson/format.ts","../src/services/atomicJson/index.ts"],"sourcesContent":["/** Owner-only modes for private JSON files and the directories created for them. */\nexport const PRIVATE_FILE_MODE = 0o600;\nexport const PRIVATE_DIRECTORY_MODE = 0o700;\n\n/** The on-disk form: two-space indent and a trailing newline. */\nexport function serializePrivateJson(value: unknown): string {\n  return `${JSON.stringify(value, null, 2)}\\n`;\n}\n","import path from 'node:path';\n\nimport {\n  createSerialRunner,\n  getOrThrow,\n  nodeHost,\n  writeFileAtomic,\n  type FileSystem,\n} from '@agimon-ai/po-core/platform';\n\nimport { PRIVATE_DIRECTORY_MODE, PRIVATE_FILE_MODE, serializePrivateJson } from './format';\n\n/**\n * Private atomic JSON writes.\n *\n * Write to a uniquely named temporary file in the destination directory, then\n * rename onto the target. Rename is atomic within a filesystem, so a reader\n * sees either the previous file or the complete new one and never a partial\n * write. The temporary name carries a random UUID, so two writers of the same\n * target cannot collide on it.\n *\n * This prevents torn reads. It does NOT prevent lost updates across processes:\n * two writers doing read-modify-write still clobber each other, because the\n * rename only publishes what the caller already computed. Writes from one\n * process to one path are queued in call order.\n *\n * AVOID:\n * - Treating this as a lock\n * - Leaving the temporary file behind when the write fails\n */\n\ntype IoContext = Parameters<FileSystem['readTextFile']>[1];\n\nconst writes = createSerialRunner();\n\n/**\n * Creates a directory and any missing ancestors, giving each directory it\n * creates `mode`, as `mkdir -p -m` does. Existing directories keep their mode.\n */\nexport async function createDirectoryWithMode(\n  fs: FileSystem,\n  directory: string,\n  mode: number,\n  context: IoContext,\n): Promise<void> {\n  const missing: string[] = [];\n  let current = path.resolve(directory);\n  while (!getOrThrow(await fs.exists(current, context))) {\n    missing.push(current);\n    const parent = path.dirname(current);\n    if (parent === current) break;\n    current = parent;\n  }\n  if (missing.length === 0) return;\n  getOrThrow(await fs.createDir(directory, { recursive: true }, context));\n  for (const created of missing.reverse()) await nodeHost.setMode(created, mode);\n}\n\n/** Writes JSON readable by its owner only, replacing the target atomically. */\nexport async function writePrivateAtomicJsonAsync(\n  fs: FileSystem,\n  filePath: string,\n  value: unknown,\n  context: IoContext,\n): Promise<void> {\n  const content = serializePrivateJson(value);\n  await writes.forKey(path.resolve(filePath))(async () => {\n    await createDirectoryWithMode(fs, path.dirname(filePath), PRIVATE_DIRECTORY_MODE, context);\n    await writeFileAtomic(fs, filePath, content, context);\n    await nodeHost.setMode(filePath, PRIVATE_FILE_MODE);\n  });\n}\n\nexport { writePrivateAtomicJson } from './sync';\n"],"mappings":";;;;;;AAKA,SAAgB,qBAAqB,OAAwB;CAC3D,OAAO,GAAG,KAAK,UAAU,OAAO,MAAM,CAAC,EAAE;AAC3C;;;AC0BA,MAAM,UAAA,GAASA,4BAAAA,mBAAAA,CAAmB;;;;;AAMlC,eAAsB,wBACpB,IACA,WACA,MACA,SACe;CACf,MAAM,UAAoB,CAAC;CAC3B,IAAI,UAAUC,UAAAA,QAAK,QAAQ,SAAS;CACpC,OAAO,EAAA,GAACC,4BAAAA,WAAAA,CAAW,MAAM,GAAG,OAAO,SAAS,OAAO,CAAC,GAAG;EACrD,QAAQ,KAAK,OAAO;EACpB,MAAM,SAASD,UAAAA,QAAK,QAAQ,OAAO;EACnC,IAAI,WAAW,SAAS;EACxB,UAAU;CACZ;CACA,IAAI,QAAQ,WAAW,GAAG;CAC1B,CAAA,GAAA,4BAAA,WAAA,CAAW,MAAM,GAAG,UAAU,WAAW,EAAE,WAAW,KAAK,GAAG,OAAO,CAAC;CACtE,KAAK,MAAM,WAAW,QAAQ,QAAQ,GAAG,MAAME,4BAAAA,SAAS,QAAQ,SAAS,IAAI;AAC/E;;AAGA,eAAsB,4BACpB,IACA,UACA,OACA,SACe;CACf,MAAM,UAAU,qBAAqB,KAAK;CAC1C,MAAM,OAAO,OAAOF,UAAAA,QAAK,QAAQ,QAAQ,CAAC,CAAC,CAAC,YAAY;EACtD,MAAM,wBAAwB,IAAIA,UAAAA,QAAK,QAAQ,QAAQ,GAAA,KAA2B,OAAO;EACzF,OAAA,GAAMG,4BAAAA,gBAAAA,CAAgB,IAAI,UAAU,SAAS,OAAO;EACpD,MAAMD,4BAAAA,SAAS,QAAQ,UAAA,GAA2B;CACpD,CAAC;AACH"}