Repository navigation
fix(react-core): preserve logical run identity across HITL resolve - #6296
Conversation
BenTaylorDev
left a comment
There was a problem hiding this comment.
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 ononRunInitializedfiring beforeRUN_STARTED, so I scripted an agent and confirmed the order isonRunInitialized -> onRunStartedEvent -> onRunFinishedEvent -> onRunFinalized.expectedInputis 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: thedetachActiveRunthrow, the!startedpath, and the catch block. I couldn't find a route that leaves a marker active. isContinuationmisclassification. I looked for a genuinely-new run that would be misread as a continuation. It would need to carryforwardedProps.command.resume, which is already semantically a resume, so I don't think the case exists.- Stale
interruptRunIdsRef. Cleared on bothRUN_STARTEDandonRunFailed; a miss degrades to "no runId", which is exactly currentmainbehavior. No hang, no collision. - Tests.
@copilotkit/core58 files / 626 tests pass;use-interrupt.test.tsx+use-copilot-action.e2e.test.tsx35/35 pass with both halves applied and core rebuilt.@copilotkit/core:buildsucceeds. 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.
|
…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)
…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]>
Summary
Preserve the logical run ID when a legacy
useCopilotAction({ renderAndWaitForResponse })frontend tool resolves andprocessAgentResultstarts 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.runAgentcontract. The public regression drives the legacy hook through itsuseHumanInTheLoopanduseFrontendToolpath, renders the approval control, resolves it, and verifies both agent calls use the same generated ID.Closes #3456
Changes
useCopilotActionregressionTest plan
pnpm -C packages/react-core exec vitest run src/hooks/__tests__/use-copilot-action.e2e.test.tsxpnpm -C packages/core exec vitest run src/__tests__/core-follow-up.test.tspnpm -C packages/react-core exec vitest run src/v2/hooks/__tests__/use-interrupt.test.tsxpnpm -C packages/core exec vitest run src/__tests__/state-manager.test.ts, 39 tests passedpnpm -C packages/react-core exec vitest run, 123 files and 1475 tests passedpnpm -C packages/core exec vitest run, 58 files and 625 tests passedpnpm -C packages/core run check-typespnpm -C packages/react-core run check-typespnpm exec oxfmt --checkon all eight changed source/test filespnpm exec oxlinton all eight changed source/test files, 5 pre-existing warnings and 0 errors