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([issue-832]): push review-queue draft filter into listDrafts

[._.]/ Adam Eivy61992148623a
diff --git a/server/services/messageDrafts.js b/server/services/messageDrafts.js
index 977ae6cd60..83afa57bd7 100644
--- a/server/services/messageDrafts.js
+++ b/server/services/messageDrafts.js
@@ -20,7 +20,15 @@ async function saveDrafts(drafts) {
export async function listDrafts(filters = {}) {
let drafts = await loadDrafts();
if (filters.accountId) drafts = drafts.filter(d => d.accountId === filters.accountId);
- if (filters.status) drafts = drafts.filter(d => d.status === filters.status);
+ if (filters.status) {
+ // `status` accepts a single value or an array (OR filter), so callers like
+ // the review-queue aggregator can push a multi-status query down to the
+ // data layer instead of loading every draft and filtering in memory.
+ // An explicit empty array means "match no status" → returns nothing,
+ // rather than collapsing back to "no filter".
+ const wanted = Array.isArray(filters.status) ? filters.status : [filters.status];
+ drafts = drafts.filter(d => wanted.includes(d.status));
+ }
return drafts.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
}
diff --git a/server/services/messageDrafts.test.js b/server/services/messageDrafts.test.js
new file mode 100644
index 0000000000..4e0f5ea696
--- /dev/null
+++ b/server/services/messageDrafts.test.js
@@ -0,0 +1,89 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+const fileStore = new Map();
+
+vi.mock('../lib/uuid.js', () => {
+ let counter = 0;
+ return { v4: () => `uuid-${++counter}` };
+});
+
+vi.mock('../lib/fileUtils.js', () => ({
+ tryReadFile: vi.fn(async (path) => fileStore.has(path) ? fileStore.get(path) : null),
+ atomicWrite: vi.fn(async (path, data) => {
+ fileStore.set(path, typeof data === 'string' ? data : JSON.stringify(data, null, 2));
+ }),
+ ensureDir: vi.fn().mockResolvedValue(undefined),
+ PATHS: { messages: '/mock/messages' },
+ safeJSONParse: (s, fallback) => { try { return JSON.parse(s); } catch { return fallback; } }
+}));
+
+const { listDrafts, createDraft } = await import('./messageDrafts.js');
+
+// Seed the file store directly so we control accountId/status without relying on
+// createDraft (which always stamps status 'draft').
+function seed(drafts) {
+ fileStore.set('/mock/messages/drafts.json', JSON.stringify(drafts));
+}
+
+describe('messageDrafts.listDrafts', () => {
+ beforeEach(() => {
+ fileStore.clear();
+ });
+
+ it('returns an empty array when no drafts file exists', async () => {
+ expect(await listDrafts()).toEqual([]);
+ });
+
+ it('returns all drafts sorted by createdAt descending when no filter', async () => {
+ seed([
+ { id: 'a', status: 'draft', createdAt: '2026-01-01T00:00:00.000Z' },
+ { id: 'b', status: 'sent', createdAt: '2026-03-01T00:00:00.000Z' }
+ ]);
+ const result = await listDrafts();
+ expect(result.map(d => d.id)).toEqual(['b', 'a']);
+ });
+
+ it('filters by a single status string', async () => {
+ seed([
+ { id: 'a', status: 'draft', createdAt: '2026-01-01T00:00:00.000Z' },
+ { id: 'b', status: 'sent', createdAt: '2026-02-01T00:00:00.000Z' },
+ { id: 'c', status: 'draft', createdAt: '2026-03-01T00:00:00.000Z' }
+ ]);
+ const result = await listDrafts({ status: 'draft' });
+ expect(result.map(d => d.id)).toEqual(['c', 'a']);
+ });
+
+ it('filters by an array of statuses (OR)', async () => {
+ seed([
+ { id: 'a', status: 'draft', createdAt: '2026-01-01T00:00:00.000Z' },
+ { id: 'b', status: 'sent', createdAt: '2026-02-01T00:00:00.000Z' },
+ { id: 'c', status: 'pending_review', createdAt: '2026-03-01T00:00:00.000Z' }
+ ]);
+ const result = await listDrafts({ status: ['draft', 'pending_review'] });
+ expect(result.map(d => d.id)).toEqual(['c', 'a']);
+ });
+
+ it('treats an explicit empty status array as "match nothing"', async () => {
+ seed([
+ { id: 'a', status: 'draft', createdAt: '2026-01-01T00:00:00.000Z' },
+ { id: 'b', status: 'sent', createdAt: '2026-02-01T00:00:00.000Z' }
+ ]);
+ expect(await listDrafts({ status: [] })).toEqual([]);
+ });
+
+ it('combines accountId and a multi-status filter', async () => {
+ seed([
+ { id: 'a', accountId: 'acct-1', status: 'draft', createdAt: '2026-01-01T00:00:00.000Z' },
+ { id: 'b', accountId: 'acct-2', status: 'draft', createdAt: '2026-02-01T00:00:00.000Z' },
+ { id: 'c', accountId: 'acct-1', status: 'sent', createdAt: '2026-03-01T00:00:00.000Z' }
+ ]);
+ const result = await listDrafts({ accountId: 'acct-1', status: ['draft', 'pending_review'] });
+ expect(result.map(d => d.id)).toEqual(['a']);
+ });
+
+ it('round-trips a created draft and finds it by status', async () => {
+ const draft = await createDraft({ accountId: 'acct-1', subject: 'hi' });
+ const result = await listDrafts({ status: ['draft', 'pending_review'] });
+ expect(result.map(d => d.id)).toEqual([draft.id]);
+ });
+});
diff --git a/server/services/reviewQueue.js b/server/services/reviewQueue.js
index 5e6c514b63..66f6dea043 100644
--- a/server/services/reviewQueue.js
+++ b/server/services/reviewQueue.js
@@ -123,10 +123,10 @@ const PRODUCERS = [
async gather() {
// Drafts the user (or an AI) prepared that haven't been sent yet. Today
// messageDrafts only emits 'draft' (vs 'approved'); 'pending_review' is
- // matched ahead of the producer adding it (see #832) so this needs no
- // change when it lands.
- const drafts = await messageDrafts.listDrafts();
- return drafts.filter(d => d.status === 'draft' || d.status === 'pending_review');
+ // matched ahead of the producer adding it. The multi-status filter is
+ // pushed down to listDrafts so the whole store isn't loaded + filtered
+ // in memory on every Review Hub load.
+ return messageDrafts.listDrafts({ status: ['draft', 'pending_review'] });
},
map(draft) {
return {
diff --git a/server/services/reviewQueue.test.js b/server/services/reviewQueue.test.js
index b49c80e8a0..c011ad73c1 100644
--- a/server/services/reviewQueue.test.js
+++ b/server/services/reviewQueue.test.js
@@ -73,11 +73,18 @@ describe('reviewQueue.buildQueue', () => {
it('surfaces drafts and CoS approvals from their producers', async () => {
cosTaskStore.getCosTasks.mockResolvedValue({ awaitingApproval: [{ id: 'sys-1', description: 'approve me', priority: 'HIGH', createdAt: '2026-06-03T09:00:00.000Z' }] });
- messageDrafts.listDrafts.mockResolvedValue([
+ // The producer pushes a multi-status filter down to listDrafts, so the mock
+ // honors it the way the real implementation does (drops the 'sent' draft).
+ const allDrafts = [
{ id: 'd1', status: 'draft', subject: 'unsent', updatedAt: '2026-06-03T08:00:00.000Z' },
{ id: 'd2', status: 'sent', subject: 'gone' }
- ]);
+ ];
+ messageDrafts.listDrafts.mockImplementation(({ status } = {}) => {
+ const wanted = Array.isArray(status) ? status : status ? [status] : null;
+ return Promise.resolve(wanted ? allDrafts.filter(d => wanted.includes(d.status)) : allDrafts);
+ });
const queue = await buildQueue();
+ expect(messageDrafts.listDrafts).toHaveBeenCalledWith({ status: ['draft', 'pending_review'] });
expect(queue.items.find(i => i.id === 'cos:sys-1')).toMatchObject({ severity: 'high', drillTo: '/cos/tasks' });
const draftRows = queue.items.filter(i => i.source === 'drafts');
expect(draftRows).toHaveLength(1);