From cef99882e576fbdcd9c29756918d38e0dbae869b Mon Sep 17 00:00:00 2001 From: AAA <35992542+TNTcraftHIM@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:24:54 -0500 Subject: [PATCH] fix(files): read restored content on the first request A restored message can retain its attachment key while replacing the content. Retire the stale local source only after validating the current room and plan, then use the existing receive path in the same call. The retained-copy regression no longer needs an initial failing read; all 45 retention controls pass. --- src/client/channel/attachments.ts | 9 +++------ tests/channel-attachment-retention.test.ts | 1 - 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/client/channel/attachments.ts b/src/client/channel/attachments.ts index 5a2c62ce5..70f51fd58 100644 --- a/src/client/channel/attachments.ts +++ b/src/client/channel/attachments.ts @@ -565,12 +565,8 @@ export class ChannelAttachments { if (this.messageRemoved(key)) throw new Error("attachment_unavailable"); if (this.sessionId && !this.rooms.some(room => room.id === key.roomId && room.authorized)) throw new Error("room_unavailable"); if (inlineImage?.original) return inlineImageFile(inlineImage, attachment.name); - const source = this.sources.get(fileKeyString(key)); - if (source) { - if (!sameContent(source.attachment, attachment)) { this.withdraw(source, true); throw new Error("attachment_unavailable"); } - return source.file; - } - const id = fileKeyString(key); + const id = fileKeyString(key), source = this.sources.get(id); + if (source && sameContent(source.attachment, attachment)) return source.file; // Runtime initialization is shared; cancellation only retires this wait. let cancel!: () => void; const cancelled = new Promise((_, reject) => { @@ -585,6 +581,7 @@ export class ChannelAttachments { const room = this.rooms.find(room => room.id === key.roomId); if (!this.current(runtime) || !room?.authorized) throw new Error("room_unavailable"); if (!this.matchesPlan({ key, attachment })) throw new Error("attachment_unavailable"); + if (source && room.available) this.withdraw(source, true); let entry = this.sinks.get(id); if (entry && !sameContent(entry.attachment, attachment)) { void this.stopSink(entry, new Error("attachment_unavailable")); diff --git a/tests/channel-attachment-retention.test.ts b/tests/channel-attachment-retention.test.ts index 8c6209ab6..fca944a59 100644 --- a/tests/channel-attachment-retention.test.ts +++ b/tests/channel-attachment-retention.test.ts @@ -612,7 +612,6 @@ test.each(["in progress", "completed without retention"])("restore replaces a re await expect(old).resolves.toBe("AAA"); } update("restored"); - if (phase === "completed without retention") await expect(read(b)).rejects.toThrow("attachment_unavailable"); const next = read(b), download = files.download(key, b); await vi.waitFor(() => expect(getFileHandle).toHaveBeenCalledTimes(2)); if (phase === "in progress") await expect(old).rejects.toThrow("attachment_unavailable");