mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
a38a3a7e92
useCopilotReadable published its context in a useEffect. React flushes passive effects child-first in tree order, so a consumer mounted before the readable runs its own useEffect against an empty context store. That is the cross-page-navigation failure: a page mounts the chat and its readable-publishing components in one commit, the chat's connect effect fires first, and the connect request carries no context. Register in useLayoutEffect instead. Layout effects run during commit, ahead of every passive effect regardless of tree order, which closes the window. Register and cleanup stay in the one effect, so both sides remain in the same phase. This matches the v2 siblings useAgentContext and useFrontendTool, the latter fixed the same way inf9b306aa4e. Adds a regression test that mounts the consumer FIRST -- mounting it second passes with either hook and proves nothing. The test fails on the unmodified hook with "expected [] to include 'employees'". Completes the half of mxmzb's #4259 thatf9b306aa4edid not cover. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>