Skip to content

Fix GGUF fallback cache context source - #7788

Merged
danielhanchen merged 2 commits into
unslothai:mainfrom
Imagineer99:fix/gguf-cache-fallback-context-source
Aug 4, 2026
Merged

danielhanchen merged 2 commits into
unslothai:mainfrom
Imagineer99:fix/gguf-cache-fallback-context-source

Conversation

@Imagineer99

Copy link
Copy Markdown
Member

Summary

  • record the cache directory that supplied GGUF variants when the Hub listing fails
  • read native context metadata from that same cache copy
  • add a regression test for an unreachable Hub with a selected non-active cache

Why

This is a small follow-up to #7767. Its normal local-only cache path records answered_from, but the equivalent fallback after a failed Hub request did not. The route therefore fell back to a repo-wide context lookup and could attach metadata from another cached copy.

Validation

The A/B branches contained the same regression test and workflow; their only production-code difference was this two-line fix.

@Imagineer99
Imagineer99 marked this pull request as ready for review August 3, 2026 14:59
danielhanchen added a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Aug 4, 2026
@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: 89bc4b3c4d

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

if cached is not None:
variants, has_vision, complete = cached
if repo_cache_dir is not None and repo_cache_dir.is_dir():
answered_from[0] = str(repo_cache_dir)

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 Record the cache entry that actually won fallback selection

When one cache root contains multiple case-folded directories for the same repo (for example, after the repo's casing changes), list_gguf_variants_from_hf_cache() scans every matching directory and may select the newer snapshot from a differently cased entry, while _repo_cache_dir_for_request() returns the canonical path constructed from the request. If that canonical directory also exists, this assignment marks it as the source even though another directory supplied the variants, so the route reads and returns context metadata from the wrong cached copy. Preserve the actual snapshot or repo directory selected by the fallback lister instead of inferring it from the request.

Useful? React with 👍 / 👎.

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.

Reproduced the mechanism. With models--org--Repo and models--org--repo both present in one root, iter_active_repo_cache_dirs matches case-folded and iter_hf_cache_snapshots pools their snapshots sorted by mtime, so the lister can answer from one entry while _repo_cache_dir_for_request reconstructs the other from the request id.

Not taking it in this PR, for two reasons.

It is pre-existing: the byte-identical guard shipped at 742-743 in #7767, and this PR only extends the same convention to the Hub-failure branch.

And it is not a regression. Without these lines context_source stays None, and routes/models.py falls back to _read_native_context_length(repo_id, is_local = False), which walks every cache root via iter_repo_cache_dirs and hits both case variants in raw iterdir order. Narrowing that to one root and one name is no worse, and in the common case better.

Worth fixing properly by threading the snapshot the lister actually selected out of list_gguf_variants_from_hf_cache and using it at both call sites, which would also close the cross-revision case. That changes a shipped return contract with other callers, so it belongs in a follow-up to #7767 rather than in a two-line PR.

cached = list_gguf_variants_from_hf_cache(repo_id, root = hub_cache)
if cached is not None:
variants, has_vision, complete = cached
if repo_cache_dir is not None and repo_cache_dir.is_dir():

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 Avoid raising while probing the fallback cache directory

When the cache directory becomes unreadable or an underlying filesystem returns an access error after the cached listing succeeds, this bare Path.is_dir() can raise PermissionError on the Python 3.12+ interpreters shipped by the project; the repository's _safe_is_dir() helpers and test_recommended_folders_permission.py explicitly document that behavior. Because this runs while handling an already-failed Hub request, the new exception replaces a usable cached response with a 500. Treat an un-stat-able directory as having no explicit context source, or guard the probe with OSError handling.

Useful? React with 👍 / 👎.

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.

Checked this one closely. The version premise is right in substance, and slightly wider than stated: is_dir() propagates EACCES on 3.9 through 3.13, and only stops raising in 3.14 (gh-101357). Confirmed on 3.12 and 3.13 here.

The line is not reachable as a first raiser, though. To get to 782 at all, list_gguf_variants_from_hf_cache(repo_id, root = hub_cache) has to return non-None, and that path already probes repo_dir / "snapshots" with an unguarded is_dir() at hub/utils/gguf.py:271, one level deeper. stat() needs +x on every ancestor, so any layout where repo_cache_dir.is_dir() could raise makes that deeper probe raise first.

Measured both layouts that could trigger it:

  • hub_cache mode 0444 (readable, not traversable): the lister raises PermissionError first, at gguf.py:271. Line 782 never runs.
  • repo cache dir mode 0000: the lister raises first, and repo_cache_dir.is_dir() returns True anyway, since stat only needs +x on the parent.

So guarding these two lines would not remove a 500 that is otherwise reachable. The genuinely unguarded probe is hub/utils/gguf.py:271, which predates this PR, and it is worth its own fix with _safe_is_dir; tracking that separately rather than here.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 89bc4b3c4d

ℹ️ 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 3867b66 into unslothai:main Aug 4, 2026
32 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants