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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (claude): re-sync settings cache after restore; preserve digital-twin read-error semantics
[._.]/ Adam Eivyde5b3cfd4f30
diff --git a/server/services/backup.js b/server/services/backup.jsindex 3f5eec874e..60ad231db1 100644--- a/server/services/backup.js+++ b/server/services/backup.js@@ -18,6 +18,7 @@ import { getBackendName } from './memoryBackend.js';import { emitErrorEvent, ServerError } from '../lib/errorHandler.js';import { isSafeSubdirFilter } from '../lib/validation.js';import { getIo } from './socket.js';+import { reloadSettings } from './settings.js';// Module-level statelet isRunning = false;@@ -484,6 +485,10 @@ export async function restoreSnapshot(destPath, snapshotId, { dryRun = true, sub}const changedFiles = await runRsync(srcDir, PATHS.data, flags);+ // A live restore rsyncs settings.json into place outside the save() path, so+ // getSettings()'s read-cache (and other settings:updated-driven caches) would+ // keep serving pre-restore values until the next save/restart. Re-sync them.+ if (!dryRun) await reloadSettings();return { dryRun, snapshotId, subdirFilter, changedFiles };}diff --git a/server/services/digital-twin-analysis.js b/server/services/digital-twin-analysis.jsindex d7de6f5ea2..05b6d0c3a3 100644--- a/server/services/digital-twin-analysis.js+++ b/server/services/digital-twin-analysis.js@@ -68,10 +68,14 @@ export async function validateCompleteness() {// Load content for all enabled documents in parallel. A missing file just// resolves to null (dropping the synchronous existsSync pre-check that- // serialized the loop and hit the filesystem twice per doc).+ // serialized the loop and hit the filesystem twice per doc); a real I/O error+ // (EACCES/EIO) still throws — the same loud-failure behavior the existsSync ++ // unguarded readFile had, so an unreadable enabled doc isn't silently reported+ // as "missing sections" instead of surfacing the error.const loaded = await Promise.all(enabledDocs.map(async (doc) => {- const content = await readFile(join(DIGITAL_TWIN_DIR, doc.filename), 'utf-8').catch(() => null);+ const content = await readFile(join(DIGITAL_TWIN_DIR, doc.filename), 'utf-8')+ .catch((err) => { if (err.code === 'ENOENT') return null; throw err; });return content === null ? null : { doc, content: content.toLowerCase() };}));diff --git a/server/services/settings.js b/server/services/settings.jsindex 0b8cfdd847..bf7bbf12b7 100644--- a/server/services/settings.js+++ b/server/services/settings.js@@ -107,6 +107,17 @@ export const __resetSettingsCache = () => {settingsCache = null;};+// Re-sync the read cache with the current on-disk settings.json and notify every+// `settings:updated` listener. Call after a process replaces settings.json+// OUTSIDE the normal save() path — e.g. a backup restore rsyncs it into place —+// since such a write bypasses the save()-emitted event the cache rides on and+// would otherwise leave every settings consumer serving pre-restore values.+export const reloadSettings = async () => {+ const cleaned = stripStoreKeys(await loadRaw());+ settingsEvents.emit('settings:updated', cleaned);+ return cleaned;+};+// Serialize all writes to settings.json on a single tail so an updateSettings// read-merge-write can't interleave with a concurrent save (two browser tabs,// a background job racing a user save) and clobber the other's patch. Readsdiff --git a/server/services/settings.test.js b/server/services/settings.test.jsindex c1f955f993..847760891c 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, __resetSettingsCache } from './settings.js';+import { getSettings, updateSettings, updateSettingsWith, reloadSettings, __resetSettingsCache } from './settings.js';describe('settings.js', () => {beforeEach(() => {@@ -71,6 +71,45 @@ describe('settings.js', () => {expect(result).toEqual(mockSettings);});++ it('memoizes the parsed file — a second call does not re-read disk', async () => {+ tryReadFile.mockResolvedValue(JSON.stringify({ theme: 'dark' }));++ await getSettings();+ await getSettings();++ // The read cache means only the first call hits disk.+ expect(tryReadFile).toHaveBeenCalledTimes(1);+ });++ it('hands out a deep copy — mutating a nested field does not corrupt the cache', async () => {+ tryReadFile.mockResolvedValue(JSON.stringify({ display: { theme: 'dark' } }));++ const first = await getSettings();+ first.display.theme = 'light'; // mutate the returned nested object++ const second = await getSettings();+ // The cache is isolated from the mutation — second read is untouched.+ expect(second.display.theme).toBe('dark');+ });+ });++ describe('reloadSettings', () => {+ it('re-syncs the cache with the current on-disk file (for out-of-band writes like restore)', async () => {+ // Warm the cache with the original file.+ tryReadFile.mockResolvedValue(JSON.stringify({ theme: 'dark' }));+ expect(await getSettings()).toEqual({ theme: 'dark' });++ // A restore rsyncs a new settings.json into place, bypassing save().+ tryReadFile.mockResolvedValue(JSON.stringify({ theme: 'light', added: true }));+ // Without reloadSettings the cache would still serve the stale value...+ expect(await getSettings()).toEqual({ theme: 'dark' });++ // ...reloadSettings re-reads disk and refreshes every settings consumer.+ const reloaded = await reloadSettings();+ expect(reloaded).toEqual({ theme: 'light', added: true });+ expect(await getSettings()).toEqual({ theme: 'light', added: true });+ });});describe('updateSettings', () => {