Skip to content

fix(react-core): preserve logical run identity across HITL resolve - #6296

Merged
BenTaylorDev merged 6 commits into
CopilotKit:mainfrom
rodboev:fix/3456-hitl-stable-run-id
Aug 5, 2026
Merged

BenTaylorDev merged 6 commits into
CopilotKit:mainfrom
rodboev:fix/3456-hitl-stable-run-id

Conversation

@rodboev

@rodboev rodboev commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Preserve the logical run ID when a legacy useCopilotAction({ renderAndWaitForResponse }) frontend tool resolves and processAgentResult starts its recursive follow-up.

The run handler binds each internal continuation handoff to the exact follow-up invocation, cancels it when setup fails or no run starts, and keeps the handoff out of the public CopilotKitCore.runAgent contract. The public regression drives the legacy hook through its useHumanInTheLoop and useFrontendTool path, renders the approval control, resolves it, and verifies both agent calls use the same generated ID.

Closes #3456

Changes

  • Preserve the originating ID across recursive frontend-tool follow-up runs
  • Keep the existing legacy HITL registration and response behavior unchanged
  • Add core follow-up coverage and a public useCopilotAction regression
  • Retain the existing standard/legacy interrupt and StateManager coverage from the earlier fix

Test plan

  • pnpm -C packages/react-core exec vitest run src/hooks/__tests__/use-copilot-action.e2e.test.tsx
  • pnpm -C packages/core exec vitest run src/__tests__/core-follow-up.test.ts
  • pnpm -C packages/react-core exec vitest run src/v2/hooks/__tests__/use-interrupt.test.tsx
  • pnpm -C packages/core exec vitest run src/__tests__/state-manager.test.ts, 39 tests passed
  • pnpm -C packages/react-core exec vitest run, 123 files and 1475 tests passed
  • pnpm -C packages/core exec vitest run, 58 files and 625 tests passed
  • pnpm -C packages/core run check-types
  • pnpm -C packages/react-core run check-types
  • pnpm exec oxfmt --check on all eight changed source/test files
  • pnpm exec oxlint on all eight changed source/test files, 5 pre-existing warnings and 0 errors

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

Approving. I verified the mechanism rather than taking the description's word for it, and this holds up well.

The design call is the right one. Keeping the continuation marker in an in-memory WeakMap keyed by agent — rather than smuggling it through forwardedProps — means it never reaches a runtime and can't collide with user data. The comment says as much, and it's the part I'd have pushed back on had it gone the other way.

What I checked:

  • Lifecycle ordering. bind() depends on onRunInitialized firing before RUN_STARTED, so I scripted an agent and confirmed the order is onRunInitialized -> onRunStartedEvent -> onRunFinishedEvent -> onRunFinalized. expectedInput is therefore always set before the marker is looked up, so the pending entry always gets matched and cancelled — no orphaned markers.
  • Cancellation coverage. cancel() is wired into every exit: the detachActiveRun throw, the !started path, and the catch block. I couldn't find a route that leaves a marker active.
  • isContinuation misclassification. I looked for a genuinely-new run that would be misread as a continuation. It would need to carry forwardedProps.command.resume, which is already semantically a resume, so I don't think the case exists.
  • Stale interruptRunIdsRef. Cleared on both RUN_STARTED and onRunFailed; a miss degrades to "no runId", which is exactly current main behavior. No hang, no collision.
  • Tests. @copilotkit/core 58 files / 626 tests pass; use-interrupt.test.tsx + use-copilot-action.e2e.test.tsx 35/35 pass with both halves applied and core rebuilt. @copilotkit/core:build succeeds. Worth noting fork PRs don't get automatic CI here, so this hadn't been exercised before now.

One non-blocking nit for a follow-up: markNextRunAsContinuation(agent, _expectedRunId?) never reads its second parameter, and it's declared in CopilotKitCoreFriendsAccess as expectedRunId?: string. Either use it to validate the handoff or drop it from both signatures before it settles into the API.

Sequencing note for anyone reading this later: this touches the same runFinished && input.runId === subRunId heuristic that #6294 rewrites, so those two conflict in run-handler.ts and state-manager.ts. Landing this first means #6294 gets rebased onto it — which is the right order, since #6294 has a separate revision pending.

I bless the code with a glad heart.

@BenTaylorDev
BenTaylorDev merged commit 7e502dd into CopilotKit:main Aug 5, 2026
1 check passed
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

⚠️ Showcase Build Incomplete

The Docker build triggered by this PR's merge did not finish for every service.
These slots were cancelled (typically killed by timeout-minutes) and pushed no image, so staging still serves their previous :latest:

  • ag2

Everything else built and was redeployed. Use Re-run failed jobs on the run to finish the fleet.

Run: https://github.com/CopilotKit/CopilotKit/actions/runs/31030973151
Commit: 7e502dd4

@rodboev — please check the build logs and fix the issue.

davidmckayv added a commit that referenced this pull request Aug 5, 2026
…the wire (#6411)

Linear:
[CPK-7786](https://linear.app/copilotkit/issue/CPK-7786/hitl-continuation-reuses-the-originating-run-id-on-the-wire-breaking)

Regression fix. **Nothing from #6296 is reverted** — its goal is kept
and moved one layer up.

## What broke

#6296 (@rodboev) preserved the logical run id across a HITL resolve by
pinning the originating id on the follow-up's agent invocation,
correctly fixing #3456 (external tracing saw one logical run split into
two halves).

Pinning it **on the wire**, though, made the transport treat the
follow-up as a resumption of a run it had already finished:

- it re-delivered that run's already-applied half, duplicating every
tool call on the message — each duplicate carrying **empty arguments**,
because a start event has none and the `TOOL_CALL_ARGS` deltas that
follow are addressed to the first copy; and
- the follow-up's own tool call never reached client state, so its card
never rendered.

In `reskinnable-demo`'s banking skin that killed teach mode: the agent
called `awaitDashboardDemonstration`, the server emitted
`TOOL_CALL_START` for it, and the live "Recording your workflow" card
never appeared — no REC indicator, no step feed, no "I'm done", so a
demonstration could not be finished or saved.

## Evidence

- Intelligence event log shows **two `RUN_STARTED`/`RUN_FINISHED` cycles
under one run id**, with `TOOL_CALL_START [awaitDashboardDemonstration]`
in the second — the server does emit it.
- Instrumented `useRenderToolCall`: **never invoked** for that tool,
despite its renderer being registered.
- Client message state after the click: `assistant
tc:["recall_memory","recall_memory"]`, `assistant
tc:["offerWorkflowRecording","offerWorkflowRecording"]` — prior cycle
re-applied, new call absent.
- Bisect: reverting #6296's four source files makes the card appear;
restoring them breaks it again. No later commit touches those files.
- Ruled out: React StrictMode (disabled in that app),
model/prompt/tool-availability differences (byte-identical between the
working and broken apps), and the view-layer dedupe from #6407.

## The fix

`markNextRunAsContinuation` already accepted an `expectedRunId` that was
never used. It now records it, and the state manager re-stamps the
continuation's events onto that id. So:

- **logical identity is preserved** — state/message association and
external tracing still see one run, which is #6296's whole point;
- **the wire identifies the invocation honestly** — the follow-up no
longer claims to be a run that already finished, so nothing is
re-delivered and the continuation's own tool call lands.

## Test changes — please review this part closely

`core-follow-up`'s run-id test asserted the *mechanism* (both
invocations carry the same wire id), which this deliberately changes. It
now asserts the *goal*: the originating id is pinned on the first
invocation, and the follow-up leaves the id to the transport. Its
sibling assertion — the thread still knows exactly one run — was already
there and passes untouched.

A new `StateManager` test covers the re-stamp directly, verified **red
before green** by dropping the `expectedRunId` lookup (it fails, along
with one of #6296's own tests).

@rodboev — flagging you directly since this touches your change from
today. If the wire-level pinning was load-bearing for something I have
not seen, say so and I will rework it.

## Verification

- `@copilotkit/core` 58 files and `@copilotkit/react-core` 123 files
pass.
- In-browser against a live Intelligence stack: before, the recording
card never rendered; after, it renders with its REC indicator and I'm
done / Cancel controls.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
pull Bot pushed a commit to TheRakeshPurohit/CopilotKit that referenced this pull request Aug 5, 2026
…the wire

CopilotKit#6296 preserved the logical run id across a HITL resolve by pinning the
originating id on the follow-up's agent invocation. That fixed CopilotKit#3456 (external
tracing saw one logical run split into two halves), but pinning it on the WIRE
made the transport treat the follow-up as a resumption of a run it had already
finished. It re-delivered that run's already-applied half — duplicating every
tool call on the message, each duplicate carrying empty arguments, since a start
event has none and the TOOL_CALL_ARGS deltas that follow are addressed to the
first copy — and the follow-up's own tool call never reached client state, so
its card never rendered.

In the reskinnable-demo banking skin that broke teach mode outright: the agent
called awaitDashboardDemonstration, the server emitted TOOL_CALL_START for it,
and the live "Recording your workflow" card never appeared, leaving no way to
finish or save the demonstration.

CopilotKit#6296's goal is kept, moved one layer up. The continuation is registered against
the originating id (markNextRunAsContinuation already took an expectedRunId
parameter, previously unused) and the state manager re-stamps the continuation's
events onto it. State/message association and external tracing still see ONE
logical run; the wire is simply allowed to identify the invocation honestly.
Nothing from CopilotKit#6296 is reverted.

core-follow-up's run-id test asserted the mechanism (both invocations carry the
same wire id), which this deliberately changes, so it now asserts the goal: the
originating id is pinned on the first invocation and the follow-up leaves it to
the transport. Its sibling assertion — the thread still knows exactly one run —
was already there and still passes untouched. A new StateManager test covers the
re-stamp directly; verified red before green by dropping the expectedRunId
lookup.

Verified in the browser against a live Intelligence stack: before, the recording
card never rendered; after, it renders with its REC indicator and I'm done /
Cancel controls. `@copilotkit/core` 58 files and `@copilotkit/react-core` 123
files pass.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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: runId changes after HITL resolve

2 participants