The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): recover root boundary from tapClientLookup races
Jakub Wolniewicz0405c2664560
diff --git a/apps/desktop/src/components/error-boundary.test.tsx b/apps/desktop/src/components/error-boundary.test.tsxnew file mode 100644index 00000000000..133de616878--- /dev/null+++ b/apps/desktop/src/components/error-boundary.test.tsx@@ -0,0 +1,108 @@+import { act, cleanup, render, screen } from '@testing-library/react'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++import { ErrorBoundary } from './error-boundary'++const TAP_LOOKUP_ERROR = new Error('tapClientLookup: Index 6 out of bounds (length: 2)')+const RELOAD_WINDOW = { name: 'Reload window', role: 'button' } as const++function makeBomb(box: { error: Error | null }) {+ return function Bomb() {+ if (box.error) {+ throw box.error+ }++ return <div>recovered</div>+ }+}++const recoveryWarningCount = (calls: unknown[][]) =>+ calls.filter(call => call.some(value => String(value).includes('auto-recovering from tapClientLookup'))).length++describe('ErrorBoundary tapClientLookup recovery', () => {+ beforeEach(() => {+ vi.useFakeTimers()+ vi.spyOn(console, 'error').mockImplementation(() => undefined)+ })++ afterEach(() => {+ cleanup()+ vi.useRealTimers()+ vi.restoreAllMocks()+ })++ it('recovers the root boundary after a transient lookup race clears', () => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const box: { error: Error | null } = { error: TAP_LOOKUP_ERROR }+ const Bomb = makeBomb(box)++ render(+ <ErrorBoundary label="root">+ <Bomb />+ </ErrorBoundary>+ )++ box.error = null+ act(() => vi.runOnlyPendingTimers())++ expect(screen.getByText('recovered')).toBeTruthy()+ expect(screen.queryByRole(RELOAD_WINDOW.role, { name: RELOAD_WINDOW.name })).toBeNull()+ expect(recoveryWarningCount(warnSpy.mock.calls)).toBe(1)+ })++ it('stops retrying a persistent lookup error after the recovery budget is exhausted', () => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const box: { error: Error | null } = { error: TAP_LOOKUP_ERROR }+ const Bomb = makeBomb(box)++ render(+ <ErrorBoundary label="root">+ <Bomb />+ </ErrorBoundary>+ )++ for (let attempt = 0; attempt < 4; attempt += 1) {+ act(() => vi.runOnlyPendingTimers())+ }++ expect(screen.getByRole(RELOAD_WINDOW.role, { name: RELOAD_WINDOW.name })).toBeTruthy()+ expect(recoveryWarningCount(warnSpy.mock.calls)).toBe(3)+ expect(vi.getTimerCount()).toBe(0)+ })++ it('does not auto-recover the same error in a scoped boundary', () => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const Bomb = makeBomb({ error: TAP_LOOKUP_ERROR })++ render(+ <ErrorBoundary fallback={() => <div>scoped fallback</div>} label="thread">+ <Bomb />+ </ErrorBoundary>+ )++ act(() => vi.runAllTimers())++ expect(screen.getByText('scoped fallback')).toBeTruthy()+ expect(recoveryWarningCount(warnSpy.mock.calls)).toBe(0)+ })++ it.each([+ ['a renamed lookup error', new Error('useClientLookup: Index 6 out of bounds (length: 2)')],+ ['an unrelated render error', new Error('some unrelated application error')]+ ])('does not auto-recover %s at root', (_label, error) => {+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const box: { error: Error | null } = { error }+ const Bomb = makeBomb(box)++ render(+ <ErrorBoundary label="root">+ <Bomb />+ </ErrorBoundary>+ )++ act(() => vi.runAllTimers())++ expect(screen.getByRole(RELOAD_WINDOW.role, { name: RELOAD_WINDOW.name })).toBeTruthy()+ expect(recoveryWarningCount(warnSpy.mock.calls)).toBe(0)+ })+})diff --git a/apps/desktop/src/components/error-boundary.tsx b/apps/desktop/src/components/error-boundary.tsxindex f89f1d50673..10fd0d614c0 100644--- a/apps/desktop/src/components/error-boundary.tsx+++ b/apps/desktop/src/components/error-boundary.tsx@@ -20,8 +20,20 @@ interface ErrorBoundaryState {error: Error | null}+// Some assistant-ui lookup races escape the message-local boundary and reach+// the root. Retry only that exact transient error class, never arbitrary render+// failures, and cap retries so a persistent failure still exposes the fallback.+const TAP_CLIENT_LOOKUP_ERROR = /^tapClientLookup: Index \d+\s+out of bounds \(length:\s*\d+\)$/i+const MAX_AUTO_RECOVERIES = 3+const AUTO_RECOVERY_WINDOW_MS = 5_000++const isTransientTapClientLookupError = (error: Error): boolean => TAP_CLIENT_LOOKUP_ERROR.test(error.message)+export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {state: ErrorBoundaryState = { error: null }+ private autoRecoveryCount = 0+ private autoRecoveryTimer: number | null = null+ private autoRecoveryWindowStart = 0static getDerivedStateFromError(error: Error): ErrorBoundaryState {return { error }@@ -31,9 +43,51 @@ export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryStconst tag = this.props.label ? `[error-boundary:${this.props.label}]` : '[error-boundary]'console.error(tag, error, info.componentStack)this.props.onError?.(error, info)++ if (this.props.label === 'root' && isTransientTapClientLookupError(error) && this.takeAutoRecoveryAttempt()) {+ console.warn(`${tag} auto-recovering from tapClientLookup render race`, error.message)+ this.scheduleAutoRecovery()+ }+ }++ componentWillUnmount() {+ this.clearAutoRecoveryTimer()}reset = () => {+ this.clearAutoRecoveryTimer()+ this.autoRecoveryCount = 0+ this.autoRecoveryWindowStart = 0+ this.setState({ error: null })+ }++ private takeAutoRecoveryAttempt(): boolean {+ const now = Date.now()++ if (now - this.autoRecoveryWindowStart > AUTO_RECOVERY_WINDOW_MS) {+ this.autoRecoveryWindowStart = now+ this.autoRecoveryCount = 0+ }++ this.autoRecoveryCount += 1++ return this.autoRecoveryCount <= MAX_AUTO_RECOVERIES+ }++ private clearAutoRecoveryTimer() {+ if (this.autoRecoveryTimer !== null) {+ window.clearTimeout(this.autoRecoveryTimer)+ this.autoRecoveryTimer = null+ }+ }++ private scheduleAutoRecovery() {+ this.clearAutoRecoveryTimer()+ this.autoRecoveryTimer = window.setTimeout(this.autoRecover, 0)+ }++ private autoRecover = () => {+ this.autoRecoveryTimer = nullthis.setState({ error: null })}