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([issue-2737]): coalesce silent-op AI error toasts per provider

[._.]/ Adam Eivy6e90bfdb4cb3
diff --git a/client/src/hooks/useAIStatusNotifications.js b/client/src/hooks/useAIStatusNotifications.js
index 18573e7d75..65e993b54e 100644
--- a/client/src/hooks/useAIStatusNotifications.js
+++ b/client/src/hooks/useAIStatusNotifications.js
@@ -22,13 +22,30 @@ import socket from '../services/socket';
* errors, or calls that take longer than a couple seconds.
* - Non-silent ops render from the start so the user sees feedback for
* explicit actions (Generate Summary, etc.) even on fast calls.
+ * - Silent-op *errors* are coalesced per-provider within a short rolling
+ * window (see COALESCE below). An unattended background job that fans out
+ * one AI call per record (e.g. a multi-goal check-in via `Promise.all` in
+ * server/services/goalCheckIn.js) can produce N systematic failures at
+ * once — each with a unique op id, so Toast's content dedupe can't collapse
+ * them. Instead of N stacked red toasts the user never watched start, they
+ * get ONE toast that counts the failures. User-triggered ops (those that
+ * passed `op` → not silent) still toast immediately, individually, with the
+ * provider's real reason — that behavior is load-bearing for #2733's
+ * single-voice Go-deeper design and is intentionally left untouched.
*/
export function useAIStatusNotifications() {
// Per-op state: { silent, opened, slowTimer? }
const opsRef = useRef(new Map());
+ // Per-provider silent-error coalescing: providerKey -> { toastId, count, timer }
+ const silentErrorsRef = useRef(new Map());
useEffect(() => {
const SLOW_CALL_MS = 2500;
+ // Rolling window over which concurrent silent-op failures from the same
+ // provider collapse into a single counted toast. A burst from `Promise.all`
+ // arrives near-simultaneously, well inside this window; genuinely
+ // spaced-out failures (> window apart) each show their real reason again.
+ const SILENT_ERROR_WINDOW_MS = 4000;
const phaseIcon = {
start: '🤖',
@@ -38,6 +55,33 @@ export function useAIStatusNotifications() {
error: '✕'
};
+ // Collapse a burst of silent-op failures from one provider into a single
+ // toast keyed by provider. The first failure in the window shows the real
+ // reason; subsequent failures update the SAME toast with a running count.
+ const coalesceSilentError = (event) => {
+ const key = event.providerId || event.providerName || '__unknown_provider__';
+ const providerLabel = event.providerName || event.providerId || 'AI provider';
+ const entry = silentErrorsRef.current.get(key);
+
+ if (!entry) {
+ const toastId = `ai-silent-error::${key}`;
+ const fresh = { toastId, count: 1, timer: undefined };
+ fresh.timer = setTimeout(() => silentErrorsRef.current.delete(key), SILENT_ERROR_WINDOW_MS);
+ silentErrorsRef.current.set(key, fresh);
+ toast.error(event.message || `${providerLabel} background AI call failed`, {
+ id: toastId, duration: 6000, icon: '✕'
+ });
+ return;
+ }
+
+ entry.count += 1;
+ if (entry.timer) clearTimeout(entry.timer);
+ entry.timer = setTimeout(() => silentErrorsRef.current.delete(key), SILENT_ERROR_WINDOW_MS);
+ toast.error(`${providerLabel} failed on ${entry.count} background AI calls`, {
+ id: entry.toastId, duration: 6000, icon: '✕'
+ });
+ };
+
const showLoading = (event) => {
const state = opsRef.current.get(event.id) || { silent: false };
// Once a toast is opened, the deferred slow-call timer is no longer needed —
@@ -90,9 +134,16 @@ export function useAIStatusNotifications() {
if (event.phase === 'error') {
if (state.slowTimer) clearTimeout(state.slowTimer);
- // Always show errors, even for silent ops — failures matter.
- toast.error(event.message || 'AI call failed', { id: event.id, duration: 6000, icon: '✕' });
opsRef.current.delete(event.id);
+ // User-triggered ops toast immediately, individually, with the real
+ // reason — failures the user is waiting on matter and must not be
+ // aggregated away. Silent-op failures (unattended background jobs) are
+ // coalesced per-provider so a systematic failure yields one toast, not N.
+ if (state.silent || event.silent) {
+ coalesceSilentError(event);
+ } else {
+ toast.error(event.message || 'AI call failed', { id: event.id, duration: 6000, icon: '✕' });
+ }
}
};
@@ -103,6 +154,10 @@ export function useAIStatusNotifications() {
if (s.slowTimer) clearTimeout(s.slowTimer);
}
opsRef.current.clear();
+ for (const e of silentErrorsRef.current.values()) {
+ if (e.timer) clearTimeout(e.timer);
+ }
+ silentErrorsRef.current.clear();
};
}, []);
}
diff --git a/client/src/hooks/useAIStatusNotifications.test.jsx b/client/src/hooks/useAIStatusNotifications.test.jsx
new file mode 100644
index 0000000000..43d5ed49c3
--- /dev/null
+++ b/client/src/hooks/useAIStatusNotifications.test.jsx
@@ -0,0 +1,128 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { renderHook, act } from '@testing-library/react';
+
+// Capture ai:status handlers registered on the socket so tests can drive events.
+const handlers = new Set();
+vi.mock('../services/socket', () => ({
+ default: {
+ on: (evt, fn) => { if (evt === 'ai:status') handlers.add(fn); },
+ off: (evt, fn) => { if (evt === 'ai:status') handlers.delete(fn); },
+ },
+}));
+
+// Record every toast call so we can assert count + message + id.
+const toastCalls = [];
+const toastFn = Object.assign(
+ (...args) => { toastCalls.push({ type: 'default', args }); },
+ {
+ error: (message, opts) => { toastCalls.push({ type: 'error', message, opts }); },
+ success: (message, opts) => { toastCalls.push({ type: 'success', message, opts }); },
+ loading: (message, opts) => { toastCalls.push({ type: 'loading', message, opts }); },
+ dismiss: () => {},
+ }
+);
+vi.mock('../components/ui/Toast', () => ({ default: toastFn }));
+
+const { useAIStatusNotifications } = await import('./useAIStatusNotifications.js');
+
+function emit(event) {
+ for (const fn of handlers) fn(event);
+}
+
+const errorCalls = () => toastCalls.filter(c => c.type === 'error');
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ handlers.clear();
+ toastCalls.length = 0;
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+});
+
+describe('useAIStatusNotifications — silent-op error coalescing', () => {
+ it('collapses a burst of silent-op failures from one provider into ONE counted toast', () => {
+ renderHook(() => useAIStatusNotifications());
+
+ // Simulate an unattended multi-goal check-in: 5 concurrent silent calls to
+ // the same failing provider, each with a unique op id (as startAIOp mints).
+ act(() => {
+ for (let i = 0; i < 5; i++) {
+ const id = `op-${i}`;
+ emit({ id, phase: 'start', silent: true, providerId: 'prov-1', providerName: 'Example Provider' });
+ emit({ id, phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'HTTP 401 Unauthorized' });
+ }
+ });
+
+ // Every silent error routes to the SAME provider-keyed toast id — one toast,
+ // not five. The first shows the real reason; later ones update to a count.
+ const errs = errorCalls();
+ const uniqueIds = new Set(errs.map(c => c.opts?.id));
+ expect(uniqueIds.size).toBe(1);
+ expect([...uniqueIds][0]).toBe('ai-silent-error::prov-1');
+
+ const last = errs[errs.length - 1];
+ expect(last.message).toBe('Example Provider failed on 5 background AI calls');
+
+ // The first failure surfaced the provider's real reason.
+ expect(errs[0].message).toBe('HTTP 401 Unauthorized');
+ });
+
+ it('keeps user-triggered (non-silent) failures immediate, individual, and with the real reason', () => {
+ renderHook(() => useAIStatusNotifications());
+
+ act(() => {
+ const a = 'op-user-a';
+ emit({ id: a, phase: 'start', silent: false, providerId: 'prov-1', providerName: 'Example Provider' });
+ emit({ id: a, phase: 'error', silent: false, providerId: 'prov-1', providerName: 'Example Provider', message: 'HTTP 500 boom' });
+
+ const b = 'op-user-b';
+ emit({ id: b, phase: 'start', silent: false, providerId: 'prov-1', providerName: 'Example Provider' });
+ emit({ id: b, phase: 'error', silent: false, providerId: 'prov-1', providerName: 'Example Provider', message: 'timeout' });
+ });
+
+ const errs = errorCalls();
+ // Two distinct failures → two distinct toasts, each keyed by its own op id.
+ expect(errs).toHaveLength(2);
+ expect(errs[0].opts?.id).toBe('op-user-a');
+ expect(errs[0].message).toBe('HTTP 500 boom');
+ expect(errs[1].opts?.id).toBe('op-user-b');
+ expect(errs[1].message).toBe('timeout');
+ });
+
+ it('coalesces silent failures per-provider, not across providers', () => {
+ renderHook(() => useAIStatusNotifications());
+
+ act(() => {
+ emit({ id: 'x1', phase: 'error', silent: true, providerId: 'prov-a', providerName: 'Provider A', message: 'a fail' });
+ emit({ id: 'x2', phase: 'error', silent: true, providerId: 'prov-b', providerName: 'Provider B', message: 'b fail' });
+ emit({ id: 'x3', phase: 'error', silent: true, providerId: 'prov-a', providerName: 'Provider A', message: 'a fail again' });
+ });
+
+ const errs = errorCalls();
+ const ids = new Set(errs.map(c => c.opts?.id));
+ expect(ids).toEqual(new Set(['ai-silent-error::prov-a', 'ai-silent-error::prov-b']));
+
+ const provA = errs.filter(c => c.opts?.id === 'ai-silent-error::prov-a');
+ expect(provA[provA.length - 1].message).toBe('Provider A failed on 2 background AI calls');
+ });
+
+ it('starts a fresh coalescing window after the previous one lapses', () => {
+ renderHook(() => useAIStatusNotifications());
+
+ act(() => {
+ emit({ id: 'g1', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'first reason' });
+ });
+ // Advance past the rolling window so the entry is cleaned up.
+ act(() => { vi.advanceTimersByTime(5000); });
+ act(() => {
+ emit({ id: 'g2', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'second reason' });
+ });
+
+ const errs = errorCalls();
+ // Both are the first-in-window, so both show their real reason (count 1).
+ expect(errs[0].message).toBe('first reason');
+ expect(errs[errs.length - 1].message).toBe('second reason');
+ });
+});