Skip to content

Build and run agents you can see, understand and trust.

fix(webui): isolate sessions when switching agents (#2429)

DiegoCluv79983e76d1ee7
diff --git a/examples/web_ui/frontend/src/hooks/useMessages.ts b/examples/web_ui/frontend/src/hooks/useMessages.ts
index a816f9089..4b7eb9c1f 100644
--- a/examples/web_ui/frontend/src/hooks/useMessages.ts
+++ b/examples/web_ui/frontend/src/hooks/useMessages.ts
@@ -256,6 +256,7 @@ export function useMessages(
// ── Lifecycle: fetch history + open SSE stream ──────────────────
useEffect(() => {
+ setLoadedKey(null);
msgsRef.current = [];
currentReplyRef.current = null;
setMsgs([]);
@@ -527,23 +528,19 @@ export function useMessages(
[agentId, sessionId],
);
- // True from the very first render after `sessionId` changes, because
- // it compares props against what was fetched rather than tracking a
- // flag an effect has yet to flip. `msgs` is still the previous
- // session's until the effect clears it, so consumers must render the
- // loading state in preference to `msgs`.
- const loading =
- agentId !== null && sessionId !== null && loadedKey !== `${agentId}:${sessionId}`;
+ const currentKey = agentId !== null && sessionId !== null ? `${agentId}:${sessionId}` : null;
+ const ownsConversation = currentKey !== null && loadedKey === currentKey;
+ const loading = currentKey !== null && !ownsConversation;
return {
- msgs,
+ msgs: ownsConversation ? msgs : [],
loading,
- phase,
- error,
+ phase: ownsConversation ? phase : ('idle' as ReplyPhase),
+ error: ownsConversation ? error : null,
send,
onUserConfirm,
onSubagentConfirm,
- subagentHitl,
+ subagentHitl: ownsConversation ? subagentHitl : [],
abort,
interrupt,
};
diff --git a/examples/web_ui/frontend/src/hooks/useSessions.ts b/examples/web_ui/frontend/src/hooks/useSessions.ts
index 8e24c2ab6..ea5aee95d 100644
--- a/examples/web_ui/frontend/src/hooks/useSessions.ts
+++ b/examples/web_ui/frontend/src/hooks/useSessions.ts
@@ -1,4 +1,4 @@
-import { useState, useEffect, useCallback } from 'react';
+import { useState, useEffect, useCallback, useRef } from 'react';
import { sessionApi } from '../api';
import type { SessionView, CreateSessionRequest, UpdateSessionRequest } from '../api';
@@ -16,9 +16,18 @@ import type { SessionView, CreateSessionRequest, UpdateSessionRequest } from '..
* helpers that all keep the local list in sync.
*/
export function useSessions(agentId: string | null) {
- const [sessions, setSessions] = useState<SessionView[]>([]);
- const [loading, setLoading] = useState(false);
- const [error, setError] = useState<Error | null>(null);
+ const [state, setState] = useState<{
+ agentId: string | null;
+ sessions: SessionView[];
+ loading: boolean;
+ error: Error | null;
+ }>({ agentId: null, sessions: [], loading: false, error: null });
+ const currentAgentIdRef = useRef(agentId);
+ const requestIdRef = useRef(0);
+ const ownsState = state.agentId === agentId;
+ const sessions = ownsState ? state.sessions : [];
+ const loading = agentId !== null && (!ownsState || state.loading);
+ const error = ownsState ? state.error : null;
/**
* Reload the session list.
@@ -31,27 +40,50 @@ export function useSessions(agentId: string | null) {
* agent or the request failed.
*/
const refetch = useCallback(async (): Promise<SessionView[]> => {
- if (!agentId) {
- setSessions([]);
+ const requestedAgentId = agentId;
+ if (currentAgentIdRef.current !== requestedAgentId) return [];
+
+ const requestId = ++requestIdRef.current;
+ const isCurrent = () =>
+ requestId === requestIdRef.current && requestedAgentId === currentAgentIdRef.current;
+
+ if (!requestedAgentId) {
+ setState({ agentId: null, sessions: [], loading: false, error: null });
return [];
}
- setLoading(true);
- setError(null);
+ setState((prev) => ({
+ agentId: requestedAgentId,
+ sessions: prev.agentId === requestedAgentId ? prev.sessions : [],
+ loading: true,
+ error: null,
+ }));
try {
- const res = await sessionApi.list(agentId);
- setSessions(res.sessions);
+ const res = await sessionApi.list(requestedAgentId);
+ if (!isCurrent()) return [];
+ setState({
+ agentId: requestedAgentId,
+ sessions: res.sessions,
+ loading: false,
+ error: null,
+ });
return res.sessions;
} catch (e) {
- setError(e as Error);
+ if (isCurrent()) {
+ setState((prev) => ({
+ agentId: requestedAgentId,
+ sessions: prev.agentId === requestedAgentId ? prev.sessions : [],
+ loading: false,
+ error: e as Error,
+ }));
+ }
return [];
- } finally {
- setLoading(false);
}
}, [agentId]);
useEffect(() => {
+ currentAgentIdRef.current = agentId;
refetch();
- }, [refetch]);
+ }, [agentId, refetch]);
/** Creates a new session and refreshes the list. */
const create = useCallback(
diff --git a/examples/web_ui/frontend/src/pages/chat/index.tsx b/examples/web_ui/frontend/src/pages/chat/index.tsx
index 94f2c5028..63ddccbdb 100644
--- a/examples/web_ui/frontend/src/pages/chat/index.tsx
+++ b/examples/web_ui/frontend/src/pages/chat/index.tsx
@@ -132,7 +132,9 @@ const ChatPageInner = () => {
const effectiveSessionId =
focusedMember && focusedMember.session_id
? focusedMember.session_id
- : (urlSessionId ?? null);
+ : currentView
+ ? (urlSessionId ?? null)
+ : null;
// Redirect: URL is missing an agent → pick the first one and rewrite
// the URL in-place (replace so we don't pollute history).