Skip to content

Commit 5eec9b0

Browse files
authored
fix: restore pending frontend HITL tools after replay (#7271)
Reloading an Intelligence thread restored frontend HITL tool calls but left them without an active response handler, so the user could see the approval UI without being able to answer it. This adds an internal frontend-tool classification and tags HITL registrations in React, Vue, and Angular. Shared core restores unanswered HITL handlers after replay and continues the agent with the user's result. Ordinary frontend tools remain passive; native interrupt handling is unchanged. React Native and legacy React wrappers inherit the React registration. Restoration finishes independently of the connection lifecycle, preserves sequential prompts across reconnects, ignores saved or remotely supplied answers, and cancels superseded interactions when switching threads. The classification is not sent in the AG-UI tool schema. Validation: formatting and lint pass; explicit type checks, full test suites, and builds pass for core, React, Vue, and Angular. Coverage includes replay → answer → continuation, saved and remote answers, repeated reconnects, sequential prompts, thread switching, and keeping tool classification off the wire. Commit hooks also passed tests and package-export checks across 24 affected projects. Live Intelligence browser validation remains a follow-up before marking this draft ready. PNI-449 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved human-in-the-loop behavior after reconnecting or switching conversations. * Pending approval requests now resume correctly without duplicating completed or remotely answered requests. * Multi-step responses remain sequential, while stale responses are safely ignored. * Regular tools continue operating normally during standard agent runs. * Stopping one agent no longer interrupts another agent’s pending interaction. * Cancelled interactions now report handler errors correctly when explicitly stopped. * **Improvements** * Human-in-the-loop tools are explicitly distinguished from regular tools across Angular, React, and Vue. * Pending interactions can be cancelled safely using abort signals. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents e65e68c + ee39ca8 commit 5eec9b0

16 files changed

Lines changed: 1120 additions & 175 deletions

‎packages/angular/src/lib/copilotkit.spec.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ describe("CopilotKit", () => {
267267
name: "approval",
268268
args: z.object({ summary: z.string() }),
269269
component: class {
270-
toolCall = signal({} as any);
270+
toolCall = signal({});
271271
},
272272
toolCall: vi.fn(),
273273
agentId: "agent-1",
@@ -277,19 +277,25 @@ describe("CopilotKit", () => {
277277

278278
expect(copilotKit.humanInTheLoopToolRenderConfigs()).toEqual([toolConfig]);
279279
expect(mockAddTool).toHaveBeenCalledWith(
280-
expect.objectContaining({ name: "approval" }),
280+
expect.objectContaining({ name: "approval", type: "human-in-the-loop" }),
281281
);
282282

283283
const tool = mockAddTool.mock.calls.at(-1)![0];
284+
const controller = new AbortController();
284285
const mockAgent = { agentId: "agent-1" };
285286
await tool.handler(
286287
{},
287288
{
288289
toolCall: { id: "call-1", function: { name: "approval" } },
289290
agent: mockAgent,
291+
signal: controller.signal,
290292
},
291293
);
292-
expect(onResultSpy).toHaveBeenCalledWith("call-1", "approval");
294+
expect(onResultSpy).toHaveBeenCalledWith(
295+
"call-1",
296+
"approval",
297+
controller.signal,
298+
);
293299

294300
onResultSpy.mockRestore();
295301
});

‎packages/angular/src/lib/copilotkit.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,13 @@ export class CopilotKit {
567567
): FrontendTool {
568568
return {
569569
...humanInTheLoopTool,
570-
handler: (args, { toolCall }) => {
571-
return this.#hitl.onResult(toolCall.id, humanInTheLoopTool.name);
570+
type: "human-in-the-loop",
571+
handler: (args, { toolCall, signal: abortSignal }) => {
572+
return this.#hitl.onResult(
573+
toolCall.id,
574+
humanInTheLoopTool.name,
575+
abortSignal,
576+
);
572577
},
573578
};
574579
}

‎packages/angular/src/lib/directives/__tests__/copilotkit-human-in-the-loop.directive.spec.ts‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { TestBed } from "@angular/core/testing";
2-
import { describe, expect, it } from "vitest";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
33
import { HumanInTheLoop } from "../../human-in-the-loop";
44

55
describe("HumanInTheLoop service", () => {
6-
it("resolves when matching result is provided", async () => {
6+
let service: HumanInTheLoop;
7+
beforeEach(() => {
78
TestBed.configureTestingModule({ providers: [HumanInTheLoop] });
8-
const service = TestBed.inject(HumanInTheLoop);
9+
service = TestBed.inject(HumanInTheLoop);
10+
});
911

12+
it("resolves when matching result is provided", async () => {
1013
const promise = service.onResult("call-1", "approval");
1114
service.addResult("call-1", "approval", { status: "ok" });
1215

1316
await expect(promise).resolves.toEqual({ status: "ok" });
1417
});
1518

1619
it("ignores non-matching results until criteria matches", async () => {
17-
TestBed.configureTestingModule({ providers: [HumanInTheLoop] });
18-
const service = TestBed.inject(HumanInTheLoop);
19-
2020
const promise = service.onResult("call-2", "verify");
2121

2222
service.addResult("call-2", "other", "nope");
@@ -33,9 +33,6 @@ describe("HumanInTheLoop service", () => {
3333
});
3434

3535
it("does not leak the bus routing keys into the resolved result", async () => {
36-
TestBed.configureTestingModule({ providers: [HumanInTheLoop] });
37-
const service = TestBed.inject(HumanInTheLoop);
38-
3936
const promise = service.onResult("call-3", "request_page_oncall");
4037
service.addResult("call-3", "request_page_oncall", { approved: true });
4138

@@ -48,4 +45,54 @@ describe("HumanInTheLoop service", () => {
4845
expect(resolved).not.toHaveProperty("toolCallId");
4946
expect(resolved).toEqual({ approved: true });
5047
});
48+
it.each([false, true])(
49+
"rejects and unsubscribes on abort (already aborted: %s)",
50+
async (alreadyAborted) => {
51+
const controller = new AbortController();
52+
if (alreadyAborted) controller.abort();
53+
const promise = service.onResult("call-1", "approval", controller.signal);
54+
const rejected = expect(promise).rejects.toThrow(
55+
"Human-in-the-loop interaction aborted",
56+
);
57+
controller.abort();
58+
await rejected;
59+
expect(service.results.observed).toBe(false);
60+
},
61+
);
62+
63+
it("detaches cancellation after receiving an answer", async () => {
64+
const controller = new AbortController();
65+
const removeListener = vi.spyOn(controller.signal, "removeEventListener");
66+
const promise = service.onResult("call-1", "approval", controller.signal);
67+
service.addResult("call-1", "approval", "yes");
68+
await expect(promise).resolves.toBe("yes");
69+
expect(service.results.observed).toBe(false);
70+
expect(removeListener).toHaveBeenCalledWith("abort", expect.any(Function));
71+
controller.abort();
72+
});
73+
74+
it("rejects if the result stream closes without an answer", async () => {
75+
const controller = new AbortController();
76+
const removeListener = vi.spyOn(controller.signal, "removeEventListener");
77+
const promise = service.onResult("call-1", "approval", controller.signal);
78+
const rejected = expect(promise).rejects.toThrow("no elements in sequence");
79+
service.results.complete();
80+
await rejected;
81+
expect(removeListener).toHaveBeenCalledWith("abort", expect.any(Function));
82+
});
83+
84+
it("keeps other waiters answerable when one aborts", async () => {
85+
const controller = new AbortController();
86+
const canceled = service.onResult("call-1", "approval", controller.signal);
87+
const active = service.onResult("call-2", "approval");
88+
const rejected = expect(canceled).rejects.toThrow(
89+
"Human-in-the-loop interaction aborted",
90+
);
91+
controller.abort();
92+
await rejected;
93+
service.addResult("call-1", "approval", "stale");
94+
service.addResult("call-2", "approval", "yes");
95+
await expect(active).resolves.toBe("yes");
96+
expect(service.results.observed).toBe(false);
97+
});
5198
});
Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { Injectable } from "@angular/core";
2-
import { filter, lastValueFrom, map, Subject, take } from "rxjs";
2+
import {
3+
filter,
4+
lastValueFrom,
5+
map,
6+
Observable,
7+
Subject,
8+
take,
9+
takeUntil,
10+
} from "rxjs";
311

412
@Injectable({ providedIn: "root" })
513
export class HumanInTheLoop {
@@ -13,19 +21,37 @@ export class HumanInTheLoop {
1321
this.results.next({ toolCallId, toolName, result });
1422
}
1523

16-
onResult(toolCallId: string, toolName: string): Promise<unknown> {
17-
return lastValueFrom(
18-
this.results.pipe(
19-
filter(
20-
(entry) =>
21-
entry.toolCallId === toolCallId && entry.toolName === toolName,
22-
),
23-
take(1),
24-
// Resolve with the bare result. toolCallId/toolName are routing keys for
25-
// this bus only — leaking them would make the tool result an envelope
26-
// that no consumer expects.
27-
map((entry) => entry.result),
24+
onResult(
25+
toolCallId: string,
26+
toolName: string,
27+
signal?: AbortSignal,
28+
): Promise<unknown> {
29+
const result = this.results.pipe(
30+
filter(
31+
(entry) =>
32+
entry.toolCallId === toolCallId && entry.toolName === toolName,
2833
),
34+
take(1),
35+
// Resolve with the bare result. toolCallId/toolName are routing keys for
36+
// this bus only — leaking them would make the tool result an envelope
37+
// that no consumer expects.
38+
map((entry) => entry.result),
2939
);
40+
if (!signal) return lastValueFrom(result);
41+
42+
const aborted = new Observable<never>((subscriber) => {
43+
const onAbort = () =>
44+
subscriber.error(new Error("Human-in-the-loop interaction aborted"));
45+
if (signal.aborted) {
46+
onAbort();
47+
return;
48+
}
49+
signal.addEventListener("abort", onAbort, { once: true });
50+
return () => signal.removeEventListener("abort", onAbort);
51+
});
52+
53+
// Completing the result detaches the abort listener. An abort errors the
54+
// stream, rejecting the promise and unsubscribing from results.
55+
return lastValueFrom(result.pipe(takeUntil(aborted)));
3056
}
3157
}

0 commit comments

Comments
 (0)