refactor(ultraplan-local): extract atomicWriteJson to lib/util
Three changes in one commit: 1. NEW lib/util/atomic-write.mjs — exports atomicWriteJson(path, obj), the canonical tmp+rename pattern. Reused by pre-compact-flush.mjs and (in subsequent steps) by the new session-state writer. 2. NEW tests/lib/atomic-write.test.mjs — 4 unit tests covering round-trip, no-orphan-tmp, overwrite-atomic, pretty-print formatting. 3. REFACTOR hooks/scripts/pre-compact-flush.mjs — replace the inline atomicWrite() with the imported atomicWriteJson(). Also fixes a pre-existing syntax error (leading whitespace + stray --resume token outside the comment block) that silently broke the hook from v3.1.0 onward — PreCompact runtime is fail-open and swallowed the error. File reformatted with standard zero-indent JS. 163 → 167 tests, 0 fail. Step 2 of /ultracontinue v3.3.0 (project 2026-05-01-ultracontinue).
This commit is contained in:
parent
bdddf52873
commit
655c8d46f8
3 changed files with 216 additions and 146 deletions
14
plugins/ultraplan-local/lib/util/atomic-write.mjs
Normal file
14
plugins/ultraplan-local/lib/util/atomic-write.mjs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue