Skip to content

Studio: keep same-second fine-tuning jobs in submission order across restarts - #16835

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
oobabooga:worktree-ft-list-creation-order
Nov 7, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
oobabooga:worktree-ft-list-creation-order

Conversation

@oobabooga

@oobabooga oobabooga commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #16720, #16832 and #16823.

When several fine-tuning jobs are created within the same second (a script submitting jobs in a loop), GET /v1/fine_tuning/jobs broke the created_at tie by the random public job id. So jobs.list(limit=1) could return an older job instead of the one just submitted, and the order among those jobs had nothing to do with submission order. An earlier revision of this branch fixed that with an in-process counter. That counter was lost when Studio restarted, so a client paging with after= across a restart could skip a job (three same-second jobs, page 1 before a restart, page 2 after: ftjob_qa never returned).

Jobs now get a nanosecond creation stamp when they are queued (kept strictly increasing, since Windows clocks can tick only every ~16 ms). It is saved with the job id mapping in the alias table, in a new nullable created_ns column, and loaded back on start. Same-second jobs are ordered by that stamp, then by public id, so a job keeps its place from queued to running to history, and across restarts. Older alias tables gain the column the first time they are opened. Jobs the API starts directly, without queueing, get a stamp too: it is saved under their usual public id, and their created_at still comes from the run. Rows without a stamp (runs started from the Studio UI, jobs from older builds) keep the public id order. Older builds name their columns explicitly when they read and write this table, so a downgrade still works.

Verification:

  • test_a_job_batched_past_keeps_its_place_when_it_starts_between_pages now also runs across a simulated restart and asserts submission order when jobs share a second. On main, the 3 same-second cells fail and the 3 others pass. Here, all 6 pass.
  • New test_an_alias_table_from_an_older_build_loads_and_gains_the_ns_column: an alias table from an older build loads, gains the column, and saves new stamps.
  • New test_jobs_created_within_one_clock_tick_list_newest_submitted_first: with a frozen clock, jobs still list newest submitted first (fails without the strictly increasing stamp).
  • New test_a_job_started_directly_lists_ahead_of_an_older_same_second_job: a queued job is cancelled and a replacement starts directly in the same second. The replacement lists first, before and after a restart.
  • Three tests computed their expected order from the random job id tie break. They now expect newest submitted first.
  • test_openai_fine_tuning.py: 125 passed. test_training_api_snippet_contract.py + tests/multi_account (fine-tuning/training selection): 129 passed.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-07T12:41:53.523464Z 00a6672 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.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 6e706accac

ℹ️ 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".

danielhanchen added a commit that referenced this pull request Nov 6, 2026
…t navigate (#16858)

## Summary

`Chat UI Tests` on Windows fails now and then in
`tests/studio/playwright_browser_bookmarks.py`, on the step that follows
a link inside the browser panel's page. In the last day it hit main
itself (run 42143788184, after #16856) and #16835 and #16758, none of
which touch the browser panel:

```
FAIL Save, followed=True: Locator expected to have Value 'https://bookmark.test/followed'
Actual value: https://bookmark.test/first
```

The failure screenshot from the main run shows the typed page still
loaded with "Follow link" on it. The click never produced a navigation,
so the address bar stayed on the first page for the whole 5 s wait. The
same Save and Enter cases pass in the same run and on the next run. All
three failures were on the Windows lane. It looks like a click lost on a
loaded Windows runner, not a regression: the page's link handler is
installed before the page's own content, and the panel accepts the first
same-tab navigation a page reports.

## Change

`tests/studio/playwright_browser_bookmarks.py` only. A small
`follow_link` helper clicks "Follow link" and waits 5 s for the address
to change, as before. If it has not, and the address is still the typed
page and the tab still says "Typed page", it clicks again, up to three
clicks in total, and prints a line when it retries.

It does not retry if the address moved anywhere else, so a wrong
navigation still fails at once. A second click cannot double a slow
navigation either: once a page has reported a same-tab navigation the
panel ignores any further one from it (`left` in `page-frame.tsx`).
Every later assertion (bookmark contents, Back to the first page,
Forward to the followed one) is unchanged, so a link that really stops
navigating still fails after the third click.

## Testing

- A fake page and address bar drive `follow_link` through five cases:
navigates on the first, second or third click (passes, 1/2/3 clicks),
never navigates (fails after 3 clicks), lands on another page (fails
after 1 click).
- `tests/studio/test_playwright_suites_run_in_ci.py`: 9 passed.
@Lyxot Lyxot self-assigned this Nov 6, 2026
@Lyxot

Lyxot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Pushed 003b7cc on top of this branch: jobs created in the same second could still be skipped in an after walk. created_at is whole seconds, and ties fell back to the order the heap was built in, which changes when some of those jobs start as a batch (three jobs created in one second list as C, B, A while queued, then B, C, A once A and C start, so after=C never returns B). Jobs are now ordered by creation time, then by a creation counter assigned when a job is queued, then by public id, so a job's position no longer changes from queued to live to finished, and jobs queued in the same second still list newest first. The issue test now also runs with all jobs created in the same second; those cases fail without this change.

@Lyxot

Lyxot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

@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: 003b7ccdb7

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 003b7ccdb7

ℹ️ 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".

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 6, 2026
…azily

Order queue, live and persisted jobs in one heap keyed by creation time,
then queue order and public id, so jobs created in the same second keep
their place when a batch moves from live to history. Queue entries share
the heap, so a short page reads only the history rows it needs.

Regression tests ported from unslothai#16835.

[skip ci]
@Lyxot

Lyxot commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

Summary for head 003b7cc:

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Nov 6, 2026
…hem (unslothai#16832)

Listing fine-tuning jobs page by page through the OpenAI-compatible API
could skip a job. When two compatible jobs started together as a
Multi-LoRA batch while an older job kept waiting, the waiting job later
started with a newer start time and jumped ahead of the batch in the
history. A client that had already paged past the batch never saw it.

Finished runs are now listed by the time the job was queued, falling
back to the start time for runs that never went through the queue or
predate it. The history keeps the order the jobs were submitted in,
however the batches came out.

Fixes unslothai#16720


Follow-up on this branch: queue, live and persisted jobs are ordered in
one pass by creation time, with jobs created in the same second ordered
by when they were queued, then by job ID. Before, those jobs could swap
places when a batch moved from running to history, and a client could
skip one. History is still read lazily: a short page reads only the rows
it needs, even after a long backlog. Regression tests for these cases
come from unslothai#16835.

---------

Co-authored-by: Daniel Han <[email protected]>
@danielhanchen danielhanchen self-assigned this Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR! The goal of this PR is to stop GET /v1/fine_tuning/jobs from skipping a job in an after walk when a Multi-LoRA batch starts ahead of a waiting job (#16720).

This has since landed on main: #16832 (which closed #16720) carries the same creation-time heap ordering and the regression tests from this branch, and #16823 kept same-second ties on the public job id so the order is the same across restarts. The branch now conflicts with main, and resolving toward main leaves an empty diff apart from the in-process _ALIAS_ORDER counter.

That counter reintroduces a skip across a backend restart, because it is held only in memory. Three finished jobs created in the same second (ftjob_qz, ftjob_qa, ftjob_qm, in that order), page 1 with limit=1 before a restart, page 2 with after= after it:

page 1 after restart skipped
main ftjob_qa ftjob_qm, ftjob_qz none
this PR ftjob_qm ftjob_qz ftjob_qa

test_openai_fine_tuning.py passes on main (120 passed). Against main, this branch's stricter tests fail only on the same-second tie order (step=0), and no job is missing. Closing as superseded by #16832 and #16823. Thanks again for tracking this down; the fix itself shipped.

…across restarts

Replaces the in-process creation counter, which was lost on restart and let an after= walk skip a job. Jobs created in the same second are ordered by a nanosecond stamp saved in the alias table (new nullable created_ns column; older tables gain it on first use), then by public id.
@danielhanchen danielhanchen changed the title Studio: list fine-tuning jobs by creation time when a batch starts ahead of a waiting job Studio: keep same-second fine-tuning jobs in submission order across restarts Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Reopened and rescoped, sorry for the noise. The ordering fix itself is on main via #16832 and #16823, but main breaks same-second ties by the random job id, so jobs.list(limit=1) can miss the job just submitted. The in-process counter from the previous head fixed that but was lost on restart. The branch is now merged with main, and the counter is replaced by a nanosecond creation stamp persisted in the alias table, so submission order holds across restarts too. Details in the updated description.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@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: d7ed6761c2

ℹ️ 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".

)
_QUEUE.append(entry)
_ALIASES[entry.alias] = entry
_ALIAS_CREATED_NS[entry.alias] = time.time_ns()

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 Evict creation stamps with discarded terminal jobs

For queued jobs cancelled or failed before starting, _finish() limits retained terminal aliases to _TERMINAL_KEEP (256), but never removes their entries from this new timestamp dictionary. In a reproduction with 10,000 enqueue/cancel cycles, _QUEUE was empty and _ALIASES contained 256 entries, while _ALIAS_CREATED_NS still contained all 10,000. These discarded jobs have no persisted runs requiring their stamps, so repeated cancellation/retry workflows now cause unbounded memory growth in a long-running Studio process. Remove the corresponding stamps when unstarted terminal aliases are evicted.

Useful? React with 👍 / 👎.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 7, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 7, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 7, 2026
@unslothai unslothai deleted a comment from oobabooga Nov 7, 2026
@unslothai unslothai deleted a comment from Lyxot Nov 7, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@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: 042a5eca8d

ℹ️ 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".

# start is final.
def item(created_at: int, public_id: str, seq: int, row: dict[str, Any], is_run: bool):
return (-created_at, public_id, seq), row, is_run
return (-created_at, -_ALIAS_CREATED_NS.get(public_id, 0), public_id, seq), row, is_run

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 Stamp direct-start jobs before applying the new tie-breaker

When a queued job is cancelled and a replacement can start immediately within the same second, create_fine_tuning_job() bypasses _enqueue() for the replacement, so it has no _ALIAS_CREATED_NS entry. This new key puts the older cancelled job ahead of the unstamped replacement, making jobs.list(limit=1) return the older job; the previous public-ID tie-breaker returned the replacement in this scenario. Give direct-start API jobs a persisted creation stamp too so mixed queued/direct jobs retain submission order.

Useful? React with 👍 / 👎.

@danielhanchen danielhanchen Nov 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixed in 00a6672: a directly started job now gets a stamp too, saved in the alias table under its usual public id (created_at still comes from the run), with a test covering the cancel-then-direct-start case before and after a restart.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 00a6672cbf

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 4ee2a51 into unslothai:main Nov 7, 2026
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.

3 participants