Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d646442
Stream hf:// files from the CDN to avoid Hub rate limits (#664)
tristan-deep Oct 26, 2026
62e40e6
Validate ZEA_MAX_CONCURRENT_REQUESTS and keep the retry log off the e…
tristan-deep Oct 26, 2026
bde3508
Redact signed URLs in fsspec's HTTP debug log
tristan-deep Oct 26, 2026
1f4122d
fixed potential burst of requests on ttl expiry
Louisvh Oct 29, 2026
4bb3aee
Merge branch 'main' into feature/hf-cdn-streaming
tristan-deep Nov 2, 2026
d3b0c59
improve fetching of small chunks
tristan-deep Nov 2, 2026
e465a91
Keep large chunks on the merged path and make read_at thread-safe
tristan-deep Nov 2, 2026
d378820
Merge remote-tracking branch 'origin/main' into feature/hf-cdn-streaming
tristan-deep Nov 4, 2026
cd2bcb6
Drop cached CDN URLs on upload and reuse branch URLs only as long as …
tristan-deep Nov 4, 2026
206600e
Ask the Hub at most once per cooldown when a URL refresh gives nothin…
tristan-deep Nov 4, 2026
9e9954e
Keep using the current URL when its early refresh fails
tristan-deep Nov 4, 2026
a138178
Redact signed URLs in the errors of failed range requests
tristan-deep Nov 4, 2026
0a2ba87
Draw no progress bar for reads below MIN_BYTES
tristan-deep Nov 4, 2026
68c62c7
Check the chunk cache with a stat in pending_bytes
tristan-deep Nov 4, 2026
7b3d0ec
Fetch the blocks of a small read concurrently
tristan-deep Nov 4, 2026
3f43635
Merge ranges only while a request downloads no more gap bytes than ch…
tristan-deep Nov 4, 2026
afcc0ef
Keep one block cache per streamed file instead of two
tristan-deep Nov 4, 2026
dca16d0
Ask the stream whether it keeps the blocks it reads
tristan-deep Nov 4, 2026
dcd792d
Share the on-disk cache helpers of the Hub listing and the CDN URLs
tristan-deep Nov 4, 2026
3fcd16b
Merge branch 'main' into feature/hf-cdn-streaming
tristan-deep Nov 4, 2026
08f275f
Merge branch 'main' into feature/hf-cdn-streaming
swpenninga Nov 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Drop cached CDN URLs on upload and reuse branch URLs only as long as …
…the listing

_hf_clear_caches() now clears zea.data.hf_remote too, so a file uploaded from this
machine is not read through the old signed URL for up to 30 minutes. Opening a file by
branch or tag reuses a cached URL no longer than ZEA_HF_CACHE_TTL, so the bytes read and
the content id from the listing stay in step. Commit-pinned URLs keep the full URL_TTL.
  • Loading branch information
tristan-deep committed Nov 4, 2026
commit cd2bcb608f2f23c783b395b4e381c9e9f4355916
23 changes: 23 additions & 0 deletions tests/data/test_hf_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ def test_expired_url_is_resolved_again(self, hub, monkeypatch):
hf_remote.resolve(HF_PATH)
assert len(hub.heads) == 2

def test_branch_reuse_is_capped_by_the_listing_ttl(self, hub, monkeypatch):
"""A branch can move after an upload, so its URL is reused no longer than the listing.
A commit cannot move, so its URL is reused until it expires."""
from zea.internal import preset_utils

commit = "c0ffee" * 6 + "c0ff"
monkeypatch.setattr(preset_utils, "_HF_CACHE_TTL", 0)
hf_remote.resolve(HF_PATH)
hf_remote.resolve(HF_PATH)
assert len(hub.heads) == 2
hf_remote.resolve(HF_PATH, revision=commit)
hf_remote.resolve(HF_PATH, revision=commit)
assert len(hub.heads) == 3

def test_clearing_the_hub_caches_drops_urls(self, hub):
"""An upload clears the hub caches, so the next open sees the new file."""
from zea.internal.preset_utils import _hf_clear_caches

hf_remote.resolve(HF_PATH)
_hf_clear_caches()
hf_remote.resolve(HF_PATH)
assert len(hub.heads) == 2

def test_refresh_skips_the_cache(self, hub):
hf_remote.resolve(HF_PATH)
hf_remote.resolve(HF_PATH, refresh=True)
Expand Down
26 changes: 22 additions & 4 deletions zea/data/hf_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
``location``, without the ``Authorization`` header (the token is for the Hub, not the CDN).

- The signed URL is treated as opaque. It is reused for :data:`URL_TTL` seconds and
resolved again when it expires or the CDN rejects it.
resolved again when it expires or the CDN rejects it. A branch or tag can point at new
content after an upload, so opening a file by one reuses a cached URL only as long as the
repo listing is cached (``ZEA_HF_CACHE_TTL``).
- Re-resolving uses the commit of the first resolve, so the file cannot change while open.
- Resolved URLs are cached in memory and on disk, so all threads and processes on a machine
(e.g. one process per GPU) share them. The disk cache files are only readable by the user.
Expand All @@ -25,6 +27,7 @@

import hashlib
import json
import re
import threading
import time
from dataclasses import asdict, dataclass
Expand Down Expand Up @@ -75,6 +78,21 @@ def _token_id(token: str | None) -> str:
return hashlib.sha256(token.encode()).hexdigest()[:16] if token else "anonymous"


def _reusable(resolved: Resolved | None, revision: str | None) -> bool:
"""Whether a cached ``resolved`` may be used to open the file at ``revision``.

A commit cannot change, so its URL is reused until it expires. A branch or tag is reused
no longer than the repo listing, so the content id and the bytes read stay in step.
"""
if resolved is None or not resolved.fresh:
return False
if revision is not None and re.fullmatch(r"[0-9a-f]{40}", revision):
return True
from zea.internal.preset_utils import _HF_CACHE_TTL

return time.time() - resolved.resolved_at < _HF_CACHE_TTL


def _origin(url: str) -> tuple[str, str]:
parts = urlsplit(url)
return parts.scheme, parts.netloc
Expand All @@ -89,7 +107,7 @@ def _load(key: tuple) -> Resolved | None:
resolved = Resolved(**json.loads(_disk_path(key).read_text()))
except (OSError, ValueError, TypeError):
return None
return resolved if resolved.fresh else None
return resolved


def _store(key: tuple, resolved: Resolved) -> None:
Expand Down Expand Up @@ -137,9 +155,9 @@ def resolve(
if not refresh:
with _memory_lock:
cached = _memory.get(key)
if cached is None or not cached.fresh:
if not _reusable(cached, revision):
cached = _load(key)
if cached is not None:
if cached is not None and _reusable(cached, revision):
with _memory_lock:
_memory[key] = cached
return cached
Expand Down
5 changes: 4 additions & 1 deletion zea/internal/preset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ def clear(self):


def _hf_clear_caches() -> None:
"""Drop the memoized repository listings and resolved download paths."""
"""Drop the memoized repository listings, resolved download paths and CDN URLs."""
import shutil

from zea.data import hf_remote

hf_remote.clear()
_LISTING_CACHE.clear()
_DOWNLOAD_CACHE.clear()
shutil.rmtree(_LISTING_DIR, ignore_errors=True)
Expand Down