From 58ef04fa687ec3e0dc4fe19241d366fb41e89aa7 Mon Sep 17 00:00:00 2001 From: devilreef <86633411+devilr33f@users.noreply.github.com> Date: Fri, 16 Jan 2026 04:36:55 +0600 Subject: [PATCH] fix: atomic state writes with full state persistence Previously only wrote dirty keys, causing data loss when multiple modules wrote concurrently. Now writes full state atomically using temp file + rename pattern. Co-Authored-By: Claude --- src/core/state-store.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/state-store.ts b/src/core/state-store.ts index 51cd31c..f4468b1 100644 --- a/src/core/state-store.ts +++ b/src/core/state-store.ts @@ -60,13 +60,17 @@ export class StateStore { return } + // Write full state, not just dirty keys (atomicity) const toSave: Record = {} - for (const key of this.dirty) { - toSave[key] = this.state.get(key) + for (const [key, value] of this.state.entries()) { + toSave[key] = value } try { - await fs.writeFile(STATE_FILE, JSON.stringify(toSave, null, 2)) + // Atomic write: temp file + rename + const tmpFile = `${STATE_FILE}.tmp` + await fs.writeFile(tmpFile, JSON.stringify(toSave, null, 2)) + await fs.rename(tmpFile, STATE_FILE) this.dirty.clear() } catch (err) {