From 243b86d29dbb03cdc155fe314f144c98b80e4d51 Mon Sep 17 00:00:00 2001 From: joshavant <830519+joshavant@users.noreply.github.com> Date: Thu, 9 Apr 2026 14:51:46 -0500 Subject: [PATCH] UI: tighten stale-response guards in agents controller --- ui/src/ui/controllers/agents.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/ui/src/ui/controllers/agents.ts b/ui/src/ui/controllers/agents.ts index e1780d712d1..099fde325fd 100644 --- a/ui/src/ui/controllers/agents.ts +++ b/ui/src/ui/controllers/agents.ts @@ -44,6 +44,10 @@ export type AgentsState = { export type AgentsConfigSaveState = AgentsState & ConfigState; +function hasSelectedAgentMismatch(state: AgentsState, agentId: string): boolean { + return Boolean(state.agentsSelectedId && state.agentsSelectedId !== agentId); +} + export async function loadAgents(state: AgentsState) { if (!state.client || !state.connected) { return; @@ -80,6 +84,9 @@ export async function loadToolsCatalog(state: AgentsState, agentId: string) { if (!state.client || !state.connected || !resolvedAgentId) { return; } + const shouldIgnoreResponse = () => + state.toolsCatalogLoadingAgentId !== resolvedAgentId || + hasSelectedAgentMismatch(state, resolvedAgentId); if (state.toolsCatalogLoading && state.toolsCatalogLoadingAgentId === resolvedAgentId) { return; } @@ -92,18 +99,12 @@ export async function loadToolsCatalog(state: AgentsState, agentId: string) { agentId: resolvedAgentId, includePlugins: true, }); - if (state.toolsCatalogLoadingAgentId !== resolvedAgentId) { - return; - } - if (state.agentsSelectedId && state.agentsSelectedId !== resolvedAgentId) { + if (shouldIgnoreResponse()) { return; } state.toolsCatalogResult = res; } catch (err) { - if (state.toolsCatalogLoadingAgentId !== resolvedAgentId) { - return; - } - if (state.agentsSelectedId && state.agentsSelectedId !== resolvedAgentId) { + if (shouldIgnoreResponse()) { return; } state.toolsCatalogResult = null; @@ -131,6 +132,9 @@ export async function loadToolsEffective( if (!state.client || !state.connected || !resolvedAgentId || !resolvedSessionKey) { return; } + const shouldIgnoreResponse = () => + state.toolsEffectiveLoadingKey !== requestKey || + hasSelectedAgentMismatch(state, resolvedAgentId); if (state.toolsEffectiveLoading && state.toolsEffectiveLoadingKey === requestKey) { return; } @@ -144,19 +148,13 @@ export async function loadToolsEffective( agentId: resolvedAgentId, sessionKey: resolvedSessionKey, }); - if (state.toolsEffectiveLoadingKey !== requestKey) { - return; - } - if (state.agentsSelectedId && state.agentsSelectedId !== resolvedAgentId) { + if (shouldIgnoreResponse()) { return; } state.toolsEffectiveResultKey = requestKey; state.toolsEffectiveResult = res; } catch (err) { - if (state.toolsEffectiveLoadingKey !== requestKey) { - return; - } - if (state.agentsSelectedId && state.agentsSelectedId !== resolvedAgentId) { + if (shouldIgnoreResponse()) { return; } state.toolsEffectiveResult = null;