Skip to content

fix(react-core): wait for provider HITL responses - #4955

Closed
Tushar-Khandelwal-2004 wants to merge 3 commits into
CopilotKit:mainfrom
Tushar-Khandelwal-2004:fix/4953-hitl-multi-route-transport
Closed

Tushar-Khandelwal-2004 wants to merge 3 commits into
CopilotKit:mainfrom
Tushar-Khandelwal-2004:fix/4953-hitl-multi-route-transport

Conversation

@Tushar-Khandelwal-2004

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes provider-level humanInTheLoop tools 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 before respond() 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 respond callback 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

node ..\..\node_modules\vitest\vitest.mjs run src/v2/providers/__tests__/CopilotKitProvider.test.tsx src/v2/providers/__tests__/CopilotKitProvider.wildcard.test.tsx

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

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@vercel

vercel Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment May 21, 2026 11:58am

Request Review

@vercel

vercel Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

@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.

@BenTaylorDev

Copy link
Copy Markdown
Contributor

Thanks @Tushar-Khandelwal-2004 — the bug is still live on main (the provider-level placeholder handler resolves undefined immediately and provider renders aren't passed respond), and the approach matches the current render-props contract. The concern is drift: this branch is a long way behind main, and the HITL rendering area has changed a lot since — the respond/toolCallId render-props contract now exists centrally.

Before merging, could you rebase onto current main and confirm the current tool-call rendering pipeline doesn't already inject respond for provider-level renderers? If it does, this manual wrapper would double-wire it. A quick repro: register a HITL tool via the humanInTheLoop prop on CopilotKitProvider (not the useHumanInTheLoop hook) and verify the run still hangs without the patch and isn't double-injected with it.

@Tushar-Khandelwal-2004
Tushar-Khandelwal-2004 force-pushed the fix/4953-hitl-multi-route-transport branch from c92b0d0 to 57f5563 Compare July 20, 2026 17:42
@Tushar-Khandelwal-2004

Copy link
Copy Markdown
Contributor Author

Thanks for flagging the drift. I’ve rebased the PR onto the latest main and updated it.
I verified that the central tool-call rendering pipeline does not inject respond for humanInTheLoop tools registered through CopilotKitProvider; that behavior is currently only handled by the useHumanInTheLoop hook wrapper. The provider-level path was therefore still resolving immediately with undefined.
The updated patch adds the same pending-resolver behavior for provider-registered HITL tools, keyed by toolCallId, and only exposes respond while the tool is executing. I also added a regression test that confirms the handler remains pending until respond() is called, so it should not be double-wired

@BenTaylorDev BenTaylorDev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Abort semantics invert the reference. This resolves undefined on 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.
  2. Confirm the target issue. #4953 reproduces with the useHumanInTheLoop hook, 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.

@Tushar-Khandelwal-2004

Copy link
Copy Markdown
Contributor Author

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 Human-in-the-loop interaction aborted rather than resolving undefined, so core records an explicit error result.

I also added coverage for the provider-prop flow itself:

  • an abort test for a pending provider HITL handler; and
  • an end-to-end event-sequence test that simulates TOOL_CALL → RUN_FINISHED, verifies the HITL UI remains interactive, and confirms it completes only after respond() is called.

I rechecked #4953 as well: the current useHumanInTheLoop hook path already has the wait/abort handling and e2e coverage, so this PR is more accurately fixing the separate <CopilotKitProvider humanInTheLoop={[...]}> path rather than the hook-specific reproduction.

@BenTaylorDev BenTaylorDev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Tushar-Khandelwal-2004

Copy link
Copy Markdown
Contributor Author

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 BenTaylorDev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@BenTaylorDev

Copy link
Copy Markdown
Contributor

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 — resolve(undefined) fired the moment the agent invoked the tool — and the parking handler keyed by tool call id plus the abort-rejects-with-an-error semantics in this PR are exactly what the fix needs. #6851 carries both of those over unchanged in substance.

Two things kept this branch from being the one that merges. The wildcard blocker from my last review is still open: the wrapper sets name: tool.name, which for a "*" registration hands the renderer "*" instead of the tool actually invoked, so a catch-all HITL render cannot tell what it is approving. And the branch has fallen behind main far enough that it no longer merges cleanly. #6851 adds the wildcard branch and pipeline-level tests that mount the real chat → tool-call → renderer path, on top of current main.

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.

BenTaylorDev added a commit that referenced this pull request Sep 8, 2026
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]>
BenTaylorDev added a commit that referenced this pull request Sep 8, 2026
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]>
BenTaylorDev added a commit that referenced this pull request Sep 8, 2026
…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 -->
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: useHumanInTheLoop silently fails under the default multi-route transport (CopilotKitProvider)

2 participants