Repository navigation
fix(react-core): wait for provider HITL responses - #4955
Tushar-Khandelwal-2004 wants to merge 3 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
@Tushar-Khandelwal-2004 is attempting to deploy a commit to the CopilotKit Team on Vercel. A member of the Team first needs to authorize it. |
|
Thanks @Tushar-Khandelwal-2004 — the bug is still live on main (the provider-level placeholder handler resolves Before merging, could you rebase onto current main and confirm the current tool-call rendering pipeline doesn't already inject |
c92b0d0 to
57f5563
Compare
|
Thanks for flagging the drift. I’ve rebased the PR onto the latest main and updated it. |
BenTaylorDev
left a comment
There was a problem hiding this comment.
Thanks for this. It does address a real, still-present gap — the provider-prop HITL path (<CopilotKitProvider humanInTheLoop={[...]}>) still has the immediate-resolve(undefined) placeholder on main (CopilotKitProvider.tsx:483-493). Two things to reconcile before merge, both stemming from the ~199 commits of HITL evolution since this was authored:
- Abort semantics invert the reference. This resolves
undefinedon abort, but #5554 (7d644f22f) deliberately changed the canonical hook to reject (new Error("Human-in-the-loop interaction aborted")) so core records an explicit error tool result instead of silently resolving to empty. Please match that — reject on abort — rather than reintroducing the empty-result-on-abort behavior #5554 removed. - Confirm the target issue. #4953 reproduces with the
useHumanInTheLoophook, which on current v2 main already has the wait+abort mechanism and e2e coverage (use-human-in-the-loop.tsx:30-58). This PR fixes the separate provider-prop path — a real bug, but possibly not the one #4953 describes. Worth checking whether #4953 still reproduces on main, and re-attaching to the right issue.
Also: the test wires the resolver plumbing directly but doesn't drive a real agent/multi-route transport or cover the abort branch — please add coverage that reproduces the actual scenario (agent emits TOOL_CALL → RUN_FINISHED → UI must persist) and exercises abort.
|
Thanks @BenTaylorDev I’ve addressed both points and pushed the update. The provider-level handler now matches the canonical hook behavior on abort: it rejects with I also added coverage for the provider-prop flow itself:
I rechecked #4953 as well: the current |
BenTaylorDev
left a comment
There was a problem hiding this comment.
Really close — the abort handling is exactly right now (rejects with the same error as the canonical hook, matching #5554), and the new transport-driven + abort tests are great. One stale assertion is failing, though: CopilotKitProvider.test.tsx:597 ("combines render components from all sources") still asserts humanRenderTool?.render).toBe(TestComponent2), but since the render is now wrapped (RenderComponent), that identity no longer holds — you updated the sibling assertion in the "waits for…" test but missed this one. Change it to .toBeDefined() (or assert the wrapped render's behavior) and it's green. Verified the rest locally: 33/34 pass, lint clean, no type errors in the changed file.
|
Thanks @BenTaylorDev - fixed the stale assertion and pushed b7853fa. It now verifies that the provider HITL renderer is present, rather than comparing it to the original component by identity, since the provider path intentionally wraps that renderer. |
BenTaylorDev
left a comment
There was a problem hiding this comment.
Thanks — the abort handling and the new transport-driven/abort tests are right, and CopilotKitProvider.test.tsx is 34/34 green now. One thing left, and it's more than a stale assertion.
CopilotKitProvider.wildcard.test.tsx:150 fails on this branch (main is 9/9, branch is 8/9). The wildcard assertion adjustment your PR description mentions isn't in the current diff — looks like it was lost in the rebase.
Please don't fix it by relaxing to .toBeDefined() — it's catching a real regression. The wrapper's name: tool.name overwrites the actual invoked tool name with "*" for wildcard registrations. The render pipeline always passes the real tool name (use-render-tool-call.tsx:49/59/69), and there's a contract test for it (defineToolCallRenderer.test.tsx:204-236, "wildcard receives actual tool name, not *"). On main the provider path passed it through unwrapped; with this patch a wildcard HITL renderer can't tell which tool it's approving. Verified: main → "deleteFile", this branch → "*".
Also needs a rebase — the branch no longer merges cleanly (small import conflict in the test file from the React 18 + 19 matrix commit 4049434b3b).
FYI, the same wildcard name-clobber already exists in the useHumanInTheLoop hook on main, and the Vue provider still has the immediate-resolve(undefined) placeholder — both out of scope here, I'll file them separately.
|
Thanks for this, and sorry it sat so long. Closing it in favor of #6851, which lands the same fix. To be clear about what happened: your diagnosis was right and your implementation was right. The provider-prop path really did never wait — Two things kept this branch from being the one that merges. The wildcard blocker from my last review is still open: the wrapper sets The credit for this one stays with you — #6851 says so in its description, and it links back here as the earlier implementation. I would genuinely welcome your next PR. |
The `humanInTheLoop` prop on `CopilotKitProvider` registered a handler that warned and resolved `undefined` the moment the agent invoked it, and it registered the renderer unwrapped, so the render never received a working `respond`. A tool declared that way jumped straight to Complete over dead controls while the agent was told the tool had succeeded. The placeholder is unchanged since the first v2 provider commit (a2ef51a), so this documented prop has never waited on the user. It fails the same way on both transports. Match the `useHumanInTheLoop` semantics: park the tool call until the render calls `respond`, expose `respond` only while the status is Executing, and reject with 'Human-in-the-loop interaction aborted' when the run is aborted so core records an explicit error tool result (#5554). Pending interactions are keyed by tool call id, and a wildcard registration keeps the invoked tool's name rather than "*". Two existing unit tests asserted the placeholder behavior (the console warning and render identity). They now assert the waiting contract and that the wrapper renders the caller's component. Mirrors the Vue fix in #6527. Related: #4953 (the provider-prop half of that report) and #4955. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The `humanInTheLoop` prop on `CopilotKitProvider` registered a handler that warned and resolved `undefined` the moment the agent invoked it, and it registered the renderer unwrapped, so the render never received a working `respond`. A tool declared that way jumped straight to Complete over dead controls while the agent was told the tool had succeeded. The placeholder is unchanged since the first v2 provider commit (a2ef51a), so this documented prop has never waited on the user. It fails the same way on both transports. Match the `useHumanInTheLoop` semantics: park the tool call until the render calls `respond`, expose `respond` only while the status is Executing, and reject with 'Human-in-the-loop interaction aborted' when the run is aborted so core records an explicit error tool result (#5554). Pending interactions are keyed by tool call id, and a wildcard registration keeps the invoked tool's name rather than "*". Two existing unit tests asserted the placeholder behavior (the console warning and render identity). They now assert the waiting contract and that the wrapper renders the caller's component. Mirrors the Vue fix in #6527. Related: #4953 (the provider-prop half of that report) and #4955. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…6851) ## Summary A human-in-the-loop tool declared through the `humanInTheLoop` **prop** on `<CopilotKitProvider>` never waited for the user. The handler resolved `undefined` the moment the agent invoked it, and the renderer was registered unwrapped, so it never received a working `respond`. The card appeared once at `Complete` over dead controls while the agent was told the tool had succeeded. This is not a regression. The placeholder handler is unchanged since the first v2 provider commit (`a2ef51aaf3`, #2794), so this documented prop has never waited. The reference page describes it as self-sufficient: "Declarative human-in-the-loop tool definitions. Each becomes both a tool handler and a tool call renderer." The `useHumanInTheLoop` hook path is unaffected and has always worked. ## Changes `packages/react-core/src/v2/providers/CopilotKitProvider.tsx`, matching `useHumanInTheLoop`: - park the tool call until the render calls `respond`, keyed by tool call id so one call of a tool cannot resolve another - wrap the registered renderer so it receives the full prop contract, with `respond` live only while the status is `Executing` - reject with `Human-in-the-loop interaction aborted` on abort, so core records an explicit error tool result rather than an empty success (#5554) - keep the invoked tool's name for a wildcard (`"*"`) registration instead of overwriting it with `"*"` Two existing unit tests asserted the placeholder behavior (the `console.warn`, and render identity). They now assert the waiting contract and that the wrapper renders the caller's component. ## Testing **New: `CopilotKitProvider.hitl.e2e.test.tsx` (4 tests).** These mount the real pipeline — `CopilotChat` -> tool calls view -> the provider's registered renderer — rather than calling the renderer directly, so a mismatch between the key the handler parks under and the tool call id the pipeline supplies cannot pass. All four fail on `main` for the right reason (`Unable to find an element by: [data-testid="hitl-respond"]` — the respond button never appears), and pass with the change. Each mechanism was mutation-checked separately: | Mutation | Result | | -- | -- | | handler resolves immediately (the old placeholder) | all 4 fail | | renderer registered unwrapped | all 4 fail | | wildcard branch removed | only the wildcard test fails | | park under the tool name instead of the tool call id | the 2 tests needing a live `respond` fail | **Suite, types, format** ``` $ node ../../node_modules/vitest/vitest.mjs run src/v2/providers/__tests__/ Test Files 17 passed (17) Tests 173 passed (173) $ tsc --noEmit (clean) ``` `oxfmt` applied to every changed file. The full `react-core` run also has 7 failures across `CopilotChatView.pinToSend.test.tsx` and `use-pin-to-send.test.tsx` (`ResizeObserver is not a constructor`). Those are pre-existing: I removed this change from the working tree and ran those two files on clean `main`, and they fail identically. **Live, in `examples/v2/react/demo` against a real agent and a rebuilt dist** | Page | Before | After | | -- | -- | -- | | provider prop, multi-route REST | card at `complete`, `respond` undefined, console warned, agent said "Please pick a time slot in the scheduler UI to confirm" over disabled buttons | parks at `executing` with live buttons for 17s, then "Booked a call with Sam about the Waterfall project meeting for Tue 14:00" | | provider prop, single route | same, and worse: "…has been scheduled" before anyone approved | parks, then reports the slot actually chosen (Wed 09:30) | | `useHumanInTheLoop` hook, multi-route REST | already correct | unchanged: `inProgress` -> `executing` -> `complete` with the chosen slot reaching the agent | ## Credit @Tushar-Khandelwal-2004 diagnosed this path and implemented the parking handler and the abort semantics first, in #4955. That PR predates the wildcard-name work and the current `main`, and its review asks were still open, so this lands the same fix rebased on `main` with the wildcard branch and pipeline-level tests added. #4955 is now closed in favor of this PR, with the credit staying with them. ## Related - #4955 — the earlier implementation of this fix (closed in favor of this PR) - #6527 — the identical Vue defect; the two changes agree on semantics - #4953 — the reporter hit the hook path, which does not reproduce on `main`; this is the confirmed provider-prop half of that report Angular already implements this path (`addHumanInTheLoop` -> `#hitl.onResult`), so React was the last framework missing it. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Human-in-the-loop tool calls now pause until a user responds. * Interactive renderers receive tool metadata and a callback for submitting responses. * Wildcard tools display the specific tool name that was invoked. * Aborted tool calls now return a clear error result. * **Bug Fixes** * Multiple pending tool calls can be resolved independently by their call ID. * Interactive handlers no longer emit an unnecessary warning while awaiting a response. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
What does this PR do?
Fixes provider-level
humanInTheLooptools so they properly wait for user interaction before resolving under the default multi-route transport.Previously, provider-registered HITL tools used a placeholder handler that resolved immediately with
undefined. In REST/multi-route flows, this caused the HITL UI to briefly render and disappear beforerespond()was invoked.This PR introduces proper pending resolver handling for provider-level HITL flows and ensures tool execution only resolves after the user responds.
Related PRs and Issues
-> Fixes #4953
Changes
-> Store pending provider-level HITL resolvers by
toolCall.id-> Pass a functional
respondcallback to HITL renderers while the tool call is executing-> Resolve the matching pending HITL handler only after the user responds
-> Handle aborted HITL tool calls by resolving and cleaning up pending resolvers
-> Update provider HITL tests to cover wait-until-respond behavior
-> Adjust wildcard/provider test assertions for wrapped HITL renderers
Testing
Notes
-> Full pre-commit lint passed for the changed files
-> Repo-wide pre-commit tests were not fully clean locally due to unrelated/flaky failures in:
->
A2UIMessageRenderer.test.tsx->
@copilotkit/sqlite-runner:test