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([settings-getsave-callers-stale-base]): guard updateSettingsWith mutator return

[._.]/ Adam Eivy04e8c4b0bcdb
diff --git a/server/services/settings.js b/server/services/settings.js
index c2ca56ad51..919fa23660 100644
--- a/server/services/settings.js
+++ b/server/services/settings.js
@@ -123,5 +123,12 @@ export const updateSettings = (patch) => queueWrite(async () => {
export const updateSettingsWith = (mutate) => queueWrite(async () => {
const current = stripStoreKeys(await loadRaw());
const next = await mutate(current);
+ // Guard the mutator's return BEFORE persisting: unlike the old
+ // saveSettings(objectLiteral) callers, a mutator with a missing `return`
+ // (or one that returns an array/primitive) would otherwise serialize
+ // garbage like `undefined`/`"foo"` into settings.json. Fail loud instead.
+ if (!isPlainObject(next)) {
+ throw new TypeError('updateSettingsWith: mutate() must return a plain settings object');
+ }
return save(next);
});
diff --git a/server/services/settings.test.js b/server/services/settings.test.js
index 722ed07b56..e71e016b57 100644
--- a/server/services/settings.test.js
+++ b/server/services/settings.test.js
@@ -230,6 +230,24 @@ describe('settings.js', () => {
// must not see it (matching the getSettings() the old callers read).
expect(seen).toEqual({ theme: 'dark' });
});
+
+ it('awaits an async mutator', async () => {
+ tryReadFile.mockResolvedValue(JSON.stringify({ a: 1 }));
+ const result = await updateSettingsWith(async (current) => {
+ await Promise.resolve();
+ return { ...current, b: 2 };
+ });
+ expect(result).toEqual({ a: 1, b: 2 });
+ });
+
+ it('throws and writes nothing when the mutator returns a non-object (missing return, array, primitive)', async () => {
+ tryReadFile.mockResolvedValue(JSON.stringify({ a: 1 }));
+ for (const bad of [undefined, null, 'str', 42, [1, 2]]) {
+ await expect(updateSettingsWith(() => bad)).rejects.toThrow(/must return a plain settings object/);
+ }
+ // No invalid content (e.g. "undefined") was ever persisted.
+ expect(atomicWrite).not.toHaveBeenCalled();
+ });
});
describe('MortalLoom store key pollution guard', () => {