Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(ui): load artifacts a reply lists but the store lacks

Sanil00290783e7f5
diff --git a/app/src/features/conversations/hooks/__tests__/useThreadArtifactsHydration.test.tsx b/app/src/features/conversations/hooks/__tests__/useThreadArtifactsHydration.test.tsx
new file mode 100644
index 0000000000..37c5467cf6
--- /dev/null
+++ b/app/src/features/conversations/hooks/__tests__/useThreadArtifactsHydration.test.tsx
@@ -0,0 +1,84 @@
+import { configureStore } from '@reduxjs/toolkit';
+import { act, renderHook, waitFor } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { Provider } from 'react-redux';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { ARTIFACT_IDS_METADATA_KEY } from '../../../../services/api/artifactRows';
+import { listArtifactsForThread } from '../../../../services/artifactDownloadService';
+import chatRuntimeReducer from '../../../../store/chatRuntimeSlice';
+import threadReducer, { loadThreadMessages } from '../../../../store/threadSlice';
+import type { ThreadMessage } from '../../../../types/thread';
+import { useThreadArtifactsHydration } from '../useThreadArtifactsHydration';
+
+vi.mock('../../../../services/artifactDownloadService', () => ({
+ listArtifactsForThread: vi.fn(),
+}));
+
+const list = vi.mocked(listArtifactsForThread);
+const listed = (artifactId: string) => ({
+ artifactId,
+ kind: 'document' as const,
+ title: artifactId,
+ path: `/tmp/${artifactId}.md`,
+ sizeBytes: 4,
+});
+const reply = (ids: string[]): ThreadMessage => ({
+ id: 'agent:req-1',
+ content: 'Made it.',
+ type: 'text',
+ extraMetadata: { requestId: 'req-1', [ARTIFACT_IDS_METADATA_KEY]: ids },
+ sender: 'agent',
+ createdAt: '2026-10-16T06:34:47Z',
+});
+
+function setup() {
+ const store = configureStore({
+ reducer: { chatRuntime: chatRuntimeReducer, thread: threadReducer },
+ });
+ const wrapper = ({ children }: { children: ReactNode }) => (
+ <Provider store={store}>{children}</Provider>
+ );
+ renderHook(() => useThreadArtifactsHydration('t-1'), { wrapper });
+ return store;
+}
+
+describe('useThreadArtifactsHydration', () => {
+ beforeEach(() => list.mockReset());
+
+ it('loads again when a reply lists an artifact the store does not have', async () => {
+ // Opened before the artifact existed.
+ list.mockResolvedValueOnce({ ok: true, artifacts: [] });
+ const store = setup();
+ await waitFor(() => expect(list).toHaveBeenCalledTimes(1));
+
+ // Its live event was missed (the socket reconnected); the re-read reply
+ // lists it.
+ list.mockResolvedValue({ ok: true, artifacts: [listed('a-new')] });
+ act(() => {
+ store.dispatch(
+ loadThreadMessages.fulfilled({ threadId: 't-1', messages: [reply(['a-new'])] }, 'r', 't-1')
+ );
+ });
+
+ await waitFor(() =>
+ expect(store.getState().chatRuntime.artifactsByThread['t-1']?.map(a => a.artifactId)).toEqual(
+ ['a-new']
+ )
+ );
+ expect(list).toHaveBeenCalledWith('t-1');
+ });
+
+ it('does not keep loading for an id the core does not list', async () => {
+ list.mockResolvedValue({ ok: true, artifacts: [] });
+ const store = setup();
+ act(() => {
+ store.dispatch(
+ loadThreadMessages.fulfilled({ threadId: 't-1', messages: [reply(['gone'])] }, 'r', 't-1')
+ );
+ });
+ await waitFor(() => expect(list).toHaveBeenCalledTimes(2));
+ await new Promise(resolve => setTimeout(resolve, 50));
+ expect(list).toHaveBeenCalledTimes(2);
+ });
+});
diff --git a/app/src/features/conversations/hooks/useThreadArtifactsHydration.ts b/app/src/features/conversations/hooks/useThreadArtifactsHydration.ts
index 98d3007849..c7f595245b 100644
--- a/app/src/features/conversations/hooks/useThreadArtifactsHydration.ts
+++ b/app/src/features/conversations/hooks/useThreadArtifactsHydration.ts
@@ -1,8 +1,9 @@
import { useEffect } from 'react';
+import { ARTIFACT_IDS_METADATA_KEY } from '../../../services/api/artifactRows';
import { listArtifactsForThread } from '../../../services/artifactDownloadService';
import { upsertArtifactReadyForThread } from '../../../store/chatRuntimeSlice';
-import { useAppDispatch } from '../../../store/hooks';
+import { useAppDispatch, useAppSelector } from '../../../store/hooks';
/**
* Load a thread's ready artifacts from the core into
@@ -11,11 +12,29 @@ import { useAppDispatch } from '../../../store/hooks';
* a restart or on a thread the app has not seen yet.
*
* Where each card renders comes from the thread's artifact rows, not from
- * here (`services/api/artifactRows.ts`).
+ * here (`services/api/artifactRows.ts`). A reply can list an artifact the
+ * store does not have — its live event went to a socket that has since
+ * reconnected, and the reconnect re-read the messages but not the artifacts —
+ * so whenever the thread's replies list an id the store lacks, the artifacts
+ * are loaded again. The rows are the record; the live event only makes the
+ * card appear sooner.
*/
export function useThreadArtifactsHydration(threadId: string | null | undefined): void {
const dispatch = useAppDispatch();
const normalizedThreadId = threadId?.trim() ?? '';
+ const missingKey = useAppSelector(state => {
+ if (!normalizedThreadId) return '';
+ const known = new Set(
+ (state.chatRuntime.artifactsByThread[normalizedThreadId] ?? []).map(a => a.artifactId)
+ );
+ const missing = new Set<string>();
+ for (const message of state.thread.messagesByThreadId[normalizedThreadId] ?? []) {
+ const ids = message.extraMetadata?.[ARTIFACT_IDS_METADATA_KEY];
+ if (!Array.isArray(ids)) continue;
+ for (const id of ids) if (typeof id === 'string' && !known.has(id)) missing.add(id);
+ }
+ return [...missing].sort().join('\n');
+ });
useEffect(() => {
if (!normalizedThreadId) return;
@@ -33,6 +52,7 @@ export function useThreadArtifactsHydration(threadId: string | null | undefined)
console.debug('[artifact] hydrating thread artifacts', {
threadId: normalizedThreadId,
count: outcome.artifacts.length,
+ missing: missingKey ? missingKey.split('\n').length : 0,
});
for (const artifact of outcome.artifacts) {
dispatch(
@@ -51,5 +71,7 @@ export function useThreadArtifactsHydration(threadId: string | null | undefined)
return () => {
cancelled = true;
};
- }, [dispatch, normalizedThreadId]);
+ // `missingKey` re-runs the load when a reply lists an artifact the store
+ // lacks; it settles to '' once loaded, so this does not loop.
+ }, [dispatch, normalizedThreadId, missingKey]);
}