Repository navigation
Studio: opt-in recursive scanning for custom model folders - #6989
danielhanchen merged 17 commits into
Conversation
Registered scan folders only surface models that sit directly inside them, so models organized in nested trees (vendor/family/model) never show up in the Hub. This adds an opt-in per-folder recursive flag: an 'Also scan sub-folders' checkbox when registering a location, a bounded walker (depth cap, visited-entry cap, symlinks never followed, model directories never descended into) feeding the existing scanners, and a safe additive column on the shared scan_folders table. Folders without the flag keep byte-identical behavior.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 66007e86ba
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| try: | ||
| folder = add_scan_folder(body.path) | ||
| folder = add_scan_folder(body.path, body.recursive) |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| if folder.get("recursive"): | ||
| from hub.services.models.local_inventory import iter_recursive_scan_dirs | ||
|
|
||
| seen = {(m.path, m.model_format, m.format_variant) for m in custom_models} |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| if child.is_symlink() or _has_immediate_model_signal(child): | ||
| continue |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| for m in _filtered( | ||
| _scan_models_dir( | ||
| subdir, | ||
| limit = _MAX_MODELS_PER_CUSTOM_FOLDER - len(generic), | ||
| entry_limit = _MAX_CUSTOM_FOLDER_ENTRIES, |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| for m in _scan_models_dir( | ||
| subdir, | ||
| limit = _MAX_MODELS_PER_FOLDER - len(custom_models), | ||
| ): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
…ntainment, resolver parity The legacy AddScanFolderRequest/ScanFolderInfo now carry the recursive field so the /api/models endpoint works, and the legacy loop reads format_variant via getattr since its LocalModelInfo lacks that field. The walker prunes on immediate weights instead of any model signal so a config-only intermediate directory is still descended into. Recursive results are containment-checked with path_is_same_or_child so a nested symlink cannot pull in models from outside the registered folder, and the OpenAI auto-switch resolver mirrors the recursive scan so a model advertised in the catalog also resolves.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a09874606d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| try: | ||
| folder = add_scan_folder(body.path) | ||
| folder = add_scan_folder(body.path, body.recursive) |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| continue | ||
| child = current / name | ||
| try: | ||
| if child.is_symlink() or _has_immediate_model_weight(child): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| method: "POST", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ path }), | ||
| body: JSON.stringify({ path, recursive }), |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| Symlinks are never followed; depth and visited-entry caps bound the walk.""" | ||
| base_depth = len(folder_path.parts) | ||
| visited = 0 | ||
| for dirpath, dirnames, _filenames in os.walk(folder_path, topdown = True, followlinks = False): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| ) | ||
| ): | ||
| key = (m.path, m.model_format, m.format_variant) | ||
| if key in seen or not path_is_same_or_child(Path(m.path), folder_path): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
…caps and containment The recursive flag is now Optional end to end, so a bare legacy add call no longer flips an existing folder back to non-recursive; only an explicit value updates the row. The walker prunes exactly what the parent scan classifies as a model (config plus weights, or a main GGUF file), so weight-only intermediate directories are still descended into, and regular files now count toward the visited-entry cap so flat folders with many files stay bounded. Containment checks also resolve a model directory's immediate weight files, rejecting rows whose weights are symlinked outside the registered folder, and the legacy add_scan_folder now rejects filesystem roots like the hub path does.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4ae26a77e5
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| for subdir in iter_recursive_scan_dirs(fp): | ||
| found += [ | ||
| m | ||
| for m in _scan_models_dir(subdir, limit = 200) | ||
| if scan_result_within_folder(m.path, fp) |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| for m in _scan_models_dir( | ||
| subdir, | ||
| limit = _MAX_MODELS_PER_FOLDER - len(custom_models), | ||
| ): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| for index, entry in enumerate(candidate.iterdir(), start = 1): | ||
| if index > _MODEL_SIGNAL_PROBE_LIMIT: | ||
| break |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
…date all weights The OpenAI auto-switch resolver now caps a recursive folder's contribution at 200 rows like the catalog scan, instead of adding up to 200 per sub-directory. The legacy recursive loop drops rows for directories that only carry a config file and no loadable weights, matching the Hub inventory path. Containment now checks every immediate weight file rather than only the first probe window, so a symlinked weight late in a large model directory can no longer escape the registered folder.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 98dae68ed8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if entry.is_file() and _is_immediate_model_weight_file(entry): | ||
| if not path_is_same_or_child(entry, folder_path): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| for m in _scan_models_dir( | ||
| subdir, | ||
| limit = _MAX_MODELS_PER_FOLDER - len(custom_models), | ||
| ): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| found.append(m) | ||
| recursive_budget -= 1 |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| if path.is_file(): | ||
| return True | ||
| return any(path.glob("*.gguf")) or _has_non_gguf_weights(path) |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| continue | ||
| child = current / name | ||
| try: | ||
| if child.is_symlink() or _is_scan_leaf_dir(child, entry_limit = entry_limit): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
…scan paths Round of consistency fixes so the Hub inventory, legacy /api/models, and OpenAI auto-switch resolver paths behave identically: - containment now rejects any immediate entry that is a symlink resolving outside the registered folder, covering companion GGUFs (mmproj/MTP) and config/tokenizer files, not just main weights - the resolver skips .studio_links/ollama_links rows like the other paths, so an Ollama link shim can no longer become an auto-switch load target - loadable-weights and link filters, and a per-directory entry cap on the flat custom-folder scans, are applied on all three paths - the model-leaf probe and the resolver budget use the shared entry limit so a late GGUF is not missed and duplicates do not exhaust the budget
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5793b0f8e6
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| Symlinks are never followed; depth and visited-entry caps bound the walk.""" | ||
| base_depth = len(folder_path.parts) | ||
| visited = 0 | ||
| for dirpath, dirnames, _filenames in os.walk(folder_path, topdown = True, followlinks = False): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| continue | ||
| child = current / name | ||
| try: | ||
| if child.is_symlink() or _is_scan_leaf_dir(child, entry_limit = entry_limit): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| visited += 1 | ||
| if entry_limit is not None and visited > entry_limit: | ||
| break |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
- iter_recursive_scan_dirs walks with os.scandir and stops reading a directory once the entry cap is reached, instead of os.walk which builds the full file listing per directory before the cap can apply. - _is_scan_leaf_dir probes the same window the parent scan uses to emit a model directory, so a directory the parent would not emit is never pruned from the walk (a wider probe could drop the model from every scan). - routes _scan_models_dir handles directories and standalone .gguf files in a single pass, so a top-level .gguf is counted against the entry limit once rather than twice and is no longer dropped when the folder fills the cap. Adds a test for descending through file-heavy directories.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e4e6493b1a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| _scan_models_dir( | ||
| subdir, | ||
| limit = _MAX_MODELS_PER_CUSTOM_FOLDER - len(generic), | ||
| entry_limit = _MAX_CUSTOM_FOLDER_ENTRIES, |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
|
|
||
| # The main GGUF sorts after 200 entries; with the probe raised to the entry | ||
| # limit the dir is still classified as a model leaf and pruned from descent. | ||
| assert model not in yielded |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| ) | ||
| if recursive: | ||
| seen = {(m.path, m.model_format, m.format_variant) for m in generic} | ||
| for subdir in iter_recursive_scan_dirs(folder_path): |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Main rewrote every file this branch touched; take main's tree and reimplement recursive scanning on top of it in the next commit.
A registered folder can now also list models in its sub-folders (unslothai#6371). One bounded walker (depth 8, 500 folders, 20000 entries; no symlinks, junctions, hidden folders or HF cache repos; never enters a folder already listed as a model) feeds the Hub inventory, the /api/models inventory and the auto-switch resolver, so all three list the same rows. Folders registered without the flag scan exactly as before.
…picker A folder with a config.json but no weights is not a model the scan lists, so the walk now enters it and finds the model below. The legacy scanner lists such folders as rows; nested rows now need weights or a diffusers pipeline, so other apps' configs in a big tree stay out of the picker.
A nested HF cache could add more rows than the folder's remaining 200-row allowance on /api/models/local and the auto-switch index.
|
Review record for #6989, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#440. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1: reviewed mirror
|
Summary
Closes #6371 (option 1). A registered model folder only listed models sitting directly inside it, so a library organised as a nested tree (
vendor/family/model, several levels deep) never showed up. Adding a location now has an Also scan sub-folders checkbox. Folders added without it scan exactly as before; whole-disk indexing (option 2) is out of scope.How it works
scan_foldersgains arecursive INTEGER NOT NULL DEFAULT 0column, added in place on existing databases.add_scan_folder_with_status(path, recursive=None):Nonekeeps the stored flag (so export re-registration never resets it),True/Falsesets it, and a flag change invalidates the auto-switch index like an insert does.nested_scan_roots, lists the sub-folders of a recursive folder to scan like the folder itself. It never enters a folder the scan already lists as a model (immediate weights or a diffusers pipeline), an HF cache repo (models--*), hidden folders,ollama_links/node_modules/Ollamablobs, symlinks or Windows junctions (reparse tag checked, so Python 3.11 is covered and OneDrive folders stay walkable). Caps: depth 8, 500 folders, 20000 entries./api/hub/local), the model picker inventory (/api/models/local) and the OpenAI auto-switch resolver. Nested rows are deduplicated against the folder's own scan (an LM Studiopublisher/repolayout is not listed twice), keep the per-folder 200-model cap, and nested HF caches are listed too.recursiveonAddScanFolderRequest/ScanFolderInfofor both/api/hub/scan-foldersand/api/models/scan-folders. UI: checkbox in the add-location dialog, and an "Includes sub-folders" note on recursive rows.Tests
studio/backend/tests/test_recursive_scan_folders.py: walker pruning, skips, depth cap; nested models found only with the flag and the flat result a subset; LM Studio layout not relisted; nested HF cache found; config-only folders do not hide a model below them and are not listed as picker rows; storage round trip, keep-on-omit, flip, and migration of a table without the column.End to end over HTTP on a tree with models at depth 1, 3 and 6: main lists only the top-level GGUF on
/api/hub/local,/api/models/localand in the resolver; this branch lists all three with the flag, and exactly main's single row without it.Screenshots
Before (merge base
617b0d4be) vs after (73b99fd65), each a fresh Studio install, adding the same library with models at depth 1, 3 and 6.The add-location dialog gains the checkbox, and a recursive location says so:
On Device filtered to the library: before lists only the top-level GGUF, after also lists the two nested models: