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 message triage correction writes (#10718)

Adam Eivye07ee70d4aec
diff --git a/server/services/messageTriageRules.js b/server/services/messageTriageRules.js
index 80119ec373..7190070c0c 100644
--- a/server/services/messageTriageRules.js
+++ b/server/services/messageTriageRules.js
@@ -1,6 +1,7 @@
import { join } from 'path';
import { z } from 'zod';
import { ServerError } from '../lib/errorHandler.js';
+import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
import { atomicWrite, ensureDir, PATHS, readJSONFileStrict } from '../lib/fileUtils.js';
const RULES_FILE = join(PATHS.messages, 'triage-rules.json');
@@ -13,6 +14,10 @@ const rulesStoreSchema = z.object({
}).passthrough())
}).passthrough();
+// One tail for the shared rules file: each load→mutate→save cycle must see the
+// previous cycle's result or overlapping corrections/deletions drop writes.
+const queueWrite = createFileWriteQueue();
+
async function loadRules() {
await ensureDir(PATHS.messages);
const { ok, value } = await readJSONFileStrict(RULES_FILE, MISSING_STORE, { logError: false });
@@ -42,7 +47,11 @@ export async function getTriageRules() {
* Record a user correction: when they take a different action than the AI recommended.
* Deduplicates by pattern — if the same sender/pattern already has a rule, update it.
*/
-export async function recordCorrection({ from, subject, triaged, corrected }) {
+export function recordCorrection({ from, subject, triaged, corrected }) {
+ return queueWrite(() => applyCorrection({ from, subject, triaged, corrected }));
+}
+
+async function applyCorrection({ from, subject, triaged, corrected }) {
const data = await loadRules();
// Build a pattern from the sender — strip email-specific parts for generalization
const senderPattern = from || 'Unknown';
@@ -72,12 +81,14 @@ export async function recordCorrection({ from, subject, triaged, corrected }) {
/**
* Delete a specific rule by index.
*/
-export async function deleteRule(index) {
- const data = await loadRules();
- if (index < 0 || index >= data.rules.length) return false;
- data.rules.splice(index, 1);
- await saveRules(data);
- return true;
+export function deleteRule(index) {
+ return queueWrite(async () => {
+ const data = await loadRules();
+ if (index < 0 || index >= data.rules.length) return false;
+ data.rules.splice(index, 1);
+ await saveRules(data);
+ return true;
+ });
}
/**
diff --git a/server/services/messageTriageRules.test.js b/server/services/messageTriageRules.test.js
new file mode 100644
index 0000000000..69b8184567
--- /dev/null
+++ b/server/services/messageTriageRules.test.js
@@ -0,0 +1,72 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// In-memory file adapter with a controllable write barrier.
+const fs = vi.hoisted(() => ({ content: null, gate: null, failNextWrite: false }));
+
+vi.mock('../lib/fileUtils.js', () => ({
+ PATHS: { messages: '/virtual/messages' },
+ ensureDir: async () => {},
+ readJSONFileStrict: async () => ({ ok: true, value: JSON.parse(fs.content) }),
+ atomicWrite: async (_path, data) => {
+ if (fs.gate) await fs.gate;
+ if (fs.failNextWrite) { fs.failNextWrite = false; throw new Error('disk full'); }
+ fs.content = JSON.stringify(data);
+ }
+}));
+
+const { recordCorrection, deleteRule, listRules } = await import('./messageTriageRules.js');
+
+const corr = (from, extra = {}) => ({ from, subject: 's', triaged: 'archive', corrected: 'delete', ...extra });
+
+beforeEach(() => {
+ fs.content = JSON.stringify({ rules: [] });
+ fs.gate = null;
+ fs.failNextWrite = false;
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+});
+
+describe('messageTriageRules write serialization', () => {
+ it('persists both rules for overlapping corrections from different senders', async () => {
+ let release;
+ fs.gate = new Promise(r => { release = r; });
+ const a = recordCorrection(corr('[email protected]'));
+ const b = recordCorrection(corr('[email protected]'));
+ release();
+ await Promise.all([a, b]);
+ expect((await listRules()).map(r => r.senderPattern).sort()).toEqual(['[email protected]', '[email protected]']);
+ });
+
+ it('persists both increments for overlapping corrections of one existing pattern', async () => {
+ await recordCorrection(corr('[email protected]'));
+ let release;
+ fs.gate = new Promise(r => { release = r; });
+ const p = [recordCorrection(corr('[email protected]')), recordCorrection(corr('[email protected]'))];
+ release();
+ await Promise.all(p);
+ const rules = await listRules();
+ expect(rules).toHaveLength(1);
+ expect(rules[0].count).toBe(3);
+ });
+
+ it('keeps unrelated rules and does not resurrect a deleted rule when delete overlaps a correction', async () => {
+ await recordCorrection(corr('[email protected]'));
+ await recordCorrection(corr('[email protected]'));
+ let release;
+ fs.gate = new Promise(r => { release = r; });
+ const del = deleteRule(0);
+ const add = recordCorrection(corr('[email protected]'));
+ release();
+ expect(await del).toBe(true);
+ await add;
+ expect((await listRules()).map(r => r.senderPattern)).toEqual(['[email protected]', '[email protected]']);
+ });
+
+ it('a rejected mutation does not poison later queued operations', async () => {
+ fs.failNextWrite = true;
+ const failed = recordCorrection(corr('[email protected]'));
+ const ok = recordCorrection(corr('[email protected]'));
+ await expect(failed).rejects.toThrow('disk full');
+ await ok;
+ expect((await listRules()).map(r => r.senderPattern)).toEqual(['[email protected]']);
+ });
+});