Skip to content

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

fix(assistant-ui): serialize concurrent file drops during attachment ingestion

Steven Enamakel0b3f5752d9c5
diff --git a/app/src/components/assistant-ui/thread.tsx b/app/src/components/assistant-ui/thread.tsx
index e4e76739aa..c68bbd5385 100644
--- a/app/src/components/assistant-ui/thread.tsx
+++ b/app/src/components/assistant-ui/thread.tsx
@@ -179,7 +179,7 @@ export type ThreadComponents = {
* attachment capability — which is every runtime that keeps attachments on
* the host side, as this app does.
*/
- onComposerFiles?: ((files: FileList | File[] | null) => void) | undefined;
+ onComposerFiles?: ((files: FileList | File[] | null) => void | Promise<void>) | undefined;
/**
* Whether the host can take files right now (feature enabled, composer
* unlocked, budget left). Drives the drag affordance only; the host still
@@ -267,6 +267,10 @@ function filesFromDrop(dataTransfer: DataTransfer | null): File[] {
function useThreadFileDrop() {
const { onComposerFiles, canAcceptComposerFiles } = useContext(ThreadComponentsContext);
const [isDraggingFiles, setIsDraggingFiles] = useState(false);
+ // Attachment validation updates host state asynchronously. Keep drops in
+ // arrival order so a second batch cannot validate against stale attachments
+ // or overwrite the first batch while it is still being processed.
+ const ingestQueueRef = useRef<Promise<void>>(Promise.resolve());
const isFileDrag = (event: React.DragEvent) =>
Array.from(event.dataTransfer?.types ?? []).includes('Files');
@@ -298,8 +302,13 @@ function useThreadFileDrop() {
debug('[assistant-composer] drop: file drag carried no readable files');
return;
}
- debug('[assistant-composer] drop: ingesting %d file(s)', files.length);
- onComposerFiles(files);
+ debug('[assistant-composer] drop: queueing %d file(s) for ingest', files.length);
+ ingestQueueRef.current = ingestQueueRef.current
+ .catch(() => undefined)
+ .then(() => onComposerFiles(files))
+ .catch(error => {
+ debug('[assistant-composer] drop: file ingest failed: %o', error);
+ });
};
return { isDraggingFiles, dropHandlers: { onDragOver, onDragLeave, onDrop } };
diff --git a/app/src/features/conversations/components/AssistantUiChat.slots.test.tsx b/app/src/features/conversations/components/AssistantUiChat.slots.test.tsx
index 6543bec56e..5ad393050b 100644
--- a/app/src/features/conversations/components/AssistantUiChat.slots.test.tsx
+++ b/app/src/features/conversations/components/AssistantUiChat.slots.test.tsx
@@ -179,4 +179,35 @@ describe('assistant-ui composer slots', () => {
expect(onAttachFiles).toHaveBeenCalledWith([file]);
expect(composerShell().getAttribute('data-dragging')).toBeNull();
});
+
+ it('serializes rapid thread drops while attachment ingestion is pending', async () => {
+ const store = buildStore();
+ let finishFirst!: () => void;
+ const firstFinished = new Promise<void>(resolve => {
+ finishFirst = resolve;
+ });
+ const onAttachFiles = vi.fn(() => firstFinished);
+ render(
+ <Provider store={store}>
+ {chat(undefined, { attachmentsEnabled: true, onAttachFiles })}
+ </Provider>
+ );
+
+ const drop = (name: string) =>
+ fireEvent.drop(threadViewport(), {
+ dataTransfer: { types: ['Files'], files: [new File(['file'], name)], items: [] },
+ });
+
+ drop('first.txt');
+ await Promise.resolve();
+ expect(onAttachFiles).toHaveBeenCalledTimes(1);
+ drop('second.txt');
+ await Promise.resolve();
+ expect(onAttachFiles).toHaveBeenCalledTimes(1);
+
+ finishFirst();
+ await firstFinished;
+ await Promise.resolve();
+ expect(onAttachFiles).toHaveBeenCalledTimes(2);
+ });
});
diff --git a/app/src/features/conversations/components/AssistantUiChat.tsx b/app/src/features/conversations/components/AssistantUiChat.tsx
index 01ad578c1e..cc20043846 100644
--- a/app/src/features/conversations/components/AssistantUiChat.tsx
+++ b/app/src/features/conversations/components/AssistantUiChat.tsx
@@ -270,7 +270,7 @@ export function AssistantUiChat({
// the picker uses. Stable like the slots above, and for the same reason: it
// is handed to `thread.tsx` through the components object.
const handleComposerFiles = useCallback((files: FileList | File[] | null) => {
- void slotPropsRef.current.onAttachFiles(files);
+ return slotPropsRef.current.onAttachFiles(files);
}, []);
const components: ThreadComponents = useMemo(
diff --git a/app/test/e2e/specs/file-drop-guard.spec.ts b/app/test/e2e/specs/file-drop-guard.spec.ts
index 449d3fc8c6..4e3061d3e7 100644
--- a/app/test/e2e/specs/file-drop-guard.spec.ts
+++ b/app/test/e2e/specs/file-drop-guard.spec.ts
@@ -36,15 +36,15 @@ describe('File drop guard', () => {
expect(result).toEqual({ dragOverPrevented: true, dropPrevented: true, fileCount: 1 });
});
- it('claims a file drop on the composer and adds it as an attachment', async () => {
+ it('claims a file drop on the thread viewport and adds it as an attachment', async () => {
await navigateViaHash('/chat');
expect(await clickByTitle('New thread', 8_000)).toBe(true);
- const composer = await waitForDataSlot('aui_composer-shell');
- const result = await dispatchFileDrop(composer, {
+ const viewport = await waitForDataSlot('aui_thread-viewport');
+ const result = await dispatchFileDrop(viewport, {
name: 'thread-drop.txt',
type: 'text/plain',
- contents: 'dropped onto the composer',
+ contents: 'dropped onto the transcript',
});
expect(result).toEqual({ dragOverPrevented: true, dropPrevented: true, fileCount: 1 });