Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: invalidate stale image picks when inputs change

Adam Eivy77638de23d65
diff --git a/client/src/hooks/useImageGenForm.js b/client/src/hooks/useImageGenForm.js
index bed66288cc..476acb1f07 100644
--- a/client/src/hooks/useImageGenForm.js
+++ b/client/src/hooks/useImageGenForm.js
@@ -288,6 +288,7 @@ export function useImageGenForm({ searchParams, setSearchParams, backend }) {
if (get('guidance')) setGuidance(get('guidance'));
if (get('quantize')) setQuantize(get('quantize'));
if (initFile && initImage.source == null) {
+ pickSeqRef.current.init += 1;
setInitImage({ source: 'gallery', file: null, name: initFile, previewUrl: `/data/images/${initFile}` });
wantI2iModeRef.current = true;
}
@@ -364,12 +365,14 @@ export function useImageGenForm({ searchParams, setSearchParams, backend }) {
};
const handleClearInitImage = () => {
+ pickSeqRef.current.init += 1;
revokeIfBlob(initImage.previewUrl);
setInitImage({ source: null, file: null, name: null, previewUrl: null });
};
const handlePickGalleryInitImage = (item) => {
if (!item?.filename) return;
+ pickSeqRef.current.init += 1;
if (item.raw) {
handleRemix(item.raw, { applyModel: false });
setPrompt(item.raw.prompt || '');
@@ -396,6 +399,7 @@ export function useImageGenForm({ searchParams, setSearchParams, backend }) {
};
const handleClearReferenceImage = (slotIndex) => {
+ pickSeqRef.current.refs[slotIndex] = (pickSeqRef.current.refs[slotIndex] ?? 0) + 1;
setReferenceImages((previous) => {
const next = [...previous];
revokeIfBlob(next[slotIndex]?.previewUrl);
@@ -421,11 +425,14 @@ export function useImageGenForm({ searchParams, setSearchParams, backend }) {
const handlePickGalleryReferenceImage = async (slotIndex, item) => {
if (!item?.filename) return;
+ const sequences = pickSeqRef.current.refs;
+ const myPick = sequences[slotIndex] = (sequences[slotIndex] ?? 0) + 1;
+ const isCurrent = () => mountedRef.current && myPick === pickSeqRef.current.refs[slotIndex];
const file = await galleryImageToFile(item.filename).catch((error) => {
- toast.error(`Failed to load reference image: ${error.message}`);
+ if (isCurrent()) toast.error(`Failed to load reference image: ${error.message}`);
return null;
});
- if (!file) return;
+ if (!file || !isCurrent()) return;
setReferenceImages((previous) => {
const next = [...previous];
revokeIfBlob(next[slotIndex]?.previewUrl);
@@ -558,6 +565,7 @@ export function useImageGenForm({ searchParams, setSearchParams, backend }) {
}, [i2iCapable, availableBackends, switchMode]);
const setGalleryInitImage = useCallback((filename) => {
+ pickSeqRef.current.init += 1;
revokeIfBlob(initImagePreviewRef.current);
setInitImage({ source: 'gallery', file: null, name: filename, previewUrl: `/data/images/${filename}` });
setInitImageStrength(0.4);
diff --git a/client/src/pages/ImageGen.objectUrls.test.jsx b/client/src/pages/ImageGen.objectUrls.test.jsx
index d78bc94974..9e16fcdf4a 100644
--- a/client/src/pages/ImageGen.objectUrls.test.jsx
+++ b/client/src/pages/ImageGen.objectUrls.test.jsx
@@ -28,12 +28,13 @@ const InitPicker = ({ initImage, onPick, onClear, onBrowse }) => (
<span data-testid="init-url">{initImage.previewUrl || ''}</span>
</div>
);
-const RefPicker = ({ referenceImages, onPick, onClear }) => (
+const RefPicker = ({ referenceImages, onPick, onClear, onBrowse }) => (
<div>
{referenceImages.map((slot, i) => (
<div key={i}>
<button type="button" onClick={() => onPick(i, { target: { files: [nextFile()] } })}>{`pick-ref-${i}`}</button>
<button type="button" onClick={() => onClear(i)}>{`clear-ref-${i}`}</button>
+ <button type="button" onClick={() => onBrowse(i)}>{`browse-ref-${i}`}</button>
<span data-testid={`ref-url-${i}`}>{slot.previewUrl || ''}</span>
</div>
))}
@@ -225,6 +226,40 @@ describe('ImageGen object-URL lifecycle', () => {
expect(objectUrl.revoked).toEqual([blobUrl]);
});
+ it('does not restore a pending upload after clear or gallery replacement', async () => {
+ const pending = [];
+ window.createImageBitmap = vi.fn((...args) => (args.length === 2
+ ? new Promise((_, reject) => { pending.push(() => reject(new Error('no decoder'))); })
+ : Promise.reject(new Error('no decoder'))));
+ await mount();
+ await click('pick-init');
+ await click('pick-ref-0');
+ await click('clear-init');
+ await click('clear-ref-0');
+ await act(async () => { pending.splice(0).forEach(finish => finish()); });
+ expect(screen.getByTestId('init-url')).toBeEmptyDOMElement();
+ expect(screen.getByTestId('ref-url-0')).toBeEmptyDOMElement();
+ expect(objectUrl.created).toEqual([]);
+
+ await click('pick-init');
+ await click('browse-init');
+ await click('gallery-select');
+ await act(async () => { pending.splice(0).forEach(finish => finish()); });
+ expect(screen.getByTestId('init-url')).toHaveTextContent('/data/images/gallery-pick.png');
+ expect(objectUrl.created).toEqual([]);
+ });
+
+ it('does not restore a gallery reference after it is cleared', async () => {
+ let finishFetch;
+ restore.push(stub(globalThis, 'fetch', vi.fn(() => new Promise(resolve => { finishFetch = resolve; }))));
+ await mount();
+ await click('browse-ref-0');
+ await click('gallery-select');
+ await click('clear-ref-0');
+ await act(async () => finishFetch({ ok: true, blob: async () => new Blob(['image'], { type: 'image/png' }) }));
+ expect(screen.getByTestId('ref-url-0')).toBeEmptyDOMElement();
+ });
+
// Two picks that overlap in the EXIF-normalization await must not each mint
// a url: only the last setState survives, so the loser's url would be
// unreachable from state, the clear path, and the unmount sweep.