Repository navigation
fix(react-core): make useCoAgent's start, run and stop work, through the v2 core - #7610
Conversation
useCoAgent returned agent.runAgent and agent.abortRun as bare references.
Both are prototype methods that read `this`, so calling them detached, as
`const { start } = useCoAgent(...)` does, threw "Cannot set properties of
undefined (setting 'isRunning')". On an HttpAgent, stop threw on
`abortController`. They are now wrapped in arrow functions.
The new test uses a real AbstractAgent subclass. The existing tests mock
runAgent with vi.fn(), which never reads `this`, so they could not see it.
Refs #3132
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (3)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughThe deprecated ChangesCo-agent method routing
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to The co-agent methods now use core execution and cancellation, with tests covering the changed behavior. No concrete issue remains that should delay merging. Security Architecture ReviewSecurity architecture risk: 🟡 Moderate · up to Run and stop now use the established tool-execution path and retain agent-specific data filtering. However, stopping one agent can interrupt another agent’s tool or approval work when they share the same application instance. Retained concerns
Security review detailsSecurity Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Hardening Proposals
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
@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/intelligence-langgraph
@copilotkit/intelligence-mastra
@copilotkit/learning
@copilotkit/mcp-apps-renderer
@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: |
|
Size Change: +228 B (+0.01%) Total Size: 1.77 MB 📦 View Changed
ℹ️ View Unchanged
|
…renders The previous commit wrapped the methods in arrow functions inside the useMemo, so every state change handed callers new functions. The bare prototype methods had a stable identity, and code such as useEffect(..., [start]) relied on that. The wrappers are now useCallback keyed on the agent. A new test fails on the previous commit and passes here. Refs #3132 Co-Authored-By: Claude Opus 5.5 <[email protected]>
A wrapper that dropped run's arguments or its returned promise still passed the suite. One test now checks that run forwards its parameters to runAgent and resolves to its result. Another calls start as a method of the hook result, which also failed on main, because `this` was the result object. Refs #3132 Co-Authored-By: Claude Opus 5.5 <[email protected]>
start and run called agent.runAgent() directly. That skipped the core's copilotkit.runAgent, so a run carried none of the config that useCoAgent sets with setProperties, no registered frontend tools and no readable context, and a frontend tool call the agent made never executed. The v1 chat path already goes through the core. stop now calls copilotkit.stopAgent, which also cancels in-flight tool handlers and the follow-up run. run honors forwardedProps and runId from its first argument; the core builds tools and context itself. The public type is unchanged. The tests now drive a real CopilotKitCore. The config/tools/context test and the tool-execution test fail on the previous commit, and a stop that bypasses the core fails the stop test. Refs #3132 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ed run resolves The generated reference page still said run maps to the v2 agent's runAgent(). The JSDoc for run now also says that a failed run resolves with an empty result instead of rejecting, because the core catches the error. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Production Digest-Pinning AuditAll 42 services digest-pinned. Run 2026-10-04 21:08:54 PDT — 0 finding(s). |
Follows from #7610: v1 `useCoAgent` called `agent.runAgent()` directly and so skipped the core. The same pattern appears in eight v2 docs examples. ## Problem `agent.runAgent()` is the agent's low-level method. It does not send the registered frontend tools or context, does not execute frontend tool calls, and does not chain follow-up runs. In the core, tools and context are attached only in `copilotkit.runAgent` and `connectAgent` (`packages/core/src/core/run-handler.ts:608`, `:811`). `programmatic-control.mdx` already names `copilotkit.runAgent({ agent })` as "the recommended default", and the Vue `CopilotChatInput` page already uses it. But eight examples showed `agent.runAgent()` for an ordinary run, so code copied from them silently loses tools. ## Changes | Page | Change | |---|---| | `integrations/langgraph/shared-state/in-app-agent-write.mdx`, `integrations/deepagents/...` | "Re-run the agent with updated state" calls `copilotkit.runAgent({ agent })`. The prose says why. | | `whats-new/v1-50.mdx` | The multi-agent example runs each agent through `copilotkit`. | | `reference/components/CopilotChatInput.mdx` | The stop-button example runs through `copilotkit.runAgent` and stops through `copilotkit.stopAgent`, matching the Vue page. | | `reference/hooks/useAgent.mdx`, `react-native/...`, `v1/...`, `vue/...` | The `runAgent` method stays documented. It is now marked low-level, says what it skips, and points to `copilotkit.runAgent`. It also says that `copilotkit.runAgent` resolves instead of rejecting when the run fails. The example is now an ordinary `forwardedProps` run through the core. Interrupt answers point to `useInterrupt` or the standard `resume` array, and `forwardedProps.command.resume` is labeled legacy. The Vue example gains the `agent.value` null check that it needs to typecheck. | | `snippets/shared/basics/programmatic-control.mdx`, `docs/programmatic-control.mdx` | The interrupt-resume guidance names `copilotkit.runAgent({ agent, resume })` for standard interrupts and labels the `forwardedProps.command.resume` form legacy. The shared snippet no longer recommends `agent.runAgent()` for a resume. | **Reviewed and left alone:** - Every `thread.runAgent(...)`: the Channels server API, a different method. - `programmatic-control.mdx`: its comparison section is the source of the guidance. - The "what happens underneath" comment in `snippets/shared/backend/ag-ui.mdx`: it describes the transport, and the core does call `agent.runAgent()`. - The `headless-threads` prose: it is about which thread a run addresses. - `dev-docs/architecture/setup-vanilla.md`: a deliberate zero-abstraction `HttpAgent` example that already says "You lose tools, context". - `examples/showcases/grok-generative-ui/README.md`: already labels `agent.runAgent()` "wrong". `deepagents` and `langgraph` (`docs_mode: generated`) both render their own `shared-state/in-app-agent-write` page, because the root has no file for that slug. ## Testing None of these blocks has a `doctest` attribute, so CI does not compile them. I typechecked each new snippet against the real packages instead. - **React** (`in-app-agent-write`, `v1-50`, `CopilotChatInput`, and the `useAgent` example; React Native re-exports the same hooks): a probe file in `packages/react-core/src` under `tsc --noEmit -p tsconfig.json` gives no errors. A control line with `copilotkit.runAgent({ agentt: 1 })` in the same probe reports `TS2561 ... 'agentt' does not exist in type 'CopilotKitCoreRunAgentParams'`, so the probe was really checked. - **Vue**: the same check under `vue-tsc --noEmit` in `packages/vue`. The snippet as first written failed with `Type 'AbstractAgent | null' is not assignable to type 'AbstractAgent'`. The old `agent.value.runAgent(...)` had the same flaw. With the null check, it gives no errors, and the control line reports `TS2561`. - **MDX syntax**: all eight files parse with `unified().use(remarkParse).use(remarkMdx)`, the parser `scripts/doc-tests/extract.ts` uses. A deliberately broken control file fails it. - **Standard `resume` snippet**: a probe in `packages/core/src` under `tsc --noEmit` accepts `copilotkit.runAgent({ agent, resume: [{ interruptId: interrupt.id, status: "resolved", payload }] })` and the new `forwardedProps` example. A control line with `status: "resolvd"` reports `TS2820 ... not assignable to type '"cancelled" | "resolved"'`. - **Not done**: a live render of the docs site. **Overlap**: open PR #7476 also edits `reference/components/CopilotChatInput.mdx`, in different sections. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Updated agent execution examples to use the CopilotKit runner, which includes frontend tools and context and handles frontend tool calls. * Clarified that direct agent execution is a low-level option, while the CopilotKit runner reports run failures to error subscribers instead of rejecting. * Explained standard interrupt resumption and distinguished it from the legacy custom-event approach. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Refs #3132. This fixes the half of that issue that reproduces on
main. The other half (state not syncing with an ADK backend) did not reproduce, so the issue stays open.Problem
v1
useCoAgentreturnedstart,runandstopas bare references toagent.runAgentandagent.abortRun. There were two defects.1. The calls crashed. Both are prototype methods that read
this. The documented usage destructures them,const { start } = useCoAgent(...), and a destructured call throws:Calling one as a method of the result (
coagent.start()) fails too, becausethisis then the result object:On an
HttpAgent(and the agents built on it),stop()threwCannot read properties of undefined (reading 'abortController').2. Behind the crash, the run skipped the v2 core.
agent.runAgent()sends the request but does none of the work thatcopilotkit.runAgent({ agent })does (seeprogrammatic-control.mdx, "copilotkit.runAgent()vsagent.runAgent()"):configthatuseCoAgentsets withcopilotkit.setPropertiesnever reached the agent.The v1 chat path (
use-copilot-chat_internal.ts) already goes through the core. Nobody hit this second defect, because the first one crashed every call first.Change
packages/react-core/src/v1-deprecated/hooks/use-coagent.ts:startandruncallcopilotkit.runAgent({ agent, forwardedProps, runId }). They takeforwardedPropsandrunIdfrom the first argument. The core builds the tools and context itself.stopcallscopilotkit.stopAgent({ agent }), as the v2 chat does. That also cancels in-flight tool handlers and the follow-up run, which a bareabortRun()did not.useCallbackwrappers keyed onagentandcopilotkit. They keep a stable identity across state changes, as the bare prototype methods did, so app code such asuseEffect(..., [start])behaves the same.run((...args: any[]) => Promise<any>) is unchanged. Its JSDoc now says which fields are honored.This is a v1 bug fix. It adds no API.
Testing
use-coagent-methods.test.tsdrives the hook with a realAbstractAgentsubclass and a realCopilotKitCore. The older tests mockrunAgentwithvi.fn(), which never readsthisand never goes through a core, so they could not see either defect.The 8 tests:
startdetached,rundetached, andstartas a method of the result each run the agent.runsends the configured properties, a registered frontend tool and readable context, plus its ownforwardedProps.runforwardsrunIdand returns the run's result.start,runandstopkeep their identity across a state change. The test also asserts that the memo did recompute, so the check is not vacuous.stopcallscopilotkit.stopAgent({ agent }), and the agent aborts.Against
main's source:Only the stability test passes there, because the bare methods were stable.
Against the previous commit of this PR (direct
agent.runAgent): the config/tools/context test and the tool-execution test fail:Mutation checks:
stopthat calls a bareagent.abortRun()fails the stop test (1 failed | 7 passed).runthat dropped its arguments and its returned promise failed the argument test.useMemofailed the stability test.On this branch:
Gates:
vitest runinpackages/react-core(full suite): 164 files, 1803 tests, all pass.nx run @copilotkit/react-core:check-types --skip-nx-cache: pass.oxlinton the changed files: 0 errors. The hook file has 13 older warnings (main has 14), and none is on a changed line.oxfmt: clean.test,publint,attwon affected projects): pass.Not done: a live check in a running v1 app. The tests use the real agent class and the real core, so they cover the binding and the core path that an app would hit.
🤖 Generated with Claude Code
Summary by CodeRabbit