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-709]): cache the review-queue health sweep; single-pass counts

[._.]/ Adam Eivy514b6cf2db47
diff --git a/server/services/reviewQueue.js b/server/services/reviewQueue.js
index 030a612899..82a3397efa 100644
--- a/server/services/reviewQueue.js
+++ b/server/services/reviewQueue.js
@@ -29,6 +29,26 @@ import * as backup from './backup.js';
// "+N more in <domain>" affordance via the per-source `total` vs `items.length`.
const PER_SOURCE_LIMIT = 25;
+// generateAlerts() runs a full system-health sweep (CPU/disk/PM2/goals/usage),
+// so cache it briefly — the Review Hub can be polled, and a stale-by-seconds
+// alert list is fine here (the dedicated health views read it live).
+const ALERTS_TTL_MS = 30_000;
+let alertsCache = { data: null, timestamp: 0 };
+
+async function getAlertsCached() {
+ if (alertsCache.data && (Date.now() - alertsCache.timestamp) < ALERTS_TTL_MS) {
+ return alertsCache.data;
+ }
+ const result = await proactiveAlerts.generateAlerts();
+ alertsCache = { data: result, timestamp: Date.now() };
+ return result;
+}
+
+// Test seam: drop the alerts cache so a suite can assert fresh per-case data.
+export function __resetAlertsCache() {
+ alertsCache = { data: null, timestamp: 0 };
+}
+
/**
* Producer registry. Each entry knows how to gather its raw items and map one
* into the normalized queue shape. `gather` returns the raw list (already
@@ -123,7 +143,7 @@ const PRODUCERS = [
drillTo: '/system-health',
async gather() {
// System/health alerts; only surface the ones worth interrupting for.
- const { alerts = [] } = await proactiveAlerts.generateAlerts();
+ const { alerts = [] } = await getAlertsCached();
return alerts.filter(a => a.severity === 'critical' || a.severity === 'high');
},
map(alert) {
@@ -207,13 +227,15 @@ export async function buildQueue() {
sources[r.source] = { label: r.label, total: r.total, shown: r.items.length, error: r.error };
}
+ const counts = { total: items.length, critical: 0, high: 0 };
+ for (const i of items) {
+ if (i.severity === 'critical') counts.critical++;
+ else if (i.severity === 'high') counts.high++;
+ }
+
return {
items,
- counts: {
- total: items.length,
- critical: items.filter(i => i.severity === 'critical').length,
- high: items.filter(i => i.severity === 'high').length
- },
+ counts,
sources,
generatedAt: new Date().toISOString()
};
diff --git a/server/services/reviewQueue.test.js b/server/services/reviewQueue.test.js
index 964aefab34..654cd8dca0 100644
--- a/server/services/reviewQueue.test.js
+++ b/server/services/reviewQueue.test.js
@@ -16,7 +16,7 @@ vi.mock('./messageDrafts.js', () => messageDrafts);
vi.mock('./proactiveAlerts.js', () => proactiveAlerts);
vi.mock('./backup.js', () => backup);
-const { buildQueue } = await import('./reviewQueue.js');
+const { buildQueue, __resetAlertsCache } = await import('./reviewQueue.js');
// Default: every producer returns "nothing needs attention".
function resetEmpty() {
@@ -32,6 +32,9 @@ describe('reviewQueue.buildQueue', () => {
beforeEach(() => {
vi.clearAllMocks();
resetEmpty();
+ // The alerts sweep is cached with a TTL; clear it so each case sees its
+ // own generateAlerts mock rather than a prior case's cached result.
+ __resetAlertsCache();
});
it('returns an empty queue when nothing needs attention', async () => {
@@ -103,6 +106,15 @@ describe('reviewQueue.buildQueue', () => {
expect(queue.counts.critical).toBe(1);
});
+ it('treats a producer returning null/non-array as empty', async () => {
+ brain.getInboxLog.mockResolvedValue(null);
+ askConversations.listConversations.mockResolvedValue(undefined);
+ const queue = await buildQueue();
+ expect(queue.items).toEqual([]);
+ expect(queue.sources.brain.total).toBe(0);
+ expect(queue.sources.brain.error).toBeNull();
+ });
+
it('degrades a failing producer to empty without sinking the queue', async () => {
brain.getInboxLog.mockRejectedValue(new Error('inbox boom'));
messageDrafts.listDrafts.mockResolvedValue([{ id: 'd1', status: 'draft', subject: 'still here' }]);