Gateway: bound agent run sequence tracking

This commit is contained in:
Vignesh Natarajan
2026-02-14 17:50:49 -08:00
parent 451deb066f
commit fc8f59261a
5 changed files with 68 additions and 1 deletions

View File

@@ -131,6 +131,52 @@ describe("agent event handler", () => {
nowSpy.mockRestore();
});
it("cleans up agent run sequence tracking when lifecycle completes", () => {
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(2_500);
const broadcast = vi.fn();
const broadcastToConnIds = vi.fn();
const nodeSendToSession = vi.fn();
const agentRunSeq = new Map<string, number>();
const chatRunState = createChatRunState();
const toolEventRecipients = createToolEventRecipientRegistry();
chatRunState.registry.add("run-cleanup", {
sessionKey: "session-cleanup",
clientRunId: "client-cleanup",
});
const handler = createAgentEventHandler({
broadcast,
broadcastToConnIds,
nodeSendToSession,
agentRunSeq,
chatRunState,
resolveSessionKeyForRun: () => undefined,
clearAgentRunContext: vi.fn(),
toolEventRecipients,
});
handler({
runId: "run-cleanup",
seq: 1,
stream: "assistant",
ts: Date.now(),
data: { text: "done" },
});
expect(agentRunSeq.get("run-cleanup")).toBe(1);
handler({
runId: "run-cleanup",
seq: 2,
stream: "lifecycle",
ts: Date.now(),
data: { phase: "end" },
});
expect(agentRunSeq.has("run-cleanup")).toBe(false);
expect(agentRunSeq.has("client-cleanup")).toBe(false);
nowSpy.mockRestore();
});
it("routes tool events only to registered recipients when verbose is enabled", () => {
const broadcast = vi.fn();
const broadcastToConnIds = vi.fn();