Skip to content

Prevent agent startup from waiting on model catalog scans - #10728

Merged
Lyxot merged 6 commits into
unslothai:mainfrom
Imagineer99:fix/start-resident-model-discovery
Sep 14, 2026
Merged

Lyxot merged 6 commits into
unslothai:mainfrom
Imagineer99:fix/start-resident-model-discovery

Conversation

@Imagineer99

@Imagineer99 Imagineer99 commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

When unsloth start attaches to a loaded model, its model-list request waits for
the full local and media catalog. If discovery exceeds the 30-second HTTP deadline,
startup fails with Couldn't list models: timed out before the agent launches.

Add an authenticated resident-model endpoint that reuses public model IDs and
context metadata, and use it for agent attachment, API-key checks, and survivor
checks. The metadata helper peeks at the existing orchestrator so a GGUF-only or
idle server does not initialize an unused backend. Full /v1/models discovery
is unchanged; older servers fall back to it on HTTP 404.

Validation:

  • 747 tests passed, including negative/positive cold-initialization coverage.
  • Keyless-access regression: both credential variants fail before the allowlist fix;
    662 focused tests pass afterward, including against current upstream main.
    Model-loading/status routes and keyless-off remain protected.
  • Real HTTP A/B with a 32-second media inventory delay: both commands fail on
    main, pass with the fix, and fail again when the CLI fix is reverted.
  • Real Studio with cached Qwen3.5 0.8B GGUF: resident listing in 9 ms, both
    start --no-launch commands succeed, and a completion returns ready.

The injected-delay test proves the failure mechanism, not the original reporter's
exact trigger. The real-model smoke covers server attachment/configuration rather
than native Desktop or agent subprocess execution. Avoiding the catalog scan
requires an updated CLI and backend.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-14T08:27:21.824654Z ad26465 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

Copy link
Copy Markdown

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

Reviewed commit: b2a3b1691d

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

Copy link
Copy Markdown
Member

Confirmed the attach path in unsloth_cli/commands/start.py still routes through /v1/models and waits on the full catalog, and the new resident route with the 404 fallback keeps older servers working. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the attach path in unsloth_cli/commands/start.py still routes every listing through /v1/models, so a slow local or media scan blows the 30 second deadline, and the new resident route with the 404 fallback looks like the right shape. Note #10693 is also adding authenticated residency endpoints in studio/backend/routes/inference.py, so could you check the two do not collide before I review?

…io paths

Describing residency peeks at the orchestrator instead of constructing one, so correct the two comments that still explained the thread hop by a cold singleton build; what it keeps off the loop now is the sync resolver-index work.

Pin two things the resident route relies on but nothing checked. A resident GGUF carrying a variant is the common shape and takes the quant branch, which must answer from the index rather than reach the disk. And an allowlisted /api/inference path is matched by its mounted spelling, which the existing route matrix only pins for the /v1 router, so a rename there could silently strand the entry.
Name the listing helper after the route it calls, so the path, the handler and the helper read alike.

_http_json and the listing call carried the same two except blocks verbatim; give them one formatter, which also lets _http_json state that it handles a failed request rather than only an HTTP error.

Report one failure when nothing is serving instead of two. An empty listing and one holding only unloaded entries are the same situation to the user, and which of them arrives depends only on the server's version.

The comments describing this path still described /v1/models, which it no longer calls. Point them at the listing the code actually requests. Two also claimed the listing carries no GGUF variant: it carries the quant while that reference resolves, and what it never carries is the runtime load settings.
Studio only started raising 404 for an unknown /api path when its SPA catch-all was changed to; before that the catch-all returned a plain dict, which FastAPI serialises as a 200 carrying {"error": "API endpoint not found"}. Discovery read that as a successful listing with no models, so against those servers `unsloth start` reported nothing loaded while a model was resident, and the survivor probe reported the model gone. The key check passed either way and hid it.

Accept the resident answer only when it carries a data list, and fall back to /v1/models otherwise. Keying on the listing rather than on one status code also covers a proxy that answers an unknown path some other way.
@Lyxot
Lyxot force-pushed the fix/start-resident-model-discovery branch from b2a3b16 to ea17c69 Compare September 14, 2026 07:17
@Lyxot

Lyxot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Rebased on main and pushed some follow-ups. The endpoint and the CLI switch are unchanged in substance; the additions are one compatibility fix plus tidying.

Fix — discovery against older servers. The fallback keyed on HTTP 404 alone, but Studio only started raising 404 for an unknown /api path once the SPA catch-all was changed to. Before that the catch-all returned a plain dict, which FastAPI serialises as a 200 carrying {"error": "API endpoint not found"}. Discovery read that as a successful listing with no models, so against exactly the servers the fallback exists for, unsloth start reported nothing loaded while a model was resident, and the survivor probe reported the model gone. The key check passed either way and hid it. It now accepts the resident answer only when it carries a data list and falls back otherwise, which keys on the listing rather than on one status code.

Tidying. _http_json and the listing call carried the same two except blocks verbatim, so they share one formatter now. The two "nothing is serving" failures were collapsed into one — an empty listing and one holding only unloaded entries are the same situation to the user, and which one arrives depended only on the server's version. The listing helper is named after the route it calls. Comments that still described /v1/models now describe the listing the code actually requests; two of them also claimed the listing carries no GGUF variant, which was never true — it carries the quant while that reference resolves, and what it never carries is the runtime load settings.

Tests for the quant probe staying off the disk (the common resident-GGUF shape, which the existing no-scan test did not reach), for the allowlisted /api/inference path being matched by its mounted spelling (the route matrix only pinned the /v1 router), for the 200 fallback shape above, and for the no-model message.

One thing worth knowing when reviewing: this does not help the agent's own /v1/models probe, which still pays the catalog scan. That is not a regression — main pays the same cost today — but it does mean the underlying slow-scan exposure is unfixed for anything that is not this CLI, including third-party OpenAI-compatible clients. Bounding the catalog scan with a deadline would fix it for every client and seems worth doing separately.

Two test failures currently on the branch reproduce on a clean main and are unrelated to this PR: unsloth_cli/tests/test_studio_update_uv_cache.py::test_a_cache_path_that_is_not_utf_8_is_recorded_and_read_back, and two cases in studio/backend/tests/test_openai_catalog.py.

@Lyxot

Lyxot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ea17c69415

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

The CLI now asks for /api/inference/loaded-models before falling back to /v1/models, but this file's FakeServer only answered the compat spelling and rejected the new one as an unexpected request. Accept both, so the stub covers the URL the CLI actually sends and still covers the fallback.
@Lyxot

Lyxot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: ad26465d2b

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

@Lyxot

Lyxot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

One more commit: tests/studio/test_cli_attach_load_knobs.py's FakeServer stub only answered /v1/models, so it rejected the new /api/inference/loaded-models request as an unexpected URL. The stub now accepts both spellings, which keeps the old-server fallback covered too. No production code changed.

Caught by running every test that imports the start CLI rather than just the two directories nearest the change:

tests/studio/install/test_studio_extra_matches_requirements.py
tests/studio/test_cli_attach_load_knobs.py
tests/studio/test_cli_start_download_liveness.py
tests/studio/test_cli_studio_stop_windows.py
tests/test_cli_export_unpacking.py
unsloth_cli/tests/test_claude_plan_gate.py
unsloth_cli/tests/test_start.py
unsloth_cli/tests/test_start_model_discovery.py

757 passed; that file was the only stub hardcoding the old URL.

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. The bug is real: /v1/models answers only after the full local catalog scan, so on a machine with a slow scan folder an agent asking what is resident at startup could outlast its deadline and read a loaded model as unloaded. Serving that question from a dedicated route that never constructs an orchestrator is the right shape, and the CLI falls back to /v1/models against older servers, so discovery keeps working across versions.

Validated on a staging CI run of this exact head: the studio, python and backend test jobs pass. The remaining red checks are off this change — the three Core jobs fail in unsloth_zoo, and Repo tests (CPU, rest) names only files this PR does not touch.

@Lyxot
Lyxot merged commit c8ed02f into unslothai:main Sep 14, 2026
49 of 52 checks passed
danielhanchen pushed a commit to NilayYadav/unsloth that referenced this pull request Nov 6, 2026
…thai#16831)

With the "Unsloth model" read-aloud engine, Read aloud in chat can take
seconds longer than Generate on the Audio page for the same text and the
same loaded speech model. A user on Discord reported 10 to 20 s in chat
against about half a second on the Audio page.

## Cause

Before every utterance, `generateStudioTtsAudio` looks up the loaded
speech model with `GET /v1/models` (`resolveReadAloudModel`, added in
unslothai#14191) so it can name it in `/audio/generate`. `/v1/models` lists every
downloaded model, and the scan behind it is cached for only 30 s
(`_CATALOG_TTL_S`). A click more than 30 s after the last listing, which
is the usual case for reading a finished reply, waits for a full rescan
of the downloaded models. The Audio page never lists the catalog, so it
does not pay this cost. Auto read-aloud and the Settings › Voice preview
use the same function and wait the same way.

## Fix

Read aloud now lists models from `GET /api/inference/loaded-models`,
which unslothai#10728 added so agent startup would not wait on the same scan. Its
rows are the same `_openai_model_objects` entries that `/v1/models`
marks `loaded: true`, including `task` and `audio_workflows` for a
speech model kept beside the chat model, so the chosen model id does not
change.

There are no backend changes. The "Custom endpoint" engine and system
voices do not use this lookup and are unchanged. If the reporter was
using "Custom endpoint", this PR does not address their delay.

Not addressed: the same report says auto read-aloud does not speak a
scheduled task's reply when the chat is opened. `AutoReadAloud` only
speaks a reply that finishes while the chat is open, which is how it
treats every chat opened from history. Speaking unread scheduled results
on open would need a separate product decision.

## Verification

Live on `origin/main` (`16060aba73`) with a CPU-only Studio,
`gemma-4-E2B-it` Q4_K_M as the chat model and Kokoro-82M loaded beside
it. I sent the request sequence the adapter sends (the listing, then
`/audio/generate` naming the speech model it found), waiting 35 s before
each one. Listing time:

| Round | `/v1/models` (main) | `/api/inference/loaded-models` (this PR)
|
| --- | --- | --- |
| 1 | 3.25 s | 0.02 s |
| 2 | 13.16 s | 0.01 s |
| 3 | 3.78 s | 0.01 s |

Generation took about 9 s on both (Kokoro on the CPU). Both listings
returned the same loaded rows, and every response came from Kokoro.

- `read-aloud-studio-voice.test.ts` now serves the loaded-models
listing, and a new test checks that read aloud never requests
`/v1/models`. It fails on main.
`tts-custom-connections-disabled.test.ts` mocks the new path.
- 380 tests in the read-aloud, TTS and model-catalog files pass. Full
frontend suite: 14943 pass and 18 fail. 17 of those also fail with
main's versions of these files. The remaining one is a find-in-page
timing test that passes 3 out of 3 times when run alone.
- Typecheck passes. Biome and ESLint report the same findings on these
files as on main.
- I did not click Read aloud in a browser.
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