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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): coalesce slow silent failures, keep reason, unique window ids
[._.]/ Adam Eivy7cf9d9da01a0
diff --git a/client/src/hooks/useAIStatusNotifications.js b/client/src/hooks/useAIStatusNotifications.jsindex b51002238b..462cd3d027 100644--- a/client/src/hooks/useAIStatusNotifications.js+++ b/client/src/hooks/useAIStatusNotifications.js@@ -36,8 +36,12 @@ import socket from '../services/socket';export function useAIStatusNotifications() {// Per-op state: { silent, opened, slowTimer? }const opsRef = useRef(new Map());- // Per-provider silent-error coalescing: providerKey -> { toastId, count, timer }+ // Per-provider silent-error coalescing: providerKey -> { toastId, count, firstReason, timer }const silentErrorsRef = useRef(new Map());+ // Monotonic window counter so each fresh coalescing window gets a unique toast+ // id (see coalesceSilentError) — a reused id would let a prior window's pending+ // auto-dismiss timer remove a successor window's toast.+ const windowSeqRef = useRef(0);useEffect(() => {const SLOW_CALL_MS = 2500;@@ -57,27 +61,36 @@ export function useAIStatusNotifications() {// 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.+ // reason; subsequent failures update the SAME toast with a running count,+ // keeping that first reason visible so the toast stays actionable.const coalesceSilentError = (event) => {const key = event.providerId || event.providerName || '__unknown_provider__';const providerLabel = event.providerName || event.providerId || 'AI provider';+ const reason = event.message || `${providerLabel} background AI call failed`;const entry = silentErrorsRef.current.get(key);if (!entry) {- const toastId = `ai-silent-error::${key}`;- const fresh = { toastId, count: 1, timer: undefined };+ // Fresh window. Give the toast a window-unique id: Toast schedules an+ // independent auto-dismiss timer per add() and never cancels it, so a+ // stable per-provider id would let a lapsed window's pending dismissal+ // remove the NEXT window's toast a second or two after it appeared.+ windowSeqRef.current += 1;+ const toastId = `ai-silent-error::${key}::${windowSeqRef.current}`;+ const fresh = { toastId, count: 1, firstReason: reason, 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: '✕'- });+ toast.error(reason, { 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`, {+ // Retain the first real reason — the count alone doesn't tell the user+ // whether it was an expired key, a timeout, or a bad response, and these+ // events can all land before the browser paints, so the counted toast is+ // often the only one the user ever sees.+ toast.error(`${providerLabel} failed on ${entry.count} background AI calls — ${entry.firstReason}`, {id: entry.toastId, duration: 6000, icon: '✕'});};@@ -135,14 +148,16 @@ export function useAIStatusNotifications() {if (event.phase === 'error') {if (state.slowTimer) clearTimeout(state.slowTimer);opsRef.current.delete(event.id);- // Coalesce ONLY silent ops that never surfaced their own toast — the- // fast-fail fan-out case an unattended job produces. A silent op that- // already opened a loading toast (a slow call whose spinner the user is- // now watching, id === event.id) must update THAT toast in place, or the- // Infinity-duration loading toast is orphaned and spins forever. A- // user-triggered op always toasts immediately, individually, with the- // real reason — that's load-bearing and must not be aggregated away.- if ((state.silent || event.silent) && !state.opened) {+ // Silent-op failures (unattended background jobs) coalesce per-provider+ // so a systematic failure yields one counted toast, not N — and this+ // must hold for SLOW failures too (an unreachable endpoint / timeout,+ // where every parallel op has already opened its own spinner). Dismiss+ // that orphan-prone Infinity-duration spinner first, then coalesce, so+ // the goal-check-in flood collapses whether the provider fails fast or+ // slow. A user-triggered op always toasts immediately, individually,+ // with the real reason — load-bearing and never aggregated away.+ if (state.silent || event.silent) {+ if (state.opened) toast.dismiss(event.id);coalesceSilentError(event);} else {toast.error(event.message || 'AI call failed', { id: event.id, duration: 6000, icon: '✕' });diff --git a/client/src/hooks/useAIStatusNotifications.test.jsx b/client/src/hooks/useAIStatusNotifications.test.jsxindex b29722af3a..32f1ed31cb 100644--- a/client/src/hooks/useAIStatusNotifications.test.jsx+++ b/client/src/hooks/useAIStatusNotifications.test.jsx@@ -18,7 +18,7 @@ const toastFn = Object.assign(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: () => {},+ dismiss: (id) => { toastCalls.push({ type: 'dismiss', id }); },});vi.mock('../components/ui/Toast', () => ({ default: toastFn }));@@ -30,6 +30,7 @@ function emit(event) {}const errorCalls = () => toastCalls.filter(c => c.type === 'error');+const dismissCalls = () => toastCalls.filter(c => c.type === 'dismiss');beforeEach(() => {vi.useFakeTimers();@@ -60,10 +61,13 @@ describe('useAIStatusNotifications — silent-op error coalescing', () => {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');+ expect([...uniqueIds][0]).toMatch(/^ai-silent-error::prov-1::/);+ // The counted toast keeps the first real reason visible — since these+ // events can all land before a paint, the counted toast is often the only+ // one the user ever sees, so a bare count would strip the actionable reason.const last = errs[errs.length - 1];- expect(last.message).toBe('Example Provider failed on 5 background AI calls');+ expect(last.message).toBe('Example Provider failed on 5 background AI calls — HTTP 401 Unauthorized');// The first failure surfaced the provider's real reason.expect(errs[0].message).toBe('HTTP 401 Unauthorized');@@ -101,35 +105,61 @@ describe('useAIStatusNotifications — silent-op error coalescing', () => {});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');+ // Ids are provider-keyed (with a window-seq suffix): two distinct providers →+ // two distinct toasts, and prov-a's two failures share one id.+ const provAIds = new Set(errs.filter(c => /::prov-a::/.test(c.opts?.id)).map(c => c.opts?.id));+ const provBIds = new Set(errs.filter(c => /::prov-b::/.test(c.opts?.id)).map(c => c.opts?.id));+ expect(provAIds.size).toBe(1);+ expect(provBIds.size).toBe(1);++ const provA = errs.filter(c => /::prov-a::/.test(c.opts?.id));+ expect(provA[provA.length - 1].message).toBe('Provider A failed on 2 background AI calls — a fail');});- it('does NOT coalesce a silent op that already surfaced its own (slow-call) toast — it updates that toast in place', () => {+ it('coalesces SLOW silent failures too — dismisses each orphan-prone spinner, collapses to one counted toast', () => {renderHook(() => useAIStatusNotifications());+ // Two silent ops both cross the 2.5s slow threshold, so each opens its own+ // Infinity-duration loading spinner (a timeout / unreachable-endpoint burst).act(() => {- emit({ id: 'slow-op', phase: 'start', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'Calling…' });+ emit({ id: 'slow-a', phase: 'start', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'Calling…' });+ emit({ id: 'slow-b', phase: 'start', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'Calling…' });});- // Cross the slow-call threshold so the deferred toast opens under id 'slow-op'.act(() => { vi.advanceTimersByTime(3000); });- const loadingCall = toastCalls.find(c => c.type === 'loading');- expect(loadingCall?.opts?.id).toBe('slow-op');+ expect(toastCalls.filter(c => c.type === 'loading').map(c => c.opts?.id).sort()).toEqual(['slow-a', 'slow-b']);act(() => {- emit({ id: 'slow-op', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'HTTP 401 Unauthorized' });+ emit({ id: 'slow-a', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'endpoint timeout' });+ emit({ id: 'slow-b', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'endpoint timeout' });});+ // Both orphan-prone spinners are dismissed by id (else they spin forever)...+ expect(dismissCalls().map(c => c.id).sort()).toEqual(['slow-a', 'slow-b']);+ // ...and the two slow failures collapse into ONE provider-keyed counted toast,+ // not two stacked red toasts — this is the exact flood the issue is about.const errs = errorCalls();- expect(errs).toHaveLength(1);- // Updates the already-visible spinner in place (id === op id), NOT the- // provider-keyed coalescing toast — otherwise the Infinity-duration loading- // toast would be orphaned and spin forever.- expect(errs[0].opts?.id).toBe('slow-op');- expect(errs[0].message).toBe('HTTP 401 Unauthorized');+ const ids = new Set(errs.map(c => c.opts?.id));+ expect(ids.size).toBe(1);+ expect(errs[errs.length - 1].message).toBe('Example Provider failed on 2 background AI calls — endpoint timeout');+ });++ it('gives each fresh window a distinct toast id so a lapsed window cannot dismiss its successor', () => {+ renderHook(() => useAIStatusNotifications());++ act(() => {+ emit({ id: 'w1', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'first' });+ });+ act(() => { vi.advanceTimersByTime(5000); }); // window lapses+ act(() => {+ emit({ id: 'w2', phase: 'error', silent: true, providerId: 'prov-1', providerName: 'Example Provider', message: 'second' });+ });++ const errs = errorCalls();+ // Same provider, two windows → two DIFFERENT toast ids (Toast never cancels a+ // prior add()'s auto-dismiss timer, so a reused id would let window 1's+ // pending dismissal remove window 2's toast).+ expect(errs[0].opts?.id).not.toBe(errs[errs.length - 1].opts?.id);+ expect(errs[0].opts?.id).toMatch(/^ai-silent-error::prov-1::/);});it('starts a fresh coalescing window after the previous one lapses', () => {