diff --git a/src/gateway/server-methods/agent.test.ts b/src/gateway/server-methods/agent.test.ts index 8d77e04db14..be7caca9aee 100644 --- a/src/gateway/server-methods/agent.test.ts +++ b/src/gateway/server-methods/agent.test.ts @@ -522,6 +522,7 @@ describe("gateway agent handler", () => { it("rejects deliver=true when backend callers use the inter_session sentinel", async () => { primeMainAgentRun(); mocks.agentCommand.mockClear(); + const context = makeContext(); const selectionSpy = vi.spyOn(channelSelection, "resolveMessageChannelSelection"); selectionSpy.mockResolvedValue({ channel: "telegram", @@ -540,6 +541,7 @@ describe("gateway agent handler", () => { }, { reqId: "inter-session-backend-deliver-1", + context, client: { connect: { role: "operator", @@ -566,6 +568,11 @@ describe("gateway agent handler", () => { code: "INVALID_REQUEST", message: expect.stringContaining("inter_session"), }); + expect(context.addChatRun).not.toHaveBeenCalled(); + expect(mocks.registerAgentRunContext).not.toHaveBeenCalledWith( + "test-inter-session-backend-deliver", + expect.anything(), + ); expect(mocks.agentCommand).not.toHaveBeenCalled(); }); diff --git a/src/gateway/server-methods/agent.ts b/src/gateway/server-methods/agent.ts index 497f15e8d39..ce7b7844356 100644 --- a/src/gateway/server-methods/agent.ts +++ b/src/gateway/server-methods/agent.ts @@ -313,6 +313,23 @@ export const agentHandlers: GatewayRequestHandlers = { return; } } + + const wantsDelivery = request.deliver === true; + const requestedInterSessionForDelivery = [request.channel, request.replyChannel].some((value) => + isInterSessionChannel(normalizeMessageChannel(value)), + ); + if (wantsDelivery && requestedInterSessionForDelivery) { + respond( + false, + undefined, + errorShape( + ErrorCodes.INVALID_REQUEST, + "delivery channel cannot use the inter_session sentinel", + ), + ); + return; + } + let resolvedSessionId = request.sessionId?.trim() || undefined; let sessionEntry: SessionEntry | undefined; let bestEffortDeliver = requestedBestEffortDeliver ?? false; @@ -472,21 +489,6 @@ export const agentHandlers: GatewayRequestHandlers = { } } - const wantsDelivery = request.deliver === true; - const requestedInterSessionForDelivery = [request.channel, request.replyChannel].some((value) => - isInterSessionChannel(normalizeMessageChannel(value)), - ); - if (wantsDelivery && requestedInterSessionForDelivery) { - respond( - false, - undefined, - errorShape( - ErrorCodes.INVALID_REQUEST, - "delivery channel cannot use the inter_session sentinel", - ), - ); - return; - } const explicitTo = typeof request.replyTo === "string" && request.replyTo.trim() ? request.replyTo.trim()