Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: serialize MortalLoom store mutations through a write queue (#10097)

Adam Eivyec0d9c355f57
diff --git a/server/services/mortalLoomStore.js b/server/services/mortalLoomStore.js
index 27697696e4..eeb65ef603 100644
--- a/server/services/mortalLoomStore.js
+++ b/server/services/mortalLoomStore.js
@@ -18,6 +18,7 @@ import { randomUUID } from 'crypto';
import { atomicWrite, safeJSONParse, readJSONFile, dataPath, ensureDir, sleep } from '../lib/fileUtils.js';
import { ICLOUD_NOT_MATERIALIZED, isEvictedStats, materializeAndWait, readIfMaterialized, requestMaterialization } from '../lib/icloudFile.js';
import { isPlainObject } from '../lib/objects.js';
+import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
import { getSettings, settingsEvents } from './settings.js';
const DEFAULT_ICLOUD_PATH = join(
@@ -447,8 +448,16 @@ async function writeStoreAtPath(path, data) {
dashboardEvents.emit('goals:changed');
}
-/** Atomic read → mutate → write. Ensures all array keys are initialized. */
-export async function updateStore(mutator) {
+// One tail for the whole store file: overlapping read → mutate → write cycles
+// would otherwise read the same pre-image and drop each other's mutations.
+const queueStoreWrite = createFileWriteQueue();
+
+/** Atomic read → mutate → write, serialized per store. Ensures all array keys are initialized. */
+export function updateStore(mutator) {
+ return queueStoreWrite(() => updateStoreUnqueued(mutator));
+}
+
+async function updateStoreUnqueued(mutator) {
// Resolve the path once and pass it through to both read and write — settings
// could change mid-call, so we'd otherwise risk reading from one path and
// writing to another (or the overwrite-guard's existsSync looking at a
diff --git a/server/services/mortalLoomStore.test.js b/server/services/mortalLoomStore.test.js
index 8e409a80dd..74fe6163a0 100644
--- a/server/services/mortalLoomStore.test.js
+++ b/server/services/mortalLoomStore.test.js
@@ -291,6 +291,15 @@ describe('updateStore', () => {
});
afterEach(() => restorePlatform());
+ it('serializes overlapping mutations so none is lost (#10097)', async () => {
+ let disk = JSON.stringify({ goals: [] });
+ // Yield between read and write so unserialized cycles would share a pre-image.
+ readFileMock.mockImplementation(async () => { const snap = disk; await new Promise((r) => setTimeout(r, 5)); return snap; });
+ writeFileMock.mockImplementation(async (_p, payload) => { disk = payload; });
+ await Promise.all(['a', 'b', 'c'].map((id) => store.updateStore((s) => { s.goals.push({ id }); })));
+ expect(JSON.parse(disk).goals.map((g) => g.id)).toEqual(['a', 'b', 'c']);
+ });
+
it('seeds a fresh store when the file does not exist', async () => {
existsResult = false;
writeFileMock.mockResolvedValue(undefined);