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: batch Obsidian resync location saves (#5213)

[._.]/ Adam Eivy5f704dd73b76
diff --git a/server/services/brainJournal.js b/server/services/brainJournal.js
index 693e91b6bc..d9b4cf8ba0 100644
--- a/server/services/brainJournal.js
+++ b/server/services/brainJournal.js
@@ -127,6 +127,27 @@ async function saveObsidianLocation(date, { obsidianPath, obsidianVaultId }) {
await atomicWrite(OBSIDIAN_LOCATIONS_FILE, map);
}
+// Apply a bulk re-sync's changed locations in one sidecar write. Re-check each
+// record under the same mutex as individual writes so a day deleted while the
+// foreground re-sync was running cannot be resurrected in the local map.
+async function saveObsidianLocations(locations) {
+ if (!locations.size) return;
+ return storeMutex(async () => {
+ const map = { ...(await loadObsidianLocations()) };
+ let changed = false;
+ for (const [date, location] of locations) {
+ if (!(await brainStorage.getById('journals', date))) continue;
+ if (map[date]?.obsidianPath === location.obsidianPath && map[date]?.obsidianVaultId === location.obsidianVaultId) continue;
+ map[date] = location;
+ changed = true;
+ }
+ if (!changed) return;
+ obsidianLocationsCache = map;
+ obsidianLocationsCacheTime = Date.now();
+ await atomicWrite(OBSIDIAN_LOCATIONS_FILE, map);
+ });
+}
+
// ── Synced journal entry store ───────────────────────────────────────────────
// Daily Log entries are keyed by calendar date in the entity store, so the
@@ -442,7 +463,7 @@ function buildObsidianNotePath(settings, date) {
* all entries now" action so users who turn off auto-sync can still trigger
* a one-shot backfill.
*/
-export async function syncToObsidian(entry, { force = false } = {}) {
+export async function syncToObsidian(entry, { force = false, locationUpdates = null } = {}) {
const settings = await getSettings();
if (!settings.obsidianVaultId) return null;
if (!force && !settings.autoSync) return null;
@@ -460,6 +481,10 @@ export async function syncToObsidian(entry, { force = false } = {}) {
// a vault swap in Settings both need to update the store so a later
// deleteJournal() unlinks the right file in the right vault.
if (entry.obsidianPath !== notePath || entry.obsidianVaultId !== vaultId) {
+ if (locationUpdates) {
+ locationUpdates.set(entry.date, { obsidianPath: notePath, obsidianVaultId: vaultId });
+ return notePath;
+ }
await persistObsidianLocation(entry.date, notePath, vaultId);
}
return notePath;
@@ -521,11 +546,12 @@ export async function resyncAllToObsidian() {
let skipped = 0;
let consecutiveSkips = 0;
let stoppedEarly = false;
+ const locationUpdates = new Map();
for (const entry of records) {
// force:true so this bulk resync still writes even when the user has
// turned off the per-write autoSync — they explicitly clicked "Re-sync
// all entries now", which is the manual-sync escape hatch.
- const path = await syncToObsidian(entry, { force: true }).catch(() => null);
+ const path = await syncToObsidian(entry, { force: true, locationUpdates }).catch(() => null);
if (path) {
synced += 1;
consecutiveSkips = 0;
@@ -547,5 +573,6 @@ export async function resyncAllToObsidian() {
break;
}
}
+ await saveObsidianLocations(locationUpdates);
return { synced, skipped, stoppedEarly };
}
diff --git a/server/services/brainJournal.test.js b/server/services/brainJournal.test.js
index 82eac4e2ef..6fd5113072 100644
--- a/server/services/brainJournal.test.js
+++ b/server/services/brainJournal.test.js
@@ -15,6 +15,7 @@ vi.mock('../lib/fileUtils.js', async () => {
const actual = await vi.importActual('../lib/fileUtils.js');
return {
...actual,
+ atomicWrite: vi.fn(actual.atomicWrite),
PATHS: { ...actual.PATHS, brain: TEMP_ROOT },
};
});
@@ -84,6 +85,7 @@ vi.mock('./brainStorage.js', () => ({
import * as journal from './brainJournal.js';
import { brainEvents, getAll, getById } from './brainStorage.js';
+import { atomicWrite } from '../lib/fileUtils.js';
import * as obsidian from './obsidian.js';
// Pull the payload of the last emit of `name`, or undefined if it never fired.
@@ -447,6 +449,22 @@ describe('brainJournal', () => {
await journal.updateSettings({ obsidianVaultId: 'v1', autoSync: false, obsidianFolder: 'Daily Log' });
});
+ it('writes all changed Obsidian locations in one sidecar save', async () => {
+ for (let i = 0; i < 3; i += 1) await journal.appendJournal(isoDay(i), `day ${i}`);
+ obsidian.upsertNote.mockResolvedValue('Daily Log/note.md');
+ atomicWrite.mockClear();
+
+ await journal.resyncAllToObsidian();
+
+ expect(atomicWrite).toHaveBeenCalledTimes(1);
+ const locations = await Promise.all([0, 1, 2].map((i) => journal.getJournal(isoDay(i))));
+ expect(locations.map(({ obsidianPath, obsidianVaultId }) => ({ obsidianPath, obsidianVaultId }))).toEqual([
+ { obsidianPath: 'Daily Log/2026-01-01.md', obsidianVaultId: 'v1' },
+ { obsidianPath: 'Daily Log/2026-01-02.md', obsidianVaultId: 'v1' },
+ { obsidianPath: 'Daily Log/2026-01-03.md', obsidianVaultId: 'v1' },
+ ]);
+ });
+
it('stops after a run of consecutive failures instead of grinding through every entry', async () => {
for (let i = 0; i < 40; i += 1) await journal.appendJournal(isoDay(i), `day ${i}`);
obsidian.upsertNote.mockClear();