Three interacting bugs caused "Failed to initialize chat" toast spam and prevented fresh session creation: 1. Race condition in handleCreateFreshSession — setting chatMode/ selectedAgentId before startFreshSession triggered the session-init useEffect, which called switchSession and resumed the old session instead of creating a new one. Fixed with skipNextSessionInitRef. 2. Infinite retry loop — shouldRetrySessionInit retried indefinitely when initializeSession failed, producing unbounded toast spam. Fixed with a 3-retry cap (initRetryCountRef). 3. Cascading re-renders — switchSession depended on activeSession in its closure, getting a new identity on every state change and re-triggering the consumer's useEffect. Fixed by reading activeSession from a ref instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
25 KiB
25 KiB