fix(gateway): reject inter_session delivery before run tracking

This commit is contained in:
Rai Butera 2026-03-13 17:08:38 +00:00
parent 9efa86be99
commit 9f3d7a3e7d
2 changed files with 24 additions and 15 deletions

View File

@ -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();
});

View File

@ -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()