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

address review (codex): guard the cold settings-cache fill against a mid-read save

[._.]/ Adam Eivy2fa32b028eee
diff --git a/server/services/settings.js b/server/services/settings.js
index bf7bbf12b7..ad43f59694 100644
--- a/server/services/settings.js
+++ b/server/services/settings.js
@@ -161,7 +161,12 @@ export const getSettings = async () => {
if (settingsCache === null) {
// stripStoreKeys builds a fresh top-level object over the just-parsed
// loadRaw() graph, which nothing else references — safe to cache directly.
- settingsCache = stripStoreKeys(await loadRaw());
+ const loaded = stripStoreKeys(await loadRaw());
+ // A save()/reloadSettings() may have populated the cache via the
+ // settings:updated listener while this cold read was awaiting the disk read.
+ // Only fill if still empty, so an older on-disk snapshot can't clobber that
+ // fresher in-memory value and strand consumers on stale settings.
+ if (settingsCache === null) settingsCache = loaded;
}
// Hand out a private deep copy so a caller mutating nested settings in place
// can't corrupt the shared cache — matching the prior per-call
diff --git a/server/services/settings.test.js b/server/services/settings.test.js
index 847760891c..2add128b2f 100644
--- a/server/services/settings.test.js
+++ b/server/services/settings.test.js
@@ -14,7 +14,7 @@ vi.mock('../lib/fileUtils.js', async (importActual) => {
});
import { atomicWrite, tryReadFile } from '../lib/fileUtils.js';
-import { getSettings, updateSettings, updateSettingsWith, reloadSettings, __resetSettingsCache } from './settings.js';
+import { getSettings, updateSettings, updateSettingsWith, reloadSettings, settingsEvents, __resetSettingsCache } from './settings.js';
describe('settings.js', () => {
beforeEach(() => {
@@ -82,6 +82,23 @@ describe('settings.js', () => {
expect(tryReadFile).toHaveBeenCalledTimes(1);
});
+ it('a settings:updated landing during a cold read does not clobber the fresher value', async () => {
+ // Hold the cold read's disk load open until we release it, so we can slip a
+ // save() in while getSettings() is awaiting loadRaw().
+ let releaseRead;
+ tryReadFile.mockReturnValue(new Promise((resolve) => { releaseRead = resolve; }));
+
+ const inflight = getSettings(); // cold — now awaiting the disk read
+ // A save completes mid-flight and populates the cache with fresh settings.
+ settingsEvents.emit('settings:updated', { theme: 'fresh' });
+ // Only now does the older on-disk snapshot resolve.
+ releaseRead(JSON.stringify({ theme: 'stale' }));
+ await inflight;
+
+ // The fresher save value must win — the resumed cold read must not overwrite it.
+ expect(await getSettings()).toEqual({ theme: 'fresh' });
+ });
+
it('hands out a deep copy — mutating a nested field does not corrupt the cache', async () => {
tryReadFile.mockResolvedValue(JSON.stringify({ display: { theme: 'dark' } }));