Skip to content

fix(lifecycle): seed hf progress from the blobs the Hub lists, not the old snapshot - #19

Draft
harveyff wants to merge 6 commits into
mainfrom
fix/hf-progress-moved-branch
Draft

harveyff wants to merge 6 commits into
mainfrom
fix/hf-progress-moved-branch

Conversation

@harveyff

@harveyff harveyff commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Problem

An image ships an older version of an hf:// model, with refs/main pointing at it. The Hub's main has since moved and the files kept their names. When llm-init downloads the new version, progress opens at 100% with every file row marked done, then climbs past the total ("36 GB of 18 GB") while still downloading. Reproduced on v1.7.21 and v1.7.26.

Root cause

estimateDownloadBudget seeded bytes_completed and the file rows from cachedHFSnapshotFiles. That function walked the snapshot named by the local refs/<revision>, which is the old commit, and matched files to the new tree by path. Every old file therefore counted as done, SeedDownloadBudget clamped the seed to the total, and tqdm then added the full transfer of the new blobs on top, because OnBytes / OnFileBytes have no ceiling.

Fix

  • hfwrap: the tree listing now records each file's BlobID: lfs.oid (sha256) for LFS files, the git oid otherwise. These are the names the HF cache files content under in blobs/. SHA256 is unchanged, so drift checks are not affected.

  • hfwrap:

    • ResolveCommit calls /api/models/<repo>/revision/<rev>, and skips the request when the revision already is a commit.
    • WriteRefCommit writes refs/<rev> atomically and refuses refs that would leave refs/. The ref keeps an existing file's mode, or gets 0644 when new, so a shared cache read under another uid still sees it (os.CreateTemp alone would leave 0600).
    • EstimateRepo reports Commit when it listed at one.
  • lifecycle, keeping the estimate and the download on one revision:

    • The budget resolves the source's revision to a commit and lists the tree at that commit.
    • runHF downloads at that commit. Afterwards it writes refs/<revision>, which hf skips when it is given a commit and which reuseHFDownload reads on the next boot.
    • MODEL_SOURCE is untouched, and the next pass resolves the branch anew. If the revision cannot be resolved, the pass downloads at the revision as given, which is the old behaviour.
  • lifecycle: drop the old-ref snapshot walk. A listed file counts as cached only when one of these is a regular file of the listed size: snapshots/<target commit>/<path> or blobs/<BlobID>. This is at most two stat calls per file, and nothing is hashed.

    • An old version never matches. Its snapshot sits under the old commit, and its blob under a different id.
    • A new version already in the cache is reused. The snapshot check is what makes this work behind mirrors that omit X-Linked-ETag: the Olares mirror sends an S3-style ETag (96a2…-10), and hf names the blob after it, so no Hub id finds it. The blob check covers content shared with an earlier revision that hf has not linked in yet.
    • <id>.incomplete partials are not seeded, because hf resumes them and the first tqdm tick already carries their bytes.
  • lifecycle / hfwrap, an unreachable remote is no longer silent. hf download answers from the snapshot refs/<revision> names when its own lookup fails, and exits 0. On Olares, a real app (Qwen3.8-27B, main moved to 4af51b3) booted at the old 4ca7207 this way while the mirror was timing out, and the log made it look like a confirmed download.

    • The revision is looked up up to three times, 2 s then 4 s apart. Each attempt asks the revision API and, when the source names one file, a HEAD on that file (whose X-Repo-Commit is the request hf itself makes) at the same time. The HEAD also sizes the file, so when the API answers first the attempt waits up to 3 s more for it. The mirror has been seen refusing the revision API with 401 while serving the HEAD.
    • Everything asked before the download (revision lookups and sizing) shares a 90 s budget (Options.MetadataBudget). Against an unreachable remote the worst case drops from about 126 s to about 66 s, and past the budget the download starts without a pinned total.
    • The lookup runs before any early return in the estimate, so a URL or Ollama source that cannot be sized no longer skips it.
    • When all three fail, hf still runs (one more chance), and the cache is still used. The log says remote unreachable: hf revision not resolved; a cached snapshot may be used instead. When hf then returns the commit the cache already held, it adds hf revision not confirmed before the download; hf returned the cached commit, which may be out of date with cached_commit. That wording does not claim the remote was unreachable, since the same commit also comes back when nothing changed upstream.
    • When the lookup succeeds, hf runs at the commit, so it cannot fall back to an older snapshot. A failed transfer is a retriable error, and the next pass looks up again.
    • .llm-init record reuse stays offline by design and is unchanged.
  • progress: with the byte budget pinned, bytes_completed is capped at bytes_total. The first overrun per pass logs download progress ran past the pinned total; capping it. This is a safety net, not the fix.

  • lifecycle: when the size cannot be fully estimated (e.g. the mirror times out the projector's HEAD), every file is still listed from the start, with the unsized ones shown without a size. Before, the page stayed on "Estimating size", then showed the projector alone at 100% for a minute before the main GGUF appeared. The total stays unpinned, and the log names the source that could not be sized.

  • lifecycle: an engine still serving the previous model is restarted when a pass moves model_path, and the model is called ready only once it is back. On an upgrade the engine started on the persisted sentinel while the new revision downloaded, and since llama.cpp -hf picks its revision at start, it kept serving the old model under a ready status. The restart goes through the existing engine_restart handoff. When the request fails or no supervise loop acts on it, the model is marked failed with the engine still serves the previous model; restart it to load the new one instead of ready, and the restart stays pending, so the next pass (/api/retry or a re-registration) asks for it again even though the run dir already names the new model. StateUnverified is left to WaitAlive. llama.cpp is asked which file it loaded (model_path in /props, via the new optional adapter.ServedModelReporter) instead of trusting the run dir: a mismatch triggers the restart, and after every WaitAlive it is checked again before ready, even when the pass restarted nothing, since /props answers 503 while a model loads. Unanswered queries are retried for about 10 s and then let through with a warning (a build without model_path); a mismatch found there is restarted within the same pass, at most once. This holds across an llm-init restart (the run dir already names the new model), does not restart an engine already moved to the new model by hand, and fails an engine that came back on the previous model. HF cache paths are compared from models--<org>--<repo> on. Other engines, and a projector-only change, keep the before/after run dir comparison. The engine keeps starting with -hf <model_name>.

  • progress / lifecycle: with no pinned total, files already on disk count toward the overall bytes as well as their rows. SeedCachedBytes starts both bytes_completed and bytes_total at the cached bytes without pinning, so files that transfer still grow the total. Before, a cached projector of 100 plus 500 downloaded of the main file read 500 overall against rows adding up to 600.

  • proxy (not about progress; this PR's diff turns on the compat job, which fails without it): the OpenWebUI alias /api/chat/completions now reaches vLLM, llama.cpp and SGLang as /v1/chat/completions. It was forwarded as is, none of them serves that path, and llama.cpp answered 404, failing test_openwebui_alias_streams_sse. The compat suite had been skipping on main, its compose stack never coming up. Body rewriting, the enum check and the metrics label still run on the client's path; only the upstream path changes.

Out of scope: MODEL_SOURCE, snapshot pinning, drift / VERIFY_LEVEL, and URL sources.

Behaviour by case

Case Before After
Branch moved, old model on disk (no --revision) starts at 100%, climbs to about 200% starts at 0, never exceeds 100%
--revision A → --revision B correct, since the new SHA's snapshot does not exist yet unchanged
Fresh install correct unchanged
Restart, record present reused without entering download unchanged (this code is not reached)
Unchanged model, no record 100% from the old-ref snapshot 100% from the target snapshot, no transfer
Force re-download seed dropped to 0 unchanged

Verification

  • go vet ./... and go test ./... pass in golang:1.26 on Linux (28 packages).

  • New and updated tests:

    • moved branch with same-name files seeds 0 and all rows wait (GGUF plus mmproj, matching the reported screenshot);
    • only the unchanged blob is seeded, while .incomplete partials and non-ID names are ignored;
    • force still seeds 0 with waiting rows;
    • hasHFBlob / hasHFFile cases, including another commit's snapshot, a branch name, and a path escaping the snapshot;
    • mirror-named blobs plus a target snapshot seed only that file, while a same-named file in the old snapshot does not count;
    • ResolveCommit (branch, pinned commit with no request, bad sha, missing API) and WriteRefCommit (nested refs, escape refused, 0644 when new, existing mode kept);
    • ensure with main moving after the estimate: hf runs at the estimated commit, refs/main names it, and the next pass reuses the record;
    • ResolveCommit falling back to the file HEAD (401 on the revision API, 302 carrying X-Repo-Commit), and refusing a HEAD without the header or a glob;
    • the lookup answers on the third attempt, and an unreachable remote is asked exactly three times;
    • ensure with the remote unreachable and hf returning the cached commit logs both warnings; when hf returns a newer commit it logs neither;
    • BlobID parsing;
    • the sink cap applies when the budget is pinned and not otherwise;
    • every file is listed when one cannot be sized, cached or not;
    • a model change restarts the engine (upgrade, unverified restart, same model, first install);
    • with no supervisor or a failed restart request, the pass stays failed without calling WaitAlive, and the retry asks for the restart again and reaches ready once it happens;
    • with an engine that reports its model: llm-init restarted with the run dir already on the new model still restarts an engine on the old one; an engine already on the new model is not restarted; one that comes back on the old model fails; after a restart by hand the retry reaches ready without another request; /props parsing and path comparison;
    • an engine still loading the previous model when llm-init starts (/props 503 at first, then the old path) is restarted once and reaches ready; one that comes back on the old model fails; /props never answering reaches ready with no restart;
    • with no pinned total, a cached projector (100) plus 500 of the main file reads 600 / 1100, matching the rows;
    • both /v1/chat/completions and the /api/chat/completions alias reach the engine as /v1/chat/completions;
    • a hung HEAD costs an attempt the 3 s grace, not its own timeout, and a remote that never answers ends the estimate at the metadata budget with both files still listed.
  • Real run against the Hub with the built image and huggingface_hub 0.36.2 (unsloth/Qwen3-0.6B-GGUF, Qwen3-0.6B-Q4_K_S.gguf, same size and different bytes between 0a91863 and 50968a4):

    • the cache names LFS blobs by sha256 and partials as <sha256>.incomplete, which confirms the lookup;
    • after a container restart at 157 MB, the pass opened at 0 with the row waiting, hf resumed, and progress ended at exactly 100% with zero samples above it.
  • On Olares (one@olares.fashion, through the system HF mirror):

    • Setup: one pod runs both harveyff/llm-init:v1.7.26-progress-fix2 and the v1.7.26 binary, against the same hf 0.36.2.
    • Each case starts from its own copy of a real cache. The "ISO" cache is 0a91863 downloaded with --revision, with refs/main pointed at it and the .llm-init record removed.
    Case v1.7.26 this PR
    ISO has 0a91863, main = 50968a4, no --revision first sample 100%, peak 200% 0%, peak 88%, ends 100%
    --revision 0a91863 → --revision 50968a4 0%, peak 90% 0%, peak 88%
    Fresh install, killed at 157 MB and restarted – 0% after restart; hf resumed the 178 MB partial; peak 90%, never above 100%
    Restart, model unchanged, record present – record reused in 0.5 s, no download phase
    Model current on disk, no record 100% 100%, no transfer (before the snapshot check: 0%)
    • Pinned-commit download (v1.7.26-progress-fix3), ISO case through the mirror:

      • hf download starting ... revision="" commit=50968a4…;
      • progress 0% → 90%, never above 100%;
      • refs/main moved from 0a91863 to 50968a4, a write hf does not make when given a commit;
      • an immediate restart reused the record in 0.5 s.
    • Upgrade with engine restart (v1.7.26-progress-fix8), Qwen3.8-27B GGUF plus mmproj, old 4ca7207 installed, main = 4af51b3: the total was pinned at exactly 18,587,014,752 bytes with both files listed; after the download the engine was restarted, llama.cpp loaded snapshots/4af51b3 (main and mmproj), and the model went Ready about 25 s later.

      Earlier runs, while the mirror's API was returning 504 and huggingface.co TLS handshakes were timing out, logged commit="" and downloaded at the branch as before.

    • Cross-revision reuse behind a mirror (a review question): not observed on Olares. A file unchanged between the two commits got a different mirror ETag under each, so hf re-downloaded it into a new blob (5f08… → a0dc…) instead of reusing the old one silently, and progress tracked that transfer. A mirror with revision-stable ETags and no X-Linked-ETag would still skip such a file without emitting progress. Covering that would need a HEAD per unmatched file. This PR leaves it out.

    The force re-download could not be observed in this harness. With no engine container, the pass waits in loading, and the retry was not picked up within 10 minutes. The unit test above covers the force seed.

harveyff and others added 6 commits November 17, 2026 18:26
…timated from

Co-authored-by: Cursor <cursoragent@cursor.com>
…a stale cache

Co-authored-by: Cursor <cursoragent@cursor.com>
…cannot be estimated

Co-authored-by: Cursor <cursoragent@cursor.com>
…re calling it ready

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

1 participant