Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): stop a pasted image attaching twice in the ACP composer (PRD-8233) (#3777)

Andrew Mikofalvye5b8f74a3f20
diff --git a/.changeset/prd-8233-acp-paste-attaches-twice.md b/.changeset/prd-8233-acp-paste-attaches-twice.md
new file mode 100644
index 000000000..3eb63ff34
--- /dev/null
+++ b/.changeset/prd-8233-acp-paste-attaches-twice.md
@@ -0,0 +1,11 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Pasting an image into the agent composer no longer attaches it twice. About a quarter of pastes produced two identical thumbnails from one Cmd+V, and the second had to be removed by hand.
+
+A pasted image arrives on the clipboard event through two accessors that describe the same payload: `items`, which also carries non-file entries, and `files`, which is the file-only subset of it. The composer read both and dropped anything it had already seen, keyed on the file's name, size, and last-modified time. That key is unstable for a paste. A pasted image has no file on disk, so the browser builds a fresh file object for each read and stamps its last-modified time with the clock at that moment rather than deriving it from the payload. Whenever the two reads landed in different milliseconds — measured at 4 of 15 pastes — the keys disagreed, the second copy looked like a new file, and both were attached.
+
+The composer now treats `items` as the authoritative list and only consults `files` when `items` yields nothing, which is what an older host or a synthetic event does. One payload is read once, so there is nothing to de-duplicate and no timestamp to depend on. Dragging files from Finder is unchanged; it was never affected, because a real file on disk reports the same last-modified time to both reads.
+
+Attaching the same picture twice on purpose also renders correctly now. The pending-attachment strip identified each thumbnail by filename and the leading bytes of its content, which two copies of one image share, so the two tiles collided on a single identity.
diff --git a/packages/app/src/components/acp/ThreadView.tsx b/packages/app/src/components/acp/ThreadView.tsx
index 4eda82e84..703c7b2c7 100644
--- a/packages/app/src/components/acp/ThreadView.tsx
+++ b/packages/app/src/components/acp/ThreadView.tsx
@@ -4225,13 +4225,17 @@ function PendingImageStrip({
return (
<div className="flex flex-wrap gap-2 px-3 pt-2 pb-1" data-testid="agent-thread-pending-images">
{images.map((image, index) => {
- // Per-kind key + label: `image` carries base64 data (unique per
- // upload); `file`/`folder` carry a workspace path (also stable per
- // upload); `blob` carries base64 too. Keys stay collision-free.
+ // Per-kind identity + label: `image`/`blob` carry base64 data,
+ // `file`/`folder` a workspace path. None of those is unique on its
+ // own — attaching one picture twice (deliberately, or via a host that
+ // hands the same payload back twice) yields two parts with identical
+ // name and identical leading base64. Index disambiguates: this list is
+ // append-ordered and `onRemove` already treats the index as the tile's
+ // identity, so the two agree.
const key =
image.kind === 'image' || image.kind === 'blob'
- ? `${image.name}:${image.data.slice(0, 24)}`
- : `${image.name}:${image.path}`;
+ ? `${index}:${image.name}:${image.data.slice(0, 24)}`
+ : `${index}:${image.name}:${image.path}`;
const src = image.kind === 'image' ? `data:${image.mimeType};base64,${image.data}` : null;
const label =
image.kind === 'file' || image.kind === 'folder'
diff --git a/packages/app/src/lib/acp/image-attachment.dom.test.ts b/packages/app/src/lib/acp/image-attachment.dom.test.ts
index 8ff18e384..e486a79b1 100644
--- a/packages/app/src/lib/acp/image-attachment.dom.test.ts
+++ b/packages/app/src/lib/acp/image-attachment.dom.test.ts
@@ -133,18 +133,67 @@ describe('collectImageFiles', () => {
expect(out.map((f) => f.name)).toEqual(['a.png']);
});
- test('deduplicates a file that shows up on both items and files', () => {
+ /** One payload exposed on both accessors, as a real host exposes it: a
+ * SEPARATE `File` object per read. Returning the same instance twice (the
+ * shape this test used to have) cannot fail — the reads are identical by
+ * construction, so it pinned the stub rather than the browser. */
+ function pastedOnBothAccessors(fromItems: File, fromFiles: File): DataTransfer {
+ return {
+ items: [{ kind: 'file' as const, type: fromItems.type, getAsFile: () => fromItems }],
+ files: {
+ length: 1,
+ item: (i: number) => (i === 0 ? fromFiles : null),
+ 0: fromFiles,
+ } as unknown as FileList,
+ } as unknown as DataTransfer;
+ }
+
+ test('reads one payload once: items wins and the files mirror is not consulted', () => {
+ const at = (lastModified: number) =>
+ new File([new Uint8Array([1])], 'a.png', { type: 'image/png', lastModified });
+ const fromItems = at(1_700_000_000_000);
+ const fromFiles = at(1_700_000_000_000);
+ const out = collectImageFiles(pastedOnBothAccessors(fromItems, fromFiles));
+ expect(out).toHaveLength(1);
+ expect(out[0]).toBe(fromItems);
+ });
+
+ /** A clipboard image has no file on disk, so Chromium builds a fresh `File`
+ * for each read of the DataTransfer and stamps `lastModified` with the clock
+ * at construction — a clock reading, not a property of the payload. The two
+ * reads therefore disagree whenever they straddle a millisecond (measured: 4
+ * of 15 pastes, Chromium 151). Reading `items` alone is what makes that
+ * disagreement unobservable; any traversal that also read `files` and tried
+ * to match the two by value would attach the image twice. */
+ test('one payload stays one file even when the two reads disagree on lastModified', () => {
+ const at = (lastModified: number) =>
+ new File([new Uint8Array([1])], 'image.png', { type: 'image/png', lastModified });
+ const fromItems = at(1_787_337_196_630);
+ const out = collectImageFiles(pastedOnBothAccessors(fromItems, at(1_787_337_196_629)));
+ expect(out).toHaveLength(1);
+ expect(out[0]).toBe(fromItems);
+ });
+
+ /** The `files` fallback is the only reader of `dataTransfer.files`, and it is
+ * what serves an older host that leaves `items` unpopulated — and the
+ * clipboard-moved-on case, where `getAsFile()` returns null for every item
+ * but the pre-race `files` snapshot is still good. */
+ test('falls back to files when items yields nothing', () => {
+ const png = makeFile(new Uint8Array([1]), 'a.png', 'image/png');
+ expect(collectImageFiles(dataTransfer([png])).map((f) => f.name)).toEqual(['a.png']);
+ });
+
+ test('falls back to files when every items entry yields a null File', () => {
const png = makeFile(new Uint8Array([1]), 'a.png', 'image/png');
const dt = {
- items: [{ kind: 'file' as const, type: 'image/png', getAsFile: () => png }],
+ items: [{ kind: 'file' as const, type: 'image/png', getAsFile: () => null }],
files: {
length: 1,
item: (i: number) => (i === 0 ? png : null),
0: png,
} as unknown as FileList,
} as unknown as DataTransfer;
- const out = collectImageFiles(dt);
- expect(out).toHaveLength(1);
+ expect(collectImageFiles(dt).map((f) => f.name)).toEqual(['a.png']);
});
test('null DataTransfer returns []', () => {
diff --git a/packages/app/src/lib/acp/image-attachment.ts b/packages/app/src/lib/acp/image-attachment.ts
index ae41c60cd..e4ea6e6b9 100644
--- a/packages/app/src/lib/acp/image-attachment.ts
+++ b/packages/app/src/lib/acp/image-attachment.ts
@@ -128,9 +128,10 @@ export async function fileToImageAttachment(
/**
* Yield image files from a DataTransfer — the drop-event and paste-event
- * source. Both `items` (paste + rich drag) and `files` (plain drag) are
- * examined so a screenshot pasted from the OS clipboard is handled the
- * same as a file dragged from Finder.
+ * source. `items` is the authoritative list; `files` is consulted only when
+ * `items` yields nothing, so a screenshot pasted from the OS clipboard is
+ * handled the same as a file dragged from Finder without either being read
+ * twice.
*/
export function collectImageFiles(dataTransfer: DataTransfer | null): File[] {
return collectFiles(dataTransfer, (file) => file.type.startsWith('image/'));
@@ -139,7 +140,7 @@ export function collectImageFiles(dataTransfer: DataTransfer | null): File[] {
/**
* Yield ALL files (any mime) from a DataTransfer — the wider path the
* agent-thread panel uses so a code file dragged from Finder attaches the
- * same way a PNG does. Same items+files traversal + dedup as the
+ * same way a PNG does. Same items-first, files-as-fallback traversal as the
* image-only variant. Excludes zero-byte entries (dragging a folder from
* Finder emits an entry with size 0 that browsers can't read as bytes).
*/
@@ -149,26 +150,42 @@ export function collectAllFiles(dataTransfer: DataTransfer | null): File[] {
function collectFiles(dataTransfer: DataTransfer | null, accept: (file: File) => boolean): File[] {
if (dataTransfer === null) return [];
- const seen = new Set<string>();
const out: File[] = [];
const remember = (file: File | null) => {
if (file === null) return;
if (!accept(file)) return;
- const key = `${file.name}|${file.size}|${file.lastModified ?? 0}`;
- if (seen.has(key)) return;
- seen.add(key);
out.push(file);
};
+ // `items` is the authoritative list: per the HTML drag-and-drop spec,
+ // `DataTransfer.files` IS the `kind: 'file'` subset of `DataTransfer.items`,
+ // so the two accessors describe one payload set rather than two. Reading
+ // both and de-duplicating by value is therefore the wrong shape — and it
+ // could not be made correct, because a clipboard payload has no file on
+ // disk: the host builds a fresh `File` per read and stamps `lastModified`
+ // with the clock at construction, so the two reads of one pasted image
+ // disagree on it whenever they straddle a millisecond, and any key carrying
+ // that field misses, so one paste lands as two attachments.
+ let itemsYieldedFiles = false;
const items = dataTransfer.items;
if (items) {
for (let i = 0; i < items.length; i += 1) {
const it = items[i];
- if (it && it.kind === 'file') remember(it.getAsFile());
+ if (!it || it.kind !== 'file') continue;
+ const file = it.getAsFile();
+ if (file === null) continue;
+ itemsYieldedFiles = true;
+ remember(file);
}
}
- const files = dataTransfer.files;
- if (files) {
- for (let i = 0; i < files.length; i += 1) remember(files[i] ?? null);
+ // Fall back to `files` only when `items` produced nothing — an older host
+ // that leaves `items` unpopulated, or a synthetic DataTransfer. Guarding on
+ // "yielded a File" rather than "had file-kind entries" keeps the fallback
+ // live when `getAsFile()` returns null for every entry.
+ if (!itemsYieldedFiles) {
+ const files = dataTransfer.files;
+ if (files) {
+ for (let i = 0; i < files.length; i += 1) remember(files[i] ?? null);
+ }
}
return out;
}