// lib/util/atomic-write.mjs
// Atomic JSON file write — writes to {path}.tmp then renames to {path}.
// Crash-safe: a partial write leaves the original file untouched.
//
// Extracted from hooks/scripts/pre-compact-flush.mjs in v3.3.0 so that
// session-state writers and progress.json writers share one implementation.

import { writeFileSync, renameSync } from 'node:fs';

export function atomicWriteJson(path, obj) {
  const tmp = path + '.tmp';
  writeFileSync(tmp, JSON.stringify(obj, null, 2));
  renameSync(tmp, path);
}
