Skip to content

Studio Images: preserve gallery selections during generation - #17719

Merged
danielhanchen merged 9 commits into
unslothai:mainfrom
oobabooga:studio-images-preserve-selection
Nov 14, 2026
Merged

danielhanchen merged 9 commits into
unslothai:mainfrom
oobabooga:studio-images-preserve-selection

Conversation

@oobabooga

@oobabooga oobabooga commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Problem

While a generation runs with live preview on, the Images page stage always shows the live preview. Clicking another image in the strip selects it, but the stage doesn't change, so that image's Recipe and the full-screen viewer can't be opened until the whole queue finishes. Reported in Unsloth Desktop: Bug/Feature Requests on r/unsloth ("having to wait until images queued for generation were completed in order to evaluate other images and their associated recipes").

Cause

  • The stage renders livePreviewSrc ? <live preview> : <selected image + toolbar>, and livePreviewSrc is set whenever busy === "generating". A selection never outranks it, and each queued job sets busy again.
  • With Runs above 1, each finished run selects its new image, replacing the user's pick.
  • After a run, the gallery refresh replaces the strip with its first 50 records, so a selection further down falls back to the newest image.

Fix

  • An image picked during a run (strip click, or "View in Images" from the Library) takes the stage, with its Recipe, viewer and Download. The progress card stays visible, and the strip's live tile keeps the preview.
  • The live tile is now a button that returns the stage to the live preview.
  • Finished runs no longer replace a pick.
  • The pick is released on a new Generate, or once the page knows nothing is running or queued (pickReleased in lib/generation-queue.ts). A freshly mounted page waits for its first status and queue responses before treating itself as idle.
  • After a run, the gallery refresh reloads every loaded strip page and keeps the selected image even when new records push it past the old end (collectGalleryWindow in lib/gallery-window.ts). It re-reads the selection after each request, so a pick made mid-refresh is kept too.

Boundaries

  • Without a pick during a run, behavior is unchanged.
  • With live preview off, a pick is also no longer replaced when a run finishes. I didn't test this mode live.
  • The refresh keeps at most one page of new records ahead of the selection; more than 50 new images between two refreshes can still drop it, as on main.
  • Showing recipes inside the Library preview is out of scope.

Verification

Before and after, main (7bf13f21c3b) vs this PR, with live preview on.

Two queued jobs; an older image is clicked while job 1 runs:

Queued run with live preview: on main the clicked lighthouse stays behind the live preview; with this PR it takes the stage with its Recipe

Runs 3; the balloon is clicked during run 1, shot while run 2 runs:

Runs 3: on main run 1's image takes the selection from the picked balloon; with this PR the balloon stays on the stage

Live in Studio (Z-Image-Turbo GGUF, RTX 6000 Ada), driven with Playwright:

Scenario main This PR
3 queued jobs, pick an older image during job 1 Live preview stays, no Recipe Picked image and Recipe on the stage
Same pick while job 2 runs Live preview again Pick still shown
Click the strip's live tile Not clickable Live preview returns
Runs 3, pick during run 1, run 1 finishes Selection replaced by the new image Pick kept

Edge cases, checked on this branch:

  • "View in Images" from the Library during a run, with the status responses delayed 8 s: the pick stays on the stage after they arrive.
  • Pick Kernel tuning and benchmarking #131 of 150 loaded images: kept after job 1's refresh. Main's refresh collapses the strip to 50, and scrolling reloads only 100.
  • A gallery that ends at 100 images, pick NameError: name 'residual' is not defined #100: kept after a finished job pushes it to Quick fixes #101, including when it is picked while that refresh is in flight.
  • After the queue drains, or after a pick made while idle, a newly queued run opens on its live preview.

npm run typecheck passes. New unit tests cover pickReleased and collectGalleryWindow, including the pushed-out selection and a pick during a pending request. The page has no DOM test harness, so images-gallery-thumbnails.test.ts guards how it wires these rules at the source level.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T01:01:04.526204Z ae7abc1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 032cd814ca

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +2853 to +2854
() => stripEpoch.current === epoch,
).catch(() => void loadGallery());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retry completion refreshes after concurrent shelf mutations

If a user pins an image while the last post-generation refresh is in flight, handleTogglePin increments stripEpoch, causing resyncWindow to return without applying the generated records. That is a normal return, so this .catch never retries; the successful pin path also does not resync, and useImageQueue will not report the same completed job again. The newly saved image can consequently remain missing from the strip until another completion or reload. Preserve the former loadGallery/fetchWhileStable retry behavior when a completion refresh is invalidated.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1074fe9: resyncWindow now reports whether it applied, and refreshLoadedGallery retries up to PAGE_MAX_ATTEMPTS when a strip mutation discards it, waiting for the image-pin queue first so the retry cannot snapshot the shelf before the pin.

let ended = false;
while (!ended) {
const keep = selectedId();
const reach = keep ? count + pageSize : count;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retire missing selections before extending the window again

If the selected image is deleted through Library, selected visually falls back but galleryCache.selectedId remains the deleted ID. With more unloaded records available, this refresh fetches one extra page and stores it in galleryCache.images; resyncWindow then uses that enlarged length as the next count. Consequently every subsequent completion refresh grows the window again (100 → 150 → 200 → …), eventually refetching the entire gallery for every queued job. Retire the missing selection after the bounded search, or avoid including spillover pages in the next base window size.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f393f5c: collectGalleryWindow now looks past the window only for a selection the previous strip held, so a selection deleted elsewhere costs one extra page on the next refresh and none after.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1074fe9767

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

stripEpoch.current += 1;
void loadGallery();
}
if (finished.some((job) => job.status === "done")) refreshLoadedGallery();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Stop superseded completion refreshes between page requests

When many strip pages are loaded and queued jobs finish faster than the window can be refetched, this new completion path starts overlapping full-window reads. Each newer refresh invalidates the previous one, but resyncWindow checks freshness only after collectGalleryWindow finishes fetching every page. A 5,000-record window therefore still issues all 100 GETs even when invalidated during its first request. Since image_gallery.list_images also walks preceding records for each offset, long queues can accumulate substantial redundant server and network work. Pass the freshness check into the collector and stop superseded reads between page requests.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d7e5577: collectGalleryWindow takes a stillFresh check and returns null after the first request that finds it false, and resyncWindow passes its own ticket and the caller's check, so a superseded refresh stops between pages.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d7e55777c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 2862 to +2864
const epoch = stripEpoch.current;
void resyncWindow(
galleryCache.images.length,
() => stripEpoch.current === epoch,
).catch(() => void loadGallery());
}),
[loadGallery, resyncWindow],
);
const fresh = () => stripEpoch.current === epoch && refreshSeq.current === ticket;
if (await resyncWindow(galleryCache.images.length, fresh)) return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Wait for pending shelf mutations before collecting the window

With multiple strip pages loaded, a completion refresh that starts after handleArchive or handleDelete has sent its request snapshots an epoch that already includes the mutation's start. If the server removes the row between page GETs while the mutation response is delayed, every freshness check passes and the collector skips the shifted boundary record. When the response finally arrives, dropFromStrip only removes the affected ID, leaving a noncontiguous cache whose loadMore offsets keep skipping the other image. Wait for pendingShelfMutations to settle before collecting and applying this window.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this. The skip needs the server to remove the row between two of the refresh's page GETs and the remaining GETs, each a full scan of the gallery directory, to finish before that same delete's response reaches the page. If the response arrives first, dropFromStrip bumps stripEpoch and the refresh retries. resyncWindow already has this exposure on main for unpin and archive restore.

Comment on lines +3708 to +3710
statusKnown: generateProbed && imageQueue.polled,
generating: busy === "generating",
queueBusy: queueBusy(imageQueue.jobs),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain the pick until pending queue submissions are observed

If a direct run finishes while handleQueue is still enqueueing jobs or awaiting its follow-up refresh, the last queue snapshot can still be idle even though the server has accepted the next job. Both probe flags remain true, so clearing busy makes this branch clear pickedDuringRun. When the queue response then arrives and the page follows that job, the live preview takes over from the image picked during the preceding run. Keep the pick while a queue submission is unobserved, or require an idle observation newer than that submission.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this. handleQueue awaits the enqueue POSTs and then imageQueue.refresh(), so the direct run would have to finish inside that one POST plus one GET. The only effect is that the stage returns to the live preview while the pick stays selected in the strip, one click away.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: d7e55777c7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: ae7abc1dc9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants