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): serialize inbox writes + guard deferred Obsidian sidecar against deleted days

Adam Eivy4b36fae40e73
diff --git a/server/services/brainJournal.js b/server/services/brainJournal.js
index 0715bcd358..3dcea42230 100644
--- a/server/services/brainJournal.js
+++ b/server/services/brainJournal.js
@@ -397,6 +397,12 @@ export async function syncToObsidian(entry, { force = false } = {}) {
// guard skips a redundant sidecar write on every sync of an unchanged day.
async function persistObsidianLocation(date, notePath, vaultId) {
return storeMutex(async () => {
+ // Re-check the live record under the lock: a fire-and-forget syncToObsidian()
+ // can land AFTER the user deleted the day (deleteJournal tombstones it and
+ // clears the sidecar). Without this guard we'd resurrect an orphan sidecar
+ // entry for a tombstoned day, which would reattach if the date is recreated.
+ // brainStorage.getById returns null for a tombstone, so this skips correctly.
+ if (!(await brainStorage.getById('journals', date))) return;
const loc = await getObsidianLocation(date);
if (loc.obsidianPath !== notePath || loc.obsidianVaultId !== vaultId) {
await saveObsidianLocation(date, { obsidianPath: notePath, obsidianVaultId: vaultId });
diff --git a/server/services/brainJournal.test.js b/server/services/brainJournal.test.js
index 1943f8aec8..457876df58 100644
--- a/server/services/brainJournal.test.js
+++ b/server/services/brainJournal.test.js
@@ -253,5 +253,23 @@ describe('brainJournal', () => {
expect(obsidian.deleteNote).not.toHaveBeenCalled();
});
+
+ it('does not resurrect an Obsidian-location sidecar entry for a day deleted before a deferred sync lands', async () => {
+ obsidian.getVaultById.mockResolvedValue({ id: 'v1', path: '/' });
+ obsidian.updateNote.mockResolvedValue({ path: 'Daily Log/2026-04-17.md' });
+ await journal.updateSettings({ obsidianVaultId: 'v1', autoSync: true, obsidianFolder: 'Daily Log' });
+
+ // Create the day, then delete it (tombstone + clear sidecar).
+ await journal.appendJournal('2026-04-17', 'content');
+ await journal.deleteJournal('2026-04-17');
+
+ // A deferred sync for the now-deleted day must NOT re-create the sidecar
+ // entry (which would reattach if the date were recreated).
+ await journal.syncToObsidian({ id: 'j1', date: '2026-04-17', content: 'content', segments: [] });
+
+ const { records } = await journal.listJournals();
+ const revived = records.find((r) => r.date === '2026-04-17');
+ expect(revived).toBeUndefined();
+ });
});
});
diff --git a/server/services/brainStorage.js b/server/services/brainStorage.js
index 3dac6f4daf..35c5cb5dd9 100644
--- a/server/services/brainStorage.js
+++ b/server/services/brainStorage.js
@@ -507,26 +507,37 @@ export async function getInboxLogById(id) {
return getById('inbox', id);
}
+// Serialize every inbox write (create/update/delete). create/update/remove are
+// each a whole-file read-modify-write of inbox.json, and the inbox has genuinely
+// concurrent same-file write paths in normal single-user operation: a capture
+// (create) immediately followed by background classification (update) on the
+// same entry, plus the boot recovery sweep. Without a lock, two such writes can
+// read the same baseline and the last save wins, dropping an entry — exactly the
+// lost-update the old JSONL appendFile path couldn't suffer. This is the
+// CLAUDE.md-sanctioned "serialize two write paths that mutate the same record"
+// case (mirrors brainJournal's storeMutex), NOT a competing-humans defense.
+const withInboxLock = createMutex();
+
/**
* Create inbox log entry. `capturedAt` is the user-facing capture time (kept
* distinct from the sync `createdAt`/`updatedAt` clocks stamped by create()).
*/
export async function createInboxLog(entry) {
- return create('inbox', { ...entry, capturedAt: entry.capturedAt || now() });
+ return withInboxLock(() => create('inbox', { ...entry, capturedAt: entry.capturedAt || now() }));
}
/**
* Update inbox log entry (partial merge — returns null if absent/tombstoned).
*/
export async function updateInboxLog(id, updates) {
- return update('inbox', id, updates);
+ return withInboxLock(() => update('inbox', id, updates));
}
/**
* Delete inbox log entry (tombstones in place for sync convergence).
*/
export async function deleteInboxLog(id) {
- return remove('inbox', id);
+ return withInboxLock(() => remove('inbox', id));
}
/**