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: page brain inbox from projection index (#5440)

[._.]/ Adam Eivy4b1e3b4730b2
diff --git a/server/services/brainStorage.js b/server/services/brainStorage.js
index e9f488bf31..c21321a117 100644
--- a/server/services/brainStorage.js
+++ b/server/services/brainStorage.js
@@ -802,15 +802,17 @@ async function appendJsonl(type, record) {
*/
export async function getInboxLog(options = {}) {
const { status, limit = 50, offset = 0 } = options;
- let records = await getAll('inbox');
-
- records = records.sort((a, b) => new Date(b.capturedAt) - new Date(a.capturedAt));
+ const rows = await resolveRecordIndex(summaryIndex, 'inbox', 0);
+ const matching = rows.filter(([, summary]) => summary && (!status || summary.status === status));
- if (status) {
- records = records.filter(r => r.status === status);
- }
+ // Bulk captures can share one timestamp. The id tiebreak keeps adjacent
+ // offset pages stable so an entry is never duplicated or skipped.
+ matching.sort(([idA, a], [idB, b]) => (b.capturedAtMs - a.capturedAtMs) || idA.localeCompare(idB));
- return records.slice(offset, offset + limit);
+ const pageIds = matching.slice(offset, offset + limit).map(([id]) => id);
+ const page = await Promise.all(pageIds.map((id) => getById('inbox', id)));
+ // A record deleted between resolving the index and loading its body is null.
+ return page.filter(Boolean);
}
/**
@@ -852,10 +854,9 @@ export async function deleteInboxLog(id) {
*/
export async function getInboxLogCounts() {
const rows = await resolveRecordIndex(summaryIndex, 'inbox', 0);
- const records = rows.map(([, summary]) => summary).filter(Boolean);
const counts = {
- total: records.length,
+ total: 0,
classifying: 0,
filed: 0,
needs_review: 0,
@@ -864,9 +865,11 @@ export async function getInboxLogCounts() {
error: 0
};
- for (const record of records) {
- if (counts[record.status] !== undefined) {
- counts[record.status]++;
+ for (const [, summary] of rows) {
+ if (!summary) continue;
+ counts.total++;
+ if (counts[summary.status] !== undefined) {
+ counts[summary.status]++;
}
}
@@ -1067,7 +1070,11 @@ const linkSummaryIndex = createRecordIndex(projectLinkSummary);
// lets their frequent count reads reuse the same per-record invalidation
// machinery without parsing full record bodies in steady state.
const projectSummary = (record) => (record && !isTombstone(record)
- ? { status: record.status, isGitHubRepo: record.isGitHubRepo }
+ ? {
+ status: record.status,
+ isGitHubRepo: record.isGitHubRepo,
+ capturedAtMs: safeDate(record.capturedAt),
+ }
: null);
const summaryIndex = createRecordIndex(projectSummary);
diff --git a/server/services/brainStorage.test.js b/server/services/brainStorage.test.js
index 472d1dad45..28d5ebb8a2 100644
--- a/server/services/brainStorage.test.js
+++ b/server/services/brainStorage.test.js
@@ -664,6 +664,74 @@ describe('getLinksPage (paginated link reads, issue #3509)', () => {
});
});
+describe('getInboxLog (paginated inbox reads, issue #5440)', () => {
+ const stampCapturedAt = async (id, capturedAt) => {
+ const record = { ...(await rawRecord('inbox', id)) };
+ if (capturedAt === undefined) delete record.capturedAt;
+ else record.capturedAt = capturedAt;
+ await writeRawRecord('inbox', id, record);
+ brainStorage.invalidateAllCaches();
+ };
+
+ it('orders and filters deterministically, excluding tombstones and putting missing dates last', async () => {
+ const oldest = await brainStorage.createInboxLog({ capturedText: 'oldest', status: 'done' });
+ const tieA = await brainStorage.createInboxLog({ capturedText: 'tie-a', status: 'needs_review' });
+ const tieB = await brainStorage.createInboxLog({ capturedText: 'tie-b', status: 'needs_review' });
+ const newest = await brainStorage.createInboxLog({ capturedText: 'newest', status: 'filed' });
+ const missing = await brainStorage.createInboxLog({ capturedText: 'missing', status: 'needs_review' });
+ const removed = await brainStorage.createInboxLog({ capturedText: 'removed', status: 'needs_review' });
+
+ await stampCapturedAt(oldest.id, ISO('2098-01-01'));
+ await stampCapturedAt(tieA.id, ISO('2098-02-01'));
+ await stampCapturedAt(tieB.id, ISO('2098-02-01'));
+ await stampCapturedAt(newest.id, ISO('2098-03-01'));
+ await stampCapturedAt(missing.id, undefined);
+ await brainStorage.deleteInboxLog(removed.id);
+
+ const tieIds = [tieA.id, tieB.id].sort();
+ const expected = [newest.id, ...tieIds, oldest.id, missing.id];
+ const pageOne = await brainStorage.getInboxLog({ limit: 2, offset: 0 });
+ const pageTwo = await brainStorage.getInboxLog({ limit: 3, offset: 2 });
+ expect([...pageOne, ...pageTwo].map(({ id }) => id)).toEqual(expected);
+ expect(new Set([...pageOne, ...pageTwo].map(({ id }) => id)).size).toBe(expected.length);
+ expect((await brainStorage.getInboxLog({ status: 'needs_review' })).map(({ id }) => id))
+ .toEqual([...tieIds, missing.id]);
+ });
+
+ it('reads only the requested page bodies once the summary index is warm', async () => {
+ for (let i = 0; i < 8; i++) {
+ await brainStorage.createInboxLog({ capturedText: `page-${i}`, status: 'needs_review' });
+ }
+ await brainStorage.getInboxLog({ status: 'needs_review', limit: 1 });
+
+ readJSONFile.mockClear();
+ const page = await brainStorage.getInboxLog({ status: 'needs_review', limit: 1 });
+
+ expect(page).toHaveLength(1);
+ await brainStorage.getInboxLogCounts();
+ expect(readJSONFile).toHaveBeenCalledTimes(1);
+ });
+
+ it('invalidates the projection after status updates and deletes', async () => {
+ const moved = await brainStorage.createInboxLog({ capturedText: 'move me', status: 'needs_review' });
+ const removed = await brainStorage.createInboxLog({ capturedText: 'delete me', status: 'needs_review' });
+ await brainStorage.getInboxLog({ status: 'needs_review' });
+ const before = await brainStorage.getInboxLogCounts();
+
+ await brainStorage.updateInboxLog(moved.id, { status: 'done' });
+ await brainStorage.deleteInboxLog(removed.id);
+
+ expect((await brainStorage.getInboxLog({ status: 'needs_review' })).map(({ id }) => id))
+ .not.toEqual(expect.arrayContaining([moved.id, removed.id]));
+ expect((await brainStorage.getInboxLog({ status: 'done' })).map(({ id }) => id)).toContain(moved.id);
+ expect(await brainStorage.getInboxLogCounts()).toMatchObject({
+ total: before.total - 1,
+ needs_review: before.needs_review - 2,
+ done: before.done + 1,
+ });
+ });
+});
+
// Overwrite a record's stored JSON directly, bypassing every brainStorage write
// path (and therefore every brainEvents signal). Used to prove the live-id index
// is answering from memory rather than re-reading the file.