Skip to content

fix(react-core): expose isReady from useAgent to guard agent subscriptions (#5000) - #6041

Merged
BenTaylorDev merged 1 commit into
CopilotKit:mainfrom
AriaZhao-coder:fix/use-agent-isready
Jul 20, 2026
Merged

BenTaylorDev merged 1 commit into
CopilotKit:mainfrom
AriaZhao-coder:fix/use-agent-isready

Conversation

@AriaZhao-coder

Copy link
Copy Markdown
Contributor

Summary

Closes #5000.

useAgent (v2) 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 return type claimed agent was 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 isReady flag to the return value:

  • false — agent is provisional (runtime connecting / error)
  • true — agent is the real, runtime-synced (or locally-registered) instance

This is exactly the API the issue requests in its Expected Behavior. It is additive and backward compatible — existing const { agent } = useAgent() callers are unaffected.

const { agent, isReady } = useAgent({ agentId });

useEffect(() => {
  if (!isReady) return; // only subscribe once the real agent is bound
  const sub = agent.subscribe({ onRunFinalized: (p) => console.log(p) });
  return () => sub.unsubscribe();
}, [agent, isReady]);

On the original crash

The crash reported in #5000 — Cannot read properties of undefined (reading 'subscribers') at AbstractAgent.subscribe — no longer reproduces on main. The provisional-agent work landed for #5533 / #5635 now guarantees useAgent always returns a fully-constructed AbstractAgent, so subscribe() 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 — useMemo now 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), isReady transitions false → true on 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 empty useEffect dep array and never re-subscribed when the agent reference changed).

Testing

  • New test file: 4/4 pass.
  • Full react-core v2 hooks suite: 35 files / 299 tests pass (the useMemo return-shape change breaks nothing).
  • tsc --noEmit clean.

Notes

  • Scope is React only, matching the issue. The Vue useAgent (packages/vue) is structured differently (reactive shallowRef, agent can be null); happy to add matching isReady as a follow-up if maintainers want cross-framework parity.

🤖 Generated with Claude Code

…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]>
@pkg-pr-new

pkg-pr-new Bot commented Jul 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@copilotkit/a2ui-renderer

pnpm add https://pkg.pr.new/@copilotkit/a2ui-renderer@6041

@copilotkit/agentcore-runner

pnpm add https://pkg.pr.new/@copilotkit/agentcore-runner@6041

@copilotkit/angular

pnpm add https://pkg.pr.new/@copilotkit/angular@6041

@copilotkit/channels

pnpm add https://pkg.pr.new/@copilotkit/channels@6041

@copilotkit/channels-core

pnpm add https://pkg.pr.new/@copilotkit/channels-core@6041

@copilotkit/channels-discord

pnpm add https://pkg.pr.new/@copilotkit/channels-discord@6041

@copilotkit/channels-intelligence

pnpm add https://pkg.pr.new/@copilotkit/channels-intelligence@6041

@copilotkit/channels-slack

pnpm add https://pkg.pr.new/@copilotkit/channels-slack@6041

@copilotkit/channels-teams

pnpm add https://pkg.pr.new/@copilotkit/channels-teams@6041

@copilotkit/channels-telegram

pnpm add https://pkg.pr.new/@copilotkit/channels-telegram@6041

@copilotkit/channels-ui

pnpm add https://pkg.pr.new/@copilotkit/channels-ui@6041

@copilotkit/channels-whatsapp

pnpm add https://pkg.pr.new/@copilotkit/channels-whatsapp@6041

@copilotkit/core

pnpm add https://pkg.pr.new/@copilotkit/core@6041

@copilotkit/react-core

pnpm add https://pkg.pr.new/@copilotkit/react-core@6041

@copilotkit/react-native

pnpm add https://pkg.pr.new/@copilotkit/react-native@6041

@copilotkit/react-textarea

pnpm add https://pkg.pr.new/@copilotkit/react-textarea@6041

@copilotkit/react-ui

pnpm add https://pkg.pr.new/@copilotkit/react-ui@6041

@copilotkit/runtime

pnpm add https://pkg.pr.new/@copilotkit/runtime@6041

@copilotkit/runtime-client-gql

pnpm add https://pkg.pr.new/@copilotkit/runtime-client-gql@6041

@copilotkit/sdk-js

pnpm add https://pkg.pr.new/@copilotkit/sdk-js@6041

@copilotkit/shared

pnpm add https://pkg.pr.new/@copilotkit/shared@6041

@copilotkit/sqlite-runner

pnpm add https://pkg.pr.new/@copilotkit/sqlite-runner@6041

@copilotkit/voice

pnpm add https://pkg.pr.new/@copilotkit/voice@6041

@copilotkit/vue

pnpm add https://pkg.pr.new/@copilotkit/vue@6041

@copilotkit/web-components

pnpm add https://pkg.pr.new/@copilotkit/web-components@6041

@copilotkit/web-inspector

pnpm add https://pkg.pr.new/@copilotkit/web-inspector@6041

commit: e5c1af4

@BenTaylorDev
BenTaylorDev merged commit 5595cf7 into CopilotKit:main Jul 20, 2026
45 of 46 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug: useAgent: agent.subscribe() throws "Cannot read properties of undefined (reading 'subscribers')" on mount

2 participants