Skip to content

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

fix(chat): serialise composer file ingest

Shanu0c8f66012738
diff --git a/app/src/features/conversations/Conversations.tsx b/app/src/features/conversations/Conversations.tsx
index 59bc055a7a..1140e0de69 100644
--- a/app/src/features/conversations/Conversations.tsx
+++ b/app/src/features/conversations/Conversations.tsx
@@ -289,6 +289,13 @@ const Conversations = ({
const [inputValue, setInputValue] = useState('');
const [attachments, setAttachments] = useState<Attachment[]>([]);
+ // What ingest counts its budget against. Tracks state on every render (so a
+ // removal or a send's clear is picked up) and is written synchronously as each
+ // file is admitted, which is what keeps two overlapping ingests honest.
+ const attachmentsRef = useRef(attachments);
+ attachmentsRef.current = attachments;
+ // Tail of the ingest queue; see `handleAttachFiles`.
+ const ingestQueueRef = useRef<Promise<void>>(Promise.resolve());
const fileInputRef = useRef<HTMLInputElement>(null);
// Imperative handle onto the transcript's own background-processes panel
// (its state now lives inside `ChatThreadView`) so the header badge below
@@ -984,12 +991,17 @@ const Conversations = ({
return true;
};
- const handleAttachFiles = async (files: FileList | File[] | null) => {
+ const ingestFiles = async (files: FileList | File[] | null) => {
if (!files) return;
- let acceptedFileCount = attachments.filter(attachment => attachment.kind === 'file').length;
+ // Counted from the ref, never from the `attachments` render snapshot: the
+ // composer now has three ingest entry points (picker, drop, paste) and two
+ // can fire before React re-renders. Both would then seed their budget from
+ // the same snapshot and each admit a full quota.
+ const admitted = attachmentsRef.current;
+ let acceptedFileCount = admitted.filter(attachment => attachment.kind === 'file').length;
// Images and videos share one image-marker budget (video = its frames), so
// track consumed markers rather than per-kind counts.
- let acceptedImageMarkers = attachments.reduce(
+ let acceptedImageMarkers = admitted.reduce(
(sum, attachment) => sum + imageMarkerCost(attachment.kind),
0
);
@@ -1040,10 +1052,30 @@ const Conversations = ({
} else {
acceptedImageMarkers += imageMarkerCost(result.attachment.kind);
}
+ // Ref first, synchronously: the next queued run counts what this one just
+ // took, without waiting for React to commit the state update.
+ attachmentsRef.current = [...attachmentsRef.current, result.attachment];
setAttachments(prev => [...prev, result.attachment]);
}
};
+ /**
+ * Serialises ingest so overlapping gestures cannot both validate against the
+ * same budget — a paste landing while a dropped file is still being read, for
+ * instance. Each run starts only once the one before it has finished writing
+ * `attachmentsRef`, so the budget it counts from is current.
+ */
+ const handleAttachFiles = (files: FileList | File[] | null): Promise<void> => {
+ const run = ingestQueueRef.current.then(() => ingestFiles(files));
+ // The queue must survive a rejected run, or one failure wedges every later
+ // attachment. Errors still surface to the caller through `run`.
+ ingestQueueRef.current = run.then(
+ () => undefined,
+ () => undefined
+ );
+ return run;
+ };
+
const handleSendMessage = async (text?: string) => {
// Guard double-submit to the SAME thread only; a send to another thread
// may proceed concurrently.
diff --git a/app/src/pages/__tests__/Conversations.attachments.test.tsx b/app/src/pages/__tests__/Conversations.attachments.test.tsx
index c1cc9a913c..69494d0601 100644
--- a/app/src/pages/__tests__/Conversations.attachments.test.tsx
+++ b/app/src/pages/__tests__/Conversations.attachments.test.tsx
@@ -10,6 +10,7 @@ import { MemoryRouter } from 'react-router-dom';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { SidebarSlotOutlet, SidebarSlotProvider } from '../../components/layout/shell/SidebarSlot';
+import { ATTACHMENT_MAX_IMAGES } from '../../lib/attachments';
import agentProfileReducer from '../../store/agentProfileSlice';
import chatRuntimeReducer from '../../store/chatRuntimeSlice';
import socketReducer from '../../store/socketSlice';
@@ -440,6 +441,40 @@ describe('Conversations — attachment feature', () => {
expect(document.querySelector('[data-slot="aui_composer-shell"] img')).toBeNull();
});
+ // Three ingest entry points can now fire before React re-renders. Seeding the
+ // budget from the `attachments` render snapshot let each of two overlapping
+ // gestures admit a full quota, so 3 + 3 images landed as 6 against a cap of 4.
+ it('holds the image budget when a drop and a paste overlap', async () => {
+ const { textarea } = await renderWithSelectedThread();
+
+ const shell = document.querySelector('[data-slot="aui_composer-shell"]') as HTMLElement;
+ const dropped = Array.from({ length: 3 }, (_, i) =>
+ makeFile(`ovl-d${i}.png`, 'image/png', 512)
+ );
+ const pasted = Array.from({ length: 3 }, (_, i) => makeFile(`ovl-p${i}.png`, 'image/png', 512));
+
+ // Both in the same tick, with no await between them — that is the race.
+ await act(async () => {
+ fireEvent.drop(shell, { dataTransfer: { files: dropped, types: ['Files'] } });
+ fireEvent.paste(textarea, {
+ clipboardData: {
+ items: pasted.map(file => ({ kind: 'file', type: 'image/png', getAsFile: () => file })),
+ },
+ });
+ });
+
+ // Count only once both ingests have settled. Asserting earlier measures how
+ // far the reads happen to have got, not the budget: mid-flight this reads 2
+ // whether or not the queue is serialised.
+ await act(async () => {
+ await new Promise(resolve => setTimeout(resolve, 400));
+ });
+
+ // Unserialised, both runs seed from the same empty snapshot and admit three
+ // each — six against a cap of four.
+ expect(screen.queryAllByText(/^ovl-/).length).toBeLessThanOrEqual(ATTACHMENT_MAX_IMAGES);
+ });
+
it('shows too-many error when selecting more than 4 images', async () => {
await renderWithSelectedThread();