From 331f09b82d8dd4719e429243140cbbcf523322f1 Mon Sep 17 00:00:00 2001 From: Bryan Marty Date: Wed, 4 Mar 2026 18:17:59 +0000 Subject: [PATCH] fix: guard threadId with same session check as deliveryContext in restart opts.agentThreadId belongs to the current agent's thread. When the restart action targets a different sessionKey, forwarding it into the sentinel would cause scheduleRestartSentinelWake to deliver the post-restart reply to the wrong thread. Apply the same isTargetingOtherSession guard used for deliveryContext: only take opts.agentThreadId when the restart targets the current session; otherwise use extracted.threadId from extractDeliveryInfo, which correctly derives threadId from the target session key. --- src/agents/tools/gateway-tool.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/agents/tools/gateway-tool.ts b/src/agents/tools/gateway-tool.ts index 800414f92ea..07772487f41 100644 --- a/src/agents/tools/gateway-tool.ts +++ b/src/agents/tools/gateway-tool.ts @@ -129,8 +129,14 @@ export function createGatewayTool(opts?: { : undefined; const extracted = extractDeliveryInfo(sessionKey); const deliveryContext = liveContext ?? extracted.deliveryContext; + // Guard threadId with the same session check as deliveryContext. When + // targeting another session, opts.agentThreadId belongs to the current + // session's thread and must not be written into the sentinel — it would + // cause scheduleRestartSentinelWake to deliver to the wrong thread. const threadId = - opts?.agentThreadId != null ? String(opts.agentThreadId) : extracted.threadId; + !isTargetingOtherSession && opts?.agentThreadId != null + ? String(opts.agentThreadId) + : extracted.threadId; const payload: RestartSentinelPayload = { kind: "restart", status: "ok",