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): scope inbox-recovery to locally-originated entries

Adam Eivy6d8e28f15184
diff --git a/server/services/brain.js b/server/services/brain.js
index 1951b1bc78..91f3ef01f6 100644
--- a/server/services/brain.js
+++ b/server/services/brain.js
@@ -10,6 +10,7 @@
import * as storage from './brainStorage.js';
import { brainEvents } from './brainStorage.js';
+import { getInstanceId } from './instances.js';
import { getActiveProvider, getProviderById } from './providers.js';
import { buildPrompt } from './promptService.js';
import { validate } from '../lib/validation.js';
@@ -706,15 +707,29 @@ export async function deleteInboxEntry(inboxLogId) {
/**
* Recover inbox entries stuck in 'classifying' status from a previous server restart.
* Resets them to 'needs_review' so the user can retry.
+ *
+ * Only touches entries THIS instance originated. Now that inbox rows are synced
+ * brain records, a peer's entry can be legitimately mid-classification (status
+ * 'classifying') on ITS machine while we hold a synced copy. Flipping that to
+ * 'needs_review' here would stamp a fresh updatedAt and, as the LWW winner,
+ * clobber the origin's real classification on the next sync. Classification only
+ * ever runs on the capturing (origin) machine, so a stuck 'classifying' entry is
+ * only ours to recover when we created it. (Pre-091 records with no
+ * originInstanceId are treated as local — backfillOriginInstanceId stamps them
+ * with our id at boot anyway, so they ARE ours.)
*/
export async function recoverStuckClassifications() {
+ const instanceId = await getInstanceId();
const entries = await storage.getInboxLog({ status: 'classifying', limit: 100 });
+ let recovered = 0;
for (const entry of entries) {
+ if (entry.originInstanceId && entry.originInstanceId !== instanceId) continue;
await storage.updateInboxLog(entry.id, { status: 'needs_review' });
+ recovered++;
console.log(`🧠 Recovered stuck classification: ${entry.id}`);
}
- if (entries.length > 0) {
- console.log(`🧠 Recovered ${entries.length} stuck classification(s)`);
+ if (recovered > 0) {
+ console.log(`🧠 Recovered ${recovered} stuck classification(s)`);
}
}
diff --git a/server/services/brain.test.js b/server/services/brain.test.js
index 1db16d4415..30ae5bd7a1 100644
--- a/server/services/brain.test.js
+++ b/server/services/brain.test.js
@@ -90,6 +90,12 @@ tryReadFile: vi.fn().mockResolvedValue(null),
ensureDirs: vi.fn().mockResolvedValue(undefined)
}));
+// recoverStuckClassifications() resolves this instance's id to skip peer-origin
+// entries; stub it to a stable id.
+vi.mock('./instances.js', () => ({
+ getInstanceId: () => Promise.resolve('local-instance'),
+}));
+
// Mock the central LLM handler — brain.js used to spawn child_process
// directly, but now delegates to runPromptThroughProvider. Tests stub it to
// return canned responses; the runner-internal mechanics (spawn args, --model
@@ -620,6 +626,25 @@ describe('brain service', () => {
expect(storage.updateInboxLog).not.toHaveBeenCalled();
});
+
+ it('skips peer-origin entries so recovery does not clobber a peer mid-classification', async () => {
+ // Now that inbox rows sync, flipping a peer's in-flight 'classifying' entry
+ // to 'needs_review' here would stamp a fresh updatedAt and win LWW,
+ // overwriting the origin's real classification. Recovery is local-only.
+ storage.getInboxLog.mockResolvedValue([
+ { id: 'mine', originInstanceId: 'local-instance' },
+ { id: 'peers', originInstanceId: 'peer-x' },
+ { id: 'legacy' }, // no origin → pre-backfill local record, treated as ours
+ ]);
+ storage.updateInboxLog.mockResolvedValue({});
+
+ await recoverStuckClassifications();
+
+ expect(storage.updateInboxLog).toHaveBeenCalledWith('mine', { status: 'needs_review' });
+ expect(storage.updateInboxLog).toHaveBeenCalledWith('legacy', { status: 'needs_review' });
+ expect(storage.updateInboxLog).not.toHaveBeenCalledWith('peers', expect.anything());
+ expect(storage.updateInboxLog).toHaveBeenCalledTimes(2);
+ });
});
// ===========================================================================