Skip to content

fix: skip fresh cached URL weight checks - #653

Closed
Ghraven wants to merge 1 commit into
Noble-Lab:mainfrom
Ghraven:fix/cache-url-weights-ttl
Closed

Ghraven wants to merge 1 commit into
Noble-Lab:mainfrom
Ghraven:fix/cache-url-weights-ttl

Conversation

@Ghraven

@Ghraven Ghraven commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #616.

_get_weights_from_url() made a HEAD request every time a URL-backed model file already existed in the cache. That meant repeat invocations still depended on network latency and remote availability even when the local cache had just been populated.

Before / after

Before:

  • First run downloaded and cached the URL model weights.
  • A second immediate run reused the cached file, but still performed a remote HEAD request first.

After:

  • Cached URL weights newer than the 24-hour TTL are returned immediately without a remote HEAD request.
  • Stale cached files still use the existing HEAD / Last-Modified freshness check before deciding whether to re-download.
  • force_download=True still bypasses the cache.

Verification

  • python -m pytest tests/unit_tests/test_unit.py -k get_weights_from_url
  • python -m ruff check --extend-ignore E402 casanovo/casanovo.py tests/unit_tests/test_unit.py
  • git diff --check

Summary by CodeRabbit

  • New Features

    • Optimized weight file caching to validate locally cached copies against a time-based validity window, eliminating unnecessary remote verification requests for recently cached weight files and improving load performance.
  • Tests

    • Enhanced unit tests to verify cache validation logic and confirm that remote verification requests are only made when the cached file is no longer within the acceptable time window.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR implements a time-based cache validity window for URL-downloaded model weights. A 24-hour TTL constant is added, and _get_weights_from_url now returns cached files immediately if their modification time is within the TTL, eliminating redundant HEAD requests to the remote URL.

Changes

URL weight cache TTL validation

Layer / File(s) Summary
Cache TTL constant and cache-first logic
casanovo/casanovo.py
Module-level constant _URL_WEIGHTS_CACHE_TTL_SECONDS defines 24-hour cache validity. _get_weights_from_url checks cached file age; if within TTL and force_download is false, it logs cache usage and returns immediately without making a remote HEAD request.
Test instrumentation and cache behavior validation
tests/unit_tests/test_unit.py
MockResponseHead adds request_counter to track HEAD invocations. Tests assert zero HEAD calls for fresh cached files, incrementing counts for stale/missing caches, and correct fallback to cache when HEAD fails or returns non-OK.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: skip fresh cached URL weight checks' clearly and concisely describes the main change: skipping unnecessary network checks for fresh cached weights.
Linked Issues check ✅ Passed The PR fully implements the requirements from issue #616: adds a 24-hour TTL constant, checks cache age before making HEAD requests, returns cached files immediately when fresh, and skips network requests accordingly.
Out of Scope Changes check ✅ Passed All changes are directly scoped to addressing issue #616: TTL constant addition, cache age logic, and corresponding test updates to verify HEAD request counts.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.48%. Comparing base (5dbcb63) to head (ee030d5).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #653      +/-   ##
==========================================
+ Coverage   95.47%   95.48%   +0.01%     
==========================================
  Files          14       14              
  Lines        1656     1660       +4     
==========================================
+ Hits         1581     1585       +4     
  Misses         75       75              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/unit_tests/test_unit.py`:
- Around line 803-808: The code computes stale_time using a naive datetime via
datetime.datetime.now().timestamp(); change this to use a timezone-aware UTC
datetime for consistency (e.g., use
datetime.datetime.now(datetime.timezone.utc).timestamp() or
.astimezone(datetime.timezone.utc).timestamp()) when calculating stale_time for
os.utime on cache_file_path so it matches the timezone-aware usage elsewhere and
keeps behavior consistent with casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3849d724-aee9-4903-a649-6937369c74b7

📥 Commits

Reviewing files that changed from the base of the PR and between 5dbcb63 and ee030d5.

📒 Files selected for processing (2)
  • casanovo/casanovo.py
  • tests/unit_tests/test_unit.py

Comment on lines +803 to +808
stale_time = (
datetime.datetime.now().timestamp()
- casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS
- 1
)
os.utime(cache_file_path, (stale_time, stale_time))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial | 💤 Low value

Consider using timezone-aware datetime for consistency.

Line 804 uses datetime.datetime.now().timestamp() without a timezone argument, while line 803 uses .astimezone(datetime.timezone.utc) for timezone awareness. Although .timestamp() converts to UTC internally and the code works correctly, using timezone-aware datetimes consistently throughout improves code clarity and follows best practices.

♻️ Proposed fix for timezone consistency
-        stale_time = (
-            datetime.datetime.now().timestamp()
-            - casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS
-            - 1
-        )
+        stale_time = (
+            datetime.datetime.now(datetime.timezone.utc).timestamp()
+            - casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS
+            - 1
+        )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
stale_time = (
datetime.datetime.now().timestamp()
- casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS
- 1
)
os.utime(cache_file_path, (stale_time, stale_time))
stale_time = (
datetime.datetime.now(datetime.timezone.utc).timestamp()
- casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS
- 1
)
os.utime(cache_file_path, (stale_time, stale_time))
🧰 Tools
🪛 Ruff (0.15.15)

[warning] 804-804: datetime.datetime.now() called without a tz argument

(DTZ005)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit_tests/test_unit.py` around lines 803 - 808, The code computes
stale_time using a naive datetime via datetime.datetime.now().timestamp();
change this to use a timezone-aware UTC datetime for consistency (e.g., use
datetime.datetime.now(datetime.timezone.utc).timestamp() or
.astimezone(datetime.timezone.utc).timestamp()) when calculating stale_time for
os.utime on cache_file_path so it matches the timezone-aware usage elsewhere and
keeps behavior consistent with casanovo._URL_WEIGHTS_CACHE_TTL_SECONDS.

@bittremieux

Copy link
Copy Markdown
Collaborator

As mentioned before, please follow our contributing guidelines. One important aspect that's mentioned there is that new contributions should be submitted against dev, never against main. I'll be closing all PRs that don't adhere to these guidelines.

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.

_get_weights_from_url makes redundant network request on every invocation even when cache is valid

2 participants