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: preserve mailbox accounts on failed reads (#9726)

Adam Eivyb8f63a6f7292
diff --git a/server/services/integrationJsonWriteback.test.js b/server/services/integrationJsonWriteback.test.js
index 546f1c3baa..23058369a1 100644
--- a/server/services/integrationJsonWriteback.test.js
+++ b/server/services/integrationJsonWriteback.test.js
@@ -38,6 +38,7 @@ vi.mock('../lib/childProcess.js', async (importOriginal) => ({
}));
const calendar = await import('./calendarAccounts.js');
+const messages = await import('./messageAccounts.js');
const datadog = await import('./datadog.js');
const github = await import('./github.js');
const obsidian = await import('./obsidian.js');
@@ -53,6 +54,14 @@ const stores = [
preserved: data => data.existing,
initialized: data => Object.values(data).some(account => account.name === 'New'),
},
+ {
+ name: 'message accounts', path: join(PATHS.messages, 'accounts.json'),
+ seed: { existing: { id: 'existing', name: 'Existing' } },
+ mutate: () => messages.createAccount({ name: 'New', type: 'gmail' }),
+ preserved: data => data.existing,
+ initialized: data => Object.values(data).some(account => account.name === 'New'),
+ rejectsNonObjectRoot: true,
+ },
{
name: 'Datadog instances', path: join(PATHS.data, 'datadog.json'),
seed: { instances: { existing: { name: 'Existing', site: 'api.datadoghq.com' } } },
@@ -122,6 +131,17 @@ describe.each(stores)('$name durable write-back', store => {
await store.mutate();
expect(store.initialized(JSON.parse(readFileSync(store.path, 'utf8')))).toBe(true);
});
+
+ if (store.rejectsNonObjectRoot) {
+ it.each(['[]', 'null', '42', '"scalar"'])('rejects a non-object root (%s)', async bytes => {
+ mkdirSync(dirname(store.path), { recursive: true });
+ writeFileSync(store.path, bytes);
+ await expect(store.mutate()).rejects.toThrow();
+ expect(readFileSync(store.path, 'utf8')).toBe(bytes);
+ await expect(messages.listAccounts()).rejects.toThrow();
+ await expect(messages.getAccount('existing')).rejects.toThrow();
+ });
+ }
});
it('tool updates already refuse to rewrite an unreadable record', async () => {
diff --git a/server/services/messageAccounts.js b/server/services/messageAccounts.js
index 5b1437a82f..ea31a8efeb 100644
--- a/server/services/messageAccounts.js
+++ b/server/services/messageAccounts.js
@@ -1,7 +1,7 @@
import { noteReadinessChanged } from './readinessNotify.js';
import { join } from 'path';
import { v4 as uuidv4 } from '../lib/uuid.js';
-import { ensureDir, PATHS, safeJSONParse, tryReadFile, atomicWrite } from '../lib/fileUtils.js';
+import { ensureDir, PATHS, readJSONFile, atomicWrite } from '../lib/fileUtils.js';
import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
import { isPlainObject } from '../lib/objects.js';
@@ -11,10 +11,9 @@ const queueAccountWrite = createFileWriteQueue();
async function loadAccounts() {
await ensureDir(PATHS.messages);
- const content = await tryReadFile(ACCOUNTS_FILE);
- if (!content) return {};
- const parsed = safeJSONParse(content, {}, { context: 'messageAccounts' });
- return isPlainObject(parsed) ? parsed : {};
+ const parsed = await readJSONFile(ACCOUNTS_FILE, {}, { strict: true, allowArray: false });
+ if (!isPlainObject(parsed)) throw new Error('Invalid message accounts file: expected an object');
+ return parsed;
}
async function saveAccounts(accounts) {
diff --git a/server/services/messageLogging.test.js b/server/services/messageLogging.test.js
index 8ea0a6a0e4..da29e853eb 100644
--- a/server/services/messageLogging.test.js
+++ b/server/services/messageLogging.test.js
@@ -10,6 +10,18 @@ vi.mock('../lib/fileUtils.js', async (original) => ({
...await original(),
PATHS: { messages: '/mock/messages' },
ensureDir: async () => {},
+ readJSONFile: async (path, defaultValue, options = {}) => {
+ const content = doubles.files.get(path);
+ if (content == null) return defaultValue;
+ try {
+ const parsed = JSON.parse(content);
+ if (options.allowArray === false && Array.isArray(parsed)) throw new Error('array not allowed');
+ return parsed;
+ } catch (error) {
+ if (options.strict) throw error;
+ return defaultValue;
+ }
+ },
tryReadFile: async path => doubles.files.get(path) ?? null,
atomicWrite: async (path, value) => { doubles.files.set(path, JSON.stringify(value)); }
}));