From 972f6ae143f3e139a4c3dbda5bcb9dfd2ffc932f Mon Sep 17 00:00:00 2001 From: Dasemu Date: Thu, 8 Oct 2026 17:28:42 +0200 Subject: [PATCH] fix: never return the reference image as the generated result Take the image baseline after attaching references so composer thumbnails are excluded, and reject downloads byte-identical to a reference so the job is retried in a fresh chat. Co-Authored-By: Claude Sonnet 5.5 --- LESSONS.md | 1 + src/chatgpt/provider.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/LESSONS.md b/LESSONS.md index 62f2aaf..7b30a6a 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -10,3 +10,4 @@ Recurring mistakes and how to avoid them. Several of these come from ZenvairoPro - **One process per profile.** Chromium locks the profile directory. Stop the service before `npm run login`. - **Don't force contradictory format rules.** Prospect rejected good images because a prompt rule and a QA aspect-ratio rule disagreed. Aspect ratio is a best-effort hint here and is not validated. - **zsh and `git show rev:path`.** Quote the argument (`git show 'abc^:path'`), or zsh modifiers such as `:r` mangle it. +- **Take the image baseline after attaching references.** Reference thumbnails show up in the composer before sending and are not inside a user message, so a baseline taken earlier lets them pass as the generated result. The provider also rejects a download byte-identical to a reference. diff --git a/src/chatgpt/provider.ts b/src/chatgpt/provider.ts index 0975dcb..94d1278 100644 --- a/src/chatgpt/provider.ts +++ b/src/chatgpt/provider.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { cp, mkdtemp } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; @@ -16,6 +17,8 @@ import { buildImageRequest } from "./prompts.js"; export const MIN_IMAGE_BYTES = 20_000; +const sha256 = (data: Buffer): string => createHash("sha256").update(data).digest("hex"); + export interface ImageProvider { generateImages(generationId: string, request: ImageRequest): Promise; checkSession(): Promise; @@ -98,8 +101,9 @@ export class ChatGptImageProvider implements ImageProvider { private async generateOne(page: Page, request: ImageRequest): Promise { await openNewChat(page, this.config.url, this.diagnostics); - const baseline = await imageSources(page); await attachReferences(page, request.references, this.diagnostics); + // Taken after the upload so the reference thumbnails in the composer are never mistaken for the result. + const baseline = await imageSources(page); await sendMessage(page, buildImageRequest(request.prompt, request.aspectRatio, request.references.length), this.diagnostics); try { await waitForTurnComplete(page, this.config.turnTimeoutMs, this.diagnostics); @@ -109,6 +113,10 @@ export class ChatGptImageProvider implements ImageProvider { } const src = await waitForGeneratedImage(page, baseline, this.config.imageTimeoutMs, this.diagnostics); const image = await downloadImage(page, src); + const digest = sha256(image); + if (request.references.some((reference) => sha256(Buffer.from(reference.base64, "base64")) === digest)) { + throw new ImageGenerationError("ChatGPT returned the reference image instead of a generated one"); + } if (image.length < MIN_IMAGE_BYTES) throw new ImageGenerationError(`Downloaded image is suspiciously small (${image.length} bytes)`); if (!imageDimensions(image)) throw new ImageGenerationError("Downloaded file is not a readable PNG, JPEG or WebP image"); return image;