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

perf: emit journals:changed as a single-day delta instead of re-reading every daily log from disk (#3510)

[._.]/ Adam Eivy16090428d1db
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index ae6282d2a6..22605af2f7 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -127,3 +127,5 @@
- **[issue-3501] Daily agent reports for past dates read as all-zero, and the dashboard's activity card re-scanned every agent you have ever run.** Both the daily report and the "today" activity card worked out which agents finished on a given date by walking the entire in-memory agent dictionary and string-matching each record's completion timestamp — so the cost grew with your whole agent history rather than with the day being asked about, and once the housekeeping sweep had evicted a day's finished agents out of live state (it keeps roughly a day), asking for that date's report returned zeros even though every one of those runs was still sitting on disk. Both now consult the date-bucket index that already maps each archived agent to its day: the index is a small in-memory id-to-date map, so a date whose agents are all still live costs no disk reads at all, and a date that has been swept out resolves from the archive instead of reporting nothing. Live records still win over their archived copies, since the archive is written at completion time and the live one carries anything recorded since. The "while you were away" card is built on the same lookup now, which also stops it re-reading up to thirty days of archives on every poll for agents it already had in hand. Asking for a report by anything other than a plain calendar day is now rejected outright rather than quietly writing an empty report under a filename nothing can read back, and a report row's duration is derived the same way every other view derives it, so a clock-skewed run can no longer show negative time.
- **[issue-3509] Opening your Bookmarks no longer reads and re-sorts every link you have ever saved.** Answering a single page of `/brain/links` meant loading and JSON-parsing every link record on disk, filtering the whole array in memory, sorting all of it by date, and then throwing away everything outside the fifty rows actually asked for — so the cost of showing page one grew with the size of the entire collection rather than with the size of the page. The links list now rides a small in-memory index of just the four fields the endpoint orders, filters, and counts on, refreshed per record as links change rather than rebuilt per request, so only the requested page's records are opened. The filters keep their exact previous meaning, including that a link saved before the GitHub flag existed still does not match "not a GitHub repo" — absent is not the same as false. Links sharing a timestamp, which is every link from one bulk import, are now ordered by id underneath the date so paging through them cannot skip or repeat a row at a page boundary, and a link with a missing or unreadable date sorts last instead of scrambling the order around it. Two neighbouring paths lost the same whole-collection read: checking that a drag-reorder batch refers to links that still exist, and checking whether a URL you are about to save is already bookmarked.
+
+- **[issue-3510] Typing in your Daily Log no longer re-reads every day you have ever written.** Each daily-log entry is its own file on disk, and every save, autosave, and dictated segment against today was rebuilding a full date-to-entry map of your entire journal history — opening and parsing every past day, merging each one's local Obsidian mirror location back onto it — purely to fill in the payload of a store-wide "the journal changed" event. Nothing consumed that map: the write paths already announce the single affected day, and that is what the memory vectorizer listens on. The event now carries just the day that moved, so an append costs the one entry it touched instead of the whole archive, and a long dictation session no longer re-reads years of entries once per sentence. A delete reports the day with an explicitly empty entry, which is distinct from a day that simply was not part of the change.
diff --git a/server/services/brainJournal.js b/server/services/brainJournal.js
index 5dc601f1c5..5ccb94bff5 100644
--- a/server/services/brainJournal.js
+++ b/server/services/brainJournal.js
@@ -136,19 +136,21 @@ async function putEntry(date, entry) {
return { ...saved, obsidianPath: loc.obsidianPath, obsidianVaultId: loc.obsidianVaultId };
}
-// The legacy journals:changed event carried the full date→entry map. Rebuild it
-// from the store (tombstones stripped) so existing consumers keep working. The
-// brainStorage 2s cache absorbs dictation bursts so this isn't a hot re-read.
-async function rawRecords() {
- const locations = await loadObsidianLocations();
- const records = {};
- for (const entry of await brainStorage.getAll('journals')) {
- const date = entry.date || entry.id;
- const loc = locations[date] || { obsidianPath: null, obsidianVaultId: null };
- records[date] = { ...entry, obsidianPath: loc.obsidianPath, obsidianVaultId: loc.obsidianVaultId };
- }
- return records;
-}
+// Store-wide "the daily log changed" signal, carrying the ONE day that moved.
+//
+// This used to ship the full date→entry map, rebuilt by reading and JSON-parsing
+// every `data/brain/journals/<date>/index.json` on disk (issue #3510). Since the
+// per-record split (#725) there is no whole-store cache to absorb that, so every
+// autosave/dictation segment against today re-read the user's entire history to
+// build a payload nobody consumed — the write paths already emit
+// `journals:upserted` / `journals:appended` / `journals:deleted` with the single
+// affected entry, and those are what the memory bridge listens on.
+//
+// Payload is now a delta: `{ date, entry }`, with `entry: null` for a delete. A
+// listener that wants more than the changed day should read it back through
+// `listJournals()` / `getJournal(date)` rather than have every writer pay for a
+// full-store materialization on the off chance someone is subscribed.
+const emitChanged = (date, entry) => brainEvents.emit('journals:changed', { date, entry });
// Accept YYYY-MM-DD only, and require a real calendar day so we can't create
// store keys like '2026-02-30' that don't sort meaningfully or round-trip.
@@ -256,7 +258,7 @@ export async function setJournalContent(date, content) {
return putEntry(date, next);
});
scheduleObsidianSync(entry);
- brainEvents.emit('journals:changed', { records: await rawRecords() });
+ emitChanged(date, entry);
// Per-entry event so downstream syncers (memory bridge) can update the
// single affected day without iterating the whole store.
brainEvents.emit('journals:upserted', { entry });
@@ -295,7 +297,7 @@ export async function appendJournal(date, text, { source = 'text' } = {}) {
return { entry: saved, segment: segmentLocal };
});
scheduleObsidianSync(entry);
- brainEvents.emit('journals:changed', { records: await rawRecords() });
+ emitChanged(date, entry);
brainEvents.emit('journals:appended', { entry, segment });
// Per-entry event so the memory bridge re-embeds only this day, not all
// of them. (Keep journals:appended separate — it carries the single new
@@ -332,7 +334,7 @@ export async function upsertAutoSection(date, body, markers) {
});
if (!entry) return null;
scheduleObsidianSync(entry);
- brainEvents.emit('journals:changed', { records: await rawRecords() });
+ emitChanged(date, entry);
brainEvents.emit('journals:upserted', { entry });
return entry;
}
@@ -353,7 +355,9 @@ export async function deleteJournal(date) {
}
// Drop the local mirror-location record for this day (sidecar is not synced).
await saveObsidianLocation(date, { obsidianPath: null, obsidianVaultId: null });
- brainEvents.emit('journals:changed', { records: await rawRecords() });
+ // `entry: null` — the day is gone; the journals:deleted event below carries
+ // the record that was removed for consumers that need its contents.
+ emitChanged(date, null);
// Explicit deletion signal so memory bridges / integrations can archive
// the corresponding vector entry — the changed event alone doesn't tell
// the bridge which record vanished.
diff --git a/server/services/brainJournal.test.js b/server/services/brainJournal.test.js
index 457876df58..c70cfcbe31 100644
--- a/server/services/brainJournal.test.js
+++ b/server/services/brainJournal.test.js
@@ -68,9 +68,12 @@ vi.mock('./brainStorage.js', () => ({
}));
import * as journal from './brainJournal.js';
-import { brainEvents } from './brainStorage.js';
+import { brainEvents, getAll } from './brainStorage.js';
import * as obsidian from './obsidian.js';
+// Pull the payload of the last emit of `name`, or undefined if it never fired.
+const lastEmit = (name) => brainEvents.emit.mock.calls.filter((c) => c[0] === name).at(-1)?.[1];
+
afterAll(() => {
rmSync(TEMP_ROOT, { recursive: true, force: true });
});
@@ -149,6 +152,26 @@ describe('brainJournal', () => {
expect(eventNames).toContain('journals:upserted');
});
+ // Issue #3510: journals:changed used to carry the full date→entry map,
+ // rebuilt by reading every historical day off disk on every autosave /
+ // dictation segment. It now carries just the day that moved.
+ it('emits journals:changed as a { date, entry } delta, not a full map', async () => {
+ await journal.appendJournal('2026-04-16', 'yesterday');
+ await journal.appendJournal('2026-04-17', 'today');
+ const payload = lastEmit('journals:changed');
+ expect(payload.date).toBe('2026-04-17');
+ expect(payload.entry.content).toBe('today');
+ expect(payload).not.toHaveProperty('records');
+ });
+
+ it('does not read every historical entry when appending to one day', async () => {
+ await journal.appendJournal('2026-04-15', 'day one');
+ await journal.appendJournal('2026-04-16', 'day two');
+ getAll.mockClear();
+ await journal.appendJournal('2026-04-17', 'day three');
+ expect(getAll).not.toHaveBeenCalled();
+ });
+
it('ignores empty/whitespace text', async () => {
const res = await journal.appendJournal('2026-04-17', ' ');
expect(res).toBeNull();
@@ -178,6 +201,32 @@ describe('brainJournal', () => {
expect(cleared.content).toBe('');
expect(cleared.segments).toEqual([]);
});
+
+ it('emits a { date, entry } delta without re-reading the store (#3510)', async () => {
+ await journal.appendJournal('2026-04-16', 'yesterday');
+ getAll.mockClear();
+ await journal.setJournalContent('2026-04-17', 'brand new');
+ expect(getAll).not.toHaveBeenCalled();
+ expect(lastEmit('journals:changed')).toMatchObject({
+ date: '2026-04-17',
+ entry: { content: 'brand new' },
+ });
+ });
+ });
+
+ describe('deleteJournal', () => {
+ // `entry: null` marks the day as gone; the journals:deleted event that
+ // follows carries the removed record for consumers that need its contents.
+ it('emits journals:changed with a null entry and no full-store read (#3510)', async () => {
+ await journal.appendJournal('2026-04-16', 'yesterday');
+ await journal.appendJournal('2026-04-17', 'today');
+ getAll.mockClear();
+
+ expect(await journal.deleteJournal('2026-04-17')).toBe(true);
+ expect(getAll).not.toHaveBeenCalled();
+ expect(lastEmit('journals:changed')).toEqual({ date: '2026-04-17', entry: null });
+ expect(lastEmit('journals:deleted').entry.content).toBe('today');
+ });
});
describe('Obsidian mirror', () => {