Skip to content

Commit 562f2ac

Browse files
committed
fix(gateway): guard webchat dispatch cleanup
1 parent ea33aac commit 562f2ac

1 file changed

Lines changed: 42 additions & 38 deletions

File tree

src/gateway/server-methods/chat.ts

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5011,54 +5011,58 @@ export const chatHandlers: GatewayRequestHandlers = {
50115011
errorMessage,
50125012
});
50135013
})
5014-
.finally(async () => {
5014+
.finally(() => {
50155015
activeRunAbort.cleanup();
50165016
clearAgentRunContext(clientRunId, lifecycleGeneration);
50175017
clearActiveChatSendDedupeRun(context.dedupe, activeChatSendDedupeKey, clientRunId);
50185018
context.removeChatRun(clientRunId, clientRunId, sessionKey);
50195019
if (!pendingDispatchLifecycleError) {
50205020
return;
50215021
}
5022-
const hasActiveRun = hasTrackedActiveSessionRun({
5023-
context,
5024-
requestedKey: rawSessionKey,
5025-
canonicalKey: sessionKey,
5026-
...(sessionKey === "global" && agentId ? { agentId } : {}),
5027-
defaultAgentId: resolveDefaultAgentId(cfg),
5028-
});
5029-
if (hasActiveRun) {
5030-
return;
5031-
}
5032-
const persisted = await persistGatewaySessionLifecycleEvent({
5033-
sessionKey,
5034-
...(sessionKey === "global" && agentId ? { agentId } : {}),
5035-
event: {
5036-
runId: clientRunId,
5037-
sessionId: pendingDispatchLifecycleError.sessionId,
5038-
lifecycleGeneration,
5039-
ts: pendingDispatchLifecycleError.endedAt,
5040-
data: {
5041-
phase: "error",
5042-
startedAt: pendingDispatchLifecycleError.startedAt,
5043-
endedAt: pendingDispatchLifecycleError.endedAt,
5044-
error: pendingDispatchLifecycleError.error,
5045-
},
5046-
},
5047-
})
5048-
.then(() => true)
5049-
.catch((persistErr) => {
5022+
const persistDispatchLifecycleError = async () => {
5023+
const dispatchError = pendingDispatchLifecycleError;
5024+
if (!dispatchError) {
5025+
return;
5026+
}
5027+
const hasActiveRun = hasTrackedActiveSessionRun({
5028+
context,
5029+
requestedKey: rawSessionKey,
5030+
canonicalKey: sessionKey,
5031+
...(sessionKey === "global" && agentId ? { agentId } : {}),
5032+
defaultAgentId: resolveDefaultAgentId(cfg),
5033+
});
5034+
if (hasActiveRun) {
5035+
return;
5036+
}
5037+
try {
5038+
await persistGatewaySessionLifecycleEvent({
5039+
sessionKey,
5040+
...(sessionKey === "global" && agentId ? { agentId } : {}),
5041+
event: {
5042+
runId: clientRunId,
5043+
sessionId: dispatchError.sessionId,
5044+
lifecycleGeneration,
5045+
ts: dispatchError.endedAt,
5046+
data: {
5047+
phase: "error",
5048+
startedAt: dispatchError.startedAt,
5049+
endedAt: dispatchError.endedAt,
5050+
error: dispatchError.error,
5051+
},
5052+
},
5053+
});
5054+
emitSessionsChanged(context, {
5055+
sessionKey,
5056+
...(agentId ? { agentId } : {}),
5057+
reason: "chat.dispatch-error",
5058+
});
5059+
} catch (persistErr: unknown) {
50505060
context.logGateway.warn(
50515061
`webchat session lifecycle persist failed after error: ${formatForLog(persistErr)}`,
50525062
);
5053-
return false;
5054-
});
5055-
if (persisted) {
5056-
emitSessionsChanged(context, {
5057-
sessionKey,
5058-
...(agentId ? { agentId } : {}),
5059-
reason: "chat.dispatch-error",
5060-
});
5061-
}
5063+
}
5064+
};
5065+
void persistDispatchLifecycleError();
50625066
});
50635067
} catch (err) {
50645068
activeRunAbort.cleanup({ force: true });

0 commit comments

Comments
 (0)