Fix image edit disk upload so stills persist before Generate.

Reject only real handoff frames, return 400 with reason, keep the new still in the client library, and surface missing-still errors from Generate instead of 500s.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Towsty
2026-10-12 12:21:20 -05:00
co-authored by Cursor
parent 7dcac5444c
commit 1bab5152f6
7 changed files with 169 additions and 46 deletions
+35 -8
View File
@@ -1519,17 +1519,18 @@ function purgeStrayExtensionStills(owner: string, catalog: Catalog) {
const protectedIds = new Set((catalog.drafts || []).map(draft => draft.stillId).filter(Boolean) as string[])
const originalHashes = originalInputHashes(owner, catalog)
const extHashes = extensionFrameHashes(owner, catalog)
const lastHashes = allLastFrameHashes(owner, catalog)
const keep: LibraryStill[] = []
const drop: LibraryStill[] = []
for (const still of catalog.stills) {
const clipId = clipIdFromStillFilename(still.filename)
const namedLastFrame = lastFrameStillName(still.filename)
const namedExtClip = Boolean(clipId && catalog.clips.some(clip => clip.id.toLowerCase() === clipId))
const matchesPipelineFrame = Boolean(still.hash && (lastHashes.has(still.hash) || extHashes.has(still.hash)))
// Only drop real extension-frame bytes (or named handoff files) — never a root-clip last frame
// that a user re-uploaded as a normal source photo.
const matchesExtensionFrame = Boolean(still.hash && extHashes.has(still.hash))
const isOriginal = Boolean(still.hash && originalHashes.has(still.hash))
const dropThis = !protectedIds.has(still.id)
&& (namedLastFrame || namedExtClip || (matchesPipelineFrame && !isOriginal))
&& (namedLastFrame || namedExtClip || (matchesExtensionFrame && !isOriginal))
if (dropThis) drop.push(still)
else keep.push(still)
}
@@ -1586,8 +1587,15 @@ export async function saveStill(params: {
chainIndex?: number
settings?: StillGenerationSettings
}): Promise<LibraryStill | null> {
if (lastFrameStillName(params.filename)) return null
const role = params.role === 'output' ? 'output' : 'input'
// Pipeline outputs may still use last-frame filenames; only user input uploads are rejected here.
if (role === 'input' && lastFrameStillName(params.filename)) {
throw createError({
statusCode: 400,
statusMessage: 'Rejected as video handoff: filename matches last-frame. Choose a regular source image.'
})
}
if (role !== 'input' && lastFrameStillName(params.filename)) return null
const catalog = readCatalog(params.ownerKey)
const folder = catalog.folders.find(item => item.id === params.folderId) || catalog.folders[0]
if (!folder) throw createError({ statusCode: 400, statusMessage: 'No library folder available' })
@@ -1596,7 +1604,15 @@ export async function saveStill(params: {
|| (item.familyId === params.familyId && (item.chainIndex || 0) === (params.chainIndex || 0))
const existing = catalog.stills.find(item => item.folderId === folder.id && item.hash === hash && stillRole(item) === role && sameChain(item))
if (existing) {
if (lastFrameStillName(existing.filename) || isPipelineStill(catalog, existing)) return null
if (role === 'input' && (lastFrameStillName(existing.filename) || isPipelineStill(catalog, existing))) {
throw createError({
statusCode: 400,
statusMessage: lastFrameStillName(existing.filename)
? 'Rejected as video handoff: filename matches last-frame. Choose a regular source image.'
: 'Rejected as video handoff: matches an internal pipeline still. Choose a regular source image.'
})
}
if (role !== 'input' && (lastFrameStillName(existing.filename) || isPipelineStill(catalog, existing))) return null
const hideInput = typeof params.hideInput === 'boolean' ? params.hideInput === true : existing.hideInput
const name = typeof params.name === 'string' ? params.name.trim().slice(0, 80) : existing.name
const prompt = typeof params.prompt === 'string' ? params.prompt : existing.prompt
@@ -1629,8 +1645,12 @@ export async function saveStill(params: {
}
return existing
}
if (role === 'input' && (allLastFrameHashes(params.ownerKey, catalog).has(hash) || extensionFrameHashes(params.ownerKey, catalog).has(hash))) {
return null
// Only reject real extension-frame bytes — not every root-clip last-frame hash (false handoff).
if (role === 'input' && extensionFrameHashes(params.ownerKey, catalog).has(hash)) {
throw createError({
statusCode: 400,
statusMessage: 'Rejected as video handoff: bytes match an extension frame. Choose a regular source image.'
})
}
const still: LibraryStill = {
id: crypto.randomUUID(),
@@ -1702,10 +1722,17 @@ export async function rememberInputStill(params: {
const folder = catalog.folders.find(item => item.id === params.folderId) || catalog.folders[0]
if (!folder) throw createError({ statusCode: 400, statusMessage: 'No library folder available' })
const hash = bytesHash(params.data)
if (extensionFrameHashes(params.ownerKey, catalog).has(hash)) return null
const existing = catalog.stills.find(item => item.folderId === folder.id && item.hash === hash && !isPipelineStill(catalog, item))
if (existing) return existing
if (/^start\.(jpe?g|png|webp)$/i.test(String(params.filename || '').trim())) return null
return saveStill({ ...params, role: 'input' })
try {
return await saveStill({ ...params, role: 'input' })
} catch (error: any) {
// Soft-skip internal handoff frames for video remember paths; upload APIs throw instead.
if (error?.statusCode === 400 && /video handoff/i.test(String(error?.statusMessage || ''))) return null
throw error
}
}
export async function saveRetryDraft(params: {
+9 -3
View File
@@ -1,4 +1,4 @@
import { openSync, readSync, closeSync } from 'node:fs'
import { existsSync, openSync, readSync, closeSync } from 'node:fs'
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import { getClip, getStill, clipVideoPath, stillPath } from '../library'
@@ -32,13 +32,19 @@ export async function resolveRequestSize(owner: string, request: any) {
const id = request.imageAId || request.identityStillId
if (id) {
const still = getStill(owner, id)
const path = stillPath(owner, id)
if (!existsSync(path)) {
throw createError({ statusCode: 404, statusMessage: 'That still is missing from the library' })
}
if (still.width && still.height) {
dimensions = { width: still.width, height: still.height }
} else {
// Header only — never read the full archive PNG into memory on Generate click
dimensions = imageDimensions(readImageHeader(stillPath(owner, id)))
dimensions = imageDimensions(readImageHeader(path))
}
if (!dimensions) {
throw createError({ statusCode: 400, statusMessage: 'Use a PNG, JPEG, or WebP' })
}
if (!dimensions) throw new Error('Cannot measure the source still.')
}
}
if (video && request.startClipId && !dimensions?.width) throw new Error('Cannot measure the previous clip.')