Repository navigation
fix(react-core): expose isReady from useAgent to guard agent subscriptions (#5000) - #6041
Merged
BenTaylorDev merged 1 commit intoJul 20, 2026
Merged
Conversation
…target the real agent (CopilotKit#5000) `useAgent` always returns a fully-constructed `AbstractAgent`: a provisional stand-in while the runtime is still connecting (or in an error state), swapped for the real agent once the `/info` sync resolves. The returned type claimed `agent` was always the real agent, giving consumers no way to tell the two apart — so one-time subscriptions (e.g. `onRunFinalized`) registered during the provisional window landed on the placeholder and missed events until the effect re-ran after the swap. Add an `isReady` flag to the return value: `false` while the agent is provisional, `true` once the real (or locally-registered) agent is bound. Additive and backward compatible. Also fix the docs' "Event Subscription" example, which used an empty `useEffect` dependency array and therefore never re-subscribed when the agent reference changed. Note: the original crash from CopilotKit#5000 ("Cannot read properties of undefined (reading 'subscribers')") no longer reproduces on `main` — the provisional-agent work (CopilotKit#5533/CopilotKit#5635) guarantees a fully-constructed agent, so `subscribe()` is always safe. The added tests lock in that no-crash behavior and cover the new `isReady` transition. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
AriaZhao-coder
requested review from
jpr5,
marthakelly,
mme,
ranst91 and
tylerslaton
as code owners
July 18, 2026 04:40
BenTaylorDev
approved these changes
Jul 20, 2026
@copilotkit/a2ui-renderer
@copilotkit/agentcore-runner
@copilotkit/angular
@copilotkit/channels
@copilotkit/channels-core
@copilotkit/channels-discord
@copilotkit/channels-intelligence
@copilotkit/channels-slack
@copilotkit/channels-teams
@copilotkit/channels-telegram
@copilotkit/channels-ui
@copilotkit/channels-whatsapp
@copilotkit/core
@copilotkit/react-core
@copilotkit/react-native
@copilotkit/react-textarea
@copilotkit/react-ui
@copilotkit/runtime
@copilotkit/runtime-client-gql
@copilotkit/sdk-js
@copilotkit/shared
@copilotkit/sqlite-runner
@copilotkit/voice
@copilotkit/vue
@copilotkit/web-components
@copilotkit/web-inspector
commit: |
This was referenced Jul 20, 2026
nuthalapativarun
added a commit
to nuthalapativarun/CopilotKit
that referenced
this pull request
Jul 25, 2026
Point "See also" links at the /integrations/langgraph/ prefixed routes (the bare /langgraph/... links 404), drop the dead Python SDK reference link (no target page), and swap the placeholder "gpt-5.4" model string for a real one (gpt-4o) in both code samples. Addresses review feedback on CopilotKit#5483. The unrelated use-agent.tsx isReady change was dropped by rebasing this branch onto upstream/main and carrying forward only the docs commit — isReady already ships on main via CopilotKit#6041.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #5000.
useAgent(v2) always returns a fully-constructedAbstractAgent— a provisional stand-in while the runtime is still connecting (or in an error state), swapped for the real agent once the/infosync resolves. The return type claimedagentwas always the real agent, so consumers had no way to tell the provisional instance from the real one. One-time subscriptions registered during the provisional window (e.g.onRunFinalized) landed on the placeholder and missed events until the effect re-ran after the swap.This PR adds an
isReadyflag to the return value:false—agentis provisional (runtime connecting / error)true—agentis the real, runtime-synced (or locally-registered) instanceThis is exactly the API the issue requests in its Expected Behavior. It is additive and backward compatible — existing
const { agent } = useAgent()callers are unaffected.On the original crash
The crash reported in #5000 —
Cannot read properties of undefined (reading 'subscribers')atAbstractAgent.subscribe— no longer reproduces onmain. The provisional-agent work landed for #5533 / #5635 now guaranteesuseAgentalways returns a fully-constructedAbstractAgent, sosubscribe()is always safe to call. The added tests lock in that no-crash behavior. What remained unaddressed was the missing readiness signal, which this PR provides.Changes
packages/react-core/src/v2/hooks/use-agent.tsx—useMemonow returns{ agent, isReady }; real agent →isReady: true, provisional paths →isReady: false. Documented with JSDoc.use-agent-subscribe-ready.test.tsx(new) — regression + behavior coverage:subscribe()does not throw while connecting (effect + during-render),isReadytransitionsfalse → trueon sync and swaps the instance, local agent is ready immediately.showcase/shell-docs/.../hooks/useAgent.mdx— signature + return-value docs updated; the Event Subscription example fixed (it used an emptyuseEffectdep array and never re-subscribed when the agent reference changed).Testing
react-corev2 hooks suite: 35 files / 299 tests pass (theuseMemoreturn-shape change breaks nothing).tsc --noEmitclean.Notes
useAgent(packages/vue) is structured differently (reactiveshallowRef,agentcan benull); happy to add matchingisReadyas a follow-up if maintainers want cross-framework parity.🤖 Generated with Claude Code