The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(chat): abort completed-turn snapshot polling on unmount
Steven Enamakel093592c3ec9c
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsxindex 6e9006a0d8..9bc57700e4 100644--- a/app/src/providers/ChatRuntimeProvider.tsx+++ b/app/src/providers/ChatRuntimeProvider.tsx@@ -620,6 +620,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {useEffect(() => {if (socketStatus !== 'connected') return;+ const snapshotPolling = new AbortController();// When a turn ends, any follow-ups the user queued behind it are about to be// dispatched by the backend as fresh turns. Nothing else persists their@@ -715,6 +716,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {};const finishChatDoneTurn = async (event: ChatDoneEvent, path: string) => {+ if (snapshotPolling.signal.aborted) return;// One store update: freeze the turn's trail, end the tail, reveal the// persisted reply in the tail's slot. See `turnSettled`.rtLog('turn_settled', { thread: event.thread_id, request: event.request_id, path });@@ -735,6 +737,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {// Persist queued follow-ups (in order, after this turn's assistant reply)// and only then clear the queue + lifecycle.await flushQueuedFollowups(event.thread_id);+ if (snapshotPolling.signal.aborted) return;dispatch(endInferenceTurn({ threadId: event.thread_id }));dispatch(clearThreadInferenceActive(event.thread_id));// Snapshot polling can outlive this completed turn. Capture the rows it@@ -752,8 +755,14 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {// ending the live lifecycle) matters: `hydrateRuntimeFromSnapshot`// intentionally refuses to overwrite an actively streaming turn.const completedSnapshot = await dispatch(- fetchAndHydrateCompletedTurnState(event.thread_id)- ).unwrap();+ fetchAndHydrateCompletedTurnState(event.thread_id, { signal: snapshotPolling.signal })+ )+ .unwrap()+ .catch(error => {+ if (snapshotPolling.signal.aborted) return null;+ throw error;+ });+ if (snapshotPolling.signal.aborted) return;if (!completedSnapshot) {dispatch(cancelUnresolvedTurnTimeline({ threadId: event.thread_id, rowIds: unresolvedRowIds })@@ -1988,6 +1997,7 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {const cleanup = subscribeChatEvents(coalesced.listeners);return () => {+ snapshotPolling.abort();rtLog('unsubscribe_chat_events');coalesced.dispose();cleanup();diff --git a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsxindex 9a5121608d..35c2044347 100644--- a/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx+++ b/app/src/providers/__tests__/ChatRuntimeProvider.test.tsx@@ -136,6 +136,9 @@ describe('ChatRuntimeProvider — dedupe, proactive resolution, mid-turn invariathread_id: 'unmounted-snapshot',request_id: 'unmounted-request',full_response: 'Delivered reply',+ rounds_used: 1,+ total_input_tokens: 0,+ total_output_tokens: 0,});});expect(threadApi.getTurnState).toHaveBeenCalledWith('unmounted-snapshot');diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.tsindex 5316aa7252..3532c1331b 100644--- a/app/src/store/chatRuntimeSlice.ts+++ b/app/src/store/chatRuntimeSlice.ts@@ -3337,10 +3337,12 @@ export const fetchAndHydrateTurnState = createAsyncThunk(*/export const fetchAndHydrateCompletedTurnState = createAsyncThunk('chatRuntime/fetchAndHydrateCompletedTurnState',- async (threadId: string, { dispatch }) => {+ async (threadId: string, { dispatch, signal }) => {for (let attempt = 0; attempt < 20; attempt += 1) {+ if (signal.aborted) return null;try {const snapshot = await threadApi.getTurnState(threadId);+ if (signal.aborted) return null;if (snapshot?.lifecycle === 'completed') {dispatch(hydrateRuntimeFromSnapshot({ snapshot }));return snapshot;@@ -3349,7 +3351,16 @@ export const fetchAndHydrateCompletedTurnState = createAsyncThunk(turnStateLog('completed snapshot fetch failed thread=%s err=%O', threadId, error);return null;}- await new Promise(resolve => window.setTimeout(resolve, 50));+ await new Promise<void>(resolve => {+ const finish = () => {+ clearTimeout(timer);+ signal.removeEventListener('abort', finish);+ resolve();+ };+ const timer = setTimeout(finish, 50);+ signal.addEventListener('abort', finish, { once: true });+ if (signal.aborted) finish();+ });}turnStateLog('completed snapshot did not arrive thread=%s', threadId);return null;